test_connection.c 35 KB

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