blob: e9d1633ec2c2a105596f037522aacd4728a69a03 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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 team5be974e2020-12-29 18:35:24 -05005#include "http2/tools/random_util.h"
QUICHE teamfd50a402018-12-07 22:54:05 -05006
7#include <cmath>
8
9namespace http2 {
10namespace 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.
bnc47904002019-08-16 11:49:48 -070014std::string GenerateHttp2HeaderName(size_t len, Http2Random* rng) {
vasilvv999985c2020-10-12 19:15:19 -070015 absl::string_view alpha_lc = "abcdefghijklmnopqrstuvwxyz";
QUICHE teamfd50a402018-12-07 22:54:05 -050016 // 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.
vasilvv999985c2020-10-12 19:15:19 -070023 absl::string_view alphanumdash_lc = "abcdefghijklmnopqrstuvwxyz0123456789-";
QUICHE teamfd50a402018-12-07 22:54:05 -050024 return rng->RandStringWithAlphabet(4, alpha_lc) +
25 rng->RandStringWithAlphabet(len - 4, alphanumdash_lc);
26}
27
bnc47904002019-08-16 11:49:48 -070028std::string GenerateWebSafeString(size_t len, Http2Random* rng) {
QUICHE teamfd50a402018-12-07 22:54:05 -050029 static const char* kWebsafe64 =
30 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
31 return rng->RandStringWithAlphabet(len, kWebsafe64);
32}
33
bnc47904002019-08-16 11:49:48 -070034std::string GenerateWebSafeString(size_t lo, size_t hi, Http2Random* rng) {
QUICHE teamfd50a402018-12-07 22:54:05 -050035 return GenerateWebSafeString(rng->UniformInRange(lo, hi), rng);
36}
37
38} // namespace test
39} // namespace http2