test_rendcache.c 39 KB

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