test_connection.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /* Copyright (c) 2015-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define CONNECTION_PRIVATE
  5. #define MAIN_PRIVATE
  6. #define CONNECTION_OR_PRIVATE
  7. #include "core/or/or.h"
  8. #include "test/test.h"
  9. #include "core/mainloop/connection.h"
  10. #include "core/or/connection_edge.h"
  11. #include "feature/hs/hs_common.h"
  12. #include "core/mainloop/main.h"
  13. #include "feature/nodelist/microdesc.h"
  14. #include "feature/nodelist/nodelist.h"
  15. #include "feature/nodelist/networkstatus.h"
  16. #include "feature/rend/rendcache.h"
  17. #include "feature/dircache/directory.h"
  18. #include "core/or/connection_or.h"
  19. #include "lib/net/resolve.h"
  20. #include "test/test_connection.h"
  21. #include "test/test_helpers.h"
  22. #include "feature/dircommon/dir_connection_st.h"
  23. #include "core/or/entry_connection_st.h"
  24. #include "feature/nodelist/node_st.h"
  25. #include "core/or/or_connection_st.h"
  26. #include "feature/nodelist/routerinfo_st.h"
  27. #include "core/or/socks_request_st.h"
  28. static void * test_conn_get_basic_setup(const struct testcase_t *tc);
  29. static int test_conn_get_basic_teardown(const struct testcase_t *tc,
  30. void *arg);
  31. static void * test_conn_get_rend_setup(const struct testcase_t *tc);
  32. static int test_conn_get_rend_teardown(const struct testcase_t *tc,
  33. void *arg);
  34. static void * test_conn_get_rsrc_setup(const struct testcase_t *tc);
  35. static int test_conn_get_rsrc_teardown(const struct testcase_t *tc,
  36. void *arg);
  37. /* Arbitrary choice - IPv4 Directory Connection to localhost */
  38. #define TEST_CONN_TYPE (CONN_TYPE_DIR)
  39. /* We assume every machine has IPv4 localhost, is that ok? */
  40. #define TEST_CONN_ADDRESS "127.0.0.1"
  41. #define TEST_CONN_PORT (12345)
  42. #define TEST_CONN_ADDRESS_PORT "127.0.0.1:12345"
  43. #define TEST_CONN_FAMILY (AF_INET)
  44. #define TEST_CONN_STATE (DIR_CONN_STATE_MIN_)
  45. #define TEST_CONN_ADDRESS_2 "127.0.0.2"
  46. #define TEST_CONN_BASIC_PURPOSE (DIR_PURPOSE_MIN_)
  47. #define TEST_CONN_REND_ADDR "cfs3rltphxxvabci"
  48. #define TEST_CONN_REND_PURPOSE (DIR_PURPOSE_FETCH_RENDDESC_V2)
  49. #define TEST_CONN_REND_PURPOSE_SUCCESSFUL (DIR_PURPOSE_HAS_FETCHED_RENDDESC_V2)
  50. #define TEST_CONN_REND_TYPE_2 (CONN_TYPE_AP)
  51. #define TEST_CONN_REND_ADDR_2 "icbavxxhptlr3sfc"
  52. #define TEST_CONN_RSRC (networkstatus_get_flavor_name(FLAV_MICRODESC))
  53. #define TEST_CONN_RSRC_PURPOSE (DIR_PURPOSE_FETCH_CONSENSUS)
  54. #define TEST_CONN_RSRC_STATE_SUCCESSFUL (DIR_CONN_STATE_CLIENT_FINISHED)
  55. #define TEST_CONN_RSRC_2 (networkstatus_get_flavor_name(FLAV_NS))
  56. #define TEST_CONN_DL_STATE (DIR_CONN_STATE_CLIENT_READING)
  57. /* see AP_CONN_STATE_IS_UNATTACHED() */
  58. #define TEST_CONN_UNATTACHED_STATE (AP_CONN_STATE_CIRCUIT_WAIT)
  59. #define TEST_CONN_ATTACHED_STATE (AP_CONN_STATE_CONNECT_WAIT)
  60. void
  61. test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr)
  62. {
  63. int rv = 0;
  64. tt_assert(addr);
  65. rv = tor_addr_lookup(address, family, addr);
  66. /* XXXX - should we retry on transient failure? */
  67. tt_int_op(rv, OP_EQ, 0);
  68. tt_assert(tor_addr_is_loopback(addr));
  69. tt_assert(tor_addr_is_v4(addr));
  70. return;
  71. done:
  72. tor_addr_make_null(addr, TEST_CONN_FAMILY);
  73. }
  74. static void *
  75. test_conn_get_basic_setup(const struct testcase_t *tc)
  76. {
  77. (void)tc;
  78. return test_conn_get_connection(TEST_CONN_STATE, TEST_CONN_TYPE,
  79. TEST_CONN_BASIC_PURPOSE);
  80. }
  81. static int
  82. test_conn_get_basic_teardown(const struct testcase_t *tc, void *arg)
  83. {
  84. (void)tc;
  85. connection_t *conn = arg;
  86. tt_assert(conn);
  87. assert_connection_ok(conn, time(NULL));
  88. /* teardown the connection as fast as possible */
  89. if (conn->linked_conn) {
  90. assert_connection_ok(conn->linked_conn, time(NULL));
  91. /* We didn't call tor_libevent_initialize(), so event_base was NULL,
  92. * so we can't rely on connection_unregister_events() use of event_del().
  93. */
  94. if (conn->linked_conn->read_event) {
  95. tor_free(conn->linked_conn->read_event);
  96. conn->linked_conn->read_event = NULL;
  97. }
  98. if (conn->linked_conn->write_event) {
  99. tor_free(conn->linked_conn->write_event);
  100. conn->linked_conn->write_event = NULL;
  101. }
  102. if (!conn->linked_conn->marked_for_close) {
  103. connection_close_immediate(conn->linked_conn);
  104. if (CONN_IS_EDGE(conn->linked_conn)) {
  105. /* Suppress warnings about all the stuff we didn't do */
  106. TO_EDGE_CONN(conn->linked_conn)->edge_has_sent_end = 1;
  107. TO_EDGE_CONN(conn->linked_conn)->end_reason =
  108. END_STREAM_REASON_INTERNAL;
  109. if (conn->linked_conn->type == CONN_TYPE_AP) {
  110. TO_ENTRY_CONN(conn->linked_conn)->socks_request->has_finished = 1;
  111. }
  112. }
  113. connection_mark_for_close(conn->linked_conn);
  114. }
  115. close_closeable_connections();
  116. }
  117. /* We didn't set the events up properly, so we can't use event_del() in
  118. * close_closeable_connections() > connection_free()
  119. * > connection_unregister_events() */
  120. if (conn->read_event) {
  121. tor_free(conn->read_event);
  122. conn->read_event = NULL;
  123. }
  124. if (conn->write_event) {
  125. tor_free(conn->write_event);
  126. conn->write_event = NULL;
  127. }
  128. if (!conn->marked_for_close) {
  129. connection_close_immediate(conn);
  130. if (CONN_IS_EDGE(conn)) {
  131. /* Suppress warnings about all the stuff we didn't do */
  132. TO_EDGE_CONN(conn)->edge_has_sent_end = 1;
  133. TO_EDGE_CONN(conn)->end_reason = END_STREAM_REASON_INTERNAL;
  134. if (conn->type == CONN_TYPE_AP) {
  135. TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
  136. }
  137. }
  138. connection_mark_for_close(conn);
  139. }
  140. close_closeable_connections();
  141. /* The unit test will fail if we return 0 */
  142. return 1;
  143. /* When conn == NULL, we can't cleanup anything */
  144. done:
  145. return 0;
  146. }
  147. static void *
  148. test_conn_get_rend_setup(const struct testcase_t *tc)
  149. {
  150. dir_connection_t *conn = DOWNCAST(dir_connection_t,
  151. test_conn_get_connection(
  152. TEST_CONN_STATE,
  153. TEST_CONN_TYPE,
  154. TEST_CONN_REND_PURPOSE));
  155. tt_assert(conn);
  156. assert_connection_ok(&conn->base_, time(NULL));
  157. rend_cache_init();
  158. /* TODO: use directory_initiate_request() to do this - maybe? */
  159. tor_assert(strlen(TEST_CONN_REND_ADDR) == REND_SERVICE_ID_LEN_BASE32);
  160. conn->rend_data = rend_data_client_create(TEST_CONN_REND_ADDR, NULL, NULL,
  161. REND_NO_AUTH);
  162. assert_connection_ok(&conn->base_, time(NULL));
  163. return conn;
  164. /* On failure */
  165. done:
  166. test_conn_get_rend_teardown(tc, conn);
  167. /* Returning NULL causes the unit test to fail */
  168. return NULL;
  169. }
  170. static int
  171. test_conn_get_rend_teardown(const struct testcase_t *tc, void *arg)
  172. {
  173. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  174. int rv = 0;
  175. tt_assert(conn);
  176. assert_connection_ok(&conn->base_, time(NULL));
  177. /* avoid a last-ditch attempt to refetch the descriptor */
  178. conn->base_.purpose = TEST_CONN_REND_PURPOSE_SUCCESSFUL;
  179. /* connection_free_() cleans up rend_data */
  180. rv = test_conn_get_basic_teardown(tc, arg);
  181. done:
  182. rend_cache_free_all();
  183. return rv;
  184. }
  185. static dir_connection_t *
  186. test_conn_download_status_add_a_connection(const char *resource)
  187. {
  188. dir_connection_t *conn = DOWNCAST(dir_connection_t,
  189. test_conn_get_connection(
  190. TEST_CONN_STATE,
  191. TEST_CONN_TYPE,
  192. TEST_CONN_RSRC_PURPOSE));
  193. tt_assert(conn);
  194. assert_connection_ok(&conn->base_, time(NULL));
  195. /* Replace the existing resource with the one we want */
  196. if (resource) {
  197. if (conn->requested_resource) {
  198. tor_free(conn->requested_resource);
  199. }
  200. conn->requested_resource = tor_strdup(resource);
  201. assert_connection_ok(&conn->base_, time(NULL));
  202. }
  203. return conn;
  204. done:
  205. test_conn_get_rsrc_teardown(NULL, conn);
  206. return NULL;
  207. }
  208. static void *
  209. test_conn_get_rsrc_setup(const struct testcase_t *tc)
  210. {
  211. (void)tc;
  212. return test_conn_download_status_add_a_connection(TEST_CONN_RSRC);
  213. }
  214. static int
  215. test_conn_get_rsrc_teardown(const struct testcase_t *tc, void *arg)
  216. {
  217. int rv = 0;
  218. connection_t *conn = (connection_t *)arg;
  219. tt_assert(conn);
  220. assert_connection_ok(conn, time(NULL));
  221. if (conn->type == CONN_TYPE_DIR) {
  222. dir_connection_t *dir_conn = DOWNCAST(dir_connection_t, arg);
  223. tt_assert(dir_conn);
  224. assert_connection_ok(&dir_conn->base_, time(NULL));
  225. /* avoid a last-ditch attempt to refetch the consensus */
  226. dir_conn->base_.state = TEST_CONN_RSRC_STATE_SUCCESSFUL;
  227. assert_connection_ok(&dir_conn->base_, time(NULL));
  228. }
  229. /* connection_free_() cleans up requested_resource */
  230. rv = test_conn_get_basic_teardown(tc, conn);
  231. done:
  232. return rv;
  233. }
  234. static void *
  235. test_conn_download_status_setup(const struct testcase_t *tc)
  236. {
  237. return (void*)tc;
  238. }
  239. static int
  240. test_conn_download_status_teardown(const struct testcase_t *tc, void *arg)
  241. {
  242. (void)arg;
  243. int rv = 0;
  244. /* Ignore arg, and just loop through the connection array */
  245. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  246. if (conn) {
  247. assert_connection_ok(conn, time(NULL));
  248. /* connection_free_() cleans up requested_resource */
  249. rv = test_conn_get_rsrc_teardown(tc, conn);
  250. tt_int_op(rv, OP_EQ, 1);
  251. }
  252. } SMARTLIST_FOREACH_END(conn);
  253. done:
  254. return rv;
  255. }
  256. /* Like connection_ap_make_link(), but does much less */
  257. static connection_t *
  258. test_conn_get_linked_connection(connection_t *l_conn, uint8_t state)
  259. {
  260. tt_assert(l_conn);
  261. assert_connection_ok(l_conn, time(NULL));
  262. /* AP connections don't seem to have purposes */
  263. connection_t *conn = test_conn_get_connection(state, CONN_TYPE_AP,
  264. 0);
  265. tt_assert(conn);
  266. assert_connection_ok(conn, time(NULL));
  267. conn->linked = 1;
  268. l_conn->linked = 1;
  269. conn->linked_conn = l_conn;
  270. l_conn->linked_conn = conn;
  271. /* we never opened a real socket, so we can just overwrite it */
  272. conn->s = TOR_INVALID_SOCKET;
  273. l_conn->s = TOR_INVALID_SOCKET;
  274. assert_connection_ok(conn, time(NULL));
  275. assert_connection_ok(l_conn, time(NULL));
  276. return conn;
  277. done:
  278. test_conn_download_status_teardown(NULL, NULL);
  279. return NULL;
  280. }
  281. static struct testcase_setup_t test_conn_get_basic_st = {
  282. test_conn_get_basic_setup, test_conn_get_basic_teardown
  283. };
  284. static struct testcase_setup_t test_conn_get_rend_st = {
  285. test_conn_get_rend_setup, test_conn_get_rend_teardown
  286. };
  287. static struct testcase_setup_t test_conn_get_rsrc_st = {
  288. test_conn_get_rsrc_setup, test_conn_get_rsrc_teardown
  289. };
  290. static struct testcase_setup_t test_conn_download_status_st = {
  291. test_conn_download_status_setup, test_conn_download_status_teardown
  292. };
  293. static void
  294. test_conn_get_basic(void *arg)
  295. {
  296. connection_t *conn = (connection_t*)arg;
  297. tor_addr_t addr, addr2;
  298. tt_assert(conn);
  299. assert_connection_ok(conn, time(NULL));
  300. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
  301. tt_assert(!tor_addr_is_null(&addr));
  302. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS_2, TEST_CONN_FAMILY, &addr2);
  303. tt_assert(!tor_addr_is_null(&addr2));
  304. /* Check that we get this connection back when we search for it by
  305. * its attributes, but get NULL when we supply a different value. */
  306. tt_assert(connection_get_by_global_id(conn->global_identifier) == conn);
  307. tt_ptr_op(connection_get_by_global_id(!conn->global_identifier), OP_EQ,
  308. NULL);
  309. tt_assert(connection_get_by_type(conn->type) == conn);
  310. tt_assert(connection_get_by_type(TEST_CONN_TYPE) == conn);
  311. tt_ptr_op(connection_get_by_type(!conn->type), OP_EQ, NULL);
  312. tt_ptr_op(connection_get_by_type(!TEST_CONN_TYPE), OP_EQ, NULL);
  313. tt_assert(connection_get_by_type_state(conn->type, conn->state)
  314. == conn);
  315. tt_assert(connection_get_by_type_state(TEST_CONN_TYPE, TEST_CONN_STATE)
  316. == conn);
  317. tt_assert(connection_get_by_type_state(!conn->type, !conn->state)
  318. == NULL);
  319. tt_assert(connection_get_by_type_state(!TEST_CONN_TYPE, !TEST_CONN_STATE)
  320. == NULL);
  321. /* Match on the connection fields themselves */
  322. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  323. &conn->addr,
  324. conn->port,
  325. conn->purpose)
  326. == conn);
  327. /* Match on the original inputs to the connection */
  328. tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
  329. &conn->addr,
  330. conn->port,
  331. conn->purpose)
  332. == conn);
  333. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  334. &addr,
  335. conn->port,
  336. conn->purpose)
  337. == conn);
  338. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  339. &conn->addr,
  340. TEST_CONN_PORT,
  341. conn->purpose)
  342. == conn);
  343. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  344. &conn->addr,
  345. conn->port,
  346. TEST_CONN_BASIC_PURPOSE)
  347. == conn);
  348. tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
  349. &addr,
  350. TEST_CONN_PORT,
  351. TEST_CONN_BASIC_PURPOSE)
  352. == conn);
  353. /* Then try each of the not-matching combinations */
  354. tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
  355. &conn->addr,
  356. conn->port,
  357. conn->purpose)
  358. == NULL);
  359. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  360. &addr2,
  361. conn->port,
  362. conn->purpose)
  363. == NULL);
  364. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  365. &conn->addr,
  366. !conn->port,
  367. conn->purpose)
  368. == NULL);
  369. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  370. &conn->addr,
  371. conn->port,
  372. !conn->purpose)
  373. == NULL);
  374. /* Then try everything not-matching */
  375. tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
  376. &addr2,
  377. !conn->port,
  378. !conn->purpose)
  379. == NULL);
  380. tt_assert(connection_get_by_type_addr_port_purpose(!TEST_CONN_TYPE,
  381. &addr2,
  382. !TEST_CONN_PORT,
  383. !TEST_CONN_BASIC_PURPOSE)
  384. == NULL);
  385. done:
  386. ;
  387. }
  388. static void
  389. test_conn_get_rend(void *arg)
  390. {
  391. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  392. tt_assert(conn);
  393. assert_connection_ok(&conn->base_, time(NULL));
  394. tt_assert(connection_get_by_type_state_rendquery(
  395. conn->base_.type,
  396. conn->base_.state,
  397. rend_data_get_address(
  398. conn->rend_data))
  399. == TO_CONN(conn));
  400. tt_assert(connection_get_by_type_state_rendquery(
  401. TEST_CONN_TYPE,
  402. TEST_CONN_STATE,
  403. TEST_CONN_REND_ADDR)
  404. == TO_CONN(conn));
  405. tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
  406. !conn->base_.state,
  407. "")
  408. == NULL);
  409. tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
  410. !TEST_CONN_STATE,
  411. TEST_CONN_REND_ADDR_2)
  412. == NULL);
  413. done:
  414. ;
  415. }
  416. #define sl_is_conn_assert(sl_input, conn) \
  417. do { \
  418. the_sl = (sl_input); \
  419. tt_int_op(smartlist_len((the_sl)), OP_EQ, 1); \
  420. tt_assert(smartlist_get((the_sl), 0) == (conn)); \
  421. smartlist_free(the_sl); the_sl = NULL; \
  422. } while (0)
  423. #define sl_no_conn_assert(sl_input) \
  424. do { \
  425. the_sl = (sl_input); \
  426. tt_int_op(smartlist_len((the_sl)), OP_EQ, 0); \
  427. smartlist_free(the_sl); the_sl = NULL; \
  428. } while (0)
  429. static void
  430. test_conn_get_rsrc(void *arg)
  431. {
  432. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  433. smartlist_t *the_sl = NULL;
  434. tt_assert(conn);
  435. assert_connection_ok(&conn->base_, time(NULL));
  436. sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
  437. conn->base_.purpose,
  438. conn->requested_resource),
  439. conn);
  440. sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
  441. TEST_CONN_RSRC_PURPOSE,
  442. TEST_CONN_RSRC),
  443. conn);
  444. sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
  445. !conn->base_.purpose,
  446. ""));
  447. sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
  448. !TEST_CONN_RSRC_PURPOSE,
  449. TEST_CONN_RSRC_2));
  450. sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  451. conn->base_.purpose,
  452. conn->requested_resource,
  453. conn->base_.state),
  454. conn);
  455. sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  456. TEST_CONN_RSRC_PURPOSE,
  457. TEST_CONN_RSRC,
  458. TEST_CONN_STATE),
  459. conn);
  460. sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  461. !conn->base_.purpose,
  462. "",
  463. !conn->base_.state));
  464. sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  465. !TEST_CONN_RSRC_PURPOSE,
  466. TEST_CONN_RSRC_2,
  467. !TEST_CONN_STATE));
  468. tt_int_op(connection_dir_count_by_purpose_and_resource(
  469. conn->base_.purpose, conn->requested_resource),
  470. OP_EQ, 1);
  471. tt_int_op(connection_dir_count_by_purpose_and_resource(
  472. TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC),
  473. OP_EQ, 1);
  474. tt_int_op(connection_dir_count_by_purpose_and_resource(
  475. !conn->base_.purpose, ""),
  476. OP_EQ, 0);
  477. tt_int_op(connection_dir_count_by_purpose_and_resource(
  478. !TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC_2),
  479. OP_EQ, 0);
  480. tt_int_op(connection_dir_count_by_purpose_resource_and_state(
  481. conn->base_.purpose, conn->requested_resource,
  482. conn->base_.state),
  483. OP_EQ, 1);
  484. tt_int_op(connection_dir_count_by_purpose_resource_and_state(
  485. TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC, TEST_CONN_STATE),
  486. OP_EQ, 1);
  487. tt_int_op(connection_dir_count_by_purpose_resource_and_state(
  488. !conn->base_.purpose, "", !conn->base_.state),
  489. OP_EQ, 0);
  490. tt_int_op(connection_dir_count_by_purpose_resource_and_state(
  491. !TEST_CONN_RSRC_PURPOSE, TEST_CONN_RSRC_2, !TEST_CONN_STATE),
  492. OP_EQ, 0);
  493. done:
  494. smartlist_free(the_sl);
  495. }
  496. static void
  497. test_conn_download_status(void *arg)
  498. {
  499. dir_connection_t *conn = NULL;
  500. dir_connection_t *conn2 = NULL;
  501. dir_connection_t *conn4 = NULL;
  502. connection_t *ap_conn = NULL;
  503. const struct testcase_t *tc = arg;
  504. consensus_flavor_t usable_flavor = (consensus_flavor_t)tc->setup_data;
  505. /* The "other flavor" trick only works if there are two flavors */
  506. tor_assert(N_CONSENSUS_FLAVORS == 2);
  507. consensus_flavor_t other_flavor = ((usable_flavor == FLAV_NS)
  508. ? FLAV_MICRODESC
  509. : FLAV_NS);
  510. const char *res = networkstatus_get_flavor_name(usable_flavor);
  511. const char *other_res = networkstatus_get_flavor_name(other_flavor);
  512. /* no connections */
  513. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  514. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  515. 0);
  516. tt_int_op(connection_dir_count_by_purpose_and_resource(
  517. TEST_CONN_RSRC_PURPOSE, res),
  518. OP_EQ, 0);
  519. tt_int_op(connection_dir_count_by_purpose_and_resource(
  520. TEST_CONN_RSRC_PURPOSE, other_res),
  521. OP_EQ, 0);
  522. /* one connection, not downloading */
  523. conn = test_conn_download_status_add_a_connection(res);
  524. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  525. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  526. 0);
  527. tt_int_op(connection_dir_count_by_purpose_and_resource(
  528. TEST_CONN_RSRC_PURPOSE, res),
  529. OP_EQ, 1);
  530. tt_int_op(connection_dir_count_by_purpose_and_resource(
  531. TEST_CONN_RSRC_PURPOSE, other_res),
  532. OP_EQ, 0);
  533. /* one connection, downloading but not linked (not possible on a client,
  534. * but possible on a relay) */
  535. conn->base_.state = TEST_CONN_DL_STATE;
  536. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  537. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  538. 0);
  539. tt_int_op(connection_dir_count_by_purpose_and_resource(
  540. TEST_CONN_RSRC_PURPOSE, res),
  541. OP_EQ, 1);
  542. tt_int_op(connection_dir_count_by_purpose_and_resource(
  543. TEST_CONN_RSRC_PURPOSE, other_res),
  544. OP_EQ, 0);
  545. /* one connection, downloading and linked, but not yet attached */
  546. ap_conn = test_conn_get_linked_connection(TO_CONN(conn),
  547. TEST_CONN_UNATTACHED_STATE);
  548. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  549. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  550. 0);
  551. tt_int_op(connection_dir_count_by_purpose_and_resource(
  552. TEST_CONN_RSRC_PURPOSE, res),
  553. OP_EQ, 1);
  554. tt_int_op(connection_dir_count_by_purpose_and_resource(
  555. TEST_CONN_RSRC_PURPOSE, other_res),
  556. OP_EQ, 0);
  557. /* one connection, downloading and linked and attached */
  558. ap_conn->state = TEST_CONN_ATTACHED_STATE;
  559. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  560. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  561. 0);
  562. tt_int_op(connection_dir_count_by_purpose_and_resource(
  563. TEST_CONN_RSRC_PURPOSE, res),
  564. OP_EQ, 1);
  565. tt_int_op(connection_dir_count_by_purpose_and_resource(
  566. TEST_CONN_RSRC_PURPOSE, other_res),
  567. OP_EQ, 0);
  568. /* one connection, linked and attached but not downloading */
  569. conn->base_.state = TEST_CONN_STATE;
  570. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  571. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  572. 0);
  573. tt_int_op(connection_dir_count_by_purpose_and_resource(
  574. TEST_CONN_RSRC_PURPOSE, res),
  575. OP_EQ, 1);
  576. tt_int_op(connection_dir_count_by_purpose_and_resource(
  577. TEST_CONN_RSRC_PURPOSE, other_res),
  578. OP_EQ, 0);
  579. /* two connections, both not downloading */
  580. conn2 = test_conn_download_status_add_a_connection(res);
  581. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  582. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  583. 0);
  584. tt_int_op(connection_dir_count_by_purpose_and_resource(
  585. TEST_CONN_RSRC_PURPOSE, res),
  586. OP_EQ, 2);
  587. tt_int_op(connection_dir_count_by_purpose_and_resource(
  588. TEST_CONN_RSRC_PURPOSE, other_res),
  589. OP_EQ, 0);
  590. /* two connections, one downloading */
  591. conn->base_.state = TEST_CONN_DL_STATE;
  592. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  593. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  594. 0);
  595. tt_int_op(connection_dir_count_by_purpose_and_resource(
  596. TEST_CONN_RSRC_PURPOSE, res),
  597. OP_EQ, 2);
  598. tt_int_op(connection_dir_count_by_purpose_and_resource(
  599. TEST_CONN_RSRC_PURPOSE, other_res),
  600. OP_EQ, 0);
  601. conn->base_.state = TEST_CONN_STATE;
  602. /* more connections, all not downloading */
  603. /* ignore the return value, it's free'd using the connection list */
  604. (void)test_conn_download_status_add_a_connection(res);
  605. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 0);
  606. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  607. 0);
  608. tt_int_op(connection_dir_count_by_purpose_and_resource(
  609. TEST_CONN_RSRC_PURPOSE, res),
  610. OP_EQ, 3);
  611. tt_int_op(connection_dir_count_by_purpose_and_resource(
  612. TEST_CONN_RSRC_PURPOSE, other_res),
  613. OP_EQ, 0);
  614. /* more connections, one downloading */
  615. conn->base_.state = TEST_CONN_DL_STATE;
  616. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  617. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  618. 0);
  619. tt_int_op(connection_dir_count_by_purpose_and_resource(
  620. TEST_CONN_RSRC_PURPOSE, res),
  621. OP_EQ, 3);
  622. tt_int_op(connection_dir_count_by_purpose_and_resource(
  623. TEST_CONN_RSRC_PURPOSE, other_res),
  624. OP_EQ, 0);
  625. /* more connections, two downloading (should never happen, but needs
  626. * to be tested for completeness) */
  627. conn2->base_.state = TEST_CONN_DL_STATE;
  628. /* ignore the return value, it's free'd using the connection list */
  629. (void)test_conn_get_linked_connection(TO_CONN(conn2),
  630. TEST_CONN_ATTACHED_STATE);
  631. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  632. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  633. 0);
  634. tt_int_op(connection_dir_count_by_purpose_and_resource(
  635. TEST_CONN_RSRC_PURPOSE, res),
  636. OP_EQ, 3);
  637. tt_int_op(connection_dir_count_by_purpose_and_resource(
  638. TEST_CONN_RSRC_PURPOSE, other_res),
  639. OP_EQ, 0);
  640. conn->base_.state = TEST_CONN_STATE;
  641. /* more connections, a different one downloading */
  642. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  643. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  644. 0);
  645. tt_int_op(connection_dir_count_by_purpose_and_resource(
  646. TEST_CONN_RSRC_PURPOSE, res),
  647. OP_EQ, 3);
  648. tt_int_op(connection_dir_count_by_purpose_and_resource(
  649. TEST_CONN_RSRC_PURPOSE, other_res),
  650. OP_EQ, 0);
  651. /* a connection for the other flavor (could happen if a client is set to
  652. * cache directory documents), one preferred flavor downloading
  653. */
  654. conn4 = test_conn_download_status_add_a_connection(other_res);
  655. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  656. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  657. 0);
  658. tt_int_op(connection_dir_count_by_purpose_and_resource(
  659. TEST_CONN_RSRC_PURPOSE, res),
  660. OP_EQ, 3);
  661. tt_int_op(connection_dir_count_by_purpose_and_resource(
  662. TEST_CONN_RSRC_PURPOSE, other_res),
  663. OP_EQ, 1);
  664. /* a connection for the other flavor (could happen if a client is set to
  665. * cache directory documents), both flavors downloading
  666. */
  667. conn4->base_.state = TEST_CONN_DL_STATE;
  668. /* ignore the return value, it's free'd using the connection list */
  669. (void)test_conn_get_linked_connection(TO_CONN(conn4),
  670. TEST_CONN_ATTACHED_STATE);
  671. tt_int_op(networkstatus_consensus_is_already_downloading(res), OP_EQ, 1);
  672. tt_int_op(networkstatus_consensus_is_already_downloading(other_res), OP_EQ,
  673. 1);
  674. tt_int_op(connection_dir_count_by_purpose_and_resource(
  675. TEST_CONN_RSRC_PURPOSE, res),
  676. OP_EQ, 3);
  677. tt_int_op(connection_dir_count_by_purpose_and_resource(
  678. TEST_CONN_RSRC_PURPOSE, other_res),
  679. OP_EQ, 1);
  680. done:
  681. /* the teardown function removes all the connections in the global list*/;
  682. }
  683. static node_t test_node;
  684. static node_t *
  685. mock_node_get_mutable_by_id(const char *digest)
  686. {
  687. (void) digest;
  688. static routerinfo_t node_ri;
  689. memset(&node_ri, 0, sizeof(node_ri));
  690. test_node.ri = &node_ri;
  691. memset(test_node.identity, 'c', sizeof(test_node.identity));
  692. tor_addr_t ipv4_addr;
  693. tor_addr_parse(&ipv4_addr, "18.0.0.1");
  694. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  695. node_ri.or_port = 1;
  696. return &test_node;
  697. }
  698. static const node_t *
  699. mock_node_get_by_id(const char *digest)
  700. {
  701. (void) digest;
  702. memset(test_node.identity, 'c', sizeof(test_node.identity));
  703. return &test_node;
  704. }
  705. /* Test whether we correctly track failed connections between relays. */
  706. static void
  707. test_failed_orconn_tracker(void *arg)
  708. {
  709. (void) arg;
  710. int can_connect;
  711. time_t now = 1281533250; /* 2010-08-11 13:27:30 UTC */
  712. (void) now;
  713. update_approx_time(now);
  714. /* Prepare the OR connection that will be used in this test */
  715. or_connection_t or_conn;
  716. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.real_addr, "18.0.0.1"));
  717. tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&or_conn.base_.addr, "18.0.0.1"));
  718. or_conn.base_.port = 1;
  719. memset(or_conn.identity_digest, 'c', sizeof(or_conn.identity_digest));
  720. /* Check whether we can connect with an empty failure cache:
  721. * this should succeed */
  722. can_connect = should_connect_to_relay(&or_conn);
  723. tt_int_op(can_connect, OP_EQ, 1);
  724. /* Now add the destination to the failure cache */
  725. note_or_connect_failed(&or_conn);
  726. /* Check again: now it shouldn't connect */
  727. can_connect = should_connect_to_relay(&or_conn);
  728. tt_int_op(can_connect, OP_EQ, 0);
  729. /* Move time forward and check again: the cache should have been cleared and
  730. * now it should connect */
  731. now += 3600;
  732. update_approx_time(now);
  733. can_connect = should_connect_to_relay(&or_conn);
  734. tt_int_op(can_connect, OP_EQ, 1);
  735. /* Now mock the node_get_*by_id() functions to start using the node subsystem
  736. * optimization. */
  737. MOCK(node_get_by_id, mock_node_get_by_id);
  738. MOCK(node_get_mutable_by_id, mock_node_get_mutable_by_id);
  739. /* Since we just started using the node subsystem it will allow connections
  740. * now */
  741. can_connect = should_connect_to_relay(&or_conn);
  742. tt_int_op(can_connect, OP_EQ, 1);
  743. /* Mark it as failed */
  744. note_or_connect_failed(&or_conn);
  745. /* Check that it shouldn't connect now */
  746. can_connect = should_connect_to_relay(&or_conn);
  747. tt_int_op(can_connect, OP_EQ, 0);
  748. /* Move time forward and check again: now it should connect */
  749. now += 3600;
  750. update_approx_time(now);
  751. can_connect = should_connect_to_relay(&or_conn);
  752. tt_int_op(can_connect, OP_EQ, 1);
  753. done:
  754. ;
  755. }
  756. #define CONNECTION_TESTCASE(name, fork, setup) \
  757. { #name, test_conn_##name, fork, &setup, NULL }
  758. /* where arg is an expression (constant, variable, compound expression) */
  759. #define CONNECTION_TESTCASE_ARG(name, fork, setup, arg) \
  760. { #name "_" #arg, test_conn_##name, fork, &setup, (void *)arg }
  761. struct testcase_t connection_tests[] = {
  762. CONNECTION_TESTCASE(get_basic, TT_FORK, test_conn_get_basic_st),
  763. CONNECTION_TESTCASE(get_rend, TT_FORK, test_conn_get_rend_st),
  764. CONNECTION_TESTCASE(get_rsrc, TT_FORK, test_conn_get_rsrc_st),
  765. CONNECTION_TESTCASE_ARG(download_status, TT_FORK,
  766. test_conn_download_status_st, FLAV_MICRODESC),
  767. CONNECTION_TESTCASE_ARG(download_status, TT_FORK,
  768. test_conn_download_status_st, FLAV_NS),
  769. //CONNECTION_TESTCASE(func_suffix, TT_FORK, setup_func_pair),
  770. { "failed_orconn_tracker", test_failed_orconn_tracker, TT_FORK, NULL, NULL },
  771. END_OF_TESTCASES
  772. };