test_hs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* Copyright (c) 2007-2016, 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. #include "or.h"
  10. #include "test.h"
  11. #include "control.h"
  12. #include "config.h"
  13. #include "hs_common.h"
  14. #include "rendcommon.h"
  15. #include "routerset.h"
  16. #include "circuitbuild.h"
  17. #include "test_helpers.h"
  18. /* mock ID digest and longname for node that's in nodelist */
  19. #define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
  20. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  21. #define STR_HSDIR_EXIST_LONGNAME \
  22. "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
  23. /* mock ID digest and longname for node that's not in nodelist */
  24. #define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
  25. "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
  26. #define STR_HSDIR_NONE_EXIST_LONGNAME \
  27. "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  28. /* DuckDuckGo descriptor as an example. */
  29. static const char *hs_desc_content = "\
  30. rendezvous-service-descriptor g5ojobzupf275beh5ra72uyhb3dkpxwg\r\n\
  31. version 2\r\n\
  32. permanent-key\r\n\
  33. -----BEGIN RSA PUBLIC KEY-----\r\n\
  34. MIGJAoGBAJ/SzzgrXPxTlFrKVhXh3buCWv2QfcNgncUpDpKouLn3AtPH5Ocys0jE\r\n\
  35. aZSKdvaiQ62md2gOwj4x61cFNdi05tdQjS+2thHKEm/KsB9BGLSLBNJYY356bupg\r\n\
  36. I5gQozM65ENelfxYlysBjJ52xSDBd8C4f/p9umdzaaaCmzXG/nhzAgMBAAE=\r\n\
  37. -----END RSA PUBLIC KEY-----\r\n\
  38. secret-id-part anmjoxxwiupreyajjt5yasimfmwcnxlf\r\n\
  39. publication-time 2015-03-11 19:00:00\r\n\
  40. protocol-versions 2,3\r\n\
  41. introduction-points\r\n\
  42. -----BEGIN MESSAGE-----\r\n\
  43. aW50cm9kdWN0aW9uLXBvaW50IDd1bnd4cmg2dG5kNGh6eWt1Z3EzaGZzdHduc2ll\r\n\
  44. cmhyCmlwLWFkZHJlc3MgMTg4LjEzOC4xMjEuMTE4Cm9uaW9uLXBvcnQgOTAwMQpv\r\n\
  45. bmlvbi1rZXkKLS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1JR0pBb0dC\r\n\
  46. QUxGRVVyeVpDbk9ROEhURmV5cDVjMTRObWVqL1BhekFLTTBxRENTNElKUWh0Y3g1\r\n\
  47. NXpRSFdOVWIKQ2hHZ0JqR1RjV3ZGRnA0N3FkdGF6WUZhVXE2c0lQKzVqeWZ5b0Q4\r\n\
  48. UmJ1bzB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N\r\n\
  77. SUdKQW9HQkFMVjNKSmtWN3lTNU9jc1lHMHNFYzFQOTVRclFRR3ZzbGJ6Wi9zRGxl\r\n\
  78. RlpKYXFSOUYvYjRUVERNClNGcFMxcU1GbldkZDgxVmRGMEdYRmN2WVpLamRJdHU2\r\n\
  79. SndBaTRJeEhxeXZtdTRKdUxrcXNaTEFLaXRLVkx4eGsKeERlMjlDNzRWMmJrOTRJ\r\n\
  80. MEgybTNKS2tzTHVwc3VxWWRVUmhOVXN0SElKZmgyZmNIalF0bEFnTUJBQUU9Ci0t\r\n\
  81. LS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0KCg==\r\n\
  82. -----END MESSAGE-----\r\n\
  83. signature\r\n\
  84. -----BEGIN SIGNATURE-----\r\n\
  85. d4OuCE5OLAOnRB6cQN6WyMEmg/BHem144Vec+eYgeWoKwx3MxXFplUjFxgnMlmwN\r\n\
  86. PcftsZf2ztN0sbNCtPgDL3d0PqvxY3iHTQAI8EbaGq/IAJUZ8U4y963dD5+Bn6JQ\r\n\
  87. myE3ctmh0vy5+QxSiRjmQBkuEpCyks7LvWvHYrhnmcg=\r\n\
  88. -----END SIGNATURE-----";
  89. /* Helper global variable for hidden service descriptor event test.
  90. * It's used as a pointer to dynamically created message buffer in
  91. * send_control_event_string_replacement function, which mocks
  92. * send_control_event_string function.
  93. *
  94. * Always free it after use! */
  95. static char *received_msg = NULL;
  96. /** Mock function for send_control_event_string
  97. */
  98. static void
  99. queue_control_event_string_replacement(uint16_t event, char *msg)
  100. {
  101. (void) event;
  102. tor_free(received_msg);
  103. received_msg = msg;
  104. }
  105. /** Mock function for node_describe_longname_by_id, it returns either
  106. * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
  107. */
  108. static const char *
  109. node_describe_longname_by_id_replacement(const char *id_digest)
  110. {
  111. if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
  112. return STR_HSDIR_EXIST_LONGNAME;
  113. } else {
  114. return STR_HSDIR_NONE_EXIST_LONGNAME;
  115. }
  116. }
  117. /** Make sure each hidden service descriptor async event generation
  118. *
  119. * function generates the message in expected format.
  120. */
  121. static void
  122. test_hs_desc_event(void *arg)
  123. {
  124. #define STR_HS_ADDR "ajhb7kljbiru65qo"
  125. #define STR_HS_CONTENT_DESC_ID "g5ojobzupf275beh5ra72uyhb3dkpxwg"
  126. #define STR_DESC_ID_BASE32 "hba3gmcgpfivzfhx5rtfqkfdhv65yrj3"
  127. int ret;
  128. rend_data_v2_t rend_query;
  129. const char *expected_msg;
  130. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  131. (void) arg;
  132. MOCK(queue_control_event_string,
  133. queue_control_event_string_replacement);
  134. MOCK(node_describe_longname_by_id,
  135. node_describe_longname_by_id_replacement);
  136. /* setup rend_query struct */
  137. memset(&rend_query, 0, sizeof(rend_query));
  138. rend_query.base_.version = 2;
  139. strncpy(rend_query.onion_address, STR_HS_ADDR,
  140. REND_SERVICE_ID_LEN_BASE32+1);
  141. rend_query.auth_type = REND_NO_AUTH;
  142. rend_query.base_.hsdirs_fp = smartlist_new();
  143. smartlist_add(rend_query.base_.hsdirs_fp, tor_memdup(HSDIR_EXIST_ID,
  144. DIGEST_LEN));
  145. /* Compute descriptor ID for replica 0, should be STR_DESC_ID_BASE32. */
  146. ret = rend_compute_v2_desc_id(rend_query.descriptor_id[0],
  147. rend_query.onion_address,
  148. NULL, 0, 0);
  149. tt_int_op(ret, ==, 0);
  150. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  151. rend_query.descriptor_id[0], DIGEST_LEN);
  152. /* Make sure rend_compute_v2_desc_id works properly. */
  153. tt_mem_op(desc_id_base32, OP_EQ, STR_DESC_ID_BASE32,
  154. sizeof(desc_id_base32));
  155. /* test request event */
  156. control_event_hs_descriptor_requested(&rend_query.base_, HSDIR_EXIST_ID,
  157. STR_DESC_ID_BASE32);
  158. expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
  159. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32 "\r\n";
  160. tt_assert(received_msg);
  161. tt_str_op(received_msg,OP_EQ, expected_msg);
  162. tor_free(received_msg);
  163. /* test received event */
  164. rend_query.auth_type = REND_BASIC_AUTH;
  165. control_event_hs_descriptor_received(rend_query.onion_address,
  166. &rend_query.base_, HSDIR_EXIST_ID);
  167. expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
  168. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32"\r\n";
  169. tt_assert(received_msg);
  170. tt_str_op(received_msg,OP_EQ, expected_msg);
  171. tor_free(received_msg);
  172. /* test failed event */
  173. rend_query.auth_type = REND_STEALTH_AUTH;
  174. control_event_hs_descriptor_failed(&rend_query.base_,
  175. HSDIR_NONE_EXIST_ID,
  176. "QUERY_REJECTED");
  177. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
  178. STR_HSDIR_NONE_EXIST_LONGNAME" REASON=QUERY_REJECTED\r\n";
  179. tt_assert(received_msg);
  180. tt_str_op(received_msg,OP_EQ, expected_msg);
  181. tor_free(received_msg);
  182. /* test invalid auth type */
  183. rend_query.auth_type = 999;
  184. control_event_hs_descriptor_failed(&rend_query.base_,
  185. HSDIR_EXIST_ID,
  186. "QUERY_REJECTED");
  187. expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
  188. STR_HSDIR_EXIST_LONGNAME " " STR_DESC_ID_BASE32\
  189. " REASON=QUERY_REJECTED\r\n";
  190. tt_assert(received_msg);
  191. tt_str_op(received_msg,OP_EQ, expected_msg);
  192. tor_free(received_msg);
  193. /* test valid content. */
  194. char *exp_msg;
  195. control_event_hs_descriptor_content(rend_query.onion_address,
  196. STR_HS_CONTENT_DESC_ID, HSDIR_EXIST_ID,
  197. hs_desc_content);
  198. tor_asprintf(&exp_msg, "650+HS_DESC_CONTENT " STR_HS_ADDR " "\
  199. STR_HS_CONTENT_DESC_ID " " STR_HSDIR_EXIST_LONGNAME\
  200. "\r\n%s\r\n.\r\n650 OK\r\n", hs_desc_content);
  201. tt_assert(received_msg);
  202. tt_str_op(received_msg, OP_EQ, exp_msg);
  203. tor_free(received_msg);
  204. tor_free(exp_msg);
  205. SMARTLIST_FOREACH(rend_query.base_.hsdirs_fp, char *, d, tor_free(d));
  206. smartlist_free(rend_query.base_.hsdirs_fp);
  207. done:
  208. UNMOCK(queue_control_event_string);
  209. UNMOCK(node_describe_longname_by_id);
  210. tor_free(received_msg);
  211. }
  212. /* Make sure we always pick the right RP, given a well formatted
  213. * Tor2webRendezvousPoints value. */
  214. static void
  215. test_pick_tor2web_rendezvous_node(void *arg)
  216. {
  217. or_options_t *options = get_options_mutable();
  218. const node_t *chosen_rp = NULL;
  219. router_crn_flags_t flags = CRN_NEED_DESC;
  220. int retval, i;
  221. const char *tor2web_rendezvous_str = "test003r";
  222. (void) arg;
  223. /* Setup fake routerlist. */
  224. helper_setup_fake_routerlist();
  225. /* Parse Tor2webRendezvousPoints as a routerset. */
  226. options->Tor2webRendezvousPoints = routerset_new();
  227. retval = routerset_parse(options->Tor2webRendezvousPoints,
  228. tor2web_rendezvous_str,
  229. "test_tor2web_rp");
  230. tt_int_op(retval, >=, 0);
  231. /* Pick rendezvous point. Make sure the correct one is
  232. picked. Repeat many times to make sure it works properly. */
  233. for (i = 0; i < 50 ; i++) {
  234. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  235. tt_assert(chosen_rp);
  236. tt_str_op(chosen_rp->ri->nickname, ==, tor2web_rendezvous_str);
  237. }
  238. done:
  239. routerset_free(options->Tor2webRendezvousPoints);
  240. }
  241. /* Make sure we never pick an RP if Tor2webRendezvousPoints doesn't
  242. * correspond to an actual node. */
  243. static void
  244. test_pick_bad_tor2web_rendezvous_node(void *arg)
  245. {
  246. or_options_t *options = get_options_mutable();
  247. const node_t *chosen_rp = NULL;
  248. router_crn_flags_t flags = CRN_NEED_DESC;
  249. int retval, i;
  250. const char *tor2web_rendezvous_str = "dummy";
  251. (void) arg;
  252. /* Setup fake routerlist. */
  253. helper_setup_fake_routerlist();
  254. /* Parse Tor2webRendezvousPoints as a routerset. */
  255. options->Tor2webRendezvousPoints = routerset_new();
  256. retval = routerset_parse(options->Tor2webRendezvousPoints,
  257. tor2web_rendezvous_str,
  258. "test_tor2web_rp");
  259. tt_int_op(retval, >=, 0);
  260. /* Pick rendezvous point. Since Tor2webRendezvousPoints was set to a
  261. dummy value, we shouldn't find any eligible RPs. */
  262. for (i = 0; i < 50 ; i++) {
  263. chosen_rp = pick_tor2web_rendezvous_node(flags, options);
  264. tt_assert(!chosen_rp);
  265. }
  266. done:
  267. routerset_free(options->Tor2webRendezvousPoints);
  268. }
  269. /* Make sure rend_data_t is valid at creation, destruction and when
  270. * duplicated. */
  271. static void
  272. test_hs_rend_data(void *arg)
  273. {
  274. int rep;
  275. rend_data_t *client = NULL, *client_dup = NULL;
  276. /* Binary format of a descriptor ID. */
  277. char desc_id[DIGEST_LEN];
  278. char client_cookie[REND_DESC_COOKIE_LEN];
  279. time_t now = time(NULL);
  280. rend_data_t *service_dup = NULL;
  281. rend_data_t *service = NULL;
  282. (void)arg;
  283. base32_decode(desc_id, sizeof(desc_id), STR_DESC_ID_BASE32,
  284. REND_DESC_ID_V2_LEN_BASE32);
  285. memset(client_cookie, 'e', sizeof(client_cookie));
  286. client = rend_data_client_create(STR_HS_ADDR, desc_id, client_cookie,
  287. REND_NO_AUTH);
  288. tt_assert(client);
  289. rend_data_v2_t *client_v2 = TO_REND_DATA_V2(client);
  290. tt_int_op(client_v2->auth_type, ==, REND_NO_AUTH);
  291. tt_str_op(client_v2->onion_address, OP_EQ, STR_HS_ADDR);
  292. tt_mem_op(client_v2->desc_id_fetch, OP_EQ, desc_id, sizeof(desc_id));
  293. tt_mem_op(client_v2->descriptor_cookie, OP_EQ, client_cookie,
  294. sizeof(client_cookie));
  295. tt_assert(client->hsdirs_fp);
  296. tt_int_op(smartlist_len(client->hsdirs_fp), ==, 0);
  297. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  298. int ret = rend_compute_v2_desc_id(desc_id, client_v2->onion_address,
  299. client_v2->descriptor_cookie, now, rep);
  300. /* That shouldn't never fail. */
  301. tt_int_op(ret, ==, 0);
  302. tt_mem_op(client_v2->descriptor_id[rep], OP_EQ, desc_id,
  303. sizeof(desc_id));
  304. }
  305. /* The rest should be zeroed because this is a client request. */
  306. tt_int_op(tor_digest_is_zero(client_v2->rend_pk_digest), ==, 1);
  307. tt_int_op(tor_digest_is_zero(client->rend_cookie), ==, 1);
  308. /* Test dup(). */
  309. client_dup = rend_data_dup(client);
  310. tt_assert(client_dup);
  311. rend_data_v2_t *client_dup_v2 = TO_REND_DATA_V2(client_dup);
  312. tt_int_op(client_dup_v2->auth_type, ==, client_v2->auth_type);
  313. tt_str_op(client_dup_v2->onion_address, OP_EQ, client_v2->onion_address);
  314. tt_mem_op(client_dup_v2->desc_id_fetch, OP_EQ, client_v2->desc_id_fetch,
  315. sizeof(client_dup_v2->desc_id_fetch));
  316. tt_mem_op(client_dup_v2->descriptor_cookie, OP_EQ, client_v2->descriptor_cookie,
  317. sizeof(client_dup_v2->descriptor_cookie));
  318. tt_assert(client_dup->hsdirs_fp);
  319. tt_int_op(smartlist_len(client_dup->hsdirs_fp), ==, 0);
  320. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  321. tt_mem_op(client_dup_v2->descriptor_id[rep], OP_EQ,
  322. client_v2->descriptor_id[rep], DIGEST_LEN);
  323. }
  324. /* The rest should be zeroed because this is a client request. */
  325. tt_int_op(tor_digest_is_zero(client_dup_v2->rend_pk_digest), ==, 1);
  326. tt_int_op(tor_digest_is_zero(client_dup->rend_cookie), ==, 1);
  327. rend_data_free(client);
  328. client = NULL;
  329. rend_data_free(client_dup);
  330. client_dup = NULL;
  331. /* Reset state. */
  332. base32_decode(desc_id, sizeof(desc_id), STR_DESC_ID_BASE32,
  333. REND_DESC_ID_V2_LEN_BASE32);
  334. memset(client_cookie, 'e', sizeof(client_cookie));
  335. /* Try with different parameters here for which some content should be
  336. * zeroed out. */
  337. client = rend_data_client_create(NULL, desc_id, NULL, REND_BASIC_AUTH);
  338. tt_assert(client);
  339. client_v2 = TO_REND_DATA_V2(client);
  340. tt_int_op(client_v2->auth_type, ==, REND_BASIC_AUTH);
  341. tt_int_op(strlen(client_v2->onion_address), ==, 0);
  342. tt_mem_op(client_v2->desc_id_fetch, OP_EQ, desc_id, sizeof(desc_id));
  343. tt_int_op(tor_mem_is_zero(client_v2->descriptor_cookie,
  344. sizeof(client_v2->descriptor_cookie)), ==, 1);
  345. tt_assert(client->hsdirs_fp);
  346. tt_int_op(smartlist_len(client->hsdirs_fp), ==, 0);
  347. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  348. tt_int_op(tor_digest_is_zero(client_v2->descriptor_id[rep]), ==, 1);
  349. }
  350. /* The rest should be zeroed because this is a client request. */
  351. tt_int_op(tor_digest_is_zero(client_v2->rend_pk_digest), ==, 1);
  352. tt_int_op(tor_digest_is_zero(client->rend_cookie), ==, 1);
  353. rend_data_free(client);
  354. client = NULL;
  355. /* Let's test the service object now. */
  356. char rend_pk_digest[DIGEST_LEN];
  357. uint8_t rend_cookie[DIGEST_LEN];
  358. memset(rend_pk_digest, 'f', sizeof(rend_pk_digest));
  359. memset(rend_cookie, 'g', sizeof(rend_cookie));
  360. service = rend_data_service_create(STR_HS_ADDR, rend_pk_digest,
  361. rend_cookie, REND_NO_AUTH);
  362. tt_assert(service);
  363. rend_data_v2_t *service_v2 = TO_REND_DATA_V2(service);
  364. tt_int_op(service_v2->auth_type, ==, REND_NO_AUTH);
  365. tt_str_op(service_v2->onion_address, OP_EQ, STR_HS_ADDR);
  366. tt_mem_op(service_v2->rend_pk_digest, OP_EQ, rend_pk_digest,
  367. sizeof(rend_pk_digest));
  368. tt_mem_op(service->rend_cookie, OP_EQ, rend_cookie, sizeof(rend_cookie));
  369. tt_assert(service->hsdirs_fp);
  370. tt_int_op(smartlist_len(service->hsdirs_fp), ==, 0);
  371. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  372. tt_int_op(tor_digest_is_zero(service_v2->descriptor_id[rep]), ==, 1);
  373. }
  374. /* The rest should be zeroed because this is a service request. */
  375. tt_int_op(tor_digest_is_zero(service_v2->descriptor_cookie), ==, 1);
  376. tt_int_op(tor_digest_is_zero(service_v2->desc_id_fetch), ==, 1);
  377. /* Test dup(). */
  378. service_dup = rend_data_dup(service);
  379. rend_data_v2_t *service_dup_v2 = TO_REND_DATA_V2(service_dup);
  380. tt_assert(service_dup);
  381. tt_int_op(service_dup_v2->auth_type, ==, service_v2->auth_type);
  382. tt_str_op(service_dup_v2->onion_address, OP_EQ, service_v2->onion_address);
  383. tt_mem_op(service_dup_v2->rend_pk_digest, OP_EQ, service_v2->rend_pk_digest,
  384. sizeof(service_dup_v2->rend_pk_digest));
  385. tt_mem_op(service_dup->rend_cookie, OP_EQ, service->rend_cookie,
  386. sizeof(service_dup->rend_cookie));
  387. tt_assert(service_dup->hsdirs_fp);
  388. tt_int_op(smartlist_len(service_dup->hsdirs_fp), ==, 0);
  389. for (rep = 0; rep < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS; rep++) {
  390. tt_int_op(tor_digest_is_zero(service_dup_v2->descriptor_id[rep]), ==, 1);
  391. }
  392. /* The rest should be zeroed because this is a service request. */
  393. tt_int_op(tor_digest_is_zero(service_dup_v2->descriptor_cookie), ==, 1);
  394. tt_int_op(tor_digest_is_zero(service_dup_v2->desc_id_fetch), ==, 1);
  395. done:
  396. rend_data_free(service);
  397. rend_data_free(service_dup);
  398. rend_data_free(client);
  399. rend_data_free(client_dup);
  400. }
  401. /* Test encoding and decoding service authorization cookies */
  402. static void
  403. test_hs_auth_cookies(void *arg)
  404. {
  405. #define TEST_COOKIE_RAW ((const uint8_t *) "abcdefghijklmnop")
  406. #define TEST_COOKIE_ENCODED "YWJjZGVmZ2hpamtsbW5vcA"
  407. #define TEST_COOKIE_ENCODED_STEALTH "YWJjZGVmZ2hpamtsbW5vcB"
  408. #define TEST_COOKIE_ENCODED_INVALID "YWJjZGVmZ2hpamtsbW5vcD"
  409. char *encoded_cookie;
  410. uint8_t raw_cookie[REND_DESC_COOKIE_LEN];
  411. rend_auth_type_t auth_type;
  412. char *err_msg;
  413. int re;
  414. (void)arg;
  415. /* Test that encoding gives the expected result */
  416. encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_BASIC_AUTH);
  417. tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED);
  418. tor_free(encoded_cookie);
  419. encoded_cookie = rend_auth_encode_cookie(TEST_COOKIE_RAW, REND_STEALTH_AUTH);
  420. tt_str_op(encoded_cookie, OP_EQ, TEST_COOKIE_ENCODED_STEALTH);
  421. tor_free(encoded_cookie);
  422. /* Decoding should give the original value */
  423. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED, raw_cookie, &auth_type,
  424. &err_msg);
  425. tt_assert(!re);
  426. tt_assert(!err_msg);
  427. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  428. tt_int_op(auth_type, OP_EQ, REND_BASIC_AUTH);
  429. memset(raw_cookie, 0, sizeof(raw_cookie));
  430. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_STEALTH, raw_cookie,
  431. &auth_type, &err_msg);
  432. tt_assert(!re);
  433. tt_assert(!err_msg);
  434. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  435. tt_int_op(auth_type, OP_EQ, REND_STEALTH_AUTH);
  436. memset(raw_cookie, 0, sizeof(raw_cookie));
  437. /* Decoding with padding characters should also work */
  438. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED "==", raw_cookie, NULL,
  439. &err_msg);
  440. tt_assert(!re);
  441. tt_assert(!err_msg);
  442. tt_mem_op(raw_cookie, OP_EQ, TEST_COOKIE_RAW, REND_DESC_COOKIE_LEN);
  443. /* Decoding with an unknown type should fail */
  444. re = rend_auth_decode_cookie(TEST_COOKIE_ENCODED_INVALID, raw_cookie,
  445. &auth_type, &err_msg);
  446. tt_int_op(re, OP_LT, 0);
  447. tt_assert(err_msg);
  448. tor_free(err_msg);
  449. done:
  450. return;
  451. }
  452. struct testcase_t hs_tests[] = {
  453. { "hs_rend_data", test_hs_rend_data, TT_FORK,
  454. NULL, NULL },
  455. { "hs_desc_event", test_hs_desc_event, TT_FORK,
  456. NULL, NULL },
  457. { "pick_tor2web_rendezvous_node", test_pick_tor2web_rendezvous_node, TT_FORK,
  458. NULL, NULL },
  459. { "pick_bad_tor2web_rendezvous_node",
  460. test_pick_bad_tor2web_rendezvous_node, TT_FORK,
  461. NULL, NULL },
  462. { "hs_auth_cookies", test_hs_auth_cookies, TT_FORK,
  463. NULL, NULL },
  464. END_OF_TESTCASES
  465. };