xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/http2/test_tools/random_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "quiche/http2/test_tools/random_util.h"
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.
GenerateHttp2HeaderName(size_t len,Http2Random * rng)14 std::string GenerateHttp2HeaderName(size_t len, Http2Random* rng) {
15   absl::string_view alpha_lc = "abcdefghijklmnopqrstuvwxyz";
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.
23   absl::string_view alphanumdash_lc = "abcdefghijklmnopqrstuvwxyz0123456789-";
24   return rng->RandStringWithAlphabet(4, alpha_lc) +
25          rng->RandStringWithAlphabet(len - 4, alphanumdash_lc);
26 }
27 
GenerateWebSafeString(size_t len,Http2Random * rng)28 std::string GenerateWebSafeString(size_t len, Http2Random* rng) {
29   static const char* kWebsafe64 =
30       "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
31   return rng->RandStringWithAlphabet(len, kWebsafe64);
32 }
33 
GenerateWebSafeString(size_t lo,size_t hi,Http2Random * rng)34 std::string GenerateWebSafeString(size_t lo, size_t hi, Http2Random* rng) {
35   return GenerateWebSafeString(rng->UniformInRange(lo, hi), rng);
36 }
37 
38 }  // namespace test
39 }  // namespace http2
40