File: test-dns_random_hh.cc

package info (click to toggle)
pdns-recursor 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,108 kB
  • sloc: cpp: 109,513; javascript: 20,651; python: 5,657; sh: 5,069; makefile: 780; ansic: 582; xml: 37
file content (126 lines) | stat: -rw-r--r-- 3,114 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef BOOST_TEST_DYN_LINK
#define BOOST_TEST_DYN_LINK
#endif

#define BOOST_TEST_NO_MAIN

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/test/unit_test.hpp>
#include <boost/assign/std/map.hpp>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wextra"
#include <boost/accumulators/statistics/median.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#pragma GCC diagnostic pop

#include "arguments.hh"
#include "dns_random.hh"
#include "namespaces.hh"

using namespace boost::accumulators;

using acc_t = accumulator_set<double, stats<tag::median(with_p_square_quantile), tag::mean(immediate)>>;

BOOST_AUTO_TEST_SUITE(test_dns_random_hh)

const std::vector<string> rndSources = {
  "auto",
  "urandom",
#if defined(HAVE_GETRANDOM)
  "getrandom",
#endif
#if defined(HAVE_ARC4RANDOM)
  "arc4random",
#endif
#if defined(HAVE_RANDOMBYTES_STIR)
  "sodium",
#endif
#if defined(HAVE_RAND_BYTES)
  "openssl",
#endif
#if defined(HAVE_KISS_RNG)
  "kiss",
#endif
};

BOOST_AUTO_TEST_CASE(test_dns_random_garbage)
{
  ::arg().set("rng") = "garbage";
  ::arg().set("entropy-source") = "/dev/urandom";
}

BOOST_AUTO_TEST_CASE(test_dns_random_upper_bound)
{
  ::arg().set("rng") = "auto";
  ::arg().set("entropy-source") = "/dev/urandom";

  map<unsigned int, bool> seen;
  for (unsigned int iteration = 0; iteration < 100000; ++iteration) {
    seen[dns_random(10)] = true;
  }

  BOOST_CHECK_EQUAL(seen[0], true);
  BOOST_CHECK_EQUAL(seen[1], true);
  BOOST_CHECK_EQUAL(seen[2], true);
  BOOST_CHECK_EQUAL(seen[3], true);
  BOOST_CHECK_EQUAL(seen[4], true);
  BOOST_CHECK_EQUAL(seen[5], true);
  BOOST_CHECK_EQUAL(seen[6], true);
  BOOST_CHECK_EQUAL(seen[7], true);
  BOOST_CHECK_EQUAL(seen[8], true);
  BOOST_CHECK_EQUAL(seen[9], true);
  BOOST_CHECK_EQUAL(seen[10], false);
}

static void test_dns_random_avg(const string& source)
{
  ::arg().set("rng") = source;
  ::arg().set("entropy-source") = "/dev/urandom";

  acc_t acc;

  for (unsigned int iteration = 0; iteration < 100000; ++iteration) {
    acc(dns_random(100000) / 100000.0);
  }
  BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);

  // please add covariance tests, chi-square, Kolmogorov-Smirnov
}

static void test_dns_random_uint32_avg(const string& source)
{
  ::arg().set("rng") = source;
  ::arg().set("entropy-source") = "/dev/urandom";

  acc_t acc;

  for (unsigned int iteration = 0; iteration < 100000; ++iteration) {
    acc(dns_random_uint32() / static_cast<double>(pdns::dns_random_engine::max()));
  }
  BOOST_CHECK_CLOSE(0.5, median(acc), 2.0); // within 2%
  BOOST_CHECK_CLOSE(0.5, mean(acc), 2.0);

  // please add covariance tests, chi-square, Kolmogorov-Smirnov
}

BOOST_AUTO_TEST_CASE(test_dns_random_average)
{
  for (const auto& source : rndSources) {
    test_dns_random_avg(source);
  }
}

BOOST_AUTO_TEST_CASE(test_dns_random_uint32_average)
{
  for (const auto& source : rndSources) {
    test_dns_random_uint32_avg(source);
  }
}

BOOST_AUTO_TEST_SUITE_END()