test_helpers.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* Copyright (c) 2014-2019, 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 MAINLOOP_PRIVATE
  11. #include "orconfig.h"
  12. #include "core/or/or.h"
  13. #include "lib/buf/buffers.h"
  14. #include "app/config/config.h"
  15. #include "lib/confmgt/confparse.h"
  16. #include "app/main/subsysmgr.h"
  17. #include "core/mainloop/connection.h"
  18. #include "lib/crypt_ops/crypto_rand.h"
  19. #include "core/mainloop/mainloop.h"
  20. #include "feature/nodelist/nodelist.h"
  21. #include "core/or/relay.h"
  22. #include "feature/nodelist/routerlist.h"
  23. #include "lib/dispatch/dispatch.h"
  24. #include "lib/dispatch/dispatch_naming.h"
  25. #include "lib/pubsub/pubsub_build.h"
  26. #include "lib/pubsub/pubsub_connect.h"
  27. #include "lib/encoding/confline.h"
  28. #include "lib/net/resolve.h"
  29. #include "core/or/cell_st.h"
  30. #include "core/or/connection_st.h"
  31. #include "feature/nodelist/node_st.h"
  32. #include "core/or/origin_circuit_st.h"
  33. #include "feature/nodelist/routerlist_st.h"
  34. #include "test/test.h"
  35. #include "test/test_helpers.h"
  36. #include "test/test_connection.h"
  37. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  38. DISABLE_GCC_WARNING(overlength-strings)
  39. /* We allow huge string constants in the unit tests, but not in the code
  40. * at large. */
  41. #endif
  42. #include "test_descriptors.inc"
  43. #include "core/or/circuitlist.h"
  44. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  45. ENABLE_GCC_WARNING(overlength-strings)
  46. #endif
  47. /* Return a statically allocated string representing yesterday's date
  48. * in ISO format. We use it so that state file items are not found to
  49. * be outdated. */
  50. const char *
  51. get_yesterday_date_str(void)
  52. {
  53. static char buf[ISO_TIME_LEN+1];
  54. time_t yesterday = time(NULL) - 24*60*60;
  55. format_iso_time(buf, yesterday);
  56. return buf;
  57. }
  58. /* NOP replacement for router_descriptor_is_older_than() */
  59. static int
  60. router_descriptor_is_older_than_replacement(const routerinfo_t *router,
  61. int seconds)
  62. {
  63. (void) router;
  64. (void) seconds;
  65. return 0;
  66. }
  67. /** Parse a file containing router descriptors and load them to our
  68. routerlist. This function is used to setup an artificial network
  69. so that we can conduct tests on it. */
  70. void
  71. helper_setup_fake_routerlist(void)
  72. {
  73. int retval;
  74. routerlist_t *our_routerlist = NULL;
  75. const smartlist_t *our_nodelist = NULL;
  76. /* Read the file that contains our test descriptors. */
  77. /* We need to mock this function otherwise the descriptors will not
  78. accepted as they are too old. */
  79. MOCK(router_descriptor_is_older_than,
  80. router_descriptor_is_older_than_replacement);
  81. /* Load all the test descriptors to the routerlist. */
  82. retval = router_load_routers_from_string(TEST_DESCRIPTORS,
  83. NULL, SAVED_IN_JOURNAL,
  84. NULL, 0, NULL);
  85. tt_int_op(retval, OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  86. /* Sanity checking of routerlist and nodelist. */
  87. our_routerlist = router_get_routerlist();
  88. tt_int_op(smartlist_len(our_routerlist->routers), OP_EQ,
  89. HELPER_NUMBER_OF_DESCRIPTORS);
  90. routerlist_assert_ok(our_routerlist);
  91. our_nodelist = nodelist_get_list();
  92. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  93. /* Mark all routers as non-guards but up and running! */
  94. SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
  95. node->is_running = 1;
  96. node->is_valid = 1;
  97. node->is_possible_guard = 0;
  98. } SMARTLIST_FOREACH_END(node);
  99. done:
  100. UNMOCK(router_descriptor_is_older_than);
  101. }
  102. void
  103. connection_write_to_buf_mock(const char *string, size_t len,
  104. connection_t *conn, int compressed)
  105. {
  106. (void) compressed;
  107. tor_assert(string);
  108. tor_assert(conn);
  109. buf_add(conn->outbuf, string, len);
  110. }
  111. char *
  112. buf_get_contents(buf_t *buf, size_t *sz_out)
  113. {
  114. tor_assert(buf);
  115. tor_assert(sz_out);
  116. char *out;
  117. *sz_out = buf_datalen(buf);
  118. if (*sz_out >= ULONG_MAX)
  119. return NULL; /* C'mon, really? */
  120. out = tor_malloc(*sz_out + 1);
  121. if (buf_get_bytes(buf, out, (unsigned long)*sz_out) != 0) {
  122. tor_free(out);
  123. return NULL;
  124. }
  125. out[*sz_out] = '\0'; /* Hopefully gratuitous. */
  126. return out;
  127. }
  128. /* Set up a fake origin circuit with the specified number of cells,
  129. * Return a pointer to the newly-created dummy circuit */
  130. circuit_t *
  131. dummy_origin_circuit_new(int n_cells)
  132. {
  133. origin_circuit_t *circ = origin_circuit_new();
  134. int i;
  135. cell_t cell;
  136. for (i=0; i < n_cells; ++i) {
  137. crypto_rand((void*)&cell, sizeof(cell));
  138. cell_queue_append_packed_copy(TO_CIRCUIT(circ),
  139. &TO_CIRCUIT(circ)->n_chan_cells,
  140. 1, &cell, 1, 0);
  141. }
  142. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_C_GENERAL;
  143. return TO_CIRCUIT(circ);
  144. }
  145. /** Mock-replacement. As tor_addr_lookup, but always fails on any
  146. * address containing a !. This is necessary for running the unit tests
  147. * on networks where DNS hijackers think it's helpful to give answers
  148. * for things like 1.2.3.4.5 or "invalidstuff!!"
  149. */
  150. int
  151. mock_tor_addr_lookup__fail_on_bad_addrs(const char *name,
  152. uint16_t family, tor_addr_t *out)
  153. {
  154. if (name && strchr(name, '!')) {
  155. return -1;
  156. }
  157. return tor_addr_lookup__real(name, family, out);
  158. }
  159. /*********** Helper funcs for making new connections/streams *****************/
  160. /* Helper for test_conn_get_connection() */
  161. static int
  162. fake_close_socket(tor_socket_t sock)
  163. {
  164. (void)sock;
  165. return 0;
  166. }
  167. static int mock_connection_connect_sockaddr_called = 0;
  168. static int fake_socket_number = TEST_CONN_FD_INIT;
  169. /* Helper for test_conn_get_connection() */
  170. static int
  171. mock_connection_connect_sockaddr(connection_t *conn,
  172. const struct sockaddr *sa,
  173. socklen_t sa_len,
  174. const struct sockaddr *bindaddr,
  175. socklen_t bindaddr_len,
  176. int *socket_error)
  177. {
  178. (void)sa_len;
  179. (void)bindaddr;
  180. (void)bindaddr_len;
  181. tor_assert(conn);
  182. tor_assert(sa);
  183. tor_assert(socket_error);
  184. mock_connection_connect_sockaddr_called++;
  185. conn->s = fake_socket_number++;
  186. tt_assert(SOCKET_OK(conn->s));
  187. /* We really should call tor_libevent_initialize() here. Because we don't,
  188. * we are relying on other parts of the code not checking if the_event_base
  189. * (and therefore event->ev_base) is NULL. */
  190. tt_int_op(connection_add_connecting(conn), OP_EQ, 0);
  191. done:
  192. /* Fake "connected" status */
  193. return 1;
  194. }
  195. /** Create and return a new connection/stream */
  196. connection_t *
  197. test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose)
  198. {
  199. connection_t *conn = NULL;
  200. tor_addr_t addr;
  201. int socket_err = 0;
  202. int in_progress = 0;
  203. MOCK(connection_connect_sockaddr,
  204. mock_connection_connect_sockaddr);
  205. MOCK(tor_close_socket, fake_close_socket);
  206. tor_init_connection_lists();
  207. conn = connection_new(type, TEST_CONN_FAMILY);
  208. tt_assert(conn);
  209. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
  210. tt_assert(!tor_addr_is_null(&addr));
  211. tor_addr_copy_tight(&conn->addr, &addr);
  212. conn->port = TEST_CONN_PORT;
  213. mock_connection_connect_sockaddr_called = 0;
  214. in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr,
  215. TEST_CONN_PORT, &socket_err);
  216. tt_int_op(mock_connection_connect_sockaddr_called, OP_EQ, 1);
  217. tt_assert(!socket_err);
  218. tt_assert(in_progress == 0 || in_progress == 1);
  219. /* fake some of the attributes so the connection looks OK */
  220. conn->state = state;
  221. conn->purpose = purpose;
  222. assert_connection_ok(conn, time(NULL));
  223. UNMOCK(connection_connect_sockaddr);
  224. UNMOCK(tor_close_socket);
  225. return conn;
  226. /* On failure */
  227. done:
  228. UNMOCK(connection_connect_sockaddr);
  229. UNMOCK(tor_close_socket);
  230. return NULL;
  231. }
  232. /* Helper function to parse a set of torrc options in a text format and return
  233. * a newly allocated or_options_t object containing the configuration. On
  234. * error, NULL is returned indicating that the conf couldn't be parsed
  235. * properly. */
  236. or_options_t *
  237. helper_parse_options(const char *conf)
  238. {
  239. int ret = 0;
  240. char *msg = NULL;
  241. or_options_t *opt = NULL;
  242. config_line_t *line = NULL;
  243. /* Kind of pointless to call this with a NULL value. */
  244. tt_assert(conf);
  245. opt = options_new();
  246. tt_assert(opt);
  247. ret = config_get_lines(conf, &line, 1);
  248. if (ret != 0) {
  249. goto done;
  250. }
  251. ret = config_assign(get_options_mgr(), opt, line, 0, &msg);
  252. if (ret != 0) {
  253. goto done;
  254. }
  255. done:
  256. config_free_lines(line);
  257. if (ret != 0) {
  258. or_options_free(opt);
  259. opt = NULL;
  260. }
  261. return opt;
  262. }
  263. /**
  264. * Dispatch alertfn callback: flush all messages right now. Implements
  265. * DELIV_IMMEDIATE.
  266. **/
  267. static void
  268. alertfn_immediate(dispatch_t *d, channel_id_t chan, void *arg)
  269. {
  270. (void) arg;
  271. dispatch_flush(d, chan, INT_MAX);
  272. }
  273. /**
  274. * Setup helper for tests that need pubsub active
  275. *
  276. * Does not hook up mainloop events. Does set immediate delivery for
  277. * all channels.
  278. */
  279. void *
  280. helper_setup_pubsub(const struct testcase_t *testcase)
  281. {
  282. dispatch_t *dispatcher = NULL;
  283. pubsub_builder_t *builder = pubsub_builder_new();
  284. channel_id_t chan = get_channel_id("orconn");
  285. (void)testcase;
  286. (void)subsystems_add_pubsub(builder);
  287. dispatcher = pubsub_builder_finalize(builder, NULL);
  288. tor_assert(dispatcher);
  289. dispatch_set_alert_fn(dispatcher, chan, alertfn_immediate, NULL);
  290. chan = get_channel_id("ocirc");
  291. dispatch_set_alert_fn(dispatcher, chan, alertfn_immediate, NULL);
  292. return dispatcher;
  293. }
  294. /**
  295. * Cleanup helper for tests that need pubsub active
  296. */
  297. int
  298. helper_cleanup_pubsub(const struct testcase_t *testcase, void *dispatcher_)
  299. {
  300. dispatch_t *dispatcher = dispatcher_;
  301. (void)testcase;
  302. dispatch_free(dispatcher);
  303. return 1;
  304. }
  305. const struct testcase_setup_t helper_pubsub_setup = {
  306. helper_setup_pubsub, helper_cleanup_pubsub
  307. };