test_hs.c 42 KB

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