VirtualBox

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

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

tstRTJson: nit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
Line 
1/* $Id: tstRTJson.cpp 74180 2018-09-10 10:47:47Z 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
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 };
111 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
112 {
113 RTERRINFOSTATIC ErrInfo;
114 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
115 int rc = RTJsonParseFromString(&hJsonVal, aTests[iTest].pszJson, RTErrInfoInitStatic(&ErrInfo));
116 if (rc != aTests[iTest].iRcResult)
117 {
118 if (RTErrInfoIsSet(&ErrInfo.Core))
119 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc\n%s",
120 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc, ErrInfo.Core.pszMsg);
121 else
122 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc",
123 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc);
124 }
125 else if (rc == VERR_JSON_MALFORMED && !RTErrInfoIsSet(&ErrInfo.Core))
126 RTTestFailed(hTest, "RTJsonParseFromString() did not return error info for \"%s\" failed", aTests[iTest].pszJson);
127 if (RT_SUCCESS(rc))
128 {
129 if (hJsonVal != NIL_RTJSONVAL)
130 RTJsonValueRelease(hJsonVal);
131 else
132 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
133 }
134 else if (hJsonVal != NIL_RTJSONVAL)
135 RTTestFailed(hTest, "RTJsonParseFromString() failed but a JSON value was returned\n");
136 }
137}
138
139/**
140 * Checks that methods not indended for the given type return the correct error.
141 */
142static void tstCorrectnessRcForInvalidType(RTTEST hTest, RTJSONVAL hJsonVal, RTJSONVALTYPE enmType)
143{
144 bool fSavedMayPanic = RTAssertSetMayPanic(false);
145 bool fSavedQuiet = RTAssertSetQuiet(true);
146
147 if ( enmType != RTJSONVALTYPE_OBJECT
148 && enmType != RTJSONVALTYPE_ARRAY)
149 {
150 /* The iterator API should return errors. */
151 RTJSONIT hJsonIt = NIL_RTJSONIT;
152 RTTEST_CHECK_RC(hTest, RTJsonIteratorBegin(hJsonVal, &hJsonIt), VERR_JSON_VALUE_INVALID_TYPE);
153 }
154
155 if (enmType != RTJSONVALTYPE_ARRAY)
156 {
157 /* The Array access methods should return errors. */
158 uint32_t cItems = 0;
159 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
160 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 0);
161 RTTEST_CHECK_RC(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems), VERR_JSON_VALUE_INVALID_TYPE);
162 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByIndex(hJsonVal, 0, &hJsonValItem), VERR_JSON_VALUE_INVALID_TYPE);
163 }
164
165 if (enmType != RTJSONVALTYPE_OBJECT)
166 {
167 /* The object access methods should return errors. */
168 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
169 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByName(hJsonVal, "test", &hJsonValMember), VERR_JSON_VALUE_INVALID_TYPE);
170 }
171
172 if (enmType != RTJSONVALTYPE_INTEGER)
173 {
174 int64_t i64Num = 0;
175 RTTEST_CHECK_RC(hTest, RTJsonValueQueryInteger(hJsonVal, &i64Num), VERR_JSON_VALUE_INVALID_TYPE);
176 }
177
178 if (enmType != RTJSONVALTYPE_NUMBER)
179 {
180 double rdNum = 0.0;
181 RTTEST_CHECK_RC(hTest, RTJsonValueQueryNumber(hJsonVal, &rdNum), VERR_JSON_VALUE_INVALID_TYPE);
182 }
183
184 if (enmType != RTJSONVALTYPE_STRING)
185 {
186 const char *psz = NULL;
187 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonVal) == NULL);
188 RTTEST_CHECK_RC(hTest, RTJsonValueQueryString(hJsonVal, &psz), VERR_JSON_VALUE_INVALID_TYPE);
189 }
190
191 RTAssertSetMayPanic(fSavedMayPanic);
192 RTAssertSetQuiet(fSavedQuiet);
193}
194
195/**
196 * Tests the array accessors.
197 */
198static void tstArray(RTTEST hTest, RTJSONVAL hJsonVal)
199{
200 uint32_t cItems = 0;
201 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 6);
202 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems));
203 RTTEST_CHECK(hTest, cItems == RTJsonValueGetArraySize(hJsonVal));
204
205 for (uint32_t i = 1; i <= 5; i++)
206 {
207 int64_t i64Num = 0;
208 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
209 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, i - 1, &hJsonValItem));
210 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_INTEGER);
211 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryInteger(hJsonValItem, &i64Num));
212 RTTEST_CHECK(hTest, i64Num == (int64_t)i);
213 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
214 }
215
216 /* Last should be string. */
217 const char *pszStr = NULL;
218 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
219 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, 5, &hJsonValItem));
220 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_STRING);
221 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryString(hJsonValItem, &pszStr));
222 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonValItem) == pszStr);
223 RTTEST_CHECK(hTest, strcmp(pszStr, "6") == 0);
224 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
225}
226
227/**
228 * Tests the iterator API for the given JSON array or object value.
229 */
230static void tstIterator(RTTEST hTest, RTJSONVAL hJsonVal)
231{
232 RTJSONIT hJsonIt = NIL_RTJSONIT;
233 int rc = RTJsonIteratorBegin(hJsonVal, &hJsonIt);
234 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
235 if (RT_SUCCESS(rc))
236 {
237 const char *pszName = NULL;
238 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
239 rc = RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName);
240 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
241 RTTEST_CHECK(hTest, pszName != NULL);
242 RTTEST_CHECK(hTest, hJsonValMember != NIL_RTJSONVAL);
243 while (RT_SUCCESS(rc))
244 {
245 RTJSONVALTYPE enmTypeMember = RTJsonValueGetType(hJsonValMember);
246 tstCorrectnessRcForInvalidType(hTest, hJsonValMember, enmTypeMember);
247
248 switch (enmTypeMember)
249 {
250 case RTJSONVALTYPE_OBJECT:
251 RTTEST_CHECK(hTest, strcmp(pszName, "subobject") == 0);
252 tstIterator(hTest, hJsonValMember);
253 break;
254 case RTJSONVALTYPE_ARRAY:
255 RTTEST_CHECK(hTest, strcmp(pszName, "array") == 0);
256 tstArray(hTest, hJsonValMember);
257 break;
258 case RTJSONVALTYPE_STRING:
259 {
260 RTTEST_CHECK(hTest, strcmp(pszName, "string") == 0);
261 const char *pszStr = NULL;
262 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryString(hJsonValMember, &pszStr));
263 RTTEST_CHECK(hTest, strcmp(pszStr, "test") == 0);
264 break;
265 }
266 case RTJSONVALTYPE_INTEGER:
267 {
268 RTTEST_CHECK(hTest, strcmp(pszName, "integer") == 0);
269 int64_t i64Num = 0;
270 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryInteger(hJsonValMember, &i64Num));
271 RTTEST_CHECK(hTest, i64Num == 100);
272 break;
273 }
274 case RTJSONVALTYPE_NUMBER:
275 {
276 RTTEST_CHECK(hTest, strcmp(pszName, "number") == 0);
277 double rdNum = 0.0;
278 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryNumber(hJsonValMember, &rdNum));
279 double const rdExpect = 22.22;
280 RTTEST_CHECK(hTest, rdNum == rdExpect);
281 break;
282 }
283 case RTJSONVALTYPE_NULL:
284 RTTEST_CHECK(hTest, strcmp(pszName, "null") == 0);
285 break;
286 case RTJSONVALTYPE_TRUE:
287 RTTEST_CHECK(hTest, strcmp(pszName, "true") == 0);
288 break;
289 case RTJSONVALTYPE_FALSE:
290 RTTEST_CHECK(hTest, strcmp(pszName, "false") == 0);
291 break;
292 default:
293 RTTestFailed(hTest, "Invalid JSON value type %u returned\n", enmTypeMember);
294 }
295
296 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValMember) == 1);
297 rc = RTJsonIteratorNext(hJsonIt);
298 RTTEST_CHECK(hTest, rc == VINF_SUCCESS || rc == VERR_JSON_ITERATOR_END);
299 if (RT_SUCCESS(rc))
300 RTTEST_CHECK_RC_OK(hTest, RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName));
301 }
302 RTJsonIteratorFree(hJsonIt);
303 }
304}
305
306/**
307 * Test that the parser returns the correct values for a valid JSON.
308 */
309static void tstCorrectness(RTTEST hTest)
310{
311 RTTestSub(hTest, "Correctness");
312
313 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
314 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonParseFromString(&hJsonVal, g_szJson, NULL));
315
316 if (hJsonVal != NIL_RTJSONVAL)
317 {
318 RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonVal);
319 if (enmType == RTJSONVALTYPE_OBJECT)
320 {
321 /* Excercise the other non object APIs to return VERR_JSON_VALUE_INVALID_TYPE. */
322 tstCorrectnessRcForInvalidType(hTest, hJsonVal, enmType);
323 tstIterator(hTest, hJsonVal);
324 }
325 else
326 RTTestFailed(hTest, "RTJsonParseFromString() returned an invalid JSON value, expected OBJECT got %u\n", enmType);
327 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonVal) == 0);
328 }
329 else
330 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
331}
332
333int main(int argc, char **argv)
334{
335 RTTEST hTest;
336 int rc = RTTestInitExAndCreate(argc, &argv, 0, "tstRTJson", &hTest);
337 if (rc)
338 return rc;
339 RTTestBanner(hTest);
340
341 tstBasic(hTest);
342 tstCorrectness(hTest);
343 for (int i = 1; i < argc; i++)
344 {
345 RTTestSubF(hTest, "file %Rbn", argv[i]);
346 RTERRINFOSTATIC ErrInfo;
347 RTJSONVAL hFileValue = NIL_RTJSONVAL;
348 rc = RTJsonParseFromFile(&hFileValue, argv[i], RTErrInfoInitStatic(&ErrInfo));
349 if (RT_SUCCESS(rc))
350 RTJsonValueRelease(hFileValue);
351 else if (RTErrInfoIsSet(&ErrInfo.Core))
352 RTTestFailed(hTest, "%Rrc - %s", rc, ErrInfo.Core.pszMsg);
353 else
354 RTTestFailed(hTest, "%Rrc", rc);
355 }
356
357 /*
358 * Summary.
359 */
360 return RTTestSummaryAndDestroy(hTest);
361}
362
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