Debian Codegraph @

src/ws/test-json.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitjson.h"
9
10 #include "testlib/cockpittest.h"
11
12 #include <math.h>
13 #include <string.h>
14
15 static const gchar *test_data =
16 "{"
17 " \"string\": \"value\","
18 " \"number\": 55.4,"
19 " \"array\": [ \"one\", \"two\", \"three\" ],"
20 " \"object\": { \"test\": \"one\" },"
21 " \"bool\": true,"
22 " \"null\": null"
23 "}";
24
25 typedef struct {
26 JsonObject *root;
27 } TestCase;
28
29 static void
30 setup (TestCase *tc,
31 gconstpointer data)
32 {
33 GError *error = NULL;
34 JsonNode *node;
35
36 node = cockpit_json_parse (test_data, -1, &error);
37 g_assert_no_error (error);
38
39 g_assert (json_node_get_node_type (node) == JSON_NODE_OBJECT);
40 tc->root = json_node_dup_object (node);
41 json_node_free (node);
42 }
43
44 static void
45 teardown (TestCase *tc,
46 gconstpointer data)
47 {
48 json_object_unref (tc->root);
49 }
50
51 static void
52 test_get_string (TestCase *tc,
53 gconstpointer data)
54 {
55 gboolean ret;
56 const gchar *value;
57
58 ret = cockpit_json_get_string (tc->root, "string", NULL, &value);
59 g_assert (ret == TRUE);
60 g_assert_cmpstr (value, ==, "value");
61
62 ret = cockpit_json_get_string (tc->root, "string", NULL, NULL);
63 g_assert (ret == TRUE);
64
65 ret = cockpit_json_get_string (tc->root, "unknown", NULL, &value);
66 g_assert (ret == TRUE);
67 g_assert_cmpstr (value, ==, NULL);
68
69 ret = cockpit_json_get_string (tc->root, "unknown", "default", &value);
70 g_assert (ret == TRUE);
71 g_assert_cmpstr (value, ==, "default");
72
73 ret = cockpit_json_get_string (tc->root, "number", NULL, &value);
74 g_assert (ret == FALSE);
75
76 ret = cockpit_json_get_string (tc->root, "number", NULL, NULL);
77 g_assert (ret == FALSE);
78 }
79
80 static void
81 test_get_int (TestCase *tc,
82 gconstpointer data)
83 {
84 gboolean ret;
85 gint64 value;
86
87 ret = cockpit_json_get_int (tc->root, "number", 0, &value);
88 g_assert (ret == TRUE);
89 g_assert_cmpint (value, ==, 55);
90
91 ret = cockpit_json_get_int (tc->root, "number", 0, NULL);
92 g_assert (ret == TRUE);
93
94 ret = cockpit_json_get_int (tc->root, "unknown", 66, &value);
95 g_assert (ret == TRUE);
96 g_assert_cmpint (value, ==, 66);
97
98 ret = cockpit_json_get_int (tc->root, "string", 66, &value);
99 g_assert (ret == FALSE);
100
101 ret = cockpit_json_get_int (tc->root, "string", 66, NULL);
102 g_assert (ret == FALSE);
103 }
104
105 static void
106 test_get_bool (TestCase *tc,
107 gconstpointer data)
108 {
109 gboolean ret;
110 gboolean value;
111
112 ret = cockpit_json_get_bool (tc->root, "bool", FALSE, &value);
113 g_assert (ret == TRUE);
114 g_assert_cmpint (value, ==, TRUE);
115
116 ret = cockpit_json_get_bool (tc->root, "bool", FALSE, NULL);
117 g_assert (ret == TRUE);
118
119 ret = cockpit_json_get_bool (tc->root, "unknown", TRUE, &value);
120 g_assert (ret == TRUE);
121 g_assert_cmpint (value, ==, TRUE);
122
123 ret = cockpit_json_get_bool (tc->root, "unknown", FALSE, &value);
124 g_assert (ret == TRUE);
125 g_assert_cmpint (value, ==, FALSE);
126
127 ret = cockpit_json_get_bool (tc->root, "string", FALSE, &value);
128 g_assert (ret == FALSE);
129
130 ret = cockpit_json_get_bool (tc->root, "string", FALSE, NULL);
131 g_assert (ret == FALSE);
132 }
133
134 static void
135 test_get_strv (TestCase *tc,
136 gconstpointer data)
137 {
138 const gchar *defawlt[] = { "1", "2", NULL };
139 gboolean ret;
140 const gchar **value;
141
142 ret = cockpit_json_get_strv (tc->root, "array", NULL, &value);
143 g_assert (ret == TRUE);
144 g_assert (value != NULL);
145 g_assert_cmpstr (value[0], ==, "one");
146 g_assert_cmpstr (value[1], ==, "two");
147 g_assert_cmpstr (value[2], ==, "three");
148 g_assert_cmpstr (value[3], ==, NULL);
149 g_free (value);
150
151 ret = cockpit_json_get_strv (tc->root, "unknown", NULL, &value);
152 g_assert (ret == TRUE);
153 g_assert (value == NULL);
154
155 ret = cockpit_json_get_strv (tc->root, "unknown", defawlt, &value);
156 g_assert (ret == TRUE);
157 g_assert (value != NULL);
158 g_assert_cmpstr (value[0], ==, "1");
159 g_assert_cmpstr (value[1], ==, "2");
160 g_assert_cmpstr (value[2], ==, NULL);
161 g_free (value);
162
163 ret = cockpit_json_get_strv (tc->root, "number", NULL, &value);
164 g_assert (ret == FALSE);
165 }
166
167 static void
168 test_get_object (TestCase *tc,
169 gconstpointer data)
170 {
171 JsonObject *defawlt = json_object_new ();
172 gboolean ret;
173 JsonObject *value;
174
175 ret = cockpit_json_get_object (tc->root, "object", NULL, &value);
176 g_assert (ret == TRUE);
177 g_assert (value != NULL);
178 g_assert_cmpstr (json_object_get_string_member (value, "test"), ==, "one");
179
180 ret = cockpit_json_get_object (tc->root, "object", NULL, NULL);
181 g_assert (ret == TRUE);
182
183 ret = cockpit_json_get_object (tc->root, "unknown", NULL, &value);
184 g_assert (ret == TRUE);
185 g_assert (value == NULL);
186
187 ret = cockpit_json_get_object (tc->root, "unknown", defawlt, &value);
188 g_assert (ret == TRUE);
189 g_assert (value == defawlt);
190
191 ret = cockpit_json_get_object (tc->root, "number", NULL, &value);
192 g_assert (ret == FALSE);
193
194 ret = cockpit_json_get_object (tc->root, "array", NULL, NULL);
195 g_assert (ret == FALSE);
196
197 json_object_unref (defawlt);
198 }
199
200 static void
201 test_parser_trims (void)
202 {
203 GError *error = NULL;
204 JsonNode *node;
205
206 /* Test that the parser trims whitespace, as long as something is present */
207
208 node = cockpit_json_parse (" 55 ", -1, &error);
209 g_assert_no_error (error);
210 g_assert (node);
211 g_assert_cmpint (json_node_get_node_type (node), ==, JSON_NODE_VALUE);
212 g_assert_cmpint (json_node_get_value_type (node), ==, G_TYPE_INT64);
213 json_node_free (node);
214
215 node = cockpit_json_parse (" \"xx\" ", -1, &error);
216 g_assert_no_error (error);
217 g_assert (node);
218 g_assert_cmpint (json_node_get_node_type (node), ==, JSON_NODE_VALUE);
219 g_assert_cmpint (json_node_get_value_type (node), ==, G_TYPE_STRING);
220 json_node_free (node);
221
222 node = cockpit_json_parse (" {\"xx\":5} ", -1, &error);
223 g_assert_no_error (error);
224 g_assert (node);
225 g_assert_cmpint (json_node_get_node_type (node), ==, JSON_NODE_OBJECT);
226 json_node_free (node);
227 }
228
229 static void
230 test_parser_empty (void)
231 {
232 GError *error = NULL;
233 JsonNode *node;
234
235 node = cockpit_json_parse ("", 0, &error);
236 g_assert_error (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_PARSE);
237 g_error_free (error);
238 g_assert (node == NULL);
239 }
240
241 static void
242 test_utf8_invalid (void)
243 {
244 const gchar *input = "\"\xff\xff\"";
245 GError *error = NULL;
246
247 if (cockpit_json_parse (input, -1, &error))
248 g_assert_not_reached ();
249
250 g_assert_error (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_INVALID_DATA);
251 g_error_free (error);
252 }
253
254 typedef struct {
255 const gchar *str;
256 const gchar *expect;
257 } FixtureString;
258
259 static const FixtureString string_fixtures[] = {
260 { "abc", "\"abc\"" },
261 { "a\x7fxc", "\"a\\u007fxc\"" },
262 { "a\033xc", "\"a\\u001bxc\"" },
263 { "a\nxc", "\"a\\nxc\"" },
264 { "a\\xc", "\"a\\\\xc\"" },
265 { "Barney B\303\244r", "\"Barney B\303\244r\"" },
266 };
267
268 static void
269 test_string_encode (gconstpointer data)
270 {
271 const FixtureString *fixture = data;
272 JsonNode *node;
273 gsize length;
274 gchar *output;
275
276 node = json_node_init_string (json_node_alloc (), fixture->str);
277 output = cockpit_json_write (node, &length);
278 g_assert_cmpstr (output, ==, fixture->expect);
279 g_assert_cmpuint (length, ==, strlen (fixture->expect));
280 g_free (output);
281 json_node_free (node);
282 }
283
284 static void
285 test_write_infinite_nan (void)
286 {
287 JsonArray *array;
288 gchar *string;
289 JsonNode *node;
290
291 array = json_array_new ();
292 json_array_add_double_element (array, 3.0); /* number */
293 json_array_add_double_element (array, 1.0/0.0); /* INFINITY */
294 json_array_add_double_element (array, NAN); /* NaN */
295
296 node = json_node_new (JSON_NODE_ARRAY);
297 json_node_take_array (node, array);
298 string = cockpit_json_write (node, NULL);
299
300 g_assert_cmpstr (string, ==, "[3,null,null]");
301
302 json_node_free (node);
303 g_free (string);
304 }
305
306 int
307 main (int argc,
308 char *argv[])
309 {
310 gchar *escaped;
311 gchar *name;
312 gint i;
313
314 cockpit_test_init (&argc, &argv);
315
316 g_test_add_func ("/json/utf8-invalid", test_utf8_invalid);
317
318 g_test_add ("/json/get-string", TestCase, NULL,
319 setup, test_get_string, teardown);
320 g_test_add ("/json/get-int", TestCase, NULL,
321 setup, test_get_int, teardown);
322 g_test_add ("/json/get-bool", TestCase, NULL,
323 setup, test_get_bool, teardown);
324 g_test_add ("/json/get-strv", TestCase, NULL,
325 setup, test_get_strv, teardown);
326 g_test_add ("/json/get-object", TestCase, NULL,
327 setup, test_get_object, teardown);
328
329 g_test_add_func ("/json/parser-trims", test_parser_trims);
330 g_test_add_func ("/json/parser-empty", test_parser_empty);
331
332 for (i = 0; i < G_N_ELEMENTS (string_fixtures); i++)
333 {
334 escaped = g_strcanon (g_strdup (string_fixtures[i].str), COCKPIT_TEST_CHARS, '_');
335 name = g_strdup_printf ("/json/string/%s%d", escaped, i);
336 g_test_add_data_func (name, string_fixtures + i, test_string_encode);
337 g_free (escaped);
338 g_free (name);
339 }
340
341 g_test_add_func ("/json/write/infinite-nan", test_write_infinite_nan);
342
343 return g_test_run ();
344 }