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 #ifndef NET_SPDY_FUZZING_HPACK_FUZZ_UTIL_H_
6 #define NET_SPDY_FUZZING_HPACK_FUZZ_UTIL_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "net/base/net_export.h"
14 #include "net/spdy/hpack_decoder.h"
15 #include "net/spdy/hpack_encoder.h"
19 class NET_EXPORT_PRIVATE HpackFuzzUtil
{
21 // A GeneratorContext holds ordered header names & values which are
22 // initially seeded and then expanded with dynamically generated data.
23 struct NET_EXPORT_PRIVATE GeneratorContext
{
26 std::vector
<std::string
> names
;
27 std::vector
<std::string
> values
;
30 // Initializes a GeneratorContext with a random seed and name/value fixtures.
31 static void InitializeGeneratorContext(GeneratorContext
* context
);
33 // Generates a header set from the generator context.
34 static std::map
<std::string
, std::string
> NextGeneratedHeaderSet(
35 GeneratorContext
* context
);
37 // Samples a size from the exponential distribution with mean |mean|,
38 // upper-bounded by |sanity_bound|.
39 static size_t SampleExponential(size_t mean
, size_t sanity_bound
);
41 // Holds an input string, and manages an offset into that string.
42 struct NET_EXPORT_PRIVATE Input
{
43 Input(); // Initializes |offset| to zero.
47 return input
.size() - offset
;
50 return input
.data() + offset
;
57 // Returns true if the next header block was set at |out|. Returns
58 // false if no input header blocks remain.
59 static bool NextHeaderBlock(Input
* input
, base::StringPiece
* out
);
61 // Returns the serialized header block length prefix for a block of
62 // |block_size| bytes.
63 static std::string
HeaderBlockPrefix(size_t block_size
);
65 // A FuzzerContext holds fuzzer input, as well as each of the decoder and
66 // encoder stages which fuzzed header blocks are processed through.
67 struct NET_EXPORT_PRIVATE FuzzerContext
{
70 scoped_ptr
<HpackDecoder
> first_stage
;
71 scoped_ptr
<HpackEncoder
> second_stage
;
72 scoped_ptr
<HpackDecoder
> third_stage
;
75 static void InitializeFuzzerContext(FuzzerContext
* context
);
77 // Runs |input_block| through |first_stage| and, iff that succeeds,
78 // |second_stage| and |third_stage| as well. Returns whether all stages
79 // processed the input without error.
80 static bool RunHeaderBlockThroughFuzzerStages(FuzzerContext
* context
,
81 base::StringPiece input_block
);
83 // Flips random bits within |buffer|. The total number of flips is
84 // |flip_per_thousand| bits for every 1,024 bytes of |buffer_length|,
86 static void FlipBits(uint8
* buffer
,
88 size_t flip_per_thousand
);
93 #endif // NET_SPDY_FUZZING_HPACK_FUZZ_UTIL_H_