blob: 24c6eba84fe374fb82856867a0a531d6d1f2d426 [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 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 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010020#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010021#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema.c"
24#include "../../src/context.c"
25#include "../../src/hash_table.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020026
Radek Krejcidd4e8d42018-10-16 14:55:43 +020027#include <stdarg.h>
28#include <stddef.h>
29#include <setjmp.h>
30#include <cmocka.h>
31
32#include <stdio.h>
33#include <string.h>
34
35#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020036
37#define BUFSIZE 1024
38char logbuf[BUFSIZE] = {0};
39
40/* set to 0 to printing error messages to stderr instead of checking them in code */
41#define ENABLE_LOGGER_CHECKING 1
42
43#if ENABLE_LOGGER_CHECKING
44static void
45logger(LY_LOG_LEVEL level, const char *msg, const char *path)
46{
47 (void) level; /* unused */
48
Radek Krejci87616bb2018-10-31 13:30:52 +010049 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020050 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
51 } else {
52 strncpy(logbuf, msg, BUFSIZE - 1);
53 }
54}
55#endif
56
57static int
58logger_setup(void **state)
59{
60 (void) state; /* unused */
61#if ENABLE_LOGGER_CHECKING
62 ly_set_log_clb(logger, 1);
63#endif
64 return 0;
65}
66
Radek Krejci4f28eda2018-11-12 11:46:16 +010067static int
68logger_teardown(void **state)
69{
70 (void) state; /* unused */
71#if ENABLE_LOGGER_CHECKING
72 if (*state) {
73 fprintf(stderr, "%s\n", logbuf);
74 }
75#endif
76 return 0;
77}
78
Radek Krejcidd4e8d42018-10-16 14:55:43 +020079void
80logbuf_clean(void)
81{
82 logbuf[0] = '\0';
83}
84
85#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010086# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020087#else
88# define logbuf_assert(str)
89#endif
90
Radek Krejcid05cbd92018-12-05 14:26:40 +010091static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
92 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
93 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
94{
95 *module_data = user_data;
96 *format = LYS_IN_YANG;
97 *free_module_data = NULL;
98 return LY_SUCCESS;
99}
100
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200101static void
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100102reset_mod(struct ly_ctx *ctx, struct lys_module *module)
103{
104 lysc_module_free(module->compiled, NULL);
105 lysp_module_free(module->parsed);
106
107 FREE_STRING(module->ctx, module->name);
108 FREE_STRING(module->ctx, module->ns);
109 FREE_STRING(module->ctx, module->prefix);
110 FREE_STRING(module->ctx, module->filepath);
111 FREE_STRING(module->ctx, module->org);
112 FREE_STRING(module->ctx, module->contact);
113 FREE_STRING(module->ctx, module->dsc);
114 FREE_STRING(module->ctx, module->ref);
Radek Krejci95710c92019-02-11 15:49:55 +0100115 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100116
117 memset(module, 0, sizeof *module);
118 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100119 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100120}
121
122static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200123test_module(void **state)
124{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100125 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200126
127 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +0100128 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200129 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200130 struct lysc_feature *f;
131 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200132
133 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200134 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100135 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100136 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200137
Radek Krejcif8f882a2018-10-31 14:51:15 +0100138 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
139 logbuf_assert("Invalid argument mod (lys_compile()).");
140 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
141 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100142 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100143 mod.implemented = 0;
144 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
145 assert_null(mod.compiled);
146 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100147 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200148 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100149 assert_string_equal("test", mod.name);
150 assert_string_equal("urn:test", mod.ns);
151 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200152 /* features */
153 assert_non_null(mod.compiled->features);
154 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200155 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200156 assert_non_null(f->iffeatures);
157 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200158 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200159 assert_non_null(iff->expr);
160 assert_non_null(iff->features);
161 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200162 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200163
Radek Krejci86d106e2018-10-18 09:53:19 +0200164 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200165
Radek Krejcif8f882a2018-10-31 14:51:15 +0100166 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200167 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200168
Radek Krejci86d106e2018-10-18 09:53:19 +0200169 lysc_module_free(mod.compiled, NULL);
170 mod.compiled = NULL;
171
Radek Krejci151a5b72018-10-19 14:21:44 +0200172 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200173 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100174 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
175 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
176 assert_null(mod.parsed);
177 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100178
179 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100180 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
181 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100182 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
183 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
184 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100185 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100186
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100187 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100188 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200189}
190
Radek Krejci151a5b72018-10-19 14:21:44 +0200191static void
192test_feature(void **state)
193{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100194 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200195
Radek Krejci313d9902018-11-08 09:42:58 +0100196 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100197 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200198 const char *str;
199 struct lysc_feature *f, *f1;
200
201 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
202 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100203 "feature orfeature {if-feature \"f1 or f2\";}\n"
204 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200205 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200206 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100207 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
208 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200209
Radek Krejci313d9902018-11-08 09:42:58 +0100210 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100211 reset_mod(ctx.ctx, &mod);
212
213 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100214 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200215 assert_non_null(mod.compiled);
216 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100217 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200218 /* all features are disabled by default */
219 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
220 assert_int_equal(0, lysc_feature_value(f));
221 }
222 /* enable f1 */
223 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200224 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200225 assert_int_equal(1, lysc_feature_value(f1));
226
Radek Krejci87616bb2018-10-31 13:30:52 +0100227 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200228 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200229 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100230 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200231 assert_int_equal(1, lysc_feature_value(f));
232
Radek Krejci87616bb2018-10-31 13:30:52 +0100233 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200234 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200235 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100236 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
237 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200238 assert_int_equal(0, lysc_feature_value(f));
239
240 /* first enable f2, so f5 can be enabled then */
241 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100242 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200243 assert_int_equal(1, lysc_feature_value(f));
244
245 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200246 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200247 assert_int_equal(0, lysc_feature_value(f));
248 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
249 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
250 assert_int_equal(0, lysc_feature_value(f));
251
Radek Krejci87616bb2018-10-31 13:30:52 +0100252 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200253 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200254 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
255 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200256 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100257 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200258 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200259 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200260 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200261 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200262
263 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200264 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200265 /* long evaluation of f8 to need to reallocate internal stack for operators */
266 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200267
Radek Krejci87616bb2018-10-31 13:30:52 +0100268 /* double negation of disabled f1 -> disabled */
269 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
270
Radek Krejci38920282018-11-01 09:24:16 +0100271 /* disable all features */
272 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
273 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
274 assert_int_equal(0, lys_feature_value(&mod, f->name));
275 }
276 /* re-setting already set feature */
277 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
278 assert_int_equal(0, lys_feature_value(&mod, "f1"));
279
280 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100281 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
282 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
283 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100284 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
285 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100286 /* test if not changed */
287 assert_int_equal(1, lys_feature_value(&mod, "f1"));
288 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100289
Radek Krejci0af46292019-01-11 16:02:31 +0100290 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;"
291 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
292 assert_non_null(modp->compiled);
293 assert_non_null(modp->compiled->features);
294 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
295 assert_non_null(modp->compiled->features[0].iffeatures);
296 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
297 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
298 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
299 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
300 assert_non_null(modp->compiled->features);
301 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
302 assert_non_null(modp->compiled->features[1].depfeatures);
303 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
304 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
305
Radek Krejci1aefdf72018-11-01 11:01:39 +0100306 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100307 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
308 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
309
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100310 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100311
312 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100313 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature f1;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100314 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100315 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100316 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100317
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100318 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100319 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100320 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100321 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100322
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100323 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100324 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100325 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100326 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100327
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100328 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100329 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100330 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100331 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100332
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100333 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100334 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100335 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100336 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100337
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100338 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100339 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100340 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100341 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100342
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100343 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100344 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100345 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100346 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100347
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100348 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100349 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
350 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100352
Radek Krejci0af46292019-01-11 16:02:31 +0100353 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
354 assert_null(lys_parse_mem(ctx.ctx, "module z{namespace urn:z; prefix z; include sz;feature f1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100355 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
356
Radek Krejci1aefdf72018-11-01 11:01:39 +0100357 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100358 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100359 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100360 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module c{namespace urn:c; prefix c; import a {prefix a;} feature f1; feature f2{if-feature 'a:f1';}}", LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100361 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
362 assert_int_equal(0, lys_feature_value(modp, "f1"));
363 assert_int_equal(1, lys_feature_value(modp, "f2"));
364
Radek Krejcid05cbd92018-12-05 14:26:40 +0100365 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100366 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200367}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200368
Radek Krejci478020e2018-10-30 16:02:14 +0100369static void
370test_identity(void **state)
371{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100372 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100373
374 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100375 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100376
377 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100378 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
379 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
380 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
381 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100382
383 assert_non_null(mod1->compiled);
384 assert_non_null(mod1->compiled->identities);
385 assert_non_null(mod2->compiled);
386 assert_non_null(mod2->compiled->identities);
387
388 assert_non_null(mod1->compiled->identities[0].derived);
389 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
390 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
391 assert_non_null(mod2->compiled->identities[0].derived);
392 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
393 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
394 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
395 assert_non_null(mod2->compiled->identities[1].derived);
396 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
397 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
398 assert_non_null(mod2->compiled->identities[2].derived);
399 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
400 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
401
Radek Krejci4d483a82019-01-17 13:12:50 +0100402 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
403 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
404 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
405 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
406
407 assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100408 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
409
Radek Krejci4d483a82019-01-17 13:12:50 +0100410 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
411 assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100412 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
413
Radek Krejci7f2a5362018-11-28 13:05:37 +0100414 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100415 ly_ctx_destroy(ctx, NULL);
416}
417
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100418static void
419test_node_container(void **state)
420{
421 (void) state; /* unused */
422
423 struct ly_ctx *ctx;
424 struct lys_module *mod;
425 struct lysc_node_container *cont;
426
427 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
428 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100429 assert_non_null(mod->compiled);
430 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
431 assert_int_equal(LYS_CONTAINER, cont->nodetype);
432 assert_string_equal("c", cont->name);
433 assert_true(cont->flags & LYS_CONFIG_W);
434 assert_true(cont->flags & LYS_STATUS_CURR);
435
Radek Krejci98094b32018-11-02 16:21:47 +0100436 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;container c {config false; status deprecated; container child;}}", LYS_IN_YANG));
Radek Krejci98094b32018-11-02 16:21:47 +0100437 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100438 assert_non_null(mod->compiled);
439 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
440 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100441 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100442 assert_non_null((cont = (struct lysc_node_container*)cont->child));
443 assert_int_equal(LYS_CONTAINER, cont->nodetype);
444 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100445 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100446 assert_string_equal("child", cont->name);
447
448 ly_ctx_destroy(ctx, NULL);
449}
450
Radek Krejci0e5d8382018-11-28 16:37:53 +0100451static void
452test_node_leaflist(void **state)
453{
454 *state = test_node_leaflist;
455
456 struct ly_ctx *ctx;
457 struct lys_module *mod;
458 struct lysc_type *type;
459 struct lysc_node_leaflist *ll;
460
461 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
462
463 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
464 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
465 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
466 "leaf-list ll2 {type leafref {path ../target;}}"
467 "leaf target {type int8;}}",
468 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100469 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
470 assert_non_null(type);
471 assert_int_equal(1, type->refcount);
472 assert_int_equal(LY_TYPE_UNION, type->basetype);
473 assert_non_null(((struct lysc_type_union*)type)->types);
474 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
475 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
476 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
477 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
478 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
479 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
480 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
481 assert_non_null(type);
482 assert_int_equal(1, type->refcount);
483 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
484 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
485 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
486
Radek Krejci6bb080b2018-11-28 17:23:41 +0100487 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf-list ll {type string;}}", LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100488 assert_non_null(mod->compiled);
489 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
490 assert_int_equal(0, ll->min);
491 assert_int_equal((uint32_t)-1, ll->max);
492
Radek Krejci6bb080b2018-11-28 17:23:41 +0100493 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;typedef mytype {type int8;default 10;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100494 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100495 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100496 assert_non_null(mod->compiled);
497 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
498 assert_non_null(ll->dflts);
499 assert_int_equal(3, ll->type->refcount);
500 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
501 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100502 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100503 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100504 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
505 assert_non_null(ll->dflts);
506 assert_int_equal(3, ll->type->refcount);
507 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
508 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100509 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
510
511 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
512 * TODO test also, RPC output parameters and notification content */
513 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
514 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100515 /* but warning is present: */
516 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
517 assert_non_null(mod->compiled);
518 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
519 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100520
521 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100522 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100523 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
524
Radek Krejci096235c2019-01-11 11:12:19 +0100525 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100526 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
527
528 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
529 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100530 assert_non_null(mod->compiled);
531 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
532 assert_non_null(ll->dflts);
533 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100534 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
535 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100536 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
537
Radek Krejci0e5d8382018-11-28 16:37:53 +0100538 *state = NULL;
539 ly_ctx_destroy(ctx, NULL);
540}
541
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100542static void
543test_node_list(void **state)
544{
545 *state = test_node_list;
546
547 struct ly_ctx *ctx;
548 struct lys_module *mod;
549 struct lysc_node_list *list;
550
551 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
552
553 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
554 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
555 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100556 list = (struct lysc_node_list*)mod->compiled->data;
557 assert_non_null(list);
558 assert_non_null(list->keys);
559 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
560 assert_string_equal("x", list->keys[0]->name);
561 assert_string_equal("y", list->keys[1]->name);
562 assert_non_null(list->child);
563 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
564 assert_true(list->child->flags & LYS_KEY);
565 assert_true(list->child->next->flags & LYS_KEY);
566 list = (struct lysc_node_list*)mod->compiled->data->next;
567 assert_non_null(list);
568 assert_null(list->keys);
569 assert_non_null(list->child);
570 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
571 assert_false(list->child->flags & LYS_KEY);
572
573 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
574 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100575 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100576 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100577 list = (struct lysc_node_list*)mod->compiled->data;
578 assert_non_null(list);
579 assert_string_equal("l", list->name);
580 assert_non_null(list->keys);
581 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
582 assert_string_equal("a", list->keys[0]->name);
583 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100584 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100585 assert_non_null(list->uniques);
586 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
587 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
588 assert_string_equal("a", list->uniques[0][0]->name);
589 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
590 assert_string_equal("b", list->uniques[0][1]->name);
591 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
592 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
593 assert_string_equal("e", list->uniques[1][0]->name);
594 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
595 assert_string_equal("d", list->uniques[1][1]->name);
596 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
597
Radek Krejci665421c2018-12-05 08:03:39 +0100598 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
599 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100600 list = (struct lysc_node_list*)mod->compiled->data;
601 assert_non_null(list);
602 assert_string_equal("l", list->name);
603 assert_non_null(list->keys);
604 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
605 assert_string_equal("a", list->keys[0]->name);
606 assert_true(list->keys[0]->flags & LYS_KEY);
607 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
608
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100609 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100610 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100611 logbuf_assert("Missing key in list representing configuration data.");
612
Radek Krejci096235c2019-01-11 11:12:19 +0100613 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
614 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100615 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
616
Radek Krejci096235c2019-01-11 11:12:19 +0100617 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
618 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100619 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
620
Radek Krejci096235c2019-01-11 11:12:19 +0100621 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
622 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100623 logbuf_assert("Key of the configuration list must not be status leaf.");
624
Radek Krejci096235c2019-01-11 11:12:19 +0100625 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
626 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100627 logbuf_assert("Configuration node cannot be child of any state data node.");
628
Radek Krejci096235c2019-01-11 11:12:19 +0100629 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
630 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100631 logbuf_assert("The list's key \"x\" not found.");
632
Radek Krejci096235c2019-01-11 11:12:19 +0100633 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
634 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100635 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
636
Radek Krejci096235c2019-01-11 11:12:19 +0100637 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
638 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100639 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
640
Radek Krejci096235c2019-01-11 11:12:19 +0100641 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
642 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100643 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
644
Radek Krejci096235c2019-01-11 11:12:19 +0100645 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
646 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100647 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
648
Radek Krejci096235c2019-01-11 11:12:19 +0100649 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
650 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100651 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
652
Radek Krejci096235c2019-01-11 11:12:19 +0100653 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
654 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100655 logbuf_assert("Duplicated key identifier \"x\".");
656
Radek Krejci096235c2019-01-11 11:12:19 +0100657 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
658 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100659 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
660
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100661 *state = NULL;
662 ly_ctx_destroy(ctx, NULL);
663}
664
Radek Krejci056d0a82018-12-06 16:57:25 +0100665static void
666test_node_choice(void **state)
667{
668 *state = test_node_choice;
669
670 struct ly_ctx *ctx;
671 struct lys_module *mod;
672 struct lysc_node_choice *ch;
673 struct lysc_node_case *cs;
674
675 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
676
677 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100678 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
679 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100680 ch = (struct lysc_node_choice*)mod->compiled->data;
681 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100682 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100683 cs = ch->cases;
684 assert_non_null(cs);
685 assert_string_equal("a", cs->name);
686 assert_ptr_equal(ch, cs->parent);
687 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100688 assert_string_equal("a1", cs->child->name);
689 assert_non_null(cs->child->next);
690 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100691 assert_ptr_equal(cs, cs->child->parent);
692 cs = (struct lysc_node_case*)cs->next;
693 assert_non_null(cs);
694 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100695 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100696 assert_ptr_equal(ch, cs->parent);
697 assert_non_null(cs->child);
698 assert_string_equal("b", cs->child->name);
699 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100700 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
701 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100702 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100703 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100704
Radek Krejci096235c2019-01-11 11:12:19 +0100705 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
706 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100707 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100708 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
709 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100710 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100711 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
712 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100713 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100714 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
715 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100716 logbuf_assert("Duplicate identifier \"b\" of case statement.");
717
Radek Krejci096235c2019-01-11 11:12:19 +0100718 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
719 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100720 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100721 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
722 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100723 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100724 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
725 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100726 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
727 /* TODO check with mandatory nodes from augment placed into the case */
728
Radek Krejci056d0a82018-12-06 16:57:25 +0100729 *state = NULL;
730 ly_ctx_destroy(ctx, NULL);
731}
732
Radek Krejci9800fb82018-12-13 14:26:23 +0100733static void
734test_node_anydata(void **state)
735{
736 *state = test_node_anydata;
737
738 struct ly_ctx *ctx;
739 struct lys_module *mod;
740 struct lysc_node_anydata *any;
741
742 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
743
744 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
745 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100746 any = (struct lysc_node_anydata*)mod->compiled->data;
747 assert_non_null(any);
748 assert_int_equal(LYS_ANYDATA, any->nodetype);
749 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
750
751 logbuf_clean();
752 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
753 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100754 any = (struct lysc_node_anydata*)mod->compiled->data;
755 assert_non_null(any);
756 assert_int_equal(LYS_ANYXML, any->nodetype);
757 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
758 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
759
760 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100761 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;anydata any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100762 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
763
764 *state = NULL;
765 ly_ctx_destroy(ctx, NULL);
766}
767
Radek Krejci0f07bed2018-11-13 14:01:40 +0100768/**
769 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
770 * do not test the expression itself
771 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100772static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100773test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100774{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100775 *state = test_type_range;
776
777 struct ly_ctx *ctx;
778 struct lys_module *mod;
779 struct lysc_type *type;
780
781 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
782
783 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type int8 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100784 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
785 assert_non_null(type);
786 assert_int_equal(LY_TYPE_INT8, type->basetype);
787 assert_non_null(((struct lysc_type_num*)type)->range);
788 assert_non_null(((struct lysc_type_num*)type)->range->parts);
789 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
790 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
791 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
792 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
793 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
794
795 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type int16 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100796 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
797 assert_non_null(type);
798 assert_int_equal(LY_TYPE_INT16, type->basetype);
799 assert_non_null(((struct lysc_type_num*)type)->range);
800 assert_non_null(((struct lysc_type_num*)type)->range->parts);
801 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
802 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
803 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
804 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
805 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
806
807 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type int32 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100808 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
809 assert_non_null(type);
810 assert_int_equal(LY_TYPE_INT32, type->basetype);
811 assert_non_null(((struct lysc_type_num*)type)->range);
812 assert_non_null(((struct lysc_type_num*)type)->range->parts);
813 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
814 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
815 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
816 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
817 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
818
819 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type int64 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100820 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
821 assert_non_null(type);
822 assert_int_equal(LY_TYPE_INT64, type->basetype);
823 assert_non_null(((struct lysc_type_num*)type)->range);
824 assert_non_null(((struct lysc_type_num*)type)->range->parts);
825 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
826 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
827 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
828 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
829 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
830
831 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;leaf l {type uint8 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100832 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
833 assert_non_null(type);
834 assert_int_equal(LY_TYPE_UINT8, type->basetype);
835 assert_non_null(((struct lysc_type_num*)type)->range);
836 assert_non_null(((struct lysc_type_num*)type)->range->parts);
837 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
838 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
839 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
840 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
841 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
842
843 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;leaf l {type uint16 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100844 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
845 assert_non_null(type);
846 assert_int_equal(LY_TYPE_UINT16, type->basetype);
847 assert_non_null(((struct lysc_type_num*)type)->range);
848 assert_non_null(((struct lysc_type_num*)type)->range->parts);
849 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
850 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
851 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
852 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
853 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
854
855 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;leaf l {type uint32 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100856 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
857 assert_non_null(type);
858 assert_int_equal(LY_TYPE_UINT32, type->basetype);
859 assert_non_null(((struct lysc_type_num*)type)->range);
860 assert_non_null(((struct lysc_type_num*)type)->range->parts);
861 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
862 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
863 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
864 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
865 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
866
867 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;leaf l {type uint64 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100868 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
869 assert_non_null(type);
870 assert_int_equal(LY_TYPE_UINT64, type->basetype);
871 assert_non_null(((struct lysc_type_num*)type)->range);
872 assert_non_null(((struct lysc_type_num*)type)->range->parts);
873 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
874 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
875 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
876 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
877 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
878
879 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
880 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100881 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
882 assert_non_null(type);
883 assert_int_equal(3, type->refcount);
884 assert_int_equal(LY_TYPE_UINT8, type->basetype);
885 assert_non_null(((struct lysc_type_num*)type)->range);
886 assert_non_null(((struct lysc_type_num*)type)->range->parts);
887 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
888
Radek Krejcif1394042019-01-08 10:53:34 +0100889 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
890 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
891 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +0100892 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
893 assert_non_null(type);
894 assert_int_equal(1, type->refcount);
895 assert_int_equal(LY_TYPE_UINT8, type->basetype);
896 assert_non_null(((struct lysc_type_num*)type)->range);
897 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
898 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
899 assert_non_null(((struct lysc_type_num*)type)->range->parts);
900 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
901 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
902 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
903
Radek Krejci0f07bed2018-11-13 14:01:40 +0100904 *state = NULL;
905 ly_ctx_destroy(ctx, NULL);
906}
907
908static void
909test_type_length(void **state)
910{
911 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100912
913 struct ly_ctx *ctx;
914 struct lys_module *mod;
915 struct lysc_type *type;
916
917 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
918
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100919 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type binary {length min {error-app-tag errortag;error-message error;}}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100920 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
921 assert_non_null(type);
922 assert_non_null(((struct lysc_type_bin*)type)->length);
923 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100924 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
925 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100926 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
927 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
928 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
929
930 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100931 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
932 assert_non_null(type);
933 assert_non_null(((struct lysc_type_bin*)type)->length);
934 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
935 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100936 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
937 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100938
939 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type binary {length min..max;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100940 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
941 assert_non_null(type);
942 assert_non_null(((struct lysc_type_bin*)type)->length);
943 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
944 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
945 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100946 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100947
948 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100949 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
950 assert_non_null(type);
951 assert_non_null(((struct lysc_type_bin*)type)->length);
952 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
953 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
954 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
955 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
956
957 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;leaf l {type binary {length 1..10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100958 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
959 assert_non_null(type);
960 assert_non_null(((struct lysc_type_bin*)type)->length);
961 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
962 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
963 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
964 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
965
966 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;leaf l {type binary {length 1..10|20..30;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100967 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
968 assert_non_null(type);
969 assert_non_null(((struct lysc_type_bin*)type)->length);
970 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
971 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
972 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
973 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
974 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
975 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
976
977 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;leaf l {type binary {length \"16 | 32\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100978 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
979 assert_non_null(type);
980 assert_non_null(((struct lysc_type_bin*)type)->length);
981 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
982 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
983 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
984 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
985 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
986 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
987
Radek Krejcib31c8992018-11-12 16:29:16 +0100988 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
989 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100990 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
991 assert_non_null(type);
992 assert_non_null(((struct lysc_type_bin*)type)->length);
993 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
994 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
995 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
996 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
997
998 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
999 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001000 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1001 assert_non_null(type);
1002 assert_non_null(((struct lysc_type_bin*)type)->length);
1003 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1004 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1005 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1006 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1007
1008 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1009 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001010 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1011 assert_non_null(type);
1012 assert_non_null(((struct lysc_type_bin*)type)->length);
1013 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1014 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1015 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1016 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1017 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1018 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1019
1020 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;typedef mytype {type binary {length 10..100;}}"
Radek Krejci56aa27c2018-11-13 14:21:59 +01001021 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001022 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1023 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001024 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001025 assert_non_null(((struct lysc_type_bin*)type)->length);
1026 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1027 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1028 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001029 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001030 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1031 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001032 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001033 assert_non_null(((struct lysc_type_bin*)type)->length);
1034 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1035 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1036 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1037 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1038
Radek Krejci56aa27c2018-11-13 14:21:59 +01001039 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1040 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001041 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1042 assert_non_null(type);
1043 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001044 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001045 assert_non_null(((struct lysc_type_str*)type)->length);
1046 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1047 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1048 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1049 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001050
Radek Krejci5969f272018-11-23 10:03:58 +01001051 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1052 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001053 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1054 assert_non_null(type);
1055 assert_int_equal(LY_TYPE_STRING, type->basetype);
1056 assert_int_equal(1, type->refcount);
1057 assert_non_null(((struct lysc_type_str*)type)->length);
1058 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1059 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1060 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1061 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1062
Radek Krejci4f28eda2018-11-12 11:46:16 +01001063 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001064 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001065 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001066 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001067 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001068 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001069 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001070 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001071 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001072 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001073 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001074 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001075 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001076 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001077 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001078 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001079 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001080 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001081 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001082 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001083 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001084 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001085 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001086 assert_null(lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001087 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001088
Radek Krejci096235c2019-01-11 11:12:19 +01001089 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1090 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001091 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001092 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1093 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001094 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001095 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1096 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001097 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001098 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1099 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001100 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001101 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
Radek Krejcib31c8992018-11-12 16:29:16 +01001102 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001103 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001104 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
Radek Krejcib31c8992018-11-12 16:29:16 +01001105 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001106 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001107 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
Radek Krejci6489bbe2018-11-12 17:05:19 +01001108 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001109 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001110
Radek Krejci096235c2019-01-11 11:12:19 +01001111 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:rr;prefix rr;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
Radek Krejci495903e2018-11-13 11:30:45 +01001112 logbuf_assert("Invalid type restrictions for binary type.");
1113
Radek Krejci4f28eda2018-11-12 11:46:16 +01001114 *state = NULL;
1115 ly_ctx_destroy(ctx, NULL);
1116}
1117
Radek Krejcicda74152018-11-13 15:27:32 +01001118static void
1119test_type_pattern(void **state)
1120{
1121 *state = test_type_pattern;
1122
1123 struct ly_ctx *ctx;
1124 struct lys_module *mod;
1125 struct lysc_type *type;
1126
1127 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1128
Radek Krejci027d5802018-11-14 16:57:28 +01001129 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;leaf l {type string {"
Radek Krejcicda74152018-11-13 15:27:32 +01001130 "pattern .* {error-app-tag errortag;error-message error;}"
1131 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001132 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1133 assert_non_null(type);
1134 assert_non_null(((struct lysc_type_str*)type)->patterns);
1135 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1136 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1137 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1138 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1139 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1140 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1141 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1142
1143 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1144 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001145 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1146 assert_non_null(type);
1147 assert_int_equal(LY_TYPE_STRING, type->basetype);
1148 assert_int_equal(1, type->refcount);
1149 assert_non_null(((struct lysc_type_str*)type)->patterns);
1150 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1151 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1152 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1153
Radek Krejci04bc1e92018-11-14 14:28:13 +01001154 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1155 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001156 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1157 assert_non_null(type);
1158 assert_int_equal(LY_TYPE_STRING, type->basetype);
1159 assert_int_equal(1, type->refcount);
1160 assert_non_null(((struct lysc_type_str*)type)->patterns);
1161 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1162 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1163
Radek Krejcicda74152018-11-13 15:27:32 +01001164 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001165 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type string {"
Radek Krejcicda74152018-11-13 15:27:32 +01001166 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001167 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1168 assert_non_null(type);
1169 assert_non_null(((struct lysc_type_str*)type)->patterns);
1170 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1171 /* TODO check some data "^Å™$" */
1172
1173 *state = NULL;
1174 ly_ctx_destroy(ctx, NULL);
1175}
1176
Radek Krejci8b764662018-11-14 14:15:13 +01001177static void
1178test_type_enum(void **state)
1179{
1180 *state = test_type_enum;
1181
1182 struct ly_ctx *ctx;
1183 struct lys_module *mod;
1184 struct lysc_type *type;
1185
1186 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1187
Radek Krejci027d5802018-11-14 16:57:28 +01001188 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;feature f; leaf l {type enumeration {"
Radek Krejci8b764662018-11-14 14:15:13 +01001189 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1190 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001191 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1192 assert_non_null(type);
1193 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1194 assert_non_null(((struct lysc_type_enum*)type)->enums);
1195 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1196 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1197 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1198 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1199 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1200 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1201 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1202 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1203 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1204 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1205 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1206 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1207 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1208 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1209
Radek Krejci027d5802018-11-14 16:57:28 +01001210 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b;feature f; typedef mytype {type enumeration {"
1211 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001212 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1213 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001214 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1215 assert_non_null(type);
1216 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1217 assert_non_null(((struct lysc_type_enum*)type)->enums);
1218 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1219 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1220 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1221 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1222 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1223
1224
1225 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001226 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1227 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1228 logbuf_assert("Invalid keyword \"if-feature\" as a child of \"enum\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
Radek Krejci8b764662018-11-14 14:15:13 +01001229 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1230 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1231 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1232 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1233 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1234 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1235 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1236 "enum one; enum one;}}}", LYS_IN_YANG));
1237 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1238 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1239 "enum '';}}}", LYS_IN_YANG));
1240 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1241 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1242 "enum ' x';}}}", LYS_IN_YANG));
1243 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1244 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1245 "enum 'x ';}}}", LYS_IN_YANG));
1246 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1247 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1248 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1249 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1250
Radek Krejci096235c2019-01-11 11:12:19 +01001251 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001252 logbuf_assert("Missing enum substatement for enumeration type.");
1253
Radek Krejci096235c2019-01-11 11:12:19 +01001254 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type enumeration {enum one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001255 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001256 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1257
Radek Krejci096235c2019-01-11 11:12:19 +01001258 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type enumeration {enum one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001259 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001260 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001261 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type enumeration {enum x {value 2147483647;}enum y;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001262 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001263 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1264
Radek Krejci096235c2019-01-11 11:12:19 +01001265 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type enumeration {enum x {value 1;}enum y {value 1;}}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001266 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001267 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1268
Radek Krejci096235c2019-01-11 11:12:19 +01001269 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001270 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001271 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001272
Radek Krejci096235c2019-01-11 11:12:19 +01001273 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
Radek Krejci027d5802018-11-14 16:57:28 +01001274 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001275 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1276
Radek Krejci8b764662018-11-14 14:15:13 +01001277 *state = NULL;
1278 ly_ctx_destroy(ctx, NULL);
1279}
1280
1281static void
1282test_type_bits(void **state)
1283{
1284 *state = test_type_bits;
1285
1286 struct ly_ctx *ctx;
1287 struct lys_module *mod;
1288 struct lysc_type *type;
1289
1290 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1291
Radek Krejci027d5802018-11-14 16:57:28 +01001292 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;feature f; leaf l {type bits {"
Radek Krejci8b764662018-11-14 14:15:13 +01001293 "bit automin; bit one {if-feature f; position 1;}"
1294 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001295 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1296 assert_non_null(type);
1297 assert_int_equal(LY_TYPE_BITS, type->basetype);
1298 assert_non_null(((struct lysc_type_bits*)type)->bits);
1299 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1300 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1301 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1302 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1303 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1304 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1305 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1306 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1307 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1308 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1309 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1310 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1311
Radek Krejci027d5802018-11-14 16:57:28 +01001312 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;feature f; typedef mytype {type bits {"
1313 "bit automin; bit one;bit two; bit seven {value 7;}bit eight;}} leaf l { type mytype {bit eight;bit seven;bit automin;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001314 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001315 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1316 assert_non_null(type);
1317 assert_int_equal(LY_TYPE_BITS, type->basetype);
1318 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001319 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1320 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1321 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1322 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1323 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1324 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1325 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001326
1327
1328 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001329 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1330 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1331 logbuf_assert("Invalid keyword \"if-feature\" as a child of \"bit\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
Radek Krejci8b764662018-11-14 14:15:13 +01001332 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1333 "bit one {position -1;}}}}", LYS_IN_YANG));
1334 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1335 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1336 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1337 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1338 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1339 "bit one; bit one;}}}", LYS_IN_YANG));
1340 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1341 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1342 "bit '11';}}}", LYS_IN_YANG));
1343 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1344 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1345 "bit 'x1$1';}}}", LYS_IN_YANG));
1346 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1347
Radek Krejci096235c2019-01-11 11:12:19 +01001348 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001349 logbuf_assert("Missing bit substatement for bits type.");
1350
Radek Krejci096235c2019-01-11 11:12:19 +01001351 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type bits {bit one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001352 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001353 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1354
Radek Krejci096235c2019-01-11 11:12:19 +01001355 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type bits {bit one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001356 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001357 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001358 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001359 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001360 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1361
Radek Krejci096235c2019-01-11 11:12:19 +01001362 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {value 1;}bit y {value 1;}}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001363 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001364 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1365
Radek Krejci096235c2019-01-11 11:12:19 +01001366 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001367 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001368 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001369
Radek Krejci096235c2019-01-11 11:12:19 +01001370 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
Radek Krejci027d5802018-11-14 16:57:28 +01001371 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001372 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1373
Radek Krejci8b764662018-11-14 14:15:13 +01001374 *state = NULL;
1375 ly_ctx_destroy(ctx, NULL);
1376}
1377
Radek Krejci6cba4292018-11-15 17:33:29 +01001378static void
1379test_type_dec64(void **state)
1380{
1381 *state = test_type_dec64;
1382
1383 struct ly_ctx *ctx;
1384 struct lys_module *mod;
1385 struct lysc_type *type;
1386
1387 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1388
1389 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1390 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001391 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1392 assert_non_null(type);
1393 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1394 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1395 assert_non_null(((struct lysc_type_dec*)type)->range);
1396 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1397 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1398 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1399 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1400
1401 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1402 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001403 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1404 assert_non_null(type);
1405 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1406 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1407 assert_non_null(((struct lysc_type_dec*)type)->range);
1408 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1409 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1410 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1411 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1412 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1413 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1414 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1415 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1416
1417 /* invalid cases */
1418 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1419 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1420 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1421 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1422 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1423 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1424
Radek Krejci096235c2019-01-11 11:12:19 +01001425 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001426 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1427
Radek Krejci096235c2019-01-11 11:12:19 +01001428 assert_null(lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001429 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001430
Radek Krejci096235c2019-01-11 11:12:19 +01001431 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
Radek Krejci6cba4292018-11-15 17:33:29 +01001432 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001433 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1434
Radek Krejci096235c2019-01-11 11:12:19 +01001435 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
Radek Krejci6cba4292018-11-15 17:33:29 +01001436 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001437 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1438
Radek Krejci096235c2019-01-11 11:12:19 +01001439 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
Radek Krejci643c8242018-11-15 17:51:11 +01001440 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001441 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1442
Radek Krejci096235c2019-01-11 11:12:19 +01001443 assert_null(lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
Radek Krejci643c8242018-11-15 17:51:11 +01001444 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001445 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1446
Radek Krejci6cba4292018-11-15 17:33:29 +01001447 *state = NULL;
1448 ly_ctx_destroy(ctx, NULL);
1449}
1450
Radek Krejci16c0f822018-11-16 10:46:10 +01001451static void
1452test_type_instanceid(void **state)
1453{
1454 *state = test_type_instanceid;
1455
1456 struct ly_ctx *ctx;
1457 struct lys_module *mod;
1458 struct lysc_type *type;
1459
1460 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1461
1462 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1463 "leaf l1 {type instance-identifier {require-instance true;}}"
1464 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001465 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1466 assert_non_null(type);
1467 assert_int_equal(LY_TYPE_INST, type->basetype);
1468 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1469
1470 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1471 assert_non_null(type);
1472 assert_int_equal(LY_TYPE_INST, type->basetype);
1473 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1474
1475 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1476 assert_non_null(type);
1477 assert_int_equal(LY_TYPE_INST, type->basetype);
1478 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1479
1480 /* invalid cases */
1481 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1482 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1483
Radek Krejci096235c2019-01-11 11:12:19 +01001484 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001485 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1486
1487 *state = NULL;
1488 ly_ctx_destroy(ctx, NULL);
1489}
1490
Radek Krejci555cb5b2018-11-16 14:54:33 +01001491static void
1492test_type_identityref(void **state)
1493{
1494 *state = test_type_identityref;
1495
1496 struct ly_ctx *ctx;
1497 struct lys_module *mod;
1498 struct lysc_type *type;
1499
1500 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1501
1502 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;identity i; identity j; identity k {base i;}"
1503 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001504 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001505 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1506 assert_non_null(type);
1507 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1508 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1509 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1510 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1511
1512 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1513 assert_non_null(type);
1514 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1515 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1516 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1517 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1518 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1519
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001520 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1521 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001522 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1523 assert_non_null(type);
1524 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1525 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1526 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1527 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1528 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1529
Radek Krejci555cb5b2018-11-16 14:54:33 +01001530 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001531 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001532 logbuf_assert("Missing base substatement for identityref type.");
1533
Radek Krejci096235c2019-01-11 11:12:19 +01001534 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001535 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001536 logbuf_assert("Missing base substatement for identityref type mytype.");
1537
Radek Krejci096235c2019-01-11 11:12:19 +01001538 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001539 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001540 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001541
Radek Krejci096235c2019-01-11 11:12:19 +01001542 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001543 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001544 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001545
Radek Krejci096235c2019-01-11 11:12:19 +01001546 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001547 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001548 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1549
Radek Krejci096235c2019-01-11 11:12:19 +01001550 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
Radek Krejci322614f2018-11-16 15:05:44 +01001551 logbuf_assert("Unable to find base (j) of identityref.");
1552
Radek Krejci096235c2019-01-11 11:12:19 +01001553 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
Radek Krejci322614f2018-11-16 15:05:44 +01001554 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1555
Radek Krejci555cb5b2018-11-16 14:54:33 +01001556 *state = NULL;
1557 ly_ctx_destroy(ctx, NULL);
1558}
1559
Radek Krejcia3045382018-11-22 14:30:31 +01001560static void
1561test_type_leafref(void **state)
1562{
1563 *state = test_type_leafref;
1564
1565 struct ly_ctx *ctx;
1566 struct lys_module *mod;
1567 struct lysc_type *type;
1568 const char *path, *name, *prefix;
1569 size_t prefix_len, name_len;
1570 int parent_times, has_predicate;
1571
1572 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1573
1574 /* lys_path_token() */
1575 path = "invalid_path";
1576 parent_times = 0;
1577 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1578 path = "..";
1579 parent_times = 0;
1580 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1581 path = "..[";
1582 parent_times = 0;
1583 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1584 path = "../";
1585 parent_times = 0;
1586 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1587 path = "/";
1588 parent_times = 0;
1589 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1590
1591 path = "../../pref:id/xxx[predicate]/invalid!!!";
1592 parent_times = 0;
1593 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1594 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1595 assert_int_equal(4, prefix_len);
1596 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1597 assert_int_equal(2, name_len);
1598 assert_int_equal(0, strncmp("id", name, name_len));
1599 assert_int_equal(2, parent_times);
1600 assert_int_equal(0, has_predicate);
1601 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1602 assert_string_equal("[predicate]/invalid!!!", path);
1603 assert_int_equal(0, prefix_len);
1604 assert_null(prefix);
1605 assert_int_equal(3, name_len);
1606 assert_int_equal(0, strncmp("xxx", name, name_len));
1607 assert_int_equal(1, has_predicate);
1608 path += 11;
1609 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1610 assert_string_equal("!!!", path);
1611 assert_int_equal(0, prefix_len);
1612 assert_null(prefix);
1613 assert_int_equal(7, name_len);
1614 assert_int_equal(0, strncmp("invalid", name, name_len));
1615
1616 path = "/absolute/prefix:path";
1617 parent_times = 0;
1618 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1619 assert_string_equal("/prefix:path", path);
1620 assert_int_equal(0, prefix_len);
1621 assert_null(prefix);
1622 assert_int_equal(8, name_len);
1623 assert_int_equal(0, strncmp("absolute", name, name_len));
1624 assert_int_equal(-1, parent_times);
1625 assert_int_equal(0, has_predicate);
1626 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1627 assert_int_equal(0, *path);
1628 assert_int_equal(6, prefix_len);
1629 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1630 assert_int_equal(4, name_len);
1631 assert_int_equal(0, strncmp("path", name, name_len));
1632 assert_int_equal(0, has_predicate);
1633
1634 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001635 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
Radek Krejcia3045382018-11-22 14:30:31 +01001636 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1637 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001638 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1639 assert_non_null(type);
1640 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1641 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001642 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001643 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1644 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001645 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1646 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1647 assert_non_null(type);
1648 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1649 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001650 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1651 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1652 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001653 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1654
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001655 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mytype {type leafref {path /b:target;}}"
Radek Krejci4ce68932018-11-28 12:53:36 +01001656 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001657 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001658 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1659 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001660 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001661 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1662 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1663 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001664 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1665 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001666 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1667
1668 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001669 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix b; import b {prefix c;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001670 "typedef mytype3 {type c:mytype {require-instance false;}}"
1671 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001672 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1673 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001674 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001675 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1676 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1677 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001678 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1679 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001680 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1681 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1682 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001683 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001684 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1685 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1686 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1687 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1688
Radek Krejci4ce68932018-11-28 12:53:36 +01001689 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1690 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1691 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001692 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1693 assert_non_null(type);
1694 assert_int_equal(1, type->refcount);
1695 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1696 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1697 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1698 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1699 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1700 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1701
Radek Krejci58d171e2018-11-23 13:50:55 +01001702 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001703 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;feature f1; feature f2;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001704 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1705 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001706 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1707 assert_non_null(type);
1708 assert_int_equal(1, type->refcount);
1709 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1710 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1711 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1712 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1713 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1714
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001715 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1716 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1717 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001718 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001719 LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +01001720 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001721 assert_non_null(type);
1722 assert_int_equal(1, type->refcount);
1723 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001724 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001725 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1726 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1727 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001728
Radek Krejci20e8a182018-12-07 11:43:21 +01001729 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1730 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1731 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1732 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001733 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1734 assert_non_null(type);
1735 assert_int_equal(1, type->refcount);
1736 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1737 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1738 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1739 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1740 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1741
Radek Krejcia3045382018-11-22 14:30:31 +01001742 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001743 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001744 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001745 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001746 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001747 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001748 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001749 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001750 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001751 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001752 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf target1 {type string;}container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001753 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001754 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001755 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001756 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001757 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001758 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001759 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001760 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001761 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001762 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001763 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001764 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001765 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001766 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001767 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001768 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001769 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001770 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001771 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001772 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1773 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001774 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001775 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001776 logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001777
Radek Krejci096235c2019-01-11 11:12:19 +01001778 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001779 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001780 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001781 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001782 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001783 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1784
Radek Krejci096235c2019-01-11 11:12:19 +01001785 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001786 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1787 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1788 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1789 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001790 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1791
Radek Krejci096235c2019-01-11 11:12:19 +01001792 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001793 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1794 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1795 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1796 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001797 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1798
Radek Krejci096235c2019-01-11 11:12:19 +01001799 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001800 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1801 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1802 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1803 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001804 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1805
Radek Krejci096235c2019-01-11 11:12:19 +01001806 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001807 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1808 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1809 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1810 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001811 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1812
Radek Krejci096235c2019-01-11 11:12:19 +01001813 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001814 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1815 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1816 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1817 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001818 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1819
Radek Krejci096235c2019-01-11 11:12:19 +01001820 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001821 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1822 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1823 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1824 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001825 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1826
Radek Krejci096235c2019-01-11 11:12:19 +01001827 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001828 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1829 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1830 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1831 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001832 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1833
Radek Krejci096235c2019-01-11 11:12:19 +01001834 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001835 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1836 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1837 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
1838 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001839 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1840
Radek Krejci096235c2019-01-11 11:12:19 +01001841 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001842 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1843 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1844 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
1845 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001846 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1847
Radek Krejci096235c2019-01-11 11:12:19 +01001848 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001849 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1850 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1851 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
1852 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001853 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1854
Radek Krejci096235c2019-01-11 11:12:19 +01001855 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001856 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1857 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1858 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
1859 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001860 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1861
Radek Krejci096235c2019-01-11 11:12:19 +01001862 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001863 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1864 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1865 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1866 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001867 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1868
Radek Krejci096235c2019-01-11 11:12:19 +01001869 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001870 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1871 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1872 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
1873 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001874 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1875
Radek Krejci096235c2019-01-11 11:12:19 +01001876 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001877 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1878 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
1879 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1880 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001881 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1882
Radek Krejci096235c2019-01-11 11:12:19 +01001883 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001884 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1885 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1886 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1887 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001888 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1889
Radek Krejci412ddfa2018-11-23 11:44:11 +01001890 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01001891 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01001892 "leaf ref1 {type leafref {path /ref2;}}"
1893 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001894 "leaf ref3 {type leafref {path /ref4;}}"
1895 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001896 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1897
Radek Krejcia3045382018-11-22 14:30:31 +01001898 *state = NULL;
1899 ly_ctx_destroy(ctx, NULL);
1900}
1901
Radek Krejci43699232018-11-23 14:59:46 +01001902static void
1903test_type_empty(void **state)
1904{
1905 *state = test_type_empty;
1906
1907 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01001908
1909 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1910
1911 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01001912 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01001913 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001914 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1915
Radek Krejci096235c2019-01-11 11:12:19 +01001916 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
Radek Krejci43699232018-11-23 14:59:46 +01001917 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001918 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1919
1920 *state = NULL;
1921 ly_ctx_destroy(ctx, NULL);
1922}
1923
Radek Krejcicdfecd92018-11-26 11:27:32 +01001924
1925static void
1926test_type_union(void **state)
1927{
1928 *state = test_type_union;
1929
1930 struct ly_ctx *ctx;
1931 struct lys_module *mod;
1932 struct lysc_type *type;
1933
1934 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1935
1936 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
1937 "typedef mytype {type union {type int8; type mybasetype;}}"
1938 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001939 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1940 assert_non_null(type);
1941 assert_int_equal(2, type->refcount);
1942 assert_int_equal(LY_TYPE_UNION, type->basetype);
1943 assert_non_null(((struct lysc_type_union*)type)->types);
1944 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1945 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
1946 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1947
1948 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
1949 "typedef mytype {type union {type int8; type mybasetype;}}"
1950 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001951 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1952 assert_non_null(type);
1953 assert_int_equal(1, type->refcount);
1954 assert_int_equal(LY_TYPE_UNION, type->basetype);
1955 assert_non_null(((struct lysc_type_union*)type)->types);
1956 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1957 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1958 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
1959 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1960
1961 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
1962 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001963 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
1964 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01001965 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001966 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1967 assert_non_null(type);
1968 assert_int_equal(1, type->refcount);
1969 assert_int_equal(LY_TYPE_UNION, type->basetype);
1970 assert_non_null(((struct lysc_type_union*)type)->types);
1971 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1972 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1973 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
1974 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1975 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
1976 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
1977
Radek Krejci6be5ff12018-11-26 13:18:15 +01001978 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01001979 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001980 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001981 logbuf_assert("Missing type substatement for union type mytype.");
1982
Radek Krejci096235c2019-01-11 11:12:19 +01001983 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
1984 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01001985
Radek Krejci096235c2019-01-11 11:12:19 +01001986 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001987 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001988 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
1989
Radek Krejci096235c2019-01-11 11:12:19 +01001990 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001991 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001992 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
1993
Radek Krejcicdfecd92018-11-26 11:27:32 +01001994 *state = NULL;
1995 ly_ctx_destroy(ctx, NULL);
1996}
1997
1998static void
1999test_type_dflt(void **state)
2000{
2001 *state = test_type_union;
2002
2003 struct ly_ctx *ctx;
2004 struct lys_module *mod;
2005 struct lysc_type *type;
2006
2007 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2008
2009 /* default is not inherited from union's types */
2010 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2011 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002012 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2013 assert_non_null(type);
2014 assert_int_equal(1, type->refcount);
2015 assert_int_equal(LY_TYPE_UNION, type->basetype);
2016 assert_non_null(((struct lysc_type_union*)type)->types);
2017 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2018 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2019 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2020 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2021 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2022
2023 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2024 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002025 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2026 assert_non_null(type);
2027 assert_int_equal(2, type->refcount);
2028 assert_int_equal(LY_TYPE_STRING, type->basetype);
2029 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2030 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2031
2032 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2033 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002034 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2035 assert_non_null(type);
2036 assert_int_equal(2, type->refcount);
2037 assert_int_equal(LY_TYPE_STRING, type->basetype);
2038 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2039 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2040
Radek Krejci6be5ff12018-11-26 13:18:15 +01002041 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2042 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2043 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002044 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2045 assert_non_null(type);
2046 assert_int_equal(4, type->refcount);
2047 assert_int_equal(LY_TYPE_STRING, type->basetype);
2048 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2049 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2050 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2051 assert_non_null(type);
2052 assert_int_equal(4, type->refcount);
2053 assert_int_equal(LY_TYPE_STRING, type->basetype);
2054 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2055 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2056
2057 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2058 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002059 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2060 assert_non_null(type);
2061 assert_int_equal(3, type->refcount);
2062 assert_int_equal(LY_TYPE_STRING, type->basetype);
2063 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2064 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2065
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002066 /* mandatory leaf does not takes default value from type */
2067 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2068 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002069 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2070 assert_non_null(type);
2071 assert_int_equal(LY_TYPE_STRING, type->basetype);
2072 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2073 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2074
Radek Krejcicdfecd92018-11-26 11:27:32 +01002075 *state = NULL;
2076 ly_ctx_destroy(ctx, NULL);
2077}
2078
Radek Krejci665421c2018-12-05 08:03:39 +01002079static void
2080test_status(void **state)
2081{
2082 *state = test_status;
2083
2084 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002085
2086 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2087
Radek Krejci096235c2019-01-11 11:12:19 +01002088 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002089 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002090 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2091
Radek Krejci096235c2019-01-11 11:12:19 +01002092 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002093 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002094 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2095
Radek Krejci096235c2019-01-11 11:12:19 +01002096 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002097 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002098 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2099
2100 *state = NULL;
2101 ly_ctx_destroy(ctx, NULL);
2102}
2103
Radek Krejcie86bf772018-12-14 11:39:53 +01002104static void
2105test_uses(void **state)
2106{
2107 *state = test_uses;
2108
2109 struct ly_ctx *ctx;
2110 struct lys_module *mod;
2111 struct lysc_node *parent, *child;
2112
2113 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2114
Radek Krejci0af46292019-01-11 16:02:31 +01002115 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2116 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002117 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2118 "grouping grp_a_top {leaf a1 {type int8;}}"
2119 "container a {uses grp_a; uses grp_a_top; uses g:grp; grouping grp_a {leaf a2 {type uint8;}}}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002120 assert_non_null((parent = mod->compiled->data));
2121 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2122 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2123 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002124 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002125 assert_non_null((child = child->next));
2126 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002127 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002128 assert_non_null((child = child->next));
2129 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002130 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002131 assert_non_null((child = child->next));
2132 assert_string_equal("y", child->name);
2133 assert_non_null(child->iffeatures);
2134 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2135 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2136 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2137 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2138 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2139 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2140
2141 /* make the imported module implemented and enable the feature */
2142 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2143 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2144 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2145 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2146 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002147
Radek Krejcib1b59152019-01-07 13:21:56 +01002148 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {type string;}}}");
2149 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp;}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002150 assert_non_null(mod->compiled->data);
2151 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2152 assert_string_equal("b", mod->compiled->data->name);
2153
Radek Krejci7f6a3812019-01-07 13:48:57 +01002154 logbuf_clean();
2155 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2156 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2157 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002158 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2159 assert_string_equal("l", mod->compiled->data->name);
2160 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2161 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2162 assert_string_equal("k", mod->compiled->data->next->name);
2163 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2164 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2165
Radek Krejcie86bf772018-12-14 11:39:53 +01002166 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002167 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002168 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2169
Radek Krejci096235c2019-01-11 11:12:19 +01002170 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002171 "grouping grp {leaf a{type string;}uses grp1;}"
2172 "grouping grp1 {leaf b {type string;}uses grp2;}"
2173 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002174 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2175
Radek Krejci096235c2019-01-11 11:12:19 +01002176 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002177 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2178
Radek Krejci096235c2019-01-11 11:12:19 +01002179 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;grouping grp{leaf a{type string;}}"
Radek Krejci76b3e962018-12-14 17:01:25 +01002180 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002181 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2182
Radek Krejci096235c2019-01-11 11:12:19 +01002183 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;grouping grp {leaf l {type string; status deprecated;}}"
Radek Krejci7f6a3812019-01-07 13:48:57 +01002184 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002185 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2186
Radek Krejci95710c92019-02-11 15:49:55 +01002187 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2188 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
2189 logbuf_assert("Duplicate identifier \"l\" of data definition statement.");
2190 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2191 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
2192 logbuf_assert("Duplicate identifier \"m\" of data definition statement.");
2193
Radek Krejcie86bf772018-12-14 11:39:53 +01002194 *state = NULL;
2195 ly_ctx_destroy(ctx, NULL);
2196}
2197
Radek Krejci01342af2019-01-03 15:18:08 +01002198static void
2199test_refine(void **state)
2200{
2201 *state = test_refine;
2202
2203 struct ly_ctx *ctx;
2204 struct lys_module *mod;
2205 struct lysc_node *parent, *child;
2206
2207 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2208
Radek Krejci096235c2019-01-11 11:12:19 +01002209 assert_non_null(lys_parse_mem(ctx, "module grp {yang-version 1.1;namespace urn:grp;prefix g; feature f;typedef mytype {type string; default cheers!;}"
2210 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2211 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2212 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2213 "leaf x {type mytype; mandatory true; must 1;}"
2214 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2215 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002216
Radek Krejcif0089082019-01-07 16:42:01 +01002217 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;import grp {prefix g;}feature fa;"
Radek Krejci01342af2019-01-03 15:18:08 +01002218 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002219 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002220 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002221 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2222 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002223 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002224 assert_non_null((parent = mod->compiled->data));
2225 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2226 assert_string_equal("c", parent->name);
2227 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2228 assert_int_equal(LYS_LEAF, child->nodetype);
2229 assert_string_equal("l", child->name);
2230 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2231 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2232 assert_non_null(child = child->next);
2233 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2234 assert_string_equal("ll", child->name);
2235 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2236 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2237 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002238 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2239 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002240 assert_non_null(child = child->next);
2241 assert_int_equal(LYS_CHOICE, child->nodetype);
2242 assert_string_equal("ch", child->name);
2243 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2244 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2245 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002246 assert_non_null(child->iffeatures);
2247 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002248 assert_non_null(child = child->next);
2249 assert_int_equal(LYS_LEAF, child->nodetype);
2250 assert_string_equal("x", child->name);
2251 assert_false(LYS_MAND_TRUE & child->flags);
2252 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002253 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2254 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002255 assert_string_equal("refined", child->dsc);
2256 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002257 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002258 assert_int_equal(LYS_ANYDATA, child->nodetype);
2259 assert_string_equal("a", child->name);
2260 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002261 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2262 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002263 assert_non_null(child->iffeatures);
2264 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002265 assert_string_equal("refined", child->dsc);
2266 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002267 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002268 assert_int_equal(LYS_CONTAINER, child->nodetype);
2269 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002270 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002271 assert_true(LYS_CONFIG_W & child->flags);
2272 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002273
2274 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002275 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002276 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002277 assert_int_equal(LYS_LEAF, child->nodetype);
2278 assert_string_equal("x", child->name);
2279 assert_false(LYS_MAND_TRUE & child->flags);
2280 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2281
2282 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002283 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002284 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002285 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2286
Radek Krejci096235c2019-01-11 11:12:19 +01002287 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002288 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002289 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2290
Radek Krejci096235c2019-01-11 11:12:19 +01002291 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002292 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002293 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2294
Radek Krejci096235c2019-01-11 11:12:19 +01002295 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002296 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002297 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2298
Radek Krejci096235c2019-01-11 11:12:19 +01002299 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002300 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002301 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002302 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002303 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002304 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2305
Radek Krejci096235c2019-01-11 11:12:19 +01002306 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002307 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002308 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" - leaf under the default case.");
2309
Radek Krejci096235c2019-01-11 11:12:19 +01002310 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002311 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002312 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2313
Radek Krejci096235c2019-01-11 11:12:19 +01002314 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002315 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002316 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2317
Radek Krejci096235c2019-01-11 11:12:19 +01002318 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;grouping grp {leaf l {type string; status deprecated;}}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002319 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002320 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2321
Radek Krejci096235c2019-01-11 11:12:19 +01002322 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002323 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002324 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2325
Radek Krejci096235c2019-01-11 11:12:19 +01002326 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002327 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002328 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2329
Radek Krejci096235c2019-01-11 11:12:19 +01002330 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002331 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002332 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2333
Radek Krejci096235c2019-01-11 11:12:19 +01002334 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002335 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002336 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2337
Radek Krejci01342af2019-01-03 15:18:08 +01002338 *state = NULL;
2339 ly_ctx_destroy(ctx, NULL);
2340}
Radek Krejcie86bf772018-12-14 11:39:53 +01002341
Radek Krejci95710c92019-02-11 15:49:55 +01002342static void
2343test_augment(void **state)
2344{
2345 *state = test_augment;
2346
2347 struct ly_ctx *ctx;
2348 struct lys_module *mod;
2349 const struct lysc_node *node;
2350 const struct lysc_node_choice *ch;
2351 const struct lysc_node_case *c;
2352
2353 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2354
2355 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2356 "container top {leaf a {type string;}}}");
2357 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2358 "leaf b {type a:atype;}}", LYS_IN_YANG));
2359 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2360 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2361 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2362 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2363 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2364 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2365 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2366 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2367 assert_non_null(node = mod->compiled->data);
2368 assert_string_equal(node->name, "top");
2369 assert_non_null(node = lysc_node_children(node));
2370 assert_string_equal(node->name, "a");
2371 assert_non_null(node = node->next);
2372 assert_string_equal(node->name, "c");
2373 assert_non_null(node = lysc_node_children(node));
2374 assert_string_equal(node->name, "c");
2375 assert_non_null(node = node->next);
2376 assert_string_equal(node->name, "d");
2377 assert_non_null(node = node->next);
2378 assert_string_equal(node->name, "c");
2379
2380 assert_non_null((mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;choice ch {leaf a {type string;}}"
2381 "augment /ch/c { leaf lc2 {type uint16;}}"
2382 "augment /ch { leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
2383 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2384 assert_null(mod->compiled->data->next);
2385 assert_string_equal("ch", ch->name);
2386 assert_non_null(c = ch->cases);
2387 assert_string_equal("a", c->name);
2388 assert_string_equal("a", c->child->name);
2389 assert_non_null(c = (const struct lysc_node_case*)c->next);
2390 assert_string_equal("b", c->name);
2391 assert_string_equal("b", c->child->name);
2392 assert_non_null(c = (const struct lysc_node_case*)c->next);
2393 assert_string_equal("c", c->name);
2394 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
2395 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
2396 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2397 assert_null(c->next);
2398
2399 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2400 "container c;"
2401 "augment /c {uses g;}}", LYS_IN_YANG)));
2402 assert_non_null(node = lysc_node_children(mod->compiled->data));
2403 assert_string_equal(node->name, "a");
2404
2405 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2406 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
2407 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found.");
2408
2409 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2410 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
2411 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2412
2413
2414
2415 *state = NULL;
2416 ly_ctx_destroy(ctx, NULL);
2417}
2418
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002419int main(void)
2420{
2421 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002422 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2423 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2424 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002425 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2426 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002427 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002428 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2429 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002430 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002431 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002432 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002433 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002434 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002435 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2436 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002437 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002438 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002439 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002440 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002441 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002442 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002443 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01002444 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01002445 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002446 };
2447
2448 return cmocka_run_group_tests(tests, NULL, NULL);
2449}