blob: b72e7546d535d8a6ec74adecbf0595fe2b80a27d [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031namespace ndn::tests {
Alexander Afanasyev75088672014-07-14 11:58:30 -070032
Yingdi Yu0b537b92015-08-27 14:39:34 -070033BOOST_AUTO_TEST_SUITE(Util)
34BOOST_AUTO_TEST_SUITE(TestRandom)
Alexander Afanasyev75088672014-07-14 11:58:30 -070035
Davide Pesaventoca488512019-01-18 17:20:09 -050036BOOST_AUTO_TEST_CASE(ThreadLocalEngine)
Davide Pesavento8db61522019-01-11 16:44:50 -050037{
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
Davide Pesavento4999b2e2022-04-23 00:32:25 -040049// RAND_get_rand_method() and RAND_set_rand_method() are deprecated in OpenSSL 3.0
50// and there is no straightforward replacement. Suppress the warnings for now, but
51// we may have to drop this test case when OpenSSL removes the RAND_METHOD API.
52#if OPENSSL_VERSION_NUMBER >= 0x30000000L
53#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
54#endif
55
Yingdi Yu0b537b92015-08-27 14:39:34 -070056// This fixture uses OpenSSL routines to set a dummy random generator that always fails
57class FailRandMethodFixture
58{
59public:
60 FailRandMethodFixture()
61 : m_dummyRandMethod{&FailRandMethodFixture::seed,
62 &FailRandMethodFixture::bytes,
63 &FailRandMethodFixture::cleanup,
64 &FailRandMethodFixture::add,
65 &FailRandMethodFixture::pseudorand,
66 &FailRandMethodFixture::status}
67 {
68 m_origRandMethod = RAND_get_rand_method();
69 RAND_set_rand_method(&m_dummyRandMethod);
70 }
71
72 ~FailRandMethodFixture()
73 {
74 RAND_set_rand_method(m_origRandMethod);
75 }
76
77private: // RAND_METHOD callbacks
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070078 static int
79 seed(const void* buf, int num)
80 {
81 return 0;
82 }
Yingdi Yu0b537b92015-08-27 14:39:34 -070083
84 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050085 bytes(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -070086 {
87 return 0;
88 }
89
90 static void
91 cleanup()
92 {
93 }
94
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070095 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050096 add(const void* buf, int num, double entropy)
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070097 {
98 return 0;
99 }
Yingdi Yu0b537b92015-08-27 14:39:34 -0700100
101 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500102 pseudorand(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -0700103 {
104 return 0;
105 }
106
107 static int
108 status()
109 {
110 return 0;
111 }
112
113private:
114 const RAND_METHOD* m_origRandMethod;
115 RAND_METHOD m_dummyRandMethod;
116};
117
118BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
119{
Davide Pesavento4999b2e2022-04-23 00:32:25 -0400120 std::array<uint8_t, 512> buf;
Davide Pesavento765abc92021-12-27 00:44:04 -0500121 BOOST_CHECK_THROW(random::generateSecureBytes(buf), std::runtime_error);
Yingdi Yu0b537b92015-08-27 14:39:34 -0700122}
123
124BOOST_AUTO_TEST_SUITE_END() // TestRandom
125BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev75088672014-07-14 11:58:30 -0700126
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400127} // namespace ndn::tests