forked from sdarwin/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimits.cpp
More file actions
395 lines (359 loc) · 12.3 KB
/
Copy pathlimits.cpp
File metadata and controls
395 lines (359 loc) · 12.3 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// Copyright (c) 2019 Vinnie Falco ([email protected])
//
// 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/cppalliance/json
//
#include <boost/json/array.hpp>
#include <boost/json/object.hpp>
#include <boost/json/string.hpp>
#include <boost/json/value.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/parser.hpp>
#include <vector>
#include "test_suite.hpp"
#include "test.hpp"
BOOST_JSON_NS_BEGIN
/*
This translation unit exercises code paths
related to library limits such as max string
length.
*/
class limits_test
{
public:
void
testValue()
{
// object too large
{
std::initializer_list<std::pair<
string_view, value_ref>> init = {
{ "1", 1},{ "2", 2},{ "3", 3},{ "4", 4},{ "5", 5},
{ "6", 6},{ "7", 7},{ "8", 8},{ "9", 9},{"10",10},
{"11",11},{"12",12},{"13",13},{"14",14},{"15",15},
{"16",16},{"17",17},{"18",18},{"19",19},{"10",10},
{"21",21},{"22",22},{"23",23},{"24",24},{"25",25},
{"26",26},{"27",27},{"28",28},{"29",29},{"30",30},
{"31",31}};
BOOST_TEST(init.size() > object::max_size());
BOOST_TEST_THROWS(value{init}, std::length_error);
}
}
void
testObject()
{
// max size
{
BOOST_TEST_THROWS(
object(object::max_size()+1),
std::length_error);
}
// max size
{
std::initializer_list<std::pair<
string_view, value_ref>> init = {
{ "1", 1},{ "2", 2},{ "3", 3},{ "4", 4},{ "5", 5},
{ "6", 6},{ "7", 7},{ "8", 8},{ "9", 9},{"10",10},
{"11",11},{"12",12},{"13",13},{"14",14},{"15",15},
{"16",16},{"17",17},{"18",18},{"19",19},{"10",10},
{"21",21},{"22",22},{"23",23},{"24",24},{"25",25},
{"26",26},{"27",27},{"28",28},{"29",29},{"30",30},
{"31",31}};
BOOST_TEST(init.size() > object::max_size());
BOOST_TEST_THROWS(
object(init.begin(), init.end()),
std::length_error);
BOOST_TEST_THROWS(
object(
make_input_iterator(init.begin()),
make_input_iterator(init.end())),
std::length_error);
}
// max key size
{
std::string const big(
string::max_size() + 1, '*');
BOOST_TEST_THROWS(
object({ {big, nullptr} }),
std::length_error);
}
}
void
testArray()
{
{
BOOST_TEST_THROWS(
array(
array::max_size()+1,
value(nullptr)),
std::length_error);
}
{
std::vector<int> v(
array::max_size()+1, 42);
BOOST_TEST_THROWS(
array(v.begin(), v.end()),
std::length_error);
}
{
std::vector<int> v(
array::max_size()+1, 42);
BOOST_TEST_THROWS(array(
make_input_iterator(v.begin()),
make_input_iterator(v.end())),
std::length_error);
}
{
array a;
BOOST_TEST_THROWS(
a.insert(a.begin(),
array::max_size() + 1,
nullptr),
std::length_error);
}
}
void
testString()
{
// strings
{
{
string s;
BOOST_TEST_THROWS(
(s.resize(s.max_size() + 1)),
std::length_error);
}
{
string s;
s.resize(100);
BOOST_TEST_THROWS(
(s.append(s.max_size() - 1, '*')),
std::length_error);
}
{
string s;
s.resize(s.max_size() - 5);
BOOST_TEST_THROWS(
(s.replace(0, 1, s.subview(0, 10))),
std::length_error);
}
{
string s;
s.resize(s.max_size() - 5);
BOOST_TEST_THROWS(
(s.replace(0, 1, 10, 'a')),
std::length_error);
}
{
string s;
s.resize(s.max_size() - 5);
BOOST_TEST_THROWS(
(s.insert(0, s.subview(0, 10))),
std::length_error);
}
#if 0
{
// VFALCO tsan doesn't like this
string s;
try
{
s.resize(s.max_size() - 1);
}
catch(std::exception const&)
{
}
}
#endif
}
// string in parser
{
parser p;
std::string const big(
string::max_size() + 1, '*');
auto const js =
"\"" + big + "\":null";
error_code ec;
auto jv = parse(js, ec);
BOOST_TEST(ec == error::string_too_large);
}
// key in parser
{
parser p;
std::string const big(
string::max_size() + 1, '*');
auto const js =
"{\"" + big + "\":null}";
error_code ec;
auto jv = parse(js, ec);
BOOST_TEST(ec == error::key_too_large);
}
}
void
testStack()
{
// max raw_stack
// VFALCO The problem here is that we need the maximum stack
// larger than the maximum string to test string parts,
// but we need the opposite to test stack overflows.
// Thus stack overflow cannot be covered by tests.
#if 0
{
std::string big;
value jv;
BOOST_TEST_THROWS(
jv = parse(
"[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,"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n"
"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n"
"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n"
"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n"
"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n"
"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\"";
error_code ec;
auto jv = parse(s, ec);
BOOST_TEST(! ec);
}
}
void
testBasicParser()
{
// overflow in on_key_part
{
null_parser p;
error_code ec;
std::string big;
big = "\\b";
big += std::string(
string::max_size()*2, '*');
auto const js =
"{\"" + big + "\":null}";
p.write(js.data(), js.size(), ec);
BOOST_TEST(ec == error::key_too_large);
}
// overflow in on_key
{
null_parser p;
error_code ec;
std::string big;
big = "\\b";
big += std::string(
(string::max_size()*3)/2, '*');
auto const js =
"{\"" + big + "\":null}";
p.write(js.data(), js.size(), ec);
BOOST_TEST(ec == error::key_too_large);
}
// overflow in on_string_part
{
null_parser p;
error_code ec;
std::string big;
big = "\\b";
big += std::string(
string::max_size()*2, '*');
auto const js =
"\"" + big + "\"";
p.write(js.data(), js.size(), ec);
BOOST_TEST(ec == error::string_too_large);
}
// overflow in on_string
{
null_parser p;
error_code ec;
std::string big;
big = "\\b";
big += std::string(
(string::max_size()*3)/2, '*');
auto const js =
"\"" + big + "\"";
p.write(js.data(), js.size(), ec);
BOOST_TEST(ec == error::string_too_large);
}
// object overflow
{
null_parser p;
error_code ec;
string_view s = R"({
"00":0,"01":0,"02":0,"03":0,"04":0,"05":0,"06":0,"07":0,"08":0,"09":0,
"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,
"20":0
})";
p.write(s.data(), s.size(), ec);
BOOST_TEST(ec == error::object_too_large);
}
// array overflow
{
null_parser p;
error_code ec;
string_view s = "["
"0,0,0,0,0,0,0,0,0,0,"
"0,0,0,0,0,0,0,0,0,0,"
"0"
"]";
p.write(s.data(), s.size(), ec);
BOOST_TEST(ec == error::array_too_large);
}
}
void
run()
{
#if ! defined(BOOST_JSON_NO_MAX_STRUCTURED_SIZE) && \
! defined(BOOST_JSON_NO_MAX_STRING_SIZE) && \
! defined(BOOST_JSON_NO_MAX_STACK_SIZE) && \
! defined(BOOST_JSON_NO_PARSER_BUFFER_SIZE)
testValue();
testObject();
testArray();
testString();
testStack();
testParser();
testBasicParser();
#else
BOOST_TEST_PASS();
#endif
}
};
TEST_SUITE(limits_test, "boost.json.limits");
BOOST_JSON_NS_END