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