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 "net/spdy/fuzzing/hpack_fuzz_util.h"
9 #include "base/base_paths.h"
10 #include "base/files/file.h"
11 #include "base/files/file_util.h"
12 #include "base/path_service.h"
13 #include "net/spdy/spdy_test_utils.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 using base::StringPiece
;
25 TEST(HpackFuzzUtilTest
, GeneratorContextInitialization
) {
26 HpackFuzzUtil::GeneratorContext context
;
27 HpackFuzzUtil::InitializeGeneratorContext(&context
);
29 // Context was seeded with initial name & value fixtures.
30 EXPECT_LT(0u, context
.names
.size());
31 EXPECT_LT(0u, context
.values
.size());
34 TEST(HpackFuzzUtil
, GeneratorContextExpansion
) {
35 HpackFuzzUtil::GeneratorContext context
;
37 SpdyHeaderBlock headers
= HpackFuzzUtil::NextGeneratedHeaderSet(&context
);
39 // Headers were generated, and the generator context was expanded.
40 EXPECT_LT(0u, headers
.size());
41 EXPECT_LT(0u, context
.names
.size());
42 EXPECT_LT(0u, context
.values
.size());
45 // TODO(jgraettinger): A better test would mock a random generator and
46 // evaluate SampleExponential along fixed points of the [0,1] domain.
47 TEST(HpackFuzzUtilTest
, SampleExponentialRegression
) {
48 // TODO(jgraettinger): Upstream uses a seeded random generator here to pin
49 // the behavior of SampleExponential. Chromium's random generation utilities
50 // are strongly secure, but provide no way to seed the generator.
51 for (size_t i
= 0; i
!= 100; ++i
) {
52 EXPECT_GE(30u, HpackFuzzUtil::SampleExponential(10, 30));
56 TEST(HpackFuzzUtilTest
, ParsesSequenceOfHeaderBlocks
) {
58 "\x00\x00\x00\x05""aaaaa"
59 "\x00\x00\x00\x04""bbbb"
60 "\x00\x00\x00\x03""ccc"
61 "\x00\x00\x00\x02""dd"
64 "\x00\x00\x00\x03""fin";
66 HpackFuzzUtil::Input input
;
67 input
.input
.assign(fixture
, arraysize(fixture
) - 1);
71 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
72 EXPECT_EQ("aaaaa", block
);
73 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
74 EXPECT_EQ("bbbb", block
);
75 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
76 EXPECT_EQ("ccc", block
);
77 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
78 EXPECT_EQ("dd", block
);
79 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
80 EXPECT_EQ("e", block
);
81 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
83 EXPECT_TRUE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
84 EXPECT_EQ("fin", block
);
85 EXPECT_FALSE(HpackFuzzUtil::NextHeaderBlock(&input
, &block
));
88 TEST(HpackFuzzUtilTest
, SerializedHeaderBlockPrefixes
) {
89 EXPECT_EQ(string("\x00\x00\x00\x00", 4), HpackFuzzUtil::HeaderBlockPrefix(0));
90 EXPECT_EQ(string("\x00\x00\x00\x05", 4), HpackFuzzUtil::HeaderBlockPrefix(5));
91 EXPECT_EQ(string("\x4f\xb3\x0a\x91", 4),
92 HpackFuzzUtil::HeaderBlockPrefix(1337133713));
95 TEST(HpackFuzzUtilTest
, PassValidInputThroughAllStages
) {
96 // Example lifted from HpackDecoderTest.SectionD4RequestHuffmanExamples.
97 string input
= a2b_hex("828684418cf1e3c2e5f23a6ba0ab90f4"
100 HpackFuzzUtil::FuzzerContext context
;
101 HpackFuzzUtil::InitializeFuzzerContext(&context
);
104 HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages(&context
, input
));
106 SpdyHeaderBlock expect
;
107 expect
[":method"] = "GET";
108 expect
[":scheme"] = "http";
109 expect
[":path"] = "/";
110 expect
[":authority"] = "www.example.com";
112 CompareSpdyHeaderBlocks(expect
, context
.third_stage
->decoded_block()));
115 TEST(HpackFuzzUtilTest
, ValidFuzzExamplesRegressionTest
) {
116 base::FilePath source_root
;
117 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &source_root
));
119 // Load the example fixtures versioned with the source tree.
120 HpackFuzzUtil::Input input
;
121 ASSERT_TRUE(base::ReadFileToString(
122 source_root
.Append(FILE_PATH_LITERAL("net"))
123 .Append(FILE_PATH_LITERAL("data"))
124 .Append(FILE_PATH_LITERAL("spdy_tests"))
125 .Append(FILE_PATH_LITERAL("examples_07.hpack")),
128 HpackFuzzUtil::FuzzerContext context
;
129 HpackFuzzUtil::InitializeFuzzerContext(&context
);
132 while (HpackFuzzUtil::NextHeaderBlock(&input
, &block
)) {
133 // As these are valid examples, all fuzz stages should succeed.
134 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages(
139 TEST(HpackFuzzUtilTest
, FlipBitsMutatesBuffer
) {
140 char buffer
[] = "testbuffer1234567890";
141 string
unmodified(buffer
, arraysize(buffer
) - 1);
143 EXPECT_EQ(unmodified
, buffer
);
144 HpackFuzzUtil::FlipBits(reinterpret_cast<uint8
*>(buffer
),
145 arraysize(buffer
) - 1,
147 EXPECT_NE(unmodified
, buffer
);