test_router.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. mock_routerinfo->onion_pkey = crypto_pk_dup_key(tap_key);
  42. mock_routerinfo->bandwidthrate = 9001;
  43. mock_routerinfo->bandwidthburst = 9002;
  44. }
  45. return mock_routerinfo;
  46. }
  47. /* If no distribution option was set, then check_bridge_distribution_setting()
  48. * should have set it to "any". */
  49. static void
  50. test_router_dump_router_to_string_no_bridge_distribution_method(void *arg)
  51. {
  52. const char* needle = "bridge-distribution-request any";
  53. or_options_t* options = get_options_mutable();
  54. routerinfo_t* router = NULL;
  55. curve25519_keypair_t ntor_keypair;
  56. ed25519_keypair_t signing_keypair;
  57. char* desc = NULL;
  58. char* found = NULL;
  59. (void)arg;
  60. NS_MOCK(router_get_my_routerinfo);
  61. options->ORPort_set = 1;
  62. options->BridgeRelay = 1;
  63. /* Generate keys which router_dump_router_to_string() expects to exist. */
  64. tt_int_op(0, ==, curve25519_keypair_generate(&ntor_keypair, 0));
  65. tt_int_op(0, ==, ed25519_keypair_generate(&signing_keypair, 0));
  66. /* Set up part of our routerinfo_t so that we don't trigger any other
  67. * assertions in router_dump_router_to_string(). */
  68. router = (routerinfo_t*)router_get_my_routerinfo();
  69. tt_ptr_op(router, !=, NULL);
  70. router->onion_curve25519_pkey = &ntor_keypair.pubkey;
  71. /* Generate our server descriptor and ensure that the substring
  72. * "bridge-distribution-request any" occurs somewhere within it. */
  73. desc = router_dump_router_to_string(router,
  74. router->identity_pkey,
  75. router->onion_pkey,
  76. &ntor_keypair,
  77. &signing_keypair);
  78. tt_ptr_op(desc, !=, NULL);
  79. found = strstr(desc, needle);
  80. tt_ptr_op(found, !=, NULL);
  81. done:
  82. NS_UNMOCK(router_get_my_routerinfo);
  83. tor_free(desc);
  84. }
  85. #define ROUTER_TEST(name, flags) \
  86. { #name, test_router_ ## name, flags, NULL, NULL }
  87. struct testcase_t router_tests[] = {
  88. ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
  89. END_OF_TESTCASES
  90. };