test_circuitbuild.c 3.6 KB

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