Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/random.hpp" |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 23 | #include "ndn-cxx/security/impl/openssl.hpp" |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 26 | |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 27 | #include <array> |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 28 | #include <thread> |
| 29 | |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | ca48851 | 2019-01-18 17:20:09 -0500 | [diff] [blame^] | 36 | BOOST_AUTO_TEST_CASE(ThreadLocalEngine) |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 37 | { |
| 38 | random::RandomNumberEngine* r1 = &random::getRandomNumberEngine(); |
| 39 | random::RandomNumberEngine* r2 = nullptr; |
| 40 | std::thread t([&r2] { r2 = &random::getRandomNumberEngine(); }); |
| 41 | t.join(); |
| 42 | random::RandomNumberEngine* r3 = &random::getRandomNumberEngine(); |
| 43 | |
| 44 | BOOST_CHECK(r2 != nullptr); |
| 45 | BOOST_CHECK_NE(r1, r2); |
| 46 | BOOST_CHECK_EQUAL(r1, r3); |
| 47 | } |
| 48 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 49 | // This fixture uses OpenSSL routines to set a dummy random generator that always fails |
| 50 | class FailRandMethodFixture |
| 51 | { |
| 52 | public: |
| 53 | FailRandMethodFixture() |
| 54 | : m_dummyRandMethod{&FailRandMethodFixture::seed, |
| 55 | &FailRandMethodFixture::bytes, |
| 56 | &FailRandMethodFixture::cleanup, |
| 57 | &FailRandMethodFixture::add, |
| 58 | &FailRandMethodFixture::pseudorand, |
| 59 | &FailRandMethodFixture::status} |
| 60 | { |
| 61 | m_origRandMethod = RAND_get_rand_method(); |
| 62 | RAND_set_rand_method(&m_dummyRandMethod); |
| 63 | } |
| 64 | |
| 65 | ~FailRandMethodFixture() |
| 66 | { |
| 67 | RAND_set_rand_method(m_origRandMethod); |
| 68 | } |
| 69 | |
| 70 | private: // RAND_METHOD callbacks |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 71 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 72 | static void |
| 73 | seed(const void* buf, int num) |
| 74 | { |
| 75 | } |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 76 | #else |
| 77 | static int |
| 78 | seed(const void* buf, int num) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 83 | |
| 84 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 85 | bytes(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 86 | { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | cleanup() |
| 92 | { |
| 93 | } |
| 94 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 95 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 96 | static void |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 97 | add(const void* buf, int num, double entropy) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 98 | { |
| 99 | } |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 100 | #else |
| 101 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 102 | add(const void* buf, int num, double entropy) |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 103 | { |
| 104 | return 0; |
| 105 | } |
| 106 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 107 | |
| 108 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 109 | pseudorand(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | status() |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | const RAND_METHOD* m_origRandMethod; |
| 122 | RAND_METHOD m_dummyRandMethod; |
| 123 | }; |
| 124 | |
| 125 | BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture) |
| 126 | { |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 127 | std::array<uint8_t, 1024> buf; |
| 128 | BOOST_CHECK_THROW(random::generateSecureBytes(buf.data(), buf.size()), std::runtime_error); |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_SUITE_END() // TestRandom |
| 132 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 133 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 134 | } // namespace tests |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 135 | } // namespace ndn |