test_router.c 3.4 KB

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