test_hs.c 37 KB

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