1 | /* $Id: tstRTJson.cpp 62021 2016-07-05 09:34:17Z 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 |
|
---|
35 | static 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 | */
|
---|
51 | static 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 | */
|
---|
101 | static 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 | if ( enmType != RTJSONVALTYPE_OBJECT
|
---|
105 | && enmType != RTJSONVALTYPE_ARRAY)
|
---|
106 | {
|
---|
107 | /* The iterator API should return errors. */
|
---|
108 | RTJSONIT hJsonIt = NIL_RTJSONIT;
|
---|
109 | RTTEST_CHECK_RC(hTest, RTJsonIteratorBegin(hJsonVal, &hJsonIt), VERR_JSON_VALUE_INVALID_TYPE);
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (enmType != RTJSONVALTYPE_ARRAY)
|
---|
113 | {
|
---|
114 | /* The Array access methods should return errors. */
|
---|
115 | uint32_t cItems = 0;
|
---|
116 | RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
|
---|
117 | RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 0);
|
---|
118 | RTTEST_CHECK_RC(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems), VERR_JSON_VALUE_INVALID_TYPE);
|
---|
119 | RTTEST_CHECK_RC(hTest, RTJsonValueQueryByIndex(hJsonVal, 0, &hJsonValItem), VERR_JSON_VALUE_INVALID_TYPE);
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (enmType != RTJSONVALTYPE_OBJECT)
|
---|
123 | {
|
---|
124 | /* The object access methods should return errors. */
|
---|
125 | RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
|
---|
126 | RTTEST_CHECK_RC(hTest, RTJsonValueQueryByName(hJsonVal, "test", &hJsonValMember), VERR_JSON_VALUE_INVALID_TYPE);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (enmType != RTJSONVALTYPE_NUMBER)
|
---|
130 | {
|
---|
131 | int64_t i64Num = 0;
|
---|
132 | RTTEST_CHECK_RC(hTest, RTJsonValueQueryInteger(hJsonVal, &i64Num), VERR_JSON_VALUE_INVALID_TYPE);
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (enmType != RTJSONVALTYPE_STRING)
|
---|
136 | {
|
---|
137 | const char *psz = NULL;
|
---|
138 | RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonVal) == NULL);
|
---|
139 | RTTEST_CHECK_RC(hTest, RTJsonValueQueryString(hJsonVal, &psz), VERR_JSON_VALUE_INVALID_TYPE);
|
---|
140 | }
|
---|
141 | #endif
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Tests the array accessors.
|
---|
146 | */
|
---|
147 | static void tstArray(RTTEST hTest, RTJSONVAL hJsonVal)
|
---|
148 | {
|
---|
149 | uint32_t cItems = 0;
|
---|
150 | RTTEST_CHECK(hTest, RTJsonValueGetArraySize(hJsonVal) == 6);
|
---|
151 | RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryArraySize(hJsonVal, &cItems));
|
---|
152 | RTTEST_CHECK(hTest, cItems == RTJsonValueGetArraySize(hJsonVal));
|
---|
153 |
|
---|
154 | for (uint32_t i = 1; i <= 5; i++)
|
---|
155 | {
|
---|
156 | int64_t i64Num = 0;
|
---|
157 | RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
|
---|
158 | RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, i - 1, &hJsonValItem));
|
---|
159 | RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_NUMBER);
|
---|
160 | RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryInteger(hJsonValItem, &i64Num));
|
---|
161 | RTTEST_CHECK(hTest, i64Num == (int64_t)i);
|
---|
162 | RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Last should be string. */
|
---|
166 | const char *pszStr = NULL;
|
---|
167 | RTJSONVAL hJsonValItem = NIL_RTJSONVAL;
|
---|
168 | RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryByIndex(hJsonVal, 5, &hJsonValItem));
|
---|
169 | RTTEST_CHECK(hTest, RTJsonValueGetType(hJsonValItem) == RTJSONVALTYPE_STRING);
|
---|
170 | RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonValueQueryString(hJsonValItem, &pszStr));
|
---|
171 | RTTEST_CHECK(hTest, RTJsonValueGetString(hJsonValItem) == pszStr);
|
---|
172 | RTTEST_CHECK(hTest, strcmp(pszStr, "6") == 0);
|
---|
173 | RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValItem) == 1);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Tests the iterator API for the given JSON array or object value.
|
---|
178 | */
|
---|
179 | static void tstIterator(RTTEST hTest, RTJSONVAL hJsonVal)
|
---|
180 | {
|
---|
181 | RTJSONIT hJsonIt = NIL_RTJSONIT;
|
---|
182 | int rc = RTJsonIteratorBegin(hJsonVal, &hJsonIt);
|
---|
183 | RTTEST_CHECK(hTest, RT_SUCCESS(rc));
|
---|
184 | if (RT_SUCCESS(rc))
|
---|
185 | {
|
---|
186 | const char *pszName = NULL;
|
---|
187 | RTJSONVAL hJsonValMember = NIL_RTJSONVAL;
|
---|
188 | rc = RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName);
|
---|
189 | RTTEST_CHECK(hTest, RT_SUCCESS(rc));
|
---|
190 | RTTEST_CHECK(hTest, pszName != NULL);
|
---|
191 | RTTEST_CHECK(hTest, hJsonValMember != NIL_RTJSONVAL);
|
---|
192 | while (RT_SUCCESS(rc))
|
---|
193 | {
|
---|
194 | RTJSONVALTYPE enmTypeMember = RTJsonValueGetType(hJsonValMember);
|
---|
195 | tstCorrectnessRcForInvalidType(hTest, hJsonValMember, enmTypeMember);
|
---|
196 |
|
---|
197 | switch (enmTypeMember)
|
---|
198 | {
|
---|
199 | case RTJSONVALTYPE_OBJECT:
|
---|
200 | RTTEST_CHECK(hTest, strcmp(pszName, "subobject") == 0);
|
---|
201 | tstIterator(hTest, hJsonValMember);
|
---|
202 | break;
|
---|
203 | case RTJSONVALTYPE_ARRAY:
|
---|
204 | RTTEST_CHECK(hTest, strcmp(pszName, "array") == 0);
|
---|
205 | tstArray(hTest, hJsonValMember);
|
---|
206 | break;
|
---|
207 | case RTJSONVALTYPE_STRING:
|
---|
208 | {
|
---|
209 | RTTEST_CHECK(hTest, strcmp(pszName, "string") == 0);
|
---|
210 | const char *pszStr = NULL;
|
---|
211 | RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryString(hJsonValMember, &pszStr));
|
---|
212 | RTTEST_CHECK(hTest, strcmp(pszStr, "test") == 0);
|
---|
213 | break;
|
---|
214 | }
|
---|
215 | case RTJSONVALTYPE_NUMBER:
|
---|
216 | {
|
---|
217 | RTTEST_CHECK(hTest, strcmp(pszName, "number") == 0);
|
---|
218 | int64_t i64Num = 0;
|
---|
219 | RTTEST_CHECK_RC_OK(hTest, RTJsonValueQueryInteger(hJsonValMember, &i64Num));
|
---|
220 | RTTEST_CHECK(hTest, i64Num == 100);
|
---|
221 | break;
|
---|
222 | }
|
---|
223 | case RTJSONVALTYPE_NULL:
|
---|
224 | RTTEST_CHECK(hTest, strcmp(pszName, "null") == 0);
|
---|
225 | break;
|
---|
226 | case RTJSONVALTYPE_TRUE:
|
---|
227 | RTTEST_CHECK(hTest, strcmp(pszName, "true") == 0);
|
---|
228 | break;
|
---|
229 | case RTJSONVALTYPE_FALSE:
|
---|
230 | RTTEST_CHECK(hTest, strcmp(pszName, "false") == 0);
|
---|
231 | break;
|
---|
232 | default:
|
---|
233 | RTTestFailed(hTest, "Invalid JSON value type %u returned\n", enmTypeMember);
|
---|
234 | }
|
---|
235 |
|
---|
236 | RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonValMember) == 1);
|
---|
237 | rc = RTJsonIteratorNext(hJsonIt);
|
---|
238 | RTTEST_CHECK(hTest, rc == VINF_SUCCESS || rc == VERR_JSON_ITERATOR_END);
|
---|
239 | if (RT_SUCCESS(rc))
|
---|
240 | RTTEST_CHECK_RC_OK(hTest, RTJsonIteratorQueryValue(hJsonIt, &hJsonValMember, &pszName));
|
---|
241 | }
|
---|
242 | RTJsonIteratorFree(hJsonIt);
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Test that the parser returns the correct values for a valid JSON.
|
---|
248 | */
|
---|
249 | static void tstCorrectness(RTTEST hTest)
|
---|
250 | {
|
---|
251 | RTTestSub(hTest, "Correctness");
|
---|
252 |
|
---|
253 | RTJSONVAL hJsonVal = NIL_RTJSONVAL;
|
---|
254 | RTTEST_CHECK_RC_OK_RETV(hTest, RTJsonParseFromString(&hJsonVal, g_pszJson, NULL));
|
---|
255 |
|
---|
256 | if (hJsonVal != NIL_RTJSONVAL)
|
---|
257 | {
|
---|
258 | RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonVal);
|
---|
259 | if (enmType == RTJSONVALTYPE_OBJECT)
|
---|
260 | {
|
---|
261 | /* Excercise the other non object APIs to return VERR_JSON_VALUE_INVALID_TYPE. */
|
---|
262 | tstCorrectnessRcForInvalidType(hTest, hJsonVal, enmType);
|
---|
263 | tstIterator(hTest, hJsonVal);
|
---|
264 | }
|
---|
265 | else
|
---|
266 | RTTestFailed(hTest, "RTJsonParseFromString() returned an invalid JSON value, expected OBJECT got %u\n", enmType);
|
---|
267 | RTTEST_CHECK(hTest, RTJsonValueRelease(hJsonVal) == 0);
|
---|
268 | }
|
---|
269 | else
|
---|
270 | RTTestFailed(hTest, "RTJsonParseFromString() returned success but no value\n");
|
---|
271 | }
|
---|
272 |
|
---|
273 | int main()
|
---|
274 | {
|
---|
275 | RTTEST hTest;
|
---|
276 | int rc = RTTestInitAndCreate("tstRTJson", &hTest);
|
---|
277 | if (rc)
|
---|
278 | return rc;
|
---|
279 | RTTestBanner(hTest);
|
---|
280 |
|
---|
281 | tstBasic(hTest);
|
---|
282 | tstCorrectness(hTest);
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Summary.
|
---|
286 | */
|
---|
287 | return RTTestSummaryAndDestroy(hTest);
|
---|
288 | }
|
---|
289 |
|
---|