Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
vasilvv | 82fa882 | 2022-05-06 05:41:24 -0700 | [diff] [blame] | 5 | #include "quiche/http2/test_tools/http2_structures_test_util.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 6 | |
| 7 | #include <cstdint> |
| 8 | |
| 9 | #include "quiche/http2/http2_constants.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 10 | #include "quiche/http2/http2_structures.h" |
vasilvv | 82fa882 | 2022-05-06 05:41:24 -0700 | [diff] [blame] | 11 | #include "quiche/http2/test_tools/http2_constants_test_util.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 12 | #include "quiche/http2/test_tools/http2_random.h" |
| 13 | |
| 14 | namespace http2 { |
| 15 | namespace test { |
| 16 | |
| 17 | void 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 | } |
| 23 | void 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 | } |
| 28 | void Randomize(Http2RstStreamFields* out, Http2Random* rng) { |
| 29 | out->error_code = static_cast<Http2ErrorCode>(rng->Rand32()); |
| 30 | } |
| 31 | void Randomize(Http2SettingFields* out, Http2Random* rng) { |
| 32 | out->parameter = static_cast<Http2SettingsParameter>(rng->Rand16()); |
| 33 | out->value = rng->Rand32(); |
| 34 | } |
| 35 | void Randomize(Http2PushPromiseFields* out, Http2Random* rng) { |
| 36 | out->promised_stream_id = rng->Rand32() & StreamIdMask(); |
| 37 | } |
| 38 | void Randomize(Http2PingFields* out, Http2Random* rng) { |
| 39 | for (int ndx = 0; ndx < 8; ++ndx) { |
| 40 | out->opaque_bytes[ndx] = rng->Rand8(); |
| 41 | } |
| 42 | } |
| 43 | void Randomize(Http2GoAwayFields* out, Http2Random* rng) { |
| 44 | out->last_stream_id = rng->Rand32() & StreamIdMask(); |
| 45 | out->error_code = static_cast<Http2ErrorCode>(rng->Rand32()); |
| 46 | } |
| 47 | void Randomize(Http2WindowUpdateFields* out, Http2Random* rng) { |
| 48 | out->window_size_increment = rng->Rand32() & 0x7fffffff; |
| 49 | } |
| 50 | void Randomize(Http2AltSvcFields* out, Http2Random* rng) { |
| 51 | out->origin_length = rng->Rand16(); |
| 52 | } |
| 53 | void Randomize(Http2PriorityUpdateFields* out, Http2Random* rng) { |
| 54 | out->prioritized_stream_id = rng->Rand32() & StreamIdMask(); |
| 55 | } |
| 56 | |
| 57 | void 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 | |
| 63 | bool 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 | |
| 74 | bool 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 | |
| 85 | bool 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 | |
| 100 | bool 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 |