File: test_Http3Frame.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (268 lines) | stat: -rw-r--r-- 8,226 bytes parent folder | download | duplicates (2)
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
/** @file
 *
 *  A brief file description
 *
 *  @section license License
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include "catch.hpp"
#include <cstdio>
#include "Http3Frame.h"
#include "Http3FrameDispatcher.h"

TEST_CASE("Http3Frame Type", "[http3]")
{
  CHECK(Http3Frame::type(reinterpret_cast<const uint8_t *>("\x00\x00"), 2) == Http3FrameType::DATA);
  // Undefined range
  CHECK(Http3Frame::type(reinterpret_cast<const uint8_t *>("\x0f\x00"), 2) == Http3FrameType::UNKNOWN);
  CHECK(Http3Frame::type(reinterpret_cast<const uint8_t *>("\xff\xff\xff\xff\xff\xff\xff\x00"), 9) == Http3FrameType::UNKNOWN);
}

TEST_CASE("Load DATA Frame", "[http3]")
{
  SECTION("No flags")
  {
    uint8_t buf1[] = {
      0x00,                   // Type
      0x04,                   // Length
      0x11, 0x22, 0x33, 0x44, // Payload
    };
    std::shared_ptr<const Http3Frame> frame1 = Http3FrameFactory::create(buf1, sizeof(buf1));
    CHECK(frame1->type() == Http3FrameType::DATA);
    CHECK(frame1->length() == 4);

    std::shared_ptr<const Http3DataFrame> data_frame = std::dynamic_pointer_cast<const Http3DataFrame>(frame1);
    CHECK(data_frame);
    CHECK(data_frame->payload_length() == 4);
    CHECK(memcmp(data_frame->payload(), "\x11\x22\x33\x44", 4) == 0);
  }

  SECTION("Have flags (invalid)")
  {
    uint8_t buf1[] = {
      0x00,                   // Type
      0x04,                   // Length
      0x11, 0x22, 0x33, 0x44, // Payload
    };
    std::shared_ptr<const Http3Frame> frame1 = Http3FrameFactory::create(buf1, sizeof(buf1));
    CHECK(frame1->type() == Http3FrameType::DATA);
    CHECK(frame1->length() == 4);

    std::shared_ptr<const Http3DataFrame> data_frame = std::dynamic_pointer_cast<const Http3DataFrame>(frame1);
    CHECK(data_frame);
    CHECK(data_frame->payload_length() == 4);
    CHECK(memcmp(data_frame->payload(), "\x11\x22\x33\x44", 4) == 0);
  }
}

TEST_CASE("Store DATA Frame", "[http3]")
{
  SECTION("Normal")
  {
    uint8_t buf[32] = {0};
    size_t len;
    uint8_t expected1[] = {
      0x00,                   // Type
      0x04,                   // Length
      0x11, 0x22, 0x33, 0x44, // Payload
    };

    uint8_t raw1[]          = "\x11\x22\x33\x44";
    ats_unique_buf payload1 = ats_unique_malloc(4);
    memcpy(payload1.get(), raw1, 4);

    Http3DataFrame data_frame(std::move(payload1), 4);
    CHECK(data_frame.length() == 4);

    auto ibb = data_frame.to_io_buffer_block();
    IOBufferReader reader;
    reader.block = ibb.get();
    len          = reader.read_avail();
    reader.read(buf, sizeof(buf));
    CHECK(len == 6);
    CHECK(memcmp(buf, expected1, len) == 0);
  }
}

TEST_CASE("Store HEADERS Frame", "[http3]")
{
  SECTION("Normal")
  {
    uint8_t buf[32] = {0};
    size_t len;
    uint8_t expected1[] = {
      0x01,                   // Type
      0x04,                   // Length
      0x11, 0x22, 0x33, 0x44, // Payload
    };

    uint8_t raw1[]              = "\x11\x22\x33\x44";
    ats_unique_buf header_block = ats_unique_malloc(4);
    memcpy(header_block.get(), raw1, 4);

    Http3HeadersFrame hdrs_frame(std::move(header_block), 4);
    CHECK(hdrs_frame.length() == 4);

    auto ibb = hdrs_frame.to_io_buffer_block();
    IOBufferReader reader;
    reader.block = ibb.get();
    len          = reader.read_avail();
    reader.read(buf, sizeof(buf));
    CHECK(len == 6);
    CHECK(memcmp(buf, expected1, len) == 0);
  }
}

TEST_CASE("Load SETTINGS Frame", "[http3]")
{
  SECTION("Normal")
  {
    uint8_t buf[] = {
      0x04,       // Type
      0x08,       // Length
      0x06,       // Identifier
      0x44, 0x00, // Value
      0x09,       // Identifier
      0x0f,       // Value
      0x4a, 0xba, // Identifier
      0x00,       // Value
    };

    std::shared_ptr<const Http3Frame> frame = Http3FrameFactory::create(buf, sizeof(buf));
    CHECK(frame->type() == Http3FrameType::SETTINGS);
    CHECK(frame->length() == sizeof(buf) - 2);

    std::shared_ptr<const Http3SettingsFrame> settings_frame = std::dynamic_pointer_cast<const Http3SettingsFrame>(frame);
    CHECK(settings_frame);
    CHECK(settings_frame->is_valid());
    CHECK(settings_frame->get(Http3SettingsId::MAX_HEADER_LIST_SIZE) == 0x0400);
    CHECK(settings_frame->get(Http3SettingsId::NUM_PLACEHOLDERS) == 0x0f);
  }
}

TEST_CASE("Store SETTINGS Frame", "[http3]")
{
  SECTION("Normal")
  {
    uint8_t expected[] = {
      0x04,       // Type
      0x08,       // Length
      0x06,       // Identifier
      0x44, 0x00, // Value
      0x09,       // Identifier
      0x0f,       // Value
      0x4a, 0x0a, // Identifier
      0x00,       // Value
    };

    Http3SettingsFrame settings_frame;
    settings_frame.set(Http3SettingsId::MAX_HEADER_LIST_SIZE, 0x0400);
    settings_frame.set(Http3SettingsId::NUM_PLACEHOLDERS, 0x0f);

    uint8_t buf[32] = {0};
    size_t len;
    auto ibb = settings_frame.to_io_buffer_block();
    IOBufferReader reader;
    reader.block = ibb.get();
    len          = reader.read_avail();
    reader.read(buf, sizeof(buf));
    CHECK(len == sizeof(expected));
    CHECK(memcmp(buf, expected, len) == 0);
  }

  SECTION("Normal from Client")
  {
    uint8_t expected[] = {
      0x04,       // Type
      0x06,       // Length
      0x06,       // Identifier
      0x44, 0x00, // Value
      0x4a, 0x0a, // Identifier
      0x00,       // Value
    };

    Http3SettingsFrame settings_frame;
    settings_frame.set(Http3SettingsId::MAX_HEADER_LIST_SIZE, 0x0400);

    uint8_t buf[32] = {0};
    size_t len;
    auto ibb = settings_frame.to_io_buffer_block();
    IOBufferReader reader;
    reader.block = ibb.get();
    len          = reader.read_avail();
    reader.read(buf, sizeof(buf));
    CHECK(len == sizeof(expected));
    CHECK(memcmp(buf, expected, len) == 0);
  }
}

TEST_CASE("Http3FrameFactory Create Unknown Frame", "[http3]")
{
  uint8_t buf1[] = {
    0x0f, // Type
    0x00, // Length
  };
  std::shared_ptr<const Http3Frame> frame1 = Http3FrameFactory::create(buf1, sizeof(buf1));
  CHECK(frame1);
  CHECK(frame1->type() == Http3FrameType::UNKNOWN);
  CHECK(frame1->length() == 0);
}

TEST_CASE("Http3FrameFactory Fast Create Frame", "[http3]")
{
  Http3FrameFactory factory;

  uint8_t buf1[] = {
    0x00,                   // Type
    0x04,                   // Length
    0x11, 0x22, 0x33, 0x44, // Payload
  };
  uint8_t buf2[] = {
    0x00,                   // Type
    0x04,                   // Length
    0xaa, 0xbb, 0xcc, 0xdd, // Payload
  };
  std::shared_ptr<const Http3Frame> frame1 = factory.fast_create(buf1, sizeof(buf1));
  CHECK(frame1 != nullptr);

  std::shared_ptr<const Http3DataFrame> data_frame1 = std::dynamic_pointer_cast<const Http3DataFrame>(frame1);
  CHECK(data_frame1 != nullptr);
  CHECK(memcmp(data_frame1->payload(), buf1 + 2, 4) == 0);

  std::shared_ptr<const Http3Frame> frame2 = factory.fast_create(buf2, sizeof(buf2));
  CHECK(frame2 != nullptr);

  std::shared_ptr<const Http3DataFrame> data_frame2 = std::dynamic_pointer_cast<const Http3DataFrame>(frame2);
  CHECK(data_frame2 != nullptr);
  CHECK(memcmp(data_frame2->payload(), buf2 + 2, 4) == 0);

  CHECK(frame1 == frame2);
}

TEST_CASE("Http3FrameFactory Fast Create Unknown Frame", "[http3]")
{
  Http3FrameFactory factory;

  uint8_t buf1[] = {
    0x0f, // Type
  };
  std::shared_ptr<const Http3Frame> frame1 = factory.fast_create(buf1, sizeof(buf1));
  CHECK(frame1);
  CHECK(frame1->type() == Http3FrameType::UNKNOWN);
}