test_connection.c 34 KB

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