test_connection.c 35 KB

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