VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTJson.cpp@ 74179

Last change on this file since 74179 was 74179, checked in by vboxsync, 7 years ago

IPRT/json: Added \uXXXX and surrogate pair tests. Refuse to decode U+0000, U+FFFE and U+FFFF. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/* $Id: tstRTJson.cpp 74179 2018-09-10 10:46:31Z vboxsync $ */
2/** @file
3 * IPRT Testcase - JSON API.
4 */
5
6/*
7 * Copyright (C) 2016-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/json.h>
32#include <iprt/string.h>
33#include <iprt/test.h>
34
35static const char *g_pszJson =
36 "{\n"
37 " \"integer\": 100,\n"
38 " \"number\": 22.22,\n"
39 " \"string\": \"test\",\n"
40 " \"array\": [1, 2, 3, 4, 5, \"6\"],\n"
41 " \"subobject\":\n"
42 " {\n"
43 " \"false\": false,\n"
44 " \"true\": true,\n"
45 " \"null\": null\n"
46 " }\n"
47 "}\n";
48
49/**
50 * Some basic tests to detect malformed JSON.
51 */
52static void tstBasic(RTTEST hTest)
53{
54 RTTestSub(hTest, "Basic valid/malformed tests");
55 static struct
56 {
57 const char *pszJson;
58 int iRcResult;
59 } const aTests[] =
60 {
61 { "", VERR_JSON_MALFORMED },
62 { ",", VERR_JSON_MALFORMED },
63 { ":", VERR_JSON_MALFORMED },
64 { " \n\t{", VERR_JSON_MALFORMED },
65 { "}", VERR_JSON_MALFORMED },
66 { "[", VERR_JSON_MALFORMED },
67 { "]", VERR_JSON_MALFORMED },
68 { "[ \"test\" : ", VERR_JSON_MALFORMED },
69 { "null", VINF_SUCCESS },
70 { "true", VINF_SUCCESS },
71 { "false", VINF_SUCCESS },
72 { "100", VINF_SUCCESS },
73 { "\"test\"", VINF_SUCCESS },
74 { "{ }", VINF_SUCCESS },
75 { "[ ]", VINF_SUCCESS },
76 { "[ 100, 200 ]", VINF_SUCCESS },
77 { "{ \"1\": 1 }", VINF_SUCCESS },
78 { "{ \"1\": 1, \"2\": 2 }", VINF_SUCCESS },
79 { "20", VINF_SUCCESS },
80 { "-20", VINF_SUCCESS },
81 { "{\"positive\":20}", VINF_SUCCESS },
82 { "{\"negative\":-20}", VINF_SUCCESS },
83 { "\"\\u0001\"", VINF_SUCCESS },
84 { "\"\\u000\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
85 { "\"\\u00\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
86 { "\"\\u0\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
87 { "\"\\u\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
88 { "\"\\uGhKl\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
89 { "\"\\u0000z\"", VERR_JSON_INVALID_CODEPOINT },
90 { "\"\\uffff\"", VERR_JSON_INVALID_CODEPOINT },
91 { "\"\\ufffe\"", VERR_JSON_INVALID_CODEPOINT },
92 { "\"\\ufffd\"", VINF_SUCCESS},
93 { "\"\\ufffd1\"", VINF_SUCCESS},
94 { "\"\\ufffd12\"", VINF_SUCCESS},
95 { "\"\\uD801\\udC37\\ud852\\uDf62\"", VINF_SUCCESS }, /* U+10437 U+24B62 */
96 { "\"\\uD801 \\udC37\"", VERR_JSON_MISSING_SURROGATE_PAIR },
97 { "\"\\uD801udC37\"", VERR_JSON_MISSING_SURROGATE_PAIR },
98 { "\"\\uD801\"", VERR_JSON_MISSING_SURROGATE_PAIR },
99 { "\"\\uD801\\\"", VERR_JSON_MISSING_SURROGATE_PAIR },
100 { "\"\\uD801\\u\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
101 { "\"\\uD801\\ud\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
102 { "\"\\uD801\\udc\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
103 { "\"\\uD801\\udc3\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
104 { "\"\\uD801\\uDc37\"", VINF_SUCCESS},
105 { "\"\\uDbff\\uDfff\"", VINF_SUCCESS},
106 };
107 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
108 {
109 RTERRINFOSTATIC ErrInfo;
110 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
111 int rc = RTJsonParseFromString(&hJsonVal, aTests[iTest].pszJson, RTErrInfoInitStatic(&ErrInfo));
112 if (rc != aTests[iTest].iRcResult)
113 {
114 if (RTErrInfoIsSet(&ErrInfo.Core))
115 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc\n%s",
116 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc, ErrInfo.Core.pszMsg);
117 else
118 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc",
119 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc);
120 }
121 else if (rc == VERR_JSON_MALFORMED && !RTErrInfoIsSet(&ErrInfo.Core))
122 RTTestFailed(hTest, "RTJsonParseFromString() did not return error info for \"%s\" failed", aTests[iTest].pszJson);
123 if (RT_SUCCESS(rc))
124 {
125 if (hJsonVal != NIL_RTJSONVAL)
126 RTJsonValueRelease(hJsonVal);
127 else
128 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
129 }
130 else if (hJsonVal != NIL_RTJSONVAL)
131 RTTestFailed(hTest, "RTJsonParseFromString() failed but a JSON value was returned\n");
132 }
133}
134
135/**
136 * Checks that methods not indended for the given type return the correct error.
137 */
138static void tstCorrectnessRcForInvalidType(RTTEST hTest, RTJSONVAL hJsonVal, RTJSONVALTYPE enmType)
139{
140 bool fSavedMayPanic = RTAssertSetMayPanic(false);
141 bool fSavedQuiet = RTAssertSetQuiet(true);
142
143 if ( enmType != RTJSONVALTYPE_OBJECT
144 && enmType != RTJSONVALTYPE_ARRAY)
145 {
146 /* The iterator API should return errors. */
147 RTJSONIT hJsonIt = NIL_RTJSONIT;
148 RTTEST_CHECK_RC(hTest, RTJsonIteratorBegin(hJsonVal, &hJsonIt), VERR_JSON_VALUE_INVALID_TYPE);
149 }
150
151 if (enmType != RTJSONVALTYPE_ARRAY)
152 {
153 /* The Array access methods should return errors. */
154 uint32_t cItems = 0;
155 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
156 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 0);
157 RTTEST_CHECK_RC(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems), VERR_JSON_VALUE_INVALID_TYPE);
158 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByIndex(hJsonVal, 0, &hJsonValItem), VERR_JSON_VALUE_INVALID_TYPE);
159 }
160
161 if (enmType != RTJSONVALTYPE_OBJECT)
162 {
163 /* The object access methods should return errors. */
164 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
165 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByName(hJsonVal, "test", &hJsonValMember), VERR_JSON_VALUE_INVALID_TYPE);
166 }
167
168 if (enmType != RTJSONVALTYPE_INTEGER)
169 {
170 int64_t i64Num = 0;
171 RTTEST_CHECK_RC(hTest, RTJsonValueQueryInteger(hJsonVal, &i64Num), VERR_JSON_VALUE_INVALID_TYPE);
172 }
173
174 if (enmType != RTJSONVALTYPE_NUMBER)
175 {
176 double rdNum = 0.0;
177 RTTEST_CHECK_RC(hTest, RTJsonValueQueryNumber(hJsonVal, &rdNum), VERR_JSON_VALUE_INVALID_TYPE);
178 }
179
180 if (enmType != RTJSONVALTYPE_STRING)
181 {
182 const char *psz = NULL;
183 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonVal) == NULL);
184 RTTEST_CHECK_RC(hTest, RTJsonValueQueryString(hJsonVal, &psz), VERR_JSON_VALUE_INVALID_TYPE);
185 }
186
187 RTAssertSetMayPanic(fSavedMayPanic);
188 RTAssertSetQuiet(fSavedQuiet);
189}
190
191/**
192 * Tests the array accessors.
193 */
194static void tstArray(RTTEST hTest, RTJSONVAL hJsonVal)
195{
196 uint32_t cItems = 0;
197 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 6);
198 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems));
199 RTTEST_CHECK(hTest, cItems == RTJsonValueGetArraySize(hJsonVal));
200
201 for (uint32_t i = 1; i <= 5; i++)
202 {
203 int64_t i64Num = 0;
204 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
205 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, i - 1, &hJsonValItem));
206 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_INTEGER);
207 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryInteger(hJsonValItem, &i64Num));
208 RTTEST_CHECK(hTest, i64Num == (int64_t)i);
209 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
210 }
211
212 /* Last should be string. */
213 const char *pszStr = NULL;
214 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
215 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, 5, &hJsonValItem));
216 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_STRING);
217 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryString(hJsonValItem, &pszStr));
218 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonValItem) == pszStr);
219 RTTEST_CHECK(hTest, strcmp(pszStr, "6") == 0);
220 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
221}
222
223/**
224 * Tests the iterator API for the given JSON array or object value.
225 */
226static void tstIterator(RTTEST hTest, RTJSONVAL hJsonVal)
227{
228 RTJSONIT hJsonIt = NIL_RTJSONIT;
229 int rc = RTJsonIteratorBegin(hJsonVal, &hJsonIt);
230 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
231 if (RT_SUCCESS(rc))
232 {
233 const char *pszName = NULL;
234 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
235 rc = RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName);
236 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
237 RTTEST_CHECK(hTest, pszName != NULL);
238 RTTEST_CHECK(hTest, hJsonValMember != NIL_RTJSONVAL);
239 while (RT_SUCCESS(rc))
240 {
241 RTJSONVALTYPE enmTypeMember = RTJsonValueGetType(hJsonValMember);
242 tstCorrectnessRcForInvalidType(hTest, hJsonValMember, enmTypeMember);
243
244 switch (enmTypeMember)
245 {
246 case RTJSONVALTYPE_OBJECT:
247 RTTEST_CHECK(hTest, strcmp(pszName, "subobject") == 0);
248 tstIterator(hTest, hJsonValMember);
249 break;
250 case RTJSONVALTYPE_ARRAY:
251 RTTEST_CHECK(hTest, strcmp(pszName, "array") == 0);
252 tstArray(hTest, hJsonValMember);
253 break;
254 case RTJSONVALTYPE_STRING:
255 {
256 RTTEST_CHECK(hTest, strcmp(pszName, "string") == 0);
257 const char *pszStr = NULL;
258 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryString(hJsonValMember, &pszStr));
259 RTTEST_CHECK(hTest, strcmp(pszStr, "test") == 0);
260 break;
261 }
262 case RTJSONVALTYPE_INTEGER:
263 {
264 RTTEST_CHECK(hTest, strcmp(pszName, "integer") == 0);
265 int64_t i64Num = 0;
266 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryInteger(hJsonValMember, &i64Num));
267 RTTEST_CHECK(hTest, i64Num == 100);
268 break;
269 }
270 case RTJSONVALTYPE_NUMBER:
271 {
272 RTTEST_CHECK(hTest, strcmp(pszName, "number") == 0);
273 double rdNum = 0.0;
274 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryNumber(hJsonValMember, &rdNum));
275 double const rdExpect = 22.22;
276 RTTEST_CHECK(hTest, rdNum == rdExpect);
277 break;
278 }
279 case RTJSONVALTYPE_NULL:
280 RTTEST_CHECK(hTest, strcmp(pszName, "null") == 0);
281 break;
282 case RTJSONVALTYPE_TRUE:
283 RTTEST_CHECK(hTest, strcmp(pszName, "true") == 0);
284 break;
285 case RTJSONVALTYPE_FALSE:
286 RTTEST_CHECK(hTest, strcmp(pszName, "false") == 0);
287 break;
288 default:
289 RTTestFailed(hTest, "Invalid JSON value type %u returned\n", enmTypeMember);
290 }
291
292 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValMember) == 1);
293 rc = RTJsonIteratorNext(hJsonIt);
294 RTTEST_CHECK(hTest, rc == VINF_SUCCESS || rc == VERR_JSON_ITERATOR_END);
295 if (RT_SUCCESS(rc))
296 RTTEST_CHECK_RC_OK(hTest, RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName));
297 }
298 RTJsonIteratorFree(hJsonIt);
299 }
300}
301
302/**
303 * Test that the parser returns the correct values for a valid JSON.
304 */
305static void tstCorrectness(RTTEST hTest)
306{
307 RTTestSub(hTest, "Correctness");
308
309 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
310 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonParseFromString(&hJsonVal, g_pszJson, NULL));
311
312 if (hJsonVal != NIL_RTJSONVAL)
313 {
314 RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonVal);
315 if (enmType == RTJSONVALTYPE_OBJECT)
316 {
317 /* Excercise the other non object APIs to return VERR_JSON_VALUE_INVALID_TYPE. */
318 tstCorrectnessRcForInvalidType(hTest, hJsonVal, enmType);
319 tstIterator(hTest, hJsonVal);
320 }
321 else
322 RTTestFailed(hTest, "RTJsonParseFromString() returned an invalid JSON value, expected OBJECT got %u\n", enmType);
323 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonVal) == 0);
324 }
325 else
326 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
327}
328
329int main(int argc, char **argv)
330{
331 RTTEST hTest;
332 int rc = RTTestInitExAndCreate(argc, &argv, 0, "tstRTJson", &hTest);
333 if (rc)
334 return rc;
335 RTTestBanner(hTest);
336
337 tstBasic(hTest);
338 tstCorrectness(hTest);
339 for (int i = 1; i < argc; i++)
340 {
341 RTTestSubF(hTest, "file %Rbn", argv[i]);
342 RTERRINFOSTATIC ErrInfo;
343 RTJSONVAL hFileValue = NIL_RTJSONVAL;
344 rc = RTJsonParseFromFile(&hFileValue, argv[i], RTErrInfoInitStatic(&ErrInfo));
345 if (RT_SUCCESS(rc))
346 RTJsonValueRelease(hFileValue);
347 else if (RTErrInfoIsSet(&ErrInfo.Core))
348 RTTestFailed(hTest, "%Rrc - %s", rc, ErrInfo.Core.pszMsg);
349 else
350 RTTestFailed(hTest, "%Rrc", rc);
351 }
352
353 /*
354 * Summary.
355 */
356 return RTTestSummaryAndDestroy(hTest);
357}
358
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette