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