1 // Copyright 2014 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.
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/file_util.h"
8 #include "base/files/file.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/spdy/fuzzing/hpack_fuzz_util.h"
11 #include "net/spdy/hpack_constants.h"
12 #include "net/spdy/hpack_encoder.h"
16 // Target file for generated HPACK header sets.
17 const char kFileToWrite[] = "file-to-write";
19 // Number of header sets to generate.
20 const char kExampleCount[] = "example-count";
24 using net::HpackFuzzUtil;
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.
31 int main(int argc, char** argv) {
32 base::AtExitManager exit_manager;
34 CommandLine::Init(argc, argv);
35 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
37 if (!command_line.HasSwitch(kFileToWrite) ||
38 !command_line.HasSwitch(kExampleCount)) {
39 LOG(ERROR) << "Usage: " << argv[0]
40 << " --" << kFileToWrite << "=/path/to/file.out"
41 << " --" << kExampleCount << "=1000";
44 string file_to_write = command_line.GetSwitchValueASCII(kFileToWrite);
46 int example_count = 0;
47 base::StringToInt(command_line.GetSwitchValueASCII(kExampleCount),
50 DVLOG(1) << "Writing output to " << file_to_write;
51 base::File file_out(base::FilePath::FromUTF8Unsafe(file_to_write),
52 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
53 CHECK(file_out.IsValid()) << file_out.error_details();
55 HpackFuzzUtil::GeneratorContext context;
56 HpackFuzzUtil::InitializeGeneratorContext(&context);
57 net::HpackEncoder encoder(net::ObtainHpackHuffmanTable());
59 for (int i = 0; i != example_count; ++i) {
60 map<string, string> headers =
61 HpackFuzzUtil::NextGeneratedHeaderSet(&context);
64 CHECK(encoder.EncodeHeaderSet(headers, &buffer));
66 string prefix = HpackFuzzUtil::HeaderBlockPrefix(buffer.size());
68 CHECK_LT(0, file_out.WriteAtCurrentPos(prefix.data(), prefix.size()));
69 CHECK_LT(0, file_out.WriteAtCurrentPos(buffer.data(), buffer.size()));
71 CHECK(file_out.Flush());
72 DVLOG(1) << "Generated " << example_count << " blocks.";