test_hs.c 42 KB

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