test_rendcache.c 39 KB

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