test_router.c 3.5 KB

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