]>
Commit | Line | Data |
---|---|---|
1c2d079d | 1 | #ifndef BOOST_TEST_DYN_LINK |
78cb2bd0 | 2 | #define BOOST_TEST_DYN_LINK |
1c2d079d FM |
3 | #endif |
4 | ||
78cb2bd0 | 5 | #define BOOST_TEST_NO_MAIN |
e59c5996 | 6 | |
9c4701ec | 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 | |
dc125378 | 13 | #pragma GCC diagnostic push |
a0e87a26 | 14 | #pragma GCC diagnostic ignored "-Wextra" |
78cb2bd0 | 15 | #include <boost/accumulators/statistics/median.hpp> |
16 | #include <boost/accumulators/statistics/mean.hpp> | |
17 | #include <boost/accumulators/accumulators.hpp> | |
18 | #include <boost/accumulators/statistics.hpp> | |
dc125378 | 19 | #pragma GCC diagnostic pop |
78cb2bd0 | 20 | |
05739b0e | 21 | #include "arguments.hh" |
78cb2bd0 | 22 | #include "dns_random.hh" |
23 | #include "namespaces.hh" | |
24 | ||
78cb2bd0 | 25 | using namespace boost::accumulators; |
26 | ||
4add9e26 | 27 | using acc_t = accumulator_set<double, stats<tag::median(with_p_square_quantile), tag::mean(immediate)>>; |
78cb2bd0 | 28 | |
29 | BOOST_AUTO_TEST_SUITE(test_dns_random_hh) | |
30 | ||
627eaf3b OM |
31 | const std::vector<string> rndSources = { |
32 | "auto", | |
33 | "urandom", | |
34 | #if defined(HAVE_GETRANDOM) | |
35 | "getrandom", | |
36 | #endif | |
4add9e26 | 37 | #if defined(HAVE_ARC4RANDOM) |
627eaf3b | 38 | "arc4random", |
4add9e26 | 39 | #endif |
627eaf3b OM |
40 | #if defined(HAVE_RANDOMBYTES_STIR) |
41 | "sodium", | |
42 | #endif | |
43 | #if defined(HAVE_RAND_BYTES) | |
44 | "openssl", | |
45 | #endif | |
46 | #if defined(HAVE_KISS_RNG) | |
47 | "kiss", | |
48 | #endif | |
49 | }; | |
05739b0e | 50 | |
03544d6f OM |
51 | BOOST_AUTO_TEST_CASE(test_dns_random_garbage) |
52 | { | |
03544d6f OM |
53 | ::arg().set("rng") = "garbage"; |
54 | ::arg().set("entropy-source") = "/dev/urandom"; | |
05739b0e RG |
55 | } |
56 | ||
03544d6f OM |
57 | BOOST_AUTO_TEST_CASE(test_dns_random_upper_bound) |
58 | { | |
59 | ::arg().set("rng") = "auto"; | |
60 | ::arg().set("entropy-source") = "/dev/urandom"; | |
0abdd3fb | 61 | |
627eaf3b OM |
62 | map<unsigned int, bool> seen; |
63 | for (unsigned int iteration = 0; iteration < 100000; ++iteration) { | |
0abdd3fb PD |
64 | seen[dns_random(10)] = true; |
65 | } | |
66 | ||
67 | BOOST_CHECK_EQUAL(seen[0], true); | |
68 | BOOST_CHECK_EQUAL(seen[1], true); | |
69 | BOOST_CHECK_EQUAL(seen[2], true); | |
70 | BOOST_CHECK_EQUAL(seen[3], true); | |
71 | BOOST_CHECK_EQUAL(seen[4], true); | |
72 | BOOST_CHECK_EQUAL(seen[5], true); | |
73 | BOOST_CHECK_EQUAL(seen[6], true); | |
74 | BOOST_CHECK_EQUAL(seen[7], true); | |
75 | BOOST_CHECK_EQUAL(seen[8], true); | |
76 | BOOST_CHECK_EQUAL(seen[9], true); | |
77 | BOOST_CHECK_EQUAL(seen[10], false); | |
78 | } | |
79 | ||
627eaf3b | 80 | static void test_dns_random_avg(const string& source) |
03544d6f | 81 | { |
627eaf3b | 82 | ::arg().set("rng") = source; |
03544d6f | 83 | ::arg().set("entropy-source") = "/dev/urandom"; |
05739b0e | 84 | |
05739b0e RG |
85 | acc_t acc; |
86 | ||
627eaf3b | 87 | for (unsigned int iteration = 0; iteration < 100000; ++iteration) { |
03544d6f | 88 | acc(dns_random(100000) / 100000.0); |
05739b0e RG |
89 | } |
90 | BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2% | |
91 | BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0); | |
92 | ||
93 | // please add covariance tests, chi-square, Kolmogorov-Smirnov | |
94 | } | |
05739b0e | 95 | |
627eaf3b | 96 | static void test_dns_random_uint32_avg(const string& source) |
03544d6f | 97 | { |
627eaf3b | 98 | ::arg().set("rng") = source; |
03544d6f | 99 | ::arg().set("entropy-source") = "/dev/urandom"; |
05739b0e | 100 | |
05739b0e RG |
101 | acc_t acc; |
102 | ||
627eaf3b OM |
103 | for (unsigned int iteration = 0; iteration < 100000; ++iteration) { |
104 | acc(dns_random_uint32() / static_cast<double>(pdns::dns_random_engine::max())); | |
05739b0e RG |
105 | } |
106 | BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2% | |
107 | BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0); | |
108 | ||
109 | // please add covariance tests, chi-square, Kolmogorov-Smirnov | |
110 | } | |
05739b0e | 111 | |
627eaf3b | 112 | BOOST_AUTO_TEST_CASE(test_dns_random_average) |
03544d6f | 113 | { |
627eaf3b OM |
114 | for (const auto& source : rndSources) { |
115 | test_dns_random_avg(source); | |
05739b0e | 116 | } |
05739b0e | 117 | } |
05739b0e | 118 | |
627eaf3b | 119 | BOOST_AUTO_TEST_CASE(test_dns_random_uint32_average) |
03544d6f | 120 | { |
627eaf3b OM |
121 | for (const auto& source : rndSources) { |
122 | test_dns_random_uint32_avg(source); | |
05739b0e | 123 | } |
05739b0e | 124 | } |
05739b0e | 125 | |
78cb2bd0 | 126 | BOOST_AUTO_TEST_SUITE_END() |