QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "http2/tools/random_util.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 6 | |
| 7 | #include <cmath> |
| 8 | |
| 9 | namespace http2 { |
| 10 | namespace test { |
| 11 | |
| 12 | // Here "word" means something that starts with a lower-case letter, and has |
| 13 | // zero or more additional characters that are numbers or lower-case letters. |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 14 | std::string GenerateHttp2HeaderName(size_t len, Http2Random* rng) { |
vasilvv | 999985c | 2020-10-12 19:15:19 -0700 | [diff] [blame] | 15 | absl::string_view alpha_lc = "abcdefghijklmnopqrstuvwxyz"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 16 | // If the name is short, just make it one word. |
| 17 | if (len < 8) { |
| 18 | return rng->RandStringWithAlphabet(len, alpha_lc); |
| 19 | } |
| 20 | // If the name is longer, ensure it starts with a word, and after that may |
| 21 | // have any character in alphanumdash_lc. 4 is arbitrary, could be as low |
| 22 | // as 1. |
vasilvv | 999985c | 2020-10-12 19:15:19 -0700 | [diff] [blame] | 23 | absl::string_view alphanumdash_lc = "abcdefghijklmnopqrstuvwxyz0123456789-"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 24 | return rng->RandStringWithAlphabet(4, alpha_lc) + |
| 25 | rng->RandStringWithAlphabet(len - 4, alphanumdash_lc); |
| 26 | } |
| 27 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 28 | std::string GenerateWebSafeString(size_t len, Http2Random* rng) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 29 | static const char* kWebsafe64 = |
| 30 | "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; |
| 31 | return rng->RandStringWithAlphabet(len, kWebsafe64); |
| 32 | } |
| 33 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 34 | std::string GenerateWebSafeString(size_t lo, size_t hi, Http2Random* rng) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 35 | return GenerateWebSafeString(rng->UniformInRange(lo, hi), rng); |
| 36 | } |
| 37 | |
| 38 | } // namespace test |
| 39 | } // namespace http2 |