| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/http2/decoder/http2_structure_decoder.h" | 5 #include "net/http2/decoder/http2_structure_decoder.h" |
| 6 | 6 |
| 7 // Tests decoding all of the fixed size HTTP/2 structures (i.e. those defined in | 7 // Tests decoding all of the fixed size HTTP/2 structures (i.e. those defined in |
| 8 // net/http2/http2_structures.h) using Http2StructureDecoder, which handles | 8 // net/http2/http2_structures.h) using Http2StructureDecoder, which handles |
| 9 // buffering of structures split across input buffer boundaries, and in turn | 9 // buffering of structures split across input buffer boundaries, and in turn |
| 10 // uses DoDecode when it has all of a structure in a contiguous buffer. | 10 // uses DoDecode when it has all of a structure in a contiguous buffer. |
| 11 | 11 |
| 12 // NOTE: This tests the first pair of Start and Resume, which don't take | 12 // NOTE: This tests the first pair of Start and Resume, which don't take |
| 13 // a remaining_payload parameter. The other pair are well tested via the | 13 // a remaining_payload parameter. The other pair are well tested via the |
| 14 // payload decoder tests, though... | 14 // payload decoder tests, though... |
| 15 // TODO(jamessynge): Create type parameterized tests for Http2StructureDecoder | 15 // TODO(jamessynge): Create type parameterized tests for Http2StructureDecoder |
| 16 // where the type is the type of structure, and with testing of both pairs of | 16 // where the type is the type of structure, and with testing of both pairs of |
| 17 // Start and Resume methods; note that it appears that the first pair will be | 17 // Start and Resume methods; note that it appears that the first pair will be |
| 18 // used only for Http2FrameHeader, and the other pair only for structures in the | 18 // used only for Http2FrameHeader, and the other pair only for structures in the |
| 19 // frame payload. | 19 // frame payload. |
| 20 | 20 |
| 21 #include <stddef.h> | 21 #include <stddef.h> |
| 22 #include <string> | 22 #include <string> |
| 23 | 23 |
| 24 #include "base/bind.h" | |
| 25 #include "base/bind_helpers.h" | |
| 26 #include "base/logging.h" | 24 #include "base/logging.h" |
| 27 #include "base/strings/string_piece.h" | 25 #include "base/strings/string_piece.h" |
| 28 #include "net/http2/decoder/decode_buffer.h" | 26 #include "net/http2/decoder/decode_buffer.h" |
| 29 #include "net/http2/decoder/decode_status.h" | 27 #include "net/http2/decoder/decode_status.h" |
| 30 #include "net/http2/http2_constants.h" | 28 #include "net/http2/http2_constants.h" |
| 31 #include "net/http2/http2_structures_test_util.h" | 29 #include "net/http2/http2_structures_test_util.h" |
| 32 #include "net/http2/tools/failure.h" | 30 #include "net/http2/tools/failure.h" |
| 33 #include "net/http2/tools/http2_frame_builder.h" | 31 #include "net/http2/tools/http2_frame_builder.h" |
| 34 #include "net/http2/tools/random_decoder_test.h" | 32 #include "net/http2/tools/random_decoder_test.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return DecodeStatus::kDecodeDone; | 93 return DecodeStatus::kDecodeDone; |
| 96 } else { | 94 } else { |
| 97 EXPECT_LT(structure_decoder_.offset(), S::EncodedSize()); | 95 EXPECT_LT(structure_decoder_.offset(), S::EncodedSize()); |
| 98 EXPECT_EQ(0u, b->Remaining()); | 96 EXPECT_EQ(0u, b->Remaining()); |
| 99 EXPECT_GT(S::EncodedSize(), old_offset + avail); | 97 EXPECT_GT(S::EncodedSize(), old_offset + avail); |
| 100 ++incomplete_resume_count_; | 98 ++incomplete_resume_count_; |
| 101 return DecodeStatus::kDecodeInProgress; | 99 return DecodeStatus::kDecodeInProgress; |
| 102 } | 100 } |
| 103 } | 101 } |
| 104 | 102 |
| 105 AssertionResult ValidatorForDecodeLeadingStructure(const S* expected, | |
| 106 const DecodeBuffer& db, | |
| 107 DecodeStatus status) { | |
| 108 VERIFY_EQ(*expected, structure_); | |
| 109 return AssertionSuccess(); | |
| 110 } | |
| 111 | 103 |
| 112 // Fully decodes the Structure at the start of data, and confirms it matches | 104 // Fully decodes the Structure at the start of data, and confirms it matches |
| 113 // *expected (if provided). | 105 // *expected (if provided). |
| 114 AssertionResult DecodeLeadingStructure(const S* expected, StringPiece data) { | 106 AssertionResult DecodeLeadingStructure(const S* expected, StringPiece data) { |
| 115 VERIFY_LE(S::EncodedSize(), data.size()); | 107 VERIFY_LE(S::EncodedSize(), data.size()); |
| 116 DecodeBuffer original(data); | 108 DecodeBuffer original(data); |
| 117 | 109 |
| 118 // The validator is called after each of the several times that the input | 110 // The validator is called after each of the several times that the input |
| 119 // DecodeBuffer is decoded, each with a different segmentation of the input. | 111 // DecodeBuffer is decoded, each with a different segmentation of the input. |
| 120 // Validate that structure_ matches the expected value, if provided. | 112 // Validate that structure_ matches the expected value, if provided. |
| 121 Validator validator = | 113 Validator validator; |
| 122 (expected == nullptr) | 114 if (expected != nullptr) { |
| 123 ? base::Bind(&SucceedingValidator) | 115 validator = [expected, this](const DecodeBuffer& db, |
| 124 : base::Bind(&Http2StructureDecoderTest:: | 116 DecodeStatus status) -> AssertionResult { |
| 125 ValidatorForDecodeLeadingStructure, | 117 VERIFY_EQ(*expected, structure_); |
| 126 base::Unretained(this), expected); | 118 return AssertionSuccess(); |
| 119 }; |
| 120 } |
| 127 | 121 |
| 128 // Before that, validate that decoding is done and that we've advanced | 122 // Before that, validate that decoding is done and that we've advanced |
| 129 // the cursor the expected amount. | 123 // the cursor the expected amount. |
| 130 Validator wrapped_validator = | 124 validator = ValidateDoneAndOffset(S::EncodedSize(), validator); |
| 131 ValidateDoneAndOffset(S::EncodedSize(), validator); | |
| 132 | 125 |
| 133 // Decode several times, with several segmentations of the input buffer. | 126 // Decode several times, with several segmentations of the input buffer. |
| 134 fast_decode_count_ = 0; | 127 fast_decode_count_ = 0; |
| 135 slow_decode_count_ = 0; | 128 slow_decode_count_ = 0; |
| 136 incomplete_start_count_ = 0; | 129 incomplete_start_count_ = 0; |
| 137 incomplete_resume_count_ = 0; | 130 incomplete_resume_count_ = 0; |
| 138 VERIFY_SUCCESS(DecodeAndValidateSeveralWays( | 131 VERIFY_SUCCESS(DecodeAndValidateSeveralWays( |
| 139 &original, kMayReturnZeroOnFirst, wrapped_validator)); | 132 &original, kMayReturnZeroOnFirst, validator)); |
| 140 VERIFY_FALSE(HasFailure()); | 133 VERIFY_FALSE(HasFailure()); |
| 141 VERIFY_EQ(S::EncodedSize(), structure_decoder_.offset()); | 134 VERIFY_EQ(S::EncodedSize(), structure_decoder_.offset()); |
| 142 VERIFY_EQ(S::EncodedSize(), original.Offset()); | 135 VERIFY_EQ(S::EncodedSize(), original.Offset()); |
| 143 VERIFY_LT(0u, fast_decode_count_); | 136 VERIFY_LT(0u, fast_decode_count_); |
| 144 VERIFY_LT(0u, slow_decode_count_); | 137 VERIFY_LT(0u, slow_decode_count_); |
| 145 VERIFY_LT(0u, incomplete_start_count_); | 138 VERIFY_LT(0u, incomplete_start_count_); |
| 146 | 139 |
| 147 // If the structure is large enough so that SelectZeroOrOne will have | 140 // If the structure is large enough so that SelectZeroOrOne will have |
| 148 // caused Resume to return false, check that occurred. | 141 // caused Resume to return false, check that occurred. |
| 149 if (S::EncodedSize() >= 2) { | 142 if (S::EncodedSize() >= 2) { |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 } | 496 } |
| 504 } | 497 } |
| 505 | 498 |
| 506 TEST_F(Http2AltSvcFieldsDecoderTest, DecodesRandomized) { | 499 TEST_F(Http2AltSvcFieldsDecoderTest, DecodesRandomized) { |
| 507 TestDecodingRandomizedStructures(); | 500 TestDecodingRandomizedStructures(); |
| 508 } | 501 } |
| 509 | 502 |
| 510 } // namespace | 503 } // namespace |
| 511 } // namespace test | 504 } // namespace test |
| 512 } // namespace net | 505 } // namespace net |
| OLD | NEW |