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