4
* A driver for the replay_database implementation
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.
52
* num_trials defines the number of trials that are used in the
53
* validation functions below
56
unsigned num_trials = 1 << 16;
62
rdb_check_adds_per_second(void);
68
printf("testing anti-replay database (rdb_t)...\n");
76
printf("rdb_check/rdb_adds per second: %e\n",
77
rdb_check_adds_per_second());
84
print_rdb(rdb_t *rdb) {
85
printf("rdb: {%u, %s}\n", rdb->window_start, v128_bit_string(&rdb->bitmask));
89
rdb_check_add(rdb_t *rdb, uint32_t idx) {
91
if (rdb_check(rdb, idx) != err_status_ok) {
92
printf("rdb_check failed at index %u\n", idx);
93
return err_status_fail;
95
if (rdb_add_index(rdb, idx) != err_status_ok) {
96
printf("rdb_add_index failed at index %u\n", idx);
97
return err_status_fail;
100
return err_status_ok;
104
rdb_check_expect_failure(rdb_t *rdb, uint32_t idx) {
107
err = rdb_check(rdb, idx);
108
if ((err != err_status_replay_old) && (err != err_status_replay_fail)) {
109
printf("rdb_check failed at index %u (false positive)\n", idx);
110
return err_status_fail;
113
return err_status_ok;
117
rdb_check_unordered(rdb_t *rdb, uint32_t idx) {
120
/* printf("index: %u\n", idx); */
121
rstat = rdb_check(rdb, idx);
122
if ((rstat != err_status_ok) && (rstat != err_status_replay_old)) {
123
printf("rdb_check_unordered failed at index %u\n", idx);
126
return err_status_ok;
136
if (rdb_init(&rdb) != err_status_ok) {
137
printf("rdb_init failed\n");
138
return err_status_init_fail;
141
/* test sequential insertion */
142
for (idx=0; idx < num_trials; idx++) {
143
err = rdb_check_add(&rdb, idx);
148
/* test for false positives */
149
for (idx=0; idx < num_trials; idx++) {
150
err = rdb_check_expect_failure(&rdb, idx);
156
if (rdb_init(&rdb) != err_status_ok) {
157
printf("rdb_init failed\n");
158
return err_status_fail;
161
/* test non-sequential insertion */
164
for (idx=0; idx < num_trials; idx++) {
165
ircvd = ut_next_index(&utc);
166
err = rdb_check_unordered(&rdb, ircvd);
171
return err_status_ok;
174
#include <time.h> /* for clock() */
175
#include <stdlib.h> /* for random() */
177
#define REPLAY_NUM_TRIALS 10000000
180
rdb_check_adds_per_second(void) {
184
int failures; /* count number of failures */
186
if (rdb_init(&rdb) != err_status_ok) {
187
printf("rdb_init failed\n");
192
for(i=0; i < REPLAY_NUM_TRIALS; i+=3) {
193
if (rdb_check(&rdb, i+2) != err_status_ok)
195
if (rdb_add_index(&rdb, i+2) != err_status_ok)
197
if (rdb_check(&rdb, i+1) != err_status_ok)
199
if (rdb_add_index(&rdb, i+1) != err_status_ok)
201
if (rdb_check(&rdb, i) != err_status_ok)
203
if (rdb_add_index(&rdb, i) != err_status_ok)
206
timer = clock() - timer;
208
return (double) CLOCKS_PER_SEC * REPLAY_NUM_TRIALS / timer;