blob: 388bb989cc4be8fefe46d9bd33a9c2568fa64561 [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/*
Davide Pesavento273ea012022-02-20 17:50:02 -05003 * Copyright (c) 2013-2022 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"
Alexander Afanasyev75088672014-07-14 11:58:30 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento74daf742018-11-23 18:14:13 -050025
Davide Pesavento80d671f2022-06-08 04:04:52 -040026#include <openssl/rand.h>
27
Davide Pesavento8db61522019-01-11 16:44:50 -050028#include <array>
Davide Pesavento8db61522019-01-11 16:44:50 -050029#include <thread>
30
Alexander Afanasyev75088672014-07-14 11:58:30 -070031namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032namespace tests {
Alexander Afanasyev75088672014-07-14 11:58:30 -070033
Yingdi Yu0b537b92015-08-27 14:39:34 -070034BOOST_AUTO_TEST_SUITE(Util)
35BOOST_AUTO_TEST_SUITE(TestRandom)
Alexander Afanasyev75088672014-07-14 11:58:30 -070036
Davide Pesaventoca488512019-01-18 17:20:09 -050037BOOST_AUTO_TEST_CASE(ThreadLocalEngine)
Davide Pesavento8db61522019-01-11 16:44:50 -050038{
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 Pesavento4999b2e2022-04-23 00:32:25 -040050// 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 Yu0b537b92015-08-27 14:39:34 -070057// This fixture uses OpenSSL routines to set a dummy random generator that always fails
58class FailRandMethodFixture
59{
60public:
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
78private: // RAND_METHOD callbacks
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070079 static int
80 seed(const void* buf, int num)
81 {
82 return 0;
83 }
Yingdi Yu0b537b92015-08-27 14:39:34 -070084
85 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050086 bytes(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -070087 {
88 return 0;
89 }
90
91 static void
92 cleanup()
93 {
94 }
95
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070096 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050097 add(const void* buf, int num, double entropy)
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070098 {
99 return 0;
100 }
Yingdi Yu0b537b92015-08-27 14:39:34 -0700101
102 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500103 pseudorand(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -0700104 {
105 return 0;
106 }
107
108 static int
109 status()
110 {
111 return 0;
112 }
113
114private:
115 const RAND_METHOD* m_origRandMethod;
116 RAND_METHOD m_dummyRandMethod;
117};
118
119BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
120{
Davide Pesavento4999b2e2022-04-23 00:32:25 -0400121 std::array<uint8_t, 512> buf;
Davide Pesavento765abc92021-12-27 00:44:04 -0500122 BOOST_CHECK_THROW(random::generateSecureBytes(buf), std::runtime_error);
Yingdi Yu0b537b92015-08-27 14:39:34 -0700123}
124
125BOOST_AUTO_TEST_SUITE_END() // TestRandom
126BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev75088672014-07-14 11:58:30 -0700127
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800128} // namespace tests
Alexander Afanasyev75088672014-07-14 11:58:30 -0700129} // namespace ndn