1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/random/internal/explicit_seed_seq.h"
16
17 #include <iterator>
18 #include <random>
19 #include <utility>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/random/seed_sequences.h"
24
25 namespace {
26
27 using ::absl::random_internal::ExplicitSeedSeq;
28
29 template <typename Sseq>
ConformsToInterface()30 bool ConformsToInterface() {
31 // Check that the SeedSequence can be default-constructed.
32 { Sseq default_constructed_seq; }
33 // Check that the SeedSequence can be constructed with two iterators.
34 {
35 uint32_t init_array[] = {1, 3, 5, 7, 9};
36 Sseq iterator_constructed_seq(init_array, &init_array[5]);
37 }
38 // Check that the SeedSequence can be std::initializer_list-constructed.
39 { Sseq list_constructed_seq = {1, 3, 5, 7, 9, 11, 13}; }
40 // Check that param() and size() return state provided to constructor.
41 {
42 uint32_t init_array[] = {1, 2, 3, 4, 5};
43 Sseq seq(init_array, &init_array[ABSL_ARRAYSIZE(init_array)]);
44 EXPECT_EQ(seq.size(), ABSL_ARRAYSIZE(init_array));
45
46 uint32_t state_array[ABSL_ARRAYSIZE(init_array)];
47 seq.param(state_array);
48
49 for (int i = 0; i < ABSL_ARRAYSIZE(state_array); i++) {
50 EXPECT_EQ(state_array[i], i + 1);
51 }
52 }
53 // Check for presence of generate() method.
54 {
55 Sseq seq;
56 uint32_t seeds[5];
57
58 seq.generate(seeds, &seeds[ABSL_ARRAYSIZE(seeds)]);
59 }
60 return true;
61 }
62 } // namespace
63
TEST(SeedSequences,CheckInterfaces)64 TEST(SeedSequences, CheckInterfaces) {
65 // Control case
66 EXPECT_TRUE(ConformsToInterface<std::seed_seq>());
67
68 // Abseil classes
69 EXPECT_TRUE(ConformsToInterface<ExplicitSeedSeq>());
70 }
71
TEST(ExplicitSeedSeq,DefaultConstructorGeneratesZeros)72 TEST(ExplicitSeedSeq, DefaultConstructorGeneratesZeros) {
73 const size_t kNumBlocks = 128;
74
75 uint32_t outputs[kNumBlocks];
76 ExplicitSeedSeq seq;
77 seq.generate(outputs, &outputs[kNumBlocks]);
78
79 for (uint32_t& seed : outputs) {
80 EXPECT_EQ(seed, 0);
81 }
82 }
83
TEST(ExplicitSeeqSeq,SeedMaterialIsForwardedIdentically)84 TEST(ExplicitSeeqSeq, SeedMaterialIsForwardedIdentically) {
85 const size_t kNumBlocks = 128;
86
87 uint32_t seed_material[kNumBlocks];
88 std::random_device urandom{"/dev/urandom"};
89 for (uint32_t& seed : seed_material) {
90 seed = urandom();
91 }
92 ExplicitSeedSeq seq(seed_material, &seed_material[kNumBlocks]);
93
94 // Check that output is same as seed-material provided to constructor.
95 {
96 const size_t kNumGenerated = kNumBlocks / 2;
97 uint32_t outputs[kNumGenerated];
98 seq.generate(outputs, &outputs[kNumGenerated]);
99 for (size_t i = 0; i < kNumGenerated; i++) {
100 EXPECT_EQ(outputs[i], seed_material[i]);
101 }
102 }
103 // Check that SeedSequence is stateless between invocations: Despite the last
104 // invocation of generate() only consuming half of the input-entropy, the same
105 // entropy will be recycled for the next invocation.
106 {
107 const size_t kNumGenerated = kNumBlocks;
108 uint32_t outputs[kNumGenerated];
109 seq.generate(outputs, &outputs[kNumGenerated]);
110 for (size_t i = 0; i < kNumGenerated; i++) {
111 EXPECT_EQ(outputs[i], seed_material[i]);
112 }
113 }
114 // Check that when more seed-material is asked for than is provided, nonzero
115 // values are still written.
116 {
117 const size_t kNumGenerated = kNumBlocks * 2;
118 uint32_t outputs[kNumGenerated];
119 seq.generate(outputs, &outputs[kNumGenerated]);
120 for (size_t i = 0; i < kNumGenerated; i++) {
121 EXPECT_EQ(outputs[i], seed_material[i % kNumBlocks]);
122 }
123 }
124 }
125
TEST(ExplicitSeedSeq,CopyAndMoveConstructors)126 TEST(ExplicitSeedSeq, CopyAndMoveConstructors) {
127 using testing::Each;
128 using testing::Eq;
129 using testing::Not;
130 using testing::Pointwise;
131
132 uint32_t entropy[4];
133 std::random_device urandom("/dev/urandom");
134 for (uint32_t& entry : entropy) {
135 entry = urandom();
136 }
137 ExplicitSeedSeq seq_from_entropy(std::begin(entropy), std::end(entropy));
138 // Copy constructor.
139 {
140 ExplicitSeedSeq seq_copy(seq_from_entropy);
141 EXPECT_EQ(seq_copy.size(), seq_from_entropy.size());
142
143 std::vector<uint32_t> seeds_1;
144 seeds_1.resize(1000, 0);
145 std::vector<uint32_t> seeds_2;
146 seeds_2.resize(1000, 1);
147
148 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
149 seq_copy.generate(seeds_2.begin(), seeds_2.end());
150
151 EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
152 }
153 // Assignment operator.
154 {
155 for (uint32_t& entry : entropy) {
156 entry = urandom();
157 }
158 ExplicitSeedSeq another_seq(std::begin(entropy), std::end(entropy));
159
160 std::vector<uint32_t> seeds_1;
161 seeds_1.resize(1000, 0);
162 std::vector<uint32_t> seeds_2;
163 seeds_2.resize(1000, 0);
164
165 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
166 another_seq.generate(seeds_2.begin(), seeds_2.end());
167
168 // Assert precondition: Sequences generated by seed-sequences are not equal.
169 EXPECT_THAT(seeds_1, Not(Pointwise(Eq(), seeds_2)));
170
171 // Apply the assignment-operator.
172 another_seq = seq_from_entropy;
173
174 // Re-generate seeds.
175 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
176 another_seq.generate(seeds_2.begin(), seeds_2.end());
177
178 // Seeds generated by seed-sequences should now be equal.
179 EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
180 }
181 // Move constructor.
182 {
183 // Get seeds from seed-sequence constructed from entropy.
184 std::vector<uint32_t> seeds_1;
185 seeds_1.resize(1000, 0);
186 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
187
188 // Apply move-constructor move the sequence to another instance.
189 absl::random_internal::ExplicitSeedSeq moved_seq(
190 std::move(seq_from_entropy));
191 std::vector<uint32_t> seeds_2;
192 seeds_2.resize(1000, 1);
193 moved_seq.generate(seeds_2.begin(), seeds_2.end());
194 // Verify that seeds produced by moved-instance are the same as original.
195 EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
196
197 // Verify that the moved-from instance now behaves like a
198 // default-constructed instance.
199 EXPECT_EQ(seq_from_entropy.size(), 0);
200 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
201 EXPECT_THAT(seeds_1, Each(Eq(0)));
202 }
203 }
204
TEST(ExplicitSeedSeq,StdURBGGoldenTests)205 TEST(ExplicitSeedSeq, StdURBGGoldenTests) {
206 // Verify that for std::- URBG instances the results are stable across
207 // platforms (these should have deterministic output).
208 {
209 ExplicitSeedSeq seed_sequence{12, 34, 56};
210 std::minstd_rand rng(seed_sequence);
211
212 std::minstd_rand::result_type values[4] = {rng(), rng(), rng(), rng()};
213 EXPECT_THAT(values,
214 testing::ElementsAre(579252, 43785881, 464353103, 1501811174));
215 }
216
217 {
218 ExplicitSeedSeq seed_sequence{12, 34, 56};
219 std::mt19937 rng(seed_sequence);
220
221 std::mt19937::result_type values[4] = {rng(), rng(), rng(), rng()};
222 EXPECT_THAT(values, testing::ElementsAre(138416803, 151130212, 33817739,
223 138416803));
224 }
225
226 {
227 ExplicitSeedSeq seed_sequence{12, 34, 56};
228 std::mt19937_64 rng(seed_sequence);
229
230 std::mt19937_64::result_type values[4] = {rng(), rng(), rng(), rng()};
231 EXPECT_THAT(values,
232 testing::ElementsAre(19738651785169348, 1464811352364190456,
233 18054685302720800, 19738651785169348));
234 }
235 }
236