Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: net/http2/decoder/decode_http2_structures_test.cc

Issue 2572343002: Use std::function instead of base::Callback in net/http2/. (Closed)
Patch Set: Rebase. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http2/decoder/decode_buffer_test.cc ('k') | net/http2/decoder/http2_frame_decoder_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/decode_http2_structures.h" 5 #include "net/http2/decoder/decode_http2_structures.h"
6 6
7 // Tests decoding all of the fixed size HTTP/2 structures (i.e. those defined 7 // Tests decoding all of the fixed size HTTP/2 structures (i.e. those defined
8 // in net/http2/http2_structures.h). 8 // in net/http2/http2_structures.h).
9 9
10 // TODO(jamessynge): Combine tests of DoDecode, MaybeDecode, SlowDecode and 10 // TODO(jamessynge): Combine tests of DoDecode, MaybeDecode, SlowDecode and
11 // Http2StructureDecoder test using gUnit's support for tests parameterized 11 // Http2StructureDecoder test using gUnit's support for tests parameterized
12 // by type. 12 // by type.
13 13
14 #include <stddef.h> 14 #include <stddef.h>
15 #include <string> 15 #include <string>
16 16
17 #include "base/bind.h"
18 #include "base/bind_helpers.h"
19 #include "base/logging.h" 17 #include "base/logging.h"
20 #include "base/strings/string_piece.h" 18 #include "base/strings/string_piece.h"
21 #include "net/http2/decoder/decode_buffer.h" 19 #include "net/http2/decoder/decode_buffer.h"
22 #include "net/http2/decoder/decode_status.h" 20 #include "net/http2/decoder/decode_status.h"
23 #include "net/http2/http2_constants.h" 21 #include "net/http2/http2_constants.h"
24 #include "net/http2/http2_structures_test_util.h" 22 #include "net/http2/http2_structures_test_util.h"
25 #include "net/http2/tools/http2_frame_builder.h" 23 #include "net/http2/tools/http2_frame_builder.h"
26 #include "net/http2/tools/random_decoder_test.h" 24 #include "net/http2/tools/random_decoder_test.h"
27 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
28 26
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // Drained the input buffer, but not yet done. 101 // Drained the input buffer, but not yet done.
104 EXPECT_TRUE(b->Empty()); 102 EXPECT_TRUE(b->Empty());
105 EXPECT_GT(S::EncodedSize(), decode_offset_); 103 EXPECT_GT(S::EncodedSize(), decode_offset_);
106 104
107 return DecodeStatus::kDecodeInProgress; 105 return DecodeStatus::kDecodeInProgress;
108 } 106 }
109 107
110 // Set the fields of |*p| to random values. 108 // Set the fields of |*p| to random values.
111 void Randomize(S* p) { ::net::test::Randomize(p, RandomPtr()); } 109 void Randomize(S* p) { ::net::test::Randomize(p, RandomPtr()); }
112 110
113 AssertionResult ValidatorForDecodeLeadingStructure(const S* expected,
114 const DecodeBuffer& db,
115 DecodeStatus status) {
116 if (expected != nullptr && *expected != structure_) {
117 return AssertionFailure()
118 << "Expected structs to be equal\nExpected: " << *expected
119 << "\n Actual: " << structure_;
120 }
121 return AssertionSuccess();
122 }
123
124 // Fully decodes the Structure at the start of data, and confirms it matches 111 // Fully decodes the Structure at the start of data, and confirms it matches
125 // *expected (if provided). 112 // *expected (if provided).
126 void DecodeLeadingStructure(const S* expected, StringPiece data) { 113 void DecodeLeadingStructure(const S* expected, StringPiece data) {
127 ASSERT_LE(S::EncodedSize(), data.size()); 114 ASSERT_LE(S::EncodedSize(), data.size());
128 DecodeBuffer original(data); 115 DecodeBuffer original(data);
129 116
130 // The validator is called after each of the several times that the input 117 // The validator is called after each of the several times that the input
131 // DecodeBuffer is decoded, each with a different segmentation of the input. 118 // DecodeBuffer is decoded, each with a different segmentation of the input.
132 // Validate that structure_ matches the expected value, if provided. 119 // Validate that structure_ matches the expected value, if provided.
133 Validator validator = 120 Validator validator = [expected, this](
134 base::Bind(&StructureDecoderTest::ValidatorForDecodeLeadingStructure, 121 const DecodeBuffer& db, DecodeStatus status) -> AssertionResult {
135 base::Unretained(this), expected); 122 if (expected != nullptr && *expected != structure_) {
123 return AssertionFailure()
124 << "Expected structs to be equal\nExpected: " << *expected
125 << "\n Actual: " << structure_;
126 }
127 return AssertionSuccess();
128 };
136 129
137 // First validate that decoding is done and that we've advanced the cursor 130 // First validate that decoding is done and that we've advanced the cursor
138 // the expected amount. 131 // the expected amount.
139 Validator wrapped_validator = 132 validator = ValidateDoneAndOffset(S::EncodedSize(), validator);
140 ValidateDoneAndOffset(S::EncodedSize(), validator);
141 133
142 // Decode several times, with several segmentations of the input buffer. 134 // Decode several times, with several segmentations of the input buffer.
143 fast_decode_count_ = 0; 135 fast_decode_count_ = 0;
144 slow_decode_count_ = 0; 136 slow_decode_count_ = 0;
145 EXPECT_TRUE(DecodeAndValidateSeveralWays( 137 EXPECT_TRUE(DecodeAndValidateSeveralWays(
146 &original, false /*return_non_zero_on_first*/, wrapped_validator)); 138 &original, false /*return_non_zero_on_first*/, validator));
147 139
148 if (!HasFailure()) { 140 if (!HasFailure()) {
149 EXPECT_EQ(S::EncodedSize(), decode_offset_); 141 EXPECT_EQ(S::EncodedSize(), decode_offset_);
150 EXPECT_EQ(S::EncodedSize(), original.Offset()); 142 EXPECT_EQ(S::EncodedSize(), original.Offset());
151 EXPECT_LT(0u, fast_decode_count_); 143 EXPECT_LT(0u, fast_decode_count_);
152 EXPECT_LT(0u, slow_decode_count_); 144 EXPECT_LT(0u, slow_decode_count_);
153 if (expected != nullptr) { 145 if (expected != nullptr) {
154 DVLOG(1) << "DecodeLeadingStructure expected: " << *expected; 146 DVLOG(1) << "DecodeLeadingStructure expected: " << *expected;
155 DVLOG(1) << "DecodeLeadingStructure actual: " << structure_; 147 DVLOG(1) << "DecodeLeadingStructure actual: " << structure_;
156 EXPECT_EQ(*expected, structure_); 148 EXPECT_EQ(*expected, structure_);
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 } 525 }
534 } 526 }
535 527
536 TEST_F(AltSvcFieldsDecoderTest, DecodesRandomized) { 528 TEST_F(AltSvcFieldsDecoderTest, DecodesRandomized) {
537 TestDecodingRandomizedStructures(100); 529 TestDecodingRandomizedStructures(100);
538 } 530 }
539 531
540 } // namespace 532 } // namespace
541 } // namespace test 533 } // namespace test
542 } // namespace net 534 } // namespace net
OLDNEW
« no previous file with comments | « net/http2/decoder/decode_buffer_test.cc ('k') | net/http2/decoder/http2_frame_decoder_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698