test_connection.c 31 KB

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