Home | History | Annotate | Download | only in fuzzing
      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/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/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   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]
     41                << " --" << kFileToWrite << "=/path/to/file.out"
     42                << " --" << kExampleCount << "=1000";
     43     return -1;
     44   }
     45   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   net::HpackEncoder encoder(net::ObtainHpackHuffmanTable());
     59 
     60   for (int i = 0; i != example_count; ++i) {
     61     map<string, string> headers =
     62         HpackFuzzUtil::NextGeneratedHeaderSet(&context);
     63 
     64     string buffer;
     65     CHECK(encoder.EncodeHeaderSet(headers, &buffer));
     66 
     67     string prefix = HpackFuzzUtil::HeaderBlockPrefix(buffer.size());
     68 
     69     CHECK_LT(0, file_out.WriteAtCurrentPos(prefix.data(), prefix.size()));
     70     CHECK_LT(0, file_out.WriteAtCurrentPos(buffer.data(), buffer.size()));
     71   }
     72   CHECK(file_out.Flush());
     73   DVLOG(1) << "Generated " << example_count << " blocks.";
     74   return 0;
     75 }
     76