Debian Codegraph @

crt/aws-c-http/tests/test_h1_decoder.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/http/private/h1_decoder.h>
7
8 #include <aws/common/array_list.h>
9 #include <aws/io/logging.h>
10 #include <aws/testing/aws_test_harness.h>
11
12 #include <ctype.h>
13 #include <stdio.h>
14
15 static const struct aws_byte_cursor s_typical_request = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
16 "Host: amazon.com\r\n"
17 "Accept-Language: fr\r\n"
18 "\r\n");
19
20 static const struct aws_byte_cursor s_typical_response = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 200 OK\r\n"
21 "Server: some-server\r\n"
22 "Content-Length: 11\r\n"
23 "\r\n"
24 "Hello noob.");
25
26 static const bool s_request = true;
27 static const bool s_response = false;
28
29 static struct aws_logger s_logger;
30
31 static int s_on_header_stub(const struct aws_h1_decoded_header *header, void *user_data) {
32 (void)header;
33 (void)user_data;
34 return AWS_OP_SUCCESS;
35 }
36
37 static int s_on_body_stub(const struct aws_byte_cursor *data, bool finished, void *user_data) {
38 (void)data;
39 (void)finished;
40 (void)user_data;
41 return AWS_OP_SUCCESS;
42 }
43
44 static int s_on_response(int code, void *user_data) {
45 int *ptr = (int *)user_data;
46 if (ptr) {
47 *ptr = code;
48 }
49 return AWS_OP_SUCCESS;
50 }
51
52 static int s_on_response_stub(int code, void *user_data) {
53 (void)code;
54 (void)user_data;
55 return AWS_OP_SUCCESS;
56 }
57
58 struct request_data {
59 enum aws_http_method method_enum;
60 struct aws_byte_cursor method_str;
61 struct aws_byte_cursor uri;
62 uint8_t buffer[1024];
63 };
64
65 static int s_on_request(
66 enum aws_http_method method_enum,
67 const struct aws_byte_cursor *method_str,
68 const struct aws_byte_cursor *uri,
69 void *user_data) {
70
71 struct request_data *request_data = (struct request_data *)user_data;
72 AWS_ASSERT(sizeof(request_data->buffer) >= uri->len + method_str->len);
73 if (request_data) {
74 request_data->method_enum = method_enum;
75
76 memcpy(request_data->buffer, method_str->ptr, method_str->len);
77 request_data->method_str = aws_byte_cursor_from_array(request_data->buffer, method_str->len);
78
79 uint8_t *uri_dst = request_data->buffer + method_str->len;
80 memcpy(uri_dst, uri->ptr, uri->len);
81 request_data->uri = aws_byte_cursor_from_array(uri_dst, uri->len);
82 }
83
84 return AWS_OP_SUCCESS;
85 }
86
87 static int s_on_request_stub(
88 enum aws_http_method method_enum,
89 const struct aws_byte_cursor *method_str,
90 const struct aws_byte_cursor *uri,
91 void *user_data) {
92
93 (void)method_enum;
94 (void)method_str;
95 (void)uri;
96 (void)user_data;
97 return AWS_OP_SUCCESS;
98 }
99
100 static int s_on_done(void *user_data) {
101 (void)user_data;
102 return AWS_OP_SUCCESS;
103 }
104
105 static void s_test_init(struct aws_allocator *allocator) {
106 aws_http_library_init(allocator);
107
108 struct aws_logger_standard_options logger_options = {
109 .level = AWS_LOG_LEVEL_TRACE,
110 .file = stderr,
111 };
112
113 aws_logger_init_standard(&s_logger, allocator, &logger_options);
114 aws_logger_set(&s_logger);
115 }
116
117 static void s_test_clean_up(void) {
118 aws_http_library_clean_up();
119 aws_logger_clean_up(&s_logger);
120 }
121
122 static void s_common_decoder_setup(
123 struct aws_allocator *allocator,
124 size_t scratch_space_size,
125 struct aws_h1_decoder_params *params,
126 bool type,
127 void *user_data) {
128
129 params->alloc = allocator;
130 params->scratch_space_initial_size = scratch_space_size;
131 params->is_decoding_requests = type;
132 params->user_data = user_data;
133 params->vtable.on_header = s_on_header_stub;
134 params->vtable.on_body = s_on_body_stub;
135 params->vtable.on_request = s_on_request_stub;
136 params->vtable.on_response = s_on_response_stub;
137 params->vtable.on_done = s_on_done;
138 }
139
140 AWS_TEST_CASE(h1_decoder_get_request, s_test_h1_decoder_get_request);
141 static int s_test_h1_decoder_get_request(struct aws_allocator *allocator, void *ctx) {
142 (void)ctx;
143 s_test_init(allocator);
144
145 struct request_data request_data;
146
147 struct aws_byte_cursor msg = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HEAD / HTTP/1.1\r\n\r\n");
148
149 struct aws_h1_decoder_params params;
150 s_common_decoder_setup(allocator, 1024, &params, s_request, &request_data);
151 params.vtable.on_request = s_on_request;
152 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
153
154 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
155 ASSERT_INT_EQUALS(AWS_HTTP_METHOD_HEAD, request_data.method_enum);
156
157 ASSERT_TRUE(aws_byte_cursor_eq(&request_data.method_str, &aws_http_method_head));
158
159 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request_data.uri, "/"));
160
161 aws_h1_decoder_destroy(decoder);
162 s_test_clean_up();
163 return AWS_OP_SUCCESS;
164 }
165
166 AWS_TEST_CASE(h1_decoder_request_bad_version, s_test_h1_decoder_request_bad_version);
167 static int s_test_h1_decoder_request_bad_version(struct aws_allocator *allocator, void *ctx) {
168 (void)ctx;
169 s_test_init(allocator);
170 struct aws_byte_cursor msg =
171 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.0\r\n\r\n"); /* Note version is 1.0 */
172
173 struct aws_h1_decoder_params params;
174 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
175 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
176
177 ASSERT_FAILS(aws_h1_decode(decoder, &msg));
178
179 aws_h1_decoder_destroy(decoder);
180 s_test_clean_up();
181 return AWS_OP_SUCCESS;
182 }
183
184 AWS_TEST_CASE(h1_decoder_response_1_0, s_test_h1_decoder_response_1_0);
185 static int s_test_h1_decoder_response_1_0(struct aws_allocator *allocator, void *ctx) {
186 (void)ctx;
187 s_test_init(allocator);
188 int code = 0;
189
190 struct aws_byte_cursor msg =
191 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.0 200 OK\r\n\r\n"); /* Note version is "1.0" */
192
193 struct aws_h1_decoder_params params;
194 s_common_decoder_setup(allocator, 1024, &params, s_response, &code);
195 params.vtable.on_response = s_on_response;
196 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
197
198 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
199 ASSERT_INT_EQUALS(200, code);
200
201 aws_h1_decoder_destroy(decoder);
202 s_test_clean_up();
203 return AWS_OP_SUCCESS;
204 }
205
206 AWS_TEST_CASE(h1_decoder_response_unsupported_version, s_test_h1_decoder_response_unsupported_version);
207 static int s_test_h1_decoder_response_unsupported_version(struct aws_allocator *allocator, void *ctx) {
208 (void)ctx;
209 s_test_init(allocator);
210 struct aws_byte_cursor msg =
211 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.2 200 OK\r\n\r\n"); /* Note version is "1.0" */
212
213 struct aws_h1_decoder_params params;
214 s_common_decoder_setup(allocator, 1024, &params, s_response, NULL);
215 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
216
217 ASSERT_FAILS(aws_h1_decode(decoder, &msg));
218
219 aws_h1_decoder_destroy(decoder);
220 s_test_clean_up();
221 return AWS_OP_SUCCESS;
222 }
223
224 AWS_TEST_CASE(h1_decoder_get_status_code, s_test_h1_decoder_get_status_code);
225 static int s_test_h1_decoder_get_status_code(struct aws_allocator *allocator, void *ctx) {
226 (void)ctx;
227 s_test_init(allocator);
228 int code;
229
230 struct aws_byte_cursor msg = s_typical_response;
231 struct aws_h1_decoder_params params;
232 s_common_decoder_setup(allocator, 1024, &params, s_response, &code);
233 params.vtable.on_response = s_on_response;
234 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
235
236 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
237 ASSERT_INT_EQUALS(200, code);
238
239 aws_h1_decoder_destroy(decoder);
240 s_test_clean_up();
241 return AWS_OP_SUCCESS;
242 }
243
244 AWS_TEST_CASE(h1_decoder_overflow_scratch_space, s_test_h1_decoder_overflow_scratch_space);
245 static int s_test_h1_decoder_overflow_scratch_space(struct aws_allocator *allocator, void *ctx) {
246 (void)ctx;
247 s_test_init(allocator);
248
249 struct aws_byte_cursor msg = s_typical_response;
250 struct aws_h1_decoder_params params;
251 s_common_decoder_setup(allocator, 4, &params, s_response, NULL);
252 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
253
254 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
255
256 aws_h1_decoder_destroy(decoder);
257 s_test_clean_up();
258 return AWS_OP_SUCCESS;
259 }
260
261 struct s_header_params {
262 int index;
263 int max_index;
264 int first_error;
265 const char **header_names;
266 };
267
268 static int s_got_header(const struct aws_h1_decoded_header *header, void *user_data) {
269 struct s_header_params *params = (struct s_header_params *)user_data;
270 if (params->index < params->max_index) {
271 if (params->first_error == AWS_OP_SUCCESS) {
272 if (!aws_byte_cursor_eq_c_str(&header->name_data, params->header_names[params->index])) {
273 params->first_error = AWS_OP_ERR;
274 }
275 }
276 params->index++;
277 } else {
278 return aws_raise_error(AWS_ERROR_UNKNOWN);
279 }
280
281 return AWS_OP_SUCCESS;
282 }
283
284 AWS_TEST_CASE(h1_decoder_receive_request_headers, s_test_h1_decoder_receive_request_headers);
285 static int s_test_h1_decoder_receive_request_headers(struct aws_allocator *allocator, void *ctx) {
286 (void)ctx;
287 s_test_init(allocator);
288 struct aws_byte_cursor msg = s_typical_request;
289 struct aws_h1_decoder_params params;
290 struct s_header_params header_params;
291 s_common_decoder_setup(allocator, 1024, &params, s_request, &header_params);
292
293 const char *header_names[] = {"Host", "Accept-Language"};
294 header_params.index = 0;
295 header_params.max_index = AWS_ARRAY_SIZE(header_names);
296 header_params.first_error = AWS_OP_SUCCESS;
297 header_params.header_names = header_names;
298
299 params.vtable.on_header = s_got_header;
300 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
301
302 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
303 ASSERT_SUCCESS(header_params.first_error);
304
305 aws_h1_decoder_destroy(decoder);
306 s_test_clean_up();
307 return AWS_OP_SUCCESS;
308 }
309
310 AWS_TEST_CASE(h1_decoder_receive_response_headers, s_test_h1_decoder_receive_response_headers);
311 static int s_test_h1_decoder_receive_response_headers(struct aws_allocator *allocator, void *ctx) {
312 (void)ctx;
313 s_test_init(allocator);
314 struct aws_byte_cursor msg = s_typical_response;
315 struct aws_h1_decoder_params params;
316 struct s_header_params header_params;
317 s_common_decoder_setup(allocator, 1024, &params, s_response, &header_params);
318
319 const char *header_names[] = {"Server", "Content-Length"};
320 header_params.index = 0;
321 header_params.max_index = AWS_ARRAY_SIZE(header_names);
322 header_params.first_error = AWS_OP_SUCCESS;
323 header_params.header_names = header_names;
324
325 params.vtable.on_header = s_got_header;
326 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
327
328 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
329 ASSERT_SUCCESS(header_params.first_error);
330
331 aws_h1_decoder_destroy(decoder);
332 s_test_clean_up();
333 return AWS_OP_SUCCESS;
334 }
335
336 AWS_TEST_CASE(h1_decoder_get_transfer_encoding_flags, s_test_h1_decoder_get_transfer_encoding_flags);
337 static int s_test_h1_decoder_get_transfer_encoding_flags(struct aws_allocator *allocator, void *ctx) {
338 (void)ctx;
339 s_test_init(allocator);
340 struct aws_byte_cursor msg = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 200 OK\r\n"
341 "Server: some-server\r\n"
342 "Transfer-Encoding: compress\r\n"
343 "Transfer-Encoding: gzip,deflate\r\n"
344 "Transfer-Encoding: chunked\r\n"
345 "\r\n");
346 struct aws_h1_decoder_params params;
347 s_common_decoder_setup(allocator, 1024, &params, s_response, NULL);
348 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
349
350 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
351 int flags = aws_h1_decoder_get_encoding_flags(decoder);
352 ASSERT_INT_EQUALS(
353 (AWS_HTTP_TRANSFER_ENCODING_CHUNKED | AWS_HTTP_TRANSFER_ENCODING_GZIP | AWS_HTTP_TRANSFER_ENCODING_DEFLATE |
354 AWS_HTTP_TRANSFER_ENCODING_DEPRECATED_COMPRESS),
355 flags);
356
357 aws_h1_decoder_destroy(decoder);
358 s_test_clean_up();
359 return AWS_OP_SUCCESS;
360 }
361
362 struct s_body_params {
363 struct aws_array_list body_data;
364 };
365
366 static int s_on_body(const struct aws_byte_cursor *data, bool finished, void *user_data) {
367 (void)finished;
368
369 struct s_body_params *params = (struct s_body_params *)user_data;
370 for (int i = 0; i < (int)data->len; ++i) {
371 aws_array_list_push_back(&params->body_data, data->ptr + i);
372 }
373
374 return AWS_OP_SUCCESS;
375 }
376
377 AWS_TEST_CASE(h1_decoder_body_unchunked, s_test_h1_decoder_body_unchunked);
378 static int s_test_h1_decoder_body_unchunked(struct aws_allocator *allocator, void *ctx) {
379 (void)ctx;
380 s_test_init(allocator);
381 struct aws_byte_cursor msg = s_typical_response;
382 struct aws_h1_decoder_params params;
383 struct s_body_params body_params;
384 s_common_decoder_setup(allocator, 1024, &params, s_response, NULL);
385
386 aws_array_list_init_dynamic(&body_params.body_data, allocator, 256, sizeof(uint8_t));
387
388 params.alloc = allocator;
389 params.scratch_space_initial_size = 1024;
390 params.vtable.on_header = s_on_header_stub;
391 params.vtable.on_body = s_on_body;
392 params.is_decoding_requests = false;
393 params.user_data = &body_params;
394 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
395
396 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
397 ASSERT_SUCCESS(memcmp(body_params.body_data.data, "Hello noob.", body_params.body_data.length));
398
399 aws_h1_decoder_destroy(decoder);
400 aws_array_list_clean_up(&body_params.body_data);
401 s_test_clean_up();
402 return AWS_OP_SUCCESS;
403 }
404
405 AWS_TEST_CASE(h1_decoder_body_chunked, s_test_h1_decoder_body_chunked);
406 static int s_test_h1_decoder_body_chunked(struct aws_allocator *allocator, void *ctx) {
407 (void)ctx;
408 s_test_init(allocator);
409 struct aws_byte_cursor msg = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
410 "Host: amazon.com\r\n"
411 "Transfer-Encoding: chunked\r\n"
412 "\r\n"
413 "D\r\n"
414 "Hello, there \r\n"
415 "1c\r\n"
416 "should be a carriage return \r\n"
417 "9\r\n"
418 "in\r\nhere.\r\n"
419 "0\r\n"
420 "\r\n");
421
422 struct aws_h1_decoder_params params;
423 struct s_body_params body_params;
424 s_common_decoder_setup(allocator, 1024, &params, s_request, &body_params);
425
426 aws_array_list_init_dynamic(&body_params.body_data, allocator, 256, sizeof(uint8_t));
427
428 params.vtable.on_body = s_on_body;
429 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
430
431 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
432 ASSERT_SUCCESS(memcmp(
433 body_params.body_data.data,
434 "Hello, there should be a carriage return in\r\nhere.",
435 body_params.body_data.length));
436
437 aws_h1_decoder_destroy(decoder);
438 aws_array_list_clean_up(&body_params.body_data);
439 s_test_clean_up();
440 return AWS_OP_SUCCESS;
441 }
442
443 AWS_TEST_CASE(h1_decoder_trailers, s_h1_decoder_trailers);
444 static int s_h1_decoder_trailers(struct aws_allocator *allocator, void *ctx) {
445 (void)ctx;
446 s_test_init(allocator);
447 struct aws_byte_cursor msg = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
448 "Host: amazon.com\r\n"
449 "Accept-Language: fr\r\n"
450 "Transfer-Encoding: chunked \r\n"
451 "Trailer: Expires\r\n"
452 "\r\n"
453 "7\r\n"
454 "Mozilla\r\n"
455 "9\r\n"
456 "Developer\r\n"
457 "7\r\n"
458 "Network\r\n"
459 "0\r\n"
460 "Expires: Wed, 21 Oct 2015 07:28:00 GMT\r\n"
461 "\r\n");
462
463 struct aws_h1_decoder_params params;
464 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
465 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
466
467 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
468
469 aws_h1_decoder_destroy(decoder);
470 s_test_clean_up();
471 return AWS_OP_SUCCESS;
472 }
473
474 AWS_TEST_CASE(h1_decoder_one_byte_at_a_time, s_h1_decoder_one_byte_at_a_time);
475 static int s_h1_decoder_one_byte_at_a_time(struct aws_allocator *allocator, void *ctx) {
476 (void)ctx;
477 s_test_init(allocator);
478 struct aws_byte_cursor msg = s_typical_request;
479
480 struct aws_h1_decoder_params params;
481 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
482 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
483
484 for (size_t i = 0; i < msg.len; ++i) {
485 struct aws_byte_cursor chunk = aws_byte_cursor_advance(&msg, 1);
486 ASSERT_SUCCESS(aws_h1_decode(decoder, &chunk));
487 }
488
489 aws_h1_decoder_destroy(decoder);
490 s_test_clean_up();
491 return AWS_OP_SUCCESS;
492 }
493
494 static int s_rand(int lo, int hi) {
495 return rand() % (hi + 1 - lo) + lo;
496 }
497
498 AWS_TEST_CASE(h1_decoder_messages_at_random_intervals, s_h1_decoder_messages_at_random_intervals);
499 static int s_h1_decoder_messages_at_random_intervals(struct aws_allocator *allocator, void *ctx) {
500 (void)ctx;
501 s_test_init(allocator);
502 const struct aws_byte_cursor requests[] = {
503 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
504 "Host: amazon.com\r\n"
505 "Accept-Language: fr\r\n"
506 "Content-Length: 6\r\n"
507 "\r\n"
508 "123456"),
509
510 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CONNECT server.example.com:80 HTTP/1.1\r\n"
511 "Host: server.example.com:80\r\n"
512 "Proxy-Authorization: basic aGVsbG86d29ybGQ=\r\n"),
513
514 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DELETE /file.html HTTP/1.1\r\n"),
515
516 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HEAD /index.html HTTP/1.1\r\n"),
517
518 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("OPTIONS /index.html HTTP/1.1\r\n"),
519
520 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("OPTIONS * HTTP/1.1\r\n"),
521
522 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PATCH /file.txt HTTP/1.1\r\n"
523 "Host: www.example.com\r\n"
524 "Content-Type: application/example\r\n"
525 "If-Match: \"e0023aa4e\"\r\n"
526 "Content-Length: 10\r\n"
527 "\r\n"
528 "0123456789"),
529
530 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
531 "Host: foo.com\r\n"
532 "Content-Type: application/x-www-form-urlencoded\r\n"
533 "Content-Length: 13\r\n"
534 "\r\n"
535 "say=Hi&to=Mom"),
536
537 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PUT /new.html HTTP/1.1\r\n"
538 "Host: example.com\r\n"
539 "Content-type: text/html\r\n"
540 "Content-length: 16\r\n"
541 "\r\n"
542 "<p>New File</p>"),
543
544 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TRACE /index.html HTTP/1.1\r\n"),
545
546 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET /home.html HTTP/1.1\r\n"
547 "Host: example.com\r\n"
548 "a-fake-header: oh what is this odd whitespace \r\n"
549 "Content-Length: 1\r\n"
550 "\r\n"
551 "X"),
552 };
553
554 /* Just seed something for determinism. */
555 srand(1);
556
557 for (size_t iter = 0; iter < AWS_ARRAY_SIZE(requests); ++iter) {
558 struct aws_byte_cursor request = requests[iter];
559
560 struct aws_h1_decoder_params params;
561 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
562 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
563
564 /* Decode message at randomized input buffer sizes from 0 to 10 bytes. */
565 while (request.len) {
566 int lo = 1;
567 int hi = 10;
568 if (hi > (int)request.len) {
569 hi = (int)request.len;
570 }
571 int interval = s_rand(lo, hi);
572 struct aws_byte_cursor chunk = aws_byte_cursor_advance(&request, interval);
573 ASSERT_SUCCESS(aws_h1_decode(decoder, &chunk));
574 }
575
576 aws_h1_decoder_destroy(decoder);
577 }
578
579 s_test_clean_up();
580 return AWS_OP_SUCCESS;
581 }
582
583 AWS_TEST_CASE(h1_decoder_bad_requests_and_assert_failure, s_h1_decoder_bad_requests_and_assert_failure);
584 static int s_h1_decoder_bad_requests_and_assert_failure(struct aws_allocator *allocator, void *ctx) {
585 (void)ctx;
586 s_test_init(allocator);
587 const struct aws_byte_cursor requests[] = {
588 /* Incorrect chunk size. */
589 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
590 "Transfer-Encoding: chunked\r\n"
591 "\r\n"
592 "7\r\n"
593 "Mozilla\r\n"
594 "2\r\n" /* Incorrect chunk size here. */
595 "Developer\r\n"
596 "7\r\n"
597 "Network\r\n"
598 "0\r\n"
599 "\r\n"),
600
601 /* Chunked should be final encoding */
602 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
603 "Transfer-Encoding: chunked, gzip\r\n"
604 "\r\n"),
605
606 /* Chunked should be final encoding, p2 */
607 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
608 "Transfer-Encoding: chunked\r\n"
609 "Transfer-Encoding: gzip\r\n"
610 "\r\n"),
611
612 /* A sender MUST NOT apply chunked more than once to a message body */
613 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
614 "Transfer-Encoding: chunked, chunked\r\n"
615 "\r\n"),
616
617 /* A sender MUST NOT apply chunked more than once to a message body, p2 */
618 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
619 "Transfer-Encoding: chunked,\r\n"
620 "Transfer-Encoding: chunked\r\n"
621 "\r\n"),
622
623 /* Invalid hex-int as chunk size. */
624 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
625 "Transfer-Encoding: chunked\r\n"
626 "\r\n"
627 "7\r\n"
628 "Mozilla\r\n"
629 "S\r\n" /* Incorrect chunk size here. */
630 "Developer\r\n"
631 "7\r\n"
632 "Network\r\n"
633 "0\r\n"
634 "\r\n"),
635
636 /* Chunk size should not have spaces. */
637 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
638 "Transfer-Encoding: chunked\r\n"
639 "\r\n"
640 " 7 \r\n"),
641
642 /* Chunk size should not start with "0x". */
643 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
644 "Transfer-Encoding: chunked\r\n"
645 "\r\n"
646 "0x7\r\n"),
647
648 /* Invalid chunk size terminator. */
649 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
650 "Transfer-Encoding: chunked\r\n"
651 "\r\n"
652 "7\r0asa90\r\n"
653 "0\r\n"
654 "\r\n"),
655
656 /* Invalid transfer coding. */
657 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
658 "Transfer-Encoding: shrinkydinky, chunked\r\n"),
659
660 /* Transfer coding cannot be blank (empty header value) */
661 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
662 "Transfer-Encoding: \r\n"
663 "Transfer-Encoding: chunked\r\n"),
664
665 /* Transfer coding cannot be blank (empty item in list) */
666 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
667 "Transfer-Encoding: gzip, ,chunked\r\n"),
668
669 /* Transfer coding cannot be blank (empty item at start of list) */
670 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
671 "Transfer-Encoding: ,chunked\r\n"),
672
673 /* Transfer coding cannot be blank (empty item at end of list) */
674 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
675 "Transfer-Encoding: chunked,\r\n"),
676
677 /* Transfer coding cannot be blank (empty header value) */
678 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
679 "Transfer-Encoding: \r\n"
680 "Transfer-Encoding: chunked\r\n"),
681
682 /* My chunk size is too big */
683 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
684 "Transfer-Encoding: chunked\r\n"
685 "\r\n"
686 "FFFFFFFFFFFFFFFFF\r\n"),
687
688 /* My content-Length is too big */
689 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
690 "Content-Length: 99999999999999999999\r\n"),
691
692 /* My content-Length is empty */
693 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
694 "Content-Length:\r\n"),
695
696 /* Has both content-length and transfer-encoding */
697 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
698 "Content-Length: 0\r\n"
699 "Transfer-Encoding: chunked\r\n"),
700
701 /* Has both transfer-encoding and content-length (but with transfer-encoding first this time) */
702 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
703 "Transfer-Encoding: chunked\r\n"
704 "Content-Length: 0\r\n"),
705
706 /* Multiple content-length headers */
707 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
708 "Content-Length: 0\r\n"
709 "Content-Length: 0\r\n"
710 "\r\n"),
711 /* Header is missing colon */
712 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
713 "Header-Missing-Colon yes it is\r\n"
714 "\r\n"),
715
716 /* Header with empty name */
717 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
718 ": header with empty name\r\n"
719 "\r\n"),
720
721 /* Header name with illegal characters */
722 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
723 "H@st: bad-char-in-name.com\r\n"),
724
725 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
726 "Host : space-after-name.com\r\n"),
727
728 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
729 " Host: space-before-name.com\r\n"),
730
731 /* Header value with illegal characters */
732 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
733 "Host: carriage-return\r.com\r\n"),
734
735 /* Forbid line folding */
736 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
737 "Host: \r\n"
738 " obsolete-line-folding.com\r\n"),
739
740 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
741 "Host: \r\n"
742 "\tobsolete-line-folding.com\r\n"),
743
744 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("POST / HTTP/1.1\r\n"
745 "Host: amazon.com\r\n"
746 "X-Line-Folding-Forbidden: one line of value\r\n"
747 " next line of value\r\n"),
748 /* Method is blank */
749 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(" / HTTP/1.1\r\n"),
750
751 /* URI is blank */
752 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET HTTP/1.1\r\n"),
753
754 /* HTTP version is blank */
755 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / \r\n"),
756
757 /* Missing spaces */
758 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET /HTTP/1.1\r\n"),
759
760 /* Missing spaces */
761 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET/HTTP/1.1\r\n"),
762
763 /* Extra space at end */
764 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1 \r\n"),
765
766 /* Illegal characters in method */
767 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("G@T / HTTP/1.1\r\n"),
768 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("G\rT / HTTP/1.1\r\n"),
769 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("G\nT / HTTP/1.1\r\n"),
770
771 /* Illegal characters in path */
772 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("G@T /\rindex.html HTTP/1.1\r\n"),
773 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("G@T /\tindex.html HTTP/1.1\r\n"),
774
775 /* Go ahead and add more cases here. */
776 };
777
778 for (size_t iter = 0; iter < AWS_ARRAY_SIZE(requests); ++iter) {
779 struct aws_byte_cursor request = requests[iter];
780
781 struct aws_h1_decoder_params params;
782 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
783 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
784
785 aws_reset_error();
786
787 ASSERT_FAILS(
788 aws_h1_decode(decoder, &request),
789 "Entry [%zu] should have failed, but it passed:\n------\n" PRInSTR "\n------\n",
790 iter,
791 AWS_BYTE_CURSOR_PRI(requests[iter]));
792
793 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, aws_last_error());
794
795 aws_h1_decoder_destroy(decoder);
796 }
797
798 s_test_clean_up();
799 return AWS_OP_SUCCESS;
800 }
801
802 AWS_TEST_CASE(h1_decoder_bad_responses_and_assert_failure, s_h1_decoder_bad_responses_and_assert_failure);
803 static int s_h1_decoder_bad_responses_and_assert_failure(struct aws_allocator *allocator, void *ctx) {
804 (void)ctx;
805 s_test_init(allocator);
806 const struct aws_byte_cursor responses[] = {
807 /* Response code not 3 digits */
808 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 1000 PHRASE\r\n"),
809
810 /* Response code not 3 digits */
811 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 99 PHRASE\r\n"),
812
813 /* Response code should not be in hex */
814 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 0x1 PHRASE\r\n"),
815
816 /* Response code should not be in hex */
817 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 FFF PHRASE\r\n"),
818
819 /* Phrase should not contain illegal characters */
820 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("HTTP/1.1 200 BAD\nPHRASE\r\n"),
821
822 /* Go ahead and add more cases here. */
823 };
824
825 for (size_t iter = 0; iter < AWS_ARRAY_SIZE(responses); ++iter) {
826 struct aws_byte_cursor response = responses[iter];
827
828 struct aws_h1_decoder_params params;
829 s_common_decoder_setup(allocator, 1024, &params, s_response, NULL);
830 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
831
832 ASSERT_FAILS(aws_h1_decode(decoder, &response));
833
834 aws_h1_decoder_destroy(decoder);
835 }
836
837 s_test_clean_up();
838 return AWS_OP_SUCCESS;
839 }
840
841 AWS_TEST_CASE(
842 h1_decoder_extraneous_buffer_data_ensure_not_processed,
843 s_test_h1_decoder_extraneous_buffer_data_ensure_not_processed);
844 static int s_test_h1_decoder_extraneous_buffer_data_ensure_not_processed(struct aws_allocator *allocator, void *ctx) {
845 (void)ctx;
846 s_test_init(allocator);
847 struct aws_byte_cursor msg =
848 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
849 "Wow look here. That's a lot of extra random stuff!");
850
851 struct aws_h1_decoder_params params;
852 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
853 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
854
855 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
856 ASSERT_INT_EQUALS(0, msg.len);
857
858 aws_h1_decoder_destroy(decoder);
859 s_test_clean_up();
860 return AWS_OP_SUCCESS;
861 }
862
863 AWS_TEST_CASE(h1_decoder_ignore_chunk_extensions, s_test_h1_decoder_ignore_chunk_extensions);
864 static int s_test_h1_decoder_ignore_chunk_extensions(struct aws_allocator *allocator, void *ctx) {
865 (void)ctx;
866 s_test_init(allocator);
867 struct aws_byte_cursor msg =
868 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("GET / HTTP/1.1\r\n"
869 "Host: amazon.com\r\n"
870 "Accept-Language: fr\r\n"
871 "Transfer-Encoding: chunked \r\n"
872 "Trailer: Expires\r\n"
873 "\r\n"
874 "7;some-dumb-chunk-extension-name=some-dumb-chunk-extension-value\r\n"
875 "Mozilla\r\n"
876 "9\r\n"
877 "Developer\r\n"
878 "7\r\n"
879 "Network\r\n"
880 "0\r\n"
881 "Expires: Wed, 21 Oct 2015 07:28:00 GMT\r\n"
882 "\r\n");
883
884 struct aws_h1_decoder_params params;
885 s_common_decoder_setup(allocator, 1024, &params, s_request, NULL);
886 struct aws_h1_decoder *decoder = aws_h1_decoder_new(&params);
887
888 ASSERT_SUCCESS(aws_h1_decode(decoder, &msg));
889
890 aws_h1_decoder_destroy(decoder);
891 s_test_clean_up();
892 return AWS_OP_SUCCESS;
893 }