Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 1 | /* |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_json.c |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for a generic JSON parser |
| 5 | * |
| 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 17 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 18 | #include "in_internal.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 19 | #include "json.h" |
| 20 | #include "utests.h" |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 21 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 22 | static void |
| 23 | test_general(void **state) |
| 24 | { |
| 25 | struct lyjson_ctx *jsonctx; |
| 26 | struct ly_in *in; |
| 27 | const char *str; |
| 28 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 29 | /* empty */ |
| 30 | str = ""; |
| 31 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 32 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 33 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 38 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 39 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 45 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 46 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 53 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 54 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 61 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 62 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void |
| 71 | test_number(void **state) |
| 72 | { |
| 73 | struct lyjson_ctx *jsonctx; |
| 74 | struct ly_in *in; |
| 75 | const char *str; |
| 76 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 77 | /* simple value */ |
| 78 | str = "11"; |
| 79 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 80 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 81 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 90 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 91 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 100 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 101 | 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 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 107 | /* integer, positive exponent */ |
| 108 | str = "550E3"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 109 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 112 | assert_string_equal("550000", jsonctx->value); |
| 113 | assert_int_equal(6, jsonctx->value_len); |
| 114 | assert_int_equal(1, jsonctx->dynamic); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 115 | lyjson_ctx_free(jsonctx); |
| 116 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 117 | str = "-550E3"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 118 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 119 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 120 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 121 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 133 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 139 | 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 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 145 | str = "-15E-1"; |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 146 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 149 | assert_string_equal("-1.5", jsonctx->value); |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 150 | assert_int_equal(4, jsonctx->value_len); |
| 151 | assert_int_equal(1, jsonctx->dynamic); |
| 152 | lyjson_ctx_free(jsonctx); |
| 153 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 154 | str = "16E-2"; |
aPiecek | 350a6bf | 2021-05-25 07:59:10 +0200 | [diff] [blame] | 155 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 158 | assert_string_equal("0.16", jsonctx->value); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 159 | assert_int_equal(4, jsonctx->value_len); |
| 160 | assert_int_equal(1, jsonctx->dynamic); |
| 161 | lyjson_ctx_free(jsonctx); |
| 162 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 163 | str = "-16E-2"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 164 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 167 | assert_string_equal("-0.16", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 168 | assert_int_equal(5, jsonctx->value_len); |
| 169 | assert_int_equal(1, jsonctx->dynamic); |
| 170 | lyjson_ctx_free(jsonctx); |
| 171 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 172 | str = "17E-3"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 173 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 176 | 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); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 195 | assert_int_equal(3, jsonctx->value_len); |
| 196 | assert_int_equal(1, jsonctx->dynamic); |
| 197 | lyjson_ctx_free(jsonctx); |
| 198 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 199 | str = "21000E-4"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 200 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 202 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 203 | assert_string_equal("2.1", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 204 | assert_int_equal(3, jsonctx->value_len); |
| 205 | assert_int_equal(1, jsonctx->dynamic); |
| 206 | lyjson_ctx_free(jsonctx); |
| 207 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 208 | str = "21000E-7"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 209 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 210 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 211 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 212 | assert_string_equal("0.0021", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 213 | assert_int_equal(6, jsonctx->value_len); |
| 214 | assert_int_equal(1, jsonctx->dynamic); |
| 215 | lyjson_ctx_free(jsonctx); |
| 216 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 217 | /* decimal number, positive exponent */ |
| 218 | str = "5.087E1"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 219 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 222 | assert_string_equal("50.87", jsonctx->value); |
| 223 | assert_int_equal(5, jsonctx->value_len); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 224 | assert_int_equal(1, jsonctx->dynamic); |
| 225 | lyjson_ctx_free(jsonctx); |
| 226 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 227 | str = "-5.087E1"; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 228 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 229 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 230 | assert_int_equal(LYJSON_NUMBER, lyjson_ctx_status(jsonctx, 0)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 231 | assert_string_equal("-50.87", jsonctx->value); |
| 232 | assert_int_equal(6, jsonctx->value_len); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 233 | assert_int_equal(1, jsonctx->dynamic); |
| 234 | lyjson_ctx_free(jsonctx); |
| 235 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 236 | str = "5.087E5"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 237 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 240 | 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); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 259 | assert_int_equal(7, jsonctx->value_len); |
| 260 | assert_int_equal(1, jsonctx->dynamic); |
| 261 | lyjson_ctx_free(jsonctx); |
| 262 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 263 | str = "0.005087E2"; |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 264 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 267 | assert_string_equal("0.5087", jsonctx->value); |
aPiecek | 58e46a5 | 2021-05-05 10:04:47 +0200 | [diff] [blame] | 268 | assert_int_equal(6, jsonctx->value_len); |
| 269 | assert_int_equal(1, jsonctx->dynamic); |
| 270 | lyjson_ctx_free(jsonctx); |
| 271 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 272 | str = "0.005087E6"; |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 273 | 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)); |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 276 | assert_string_equal("5087", jsonctx->value); |
aPiecek | a40b466 | 2021-05-13 15:47:03 +0200 | [diff] [blame] | 277 | assert_int_equal(4, jsonctx->value_len); |
| 278 | assert_int_equal(1, jsonctx->dynamic); |
| 279 | lyjson_ctx_free(jsonctx); |
| 280 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 281 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 457 | /* various invalid inputs */ |
| 458 | str = "-x"; |
| 459 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 460 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 462 | |
| 463 | str = " -"; |
| 464 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 465 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 466 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 467 | |
| 468 | str = "--1"; |
| 469 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 470 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 472 | |
| 473 | str = "+1"; |
| 474 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 475 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 477 | |
| 478 | str = " 1.x "; |
| 479 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 480 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 482 | |
| 483 | str = "1."; |
| 484 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 485 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 486 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 487 | |
| 488 | str = " 1eo "; |
| 489 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 490 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 492 | |
| 493 | str = "1e"; |
| 494 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 495 | assert_int_equal(LY_EVALID, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
| 496 | CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 497 | |
aPiecek | 76034c3 | 2021-06-08 15:03:11 +0200 | [diff] [blame] | 498 | 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 | |
aPiecek | 28e101a | 2021-06-10 09:09:31 +0200 | [diff] [blame^] | 508 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 513 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 516 | /* now string is tested in file ./tests/utests/types/string.c */ |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 517 | static void |
| 518 | test_string(void **state) |
| 519 | { |
| 520 | struct lyjson_ctx *jsonctx; |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 521 | struct ly_in *in = NULL; |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 522 | const char *str; |
| 523 | |
Radek Iša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 524 | str = ""; |
| 525 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
| 526 | |
| 527 | #if 0 |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 528 | /* simple string */ |
| 529 | str = "\"hello\""; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 530 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 531 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 540 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 541 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 552 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 553 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 562 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 564 | |
| 565 | str = "\"\\f\""; |
| 566 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 567 | 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ša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 569 | #endif |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 570 | |
| 571 | /* unterminated string */ |
| 572 | str = "\"unterminated string"; |
| 573 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 574 | 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ša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 576 | #if 0 |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 577 | /* invalid escape sequence */ |
| 578 | str = "\"char \\x \""; |
| 579 | assert_non_null(ly_in_memory(in, str)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 580 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 582 | |
| 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 586 | 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ša | 447abb8 | 2021-03-04 14:08:56 +0100 | [diff] [blame] | 588 | #endif |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 589 | |
| 590 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static void |
| 594 | test_object(void **state) |
| 595 | { |
| 596 | struct lyjson_ctx *jsonctx; |
| 597 | struct ly_in *in; |
| 598 | const char *str; |
| 599 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 600 | /* empty */ |
| 601 | str = " { } "; |
| 602 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 603 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 604 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 612 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 613 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 633 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 634 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 660 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 661 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 690 | 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 Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 692 | |
| 693 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static void |
| 697 | test_array(void **state) |
| 698 | { |
| 699 | struct lyjson_ctx *jsonctx; |
| 700 | struct ly_in *in; |
| 701 | const char *str; |
| 702 | |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 703 | /* empty */ |
| 704 | str = " [ ] "; |
| 705 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str, &in)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 706 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 707 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 715 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 716 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 733 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 734 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 766 | assert_int_equal(LY_SUCCESS, lyjson_ctx_new(UTEST_LYCTX, in, &jsonctx)); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 767 | assert_int_equal(LY_EVALID, lyjson_ctx_next(jsonctx, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 768 | CHECK_LOG_CTX("Invalid character sequence \", null]\", expected a JSON value.", "Line number 1."); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 769 | lyjson_ctx_free(jsonctx); |
| 770 | |
| 771 | ly_in_free(in, 0); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 774 | int |
| 775 | main(void) |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 776 | { |
| 777 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 778 | UTEST(test_general), |
| 779 | UTEST(test_number), |
| 780 | UTEST(test_string), |
| 781 | UTEST(test_object), |
| 782 | UTEST(test_array), |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 783 | }; |
| 784 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 785 | return cmocka_run_group_tests(tests, NULL, NULL); |
Radek Krejci | 50f0c6b | 2020-06-18 16:31:48 +0200 | [diff] [blame] | 786 | } |