blob: 1896b8adf4ad7b907982d815bfeb687cbb0967a7 [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"
Radek Krejci50f0c6b2020-06-18 16:31:48 +020020static void
21test_general(void **state)
22{
23 struct lyjson_ctx *jsonctx;
24 struct ly_in *in;
25 const char *str;
26
Radek Krejci50f0c6b2020-06-18 16:31:48 +020027 /* empty */
28 str = "";
29 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010030 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020031 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
32 lyjson_ctx_free(jsonctx);
33
34 str = " \n\t \n";
35 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010036 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020037 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
38 lyjson_ctx_free(jsonctx);
39
40 /* constant values */
41 str = "true";
42 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010043 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020044 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
45 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
46 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
47 lyjson_ctx_free(jsonctx);
48
49 str = "false";
50 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010051 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020052 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
53 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
54 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
55 lyjson_ctx_free(jsonctx);
56
57 str = "null";
58 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010059 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020060 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
61 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
62 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
63 lyjson_ctx_free(jsonctx);
64
65 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020066}
67
68static void
69test_number(void **state)
70{
71 struct lyjson_ctx *jsonctx;
72 struct ly_in *in;
73 const char *str;
74
Radek Krejci50f0c6b2020-06-18 16:31:48 +020075 /* simple value */
76 str = "11";
77 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010078 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020079 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
80 assert_string_equal("11", jsonctx->value);
81 assert_int_equal(2, jsonctx->value_len);
82 assert_int_equal(0, jsonctx->dynamic);
83 lyjson_ctx_free(jsonctx);
84
85 /* fraction number */
86 str = "37.7668";
87 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010088 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020089 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
90 assert_string_equal("37.7668", jsonctx->value);
91 assert_int_equal(7, jsonctx->value_len);
92 assert_int_equal(0, jsonctx->dynamic);
93 lyjson_ctx_free(jsonctx);
94
95 /* negative number */
96 str = "-122.3959";
97 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +010098 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +020099 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
100 assert_string_equal("-122.3959", jsonctx->value);
101 assert_int_equal(9, jsonctx->value_len);
102 assert_int_equal(0, jsonctx->dynamic);
103 lyjson_ctx_free(jsonctx);
104
aPiecek76034c32021-06-08 15:03:11 +0200105 /* integer, positive exponent */
106 str = "550E3";
aPieceka40b4662021-05-13 15:47:03 +0200107 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100108 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200109 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200110 assert_string_equal("550000", jsonctx->value);
111 assert_int_equal(6, jsonctx->value_len);
112 assert_int_equal(1, jsonctx->dynamic);
aPieceka40b4662021-05-13 15:47:03 +0200113 lyjson_ctx_free(jsonctx);
114
aPiecek76034c32021-06-08 15:03:11 +0200115 str = "-550E3";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200116 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100117 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200118 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200119 assert_string_equal("-550000", jsonctx->value);
120 assert_int_equal(7, jsonctx->value_len);
121 assert_int_equal(1, jsonctx->dynamic);
122 lyjson_ctx_free(jsonctx);
123
124 /* integer, negative exponent */
125 str = "1E-1";
126 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100127 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200128 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
129 assert_string_equal("0.1", jsonctx->value);
130 assert_int_equal(3, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200131 assert_int_equal(1, jsonctx->dynamic);
132 lyjson_ctx_free(jsonctx);
133
134 str = "15E-1";
135 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100136 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200137 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
138 assert_string_equal("1.5", jsonctx->value);
139 assert_int_equal(3, jsonctx->value_len);
140 assert_int_equal(1, jsonctx->dynamic);
141 lyjson_ctx_free(jsonctx);
142
aPiecek76034c32021-06-08 15:03:11 +0200143 str = "-15E-1";
aPiecek350a6bf2021-05-25 07:59:10 +0200144 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100145 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek350a6bf2021-05-25 07:59:10 +0200146 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200147 assert_string_equal("-1.5", jsonctx->value);
aPiecek350a6bf2021-05-25 07:59:10 +0200148 assert_int_equal(4, jsonctx->value_len);
149 assert_int_equal(1, jsonctx->dynamic);
150 lyjson_ctx_free(jsonctx);
151
aPiecek76034c32021-06-08 15:03:11 +0200152 str = "16E-2";
aPiecek350a6bf2021-05-25 07:59:10 +0200153 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100154 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek350a6bf2021-05-25 07:59:10 +0200155 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200156 assert_string_equal("0.16", jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200157 assert_int_equal(4, jsonctx->value_len);
158 assert_int_equal(1, jsonctx->dynamic);
159 lyjson_ctx_free(jsonctx);
160
aPiecek76034c32021-06-08 15:03:11 +0200161 str = "-16E-2";
aPiecek58e46a52021-05-05 10:04:47 +0200162 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100163 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek58e46a52021-05-05 10:04:47 +0200164 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200165 assert_string_equal("-0.16", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200166 assert_int_equal(5, jsonctx->value_len);
167 assert_int_equal(1, jsonctx->dynamic);
168 lyjson_ctx_free(jsonctx);
169
aPiecek76034c32021-06-08 15:03:11 +0200170 str = "17E-3";
aPieceka40b4662021-05-13 15:47:03 +0200171 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100172 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200173 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200174 assert_string_equal("0.017", jsonctx->value);
175 assert_int_equal(5, jsonctx->value_len);
176 assert_int_equal(1, jsonctx->dynamic);
177 lyjson_ctx_free(jsonctx);
178
179 str = "-17E-3";
180 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100181 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200182 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
183 assert_string_equal("-0.017", jsonctx->value);
184 assert_int_equal(6, jsonctx->value_len);
185 assert_int_equal(1, jsonctx->dynamic);
186 lyjson_ctx_free(jsonctx);
187
188 str = "21000E-2";
189 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100190 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200191 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
192 assert_string_equal("210", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200193 assert_int_equal(3, jsonctx->value_len);
194 assert_int_equal(1, jsonctx->dynamic);
195 lyjson_ctx_free(jsonctx);
196
aPiecek76034c32021-06-08 15:03:11 +0200197 str = "21000E-4";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200198 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100199 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200200 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200201 assert_string_equal("2.1", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200202 assert_int_equal(3, jsonctx->value_len);
203 assert_int_equal(1, jsonctx->dynamic);
204 lyjson_ctx_free(jsonctx);
205
aPiecek76034c32021-06-08 15:03:11 +0200206 str = "21000E-7";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200207 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100208 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200209 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200210 assert_string_equal("0.0021", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200211 assert_int_equal(6, jsonctx->value_len);
212 assert_int_equal(1, jsonctx->dynamic);
213 lyjson_ctx_free(jsonctx);
214
aPiecek76034c32021-06-08 15:03:11 +0200215 /* decimal number, positive exponent */
216 str = "5.087E1";
aPieceka40b4662021-05-13 15:47:03 +0200217 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100218 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200219 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200220 assert_string_equal("50.87", jsonctx->value);
221 assert_int_equal(5, jsonctx->value_len);
aPieceka40b4662021-05-13 15:47:03 +0200222 assert_int_equal(1, jsonctx->dynamic);
223 lyjson_ctx_free(jsonctx);
224
aPiecek76034c32021-06-08 15:03:11 +0200225 str = "-5.087E1";
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200226 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100227 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200228 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200229 assert_string_equal("-50.87", jsonctx->value);
230 assert_int_equal(6, jsonctx->value_len);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200231 assert_int_equal(1, jsonctx->dynamic);
232 lyjson_ctx_free(jsonctx);
233
aPiecek76034c32021-06-08 15:03:11 +0200234 str = "5.087E5";
aPiecek58e46a52021-05-05 10:04:47 +0200235 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100236 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek58e46a52021-05-05 10:04:47 +0200237 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200238 assert_string_equal("508700", jsonctx->value);
239 assert_int_equal(6, jsonctx->value_len);
240 assert_int_equal(1, jsonctx->dynamic);
241 lyjson_ctx_free(jsonctx);
242
243 str = "59.1e+1";
244 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100245 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200246 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
247 assert_string_equal("591", jsonctx->value);
248 assert_int_equal(3, jsonctx->value_len);
249 assert_int_equal(1, jsonctx->dynamic);
250 lyjson_ctx_free(jsonctx);
251
252 str = "0.005087E1";
253 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100254 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200255 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
256 assert_string_equal("0.05087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200257 assert_int_equal(7, jsonctx->value_len);
258 assert_int_equal(1, jsonctx->dynamic);
259 lyjson_ctx_free(jsonctx);
260
aPiecek76034c32021-06-08 15:03:11 +0200261 str = "0.005087E2";
aPiecek58e46a52021-05-05 10:04:47 +0200262 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek58e46a52021-05-05 10:04:47 +0200264 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200265 assert_string_equal("0.5087", jsonctx->value);
aPiecek58e46a52021-05-05 10:04:47 +0200266 assert_int_equal(6, jsonctx->value_len);
267 assert_int_equal(1, jsonctx->dynamic);
268 lyjson_ctx_free(jsonctx);
269
aPiecek76034c32021-06-08 15:03:11 +0200270 str = "0.005087E6";
aPieceka40b4662021-05-13 15:47:03 +0200271 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100272 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPieceka40b4662021-05-13 15:47:03 +0200273 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
aPiecek76034c32021-06-08 15:03:11 +0200274 assert_string_equal("5087", jsonctx->value);
aPieceka40b4662021-05-13 15:47:03 +0200275 assert_int_equal(4, jsonctx->value_len);
276 assert_int_equal(1, jsonctx->dynamic);
277 lyjson_ctx_free(jsonctx);
278
aPiecek76034c32021-06-08 15:03:11 +0200279 str = "0.05087E6";
280 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100281 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200282 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
283 assert_string_equal("50870", jsonctx->value);
284 assert_int_equal(5, jsonctx->value_len);
285 assert_int_equal(1, jsonctx->dynamic);
286 lyjson_ctx_free(jsonctx);
287
288 str = "0.005087E8";
289 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100290 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200291 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
292 assert_string_equal("508700", jsonctx->value);
293 assert_int_equal(6, jsonctx->value_len);
294 assert_int_equal(1, jsonctx->dynamic);
295 lyjson_ctx_free(jsonctx);
296
297 /* decimal number, negative exponent */
298 str = "35.94e-1";
299 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100300 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200301 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
302 assert_string_equal("3.594", jsonctx->value);
303 assert_int_equal(5, jsonctx->value_len);
304 assert_int_equal(1, jsonctx->dynamic);
305 lyjson_ctx_free(jsonctx);
306
307 str = "-35.94e-1";
308 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100309 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200310 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
311 assert_string_equal("-3.594", jsonctx->value);
312 assert_int_equal(6, jsonctx->value_len);
313 assert_int_equal(1, jsonctx->dynamic);
314 lyjson_ctx_free(jsonctx);
315
316 str = "35.94e-2";
317 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100318 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200319 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
320 assert_string_equal("0.3594", jsonctx->value);
321 assert_int_equal(6, jsonctx->value_len);
322 assert_int_equal(1, jsonctx->dynamic);
323 lyjson_ctx_free(jsonctx);
324
325 str = "35.94e-3";
326 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100327 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200328 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
329 assert_string_equal("0.03594", jsonctx->value);
330 assert_int_equal(7, jsonctx->value_len);
331 assert_int_equal(1, jsonctx->dynamic);
332 lyjson_ctx_free(jsonctx);
333
334 str = "0.3594e-1";
335 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100336 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200337 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
338 assert_string_equal("0.03594", jsonctx->value);
339 assert_int_equal(7, jsonctx->value_len);
340 assert_int_equal(1, jsonctx->dynamic);
341 lyjson_ctx_free(jsonctx);
342
343 str = "0.03594e-1";
344 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100345 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200346 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
347 assert_string_equal("0.003594", jsonctx->value);
348 assert_int_equal(8, jsonctx->value_len);
349 assert_int_equal(1, jsonctx->dynamic);
350 lyjson_ctx_free(jsonctx);
351
352 str = "0.003594e-1";
353 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100354 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200355 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
356 assert_string_equal("0.0003594", jsonctx->value);
357 assert_int_equal(9, jsonctx->value_len);
358 assert_int_equal(1, jsonctx->dynamic);
359 lyjson_ctx_free(jsonctx);
360
361 str = "0.3594e-2";
362 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100363 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200364 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
365 assert_string_equal("0.003594", jsonctx->value);
366 assert_int_equal(8, jsonctx->value_len);
367 assert_int_equal(1, jsonctx->dynamic);
368 lyjson_ctx_free(jsonctx);
369
370 str = "0.03594e-2";
371 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100372 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200373 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
374 assert_string_equal("0.0003594", jsonctx->value);
375 assert_int_equal(9, jsonctx->value_len);
376 assert_int_equal(1, jsonctx->dynamic);
377 lyjson_ctx_free(jsonctx);
378
379 str = "0.003594e-2";
380 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100381 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200382 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
383 assert_string_equal("0.00003594", jsonctx->value);
384 assert_int_equal(10, jsonctx->value_len);
385 assert_int_equal(1, jsonctx->dynamic);
386 lyjson_ctx_free(jsonctx);
387
388 /* zero */
389 str = "0";
390 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100391 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200392 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
393 assert_true(jsonctx->value[0] == '0');
394 assert_int_equal(1, jsonctx->value_len);
395 assert_int_equal(0, jsonctx->dynamic);
396 lyjson_ctx_free(jsonctx);
397
398 str = "-0";
399 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100400 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200401 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
402 assert_true(jsonctx->value[0] == '-');
403 assert_true(jsonctx->value[1] == '0');
404 assert_int_equal(2, jsonctx->value_len);
405 assert_int_equal(0, jsonctx->dynamic);
406 lyjson_ctx_free(jsonctx);
407
408 str = "94E0";
409 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100410 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200411 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
412 assert_true(jsonctx->value[0] == '9');
413 assert_true(jsonctx->value[1] == '4');
414 assert_int_equal(2, jsonctx->value_len);
415 assert_int_equal(0, jsonctx->dynamic);
416 lyjson_ctx_free(jsonctx);
417
418 str = "0E2";
419 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100420 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200421 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
422 assert_true(jsonctx->value[0] == '0');
423 assert_int_equal(1, jsonctx->value_len);
424 assert_int_equal(0, jsonctx->dynamic);
425 lyjson_ctx_free(jsonctx);
426
427 str = "-0E2";
428 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100429 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200430 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
431 assert_true(jsonctx->value[0] == '-');
432 assert_true(jsonctx->value[1] == '0');
433 assert_int_equal(2, jsonctx->value_len);
434 assert_int_equal(0, jsonctx->dynamic);
435 lyjson_ctx_free(jsonctx);
436
437 str = "5.320e+2";
438 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100439 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200440 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
441 assert_string_equal("532", jsonctx->value);
442 assert_int_equal(3, jsonctx->value_len);
443 assert_int_equal(1, jsonctx->dynamic);
444 lyjson_ctx_free(jsonctx);
445
446 str = "5.320e-1";
447 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100448 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200449 assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0));
450 assert_string_equal("0.532", jsonctx->value);
451 assert_int_equal(5, jsonctx->value_len);
452 assert_int_equal(1, jsonctx->dynamic);
453 lyjson_ctx_free(jsonctx);
454
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200455 /* various invalid inputs */
456 str = "-x";
457 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100458 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200459 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200460
461 str = " -";
462 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100463 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200465
466 str = "--1";
467 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100468 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200469 CHECK_LOG_CTX("Invalid character in JSON Number value (\"-\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200470
471 str = "+1";
472 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100473 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200474 CHECK_LOG_CTX("Invalid character sequence \"+1\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200475
476 str = " 1.x ";
477 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100478 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200479 CHECK_LOG_CTX("Invalid character in JSON Number value (\"x\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200480
481 str = "1.";
482 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100483 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200484 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200485
486 str = " 1eo ";
487 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 CHECK_LOG_CTX("Invalid character in JSON Number value (\"o\").", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200490
491 str = "1e";
492 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100493 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200494 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200495
aPiecek76034c32021-06-08 15:03:11 +0200496 str = "1E1000";
497 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100498 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200499 CHECK_LOG_CTX("Number encoded as a string exceeded the LY_NUMBER_MAXLEN limit.", "Line number 1.");
500
501 str = "1e9999999999999999999";
502 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100503 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek76034c32021-06-08 15:03:11 +0200504 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1e9999999999999999999).", "Line number 1.");
505
aPiecek0ba088e2021-06-15 12:53:17 +0200506 str = "1.1e66000";
507 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100508 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek0ba088e2021-06-15 12:53:17 +0200509 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e66000).", "Line number 1.");
510
511 str = "1.1e-66000";
512 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100513 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek0ba088e2021-06-15 12:53:17 +0200514 CHECK_LOG_CTX("Exponent out-of-bounds in a JSON Number value (1.1e-66000).", "Line number 1.");
515
aPiecek28e101a2021-06-10 09:09:31 +0200516 str = "-2.1e0.";
517 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100518 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
aPiecek28e101a2021-06-10 09:09:31 +0200519 CHECK_LOG_CTX("Unexpected character \".\" after JSON number.", "Line number 1.");
520
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200521 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200522}
523
Radek Iša447abb82021-03-04 14:08:56 +0100524/* now string is tested in file ./tests/utests/types/string.c */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200525static void
526test_string(void **state)
527{
528 struct lyjson_ctx *jsonctx;
Radek Iša447abb82021-03-04 14:08:56 +0100529 struct ly_in *in = NULL;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200530 const char *str;
531
Radek Iša447abb82021-03-04 14:08:56 +0100532 str = "";
533 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
534
535#if 0
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200536 /* simple string */
537 str = "\"hello\"";
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100538 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200539 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
540 assert_ptr_equal(&str[1], jsonctx->value);
541 assert_int_equal(5, jsonctx->value_len);
542 assert_int_equal(0, jsonctx->dynamic);
543 lyjson_ctx_free(jsonctx);
544
545 /* 4-byte utf8 character */
546 str = "\"\\t𠜎\"";
547 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100548 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200549 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
550 assert_string_equal("\t𠜎", jsonctx->value);
551 assert_int_equal(5, jsonctx->value_len);
552 assert_int_equal(1, jsonctx->dynamic);
553 lyjson_ctx_free(jsonctx);
554
555 /* valid escape sequences - note that here it mixes valid JSON string characters (RFC 7159, sec. 7) and
556 * valid characters in YANG string type (RFC 7950, sec. 9.4). Since the latter is a subset of JSON string,
557 * the YANG string type's restrictions apply to the JSON escape sequences */
558 str = "\"\\\" \\\\ \\r \\/ \\n \\t \\u20ac\"";
559 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100560 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200561 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
562 assert_string_equal("\" \\ \r / \n \t €", jsonctx->value);
563 assert_int_equal(15, jsonctx->value_len);
564 assert_int_equal(1, jsonctx->dynamic);
565 lyjson_ctx_free(jsonctx);
566
567 /* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */
568 str = "\"\\b\"";
569 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100570 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200571 CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200572
573 str = "\"\\f\"";
574 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100575 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200576 CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100577#endif
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200578
579 /* unterminated string */
580 str = "\"unterminated string";
581 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100582 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200583 CHECK_LOG_CTX("Missing quotation-mark at the end of a JSON string.", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100584#if 0
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200585 /* invalid escape sequence */
586 str = "\"char \\x \"";
587 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100588 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200589 CHECK_LOG_CTX("Invalid character escape sequence \\x.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200590
591 /* new line is allowed only as escaped character in JSON */
592 str = "\"\n\"";
593 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100594 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200595 CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", "Line number 1.");
Radek Iša447abb82021-03-04 14:08:56 +0100596#endif
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200597
598 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200599}
600
601static void
602test_object(void **state)
603{
604 struct lyjson_ctx *jsonctx;
605 struct ly_in *in;
606 const char *str;
607
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200608 /* empty */
609 str = " { } ";
610 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100611 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200612 assert_int_equal(LYJSON_OBJECT_EMPTY, lyjson_ctx_status(jsonctx, 0));
613 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
614 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
615 lyjson_ctx_free(jsonctx);
616
617 /* simple value */
618 str = "{\"name\" : \"Radek\"}";
619 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100620 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200621 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
622 assert_ptr_equal(&str[2], jsonctx->value);
623 assert_int_equal(4, jsonctx->value_len);
624 assert_int_equal(0, jsonctx->dynamic);
625 assert_string_equal("\"Radek\"}", jsonctx->in->current);
626 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
627 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
628 assert_string_equal("Radek\"}", jsonctx->value);
629 assert_int_equal(5, jsonctx->value_len);
630 assert_int_equal(0, jsonctx->dynamic);
631 assert_string_equal("}", jsonctx->in->current);
632 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
633 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
634 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
635 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
636 lyjson_ctx_free(jsonctx);
637
638 /* two values */
639 str = "{\"smart\" : true,\"handsom\":false}";
640 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100641 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200642 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
643 assert_string_equal("smart\" : true,\"handsom\":false}", jsonctx->value);
644 assert_int_equal(5, jsonctx->value_len);
645 assert_int_equal(0, jsonctx->dynamic);
646 assert_string_equal("true,\"handsom\":false}", jsonctx->in->current);
647 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
648 assert_int_equal(LYJSON_TRUE, lyjson_ctx_status(jsonctx, 0));
649 assert_string_equal(",\"handsom\":false}", jsonctx->in->current);
650 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
651 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
652 assert_string_equal("handsom\":false}", jsonctx->value);
653 assert_int_equal(7, jsonctx->value_len);
654 assert_int_equal(0, jsonctx->dynamic);
655 assert_string_equal("false}", jsonctx->in->current);
656 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
657 assert_int_equal(LYJSON_FALSE, lyjson_ctx_status(jsonctx, 0));
658 assert_string_equal("}", jsonctx->in->current);
659 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
660 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
661 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
662 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
663 lyjson_ctx_free(jsonctx);
664
665 /* inherited objects */
666 str = "{\"person\" : {\"name\":\"Radek\"}}";
667 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100668 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200669 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
670 assert_string_equal("person\" : {\"name\":\"Radek\"}}", jsonctx->value);
671 assert_int_equal(6, jsonctx->value_len);
672 assert_int_equal(0, jsonctx->dynamic);
673 assert_string_equal("{\"name\":\"Radek\"}}", jsonctx->in->current);
674 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
675 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
676 assert_string_equal("name\":\"Radek\"}}", jsonctx->value);
677 assert_int_equal(4, jsonctx->value_len);
678 assert_int_equal(0, jsonctx->dynamic);
679 assert_string_equal("\"Radek\"}}", jsonctx->in->current);
680 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
681 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
682 assert_string_equal("Radek\"}}", jsonctx->value);
683 assert_int_equal(5, jsonctx->value_len);
684 assert_int_equal(0, jsonctx->dynamic);
685 assert_string_equal("}}", jsonctx->in->current);
686 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
687 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
688 assert_string_equal("}", jsonctx->in->current);
689 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
690 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
691 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
692 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
693 lyjson_ctx_free(jsonctx);
694
695 /* new line is allowed only as escaped character in JSON */
696 str = "{ unquoted : \"data\"}";
697 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100698 assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Iša56ca9e42020-09-08 18:42:00 +0200699 CHECK_LOG_CTX("Invalid character sequence \"unquoted : \"data\"}\", expected a JSON object's member.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200700
701 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200702}
703
704static void
705test_array(void **state)
706{
707 struct lyjson_ctx *jsonctx;
708 struct ly_in *in;
709 const char *str;
710
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200711 /* empty */
712 str = " [ ] ";
713 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100714 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200715 assert_int_equal(LYJSON_ARRAY_EMPTY, lyjson_ctx_status(jsonctx, 0));
716 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
717 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
718 lyjson_ctx_free(jsonctx);
719
720 /* simple value */
721 str = "[ null]";
722 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100723 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200724 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
725 assert_null(jsonctx->value);
726 assert_int_equal(0, jsonctx->value_len);
727 assert_int_equal(0, jsonctx->dynamic);
728 assert_string_equal("null]", jsonctx->in->current);
729 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
730 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
731 assert_string_equal("]", jsonctx->in->current);
732 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
733 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
734 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
735 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
736 lyjson_ctx_free(jsonctx);
737
738 /* two values */
739 str = "[{\"a\":null},\"x\"]";
740 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100741 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200742 assert_int_equal(LYJSON_ARRAY, lyjson_ctx_status(jsonctx, 0));
743 assert_null(jsonctx->value);
744 assert_int_equal(0, jsonctx->value_len);
745 assert_int_equal(0, jsonctx->dynamic);
746 assert_string_equal("{\"a\":null},\"x\"]", jsonctx->in->current);
747 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
748 assert_int_equal(LYJSON_OBJECT, lyjson_ctx_status(jsonctx, 0));
749 assert_string_equal("a\":null},\"x\"]", jsonctx->value);
750 assert_int_equal(1, jsonctx->value_len);
751 assert_int_equal(0, jsonctx->dynamic);
752 assert_string_equal("null},\"x\"]", jsonctx->in->current);
753 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
754 assert_int_equal(LYJSON_NULL, lyjson_ctx_status(jsonctx, 0));
755 assert_string_equal("},\"x\"]", jsonctx->in->current);
756 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
757 assert_int_equal(LYJSON_OBJECT_CLOSED, lyjson_ctx_status(jsonctx, 0));
758 assert_string_equal(",\"x\"]", jsonctx->in->current);
759 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
760 assert_int_equal(LYJSON_STRING, lyjson_ctx_status(jsonctx, 0));
761 assert_string_equal("x\"]", jsonctx->value);
762 assert_int_equal(1, jsonctx->value_len);
763 assert_int_equal(0, jsonctx->dynamic);
764 assert_string_equal("]", jsonctx->in->current);
765 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
766 assert_int_equal(LYJSON_ARRAY_CLOSED, lyjson_ctx_status(jsonctx, 0));
767 assert_int_equal(LY_SUCCESS, lyjson_ctx_next(jsonctx, NULL));
768 assert_int_equal(LYJSON_END, lyjson_ctx_status(jsonctx, 0));
769 lyjson_ctx_free(jsonctx);
770
771 /* new line is allowed only as escaped character in JSON */
772 str = "[ , null]";
773 assert_non_null(ly_in_memory(in, str));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100774 assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, 0, &jsonctx));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200775 assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200776 CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200777 lyjson_ctx_free(jsonctx);
778
779 ly_in_free(in, 0);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200780}
781
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100782int
783main(void)
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200784{
785 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200786 UTEST(test_general),
787 UTEST(test_number),
788 UTEST(test_string),
789 UTEST(test_object),
790 UTEST(test_array),
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200791 };
792
Radek Iša56ca9e42020-09-08 18:42:00 +0200793 return cmocka_run_group_tests(tests, NULL, NULL);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200794}