test_hs.c 42 KB

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