test_hs.c 42 KB

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