]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/http/rfc7230.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / test / beast / http / rfc7230.cpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/beast/http/rfc7230.hpp>
12
13 #include <boost/beast/http/detail/rfc7230.hpp>
14 #include <boost/beast/unit_test/suite.hpp>
15 #include <string>
16 #include <vector>
17
18 #include <boost/beast/core/detail/empty_base_optimization.hpp>
19
20 namespace boost {
21 namespace beast {
22 namespace http {
23
24 class rfc7230_test : public beast::unit_test::suite
25 {
26 public:
27 static
28 std::string
29 fmt(std::string const& s)
30 {
31 return '\'' + s + '\'';
32 }
33
34 static
35 std::string
36 str(string_view s)
37 {
38 return std::string(s.data(), s.size());
39 }
40
41 static
42 std::string
43 str(param_list const& c)
44 {
45 std::string s;
46 for(auto const& p : c)
47 {
48 s.push_back(';');
49 s.append(str(p.first));
50 if(! p.second.empty())
51 {
52 s.push_back('=');
53 s.append(str(p.second));
54 }
55 }
56 return s;
57 }
58
59 void
60 testParamList()
61 {
62 auto const ce =
63 [&](std::string const& s)
64 {
65 auto const got = str(param_list{s});
66 BEAST_EXPECTS(got == s, fmt(got));
67 };
68 auto const cs =
69 [&](std::string const& s, std::string const& answer)
70 {
71 ce(answer);
72 auto const got = str(param_list{s});
73 ce(got);
74 BEAST_EXPECTS(got == answer, fmt(got));
75 };
76 auto const cq =
77 [&](std::string const& s, std::string const& answer)
78 {
79 auto const got = str(param_list{s});
80 BEAST_EXPECTS(got == answer, fmt(got));
81 };
82
83 ce("");
84 ce(";x");
85 ce(";xy");
86 ce(";x;y");
87
88 ce("");
89 cs(" ;\t i =\t 1 \t", ";i=1");
90 cq("\t; \t xyz=1 ; ijk=\"q\\\"t\"", ";xyz=1;ijk=q\"t");
91 ce(";x;y");
92
93 // invalid strings
94 cs(";", "");
95 cs(";,", "");
96 cq(";x=,", "");
97 cq(";xy=\"", "");
98 cq(";xy=\"\x7f", "");
99 cq(";xy=\"\\", "");
100 cq(";xy=\"\\\x01\"", "");
101 }
102
103 static
104 std::string
105 str(ext_list const& ex)
106 {
107 std::string s;
108 for(auto const& e : ex)
109 {
110 if(! s.empty())
111 s += ',';
112 s.append(str(e.first));
113 s += str(e.second);
114 }
115 return s;
116 }
117
118 void
119 testExtList()
120 {
121 auto const ce =
122 [&](std::string const& s)
123 {
124 auto const got = str(ext_list{s});
125 BEAST_EXPECTS(got == s, fmt(got));
126 };
127 auto const cs =
128 [&](std::string const& s, std::string const& good)
129 {
130 ce(good);
131 auto const got = str(ext_list{s});
132 ce(got);
133 BEAST_EXPECTS(got == good, fmt(got));
134 };
135 auto const cq =
136 [&](std::string const& s, std::string const& good)
137 {
138 auto const got = str(ext_list{s});
139 BEAST_EXPECTS(got == good, fmt(got));
140 };
141 /*
142 ext-basic_parsed_list = *( "," OWS ) ext *( OWS "," [ OWS ext ] )
143 ext = token param-basic_parsed_list
144 param-basic_parsed_list = *( OWS ";" OWS param )
145 param = token OWS "=" OWS ( token / quoted-string )
146 */
147 cs(",", "");
148 cs(", ", "");
149 cs(",\t", "");
150 cs(", \t", "");
151 cs(" ", "");
152 cs(" ,", "");
153 cs("\t,", "");
154 cs("\t , \t", "");
155 cs(",,", "");
156 cs(" , \t,, \t,", "");
157 cs( "permessage-deflate; client_no_context_takeover; client_max_window_bits",
158 "permessage-deflate;client_no_context_takeover;client_max_window_bits");
159
160 ce("a");
161 ce("ab");
162 ce("a,b");
163 cs(" a ", "a");
164 cs("\t a, b\t , c\t", "a,b,c");
165 ce("a;b");
166 ce("a;b;c");
167
168 cs("a; \t i\t=\t \t1\t ", "a;i=1");
169 ce("a;i=1;j=2;k=3");
170 ce("a;i=1;j=2;k=3,b;i=4;j=5;k=6");
171
172 cq("ab;x=\" \"", "ab;x= ");
173 cq("ab;x=\"\\\"\"", "ab;x=\"");
174
175 BEAST_EXPECT(ext_list{"a,b;i=1,c;j=2;k=3"}.exists("A"));
176 BEAST_EXPECT(ext_list{"a,b;i=1,c;j=2;k=3"}.exists("b"));
177 BEAST_EXPECT(! ext_list{"a,b;i=1,c;j=2;k=3"}.exists("d"));
178
179 // invalid strings
180 cs("i j", "i");
181 cs(";", "");
182 }
183
184 static
185 std::string
186 str(token_list const& c)
187 {
188 bool first = true;
189 std::string s;
190 for(auto const& p : c)
191 {
192 if(! first)
193 s.push_back(',');
194 s.append(str(p));
195 first = false;
196 }
197 return s;
198 }
199
200 void
201 testTokenList()
202 {
203 auto const ce =
204 [&](std::string const& s)
205 {
206 auto const got = str(token_list{s});
207 BEAST_EXPECTS(got == s, fmt(got));
208 };
209 auto const cs =
210 [&](std::string const& s, std::string const& good)
211 {
212 ce(good);
213 auto const got = str(token_list{s});
214 ce(got);
215 BEAST_EXPECTS(got == good, fmt(got));
216 };
217
218 cs("", "");
219 cs(" ", "");
220 cs(" ", "");
221 cs("\t", "");
222 cs(" \t ", "");
223 cs(",", "");
224 cs(",,", "");
225 cs(" ,", "");
226 cs(" , ,", "");
227 cs(" x", "x");
228 cs(" \t x", "x");
229 cs("x ", "x");
230 cs("x \t", "x");
231 cs(" \t x \t ", "x");
232 ce("x,y");
233 cs("x ,\ty ", "x,y");
234 cs("x, y, z", "x,y,z");
235
236 BEAST_EXPECT(token_list{"a,b,c"}.exists("A"));
237 BEAST_EXPECT(token_list{"a,b,c"}.exists("b"));
238 BEAST_EXPECT(! token_list{"a,b,c"}.exists("d"));
239
240 // invalid
241 cs("x y", "x");
242 }
243
244 template<class Policy>
245 static
246 std::vector<std::string>
247 to_vector(string_view in)
248 {
249 std::vector<std::string> v;
250 detail::basic_parsed_list<Policy> list{in};
251 for(auto const& s :
252 detail::basic_parsed_list<Policy>{in})
253 v.emplace_back(s.data(), s.size());
254 return v;
255 }
256
257 template<class Policy>
258 void
259 validate(string_view in,
260 std::vector<std::string> const& v)
261 {
262 BEAST_EXPECT(to_vector<Policy>(in) == v);
263 }
264
265 template<class Policy>
266 void
267 good(string_view in)
268 {
269 BEAST_EXPECT(validate_list(
270 detail::basic_parsed_list<Policy>{in}));
271 }
272
273 template<class Policy>
274 void
275 good(string_view in,
276 std::vector<std::string> const& v)
277 {
278 BEAST_EXPECT(validate_list(
279 detail::basic_parsed_list<Policy>{in}));
280 validate<Policy>(in, v);
281 }
282
283 template<class Policy>
284 void
285 bad(string_view in)
286 {
287 BEAST_EXPECT(! validate_list(
288 detail::basic_parsed_list<Policy>{in}));
289 }
290
291 void
292 testOptTokenList()
293 {
294 /*
295 #token = [ ( "," / token ) *( OWS "," [ OWS token ] ) ]
296 */
297 using type = detail::opt_token_list_policy;
298
299 good<type>("", {});
300 good<type>(" ", {});
301 good<type>("\t", {});
302 good<type>(" \t", {});
303 good<type>(",", {});
304 good<type>(",,", {});
305 good<type>(", ,", {});
306 good<type>(",\t,", {});
307 good<type>(", \t,", {});
308 good<type>(", \t, ", {});
309 good<type>(", \t,\t", {});
310 good<type>(", \t, \t", {});
311
312 good<type>("x", {"x"});
313 good<type>(" x", {"x"});
314 good<type>("x,,", {"x"});
315 good<type>("x, ,", {"x"});
316 good<type>("x,, ", {"x"});
317 good<type>("x,,,", {"x"});
318
319 good<type>("x,y", {"x","y"});
320 good<type>("x ,y", {"x","y"});
321 good<type>("x\t,y", {"x","y"});
322 good<type>("x \t,y", {"x","y"});
323 good<type>(" x,y", {"x","y"});
324 good<type>(" x,y ", {"x","y"});
325 good<type>(",x,y", {"x","y"});
326 good<type>("x,y,", {"x","y"});
327 good<type>(",,x,y", {"x","y"});
328 good<type>(",x,,y", {"x","y"});
329 good<type>(",x,y,", {"x","y"});
330 good<type>("x ,, y", {"x","y"});
331 good<type>("x , ,y", {"x","y"});
332
333 good<type>("x,y,z", {"x","y","z"});
334
335 bad<type>("(");
336 bad<type>("x(");
337 bad<type>("(x");
338 bad<type>(",(");
339 bad<type>("(,");
340 bad<type>("x,(");
341 bad<type>("(,x");
342 bad<type>("x y");
343 }
344
345 void
346 run()
347 {
348 testOptTokenList();
349 #if 0
350 testParamList();
351 testExtList();
352 testTokenList();
353 #endif
354 }
355 };
356
357 BEAST_DEFINE_TESTSUITE(beast,http,rfc7230);
358
359 } // http
360 } // beast
361 } // boost