test_rendcache.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /* Copyright (c) 2010-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "or.h"
  5. #include "test.h"
  6. #define RENDCACHE_PRIVATE
  7. #include "rendcache.h"
  8. #include "router.h"
  9. #include "routerlist.h"
  10. #include "config.h"
  11. #include "hs_common.h"
  12. #include "rend_test_helpers.h"
  13. #include "log_test_helpers.h"
  14. #define NS_MODULE rend_cache
  15. static const int RECENT_TIME = -10;
  16. static const int TIME_IN_THE_PAST = -(REND_CACHE_MAX_AGE + \
  17. REND_CACHE_MAX_SKEW + 60);
  18. static const int TIME_IN_THE_FUTURE = REND_CACHE_MAX_SKEW + 60;
  19. static void
  20. test_rend_cache_lookup_entry(void *data)
  21. {
  22. int ret;
  23. rend_data_t *mock_rend_query = NULL;
  24. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  25. rend_cache_entry_t *entry = NULL;
  26. rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  27. char *service_id = NULL;
  28. (void)data;
  29. rend_cache_init();
  30. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  31. ret = rend_cache_lookup_entry("abababababababab", 0, NULL);
  32. tt_int_op(ret, OP_EQ, -ENOENT);
  33. ret = rend_cache_lookup_entry("invalid query", 2, NULL);
  34. tt_int_op(ret, OP_EQ, -EINVAL);
  35. ret = rend_cache_lookup_entry("abababababababab", 2, NULL);
  36. tt_int_op(ret, OP_EQ, -ENOENT);
  37. ret = rend_cache_lookup_entry("abababababababab", 4224, NULL);
  38. tt_int_op(ret, OP_EQ, -ENOENT);
  39. mock_rend_query = mock_rend_data(service_id);
  40. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  41. DIGEST_LEN);
  42. rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32,
  43. mock_rend_query, NULL);
  44. ret = rend_cache_lookup_entry(service_id, 2, NULL);
  45. tt_int_op(ret, OP_EQ, 0);
  46. ret = rend_cache_lookup_entry(service_id, 2, &entry);
  47. tt_int_op(ret, OP_EQ, 0);
  48. tt_assert(entry);
  49. tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str));
  50. tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str);
  51. done:
  52. rend_encoded_v2_service_descriptor_free(desc_holder);
  53. tor_free(service_id);
  54. rend_cache_free_all();
  55. rend_data_free(mock_rend_query);
  56. }
  57. static void
  58. test_rend_cache_store_v2_desc_as_client(void *data)
  59. {
  60. int ret;
  61. rend_data_t *mock_rend_query;
  62. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  63. rend_cache_entry_t *entry = NULL;
  64. rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  65. char *service_id = NULL;
  66. char client_cookie[REND_DESC_COOKIE_LEN];
  67. (void)data;
  68. rend_cache_init();
  69. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  70. // Test success
  71. mock_rend_query = mock_rend_data(service_id);
  72. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  73. DIGEST_LEN);
  74. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  75. desc_id_base32, mock_rend_query,
  76. &entry);
  77. tt_int_op(ret, OP_EQ, 0);
  78. tt_assert(entry);
  79. tt_int_op(entry->len, OP_EQ, strlen(desc_holder->desc_str));
  80. tt_str_op(entry->desc, OP_EQ, desc_holder->desc_str);
  81. // Test various failure modes
  82. // TODO: a too long desc_id_base32 argument crashes the function
  83. /* ret = rend_cache_store_v2_desc_as_client( */
  84. /* desc_holder->desc_str, */
  85. /* "3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG3TOOLONG", */
  86. /* &mock_rend_query, NULL); */
  87. /* tt_int_op(ret, OP_EQ, -1); */
  88. // Test bad base32 failure
  89. // This causes an assertion failure if we're running with assertions.
  90. // But when building without asserts, we can test it.
  91. #ifdef DISABLE_ASSERTS_IN_UNIT_TESTS
  92. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  93. "!xqunszqnaolrrfmtzgaki7mxelgvkj", mock_rend_query, NULL);
  94. tt_int_op(ret, OP_EQ, -1);
  95. #endif
  96. // Test invalid descriptor
  97. ret = rend_cache_store_v2_desc_as_client("invalid descriptor",
  98. "3xqunszqnaolrrfmtzgaki7mxelgvkje", mock_rend_query, NULL);
  99. tt_int_op(ret, OP_EQ, -1);
  100. // TODO: it doesn't seem to be possible to test invalid service ID condition.
  101. // that means it is likely not possible to have that condition without
  102. // earlier conditions failing first (such as signature checking of the desc)
  103. rend_cache_free_all();
  104. // Test mismatch between service ID and onion address
  105. rend_cache_init();
  106. strncpy(TO_REND_DATA_V2(mock_rend_query)->onion_address, "abc",
  107. REND_SERVICE_ID_LEN_BASE32+1);
  108. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  109. desc_id_base32,
  110. mock_rend_query, NULL);
  111. tt_int_op(ret, OP_EQ, -1);
  112. rend_cache_free_all();
  113. rend_data_free(mock_rend_query);
  114. // Test incorrect descriptor ID
  115. rend_cache_init();
  116. mock_rend_query = mock_rend_data(service_id);
  117. char orig = desc_id_base32[0];
  118. if (desc_id_base32[0] == 'a')
  119. desc_id_base32[0] = 'b';
  120. else
  121. desc_id_base32[0] = 'a';
  122. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  123. desc_id_base32, mock_rend_query,
  124. NULL);
  125. tt_int_op(ret, OP_EQ, -1);
  126. desc_id_base32[0] = orig;
  127. rend_cache_free_all();
  128. // Test too old descriptor
  129. rend_cache_init();
  130. rend_encoded_v2_service_descriptor_free(desc_holder);
  131. tor_free(service_id);
  132. rend_data_free(mock_rend_query);
  133. generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3);
  134. mock_rend_query = mock_rend_data(service_id);
  135. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  136. DIGEST_LEN);
  137. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  138. desc_id_base32,
  139. mock_rend_query, NULL);
  140. tt_int_op(ret, OP_EQ, -1);
  141. rend_cache_free_all();
  142. // Test too new descriptor (in the future)
  143. rend_cache_init();
  144. rend_encoded_v2_service_descriptor_free(desc_holder);
  145. tor_free(service_id);
  146. rend_data_free(mock_rend_query);
  147. generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
  148. mock_rend_query = mock_rend_data(service_id);
  149. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  150. DIGEST_LEN);
  151. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  152. desc_id_base32, mock_rend_query,
  153. NULL);
  154. tt_int_op(ret, OP_EQ, -1);
  155. rend_cache_free_all();
  156. // Test when a descriptor is already in the cache
  157. rend_cache_init();
  158. rend_encoded_v2_service_descriptor_free(desc_holder);
  159. tor_free(service_id);
  160. rend_data_free(mock_rend_query);
  161. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  162. mock_rend_query = mock_rend_data(service_id);
  163. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  164. DIGEST_LEN);
  165. rend_cache_store_v2_desc_as_client(desc_holder->desc_str, desc_id_base32,
  166. mock_rend_query, NULL);
  167. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  168. desc_id_base32, mock_rend_query,
  169. NULL);
  170. tt_int_op(ret, OP_EQ, 0);
  171. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  172. desc_id_base32, mock_rend_query,
  173. &entry);
  174. tt_int_op(ret, OP_EQ, 0);
  175. tt_assert(entry);
  176. rend_cache_free_all();
  177. // Test unsuccessful decrypting of introduction points
  178. rend_cache_init();
  179. rend_encoded_v2_service_descriptor_free(desc_holder);
  180. tor_free(service_id);
  181. rend_data_free(mock_rend_query);
  182. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  183. mock_rend_query = mock_rend_data(service_id);
  184. TO_REND_DATA_V2(mock_rend_query)->auth_type = REND_BASIC_AUTH;
  185. client_cookie[0] = 'A';
  186. memcpy(TO_REND_DATA_V2(mock_rend_query)->descriptor_cookie, client_cookie,
  187. REND_DESC_COOKIE_LEN);
  188. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  189. DIGEST_LEN);
  190. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  191. desc_id_base32, mock_rend_query,
  192. NULL);
  193. tt_int_op(ret, OP_EQ, 0);
  194. rend_cache_free_all();
  195. // Test successful run when we have REND_BASIC_AUTH but not cookie
  196. rend_cache_init();
  197. rend_encoded_v2_service_descriptor_free(desc_holder);
  198. tor_free(service_id);
  199. rend_data_free(mock_rend_query);
  200. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  201. mock_rend_query = mock_rend_data(service_id);
  202. TO_REND_DATA_V2(mock_rend_query)->auth_type = REND_BASIC_AUTH;
  203. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  204. DIGEST_LEN);
  205. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  206. desc_id_base32, mock_rend_query,
  207. NULL);
  208. tt_int_op(ret, OP_EQ, 0);
  209. rend_cache_free_all();
  210. // Test when we have no introduction points
  211. rend_cache_init();
  212. rend_encoded_v2_service_descriptor_free(desc_holder);
  213. tor_free(service_id);
  214. rend_data_free(mock_rend_query);
  215. generate_desc(RECENT_TIME, &desc_holder, &service_id, 0);
  216. mock_rend_query = mock_rend_data(service_id);
  217. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  218. DIGEST_LEN);
  219. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  220. desc_id_base32, mock_rend_query,
  221. NULL);
  222. tt_int_op(ret, OP_EQ, -1);
  223. rend_cache_free_all();
  224. // Test when we have too many intro points
  225. rend_cache_init();
  226. rend_encoded_v2_service_descriptor_free(desc_holder);
  227. tor_free(service_id);
  228. rend_data_free(mock_rend_query);
  229. generate_desc(RECENT_TIME, &desc_holder, &service_id, MAX_INTRO_POINTS+1);
  230. mock_rend_query = mock_rend_data(service_id);
  231. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  232. DIGEST_LEN);
  233. ret = rend_cache_store_v2_desc_as_client(desc_holder->desc_str,
  234. desc_id_base32, mock_rend_query,
  235. NULL);
  236. tt_int_op(ret, OP_EQ, -1);
  237. done:
  238. rend_encoded_v2_service_descriptor_free(desc_holder);
  239. tor_free(service_id);
  240. rend_cache_free_all();
  241. rend_data_free(mock_rend_query);
  242. }
  243. static void
  244. test_rend_cache_store_v2_desc_as_client_with_different_time(void *data)
  245. {
  246. int ret;
  247. rend_data_t *mock_rend_query;
  248. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  249. rend_service_descriptor_t *generated = NULL;
  250. smartlist_t *descs = smartlist_new();
  251. time_t t;
  252. char *service_id = NULL;
  253. rend_encoded_v2_service_descriptor_t *desc_holder_newer;
  254. rend_encoded_v2_service_descriptor_t *desc_holder_older;
  255. t = time(NULL);
  256. rend_cache_init();
  257. create_descriptor(&generated, &service_id, 3);
  258. generated->timestamp = t + RECENT_TIME;
  259. rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
  260. REND_NO_AUTH, NULL, NULL);
  261. desc_holder_newer = ((rend_encoded_v2_service_descriptor_t *)
  262. smartlist_get(descs, 0));
  263. smartlist_set(descs, 0, NULL);
  264. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  265. rend_encoded_v2_service_descriptor_free(d));
  266. smartlist_free(descs);
  267. descs = smartlist_new();
  268. generated->timestamp = (t + RECENT_TIME) - 20;
  269. rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
  270. REND_NO_AUTH, NULL, NULL);
  271. desc_holder_older = ((rend_encoded_v2_service_descriptor_t *)
  272. smartlist_get(descs, 0));
  273. smartlist_set(descs, 0, NULL);
  274. (void)data;
  275. // Test when a descriptor is already in the cache and it is newer than the
  276. // one we submit
  277. mock_rend_query = mock_rend_data(service_id);
  278. base32_encode(desc_id_base32, sizeof(desc_id_base32),
  279. desc_holder_newer->desc_id, DIGEST_LEN);
  280. rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str,
  281. desc_id_base32, mock_rend_query, NULL);
  282. ret = rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str,
  283. desc_id_base32, mock_rend_query,
  284. NULL);
  285. tt_int_op(ret, OP_EQ, 0);
  286. rend_cache_free_all();
  287. // Test when an old descriptor is in the cache and we submit a newer one
  288. rend_cache_init();
  289. rend_cache_store_v2_desc_as_client(desc_holder_older->desc_str,
  290. desc_id_base32, mock_rend_query, NULL);
  291. ret = rend_cache_store_v2_desc_as_client(desc_holder_newer->desc_str,
  292. desc_id_base32, mock_rend_query,
  293. NULL);
  294. tt_int_op(ret, OP_EQ, 0);
  295. done:
  296. rend_encoded_v2_service_descriptor_free(desc_holder_newer);
  297. rend_encoded_v2_service_descriptor_free(desc_holder_older);
  298. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  299. rend_encoded_v2_service_descriptor_free(d));
  300. smartlist_free(descs);
  301. rend_service_descriptor_free(generated);
  302. tor_free(service_id);
  303. rend_cache_free_all();
  304. rend_data_free(mock_rend_query);
  305. }
  306. #define NS_SUBMODULE lookup_v2_desc_as_dir
  307. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  308. static routerinfo_t *mock_routerinfo;
  309. static const routerinfo_t *
  310. NS(router_get_my_routerinfo)(void)
  311. {
  312. if (!mock_routerinfo) {
  313. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  314. }
  315. return mock_routerinfo;
  316. }
  317. static void
  318. test_rend_cache_lookup_v2_desc_as_dir(void *data)
  319. {
  320. int ret;
  321. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  322. rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  323. char *service_id = NULL;
  324. const char *ret_desc = NULL;
  325. (void)data;
  326. NS_MOCK(router_get_my_routerinfo);
  327. rend_cache_init();
  328. // Test invalid base32
  329. ret = rend_cache_lookup_v2_desc_as_dir("!bababababababab", NULL);
  330. tt_int_op(ret, OP_EQ, -1);
  331. // Test non-existent descriptor but well formed
  332. ret = rend_cache_lookup_v2_desc_as_dir("3xqunszqnaolrrfmtzgaki7mxelgvkje",
  333. NULL);
  334. tt_int_op(ret, OP_EQ, 0);
  335. // Test existing descriptor
  336. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  337. rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  338. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  339. DIGEST_LEN);
  340. ret = rend_cache_lookup_v2_desc_as_dir(desc_id_base32, &ret_desc);
  341. tt_int_op(ret, OP_EQ, 1);
  342. tt_assert(ret_desc);
  343. done:
  344. NS_UNMOCK(router_get_my_routerinfo);
  345. tor_free(mock_routerinfo);
  346. rend_cache_free_all();
  347. rend_encoded_v2_service_descriptor_free(desc_holder);
  348. tor_free(service_id);
  349. }
  350. #undef NS_SUBMODULE
  351. #define NS_SUBMODULE store_v2_desc_as_dir
  352. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  353. static const routerinfo_t *
  354. NS(router_get_my_routerinfo)(void)
  355. {
  356. return mock_routerinfo;
  357. }
  358. static void
  359. test_rend_cache_store_v2_desc_as_dir(void *data)
  360. {
  361. (void)data;
  362. int ret;
  363. rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  364. char *service_id = NULL;
  365. NS_MOCK(router_get_my_routerinfo);
  366. rend_cache_init();
  367. // Test when we can't parse the descriptor
  368. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  369. ret = rend_cache_store_v2_desc_as_dir("unparseable");
  370. tt_int_op(ret, OP_EQ, -1);
  371. // Test when we have an old descriptor
  372. generate_desc(TIME_IN_THE_PAST, &desc_holder, &service_id, 3);
  373. ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  374. tt_int_op(ret, OP_EQ, 0);
  375. rend_encoded_v2_service_descriptor_free(desc_holder);
  376. tor_free(service_id);
  377. // Test when we have a descriptor in the future
  378. generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
  379. ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  380. tt_int_op(ret, OP_EQ, 0);
  381. rend_encoded_v2_service_descriptor_free(desc_holder);
  382. tor_free(service_id);
  383. // Test when two descriptors
  384. generate_desc(TIME_IN_THE_FUTURE, &desc_holder, &service_id, 3);
  385. ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  386. tt_int_op(ret, OP_EQ, 0);
  387. rend_encoded_v2_service_descriptor_free(desc_holder);
  388. tor_free(service_id);
  389. // Test when asking for hidden service statistics HiddenServiceStatistics
  390. rend_cache_purge();
  391. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  392. get_options_mutable()->HiddenServiceStatistics = 1;
  393. ret = rend_cache_store_v2_desc_as_dir(desc_holder->desc_str);
  394. tt_int_op(ret, OP_EQ, 0);
  395. done:
  396. NS_UNMOCK(router_get_my_routerinfo);
  397. rend_encoded_v2_service_descriptor_free(desc_holder);
  398. tor_free(service_id);
  399. rend_cache_free_all();
  400. tor_free(mock_routerinfo);
  401. }
  402. static void
  403. test_rend_cache_store_v2_desc_as_dir_with_different_time(void *data)
  404. {
  405. (void)data;
  406. int ret;
  407. rend_service_descriptor_t *generated = NULL;
  408. smartlist_t *descs = smartlist_new();
  409. time_t t;
  410. char *service_id = NULL;
  411. rend_encoded_v2_service_descriptor_t *desc_holder_newer;
  412. rend_encoded_v2_service_descriptor_t *desc_holder_older;
  413. NS_MOCK(router_get_my_routerinfo);
  414. rend_cache_init();
  415. t = time(NULL);
  416. create_descriptor(&generated, &service_id, 3);
  417. generated->timestamp = t + RECENT_TIME;
  418. rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
  419. REND_NO_AUTH, NULL, NULL);
  420. desc_holder_newer = ((rend_encoded_v2_service_descriptor_t *)
  421. smartlist_get(descs, 0));
  422. smartlist_set(descs, 0, NULL);
  423. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  424. rend_encoded_v2_service_descriptor_free(d));
  425. smartlist_free(descs);
  426. descs = smartlist_new();
  427. generated->timestamp = (t + RECENT_TIME) - 20;
  428. rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
  429. REND_NO_AUTH, NULL, NULL);
  430. desc_holder_older = ((rend_encoded_v2_service_descriptor_t *)
  431. smartlist_get(descs, 0));
  432. smartlist_set(descs, 0, NULL);
  433. // Test when we have a newer descriptor stored
  434. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  435. rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str);
  436. ret = rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str);
  437. tt_int_op(ret, OP_EQ, 0);
  438. // Test when we have an old descriptor stored
  439. rend_cache_purge();
  440. rend_cache_store_v2_desc_as_dir(desc_holder_older->desc_str);
  441. ret = rend_cache_store_v2_desc_as_dir(desc_holder_newer->desc_str);
  442. tt_int_op(ret, OP_EQ, 0);
  443. done:
  444. NS_UNMOCK(router_get_my_routerinfo);
  445. rend_cache_free_all();
  446. rend_service_descriptor_free(generated);
  447. tor_free(service_id);
  448. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  449. rend_encoded_v2_service_descriptor_free(d));
  450. smartlist_free(descs);
  451. rend_encoded_v2_service_descriptor_free(desc_holder_newer);
  452. rend_encoded_v2_service_descriptor_free(desc_holder_older);
  453. tor_free(mock_routerinfo);
  454. }
  455. static void
  456. test_rend_cache_store_v2_desc_as_dir_with_different_content(void *data)
  457. {
  458. (void)data;
  459. int ret;
  460. rend_service_descriptor_t *generated = NULL;
  461. smartlist_t *descs = smartlist_new();
  462. time_t t;
  463. char *service_id = NULL;
  464. rend_encoded_v2_service_descriptor_t *desc_holder_one = NULL;
  465. rend_encoded_v2_service_descriptor_t *desc_holder_two = NULL;
  466. NS_MOCK(router_get_my_routerinfo);
  467. rend_cache_init();
  468. t = time(NULL);
  469. create_descriptor(&generated, &service_id, 3);
  470. generated->timestamp = t + RECENT_TIME;
  471. rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
  472. REND_NO_AUTH, NULL, NULL);
  473. desc_holder_one = ((rend_encoded_v2_service_descriptor_t *)
  474. smartlist_get(descs, 0));
  475. smartlist_set(descs, 0, NULL);
  476. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  477. rend_encoded_v2_service_descriptor_free(d));
  478. smartlist_free(descs);
  479. descs = smartlist_new();
  480. generated->timestamp = t + RECENT_TIME;
  481. generated->protocols = 41;
  482. rend_encode_v2_descriptors(descs, generated, t + RECENT_TIME, 0,
  483. REND_NO_AUTH, NULL, NULL);
  484. desc_holder_two = ((rend_encoded_v2_service_descriptor_t *)
  485. smartlist_get(descs, 0));
  486. smartlist_set(descs, 0, NULL);
  487. // Test when we have another descriptor stored, with a different descriptor
  488. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  489. rend_cache_store_v2_desc_as_dir(desc_holder_one->desc_str);
  490. ret = rend_cache_store_v2_desc_as_dir(desc_holder_two->desc_str);
  491. tt_int_op(ret, OP_EQ, 0);
  492. done:
  493. NS_UNMOCK(router_get_my_routerinfo);
  494. rend_cache_free_all();
  495. rend_service_descriptor_free(generated);
  496. tor_free(service_id);
  497. SMARTLIST_FOREACH(descs, rend_encoded_v2_service_descriptor_t *, d,
  498. rend_encoded_v2_service_descriptor_free(d));
  499. smartlist_free(descs);
  500. rend_encoded_v2_service_descriptor_free(desc_holder_one);
  501. rend_encoded_v2_service_descriptor_free(desc_holder_two);
  502. }
  503. #undef NS_SUBMODULE
  504. static void
  505. test_rend_cache_init(void *data)
  506. {
  507. (void)data;
  508. tt_assert_msg(!rend_cache, "rend_cache should be NULL when starting");
  509. tt_assert_msg(!rend_cache_v2_dir, "rend_cache_v2_dir should be NULL "
  510. "when starting");
  511. tt_assert_msg(!rend_cache_failure, "rend_cache_failure should be NULL when "
  512. "starting");
  513. rend_cache_init();
  514. tt_assert_msg(rend_cache, "rend_cache should not be NULL after initing");
  515. tt_assert_msg(rend_cache_v2_dir, "rend_cache_v2_dir should not be NULL "
  516. "after initing");
  517. tt_assert_msg(rend_cache_failure, "rend_cache_failure should not be NULL "
  518. "after initing");
  519. tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
  520. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
  521. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  522. done:
  523. rend_cache_free_all();
  524. }
  525. static void
  526. test_rend_cache_decrement_allocation(void *data)
  527. {
  528. (void)data;
  529. // Test when the cache has enough allocations
  530. rend_cache_total_allocation = 10;
  531. rend_cache_decrement_allocation(3);
  532. tt_int_op(rend_cache_total_allocation, OP_EQ, 7);
  533. // Test when there are not enough allocations
  534. rend_cache_total_allocation = 1;
  535. setup_full_capture_of_logs(LOG_WARN);
  536. rend_cache_decrement_allocation(2);
  537. tt_int_op(rend_cache_total_allocation, OP_EQ, 0);
  538. expect_single_log_msg_containing(
  539. "Underflow in rend_cache_decrement_allocation");
  540. teardown_capture_of_logs();
  541. // And again
  542. rend_cache_decrement_allocation(2);
  543. tt_int_op(rend_cache_total_allocation, OP_EQ, 0);
  544. done:
  545. teardown_capture_of_logs();
  546. }
  547. static void
  548. test_rend_cache_increment_allocation(void *data)
  549. {
  550. (void)data;
  551. // Test when the cache is not overflowing
  552. rend_cache_total_allocation = 5;
  553. rend_cache_increment_allocation(3);
  554. tt_int_op(rend_cache_total_allocation, OP_EQ, 8);
  555. // Test when there are too many allocations
  556. rend_cache_total_allocation = SIZE_MAX-1;
  557. setup_full_capture_of_logs(LOG_WARN);
  558. rend_cache_increment_allocation(2);
  559. tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
  560. expect_single_log_msg_containing(
  561. "Overflow in rend_cache_increment_allocation");
  562. teardown_capture_of_logs();
  563. // And again
  564. rend_cache_increment_allocation(2);
  565. tt_u64_op(rend_cache_total_allocation, OP_EQ, SIZE_MAX);
  566. done:
  567. teardown_capture_of_logs();
  568. }
  569. static void
  570. test_rend_cache_failure_intro_entry_new(void *data)
  571. {
  572. time_t now;
  573. rend_cache_failure_intro_t *entry;
  574. rend_intro_point_failure_t failure;
  575. (void)data;
  576. failure = INTRO_POINT_FAILURE_TIMEOUT;
  577. now = time(NULL);
  578. entry = rend_cache_failure_intro_entry_new(failure);
  579. tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_TIMEOUT);
  580. tt_int_op(entry->created_ts, OP_GE, now-5);
  581. tt_int_op(entry->created_ts, OP_LE, now+5);
  582. done:
  583. tor_free(entry);
  584. }
  585. static void
  586. test_rend_cache_failure_intro_lookup(void *data)
  587. {
  588. (void)data;
  589. int ret;
  590. rend_cache_failure_t *failure;
  591. rend_cache_failure_intro_t *ip;
  592. rend_cache_failure_intro_t *entry;
  593. const char key_ip_one[DIGEST_LEN] = "ip1";
  594. const char key_ip_two[DIGEST_LEN] = "ip2";
  595. const char key_foo[DIGEST_LEN] = "foo1";
  596. rend_cache_init();
  597. failure = rend_cache_failure_entry_new();
  598. ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  599. digestmap_set(failure->intro_failures, key_ip_one, ip);
  600. strmap_set_lc(rend_cache_failure, "foo1", failure);
  601. // Test not found
  602. ret = cache_failure_intro_lookup((const uint8_t *) key_foo, "foo2", NULL);
  603. tt_int_op(ret, OP_EQ, 0);
  604. // Test found with no intro failures in it
  605. ret = cache_failure_intro_lookup((const uint8_t *) key_ip_two, "foo1", NULL);
  606. tt_int_op(ret, OP_EQ, 0);
  607. // Test found
  608. ret = cache_failure_intro_lookup((const uint8_t *) key_ip_one, "foo1", NULL);
  609. tt_int_op(ret, OP_EQ, 1);
  610. // Test found and asking for entry
  611. cache_failure_intro_lookup((const uint8_t *) key_ip_one, "foo1", &entry);
  612. tt_assert(entry);
  613. tt_assert(entry == ip);
  614. done:
  615. rend_cache_free_all();
  616. }
  617. static void
  618. test_rend_cache_clean(void *data)
  619. {
  620. rend_cache_entry_t *one, *two;
  621. rend_service_descriptor_t *desc_one, *desc_two;
  622. strmap_iter_t *iter = NULL;
  623. const char *key;
  624. void *val;
  625. (void)data;
  626. rend_cache_init();
  627. // Test with empty rendcache
  628. rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
  629. tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
  630. // Test with two old entries
  631. one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  632. two = tor_malloc_zero(sizeof(rend_cache_entry_t));
  633. desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  634. desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  635. one->parsed = desc_one;
  636. two->parsed = desc_two;
  637. desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
  638. desc_two->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
  639. desc_one->pk = pk_generate(0);
  640. desc_two->pk = pk_generate(1);
  641. strmap_set_lc(rend_cache, "foo1", one);
  642. strmap_set_lc(rend_cache, "foo2", two);
  643. rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
  644. tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
  645. // Test with one old entry and one newer entry
  646. one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  647. two = tor_malloc_zero(sizeof(rend_cache_entry_t));
  648. desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  649. desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  650. one->parsed = desc_one;
  651. two->parsed = desc_two;
  652. desc_one->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
  653. desc_two->timestamp = time(NULL) - 100;
  654. desc_one->pk = pk_generate(0);
  655. desc_two->pk = pk_generate(1);
  656. strmap_set_lc(rend_cache, "foo1", one);
  657. strmap_set_lc(rend_cache, "foo2", two);
  658. rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
  659. tt_int_op(strmap_size(rend_cache), OP_EQ, 1);
  660. iter = strmap_iter_init(rend_cache);
  661. strmap_iter_get(iter, &key, &val);
  662. tt_str_op(key, OP_EQ, "foo2");
  663. done:
  664. rend_cache_free_all();
  665. }
  666. static void
  667. test_rend_cache_failure_entry_new(void *data)
  668. {
  669. rend_cache_failure_t *failure;
  670. (void)data;
  671. failure = rend_cache_failure_entry_new();
  672. tt_assert(failure);
  673. tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 0);
  674. done:
  675. rend_cache_failure_entry_free(failure);
  676. }
  677. static void
  678. test_rend_cache_failure_entry_free(void *data)
  679. {
  680. (void)data;
  681. // Test that it can deal with a NULL argument
  682. rend_cache_failure_entry_free_(NULL);
  683. /* done: */
  684. /* (void)0; */
  685. }
  686. static void
  687. test_rend_cache_failure_clean(void *data)
  688. {
  689. rend_cache_failure_t *failure;
  690. rend_cache_failure_intro_t *ip_one, *ip_two;
  691. const char key_one[DIGEST_LEN] = "ip1";
  692. const char key_two[DIGEST_LEN] = "ip2";
  693. (void)data;
  694. rend_cache_init();
  695. // Test with empty failure cache
  696. rend_cache_failure_clean(time(NULL));
  697. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  698. // Test with one empty failure entry
  699. failure = rend_cache_failure_entry_new();
  700. strmap_set_lc(rend_cache_failure, "foo1", failure);
  701. rend_cache_failure_clean(time(NULL));
  702. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  703. // Test with one new intro point
  704. failure = rend_cache_failure_entry_new();
  705. ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  706. digestmap_set(failure->intro_failures, key_one, ip_one);
  707. strmap_set_lc(rend_cache_failure, "foo1", failure);
  708. rend_cache_failure_clean(time(NULL));
  709. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
  710. // Test with one old intro point
  711. rend_cache_failure_purge();
  712. failure = rend_cache_failure_entry_new();
  713. ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  714. ip_one->created_ts = time(NULL) - 7*60;
  715. digestmap_set(failure->intro_failures, key_one, ip_one);
  716. strmap_set_lc(rend_cache_failure, "foo1", failure);
  717. rend_cache_failure_clean(time(NULL));
  718. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  719. // Test with one old intro point and one new one
  720. rend_cache_failure_purge();
  721. failure = rend_cache_failure_entry_new();
  722. ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  723. ip_one->created_ts = time(NULL) - 7*60;
  724. digestmap_set(failure->intro_failures, key_one, ip_one);
  725. ip_two = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  726. ip_two->created_ts = time(NULL) - 2*60;
  727. digestmap_set(failure->intro_failures, key_two, ip_two);
  728. strmap_set_lc(rend_cache_failure, "foo1", failure);
  729. rend_cache_failure_clean(time(NULL));
  730. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
  731. tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 1);
  732. done:
  733. rend_cache_free_all();
  734. }
  735. static void
  736. test_rend_cache_failure_remove(void *data)
  737. {
  738. rend_service_descriptor_t *desc;
  739. (void)data;
  740. rend_cache_init();
  741. // Test that it deals well with a NULL desc
  742. rend_cache_failure_remove(NULL);
  743. // Test a descriptor that isn't in the cache
  744. desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  745. desc->pk = pk_generate(0);
  746. rend_cache_failure_remove(desc);
  747. // There seems to not exist any way of getting rend_cache_failure_remove()
  748. // to fail because of a problem with rend_get_service_id from here
  749. rend_cache_free_all();
  750. rend_service_descriptor_free(desc);
  751. /* done: */
  752. /* (void)0; */
  753. }
  754. static void
  755. test_rend_cache_free_all(void *data)
  756. {
  757. rend_cache_failure_t *failure;
  758. rend_cache_entry_t *one;
  759. rend_service_descriptor_t *desc_one;
  760. (void)data;
  761. rend_cache_init();
  762. failure = rend_cache_failure_entry_new();
  763. strmap_set_lc(rend_cache_failure, "foo1", failure);
  764. one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  765. desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  766. one->parsed = desc_one;
  767. desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
  768. desc_one->pk = pk_generate(0);
  769. strmap_set_lc(rend_cache, "foo1", one);
  770. rend_cache_free_all();
  771. tt_ptr_op(rend_cache, OP_EQ, NULL);
  772. tt_ptr_op(rend_cache_v2_dir, OP_EQ, NULL);
  773. tt_ptr_op(rend_cache_failure, OP_EQ, NULL);
  774. tt_assert(!rend_cache_total_allocation);
  775. done:
  776. rend_cache_free_all();
  777. }
  778. static void
  779. test_rend_cache_entry_free(void *data)
  780. {
  781. (void)data;
  782. rend_cache_entry_t *e;
  783. // Handles NULL correctly
  784. rend_cache_entry_free_(NULL);
  785. // Handles NULL descriptor correctly
  786. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  787. rend_cache_entry_free(e);
  788. // Handles non-NULL descriptor correctly
  789. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  790. e->desc = tor_malloc(10);
  791. rend_cache_entry_free(e);
  792. /* done: */
  793. /* (void)0; */
  794. }
  795. static void
  796. test_rend_cache_purge(void *data)
  797. {
  798. (void)data;
  799. // Deals with a NULL rend_cache
  800. rend_cache_purge();
  801. tt_assert(rend_cache);
  802. tt_assert(strmap_size(rend_cache) == 0);
  803. // Deals with existing rend_cache
  804. rend_cache_free_all();
  805. rend_cache_init();
  806. tt_assert(rend_cache);
  807. tt_assert(strmap_size(rend_cache) == 0);
  808. rend_cache_purge();
  809. tt_assert(rend_cache);
  810. tt_assert(strmap_size(rend_cache) == 0);
  811. done:
  812. rend_cache_free_all();
  813. }
  814. static void
  815. test_rend_cache_failure_intro_add(void *data)
  816. {
  817. (void)data;
  818. rend_cache_failure_t *fail_entry;
  819. rend_cache_failure_intro_t *entry;
  820. const char identity[DIGEST_LEN] = "foo1";
  821. rend_cache_init();
  822. // Adds non-existing entry
  823. cache_failure_intro_add((const uint8_t *) identity, "foo2",
  824. INTRO_POINT_FAILURE_TIMEOUT);
  825. fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
  826. tt_assert(fail_entry);
  827. tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
  828. entry = digestmap_get(fail_entry->intro_failures, identity);
  829. tt_assert(entry);
  830. // Adds existing entry
  831. cache_failure_intro_add((const uint8_t *) identity, "foo2",
  832. INTRO_POINT_FAILURE_TIMEOUT);
  833. fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
  834. tt_assert(fail_entry);
  835. tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
  836. entry = digestmap_get(fail_entry->intro_failures, identity);
  837. tt_assert(entry);
  838. done:
  839. rend_cache_free_all();
  840. }
  841. static void
  842. test_rend_cache_intro_failure_note(void *data)
  843. {
  844. (void)data;
  845. rend_cache_failure_t *fail_entry;
  846. rend_cache_failure_intro_t *entry;
  847. const char key[DIGEST_LEN] = "foo1";
  848. rend_cache_init();
  849. // Test not found
  850. rend_cache_intro_failure_note(INTRO_POINT_FAILURE_TIMEOUT,
  851. (const uint8_t *) key, "foo2");
  852. fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
  853. tt_assert(fail_entry);
  854. tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
  855. entry = digestmap_get(fail_entry->intro_failures, key);
  856. tt_assert(entry);
  857. tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_TIMEOUT);
  858. // Test found
  859. rend_cache_intro_failure_note(INTRO_POINT_FAILURE_UNREACHABLE,
  860. (const uint8_t *) key, "foo2");
  861. tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_UNREACHABLE);
  862. done:
  863. rend_cache_free_all();
  864. }
  865. #define NS_SUBMODULE clean_v2_descs_as_dir
  866. static void
  867. test_rend_cache_clean_v2_descs_as_dir(void *data)
  868. {
  869. rend_cache_entry_t *e;
  870. time_t now, cutoff;
  871. rend_service_descriptor_t *desc;
  872. now = time(NULL);
  873. cutoff = now - (REND_CACHE_MAX_AGE + REND_CACHE_MAX_SKEW);
  874. const char key[DIGEST_LEN] = "abcde";
  875. (void)data;
  876. rend_cache_init();
  877. // Test running with an empty cache
  878. rend_cache_clean_v2_descs_as_dir(cutoff);
  879. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
  880. // Test with only one new entry
  881. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  882. e->last_served = now;
  883. desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  884. desc->timestamp = now;
  885. desc->pk = pk_generate(0);
  886. e->parsed = desc;
  887. digestmap_set(rend_cache_v2_dir, key, e);
  888. /* Set the cutoff to minus 10 seconds. */
  889. rend_cache_clean_v2_descs_as_dir(cutoff - 10);
  890. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 1);
  891. // Test with one old entry
  892. desc->timestamp = cutoff - 1000;
  893. rend_cache_clean_v2_descs_as_dir(cutoff);
  894. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
  895. done:
  896. rend_cache_free_all();
  897. }
  898. #undef NS_SUBMODULE
  899. static void
  900. test_rend_cache_entry_allocation(void *data)
  901. {
  902. (void)data;
  903. size_t ret;
  904. rend_cache_entry_t *e = NULL;
  905. // Handles a null argument
  906. ret = rend_cache_entry_allocation(NULL);
  907. tt_int_op(ret, OP_EQ, 0);
  908. // Handles a non-null argument
  909. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  910. ret = rend_cache_entry_allocation(e);
  911. tt_int_op(ret, OP_GT, sizeof(rend_cache_entry_t));
  912. done:
  913. tor_free(e);
  914. }
  915. static void
  916. test_rend_cache_failure_intro_entry_free(void *data)
  917. {
  918. (void)data;
  919. rend_cache_failure_intro_t *entry;
  920. // Handles a null argument
  921. rend_cache_failure_intro_entry_free_(NULL);
  922. // Handles a non-null argument
  923. entry = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  924. rend_cache_failure_intro_entry_free(entry);
  925. }
  926. static void
  927. test_rend_cache_failure_purge(void *data)
  928. {
  929. (void)data;
  930. // Handles a null failure cache
  931. strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void);
  932. rend_cache_failure = NULL;
  933. rend_cache_failure_purge();
  934. tt_ptr_op(rend_cache_failure, OP_NE, NULL);
  935. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  936. done:
  937. rend_cache_free_all();
  938. }
  939. static void
  940. test_rend_cache_validate_intro_point_failure(void *data)
  941. {
  942. (void)data;
  943. rend_service_descriptor_t *desc = NULL;
  944. char *service_id = NULL;
  945. rend_intro_point_t *intro = NULL;
  946. const char *identity = NULL;
  947. rend_cache_failure_t *failure;
  948. rend_cache_failure_intro_t *ip;
  949. rend_cache_init();
  950. create_descriptor(&desc, &service_id, 3);
  951. desc->timestamp = time(NULL) + RECENT_TIME;
  952. intro = (rend_intro_point_t *)smartlist_get(desc->intro_nodes, 0);
  953. identity = intro->extend_info->identity_digest;
  954. failure = rend_cache_failure_entry_new();
  955. ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  956. digestmap_set(failure->intro_failures, identity, ip);
  957. strmap_set_lc(rend_cache_failure, service_id, failure);
  958. // Test when we have an intro point in our cache
  959. validate_intro_point_failure(desc, service_id);
  960. tt_int_op(smartlist_len(desc->intro_nodes), OP_EQ, 2);
  961. done:
  962. rend_cache_free_all();
  963. rend_service_descriptor_free(desc);
  964. tor_free(service_id);
  965. }
  966. struct testcase_t rend_cache_tests[] = {
  967. { "init", test_rend_cache_init, 0, NULL, NULL },
  968. { "decrement_allocation", test_rend_cache_decrement_allocation, 0,
  969. NULL, NULL },
  970. { "increment_allocation", test_rend_cache_increment_allocation, 0,
  971. NULL, NULL },
  972. { "clean", test_rend_cache_clean, TT_FORK, NULL, NULL },
  973. { "clean_v2_descs_as_dir", test_rend_cache_clean_v2_descs_as_dir, 0,
  974. NULL, NULL },
  975. { "entry_allocation", test_rend_cache_entry_allocation, 0, NULL, NULL },
  976. { "entry_free", test_rend_cache_entry_free, 0, NULL, NULL },
  977. { "failure_intro_entry_free", test_rend_cache_failure_intro_entry_free, 0,
  978. NULL, NULL },
  979. { "free_all", test_rend_cache_free_all, 0, NULL, NULL },
  980. { "purge", test_rend_cache_purge, 0, NULL, NULL },
  981. { "failure_clean", test_rend_cache_failure_clean, 0, NULL, NULL },
  982. { "failure_entry_new", test_rend_cache_failure_entry_new, 0, NULL, NULL },
  983. { "failure_entry_free", test_rend_cache_failure_entry_free, 0, NULL, NULL },
  984. { "failure_intro_add", test_rend_cache_failure_intro_add, 0, NULL, NULL },
  985. { "failure_intro_entry_new", test_rend_cache_failure_intro_entry_new, 0,
  986. NULL, NULL },
  987. { "failure_intro_lookup", test_rend_cache_failure_intro_lookup, 0,
  988. NULL, NULL },
  989. { "failure_purge", test_rend_cache_failure_purge, 0, NULL, NULL },
  990. { "failure_remove", test_rend_cache_failure_remove, 0, NULL, NULL },
  991. { "intro_failure_note", test_rend_cache_intro_failure_note, 0, NULL, NULL },
  992. { "lookup", test_rend_cache_lookup_entry, 0, NULL, NULL },
  993. { "lookup_v2_desc_as_dir", test_rend_cache_lookup_v2_desc_as_dir, 0,
  994. NULL, NULL },
  995. { "store_v2_desc_as_client", test_rend_cache_store_v2_desc_as_client, 0,
  996. NULL, NULL },
  997. { "store_v2_desc_as_client_with_different_time",
  998. test_rend_cache_store_v2_desc_as_client_with_different_time, 0,
  999. NULL, NULL },
  1000. { "store_v2_desc_as_dir", test_rend_cache_store_v2_desc_as_dir, 0,
  1001. NULL, NULL },
  1002. { "store_v2_desc_as_dir_with_different_time",
  1003. test_rend_cache_store_v2_desc_as_dir_with_different_time, 0, NULL, NULL },
  1004. { "store_v2_desc_as_dir_with_different_content",
  1005. test_rend_cache_store_v2_desc_as_dir_with_different_content, 0,
  1006. NULL, NULL },
  1007. { "validate_intro_point_failure",
  1008. test_rend_cache_validate_intro_point_failure, 0, NULL, NULL },
  1009. END_OF_TESTCASES
  1010. };