| 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 "net/spdy/spdy_test_utils.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; |
| 24 using test::a2b_hex; |
| 21 | 25 |
| 22 TEST(HpackFuzzUtilTest, GeneratorContextInitialization) { | 26 TEST(HpackFuzzUtilTest, GeneratorContextInitialization) { |
| 23 HpackFuzzUtil::GeneratorContext context; | 27 HpackFuzzUtil::GeneratorContext context; |
| 24 HpackFuzzUtil::InitializeGeneratorContext(&context); | 28 HpackFuzzUtil::InitializeGeneratorContext(&context); |
| 25 | 29 |
| 26 // Context was seeded with initial name & value fixtures. | 30 // Context was seeded with initial name & value fixtures. |
| 27 EXPECT_LT(0u, context.names.size()); | 31 EXPECT_LT(0u, context.names.size()); |
| 28 EXPECT_LT(0u, context.values.size()); | 32 EXPECT_LT(0u, context.values.size()); |
| 29 } | 33 } |
| 30 | 34 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 88 |
| 85 TEST(HpackFuzzUtilTest, SerializedHeaderBlockPrefixes) { | 89 TEST(HpackFuzzUtilTest, SerializedHeaderBlockPrefixes) { |
| 86 EXPECT_EQ(string("\x00\x00\x00\x00", 4), HpackFuzzUtil::HeaderBlockPrefix(0)); | 90 EXPECT_EQ(string("\x00\x00\x00\x00", 4), HpackFuzzUtil::HeaderBlockPrefix(0)); |
| 87 EXPECT_EQ(string("\x00\x00\x00\x05", 4), HpackFuzzUtil::HeaderBlockPrefix(5)); | 91 EXPECT_EQ(string("\x00\x00\x00\x05", 4), HpackFuzzUtil::HeaderBlockPrefix(5)); |
| 88 EXPECT_EQ(string("\x4f\xb3\x0a\x91", 4), | 92 EXPECT_EQ(string("\x4f\xb3\x0a\x91", 4), |
| 89 HpackFuzzUtil::HeaderBlockPrefix(1337133713)); | 93 HpackFuzzUtil::HeaderBlockPrefix(1337133713)); |
| 90 } | 94 } |
| 91 | 95 |
| 92 TEST(HpackFuzzUtilTest, PassValidInputThroughAllStages) { | 96 TEST(HpackFuzzUtilTest, PassValidInputThroughAllStages) { |
| 93 // Example lifted from HpackDecoderTest.SectionD3RequestHuffmanExamples. | 97 // Example lifted from HpackDecoderTest.SectionD3RequestHuffmanExamples. |
| 94 char input[] = | 98 string input = a2b_hex("828786448ce7cf9bebe89b6fb16fa9b6ff"); |
| 95 "\x82\x87\x86\x04\x8b\xdb\x6d\x88\x3e\x68\xd1\xcb\x12\x25\xba\x7f"; | |
| 96 | 99 |
| 97 HpackFuzzUtil::FuzzerContext context; | 100 HpackFuzzUtil::FuzzerContext context; |
| 98 HpackFuzzUtil::InitializeFuzzerContext(&context); | 101 HpackFuzzUtil::InitializeFuzzerContext(&context); |
| 99 | 102 |
| 100 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages( | 103 EXPECT_TRUE( |
| 101 &context, | 104 HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages(&context, input)); |
| 102 StringPiece(input, arraysize(input) - 1))); | |
| 103 | 105 |
| 104 std::map<string, string> expect; | 106 std::map<string, string> expect; |
| 105 expect[":method"] = "GET"; | 107 expect[":method"] = "GET"; |
| 106 expect[":scheme"] = "http"; | 108 expect[":scheme"] = "http"; |
| 107 expect[":path"] = "/"; | 109 expect[":path"] = "/"; |
| 108 expect[":authority"] = "www.example.com"; | 110 expect[":authority"] = "www.example.com"; |
| 109 EXPECT_EQ(expect, context.third_stage->decoded_block()); | 111 EXPECT_EQ(expect, context.third_stage->decoded_block()); |
| 110 } | 112 } |
| 111 | 113 |
| 112 TEST(HpackFuzzUtilTest, ValidFuzzExamplesRegressionTest) { | 114 TEST(HpackFuzzUtilTest, ValidFuzzExamplesRegressionTest) { |
| 113 base::FilePath source_root; | 115 base::FilePath source_root; |
| 114 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root)); | 116 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root)); |
| 115 | 117 |
| 116 // Load the example fixtures versioned with the source tree. | 118 // Load the example fixtures versioned with the source tree. |
| 117 HpackFuzzUtil::Input input; | 119 HpackFuzzUtil::Input input; |
| 118 ASSERT_TRUE(base::ReadFileToString( | 120 ASSERT_TRUE(base::ReadFileToString( |
| 119 source_root.Append(FILE_PATH_LITERAL("net")) | 121 source_root.Append(FILE_PATH_LITERAL("net")) |
| 120 .Append(FILE_PATH_LITERAL("data")) | 122 .Append(FILE_PATH_LITERAL("data")) |
| 121 .Append(FILE_PATH_LITERAL("spdy_tests")) | 123 .Append(FILE_PATH_LITERAL("spdy_tests")) |
| 122 .Append(FILE_PATH_LITERAL("examples.hpack")), | 124 .Append(FILE_PATH_LITERAL("examples_07.hpack")), |
| 123 &input.input)); | 125 &input.input)); |
| 124 | 126 |
| 125 HpackFuzzUtil::FuzzerContext context; | 127 HpackFuzzUtil::FuzzerContext context; |
| 126 HpackFuzzUtil::InitializeFuzzerContext(&context); | 128 HpackFuzzUtil::InitializeFuzzerContext(&context); |
| 127 | 129 |
| 128 StringPiece block; | 130 StringPiece block; |
| 129 while (HpackFuzzUtil::NextHeaderBlock(&input, &block)) { | 131 while (HpackFuzzUtil::NextHeaderBlock(&input, &block)) { |
| 130 // As these are valid examples, all fuzz stages should succeed. | 132 // As these are valid examples, all fuzz stages should succeed. |
| 131 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages( | 133 EXPECT_TRUE(HpackFuzzUtil::RunHeaderBlockThroughFuzzerStages( |
| 132 &context, block)); | 134 &context, block)); |
| 133 } | 135 } |
| 134 } | 136 } |
| 135 | 137 |
| 136 TEST(HpackFuzzUtilTest, FlipBitsMutatesBuffer) { | 138 TEST(HpackFuzzUtilTest, FlipBitsMutatesBuffer) { |
| 137 char buffer[] = "testbuffer1234567890"; | 139 char buffer[] = "testbuffer1234567890"; |
| 138 string unmodified(buffer, arraysize(buffer) - 1); | 140 string unmodified(buffer, arraysize(buffer) - 1); |
| 139 | 141 |
| 140 EXPECT_EQ(unmodified, buffer); | 142 EXPECT_EQ(unmodified, buffer); |
| 141 HpackFuzzUtil::FlipBits(reinterpret_cast<uint8*>(buffer), | 143 HpackFuzzUtil::FlipBits(reinterpret_cast<uint8*>(buffer), |
| 142 arraysize(buffer) - 1, | 144 arraysize(buffer) - 1, |
| 143 1); | 145 1); |
| 144 EXPECT_NE(unmodified, buffer); | 146 EXPECT_NE(unmodified, buffer); |
| 145 } | 147 } |
| 146 | 148 |
| 149 } // namespace |
| 150 |
| 147 } // namespace net | 151 } // namespace net |
| OLD | NEW |