blob: 9cf7325c67b5ff6e632a2e40552304af808b22a8 [file] [log] [blame]
Alexander Afanasyev75088672014-07-14 11:58:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
3 * Copyright (c) 2013-2018 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/random.hpp"
Junxiao Shi24c5a002018-12-12 04:47:15 +000023#include "ndn-cxx/security/impl/openssl.hpp"
Alexander Afanasyev75088672014-07-14 11:58:30 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento74daf742018-11-23 18:14:13 -050026
27#include <boost/mpl/vector.hpp>
Alexander Afanasyev75088672014-07-14 11:58:30 -070028#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
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700161#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -0700162 static void
163 seed(const void* buf, int num)
164 {
165 }
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700166#else
167 static int
168 seed(const void* buf, int num)
169 {
170 return 0;
171 }
172#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -0700173
174 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500175 bytes(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -0700176 {
177 return 0;
178 }
179
180 static void
181 cleanup()
182 {
183 }
184
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700185#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -0700186 static void
Davide Pesavento74daf742018-11-23 18:14:13 -0500187 add(const void* buf, int num, double entropy)
Yingdi Yu0b537b92015-08-27 14:39:34 -0700188 {
189 }
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700190#else
191 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500192 add(const void* buf, int num, double entropy)
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700193 {
194 return 0;
195 }
196#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -0700197
198 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500199 pseudorand(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -0700200 {
201 return 0;
202 }
203
204 static int
205 status()
206 {
207 return 0;
208 }
209
210private:
211 const RAND_METHOD* m_origRandMethod;
212 RAND_METHOD m_dummyRandMethod;
213};
214
215BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
216{
217 uint8_t buf[1024] = {0};
218 BOOST_CHECK_THROW(random::generateSecureBytes(buf, sizeof(buf)), std::runtime_error);
219}
220
221BOOST_AUTO_TEST_SUITE_END() // TestRandom
222BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev75088672014-07-14 11:58:30 -0700223
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800224} // namespace tests
Alexander Afanasyev75088672014-07-14 11:58:30 -0700225} // namespace ndn