test_connection.c 34 KB

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