test_circuitbuild.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define CIRCUITBUILD_PRIVATE
  6. #include "or.h"
  7. #include "test.h"
  8. #include "test_helpers.h"
  9. #include "log_test_helpers.h"
  10. #include "config.h"
  11. #include "circuitbuild.h"
  12. /* Dummy nodes smartlist for testing */
  13. static smartlist_t dummy_nodes;
  14. /* Dummy exit extend_info for testing */
  15. static extend_info_t dummy_ei;
  16. static int
  17. mock_count_acceptable_nodes(smartlist_t *nodes)
  18. {
  19. (void)nodes;
  20. return DEFAULT_ROUTE_LEN + 1;
  21. }
  22. /* Test route lengths when the caller of new_route_len() doesn't
  23. * specify exit_ei. */
  24. static void
  25. test_new_route_len_noexit(void *arg)
  26. {
  27. int r;
  28. (void)arg;
  29. MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
  30. r = new_route_len(CIRCUIT_PURPOSE_C_GENERAL, NULL, &dummy_nodes);
  31. tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
  32. r = new_route_len(CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT, NULL, &dummy_nodes);
  33. tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
  34. r = new_route_len(CIRCUIT_PURPOSE_S_CONNECT_REND, NULL, &dummy_nodes);
  35. tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
  36. done:
  37. UNMOCK(count_acceptable_nodes);
  38. }
  39. /* Test route lengths where someone else chose the "exit" node, which
  40. * require an extra hop for safety. */
  41. static void
  42. test_new_route_len_unsafe_exit(void *arg)
  43. {
  44. int r;
  45. (void)arg;
  46. MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
  47. /* connecting to hidden service directory */
  48. r = new_route_len(CIRCUIT_PURPOSE_C_GENERAL, &dummy_ei, &dummy_nodes);
  49. tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
  50. /* client connecting to introduction point */
  51. r = new_route_len(CIRCUIT_PURPOSE_C_INTRODUCING, &dummy_ei, &dummy_nodes);
  52. tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
  53. /* hidden service connecting to rendezvous point */
  54. r = new_route_len(CIRCUIT_PURPOSE_S_CONNECT_REND, &dummy_ei, &dummy_nodes);
  55. tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
  56. done:
  57. UNMOCK(count_acceptable_nodes);
  58. }
  59. /* Test route lengths where we chose the "exit" node, which don't
  60. * require an extra hop for safety. */
  61. static void
  62. test_new_route_len_safe_exit(void *arg)
  63. {
  64. int r;
  65. (void)arg;
  66. MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
  67. /* hidden service connecting to introduction point */
  68. r = new_route_len(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, &dummy_ei,
  69. &dummy_nodes);
  70. tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
  71. /* router testing its own reachability */
  72. r = new_route_len(CIRCUIT_PURPOSE_TESTING, &dummy_ei, &dummy_nodes);
  73. tt_int_op(DEFAULT_ROUTE_LEN, OP_EQ, r);
  74. done:
  75. UNMOCK(count_acceptable_nodes);
  76. }
  77. /* Make sure a non-fatal assertion fails when new_route_len() gets an
  78. * unexpected circuit purpose. */
  79. static void
  80. test_new_route_len_unhandled_exit(void *arg)
  81. {
  82. int r;
  83. (void)arg;
  84. MOCK(count_acceptable_nodes, mock_count_acceptable_nodes);
  85. tor_capture_bugs_(1);
  86. setup_full_capture_of_logs(LOG_WARN);
  87. r = new_route_len(CIRCUIT_PURPOSE_CONTROLLER, &dummy_ei, &dummy_nodes);
  88. tt_int_op(DEFAULT_ROUTE_LEN + 1, OP_EQ, r);
  89. tt_int_op(smartlist_len(tor_get_captured_bug_log_()), OP_EQ, 1);
  90. tt_str_op(smartlist_get(tor_get_captured_bug_log_(), 0), OP_EQ,
  91. "!(exit_ei && !known_purpose)");
  92. expect_single_log_msg_containing("Unhandled purpose");
  93. expect_single_log_msg_containing("with a chosen exit; assuming routelen");
  94. teardown_capture_of_logs();
  95. tor_end_capture_bugs_();
  96. done:
  97. UNMOCK(count_acceptable_nodes);
  98. }
  99. struct testcase_t circuitbuild_tests[] = {
  100. { "noexit", test_new_route_len_noexit, 0, NULL, NULL },
  101. { "safe_exit", test_new_route_len_safe_exit, 0, NULL, NULL },
  102. { "unsafe_exit", test_new_route_len_unsafe_exit, 0, NULL, NULL },
  103. { "unhandled_exit", test_new_route_len_unhandled_exit, 0, NULL, NULL },
  104. END_OF_TESTCASES
  105. };