Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / spdy / fuzzing / hpack_example_generator.cc
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.
4
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"
13
14 namespace {
15
16 // Target file for generated HPACK header sets.
17 const char kFileToWrite[] = "file-to-write";
18
19 // Number of header sets to generate.
20 const char kExampleCount[] = "example-count";
21
22 }  // namespace
23
24 using net::HpackFuzzUtil;
25 using std::map;
26 using std::string;
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.
31 int main(int argc, char** argv) {
32   base::AtExitManager exit_manager;
33
34   CommandLine::Init(argc, argv);
35   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
36
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";
42     return -1;
43   }
44   string file_to_write = command_line.GetSwitchValueASCII(kFileToWrite);
45
46   int example_count = 0;
47   base::StringToInt(command_line.GetSwitchValueASCII(kExampleCount),
48                     &example_count);
49
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();
54
55   HpackFuzzUtil::GeneratorContext context;
56   HpackFuzzUtil::InitializeGeneratorContext(&context);
57   net::HpackEncoder encoder(net::ObtainHpackHuffmanTable());
58
59   for (int i = 0; i != example_count; ++i) {
60     map<string, string> headers =
61         HpackFuzzUtil::NextGeneratedHeaderSet(&context);
62
63     string buffer;
64     CHECK(encoder.EncodeHeaderSet(headers, &buffer));
65
66     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 }