blob: bfa486a532c0826ef8d7ecf7b2042fc97b5178c1 [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 Pesavento765abc92021-12-27 00:44:04 -05003 * Copyright (c) 2013-2021 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#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -070072 static void
73 seed(const void* buf, int num)
74 {
75 }
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070076#else
77 static int
78 seed(const void* buf, int num)
79 {
80 return 0;
81 }
82#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
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#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -070096 static void
Davide Pesavento74daf742018-11-23 18:14:13 -050097 add(const void* buf, int num, double entropy)
Yingdi Yu0b537b92015-08-27 14:39:34 -070098 {
99 }
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700100#else
101 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500102 add(const void* buf, int num, double entropy)
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700103 {
104 return 0;
105 }
106#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yu0b537b92015-08-27 14:39:34 -0700107
108 static int
Davide Pesavento74daf742018-11-23 18:14:13 -0500109 pseudorand(unsigned char* buf, int num)
Yingdi Yu0b537b92015-08-27 14:39:34 -0700110 {
111 return 0;
112 }
113
114 static int
115 status()
116 {
117 return 0;
118 }
119
120private:
121 const RAND_METHOD* m_origRandMethod;
122 RAND_METHOD m_dummyRandMethod;
123};
124
125BOOST_FIXTURE_TEST_CASE(Error, FailRandMethodFixture)
126{
Davide Pesavento8db61522019-01-11 16:44:50 -0500127 std::array<uint8_t, 1024> buf;
Davide Pesavento765abc92021-12-27 00:44:04 -0500128 BOOST_CHECK_THROW(random::generateSecureBytes(buf), std::runtime_error);
Yingdi Yu0b537b92015-08-27 14:39:34 -0700129}
130
131BOOST_AUTO_TEST_SUITE_END() // TestRandom
132BOOST_AUTO_TEST_SUITE_END() // Util
Alexander Afanasyev75088672014-07-14 11:58:30 -0700133
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800134} // namespace tests
Alexander Afanasyev75088672014-07-14 11:58:30 -0700135} // namespace ndn