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 template <typename Sseq>
ConformsToInterface()28 bool ConformsToInterface() {
29   // Check that the SeedSequence can be default-constructed.
30   { Sseq default_constructed_seq; }
31   // Check that the SeedSequence can be constructed with two iterators.
32   {
33     uint32_t init_array[] = {1, 3, 5, 7, 9};
34     Sseq iterator_constructed_seq(init_array, &init_array[5]);
35   }
36   // Check that the SeedSequence can be std::initializer_list-constructed.
37   { Sseq list_constructed_seq = {1, 3, 5, 7, 9, 11, 13}; }
38   // Check that param() and size() return state provided to constructor.
39   {
40     uint32_t init_array[] = {1, 2, 3, 4, 5};
41     Sseq seq(init_array, &init_array[ABSL_ARRAYSIZE(init_array)]);
42     EXPECT_EQ(seq.size(), ABSL_ARRAYSIZE(init_array));
43 
44     uint32_t state_array[ABSL_ARRAYSIZE(init_array)];
45     seq.param(state_array);
46 
47     for (int i = 0; i < ABSL_ARRAYSIZE(state_array); i++) {
48       EXPECT_EQ(state_array[i], i + 1);
49     }
50   }
51   // Check for presence of generate() method.
52   {
53     Sseq seq;
54     uint32_t seeds[5];
55 
56     seq.generate(seeds, &seeds[ABSL_ARRAYSIZE(seeds)]);
57   }
58   return true;
59 }
60 }  // namespace
61 
TEST(SeedSequences,CheckInterfaces)62 TEST(SeedSequences, CheckInterfaces) {
63   // Control case
64   EXPECT_TRUE(ConformsToInterface<std::seed_seq>());
65 
66   // Abseil classes
67   EXPECT_TRUE(ConformsToInterface<absl::random_internal::ExplicitSeedSeq>());
68 }
69 
TEST(ExplicitSeedSeq,DefaultConstructorGeneratesZeros)70 TEST(ExplicitSeedSeq, DefaultConstructorGeneratesZeros) {
71   const size_t kNumBlocks = 128;
72 
73   uint32_t outputs[kNumBlocks];
74   absl::random_internal::ExplicitSeedSeq seq;
75   seq.generate(outputs, &outputs[kNumBlocks]);
76 
77   for (uint32_t& seed : outputs) {
78     EXPECT_EQ(seed, 0);
79   }
80 }
81 
TEST(ExplicitSeeqSeq,SeedMaterialIsForwardedIdentically)82 TEST(ExplicitSeeqSeq, SeedMaterialIsForwardedIdentically) {
83   const size_t kNumBlocks = 128;
84 
85   uint32_t seed_material[kNumBlocks];
86   std::random_device urandom{"/dev/urandom"};
87   for (uint32_t& seed : seed_material) {
88     seed = urandom();
89   }
90   absl::random_internal::ExplicitSeedSeq seq(seed_material,
91                                              &seed_material[kNumBlocks]);
92 
93   // Check that output is same as seed-material provided to constructor.
94   {
95     const size_t kNumGenerated = kNumBlocks / 2;
96     uint32_t outputs[kNumGenerated];
97     seq.generate(outputs, &outputs[kNumGenerated]);
98     for (size_t i = 0; i < kNumGenerated; i++) {
99       EXPECT_EQ(outputs[i], seed_material[i]);
100     }
101   }
102   // Check that SeedSequence is stateless between invocations: Despite the last
103   // invocation of generate() only consuming half of the input-entropy, the same
104   // entropy will be recycled for the next invocation.
105   {
106     const size_t kNumGenerated = kNumBlocks;
107     uint32_t outputs[kNumGenerated];
108     seq.generate(outputs, &outputs[kNumGenerated]);
109     for (size_t i = 0; i < kNumGenerated; i++) {
110       EXPECT_EQ(outputs[i], seed_material[i]);
111     }
112   }
113   // Check that when more seed-material is asked for than is provided, nonzero
114   // values are still written.
115   {
116     const size_t kNumGenerated = kNumBlocks * 2;
117     uint32_t outputs[kNumGenerated];
118     seq.generate(outputs, &outputs[kNumGenerated]);
119     for (size_t i = 0; i < kNumGenerated; i++) {
120       EXPECT_EQ(outputs[i], seed_material[i % kNumBlocks]);
121     }
122   }
123 }
124 
TEST(ExplicitSeedSeq,CopyAndMoveConstructors)125 TEST(ExplicitSeedSeq, CopyAndMoveConstructors) {
126   using testing::Each;
127   using testing::Eq;
128   using testing::Not;
129   using testing::Pointwise;
130 
131   uint32_t entropy[4];
132   std::random_device urandom("/dev/urandom");
133   for (uint32_t& entry : entropy) {
134     entry = urandom();
135   }
136   absl::random_internal::ExplicitSeedSeq seq_from_entropy(std::begin(entropy),
137                                                           std::end(entropy));
138   // Copy constructor.
139   {
140     absl::random_internal::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     absl::random_internal::ExplicitSeedSeq another_seq(std::begin(entropy),
159                                                        std::end(entropy));
160 
161     std::vector<uint32_t> seeds_1;
162     seeds_1.resize(1000, 0);
163     std::vector<uint32_t> seeds_2;
164     seeds_2.resize(1000, 0);
165 
166     seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
167     another_seq.generate(seeds_2.begin(), seeds_2.end());
168 
169     // Assert precondition: Sequences generated by seed-sequences are not equal.
170     EXPECT_THAT(seeds_1, Not(Pointwise(Eq(), seeds_2)));
171 
172     // Apply the assignment-operator.
173     another_seq = seq_from_entropy;
174 
175     // Re-generate seeds.
176     seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
177     another_seq.generate(seeds_2.begin(), seeds_2.end());
178 
179     // Seeds generated by seed-sequences should now be equal.
180     EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
181   }
182   // Move constructor.
183   {
184     // Get seeds from seed-sequence constructed from entropy.
185     std::vector<uint32_t> seeds_1;
186     seeds_1.resize(1000, 0);
187     seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
188 
189     // Apply move-constructor move the sequence to another instance.
190     absl::random_internal::ExplicitSeedSeq moved_seq(
191         std::move(seq_from_entropy));
192     std::vector<uint32_t> seeds_2;
193     seeds_2.resize(1000, 1);
194     moved_seq.generate(seeds_2.begin(), seeds_2.end());
195     // Verify that seeds produced by moved-instance are the same as original.
196     EXPECT_THAT(seeds_1, Pointwise(Eq(), seeds_2));
197 
198     // Verify that the moved-from instance now behaves like a
199     // default-constructed instance.
200     EXPECT_EQ(seq_from_entropy.size(), 0);
201     seq_from_entropy.generate(seeds_1.begin(), seeds_1.end());
202     EXPECT_THAT(seeds_1, Each(Eq(0)));
203   }
204 }
205