blob: 18af4ff63c8d5d0aef1f459cdf812b9b1c6ced00 [file] [log] [blame]
Alexander Afanasyev75088672014-07-14 11:58:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0b537b92015-08-27 14:39:34 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyev75088672014-07-14 11:58:30 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/random.hpp"
23
24#include "boost-test.hpp"
25#include <boost/mpl/vector.hpp>
Yingdi Yu0b537b92015-08-27 14:39:34 -070026#include "security/detail/openssl.hpp"
Alexander Afanasyev75088672014-07-14 11:58:30 -070027
28#include <cmath>
29
30namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031namespace tests {
Alexander Afanasyev75088672014-07-14 11:58:30 -070032
Yingdi Yu0b537b92015-08-27 14:39:34 -070033BOOST_AUTO_TEST_SUITE(Util)
34BOOST_AUTO_TEST_SUITE(TestRandom)
Alexander Afanasyev75088672014-07-14 11:58:30 -070035
36class PseudoRandomWord32
37{
38public:
39 static uint32_t
40 generate()
41 {
42 return random::generateWord32();
43 }
44};
45
46class PseudoRandomWord64
47{
48public:
49 static uint64_t
50 generate()
51 {
52 return random::generateWord64();
53 }
54};
55
56class SecureRandomWord32
57{
58public:
59 static uint32_t
60 generate()
61 {
62 return random::generateSecureWord32();
63 }
64};
65
66class SecureRandomWord64
67{
68public:
69 static uint64_t
70 generate()
71 {
72 return random::generateSecureWord64();
73 }
74};
75
76typedef boost::mpl::vector<PseudoRandomWord32,
77 PseudoRandomWord64,
78 SecureRandomWord32,
79 SecureRandomWord64> RandomGenerators;
80
Yingdi Yu0b537b92015-08-27 14:39:34 -070081
82static double
83getDeviation(const std::vector<uint32_t>& counts, size_t size)
Alexander Afanasyev75088672014-07-14 11:58:30 -070084{
85 // Kolmogorov-Smirnov Goodness-of-Fit Test
86 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
87
Yingdi Yu0b537b92015-08-27 14:39:34 -070088 std::vector<double> edf(counts.size(), 0.0);
89 double probability = 0.0;
90 for (size_t i = 0; i < counts.size(); i++) {
91 probability += 1.0 * counts[i] / size;
92 edf[i] = probability;
93 }
94
95 double t = 0.0;
96 for (size_t i = 0; i < counts.size(); i++) {
97 t = std::max(t, std::abs(edf[i] - (i * 1.0 / counts.size())));
98 }
99
100 return t;
101}
102
103
104BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators)
105{
Alexander Afanasyev75088672014-07-14 11:58:30 -0700106 const size_t MAX_BINS = 32;
107 const uint32_t MAX_ITERATIONS = 35;
108
109 std::vector<uint32_t> counts(MAX_BINS, 0);
110
111 for (uint32_t i = 0; i < MAX_ITERATIONS; i++) {
112 counts[RandomGenerator::generate() % MAX_BINS]++;
113 }
114
Yingdi Yu0b537b92015-08-27 14:39:34 -0700115 // Check if it is uniform distribution with confidence 0.95
116 // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7
117 BOOST_WARN_LE(getDeviation(counts, MAX_ITERATIONS), 0.230);
118}
Alexander Afanasyev75088672014-07-14 11:58:30 -0700119
Yingdi Yu0b537b92015-08-27 14:39:34 -0700120BOOST_AUTO_TEST_CASE(GenerateRandomBytes)
121{
122 // Kolmogorov-Smirnov Goodness-of-Fit Test
123 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm
124
125 uint8_t buf[1024] = {0};
126 random::generateSecureBytes(buf, sizeof(buf));
127
128 std::vector<uint32_t> counts(256, 0);
129
130 for (size_t i = 0; i < sizeof(buf); i++) {
131 counts[buf[i]]++;
Alexander Afanasyev75088672014-07-14 11:58:30 -0700132 }
133
134 // Check if it is uniform distribution with confidence 0.95
135 // http://dlc.erieri.com/onlinetextbook/index.cfm?fuseaction=textbook.appendix&FileName=Table7
Yingdi Yu0b537b92015-08-27 14:39:34 -0700136 BOOST_WARN_LE(getDeviation(counts, sizeof(buf)), 0.230);
Alexander Afanasyev75088672014-07-14 11:58:30 -0700137}
138
Yingdi Yu0b537b92015-08-27 14:39:34 -0700139// This fixture uses OpenSSL routines to set a dummy random generator that always fails
140class FailRandMethodFixture
141{
142public:
143 FailRandMethodFixture()
144 : m_dummyRandMethod{&FailRandMethodFixture::seed,
145 &FailRandMethodFixture::bytes,
146 &FailRandMethodFixture::cleanup,
147 &FailRandMethodFixture::add,
148 &FailRandMethodFixture::pseudorand,
149 &FailRandMethodFixture::status}
150 {
151 m_origRandMethod = RAND_get_rand_method();
152 RAND_set_rand_method(&m_dummyRandMethod);
153 }
154
155 ~FailRandMethodFixture()
156 {
157 RAND_set_rand_method(m_origRandMethod);
158 }
159
160private: // RAND_METHOD callbacks
161 static void
162 seed(const void* buf, int num)
163 {
164 }
165
166 static int
167 bytes(unsigned char *buf, int num)
168 {
169 return 0;
170 }
171
172 static void
173 cleanup()
174 {
175 }
176
177 static void
178 add(const void *buf, int num, double entropy)
179 {
180 }
181
182 static int
183 pseudorand(unsigned char *buf, int num)
184 {
185 return 0;
186 }
187
188 static int
189 status()
190 {
191 return 0;
192 }
193
194private:
195 const RAND_METHOD* m_origRandMethod;
196 RAND_METHOD m_dummyRandMethod;
197};
198
199BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
200{
201 uint8_t buf[1024] = {0};
202 BOOST_CHECK_THROW(random::generateSecureBytes(buf, sizeof(buf)), std::runtime_error);
203}
204
205BOOST_AUTO_TEST_SUITE_END() // TestRandom
206BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev75088672014-07-14 11:58:30 -0700207
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800208} // namespace tests
Alexander Afanasyev75088672014-07-14 11:58:30 -0700209} // namespace ndn