VirtualBox

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

Last change on this file since 76321 was 76321, checked in by vboxsync, 6 years ago

iprt/mp-r0drv-linux.c: Attempt at fixing !CONFIG_SMP warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1/* $Id: tstRTJson.cpp 76321 2018-12-20 18:01:22Z 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.215389.xyz. 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
35
36/*********************************************************************************************************************************
37* Global Variables *
38*********************************************************************************************************************************/
39static const char g_szJson[] =
40 "{\n"
41 " \"integer\": 100,\n"
42 " \"number\": 22.22,\n"
43 " \"string\": \"test\",\n"
44 " \"array\": [1, 2, 3, 4, 5, \"6\"],\n"
45 " \"subobject\":\n"
46 " {\n"
47 " \"false\": false,\n"
48 " \"true\": true,\n"
49 " \"null\": null\n"
50 " }\n"
51 "}\n";
52
53/**
54 * Some basic tests to detect malformed JSON.
55 */
56static void tstBasic(RTTEST hTest)
57{
58 RTTestSub(hTest, "Basic valid/malformed tests");
59 static struct
60 {
61 const char *pszJson;
62 int iRcResult;
63 } const aTests[] =
64 {
65 { "", VERR_JSON_MALFORMED },
66 { ",", VERR_JSON_MALFORMED },
67 { ":", VERR_JSON_MALFORMED },
68 { " \n\t{", VERR_JSON_MALFORMED },
69 { "}", VERR_JSON_MALFORMED },
70 { "[", VERR_JSON_MALFORMED },
71 { "]", VERR_JSON_MALFORMED },
72 { "[ \"test\" : ", VERR_JSON_MALFORMED },
73 { "null", VINF_SUCCESS },
74 { "true", VINF_SUCCESS },
75 { "false", VINF_SUCCESS },
76 { "100", VINF_SUCCESS },
77 { "\"test\"", VINF_SUCCESS },
78 { "{ }", VINF_SUCCESS },
79 { "[ ]", VINF_SUCCESS },
80 { "[ 100, 200 ]", VINF_SUCCESS },
81 { "{ \"1\": 1 }", VINF_SUCCESS },
82 { "{ \"1\": 1, \"2\": 2 }", VINF_SUCCESS },
83 { "20", VINF_SUCCESS },
84 { "-20", VINF_SUCCESS },
85 { "{\"positive\":20}", VINF_SUCCESS },
86 { "{\"negative\":-20}", VINF_SUCCESS },
87 { "\"\\u0001\"", VINF_SUCCESS },
88 { "\"\\u000\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
89 { "\"\\u00\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
90 { "\"\\u0\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
91 { "\"\\u\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
92 { "\"\\uGhKl\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
93 { "\"\\u0000z\"", VERR_JSON_INVALID_CODEPOINT },
94 { "\"\\uffff\"", VERR_JSON_INVALID_CODEPOINT },
95 { "\"\\ufffe\"", VERR_JSON_INVALID_CODEPOINT },
96 { "\"\\ufffd\"", VINF_SUCCESS},
97 { "\"\\ufffd1\"", VINF_SUCCESS},
98 { "\"\\ufffd12\"", VINF_SUCCESS},
99 { "\"\\uD801\\udC37\\ud852\\uDf62\"", VINF_SUCCESS }, /* U+10437 U+24B62 */
100 { "\"\\uD801 \\udC37\"", VERR_JSON_MISSING_SURROGATE_PAIR },
101 { "\"\\uD801udC37\"", VERR_JSON_MISSING_SURROGATE_PAIR },
102 { "\"\\uD801\"", VERR_JSON_MISSING_SURROGATE_PAIR },
103 { "\"\\uD801\\\"", VERR_JSON_MISSING_SURROGATE_PAIR },
104 { "\"\\uD801\\u\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
105 { "\"\\uD801\\ud\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
106 { "\"\\uD801\\udc\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
107 { "\"\\uD801\\udc3\"", VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE },
108 { "\"\\uD801\\uDc37\"", VINF_SUCCESS},
109 { "\"\\uDbff\\uDfff\"", VINF_SUCCESS},
110 { "\"\\t\\n\\b\\f\\r\\\\\\/\"", VINF_SUCCESS},
111 };
112AssertFailed(); // DONT COMMIT THIS!
113 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
114 {
115 RTERRINFOSTATIC ErrInfo;
116 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
117 int rc = RTJsonParseFromString(&hJsonVal, aTests[iTest].pszJson, RTErrInfoInitStatic(&ErrInfo));
118 if (rc != aTests[iTest].iRcResult)
119 {
120 if (RTErrInfoIsSet(&ErrInfo.Core))
121 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc\n%s",
122 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc, ErrInfo.Core.pszMsg);
123 else
124 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc",
125 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc);
126 }
127 else if (rc == VERR_JSON_MALFORMED && !RTErrInfoIsSet(&ErrInfo.Core))
128 RTTestFailed(hTest, "RTJsonParseFromString() did not return error info for \"%s\" failed", aTests[iTest].pszJson);
129 if (RT_SUCCESS(rc))
130 {
131 if (hJsonVal != NIL_RTJSONVAL)
132 RTJsonValueRelease(hJsonVal);
133 else
134 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
135 }
136 else if (hJsonVal != NIL_RTJSONVAL)
137 RTTestFailed(hTest, "RTJsonParseFromString() failed but a JSON value was returned\n");
138 }
139}
140
141/**
142 * Checks that methods not indended for the given type return the correct error.
143 */
144static void tstCorrectnessRcForInvalidType(RTTEST hTest, RTJSONVAL hJsonVal, RTJSONVALTYPE enmType)
145{
146 bool fSavedMayPanic = RTAssertSetMayPanic(false);
147 bool fSavedQuiet = RTAssertSetQuiet(true);
148
149 if ( enmType != RTJSONVALTYPE_OBJECT
150 && enmType != RTJSONVALTYPE_ARRAY)
151 {
152 /* The iterator API should return errors. */
153 RTJSONIT hJsonIt = NIL_RTJSONIT;
154 RTTEST_CHECK_RC(hTest, RTJsonIteratorBegin(hJsonVal, &hJsonIt), VERR_JSON_VALUE_INVALID_TYPE);
155 }
156
157 if (enmType != RTJSONVALTYPE_ARRAY)
158 {
159 /* The Array access methods should return errors. */
160 uint32_t cItems = 0;
161 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
162 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 0);
163 RTTEST_CHECK_RC(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems), VERR_JSON_VALUE_INVALID_TYPE);
164 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByIndex(hJsonVal, 0, &hJsonValItem), VERR_JSON_VALUE_INVALID_TYPE);
165 }
166
167 if (enmType != RTJSONVALTYPE_OBJECT)
168 {
169 /* The object access methods should return errors. */
170 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
171 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByName(hJsonVal, "test", &hJsonValMember), VERR_JSON_VALUE_INVALID_TYPE);
172 }
173
174 if (enmType != RTJSONVALTYPE_INTEGER)
175 {
176 int64_t i64Num = 0;
177 RTTEST_CHECK_RC(hTest, RTJsonValueQueryInteger(hJsonVal, &i64Num), VERR_JSON_VALUE_INVALID_TYPE);
178 }
179
180 if (enmType != RTJSONVALTYPE_NUMBER)
181 {
182 double rdNum = 0.0;
183 RTTEST_CHECK_RC(hTest, RTJsonValueQueryNumber(hJsonVal, &rdNum), VERR_JSON_VALUE_INVALID_TYPE);
184 }
185
186 if (enmType != RTJSONVALTYPE_STRING)
187 {
188 const char *psz = NULL;
189 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonVal) == NULL);
190 RTTEST_CHECK_RC(hTest, RTJsonValueQueryString(hJsonVal, &psz), VERR_JSON_VALUE_INVALID_TYPE);
191 }
192
193 RTAssertSetMayPanic(fSavedMayPanic);
194 RTAssertSetQuiet(fSavedQuiet);
195}
196
197/**
198 * Tests the array accessors.
199 */
200static void tstArray(RTTEST hTest, RTJSONVAL hJsonVal)
201{
202 uint32_t cItems = 0;
203 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 6);
204 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems));
205 RTTEST_CHECK(hTest, cItems == RTJsonValueGetArraySize(hJsonVal));
206
207 for (uint32_t i = 1; i <= 5; i++)
208 {
209 int64_t i64Num = 0;
210 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
211 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, i - 1, &hJsonValItem));
212 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_INTEGER);
213 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryInteger(hJsonValItem, &i64Num));
214 RTTEST_CHECK(hTest, i64Num == (int64_t)i);
215 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
216 }
217
218 /* Last should be string. */
219 const char *pszStr = NULL;
220 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
221 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, 5, &hJsonValItem));
222 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_STRING);
223 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryString(hJsonValItem, &pszStr));
224 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonValItem) == pszStr);
225 RTTEST_CHECK(hTest, strcmp(pszStr, "6") == 0);
226 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
227}
228
229/**
230 * Tests the iterator API for the given JSON array or object value.
231 */
232static void tstIterator(RTTEST hTest, RTJSONVAL hJsonVal)
233{
234 RTJSONIT hJsonIt = NIL_RTJSONIT;
235 int rc = RTJsonIteratorBegin(hJsonVal, &hJsonIt);
236 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
237 if (RT_SUCCESS(rc))
238 {
239 const char *pszName = NULL;
240 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
241 rc = RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName);
242 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
243 RTTEST_CHECK(hTest, pszName != NULL);
244 RTTEST_CHECK(hTest, hJsonValMember != NIL_RTJSONVAL);
245 while (RT_SUCCESS(rc))
246 {
247 RTJSONVALTYPE enmTypeMember = RTJsonValueGetType(hJsonValMember);
248 tstCorrectnessRcForInvalidType(hTest, hJsonValMember, enmTypeMember);
249
250 switch (enmTypeMember)
251 {
252 case RTJSONVALTYPE_OBJECT:
253 RTTEST_CHECK(hTest, strcmp(pszName, "subobject") == 0);
254 tstIterator(hTest, hJsonValMember);
255 break;
256 case RTJSONVALTYPE_ARRAY:
257 RTTEST_CHECK(hTest, strcmp(pszName, "array") == 0);
258 tstArray(hTest, hJsonValMember);
259 break;
260 case RTJSONVALTYPE_STRING:
261 {
262 RTTEST_CHECK(hTest, strcmp(pszName, "string") == 0);
263 const char *pszStr = NULL;
264 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryString(hJsonValMember, &pszStr));
265 RTTEST_CHECK(hTest, strcmp(pszStr, "test") == 0);
266 break;
267 }
268 case RTJSONVALTYPE_INTEGER:
269 {
270 RTTEST_CHECK(hTest, strcmp(pszName, "integer") == 0);
271 int64_t i64Num = 0;
272 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryInteger(hJsonValMember, &i64Num));
273 RTTEST_CHECK(hTest, i64Num == 100);
274 break;
275 }
276 case RTJSONVALTYPE_NUMBER:
277 {
278 RTTEST_CHECK(hTest, strcmp(pszName, "number") == 0);
279 double rdNum = 0.0;
280 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryNumber(hJsonValMember, &rdNum));
281 double const rdExpect = 22.22;
282 RTTEST_CHECK(hTest, rdNum == rdExpect);
283 break;
284 }
285 case RTJSONVALTYPE_NULL:
286 RTTEST_CHECK(hTest, strcmp(pszName, "null") == 0);
287 break;
288 case RTJSONVALTYPE_TRUE:
289 RTTEST_CHECK(hTest, strcmp(pszName, "true") == 0);
290 break;
291 case RTJSONVALTYPE_FALSE:
292 RTTEST_CHECK(hTest, strcmp(pszName, "false") == 0);
293 break;
294 default:
295 RTTestFailed(hTest, "Invalid JSON value type %u returned\n", enmTypeMember);
296 }
297
298 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValMember) == 1);
299 rc = RTJsonIteratorNext(hJsonIt);
300 RTTEST_CHECK(hTest, rc == VINF_SUCCESS || rc == VERR_JSON_ITERATOR_END);
301 if (RT_SUCCESS(rc))
302 RTTEST_CHECK_RC_OK(hTest, RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName));
303 }
304 RTJsonIteratorFree(hJsonIt);
305 }
306}
307
308/**
309 * Test that the parser returns the correct values for a valid JSON.
310 */
311static void tstCorrectness(RTTEST hTest)
312{
313 RTTestSub(hTest, "Correctness");
314
315 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
316 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonParseFromString(&hJsonVal, g_szJson, NULL));
317
318 if (hJsonVal != NIL_RTJSONVAL)
319 {
320 RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonVal);
321 if (enmType == RTJSONVALTYPE_OBJECT)
322 {
323 /* Excercise the other non object APIs to return VERR_JSON_VALUE_INVALID_TYPE. */
324 tstCorrectnessRcForInvalidType(hTest, hJsonVal, enmType);
325 tstIterator(hTest, hJsonVal);
326 }
327 else
328 RTTestFailed(hTest, "RTJsonParseFromString() returned an invalid JSON value, expected OBJECT got %u\n", enmType);
329 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonVal) == 0);
330 }
331 else
332 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
333}
334
335int main(int argc, char **argv)
336{
337 RTTEST hTest;
338 int rc = RTTestInitExAndCreate(argc, &argv, 0, "tstRTJson", &hTest);
339 if (rc)
340 return rc;
341 RTTestBanner(hTest);
342
343 tstBasic(hTest);
344 tstCorrectness(hTest);
345 for (int i = 1; i < argc; i++)
346 {
347 RTTestSubF(hTest, "file %Rbn", argv[i]);
348 RTERRINFOSTATIC ErrInfo;
349 RTJSONVAL hFileValue = NIL_RTJSONVAL;
350 rc = RTJsonParseFromFile(&hFileValue, argv[i], RTErrInfoInitStatic(&ErrInfo));
351 if (RT_SUCCESS(rc))
352 RTJsonValueRelease(hFileValue);
353 else if (RTErrInfoIsSet(&ErrInfo.Core))
354 RTTestFailed(hTest, "%Rrc - %s", rc, ErrInfo.Core.pszMsg);
355 else
356 RTTestFailed(hTest, "%Rrc", rc);
357 }
358
359 /*
360 * Summary.
361 */
362 return RTTestSummaryAndDestroy(hTest);
363}
364
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