blob: cf47400c046ac81fe77c57d0631b27d9fe37ad8b [file] [log] [blame]
Radek Krejci50f0c6b2020-06-18 16:31:48 +02001/*
2 * @file json.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for a generic JSON parser
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci50f0c6b2020-06-18 16:31:48 +020016
Radek Krejci50f0c6b2020-06-18 16:31:48 +020017#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "in_internal.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010019#include "json.h"
20#include "utests.h"
Radek Krejci50f0c6b2020-06-18 16:31:48 +020021
Radek Krejci50f0c6b2020-06-18 16:31:48 +020022static void
23test_general(void **state)
24{
25 struct lyjson_ctx *jsonctx;
26 struct ly_in *in;
27 const char *str;
28
Radek Krejci50f0c6b2020-06-18 16:31:48 +020029 /* empty */
30 str = "";
31 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020032 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020033 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
34 lyjson_ctx_free(jsonctx);
35
36 str = " \n\t \n";
37 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +020038 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020039 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
40 lyjson_ctx_free(jsonctx);
41
42 /* constant values */
43 str = "true";
44 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +020045 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020046 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
47 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
48 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
49 lyjson_ctx_free(jsonctx);
50
51 str = "false";
52 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +020053 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020054 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
55 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
56 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
57 lyjson_ctx_free(jsonctx);
58
59 str = "null";
60 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +020061 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020062 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
63 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
64 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
65 lyjson_ctx_free(jsonctx);
66
67 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020068}
69
70static void
71test_number(void **state)
72{
73 struct lyjson_ctx *jsonctx;
74 struct ly_in *in;
75 const char *str;
76
Radek Krejci50f0c6b2020-06-18 16:31:48 +020077 /* simple value */
78 str = "11";
79 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +020080 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020081 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
82 assert_string_equal("11", jsonctx->value);
83 assert_int_equal(2, jsonctx->value_len);
84 assert_int_equal(0, jsonctx->dynamic);
85 lyjson_ctx_free(jsonctx);
86
87 /* fraction number */
88 str = "37.7668";
89 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +020090 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020091 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
92 assert_string_equal("37.7668", jsonctx->value);
93 assert_int_equal(7, jsonctx->value_len);
94 assert_int_equal(0, jsonctx->dynamic);
95 lyjson_ctx_free(jsonctx);
96
97 /* negative number */
98 str = "-122.3959";
99 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200100 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200101 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
102 assert_string_equal("-122.3959", jsonctx->value);
103 assert_int_equal(9, jsonctx->value_len);
104 assert_int_equal(0, jsonctx->dynamic);
105 lyjson_ctx_free(jsonctx);
106
107 /* exp number */
108 str = "1E10";
109 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200111 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
112 assert_string_equal("10000000000", jsonctx->value);
113 assert_int_equal(11, jsonctx->value_len);
114 assert_int_equal(1, jsonctx->dynamic);
115 lyjson_ctx_free(jsonctx);
116
117 str = "15E-1";
118 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200119 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200120 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
121 assert_string_equal("1.5", jsonctx->value);
122 assert_int_equal(3, jsonctx->value_len);
123 assert_int_equal(1, jsonctx->dynamic);
124 lyjson_ctx_free(jsonctx);
125
126 str = "15E-3";
127 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200128 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200129 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
130 assert_string_equal("0.015", jsonctx->value);
131 assert_int_equal(5, jsonctx->value_len);
132 assert_int_equal(1, jsonctx->dynamic);
133 lyjson_ctx_free(jsonctx);
134
135 /* exp fraction number */
136 str = "1.1e3";
137 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200139 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
140 assert_string_equal("1100", jsonctx->value);
141 assert_int_equal(4, jsonctx->value_len);
142 assert_int_equal(1, jsonctx->dynamic);
143 lyjson_ctx_free(jsonctx);
144
145 /* negative exp fraction number */
146 str = "1.1e-3";
147 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200148 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200149 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
150 assert_string_equal("0.0011", jsonctx->value);
151 assert_int_equal(6, jsonctx->value_len);
152 assert_int_equal(1, jsonctx->dynamic);
153 lyjson_ctx_free(jsonctx);
154
155 /* exp negative fraction number */
156 str = "-0.11e3";
157 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200158 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200159 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
160 assert_string_equal("-110", jsonctx->value);
161 assert_int_equal(4, jsonctx->value_len);
162 assert_int_equal(1, jsonctx->dynamic);
163 lyjson_ctx_free(jsonctx);
164
165 /* negative exp negative fraction number */
166 str = "-3.14e-3";
167 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200168 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200169 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
170 assert_string_equal("-0.00314", jsonctx->value);
171 assert_int_equal(8, jsonctx->value_len);
172 assert_int_equal(1, jsonctx->dynamic);
173 lyjson_ctx_free(jsonctx);
174
175 /* various invalid inputs */
176 str = "-x";
177 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
179 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200180
181 str = " -";
182 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
184 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200185
186 str = "--1";
187 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
189 CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200190
191 str = "+1";
192 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
194 CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200195
196 str = " 1.x ";
197 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200198 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
199 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200200
201 str = "1.";
202 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200203 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
204 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200205
206 str = " 1eo ";
207 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200208 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
209 CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200210
211 str = "1e";
212 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200213 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
214 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200215
216 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200217}
218
219static void
220test_string(void **state)
221{
222 struct lyjson_ctx *jsonctx;
223 struct ly_in *in;
224 const char *str;
225
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200226 /* simple string */
227 str = "\"hello\"";
228 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200229 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200230 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
231 assert_ptr_equal(&str[1], jsonctx->value);
232 assert_int_equal(5, jsonctx->value_len);
233 assert_int_equal(0, jsonctx->dynamic);
234 lyjson_ctx_free(jsonctx);
235
236 /* 4-byte utf8 character */
237 str = "\"\\t𠜎\"";
238 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200240 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
241 assert_string_equal("\t𠜎", jsonctx->value);
242 assert_int_equal(5, jsonctx->value_len);
243 assert_int_equal(1, jsonctx->dynamic);
244 lyjson_ctx_free(jsonctx);
245
246 /* valid escape sequences - note that here it mixes valid JSON string characters (RFC 7159, sec. 7) and
247 * valid characters in YANG string type (RFC 7950, sec. 9.4). Since the latter is a subset of JSON string,
248 * the YANG string type's restrictions apply to the JSON escape sequences */
249 str = "\"\\\" \\\\ \\r \\/ \\n \\t \\u20ac\"";
250 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200251 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200252 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
253 assert_string_equal("\" \\ \r / \n \t €", jsonctx->value);
254 assert_int_equal(15, jsonctx->value_len);
255 assert_int_equal(1, jsonctx->dynamic);
256 lyjson_ctx_free(jsonctx);
257
258 /* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */
259 str = "\"\\b\"";
260 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200261 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
262 CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200263
264 str = "\"\\f\"";
265 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200266 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
267 CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200268
269 /* unterminated string */
270 str = "\"unterminated string";
271 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
273 CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200274
275 /* invalid escape sequence */
276 str = "\"char \\x \"";
277 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200278 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
279 CHECK_LOG_CTX("Invalid character escape sequence \\x.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200280
281 /* new line is allowed only as escaped character in JSON */
282 str = "\"\n\"";
283 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200284 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
285 CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200286
287 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200288}
289
290static void
291test_object(void **state)
292{
293 struct lyjson_ctx *jsonctx;
294 struct ly_in *in;
295 const char *str;
296
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200297 /* empty */
298 str = " { } ";
299 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200300 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200301 assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0));
302 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
303 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
304 lyjson_ctx_free(jsonctx);
305
306 /* simple value */
307 str = "{\"name\" : \"Radek\"}";
308 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200309 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200310 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
311 assert_ptr_equal(&str[2], jsonctx->value);
312 assert_int_equal(4, jsonctx->value_len);
313 assert_int_equal(0, jsonctx->dynamic);
314 assert_string_equal("\"Radek\"}", jsonctx->in->current);
315 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
316 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
317 assert_string_equal("Radek\"}", jsonctx->value);
318 assert_int_equal(5, jsonctx->value_len);
319 assert_int_equal(0, jsonctx->dynamic);
320 assert_string_equal("}", jsonctx->in->current);
321 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
322 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
323 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
324 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
325 lyjson_ctx_free(jsonctx);
326
327 /* two values */
328 str = "{\"smart\" : true,\"handsom\":false}";
329 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200331 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
332 assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value);
333 assert_int_equal(5, jsonctx->value_len);
334 assert_int_equal(0, jsonctx->dynamic);
335 assert_string_equal("true,\"handsom\":false}", jsonctx->in->current);
336 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
337 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
338 assert_string_equal(",\"handsom\":false}", jsonctx->in->current);
339 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
340 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
341 assert_string_equal("handsom\":false}", jsonctx->value);
342 assert_int_equal(7, jsonctx->value_len);
343 assert_int_equal(0, jsonctx->dynamic);
344 assert_string_equal("false}", jsonctx->in->current);
345 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
346 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
347 assert_string_equal("}", jsonctx->in->current);
348 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
349 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
350 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
351 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
352 lyjson_ctx_free(jsonctx);
353
354 /* inherited objects */
355 str = "{\"person\" : {\"name\":\"Radek\"}}";
356 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200357 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200358 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
359 assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value);
360 assert_int_equal(6, jsonctx->value_len);
361 assert_int_equal(0, jsonctx->dynamic);
362 assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current);
363 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
364 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
365 assert_string_equal("name\":\"Radek\"}}", jsonctx->value);
366 assert_int_equal(4, jsonctx->value_len);
367 assert_int_equal(0, jsonctx->dynamic);
368 assert_string_equal("\"Radek\"}}", jsonctx->in->current);
369 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
370 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
371 assert_string_equal("Radek\"}}", jsonctx->value);
372 assert_int_equal(5, jsonctx->value_len);
373 assert_int_equal(0, jsonctx->dynamic);
374 assert_string_equal("}}", jsonctx->in->current);
375 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
376 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
377 assert_string_equal("}", jsonctx->in->current);
378 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
379 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
380 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
381 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
382 lyjson_ctx_free(jsonctx);
383
384 /* new line is allowed only as escaped character in JSON */
385 str = "{ unquoted : \"data\"}";
386 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200387 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
388 CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200389
390 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200391}
392
393static void
394test_array(void **state)
395{
396 struct lyjson_ctx *jsonctx;
397 struct ly_in *in;
398 const char *str;
399
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200400 /* empty */
401 str = " [ ] ";
402 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200403 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200404 assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0));
405 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
406 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
407 lyjson_ctx_free(jsonctx);
408
409 /* simple value */
410 str = "[ null]";
411 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200412 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200413 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
414 assert_null(jsonctx->value);
415 assert_int_equal(0, jsonctx->value_len);
416 assert_int_equal(0, jsonctx->dynamic);
417 assert_string_equal("null]", jsonctx->in->current);
418 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
419 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
420 assert_string_equal("]", jsonctx->in->current);
421 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
422 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
423 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
424 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
425 lyjson_ctx_free(jsonctx);
426
427 /* two values */
428 str = "[{\"a\":null},\"x\"]";
429 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200430 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200431 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
432 assert_null(jsonctx->value);
433 assert_int_equal(0, jsonctx->value_len);
434 assert_int_equal(0, jsonctx->dynamic);
435 assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current);
436 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
437 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
438 assert_string_equal("a\":null},\"x\"]", jsonctx->value);
439 assert_int_equal(1, jsonctx->value_len);
440 assert_int_equal(0, jsonctx->dynamic);
441 assert_string_equal("null},\"x\"]", jsonctx->in->current);
442 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
443 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
444 assert_string_equal("},\"x\"]", jsonctx->in->current);
445 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
446 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
447 assert_string_equal(",\"x\"]", jsonctx->in->current);
448 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
449 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
450 assert_string_equal("x\"]", jsonctx->value);
451 assert_int_equal(1, jsonctx->value_len);
452 assert_int_equal(0, jsonctx->dynamic);
453 assert_string_equal("]", jsonctx->in->current);
454 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
455 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
456 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
457 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
458 lyjson_ctx_free(jsonctx);
459
460 /* new line is allowed only as escaped character in JSON */
461 str = "[ , null]";
462 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200463 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200464 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200465 CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200466 lyjson_ctx_free(jsonctx);
467
468 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200469}
470
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100471int
472main(void)
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200473{
474 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200475 UTEST(test_general),
476 UTEST(test_number),
477 UTEST(test_string),
478 UTEST(test_object),
479 UTEST(test_array),
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200480 };
481
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200483}