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

Side by Side Diff: net/http2/hpack/decoder/hpack_entry_decoder_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
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/hpack/decoder/hpack_entry_decoder.h" 5 #include "net/http2/hpack/decoder/hpack_entry_decoder.h"
6 6
7 // Tests of HpackEntryDecoder. 7 // Tests of HpackEntryDecoder.
8 8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "net/http2/hpack/decoder/hpack_entry_collector.h" 9 #include "net/http2/hpack/decoder/hpack_entry_collector.h"
12 #include "net/http2/hpack/tools/hpack_block_builder.h" 10 #include "net/http2/hpack/tools/hpack_block_builder.h"
13 #include "net/http2/tools/failure.h" 11 #include "net/http2/tools/failure.h"
14 #include "net/http2/tools/http2_random.h" 12 #include "net/http2/tools/http2_random.h"
15 #include "net/http2/tools/random_decoder_test.h" 13 #include "net/http2/tools/random_decoder_test.h"
16 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
17 15
18 using ::testing::AssertionResult; 16 using ::testing::AssertionResult;
19 using std::string; 17 using std::string;
20 18
21 namespace net { 19 namespace net {
22 namespace test { 20 namespace test {
23 namespace { 21 namespace {
24 22
25 class HpackEntryDecoderTest : public RandomDecoderTest { 23 class HpackEntryDecoderTest : public RandomDecoderTest {
26 public:
27 AssertionResult ValidateIndexedHeader(uint32_t ndx) {
28 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateIndexedHeader(ndx));
29 }
30
31 AssertionResult ValidateForIndexedLiteralValue_Literal() {
32 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralValueHeader(
33 HpackEntryType::kIndexedLiteralHeader, 0x40, false, "custom-header"));
34 }
35
36 AssertionResult ValidateForIndexedLiteralNameValue_Literal() {
37 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralNameValueHeader(
38 HpackEntryType::kIndexedLiteralHeader, false, "custom-key", false,
39 "custom-header"));
40 }
41
42 AssertionResult ValidateForDynamicTableSizeUpdate_Literal() {
43 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateDynamicTableSizeUpdate(31));
44 }
45 24
46 protected: 25 protected:
47 HpackEntryDecoderTest() : listener_(&collector_) {} 26 HpackEntryDecoderTest() : listener_(&collector_) {}
48 27
49 DecodeStatus StartDecoding(DecodeBuffer* b) override { 28 DecodeStatus StartDecoding(DecodeBuffer* b) override {
50 collector_.Clear(); 29 collector_.Clear();
51 return decoder_.Start(b, &listener_); 30 return decoder_.Start(b, &listener_);
52 } 31 }
53 32
54 DecodeStatus ResumeDecoding(DecodeBuffer* b) override { 33 DecodeStatus ResumeDecoding(DecodeBuffer* b) override {
(...skipping 17 matching lines...) Expand all
72 51
73 HpackEntryDecoder decoder_; 52 HpackEntryDecoder decoder_;
74 HpackEntryCollector collector_; 53 HpackEntryCollector collector_;
75 HpackEntryDecoderVLoggingListener listener_; 54 HpackEntryDecoderVLoggingListener listener_;
76 }; 55 };
77 56
78 TEST_F(HpackEntryDecoderTest, IndexedHeader_Literals) { 57 TEST_F(HpackEntryDecoderTest, IndexedHeader_Literals) {
79 { 58 {
80 const char input[] = {0x82u}; // == Index 2 == 59 const char input[] = {0x82u}; // == Index 2 ==
81 DecodeBuffer b(input); 60 DecodeBuffer b(input);
82 NoArgValidator do_check = 61 NoArgValidator do_check = [this]() {
83 base::Bind(&HpackEntryDecoderTest::ValidateIndexedHeader, 62 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateIndexedHeader(2));
84 base::Unretained(this), 2); 63 };
85 EXPECT_TRUE( 64 EXPECT_TRUE(
86 DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); 65 DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check)));
87 EXPECT_TRUE(do_check.Run()); 66 EXPECT_TRUE(do_check());
88 } 67 }
89 collector_.Clear(); 68 collector_.Clear();
90 { 69 {
91 const char input[] = {0xfeu}; // == Index 126 == 70 const char input[] = {0xfeu}; // == Index 126 ==
92 DecodeBuffer b(input); 71 DecodeBuffer b(input);
93 NoArgValidator do_check = 72 NoArgValidator do_check = [this]() {
94 base::Bind(&HpackEntryDecoderTest::ValidateIndexedHeader, 73 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateIndexedHeader(126));
95 base::Unretained(this), 126); 74 };
96 EXPECT_TRUE( 75 EXPECT_TRUE(
97 DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); 76 DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check)));
98 EXPECT_TRUE(do_check.Run()); 77 EXPECT_TRUE(do_check());
99 } 78 }
100 collector_.Clear(); 79 collector_.Clear();
101 { 80 {
102 const char input[] = {0xffu, 0x00}; // == Index 127 == 81 const char input[] = {0xffu, 0x00}; // == Index 127 ==
103 DecodeBuffer b(input); 82 DecodeBuffer b(input);
104 NoArgValidator do_check = 83 NoArgValidator do_check = [this]() {
105 base::Bind(&HpackEntryDecoderTest::ValidateIndexedHeader, 84 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateIndexedHeader(127));
106 base::Unretained(this), 127); 85 };
107 EXPECT_TRUE( 86 EXPECT_TRUE(
108 DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); 87 DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check)));
109 EXPECT_TRUE(do_check.Run()); 88 EXPECT_TRUE(do_check());
110 } 89 }
111 } 90 }
112 91
113 TEST_F(HpackEntryDecoderTest, IndexedHeader_Various) { 92 TEST_F(HpackEntryDecoderTest, IndexedHeader_Various) {
114 // Indices chosen to hit encoding and table boundaries. 93 // Indices chosen to hit encoding and table boundaries.
115 for (const uint32_t ndx : {1, 2, 61, 62, 63, 126, 127, 254, 255, 256}) { 94 for (const uint32_t ndx : {1, 2, 61, 62, 63, 126, 127, 254, 255, 256}) {
116 HpackBlockBuilder hbb; 95 HpackBlockBuilder hbb;
117 hbb.AppendIndexedHeader(ndx); 96 hbb.AppendIndexedHeader(ndx);
118 97
119 NoArgValidator do_check = 98 NoArgValidator do_check = [this, ndx]() {
120 base::Bind(&HpackEntryDecoderTest::ValidateIndexedHeader, 99 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateIndexedHeader(ndx));
121 base::Unretained(this), ndx); 100 };
122 EXPECT_TRUE( 101 EXPECT_TRUE(
123 DecodeAndValidateSeveralWays(hbb, ValidateDoneAndEmpty(do_check))); 102 DecodeAndValidateSeveralWays(hbb, ValidateDoneAndEmpty(do_check)));
124 EXPECT_TRUE(do_check.Run()); 103 EXPECT_TRUE(do_check());
125 } 104 }
126 } 105 }
127 106
128 TEST_F(HpackEntryDecoderTest, IndexedLiteralValue_Literal) { 107 TEST_F(HpackEntryDecoderTest, IndexedLiteralValue_Literal) {
129 const char input[] = 108 const char input[] =
130 "\x7f" // == Literal indexed, name index 0x40 == 109 "\x7f" // == Literal indexed, name index 0x40 ==
131 "\x01" // 2nd byte of name index (0x01 + 0x3f == 0x40) 110 "\x01" // 2nd byte of name index (0x01 + 0x3f == 0x40)
132 "\x0d" // Value length (13) 111 "\x0d" // Value length (13)
133 "custom-header"; // Value 112 "custom-header"; // Value
134 DecodeBuffer b(input, sizeof input - 1); 113 DecodeBuffer b(input, sizeof input - 1);
135 NoArgValidator do_check = 114 NoArgValidator do_check = [this]() {
136 base::Bind(&HpackEntryDecoderTest::ValidateForIndexedLiteralValue_Literal, 115 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralValueHeader(
137 base::Unretained(this)); 116 HpackEntryType::kIndexedLiteralHeader, 0x40, false, "custom-header"));
117 };
138 EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); 118 EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check)));
139 EXPECT_TRUE(do_check.Run()); 119 EXPECT_TRUE(do_check());
140 } 120 }
141 121
142 TEST_F(HpackEntryDecoderTest, IndexedLiteralNameValue_Literal) { 122 TEST_F(HpackEntryDecoderTest, IndexedLiteralNameValue_Literal) {
143 const char input[] = 123 const char input[] =
144 "\x40" // == Literal indexed == 124 "\x40" // == Literal indexed ==
145 "\x0a" // Name length (10) 125 "\x0a" // Name length (10)
146 "custom-key" // Name 126 "custom-key" // Name
147 "\x0d" // Value length (13) 127 "\x0d" // Value length (13)
148 "custom-header"; // Value 128 "custom-header"; // Value
149 129
150 DecodeBuffer b(input, sizeof input - 1); 130 DecodeBuffer b(input, sizeof input - 1);
151 NoArgValidator do_check = base::Bind( 131 NoArgValidator do_check = [this]() {
152 &HpackEntryDecoderTest::ValidateForIndexedLiteralNameValue_Literal, 132 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralNameValueHeader(
153 base::Unretained(this)); 133 HpackEntryType::kIndexedLiteralHeader, false, "custom-key", false,
134 "custom-header"));
135 };
154 EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); 136 EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check)));
155 EXPECT_TRUE(do_check.Run()); 137 EXPECT_TRUE(do_check());
156 } 138 }
157 139
158 TEST_F(HpackEntryDecoderTest, DynamicTableSizeUpdate_Literal) { 140 TEST_F(HpackEntryDecoderTest, DynamicTableSizeUpdate_Literal) {
159 // Size update, length 31. 141 // Size update, length 31.
160 const char input[] = "\x3f\x00"; 142 const char input[] = "\x3f\x00";
161 DecodeBuffer b(input, 2); 143 DecodeBuffer b(input, 2);
162 NoArgValidator do_check = base::Bind( 144 NoArgValidator do_check = [this]() {
163 &HpackEntryDecoderTest::ValidateForDynamicTableSizeUpdate_Literal, 145 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateDynamicTableSizeUpdate(31));
164 base::Unretained(this)); 146 };
165 EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check))); 147 EXPECT_TRUE(DecodeAndValidateSeveralWays(&b, ValidateDoneAndEmpty(do_check)));
166 EXPECT_TRUE(do_check.Run()); 148 EXPECT_TRUE(do_check());
167 } 149 }
168 150
169 class HpackLiteralEntryDecoderTest 151 class HpackLiteralEntryDecoderTest
170 : public HpackEntryDecoderTest, 152 : public HpackEntryDecoderTest,
171 public ::testing::WithParamInterface<HpackEntryType> { 153 public ::testing::WithParamInterface<HpackEntryType> {
172 public:
173 AssertionResult ValidateForRandNameIndexAndLiteralValue(
174 uint32_t ndx,
175 bool value_is_huffman_encoded,
176 const string& value) {
177 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralValueHeader(
178 entry_type_, ndx, value_is_huffman_encoded, value));
179 }
180
181 AssertionResult ValidateForRandLiteralNameAndValue(
182 bool name_is_huffman_encoded,
183 const string& name,
184 bool value_is_huffman_encoded,
185 const string& value) {
186 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralNameValueHeader(
187 entry_type_, name_is_huffman_encoded, name, value_is_huffman_encoded,
188 value));
189 }
190 154
191 protected: 155 protected:
192 HpackLiteralEntryDecoderTest() : entry_type_(GetParam()) {} 156 HpackLiteralEntryDecoderTest() : entry_type_(GetParam()) {}
193 157
194 const HpackEntryType entry_type_; 158 const HpackEntryType entry_type_;
195 }; 159 };
196 160
197 INSTANTIATE_TEST_CASE_P( 161 INSTANTIATE_TEST_CASE_P(
198 AllLiteralTypes, 162 AllLiteralTypes,
199 HpackLiteralEntryDecoderTest, 163 HpackLiteralEntryDecoderTest,
200 testing::Values(HpackEntryType::kIndexedLiteralHeader, 164 testing::Values(HpackEntryType::kIndexedLiteralHeader,
201 HpackEntryType::kUnindexedLiteralHeader, 165 HpackEntryType::kUnindexedLiteralHeader,
202 HpackEntryType::kNeverIndexedLiteralHeader)); 166 HpackEntryType::kNeverIndexedLiteralHeader));
203 167
204 TEST_P(HpackLiteralEntryDecoderTest, RandNameIndexAndLiteralValue) { 168 TEST_P(HpackLiteralEntryDecoderTest, RandNameIndexAndLiteralValue) {
205 for (int n = 0; n < 10; n++) { 169 for (int n = 0; n < 10; n++) {
206 const uint32_t ndx = 1 + Random().Rand8(); 170 const uint32_t ndx = 1 + Random().Rand8();
207 const bool value_is_huffman_encoded = (n % 2) == 0; 171 const bool value_is_huffman_encoded = (n % 2) == 0;
208 const string value = Random().RandString(Random().Rand8()); 172 const string value = Random().RandString(Random().Rand8());
209 HpackBlockBuilder hbb; 173 HpackBlockBuilder hbb;
210 hbb.AppendNameIndexAndLiteralValue(entry_type_, ndx, 174 hbb.AppendNameIndexAndLiteralValue(entry_type_, ndx,
211 value_is_huffman_encoded, value); 175 value_is_huffman_encoded, value);
212 NoArgValidator do_check = base::Bind( 176 NoArgValidator do_check = [this, ndx, value_is_huffman_encoded,
213 &HpackLiteralEntryDecoderTest::ValidateForRandNameIndexAndLiteralValue, 177 value]() -> AssertionResult {
214 base::Unretained(this), ndx, value_is_huffman_encoded, value); 178 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralValueHeader(
179 entry_type_, ndx, value_is_huffman_encoded, value));
180 };
215 EXPECT_TRUE( 181 EXPECT_TRUE(
216 DecodeAndValidateSeveralWays(hbb, ValidateDoneAndEmpty(do_check))); 182 DecodeAndValidateSeveralWays(hbb, ValidateDoneAndEmpty(do_check)));
217 EXPECT_TRUE(do_check.Run()); 183 EXPECT_TRUE(do_check());
218 } 184 }
219 } 185 }
220 186
221 TEST_P(HpackLiteralEntryDecoderTest, RandLiteralNameAndValue) { 187 TEST_P(HpackLiteralEntryDecoderTest, RandLiteralNameAndValue) {
222 for (int n = 0; n < 10; n++) { 188 for (int n = 0; n < 10; n++) {
223 const bool name_is_huffman_encoded = (n & 1) == 0; 189 const bool name_is_huffman_encoded = (n & 1) == 0;
224 const int name_len = 1 + Random().Rand8(); 190 const int name_len = 1 + Random().Rand8();
225 const string name = Random().RandString(name_len); 191 const string name = Random().RandString(name_len);
226 const bool value_is_huffman_encoded = (n & 2) == 0; 192 const bool value_is_huffman_encoded = (n & 2) == 0;
227 const int value_len = Random().Skewed(10); 193 const int value_len = Random().Skewed(10);
228 const string value = Random().RandString(value_len); 194 const string value = Random().RandString(value_len);
229 HpackBlockBuilder hbb; 195 HpackBlockBuilder hbb;
230 hbb.AppendLiteralNameAndValue(entry_type_, name_is_huffman_encoded, name, 196 hbb.AppendLiteralNameAndValue(entry_type_, name_is_huffman_encoded, name,
231 value_is_huffman_encoded, value); 197 value_is_huffman_encoded, value);
232 NoArgValidator do_check = base::Bind( 198 NoArgValidator do_check = [this, name_is_huffman_encoded, name,
233 &HpackLiteralEntryDecoderTest::ValidateForRandLiteralNameAndValue, 199 value_is_huffman_encoded,
234 base::Unretained(this), name_is_huffman_encoded, name, 200 value]() -> AssertionResult {
235 value_is_huffman_encoded, value); 201 VERIFY_AND_RETURN_SUCCESS(collector_.ValidateLiteralNameValueHeader(
202 entry_type_, name_is_huffman_encoded, name, value_is_huffman_encoded,
203 value));
204 };
236 EXPECT_TRUE( 205 EXPECT_TRUE(
237 DecodeAndValidateSeveralWays(hbb, ValidateDoneAndEmpty(do_check))); 206 DecodeAndValidateSeveralWays(hbb, ValidateDoneAndEmpty(do_check)));
238 EXPECT_TRUE(do_check.Run()); 207 EXPECT_TRUE(do_check());
239 } 208 }
240 } 209 }
241 210
242 } // namespace 211 } // namespace
243 } // namespace test 212 } // namespace test
244 } // namespace net 213 } // namespace net
OLDNEW
« no previous file with comments | « net/http2/hpack/decoder/hpack_block_decoder_test.cc ('k') | net/http2/hpack/decoder/hpack_entry_type_decoder_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698