|
| 1 | +/** @file |
| 2 | +
|
| 3 | + Catch-based unit tests for various header logic. |
| 4 | + This replaces the old regression tests in HdrTest.cc. |
| 5 | +
|
| 6 | + @section license License |
| 7 | +
|
| 8 | + Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. |
| 9 | + See the NOTICE file distributed with this work for additional information regarding copyright |
| 10 | + ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 11 | + "License"); you may not use this file except in compliance with the License. You may obtain a |
| 12 | + copy of the License at |
| 13 | +
|
| 14 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +
|
| 16 | + Unless required by applicable law or agreed to in writing, software distributed under the License |
| 17 | + is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 18 | + or implied. See the License for the specific language governing permissions and limitations under |
| 19 | + the License. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <cstdio> |
| 23 | + |
| 24 | +#include "catch.hpp" |
| 25 | + |
| 26 | +#include "MIME.h" |
| 27 | + |
| 28 | +TEST_CASE("HdrTest", "[proxy][hdrtest]") |
| 29 | +{ |
| 30 | + mime_init(); |
| 31 | + |
| 32 | + SECTION("Field Char Check") |
| 33 | + { |
| 34 | + static struct { |
| 35 | + std::string_view line; |
| 36 | + ParseResult expected; |
| 37 | + } test_cases[] = { |
| 38 | + //// |
| 39 | + // Field Name |
| 40 | + {"Content-Length: 10\r\n", PARSE_RESULT_CONT}, |
| 41 | + {"Content-Length\x0b: 10\r\n", PARSE_RESULT_ERROR}, |
| 42 | + //// |
| 43 | + // Field Value |
| 44 | + // SP |
| 45 | + {"Content-Length: 10\r\n", PARSE_RESULT_CONT}, |
| 46 | + {"Foo: ab cd\r\n", PARSE_RESULT_CONT}, |
| 47 | + // HTAB |
| 48 | + {"Foo: ab\td/cd\r\n", PARSE_RESULT_CONT}, |
| 49 | + // VCHAR |
| 50 | + {"Foo: ab\x21/cd\r\n", PARSE_RESULT_CONT}, |
| 51 | + {"Foo: ab\x7e/cd\r\n", PARSE_RESULT_CONT}, |
| 52 | + // DEL |
| 53 | + {"Foo: ab\x7f/cd\r\n", PARSE_RESULT_ERROR}, |
| 54 | + // obs-text |
| 55 | + {"Foo: ab\x80/cd\r\n", PARSE_RESULT_CONT}, |
| 56 | + {"Foo: ab\xff/cd\r\n", PARSE_RESULT_CONT}, |
| 57 | + // control char |
| 58 | + {"Content-Length: 10\x0b\r\n", PARSE_RESULT_ERROR}, |
| 59 | + {"Content-Length:\x0b 10\r\n", PARSE_RESULT_ERROR}, |
| 60 | + {"Foo: ab\x1d/cd\r\n", PARSE_RESULT_ERROR}, |
| 61 | + }; |
| 62 | + |
| 63 | + MIMEHdr hdr; |
| 64 | + MIMEParser parser; |
| 65 | + mime_parser_init(&parser); |
| 66 | + |
| 67 | + for (const auto &t : test_cases) { |
| 68 | + mime_parser_clear(&parser); |
| 69 | + |
| 70 | + const char *start = t.line.data(); |
| 71 | + const char *end = start + t.line.size(); |
| 72 | + |
| 73 | + int r = hdr.parse(&parser, &start, end, false, false); |
| 74 | + if (r != t.expected) { |
| 75 | + std::printf("Expected %s is %s, but not", t.line.data(), t.expected == PARSE_RESULT_ERROR ? "invalid" : "valid"); |
| 76 | + CHECK(false); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments