blob: 73c3825097516eca010e191bd54956c05d1a0be2 [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"
Junxiao Shi24c5a002018-12-12 04:47:15 +000023#include "ndn-cxx/security/impl/openssl.hpp"
Alexander Afanasyev75088672014-07-14 11:58:30 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento74daf742018-11-23 18:14:13 -050026
Davide Pesavento8db61522019-01-11 16:44:50 -050027#include <array>
Davide Pesavento8db61522019-01-11 16:44:50 -050028#include <thread>
29
Alexander Afanasyev75088672014-07-14 11:58:30 -070030namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031namespace 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
Yingdi Yu0b537b92015-08-27 14:39:34 -070049// This fixture uses OpenSSL routines to set a dummy random generator that always fails
50class FailRandMethodFixture
51{
52public:
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
70private: // RAND_METHOD callbacks
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070071 static int
72 seed(const void* buf, int num)
73 {
74 return 0;
75 }
Yingdi Yu0b537b92015-08-27 14:39:34 -070076
77 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050078 bytes(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -070079 {
80 return 0;
81 }
82
83 static void
84 cleanup()
85 {
86 }
87
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070088 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050089 add(const void* buf, int num, double entropy)
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070090 {
91 return 0;
92 }
Yingdi Yu0b537b92015-08-27 14:39:34 -070093
94 static int
Davide Pesavento74daf742018-11-23 18:14:13 -050095 pseudorand(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -070096 {
97 return 0;
98 }
99
100 static int
101 status()
102 {
103 return 0;
104 }
105
106private:
107 const RAND_METHOD* m_origRandMethod;
108 RAND_METHOD m_dummyRandMethod;
109};
110
111BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
112{
Davide Pesavento8db61522019-01-11 16:44:50 -0500113 std::array<uint8_t, 1024> buf;
Davide Pesavento765abc92021-12-27 00:44:04 -0500114 BOOST_CHECK_THROW(random::generateSecureBytes(buf), std::runtime_error);
Yingdi Yu0b537b92015-08-27 14:39:34 -0700115}
116
117BOOST_AUTO_TEST_SUITE_END() // TestRandom
118BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev75088672014-07-14 11:58:30 -0700119
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800120} // namespace tests
Alexander Afanasyev75088672014-07-14 11:58:30 -0700121} // namespace ndn