VirtualBox

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

Last change on this file since 62819 was 62727, checked in by vboxsync, 8 years ago

IPRT/testcases: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: tstRTJson.cpp 62727 2016-07-30 00:20:48Z vboxsync $ */
2/** @file
3 * IPRT Testcase - JSON API.
4 */
5
6/*
7 * Copyright (C) 2016 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 };
79 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
80 {
81 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
82 int rc = RTJsonParseFromString(&hJsonVal, aTests[iTest].pszJson, NULL);
83 if (rc != aTests[iTest].iRcResult)
84 RTTestFailed(hTest, "RTJsonParseFromString() for \"%s\" failed, expected %Rrc got %Rrc\n",
85 aTests[iTest].pszJson, aTests[iTest].iRcResult, rc);
86 if (RT_SUCCESS(rc))
87 {
88 if (hJsonVal != NIL_RTJSONVAL)
89 RTJsonValueRelease(hJsonVal);
90 else
91 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
92 }
93 else if (hJsonVal != NIL_RTJSONVAL)
94 RTTestFailed(hTest, "RTJsonParseFromString() failed but a JSON value was returned\n");
95 }
96}
97
98/**
99 * Checks that methods not indended for the given type return the correct error.
100 */
101static void tstCorrectnessRcForInvalidType(RTTEST hTest, RTJSONVAL hJsonVal, RTJSONVALTYPE enmType)
102{
103#ifndef RT_STRICT /* Enable manually if assertions are enabled or it will assert all over the place for debug builds. */
104/** @todo you can disable assertions and all the noise. See RTAssertSetMayPanic and RTAssertSetQuiet. */
105 if ( enmType != RTJSONVALTYPE_OBJECT
106 && enmType != RTJSONVALTYPE_ARRAY)
107 {
108 /* The iterator API should return errors. */
109 RTJSONIT hJsonIt = NIL_RTJSONIT;
110 RTTEST_CHECK_RC(hTest, RTJsonIteratorBegin(hJsonVal, &hJsonIt), VERR_JSON_VALUE_INVALID_TYPE);
111 }
112
113 if (enmType != RTJSONVALTYPE_ARRAY)
114 {
115 /* The Array access methods should return errors. */
116 uint32_t cItems = 0;
117 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
118 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 0);
119 RTTEST_CHECK_RC(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems), VERR_JSON_VALUE_INVALID_TYPE);
120 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByIndex(hJsonVal, 0, &hJsonValItem), VERR_JSON_VALUE_INVALID_TYPE);
121 }
122
123 if (enmType != RTJSONVALTYPE_OBJECT)
124 {
125 /* The object access methods should return errors. */
126 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
127 RTTEST_CHECK_RC(hTest, RTJsonValueQueryByName(hJsonVal, "test", &hJsonValMember), VERR_JSON_VALUE_INVALID_TYPE);
128 }
129
130 if (enmType != RTJSONVALTYPE_NUMBER)
131 {
132 int64_t i64Num = 0;
133 RTTEST_CHECK_RC(hTest, RTJsonValueQueryInteger(hJsonVal, &i64Num), VERR_JSON_VALUE_INVALID_TYPE);
134 }
135
136 if (enmType != RTJSONVALTYPE_STRING)
137 {
138 const char *psz = NULL;
139 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonVal) == NULL);
140 RTTEST_CHECK_RC(hTest, RTJsonValueQueryString(hJsonVal, &psz), VERR_JSON_VALUE_INVALID_TYPE);
141 }
142#else
143 RT_NOREF3(hTest, hJsonVal, enmType);
144#endif
145}
146
147/**
148 * Tests the array accessors.
149 */
150static void tstArray(RTTEST hTest, RTJSONVAL hJsonVal)
151{
152 uint32_t cItems = 0;
153 RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 6);
154 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems));
155 RTTEST_CHECK(hTest, cItems == RTJsonValueGetArraySize(hJsonVal));
156
157 for (uint32_t i = 1; i <= 5; i++)
158 {
159 int64_t i64Num = 0;
160 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
161 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, i - 1, &hJsonValItem));
162 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_NUMBER);
163 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryInteger(hJsonValItem, &i64Num));
164 RTTEST_CHECK(hTest, i64Num == (int64_t)i);
165 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
166 }
167
168 /* Last should be string. */
169 const char *pszStr = NULL;
170 RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
171 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, 5, &hJsonValItem));
172 RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_STRING);
173 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryString(hJsonValItem, &pszStr));
174 RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonValItem) == pszStr);
175 RTTEST_CHECK(hTest, strcmp(pszStr, "6") == 0);
176 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
177}
178
179/**
180 * Tests the iterator API for the given JSON array or object value.
181 */
182static void tstIterator(RTTEST hTest, RTJSONVAL hJsonVal)
183{
184 RTJSONIT hJsonIt = NIL_RTJSONIT;
185 int rc = RTJsonIteratorBegin(hJsonVal, &hJsonIt);
186 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
187 if (RT_SUCCESS(rc))
188 {
189 const char *pszName = NULL;
190 RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
191 rc = RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName);
192 RTTEST_CHECK(hTest, RT_SUCCESS(rc));
193 RTTEST_CHECK(hTest, pszName != NULL);
194 RTTEST_CHECK(hTest, hJsonValMember != NIL_RTJSONVAL);
195 while (RT_SUCCESS(rc))
196 {
197 RTJSONVALTYPE enmTypeMember = RTJsonValueGetType(hJsonValMember);
198 tstCorrectnessRcForInvalidType(hTest, hJsonValMember, enmTypeMember);
199
200 switch (enmTypeMember)
201 {
202 case RTJSONVALTYPE_OBJECT:
203 RTTEST_CHECK(hTest, strcmp(pszName, "subobject") == 0);
204 tstIterator(hTest, hJsonValMember);
205 break;
206 case RTJSONVALTYPE_ARRAY:
207 RTTEST_CHECK(hTest, strcmp(pszName, "array") == 0);
208 tstArray(hTest, hJsonValMember);
209 break;
210 case RTJSONVALTYPE_STRING:
211 {
212 RTTEST_CHECK(hTest, strcmp(pszName, "string") == 0);
213 const char *pszStr = NULL;
214 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryString(hJsonValMember, &pszStr));
215 RTTEST_CHECK(hTest, strcmp(pszStr, "test") == 0);
216 break;
217 }
218 case RTJSONVALTYPE_NUMBER:
219 {
220 RTTEST_CHECK(hTest, strcmp(pszName, "number") == 0);
221 int64_t i64Num = 0;
222 RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryInteger(hJsonValMember, &i64Num));
223 RTTEST_CHECK(hTest, i64Num == 100);
224 break;
225 }
226 case RTJSONVALTYPE_NULL:
227 RTTEST_CHECK(hTest, strcmp(pszName, "null") == 0);
228 break;
229 case RTJSONVALTYPE_TRUE:
230 RTTEST_CHECK(hTest, strcmp(pszName, "true") == 0);
231 break;
232 case RTJSONVALTYPE_FALSE:
233 RTTEST_CHECK(hTest, strcmp(pszName, "false") == 0);
234 break;
235 default:
236 RTTestFailed(hTest, "Invalid JSON value type %u returned\n", enmTypeMember);
237 }
238
239 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValMember) == 1);
240 rc = RTJsonIteratorNext(hJsonIt);
241 RTTEST_CHECK(hTest, rc == VINF_SUCCESS || rc == VERR_JSON_ITERATOR_END);
242 if (RT_SUCCESS(rc))
243 RTTEST_CHECK_RC_OK(hTest, RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName));
244 }
245 RTJsonIteratorFree(hJsonIt);
246 }
247}
248
249/**
250 * Test that the parser returns the correct values for a valid JSON.
251 */
252static void tstCorrectness(RTTEST hTest)
253{
254 RTTestSub(hTest, "Correctness");
255
256 RTJSONVAL hJsonVal = NIL_RTJSONVAL;
257 RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonParseFromString(&hJsonVal, g_pszJson, NULL));
258
259 if (hJsonVal != NIL_RTJSONVAL)
260 {
261 RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonVal);
262 if (enmType == RTJSONVALTYPE_OBJECT)
263 {
264 /* Excercise the other non object APIs to return VERR_JSON_VALUE_INVALID_TYPE. */
265 tstCorrectnessRcForInvalidType(hTest, hJsonVal, enmType);
266 tstIterator(hTest, hJsonVal);
267 }
268 else
269 RTTestFailed(hTest, "RTJsonParseFromString() returned an invalid JSON value, expected OBJECT got %u\n", enmType);
270 RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonVal) == 0);
271 }
272 else
273 RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
274}
275
276int main()
277{
278 RTTEST hTest;
279 int rc = RTTestInitAndCreate("tstRTJson", &hTest);
280 if (rc)
281 return rc;
282 RTTestBanner(hTest);
283
284 tstBasic(hTest);
285 tstCorrectness(hTest);
286
287 /*
288 * Summary.
289 */
290 return RTTestSummaryAndDestroy(hTest);
291}
292
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