VirtualBox

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

Last change on this file since 104626 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

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

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