test_hs.c 42 KB

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