test_hs.c 40 KB

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