1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * SPDX-License-Identifier: MPL-2.0
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /*
15  * IMPORTANT NOTE:
16  * These tests work by generating a large number of pseudo-random numbers
17  * and then statistically analyzing them to determine whether they seem
18  * random. The test is expected to fail on occasion by random happenstance.
19  */
20 
21 #if HAVE_CMOCKA
22 
23 #include <inttypes.h>
24 #include <math.h>
25 #include <sched.h> /* IWYU pragma: keep */
26 #include <setjmp.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #define UNIT_TESTING
33 #include <cmocka.h>
34 
35 #include <isc/commandline.h>
36 #include <isc/mem.h>
37 #include <isc/nonce.h>
38 #include <isc/print.h>
39 #include <isc/random.h>
40 #include <isc/result.h>
41 #include <isc/util.h>
42 
43 #include "isctest.h"
44 
45 #define REPS 25000
46 
47 typedef double(pvalue_func_t)(isc_mem_t *mctx, uint16_t *values, size_t length);
48 
49 /* igamc(), igam(), etc. were adapted (and cleaned up) from the Cephes
50  * math library:
51  *
52  * Cephes Math Library Release 2.8:  June, 2000
53  * Copyright 1985, 1987, 2000 by Stephen L. Moshier
54  *
55  * The Cephes math library was released into the public domain as part
56  * of netlib.
57  */
58 
59 static double MACHEP = 1.11022302462515654042E-16;
60 static double MAXLOG = 7.09782712893383996843E2;
61 static double big = 4.503599627370496e15;
62 static double biginv = 2.22044604925031308085e-16;
63 
64 static double
65 igamc(double a, double x);
66 static double
67 igam(double a, double x);
68 
69 /* Set to true (or use -v option) for verbose output */
70 static bool verbose = false;
71 
72 typedef enum {
73 	ISC_RANDOM8,
74 	ISC_RANDOM16,
75 	ISC_RANDOM32,
76 	ISC_RANDOM_BYTES,
77 	ISC_RANDOM_UNIFORM,
78 	ISC_NONCE_BYTES
79 } isc_random_func;
80 
81 static int
_setup(void ** state)82 _setup(void **state) {
83 	isc_result_t result;
84 
85 	UNUSED(state);
86 
87 	result = isc_test_begin(NULL, true, 0);
88 	assert_int_equal(result, ISC_R_SUCCESS);
89 
90 	return (0);
91 }
92 
93 static int
_teardown(void ** state)94 _teardown(void **state) {
95 	UNUSED(state);
96 
97 	isc_test_end();
98 
99 	return (0);
100 }
101 
102 static double
igamc(double a,double x)103 igamc(double a, double x) {
104 	double ans, ax, c, yc, r, t, y, z;
105 	double pk, pkm1, pkm2, qk, qkm1, qkm2;
106 
107 	if ((x <= 0) || (a <= 0)) {
108 		return (1.0);
109 	}
110 
111 	if ((x < 1.0) || (x < a)) {
112 		return (1.0 - igam(a, x));
113 	}
114 
115 	ax = a * log(x) - x - lgamma(a);
116 	if (ax < -MAXLOG) {
117 		print_error("# igamc: UNDERFLOW, ax=%f\n", ax);
118 		return (0.0);
119 	}
120 	ax = exp(ax);
121 
122 	/* continued fraction */
123 	y = 1.0 - a;
124 	z = x + y + 1.0;
125 	c = 0.0;
126 	pkm2 = 1.0;
127 	qkm2 = x;
128 	pkm1 = x + 1.0;
129 	qkm1 = z * x;
130 	ans = pkm1 / qkm1;
131 
132 	do {
133 		c += 1.0;
134 		y += 1.0;
135 		z += 2.0;
136 		yc = y * c;
137 		pk = pkm1 * z - pkm2 * yc;
138 		qk = qkm1 * z - qkm2 * yc;
139 		if (qk != 0) {
140 			r = pk / qk;
141 			t = fabs((ans - r) / r);
142 			ans = r;
143 		} else {
144 			t = 1.0;
145 		}
146 
147 		pkm2 = pkm1;
148 		pkm1 = pk;
149 		qkm2 = qkm1;
150 		qkm1 = qk;
151 
152 		if (fabs(pk) > big) {
153 			pkm2 *= biginv;
154 			pkm1 *= biginv;
155 			qkm2 *= biginv;
156 			qkm1 *= biginv;
157 		}
158 	} while (t > MACHEP);
159 
160 	return (ans * ax);
161 }
162 
163 static double
igam(double a,double x)164 igam(double a, double x) {
165 	double ans, ax, c, r;
166 
167 	if ((x <= 0) || (a <= 0)) {
168 		return (0.0);
169 	}
170 
171 	if ((x > 1.0) && (x > a)) {
172 		return (1.0 - igamc(a, x));
173 	}
174 
175 	/* Compute  x**a * exp(-x) / md_gamma(a)  */
176 	ax = a * log(x) - x - lgamma(a);
177 	if (ax < -MAXLOG) {
178 		print_error("# igam: UNDERFLOW, ax=%f\n", ax);
179 		return (0.0);
180 	}
181 	ax = exp(ax);
182 
183 	/* power series */
184 	r = a;
185 	c = 1.0;
186 	ans = 1.0;
187 
188 	do {
189 		r += 1.0;
190 		c *= x / r;
191 		ans += c;
192 	} while (c / ans > MACHEP);
193 
194 	return (ans * ax / a);
195 }
196 
197 static int8_t scounts_table[65536];
198 static uint8_t bitcounts_table[65536];
199 
200 static int8_t
scount_calculate(uint16_t n)201 scount_calculate(uint16_t n) {
202 	int i;
203 	int8_t sc;
204 
205 	sc = 0;
206 	for (i = 0; i < 16; i++) {
207 		uint16_t lsb;
208 
209 		lsb = n & 1;
210 		if (lsb != 0) {
211 			sc += 1;
212 		} else {
213 			sc -= 1;
214 		}
215 
216 		n >>= 1;
217 	}
218 
219 	return (sc);
220 }
221 
222 static uint8_t
bitcount_calculate(uint16_t n)223 bitcount_calculate(uint16_t n) {
224 	int i;
225 	uint8_t bc;
226 
227 	bc = 0;
228 	for (i = 0; i < 16; i++) {
229 		uint16_t lsb;
230 
231 		lsb = n & 1;
232 		if (lsb != 0) {
233 			bc += 1;
234 		}
235 
236 		n >>= 1;
237 	}
238 
239 	return (bc);
240 }
241 
242 static void
tables_init(void)243 tables_init(void) {
244 	uint32_t i;
245 
246 	for (i = 0; i < 65536; i++) {
247 		scounts_table[i] = scount_calculate(i);
248 		bitcounts_table[i] = bitcount_calculate(i);
249 	}
250 }
251 
252 /*
253  * The following code for computing Marsaglia's rank is based on the
254  * implementation in cdbinrnk.c from the diehard tests by George
255  * Marsaglia.
256  *
257  * This function destroys (modifies) the data passed in bits.
258  */
259 static uint32_t
matrix_binaryrank(uint32_t * bits,size_t rows,size_t cols)260 matrix_binaryrank(uint32_t *bits, size_t rows, size_t cols) {
261 	size_t i, j, k;
262 	unsigned int rt = 0;
263 	uint32_t rank = 0;
264 	uint32_t tmp;
265 
266 	for (k = 0; k < rows; k++) {
267 		i = k;
268 
269 		while (rt >= cols || ((bits[i] >> rt) & 1) == 0) {
270 			i++;
271 
272 			if (i < rows) {
273 				continue;
274 			} else {
275 				rt++;
276 				if (rt < cols) {
277 					i = k;
278 					continue;
279 				}
280 			}
281 
282 			return (rank);
283 		}
284 
285 		rank++;
286 		if (i != k) {
287 			tmp = bits[i];
288 			bits[i] = bits[k];
289 			bits[k] = tmp;
290 		}
291 
292 		for (j = i + 1; j < rows; j++) {
293 			if (((bits[j] >> rt) & 1) == 0) {
294 				continue;
295 			} else {
296 				bits[j] ^= bits[k];
297 			}
298 		}
299 
300 		rt++;
301 	}
302 
303 	return (rank);
304 }
305 
306 static void
random_test(pvalue_func_t * func,isc_random_func test_func)307 random_test(pvalue_func_t *func, isc_random_func test_func) {
308 	uint32_t m;
309 	uint32_t j;
310 	uint32_t histogram[11] = { 0 };
311 	uint32_t passed;
312 	double proportion;
313 	double p_hat;
314 	double lower_confidence, higher_confidence;
315 	double chi_square;
316 	double p_value_t;
317 	double alpha;
318 
319 	tables_init();
320 
321 	m = 1000;
322 	passed = 0;
323 
324 	for (j = 0; j < m; j++) {
325 		uint32_t i;
326 		uint32_t values[REPS];
327 		uint16_t *uniform_values;
328 		double p_value;
329 
330 		switch (test_func) {
331 		case ISC_RANDOM8:
332 			for (i = 0; i < (sizeof(values) / sizeof(*values)); i++)
333 			{
334 				values[i] = isc_random8();
335 			}
336 			break;
337 		case ISC_RANDOM16:
338 			for (i = 0; i < (sizeof(values) / sizeof(*values)); i++)
339 			{
340 				values[i] = isc_random16();
341 			}
342 			break;
343 		case ISC_RANDOM32:
344 			for (i = 0; i < (sizeof(values) / sizeof(*values)); i++)
345 			{
346 				values[i] = isc_random32();
347 			}
348 			break;
349 		case ISC_RANDOM_BYTES:
350 			isc_random_buf(values, sizeof(values));
351 			break;
352 		case ISC_RANDOM_UNIFORM:
353 			uniform_values = (uint16_t *)values;
354 			for (i = 0;
355 			     i < (sizeof(values) / (sizeof(*uniform_values)));
356 			     i++) {
357 				uniform_values[i] =
358 					isc_random_uniform(UINT16_MAX);
359 			}
360 			break;
361 		case ISC_NONCE_BYTES:
362 			isc_nonce_buf(values, sizeof(values));
363 			break;
364 		}
365 
366 		p_value = (*func)(test_mctx, (uint16_t *)values, REPS * 2);
367 		if (p_value >= 0.01) {
368 			passed++;
369 		}
370 
371 		assert_in_range(p_value, 0.0, 1.0);
372 
373 		i = (int)floor(p_value * 10);
374 		histogram[i]++;
375 	}
376 
377 	/*
378 	 * Check proportion of sequences passing a test (see section
379 	 * 4.2.1 in NIST SP 800-22).
380 	 */
381 	alpha = 0.01; /* the significance level */
382 	proportion = (double)passed / (double)m;
383 	p_hat = 1.0 - alpha;
384 	lower_confidence = p_hat - (3.0 * sqrt((p_hat * (1.0 - p_hat)) / m));
385 	higher_confidence = p_hat + (3.0 * sqrt((p_hat * (1.0 - p_hat)) / m));
386 
387 	if (verbose) {
388 		print_message("# passed=%u/1000\n", passed);
389 		print_message("# higher_confidence=%f, lower_confidence=%f, "
390 			      "proportion=%f\n",
391 			      higher_confidence, lower_confidence, proportion);
392 	}
393 
394 	assert_in_range(proportion, lower_confidence, higher_confidence);
395 
396 	/*
397 	 * Check uniform distribution of p-values (see section 4.2.2 in
398 	 * NIST SP 800-22).
399 	 */
400 
401 	/* Fold histogram[10] (p_value = 1.0) into histogram[9] for
402 	 * interval [0.9, 1.0]
403 	 */
404 	histogram[9] += histogram[10];
405 	histogram[10] = 0;
406 
407 	/* Pre-requisite that at least 55 sequences are processed. */
408 	assert_true(m >= 55);
409 
410 	if (verbose) {
411 		print_message("# ");
412 	}
413 
414 	chi_square = 0.0;
415 	for (j = 0; j < 10; j++) {
416 		double numer;
417 		double denom;
418 
419 		if (verbose) {
420 			print_message("hist%u=%u ", j, histogram[j]);
421 		}
422 
423 		numer = (histogram[j] - (m / 10.0)) *
424 			(histogram[j] - (m / 10.0));
425 		denom = m / 10.0;
426 		chi_square += numer / denom;
427 	}
428 
429 	if (verbose) {
430 		print_message("\n");
431 	}
432 
433 	p_value_t = igamc(9 / 2.0, chi_square / 2.0);
434 
435 	assert_true(p_value_t >= 0.0001);
436 }
437 
438 /*
439  * This is a frequency (monobits) test taken from the NIST SP 800-22
440  * RANDOM test suite.
441  */
442 static double
monobit(isc_mem_t * mctx,uint16_t * values,size_t length)443 monobit(isc_mem_t *mctx, uint16_t *values, size_t length) {
444 	size_t i;
445 	int32_t scount;
446 	uint32_t numbits;
447 	double s_obs;
448 	double p_value;
449 
450 	UNUSED(mctx);
451 
452 	numbits = length * sizeof(*values) * 8;
453 	scount = 0;
454 
455 	for (i = 0; i < length; i++) {
456 		scount += scounts_table[values[i]];
457 	}
458 
459 	/* Preconditions (section 2.1.7 in NIST SP 800-22) */
460 	assert_true(numbits >= 100);
461 
462 	if (verbose) {
463 		print_message("# numbits=%u, scount=%d\n", numbits, scount);
464 	}
465 
466 	s_obs = abs(scount) / sqrt(numbits);
467 	p_value = erfc(s_obs / sqrt(2.0));
468 
469 	return (p_value);
470 }
471 
472 /*
473  * This is the runs test taken from the NIST SP 800-22 RNG test suite.
474  */
475 static double
runs(isc_mem_t * mctx,uint16_t * values,size_t length)476 runs(isc_mem_t *mctx, uint16_t *values, size_t length) {
477 	size_t i;
478 	uint32_t bcount;
479 	uint32_t numbits;
480 	double pi;
481 	double tau;
482 	uint32_t j;
483 	uint32_t b;
484 	uint8_t bit_this;
485 	uint8_t bit_prev;
486 	uint32_t v_obs;
487 	double numer;
488 	double denom;
489 	double p_value;
490 
491 	UNUSED(mctx);
492 
493 	numbits = length * sizeof(*values) * 8;
494 	bcount = 0;
495 
496 	for (i = 0; i < length; i++) {
497 		bcount += bitcounts_table[values[i]];
498 	}
499 
500 	if (verbose) {
501 		print_message("# numbits=%u, bcount=%u\n", numbits, bcount);
502 	}
503 
504 	pi = (double)bcount / (double)numbits;
505 	tau = 2.0 / sqrt(numbits);
506 
507 	/* Preconditions (section 2.3.7 in NIST SP 800-22) */
508 	assert_true(numbits >= 100);
509 
510 	/*
511 	 * Pre-condition implied from the monobit test. This can fail
512 	 * for some sequences, and the p-value is taken as 0 in these
513 	 * cases.
514 	 */
515 	if (fabs(pi - 0.5) >= tau) {
516 		return (0.0);
517 	}
518 
519 	/* Compute v_obs */
520 	j = 0;
521 	b = 14;
522 	bit_prev = (values[j] & (1U << 15)) == 0 ? 0 : 1;
523 
524 	v_obs = 0;
525 
526 	for (i = 1; i < numbits; i++) {
527 		bit_this = (values[j] & (1U << b)) == 0 ? 0 : 1;
528 		if (b == 0) {
529 			b = 15;
530 			j++;
531 		} else {
532 			b--;
533 		}
534 
535 		v_obs += bit_this ^ bit_prev;
536 
537 		bit_prev = bit_this;
538 	}
539 
540 	v_obs += 1;
541 
542 	numer = fabs(v_obs - (2.0 * numbits * pi * (1.0 - pi)));
543 	denom = 2.0 * sqrt(2.0 * numbits) * pi * (1.0 - pi);
544 
545 	p_value = erfc(numer / denom);
546 
547 	return (p_value);
548 }
549 
550 /*
551  * This is the block frequency test taken from the NIST SP 800-22 RNG
552  * test suite.
553  */
554 static double
blockfrequency(isc_mem_t * mctx,uint16_t * values,size_t length)555 blockfrequency(isc_mem_t *mctx, uint16_t *values, size_t length) {
556 	uint32_t i;
557 	uint32_t numbits;
558 	uint32_t mbits;
559 	uint32_t mwords;
560 	uint32_t numblocks;
561 	double *pi;
562 	double chi_square;
563 	double p_value;
564 
565 	numbits = length * sizeof(*values) * 8;
566 	mbits = 32000;
567 	mwords = mbits / 16;
568 	numblocks = numbits / mbits;
569 
570 	if (verbose) {
571 		print_message("# numblocks=%u\n", numblocks);
572 	}
573 
574 	/* Preconditions (section 2.2.7 in NIST SP 800-22) */
575 	assert_true(numbits >= 100);
576 	assert_true(mbits >= 20);
577 	assert_true((double)mbits > (0.01 * numbits));
578 	assert_true(numblocks < 100);
579 	assert_true(numbits >= (mbits * numblocks));
580 
581 	pi = isc_mem_get(mctx, numblocks * sizeof(double));
582 	assert_non_null(pi);
583 
584 	for (i = 0; i < numblocks; i++) {
585 		uint32_t j;
586 		pi[i] = 0.0;
587 		for (j = 0; j < mwords; j++) {
588 			uint32_t idx;
589 
590 			idx = i * mwords + j;
591 			pi[i] += bitcounts_table[values[idx]];
592 		}
593 		pi[i] /= mbits;
594 	}
595 
596 	/* Compute chi_square */
597 	chi_square = 0.0;
598 	for (i = 0; i < numblocks; i++) {
599 		chi_square += (pi[i] - 0.5) * (pi[i] - 0.5);
600 	}
601 
602 	chi_square *= 4 * mbits;
603 
604 	isc_mem_put(mctx, pi, numblocks * sizeof(double));
605 
606 	if (verbose) {
607 		print_message("# chi_square=%f\n", chi_square);
608 	}
609 
610 	p_value = igamc(numblocks * 0.5, chi_square * 0.5);
611 
612 	return (p_value);
613 }
614 
615 /*
616  * This is the binary matrix rank test taken from the NIST SP 800-22 RNG
617  * test suite.
618  */
619 static double
binarymatrixrank(isc_mem_t * mctx,uint16_t * values,size_t length)620 binarymatrixrank(isc_mem_t *mctx, uint16_t *values, size_t length) {
621 	uint32_t i;
622 	size_t matrix_m;
623 	size_t matrix_q;
624 	uint32_t num_matrices;
625 	size_t numbits;
626 	uint32_t fm_0;
627 	uint32_t fm_1;
628 	uint32_t fm_rest;
629 	double term1;
630 	double term2;
631 	double term3;
632 	double chi_square;
633 	double p_value;
634 
635 	UNUSED(mctx);
636 
637 	matrix_m = 32;
638 	matrix_q = 32;
639 	num_matrices = length / ((matrix_m * matrix_q) / 16);
640 	numbits = num_matrices * matrix_m * matrix_q;
641 
642 	/* Preconditions (section 2.5.7 in NIST SP 800-22) */
643 	assert_int_equal(matrix_m, 32);
644 	assert_int_equal(matrix_q, 32);
645 	assert_true(numbits >= (38 * matrix_m * matrix_q));
646 
647 	fm_0 = 0;
648 	fm_1 = 0;
649 	fm_rest = 0;
650 	for (i = 0; i < num_matrices; i++) {
651 		/*
652 		 * Each uint32_t supplies 32 bits, so a 32x32 bit matrix
653 		 * takes up uint32_t array of size 32.
654 		 */
655 		uint32_t bits[32];
656 		int j;
657 		uint32_t rank;
658 
659 		for (j = 0; j < 32; j++) {
660 			size_t idx;
661 			uint32_t r1;
662 			uint32_t r2;
663 
664 			idx = i * ((matrix_m * matrix_q) / 16);
665 			idx += j * 2;
666 
667 			r1 = values[idx];
668 			r2 = values[idx + 1];
669 			bits[j] = (r1 << 16) | r2;
670 		}
671 
672 		rank = matrix_binaryrank(bits, matrix_m, matrix_q);
673 
674 		if (rank == matrix_m) {
675 			fm_0++;
676 		} else if (rank == (matrix_m - 1)) {
677 			fm_1++;
678 		} else {
679 			fm_rest++;
680 		}
681 	}
682 
683 	/* Compute chi_square */
684 	term1 = ((fm_0 - (0.2888 * num_matrices)) *
685 		 (fm_0 - (0.2888 * num_matrices))) /
686 		(0.2888 * num_matrices);
687 	term2 = ((fm_1 - (0.5776 * num_matrices)) *
688 		 (fm_1 - (0.5776 * num_matrices))) /
689 		(0.5776 * num_matrices);
690 	term3 = ((fm_rest - (0.1336 * num_matrices)) *
691 		 (fm_rest - (0.1336 * num_matrices))) /
692 		(0.1336 * num_matrices);
693 
694 	chi_square = term1 + term2 + term3;
695 
696 	if (verbose) {
697 		print_message("# fm_0=%u, fm_1=%u, fm_rest=%u, chi_square=%f\n",
698 			      fm_0, fm_1, fm_rest, chi_square);
699 	}
700 
701 	p_value = exp(-chi_square * 0.5);
702 
703 	return (p_value);
704 }
705 
706 /***
707  *** Tests for isc_random32() function
708  ***/
709 
710 /* Monobit test for the RANDOM */
711 static void
isc_random32_monobit(void ** state)712 isc_random32_monobit(void **state) {
713 	UNUSED(state);
714 
715 	random_test(monobit, ISC_RANDOM32);
716 }
717 
718 /* Runs test for the RANDOM */
719 static void
isc_random32_runs(void ** state)720 isc_random32_runs(void **state) {
721 	UNUSED(state);
722 
723 	random_test(runs, ISC_RANDOM32);
724 }
725 
726 /* Block frequency test for the RANDOM */
727 static void
isc_random32_blockfrequency(void ** state)728 isc_random32_blockfrequency(void **state) {
729 	UNUSED(state);
730 
731 	random_test(blockfrequency, ISC_RANDOM32);
732 }
733 
734 /* Binary matrix rank test for the RANDOM */
735 static void
isc_random32_binarymatrixrank(void ** state)736 isc_random32_binarymatrixrank(void **state) {
737 	UNUSED(state);
738 
739 	random_test(binarymatrixrank, ISC_RANDOM32);
740 }
741 
742 /***
743  *** Tests for isc_random_bytes() function
744  ***/
745 
746 /* Monobit test for the RANDOM */
747 static void
isc_random_bytes_monobit(void ** state)748 isc_random_bytes_monobit(void **state) {
749 	UNUSED(state);
750 
751 	random_test(monobit, ISC_RANDOM_BYTES);
752 }
753 
754 /* Runs test for the RANDOM */
755 static void
isc_random_bytes_runs(void ** state)756 isc_random_bytes_runs(void **state) {
757 	UNUSED(state);
758 
759 	random_test(runs, ISC_RANDOM_BYTES);
760 }
761 
762 /* Block frequency test for the RANDOM */
763 static void
isc_random_bytes_blockfrequency(void ** state)764 isc_random_bytes_blockfrequency(void **state) {
765 	UNUSED(state);
766 
767 	random_test(blockfrequency, ISC_RANDOM_BYTES);
768 }
769 
770 /* Binary matrix rank test for the RANDOM */
771 static void
isc_random_bytes_binarymatrixrank(void ** state)772 isc_random_bytes_binarymatrixrank(void **state) {
773 	UNUSED(state);
774 
775 	random_test(binarymatrixrank, ISC_RANDOM_BYTES);
776 }
777 
778 /***
779  *** Tests for isc_random_uniform() function:
780  ***/
781 
782 /* Monobit test for the RANDOM */
783 static void
isc_random_uniform_monobit(void ** state)784 isc_random_uniform_monobit(void **state) {
785 	UNUSED(state);
786 
787 	random_test(monobit, ISC_RANDOM_UNIFORM);
788 }
789 
790 /* Runs test for the RANDOM */
791 static void
isc_random_uniform_runs(void ** state)792 isc_random_uniform_runs(void **state) {
793 	UNUSED(state);
794 
795 	random_test(runs, ISC_RANDOM_UNIFORM);
796 }
797 
798 /* Block frequency test for the RANDOM */
799 static void
isc_random_uniform_blockfrequency(void ** state)800 isc_random_uniform_blockfrequency(void **state) {
801 	UNUSED(state);
802 
803 	random_test(blockfrequency, ISC_RANDOM_UNIFORM);
804 }
805 
806 /* Binary matrix rank test for the RANDOM */
807 static void
isc_random_uniform_binarymatrixrank(void ** state)808 isc_random_uniform_binarymatrixrank(void **state) {
809 	UNUSED(state);
810 
811 	random_test(binarymatrixrank, ISC_RANDOM_UNIFORM);
812 }
813 
814 /* Tests for isc_nonce_bytes() function */
815 
816 /* Monobit test for the RANDOM */
817 static void
isc_nonce_bytes_monobit(void ** state)818 isc_nonce_bytes_monobit(void **state) {
819 	UNUSED(state);
820 
821 	random_test(monobit, ISC_NONCE_BYTES);
822 }
823 
824 /* Runs test for the RANDOM */
825 static void
isc_nonce_bytes_runs(void ** state)826 isc_nonce_bytes_runs(void **state) {
827 	UNUSED(state);
828 
829 	random_test(runs, ISC_NONCE_BYTES);
830 }
831 
832 /* Block frequency test for the RANDOM */
833 static void
isc_nonce_bytes_blockfrequency(void ** state)834 isc_nonce_bytes_blockfrequency(void **state) {
835 	UNUSED(state);
836 
837 	random_test(blockfrequency, ISC_NONCE_BYTES);
838 }
839 
840 /* Binary matrix rank test for the RANDOM */
841 static void
isc_nonce_bytes_binarymatrixrank(void ** state)842 isc_nonce_bytes_binarymatrixrank(void **state) {
843 	UNUSED(state);
844 
845 	random_test(binarymatrixrank, ISC_NONCE_BYTES);
846 }
847 
848 int
main(int argc,char ** argv)849 main(int argc, char **argv) {
850 	const struct CMUnitTest tests[] = {
851 		cmocka_unit_test(isc_random32_monobit),
852 		cmocka_unit_test(isc_random32_runs),
853 		cmocka_unit_test(isc_random32_blockfrequency),
854 		cmocka_unit_test(isc_random32_binarymatrixrank),
855 		cmocka_unit_test(isc_random_bytes_monobit),
856 		cmocka_unit_test(isc_random_bytes_runs),
857 		cmocka_unit_test(isc_random_bytes_blockfrequency),
858 		cmocka_unit_test(isc_random_bytes_binarymatrixrank),
859 		cmocka_unit_test(isc_random_uniform_monobit),
860 		cmocka_unit_test(isc_random_uniform_runs),
861 		cmocka_unit_test(isc_random_uniform_blockfrequency),
862 		cmocka_unit_test(isc_random_uniform_binarymatrixrank),
863 		cmocka_unit_test(isc_nonce_bytes_monobit),
864 		cmocka_unit_test(isc_nonce_bytes_runs),
865 		cmocka_unit_test(isc_nonce_bytes_blockfrequency),
866 		cmocka_unit_test(isc_nonce_bytes_binarymatrixrank),
867 	};
868 	int c;
869 
870 	while ((c = isc_commandline_parse(argc, argv, "v")) != -1) {
871 		switch (c) {
872 		case 'v':
873 			verbose = true;
874 			break;
875 		default:
876 			break;
877 		}
878 	}
879 
880 	return (cmocka_run_group_tests(tests, _setup, _teardown));
881 }
882 
883 #else /* HAVE_CMOCKA */
884 
885 #include <stdio.h>
886 
887 int
main(void)888 main(void) {
889 	printf("1..0 # Skipped: cmocka not available\n");
890 	return (SKIPPED_TEST_EXIT_CODE);
891 }
892 
893 #endif /* if HAVE_CMOCKA */
894