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" |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 25 | |
Davide Pesavento | 80d671f | 2022-06-08 04:04:52 -0400 | [diff] [blame] | 26 | #include <openssl/rand.h> |
| 27 | |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 28 | #include <array> |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 29 | #include <thread> |
| 30 | |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 31 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 32 | namespace tests { |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 33 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Util) |
| 35 | BOOST_AUTO_TEST_SUITE(TestRandom) |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | ca48851 | 2019-01-18 17:20:09 -0500 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(ThreadLocalEngine) |
Davide Pesavento | 8db6152 | 2019-01-11 16:44:50 -0500 | [diff] [blame] | 38 | { |
| 39 | random::RandomNumberEngine* r1 = &random::getRandomNumberEngine(); |
| 40 | random::RandomNumberEngine* r2 = nullptr; |
| 41 | std::thread t([&r2] { r2 = &random::getRandomNumberEngine(); }); |
| 42 | t.join(); |
| 43 | random::RandomNumberEngine* r3 = &random::getRandomNumberEngine(); |
| 44 | |
| 45 | BOOST_CHECK(r2 != nullptr); |
| 46 | BOOST_CHECK_NE(r1, r2); |
| 47 | BOOST_CHECK_EQUAL(r1, r3); |
| 48 | } |
| 49 | |
Davide Pesavento | 4999b2e | 2022-04-23 00:32:25 -0400 | [diff] [blame] | 50 | // RAND_get_rand_method() and RAND_set_rand_method() are deprecated in OpenSSL 3.0 |
| 51 | // and there is no straightforward replacement. Suppress the warnings for now, but |
| 52 | // we may have to drop this test case when OpenSSL removes the RAND_METHOD API. |
| 53 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 54 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 55 | #endif |
| 56 | |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 57 | // This fixture uses OpenSSL routines to set a dummy random generator that always fails |
| 58 | class FailRandMethodFixture |
| 59 | { |
| 60 | public: |
| 61 | FailRandMethodFixture() |
| 62 | : m_dummyRandMethod{&FailRandMethodFixture::seed, |
| 63 | &FailRandMethodFixture::bytes, |
| 64 | &FailRandMethodFixture::cleanup, |
| 65 | &FailRandMethodFixture::add, |
| 66 | &FailRandMethodFixture::pseudorand, |
| 67 | &FailRandMethodFixture::status} |
| 68 | { |
| 69 | m_origRandMethod = RAND_get_rand_method(); |
| 70 | RAND_set_rand_method(&m_dummyRandMethod); |
| 71 | } |
| 72 | |
| 73 | ~FailRandMethodFixture() |
| 74 | { |
| 75 | RAND_set_rand_method(m_origRandMethod); |
| 76 | } |
| 77 | |
| 78 | private: // RAND_METHOD callbacks |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 79 | static int |
| 80 | seed(const void* buf, int num) |
| 81 | { |
| 82 | return 0; |
| 83 | } |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 84 | |
| 85 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 86 | bytes(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | cleanup() |
| 93 | { |
| 94 | } |
| 95 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 96 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 97 | add(const void* buf, int num, double entropy) |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 98 | { |
| 99 | return 0; |
| 100 | } |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 101 | |
| 102 | static int |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 103 | pseudorand(unsigned char* buf, int num) |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 104 | { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | status() |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | const RAND_METHOD* m_origRandMethod; |
| 116 | RAND_METHOD m_dummyRandMethod; |
| 117 | }; |
| 118 | |
| 119 | BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture) |
| 120 | { |
Davide Pesavento | 4999b2e | 2022-04-23 00:32:25 -0400 | [diff] [blame] | 121 | std::array<uint8_t, 512> buf; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 122 | BOOST_CHECK_THROW(random::generateSecureBytes(buf), std::runtime_error); |
Yingdi Yu | 0b537b9 | 2015-08-27 14:39:34 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_SUITE_END() // TestRandom |
| 126 | BOOST_AUTO_TEST_SUITE_END() // Util |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 127 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 128 | } // namespace tests |
Alexander Afanasyev | 7508867 | 2014-07-14 11:58:30 -0700 | [diff] [blame] | 129 | } // namespace ndn |