• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/salted_seed_seq.h"
16 
17 #include <iterator>
18 #include <random>
19 #include <utility>
20 #include <vector>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 
25 using absl::random_internal::GetSaltMaterial;
26 using absl::random_internal::MakeSaltedSeedSeq;
27 using absl::random_internal::SaltedSeedSeq;
28 using testing::Eq;
29 using testing::Pointwise;
30 
31 namespace {
32 
33 template <typename Sseq>
ConformsToInterface()34 void ConformsToInterface() {
35   // Check that the SeedSequence can be default-constructed.
36   { Sseq default_constructed_seq; }
37   // Check that the SeedSequence can be constructed with two iterators.
38   {
39     uint32_t init_array[] = {1, 3, 5, 7, 9};
40     Sseq iterator_constructed_seq(std::begin(init_array), std::end(init_array));
41   }
42   // Check that the SeedSequence can be std::initializer_list-constructed.
43   { Sseq list_constructed_seq = {1, 3, 5, 7, 9, 11, 13}; }
44   // Check that param() and size() return state provided to constructor.
45   {
46     uint32_t init_array[] = {1, 2, 3, 4, 5};
47     Sseq seq(std::begin(init_array), std::end(init_array));
48     EXPECT_EQ(seq.size(), ABSL_ARRAYSIZE(init_array));
49 
50     std::vector<uint32_t> state_vector;
51     seq.param(std::back_inserter(state_vector));
52 
53     EXPECT_EQ(state_vector.size(), ABSL_ARRAYSIZE(init_array));
54     for (int i = 0; i < state_vector.size(); i++) {
55       EXPECT_EQ(state_vector[i], i + 1);
56     }
57   }
58   // Check for presence of generate() method.
59   {
60     Sseq seq;
61     uint32_t seeds[5];
62 
63     seq.generate(std::begin(seeds), std::end(seeds));
64   }
65 }
66 
TEST(SaltedSeedSeq,CheckInterfaces)67 TEST(SaltedSeedSeq, CheckInterfaces) {
68   // Control case
69   ConformsToInterface<std::seed_seq>();
70 
71   // Abseil classes
72   ConformsToInterface<SaltedSeedSeq<std::seed_seq>>();
73 }
74 
TEST(SaltedSeedSeq,CheckConstructingFromOtherSequence)75 TEST(SaltedSeedSeq, CheckConstructingFromOtherSequence) {
76   std::vector<uint32_t> seed_values(10, 1);
77   std::seed_seq seq(seed_values.begin(), seed_values.end());
78   auto salted_seq = MakeSaltedSeedSeq(std::move(seq));
79 
80   EXPECT_EQ(seq.size(), salted_seq.size());
81 
82   std::vector<uint32_t> param_result;
83   seq.param(std::back_inserter(param_result));
84 
85   EXPECT_EQ(seed_values, param_result);
86 }
87 
TEST(SaltedSeedSeq,SaltedSaltedSeedSeqIsNotDoubleSalted)88 TEST(SaltedSeedSeq, SaltedSaltedSeedSeqIsNotDoubleSalted) {
89   uint32_t init[] = {1, 3, 5, 7, 9};
90 
91   std::seed_seq seq(std::begin(init), std::end(init));
92 
93   // The first salting.
94   SaltedSeedSeq<std::seed_seq> salted_seq = MakeSaltedSeedSeq(std::move(seq));
95   uint32_t a[16];
96   salted_seq.generate(std::begin(a), std::end(a));
97 
98   // The second salting.
99   SaltedSeedSeq<std::seed_seq> salted_salted_seq =
100       MakeSaltedSeedSeq(std::move(salted_seq));
101   uint32_t b[16];
102   salted_salted_seq.generate(std::begin(b), std::end(b));
103 
104   // ... both should be equal.
105   EXPECT_THAT(b, Pointwise(Eq(), a)) << "a[0] " << a[0];
106 }
107 
TEST(SaltedSeedSeq,SeedMaterialIsSalted)108 TEST(SaltedSeedSeq, SeedMaterialIsSalted) {
109   const size_t kNumBlocks = 16;
110 
111   uint32_t seed_material[kNumBlocks];
112   std::random_device urandom{"/dev/urandom"};
113   for (uint32_t& seed : seed_material) {
114     seed = urandom();
115   }
116 
117   std::seed_seq seq(std::begin(seed_material), std::end(seed_material));
118   SaltedSeedSeq<std::seed_seq> salted_seq(std::begin(seed_material),
119                                           std::end(seed_material));
120 
121   bool salt_is_available = GetSaltMaterial().has_value();
122 
123   // If salt is available generated sequence should be different.
124   if (salt_is_available) {
125     uint32_t outputs[kNumBlocks];
126     uint32_t salted_outputs[kNumBlocks];
127 
128     seq.generate(std::begin(outputs), std::end(outputs));
129     salted_seq.generate(std::begin(salted_outputs), std::end(salted_outputs));
130 
131     EXPECT_THAT(outputs, Pointwise(testing::Ne(), salted_outputs));
132   }
133 }
134 
TEST(SaltedSeedSeq,GenerateAcceptsDifferentTypes)135 TEST(SaltedSeedSeq, GenerateAcceptsDifferentTypes) {
136   const size_t kNumBlocks = 4;
137 
138   SaltedSeedSeq<std::seed_seq> seq({1, 2, 3});
139 
140   uint32_t expected[kNumBlocks];
141   seq.generate(std::begin(expected), std::end(expected));
142 
143   // 32-bit outputs
144   {
145     unsigned long seed_material[kNumBlocks];  // NOLINT(runtime/int)
146     seq.generate(std::begin(seed_material), std::end(seed_material));
147     EXPECT_THAT(seed_material, Pointwise(Eq(), expected));
148   }
149   {
150     unsigned int seed_material[kNumBlocks];  // NOLINT(runtime/int)
151     seq.generate(std::begin(seed_material), std::end(seed_material));
152     EXPECT_THAT(seed_material, Pointwise(Eq(), expected));
153   }
154 
155   // 64-bit outputs.
156   {
157     uint64_t seed_material[kNumBlocks];
158     seq.generate(std::begin(seed_material), std::end(seed_material));
159     EXPECT_THAT(seed_material, Pointwise(Eq(), expected));
160   }
161   {
162     int64_t seed_material[kNumBlocks];
163     seq.generate(std::begin(seed_material), std::end(seed_material));
164     EXPECT_THAT(seed_material, Pointwise(Eq(), expected));
165   }
166 }
167 
168 }  // namespace
169