blob: b2cfafd0f4cb5553d4688082b5fa0ffc2c513c8f [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
aPiecek28e101a2021-06-10 09:09:31 +0200508 str = "-2.1e0.";
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("Unexpected character \".\" after JSON number.", "Line number 1.");
512
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200513 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200514}
515
Radek Iša447abb82021-03-04 14:08:56 +0100516/* now string is tested in file ./tests/utests/types/string.c */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200517static void
518test_string(void **state)
519{
520 struct lyjson_ctx *jsonctx;
Radek Iša447abb82021-03-04 14:08:56 +0100521 struct ly_in *in = NULL;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200522 const char *str;
523
Radek Iša447abb82021-03-04 14:08:56 +0100524 str = "";
525 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
526
527#if 0
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200528 /* simple string */
529 str = "\"hello\"";
Radek Iša56ca9e42020-09-08 18:42:00 +0200530 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200531 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
532 assert_ptr_equal(&str[1], jsonctx->value);
533 assert_int_equal(5, jsonctx->value_len);
534 assert_int_equal(0, jsonctx->dynamic);
535 lyjson_ctx_free(jsonctx);
536
537 /* 4-byte utf8 character */
538 str = "\"\\t𠜎\"";
539 assert_non_null(ly_in_memory(in, str));
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_string_equal("\t𠜎", jsonctx->value);
543 assert_int_equal(5, jsonctx->value_len);
544 assert_int_equal(1, jsonctx->dynamic);
545 lyjson_ctx_free(jsonctx);
546
547 /* valid escape sequences - note that here it mixes valid JSON string characters (RFC 7159, sec. 7) and
548 * valid characters in YANG string type (RFC 7950, sec. 9.4). Since the latter is a subset of JSON string,
549 * the YANG string type's restrictions apply to the JSON escape sequences */
550 str = "\"\\\" \\\\ \\r \\/ \\n \\t \\u20ac\"";
551 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200552 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200553 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
554 assert_string_equal("\" \\ \r / \n \t €", jsonctx->value);
555 assert_int_equal(15, jsonctx->value_len);
556 assert_int_equal(1, jsonctx->dynamic);
557 lyjson_ctx_free(jsonctx);
558
559 /* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */
560 str = "\"\\b\"";
561 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200562 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
563 CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200564
565 str = "\"\\f\"";
566 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200567 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
568 CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100569#endif
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200570
571 /* unterminated string */
572 str = "\"unterminated string";
573 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200574 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
575 CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100576#if 0
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200577 /* invalid escape sequence */
578 str = "\"char \\x \"";
579 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200580 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
581 CHECK_LOG_CTX("Invalid character escape sequence \\x.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200582
583 /* new line is allowed only as escaped character in JSON */
584 str = "\"\n\"";
585 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200586 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
587 CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100588#endif
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200589
590 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200591}
592
593static void
594test_object(void **state)
595{
596 struct lyjson_ctx *jsonctx;
597 struct ly_in *in;
598 const char *str;
599
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200600 /* empty */
601 str = " { } ";
602 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200603 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200604 assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0));
605 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
606 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
607 lyjson_ctx_free(jsonctx);
608
609 /* simple value */
610 str = "{\"name\" : \"Radek\"}";
611 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200612 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200613 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
614 assert_ptr_equal(&str[2], jsonctx->value);
615 assert_int_equal(4, jsonctx->value_len);
616 assert_int_equal(0, jsonctx->dynamic);
617 assert_string_equal("\"Radek\"}", jsonctx->in->current);
618 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
619 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
620 assert_string_equal("Radek\"}", jsonctx->value);
621 assert_int_equal(5, jsonctx->value_len);
622 assert_int_equal(0, jsonctx->dynamic);
623 assert_string_equal("}", jsonctx->in->current);
624 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
625 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
626 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
627 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
628 lyjson_ctx_free(jsonctx);
629
630 /* two values */
631 str = "{\"smart\" : true,\"handsom\":false}";
632 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200633 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200634 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
635 assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value);
636 assert_int_equal(5, jsonctx->value_len);
637 assert_int_equal(0, jsonctx->dynamic);
638 assert_string_equal("true,\"handsom\":false}", jsonctx->in->current);
639 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
640 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
641 assert_string_equal(",\"handsom\":false}", jsonctx->in->current);
642 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
643 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
644 assert_string_equal("handsom\":false}", jsonctx->value);
645 assert_int_equal(7, jsonctx->value_len);
646 assert_int_equal(0, jsonctx->dynamic);
647 assert_string_equal("false}", jsonctx->in->current);
648 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
649 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
650 assert_string_equal("}", jsonctx->in->current);
651 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
652 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
653 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
654 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
655 lyjson_ctx_free(jsonctx);
656
657 /* inherited objects */
658 str = "{\"person\" : {\"name\":\"Radek\"}}";
659 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200660 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200661 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
662 assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value);
663 assert_int_equal(6, jsonctx->value_len);
664 assert_int_equal(0, jsonctx->dynamic);
665 assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current);
666 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
667 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
668 assert_string_equal("name\":\"Radek\"}}", jsonctx->value);
669 assert_int_equal(4, jsonctx->value_len);
670 assert_int_equal(0, jsonctx->dynamic);
671 assert_string_equal("\"Radek\"}}", jsonctx->in->current);
672 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
673 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
674 assert_string_equal("Radek\"}}", jsonctx->value);
675 assert_int_equal(5, jsonctx->value_len);
676 assert_int_equal(0, jsonctx->dynamic);
677 assert_string_equal("}}", jsonctx->in->current);
678 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
679 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
680 assert_string_equal("}", jsonctx->in->current);
681 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
682 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
683 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
684 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
685 lyjson_ctx_free(jsonctx);
686
687 /* new line is allowed only as escaped character in JSON */
688 str = "{ unquoted : \"data\"}";
689 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200690 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
691 CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200692
693 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200694}
695
696static void
697test_array(void **state)
698{
699 struct lyjson_ctx *jsonctx;
700 struct ly_in *in;
701 const char *str;
702
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200703 /* empty */
704 str = " [ ] ";
705 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200706 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200707 assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0));
708 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
709 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
710 lyjson_ctx_free(jsonctx);
711
712 /* simple value */
713 str = "[ null]";
714 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200715 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200716 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
717 assert_null(jsonctx->value);
718 assert_int_equal(0, jsonctx->value_len);
719 assert_int_equal(0, jsonctx->dynamic);
720 assert_string_equal("null]", jsonctx->in->current);
721 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
722 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
723 assert_string_equal("]", jsonctx->in->current);
724 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
725 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
726 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
727 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
728 lyjson_ctx_free(jsonctx);
729
730 /* two values */
731 str = "[{\"a\":null},\"x\"]";
732 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200733 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200734 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
735 assert_null(jsonctx->value);
736 assert_int_equal(0, jsonctx->value_len);
737 assert_int_equal(0, jsonctx->dynamic);
738 assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current);
739 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
740 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
741 assert_string_equal("a\":null},\"x\"]", jsonctx->value);
742 assert_int_equal(1, jsonctx->value_len);
743 assert_int_equal(0, jsonctx->dynamic);
744 assert_string_equal("null},\"x\"]", jsonctx->in->current);
745 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
746 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
747 assert_string_equal("},\"x\"]", jsonctx->in->current);
748 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
749 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
750 assert_string_equal(",\"x\"]", jsonctx->in->current);
751 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
752 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
753 assert_string_equal("x\"]", jsonctx->value);
754 assert_int_equal(1, jsonctx->value_len);
755 assert_int_equal(0, jsonctx->dynamic);
756 assert_string_equal("]", jsonctx->in->current);
757 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
758 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
759 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
760 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
761 lyjson_ctx_free(jsonctx);
762
763 /* new line is allowed only as escaped character in JSON */
764 str = "[ , null]";
765 assert_non_null(ly_in_memory(in, str));
Radek Iša56ca9e42020-09-08 18:42:00 +0200766 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200767 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200768 CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200769 lyjson_ctx_free(jsonctx);
770
771 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200772}
773
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100774int
775main(void)
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200776{
777 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200778 UTEST(test_general),
779 UTEST(test_number),
780 UTEST(test_string),
781 UTEST(test_object),
782 UTEST(test_array),
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200783 };
784
Radek Iša56ca9e42020-09-08 18:42:00 +0200785 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200786}