roc_driver.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2017, 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. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47. #include <stdio.h>
  48. /*
  49. * defining ROC_TEST causes small datatypes to be used in
  50. * srtp_xtd_seq_num_t - this allows the functions to be exhaustively tested.
  51. */
  52. #if ROC_NEEDS_TO_BE_TESTED
  53. #define ROC_TEST
  54. #endif
  55. #include "rdbx.h"
  56. #include "ut_sim.h"
  57. srtp_err_status_t roc_test(int num_trials);
  58. int main(void)
  59. {
  60. srtp_err_status_t status;
  61. printf("rollover counter test driver\n"
  62. "David A. McGrew\n"
  63. "Cisco Systems, Inc.\n");
  64. printf("testing index functions...");
  65. status = roc_test(1 << 18);
  66. if (status) {
  67. printf("failed\n");
  68. exit(status);
  69. }
  70. printf("passed\n");
  71. return 0;
  72. }
  73. #define ROC_VERBOSE 0
  74. srtp_err_status_t roc_test(int num_trials)
  75. {
  76. srtp_xtd_seq_num_t local, est, ref;
  77. ut_connection utc;
  78. int i, num_bad_est = 0;
  79. int delta;
  80. uint32_t ircvd;
  81. double failure_rate;
  82. srtp_index_init(&local);
  83. srtp_index_init(&ref);
  84. srtp_index_init(&est);
  85. printf("\n\ttesting sequential insertion...");
  86. for (i = 0; i < 2048; i++) {
  87. srtp_index_guess(&local, &est, (uint16_t)ref);
  88. #if ROC_VERBOSE
  89. printf("%lld, %lld, %d\n", ref, est, i);
  90. #endif
  91. if (ref != est) {
  92. #if ROC_VERBOSE
  93. printf(" *bad estimate*\n");
  94. #endif
  95. ++num_bad_est;
  96. }
  97. srtp_index_advance(&ref, 1);
  98. }
  99. failure_rate = (double)num_bad_est / num_trials;
  100. if (failure_rate > 0.01) {
  101. printf("error: failure rate too high (%d bad estimates in %d trials)\n",
  102. num_bad_est, num_trials);
  103. return srtp_err_status_algo_fail;
  104. }
  105. printf("done\n");
  106. printf("\ttesting non-sequential insertion...");
  107. srtp_index_init(&local);
  108. srtp_index_init(&ref);
  109. srtp_index_init(&est);
  110. ut_init(&utc);
  111. for (i = 0; i < num_trials; i++) {
  112. /* get next seq num from unreliable transport simulator */
  113. ircvd = ut_next_index(&utc);
  114. /* set ref to value of ircvd */
  115. ref = ircvd;
  116. /* estimate index based on low bits of ircvd */
  117. delta = srtp_index_guess(&local, &est, (uint16_t)ref);
  118. #if ROC_VERBOSE
  119. printf("ref: %lld, local: %lld, est: %lld, ircvd: %d, delta: %d\n", ref,
  120. local, est, ircvd, delta);
  121. #endif
  122. if (local + delta != est) {
  123. printf(" *bad delta*: local %llu + delta %d != est %llu\n",
  124. (unsigned long long)local, delta, (unsigned long long)est);
  125. return srtp_err_status_algo_fail;
  126. }
  127. /* now update local srtp_xtd_seq_num_t as necessary */
  128. if (delta > 0)
  129. srtp_index_advance(&local, delta);
  130. if (ref != est) {
  131. #if ROC_VERBOSE
  132. printf(" *bad estimate*\n");
  133. #endif
  134. /* record failure event */
  135. ++num_bad_est;
  136. /* reset local value to correct value */
  137. local = ref;
  138. }
  139. }
  140. failure_rate = (double)num_bad_est / num_trials;
  141. if (failure_rate > 0.01) {
  142. printf("error: failure rate too high (%d bad estimates in %d trials)\n",
  143. num_bad_est, num_trials);
  144. return srtp_err_status_algo_fail;
  145. }
  146. printf("done\n");
  147. return srtp_err_status_ok;
  148. }