2014-06-04 13:38:59 +05:30
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2014-06-04 13:38:59 +05:30
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2014-06-04 13:38:59 +05:30
|
|
|
*/
|
|
|
|
|
2018-03-08 23:31:22 -08:00
|
|
|
/*
|
|
|
|
* IMPORTANT NOTE:
|
|
|
|
* These tests work by generating a large number of pseudo-random numbers
|
|
|
|
* and then statistically analyzing them to determine whether they seem
|
|
|
|
* random. The test is expected to fail on occasion by random happenstance.
|
|
|
|
*/
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2018-10-26 08:40:30 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
|
|
|
#include <isc/commandline.h>
|
2014-06-04 13:38:59 +05:30
|
|
|
#include <isc/mem.h>
|
2018-05-28 15:22:23 +02:00
|
|
|
#include <isc/nonce.h>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2018-10-26 08:40:30 -07:00
|
|
|
#include <isc/random.h>
|
|
|
|
#include <isc/result.h>
|
2014-06-04 13:38:59 +05:30
|
|
|
#include <isc/util.h>
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
#include "isctest.h"
|
|
|
|
|
2018-03-08 23:31:22 -08:00
|
|
|
#define REPS 25000
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
typedef double(pvalue_func_t)(isc_mem_t *mctx, uint16_t *values, size_t length);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* igamc(), igam(), etc. were adapted (and cleaned up) from the Cephes
|
|
|
|
* math library:
|
|
|
|
*
|
|
|
|
* Cephes Math Library Release 2.8: June, 2000
|
|
|
|
* Copyright 1985, 1987, 2000 by Stephen L. Moshier
|
|
|
|
*
|
|
|
|
* The Cephes math library was released into the public domain as part
|
|
|
|
* of netlib.
|
2020-02-12 13:59:18 +01:00
|
|
|
*/
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
static double MACHEP = 1.11022302462515654042E-16;
|
|
|
|
static double MAXLOG = 7.09782712893383996843E2;
|
|
|
|
static double big = 4.503599627370496e15;
|
2020-02-12 13:59:18 +01:00
|
|
|
static double biginv = 2.22044604925031308085e-16;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static double
|
|
|
|
igamc(double a, double x);
|
|
|
|
static double
|
|
|
|
igam(double a, double x);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-11-16 08:19:06 +00:00
|
|
|
/* Set to true (or use -v option) for verbose output */
|
2018-10-26 08:40:30 -07:00
|
|
|
static bool verbose = false;
|
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
typedef enum {
|
|
|
|
ISC_RANDOM8,
|
|
|
|
ISC_RANDOM16,
|
|
|
|
ISC_RANDOM32,
|
|
|
|
ISC_RANDOM_BYTES,
|
|
|
|
ISC_RANDOM_UNIFORM,
|
|
|
|
ISC_NONCE_BYTES
|
|
|
|
} isc_random_func;
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2019-11-09 14:01:08 +01:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2019-11-09 14:01:08 +01:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:38:59 +05:30
|
|
|
static double
|
2020-02-13 14:44:37 -08:00
|
|
|
igamc(double a, double x) {
|
2014-06-04 13:38:59 +05:30
|
|
|
double ans, ax, c, yc, r, t, y, z;
|
|
|
|
double pk, pkm1, pkm2, qk, qkm1, qkm2;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((x <= 0) || (a <= 0)) {
|
2014-06-04 13:38:59 +05:30
|
|
|
return (1.0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((x < 1.0) || (x < a)) {
|
2014-06-04 13:38:59 +05:30
|
|
|
return (1.0 - igam(a, x));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
ax = a * log(x) - x - lgamma(a);
|
|
|
|
if (ax < -MAXLOG) {
|
2018-10-26 08:40:30 -07:00
|
|
|
print_error("# igamc: UNDERFLOW, ax=%f\n", ax);
|
2014-06-04 13:38:59 +05:30
|
|
|
return (0.0);
|
|
|
|
}
|
|
|
|
ax = exp(ax);
|
|
|
|
|
|
|
|
/* continued fraction */
|
|
|
|
y = 1.0 - a;
|
|
|
|
z = x + y + 1.0;
|
|
|
|
c = 0.0;
|
|
|
|
pkm2 = 1.0;
|
|
|
|
qkm2 = x;
|
|
|
|
pkm1 = x + 1.0;
|
|
|
|
qkm1 = z * x;
|
|
|
|
ans = pkm1 / qkm1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
c += 1.0;
|
|
|
|
y += 1.0;
|
|
|
|
z += 2.0;
|
|
|
|
yc = y * c;
|
2020-02-12 13:59:18 +01:00
|
|
|
pk = pkm1 * z - pkm2 * yc;
|
|
|
|
qk = qkm1 * z - qkm2 * yc;
|
2014-06-04 13:38:59 +05:30
|
|
|
if (qk != 0) {
|
|
|
|
r = pk / qk;
|
|
|
|
t = fabs((ans - r) / r);
|
|
|
|
ans = r;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-06-04 13:38:59 +05:30
|
|
|
t = 1.0;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
pkm2 = pkm1;
|
|
|
|
pkm1 = pk;
|
|
|
|
qkm2 = qkm1;
|
|
|
|
qkm1 = qk;
|
|
|
|
|
|
|
|
if (fabs(pk) > big) {
|
|
|
|
pkm2 *= biginv;
|
|
|
|
pkm1 *= biginv;
|
|
|
|
qkm2 *= biginv;
|
|
|
|
qkm1 *= biginv;
|
|
|
|
}
|
|
|
|
} while (t > MACHEP);
|
|
|
|
|
|
|
|
return (ans * ax);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
2020-02-13 14:44:37 -08:00
|
|
|
igam(double a, double x) {
|
2014-06-04 13:38:59 +05:30
|
|
|
double ans, ax, c, r;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((x <= 0) || (a <= 0)) {
|
2014-06-04 13:38:59 +05:30
|
|
|
return (0.0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((x > 1.0) && (x > a)) {
|
2014-06-04 13:38:59 +05:30
|
|
|
return (1.0 - igamc(a, x));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* Compute x**a * exp(-x) / md_gamma(a) */
|
|
|
|
ax = a * log(x) - x - lgamma(a);
|
2020-02-12 13:59:18 +01:00
|
|
|
if (ax < -MAXLOG) {
|
2018-10-26 08:40:30 -07:00
|
|
|
print_error("# igam: UNDERFLOW, ax=%f\n", ax);
|
2014-06-04 13:38:59 +05:30
|
|
|
return (0.0);
|
|
|
|
}
|
|
|
|
ax = exp(ax);
|
|
|
|
|
|
|
|
/* power series */
|
|
|
|
r = a;
|
|
|
|
c = 1.0;
|
|
|
|
ans = 1.0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
r += 1.0;
|
|
|
|
c *= x / r;
|
|
|
|
ans += c;
|
|
|
|
} while (c / ans > MACHEP);
|
|
|
|
|
|
|
|
return (ans * ax / a);
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
static int8_t scounts_table[65536];
|
2018-03-28 14:19:37 +02:00
|
|
|
static uint8_t bitcounts_table[65536];
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
static int8_t
|
2020-02-13 14:44:37 -08:00
|
|
|
scount_calculate(uint16_t n) {
|
|
|
|
int i;
|
2018-03-28 14:19:37 +02:00
|
|
|
int8_t sc;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
sc = 0;
|
|
|
|
for (i = 0; i < 16; i++) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint16_t lsb;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
lsb = n & 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (lsb != 0) {
|
2014-06-04 13:38:59 +05:30
|
|
|
sc += 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-06-04 13:38:59 +05:30
|
|
|
sc -= 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sc);
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
static uint8_t
|
2020-02-13 14:44:37 -08:00
|
|
|
bitcount_calculate(uint16_t n) {
|
|
|
|
int i;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint8_t bc;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
bc = 0;
|
|
|
|
for (i = 0; i < 16; i++) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint16_t lsb;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
lsb = n & 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (lsb != 0) {
|
2014-06-04 13:38:59 +05:30
|
|
|
bc += 1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
tables_init(void) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t i;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
for (i = 0; i < 65536; i++) {
|
|
|
|
scounts_table[i] = scount_calculate(i);
|
|
|
|
bitcounts_table[i] = bitcount_calculate(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following code for computing Marsaglia's rank is based on the
|
|
|
|
* implementation in cdbinrnk.c from the diehard tests by George
|
|
|
|
* Marsaglia.
|
|
|
|
*
|
|
|
|
* This function destroys (modifies) the data passed in bits.
|
|
|
|
*/
|
2018-03-28 14:19:37 +02:00
|
|
|
static uint32_t
|
2020-02-13 14:44:37 -08:00
|
|
|
matrix_binaryrank(uint32_t *bits, size_t rows, size_t cols) {
|
|
|
|
size_t i, j, k;
|
2017-12-06 21:00:14 +11:00
|
|
|
unsigned int rt = 0;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t rank = 0;
|
|
|
|
uint32_t tmp;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
for (k = 0; k < rows; k++) {
|
|
|
|
i = k;
|
|
|
|
|
2017-12-06 21:00:14 +11:00
|
|
|
while (rt >= cols || ((bits[i] >> rt) & 1) == 0) {
|
2014-06-04 13:38:59 +05:30
|
|
|
i++;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (i < rows) {
|
2014-06-04 13:38:59 +05:30
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-06-04 13:38:59 +05:30
|
|
|
rt++;
|
|
|
|
if (rt < cols) {
|
|
|
|
i = k;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rank);
|
|
|
|
}
|
|
|
|
|
|
|
|
rank++;
|
|
|
|
if (i != k) {
|
|
|
|
tmp = bits[i];
|
|
|
|
bits[i] = bits[k];
|
|
|
|
bits[k] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = i + 1; j < rows; j++) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (((bits[j] >> rt) & 1) == 0) {
|
2014-06-04 13:38:59 +05:30
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-06-04 13:38:59 +05:30
|
|
|
bits[j] ^= bits[k];
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
rt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rank);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
random_test(pvalue_func_t *func, isc_random_func test_func) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t m;
|
|
|
|
uint32_t j;
|
|
|
|
uint32_t histogram[11] = { 0 };
|
|
|
|
uint32_t passed;
|
2020-02-13 14:44:37 -08:00
|
|
|
double proportion;
|
|
|
|
double p_hat;
|
|
|
|
double lower_confidence, higher_confidence;
|
|
|
|
double chi_square;
|
|
|
|
double p_value_t;
|
|
|
|
double alpha;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
tables_init();
|
|
|
|
|
|
|
|
m = 1000;
|
|
|
|
passed = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < m; j++) {
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t values[REPS];
|
2018-03-28 14:19:37 +02:00
|
|
|
uint16_t *uniform_values;
|
2020-02-13 14:44:37 -08:00
|
|
|
double p_value;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
switch (test_func) {
|
|
|
|
case ISC_RANDOM8:
|
2020-02-13 14:44:37 -08:00
|
|
|
for (i = 0; i < (sizeof(values) / sizeof(*values)); i++)
|
|
|
|
{
|
2018-05-28 15:22:23 +02:00
|
|
|
values[i] = isc_random8();
|
2018-04-19 08:48:28 -07:00
|
|
|
}
|
2018-05-28 15:22:23 +02:00
|
|
|
break;
|
|
|
|
case ISC_RANDOM16:
|
2020-02-13 14:44:37 -08:00
|
|
|
for (i = 0; i < (sizeof(values) / sizeof(*values)); i++)
|
|
|
|
{
|
2018-05-28 15:22:23 +02:00
|
|
|
values[i] = isc_random16();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISC_RANDOM32:
|
2020-02-13 14:44:37 -08:00
|
|
|
for (i = 0; i < (sizeof(values) / sizeof(*values)); i++)
|
|
|
|
{
|
2018-05-28 15:22:23 +02:00
|
|
|
values[i] = isc_random32();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISC_RANDOM_BYTES:
|
2018-04-22 14:56:28 +02:00
|
|
|
isc_random_buf(values, sizeof(values));
|
2018-05-28 15:22:23 +02:00
|
|
|
break;
|
|
|
|
case ISC_RANDOM_UNIFORM:
|
2018-03-28 14:19:37 +02:00
|
|
|
uniform_values = (uint16_t *)values;
|
2018-05-28 15:22:23 +02:00
|
|
|
for (i = 0;
|
2019-09-26 14:55:53 +02:00
|
|
|
i < (sizeof(values) / (sizeof(*uniform_values)));
|
2020-02-12 13:59:18 +01:00
|
|
|
i++) {
|
2018-05-28 15:22:23 +02:00
|
|
|
uniform_values[i] =
|
2018-03-28 14:19:37 +02:00
|
|
|
isc_random_uniform(UINT16_MAX);
|
2018-05-28 15:22:23 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISC_NONCE_BYTES:
|
|
|
|
isc_nonce_buf(values, sizeof(values));
|
|
|
|
break;
|
2017-11-06 10:44:37 -08:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
p_value = (*func)(test_mctx, (uint16_t *)values, REPS * 2);
|
2018-04-19 08:48:28 -07:00
|
|
|
if (p_value >= 0.01) {
|
2014-06-04 13:38:59 +05:30
|
|
|
passed++;
|
2018-04-19 08:48:28 -07:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_in_range(p_value, 0.0, 1.0);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
i = (int)floor(p_value * 10);
|
2014-06-04 13:38:59 +05:30
|
|
|
histogram[i]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check proportion of sequences passing a test (see section
|
|
|
|
* 4.2.1 in NIST SP 800-22).
|
|
|
|
*/
|
2018-04-19 08:48:28 -07:00
|
|
|
alpha = 0.01; /* the significance level */
|
2020-02-12 13:59:18 +01:00
|
|
|
proportion = (double)passed / (double)m;
|
2018-04-19 08:48:28 -07:00
|
|
|
p_hat = 1.0 - alpha;
|
|
|
|
lower_confidence = p_hat - (3.0 * sqrt((p_hat * (1.0 - p_hat)) / m));
|
|
|
|
higher_confidence = p_hat + (3.0 * sqrt((p_hat * (1.0 - p_hat)) / m));
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# passed=%u/1000\n", passed);
|
|
|
|
print_message("# higher_confidence=%f, lower_confidence=%f, "
|
|
|
|
"proportion=%f\n",
|
|
|
|
higher_confidence, lower_confidence, proportion);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_in_range(proportion, lower_confidence, higher_confidence);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Check uniform distribution of p-values (see section 4.2.2 in
|
|
|
|
* NIST SP 800-22).
|
|
|
|
*/
|
|
|
|
|
2018-04-19 08:48:28 -07:00
|
|
|
/* Fold histogram[10] (p_value = 1.0) into histogram[9] for
|
|
|
|
* interval [0.9, 1.0]
|
|
|
|
*/
|
|
|
|
histogram[9] += histogram[10];
|
|
|
|
histogram[10] = 0;
|
|
|
|
|
2014-06-04 13:38:59 +05:30
|
|
|
/* Pre-requisite that at least 55 sequences are processed. */
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_true(m >= 55);
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
print_message("# ");
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
chi_square = 0.0;
|
|
|
|
for (j = 0; j < 10; j++) {
|
|
|
|
double numer;
|
|
|
|
double denom;
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("hist%u=%u ", j, histogram[j]);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
numer = (histogram[j] - (m / 10.0)) *
|
|
|
|
(histogram[j] - (m / 10.0));
|
|
|
|
denom = m / 10.0;
|
|
|
|
chi_square += numer / denom;
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("\n");
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
p_value_t = igamc(9 / 2.0, chi_square / 2.0);
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_true(p_value_t >= 0.0001);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a frequency (monobits) test taken from the NIST SP 800-22
|
2018-04-22 14:56:28 +02:00
|
|
|
* RANDOM test suite.
|
2014-06-04 13:38:59 +05:30
|
|
|
*/
|
|
|
|
static double
|
2020-02-13 14:44:37 -08:00
|
|
|
monobit(isc_mem_t *mctx, uint16_t *values, size_t length) {
|
|
|
|
size_t i;
|
|
|
|
int32_t scount;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t numbits;
|
2020-02-13 14:44:37 -08:00
|
|
|
double s_obs;
|
|
|
|
double p_value;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
UNUSED(mctx);
|
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
numbits = length * sizeof(*values) * 8;
|
2014-06-04 13:38:59 +05:30
|
|
|
scount = 0;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
for (i = 0; i < length; i++) {
|
2014-06-04 13:38:59 +05:30
|
|
|
scount += scounts_table[values[i]];
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* Preconditions (section 2.1.7 in NIST SP 800-22) */
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_true(numbits >= 100);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# numbits=%u, scount=%d\n", numbits, scount);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2015-04-29 03:16:50 +10:00
|
|
|
s_obs = abs(scount) / sqrt(numbits);
|
2014-06-04 13:38:59 +05:30
|
|
|
p_value = erfc(s_obs / sqrt(2.0));
|
|
|
|
|
|
|
|
return (p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the runs test taken from the NIST SP 800-22 RNG test suite.
|
|
|
|
*/
|
|
|
|
static double
|
2020-02-13 14:44:37 -08:00
|
|
|
runs(isc_mem_t *mctx, uint16_t *values, size_t length) {
|
|
|
|
size_t i;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t bcount;
|
|
|
|
uint32_t numbits;
|
2020-02-13 14:44:37 -08:00
|
|
|
double pi;
|
|
|
|
double tau;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t j;
|
|
|
|
uint32_t b;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint8_t bit_this;
|
|
|
|
uint8_t bit_prev;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t v_obs;
|
2020-02-13 14:44:37 -08:00
|
|
|
double numer;
|
|
|
|
double denom;
|
|
|
|
double p_value;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
UNUSED(mctx);
|
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
numbits = length * sizeof(*values) * 8;
|
2014-06-04 13:38:59 +05:30
|
|
|
bcount = 0;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
for (i = 0; i < length; i++) {
|
2014-06-04 13:38:59 +05:30
|
|
|
bcount += bitcounts_table[values[i]];
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# numbits=%u, bcount=%u\n", numbits, bcount);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
pi = (double)bcount / (double)numbits;
|
2014-06-04 13:38:59 +05:30
|
|
|
tau = 2.0 / sqrt(numbits);
|
|
|
|
|
|
|
|
/* Preconditions (section 2.3.7 in NIST SP 800-22) */
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_true(numbits >= 100);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre-condition implied from the monobit test. This can fail
|
|
|
|
* for some sequences, and the p-value is taken as 0 in these
|
|
|
|
* cases.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (fabs(pi - 0.5) >= tau) {
|
2014-06-04 13:38:59 +05:30
|
|
|
return (0.0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* Compute v_obs */
|
|
|
|
j = 0;
|
|
|
|
b = 14;
|
|
|
|
bit_prev = (values[j] & (1U << 15)) == 0 ? 0 : 1;
|
|
|
|
|
|
|
|
v_obs = 0;
|
|
|
|
|
|
|
|
for (i = 1; i < numbits; i++) {
|
|
|
|
bit_this = (values[j] & (1U << b)) == 0 ? 0 : 1;
|
|
|
|
if (b == 0) {
|
|
|
|
b = 15;
|
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
|
|
|
|
v_obs += bit_this ^ bit_prev;
|
|
|
|
|
|
|
|
bit_prev = bit_this;
|
|
|
|
}
|
|
|
|
|
|
|
|
v_obs += 1;
|
|
|
|
|
|
|
|
numer = fabs(v_obs - (2.0 * numbits * pi * (1.0 - pi)));
|
|
|
|
denom = 2.0 * sqrt(2.0 * numbits) * pi * (1.0 - pi);
|
|
|
|
|
|
|
|
p_value = erfc(numer / denom);
|
|
|
|
|
|
|
|
return (p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the block frequency test taken from the NIST SP 800-22 RNG
|
|
|
|
* test suite.
|
|
|
|
*/
|
|
|
|
static double
|
2020-02-13 14:44:37 -08:00
|
|
|
blockfrequency(isc_mem_t *mctx, uint16_t *values, size_t length) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t numbits;
|
|
|
|
uint32_t mbits;
|
|
|
|
uint32_t mwords;
|
|
|
|
uint32_t numblocks;
|
2020-02-13 14:44:37 -08:00
|
|
|
double *pi;
|
|
|
|
double chi_square;
|
|
|
|
double p_value;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
numbits = length * sizeof(*values) * 8;
|
2014-06-04 13:38:59 +05:30
|
|
|
mbits = 32000;
|
|
|
|
mwords = mbits / 16;
|
|
|
|
numblocks = numbits / mbits;
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# numblocks=%u\n", numblocks);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* Preconditions (section 2.2.7 in NIST SP 800-22) */
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_true(numbits >= 100);
|
|
|
|
assert_true(mbits >= 20);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_true((double)mbits > (0.01 * numbits));
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_true(numblocks < 100);
|
|
|
|
assert_true(numbits >= (mbits * numblocks));
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
pi = isc_mem_get(mctx, numblocks * sizeof(double));
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_non_null(pi);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
for (i = 0; i < numblocks; i++) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t j;
|
2014-06-04 13:38:59 +05:30
|
|
|
pi[i] = 0.0;
|
|
|
|
for (j = 0; j < mwords; j++) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t idx;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
idx = i * mwords + j;
|
|
|
|
pi[i] += bitcounts_table[values[idx]];
|
|
|
|
}
|
|
|
|
pi[i] /= mbits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute chi_square */
|
|
|
|
chi_square = 0.0;
|
2020-02-13 21:48:23 +01:00
|
|
|
for (i = 0; i < numblocks; i++) {
|
2014-06-04 13:38:59 +05:30
|
|
|
chi_square += (pi[i] - 0.5) * (pi[i] - 0.5);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
chi_square *= 4 * mbits;
|
|
|
|
|
|
|
|
isc_mem_put(mctx, pi, numblocks * sizeof(double));
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# chi_square=%f\n", chi_square);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
p_value = igamc(numblocks * 0.5, chi_square * 0.5);
|
|
|
|
|
|
|
|
return (p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the binary matrix rank test taken from the NIST SP 800-22 RNG
|
|
|
|
* test suite.
|
|
|
|
*/
|
|
|
|
static double
|
2020-02-13 14:44:37 -08:00
|
|
|
binarymatrixrank(isc_mem_t *mctx, uint16_t *values, size_t length) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t i;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t matrix_m;
|
|
|
|
size_t matrix_q;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t num_matrices;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t numbits;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t fm_0;
|
|
|
|
uint32_t fm_1;
|
|
|
|
uint32_t fm_rest;
|
2020-02-13 14:44:37 -08:00
|
|
|
double term1;
|
|
|
|
double term2;
|
|
|
|
double term3;
|
|
|
|
double chi_square;
|
|
|
|
double p_value;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
UNUSED(mctx);
|
|
|
|
|
|
|
|
matrix_m = 32;
|
|
|
|
matrix_q = 32;
|
|
|
|
num_matrices = length / ((matrix_m * matrix_q) / 16);
|
|
|
|
numbits = num_matrices * matrix_m * matrix_q;
|
|
|
|
|
|
|
|
/* Preconditions (section 2.5.7 in NIST SP 800-22) */
|
2018-10-26 08:40:30 -07:00
|
|
|
assert_int_equal(matrix_m, 32);
|
|
|
|
assert_int_equal(matrix_q, 32);
|
|
|
|
assert_true(numbits >= (38 * matrix_m * matrix_q));
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
fm_0 = 0;
|
|
|
|
fm_1 = 0;
|
|
|
|
fm_rest = 0;
|
|
|
|
for (i = 0; i < num_matrices; i++) {
|
|
|
|
/*
|
2018-03-28 14:19:37 +02:00
|
|
|
* Each uint32_t supplies 32 bits, so a 32x32 bit matrix
|
|
|
|
* takes up uint32_t array of size 32.
|
2014-06-04 13:38:59 +05:30
|
|
|
*/
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t bits[32];
|
2020-02-13 14:44:37 -08:00
|
|
|
int j;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t rank;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
for (j = 0; j < 32; j++) {
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t idx;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t r1;
|
|
|
|
uint32_t r2;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
idx = i * ((matrix_m * matrix_q) / 16);
|
|
|
|
idx += j * 2;
|
|
|
|
|
|
|
|
r1 = values[idx];
|
|
|
|
r2 = values[idx + 1];
|
|
|
|
bits[j] = (r1 << 16) | r2;
|
|
|
|
}
|
|
|
|
|
|
|
|
rank = matrix_binaryrank(bits, matrix_m, matrix_q);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (rank == matrix_m) {
|
2014-06-04 13:38:59 +05:30
|
|
|
fm_0++;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (rank == (matrix_m - 1)) {
|
2014-06-04 13:38:59 +05:30
|
|
|
fm_1++;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-06-04 13:38:59 +05:30
|
|
|
fm_rest++;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute chi_square */
|
|
|
|
term1 = ((fm_0 - (0.2888 * num_matrices)) *
|
2020-02-12 13:59:18 +01:00
|
|
|
(fm_0 - (0.2888 * num_matrices))) /
|
|
|
|
(0.2888 * num_matrices);
|
2014-06-04 13:38:59 +05:30
|
|
|
term2 = ((fm_1 - (0.5776 * num_matrices)) *
|
2020-02-12 13:59:18 +01:00
|
|
|
(fm_1 - (0.5776 * num_matrices))) /
|
|
|
|
(0.5776 * num_matrices);
|
2014-06-04 13:38:59 +05:30
|
|
|
term3 = ((fm_rest - (0.1336 * num_matrices)) *
|
2020-02-12 13:59:18 +01:00
|
|
|
(fm_rest - (0.1336 * num_matrices))) /
|
|
|
|
(0.1336 * num_matrices);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
chi_square = term1 + term2 + term3;
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
if (verbose) {
|
|
|
|
print_message("# fm_0=%u, fm_1=%u, fm_rest=%u, chi_square=%f\n",
|
|
|
|
fm_0, fm_1, fm_rest, chi_square);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
p_value = exp(-chi_square * 0.5);
|
|
|
|
|
|
|
|
return (p_value);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/***
|
|
|
|
*** Tests for isc_random32() function
|
|
|
|
***/
|
2018-05-28 15:22:23 +02:00
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Monobit test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random32_monobit(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(monobit, ISC_RANDOM32);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Runs test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random32_runs(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(runs, ISC_RANDOM32);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Block frequency test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random32_blockfrequency(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(blockfrequency, ISC_RANDOM32);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Binary matrix rank test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random32_binarymatrixrank(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(binarymatrixrank, ISC_RANDOM32);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/***
|
|
|
|
*** Tests for isc_random_bytes() function
|
|
|
|
***/
|
2018-05-28 15:22:23 +02:00
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Monobit test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_bytes_monobit(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2017-11-06 10:44:37 -08:00
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(monobit, ISC_RANDOM_BYTES);
|
2017-11-06 10:44:37 -08:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Runs test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_bytes_runs(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2017-11-06 10:44:37 -08:00
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(runs, ISC_RANDOM_BYTES);
|
2017-11-06 10:44:37 -08:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Block frequency test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_bytes_blockfrequency(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2017-11-06 10:44:37 -08:00
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(blockfrequency, ISC_RANDOM_BYTES);
|
2017-11-06 10:44:37 -08:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Binary matrix rank test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_bytes_binarymatrixrank(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2017-11-06 10:44:37 -08:00
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(binarymatrixrank, ISC_RANDOM_BYTES);
|
2017-11-06 10:44:37 -08:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/***
|
|
|
|
*** Tests for isc_random_uniform() function:
|
|
|
|
***/
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Monobit test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_uniform_monobit(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(monobit, ISC_RANDOM_UNIFORM);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Runs test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_uniform_runs(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(runs, ISC_RANDOM_UNIFORM);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Block frequency test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_uniform_blockfrequency(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
random_test(blockfrequency, ISC_RANDOM_UNIFORM);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Binary matrix rank test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_random_uniform_binarymatrixrank(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(binarymatrixrank, ISC_RANDOM_UNIFORM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tests for isc_nonce_bytes() function */
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Monobit test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nonce_bytes_monobit(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(monobit, ISC_NONCE_BYTES);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Runs test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nonce_bytes_runs(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(runs, ISC_NONCE_BYTES);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Block frequency test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nonce_bytes_blockfrequency(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
|
|
|
random_test(blockfrequency, ISC_NONCE_BYTES);
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
/* Binary matrix rank test for the RANDOM */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nonce_bytes_binarymatrixrank(void **state) {
|
2018-10-26 08:40:30 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
random_test(binarymatrixrank, ISC_NONCE_BYTES);
|
2018-05-28 15:22:23 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(int argc, char **argv) {
|
2018-10-26 08:40:30 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(isc_random32_monobit),
|
|
|
|
cmocka_unit_test(isc_random32_runs),
|
|
|
|
cmocka_unit_test(isc_random32_blockfrequency),
|
|
|
|
cmocka_unit_test(isc_random32_binarymatrixrank),
|
|
|
|
cmocka_unit_test(isc_random_bytes_monobit),
|
|
|
|
cmocka_unit_test(isc_random_bytes_runs),
|
|
|
|
cmocka_unit_test(isc_random_bytes_blockfrequency),
|
|
|
|
cmocka_unit_test(isc_random_bytes_binarymatrixrank),
|
|
|
|
cmocka_unit_test(isc_random_uniform_monobit),
|
|
|
|
cmocka_unit_test(isc_random_uniform_runs),
|
|
|
|
cmocka_unit_test(isc_random_uniform_blockfrequency),
|
|
|
|
cmocka_unit_test(isc_random_uniform_binarymatrixrank),
|
|
|
|
cmocka_unit_test(isc_nonce_bytes_monobit),
|
|
|
|
cmocka_unit_test(isc_nonce_bytes_runs),
|
|
|
|
cmocka_unit_test(isc_nonce_bytes_blockfrequency),
|
|
|
|
cmocka_unit_test(isc_nonce_bytes_binarymatrixrank),
|
|
|
|
};
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = isc_commandline_parse(argc, argv, "v")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
return (cmocka_run_group_tests(tests, _setup, _teardown));
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2018-10-26 08:40:30 -07:00
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-26 08:40:30 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
2018-10-26 08:40:30 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|