test_helpers.c 7.7 KB

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