]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/test-dns_random_hh.cc
rec: ensure correct service user on debian
[thirdparty/pdns.git] / pdns / test-dns_random_hh.cc
CommitLineData
78cb2bd0 1#define BOOST_TEST_DYN_LINK
2#define BOOST_TEST_NO_MAIN
e59c5996 3
34eb951c 4// Disable this code for gcc 4.8 and lower
4b587bc5 5#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8) || !__GNUC__
9c4701ec 6
7#ifdef HAVE_CONFIG_H
e59c5996 8#include "config.h"
870a0fe4 9#endif
78cb2bd0 10#include <boost/test/unit_test.hpp>
11#include <boost/assign/std/map.hpp>
fa8fd4d2 12
78cb2bd0 13#include <boost/accumulators/statistics/median.hpp>
14#include <boost/accumulators/statistics/mean.hpp>
15#include <boost/accumulators/accumulators.hpp>
16#include <boost/accumulators/statistics.hpp>
17
05739b0e 18#include "arguments.hh"
78cb2bd0 19#include "dns_random.hh"
20#include "namespaces.hh"
21
e59c5996 22
78cb2bd0 23using namespace boost;
24using namespace boost::accumulators;
25
26typedef accumulator_set<
27 double
28 , stats<boost::accumulators::tag::median(with_p_square_quantile),
232f0877
CH
29 boost::accumulators::tag::mean(immediate)
30 >
78cb2bd0 31 > acc_t;
32
33
34
35BOOST_AUTO_TEST_SUITE(test_dns_random_hh)
36
809137d7 37BOOST_AUTO_TEST_CASE(test_dns_random_auto_average) {
e59c5996 38
05739b0e
RG
39 ::arg().set("rng")="auto";
40 ::arg().set("entropy-source")="/dev/urandom";
e59c5996 41
05739b0e 42 dns_random_init("", true);
e59c5996 43
78cb2bd0 44 acc_t acc;
45
78cb2bd0 46 for(unsigned int n=0; n < 100000; ++n) {
47 acc(dns_random(100000)/100000.0);
48 }
49 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
50 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
e59c5996 51
78cb2bd0 52 // please add covariance tests, chi-square, Kolmogorov-Smirnov
53}
54
809137d7 55BOOST_AUTO_TEST_CASE(test_dns_random_urandom_average) {
05739b0e
RG
56
57 ::arg().set("rng")="urandom";
58 ::arg().set("entropy-source")="/dev/urandom";
59
60 dns_random_init("", true);
61
62 acc_t acc;
63
64 for(unsigned int n=0; n < 100000; ++n) {
65 acc(dns_random(100000)/100000.0);
66 }
67 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
68 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
69
70 // please add covariance tests, chi-square, Kolmogorov-Smirnov
71}
72
809137d7 73BOOST_AUTO_TEST_CASE(test_dns_random_garbage) {
05739b0e
RG
74
75 ::arg().set("rng")="garbage";
76 ::arg().set("entropy-source")="/dev/urandom";
77
78 BOOST_CHECK_THROW(dns_random_init("", true), std::runtime_error);
79}
80
05739b0e 81#if defined(HAVE_GETRANDOM)
809137d7 82BOOST_AUTO_TEST_CASE(test_dns_random_getrandom_average) {
05739b0e
RG
83
84 ::arg().set("rng")="getrandom";
85 ::arg().set("entropy-source")="/dev/urandom";
86
87 dns_random_init("", true);
88
89 acc_t acc;
90
91 for(unsigned int n=0; n < 100000; ++n) {
92 acc(dns_random(100000)/100000.0);
93 }
94 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
95 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
96
97 // please add covariance tests, chi-square, Kolmogorov-Smirnov
98}
99#endif
100
101#if defined(HAVE_ARC4RANDOM)
809137d7 102BOOST_AUTO_TEST_CASE(test_dns_random_getrandom_average) {
05739b0e
RG
103
104 ::arg().set("rng")="arc4random";
105 ::arg().set("entropy-source")="/dev/urandom";
106
107 dns_random_init("", true);
108
109 acc_t acc;
110
111 for(unsigned int n=0; n < 100000; ++n) {
112 acc(dns_random(100000)/100000.0);
113 }
114 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
115 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
116
117 // please add covariance tests, chi-square, Kolmogorov-Smirnov
118}
119#endif
120
121#if defined(HAVE_RANDOMBYTES_STIR)
809137d7 122BOOST_AUTO_TEST_CASE(test_dns_random_sodium_average) {
05739b0e
RG
123
124 ::arg().set("rng")="sodium";
125 ::arg().set("entropy-source")="/dev/urandom";
126
127 dns_random_init("", true);
128
129 acc_t acc;
130
131 for(unsigned int n=0; n < 100000; ++n) {
132 acc(dns_random(100000)/100000.0);
133 }
134 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
135 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
136
137 // please add covariance tests, chi-square, Kolmogorov-Smirnov
138}
139#endif
140
141#if defined(HAVE_RAND_BYTES)
809137d7 142BOOST_AUTO_TEST_CASE(test_dns_random_openssl_average) {
05739b0e
RG
143
144 ::arg().set("rng")="openssl";
145 ::arg().set("entropy-source")="/dev/urandom";
146
147 dns_random_init("", true);
148
149 acc_t acc;
150
151 for(unsigned int n=0; n < 100000; ++n) {
152 acc(dns_random(100000)/100000.0);
153 }
154 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
155 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
156
157 // please add covariance tests, chi-square, Kolmogorov-Smirnov
158}
159#endif
160
161#if defined(HAVE_KISS_RNG)
809137d7 162BOOST_AUTO_TEST_CASE(test_dns_random_kiss_average) {
05739b0e
RG
163
164 ::arg().set("rng")="kiss";
165 ::arg().set("entropy-source")="/dev/urandom";
166
167 dns_random_init("", true);
168
169 acc_t acc;
170
171 for(unsigned int n=0; n < 100000; ++n) {
172 acc(dns_random(100000)/100000.0);
173 }
174 BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
175 BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);
176
177 // please add covariance tests, chi-square, Kolmogorov-Smirnov
178}
179#endif
e59c5996 180
181
78cb2bd0 182BOOST_AUTO_TEST_SUITE_END()
e59c5996 183
4d68b3b1 184#endif