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