test_replay.c 3.7 KB

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