test_connection.c 34 KB

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