hpack_example_generator.cc 2.6 KB

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