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(1000, 0);
144 std::vector<uint32_t> seeds_2(1000, 1);
145
146 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
147 seq_copy.generate(seeds_2.begin(), seeds_2.end());
148
149 EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
150 }
151 // Assignment operator.
152 {
153 for (uint32_t& entry : entropy) {
154 entry = urandom();
155 }
156 ExplicitSeedSeq another_seq(std::begin(entropy), std::end(entropy));
157
158 std::vector<uint32_t> seeds_1(1000, 0);
159 std::vector<uint32_t> seeds_2(1000, 0);
160
161 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
162 another_seq.generate(seeds_2.begin(), seeds_2.end());
163
164 // Assert precondition: Sequences generated by seed-sequences are not equal.
165 EXPECT_THAT(seeds_1, Not(Pointwise(Eq(), seeds_2)));
166
167 // Apply the assignment-operator.
168 // GCC 12 has a false-positive -Wstringop-overflow warning here.
169 #if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
170 #pragma GCC diagnostic push
171 #pragma GCC diagnostic ignored "-Wstringop-overflow"
172 #endif
173 another_seq = seq_from_entropy;
174 #if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
175 #pragma GCC diagnostic pop
176 #endif
177
178 // Re-generate seeds.
179 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
180 another_seq.generate(seeds_2.begin(), seeds_2.end());
181
182 // Seeds generated by seed-sequences should now be equal.
183 EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
184 }
185 // Move constructor.
186 {
187 // Get seeds from seed-sequence constructed from entropy.
188 std::vector<uint32_t> seeds_1(1000, 0);
189 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
190
191 // Apply move-constructor move the sequence to another instance.
192 absl::random_internal::ExplicitSeedSeq moved_seq(
193 std::move(seq_from_entropy));
194 std::vector<uint32_t> seeds_2(1000, 1);
195 moved_seq.generate(seeds_2.begin(), seeds_2.end());
196 // Verify that seeds produced by moved-instance are the same as original.
197 EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
198
199 // Verify that the moved-from instance now behaves like a
200 // default-constructed instance.
201 EXPECT_EQ(seq_from_entropy.size(), 0);
202 seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
203 EXPECT_THAT(seeds_1, Each(Eq(0)));
204 }
205 }
206
TEST(ExplicitSeedSeq,StdURBGGoldenTests)207 TEST(ExplicitSeedSeq, StdURBGGoldenTests) {
208 // Verify that for std::- URBG instances the results are stable across
209 // platforms (these should have deterministic output).
210 {
211 ExplicitSeedSeq seed_sequence{12, 34, 56};
212 std::minstd_rand rng(seed_sequence);
213
214 std::minstd_rand::result_type values[4] = {rng(), rng(), rng(), rng()};
215 EXPECT_THAT(values,
216 testing::ElementsAre(579252, 43785881, 464353103, 1501811174));
217 }
218
219 {
220 ExplicitSeedSeq seed_sequence{12, 34, 56};
221 std::mt19937 rng(seed_sequence);
222
223 std::mt19937::result_type values[4] = {rng(), rng(), rng(), rng()};
224 EXPECT_THAT(values, testing::ElementsAre(138416803, 151130212, 33817739,
225 138416803));
226 }
227
228 {
229 ExplicitSeedSeq seed_sequence{12, 34, 56};
230 std::mt19937_64 rng(seed_sequence);
231
232 std::mt19937_64::result_type values[4] = {rng(), rng(), rng(), rng()};
233 EXPECT_THAT(values,
234 testing::ElementsAre(19738651785169348, 1464811352364190456,
235 18054685302720800, 19738651785169348));
236 }
237 }
238