OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "net/http2/tools/random_util.h" |
| 6 |
| 7 #include <cmath> |
| 8 |
| 9 #include "net/http2/tools/http2_random.h" |
| 10 |
| 11 using std::string; |
| 12 using base::StringPiece; |
| 13 |
| 14 namespace net { |
| 15 namespace test { |
| 16 |
| 17 const char kWebsafe64[] = |
| 18 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; |
| 19 |
| 20 string RandomString(RandomBase* rng, int len, StringPiece alphabet) { |
| 21 string random_string; |
| 22 random_string.reserve(len); |
| 23 for (int i = 0; i < len; ++i) |
| 24 random_string.push_back(alphabet[rng->Uniform(alphabet.size())]); |
| 25 return random_string; |
| 26 } |
| 27 |
| 28 size_t GenerateUniformInRange(size_t lo, size_t hi, RandomBase* rng) { |
| 29 if (lo + 1 >= hi) { |
| 30 return lo; |
| 31 } |
| 32 return lo + rng->Rand64() % (hi - lo); |
| 33 } |
| 34 |
| 35 // Here "word" means something that starts with a lower-case letter, and has |
| 36 // zero or more additional characters that are numbers or lower-case letters. |
| 37 string GenerateHttp2HeaderName(size_t len, RandomBase* rng) { |
| 38 StringPiece alpha_lc = "abcdefghijklmnopqrstuvwxyz"; |
| 39 // If the name is short, just make it one word. |
| 40 if (len < 8) { |
| 41 return RandomString(rng, len, alpha_lc); |
| 42 } |
| 43 // If the name is longer, ensure it starts with a word, and after that may |
| 44 // have any character in alphanumdash_lc. 4 is arbitrary, could be as low |
| 45 // as 1. |
| 46 StringPiece alphanumdash_lc = "abcdefghijklmnopqrstuvwxyz0123456789-"; |
| 47 return RandomString(rng, 4, alpha_lc) + |
| 48 RandomString(rng, len - 4, alphanumdash_lc); |
| 49 } |
| 50 |
| 51 string GenerateWebSafeString(size_t len, RandomBase* rng) { |
| 52 return RandomString(rng, len, kWebsafe64); |
| 53 } |
| 54 |
| 55 string GenerateWebSafeString(size_t lo, size_t hi, RandomBase* rng) { |
| 56 return GenerateWebSafeString(GenerateUniformInRange(lo, hi, rng), rng); |
| 57 } |
| 58 |
| 59 } // namespace test |
| 60 } // namespace net |
OLD | NEW |