test_replay.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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)
  18. {
  19. replaycache_t *r = NULL;
  20. r = replaycache_new(600, 300);
  21. test_assert(r != NULL);
  22. done:
  23. if (r) replaycache_free(r);
  24. return;
  25. }
  26. static void
  27. test_replaycache_badalloc(void)
  28. {
  29. replaycache_t *r = NULL;
  30. /* Negative horizon should fail */
  31. r = replaycache_new(-600, 300);
  32. test_assert(r == NULL);
  33. /* Negative interval should get adjusted to zero */
  34. r = replaycache_new(600, -300);
  35. test_assert(r != NULL);
  36. test_eq(r->scrub_interval, 0);
  37. replaycache_free(r);
  38. /* Negative horizon and negative interval should still fail */
  39. r = replaycache_new(-600, -300);
  40. test_assert(r == NULL);
  41. done:
  42. if (r) replaycache_free(r);
  43. return;
  44. }
  45. static void
  46. test_replaycache_free_null(void)
  47. {
  48. replaycache_free(NULL);
  49. /* Assert that we're here without horrible death */
  50. test_assert(1);
  51. done:
  52. return;
  53. }
  54. static void
  55. test_replaycache_miss(void)
  56. {
  57. replaycache_t *r = NULL;
  58. int result;
  59. r = replaycache_new(600, 300);
  60. test_assert(r != NULL);
  61. result =
  62. replaycache_add_and_test_internal(1200, r, test_buffer,
  63. strlen(test_buffer), NULL);
  64. test_eq(result, 0);
  65. /* poke the bad-parameter error case too */
  66. result =
  67. replaycache_add_and_test_internal(1200, NULL, test_buffer,
  68. strlen(test_buffer), NULL);
  69. test_eq(result, 0);
  70. done:
  71. if (r) replaycache_free(r);
  72. return;
  73. }
  74. static void
  75. test_replaycache_hit(void)
  76. {
  77. replaycache_t *r = NULL;
  78. int result;
  79. r = replaycache_new(600, 300);
  80. test_assert(r != NULL);
  81. result =
  82. replaycache_add_and_test_internal(1200, r, test_buffer,
  83. strlen(test_buffer), NULL);
  84. test_eq(result, 0);
  85. result =
  86. replaycache_add_and_test_internal(1300, r, test_buffer,
  87. strlen(test_buffer), NULL);
  88. test_eq(result, 1);
  89. done:
  90. if (r) replaycache_free(r);
  91. return;
  92. }
  93. static void
  94. test_replaycache_age(void)
  95. {
  96. replaycache_t *r = NULL;
  97. int result;
  98. r = replaycache_new(600, 300);
  99. test_assert(r != NULL);
  100. result =
  101. replaycache_add_and_test_internal(1200, r, test_buffer,
  102. strlen(test_buffer), NULL);
  103. test_eq(result, 0);
  104. result =
  105. replaycache_add_and_test_internal(1300, r, test_buffer,
  106. strlen(test_buffer), NULL);
  107. test_eq(result, 1);
  108. result =
  109. replaycache_add_and_test_internal(3000, r, test_buffer,
  110. strlen(test_buffer), NULL);
  111. test_eq(result, 0);
  112. done:
  113. if (r) replaycache_free(r);
  114. return;
  115. }
  116. static void
  117. test_replaycache_elapsed(void)
  118. {
  119. replaycache_t *r = NULL;
  120. int result;
  121. time_t elapsed;
  122. r = replaycache_new(600, 300);
  123. test_assert(r != NULL);
  124. result =
  125. replaycache_add_and_test_internal(1200, r, test_buffer,
  126. strlen(test_buffer), NULL);
  127. test_eq(result, 0);
  128. result =
  129. replaycache_add_and_test_internal(1300, r, test_buffer,
  130. strlen(test_buffer), &elapsed);
  131. test_eq(result, 1);
  132. test_eq(elapsed, 100);
  133. done:
  134. if (r) replaycache_free(r);
  135. return;
  136. }
  137. static void
  138. test_replaycache_noexpire(void)
  139. {
  140. replaycache_t *r = NULL;
  141. int result;
  142. r = replaycache_new(0, 0);
  143. test_assert(r != NULL);
  144. result =
  145. replaycache_add_and_test_internal(1200, r, test_buffer,
  146. strlen(test_buffer), NULL);
  147. test_eq(result, 0);
  148. result =
  149. replaycache_add_and_test_internal(1300, r, test_buffer,
  150. strlen(test_buffer), NULL);
  151. test_eq(result, 1);
  152. result =
  153. replaycache_add_and_test_internal(3000, r, test_buffer,
  154. strlen(test_buffer), NULL);
  155. test_eq(result, 1);
  156. done:
  157. if (r) replaycache_free(r);
  158. return;
  159. }
  160. static void
  161. test_replaycache_scrub(void)
  162. {
  163. replaycache_t *r = NULL;
  164. int result;
  165. r = replaycache_new(600, 300);
  166. test_assert(r != NULL);
  167. /* Set up like in test_replaycache_hit() */
  168. result =
  169. replaycache_add_and_test_internal(100, r, test_buffer,
  170. strlen(test_buffer), NULL);
  171. test_eq(result, 0);
  172. result =
  173. replaycache_add_and_test_internal(200, r, test_buffer,
  174. strlen(test_buffer), NULL);
  175. test_eq(result, 1);
  176. /*
  177. * Poke a few replaycache_scrub_if_needed_internal() error cases that
  178. * can't happen through replaycache_add_and_test_internal()
  179. */
  180. /* Null cache */
  181. replaycache_scrub_if_needed_internal(300, NULL);
  182. /* Assert we're still here */
  183. test_assert(1);
  184. /* Make sure we hit the aging-out case too */
  185. replaycache_scrub_if_needed_internal(1500, r);
  186. /* Assert that we aged it */
  187. test_eq(digestmap_size(r->digests_seen), 0);
  188. done:
  189. if (r) replaycache_free(r);
  190. return;
  191. }
  192. static void
  193. test_replaycache_future(void)
  194. {
  195. replaycache_t *r = NULL;
  196. int result;
  197. time_t elapsed = 0;
  198. r = replaycache_new(600, 300);
  199. test_assert(r != NULL);
  200. /* Set up like in test_replaycache_hit() */
  201. result =
  202. replaycache_add_and_test_internal(100, r, test_buffer,
  203. strlen(test_buffer), &elapsed);
  204. test_eq(result, 0);
  205. /* elapsed should still be 0, since it wasn't written */
  206. test_eq(elapsed, 0);
  207. result =
  208. replaycache_add_and_test_internal(200, r, test_buffer,
  209. strlen(test_buffer), &elapsed);
  210. test_eq(result, 1);
  211. /* elapsed should be the time since the last hit */
  212. test_eq(elapsed, 100);
  213. /*
  214. * Now let's turn the clock back to get coverage on the cache entry from the
  215. * future not-supposed-to-happen case.
  216. */
  217. result =
  218. replaycache_add_and_test_internal(150, r, test_buffer,
  219. strlen(test_buffer), &elapsed);
  220. /* We should still get a hit */
  221. test_eq(result, 1);
  222. /* ...but it shouldn't let us see a negative elapsed time */
  223. test_eq(elapsed, 0);
  224. done:
  225. if (r) replaycache_free(r);
  226. return;
  227. }
  228. static void
  229. test_replaycache_realtime(void)
  230. {
  231. replaycache_t *r = NULL;
  232. /*
  233. * Negative so we fail if replaycache_add_test_and_elapsed() doesn't
  234. * write to elapsed.
  235. */
  236. time_t elapsed = -1;
  237. int result;
  238. /* Test the realtime as well as *_internal() entry points */
  239. r = replaycache_new(600, 300);
  240. test_assert(r != NULL);
  241. /* This should miss */
  242. result =
  243. replaycache_add_and_test(r, test_buffer, strlen(test_buffer));
  244. test_eq(result, 0);
  245. /* This should hit */
  246. result =
  247. replaycache_add_and_test(r, test_buffer, strlen(test_buffer));
  248. test_eq(result, 1);
  249. /* This should hit and return a small elapsed time */
  250. result =
  251. replaycache_add_test_and_elapsed(r, test_buffer,
  252. strlen(test_buffer), &elapsed);
  253. test_eq(result, 1);
  254. test_assert(elapsed >= 0);
  255. test_assert(elapsed <= 5);
  256. /* Scrub it to exercise that entry point too */
  257. replaycache_scrub_if_needed(r);
  258. done:
  259. if (r) replaycache_free(r);
  260. return;
  261. }
  262. #define REPLAYCACHE_LEGACY(name) \
  263. { #name, legacy_test_helper, 0, &legacy_setup, test_replaycache_ ## name }
  264. struct testcase_t replaycache_tests[] = {
  265. REPLAYCACHE_LEGACY(alloc),
  266. REPLAYCACHE_LEGACY(badalloc),
  267. REPLAYCACHE_LEGACY(free_null),
  268. REPLAYCACHE_LEGACY(miss),
  269. REPLAYCACHE_LEGACY(hit),
  270. REPLAYCACHE_LEGACY(age),
  271. REPLAYCACHE_LEGACY(elapsed),
  272. REPLAYCACHE_LEGACY(noexpire),
  273. REPLAYCACHE_LEGACY(scrub),
  274. REPLAYCACHE_LEGACY(future),
  275. REPLAYCACHE_LEGACY(realtime),
  276. END_OF_TESTCASES
  277. };