1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <float.h>
4 #include <math.h>
5
6 #include "alloc-util.h"
7 #include "escape.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "json-internal.h"
11 #include "json.h"
12 #include "string-util.h"
13 #include "strv.h"
14 #include "tests.h"
15 #include "util.h"
16
test_tokenizer_one(const char * data,...)17 static void test_tokenizer_one(const char *data, ...) {
18 unsigned line = 0, column = 0;
19 void *state = NULL;
20 va_list ap;
21
22 _cleanup_free_ char *cdata;
23 assert_se(cdata = cescape(data));
24 log_info("/* %s data=\"%s\" */", __func__, cdata);
25
26 va_start(ap, data);
27
28 for (;;) {
29 unsigned token_line, token_column;
30 _cleanup_free_ char *str = NULL;
31 JsonValue v = JSON_VALUE_NULL;
32 int t, tt;
33
34 t = json_tokenize(&data, &str, &v, &token_line, &token_column, &state, &line, &column);
35 tt = va_arg(ap, int);
36
37 assert_se(t == tt);
38
39 if (t == JSON_TOKEN_END || t < 0)
40 break;
41
42 else if (t == JSON_TOKEN_STRING) {
43 const char *nn;
44
45 nn = va_arg(ap, const char *);
46 assert_se(streq_ptr(nn, str));
47
48 } else if (t == JSON_TOKEN_REAL) {
49 double d;
50
51 d = va_arg(ap, double);
52
53 assert_se(fabsl(d - v.real) < 1e-10 ||
54 fabsl((d - v.real) / v.real) < 1e-10);
55
56 } else if (t == JSON_TOKEN_INTEGER) {
57 int64_t i;
58
59 i = va_arg(ap, int64_t);
60 assert_se(i == v.integer);
61
62 } else if (t == JSON_TOKEN_UNSIGNED) {
63 uint64_t u;
64
65 u = va_arg(ap, uint64_t);
66 assert_se(u == v.unsig);
67
68 } else if (t == JSON_TOKEN_BOOLEAN) {
69 bool b;
70
71 b = va_arg(ap, int);
72 assert_se(b == v.boolean);
73 }
74 }
75
76 va_end(ap);
77 }
78
79 typedef void (*Test)(JsonVariant *);
80
test_variant_one(const char * data,Test test)81 static void test_variant_one(const char *data, Test test) {
82 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
83 _cleanup_free_ char *s = NULL;
84 int r;
85
86 _cleanup_free_ char *cdata;
87 assert_se(cdata = cescape(data));
88 log_info("/* %s data=\"%s\" */", __func__, cdata);
89
90 r = json_parse(data, 0, &v, NULL, NULL);
91 assert_se(r == 0);
92 assert_se(v);
93
94 r = json_variant_format(v, 0, &s);
95 assert_se(r >= 0);
96 assert_se(s);
97 assert_se((size_t) r == strlen(s));
98
99 log_info("formatted normally: %s\n", s);
100
101 r = json_parse(data, JSON_PARSE_SENSITIVE, &w, NULL, NULL);
102 assert_se(r == 0);
103 assert_se(w);
104 assert_se(json_variant_has_type(v, json_variant_type(w)));
105 assert_se(json_variant_has_type(w, json_variant_type(v)));
106 assert_se(json_variant_equal(v, w));
107
108 s = mfree(s);
109 w = json_variant_unref(w);
110
111 r = json_variant_format(v, JSON_FORMAT_PRETTY, &s);
112 assert_se(r >= 0);
113 assert_se(s);
114 assert_se((size_t) r == strlen(s));
115
116 log_info("formatted prettily:\n%s", s);
117
118 r = json_parse(data, 0, &w, NULL, NULL);
119 assert_se(r == 0);
120 assert_se(w);
121
122 assert_se(json_variant_has_type(v, json_variant_type(w)));
123 assert_se(json_variant_has_type(w, json_variant_type(v)));
124 assert_se(json_variant_equal(v, w));
125
126 s = mfree(s);
127 r = json_variant_format(v, JSON_FORMAT_COLOR, &s);
128 assert_se(r >= 0);
129 assert_se(s);
130 assert_se((size_t) r == strlen(s));
131 printf("Normal with color: %s\n", s);
132
133 s = mfree(s);
134 r = json_variant_format(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, &s);
135 assert_se(r >= 0);
136 assert_se(s);
137 assert_se((size_t) r == strlen(s));
138 printf("Pretty with color:\n%s\n", s);
139
140 if (test)
141 test(v);
142 }
143
test_1(JsonVariant * v)144 static void test_1(JsonVariant *v) {
145 JsonVariant *p, *q;
146 unsigned i;
147
148 log_info("/* %s */", __func__);
149
150 /* 3 keys + 3 values */
151 assert_se(json_variant_elements(v) == 6);
152
153 /* has k */
154 p = json_variant_by_key(v, "k");
155 assert_se(p && json_variant_type(p) == JSON_VARIANT_STRING);
156
157 /* k equals v */
158 assert_se(streq(json_variant_string(p), "v"));
159
160 /* has foo */
161 p = json_variant_by_key(v, "foo");
162 assert_se(p && json_variant_type(p) == JSON_VARIANT_ARRAY && json_variant_elements(p) == 3);
163
164 /* check foo[0] = 1, foo[1] = 2, foo[2] = 3 */
165 for (i = 0; i < 3; ++i) {
166 q = json_variant_by_index(p, i);
167 assert_se(q && json_variant_type(q) == JSON_VARIANT_UNSIGNED && json_variant_unsigned(q) == (i+1));
168 assert_se(q && json_variant_has_type(q, JSON_VARIANT_INTEGER) && json_variant_integer(q) == (i+1));
169 }
170
171 /* has bar */
172 p = json_variant_by_key(v, "bar");
173 assert_se(p && json_variant_type(p) == JSON_VARIANT_OBJECT && json_variant_elements(p) == 2);
174
175 /* zap is null */
176 q = json_variant_by_key(p, "zap");
177 assert_se(q && json_variant_type(q) == JSON_VARIANT_NULL);
178 }
179
test_2(JsonVariant * v)180 static void test_2(JsonVariant *v) {
181 JsonVariant *p, *q;
182
183 log_info("/* %s */", __func__);
184
185 /* 2 keys + 2 values */
186 assert_se(json_variant_elements(v) == 4);
187
188 /* has mutant */
189 p = json_variant_by_key(v, "mutant");
190 assert_se(p && json_variant_type(p) == JSON_VARIANT_ARRAY && json_variant_elements(p) == 4);
191
192 /* mutant[0] == 1 */
193 q = json_variant_by_index(p, 0);
194 assert_se(q && json_variant_type(q) == JSON_VARIANT_UNSIGNED && json_variant_unsigned(q) == 1);
195 assert_se(q && json_variant_has_type(q, JSON_VARIANT_INTEGER) && json_variant_integer(q) == 1);
196
197 /* mutant[1] == null */
198 q = json_variant_by_index(p, 1);
199 assert_se(q && json_variant_type(q) == JSON_VARIANT_NULL);
200
201 /* mutant[2] == "1" */
202 q = json_variant_by_index(p, 2);
203 assert_se(q && json_variant_type(q) == JSON_VARIANT_STRING && streq(json_variant_string(q), "1"));
204
205 /* mutant[3] == JSON_VARIANT_OBJECT */
206 q = json_variant_by_index(p, 3);
207 assert_se(q && json_variant_type(q) == JSON_VARIANT_OBJECT && json_variant_elements(q) == 2);
208
209 /* has 1 */
210 p = json_variant_by_key(q, "1");
211 assert_se(p && json_variant_type(p) == JSON_VARIANT_ARRAY && json_variant_elements(p) == 2);
212
213 /* "1"[0] == 1 */
214 q = json_variant_by_index(p, 0);
215 assert_se(q && json_variant_type(q) == JSON_VARIANT_UNSIGNED && json_variant_unsigned(q) == 1);
216 assert_se(q && json_variant_has_type(q, JSON_VARIANT_INTEGER) && json_variant_integer(q) == 1);
217
218 /* "1"[1] == "1" */
219 q = json_variant_by_index(p, 1);
220 assert_se(q && json_variant_type(q) == JSON_VARIANT_STRING && streq(json_variant_string(q), "1"));
221
222 /* has thisisaverylongproperty */
223 p = json_variant_by_key(v, "thisisaverylongproperty");
224 assert_se(p && json_variant_type(p) == JSON_VARIANT_REAL && fabsl(json_variant_real(p) - 1.27) < 0.001);
225 }
226
test_zeroes(JsonVariant * v)227 static void test_zeroes(JsonVariant *v) {
228 /* Make sure zero is how we expect it. */
229 log_info("/* %s */", __func__);
230
231 assert_se(json_variant_elements(v) == 13);
232
233 for (size_t i = 0; i < json_variant_elements(v); i++) {
234 JsonVariant *w;
235 size_t j;
236
237 assert_se(w = json_variant_by_index(v, i));
238
239 assert_se(json_variant_integer(w) == 0);
240 assert_se(json_variant_unsigned(w) == 0U);
241
242 DISABLE_WARNING_FLOAT_EQUAL;
243 assert_se(json_variant_real(w) == 0.0L);
244 REENABLE_WARNING;
245
246 assert_se(json_variant_is_integer(w));
247 assert_se(json_variant_is_unsigned(w));
248 assert_se(json_variant_is_real(w));
249 assert_se(json_variant_is_number(w));
250
251 assert_se(!json_variant_is_negative(w));
252
253 assert_se(IN_SET(json_variant_type(w), JSON_VARIANT_INTEGER, JSON_VARIANT_UNSIGNED, JSON_VARIANT_REAL));
254
255 for (j = 0; j < json_variant_elements(v); j++) {
256 JsonVariant *q;
257
258 assert_se(q = json_variant_by_index(v, j));
259
260 assert_se(json_variant_equal(w, q));
261 }
262 }
263 }
264
TEST(build)265 TEST(build) {
266 _cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL;
267 _cleanup_free_ char *s = NULL, *t = NULL;
268
269 assert_se(json_build(&a, JSON_BUILD_STRING("hallo")) >= 0);
270 assert_se(json_build(&b, JSON_BUILD_LITERAL(" \"hallo\" ")) >= 0);
271 assert_se(json_variant_equal(a, b));
272
273 b = json_variant_unref(b);
274
275 assert_se(json_build(&b, JSON_BUILD_VARIANT(a)) >= 0);
276 assert_se(json_variant_equal(a, b));
277
278 b = json_variant_unref(b);
279 assert_se(json_build(&b, JSON_BUILD_STRING("pief")) >= 0);
280 assert_se(!json_variant_equal(a, b));
281
282 a = json_variant_unref(a);
283 b = json_variant_unref(b);
284
285 assert_se(json_build(&a, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("one", JSON_BUILD_INTEGER(7)),
286 JSON_BUILD_PAIR("two", JSON_BUILD_REAL(2.0)),
287 JSON_BUILD_PAIR("three", JSON_BUILD_INTEGER(0)))) >= 0);
288
289 assert_se(json_build(&b, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("two", JSON_BUILD_INTEGER(2)),
290 JSON_BUILD_PAIR("three", JSON_BUILD_REAL(0)),
291 JSON_BUILD_PAIR("one", JSON_BUILD_REAL(7)))) >= 0);
292
293 assert_se(json_variant_equal(a, b));
294
295 a = json_variant_unref(a);
296 b = json_variant_unref(b);
297
298 const char* arr_1234[] = {"one", "two", "three", "four", NULL};
299 assert_se(json_build(&a, JSON_BUILD_ARRAY(JSON_BUILD_OBJECT(JSON_BUILD_PAIR("x", JSON_BUILD_BOOLEAN(true)),
300 JSON_BUILD_PAIR("y", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("this", JSON_BUILD_NULL)))),
301 JSON_BUILD_VARIANT(NULL),
302 JSON_BUILD_LITERAL(NULL),
303 JSON_BUILD_STRING(NULL),
304 JSON_BUILD_NULL,
305 JSON_BUILD_INTEGER(77),
306 JSON_BUILD_ARRAY(JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("foobar")),
307 JSON_BUILD_VARIANT(JSON_VARIANT_STRING_CONST("zzz"))),
308 JSON_BUILD_STRV((char**) arr_1234))) >= 0);
309
310 assert_se(json_variant_format(a, 0, &s) >= 0);
311 log_info("GOT: %s\n", s);
312 assert_se(json_parse(s, 0, &b, NULL, NULL) >= 0);
313 assert_se(json_variant_equal(a, b));
314
315 a = json_variant_unref(a);
316 b = json_variant_unref(b);
317
318 assert_se(json_build(&a, JSON_BUILD_REAL(M_PI)) >= 0);
319
320 s = mfree(s);
321 assert_se(json_variant_format(a, 0, &s) >= 0);
322 log_info("GOT: %s\n", s);
323 assert_se(json_parse(s, 0, &b, NULL, NULL) >= 0);
324 assert_se(json_variant_format(b, 0, &t) >= 0);
325 log_info("GOT: %s\n", t);
326
327 assert_se(streq(s, t));
328
329 a = json_variant_unref(a);
330 b = json_variant_unref(b);
331
332 assert_se(json_build(&a, JSON_BUILD_OBJECT(
333 JSON_BUILD_PAIR("x", JSON_BUILD_STRING("y")),
334 JSON_BUILD_PAIR("z", JSON_BUILD_CONST_STRING("a")),
335 JSON_BUILD_PAIR("b", JSON_BUILD_CONST_STRING("c"))
336 )) >= 0);
337
338 assert_se(json_build(&b, JSON_BUILD_OBJECT(
339 JSON_BUILD_PAIR("x", JSON_BUILD_STRING("y")),
340 JSON_BUILD_PAIR_CONDITION(false, "p", JSON_BUILD_STRING("q")),
341 JSON_BUILD_PAIR_CONDITION(true, "z", JSON_BUILD_CONST_STRING("a")),
342 JSON_BUILD_PAIR_CONDITION(false, "j", JSON_BUILD_ARRAY(JSON_BUILD_STRING("k"), JSON_BUILD_CONST_STRING("u"), JSON_BUILD_CONST_STRING("i"))),
343 JSON_BUILD_PAIR("b", JSON_BUILD_CONST_STRING("c"))
344 )) >= 0);
345
346 assert_se(json_variant_equal(a, b));
347 }
348
TEST(source)349 TEST(source) {
350 static const char data[] =
351 "\n"
352 "\n"
353 "{\n"
354 "\"foo\" : \"bar\", \n"
355 "\"qüüx\" : [ 1, 2, 3,\n"
356 "4,\n"
357 "5 ],\n"
358 "\"miep\" : { \"hallo\" : 1 },\n"
359 "\n"
360 "\"zzzzzz\" \n"
361 ":\n"
362 "[ true, \n"
363 "false, 7.5, {} ]\n"
364 "}\n";
365
366 _cleanup_fclose_ FILE *f = NULL;
367 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
368
369 printf("--- original begin ---\n"
370 "%s"
371 "--- original end ---\n", data);
372
373 assert_se(f = fmemopen_unlocked((void*) data, strlen(data), "r"));
374
375 assert_se(json_parse_file(f, "waldo", 0, &v, NULL, NULL) >= 0);
376
377 printf("--- non-pretty begin ---\n");
378 json_variant_dump(v, 0, stdout, NULL);
379 printf("\n--- non-pretty end ---\n");
380
381 printf("--- pretty begin ---\n");
382 json_variant_dump(v, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR|JSON_FORMAT_SOURCE, stdout, NULL);
383 printf("--- pretty end ---\n");
384 }
385
TEST(depth)386 TEST(depth) {
387 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
388 int r;
389
390 v = JSON_VARIANT_STRING_CONST("start");
391
392 /* Let's verify that the maximum depth checks work */
393
394 for (unsigned i = 0;; i++) {
395 _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
396
397 assert_se(i <= UINT16_MAX);
398 if (i & 1)
399 r = json_variant_new_array(&w, &v, 1);
400 else
401 r = json_variant_new_object(&w, (JsonVariant*[]) { JSON_VARIANT_STRING_CONST("key"), v }, 2);
402 if (r == -ELNRNG) {
403 log_info("max depth at %u", i);
404 break;
405 }
406 #if HAS_FEATURE_MEMORY_SANITIZER
407 /* msan doesn't like the stack nesting to be too deep. Let's quit early. */
408 if (i >= 128) {
409 log_info("quitting early at depth %u", i);
410 break;
411 }
412 #endif
413
414 assert_se(r >= 0);
415
416 json_variant_unref(v);
417 v = TAKE_PTR(w);
418 }
419
420 json_variant_dump(v, 0, stdout, NULL);
421 fputs("\n", stdout);
422 }
423
TEST(normalize)424 TEST(normalize) {
425 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
426 _cleanup_free_ char *t = NULL;
427
428 assert_se(json_build(&v, JSON_BUILD_OBJECT(
429 JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")),
430 JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")),
431 JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0);
432
433 assert_se(!json_variant_is_sorted(v));
434 assert_se(!json_variant_is_normalized(v));
435
436 assert_se(json_variant_format(v, 0, &t) >= 0);
437 assert_se(streq(t, "{\"b\":\"x\",\"c\":\"y\",\"a\":\"z\"}"));
438 t = mfree(t);
439
440 assert_se(json_build(&w, JSON_BUILD_OBJECT(
441 JSON_BUILD_PAIR("bar", JSON_BUILD_STRING("zzz")),
442 JSON_BUILD_PAIR("foo", JSON_BUILD_VARIANT(v)))) >= 0);
443
444 assert_se(json_variant_is_sorted(w));
445 assert_se(!json_variant_is_normalized(w));
446
447 assert_se(json_variant_format(w, 0, &t) >= 0);
448 assert_se(streq(t, "{\"bar\":\"zzz\",\"foo\":{\"b\":\"x\",\"c\":\"y\",\"a\":\"z\"}}"));
449 t = mfree(t);
450
451 assert_se(json_variant_sort(&v) >= 0);
452 assert_se(json_variant_is_sorted(v));
453 assert_se(json_variant_is_normalized(v));
454
455 assert_se(json_variant_format(v, 0, &t) >= 0);
456 assert_se(streq(t, "{\"a\":\"z\",\"b\":\"x\",\"c\":\"y\"}"));
457 t = mfree(t);
458
459 assert_se(json_variant_normalize(&w) >= 0);
460 assert_se(json_variant_is_sorted(w));
461 assert_se(json_variant_is_normalized(w));
462
463 assert_se(json_variant_format(w, 0, &t) >= 0);
464 assert_se(streq(t, "{\"bar\":\"zzz\",\"foo\":{\"a\":\"z\",\"b\":\"x\",\"c\":\"y\"}}"));
465 t = mfree(t);
466 }
467
TEST(bisect)468 TEST(bisect) {
469 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
470
471 /* Tests the bisection logic in json_variant_by_key() */
472
473 for (char c = 'z'; c >= 'a'; c--) {
474
475 if ((c % 3) == 0)
476 continue;
477
478 _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
479 assert_se(json_variant_new_stringn(&w, (char[4]) { '<', c, c, '>' }, 4) >= 0);
480 assert_se(json_variant_set_field(&v, (char[2]) { c, 0 }, w) >= 0);
481 }
482
483 json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL);
484
485 assert_se(!json_variant_is_sorted(v));
486 assert_se(!json_variant_is_normalized(v));
487 assert_se(json_variant_normalize(&v) >= 0);
488 assert_se(json_variant_is_sorted(v));
489 assert_se(json_variant_is_normalized(v));
490
491 json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL);
492
493 for (char c = 'a'; c <= 'z'; c++) {
494 JsonVariant *k;
495 const char *z;
496
497 k = json_variant_by_key(v, (char[2]) { c, 0 });
498 assert_se(!k == ((c % 3) == 0));
499
500 if (!k)
501 continue;
502
503 assert_se(json_variant_is_string(k));
504
505 z = (char[5]){ '<', c, c, '>', 0};
506 assert_se(streq(json_variant_string(k), z));
507 }
508 }
509
test_float_match(JsonVariant * v)510 static void test_float_match(JsonVariant *v) {
511 const double delta = 0.0001;
512
513 assert_se(json_variant_is_array(v));
514 assert_se(json_variant_elements(v) == 9);
515 assert_se(fabsl((double) 1.0 - ((double) DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
516 assert_se(fabsl((double) 1.0 - ((double) DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
517 assert_se(json_variant_is_null(json_variant_by_index(v, 2))); /* nan is not supported by json → null */
518 assert_se(json_variant_is_null(json_variant_by_index(v, 3))); /* +inf is not supported by json → null */
519 assert_se(json_variant_is_null(json_variant_by_index(v, 4))); /* -inf is not supported by json → null */
520 assert_se(json_variant_is_null(json_variant_by_index(v, 5)) ||
521 fabsl((double) 1.0 - ((double) HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
522 assert_se(json_variant_is_real(json_variant_by_index(v, 6)) &&
523 json_variant_is_integer(json_variant_by_index(v, 6)) &&
524 json_variant_integer(json_variant_by_index(v, 6)) == 0);
525 assert_se(json_variant_is_real(json_variant_by_index(v, 7)) &&
526 json_variant_is_integer(json_variant_by_index(v, 7)) &&
527 json_variant_integer(json_variant_by_index(v, 7)) == 10);
528 assert_se(json_variant_is_real(json_variant_by_index(v, 8)) &&
529 json_variant_is_integer(json_variant_by_index(v, 8)) &&
530 json_variant_integer(json_variant_by_index(v, 8)) == -10);
531 }
532
TEST(float)533 TEST(float) {
534 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
535 _cleanup_free_ char *text = NULL;
536
537 assert_se(json_build(&v, JSON_BUILD_ARRAY(
538 JSON_BUILD_REAL(DBL_MIN),
539 JSON_BUILD_REAL(DBL_MAX),
540 JSON_BUILD_REAL(NAN),
541 JSON_BUILD_REAL(INFINITY),
542 JSON_BUILD_REAL(-INFINITY),
543 JSON_BUILD_REAL(HUGE_VAL),
544 JSON_BUILD_REAL(0),
545 JSON_BUILD_REAL(10),
546 JSON_BUILD_REAL(-10))) >= 0);
547
548 json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL);
549
550 test_float_match(v);
551
552 assert_se(json_variant_format(v, 0, &text) >= 0);
553 assert_se(json_parse(text, 0, &w, NULL, NULL) >= 0);
554
555 json_variant_dump(w, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL);
556
557 test_float_match(w);
558 }
559
test_equal_text(JsonVariant * v,const char * text)560 static void test_equal_text(JsonVariant *v, const char *text) {
561 _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
562
563 assert_se(json_parse(text, 0, &w, NULL, NULL) >= 0);
564 assert_se(json_variant_equal(v, w) || (!v && json_variant_is_null(w)));
565 }
566
TEST(set_field)567 TEST(set_field) {
568 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
569
570 test_equal_text(v, "null");
571 assert_se(json_variant_set_field(&v, "foo", NULL) >= 0);
572 test_equal_text(v, "{\"foo\" : null}");
573 assert_se(json_variant_set_field(&v, "bar", JSON_VARIANT_STRING_CONST("quux")) >= 0);
574 test_equal_text(v, "{\"foo\" : null, \"bar\" : \"quux\"}");
575 assert_se(json_variant_set_field(&v, "foo", JSON_VARIANT_STRING_CONST("quux2")) >= 0);
576 test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : \"quux\"}");
577 assert_se(json_variant_set_field(&v, "bar", NULL) >= 0);
578 test_equal_text(v, "{\"foo\" : \"quux2\", \"bar\" : null}");
579 }
580
TEST(tokenizer)581 TEST(tokenizer) {
582 test_tokenizer_one("x", -EINVAL);
583 test_tokenizer_one("", JSON_TOKEN_END);
584 test_tokenizer_one(" ", JSON_TOKEN_END);
585 test_tokenizer_one("0", JSON_TOKEN_UNSIGNED, (uint64_t) 0, JSON_TOKEN_END);
586 test_tokenizer_one("-0", JSON_TOKEN_INTEGER, (int64_t) 0, JSON_TOKEN_END);
587 test_tokenizer_one("1234", JSON_TOKEN_UNSIGNED, (uint64_t) 1234, JSON_TOKEN_END);
588 test_tokenizer_one("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END);
589 test_tokenizer_one("18446744073709551615", JSON_TOKEN_UNSIGNED, (uint64_t) UINT64_MAX, JSON_TOKEN_END);
590 test_tokenizer_one("-9223372036854775808", JSON_TOKEN_INTEGER, (int64_t) INT64_MIN, JSON_TOKEN_END);
591 test_tokenizer_one("18446744073709551616", JSON_TOKEN_REAL, (double) 18446744073709551616.0L, JSON_TOKEN_END);
592 test_tokenizer_one("-9223372036854775809", JSON_TOKEN_REAL, (double) -9223372036854775809.0L, JSON_TOKEN_END);
593 test_tokenizer_one("-1234", JSON_TOKEN_INTEGER, (int64_t) -1234, JSON_TOKEN_END);
594 test_tokenizer_one("3.141", JSON_TOKEN_REAL, (double) 3.141, JSON_TOKEN_END);
595 test_tokenizer_one("0.0", JSON_TOKEN_REAL, (double) 0.0, JSON_TOKEN_END);
596 test_tokenizer_one("7e3", JSON_TOKEN_REAL, (double) 7e3, JSON_TOKEN_END);
597 test_tokenizer_one("-7e-3", JSON_TOKEN_REAL, (double) -7e-3, JSON_TOKEN_END);
598 test_tokenizer_one("true", JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_END);
599 test_tokenizer_one("false", JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_END);
600 test_tokenizer_one("null", JSON_TOKEN_NULL, JSON_TOKEN_END);
601 test_tokenizer_one("{}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END);
602 test_tokenizer_one("\t {\n} \n", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END);
603 test_tokenizer_one("[]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
604 test_tokenizer_one("\t [] \n\n", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
605 test_tokenizer_one("\"\"", JSON_TOKEN_STRING, "", JSON_TOKEN_END);
606 test_tokenizer_one("\"foo\"", JSON_TOKEN_STRING, "foo", JSON_TOKEN_END);
607 test_tokenizer_one("\"foo\\nfoo\"", JSON_TOKEN_STRING, "foo\nfoo", JSON_TOKEN_END);
608 test_tokenizer_one("{\"foo\" : \"bar\"}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_STRING, "foo", JSON_TOKEN_COLON, JSON_TOKEN_STRING, "bar", JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END);
609 test_tokenizer_one("{\"foo\" : [true, false]}", JSON_TOKEN_OBJECT_OPEN, JSON_TOKEN_STRING, "foo", JSON_TOKEN_COLON, JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_BOOLEAN, true, JSON_TOKEN_COMMA, JSON_TOKEN_BOOLEAN, false, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_OBJECT_CLOSE, JSON_TOKEN_END);
610 test_tokenizer_one("\"\xef\xbf\xbd\"", JSON_TOKEN_STRING, "\xef\xbf\xbd", JSON_TOKEN_END);
611 test_tokenizer_one("\"\\ufffd\"", JSON_TOKEN_STRING, "\xef\xbf\xbd", JSON_TOKEN_END);
612 test_tokenizer_one("\"\\uf\"", -EINVAL);
613 test_tokenizer_one("\"\\ud800a\"", -EINVAL);
614 test_tokenizer_one("\"\\udc00\\udc00\"", -EINVAL);
615 test_tokenizer_one("\"\\ud801\\udc37\"", JSON_TOKEN_STRING, "\xf0\x90\x90\xb7", JSON_TOKEN_END);
616
617 test_tokenizer_one("[1, 2, -3]", JSON_TOKEN_ARRAY_OPEN, JSON_TOKEN_UNSIGNED, (uint64_t) 1, JSON_TOKEN_COMMA, JSON_TOKEN_UNSIGNED, (uint64_t) 2, JSON_TOKEN_COMMA, JSON_TOKEN_INTEGER, (int64_t) -3, JSON_TOKEN_ARRAY_CLOSE, JSON_TOKEN_END);
618 }
619
TEST(variant)620 TEST(variant) {
621 test_variant_one("{\"k\": \"v\", \"foo\": [1, 2, 3], \"bar\": {\"zap\": null}}", test_1);
622 test_variant_one("{\"mutant\": [1, null, \"1\", {\"1\": [1, \"1\"]}], \"thisisaverylongproperty\": 1.27}", test_2);
623 test_variant_one("{\"foo\" : \"\\u0935\\u093f\\u0935\\u0947\\u0915\\u0916\\u094d\\u092f\\u093e\\u0924\\u093f\\u0930\\u0935\\u093f\\u092a\\u094d\\u0932\\u0935\\u093e\\u0020\\u0939\\u093e\\u0928\\u094b\\u092a\\u093e\\u092f\\u0903\\u0964\"}", NULL);
624
625 test_variant_one("[ 0, -0, 0.0, -0.0, 0.000, -0.000, 0e0, -0e0, 0e+0, -0e-0, 0e-0, -0e000, 0e+000 ]", test_zeroes);
626 }
627
628 DEFINE_TEST_MAIN(LOG_DEBUG);
629