test_replay.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (c) 2012, 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. if (!r) goto done;
  23. done:
  24. if (r) replaycache_free(r);
  25. return;
  26. }
  27. static void
  28. test_replaycache_miss(void)
  29. {
  30. replaycache_t *r = NULL;
  31. int result;
  32. r = replaycache_new(600, 300);
  33. test_assert(r != NULL);
  34. if (!r) goto done;
  35. result =
  36. replaycache_add_and_test_internal(1200, r, test_buffer,
  37. (int)strlen(test_buffer), NULL);
  38. test_eq(result, 0);
  39. done:
  40. if (r) replaycache_free(r);
  41. return;
  42. }
  43. static void
  44. test_replaycache_hit(void)
  45. {
  46. replaycache_t *r = NULL;
  47. int result;
  48. r = replaycache_new(600, 300);
  49. test_assert(r != NULL);
  50. if (!r) goto done;
  51. result =
  52. replaycache_add_and_test_internal(1200, r, test_buffer,
  53. (int)strlen(test_buffer), NULL);
  54. test_eq(result, 0);
  55. result =
  56. replaycache_add_and_test_internal(1300, r, test_buffer,
  57. (int)strlen(test_buffer), NULL);
  58. test_eq(result, 1);
  59. done:
  60. if (r) replaycache_free(r);
  61. return;
  62. }
  63. static void
  64. test_replaycache_age(void)
  65. {
  66. replaycache_t *r = NULL;
  67. int result;
  68. r = replaycache_new(600, 300);
  69. test_assert(r != NULL);
  70. if (!r) goto done;
  71. result =
  72. replaycache_add_and_test_internal(1200, r, test_buffer,
  73. (int)strlen(test_buffer), NULL);
  74. test_eq(result, 0);
  75. result =
  76. replaycache_add_and_test_internal(1300, r, test_buffer,
  77. (int)strlen(test_buffer), NULL);
  78. test_eq(result, 1);
  79. result =
  80. replaycache_add_and_test_internal(3000, r, test_buffer,
  81. (int)strlen(test_buffer), NULL);
  82. test_eq(result, 0);
  83. done:
  84. if (r) replaycache_free(r);
  85. return;
  86. }
  87. static void
  88. test_replaycache_elapsed(void)
  89. {
  90. replaycache_t *r = NULL;
  91. int result;
  92. time_t elapsed;
  93. r = replaycache_new(600, 300);
  94. test_assert(r != NULL);
  95. if (!r) goto done;
  96. result =
  97. replaycache_add_and_test_internal(1200, r, test_buffer,
  98. (int)strlen(test_buffer), NULL);
  99. test_eq(result, 0);
  100. result =
  101. replaycache_add_and_test_internal(1300, r, test_buffer,
  102. (int)strlen(test_buffer), &elapsed);
  103. test_eq(result, 1);
  104. test_eq(elapsed, 100);
  105. done:
  106. if (r) replaycache_free(r);
  107. return;
  108. }
  109. static void
  110. test_replaycache_noexpire(void)
  111. {
  112. replaycache_t *r = NULL;
  113. int result;
  114. r = replaycache_new(0, 0);
  115. test_assert(r != NULL);
  116. if (!r) goto done;
  117. result =
  118. replaycache_add_and_test_internal(1200, r, test_buffer,
  119. (int)strlen(test_buffer), NULL);
  120. test_eq(result, 0);
  121. result =
  122. replaycache_add_and_test_internal(1300, r, test_buffer,
  123. (int)strlen(test_buffer), NULL);
  124. test_eq(result, 1);
  125. result =
  126. replaycache_add_and_test_internal(3000, r, test_buffer,
  127. (int)strlen(test_buffer), NULL);
  128. test_eq(result, 1);
  129. done:
  130. if (r) replaycache_free(r);
  131. return;
  132. }
  133. #define REPLAYCACHE_LEGACY(name) \
  134. { #name, legacy_test_helper, 0, &legacy_setup, test_replaycache_ ## name }
  135. struct testcase_t replaycache_tests[] = {
  136. REPLAYCACHE_LEGACY(alloc),
  137. REPLAYCACHE_LEGACY(miss),
  138. REPLAYCACHE_LEGACY(hit),
  139. REPLAYCACHE_LEGACY(age),
  140. REPLAYCACHE_LEGACY(elapsed),
  141. REPLAYCACHE_LEGACY(noexpire),
  142. END_OF_TESTCASES
  143. };