Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 4 | * |
| 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 Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 26 | #include "security/detail/openssl.hpp" |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 27 | |
| 28 | #include <cmath> |
| 29 | |
| 30 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | namespace tests { |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 33 | BOOST_AUTO_TEST_SUITE(Util) |
| 34 | BOOST_AUTO_TEST_SUITE(TestRandom) |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 35 | |
| 36 | class PseudoRandomWord32 |
| 37 | { |
| 38 | public: |
| 39 | static uint32_t |
| 40 | generate() |
| 41 | { |
| 42 | return random::generateWord32(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | class PseudoRandomWord64 |
| 47 | { |
| 48 | public: |
| 49 | static uint64_t |
| 50 | generate() |
| 51 | { |
| 52 | return random::generateWord64(); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | class SecureRandomWord32 |
| 57 | { |
| 58 | public: |
| 59 | static uint32_t |
| 60 | generate() |
| 61 | { |
| 62 | return random::generateSecureWord32(); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | class SecureRandomWord64 |
| 67 | { |
| 68 | public: |
| 69 | static uint64_t |
| 70 | generate() |
| 71 | { |
| 72 | return random::generateSecureWord64(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | typedef boost::mpl::vector<PseudoRandomWord32, |
| 77 | PseudoRandomWord64, |
| 78 | SecureRandomWord32, |
| 79 | SecureRandomWord64> RandomGenerators; |
| 80 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 81 | |
| 82 | static double |
| 83 | getDeviation(const std::vector<uint32_t>& counts, size_t size) |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 84 | { |
| 85 | // Kolmogorov-Smirnov Goodness-of-Fit Test |
| 86 | // http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm |
| 87 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 88 | 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 | |
| 104 | BOOST_AUTO_TEST_CASE_TEMPLATE(GoodnessOfFit, RandomGenerator, RandomGenerators) |
| 105 | { |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 106 | 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 Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 115 | // 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 Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 119 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 120 | BOOST_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 Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 132 | } |
| 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 Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 136 | BOOST_WARN_LE(getDeviation(counts, sizeof(buf)), 0.230); |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame^] | 139 | // This fixture uses OpenSSL routines to set a dummy random generator that always fails |
| 140 | class FailRandMethodFixture |
| 141 | { |
| 142 | public: |
| 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 | |
| 160 | private: // 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 | |
| 194 | private: |
| 195 | const RAND_METHOD* m_origRandMethod; |
| 196 | RAND_METHOD m_dummyRandMethod; |
| 197 | }; |
| 198 | |
| 199 | BOOST_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 | |
| 205 | BOOST_AUTO_TEST_SUITE_END() // TestRandom |
| 206 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 207 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 208 | } // namespace tests |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 209 | } // namespace ndn |