test_replay.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* Copyright (c) 2012-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define REPLAYCACHE_PRIVATE
  4. #include "orconfig.h"
  5. #include "or.h"
  6. #include "replaycache.h"
  7. #include "test.h"
  8. static const char *test_buffer =
  9. "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed do eiusmod"
  10. " tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim"
  11. " veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea"
  12. " commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
  13. " velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
  14. " occaecat cupidatat non proident, sunt in culpa qui officia deserunt"
  15. " mollit anim id est laborum.";
  16. static void
  17. test_replaycache_alloc(void *arg)
  18. {
  19. replaycache_t *r = NULL;
  20. (void)arg;
  21. r = replaycache_new(600, 300);
  22. tt_assert(r != NULL);
  23. done:
  24. if (r) replaycache_free(r);
  25. return;
  26. }
  27. static void
  28. test_replaycache_badalloc(void *arg)
  29. {
  30. replaycache_t *r = NULL;
  31. /* Negative horizon should fail */
  32. (void)arg;
  33. r = replaycache_new(-600, 300);
  34. tt_assert(r == NULL);
  35. /* Negative interval should get adjusted to zero */
  36. r = replaycache_new(600, -300);
  37. tt_assert(r != NULL);
  38. tt_int_op(r->scrub_interval,==, 0);
  39. replaycache_free(r);
  40. /* Negative horizon and negative interval should still fail */
  41. r = replaycache_new(-600, -300);
  42. tt_assert(r == NULL);
  43. done:
  44. if (r) replaycache_free(r);
  45. return;
  46. }
  47. static void
  48. test_replaycache_free_null(void *arg)
  49. {
  50. (void)arg;
  51. replaycache_free(NULL);
  52. /* Assert that we're here without horrible death */
  53. tt_assert(1);
  54. done:
  55. return;
  56. }
  57. static void
  58. test_replaycache_miss(void *arg)
  59. {
  60. replaycache_t *r = NULL;
  61. int result;
  62. (void)arg;
  63. r = replaycache_new(600, 300);
  64. tt_assert(r != NULL);
  65. result =
  66. replaycache_add_and_test_internal(1200, r, test_buffer,
  67. strlen(test_buffer), NULL);
  68. tt_int_op(result,==, 0);
  69. /* poke the bad-parameter error case too */
  70. result =
  71. replaycache_add_and_test_internal(1200, NULL, test_buffer,
  72. strlen(test_buffer), NULL);
  73. tt_int_op(result,==, 0);
  74. done:
  75. if (r) replaycache_free(r);
  76. return;
  77. }
  78. static void
  79. test_replaycache_hit(void *arg)
  80. {
  81. replaycache_t *r = NULL;
  82. int result;
  83. (void)arg;
  84. r = replaycache_new(600, 300);
  85. tt_assert(r != NULL);
  86. result =
  87. replaycache_add_and_test_internal(1200, r, test_buffer,
  88. strlen(test_buffer), NULL);
  89. tt_int_op(result,==, 0);
  90. result =
  91. replaycache_add_and_test_internal(1300, r, test_buffer,
  92. strlen(test_buffer), NULL);
  93. tt_int_op(result,==, 1);
  94. done:
  95. if (r) replaycache_free(r);
  96. return;
  97. }
  98. static void
  99. test_replaycache_age(void *arg)
  100. {
  101. replaycache_t *r = NULL;
  102. int result;
  103. (void)arg;
  104. r = replaycache_new(600, 300);
  105. tt_assert(r != NULL);
  106. result =
  107. replaycache_add_and_test_internal(1200, r, test_buffer,
  108. strlen(test_buffer), NULL);
  109. tt_int_op(result,==, 0);
  110. result =
  111. replaycache_add_and_test_internal(1300, r, test_buffer,
  112. strlen(test_buffer), NULL);
  113. tt_int_op(result,==, 1);
  114. result =
  115. replaycache_add_and_test_internal(3000, r, test_buffer,
  116. strlen(test_buffer), NULL);
  117. tt_int_op(result,==, 0);
  118. done:
  119. if (r) replaycache_free(r);
  120. return;
  121. }
  122. static void
  123. test_replaycache_elapsed(void *arg)
  124. {
  125. replaycache_t *r = NULL;
  126. int result;
  127. time_t elapsed;
  128. (void)arg;
  129. r = replaycache_new(600, 300);
  130. tt_assert(r != NULL);
  131. result =
  132. replaycache_add_and_test_internal(1200, r, test_buffer,
  133. strlen(test_buffer), NULL);
  134. tt_int_op(result,==, 0);
  135. result =
  136. replaycache_add_and_test_internal(1300, r, test_buffer,
  137. strlen(test_buffer), &elapsed);
  138. tt_int_op(result,==, 1);
  139. tt_int_op(elapsed,==, 100);
  140. done:
  141. if (r) replaycache_free(r);
  142. return;
  143. }
  144. static void
  145. test_replaycache_noexpire(void *arg)
  146. {
  147. replaycache_t *r = NULL;
  148. int result;
  149. (void)arg;
  150. r = replaycache_new(0, 0);
  151. tt_assert(r != NULL);
  152. result =
  153. replaycache_add_and_test_internal(1200, r, test_buffer,
  154. strlen(test_buffer), NULL);
  155. tt_int_op(result,==, 0);
  156. result =
  157. replaycache_add_and_test_internal(1300, r, test_buffer,
  158. strlen(test_buffer), NULL);
  159. tt_int_op(result,==, 1);
  160. result =
  161. replaycache_add_and_test_internal(3000, r, test_buffer,
  162. strlen(test_buffer), NULL);
  163. tt_int_op(result,==, 1);
  164. done:
  165. if (r) replaycache_free(r);
  166. return;
  167. }
  168. static void
  169. test_replaycache_scrub(void *arg)
  170. {
  171. replaycache_t *r = NULL;
  172. int result;
  173. (void)arg;
  174. r = replaycache_new(600, 300);
  175. tt_assert(r != NULL);
  176. /* Set up like in test_replaycache_hit() */
  177. result =
  178. replaycache_add_and_test_internal(100, r, test_buffer,
  179. strlen(test_buffer), NULL);
  180. tt_int_op(result,==, 0);
  181. result =
  182. replaycache_add_and_test_internal(200, r, test_buffer,
  183. strlen(test_buffer), NULL);
  184. tt_int_op(result,==, 1);
  185. /*
  186. * Poke a few replaycache_scrub_if_needed_internal() error cases that
  187. * can't happen through replaycache_add_and_test_internal()
  188. */
  189. /* Null cache */
  190. replaycache_scrub_if_needed_internal(300, NULL);
  191. /* Assert we're still here */
  192. tt_assert(1);
  193. /* Make sure we hit the aging-out case too */
  194. replaycache_scrub_if_needed_internal(1500, r);
  195. /* Assert that we aged it */
  196. tt_int_op(digestmap_size(r->digests_seen),==, 0);
  197. done:
  198. if (r) replaycache_free(r);
  199. return;
  200. }
  201. static void
  202. test_replaycache_future(void *arg)
  203. {
  204. replaycache_t *r = NULL;
  205. int result;
  206. time_t elapsed = 0;
  207. (void)arg;
  208. r = replaycache_new(600, 300);
  209. tt_assert(r != NULL);
  210. /* Set up like in test_replaycache_hit() */
  211. result =
  212. replaycache_add_and_test_internal(100, r, test_buffer,
  213. strlen(test_buffer), &elapsed);
  214. tt_int_op(result,==, 0);
  215. /* elapsed should still be 0, since it wasn't written */
  216. tt_int_op(elapsed,==, 0);
  217. result =
  218. replaycache_add_and_test_internal(200, r, test_buffer,
  219. strlen(test_buffer), &elapsed);
  220. tt_int_op(result,==, 1);
  221. /* elapsed should be the time since the last hit */
  222. tt_int_op(elapsed,==, 100);
  223. /*
  224. * Now let's turn the clock back to get coverage on the cache entry from the
  225. * future not-supposed-to-happen case.
  226. */
  227. result =
  228. replaycache_add_and_test_internal(150, r, test_buffer,
  229. strlen(test_buffer), &elapsed);
  230. /* We should still get a hit */
  231. tt_int_op(result,==, 1);
  232. /* ...but it shouldn't let us see a negative elapsed time */
  233. tt_int_op(elapsed,==, 0);
  234. done:
  235. if (r) replaycache_free(r);
  236. return;
  237. }
  238. static void
  239. test_replaycache_realtime(void *arg)
  240. {
  241. replaycache_t *r = NULL;
  242. /*
  243. * Negative so we fail if replaycache_add_test_and_elapsed() doesn't
  244. * write to elapsed.
  245. */
  246. time_t elapsed = -1;
  247. int result;
  248. /* Test the realtime as well as *_internal() entry points */
  249. (void)arg;
  250. r = replaycache_new(600, 300);
  251. tt_assert(r != NULL);
  252. /* This should miss */
  253. result =
  254. replaycache_add_and_test(r, test_buffer, strlen(test_buffer));
  255. tt_int_op(result,==, 0);
  256. /* This should hit */
  257. result =
  258. replaycache_add_and_test(r, test_buffer, strlen(test_buffer));
  259. tt_int_op(result,==, 1);
  260. /* This should hit and return a small elapsed time */
  261. result =
  262. replaycache_add_test_and_elapsed(r, test_buffer,
  263. strlen(test_buffer), &elapsed);
  264. tt_int_op(result,==, 1);
  265. tt_assert(elapsed >= 0);
  266. tt_assert(elapsed <= 5);
  267. /* Scrub it to exercise that entry point too */
  268. replaycache_scrub_if_needed(r);
  269. done:
  270. if (r) replaycache_free(r);
  271. return;
  272. }
  273. #define REPLAYCACHE_LEGACY(name) \
  274. { #name, test_replaycache_ ## name , 0, NULL, NULL }
  275. struct testcase_t replaycache_tests[] = {
  276. REPLAYCACHE_LEGACY(alloc),
  277. REPLAYCACHE_LEGACY(badalloc),
  278. REPLAYCACHE_LEGACY(free_null),
  279. REPLAYCACHE_LEGACY(miss),
  280. REPLAYCACHE_LEGACY(hit),
  281. REPLAYCACHE_LEGACY(age),
  282. REPLAYCACHE_LEGACY(elapsed),
  283. REPLAYCACHE_LEGACY(noexpire),
  284. REPLAYCACHE_LEGACY(scrub),
  285. REPLAYCACHE_LEGACY(future),
  286. REPLAYCACHE_LEGACY(realtime),
  287. END_OF_TESTCASES
  288. };