test_connection.c 34 KB

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