test_hs.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs.c
  5. * \brief Unit tests for hidden service.
  6. **/
  7. #define CONTROL_EVENTS_PRIVATE
  8. #define CIRCUITBUILD_PRIVATE
  9. #define RENDCOMMON_PRIVATE
  10. #define RENDSERVICE_PRIVATE
  11. #define HS_SERVICE_PRIVATE
  12. #include "core/or/or.h"
  13. #include "test/test.h"
  14. #include "feature/control/control.h"
  15. #include "feature/control/control_events.h"
  16. #include "feature/control/control_fmt.h"
  17. #include "app/config/config.h"
  18. #include "feature/hs/hs_common.h"
  19. #include "feature/rend/rendcommon.h"
  20. #include "feature/rend/rendservice.h"
  21. #include "feature/nodelist/routerlist.h"
  22. #include "feature/nodelist/routerset.h"
  23. #include "core/or/circuitbuild.h"
  24. #include "feature/nodelist/node_st.h"
  25. #include "feature/rend/rend_encoded_v2_service_descriptor_st.h"
  26. #include "feature/rend/rend_intro_point_st.h"
  27. #include "feature/nodelist/routerinfo_st.h"
  28. #include "test/test_helpers.h"
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. /* mock ID digest and longname for node that's in nodelist */
  33. #define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
  34. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  35. #define STR_HSDIR_EXIST_LONGNAME \
  36. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
  37. /* mock ID digest and longname for node that's not in nodelist */
  38. #define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
  39. "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
  40. #define STR_HSDIR_NONE_EXIST_LONGNAME \
  41. "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  42. /* DuckDuckGo descriptor as an example. This one has extra "\r" at the end so
  43. * the control port is happy. */
  44. static const char *hs_desc_content_control = "\
  45. rendezvous-service-descriptor g5ojobzupf275beh5ra72uyhb3dkpxwg\r\n\
  46. version 2\r\n\
  47. permanent-key\r\n\
  48. -----BEGIN RSA PUBLIC KEY-----\r\n\
  49. MIGJAoGBAJ/SzzgrXPxTlFrKVhXh3buCWv2QfcNgncUpDpKouLn3AtPH5Ocys0jE\r\n\
  50. aZSKdvaiQ62md2gOwj4x61cFNdi05tdQjS+2thHKEm/KsB9BGLSLBNJYY356bupg\r\n\
  51. I5gQozM65ENelfxYlysBjJ52xSDBd8C4f/p9umdzaaaCmzXG/nhzAgMBAAE=\r\n\
  52. -----END RSA PUBLIC KEY-----\r\n\
  53. secret-id-part anmjoxxwiupreyajjt5yasimfmwcnxlf\r\n\
  54. publication-time 2015-03-11 19:00:00\r\n\
  55. protocol-versions 2,3\r\n\
  56. introduction-points\r\n\
  57. -----BEGIN MESSAGE-----\r\n\
  58. aW50cm9kdWN0aW9uLXBvaW50IDd1bnd4cmg2dG5kNGh6eWt1Z3EzaGZzdHduc2ll\r\n\
  59. cmhyCmlwLWFkZHJlc3MgMTg4LjEzOC4xMjEuMTE4Cm9uaW9uLXBvcnQgOTAwMQpv\r\n\
  60. bmlvbi1rZXkKLS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JR0pBb0dC\r\n\
  61. QUxGRVVyeVpDbk9ROEhURmV5cDVjMTRObWVqL1BhekFLTTBxRENTNElKUWh0Y3g1\r\n\
  62. NXpRSFdOVWIKQ2hHZ0JqR1RjV3ZGRnA0N3FkdGF6WUZhVXE2c0lQKzVqeWZ5b0Q4\r\n\
  63. UmJ1bzBwQmFWclJjMmNhYUptWWM0RDh6Vgpuby9sZnhzOVVaQnZ1cWY4eHIrMDB2\r\n\
  64. S0JJNmFSMlA2OE1WeDhrMExqcUpUU2RKOE9idm9yQWdNQkFBRT0KLS0tLS1FTkQg\r\n\
  65. UlNBIFBVQkxJQyBLRVktLS0tLQpzZXJ2aWNlLWtleQotLS0tLUJFR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RLVkx4eGsKeERlMjlDNzRWMmJrOTRJ\r\n\
  95. MEgybTNKS2tzTHVwc3VxWWRVUmhOVXN0SElKZmgyZmNIalF0bEFnTUJBQUU9Ci0t\r\n\
  96. LS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0KCg==\r\n\
  97. -----END MESSAGE-----\r\n\
  98. signature\r\n\
  99. -----BEGIN SIGNATURE-----\r\n\
  100. d4OuCE5OLAOnRB6cQN6WyMEmg/BHem144Vec+eYgeWoKwx3MxXFplUjFxgnMlmwN\r\n\
  101. PcftsZf2ztN0sbNCtPgDL3d0PqvxY3iHTQAI8EbaGq/IAJUZ8U4y963dD5+Bn6JQ\r\n\
  102. myE3ctmh0vy5+QxSiRjmQBkuEpCyks7LvWvHYrhnmcg=\r\n\
  103. -----END SIGNATURE-----";
  104. /* DuckDuckGo descriptor as an example. */
  105. static const char *hs_desc_content = "\
  106. rendezvous-service-descriptor g5ojobzupf275beh5ra72uyhb3dkpxwg\n\
  107. version 2\n\
  108. permanent-key\n\
  109. -----BEGIN RSA PUBLIC KEY-----\n\
  110. MIGJAoGBAJ/SzzgrXPxTlFrKVhXh3buCWv2QfcNgncUpDpKouLn3AtPH5Ocys0jE\n\
  111. aZSKdvaiQ62md2gOwj4x61cFNdi05tdQjS+2thHKEm/KsB9BGLSLBNJYY356bupg\n\
  112. I5gQozM65ENelfxYlysBjJ52xSDBd8C4f/p9umdzaaaCmzXG/nhzAgMBAAE=\n\
  113. -----END RSA PUBLIC KEY-----\n\
  114. secret-id-part anmjoxxwiupreyajjt5yasimfmwcnxlf\n\
  115. publication-time 2015-03-11 19:00:00\n\
  116. protocol-versions 2,3\n\
  117. introduction-points\n\
  118. -----BEGIN MESSAGE-----\n\
  119. aW50cm9kdWN0aW9uLXBvaW50IDd1bnd4cmg2dG5kNGh6eWt1Z3EzaGZzdHduc2ll\n\
  120. cmhyCmlwLWFkZHJlc3MgMTg4LjEzOC4xMjEuMTE4Cm9uaW9uLXBvcnQgOTAwM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c5OG5vSmdGUGRUZ05iMjdKYmVUZENLVFBrTEtscXFt\n\
  151. b3NveUN2RitRa25vUS9BZ01CQUFFPQotLS0tLUVORCBSU0EgUFVCTElDIEtFWS0t\n\
  152. LS0tCnNlcnZpY2Uta2V5Ci0tLS0tQkVHSU4gUlNBIFBVQkxJQyBLRVktLS0tLQpN\n\
  153. SUdKQW9HQkFMVjNKSmtWN3lTNU9jc1lHMHNFYzFQOTVRclFRR3ZzbGJ6Wi9zRGxl\n\
  154. RlpKYXFSOUYvYjRUVERNClNGcFMxcU1GbldkZDgxVmRGMEdYRmN2WVpLamRJdHU2\n\
  155. SndBaTRJeEhxeXZtdTRKdUxrcXNaTEFLaXRLVkx4eGsKeERlMjlDNzRWMmJrOTRJ\n\
  156. MEgybTNKS2tzTHVwc3VxWWRVUmhOVXN0SElKZmgyZmNIalF0bEFnTUJBQUU9Ci0t\n\
  157. LS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0KCg==\n\
  158. -----END MESSAGE-----\n\
  159. signature\n\
  160. -----BEGIN SIGNATURE-----\n\
  161. d4OuCE5OLAOnRB6cQN6WyMEmg/BHem144Vec+eYgeWoKwx3MxXFplUjFxgnMlmwN\n\
  162. PcftsZf2ztN0sbNCtPgDL3d0PqvxY3iHTQAI8EbaGq/IAJUZ8U4y963dD5+Bn6JQ\n\
  163. myE3ctmh0vy5+QxSiRjmQBkuEpCyks7LvWvHYrhnmcg=\n\
  164. -----END SIGNATURE-----";
  165. /* Helper global variable for hidden service descriptor event test.
  166. * It's used as a pointer to dynamically created message buffer in
  167. * send_control_event_string_replacement function, which mocks
  168. * send_control_event_string function.
  169. *
  170. * Always free it after use! */
  171. static char *received_msg = NULL;
  172. /** Mock function for send_control_event_string
  173. */
  174. static void
  175. queue_control_event_string_replacement(uint16_t event, char *msg)
  176. {
  177. (void) event;
  178. tor_free(received_msg);
  179. received_msg = msg;
  180. }
  181. /** Mock function for node_describe_longname_by_id, it returns either
  182. * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
  183. */
  184. static const char *
  185. node_describe_longname_by_id_replacement(const char *id_digest)
  186. {
  187. if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
  188. return STR_HSDIR_EXIST_LONGNAME;
  189. } else {
  190. return STR_HSDIR_NONE_EXIST_LONGNAME;
  191. }
  192. }
  193. /** Test that we can parse a hardcoded v2 HS desc. */
  194. static void
  195. test_hs_parse_static_v2_desc(void *arg)
  196. {
  197. int ret;
  198. rend_encoded_v2_service_descriptor_t desc;
  199. (void) arg;
  200. /* Test an obviously not parseable string */
  201. desc.desc_str = tor_strdup("ceci n'est pas un HS descriptor");
  202. ret = rend_desc_v2_is_parsable(&desc);
  203. tor_free(desc.desc_str);
  204. tt_int_op(ret, OP_EQ, 0);
  205. /* Test an actual descriptor */
  206. desc.desc_str = tor_strdup(hs_desc_content);
  207. ret = rend_desc_v2_is_parsable(&desc);
  208. tor_free(desc.desc_str);
  209. tt_int_op(ret, OP_EQ, 1);
  210. done: ;
  211. }
  212. /** Make sure each hidden service descriptor async event generation
  213. *
  214. * function generates the message in expected format.
  215. */
  216. static void
  217. test_hs_desc_event(void *arg)
  218. {
  219. #define STR_HS_ADDR "ajhb7kljbiru65qo"
  220. #define STR_HS_CONTENT_DESC_ID "g5ojobzupf275beh5ra72uyhb3dkpxwg"
  221. #define STR_DESC_ID_BASE32 "hba3gmcgpfivzfhx5rtfqkfdhv65yrj3"
  222. int ret;
  223. rend_data_v2_t rend_query;
  224. const char *expected_msg;
  225. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  226. (void) arg;
  227. MOCK(queue_control_event_string,
  228. queue_control_event_string_replacement);
  229. MOCK(node_describe_longname_by_id,
  230. node_describe_longname_by_id_replacement);
  231. /* setup rend_query struct */
  232. memset(&rend_query, 0, sizeof(rend_query));
  233. rend_query.base_.version = 2;
  234. strncpy(rend_query.onion_address, STR_HS_ADDR,
  235. REND_SERVICE_ID_LEN_BASE32+1);
  236. rend_query.auth_type = REND_NO_AUTH;
  237. rend_query.base_.hsdirs_fp = smartlist_new();
  238. smartlist_add(rend_query.base_.hsdirs_fp, tor_memdup(HSDIR_EXIST_ID,
  239. DIGEST_LEN));
  240. /* Compute descriptor ID for replica 0, should be STR_DESC_ID_BASE32. */
  241. ret = rend_compute_v2_desc_id(rend_query.descriptor_id[0],
  242. rend_query.onion_address,
  243. NULL, 0, 0);
  244. tt_int_op(ret, OP_EQ, 0);
  245. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  246. rend_query.descriptor_id[0], DIGEST_LEN);
  247. /* Make sure rend_compute_v2_desc_id works properly. */
  248. tt_mem_op(desc_id_base32, OP_EQ, STR_DESC_ID_BASE32,
  249. sizeof(desc_id_base32));
  250. /* test request event */
  251. control_event_hs_descriptor_requested(rend_query.onion_address,
  252. rend_query.auth_type, HSDIR_EXIST_ID,
  253. STR_DESC_ID_BASE32, NULL);
  254. expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
  255. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32 "\r\n";
  256. tt_assert(received_msg);
  257. tt_str_op(received_msg,OP_EQ, expected_msg);
  258. tor_free(received_msg);
  259. /* test received event */
  260. rend_query.auth_type = REND_BASIC_AUTH;
  261. control_event_hsv2_descriptor_received(rend_query.onion_address,
  262. &rend_query.base_, HSDIR_EXIST_ID);
  263. expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
  264. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32"\r\n";
  265. tt_assert(received_msg);
  266. tt_str_op(received_msg,OP_EQ, expected_msg);
  267. tor_free(received_msg);
  268. /* test failed event */
  269. rend_query.auth_type = REND_STEALTH_AUTH;
  270. control_event_hsv2_descriptor_failed(&rend_query.base_,
  271. HSDIR_NONE_EXIST_ID,
  272. "QUERY_REJECTED");
  273. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
  274. STR_HSDIR_NONE_EXIST_LONGNAME" REASON=QUERY_REJECTED\r\n";
  275. tt_assert(received_msg);
  276. tt_str_op(received_msg,OP_EQ, expected_msg);
  277. tor_free(received_msg);
  278. /* test invalid auth type */
  279. rend_query.auth_type = 999;
  280. control_event_hsv2_descriptor_failed(&rend_query.base_,
  281. HSDIR_EXIST_ID,
  282. "QUERY_REJECTED");
  283. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
  284. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32\
  285. " REASON=QUERY_REJECTED\r\n";
  286. tt_assert(received_msg);
  287. tt_str_op(received_msg,OP_EQ, expected_msg);
  288. tor_free(received_msg);
  289. /* test no HSDir fingerprint type */
  290. rend_query.auth_type = REND_NO_AUTH;
  291. control_event_hsv2_descriptor_failed(&rend_query.base_, NULL,
  292. "QUERY_NO_HSDIR");
  293. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" NO_AUTH " \
  294. "UNKNOWN REASON=QUERY_NO_HSDIR\r\n";
  295. tt_assert(received_msg);
  296. tt_str_op(received_msg,OP_EQ, expected_msg);
  297. tor_free(received_msg);
  298. /* Test invalid content with no HSDir fingerprint. */
  299. char *exp_msg;
  300. control_event_hs_descriptor_content(rend_query.onion_address,
  301. STR_HS_CONTENT_DESC_ID, NULL, NULL);
  302. tor_asprintf(&exp_msg, "650+HS_DESC_CONTENT " STR_HS_ADDR " "\
  303. STR_HS_CONTENT_DESC_ID " UNKNOWN" \
  304. "\r\n\r\n.\r\n650 OK\r\n");
  305. tt_assert(received_msg);
  306. tt_str_op(received_msg, OP_EQ, exp_msg);
  307. tor_free(received_msg);
  308. tor_free(exp_msg);
  309. /* test valid content. */
  310. control_event_hs_descriptor_content(rend_query.onion_address,
  311. STR_HS_CONTENT_DESC_ID, HSDIR_EXIST_ID,
  312. hs_desc_content_control);
  313. tor_asprintf(&exp_msg, "650+HS_DESC_CONTENT " STR_HS_ADDR " "\
  314. STR_HS_CONTENT_DESC_ID " " STR_HSDIR_EXIST_LONGNAME\
  315. "\r\n%s\r\n.\r\n650 OK\r\n", hs_desc_content_control);
  316. tt_assert(received_msg);
  317. tt_str_op(received_msg, OP_EQ, exp_msg);
  318. tor_free(received_msg);
  319. tor_free(exp_msg);
  320. SMARTLIST_FOREACH(rend_query.base_.hsdirs_fp, char *, d, tor_free(d));
  321. smartlist_free(rend_query.base_.hsdirs_fp);
  322. done:
  323. UNMOCK(queue_control_event_string);
  324. UNMOCK(node_describe_longname_by_id);
  325. tor_free(received_msg);
  326. }
  327. /* Make sure rend_data_t is valid at creation, destruction and when
  328. * duplicated. */
  329. static void
  330. test_hs_rend_data(void *arg)
  331. {
  332. int rep;
  333. rend_data_t *client = NULL, *client_dup = NULL;
  334. /* Binary format of a descriptor ID. */
  335. char desc_id[DIGEST_LEN];
  336. char client_cookie[REND_DESC_COOKIE_LEN];
  337. time_t now = time(NULL);
  338. rend_data_t *service_dup = NULL;
  339. rend_data_t *service = NULL;
  340. (void)arg;
  341. base32_decode(desc_id, sizeof(desc_id), STR_DESC_ID_BASE32,
  342. REND_DESC_ID_V2_LEN_BASE32);
  343. memset(client_cookie, 'e', sizeof(client_cookie));
  344. client = rend_data_client_create(STR_HS_ADDR, desc_id, client_cookie,
  345. REND_NO_AUTH);
  346. tt_assert(client);
  347. rend_data_v2_t *client_v2 = TO_REND_DATA_V2(client);
  348. tt_int_op(client_v2->auth_type, OP_EQ, REND_NO_AUTH);
  349. tt_str_op(client_v2->onion_address, OP_EQ, STR_HS_ADDR);
  350. tt_mem_op(client_v2->desc_id_fetch, OP_EQ, desc_id, sizeof(desc_id));
  351. tt_mem_op(client_v2->descriptor_cookie, OP_EQ, client_cookie,
  352. sizeof(client_cookie));
  353. tt_assert(client->hsdirs_fp);
  354. tt_int_op(smartlist_len(client->hsdirs_fp), OP_EQ, 0);
  355. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  356. int ret = rend_compute_v2_desc_id(desc_id, client_v2->onion_address,
  357. client_v2->descriptor_cookie, now, rep);
  358. /* That shouldn't never fail. */
  359. tt_int_op(ret, OP_EQ, 0);
  360. tt_mem_op(client_v2->descriptor_id[rep], OP_EQ, desc_id,
  361. sizeof(desc_id));
  362. }
  363. /* The rest should be zeroed because this is a client request. */
  364. tt_int_op(tor_digest_is_zero(client_v2->rend_pk_digest), OP_EQ, 1);
  365. tt_int_op(tor_digest_is_zero(client->rend_cookie), OP_EQ, 1);
  366. /* Test dup(). */
  367. client_dup = rend_data_dup(client);
  368. tt_assert(client_dup);
  369. rend_data_v2_t *client_dup_v2 = TO_REND_DATA_V2(client_dup);
  370. tt_int_op(client_dup_v2->auth_type, OP_EQ, client_v2->auth_type);
  371. tt_str_op(client_dup_v2->onion_address, OP_EQ, client_v2->onion_address);
  372. tt_mem_op(client_dup_v2->desc_id_fetch, OP_EQ, client_v2->desc_id_fetch,
  373. sizeof(client_dup_v2->desc_id_fetch));
  374. tt_mem_op(client_dup_v2->descriptor_cookie, OP_EQ,
  375. client_v2->descriptor_cookie,
  376. sizeof(client_dup_v2->descriptor_cookie));
  377. tt_assert(client_dup->hsdirs_fp);
  378. tt_int_op(smartlist_len(client_dup->hsdirs_fp), OP_EQ, 0);
  379. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  380. tt_mem_op(client_dup_v2->descriptor_id[rep], OP_EQ,
  381. client_v2->descriptor_id[rep], DIGEST_LEN);
  382. }
  383. /* The rest should be zeroed because this is a client request. */
  384. tt_int_op(tor_digest_is_zero(client_dup_v2->rend_pk_digest), OP_EQ, 1);
  385. tt_int_op(tor_digest_is_zero(client_dup->rend_cookie), OP_EQ, 1);
  386. rend_data_free(client);
  387. client = NULL;
  388. rend_data_free(client_dup);
  389. client_dup = NULL;
  390. /* Reset state. */
  391. base32_decode(desc_id, sizeof(desc_id), STR_DESC_ID_BASE32,
  392. REND_DESC_ID_V2_LEN_BASE32);
  393. memset(client_cookie, 'e', sizeof(client_cookie));
  394. /* Try with different parameters here for which some content should be
  395. * zeroed out. */
  396. client = rend_data_client_create(NULL, desc_id, NULL, REND_BASIC_AUTH);
  397. tt_assert(client);
  398. client_v2 = TO_REND_DATA_V2(client);
  399. tt_int_op(client_v2->auth_type, OP_EQ, REND_BASIC_AUTH);
  400. tt_int_op(strlen(client_v2->onion_address), OP_EQ, 0);
  401. tt_mem_op(client_v2->desc_id_fetch, OP_EQ, desc_id, sizeof(desc_id));
  402. tt_int_op(tor_mem_is_zero(client_v2->descriptor_cookie,
  403. sizeof(client_v2->descriptor_cookie)), OP_EQ, 1);
  404. tt_assert(client->hsdirs_fp);
  405. tt_int_op(smartlist_len(client->hsdirs_fp), OP_EQ, 0);
  406. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  407. tt_int_op(tor_digest_is_zero(client_v2->descriptor_id[rep]), OP_EQ, 1);
  408. }
  409. /* The rest should be zeroed because this is a client request. */
  410. tt_int_op(tor_digest_is_zero(client_v2->rend_pk_digest), OP_EQ, 1);
  411. tt_int_op(tor_digest_is_zero(client->rend_cookie), OP_EQ, 1);
  412. rend_data_free(client);
  413. client = NULL;
  414. /* Let's test the service object now. */
  415. char rend_pk_digest[DIGEST_LEN];
  416. uint8_t rend_cookie[DIGEST_LEN];
  417. memset(rend_pk_digest, 'f', sizeof(rend_pk_digest));
  418. memset(rend_cookie, 'g', sizeof(rend_cookie));
  419. service = rend_data_service_create(STR_HS_ADDR, rend_pk_digest,
  420. rend_cookie, REND_NO_AUTH);
  421. tt_assert(service);
  422. rend_data_v2_t *service_v2 = TO_REND_DATA_V2(service);
  423. tt_int_op(service_v2->auth_type, OP_EQ, REND_NO_AUTH);
  424. tt_str_op(service_v2->onion_address, OP_EQ, STR_HS_ADDR);
  425. tt_mem_op(service_v2->rend_pk_digest, OP_EQ, rend_pk_digest,
  426. sizeof(rend_pk_digest));
  427. tt_mem_op(service->rend_cookie, OP_EQ, rend_cookie, sizeof(rend_cookie));
  428. tt_assert(service->hsdirs_fp);
  429. tt_int_op(smartlist_len(service->hsdirs_fp), OP_EQ, 0);
  430. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  431. tt_int_op(tor_digest_is_zero(service_v2->descriptor_id[rep]), OP_EQ, 1);
  432. }
  433. /* The rest should be zeroed because this is a service request. */
  434. tt_int_op(tor_digest_is_zero(service_v2->descriptor_cookie), OP_EQ, 1);
  435. tt_int_op(tor_digest_is_zero(service_v2->desc_id_fetch), OP_EQ, 1);
  436. /* Test dup(). */
  437. service_dup = rend_data_dup(service);
  438. rend_data_v2_t *service_dup_v2 = TO_REND_DATA_V2(service_dup);
  439. tt_assert(service_dup);
  440. tt_int_op(service_dup_v2->auth_type, OP_EQ, service_v2->auth_type);
  441. tt_str_op(service_dup_v2->onion_address, OP_EQ, service_v2->onion_address);
  442. tt_mem_op(service_dup_v2->rend_pk_digest, OP_EQ, service_v2->rend_pk_digest,
  443. sizeof(service_dup_v2->rend_pk_digest));
  444. tt_mem_op(service_dup->rend_cookie, OP_EQ, service->rend_cookie,
  445. sizeof(service_dup->rend_cookie));
  446. tt_assert(service_dup->hsdirs_fp);
  447. tt_int_op(smartlist_len(service_dup->hsdirs_fp), OP_EQ, 0);
  448. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  449. tt_assert(tor_digest_is_zero(service_dup_v2->descriptor_id[rep]));
  450. }
  451. /* The rest should be zeroed because this is a service request. */
  452. tt_int_op(tor_digest_is_zero(service_dup_v2->descriptor_cookie), OP_EQ, 1);
  453. tt_int_op(tor_digest_is_zero(service_dup_v2->desc_id_fetch), OP_EQ, 1);
  454. done:
  455. rend_data_free(service);
  456. rend_data_free(service_dup);
  457. rend_data_free(client);
  458. rend_data_free(client_dup);
  459. }
  460. /* Test encoding and decoding service authorization cookies */
  461. static void
  462. test_hs_auth_cookies(void *arg)
  463. {
  464. #define TEST_COOKIE_RAW ((const uint8_t *) "abcdefghijklmnop")
  465. #define TEST_COOKIE_ENCODED "YWJjZGVmZ2hpamtsbW5vcA"
  466. #define TEST_COOKIE_ENCODED_STEALTH "YWJjZGVmZ2hpamtsbW5vcB"
  467. #define TEST_COOKIE_ENCODED_INVALID "YWJjZGVmZ2hpamtsbW5vcD"
  468. char *encoded_cookie = NULL;
  469. uint8_t raw_cookie[REND_DESC_COOKIE_LEN];
  470. rend_auth_type_t auth_type;
  471. char *err_msg = NULL;
  472. int re;
  473. (void)arg;
  474. /* Test that encoding gives the expected result */
  475. encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_BASIC_AUTH);
  476. tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED);
  477. tor_free(encoded_cookie);
  478. encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_STEALTH_AUTH);
  479. tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED_STEALTH);
  480. tor_free(encoded_cookie);
  481. /* Decoding should give the original value */
  482. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED, raw_cookie, &auth_type,
  483. &err_msg);
  484. tt_assert(!re);
  485. tt_ptr_op(err_msg, OP_EQ, NULL);
  486. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  487. tt_int_op(auth_type, OP_EQ, REND_BASIC_AUTH);
  488. memset(raw_cookie, 0, sizeof(raw_cookie));
  489. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_STEALTH, raw_cookie,
  490. &auth_type, &err_msg);
  491. tt_assert(!re);
  492. tt_ptr_op(err_msg, OP_EQ, NULL);
  493. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  494. tt_int_op(auth_type, OP_EQ, REND_STEALTH_AUTH);
  495. memset(raw_cookie, 0, sizeof(raw_cookie));
  496. /* Decoding with padding characters should also work */
  497. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED "==", raw_cookie, NULL,
  498. &err_msg);
  499. tt_assert(!re);
  500. tt_ptr_op(err_msg, OP_EQ, NULL);
  501. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  502. /* Decoding with an unknown type should fail */
  503. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_INVALID, raw_cookie,
  504. &auth_type, &err_msg);
  505. tt_int_op(re, OP_LT, 0);
  506. tt_assert(err_msg);
  507. tor_free(err_msg);
  508. done:
  509. tor_free(encoded_cookie);
  510. tor_free(err_msg);
  511. return;
  512. }
  513. static int mock_get_options_calls = 0;
  514. static or_options_t *mock_options = NULL;
  515. static void
  516. reset_options(or_options_t *options, int *get_options_calls)
  517. {
  518. memset(options, 0, sizeof(or_options_t));
  519. options->TestingTorNetwork = 1;
  520. *get_options_calls = 0;
  521. }
  522. static const or_options_t *
  523. mock_get_options(void)
  524. {
  525. ++mock_get_options_calls;
  526. tor_assert(mock_options);
  527. return mock_options;
  528. }
  529. /* arg can't be 0 (the test fails) or 2 (the test is skipped) */
  530. #define CREATE_HS_DIR_NONE ((intptr_t)0x04)
  531. #define CREATE_HS_DIR1 ((intptr_t)0x08)
  532. #define CREATE_HS_DIR2 ((intptr_t)0x10)
  533. /* Test that single onion poisoning works. */
  534. static void
  535. test_single_onion_poisoning(void *arg)
  536. {
  537. or_options_t opt;
  538. mock_options = &opt;
  539. reset_options(mock_options, &mock_get_options_calls);
  540. MOCK(get_options, mock_get_options);
  541. int ret = -1;
  542. intptr_t create_dir_mask = (intptr_t)arg;
  543. /* Get directories with a random suffix so we can repeat the tests */
  544. mock_options->DataDirectory = tor_strdup(get_fname_rnd("test_data_dir"));
  545. rend_service_t *service_1 = tor_malloc_zero(sizeof(rend_service_t));
  546. char *dir1 = tor_strdup(get_fname_rnd("test_hs_dir1"));
  547. rend_service_t *service_2 = tor_malloc_zero(sizeof(rend_service_t));
  548. char *dir2 = tor_strdup(get_fname_rnd("test_hs_dir2"));
  549. smartlist_t *services = smartlist_new();
  550. char *poison_path = NULL;
  551. char *err_msg = NULL;
  552. mock_options->HiddenServiceSingleHopMode = 1;
  553. mock_options->HiddenServiceNonAnonymousMode = 1;
  554. /* Create the data directory, and, if the correct bit in arg is set,
  555. * create a directory for that service.
  556. * The data directory is required for the lockfile, which is used when
  557. * loading keys. */
  558. ret = check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
  559. tt_int_op(ret, OP_EQ, 0);
  560. if (create_dir_mask & CREATE_HS_DIR1) {
  561. ret = check_private_dir(dir1, CPD_CREATE, NULL);
  562. tt_int_op(ret, OP_EQ, 0);
  563. }
  564. if (create_dir_mask & CREATE_HS_DIR2) {
  565. ret = check_private_dir(dir2, CPD_CREATE, NULL);
  566. tt_int_op(ret, OP_EQ, 0);
  567. }
  568. service_1->directory = dir1;
  569. service_2->directory = dir2;
  570. /* The services own the directory pointers now */
  571. dir1 = dir2 = NULL;
  572. /* Add port to service 1 */
  573. service_1->ports = smartlist_new();
  574. service_2->ports = smartlist_new();
  575. rend_service_port_config_t *port1 = rend_service_parse_port_config("80", " ",
  576. &err_msg);
  577. tt_assert(port1);
  578. tt_ptr_op(err_msg, OP_EQ, NULL);
  579. smartlist_add(service_1->ports, port1);
  580. rend_service_port_config_t *port2 = rend_service_parse_port_config("90", " ",
  581. &err_msg);
  582. /* Add port to service 2 */
  583. tt_assert(port2);
  584. tt_ptr_op(err_msg, OP_EQ, NULL);
  585. smartlist_add(service_2->ports, port2);
  586. /* No services, a service to verify, no problem! */
  587. mock_options->HiddenServiceSingleHopMode = 0;
  588. mock_options->HiddenServiceNonAnonymousMode = 0;
  589. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  590. tt_int_op(ret, OP_EQ, 0);
  591. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  592. tt_int_op(ret, OP_EQ, 0);
  593. /* Either way, no problem. */
  594. mock_options->HiddenServiceSingleHopMode = 1;
  595. mock_options->HiddenServiceNonAnonymousMode = 1;
  596. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  597. tt_int_op(ret, OP_EQ, 0);
  598. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  599. tt_int_op(ret, OP_EQ, 0);
  600. /* Add the first service */
  601. ret = hs_check_service_private_dir(mock_options->User, service_1->directory,
  602. service_1->dir_group_readable, 1);
  603. tt_int_op(ret, OP_EQ, 0);
  604. smartlist_add(services, service_1);
  605. /* But don't add the second service yet. */
  606. /* Service directories, but no previous keys, no problem! */
  607. mock_options->HiddenServiceSingleHopMode = 0;
  608. mock_options->HiddenServiceNonAnonymousMode = 0;
  609. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  610. tt_int_op(ret, OP_EQ, 0);
  611. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  612. tt_int_op(ret, OP_EQ, 0);
  613. /* Either way, no problem. */
  614. mock_options->HiddenServiceSingleHopMode = 1;
  615. mock_options->HiddenServiceNonAnonymousMode = 1;
  616. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  617. tt_int_op(ret, OP_EQ, 0);
  618. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  619. tt_int_op(ret, OP_EQ, 0);
  620. /* Poison! Poison! Poison!
  621. * This can only be done in HiddenServiceSingleHopMode. */
  622. mock_options->HiddenServiceSingleHopMode = 1;
  623. mock_options->HiddenServiceNonAnonymousMode = 1;
  624. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  625. tt_int_op(ret, OP_EQ, 0);
  626. /* Poisoning twice is a no-op. */
  627. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  628. tt_int_op(ret, OP_EQ, 0);
  629. /* Poisoned service directories, but no previous keys, no problem! */
  630. mock_options->HiddenServiceSingleHopMode = 0;
  631. mock_options->HiddenServiceNonAnonymousMode = 0;
  632. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  633. tt_int_op(ret, OP_EQ, 0);
  634. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  635. tt_int_op(ret, OP_EQ, 0);
  636. /* Either way, no problem. */
  637. mock_options->HiddenServiceSingleHopMode = 1;
  638. mock_options->HiddenServiceNonAnonymousMode = 1;
  639. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  640. tt_int_op(ret, OP_EQ, 0);
  641. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  642. tt_int_op(ret, OP_EQ, 0);
  643. /* Now add some keys, and we'll have a problem. */
  644. ret = rend_service_load_all_keys(services);
  645. tt_int_op(ret, OP_EQ, 0);
  646. /* Poisoned service directories with previous keys are not allowed. */
  647. mock_options->HiddenServiceSingleHopMode = 0;
  648. mock_options->HiddenServiceNonAnonymousMode = 0;
  649. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  650. tt_int_op(ret, OP_LT, 0);
  651. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  652. tt_int_op(ret, OP_EQ, 0);
  653. /* But they are allowed if we're in non-anonymous mode. */
  654. mock_options->HiddenServiceSingleHopMode = 1;
  655. mock_options->HiddenServiceNonAnonymousMode = 1;
  656. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  657. tt_int_op(ret, OP_EQ, 0);
  658. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  659. tt_int_op(ret, OP_EQ, 0);
  660. /* Re-poisoning directories with existing keys is a no-op, because
  661. * directories with existing keys are ignored. */
  662. mock_options->HiddenServiceSingleHopMode = 1;
  663. mock_options->HiddenServiceNonAnonymousMode = 1;
  664. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  665. tt_int_op(ret, OP_EQ, 0);
  666. /* And it keeps the poison. */
  667. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  668. tt_int_op(ret, OP_EQ, 0);
  669. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  670. tt_int_op(ret, OP_EQ, 0);
  671. /* Now add the second service: it has no key and no poison file */
  672. ret = hs_check_service_private_dir(mock_options->User, service_2->directory,
  673. service_2->dir_group_readable, 1);
  674. tt_int_op(ret, OP_EQ, 0);
  675. smartlist_add(services, service_2);
  676. /* A new service, and an existing poisoned service. Not ok. */
  677. mock_options->HiddenServiceSingleHopMode = 0;
  678. mock_options->HiddenServiceNonAnonymousMode = 0;
  679. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  680. tt_int_op(ret, OP_LT, 0);
  681. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  682. tt_int_op(ret, OP_EQ, 0);
  683. /* But ok to add in non-anonymous mode. */
  684. mock_options->HiddenServiceSingleHopMode = 1;
  685. mock_options->HiddenServiceNonAnonymousMode = 1;
  686. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  687. tt_int_op(ret, OP_EQ, 0);
  688. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  689. tt_int_op(ret, OP_EQ, 0);
  690. /* Now remove the poisoning from the first service, and we have the opposite
  691. * problem. */
  692. poison_path = rend_service_sos_poison_path(service_1);
  693. tt_assert(poison_path);
  694. ret = unlink(poison_path);
  695. tt_int_op(ret, OP_EQ, 0);
  696. /* Unpoisoned service directories with previous keys are ok, as are empty
  697. * directories. */
  698. mock_options->HiddenServiceSingleHopMode = 0;
  699. mock_options->HiddenServiceNonAnonymousMode = 0;
  700. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  701. tt_int_op(ret, OP_EQ, 0);
  702. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  703. tt_int_op(ret, OP_EQ, 0);
  704. /* But the existing unpoisoned key is not ok in non-anonymous mode, even if
  705. * there is an empty service. */
  706. mock_options->HiddenServiceSingleHopMode = 1;
  707. mock_options->HiddenServiceNonAnonymousMode = 1;
  708. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  709. tt_int_op(ret, OP_LT, 0);
  710. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  711. tt_int_op(ret, OP_EQ, 0);
  712. /* Poisoning directories with existing keys is a no-op, because directories
  713. * with existing keys are ignored. But the new directory should poison. */
  714. mock_options->HiddenServiceSingleHopMode = 1;
  715. mock_options->HiddenServiceNonAnonymousMode = 1;
  716. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  717. tt_int_op(ret, OP_EQ, 0);
  718. ret = rend_service_poison_new_single_onion_dir(service_2, mock_options);
  719. tt_int_op(ret, OP_EQ, 0);
  720. /* And the old directory remains unpoisoned. */
  721. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  722. tt_int_op(ret, OP_LT, 0);
  723. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  724. tt_int_op(ret, OP_EQ, 0);
  725. /* And the new directory should be ignored, because it has no key. */
  726. mock_options->HiddenServiceSingleHopMode = 0;
  727. mock_options->HiddenServiceNonAnonymousMode = 0;
  728. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  729. tt_int_op(ret, OP_EQ, 0);
  730. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  731. tt_int_op(ret, OP_EQ, 0);
  732. /* Re-poisoning directories without existing keys is a no-op. */
  733. mock_options->HiddenServiceSingleHopMode = 1;
  734. mock_options->HiddenServiceNonAnonymousMode = 1;
  735. ret = rend_service_poison_new_single_onion_dir(service_1, mock_options);
  736. tt_int_op(ret, OP_EQ, 0);
  737. ret = rend_service_poison_new_single_onion_dir(service_2, mock_options);
  738. tt_int_op(ret, OP_EQ, 0);
  739. /* And the old directory remains unpoisoned. */
  740. ret = rend_service_verify_single_onion_poison(service_1, mock_options);
  741. tt_int_op(ret, OP_LT, 0);
  742. ret = rend_service_verify_single_onion_poison(service_2, mock_options);
  743. tt_int_op(ret, OP_EQ, 0);
  744. done:
  745. /* The test harness deletes the directories at exit */
  746. tor_free(poison_path);
  747. tor_free(dir1);
  748. tor_free(dir2);
  749. smartlist_free(services);
  750. rend_service_free(service_1);
  751. rend_service_free(service_2);
  752. UNMOCK(get_options);
  753. tor_free(mock_options->DataDirectory);
  754. tor_free(err_msg);
  755. }
  756. static rend_service_t *
  757. helper_create_rend_service(const char *path)
  758. {
  759. rend_service_t *s = tor_malloc_zero(sizeof(rend_service_t));
  760. s->ports = smartlist_new();
  761. s->intro_nodes = smartlist_new();
  762. s->expiring_nodes = smartlist_new();
  763. if (path) {
  764. s->directory = tor_strdup(path);
  765. }
  766. return s;
  767. }
  768. static void
  769. test_prune_services_on_reload(void *arg)
  770. {
  771. smartlist_t *new = smartlist_new(), *old = smartlist_new();
  772. /* Non ephemeral service. */
  773. rend_service_t *s1 = helper_create_rend_service("SomePath");
  774. /* Create a non ephemeral service with the _same_ path as so we can test the
  775. * transfer of introduction point between the same services on reload. */
  776. rend_service_t *s2 = helper_create_rend_service(s1->directory);
  777. /* Ephemeral service (directory is NULL). */
  778. rend_service_t *e1 = helper_create_rend_service(NULL);
  779. rend_service_t *e2 = helper_create_rend_service(NULL);
  780. (void) arg;
  781. {
  782. /* Add both services to the old list. */
  783. smartlist_add(old, s1);
  784. smartlist_add(old, e1);
  785. /* Only put the non ephemeral in the new list. */
  786. smartlist_add(new, s1);
  787. set_rend_service_list(old);
  788. set_rend_rend_service_staging_list(new);
  789. rend_service_prune_list_impl_();
  790. /* We expect that the ephemeral one is in the new list but removed from
  791. * the old one. */
  792. tt_int_op(smartlist_len(old), OP_EQ, 1);
  793. tt_assert(smartlist_get(old, 0) == s1);
  794. tt_int_op(smartlist_len(new), OP_EQ, 2);
  795. tt_assert(smartlist_get(new, 0) == s1);
  796. tt_assert(smartlist_get(new, 1) == e1);
  797. /* Cleanup for next test. */
  798. smartlist_clear(new);
  799. smartlist_clear(old);
  800. }
  801. {
  802. /* This test will make sure that only the ephemeral service is kept if the
  803. * new list is empty. The old list should contain only the non ephemeral
  804. * one. */
  805. smartlist_add(old, s1);
  806. smartlist_add(old, e1);
  807. set_rend_service_list(old);
  808. set_rend_rend_service_staging_list(new);
  809. rend_service_prune_list_impl_();
  810. tt_int_op(smartlist_len(old), OP_EQ, 1);
  811. tt_assert(smartlist_get(old, 0) == s1);
  812. tt_int_op(smartlist_len(new), OP_EQ, 1);
  813. tt_assert(smartlist_get(new, 0) == e1);
  814. /* Cleanup for next test. */
  815. smartlist_clear(new);
  816. smartlist_clear(old);
  817. }
  818. {
  819. /* This test makes sure that the new list stays the same even from the old
  820. * list being completely different. */
  821. smartlist_add(new, s1);
  822. smartlist_add(new, e1);
  823. set_rend_service_list(old);
  824. set_rend_rend_service_staging_list(new);
  825. rend_service_prune_list_impl_();
  826. tt_int_op(smartlist_len(old), OP_EQ, 0);
  827. tt_int_op(smartlist_len(new), OP_EQ, 2);
  828. tt_assert(smartlist_get(new, 0) == s1);
  829. tt_assert(smartlist_get(new, 1) == e1);
  830. /* Cleanup for next test. */
  831. smartlist_clear(new);
  832. }
  833. {
  834. rend_intro_point_t ip1;
  835. /* This IP should be found in the s2 service after pruning. */
  836. smartlist_add(s1->intro_nodes, &ip1);
  837. /* Setup our list. */
  838. smartlist_add(old, s1);
  839. smartlist_add(new, s2);
  840. set_rend_service_list(old);
  841. set_rend_rend_service_staging_list(new);
  842. rend_service_prune_list_impl_();
  843. tt_int_op(smartlist_len(old), OP_EQ, 1);
  844. /* Intro nodes have been moved to the s2 in theory so it must be empty. */
  845. tt_int_op(smartlist_len(s1->intro_nodes), OP_EQ, 0);
  846. tt_int_op(smartlist_len(new), OP_EQ, 1);
  847. rend_service_t *elem = smartlist_get(new, 0);
  848. tt_assert(elem);
  849. tt_assert(elem == s2);
  850. tt_int_op(smartlist_len(elem->intro_nodes), OP_EQ, 1);
  851. tt_assert(smartlist_get(elem->intro_nodes, 0) == &ip1);
  852. smartlist_clear(s1->intro_nodes);
  853. smartlist_clear(s2->intro_nodes);
  854. /* Cleanup for next test. */
  855. smartlist_clear(new);
  856. smartlist_clear(old);
  857. }
  858. {
  859. /* Test two ephemeral services. */
  860. smartlist_add(old, e1);
  861. smartlist_add(old, e2);
  862. set_rend_service_list(old);
  863. set_rend_rend_service_staging_list(new);
  864. rend_service_prune_list_impl_();
  865. /* Check if they've all been transferred. */
  866. tt_int_op(smartlist_len(old), OP_EQ, 0);
  867. tt_int_op(smartlist_len(new), OP_EQ, 2);
  868. }
  869. done:
  870. rend_service_free(s1);
  871. rend_service_free(s2);
  872. rend_service_free(e1);
  873. rend_service_free(e2);
  874. smartlist_free(new);
  875. smartlist_free(old);
  876. }
  877. struct testcase_t hs_tests[] = {
  878. { "hs_rend_data", test_hs_rend_data, TT_FORK,
  879. NULL, NULL },
  880. { "hs_parse_static_v2_desc", test_hs_parse_static_v2_desc, TT_FORK,
  881. NULL, NULL },
  882. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  883. NULL, NULL },
  884. { "hs_auth_cookies", test_hs_auth_cookies, TT_FORK,
  885. NULL, NULL },
  886. { "single_onion_poisoning_create_dir_none", test_single_onion_poisoning,
  887. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR_NONE) },
  888. { "single_onion_poisoning_create_dir1", test_single_onion_poisoning,
  889. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR1) },
  890. { "single_onion_poisoning_create_dir2", test_single_onion_poisoning,
  891. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR2) },
  892. { "single_onion_poisoning_create_dir_both", test_single_onion_poisoning,
  893. TT_FORK, &passthrough_setup, (void*)(CREATE_HS_DIR1 | CREATE_HS_DIR2) },
  894. { "prune_services_on_reload", test_prune_services_on_reload, TT_FORK,
  895. NULL, NULL },
  896. END_OF_TESTCASES
  897. };