test_connection.c 34 KB

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