| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/fuzzing/hpack_fuzz_util.h" | 5 #include "net/spdy/fuzzing/hpack_fuzz_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 19 namespace { |
| 20 |
| 18 using base::StringPiece; | 21 using base::StringPiece; |
| 19 using std::map; | 22 using std::map; |
| 20 using std::string; | 23 using std::string; |
| 21 | 24 |
| 25 std::string a2b_hex(const char* hex_data) { |
| 26 std::vector<uint8> output; |
| 27 std::string result; |
| 28 if (base::HexStringToBytes(hex_data, &output)) |
| 29 result.assign(reinterpret_cast<const char*>(&output[0]), output.size()); |
| 30 return result; |
| 31 } |
| 32 |
| 22 TEST(HpackFuzzUtilTest, GeneratorContextInitialization) { | 33 TEST(HpackFuzzUtilTest, GeneratorContextInitialization) { |
| 23 HpackFuzzUtil::GeneratorContext context; | 34 HpackFuzzUtil::GeneratorContext context; |
| 24 HpackFuzzUtil::InitializeGeneratorContext(&context); | 35 HpackFuzzUtil::InitializeGeneratorContext(&context); |
| 25 | 36 |
| 26 // Context was seeded with initial name & value fixtures. | 37 // Context was seeded with initial name & value fixtures. |
| 27 EXPECT_LT(0u, context.names.size()); | 38 EXPECT_LT(0u, context.names.size()); |
| 28 EXPECT_LT(0u, context.values.size()); | 39 EXPECT_LT(0u, context.values.size()); |
| 29 } | 40 } |
| 30 | 41 |
| 31 TEST(HpackFuzzUtil, GeneratorContextExpansion) { | 42 TEST(HpackFuzzUtil, GeneratorContextExpansion) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 95 |
| 85 TEST(HpackFuzzUtilTest, SerializedHeaderBlockPrefixes) { | 96 TEST(HpackFuzzUtilTest, SerializedHeaderBlockPrefixes) { |
| 86 EXPECT_EQ(string("\x00\x00\x00\x00", 4), HpackFuzzUtil::HeaderBlockPrefix(0)); | 97 EXPECT_EQ(string("\x00\x00\x00\x00", 4), HpackFuzzUtil::HeaderBlockPrefix(0)); |
| 87 EXPECT_EQ(string("\x00\x00\x00\x05", 4), HpackFuzzUtil::HeaderBlockPrefix(5)); | 98 EXPECT_EQ(string("\x00\x00\x00\x05", 4), HpackFuzzUtil::HeaderBlockPrefix(5)); |
| 88 EXPECT_EQ(string("\x4f\xb3\x0a\x91", 4), | 99 EXPECT_EQ(string("\x4f\xb3\x0a\x91", 4), |
| 89 HpackFuzzUtil::HeaderBlockPrefix(1337133713)); | 100 HpackFuzzUtil::HeaderBlockPrefix(1337133713)); |
| 90 } | 101 } |
| 91 | 102 |
| 92 TEST(HpackFuzzUtilTest, PassValidInputThroughAllStages) { | 103 TEST(HpackFuzzUtilTest, PassValidInputThroughAllStages) { |
| 93 // Example lifted from HpackDecoderTest.SectionD3RequestHuffmanExamples. | 104 // Example lifted from HpackDecoderTest.SectionD3RequestHuffmanExamples. |
| 94 char input[] = | 105 string input = a2b_hex("828786448ce7cf9bebe89b6fb16fa9b6ff"); |
| 95 "\x82\x87\x86\x04\x8b\xdb\x6d\x88\x3e\x68\xd1\xcb\x12\x25\xba\x7f"; | |
| 96 | 106 |
| 97 HpackFuzzUtil::FuzzerContext context; | 107 HpackFuzzUtil::FuzzerContext context; |
| 98 HpackFuzzUtil::InitializeFuzzerContext(&context); | 108 HpackFuzzUtil::InitializeFuzzerContext(&context); |
| 99 | 109 |
| 100 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages( | 110 EXPECT_TRUE( |
| 101 &context, | 111 HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages(&context, input)); |
| 102 StringPiece(input, arraysize(input) - 1))); | |
| 103 | 112 |
| 104 std::map<string, string> expect; | 113 std::map<string, string> expect; |
| 105 expect[":method"] = "GET"; | 114 expect[":method"] = "GET"; |
| 106 expect[":scheme"] = "http"; | 115 expect[":scheme"] = "http"; |
| 107 expect[":path"] = "/"; | 116 expect[":path"] = "/"; |
| 108 expect[":authority"] = "www.example.com"; | 117 expect[":authority"] = "www.example.com"; |
| 109 EXPECT_EQ(expect, context.third_stage->decoded_block()); | 118 EXPECT_EQ(expect, context.third_stage->decoded_block()); |
| 110 } | 119 } |
| 111 | 120 |
| 112 TEST(HpackFuzzUtilTest, ValidFuzzExamplesRegressionTest) { | 121 TEST(HpackFuzzUtilTest, ValidFuzzExamplesRegressionTest) { |
| 113 base::FilePath source_root; | 122 base::FilePath source_root; |
| 114 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root)); | 123 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root)); |
| 115 | 124 |
| 116 // Load the example fixtures versioned with the source tree. | 125 // Load the example fixtures versioned with the source tree. |
| 117 HpackFuzzUtil::Input input; | 126 HpackFuzzUtil::Input input; |
| 118 ASSERT_TRUE(base::ReadFileToString( | 127 ASSERT_TRUE(base::ReadFileToString( |
| 119 source_root.Append(FILE_PATH_LITERAL("net")) | 128 source_root.Append(FILE_PATH_LITERAL("net")) |
| 120 .Append(FILE_PATH_LITERAL("data")) | 129 .Append(FILE_PATH_LITERAL("data")) |
| 121 .Append(FILE_PATH_LITERAL("spdy_tests")) | 130 .Append(FILE_PATH_LITERAL("spdy_tests")) |
| 122 .Append(FILE_PATH_LITERAL("examples.hpack")), | 131 .Append(FILE_PATH_LITERAL("examples_07.hpack")), |
| 123 &input.input)); | 132 &input.input)); |
| 124 | 133 |
| 125 HpackFuzzUtil::FuzzerContext context; | 134 HpackFuzzUtil::FuzzerContext context; |
| 126 HpackFuzzUtil::InitializeFuzzerContext(&context); | 135 HpackFuzzUtil::InitializeFuzzerContext(&context); |
| 127 | 136 |
| 128 StringPiece block; | 137 StringPiece block; |
| 129 while (HpackFuzzUtil::NextHeaderBlock(&input, &block)) { | 138 while (HpackFuzzUtil::NextHeaderBlock(&input, &block)) { |
| 130 // As these are valid examples, all fuzz stages should succeed. | 139 // As these are valid examples, all fuzz stages should succeed. |
| 131 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages( | 140 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages( |
| 132 &context, block)); | 141 &context, block)); |
| 133 } | 142 } |
| 134 } | 143 } |
| 135 | 144 |
| 136 TEST(HpackFuzzUtilTest, FlipBitsMutatesBuffer) { | 145 TEST(HpackFuzzUtilTest, FlipBitsMutatesBuffer) { |
| 137 char buffer[] = "testbuffer1234567890"; | 146 char buffer[] = "testbuffer1234567890"; |
| 138 string unmodified(buffer, arraysize(buffer) - 1); | 147 string unmodified(buffer, arraysize(buffer) - 1); |
| 139 | 148 |
| 140 EXPECT_EQ(unmodified, buffer); | 149 EXPECT_EQ(unmodified, buffer); |
| 141 HpackFuzzUtil::FlipBits(reinterpret_cast<uint8*>(buffer), | 150 HpackFuzzUtil::FlipBits(reinterpret_cast<uint8*>(buffer), |
| 142 arraysize(buffer) - 1, | 151 arraysize(buffer) - 1, |
| 143 1); | 152 1); |
| 144 EXPECT_NE(unmodified, buffer); | 153 EXPECT_NE(unmodified, buffer); |
| 145 } | 154 } |
| 146 | 155 |
| 156 } // namespace |
| 157 |
| 147 } // namespace net | 158 } // namespace net |
| OLD | NEW |