test_circuitbuild.c 3.9 KB

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