test_hs.c 42 KB

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