scclib
Stable Cloud Computing C++ Library
random.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <crypto/random.h>
32 #include <gtest/gtest.h>
33 #include <iostream>
34 #include <string>
35 #include <random>
36 #include <map>
37 #include <vector>
38 
45 using std::cout;
46 using std::endl;
47 using std::map;
48 using std::string;
49 using std::uniform_int_distribution;
50 using std::mt19937;
52 
53 
54 TEST(random_tests, uints)
55 {
56  uint32_t r32 = 0;
57  for (int i = 1; i < 5; i++)
58  {
59  uint32_t last = r32;
60  r32 = RandomEngine::rand_uint32();
61  cout << "Rand uint32_t: " << r32 << endl;
62  EXPECT_NE(last, r32);
63  }
64  uint64_t r64 = 0;
65  for (int i = 1; i < 5; i++)
66  {
67  uint64_t last = r64;
68  RandomEngine::rand_bytes(&r64, sizeof(r64));
69  cout << "Rand uint64_t: " << r64 << endl;
70  EXPECT_NE(last, r64);
71  }
72  RandomEngine rd;
73  r32 = 0;
74  for (int i = 1; i < 5; i++)
75  {
76  uint32_t last = r32;
77  r32 = rd();
78  cout << "Rand operator(): " << r32 << endl;
79  EXPECT_NE(last, r32);
80  }
81 }
82 
83 TEST(random_tests, seed)
84 {
85  string seed1("this is a random seed for the generator");
86  seed1.resize(32);
87  string seed2("this is a different random seed for the generator");
88  seed2.resize(32);
89 
90  RandomEngine::seed(seed1);
91  auto r1 = RandomEngine::rand_uint64();
92  cout << "rand with seed1: " << r1 << endl;
93 
94  RandomEngine::random_seed();
95  auto r2 = RandomEngine::rand_uint64();
96  cout << "rand with random_seed: " << r2 << endl;
97  EXPECT_NE(r1, r2);
98 
99  RandomEngine::seed(seed2);
100  auto r3 = RandomEngine::rand_uint64();
101  cout << "rand with seed2: " << r3 << endl;
102  EXPECT_NE(r1, r3);
103  EXPECT_NE(r2, r3);
104 
105 
106  RandomEngine::seed(seed1);
107  auto x1 = RandomEngine::rand_uint64();
108  cout << "rand with seed1: " << x1 << endl;
109 
110  RandomEngine::seed(seed2);
111  auto x2 = RandomEngine::rand_uint64();
112  cout << "rand with seed2: " << x2 << endl;
113 
114  /*
115  Note: on openssl, RAND_seed is equivalent to RAND_add, so reseeding does not guarantee the same random output.
116  On IPP, the random seed value can be reseeded to produce the same random output.
117  */
118  //EXPECT_EQ(x1, r1);
119 }
120 
121 TEST(random_tests, mersenne_twister_engine)
122 {
123  RandomEngine rd;
124  mt19937 gen(rd()); // mersenne_twister_engine
125  uniform_int_distribution<int> dis(0, 9);
126  map<int, int> hist;
127  for (int n = 0; n < 100000; ++n)
128  {
129  ++hist[dis(gen)];
130  }
131  cout << "mersenne twister 100000 values 0 to 9 distribution:" << endl;
132  for (auto p : hist)
133  {
134  cout << p.first << " : " << p.second << endl;;
135  }
136  ASSERT_EQ(hist.size(), 10);
137  for (int i = 0; i < 10; i++)
138  {
139  ASSERT_NE(hist.find(i), hist.end());
140  ASSERT_GT(hist[i], 0);
141  }
142 }
143 
144 TEST(random_tests, bytes_sanity)
145 {
146  uint8_t by[5];
147  RandomEngine::rand_bytes(by, 5);
148  cout << "tbytes " << 5 << ": ";
149  cout << std::hex << (uint16_t)by[0] << std::dec << " ";
150  auto b = by[0];
151  int same = 1;
152  for (int i = 1; i < 5; i++)
153  {
154  if (by[i] == b) same++;
155  cout << std::hex << (uint16_t)by[i] << std::dec << " ";
156  }
157  cout << " same as byte[0]=" << same;
158  cout << endl;
159  EXPECT_NE(same, 5);
160 }
The random number generator is initialized with random entropy on first use.
Definition: random.h:58
Random number generator.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521