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