test_router.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* Copyright (c) 2017, isis agora lovecruft */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file test_router.c
  6. * \brief Unittests for code in router.c
  7. **/
  8. #include "core/or/or.h"
  9. #include "app/config/config.h"
  10. #include "lib/crypt_ops/crypto_curve25519.h"
  11. #include "lib/crypt_ops/crypto_ed25519.h"
  12. #include "feature/relay/router.h"
  13. #include "feature/nodelist/routerlist.h"
  14. #include "feature/nodelist/routerinfo_st.h"
  15. /* Test suite stuff */
  16. #include "test/test.h"
  17. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  18. static routerinfo_t* mock_routerinfo;
  19. static const routerinfo_t*
  20. NS(router_get_my_routerinfo)(void)
  21. {
  22. crypto_pk_t* ident_key;
  23. crypto_pk_t* tap_key;
  24. time_t now;
  25. if (!mock_routerinfo) {
  26. /* Mock the published timestamp, otherwise router_dump_router_to_string()
  27. * will poop its pants. */
  28. time(&now);
  29. /* We'll need keys, or router_dump_router_to_string() would return NULL. */
  30. ident_key = pk_generate(0);
  31. tap_key = pk_generate(0);
  32. tor_assert(ident_key != NULL);
  33. tor_assert(tap_key != NULL);
  34. mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
  35. mock_routerinfo->nickname = tor_strdup("ConlonNancarrow");
  36. mock_routerinfo->addr = 123456789;
  37. mock_routerinfo->or_port = 443;
  38. mock_routerinfo->platform = tor_strdup("unittest");
  39. mock_routerinfo->cache_info.published_on = now;
  40. mock_routerinfo->identity_pkey = crypto_pk_dup_key(ident_key);
  41. router_set_rsa_onion_pkey(tap_key, &mock_routerinfo->onion_pkey,
  42. &mock_routerinfo->onion_pkey_len);
  43. mock_routerinfo->bandwidthrate = 9001;
  44. mock_routerinfo->bandwidthburst = 9002;
  45. }
  46. return mock_routerinfo;
  47. }
  48. /* If no distribution option was set, then check_bridge_distribution_setting()
  49. * should have set it to "any". */
  50. static void
  51. test_router_dump_router_to_string_no_bridge_distribution_method(void *arg)
  52. {
  53. const char* needle = "bridge-distribution-request any";
  54. or_options_t* options = get_options_mutable();
  55. routerinfo_t* router = NULL;
  56. curve25519_keypair_t ntor_keypair;
  57. ed25519_keypair_t signing_keypair;
  58. char* desc = NULL;
  59. char* found = NULL;
  60. (void)arg;
  61. NS_MOCK(router_get_my_routerinfo);
  62. options->ORPort_set = 1;
  63. options->BridgeRelay = 1;
  64. /* Generate keys which router_dump_router_to_string() expects to exist. */
  65. tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
  66. tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
  67. /* Set up part of our routerinfo_t so that we don't trigger any other
  68. * assertions in router_dump_router_to_string(). */
  69. router = (routerinfo_t*)router_get_my_routerinfo();
  70. tt_ptr_op(router, !=, NULL);
  71. router->onion_curve25519_pkey = &ntor_keypair.pubkey;
  72. /* Generate our server descriptor and ensure that the substring
  73. * "bridge-distribution-request any" occurs somewhere within it. */
  74. crypto_pk_t *onion_pkey = router_get_rsa_onion_pkey(router->onion_pkey,
  75. router->onion_pkey_len);
  76. desc = router_dump_router_to_string(router,
  77. router->identity_pkey,
  78. onion_pkey,
  79. &ntor_keypair,
  80. &signing_keypair);
  81. crypto_pk_free(onion_pkey);
  82. tt_ptr_op(desc, !=, NULL);
  83. found = strstr(desc, needle);
  84. tt_ptr_op(found, !=, NULL);
  85. done:
  86. NS_UNMOCK(router_get_my_routerinfo);
  87. tor_free(desc);
  88. }
  89. #define ROUTER_TEST(name, flags) \
  90. { #name, test_router_ ## name, flags, NULL, NULL }
  91. struct testcase_t router_tests[] = {
  92. ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
  93. END_OF_TESTCASES
  94. };