]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rapidjson/test/unittest/jsoncheckertest.cpp
1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
8 // http://opensource.org/licenses/MIT
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
17 #include "rapidjson/document.h"
19 using namespace rapidjson
;
21 static char* ReadFile(const char* filename
, size_t& length
) {
22 const char *paths
[] = {
26 "../../bin/jsonchecker",
27 "../../../bin/jsonchecker"
31 for (size_t i
= 0; i
< sizeof(paths
) / sizeof(paths
[0]); i
++) {
32 sprintf(buffer
, "%s/%s", paths
[i
], filename
);
33 fp
= fopen(buffer
, "rb");
41 fseek(fp
, 0, SEEK_END
);
42 length
= static_cast<size_t>(ftell(fp
));
43 fseek(fp
, 0, SEEK_SET
);
44 char* json
= static_cast<char*>(malloc(length
+ 1));
45 size_t readLength
= fread(json
, 1, length
, fp
);
46 json
[readLength
] = '\0';
51 TEST(JsonChecker
, Reader
) {
54 // jsonchecker/failXX.json
55 for (int i
= 1; i
<= 33; i
++) {
56 if (i
== 1) // fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159).
58 if (i
== 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
61 sprintf(filename
, "fail%d.json", i
);
63 char* json
= ReadFile(filename
, length
);
65 printf("jsonchecker file %s not found", filename
);
70 GenericDocument
<UTF8
<>, CrtAllocator
> document
; // Use Crt allocator to check exception-safety (no memory leak)
72 EXPECT_TRUE(document
.HasParseError());
74 document
.Parse
<kParseIterativeFlag
>(json
);
75 EXPECT_TRUE(document
.HasParseError());
81 for (int i
= 1; i
<= 3; i
++) {
82 sprintf(filename
, "pass%d.json", i
);
84 char* json
= ReadFile(filename
, length
);
86 printf("jsonchecker file %s not found", filename
);
90 GenericDocument
<UTF8
<>, CrtAllocator
> document
; // Use Crt allocator to check exception-safety (no memory leak)
92 EXPECT_FALSE(document
.HasParseError());
94 document
.Parse
<kParseIterativeFlag
>(json
);
95 EXPECT_FALSE(document
.HasParseError());