QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_HTTP2_TEST_TOOLS_HTTP2_RANDOM_H_ |
| 6 | #define QUICHE_HTTP2_TEST_TOOLS_HTTP2_RANDOM_H_ |
| 7 | |
| 8 | #include <cmath> |
| 9 | #include <cstdint> |
| 10 | #include <limits> |
| 11 | #include <random> |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 12 | #include <string> |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 13 | |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 15 | |
| 16 | namespace http2 { |
| 17 | namespace test { |
| 18 | |
| 19 | // The random number generator used for unit tests. Since the algorithm is |
| 20 | // deterministic and fixed, this can be used to reproduce flakes in the unit |
| 21 | // tests caused by specific random values. |
| 22 | class Http2Random { |
| 23 | public: |
| 24 | Http2Random(); |
| 25 | |
| 26 | Http2Random(const Http2Random&) = delete; |
| 27 | Http2Random& operator=(const Http2Random&) = delete; |
| 28 | |
| 29 | // Reproducible random number generation: by using the same key, the same |
| 30 | // sequence of results is obtained. |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 31 | explicit Http2Random(quiche::QuicheStringPiece key); |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 32 | std::string Key() const; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 33 | |
| 34 | void FillRandom(void* buffer, size_t buffer_size); |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 35 | std::string RandString(int length); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 36 | |
| 37 | // Returns a random 64-bit value. |
| 38 | uint64_t Rand64(); |
| 39 | |
| 40 | // Return a uniformly distrubted random number in [0, n). |
| 41 | uint32_t Uniform(uint32_t n) { return Rand64() % n; } |
| 42 | // Return a uniformly distrubted random number in [lo, hi). |
| 43 | uint64_t UniformInRange(uint64_t lo, uint64_t hi) { |
| 44 | return lo + Rand64() % (hi - lo); |
| 45 | } |
| 46 | // Return an integer of logarithmically random scale. |
| 47 | uint32_t Skewed(uint32_t max_log) { |
| 48 | const uint32_t base = Rand32() % (max_log + 1); |
| 49 | const uint32_t mask = ((base < 32) ? (1u << base) : 0u) - 1u; |
| 50 | return Rand32() & mask; |
| 51 | } |
| 52 | // Return a random number in [0, max] range that skews low. |
| 53 | uint64_t RandomSizeSkewedLow(uint64_t max) { |
| 54 | return std::round(max * std::pow(RandDouble(), 2)); |
| 55 | } |
| 56 | |
| 57 | // Returns a random double between 0 and 1. |
| 58 | double RandDouble(); |
| 59 | float RandFloat() { return RandDouble(); } |
| 60 | |
| 61 | // Has 1/n chance of returning true. |
| 62 | bool OneIn(int n) { return Uniform(n) == 0; } |
| 63 | |
| 64 | uint8_t Rand8() { return Rand64(); } |
| 65 | uint16_t Rand16() { return Rand64(); } |
| 66 | uint32_t Rand32() { return Rand64(); } |
| 67 | |
| 68 | // Return a random string consisting of the characters from the specified |
| 69 | // alphabet. |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame] | 70 | std::string RandStringWithAlphabet(int length, |
| 71 | quiche::QuicheStringPiece alphabet); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 72 | |
| 73 | // STL UniformRandomNumberGenerator implementation. |
| 74 | using result_type = uint64_t; |
| 75 | static constexpr result_type min() { return 0; } |
| 76 | static constexpr result_type max() { |
| 77 | return std::numeric_limits<result_type>::max(); |
| 78 | } |
| 79 | result_type operator()() { return Rand64(); } |
| 80 | |
| 81 | private: |
| 82 | uint8_t key_[32]; |
| 83 | uint32_t counter_ = 0; |
| 84 | }; |
| 85 | |
| 86 | } // namespace test |
| 87 | } // namespace http2 |
| 88 | |
| 89 | #endif // QUICHE_HTTP2_TEST_TOOLS_HTTP2_RANDOM_H_ |