test_helpers.c 7.8 KB

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