nonsecure_base_test.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "absl/random/internal/nonsecure_base.h"
  15. #include <algorithm>
  16. #include <iostream>
  17. #include <memory>
  18. #include <random>
  19. #include <sstream>
  20. #include "gtest/gtest.h"
  21. #include "absl/random/distributions.h"
  22. #include "absl/random/random.h"
  23. #include "absl/strings/str_cat.h"
  24. namespace {
  25. using ExampleNonsecureURBG =
  26. absl::random_internal::NonsecureURBGBase<std::mt19937>;
  27. template <typename T>
  28. void Use(const T&) {}
  29. } // namespace
  30. TEST(NonsecureURBGBase, DefaultConstructorIsValid) {
  31. ExampleNonsecureURBG urbg;
  32. }
  33. // Ensure that the recommended template-instantiations are valid.
  34. TEST(RecommendedTemplates, CanBeConstructed) {
  35. absl::BitGen default_generator;
  36. absl::InsecureBitGen insecure_generator;
  37. }
  38. TEST(RecommendedTemplates, CanDiscardValues) {
  39. absl::BitGen default_generator;
  40. absl::InsecureBitGen insecure_generator;
  41. default_generator.discard(5);
  42. insecure_generator.discard(5);
  43. }
  44. TEST(NonsecureURBGBase, StandardInterface) {
  45. // Names after definition of [rand.req.urbg] in C++ standard.
  46. // e us a value of E
  47. // v is a lvalue of E
  48. // x, y are possibly const values of E
  49. // s is a value of T
  50. // q is a value satisfying requirements of seed_sequence
  51. // z is a value of type unsigned long long
  52. // os is a some specialization of basic_ostream
  53. // is is a some specialization of basic_istream
  54. using E = absl::random_internal::NonsecureURBGBase<std::minstd_rand>;
  55. using T = typename E::result_type;
  56. static_assert(!std::is_copy_constructible<E>::value,
  57. "NonsecureURBGBase should not be copy constructible");
  58. static_assert(!absl::is_copy_assignable<E>::value,
  59. "NonsecureURBGBase should not be copy assignable");
  60. static_assert(std::is_move_constructible<E>::value,
  61. "NonsecureURBGBase should be move constructible");
  62. static_assert(absl::is_move_assignable<E>::value,
  63. "NonsecureURBGBase should be move assignable");
  64. static_assert(std::is_same<decltype(std::declval<E>()()), T>::value,
  65. "return type of operator() must be result_type");
  66. {
  67. const E x, y;
  68. Use(x);
  69. Use(y);
  70. static_assert(std::is_same<decltype(x == y), bool>::value,
  71. "return type of operator== must be bool");
  72. static_assert(std::is_same<decltype(x != y), bool>::value,
  73. "return type of operator== must be bool");
  74. }
  75. E e;
  76. std::seed_seq q{1, 2, 3};
  77. E{};
  78. E{q};
  79. // Copy constructor not supported.
  80. // E{x};
  81. // result_type seed constructor not supported.
  82. // E{T{1}};
  83. // Move constructors are supported.
  84. {
  85. E tmp(q);
  86. E m = std::move(tmp);
  87. E n(std::move(m));
  88. EXPECT_TRUE(e != n);
  89. }
  90. // Comparisons work.
  91. {
  92. // MSVC emits error 2718 when using EXPECT_EQ(e, x)
  93. // * actual parameter with __declspec(align('#')) won't be aligned
  94. E a(q);
  95. E b(q);
  96. EXPECT_TRUE(a != e);
  97. EXPECT_TRUE(a == b);
  98. a();
  99. EXPECT_TRUE(a != b);
  100. }
  101. // e.seed(s) not supported.
  102. // [rand.req.eng] specifies the parameter as 'unsigned long long'
  103. // e.discard(unsigned long long) is supported.
  104. unsigned long long z = 1; // NOLINT(runtime/int)
  105. e.discard(z);
  106. }
  107. TEST(NonsecureURBGBase, SeedSeqConstructorIsValid) {
  108. std::seed_seq seq;
  109. ExampleNonsecureURBG rbg(seq);
  110. }
  111. TEST(NonsecureURBGBase, CompatibleWithDistributionUtils) {
  112. ExampleNonsecureURBG rbg;
  113. absl::Uniform(rbg, 0, 100);
  114. absl::Uniform(rbg, 0.5, 0.7);
  115. absl::Poisson<uint32_t>(rbg);
  116. absl::Exponential<float>(rbg);
  117. }
  118. TEST(NonsecureURBGBase, CompatibleWithStdDistributions) {
  119. ExampleNonsecureURBG rbg;
  120. // Cast to void to suppress [[nodiscard]] warnings
  121. static_cast<void>(std::uniform_int_distribution<uint32_t>(0, 100)(rbg));
  122. static_cast<void>(std::uniform_real_distribution<float>()(rbg));
  123. static_cast<void>(std::bernoulli_distribution(0.2)(rbg));
  124. }
  125. TEST(NonsecureURBGBase, ConsecutiveDefaultInstancesYieldUniqueVariates) {
  126. const size_t kNumSamples = 128;
  127. ExampleNonsecureURBG rbg1;
  128. ExampleNonsecureURBG rbg2;
  129. for (size_t i = 0; i < kNumSamples; i++) {
  130. EXPECT_NE(rbg1(), rbg2());
  131. }
  132. }
  133. TEST(NonsecureURBGBase, EqualSeedSequencesYieldEqualVariates) {
  134. std::seed_seq seq;
  135. ExampleNonsecureURBG rbg1(seq);
  136. ExampleNonsecureURBG rbg2(seq);
  137. // ExampleNonsecureURBG rbg3({1, 2, 3}); // Should not compile.
  138. for (uint32_t i = 0; i < 1000; i++) {
  139. EXPECT_EQ(rbg1(), rbg2());
  140. }
  141. rbg1.discard(100);
  142. rbg2.discard(100);
  143. // The sequences should continue after discarding
  144. for (uint32_t i = 0; i < 1000; i++) {
  145. EXPECT_EQ(rbg1(), rbg2());
  146. }
  147. }
  148. // This is a PRNG-compatible type specifically designed to test
  149. // that NonsecureURBGBase::Seeder can correctly handle iterators
  150. // to arbitrary non-uint32_t size types.
  151. template <typename T>
  152. struct SeederTestEngine {
  153. using result_type = T;
  154. static constexpr result_type(min)() {
  155. return (std::numeric_limits<result_type>::min)();
  156. }
  157. static constexpr result_type(max)() {
  158. return (std::numeric_limits<result_type>::max)();
  159. }
  160. template <class SeedSequence,
  161. typename = typename absl::enable_if_t<
  162. !std::is_same<SeedSequence, SeederTestEngine>::value>>
  163. explicit SeederTestEngine(SeedSequence&& seq) {
  164. seed(seq);
  165. }
  166. SeederTestEngine(const SeederTestEngine&) = default;
  167. SeederTestEngine& operator=(const SeederTestEngine&) = default;
  168. SeederTestEngine(SeederTestEngine&&) = default;
  169. SeederTestEngine& operator=(SeederTestEngine&&) = default;
  170. result_type operator()() { return state[0]; }
  171. template <class SeedSequence>
  172. void seed(SeedSequence&& seq) {
  173. std::fill(std::begin(state), std::end(state), T(0));
  174. seq.generate(std::begin(state), std::end(state));
  175. }
  176. T state[2];
  177. };
  178. TEST(NonsecureURBGBase, SeederWorksForU32) {
  179. using U32 =
  180. absl::random_internal::NonsecureURBGBase<SeederTestEngine<uint32_t>>;
  181. U32 x;
  182. EXPECT_NE(0, x());
  183. }
  184. TEST(NonsecureURBGBase, SeederWorksForU64) {
  185. using U64 =
  186. absl::random_internal::NonsecureURBGBase<SeederTestEngine<uint64_t>>;
  187. U64 x;
  188. EXPECT_NE(0, x());
  189. }