1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http2/http2_structures.h"
// Tests are focused on Http2FrameHeader because it has by far the most
// methods of any of the structures.
// Note that EXPECT.*DEATH tests are slow (a fork is probably involved).
// And in case you're wondering, yes, these are ridiculously thorough tests,
// but believe it or not, I've found stupid bugs this way.
#include <memory>
#include <ostream>
#include <sstream>
#include <tuple>
#include <type_traits>
#include <vector>
#include "net/http2/http2_structures_test_util.h"
#include "net/http2/platform/api/http2_string_utils.h"
#include "net/http2/tools/failure.h"
#include "net/http2/tools/http2_random.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::AssertionResult;
using ::testing::AssertionSuccess;
using ::testing::Combine;
using ::testing::HasSubstr;
using ::testing::MatchesRegex;
using ::testing::Not;
using ::testing::Values;
using ::testing::ValuesIn;
namespace net {
namespace test {
namespace {
template <typename E>
E IncrementEnum(E e) {
using I = typename std::underlying_type<E>::type;
return static_cast<E>(1 + static_cast<I>(e));
}
#if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
std::vector<Http2FrameType> ValidFrameTypes() {
std::vector<Http2FrameType> valid_types{Http2FrameType::DATA};
while (valid_types.back() != Http2FrameType::ALTSVC) {
valid_types.push_back(IncrementEnum(valid_types.back()));
}
return valid_types;
}
#endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
TEST(Http2FrameHeaderTest, Constructor) {
Http2Random random;
uint8_t frame_type = 0;
do {
// Only the payload length is DCHECK'd in the constructor, so we need to
// make sure it is a "uint24".
uint32_t payload_length = random.Rand32() & 0xffffff;
Http2FrameType type = static_cast<Http2FrameType>(frame_type);
uint8_t flags = random.Rand8();
uint32_t stream_id = random.Rand32();
Http2FrameHeader v(payload_length, type, flags, stream_id);
EXPECT_EQ(payload_length, v.payload_length);
EXPECT_EQ(type, v.type);
EXPECT_EQ(flags, v.flags);
EXPECT_EQ(stream_id, v.stream_id);
} while (frame_type++ == 255);
#if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
EXPECT_DEBUG_DEATH(Http2FrameHeader(0x01000000, Http2FrameType::DATA, 0, 1),
"payload_length");
#endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
}
TEST(Http2FrameHeaderTest, Eq) {
Http2Random random;
uint32_t payload_length = random.Rand32() & 0xffffff;
Http2FrameType type = static_cast<Http2FrameType>(random.Rand8());
uint8_t flags = random.Rand8();
uint32_t stream_id = random.Rand32();
Http2FrameHeader v(payload_length, type, flags, stream_id);
EXPECT_EQ(payload_length, v.payload_length);
EXPECT_EQ(type, v.type);
EXPECT_EQ(flags, v.flags);
EXPECT_EQ(stream_id, v.stream_id);
Http2FrameHeader u(0, type, ~flags, stream_id);
EXPECT_NE(u, v);
EXPECT_NE(v, u);
EXPECT_FALSE(u == v);
EXPECT_FALSE(v == u);
EXPECT_TRUE(u != v);
EXPECT_TRUE(v != u);
u = v;
EXPECT_EQ(u, v);
EXPECT_EQ(v, u);
EXPECT_TRUE(u == v);
EXPECT_TRUE(v == u);
EXPECT_FALSE(u != v);
EXPECT_FALSE(v != u);
}
#if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
// The tests of the valid frame types include EXPECT_DEBUG_DEATH, which is
// quite slow, so using value parameterized tests in order to allow sharding.
class Http2FrameHeaderTypeAndFlagTest
: public ::testing::TestWithParam<
std::tuple<Http2FrameType, Http2FrameFlag>> {
protected:
Http2FrameHeaderTypeAndFlagTest()
: type_(std::get<0>(GetParam())), flags_(std::get<1>(GetParam())) {
LOG(INFO) << "Frame type: " << type_;
LOG(INFO) << "Frame flags: " << Http2FrameFlagsToString(type_, flags_);
}
const Http2FrameType type_;
const Http2FrameFlag flags_;
};
class IsEndStreamTest : public Http2FrameHeaderTypeAndFlagTest {};
INSTANTIATE_TEST_CASE_P(IsEndStream,
IsEndStreamTest,
Combine(ValuesIn(ValidFrameTypes()),
Values(~Http2FrameFlag::END_STREAM, 0xff)));
TEST_P(IsEndStreamTest, IsEndStream) {
const bool is_set =
(flags_ & Http2FrameFlag::END_STREAM) == Http2FrameFlag::END_STREAM;
Http2String flags_string;
Http2FrameHeader v(0, type_, flags_, 0);
switch (type_) {
case Http2FrameType::DATA:
case Http2FrameType::HEADERS:
EXPECT_EQ(is_set, v.IsEndStream()) << v;
flags_string = v.FlagsToString();
if (is_set) {
EXPECT_THAT(flags_string, MatchesRegex(".*\\|?END_STREAM\\|.*"));
} else {
EXPECT_THAT(flags_string, Not(HasSubstr("END_STREAM")));
}
v.RetainFlags(Http2FrameFlag::END_STREAM);
EXPECT_EQ(is_set, v.IsEndStream()) << v;
{
std::stringstream s;
s << v;
EXPECT_EQ(v.ToString(), s.str());
if (is_set) {
EXPECT_THAT(s.str(), HasSubstr("flags=END_STREAM,"));
} else {
EXPECT_THAT(s.str(), HasSubstr("flags=,"));
}
}
break;
default:
EXPECT_DEBUG_DEATH(v.IsEndStream(), "DATA.*HEADERS") << v;
}
}
class IsACKTest : public Http2FrameHeaderTypeAndFlagTest {};
INSTANTIATE_TEST_CASE_P(IsAck,
IsACKTest,
Combine(ValuesIn(ValidFrameTypes()),
Values(~Http2FrameFlag::ACK, 0xff)));
TEST_P(IsACKTest, IsAck) {
const bool is_set = (flags_ & Http2FrameFlag::ACK) == Http2FrameFlag::ACK;
Http2String flags_string;
Http2FrameHeader v(0, type_, flags_, 0);
switch (type_) {
case Http2FrameType::SETTINGS:
case Http2FrameType::PING:
EXPECT_EQ(is_set, v.IsAck()) << v;
flags_string = v.FlagsToString();
if (is_set) {
EXPECT_THAT(flags_string, MatchesRegex(".*\\|?ACK\\|.*"));
} else {
EXPECT_THAT(flags_string, Not(HasSubstr("ACK")));
}
v.RetainFlags(Http2FrameFlag::ACK);
EXPECT_EQ(is_set, v.IsAck()) << v;
{
std::stringstream s;
s << v;
EXPECT_EQ(v.ToString(), s.str());
if (is_set) {
EXPECT_THAT(s.str(), HasSubstr("flags=ACK,"));
} else {
EXPECT_THAT(s.str(), HasSubstr("flags=,"));
}
}
break;
default:
EXPECT_DEBUG_DEATH(v.IsAck(), "SETTINGS.*PING") << v;
}
}
class IsEndHeadersTest : public Http2FrameHeaderTypeAndFlagTest {};
INSTANTIATE_TEST_CASE_P(IsEndHeaders,
IsEndHeadersTest,
Combine(ValuesIn(ValidFrameTypes()),
Values(~Http2FrameFlag::END_HEADERS, 0xff)));
TEST_P(IsEndHeadersTest, IsEndHeaders) {
const bool is_set =
(flags_ & Http2FrameFlag::END_HEADERS) == Http2FrameFlag::END_HEADERS;
Http2String flags_string;
Http2FrameHeader v(0, type_, flags_, 0);
switch (type_) {
case Http2FrameType::HEADERS:
case Http2FrameType::PUSH_PROMISE:
case Http2FrameType::CONTINUATION:
EXPECT_EQ(is_set, v.IsEndHeaders()) << v;
flags_string = v.FlagsToString();
if (is_set) {
EXPECT_THAT(flags_string, MatchesRegex(".*\\|?END_HEADERS\\|.*"));
} else {
EXPECT_THAT(flags_string, Not(HasSubstr("END_HEADERS")));
}
v.RetainFlags(Http2FrameFlag::END_HEADERS);
EXPECT_EQ(is_set, v.IsEndHeaders()) << v;
{
std::stringstream s;
s << v;
EXPECT_EQ(v.ToString(), s.str());
if (is_set) {
EXPECT_THAT(s.str(), HasSubstr("flags=END_HEADERS,"));
} else {
EXPECT_THAT(s.str(), HasSubstr("flags=,"));
}
}
break;
default:
EXPECT_DEBUG_DEATH(v.IsEndHeaders(),
"HEADERS.*PUSH_PROMISE.*CONTINUATION")
<< v;
}
}
class IsPaddedTest : public Http2FrameHeaderTypeAndFlagTest {};
INSTANTIATE_TEST_CASE_P(IsPadded,
IsPaddedTest,
Combine(ValuesIn(ValidFrameTypes()),
Values(~Http2FrameFlag::PADDED, 0xff)));
TEST_P(IsPaddedTest, IsPadded) {
const bool is_set =
(flags_ & Http2FrameFlag::PADDED) == Http2FrameFlag::PADDED;
Http2String flags_string;
Http2FrameHeader v(0, type_, flags_, 0);
switch (type_) {
case Http2FrameType::DATA:
case Http2FrameType::HEADERS:
case Http2FrameType::PUSH_PROMISE:
EXPECT_EQ(is_set, v.IsPadded()) << v;
flags_string = v.FlagsToString();
if (is_set) {
EXPECT_THAT(flags_string, MatchesRegex(".*\\|?PADDED\\|.*"));
} else {
EXPECT_THAT(flags_string, Not(HasSubstr("PADDED")));
}
v.RetainFlags(Http2FrameFlag::PADDED);
EXPECT_EQ(is_set, v.IsPadded()) << v;
{
std::stringstream s;
s << v;
EXPECT_EQ(v.ToString(), s.str());
if (is_set) {
EXPECT_THAT(s.str(), HasSubstr("flags=PADDED,"));
} else {
EXPECT_THAT(s.str(), HasSubstr("flags=,"));
}
}
break;
default:
EXPECT_DEBUG_DEATH(v.IsPadded(), "DATA.*HEADERS.*PUSH_PROMISE") << v;
}
}
class HasPriorityTest : public Http2FrameHeaderTypeAndFlagTest {};
INSTANTIATE_TEST_CASE_P(HasPriority,
HasPriorityTest,
Combine(ValuesIn(ValidFrameTypes()),
Values(~Http2FrameFlag::PRIORITY, 0xff)));
TEST_P(HasPriorityTest, HasPriority) {
const bool is_set =
(flags_ & Http2FrameFlag::PRIORITY) == Http2FrameFlag::PRIORITY;
Http2String flags_string;
Http2FrameHeader v(0, type_, flags_, 0);
switch (type_) {
case Http2FrameType::HEADERS:
EXPECT_EQ(is_set, v.HasPriority()) << v;
flags_string = v.FlagsToString();
if (is_set) {
EXPECT_THAT(flags_string, MatchesRegex(".*\\|?PRIORITY\\|.*"));
} else {
EXPECT_THAT(flags_string, Not(HasSubstr("PRIORITY")));
}
v.RetainFlags(Http2FrameFlag::PRIORITY);
EXPECT_EQ(is_set, v.HasPriority()) << v;
{
std::stringstream s;
s << v;
EXPECT_EQ(v.ToString(), s.str());
if (is_set) {
EXPECT_THAT(s.str(), HasSubstr("flags=PRIORITY,"));
} else {
EXPECT_THAT(s.str(), HasSubstr("flags=,"));
}
}
break;
default:
EXPECT_DEBUG_DEATH(v.HasPriority(), "HEADERS") << v;
}
}
TEST(Http2PriorityFieldsTest, Constructor) {
Http2Random random;
uint32_t stream_dependency = random.Rand32() & StreamIdMask();
uint32_t weight = 1 + random.Rand8();
bool is_exclusive = random.OneIn(2);
Http2PriorityFields v(stream_dependency, weight, is_exclusive);
EXPECT_EQ(stream_dependency, v.stream_dependency);
EXPECT_EQ(weight, v.weight);
EXPECT_EQ(is_exclusive, v.is_exclusive);
// The high-bit must not be set on the stream id.
EXPECT_DEBUG_DEATH(
Http2PriorityFields(stream_dependency | 0x80000000, weight, is_exclusive),
"31-bit");
// The weight must be in the range 1-256.
EXPECT_DEBUG_DEATH(Http2PriorityFields(stream_dependency, 0, is_exclusive),
"too small");
EXPECT_DEBUG_DEATH(
Http2PriorityFields(stream_dependency, weight + 256, is_exclusive),
"too large");
}
#endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
TEST(Http2RstStreamFieldsTest, IsSupported) {
Http2RstStreamFields v{Http2ErrorCode::HTTP2_NO_ERROR};
EXPECT_TRUE(v.IsSupportedErrorCode()) << v;
Http2RstStreamFields u{static_cast<Http2ErrorCode>(~0)};
EXPECT_FALSE(u.IsSupportedErrorCode()) << v;
}
TEST(Http2SettingFieldsTest, Misc) {
Http2Random random;
Http2SettingsParameter parameter =
static_cast<Http2SettingsParameter>(random.Rand16());
uint32_t value = random.Rand32();
Http2SettingFields v(parameter, value);
EXPECT_EQ(v, v);
EXPECT_EQ(parameter, v.parameter);
EXPECT_EQ(value, v.value);
if (static_cast<uint16_t>(parameter) < 7) {
EXPECT_TRUE(v.IsSupportedParameter()) << v;
} else {
EXPECT_FALSE(v.IsSupportedParameter()) << v;
}
Http2SettingFields u(parameter, ~value);
EXPECT_NE(v, u);
EXPECT_EQ(v.parameter, u.parameter);
EXPECT_NE(v.value, u.value);
Http2SettingFields w(IncrementEnum(parameter), value);
EXPECT_NE(v, w);
EXPECT_NE(v.parameter, w.parameter);
EXPECT_EQ(v.value, w.value);
Http2SettingFields x(Http2SettingsParameter::MAX_FRAME_SIZE, 123);
std::stringstream s;
s << x;
EXPECT_EQ("parameter=MAX_FRAME_SIZE, value=123", s.str());
}
TEST(Http2PushPromiseTest, Misc) {
Http2Random random;
uint32_t promised_stream_id = random.Rand32() & StreamIdMask();
Http2PushPromiseFields v{promised_stream_id};
EXPECT_EQ(promised_stream_id, v.promised_stream_id);
EXPECT_EQ(v, v);
std::stringstream s;
s << v;
EXPECT_EQ(Http2StrCat("promised_stream_id=", promised_stream_id), s.str());
// High-bit is reserved, but not used, so we can set it.
promised_stream_id |= 0x80000000;
Http2PushPromiseFields w{promised_stream_id};
EXPECT_EQ(w, w);
EXPECT_NE(v, w);
v.promised_stream_id = promised_stream_id;
EXPECT_EQ(v, w);
}
TEST(Http2PingFieldsTest, Misc) {
Http2PingFields v{{'8', ' ', 'b', 'y', 't', 'e', 's', '\0'}};
std::stringstream s;
s << v;
EXPECT_EQ("opaque_bytes=0x3820627974657300", s.str());
}
TEST(Http2GoAwayFieldsTest, Misc) {
Http2Random random;
uint32_t last_stream_id = random.Rand32() & StreamIdMask();
Http2ErrorCode error_code = static_cast<Http2ErrorCode>(random.Rand32());
Http2GoAwayFields v(last_stream_id, error_code);
EXPECT_EQ(v, v);
EXPECT_EQ(last_stream_id, v.last_stream_id);
EXPECT_EQ(error_code, v.error_code);
if (static_cast<uint32_t>(error_code) < 14) {
EXPECT_TRUE(v.IsSupportedErrorCode()) << v;
} else {
EXPECT_FALSE(v.IsSupportedErrorCode()) << v;
}
Http2GoAwayFields u(~last_stream_id, error_code);
EXPECT_NE(v, u);
EXPECT_NE(v.last_stream_id, u.last_stream_id);
EXPECT_EQ(v.error_code, u.error_code);
}
TEST(Http2WindowUpdateTest, Misc) {
Http2Random random;
uint32_t window_size_increment = random.Rand32() & UInt31Mask();
Http2WindowUpdateFields v{window_size_increment};
EXPECT_EQ(window_size_increment, v.window_size_increment);
EXPECT_EQ(v, v);
std::stringstream s;
s << v;
EXPECT_EQ(Http2StrCat("window_size_increment=", window_size_increment),
s.str());
// High-bit is reserved, but not used, so we can set it.
window_size_increment |= 0x80000000;
Http2WindowUpdateFields w{window_size_increment};
EXPECT_EQ(w, w);
EXPECT_NE(v, w);
v.window_size_increment = window_size_increment;
EXPECT_EQ(v, w);
}
TEST(Http2AltSvcTest, Misc) {
Http2Random random;
uint16_t origin_length = random.Rand16();
Http2AltSvcFields v{origin_length};
EXPECT_EQ(origin_length, v.origin_length);
EXPECT_EQ(v, v);
std::stringstream s;
s << v;
EXPECT_EQ(Http2StrCat("origin_length=", origin_length), s.str());
Http2AltSvcFields w{++origin_length};
EXPECT_EQ(w, w);
EXPECT_NE(v, w);
v.origin_length = w.origin_length;
EXPECT_EQ(v, w);
}
} // namespace
} // namespace test
} // namespace net
|