blob: a8d3af4f9c96ff1293c88ee4cdc078a9809a5040 [file] [log] [blame]
Radek Krejci50f0c6b2020-06-18 16:31:48 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_json.c
Radek Krejci50f0c6b2020-06-18 16:31:48 +02003 * @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
aPiecek76034c32021-06-08 15:03:11 +0200107 /* integer, positive exponent */
108 str = "550E3";
aPieceka40b4662021-05-13 15:47:03 +0200109 assert_non_null(ly_in_memory(in, str));
110 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
111 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200112 assert_string_equal("550000", jsonctx->value);
113 assert_int_equal(6, jsonctx->value_len);
114 assert_int_equal(1, jsonctx->dynamic);
aPieceka40b4662021-05-13 15:47:03 +0200115 lyjson_ctx_free(jsonctx);
116
aPiecek76034c32021-06-08 15:03:11 +0200117 str = "-550E3";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200118 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));
aPiecek76034c32021-06-08 15:03:11 +0200121 assert_string_equal("-550000", jsonctx->value);
122 assert_int_equal(7, jsonctx->value_len);
123 assert_int_equal(1, jsonctx->dynamic);
124 lyjson_ctx_free(jsonctx);
125
126 /* integer, negative exponent */
127 str = "1E-1";
128 assert_non_null(ly_in_memory(in, str));
129 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
130 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
131 assert_string_equal("0.1", jsonctx->value);
132 assert_int_equal(3, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200133 assert_int_equal(1, jsonctx->dynamic);
134 lyjson_ctx_free(jsonctx);
135
136 str = "15E-1";
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("1.5", jsonctx->value);
141 assert_int_equal(3, jsonctx->value_len);
142 assert_int_equal(1, jsonctx->dynamic);
143 lyjson_ctx_free(jsonctx);
144
aPiecek76034c32021-06-08 15:03:11 +0200145 str = "-15E-1";
aPiecek350a6bf2021-05-25 07:59:10 +0200146 assert_non_null(ly_in_memory(in, str));
147 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
148 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200149 assert_string_equal("-1.5", jsonctx->value);
aPiecek350a6bf2021-05-25 07:59:10 +0200150 assert_int_equal(4, jsonctx->value_len);
151 assert_int_equal(1, jsonctx->dynamic);
152 lyjson_ctx_free(jsonctx);
153
aPiecek76034c32021-06-08 15:03:11 +0200154 str = "16E-2";
aPiecek350a6bf2021-05-25 07:59:10 +0200155 assert_non_null(ly_in_memory(in, str));
156 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
157 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200158 assert_string_equal("0.16", jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200159 assert_int_equal(4, jsonctx->value_len);
160 assert_int_equal(1, jsonctx->dynamic);
161 lyjson_ctx_free(jsonctx);
162
aPiecek76034c32021-06-08 15:03:11 +0200163 str = "-16E-2";
aPiecek58e46a52021-05-05 10:04:47 +0200164 assert_non_null(ly_in_memory(in, str));
165 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
166 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200167 assert_string_equal("-0.16", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200168 assert_int_equal(5, jsonctx->value_len);
169 assert_int_equal(1, jsonctx->dynamic);
170 lyjson_ctx_free(jsonctx);
171
aPiecek76034c32021-06-08 15:03:11 +0200172 str = "17E-3";
aPieceka40b4662021-05-13 15:47:03 +0200173 assert_non_null(ly_in_memory(in, str));
174 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
175 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200176 assert_string_equal("0.017", jsonctx->value);
177 assert_int_equal(5, jsonctx->value_len);
178 assert_int_equal(1, jsonctx->dynamic);
179 lyjson_ctx_free(jsonctx);
180
181 str = "-17E-3";
182 assert_non_null(ly_in_memory(in, str));
183 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
184 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
185 assert_string_equal("-0.017", jsonctx->value);
186 assert_int_equal(6, jsonctx->value_len);
187 assert_int_equal(1, jsonctx->dynamic);
188 lyjson_ctx_free(jsonctx);
189
190 str = "21000E-2";
191 assert_non_null(ly_in_memory(in, str));
192 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
193 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
194 assert_string_equal("210", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200195 assert_int_equal(3, jsonctx->value_len);
196 assert_int_equal(1, jsonctx->dynamic);
197 lyjson_ctx_free(jsonctx);
198
aPiecek76034c32021-06-08 15:03:11 +0200199 str = "21000E-4";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200200 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200202 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200203 assert_string_equal("2.1", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200204 assert_int_equal(3, jsonctx->value_len);
205 assert_int_equal(1, jsonctx->dynamic);
206 lyjson_ctx_free(jsonctx);
207
aPiecek76034c32021-06-08 15:03:11 +0200208 str = "21000E-7";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200209 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200211 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200212 assert_string_equal("0.0021", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200213 assert_int_equal(6, jsonctx->value_len);
214 assert_int_equal(1, jsonctx->dynamic);
215 lyjson_ctx_free(jsonctx);
216
aPiecek76034c32021-06-08 15:03:11 +0200217 /* decimal number, positive exponent */
218 str = "5.087E1";
aPieceka40b4662021-05-13 15:47:03 +0200219 assert_non_null(ly_in_memory(in, str));
220 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
221 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200222 assert_string_equal("50.87", jsonctx->value);
223 assert_int_equal(5, jsonctx->value_len);
aPieceka40b4662021-05-13 15:47:03 +0200224 assert_int_equal(1, jsonctx->dynamic);
225 lyjson_ctx_free(jsonctx);
226
aPiecek76034c32021-06-08 15:03:11 +0200227 str = "-5.087E1";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200228 assert_non_null(ly_in_memory(in, str));
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_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200231 assert_string_equal("-50.87", jsonctx->value);
232 assert_int_equal(6, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200233 assert_int_equal(1, jsonctx->dynamic);
234 lyjson_ctx_free(jsonctx);
235
aPiecek76034c32021-06-08 15:03:11 +0200236 str = "5.087E5";
aPiecek58e46a52021-05-05 10:04:47 +0200237 assert_non_null(ly_in_memory(in, str));
238 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
239 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200240 assert_string_equal("508700", jsonctx->value);
241 assert_int_equal(6, jsonctx->value_len);
242 assert_int_equal(1, jsonctx->dynamic);
243 lyjson_ctx_free(jsonctx);
244
245 str = "59.1e+1";
246 assert_non_null(ly_in_memory(in, str));
247 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
248 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
249 assert_string_equal("591", jsonctx->value);
250 assert_int_equal(3, jsonctx->value_len);
251 assert_int_equal(1, jsonctx->dynamic);
252 lyjson_ctx_free(jsonctx);
253
254 str = "0.005087E1";
255 assert_non_null(ly_in_memory(in, str));
256 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
257 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
258 assert_string_equal("0.05087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200259 assert_int_equal(7, jsonctx->value_len);
260 assert_int_equal(1, jsonctx->dynamic);
261 lyjson_ctx_free(jsonctx);
262
aPiecek76034c32021-06-08 15:03:11 +0200263 str = "0.005087E2";
aPiecek58e46a52021-05-05 10:04:47 +0200264 assert_non_null(ly_in_memory(in, str));
265 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
266 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200267 assert_string_equal("0.5087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200268 assert_int_equal(6, jsonctx->value_len);
269 assert_int_equal(1, jsonctx->dynamic);
270 lyjson_ctx_free(jsonctx);
271
aPiecek76034c32021-06-08 15:03:11 +0200272 str = "0.005087E6";
aPieceka40b4662021-05-13 15:47:03 +0200273 assert_non_null(ly_in_memory(in, str));
274 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
275 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200276 assert_string_equal("5087", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200277 assert_int_equal(4, jsonctx->value_len);
278 assert_int_equal(1, jsonctx->dynamic);
279 lyjson_ctx_free(jsonctx);
280
aPiecek76034c32021-06-08 15:03:11 +0200281 str = "0.05087E6";
282 assert_non_null(ly_in_memory(in, str));
283 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
284 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
285 assert_string_equal("50870", jsonctx->value);
286 assert_int_equal(5, jsonctx->value_len);
287 assert_int_equal(1, jsonctx->dynamic);
288 lyjson_ctx_free(jsonctx);
289
290 str = "0.005087E8";
291 assert_non_null(ly_in_memory(in, str));
292 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
293 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
294 assert_string_equal("508700", jsonctx->value);
295 assert_int_equal(6, jsonctx->value_len);
296 assert_int_equal(1, jsonctx->dynamic);
297 lyjson_ctx_free(jsonctx);
298
299 /* decimal number, negative exponent */
300 str = "35.94e-1";
301 assert_non_null(ly_in_memory(in, str));
302 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
303 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
304 assert_string_equal("3.594", jsonctx->value);
305 assert_int_equal(5, jsonctx->value_len);
306 assert_int_equal(1, jsonctx->dynamic);
307 lyjson_ctx_free(jsonctx);
308
309 str = "-35.94e-1";
310 assert_non_null(ly_in_memory(in, str));
311 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
312 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
313 assert_string_equal("-3.594", jsonctx->value);
314 assert_int_equal(6, jsonctx->value_len);
315 assert_int_equal(1, jsonctx->dynamic);
316 lyjson_ctx_free(jsonctx);
317
318 str = "35.94e-2";
319 assert_non_null(ly_in_memory(in, str));
320 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
321 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
322 assert_string_equal("0.3594", jsonctx->value);
323 assert_int_equal(6, jsonctx->value_len);
324 assert_int_equal(1, jsonctx->dynamic);
325 lyjson_ctx_free(jsonctx);
326
327 str = "35.94e-3";
328 assert_non_null(ly_in_memory(in, str));
329 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
330 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
331 assert_string_equal("0.03594", jsonctx->value);
332 assert_int_equal(7, jsonctx->value_len);
333 assert_int_equal(1, jsonctx->dynamic);
334 lyjson_ctx_free(jsonctx);
335
336 str = "0.3594e-1";
337 assert_non_null(ly_in_memory(in, str));
338 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
339 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
340 assert_string_equal("0.03594", jsonctx->value);
341 assert_int_equal(7, jsonctx->value_len);
342 assert_int_equal(1, jsonctx->dynamic);
343 lyjson_ctx_free(jsonctx);
344
345 str = "0.03594e-1";
346 assert_non_null(ly_in_memory(in, str));
347 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
348 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
349 assert_string_equal("0.003594", jsonctx->value);
350 assert_int_equal(8, jsonctx->value_len);
351 assert_int_equal(1, jsonctx->dynamic);
352 lyjson_ctx_free(jsonctx);
353
354 str = "0.003594e-1";
355 assert_non_null(ly_in_memory(in, str));
356 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
357 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
358 assert_string_equal("0.0003594", jsonctx->value);
359 assert_int_equal(9, jsonctx->value_len);
360 assert_int_equal(1, jsonctx->dynamic);
361 lyjson_ctx_free(jsonctx);
362
363 str = "0.3594e-2";
364 assert_non_null(ly_in_memory(in, str));
365 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
366 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
367 assert_string_equal("0.003594", jsonctx->value);
368 assert_int_equal(8, jsonctx->value_len);
369 assert_int_equal(1, jsonctx->dynamic);
370 lyjson_ctx_free(jsonctx);
371
372 str = "0.03594e-2";
373 assert_non_null(ly_in_memory(in, str));
374 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
375 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
376 assert_string_equal("0.0003594", jsonctx->value);
377 assert_int_equal(9, jsonctx->value_len);
378 assert_int_equal(1, jsonctx->dynamic);
379 lyjson_ctx_free(jsonctx);
380
381 str = "0.003594e-2";
382 assert_non_null(ly_in_memory(in, str));
383 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
384 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
385 assert_string_equal("0.00003594", jsonctx->value);
386 assert_int_equal(10, jsonctx->value_len);
387 assert_int_equal(1, jsonctx->dynamic);
388 lyjson_ctx_free(jsonctx);
389
390 /* zero */
391 str = "0";
392 assert_non_null(ly_in_memory(in, str));
393 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
394 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
395 assert_true(jsonctx->value[0] == '0');
396 assert_int_equal(1, jsonctx->value_len);
397 assert_int_equal(0, jsonctx->dynamic);
398 lyjson_ctx_free(jsonctx);
399
400 str = "-0";
401 assert_non_null(ly_in_memory(in, str));
402 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
403 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
404 assert_true(jsonctx->value[0] == '-');
405 assert_true(jsonctx->value[1] == '0');
406 assert_int_equal(2, jsonctx->value_len);
407 assert_int_equal(0, jsonctx->dynamic);
408 lyjson_ctx_free(jsonctx);
409
410 str = "94E0";
411 assert_non_null(ly_in_memory(in, str));
412 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
413 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
414 assert_true(jsonctx->value[0] == '9');
415 assert_true(jsonctx->value[1] == '4');
416 assert_int_equal(2, jsonctx->value_len);
417 assert_int_equal(0, jsonctx->dynamic);
418 lyjson_ctx_free(jsonctx);
419
420 str = "0E2";
421 assert_non_null(ly_in_memory(in, str));
422 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
423 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
424 assert_true(jsonctx->value[0] == '0');
425 assert_int_equal(1, jsonctx->value_len);
426 assert_int_equal(0, jsonctx->dynamic);
427 lyjson_ctx_free(jsonctx);
428
429 str = "-0E2";
430 assert_non_null(ly_in_memory(in, str));
431 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
432 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
433 assert_true(jsonctx->value[0] == '-');
434 assert_true(jsonctx->value[1] == '0');
435 assert_int_equal(2, jsonctx->value_len);
436 assert_int_equal(0, jsonctx->dynamic);
437 lyjson_ctx_free(jsonctx);
438
439 str = "5.320e+2";
440 assert_non_null(ly_in_memory(in, str));
441 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
442 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
443 assert_string_equal("532", jsonctx->value);
444 assert_int_equal(3, jsonctx->value_len);
445 assert_int_equal(1, jsonctx->dynamic);
446 lyjson_ctx_free(jsonctx);
447
448 str = "5.320e-1";
449 assert_non_null(ly_in_memory(in, str));
450 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
451 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
452 assert_string_equal("0.532", jsonctx->value);
453 assert_int_equal(5, jsonctx->value_len);
454 assert_int_equal(1, jsonctx->dynamic);
455 lyjson_ctx_free(jsonctx);
456
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200457 /* various invalid inputs */
458 str = "-x";
459 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200460 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
461 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200462
463 str = " -";
464 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200465 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
466 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200467
468 str = "--1";
469 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200470 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
471 CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200472
473 str = "+1";
474 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200475 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
476 CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200477
478 str = " 1.x ";
479 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200480 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
481 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200482
483 str = "1.";
484 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200485 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
486 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200487
488 str = " 1eo ";
489 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200490 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
491 CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200492
493 str = "1e";
494 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200495 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
496 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200497
aPiecek76034c32021-06-08 15:03:11 +0200498 str = "1E1000";
499 assert_non_null(ly_in_memory(in, str));
500 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
501 CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", "Line number 1.");
502
503 str = "1e9999999999999999999";
504 assert_non_null(ly_in_memory(in, str));
505 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
506 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", "Line number 1.");
507
aPiecek0ba088e2021-06-15 12:53:17 +0200508 str = "1.1e66000";
509 assert_non_null(ly_in_memory(in, str));
510 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
511 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", "Line number 1.");
512
513 str = "1.1e-66000";
514 assert_non_null(ly_in_memory(in, str));
515 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
516 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", "Line number 1.");
517
aPiecek28e101a2021-06-10 09:09:31 +0200518 str = "-2.1e0.";
519 assert_non_null(ly_in_memory(in, str));
520 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
521 CHECK_LOG_CTX("Unexpected character \".\" after JSON number.", "Line number 1.");
522
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200523 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200524}
525
Radek Iša447abb82021-03-04 14:08:56 +0100526/* now string is tested in file ./tests/utests/types/string.c */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200527static void
528test_string(void **state)
529{
530 struct lyjson_ctx *jsonctx;
Radek Iša447abb82021-03-04 14:08:56 +0100531 struct ly_in *in = NULL;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200532 const char *str;
533
Radek Iša447abb82021-03-04 14:08:56 +0100534 str = "";
535 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
536
537#if 0
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200538 /* simple string */
539 str = "\"hello\"";
Radek Iša56ca9e42020-09-08 18:42:00 +0200540 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200541 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
542 assert_ptr_equal(&str[1], jsonctx->value);
543 assert_int_equal(5, jsonctx->value_len);
544 assert_int_equal(0, jsonctx->dynamic);
545 lyjson_ctx_free(jsonctx);
546
547 /* 4-byte utf8 character */
548 str = "\"\\t𠜎\"";
549 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200550 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200551 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
552 assert_string_equal("\t𠜎", jsonctx->value);
553 assert_int_equal(5, jsonctx->value_len);
554 assert_int_equal(1, jsonctx->dynamic);
555 lyjson_ctx_free(jsonctx);
556
557 /* valid escape sequences - note that here it mixes valid JSON string characters (RFC 7159, sec. 7) and
558 * valid characters in YANG string type (RFC 7950, sec. 9.4). Since the latter is a subset of JSON string,
559 * the YANG string type's restrictions apply to the JSON escape sequences */
560 str = "\"\\\" \\\\ \\r \\/ \\n \\t \\u20ac\"";
561 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200562 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200563 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
564 assert_string_equal("\" \\ \r / \n \t €", jsonctx->value);
565 assert_int_equal(15, jsonctx->value_len);
566 assert_int_equal(1, jsonctx->dynamic);
567 lyjson_ctx_free(jsonctx);
568
569 /* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */
570 str = "\"\\b\"";
571 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200572 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
573 CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200574
575 str = "\"\\f\"";
576 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200577 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
578 CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100579#endif
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200580
581 /* unterminated string */
582 str = "\"unterminated string";
583 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200584 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
585 CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100586#if 0
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200587 /* invalid escape sequence */
588 str = "\"char \\x \"";
589 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200590 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
591 CHECK_LOG_CTX("Invalid character escape sequence \\x.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200592
593 /* new line is allowed only as escaped character in JSON */
594 str = "\"\n\"";
595 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200596 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
597 CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100598#endif
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200599
600 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200601}
602
603static void
604test_object(void **state)
605{
606 struct lyjson_ctx *jsonctx;
607 struct ly_in *in;
608 const char *str;
609
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200610 /* empty */
611 str = " { } ";
612 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200613 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200614 assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0));
615 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
616 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
617 lyjson_ctx_free(jsonctx);
618
619 /* simple value */
620 str = "{\"name\" : \"Radek\"}";
621 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200622 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200623 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
624 assert_ptr_equal(&str[2], jsonctx->value);
625 assert_int_equal(4, jsonctx->value_len);
626 assert_int_equal(0, jsonctx->dynamic);
627 assert_string_equal("\"Radek\"}", jsonctx->in->current);
628 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
629 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
630 assert_string_equal("Radek\"}", jsonctx->value);
631 assert_int_equal(5, jsonctx->value_len);
632 assert_int_equal(0, jsonctx->dynamic);
633 assert_string_equal("}", jsonctx->in->current);
634 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
635 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
636 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
637 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
638 lyjson_ctx_free(jsonctx);
639
640 /* two values */
641 str = "{\"smart\" : true,\"handsom\":false}";
642 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200643 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200644 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
645 assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value);
646 assert_int_equal(5, jsonctx->value_len);
647 assert_int_equal(0, jsonctx->dynamic);
648 assert_string_equal("true,\"handsom\":false}", jsonctx->in->current);
649 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
650 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
651 assert_string_equal(",\"handsom\":false}", jsonctx->in->current);
652 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
653 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
654 assert_string_equal("handsom\":false}", jsonctx->value);
655 assert_int_equal(7, jsonctx->value_len);
656 assert_int_equal(0, jsonctx->dynamic);
657 assert_string_equal("false}", jsonctx->in->current);
658 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
659 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
660 assert_string_equal("}", jsonctx->in->current);
661 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
662 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
663 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
664 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
665 lyjson_ctx_free(jsonctx);
666
667 /* inherited objects */
668 str = "{\"person\" : {\"name\":\"Radek\"}}";
669 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200670 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200671 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
672 assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value);
673 assert_int_equal(6, jsonctx->value_len);
674 assert_int_equal(0, jsonctx->dynamic);
675 assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current);
676 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
677 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
678 assert_string_equal("name\":\"Radek\"}}", jsonctx->value);
679 assert_int_equal(4, jsonctx->value_len);
680 assert_int_equal(0, jsonctx->dynamic);
681 assert_string_equal("\"Radek\"}}", jsonctx->in->current);
682 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
683 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
684 assert_string_equal("Radek\"}}", jsonctx->value);
685 assert_int_equal(5, jsonctx->value_len);
686 assert_int_equal(0, jsonctx->dynamic);
687 assert_string_equal("}}", jsonctx->in->current);
688 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
689 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
690 assert_string_equal("}", jsonctx->in->current);
691 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
692 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
693 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
694 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
695 lyjson_ctx_free(jsonctx);
696
697 /* new line is allowed only as escaped character in JSON */
698 str = "{ unquoted : \"data\"}";
699 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200700 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
701 CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200702
703 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200704}
705
706static void
707test_array(void **state)
708{
709 struct lyjson_ctx *jsonctx;
710 struct ly_in *in;
711 const char *str;
712
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200713 /* empty */
714 str = " [ ] ";
715 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200716 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200717 assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0));
718 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
719 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
720 lyjson_ctx_free(jsonctx);
721
722 /* simple value */
723 str = "[ null]";
724 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200725 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200726 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
727 assert_null(jsonctx->value);
728 assert_int_equal(0, jsonctx->value_len);
729 assert_int_equal(0, jsonctx->dynamic);
730 assert_string_equal("null]", jsonctx->in->current);
731 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
732 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
733 assert_string_equal("]", jsonctx->in->current);
734 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
735 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
736 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
737 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
738 lyjson_ctx_free(jsonctx);
739
740 /* two values */
741 str = "[{\"a\":null},\"x\"]";
742 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200743 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200744 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
745 assert_null(jsonctx->value);
746 assert_int_equal(0, jsonctx->value_len);
747 assert_int_equal(0, jsonctx->dynamic);
748 assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current);
749 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
750 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
751 assert_string_equal("a\":null},\"x\"]", jsonctx->value);
752 assert_int_equal(1, jsonctx->value_len);
753 assert_int_equal(0, jsonctx->dynamic);
754 assert_string_equal("null},\"x\"]", jsonctx->in->current);
755 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
756 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
757 assert_string_equal("},\"x\"]", jsonctx->in->current);
758 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
759 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
760 assert_string_equal(",\"x\"]", jsonctx->in->current);
761 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
762 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
763 assert_string_equal("x\"]", jsonctx->value);
764 assert_int_equal(1, jsonctx->value_len);
765 assert_int_equal(0, jsonctx->dynamic);
766 assert_string_equal("]", jsonctx->in->current);
767 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
768 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
769 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
770 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
771 lyjson_ctx_free(jsonctx);
772
773 /* new line is allowed only as escaped character in JSON */
774 str = "[ , null]";
775 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200776 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200777 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200778 CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200779 lyjson_ctx_free(jsonctx);
780
781 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200782}
783
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100784int
785main(void)
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200786{
787 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200788 UTEST(test_general),
789 UTEST(test_number),
790 UTEST(test_string),
791 UTEST(test_object),
792 UTEST(test_array),
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200793 };
794
Radek Iša56ca9e42020-09-08 18:42:00 +0200795 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200796}