test_connection.c 34 KB

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