blob: 09bf1eee5366ce3e0d6f5c1b1390fcd034e6ddcb [file] [log] [blame]
Bence Békybac04052022-04-07 15:44:29 -04001// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
vasilvv82fa8822022-05-06 05:41:24 -07005#include "quiche/http2/test_tools/http2_structures_test_util.h"
Bence Békybac04052022-04-07 15:44:29 -04006
7#include <cstdint>
8
9#include "quiche/http2/http2_constants.h"
Bence Békybac04052022-04-07 15:44:29 -040010#include "quiche/http2/http2_structures.h"
vasilvv82fa8822022-05-06 05:41:24 -070011#include "quiche/http2/test_tools/http2_constants_test_util.h"
Bence Békybac04052022-04-07 15:44:29 -040012#include "quiche/http2/test_tools/http2_random.h"
13
14namespace http2 {
15namespace test {
16
17void Randomize(Http2FrameHeader* out, Http2Random* rng) {
18 out->payload_length = rng->Rand32() & 0xffffff;
19 out->type = static_cast<Http2FrameType>(rng->Rand8());
20 out->flags = static_cast<Http2FrameFlag>(rng->Rand8());
21 out->stream_id = rng->Rand32() & StreamIdMask();
22}
23void Randomize(Http2PriorityFields* out, Http2Random* rng) {
24 out->stream_dependency = rng->Rand32() & StreamIdMask();
25 out->weight = rng->Rand8() + 1;
26 out->is_exclusive = rng->OneIn(2);
27}
28void Randomize(Http2RstStreamFields* out, Http2Random* rng) {
29 out->error_code = static_cast<Http2ErrorCode>(rng->Rand32());
30}
31void Randomize(Http2SettingFields* out, Http2Random* rng) {
32 out->parameter = static_cast<Http2SettingsParameter>(rng->Rand16());
33 out->value = rng->Rand32();
34}
35void Randomize(Http2PushPromiseFields* out, Http2Random* rng) {
36 out->promised_stream_id = rng->Rand32() & StreamIdMask();
37}
38void Randomize(Http2PingFields* out, Http2Random* rng) {
39 for (int ndx = 0; ndx < 8; ++ndx) {
40 out->opaque_bytes[ndx] = rng->Rand8();
41 }
42}
43void Randomize(Http2GoAwayFields* out, Http2Random* rng) {
44 out->last_stream_id = rng->Rand32() & StreamIdMask();
45 out->error_code = static_cast<Http2ErrorCode>(rng->Rand32());
46}
47void Randomize(Http2WindowUpdateFields* out, Http2Random* rng) {
48 out->window_size_increment = rng->Rand32() & 0x7fffffff;
49}
50void Randomize(Http2AltSvcFields* out, Http2Random* rng) {
51 out->origin_length = rng->Rand16();
52}
53void Randomize(Http2PriorityUpdateFields* out, Http2Random* rng) {
54 out->prioritized_stream_id = rng->Rand32() & StreamIdMask();
55}
56
57void ScrubFlagsOfHeader(Http2FrameHeader* header) {
58 uint8_t invalid_mask = InvalidFlagMaskForFrameType(header->type);
59 uint8_t keep_mask = ~invalid_mask;
60 header->RetainFlags(keep_mask);
61}
62
63bool FrameIsPadded(const Http2FrameHeader& header) {
64 switch (header.type) {
65 case Http2FrameType::DATA:
66 case Http2FrameType::HEADERS:
67 case Http2FrameType::PUSH_PROMISE:
68 return header.IsPadded();
69 default:
70 return false;
71 }
72}
73
74bool FrameHasPriority(const Http2FrameHeader& header) {
75 switch (header.type) {
76 case Http2FrameType::HEADERS:
77 return header.HasPriority();
78 case Http2FrameType::PRIORITY:
79 return true;
80 default:
81 return false;
82 }
83}
84
85bool FrameCanHavePayload(const Http2FrameHeader& header) {
86 switch (header.type) {
87 case Http2FrameType::DATA:
88 case Http2FrameType::HEADERS:
89 case Http2FrameType::PUSH_PROMISE:
90 case Http2FrameType::CONTINUATION:
91 case Http2FrameType::PING:
92 case Http2FrameType::GOAWAY:
93 case Http2FrameType::ALTSVC:
94 return true;
95 default:
96 return false;
97 }
98}
99
100bool FrameCanHaveHpackPayload(const Http2FrameHeader& header) {
101 switch (header.type) {
102 case Http2FrameType::HEADERS:
103 case Http2FrameType::PUSH_PROMISE:
104 case Http2FrameType::CONTINUATION:
105 return true;
106 default:
107 return false;
108 }
109}
110
111} // namespace test
112} // namespace http2