test_rendcache.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /* Copyright (c) 2010-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "core/or/or.h"
  5. #include "test/test.h"
  6. #define RENDCACHE_PRIVATE
  7. #include "feature/rend/rendcache.h"
  8. #include "feature/relay/router.h"
  9. #include "feature/nodelist/routerlist.h"
  10. #include "app/config/config.h"
  11. #include "feature/hs/hs_common.h"
  12. #include "core/or/extend_info_st.h"
  13. #include "feature/rend/rend_encoded_v2_service_descriptor_st.h"
  14. #include "feature/rend/rend_intro_point_st.h"
  15. #include "feature/rend/rend_service_descriptor_st.h"
  16. #include "feature/nodelist/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. rend_cache_increment_allocation(rend_cache_entry_allocation(one));
  648. strmap_set_lc(rend_cache, "foo2", two);
  649. rend_cache_increment_allocation(rend_cache_entry_allocation(two));
  650. rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
  651. tt_int_op(strmap_size(rend_cache), OP_EQ, 0);
  652. // Test with one old entry and one newer entry
  653. one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  654. two = tor_malloc_zero(sizeof(rend_cache_entry_t));
  655. desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  656. desc_two = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  657. one->parsed = desc_one;
  658. two->parsed = desc_two;
  659. desc_one->timestamp = (time(NULL) + TIME_IN_THE_PAST) - 10;
  660. desc_two->timestamp = time(NULL) - 100;
  661. desc_one->pk = pk_generate(0);
  662. desc_two->pk = pk_generate(1);
  663. rend_cache_increment_allocation(rend_cache_entry_allocation(one));
  664. strmap_set_lc(rend_cache, "foo1", one);
  665. rend_cache_increment_allocation(rend_cache_entry_allocation(two));
  666. strmap_set_lc(rend_cache, "foo2", two);
  667. rend_cache_clean(time(NULL), REND_CACHE_TYPE_CLIENT);
  668. tt_int_op(strmap_size(rend_cache), OP_EQ, 1);
  669. iter = strmap_iter_init(rend_cache);
  670. strmap_iter_get(iter, &key, &val);
  671. tt_str_op(key, OP_EQ, "foo2");
  672. done:
  673. rend_cache_free_all();
  674. }
  675. static void
  676. test_rend_cache_failure_entry_new(void *data)
  677. {
  678. rend_cache_failure_t *failure;
  679. (void)data;
  680. failure = rend_cache_failure_entry_new();
  681. tt_assert(failure);
  682. tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 0);
  683. done:
  684. rend_cache_failure_entry_free(failure);
  685. }
  686. static void
  687. test_rend_cache_failure_entry_free(void *data)
  688. {
  689. (void)data;
  690. // Test that it can deal with a NULL argument
  691. rend_cache_failure_entry_free_(NULL);
  692. /* done: */
  693. /* (void)0; */
  694. }
  695. static void
  696. test_rend_cache_failure_clean(void *data)
  697. {
  698. rend_cache_failure_t *failure;
  699. rend_cache_failure_intro_t *ip_one, *ip_two;
  700. const char key_one[DIGEST_LEN] = "ip1";
  701. const char key_two[DIGEST_LEN] = "ip2";
  702. (void)data;
  703. rend_cache_init();
  704. // Test with empty failure cache
  705. rend_cache_failure_clean(time(NULL));
  706. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  707. // Test with one empty failure entry
  708. failure = rend_cache_failure_entry_new();
  709. strmap_set_lc(rend_cache_failure, "foo1", failure);
  710. rend_cache_failure_clean(time(NULL));
  711. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  712. // Test with one new intro point
  713. failure = rend_cache_failure_entry_new();
  714. ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  715. digestmap_set(failure->intro_failures, key_one, ip_one);
  716. strmap_set_lc(rend_cache_failure, "foo1", failure);
  717. rend_cache_failure_clean(time(NULL));
  718. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
  719. // Test with one old intro point
  720. rend_cache_failure_purge();
  721. failure = rend_cache_failure_entry_new();
  722. ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  723. ip_one->created_ts = time(NULL) - 7*60;
  724. digestmap_set(failure->intro_failures, key_one, ip_one);
  725. strmap_set_lc(rend_cache_failure, "foo1", failure);
  726. rend_cache_failure_clean(time(NULL));
  727. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  728. // Test with one old intro point and one new one
  729. rend_cache_failure_purge();
  730. failure = rend_cache_failure_entry_new();
  731. ip_one = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  732. ip_one->created_ts = time(NULL) - 7*60;
  733. digestmap_set(failure->intro_failures, key_one, ip_one);
  734. ip_two = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  735. ip_two->created_ts = time(NULL) - 2*60;
  736. digestmap_set(failure->intro_failures, key_two, ip_two);
  737. strmap_set_lc(rend_cache_failure, "foo1", failure);
  738. rend_cache_failure_clean(time(NULL));
  739. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 1);
  740. tt_int_op(digestmap_size(failure->intro_failures), OP_EQ, 1);
  741. done:
  742. rend_cache_free_all();
  743. }
  744. static void
  745. test_rend_cache_failure_remove(void *data)
  746. {
  747. rend_service_descriptor_t *desc;
  748. (void)data;
  749. rend_cache_init();
  750. // Test that it deals well with a NULL desc
  751. rend_cache_failure_remove(NULL);
  752. // Test a descriptor that isn't in the cache
  753. desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  754. desc->pk = pk_generate(0);
  755. rend_cache_failure_remove(desc);
  756. // There seems to not exist any way of getting rend_cache_failure_remove()
  757. // to fail because of a problem with rend_get_service_id from here
  758. rend_cache_free_all();
  759. rend_service_descriptor_free(desc);
  760. /* done: */
  761. /* (void)0; */
  762. }
  763. static void
  764. test_rend_cache_free_all(void *data)
  765. {
  766. rend_cache_failure_t *failure;
  767. rend_cache_entry_t *one;
  768. rend_service_descriptor_t *desc_one;
  769. (void)data;
  770. rend_cache_init();
  771. failure = rend_cache_failure_entry_new();
  772. strmap_set_lc(rend_cache_failure, "foo1", failure);
  773. one = tor_malloc_zero(sizeof(rend_cache_entry_t));
  774. desc_one = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  775. one->parsed = desc_one;
  776. desc_one->timestamp = time(NULL) + TIME_IN_THE_PAST;
  777. desc_one->pk = pk_generate(0);
  778. strmap_set_lc(rend_cache, "foo1", one);
  779. rend_cache_free_all();
  780. tt_ptr_op(rend_cache, OP_EQ, NULL);
  781. tt_ptr_op(rend_cache_v2_dir, OP_EQ, NULL);
  782. tt_ptr_op(rend_cache_failure, OP_EQ, NULL);
  783. tt_assert(!rend_cache_total_allocation);
  784. done:
  785. rend_cache_free_all();
  786. }
  787. static void
  788. test_rend_cache_entry_free(void *data)
  789. {
  790. (void)data;
  791. rend_cache_entry_t *e;
  792. // Handles NULL correctly
  793. rend_cache_entry_free_(NULL);
  794. // Handles NULL descriptor correctly
  795. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  796. rend_cache_entry_free(e);
  797. // Handles non-NULL descriptor correctly
  798. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  799. e->desc = tor_malloc(10);
  800. rend_cache_entry_free(e);
  801. /* done: */
  802. /* (void)0; */
  803. }
  804. static void
  805. test_rend_cache_purge(void *data)
  806. {
  807. (void)data;
  808. // Deals with a NULL rend_cache
  809. rend_cache_purge();
  810. tt_assert(rend_cache);
  811. tt_assert(strmap_size(rend_cache) == 0);
  812. // Deals with existing rend_cache
  813. rend_cache_free_all();
  814. rend_cache_init();
  815. tt_assert(rend_cache);
  816. tt_assert(strmap_size(rend_cache) == 0);
  817. rend_cache_purge();
  818. tt_assert(rend_cache);
  819. tt_assert(strmap_size(rend_cache) == 0);
  820. done:
  821. rend_cache_free_all();
  822. }
  823. static void
  824. test_rend_cache_failure_intro_add(void *data)
  825. {
  826. (void)data;
  827. rend_cache_failure_t *fail_entry;
  828. rend_cache_failure_intro_t *entry;
  829. const char identity[DIGEST_LEN] = "foo1";
  830. rend_cache_init();
  831. // Adds non-existing entry
  832. cache_failure_intro_add((const uint8_t *) identity, "foo2",
  833. INTRO_POINT_FAILURE_TIMEOUT);
  834. fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
  835. tt_assert(fail_entry);
  836. tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
  837. entry = digestmap_get(fail_entry->intro_failures, identity);
  838. tt_assert(entry);
  839. // Adds existing entry
  840. cache_failure_intro_add((const uint8_t *) identity, "foo2",
  841. INTRO_POINT_FAILURE_TIMEOUT);
  842. fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
  843. tt_assert(fail_entry);
  844. tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
  845. entry = digestmap_get(fail_entry->intro_failures, identity);
  846. tt_assert(entry);
  847. done:
  848. rend_cache_free_all();
  849. }
  850. static void
  851. test_rend_cache_intro_failure_note(void *data)
  852. {
  853. (void)data;
  854. rend_cache_failure_t *fail_entry;
  855. rend_cache_failure_intro_t *entry;
  856. const char key[DIGEST_LEN] = "foo1";
  857. rend_cache_init();
  858. // Test not found
  859. rend_cache_intro_failure_note(INTRO_POINT_FAILURE_TIMEOUT,
  860. (const uint8_t *) key, "foo2");
  861. fail_entry = strmap_get_lc(rend_cache_failure, "foo2");
  862. tt_assert(fail_entry);
  863. tt_int_op(digestmap_size(fail_entry->intro_failures), OP_EQ, 1);
  864. entry = digestmap_get(fail_entry->intro_failures, key);
  865. tt_assert(entry);
  866. tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_TIMEOUT);
  867. // Test found
  868. rend_cache_intro_failure_note(INTRO_POINT_FAILURE_UNREACHABLE,
  869. (const uint8_t *) key, "foo2");
  870. tt_int_op(entry->failure_type, OP_EQ, INTRO_POINT_FAILURE_UNREACHABLE);
  871. done:
  872. rend_cache_free_all();
  873. }
  874. #define NS_SUBMODULE clean_v2_descs_as_dir
  875. static void
  876. test_rend_cache_clean_v2_descs_as_dir(void *data)
  877. {
  878. rend_cache_entry_t *e;
  879. time_t now, cutoff;
  880. rend_service_descriptor_t *desc;
  881. now = time(NULL);
  882. cutoff = now - (REND_CACHE_MAX_AGE + REND_CACHE_MAX_SKEW);
  883. const char key[DIGEST_LEN] = "abcde";
  884. (void)data;
  885. rend_cache_init();
  886. // Test running with an empty cache
  887. rend_cache_clean_v2_descs_as_dir(cutoff);
  888. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
  889. // Test with only one new entry
  890. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  891. e->last_served = now;
  892. desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
  893. desc->timestamp = now;
  894. desc->pk = pk_generate(0);
  895. e->parsed = desc;
  896. digestmap_set(rend_cache_v2_dir, key, e);
  897. /* Set the cutoff to minus 10 seconds. */
  898. rend_cache_clean_v2_descs_as_dir(cutoff - 10);
  899. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 1);
  900. // Test with one old entry
  901. desc->timestamp = cutoff - 1000;
  902. rend_cache_clean_v2_descs_as_dir(cutoff);
  903. tt_int_op(digestmap_size(rend_cache_v2_dir), OP_EQ, 0);
  904. done:
  905. rend_cache_free_all();
  906. }
  907. #undef NS_SUBMODULE
  908. static void
  909. test_rend_cache_entry_allocation(void *data)
  910. {
  911. (void)data;
  912. size_t ret;
  913. rend_cache_entry_t *e = NULL;
  914. // Handles a null argument
  915. ret = rend_cache_entry_allocation(NULL);
  916. tt_int_op(ret, OP_EQ, 0);
  917. // Handles a non-null argument
  918. e = tor_malloc_zero(sizeof(rend_cache_entry_t));
  919. ret = rend_cache_entry_allocation(e);
  920. tt_int_op(ret, OP_GT, sizeof(rend_cache_entry_t));
  921. done:
  922. tor_free(e);
  923. }
  924. static void
  925. test_rend_cache_failure_intro_entry_free(void *data)
  926. {
  927. (void)data;
  928. rend_cache_failure_intro_t *entry;
  929. // Handles a null argument
  930. rend_cache_failure_intro_entry_free_(NULL);
  931. // Handles a non-null argument
  932. entry = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  933. rend_cache_failure_intro_entry_free(entry);
  934. }
  935. static void
  936. test_rend_cache_failure_purge(void *data)
  937. {
  938. (void)data;
  939. // Handles a null failure cache
  940. strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void);
  941. rend_cache_failure = NULL;
  942. rend_cache_failure_purge();
  943. tt_ptr_op(rend_cache_failure, OP_NE, NULL);
  944. tt_int_op(strmap_size(rend_cache_failure), OP_EQ, 0);
  945. done:
  946. rend_cache_free_all();
  947. }
  948. static void
  949. test_rend_cache_validate_intro_point_failure(void *data)
  950. {
  951. (void)data;
  952. rend_service_descriptor_t *desc = NULL;
  953. char *service_id = NULL;
  954. rend_intro_point_t *intro = NULL;
  955. const char *identity = NULL;
  956. rend_cache_failure_t *failure;
  957. rend_cache_failure_intro_t *ip;
  958. rend_cache_init();
  959. create_descriptor(&desc, &service_id, 3);
  960. desc->timestamp = time(NULL) + RECENT_TIME;
  961. intro = (rend_intro_point_t *)smartlist_get(desc->intro_nodes, 0);
  962. identity = intro->extend_info->identity_digest;
  963. failure = rend_cache_failure_entry_new();
  964. ip = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
  965. digestmap_set(failure->intro_failures, identity, ip);
  966. strmap_set_lc(rend_cache_failure, service_id, failure);
  967. // Test when we have an intro point in our cache
  968. validate_intro_point_failure(desc, service_id);
  969. tt_int_op(smartlist_len(desc->intro_nodes), OP_EQ, 2);
  970. done:
  971. rend_cache_free_all();
  972. rend_service_descriptor_free(desc);
  973. tor_free(service_id);
  974. }
  975. struct testcase_t rend_cache_tests[] = {
  976. { "init", test_rend_cache_init, 0, NULL, NULL },
  977. { "decrement_allocation", test_rend_cache_decrement_allocation, 0,
  978. NULL, NULL },
  979. { "increment_allocation", test_rend_cache_increment_allocation, 0,
  980. NULL, NULL },
  981. { "clean", test_rend_cache_clean, TT_FORK, NULL, NULL },
  982. { "clean_v2_descs_as_dir", test_rend_cache_clean_v2_descs_as_dir, 0,
  983. NULL, NULL },
  984. { "entry_allocation", test_rend_cache_entry_allocation, 0, NULL, NULL },
  985. { "entry_free", test_rend_cache_entry_free, 0, NULL, NULL },
  986. { "failure_intro_entry_free", test_rend_cache_failure_intro_entry_free, 0,
  987. NULL, NULL },
  988. { "free_all", test_rend_cache_free_all, 0, NULL, NULL },
  989. { "purge", test_rend_cache_purge, 0, NULL, NULL },
  990. { "failure_clean", test_rend_cache_failure_clean, 0, NULL, NULL },
  991. { "failure_entry_new", test_rend_cache_failure_entry_new, 0, NULL, NULL },
  992. { "failure_entry_free", test_rend_cache_failure_entry_free, 0, NULL, NULL },
  993. { "failure_intro_add", test_rend_cache_failure_intro_add, 0, NULL, NULL },
  994. { "failure_intro_entry_new", test_rend_cache_failure_intro_entry_new, 0,
  995. NULL, NULL },
  996. { "failure_intro_lookup", test_rend_cache_failure_intro_lookup, 0,
  997. NULL, NULL },
  998. { "failure_purge", test_rend_cache_failure_purge, 0, NULL, NULL },
  999. { "failure_remove", test_rend_cache_failure_remove, 0, NULL, NULL },
  1000. { "intro_failure_note", test_rend_cache_intro_failure_note, 0, NULL, NULL },
  1001. { "lookup", test_rend_cache_lookup_entry, 0, NULL, NULL },
  1002. { "lookup_v2_desc_as_dir", test_rend_cache_lookup_v2_desc_as_dir, 0,
  1003. NULL, NULL },
  1004. { "store_v2_desc_as_client", test_rend_cache_store_v2_desc_as_client, 0,
  1005. NULL, NULL },
  1006. { "store_v2_desc_as_client_with_different_time",
  1007. test_rend_cache_store_v2_desc_as_client_with_different_time, 0,
  1008. NULL, NULL },
  1009. { "store_v2_desc_as_dir", test_rend_cache_store_v2_desc_as_dir, 0,
  1010. NULL, NULL },
  1011. { "store_v2_desc_as_dir_with_different_time",
  1012. test_rend_cache_store_v2_desc_as_dir_with_different_time, 0, NULL, NULL },
  1013. { "store_v2_desc_as_dir_with_different_content",
  1014. test_rend_cache_store_v2_desc_as_dir_with_different_content, 0,
  1015. NULL, NULL },
  1016. { "validate_intro_point_failure",
  1017. test_rend_cache_validate_intro_point_failure, 0, NULL, NULL },
  1018. END_OF_TESTCASES
  1019. };