roc_driver.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * roc_driver.c
  3. *
  4. * test driver for rollover counter replay implementation
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #include <stdio.h>
  45. /*
  46. * defining ROC_TEST causes small datatypes to be used in
  47. * xtd_seq_num_t - this allows the functions to be exhaustively tested.
  48. */
  49. #if ROC_NEEDS_TO_BE_TESTED
  50. #define ROC_TEST
  51. #endif
  52. #include "rdbx.h"
  53. #include "ut_sim.h"
  54. err_status_t
  55. roc_test(int num_trials);
  56. int
  57. main (void) {
  58. err_status_t status;
  59. printf("rollover counter test driver\n"
  60. "David A. McGrew\n"
  61. "Cisco Systems, Inc.\n");
  62. printf("testing index functions...");
  63. status = roc_test(1 << 18);
  64. if (status) {
  65. printf("failed\n");
  66. exit(status);
  67. }
  68. printf("passed\n");
  69. return 0;
  70. }
  71. #define ROC_VERBOSE 0
  72. err_status_t
  73. roc_test(int num_trials) {
  74. xtd_seq_num_t local, est, ref;
  75. ut_connection utc;
  76. int i, num_bad_est = 0;
  77. int delta;
  78. uint32_t ircvd;
  79. double failure_rate;
  80. index_init(&local);
  81. index_init(&ref);
  82. index_init(&est);
  83. printf("\n\ttesting sequential insertion...");
  84. for (i=0; i < 2048; i++) {
  85. delta = index_guess(&local, &est, (uint16_t) ref);
  86. #if ROC_VERBOSE
  87. printf("%lld, %lld, %d\n", ref, est, i);
  88. #endif
  89. if (ref != est) {
  90. #if ROC_VERBOSE
  91. printf(" *bad estimate*\n");
  92. #endif
  93. ++num_bad_est;
  94. }
  95. index_advance(&ref, 1);
  96. }
  97. failure_rate = (double) num_bad_est / num_trials;
  98. if (failure_rate > 0.01) {
  99. printf("error: failure rate too high (%d bad estimates in %d trials)\n",
  100. num_bad_est, num_trials);
  101. return err_status_algo_fail;
  102. }
  103. printf("done\n");
  104. printf("\ttesting non-sequential insertion...");
  105. index_init(&local);
  106. index_init(&ref);
  107. index_init(&est);
  108. ut_init(&utc);
  109. for (i=0; i < num_trials; i++) {
  110. /* get next seq num from unreliable transport simulator */
  111. ircvd = ut_next_index(&utc);
  112. /* set ref to value of ircvd */
  113. ref = ircvd;
  114. /* estimate index based on low bits of ircvd */
  115. delta = index_guess(&local, &est, (uint16_t) ref);
  116. #if ROC_VERBOSE
  117. printf("ref: %lld, local: %lld, est: %lld, ircvd: %d, delta: %d\n",
  118. ref, local, est, ircvd, delta);
  119. #endif
  120. if (local + delta != est) {
  121. printf(" *bad delta*: local %llu + delta %d != est %llu\n",
  122. (unsigned long long)local, delta, (unsigned long long)est);
  123. return err_status_algo_fail;
  124. }
  125. /* now update local xtd_seq_num_t as necessary */
  126. if (delta > 0)
  127. index_advance(&local, delta);
  128. if (ref != est) {
  129. #if ROC_VERBOSE
  130. printf(" *bad estimate*\n");
  131. #endif
  132. /* record failure event */
  133. ++num_bad_est;
  134. /* reset local value to correct value */
  135. local = ref;
  136. }
  137. }
  138. failure_rate = (double) num_bad_est / num_trials;
  139. if (failure_rate > 0.01) {
  140. printf("error: failure rate too high (%d bad estimates in %d trials)\n",
  141. num_bad_est, num_trials);
  142. return err_status_algo_fail;
  143. }
  144. printf("done\n");
  145. return err_status_ok;
  146. }