test_hs.c 42 KB

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