double rdbx_check_adds_per_second(int num_trials, unsigned long ws) { uint32_t i; int delta; rdbx_t rdbx; xtd_seq_num_t est; clock_t timer; int failures; /* count number of failures */ if (rdbx_init(&rdbx, ws) != err_status_ok) { printf("replay_init failed\n"); exit(1); } failures = 0; timer = clock(); for(i=0; i < (uint32_t)num_trials; i++) { delta = index_guess(&rdbx.index, &est, i); if (rdbx_check(&rdbx, delta) != err_status_ok) ++failures; else if (rdbx_add_index(&rdbx, delta) != err_status_ok) ++failures; } timer = clock() - timer; printf("number of failures: %d \n", failures); rdbx_uninit(&rdbx); return (double) CLOCKS_PER_SEC * num_trials / timer; }
err_status_t rdbx_check_expect_failure(rdbx_t *rdbx, uint32_t idx) { int delta; xtd_seq_num_t est; err_status_t status; delta = index_guess(&rdbx->index, &est, idx); status = rdbx_check(rdbx, delta); if (status == err_status_ok) { printf("delta: %d ", delta); printf("replay_check failed at index %u (false positive)\n", idx); return err_status_algo_fail; } return err_status_ok; }
err_status_t rdbx_check_add(rdbx_t *rdbx, uint32_t idx) { int delta; xtd_seq_num_t est; delta = index_guess(&rdbx->index, &est, idx); if (rdbx_check(rdbx, delta) != err_status_ok) { printf("replay_check failed at index %u\n", idx); return err_status_algo_fail; } /* * in practice, we'd authenticate the packet containing idx, using * the estimated value est, at this point */ if (rdbx_add_index(rdbx, delta) != err_status_ok) { printf("rdbx_add_index failed at index %u\n", idx); return err_status_algo_fail; } return err_status_ok; }
err_status_t roc_test(int num_trials) { xtd_seq_num_t local, est, ref; ut_connection utc; int i, num_bad_est = 0; int delta; uint32_t ircvd; double failure_rate; index_init(&local); index_init(&ref); index_init(&est); printf("\n\ttesting sequential insertion..."); for (i=0; i < 2048; i++) { delta = index_guess(&local, &est, (uint16_t) ref); #if ROC_VERBOSE printf("%lld, %lld, %d\n", ref, est, i); #endif if (ref != est) { #if ROC_VERBOSE printf(" *bad estimate*\n"); #endif ++num_bad_est; } index_advance(&ref, 1); } failure_rate = (double) num_bad_est / num_trials; if (failure_rate > 0.01) { printf("error: failure rate too high (%d bad estimates in %d trials)\n", num_bad_est, num_trials); return err_status_algo_fail; } printf("done\n"); printf("\ttesting non-sequential insertion..."); index_init(&local); index_init(&ref); index_init(&est); ut_init(&utc); for (i=0; i < num_trials; i++) { /* get next seq num from unreliable transport simulator */ ircvd = ut_next_index(&utc); /* set ref to value of ircvd */ ref = ircvd; /* estimate index based on low bits of ircvd */ delta = index_guess(&local, &est, (uint16_t) ref); #if ROC_VERBOSE printf("ref: %lld, local: %lld, est: %lld, ircvd: %d, delta: %d\n", ref, local, est, ircvd, delta); #endif /* now update local xtd_seq_num_t as necessary */ if (delta > 0) index_advance(&local, delta); if (ref != est) { #if ROC_VERBOSE printf(" *bad estimate*\n"); #endif /* record failure event */ ++num_bad_est; /* reset local value to correct value */ local = ref; } } failure_rate = (double) num_bad_est / num_trials; if (failure_rate > 0.01) { printf("error: failure rate too high (%d bad estimates in %d trials)\n", num_bad_est, num_trials); return err_status_algo_fail; } printf("done\n"); return err_status_ok; }