test_hs.c 42 KB

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