VirtualBox

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

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

IPRT/json: Made the json parser report error info. Currently we're failing to parse negative numbers, added this to tstRTJson. bugref:9167

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