test_helpers.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (c) 2014-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_helpers.c
  5. * \brief Some helper functions to avoid code duplication in unit tests.
  6. */
  7. #define ROUTERLIST_PRIVATE
  8. #define CONNECTION_PRIVATE
  9. #define MAIN_PRIVATE
  10. #include "orconfig.h"
  11. #include "or.h"
  12. #include "connection.h"
  13. #include "main.h"
  14. #include "relay.h"
  15. #include "routerlist.h"
  16. #include "nodelist.h"
  17. #include "buffers.h"
  18. #include "test.h"
  19. #include "test_helpers.h"
  20. #include "test_connection.h"
  21. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  22. DISABLE_GCC_WARNING(overlength-strings)
  23. /* We allow huge string constants in the unit tests, but not in the code
  24. * at large. */
  25. #endif
  26. #include "test_descriptors.inc"
  27. #include "or.h"
  28. #include "circuitlist.h"
  29. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  30. ENABLE_GCC_WARNING(overlength-strings)
  31. #endif
  32. /* Return a statically allocated string representing yesterday's date
  33. * in ISO format. We use it so that state file items are not found to
  34. * be outdated. */
  35. const char *
  36. get_yesterday_date_str(void)
  37. {
  38. static char buf[ISO_TIME_LEN+1];
  39. time_t yesterday = time(NULL) - 24*60*60;
  40. format_iso_time(buf, yesterday);
  41. return buf;
  42. }
  43. /* NOP replacement for router_descriptor_is_older_than() */
  44. static int
  45. router_descriptor_is_older_than_replacement(const routerinfo_t *router,
  46. int seconds)
  47. {
  48. (void) router;
  49. (void) seconds;
  50. return 0;
  51. }
  52. /** Parse a file containing router descriptors and load them to our
  53. routerlist. This function is used to setup an artificial network
  54. so that we can conduct tests on it. */
  55. void
  56. helper_setup_fake_routerlist(void)
  57. {
  58. int retval;
  59. routerlist_t *our_routerlist = NULL;
  60. smartlist_t *our_nodelist = NULL;
  61. /* Read the file that contains our test descriptors. */
  62. /* We need to mock this function otherwise the descriptors will not
  63. accepted as they are too old. */
  64. MOCK(router_descriptor_is_older_than,
  65. router_descriptor_is_older_than_replacement);
  66. /* Load all the test descriptors to the routerlist. */
  67. retval = router_load_routers_from_string(TEST_DESCRIPTORS,
  68. NULL, SAVED_IN_JOURNAL,
  69. NULL, 0, NULL);
  70. tt_int_op(retval, ==, HELPER_NUMBER_OF_DESCRIPTORS);
  71. /* Sanity checking of routerlist and nodelist. */
  72. our_routerlist = router_get_routerlist();
  73. tt_int_op(smartlist_len(our_routerlist->routers), ==,
  74. HELPER_NUMBER_OF_DESCRIPTORS);
  75. routerlist_assert_ok(our_routerlist);
  76. our_nodelist = nodelist_get_list();
  77. tt_int_op(smartlist_len(our_nodelist), ==, HELPER_NUMBER_OF_DESCRIPTORS);
  78. /* Mark all routers as non-guards but up and running! */
  79. SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
  80. node->is_running = 1;
  81. node->is_valid = 1;
  82. node->is_possible_guard = 0;
  83. } SMARTLIST_FOREACH_END(node);
  84. done:
  85. UNMOCK(router_descriptor_is_older_than);
  86. }
  87. void
  88. connection_write_to_buf_mock(const char *string, size_t len,
  89. connection_t *conn, int compressed)
  90. {
  91. (void) compressed;
  92. tor_assert(string);
  93. tor_assert(conn);
  94. write_to_buf(string, len, conn->outbuf);
  95. }
  96. /* Set up a fake origin circuit with the specified number of cells,
  97. * Return a pointer to the newly-created dummy circuit */
  98. circuit_t *
  99. dummy_origin_circuit_new(int n_cells)
  100. {
  101. origin_circuit_t *circ = origin_circuit_new();
  102. int i;
  103. cell_t cell;
  104. for (i=0; i < n_cells; ++i) {
  105. crypto_rand((void*)&cell, sizeof(cell));
  106. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  107. &TO_CIRCUIT(circ)->n_chan_cells,
  108. 1, &cell, 1, 0);
  109. }
  110. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  111. return TO_CIRCUIT(circ);
  112. }
  113. /** Mock-replacement. As tor_addr_lookup, but always fails on any
  114. * address containing a !. This is necessary for running the unit tests
  115. * on networks where DNS hijackers think it's helpful to give answers
  116. * for things like 1.2.3.4.5 or "invalidstuff!!"
  117. */
  118. int
  119. mock_tor_addr_lookup__fail_on_bad_addrs(const char *name,
  120. uint16_t family, tor_addr_t *out)
  121. {
  122. if (name && strchr(name, '!')) {
  123. return -1;
  124. }
  125. return tor_addr_lookup__real(name, family, out);
  126. }
  127. /*********** Helper funcs for making new connections/streams *****************/
  128. /* Helper for test_conn_get_connection() */
  129. static int
  130. fake_close_socket(evutil_socket_t sock)
  131. {
  132. (void)sock;
  133. return 0;
  134. }
  135. static int mock_connection_connect_sockaddr_called = 0;
  136. static int fake_socket_number = TEST_CONN_FD_INIT;
  137. /* Helper for test_conn_get_connection() */
  138. static int
  139. mock_connection_connect_sockaddr(connection_t *conn,
  140. const struct sockaddr *sa,
  141. socklen_t sa_len,
  142. const struct sockaddr *bindaddr,
  143. socklen_t bindaddr_len,
  144. int *socket_error)
  145. {
  146. (void)sa_len;
  147. (void)bindaddr;
  148. (void)bindaddr_len;
  149. tor_assert(conn);
  150. tor_assert(sa);
  151. tor_assert(socket_error);
  152. mock_connection_connect_sockaddr_called++;
  153. conn->s = fake_socket_number++;
  154. tt_assert(SOCKET_OK(conn->s));
  155. /* We really should call tor_libevent_initialize() here. Because we don't,
  156. * we are relying on other parts of the code not checking if the_event_base
  157. * (and therefore event->ev_base) is NULL. */
  158. tt_assert(connection_add_connecting(conn) == 0);
  159. done:
  160. /* Fake "connected" status */
  161. return 1;
  162. }
  163. /** Create and return a new connection/stream */
  164. connection_t *
  165. test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose)
  166. {
  167. connection_t *conn = NULL;
  168. tor_addr_t addr;
  169. int socket_err = 0;
  170. int in_progress = 0;
  171. MOCK(connection_connect_sockaddr,
  172. mock_connection_connect_sockaddr);
  173. MOCK(tor_close_socket, fake_close_socket);
  174. init_connection_lists();
  175. conn = connection_new(type, TEST_CONN_FAMILY);
  176. tt_assert(conn);
  177. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
  178. tt_assert(!tor_addr_is_null(&addr));
  179. tor_addr_copy_tight(&conn->addr, &addr);
  180. conn->port = TEST_CONN_PORT;
  181. mock_connection_connect_sockaddr_called = 0;
  182. in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr,
  183. TEST_CONN_PORT, &socket_err);
  184. tt_assert(mock_connection_connect_sockaddr_called == 1);
  185. tt_assert(!socket_err);
  186. tt_assert(in_progress == 0 || in_progress == 1);
  187. /* fake some of the attributes so the connection looks OK */
  188. conn->state = state;
  189. conn->purpose = purpose;
  190. assert_connection_ok(conn, time(NULL));
  191. UNMOCK(connection_connect_sockaddr);
  192. UNMOCK(tor_close_socket);
  193. return conn;
  194. /* On failure */
  195. done:
  196. UNMOCK(connection_connect_sockaddr);
  197. UNMOCK(tor_close_socket);
  198. return NULL;
  199. }