forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk_encode.cpp
More file actions
298 lines (254 loc) · 7.54 KB
/
Copy pathchunk_encode.cpp
File metadata and controls
298 lines (254 loc) · 7.54 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
#include <boost/beast/http/chunk_encode.hpp>
#include "message_fuzz.hpp"
#include <boost/beast/core/buffers_to_string.hpp>
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/http/fields.hpp>
#include <boost/beast/test/fuzz.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/optional.hpp>
#include <random>
namespace boost {
namespace beast {
namespace http {
class chunk_encode_test
: public beast::unit_test::suite
{
public:
struct not_chunk_extensions {};
BOOST_STATIC_ASSERT(
detail::is_chunk_extensions<chunk_extensions>::value);
BOOST_STATIC_ASSERT(
! detail::is_chunk_extensions<not_chunk_extensions>::value);
template<class T, class... Args>
void
check(string_view match, Args&&... args)
{
T t(std::forward<Args>(args)...);
BEAST_EXPECT(buffers_to_string(t) == match);
T t2(t);
BEAST_EXPECT(buffers_to_string(t2) == match);
T t3(std::move(t2));
BEAST_EXPECT(buffers_to_string(t3) == match);
}
template<class T, class... Args>
void
check_fwd(string_view match, Args&&... args)
{
T t(std::forward<Args>(args)...);
BEAST_EXPECT(buffers_to_string(t) == match);
T t2(t);
BEAST_EXPECT(buffers_to_string(t2) == match);
T t3(std::move(t2));
BEAST_EXPECT(buffers_to_string(t3) == match);
}
using cb_t = net::const_buffer;
static
cb_t
cb(string_view s)
{
return {s.data(), s.size()};
}
void
testChunkCRLF()
{
#if ! defined(BOOST_GCC) || BOOST_GCC >= 50000
check<chunk_crlf>("\r\n");
#endif
}
void
testChunkHeader()
{
check<chunk_header>("10\r\n", 16u);
check<chunk_header>("20;x\r\n", 32u, ";x");
chunk_extensions exts;
exts.insert("y");
exts.insert("z");
check<chunk_header>("30;y;z\r\n", 48u, exts);
{
auto exts2 = exts;
check_fwd<chunk_header>(
"30;y;z\r\n", 48u, std::move(exts2));
}
check<chunk_header>("30;y;z\r\n", 48u, exts,
std::allocator<double>{});
{
auto exts2 = exts;
check<chunk_header>(
"30;y;z\r\n", 48u, std::move(exts2),
std::allocator<double>{});
}
}
void
testChunkBody()
{
check<chunk_body<cb_t>>(
"3\r\n***\r\n", cb("***"));
check<chunk_body<cb_t>>(
"3;x\r\n***\r\n", cb("***"), ";x");
chunk_extensions exts;
exts.insert("y");
exts.insert("z");
check<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), exts);
{
auto exts2 = exts;
check_fwd<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), std::move(exts2));
}
check<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), exts, std::allocator<double>{});
{
auto exts2 = exts;
check_fwd<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), std::move(exts2),
std::allocator<double>{});
}
}
void
testChunkFinal()
{
check<chunk_last<>>(
"0\r\n\r\n");
check<chunk_last<cb_t>>(
"0\r\nMD5:ou812\r\n\r\n",
cb("MD5:ou812\r\n\r\n"));
fields trailers;
trailers.set(field::content_md5, "ou812");
check<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
trailers);
{
auto trailers2 = trailers;
check_fwd<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
std::move(trailers2));
}
check<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
trailers, std::allocator<double>{});
{
auto trailers2 = trailers;
check<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
std::move(trailers2), std::allocator<double>{});
}
}
void
testChunkExtensions()
{
auto const str =
[](chunk_extensions const& ce)
{
std::string s;
for(auto const& v : ce)
{
s.append(std::string(v.first));
s.push_back(',');
if(! v.second.empty())
{
s.append(std::string(v.second));
s.push_back(',');
}
}
return s;
};
chunk_extensions ce;
ce.insert("x");
BEAST_EXPECT(ce.str() == ";x");
BEAST_EXPECT(str(ce) == "x,");
ce.insert("y", "z");
BEAST_EXPECT(ce.str() == ";x;y=z");
BEAST_EXPECT(str(ce) == "x,y,z,");
ce.insert("z", R"(")");
BEAST_EXPECT(ce.str() == R"(;x;y=z;z="\"")");
BEAST_EXPECT(str(ce) == R"(x,y,z,z,",)");
ce.insert("p", R"(\)");
BEAST_EXPECT(ce.str() == R"(;x;y=z;z="\"";p="\\")");
BEAST_EXPECT(str(ce) == R"(x,y,z,z,",p,\,)");
ce.insert("q", R"(1"2\)");
BEAST_EXPECT(ce.str() == R"(;x;y=z;z="\"";p="\\";q="1\"2\\")");
BEAST_EXPECT(str(ce) == R"(x,y,z,z,",p,\,q,1"2\,)");
}
void
testParseChunkExtensions()
{
auto const grind =
[&](string_view s)
{
error_code ec;
static_string<200> ss{s};
test::fuzz_rand r;
for(auto i = 3; i--;)
{
test::fuzz(ss, 5, 5, r,
[&](string_view s)
{
chunk_extensions c1;
c1.parse(s, ec);
if(ec)
{
pass();
return;
}
chunk_extensions c2;
c2.parse(c1.str(), ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
chunk_extensions c3;
for(auto const& v : c2)
if(v.second.empty())
c3.insert(v.first);
else
c3.insert(v.first, v.second);
BEAST_EXPECTS(c2.str() == c3.str(), c3.str());
});
}
};
auto const good =
[&](string_view s)
{
error_code ec;
chunk_extensions ce;
ce.parse(s, ec);
BEAST_EXPECTS(! ec, ec.message());
grind(s);
};
auto const bad =
[&](string_view s)
{
error_code ec;
chunk_extensions ce;
ce.parse(s, ec);
BEAST_EXPECT(ec);
grind(s);
};
chunkExtensionsTest(good, bad);
}
void
run() override
{
testChunkCRLF();
testChunkHeader();
testChunkBody();
testChunkFinal();
testChunkExtensions();
testParseChunkExtensions();
}
};
BEAST_DEFINE_TESTSUITE(beast,http,chunk_encode);
} // http
} // beast
} // boost