test_helpers.c 7.7 KB

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