4
* driver for the rdbx implementation (replay database with extended range)
12
* Copyright (c) 2001-2006, Cisco Systems, Inc.
13
* All rights reserved.
15
* Redistribution and use in source and binary forms, with or without
16
* modification, are permitted provided that the following conditions
19
* Redistributions of source code must retain the above copyright
20
* notice, this list of conditions and the following disclaimer.
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.
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.
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.
46
#include <stdio.h> /* for printf() */
47
#include "getopt_s.h" /* for local getopt() */
52
#error "rdbx_t won't work with ROC_TEST - bitmask same size as seq_median"
58
test_replay_dbx(int num_trials);
61
rdbx_check_adds_per_second(int num_trials);
64
usage(char *prog_name) {
65
printf("usage: %s [ -t | -v ]\n", prog_name);
70
main (int argc, char *argv[]) {
74
unsigned do_timing_test = 0;
75
unsigned do_validation = 0;
77
/* process input arguments */
79
q = getopt_s(argc, argv, "tv");
94
printf("rdbx (replay database w/ extended range) test driver\n"
96
"Cisco Systems, Inc.\n");
98
if (!do_validation && !do_timing_test)
102
printf("testing rdbx_t...\n");
104
status = test_replay_dbx(1 << 12);
112
if (do_timing_test) {
113
rate = rdbx_check_adds_per_second(1 << 18);
114
printf("rdbx_check/replay_adds per second: %e\n", rate);
121
print_rdbx(rdbx_t *rdbx) {
122
printf("rdbx: {%llu, %s}\n",
123
(unsigned long long)(rdbx->index), v128_bit_string(&rdbx->bitmask));
128
* rdbx_check_add(rdbx, idx) checks a known-to-be-good idx against
129
* rdbx, then adds it. if a failure is detected (i.e., the check
130
* indicates that the value is already in rdbx) then
131
* err_status_algo_fail is returned.
136
rdbx_check_add(rdbx_t *rdbx, uint32_t idx) {
140
delta = index_guess(&rdbx->index, &est, idx);
142
if (rdbx_check(rdbx, delta) != err_status_ok) {
143
printf("replay_check failed at index %u\n", idx);
144
return err_status_algo_fail;
148
* in practice, we'd authenticate the packet containing idx, using
149
* the estimated value est, at this point
152
if (rdbx_add_index(rdbx, delta) != err_status_ok) {
153
printf("rdbx_add_index failed at index %u\n", idx);
154
return err_status_algo_fail;
157
return err_status_ok;
161
* rdbx_check_expect_failure(rdbx_t *rdbx, uint32_t idx)
163
* checks that a sequence number idx is in the replay database
164
* and thus will be rejected
168
rdbx_check_expect_failure(rdbx_t *rdbx, uint32_t idx) {
173
delta = index_guess(&rdbx->index, &est, idx);
175
status = rdbx_check(rdbx, delta);
176
if (status == err_status_ok) {
177
printf("delta: %d ", delta);
178
printf("replay_check failed at index %u (false positive)\n", idx);
179
return err_status_algo_fail;
182
return err_status_ok;
186
rdbx_check_unordered(rdbx_t *rdbx, uint32_t idx) {
189
rstat = rdbx_check(rdbx, idx);
190
if ((rstat != err_status_ok) && (rstat != err_status_replay_old)) {
191
printf("replay_check_unordered failed at index %u\n", idx);
192
return err_status_algo_fail;
194
return err_status_ok;
200
test_replay_dbx(int num_trials) {
207
status = rdbx_init(&rdbx);
209
printf("replay_init failed with error code %d\n", status);
214
* test sequential insertion
216
printf("\ttesting sequential insertion...");
217
for (idx=0; idx < num_trials; idx++) {
218
status = rdbx_check_add(&rdbx, idx);
225
* test for false positives by checking all of the index
226
* values which we've just added
228
* note that we limit the number of trials here, since allowing the
229
* rollover counter to roll over would defeat this test
231
num_fp_trials = num_trials % 0x10000;
232
if (num_fp_trials == 0) {
233
printf("warning: no false positive tests performed\n");
235
printf("\ttesting for false positives...");
236
for (idx=0; idx < num_fp_trials; idx++) {
237
status = rdbx_check_expect_failure(&rdbx, idx);
244
if (rdbx_init(&rdbx) != err_status_ok) {
245
printf("replay_init failed\n");
246
return err_status_init_fail;
250
* test non-sequential insertion
252
* this test covers only fase negatives, since the values returned
253
* by ut_next_index(...) are distinct
257
printf("\ttesting non-sequential insertion...");
258
for (idx=0; idx < num_trials; idx++) {
259
ircvd = ut_next_index(&utc);
260
status = rdbx_check_unordered(&rdbx, ircvd);
266
return err_status_ok;
271
#include <time.h> /* for clock() */
272
#include <stdlib.h> /* for random() */
275
rdbx_check_adds_per_second(int num_trials) {
281
int failures; /* count number of failures */
283
if (rdbx_init(&rdbx) != err_status_ok) {
284
printf("replay_init failed\n");
290
for(i=0; i < num_trials; i++) {
292
delta = index_guess(&rdbx.index, &est, i);
294
if (rdbx_check(&rdbx, delta) != err_status_ok)
297
if (rdbx_add_index(&rdbx, delta) != err_status_ok)
300
timer = clock() - timer;
302
printf("number of failures: %d \n", failures);
304
return (double) CLOCKS_PER_SEC * num_trials / timer;