test_connection.c 34 KB

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