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 | 273ea01 | 2022-02-20 17:50:02 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2022 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 | static int |
| 72 | seed(const void* buf, int num) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 76 | |
| 77 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 78 | bytes(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | cleanup() |
| 85 | { |
| 86 | } |
| 87 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 88 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 89 | add(const void* buf, int num, double entropy) |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 90 | { |
| 91 | return 0; |
| 92 | } |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 93 | |
| 94 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 95 | pseudorand(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 96 | { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | status() |
| 102 | { |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | const RAND_METHOD* m_origRandMethod; |
| 108 | RAND_METHOD m_dummyRandMethod; |
| 109 | }; |
| 110 | |
| 111 | BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture) |
| 112 | { |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 113 | std::array<uint8_t, 1024> buf; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 114 | BOOST_CHECK_THROW(random::generateSecureBytes(buf), std::runtime_error); |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_SUITE_END() // TestRandom |
| 118 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 119 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 120 | } // namespace tests |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 121 | } // namespace ndn |