test_connection.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. (void)tc;
  274. /* Don't return NULL, that causes the test to fail */
  275. return (void*)"ok";
  276. }
  277. static int
  278. test_conn_download_status_teardown(const struct testcase_t *tc, void *arg)
  279. {
  280. (void)arg;
  281. int rv = 0;
  282. /* Ignore arg, and just loop through the connection array */
  283. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  284. if (conn) {
  285. assert_connection_ok(conn, time(NULL));
  286. /* connection_free_() cleans up requested_resource */
  287. rv = test_conn_get_rsrc_teardown(tc, conn);
  288. tt_assert(rv == 1);
  289. }
  290. } SMARTLIST_FOREACH_END(conn);
  291. done:
  292. return rv;
  293. }
  294. /* Like connection_ap_make_link(), but does much less */
  295. static connection_t *
  296. test_conn_get_linked_connection(connection_t *l_conn, uint8_t state)
  297. {
  298. tt_assert(l_conn);
  299. assert_connection_ok(l_conn, time(NULL));
  300. /* AP connections don't seem to have purposes */
  301. connection_t *conn = test_conn_get_connection(state, CONN_TYPE_AP,
  302. 0);
  303. tt_assert(conn);
  304. assert_connection_ok(conn, time(NULL));
  305. conn->linked = 1;
  306. l_conn->linked = 1;
  307. conn->linked_conn = l_conn;
  308. l_conn->linked_conn = conn;
  309. /* we never opened a real socket, so we can just overwrite it */
  310. conn->s = TOR_INVALID_SOCKET;
  311. l_conn->s = TOR_INVALID_SOCKET;
  312. assert_connection_ok(conn, time(NULL));
  313. assert_connection_ok(l_conn, time(NULL));
  314. return conn;
  315. done:
  316. test_conn_download_status_teardown(NULL, NULL);
  317. return NULL;
  318. }
  319. static struct testcase_setup_t test_conn_get_basic_st = {
  320. test_conn_get_basic_setup, test_conn_get_basic_teardown
  321. };
  322. static struct testcase_setup_t test_conn_get_rend_st = {
  323. test_conn_get_rend_setup, test_conn_get_rend_teardown
  324. };
  325. static struct testcase_setup_t test_conn_get_rsrc_st = {
  326. test_conn_get_rsrc_setup, test_conn_get_rsrc_teardown
  327. };
  328. static struct testcase_setup_t test_conn_download_status_st = {
  329. test_conn_download_status_setup, test_conn_download_status_teardown
  330. };
  331. static void
  332. test_conn_get_basic(void *arg)
  333. {
  334. connection_t *conn = (connection_t*)arg;
  335. tor_addr_t addr, addr2;
  336. tt_assert(conn);
  337. assert_connection_ok(conn, time(NULL));
  338. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr);
  339. tt_assert(!tor_addr_is_null(&addr));
  340. test_conn_lookup_addr_helper(TEST_CONN_ADDRESS_2, TEST_CONN_FAMILY, &addr2);
  341. tt_assert(!tor_addr_is_null(&addr2));
  342. /* Check that we get this connection back when we search for it by
  343. * its attributes, but get NULL when we supply a different value. */
  344. tt_assert(connection_get_by_global_id(conn->global_identifier) == conn);
  345. tt_assert(connection_get_by_global_id(!conn->global_identifier) == NULL);
  346. tt_assert(connection_get_by_type(conn->type) == conn);
  347. tt_assert(connection_get_by_type(TEST_CONN_TYPE) == conn);
  348. tt_assert(connection_get_by_type(!conn->type) == NULL);
  349. tt_assert(connection_get_by_type(!TEST_CONN_TYPE) == NULL);
  350. tt_assert(connection_get_by_type_state(conn->type, conn->state)
  351. == conn);
  352. tt_assert(connection_get_by_type_state(TEST_CONN_TYPE, TEST_CONN_STATE)
  353. == conn);
  354. tt_assert(connection_get_by_type_state(!conn->type, !conn->state)
  355. == NULL);
  356. tt_assert(connection_get_by_type_state(!TEST_CONN_TYPE, !TEST_CONN_STATE)
  357. == NULL);
  358. /* Match on the connection fields themselves */
  359. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  360. &conn->addr,
  361. conn->port,
  362. conn->purpose)
  363. == conn);
  364. /* Match on the original inputs to the connection */
  365. tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
  366. &conn->addr,
  367. conn->port,
  368. conn->purpose)
  369. == conn);
  370. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  371. &addr,
  372. conn->port,
  373. conn->purpose)
  374. == conn);
  375. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  376. &conn->addr,
  377. TEST_CONN_PORT,
  378. conn->purpose)
  379. == conn);
  380. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  381. &conn->addr,
  382. conn->port,
  383. TEST_CONN_BASIC_PURPOSE)
  384. == conn);
  385. tt_assert(connection_get_by_type_addr_port_purpose(TEST_CONN_TYPE,
  386. &addr,
  387. TEST_CONN_PORT,
  388. TEST_CONN_BASIC_PURPOSE)
  389. == conn);
  390. /* Then try each of the not-matching combinations */
  391. tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
  392. &conn->addr,
  393. conn->port,
  394. conn->purpose)
  395. == NULL);
  396. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  397. &addr2,
  398. conn->port,
  399. conn->purpose)
  400. == NULL);
  401. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  402. &conn->addr,
  403. !conn->port,
  404. conn->purpose)
  405. == NULL);
  406. tt_assert(connection_get_by_type_addr_port_purpose(conn->type,
  407. &conn->addr,
  408. conn->port,
  409. !conn->purpose)
  410. == NULL);
  411. /* Then try everything not-matching */
  412. tt_assert(connection_get_by_type_addr_port_purpose(!conn->type,
  413. &addr2,
  414. !conn->port,
  415. !conn->purpose)
  416. == NULL);
  417. tt_assert(connection_get_by_type_addr_port_purpose(!TEST_CONN_TYPE,
  418. &addr2,
  419. !TEST_CONN_PORT,
  420. !TEST_CONN_BASIC_PURPOSE)
  421. == NULL);
  422. done:
  423. ;
  424. }
  425. static void
  426. test_conn_get_rend(void *arg)
  427. {
  428. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  429. tt_assert(conn);
  430. assert_connection_ok(&conn->base_, time(NULL));
  431. tt_assert(connection_get_by_type_state_rendquery(
  432. conn->base_.type,
  433. conn->base_.state,
  434. conn->rend_data->onion_address)
  435. == TO_CONN(conn));
  436. tt_assert(connection_get_by_type_state_rendquery(
  437. TEST_CONN_TYPE,
  438. TEST_CONN_STATE,
  439. TEST_CONN_REND_ADDR)
  440. == TO_CONN(conn));
  441. tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
  442. !conn->base_.state,
  443. "")
  444. == NULL);
  445. tt_assert(connection_get_by_type_state_rendquery(TEST_CONN_REND_TYPE_2,
  446. !TEST_CONN_STATE,
  447. TEST_CONN_REND_ADDR_2)
  448. == NULL);
  449. done:
  450. ;
  451. }
  452. #define sl_is_conn_assert(sl_input, conn) \
  453. do { \
  454. the_sl = (sl_input); \
  455. tt_assert(smartlist_len((the_sl)) == 1); \
  456. tt_assert(smartlist_get((the_sl), 0) == (conn)); \
  457. smartlist_free(the_sl); the_sl = NULL; \
  458. } while (0)
  459. #define sl_no_conn_assert(sl_input) \
  460. do { \
  461. the_sl = (sl_input); \
  462. tt_assert(smartlist_len((the_sl)) == 0); \
  463. smartlist_free(the_sl); the_sl = NULL; \
  464. } while (0)
  465. static void
  466. test_conn_get_rsrc(void *arg)
  467. {
  468. dir_connection_t *conn = DOWNCAST(dir_connection_t, arg);
  469. smartlist_t *the_sl = NULL;
  470. tt_assert(conn);
  471. assert_connection_ok(&conn->base_, time(NULL));
  472. sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
  473. conn->base_.purpose,
  474. conn->requested_resource),
  475. conn);
  476. sl_is_conn_assert(connection_dir_list_by_purpose_and_resource(
  477. TEST_CONN_RSRC_PURPOSE,
  478. TEST_CONN_RSRC),
  479. conn);
  480. sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
  481. !conn->base_.purpose,
  482. ""));
  483. sl_no_conn_assert(connection_dir_list_by_purpose_and_resource(
  484. !TEST_CONN_RSRC_PURPOSE,
  485. TEST_CONN_RSRC_2));
  486. sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  487. conn->base_.purpose,
  488. conn->requested_resource,
  489. conn->base_.state),
  490. conn);
  491. sl_is_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  492. TEST_CONN_RSRC_PURPOSE,
  493. TEST_CONN_RSRC,
  494. TEST_CONN_STATE),
  495. conn);
  496. sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  497. !conn->base_.purpose,
  498. "",
  499. !conn->base_.state));
  500. sl_no_conn_assert(connection_dir_list_by_purpose_resource_and_state(
  501. !TEST_CONN_RSRC_PURPOSE,
  502. TEST_CONN_RSRC_2,
  503. !TEST_CONN_STATE));
  504. tt_assert(connection_dir_count_by_purpose_and_resource(
  505. conn->base_.purpose,
  506. conn->requested_resource)
  507. == 1);
  508. tt_assert(connection_dir_count_by_purpose_and_resource(
  509. TEST_CONN_RSRC_PURPOSE,
  510. TEST_CONN_RSRC)
  511. == 1);
  512. tt_assert(connection_dir_count_by_purpose_and_resource(
  513. !conn->base_.purpose,
  514. "")
  515. == 0);
  516. tt_assert(connection_dir_count_by_purpose_and_resource(
  517. !TEST_CONN_RSRC_PURPOSE,
  518. TEST_CONN_RSRC_2)
  519. == 0);
  520. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  521. conn->base_.purpose,
  522. conn->requested_resource,
  523. conn->base_.state)
  524. == 1);
  525. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  526. TEST_CONN_RSRC_PURPOSE,
  527. TEST_CONN_RSRC,
  528. TEST_CONN_STATE)
  529. == 1);
  530. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  531. !conn->base_.purpose,
  532. "",
  533. !conn->base_.state)
  534. == 0);
  535. tt_assert(connection_dir_count_by_purpose_resource_and_state(
  536. !TEST_CONN_RSRC_PURPOSE,
  537. TEST_CONN_RSRC_2,
  538. !TEST_CONN_STATE)
  539. == 0);
  540. done:
  541. smartlist_free(the_sl);
  542. }
  543. static void
  544. test_conn_download_status(void *arg)
  545. {
  546. dir_connection_t *conn = NULL;
  547. dir_connection_t *conn2 = NULL;
  548. dir_connection_t *conn3 = NULL;
  549. dir_connection_t *conn4 = NULL;
  550. connection_t *ap_conn = NULL;
  551. connection_t *ap_conn2 = NULL;
  552. /* we never create an ap_conn for conn3 */
  553. connection_t *ap_conn4 = NULL;
  554. consensus_flavor_t usable_flavor = (consensus_flavor_t)arg;
  555. /* The "other flavor" trick only works if there are two flavors */
  556. tor_assert(N_CONSENSUS_FLAVORS == 2);
  557. consensus_flavor_t other_flavor = ((usable_flavor == FLAV_NS)
  558. ? FLAV_MICRODESC
  559. : FLAV_NS);
  560. const char *res = networkstatus_get_flavor_name(usable_flavor);
  561. const char *other_res = networkstatus_get_flavor_name(other_flavor);
  562. /* no connections */
  563. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  564. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  565. tt_assert(connection_dir_count_by_purpose_and_resource(
  566. TEST_CONN_RSRC_PURPOSE,
  567. res) == 0);
  568. tt_assert(connection_dir_count_by_purpose_and_resource(
  569. TEST_CONN_RSRC_PURPOSE,
  570. other_res) == 0);
  571. /* one connection, not downloading */
  572. conn = test_conn_download_status_add_a_connection(res);
  573. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  574. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  575. tt_assert(connection_dir_count_by_purpose_and_resource(
  576. TEST_CONN_RSRC_PURPOSE,
  577. res) == 1);
  578. tt_assert(connection_dir_count_by_purpose_and_resource(
  579. TEST_CONN_RSRC_PURPOSE,
  580. other_res) == 0);
  581. /* one connection, downloading but not linked (not possible on a client,
  582. * but possible on a relay) */
  583. conn->base_.state = TEST_CONN_DL_STATE;
  584. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  585. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  586. tt_assert(connection_dir_count_by_purpose_and_resource(
  587. TEST_CONN_RSRC_PURPOSE,
  588. res) == 1);
  589. tt_assert(connection_dir_count_by_purpose_and_resource(
  590. TEST_CONN_RSRC_PURPOSE,
  591. other_res) == 0);
  592. /* one connection, downloading and linked, but not yet attached */
  593. ap_conn = test_conn_get_linked_connection(TO_CONN(conn),
  594. TEST_CONN_UNATTACHED_STATE);
  595. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  596. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  597. tt_assert(connection_dir_count_by_purpose_and_resource(
  598. TEST_CONN_RSRC_PURPOSE,
  599. res) == 1);
  600. tt_assert(connection_dir_count_by_purpose_and_resource(
  601. TEST_CONN_RSRC_PURPOSE,
  602. other_res) == 0);
  603. /* one connection, downloading and linked and attached */
  604. ap_conn->state = TEST_CONN_ATTACHED_STATE;
  605. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  606. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  607. tt_assert(connection_dir_count_by_purpose_and_resource(
  608. TEST_CONN_RSRC_PURPOSE,
  609. res) == 1);
  610. tt_assert(connection_dir_count_by_purpose_and_resource(
  611. TEST_CONN_RSRC_PURPOSE,
  612. other_res) == 0);
  613. /* one connection, linked and attached but not downloading */
  614. conn->base_.state = TEST_CONN_STATE;
  615. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  616. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  617. tt_assert(connection_dir_count_by_purpose_and_resource(
  618. TEST_CONN_RSRC_PURPOSE,
  619. res) == 1);
  620. tt_assert(connection_dir_count_by_purpose_and_resource(
  621. TEST_CONN_RSRC_PURPOSE,
  622. other_res) == 0);
  623. /* two connections, both not downloading */
  624. conn2 = test_conn_download_status_add_a_connection(res);
  625. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  626. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  627. tt_assert(connection_dir_count_by_purpose_and_resource(
  628. TEST_CONN_RSRC_PURPOSE,
  629. res) == 2);
  630. tt_assert(connection_dir_count_by_purpose_and_resource(
  631. TEST_CONN_RSRC_PURPOSE,
  632. other_res) == 0);
  633. /* two connections, one downloading */
  634. conn->base_.state = TEST_CONN_DL_STATE;
  635. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  636. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  637. tt_assert(connection_dir_count_by_purpose_and_resource(
  638. TEST_CONN_RSRC_PURPOSE,
  639. res) == 2);
  640. tt_assert(connection_dir_count_by_purpose_and_resource(
  641. TEST_CONN_RSRC_PURPOSE,
  642. other_res) == 0);
  643. conn->base_.state = TEST_CONN_STATE;
  644. /* more connections, all not downloading */
  645. conn3 = test_conn_download_status_add_a_connection(res);
  646. tt_assert(networkstatus_consensus_is_already_downloading(res) == 0);
  647. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  648. tt_assert(connection_dir_count_by_purpose_and_resource(
  649. TEST_CONN_RSRC_PURPOSE,
  650. res) == 3);
  651. tt_assert(connection_dir_count_by_purpose_and_resource(
  652. TEST_CONN_RSRC_PURPOSE,
  653. other_res) == 0);
  654. /* more connections, one downloading */
  655. conn->base_.state = TEST_CONN_DL_STATE;
  656. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  657. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  658. tt_assert(connection_dir_count_by_purpose_and_resource(
  659. TEST_CONN_RSRC_PURPOSE,
  660. res) == 3);
  661. tt_assert(connection_dir_count_by_purpose_and_resource(
  662. TEST_CONN_RSRC_PURPOSE,
  663. other_res) == 0);
  664. /* more connections, two downloading (should never happen, but needs
  665. * to be tested for completeness) */
  666. conn2->base_.state = TEST_CONN_DL_STATE;
  667. ap_conn2 = test_conn_get_linked_connection(TO_CONN(conn2),
  668. TEST_CONN_ATTACHED_STATE);
  669. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  670. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  671. tt_assert(connection_dir_count_by_purpose_and_resource(
  672. TEST_CONN_RSRC_PURPOSE,
  673. res) == 3);
  674. tt_assert(connection_dir_count_by_purpose_and_resource(
  675. TEST_CONN_RSRC_PURPOSE,
  676. other_res) == 0);
  677. conn->base_.state = TEST_CONN_STATE;
  678. /* more connections, a different one downloading */
  679. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  680. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  681. tt_assert(connection_dir_count_by_purpose_and_resource(
  682. TEST_CONN_RSRC_PURPOSE,
  683. res) == 3);
  684. tt_assert(connection_dir_count_by_purpose_and_resource(
  685. TEST_CONN_RSRC_PURPOSE,
  686. other_res) == 0);
  687. /* a connection for the other flavor (could happen if a client is set to
  688. * cache directory documents), one preferred flavor downloading
  689. */
  690. conn4 = test_conn_download_status_add_a_connection(other_res);
  691. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  692. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 0);
  693. tt_assert(connection_dir_count_by_purpose_and_resource(
  694. TEST_CONN_RSRC_PURPOSE,
  695. res) == 3);
  696. tt_assert(connection_dir_count_by_purpose_and_resource(
  697. TEST_CONN_RSRC_PURPOSE,
  698. other_res) == 1);
  699. /* a connection for the other flavor (could happen if a client is set to
  700. * cache directory documents), both flavors downloading
  701. */
  702. conn4->base_.state = TEST_CONN_DL_STATE;
  703. ap_conn4 = test_conn_get_linked_connection(TO_CONN(conn4),
  704. TEST_CONN_ATTACHED_STATE);
  705. tt_assert(networkstatus_consensus_is_already_downloading(res) == 1);
  706. tt_assert(networkstatus_consensus_is_already_downloading(other_res) == 1);
  707. tt_assert(connection_dir_count_by_purpose_and_resource(
  708. TEST_CONN_RSRC_PURPOSE,
  709. res) == 3);
  710. tt_assert(connection_dir_count_by_purpose_and_resource(
  711. TEST_CONN_RSRC_PURPOSE,
  712. other_res) == 1);
  713. conn4->base_.state = TEST_CONN_STATE;
  714. done:
  715. /* the teardown function removes all the connections */;
  716. }
  717. #define CONNECTION_TESTCASE(name, fork, setup) \
  718. { #name, test_conn_##name, fork, &setup, NULL }
  719. /* where arg is an expression (constant, varaible, compound expression) */
  720. #define CONNECTION_TESTCASE_ARG(name, fork, setup, arg) \
  721. { #name "_" #arg, test_conn_##name, fork, &setup, (void *)arg }
  722. struct testcase_t connection_tests[] = {
  723. CONNECTION_TESTCASE(get_basic, TT_FORK, test_conn_get_basic_st),
  724. CONNECTION_TESTCASE(get_rend, TT_FORK, test_conn_get_rend_st),
  725. CONNECTION_TESTCASE(get_rsrc, TT_FORK, test_conn_get_rsrc_st),
  726. CONNECTION_TESTCASE_ARG(download_status, TT_FORK,
  727. test_conn_download_status_st, FLAV_MICRODESC),
  728. CONNECTION_TESTCASE_ARG(download_status, TT_FORK,
  729. test_conn_download_status_st, FLAV_NS),
  730. //CONNECTION_TESTCASE(func_suffix, TT_FORK, setup_func_pair),
  731. END_OF_TESTCASES
  732. };