1 /* Copyright (c) 2012-2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define REPLAYCACHE_PRIVATE
7 #include "core/or/or.h"
8 #include "feature/hs_common/replaycache.h"
11 static const char *test_buffer
=
12 "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed do eiusmod"
13 " tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
14 " veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
15 " commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
16 " velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
17 " occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
18 " mollit anim id est laborum.";
20 static const char *test_buffer_2
=
21 "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis"
22 " praesentium voluptatum deleniti atque corrupti quos dolores et quas"
23 " molestias excepturi sint occaecati cupiditate non provident, similique"
24 " sunt in culpa qui officia deserunt mollitia animi, id est laborum et"
25 " dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio."
26 " Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil"
27 " impedit quo minus id quod maxime placeat facere possimus, omnis voluptas"
28 " assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut"
29 " officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates"
30 " repudiandae sint et molestiae non recusandae. Itaque earum rerum hic"
31 " tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias"
32 " consequatur aut perferendis doloribus asperiores repellat.";
35 test_replaycache_alloc(void *arg
)
37 replaycache_t
*r
= NULL
;
40 r
= replaycache_new(600, 300);
41 tt_ptr_op(r
, OP_NE
, NULL
);
44 if (r
) replaycache_free(r
);
50 test_replaycache_badalloc(void *arg
)
52 replaycache_t
*r
= NULL
;
54 /* Negative horizon should fail */
56 r
= replaycache_new(-600, 300);
57 tt_ptr_op(r
, OP_EQ
, NULL
);
58 /* Negative interval should get adjusted to zero */
59 r
= replaycache_new(600, -300);
60 tt_ptr_op(r
, OP_NE
, NULL
);
61 tt_int_op(r
->scrub_interval
,OP_EQ
, 0);
63 /* Negative horizon and negative interval should still fail */
64 r
= replaycache_new(-600, -300);
65 tt_ptr_op(r
, OP_EQ
, NULL
);
68 if (r
) replaycache_free(r
);
74 test_replaycache_free_null(void *arg
)
77 replaycache_free_(NULL
);
78 /* Assert that we're here without horrible death */
86 test_replaycache_miss(void *arg
)
88 replaycache_t
*r
= NULL
;
92 r
= replaycache_new(600, 300);
93 tt_ptr_op(r
, OP_NE
, NULL
);
96 replaycache_add_and_test_internal(1200, r
, test_buffer
,
97 strlen(test_buffer
), NULL
);
98 tt_int_op(result
,OP_EQ
, 0);
100 /* make sure a different buffer misses as well */
102 replaycache_add_and_test_internal(1200, NULL
, test_buffer_2
,
103 strlen(test_buffer_2
), NULL
);
104 tt_int_op(result
,OP_EQ
, 0);
106 /* poke the bad-parameter error case too */
108 replaycache_add_and_test_internal(1200, NULL
, test_buffer
,
109 strlen(test_buffer
), NULL
);
110 tt_int_op(result
,OP_EQ
, 0);
113 if (r
) replaycache_free(r
);
119 test_replaycache_hit(void *arg
)
121 replaycache_t
*r
= NULL
;
125 r
= replaycache_new(600, 300);
126 tt_ptr_op(r
, OP_NE
, NULL
);
129 replaycache_add_and_test_internal(1200, r
, test_buffer
,
130 strlen(test_buffer
), NULL
);
131 tt_int_op(result
,OP_EQ
, 0);
134 replaycache_add_and_test_internal(1300, r
, test_buffer
,
135 strlen(test_buffer
), NULL
);
136 tt_int_op(result
,OP_EQ
, 1);
138 /* make sure a different buffer misses then hits as well */
141 replaycache_add_and_test_internal(1200, r
, test_buffer_2
,
142 strlen(test_buffer_2
), NULL
);
143 tt_int_op(result
,OP_EQ
, 0);
146 replaycache_add_and_test_internal(1300, r
, test_buffer_2
,
147 strlen(test_buffer_2
), NULL
);
148 tt_int_op(result
,OP_EQ
, 1);
151 if (r
) replaycache_free(r
);
157 test_replaycache_age(void *arg
)
159 replaycache_t
*r
= NULL
;
163 r
= replaycache_new(600, 300);
164 tt_ptr_op(r
, OP_NE
, NULL
);
167 replaycache_add_and_test_internal(1200, r
, test_buffer
,
168 strlen(test_buffer
), NULL
);
169 tt_int_op(result
,OP_EQ
, 0);
172 replaycache_add_and_test_internal(1300, r
, test_buffer
,
173 strlen(test_buffer
), NULL
);
174 tt_int_op(result
,OP_EQ
, 1);
177 replaycache_add_and_test_internal(3000, r
, test_buffer
,
178 strlen(test_buffer
), NULL
);
179 tt_int_op(result
,OP_EQ
, 0);
182 if (r
) replaycache_free(r
);
188 test_replaycache_elapsed(void *arg
)
190 replaycache_t
*r
= NULL
;
195 r
= replaycache_new(600, 300);
196 tt_ptr_op(r
, OP_NE
, NULL
);
199 replaycache_add_and_test_internal(1200, r
, test_buffer
,
200 strlen(test_buffer
), NULL
);
201 tt_int_op(result
,OP_EQ
, 0);
204 replaycache_add_and_test_internal(1300, r
, test_buffer
,
205 strlen(test_buffer
), &elapsed
);
206 tt_int_op(result
,OP_EQ
, 1);
207 tt_int_op(elapsed
,OP_EQ
, 100);
210 if (r
) replaycache_free(r
);
216 test_replaycache_noexpire(void *arg
)
218 replaycache_t
*r
= NULL
;
222 r
= replaycache_new(0, 0);
223 tt_ptr_op(r
, OP_NE
, NULL
);
226 replaycache_add_and_test_internal(1200, r
, test_buffer
,
227 strlen(test_buffer
), NULL
);
228 tt_int_op(result
,OP_EQ
, 0);
231 replaycache_add_and_test_internal(1300, r
, test_buffer
,
232 strlen(test_buffer
), NULL
);
233 tt_int_op(result
,OP_EQ
, 1);
236 replaycache_add_and_test_internal(3000, r
, test_buffer
,
237 strlen(test_buffer
), NULL
);
238 tt_int_op(result
,OP_EQ
, 1);
241 if (r
) replaycache_free(r
);
247 test_replaycache_scrub(void *arg
)
249 replaycache_t
*r
= NULL
;
253 r
= replaycache_new(600, 300);
254 tt_ptr_op(r
, OP_NE
, NULL
);
256 /* Set up like in test_replaycache_hit() */
258 replaycache_add_and_test_internal(100, r
, test_buffer
,
259 strlen(test_buffer
), NULL
);
260 tt_int_op(result
,OP_EQ
, 0);
263 replaycache_add_and_test_internal(200, r
, test_buffer
,
264 strlen(test_buffer
), NULL
);
265 tt_int_op(result
,OP_EQ
, 1);
268 * Poke a few replaycache_scrub_if_needed_internal() error cases that
269 * can't happen through replaycache_add_and_test_internal()
273 replaycache_scrub_if_needed_internal(300, NULL
);
274 /* Assert we're still here */
277 /* Make sure we hit the aging-out case too */
278 replaycache_scrub_if_needed_internal(1500, r
);
279 /* Assert that we aged it */
280 tt_int_op(digest256map_size(r
->digests_seen
),OP_EQ
, 0);
283 if (r
) replaycache_free(r
);
289 test_replaycache_future(void *arg
)
291 replaycache_t
*r
= NULL
;
296 r
= replaycache_new(600, 300);
297 tt_ptr_op(r
, OP_NE
, NULL
);
299 /* Set up like in test_replaycache_hit() */
301 replaycache_add_and_test_internal(100, r
, test_buffer
,
302 strlen(test_buffer
), &elapsed
);
303 tt_int_op(result
,OP_EQ
, 0);
304 /* elapsed should still be 0, since it wasn't written */
305 tt_int_op(elapsed
,OP_EQ
, 0);
308 replaycache_add_and_test_internal(200, r
, test_buffer
,
309 strlen(test_buffer
), &elapsed
);
310 tt_int_op(result
,OP_EQ
, 1);
311 /* elapsed should be the time since the last hit */
312 tt_int_op(elapsed
,OP_EQ
, 100);
315 * Now let's turn the clock back to get coverage on the cache entry from the
316 * future not-supposed-to-happen case.
319 replaycache_add_and_test_internal(150, r
, test_buffer
,
320 strlen(test_buffer
), &elapsed
);
321 /* We should still get a hit */
322 tt_int_op(result
,OP_EQ
, 1);
323 /* ...but it shouldn't let us see a negative elapsed time */
324 tt_int_op(elapsed
,OP_EQ
, 0);
327 if (r
) replaycache_free(r
);
333 test_replaycache_realtime(void *arg
)
335 replaycache_t
*r
= NULL
;
337 * Negative so we fail if replaycache_add_test_and_elapsed() doesn't
343 /* Test the realtime as well as *_internal() entry points */
345 r
= replaycache_new(600, 300);
346 tt_ptr_op(r
, OP_NE
, NULL
);
348 /* This should miss */
350 replaycache_add_and_test(r
, test_buffer
, strlen(test_buffer
));
351 tt_int_op(result
,OP_EQ
, 0);
353 /* This should hit */
355 replaycache_add_and_test(r
, test_buffer
, strlen(test_buffer
));
356 tt_int_op(result
,OP_EQ
, 1);
358 /* This should hit and return a small elapsed time */
360 replaycache_add_test_and_elapsed(r
, test_buffer
,
361 strlen(test_buffer
), &elapsed
);
362 tt_int_op(result
,OP_EQ
, 1);
363 tt_assert(elapsed
>= 0);
364 tt_assert(elapsed
<= 5);
366 /* Scrub it to exercise that entry point too */
367 replaycache_scrub_if_needed(r
);
370 if (r
) replaycache_free(r
);
374 #define REPLAYCACHE_LEGACY(name) \
375 { #name, test_replaycache_ ## name , 0, NULL, NULL }
377 struct testcase_t replaycache_tests
[] = {
378 REPLAYCACHE_LEGACY(alloc
),
379 REPLAYCACHE_LEGACY(badalloc
),
380 REPLAYCACHE_LEGACY(free_null
),
381 REPLAYCACHE_LEGACY(miss
),
382 REPLAYCACHE_LEGACY(hit
),
383 REPLAYCACHE_LEGACY(age
),
384 REPLAYCACHE_LEGACY(elapsed
),
385 REPLAYCACHE_LEGACY(noexpire
),
386 REPLAYCACHE_LEGACY(scrub
),
387 REPLAYCACHE_LEGACY(future
),
388 REPLAYCACHE_LEGACY(realtime
),