xref: /aosp_15_r20/external/cronet/net/spdy/fuzzing/hpack_example_generator.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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 "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/files/file.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/spdy/fuzzing/hpack_fuzz_util.h"
11 #include "net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_constants.h"
12 #include "net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_encoder.h"
13 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
14 
15 namespace {
16 
17 // Target file for generated HPACK header sets.
18 const char kFileToWrite[] = "file-to-write";
19 
20 // Number of header sets to generate.
21 const char kExampleCount[] = "example-count";
22 
23 }  // namespace
24 
25 using spdy::HpackFuzzUtil;
26 using std::map;
27 
28 // Generates a configurable number of header sets (using HpackFuzzUtil), and
29 // sequentially encodes each header set with an HpackEncoder. Encoded header
30 // sets are written to the output file in length-prefixed blocks.
main(int argc,char ** argv)31 int main(int argc, char** argv) {
32   base::AtExitManager exit_manager;
33 
34   base::CommandLine::Init(argc, argv);
35   const base::CommandLine& command_line =
36       *base::CommandLine::ForCurrentProcess();
37 
38   if (!command_line.HasSwitch(kFileToWrite) ||
39       !command_line.HasSwitch(kExampleCount)) {
40     LOG(ERROR) << "Usage: " << argv[0] << " --" << kFileToWrite
41                << "=/path/to/file.out"
42                << " --" << kExampleCount << "=1000";
43     return -1;
44   }
45   std::string file_to_write = command_line.GetSwitchValueASCII(kFileToWrite);
46 
47   int example_count = 0;
48   base::StringToInt(command_line.GetSwitchValueASCII(kExampleCount),
49                     &example_count);
50 
51   DVLOG(1) << "Writing output to " << file_to_write;
52   base::File file_out(base::FilePath::FromUTF8Unsafe(file_to_write),
53                       base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
54   CHECK(file_out.IsValid()) << file_out.error_details();
55 
56   HpackFuzzUtil::GeneratorContext context;
57   HpackFuzzUtil::InitializeGeneratorContext(&context);
58   spdy::HpackEncoder encoder;
59 
60   for (int i = 0; i != example_count; ++i) {
61     spdy::Http2HeaderBlock headers =
62         HpackFuzzUtil::NextGeneratedHeaderSet(&context);
63 
64     std::string buffer = encoder.EncodeHeaderBlock(headers);
65 
66     std::string prefix = HpackFuzzUtil::HeaderBlockPrefix(buffer.size());
67 
68     CHECK_LT(0, file_out.WriteAtCurrentPos(prefix.data(), prefix.size()));
69     CHECK_LT(0, file_out.WriteAtCurrentPos(buffer.data(), buffer.size()));
70   }
71   CHECK(file_out.Flush());
72   DVLOG(1) << "Generated " << example_count << " blocks.";
73   return 0;
74 }
75