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 "main.h"
  10. #include "microdesc.h"
  11. #include "networkstatus.h"
  12. #include "rendcache.h"
  13. #include "directory.h"
  14. static void test_conn_lookup_addr_helper(const char *address,
  15. int family,
  16. tor_addr_t *addr);
  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. #define TEST_CONN_FD_INIT 50
  50. static int mock_connection_connect_sockaddr_called = 0;
  51. static int fake_socket_number = TEST_CONN_FD_INIT;
  52. static int
  53. mock_connection_connect_sockaddr(connection_t *conn,
  54. const struct sockaddr *sa,
  55. socklen_t sa_len,
  56. const struct sockaddr *bindaddr,
  57. socklen_t bindaddr_len,
  58. int *socket_error)
  59. {
  60. (void)sa_len;
  61. (void)bindaddr;
  62. (void)bindaddr_len;
  63. tor_assert(conn);
  64. tor_assert(sa);
  65. tor_assert(socket_error);
  66. mock_connection_connect_sockaddr_called++;
  67. conn->s = fake_socket_number++;
  68. tt_assert(SOCKET_OK(conn->s));
  69. /* We really should call tor_libevent_initialize() here. Because we don't,
  70. * we are relying on other parts of the code not checking if the_event_base
  71. * (and therefore event->ev_base) is NULL. */
  72. tt_assert(connection_add_connecting(conn) == 0);
  73. done:
  74. /* Fake "connected" status */
  75. return 1;
  76. }
  77. static void
  78. test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr)
  79. {
  80. int rv = 0;
  81. tt_assert(addr);
  82. rv = tor_addr_lookup(address, family, addr);
  83. /* XXXX - should we retry on transient failure? */
  84. tt_assert(rv == 0);
  85. tt_assert(tor_addr_is_loopback(addr));
  86. tt_assert(tor_addr_is_v4(addr));
  87. return;
  88. done:
  89. tor_addr_make_null(addr, TEST_CONN_FAMILY);
  90. }
  91. static connection_t *
  92. test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose)
  93. {
  94. connection_t *conn = NULL;
  95. tor_addr_t addr;
  96. int socket_err = 0;
  97. int in_progress = 0;
  98. MOCK(connection_connect_sockaddr,
  99. mock_connection_connect_sockaddr);
  100. init_connection_lists();
  101. conn = connection_new(type, TEST_CONN_FAMILY);
  102. tt_assert(conn);
  103. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
  104. tt_assert(!tor_addr_is_null(&addr));
  105. tor_addr_copy_tight(&conn->addr, &addr);
  106. conn->port = TEST_CONN_PORT;
  107. mock_connection_connect_sockaddr_called = 0;
  108. in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr,
  109. TEST_CONN_PORT, &socket_err);
  110. tt_assert(mock_connection_connect_sockaddr_called == 1);
  111. tt_assert(!socket_err);
  112. tt_assert(in_progress == 0 || in_progress == 1);
  113. /* fake some of the attributes so the connection looks OK */
  114. conn->state = state;
  115. conn->purpose = purpose;
  116. assert_connection_ok(conn, time(NULL));
  117. UNMOCK(connection_connect_sockaddr);
  118. return conn;
  119. /* On failure */
  120. done:
  121. UNMOCK(connection_connect_sockaddr);
  122. return NULL;
  123. }
  124. static void *
  125. test_conn_get_basic_setup(const struct testcase_t *tc)
  126. {
  127. (void)tc;
  128. return test_conn_get_connection(TEST_CONN_STATE, TEST_CONN_TYPE,
  129. TEST_CONN_BASIC_PURPOSE);
  130. }
  131. static int
  132. test_conn_get_basic_teardown(const struct testcase_t *tc, void *arg)
  133. {
  134. (void)tc;
  135. connection_t *conn = arg;
  136. tt_assert(conn);
  137. assert_connection_ok(conn, time(NULL));
  138. /* teardown the connection as fast as possible */
  139. if (conn->linked_conn) {
  140. assert_connection_ok(conn->linked_conn, time(NULL));
  141. /* We didn't call tor_libevent_initialize(), so event_base was NULL,
  142. * so we can't rely on connection_unregister_events() use of event_del().
  143. */
  144. if (conn->linked_conn->read_event) {
  145. tor_free(conn->linked_conn->read_event);
  146. conn->linked_conn->read_event = NULL;
  147. }
  148. if (conn->linked_conn->write_event) {
  149. tor_free(conn->linked_conn->write_event);
  150. conn->linked_conn->write_event = NULL;
  151. }
  152. if (!conn->linked_conn->marked_for_close) {
  153. connection_close_immediate(conn->linked_conn);
  154. connection_mark_for_close(conn->linked_conn);
  155. }
  156. close_closeable_connections();
  157. }
  158. /* We didn't set the events up properly, so we can't use event_del() in
  159. * close_closeable_connections() > connection_free()
  160. * > connection_unregister_events() */
  161. if (conn->read_event) {
  162. tor_free(conn->read_event);
  163. conn->read_event = NULL;
  164. }
  165. if (conn->write_event) {
  166. tor_free(conn->write_event);
  167. conn->write_event = NULL;
  168. }
  169. if (!conn->marked_for_close) {
  170. connection_close_immediate(conn);
  171. connection_mark_for_close(conn);
  172. }
  173. close_closeable_connections();
  174. /* The unit test will fail if we return 0 */
  175. return 1;
  176. /* When conn == NULL, we can't cleanup anything */
  177. done:
  178. return 0;
  179. }
  180. static void *
  181. test_conn_get_rend_setup(const struct testcase_t *tc)
  182. {
  183. dir_connection_t *conn = DOWNCAST(dir_connection_t,
  184. test_conn_get_connection(
  185. TEST_CONN_STATE,
  186. TEST_CONN_TYPE,
  187. TEST_CONN_REND_PURPOSE));
  188. tt_assert(conn);
  189. assert_connection_ok(&conn->base_, time(NULL));
  190. rend_cache_init();
  191. /* TODO: use directory_initiate_command_rend() to do this - maybe? */
  192. conn->rend_data = tor_malloc_zero(sizeof(rend_data_t));
  193. tor_assert(strlen(TEST_CONN_REND_ADDR) == REND_SERVICE_ID_LEN_BASE32);
  194. memcpy(conn->rend_data->onion_address,
  195. TEST_CONN_REND_ADDR,
  196. REND_SERVICE_ID_LEN_BASE32+1);
  197. conn->rend_data->hsdirs_fp = smartlist_new();
  198. assert_connection_ok(&conn->base_, time(NULL));
  199. return conn;
  200. /* On failure */
  201. done:
  202. test_conn_get_rend_teardown(tc, conn);
  203. /* Returning NULL causes the unit test to fail */
  204. return NULL;
  205. }
  206. static int
  207. test_conn_get_rend_teardown(const struct testcase_t *tc, void *arg)
  208. {
  209. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  210. int rv = 0;
  211. tt_assert(conn);
  212. assert_connection_ok(&conn->base_, time(NULL));
  213. /* avoid a last-ditch attempt to refetch the descriptor */
  214. conn->base_.purpose = TEST_CONN_REND_PURPOSE_SUCCESSFUL;
  215. /* connection_free_() cleans up rend_data */
  216. rv = test_conn_get_basic_teardown(tc, arg);
  217. done:
  218. rend_cache_free_all();
  219. return rv;
  220. }
  221. static dir_connection_t *
  222. test_conn_download_status_add_a_connection(const char *resource)
  223. {
  224. dir_connection_t *conn = DOWNCAST(dir_connection_t,
  225. test_conn_get_connection(
  226. TEST_CONN_STATE,
  227. TEST_CONN_TYPE,
  228. TEST_CONN_RSRC_PURPOSE));
  229. tt_assert(conn);
  230. assert_connection_ok(&conn->base_, time(NULL));
  231. /* Replace the existing resource with the one we want */
  232. if (resource) {
  233. if (conn->requested_resource) {
  234. tor_free(conn->requested_resource);
  235. }
  236. conn->requested_resource = tor_strdup(resource);
  237. assert_connection_ok(&conn->base_, time(NULL));
  238. }
  239. return conn;
  240. done:
  241. test_conn_get_rsrc_teardown(NULL, conn);
  242. return NULL;
  243. }
  244. static void *
  245. test_conn_get_rsrc_setup(const struct testcase_t *tc)
  246. {
  247. (void)tc;
  248. return test_conn_download_status_add_a_connection(TEST_CONN_RSRC);
  249. }
  250. static int
  251. test_conn_get_rsrc_teardown(const struct testcase_t *tc, void *arg)
  252. {
  253. int rv = 0;
  254. connection_t *conn = (connection_t *)arg;
  255. tt_assert(conn);
  256. assert_connection_ok(conn, time(NULL));
  257. if (conn->type == CONN_TYPE_DIR) {
  258. dir_connection_t *dir_conn = DOWNCAST(dir_connection_t, arg);
  259. tt_assert(dir_conn);
  260. assert_connection_ok(&dir_conn->base_, time(NULL));
  261. /* avoid a last-ditch attempt to refetch the consensus */
  262. dir_conn->base_.state = TEST_CONN_RSRC_STATE_SUCCESSFUL;
  263. assert_connection_ok(&dir_conn->base_, time(NULL));
  264. }
  265. /* connection_free_() cleans up requested_resource */
  266. rv = test_conn_get_basic_teardown(tc, conn);
  267. done:
  268. return rv;
  269. }
  270. static void *
  271. test_conn_download_status_setup(const struct testcase_t *tc)
  272. {
  273. return (void*)tc;
  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. conn->rend_data->onion_address)
  433. == TO_CONN(conn));
  434. tt_assert(connection_get_by_type_state_rendquery(
  435. TEST_CONN_TYPE,
  436. TEST_CONN_STATE,
  437. TEST_CONN_REND_ADDR)
  438. == TO_CONN(conn));
  439. tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
  440. !conn->base_.state,
  441. "")
  442. == NULL);
  443. tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
  444. !TEST_CONN_STATE,
  445. TEST_CONN_REND_ADDR_2)
  446. == NULL);
  447. done:
  448. ;
  449. }
  450. #define sl_is_conn_assert(sl_input, conn) \
  451. do { \
  452. the_sl = (sl_input); \
  453. tt_assert(smartlist_len((the_sl)) == 1); \
  454. tt_assert(smartlist_get((the_sl), 0) == (conn)); \
  455. smartlist_free(the_sl); the_sl = NULL; \
  456. } while (0)
  457. #define sl_no_conn_assert(sl_input) \
  458. do { \
  459. the_sl = (sl_input); \
  460. tt_assert(smartlist_len((the_sl)) == 0); \
  461. smartlist_free(the_sl); the_sl = NULL; \
  462. } while (0)
  463. static void
  464. test_conn_get_rsrc(void *arg)
  465. {
  466. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  467. smartlist_t *the_sl = NULL;
  468. tt_assert(conn);
  469. assert_connection_ok(&conn->base_, time(NULL));
  470. sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
  471. conn->base_.purpose,
  472. conn->requested_resource),
  473. conn);
  474. sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
  475. TEST_CONN_RSRC_PURPOSE,
  476. TEST_CONN_RSRC),
  477. conn);
  478. sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
  479. !conn->base_.purpose,
  480. ""));
  481. sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
  482. !TEST_CONN_RSRC_PURPOSE,
  483. TEST_CONN_RSRC_2));
  484. sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  485. conn->base_.purpose,
  486. conn->requested_resource,
  487. conn->base_.state),
  488. conn);
  489. sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  490. TEST_CONN_RSRC_PURPOSE,
  491. TEST_CONN_RSRC,
  492. TEST_CONN_STATE),
  493. conn);
  494. sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  495. !conn->base_.purpose,
  496. "",
  497. !conn->base_.state));
  498. sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  499. !TEST_CONN_RSRC_PURPOSE,
  500. TEST_CONN_RSRC_2,
  501. !TEST_CONN_STATE));
  502. tt_assert(connection_dir_count_by_purpose_and_resource(
  503. conn->base_.purpose,
  504. conn->requested_resource)
  505. == 1);
  506. tt_assert(connection_dir_count_by_purpose_and_resource(
  507. TEST_CONN_RSRC_PURPOSE,
  508. TEST_CONN_RSRC)
  509. == 1);
  510. tt_assert(connection_dir_count_by_purpose_and_resource(
  511. !conn->base_.purpose,
  512. "")
  513. == 0);
  514. tt_assert(connection_dir_count_by_purpose_and_resource(
  515. !TEST_CONN_RSRC_PURPOSE,
  516. TEST_CONN_RSRC_2)
  517. == 0);
  518. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  519. conn->base_.purpose,
  520. conn->requested_resource,
  521. conn->base_.state)
  522. == 1);
  523. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  524. TEST_CONN_RSRC_PURPOSE,
  525. TEST_CONN_RSRC,
  526. TEST_CONN_STATE)
  527. == 1);
  528. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  529. !conn->base_.purpose,
  530. "",
  531. !conn->base_.state)
  532. == 0);
  533. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  534. !TEST_CONN_RSRC_PURPOSE,
  535. TEST_CONN_RSRC_2,
  536. !TEST_CONN_STATE)
  537. == 0);
  538. done:
  539. smartlist_free(the_sl);
  540. }
  541. static void
  542. test_conn_download_status(void *arg)
  543. {
  544. dir_connection_t *conn = NULL;
  545. dir_connection_t *conn2 = NULL;
  546. dir_connection_t *conn4 = NULL;
  547. connection_t *ap_conn = NULL;
  548. const struct testcase_t *tc = arg;
  549. consensus_flavor_t usable_flavor = (consensus_flavor_t)tc->setup_data;
  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. };