test_helpers.c 8.0 KB

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