test_helpers.c 7.6 KB

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