test_connection.c 35 KB

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