]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/chunk_parsers_test.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / tests / unit / chunk_parsers_test.cc
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19 /*
20 * Copyright (C) 2020 ScyllaDB.
21 */
22
23 #include <seastar/core/ragel.hh>
24 #include <seastar/core/sstring.hh>
25 #include <seastar/core/thread.hh>
26 #include <seastar/core/temporary_buffer.hh>
27 #include <seastar/http/chunk_parsers.hh>
28 #include <seastar/http/internal/content_source.hh>
29 #include <seastar/testing/test_case.hh>
30 #include <tuple>
31 #include <utility>
32 #include <vector>
33
34 using namespace seastar;
35
36 SEASTAR_TEST_CASE(test_size_and_extensions_parsing) {
37 struct test_set {
38 sstring msg;
39 bool parsable;
40 sstring size = "";
41 std::vector<std::pair<sstring, sstring>> extensions;
42
43 temporary_buffer<char> buf() {
44 return temporary_buffer<char>(msg.c_str(), msg.size());
45 }
46 };
47
48 std::vector<test_set> tests = {
49 { "14;name=value\r\n", true, "14", { {"name", "value"} } },
50 { "abcdef;name=value;name2=\"value2\"\r\n", true, "abcdef" },
51 { "1efg;name=value\r\n", false },
52 { "aa;tchars.^_`|123=t1!#$%&'*+-.~\r\n", true, "aa", { {"tchars.^_`|123", "t1!#$%&'*+-.~"} } },
53 { "1;quoted=\"hello world\";quoted-pair=\"\\a\\b\\cd\\\\ef\"\r\n", true, "1", { {"quoted", "hello world"}, {"quoted-pair", "abcd\\ef"} } },
54 { "2;bad-quoted-pair=\"abc\\\"\r\n", false },
55 { "3;quoted-pair-outside-quoted-string=\\q\\p\r\n", false },
56 { "4;whitespace-outside-quoted-string=quoted string\r\n", false },
57 { "5;quotation-mark-inside-quoted-string=\"quoted\"mark\"\r\n", false },
58 { "6; bad=space\r\n", false },
59 { "7;sole-name\r\n", true, "7", { { "sole-name", ""} } },
60 { "8;empty_value=\"\"\r\n", true, "8", { { "empty_value", ""} } },
61 { "0\r\n", true, "0" }
62 };
63
64 http_chunk_size_and_ext_parser parser;
65 for (auto& tset : tests) {
66 parser.init();
67 BOOST_REQUIRE(parser(tset.buf()).get0().has_value());
68 BOOST_REQUIRE_NE(parser.failed(), tset.parsable);
69 if (tset.parsable) {
70 BOOST_REQUIRE_EQUAL(parser.get_size(), std::move(tset.size));
71 auto exts = parser.get_parsed_extensions();
72 for (auto& ext : tset.extensions) {
73 BOOST_REQUIRE_EQUAL(exts[ext.first], ext.second);
74 }
75 }
76 }
77 return make_ready_future<>();
78 }
79
80 SEASTAR_TEST_CASE(test_trailer_headers_parsing) {
81 struct test_set {
82 sstring msg;
83 bool parsable;
84 sstring header_name = "";
85 sstring header_value = "";
86
87 temporary_buffer<char> buf() {
88 return temporary_buffer<char>(msg.c_str(), msg.size());
89 }
90 };
91
92 std::vector<test_set> tests = {
93 // the headers follow the same rules as in the request parser
94 { "Host: test\r\n\r\n", true, "Host", "test" },
95 { "Header: Field\r\n\r\n", true, "Header", "Field" },
96 { "Header: \r\n\r\n", true, "Header", "" },
97 { "Header: f i e l d \r\n\r\n", true, "Header", "f i e l d" },
98 { "Header: fiel\r\n d\r\n\r\n", true, "Header", "fiel d" },
99 { "tchars.^_`|123: printable!@#%^&*()obs_text\x80\x81\xff\r\n\r\n", true,
100 "tchars.^_`|123", "printable!@#%^&*()obs_text\x80\x81\xff" },
101 { "Header: Field\r\nHeader: Field2\r\n\r\n", true, "Header", "Field,Field2" },
102 { "Header : Field\r\n\r\n", false },
103 { "Header Field\r\n\r\n", false },
104 { "Header@: Field\r\n\r\n", false },
105 { "Header: fiel\r\nd \r\n\r\n", false },
106 { "\r\n", true }
107 };
108
109 http_chunk_trailer_parser parser;
110 for (auto& tset : tests) {
111 parser.init();
112 BOOST_REQUIRE(parser(tset.buf()).get0().has_value());
113 BOOST_REQUIRE_NE(parser.failed(), tset.parsable);
114 if (tset.parsable) {
115 auto heads = parser.get_parsed_headers();
116 BOOST_REQUIRE_EQUAL(heads[std::move(tset.header_name)], std::move(tset.header_value));
117 }
118 }
119 return make_ready_future<>();
120 }