test_connection.c 34 KB

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