blob: b098ead23de997e6e1c5c35c9eee62311484da2d [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));
Radek Krejci8cce8532019-03-05 11:27:45 +0100183 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +0100184 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 Krejci09a1fc52019-02-13 10:55:17 +0100357 assert_null(lys_parse_mem(ctx.ctx, "module aa{namespace urn:aa; prefix aa; feature f1 {if-feature f2;} feature f2 {if-feature f1;}}", LYS_IN_YANG));
358 logbuf_assert("Feature \"f1\" is indirectly referenced from itself.");
359 assert_null(lys_parse_mem(ctx.ctx, "module ab{namespace urn:ab; prefix ab; feature f1 {if-feature f1;}}", LYS_IN_YANG));
360 logbuf_assert("Feature \"f1\" is referenced from itself.");
361
Radek Krejci1aefdf72018-11-01 11:01:39 +0100362 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100363 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100364 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100365 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 +0100366 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
367 assert_int_equal(0, lys_feature_value(modp, "f1"));
368 assert_int_equal(1, lys_feature_value(modp, "f2"));
369
Radek Krejcid05cbd92018-12-05 14:26:40 +0100370 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100371 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200372}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200373
Radek Krejci478020e2018-10-30 16:02:14 +0100374static void
375test_identity(void **state)
376{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100377 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100378
379 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100380 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100381
382 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100383 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
384 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
385 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
386 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100387
388 assert_non_null(mod1->compiled);
389 assert_non_null(mod1->compiled->identities);
390 assert_non_null(mod2->compiled);
391 assert_non_null(mod2->compiled->identities);
392
393 assert_non_null(mod1->compiled->identities[0].derived);
394 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
395 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
396 assert_non_null(mod2->compiled->identities[0].derived);
397 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
398 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
399 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
400 assert_non_null(mod2->compiled->identities[1].derived);
401 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
402 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
403 assert_non_null(mod2->compiled->identities[2].derived);
404 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
405 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
406
Radek Krejci4d483a82019-01-17 13:12:50 +0100407 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
408 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
409 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
410 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
411
412 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 +0100413 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
414
Radek Krejci4d483a82019-01-17 13:12:50 +0100415 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
416 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 +0100417 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
418
Radek Krejci38222632019-02-12 16:55:05 +0100419 assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG));
420 logbuf_assert("Unable to find base (i2) of identity \"i1\".");
421
422 assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG));
423 logbuf_assert("Identity \"i1\" is derived from itself.");
424 assert_null(lys_parse_mem(ctx, "module de{namespace urn:de; prefix de; identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}}", LYS_IN_YANG));
425 logbuf_assert("Identity \"i1\" is indirectly derived from itself.");
426
Radek Krejci7f2a5362018-11-28 13:05:37 +0100427 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100428 ly_ctx_destroy(ctx, NULL);
429}
430
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100431static void
432test_node_container(void **state)
433{
434 (void) state; /* unused */
435
436 struct ly_ctx *ctx;
437 struct lys_module *mod;
438 struct lysc_node_container *cont;
439
440 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
441 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 +0100442 assert_non_null(mod->compiled);
443 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
444 assert_int_equal(LYS_CONTAINER, cont->nodetype);
445 assert_string_equal("c", cont->name);
446 assert_true(cont->flags & LYS_CONFIG_W);
447 assert_true(cont->flags & LYS_STATUS_CURR);
448
Radek Krejci98094b32018-11-02 16:21:47 +0100449 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 +0100450 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100451 assert_non_null(mod->compiled);
452 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
453 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100454 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100455 assert_non_null((cont = (struct lysc_node_container*)cont->child));
456 assert_int_equal(LYS_CONTAINER, cont->nodetype);
457 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100458 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100459 assert_string_equal("child", cont->name);
460
461 ly_ctx_destroy(ctx, NULL);
462}
463
Radek Krejci0e5d8382018-11-28 16:37:53 +0100464static void
465test_node_leaflist(void **state)
466{
467 *state = test_node_leaflist;
468
469 struct ly_ctx *ctx;
470 struct lys_module *mod;
471 struct lysc_type *type;
472 struct lysc_node_leaflist *ll;
473
474 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
475
476 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
477 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
478 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
479 "leaf-list ll2 {type leafref {path ../target;}}"
480 "leaf target {type int8;}}",
481 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100482 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
483 assert_non_null(type);
484 assert_int_equal(1, type->refcount);
485 assert_int_equal(LY_TYPE_UNION, type->basetype);
486 assert_non_null(((struct lysc_type_union*)type)->types);
487 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
488 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
489 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
490 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
491 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
492 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
493 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
494 assert_non_null(type);
495 assert_int_equal(1, type->refcount);
496 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
497 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
498 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
499
Radek Krejci6bb080b2018-11-28 17:23:41 +0100500 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 +0100501 assert_non_null(mod->compiled);
502 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
503 assert_int_equal(0, ll->min);
504 assert_int_equal((uint32_t)-1, ll->max);
505
Radek Krejci6bb080b2018-11-28 17:23:41 +0100506 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 +0100507 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100508 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100509 assert_non_null(mod->compiled);
510 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
511 assert_non_null(ll->dflts);
512 assert_int_equal(3, ll->type->refcount);
513 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
514 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100515 assert_string_equal("1", ll->dflts[1]);
Radek Krejci93dcc392019-02-19 10:43:38 +0100516 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_DFLT | LYS_SET_CONFIG, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100517 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
518 assert_non_null(ll->dflts);
519 assert_int_equal(3, ll->type->refcount);
520 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
521 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100522 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
523
Radek Krejcifc11bd72019-04-11 16:00:05 +0200524 /* ordered-by is ignored for state data, RPC/action output parameters and notification content */
Radek Krejcia6d57732018-11-29 13:40:37 +0100525 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
526 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100527 /* but warning is present: */
Radek Krejcifc11bd72019-04-11 16:00:05 +0200528 logbuf_assert("The ordered-by statement is ignored in lists representing state data ().");
Radek Krejcia6d57732018-11-29 13:40:37 +0100529 assert_non_null(mod->compiled);
530 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
Radek Krejci93dcc392019-02-19 10:43:38 +0100531 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200532 logbuf_clean();
533
534 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;"
535 "rpc oper {output {leaf-list ll {type string; ordered-by user;}}}}", LYS_IN_YANG));
536 logbuf_assert("The ordered-by statement is ignored in lists representing RPC/action output parameters ().");
537 logbuf_clean();
538
539 assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f;"
540 "notification event {leaf-list ll {type string; ordered-by user;}}}", LYS_IN_YANG));
541 logbuf_assert("The ordered-by statement is ignored in lists representing notification content ().");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100542
543 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100544 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 +0100545 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
546
Radek Krejci096235c2019-01-11 11:12:19 +0100547 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 +0100548 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
549
550 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
551 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100552 assert_non_null(mod->compiled);
553 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
554 assert_non_null(ll->dflts);
555 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100556 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
557 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100558 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
559
Radek Krejci0e5d8382018-11-28 16:37:53 +0100560 *state = NULL;
561 ly_ctx_destroy(ctx, NULL);
562}
563
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100564static void
565test_node_list(void **state)
566{
567 *state = test_node_list;
568
569 struct ly_ctx *ctx;
570 struct lys_module *mod;
571 struct lysc_node_list *list;
572
573 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
574
575 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
576 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
577 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100578 list = (struct lysc_node_list*)mod->compiled->data;
579 assert_non_null(list);
580 assert_non_null(list->keys);
581 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
582 assert_string_equal("x", list->keys[0]->name);
583 assert_string_equal("y", list->keys[1]->name);
584 assert_non_null(list->child);
585 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
586 assert_true(list->child->flags & LYS_KEY);
587 assert_true(list->child->next->flags & LYS_KEY);
588 list = (struct lysc_node_list*)mod->compiled->data->next;
589 assert_non_null(list);
590 assert_null(list->keys);
591 assert_non_null(list->child);
Radek Krejci93dcc392019-02-19 10:43:38 +0100592 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, list->flags);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100593 assert_false(list->child->flags & LYS_KEY);
594
595 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
596 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100597 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100598 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100599 list = (struct lysc_node_list*)mod->compiled->data;
600 assert_non_null(list);
601 assert_string_equal("l", list->name);
602 assert_non_null(list->keys);
603 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
604 assert_string_equal("a", list->keys[0]->name);
605 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100606 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100607 assert_non_null(list->uniques);
608 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
609 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
610 assert_string_equal("a", list->uniques[0][0]->name);
611 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
612 assert_string_equal("b", list->uniques[0][1]->name);
613 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
614 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
615 assert_string_equal("e", list->uniques[1][0]->name);
616 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
617 assert_string_equal("d", list->uniques[1][1]->name);
618 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
619
Radek Krejci665421c2018-12-05 08:03:39 +0100620 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
621 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100622 list = (struct lysc_node_list*)mod->compiled->data;
623 assert_non_null(list);
624 assert_string_equal("l", list->name);
625 assert_non_null(list->keys);
626 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
627 assert_string_equal("a", list->keys[0]->name);
628 assert_true(list->keys[0]->flags & LYS_KEY);
629 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
630
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100631 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100632 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 +0100633 logbuf_assert("Missing key in list representing configuration data.");
634
Radek Krejci096235c2019-01-11 11:12:19 +0100635 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
636 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100637 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
638
Radek Krejci096235c2019-01-11 11:12:19 +0100639 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
640 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100641 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
642
Radek Krejci096235c2019-01-11 11:12:19 +0100643 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
644 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100645 logbuf_assert("Key of the configuration list must not be status leaf.");
646
Radek Krejci096235c2019-01-11 11:12:19 +0100647 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
648 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100649 logbuf_assert("Configuration node cannot be child of any state data node.");
650
Radek Krejci096235c2019-01-11 11:12:19 +0100651 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
652 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100653 logbuf_assert("The list's key \"x\" not found.");
654
Radek Krejci096235c2019-01-11 11:12:19 +0100655 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
656 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100657 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100658
Radek Krejci096235c2019-01-11 11:12:19 +0100659 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
660 "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 +0100661 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
662
Radek Krejci096235c2019-01-11 11:12:19 +0100663 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
664 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100665 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
666
Radek Krejci096235c2019-01-11 11:12:19 +0100667 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
668 "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 +0100669 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
670
Radek Krejci096235c2019-01-11 11:12:19 +0100671 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
672 "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 +0100673 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
674
Radek Krejci096235c2019-01-11 11:12:19 +0100675 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
676 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100677 logbuf_assert("Duplicated key identifier \"x\".");
678
Radek Krejci096235c2019-01-11 11:12:19 +0100679 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
680 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100681 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
682
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100683 *state = NULL;
684 ly_ctx_destroy(ctx, NULL);
685}
686
Radek Krejci056d0a82018-12-06 16:57:25 +0100687static void
688test_node_choice(void **state)
689{
690 *state = test_node_choice;
691
692 struct ly_ctx *ctx;
693 struct lys_module *mod;
694 struct lysc_node_choice *ch;
695 struct lysc_node_case *cs;
696
697 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
698
699 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100700 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
701 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100702 ch = (struct lysc_node_choice*)mod->compiled->data;
703 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100704 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100705 cs = ch->cases;
706 assert_non_null(cs);
707 assert_string_equal("a", cs->name);
708 assert_ptr_equal(ch, cs->parent);
709 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100710 assert_string_equal("a1", cs->child->name);
711 assert_non_null(cs->child->next);
712 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100713 assert_ptr_equal(cs, cs->child->parent);
714 cs = (struct lysc_node_case*)cs->next;
715 assert_non_null(cs);
716 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100717 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100718 assert_ptr_equal(ch, cs->parent);
719 assert_non_null(cs->child);
720 assert_string_equal("b", cs->child->name);
721 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100722 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
723 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100724 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100725 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100726
Radek Krejci096235c2019-01-11 11:12:19 +0100727 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
728 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100729 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100730 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
731 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100732 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100733 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
734 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100735 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100736 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
737 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100738 logbuf_assert("Duplicate identifier \"b\" of case statement.");
739
Radek Krejci096235c2019-01-11 11:12:19 +0100740 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
741 "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 +0100742 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100743 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
744 "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 +0100745 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100746 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
747 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100748 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
749 /* TODO check with mandatory nodes from augment placed into the case */
750
Radek Krejci056d0a82018-12-06 16:57:25 +0100751 *state = NULL;
752 ly_ctx_destroy(ctx, NULL);
753}
754
Radek Krejci9800fb82018-12-13 14:26:23 +0100755static void
756test_node_anydata(void **state)
757{
758 *state = test_node_anydata;
759
760 struct ly_ctx *ctx;
761 struct lys_module *mod;
762 struct lysc_node_anydata *any;
763
764 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
765
766 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
767 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100768 any = (struct lysc_node_anydata*)mod->compiled->data;
769 assert_non_null(any);
770 assert_int_equal(LYS_ANYDATA, any->nodetype);
Radek Krejci93dcc392019-02-19 10:43:38 +0100771 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags);
Radek Krejci9800fb82018-12-13 14:26:23 +0100772
773 logbuf_clean();
774 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
775 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100776 any = (struct lysc_node_anydata*)mod->compiled->data;
777 assert_non_null(any);
778 assert_int_equal(LYS_ANYXML, any->nodetype);
779 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
780 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
781
782 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100783 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 +0100784 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
785
786 *state = NULL;
787 ly_ctx_destroy(ctx, NULL);
788}
789
Radek Krejcif538ce52019-03-05 10:46:14 +0100790static void
791test_action(void **state)
792{
793 *state = test_action;
794
795 struct ly_ctx *ctx;
796 struct lys_module *mod;
797 const struct lysc_action *rpc;
798
799 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
800
801 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
802 "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG));
803 rpc = mod->compiled->rpcs;
804 assert_non_null(rpc);
805 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
806 assert_int_equal(LYS_ACTION, rpc->nodetype);
807 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
808 assert_string_equal("a", rpc->name);
809
810 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
811 "action b {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}}", LYS_IN_YANG));
812 rpc = lysc_node_actions(mod->compiled->data);
813 assert_non_null(rpc);
814 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
815 assert_int_equal(LYS_ACTION, rpc->nodetype);
816 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
817 assert_string_equal("b", rpc->name);
818
819 /* invalid */
820 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG));
821 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
822
Radek Krejci8cce8532019-03-05 11:27:45 +0100823 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG));
824 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
825 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1; namespace urn:cc;prefix cc;container c {leaf y {type string;} action y;}}", LYS_IN_YANG));
826 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
827 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1; namespace urn:dd;prefix dd;container c {action z; action z;}}", LYS_IN_YANG));
828 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200829 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} notification w;}");
Radek Krejci8cce8532019-03-05 11:27:45 +0100830 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG));
831 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
832
Radek Krejcifc11bd72019-04-11 16:00:05 +0200833 assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}"
834 "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
835 logbuf_assert("Action \"invalid\" is placed inside another RPC/action.");
836
837 assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}"
838 "augment /test/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
839 logbuf_assert("Action \"invalid\" is placed inside Notification.");
840
841 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; notification test {container a {uses grp;}}"
842 "grouping grp {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
843 logbuf_assert("Action \"invalid\" is placed inside Notification.");
844
845 *state = NULL;
846 ly_ctx_destroy(ctx, NULL);
847}
848
849static void
850test_notification(void **state)
851{
852 *state = test_notification;
853
854 struct ly_ctx *ctx;
855 struct lys_module *mod;
856 const struct lysc_notif *notif;
857
858 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
859
860 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
861 "notification a1 {leaf x {type int8;}} notification a2;}", LYS_IN_YANG));
862 notif = mod->compiled->notifs;
863 assert_non_null(notif);
864 assert_int_equal(2, LY_ARRAY_SIZE(notif));
865 assert_int_equal(LYS_NOTIF, notif->nodetype);
866 assert_int_equal(LYS_STATUS_CURR, notif->flags);
867 assert_string_equal("a1", notif->name);
868 assert_non_null(notif->data);
869 assert_string_equal("x", notif->data->name);
870 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
871 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
872 assert_string_equal("a2", notif[1].name);
873 assert_null(notif[1].data);
874
875 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
876 "notification b1 {leaf x {type int8;}} notification b2;}}", LYS_IN_YANG));
877 notif = lysc_node_notifs(mod->compiled->data);
878 assert_non_null(notif);
879 assert_int_equal(2, LY_ARRAY_SIZE(notif));
880 assert_int_equal(LYS_NOTIF, notif->nodetype);
881 assert_int_equal(LYS_STATUS_CURR, notif->flags);
882 assert_string_equal("b1", notif->name);
883 assert_non_null(notif->data);
884 assert_string_equal("x", notif->data->name);
885 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
886 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
887 assert_string_equal("b2", notif[1].name);
888 assert_null(notif[1].data);
889
890 /* invalid */
891 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {notification x;}}", LYS_IN_YANG));
892 logbuf_assert("Invalid keyword \"notification\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
893
894 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} notification x;}", LYS_IN_YANG));
895 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
896 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1; namespace urn:cc;prefix cc;container c {leaf y {type string;} notification y;}}", LYS_IN_YANG));
897 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
898 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1; namespace urn:dd;prefix dd;container c {notification z; notification z;}}", LYS_IN_YANG));
899 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
900 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}");
901 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; notification w;}", LYS_IN_YANG));
902 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
903
904 assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}"
905 "augment /test/input/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
906 logbuf_assert("Notification \"invalid\" is placed inside RPC/action.");
907
908 assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}"
909 "augment /test/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
910 logbuf_assert("Notification \"invalid\" is placed inside another Notification.");
911
912 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; rpc test {input {container a {uses grp;}}}"
913 "grouping grp {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
914 logbuf_assert("Notification \"invalid\" is placed inside RPC/action.");
915
Radek Krejcif538ce52019-03-05 10:46:14 +0100916 *state = NULL;
917 ly_ctx_destroy(ctx, NULL);
918}
919
Radek Krejci0f07bed2018-11-13 14:01:40 +0100920/**
921 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
922 * do not test the expression itself
923 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100924static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100925test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100926{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100927 *state = test_type_range;
928
929 struct ly_ctx *ctx;
930 struct lys_module *mod;
931 struct lysc_type *type;
932
933 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
934
935 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 +0100936 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
937 assert_non_null(type);
938 assert_int_equal(LY_TYPE_INT8, type->basetype);
939 assert_non_null(((struct lysc_type_num*)type)->range);
940 assert_non_null(((struct lysc_type_num*)type)->range->parts);
941 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
942 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
943 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
944 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
945 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
946
947 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 +0100948 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
949 assert_non_null(type);
950 assert_int_equal(LY_TYPE_INT16, type->basetype);
951 assert_non_null(((struct lysc_type_num*)type)->range);
952 assert_non_null(((struct lysc_type_num*)type)->range->parts);
953 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
954 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
955 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
956 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
957 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
958
959 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 +0100960 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
961 assert_non_null(type);
962 assert_int_equal(LY_TYPE_INT32, type->basetype);
963 assert_non_null(((struct lysc_type_num*)type)->range);
964 assert_non_null(((struct lysc_type_num*)type)->range->parts);
965 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
966 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
967 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
968 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
969 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
970
971 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 +0100972 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
973 assert_non_null(type);
974 assert_int_equal(LY_TYPE_INT64, type->basetype);
975 assert_non_null(((struct lysc_type_num*)type)->range);
976 assert_non_null(((struct lysc_type_num*)type)->range->parts);
977 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
978 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
979 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
980 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
981 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
982
983 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 +0100984 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
985 assert_non_null(type);
986 assert_int_equal(LY_TYPE_UINT8, type->basetype);
987 assert_non_null(((struct lysc_type_num*)type)->range);
988 assert_non_null(((struct lysc_type_num*)type)->range->parts);
989 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
990 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
991 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
992 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
993 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
994
995 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 +0100996 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
997 assert_non_null(type);
998 assert_int_equal(LY_TYPE_UINT16, type->basetype);
999 assert_non_null(((struct lysc_type_num*)type)->range);
1000 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1001 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1002 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1003 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1004 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1005 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1006
1007 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 +01001008 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1009 assert_non_null(type);
1010 assert_int_equal(LY_TYPE_UINT32, type->basetype);
1011 assert_non_null(((struct lysc_type_num*)type)->range);
1012 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1013 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1014 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1015 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1016 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1017 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1018
1019 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 +01001020 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1021 assert_non_null(type);
1022 assert_int_equal(LY_TYPE_UINT64, type->basetype);
1023 assert_non_null(((struct lysc_type_num*)type)->range);
1024 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1025 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1026 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1027 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1028 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1029 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1030
1031 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
1032 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +01001033 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1034 assert_non_null(type);
1035 assert_int_equal(3, type->refcount);
1036 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1037 assert_non_null(((struct lysc_type_num*)type)->range);
1038 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1039 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1040
Radek Krejcif1394042019-01-08 10:53:34 +01001041 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
1042 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
1043 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +01001044 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1045 assert_non_null(type);
1046 assert_int_equal(1, type->refcount);
1047 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1048 assert_non_null(((struct lysc_type_num*)type)->range);
1049 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
1050 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
1051 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1052 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1053 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1054 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1055
Radek Krejci0f07bed2018-11-13 14:01:40 +01001056 *state = NULL;
1057 ly_ctx_destroy(ctx, NULL);
1058}
1059
1060static void
1061test_type_length(void **state)
1062{
1063 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +01001064
1065 struct ly_ctx *ctx;
1066 struct lys_module *mod;
1067 struct lysc_type *type;
1068
1069 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1070
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001071 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 +01001072 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1073 assert_non_null(type);
1074 assert_non_null(((struct lysc_type_bin*)type)->length);
1075 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001076 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
1077 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001078 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1079 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1080 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1081
1082 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 +01001083 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1084 assert_non_null(type);
1085 assert_non_null(((struct lysc_type_bin*)type)->length);
1086 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1087 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +01001088 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1089 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001090
1091 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 +01001092 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1093 assert_non_null(type);
1094 assert_non_null(((struct lysc_type_bin*)type)->length);
1095 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1096 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1097 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +01001098 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001099
1100 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 +01001101 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1102 assert_non_null(type);
1103 assert_non_null(((struct lysc_type_bin*)type)->length);
1104 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1105 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1106 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1107 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1108
1109 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 +01001110 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1111 assert_non_null(type);
1112 assert_non_null(((struct lysc_type_bin*)type)->length);
1113 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1114 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1115 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1116 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1117
1118 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 +01001119 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1120 assert_non_null(type);
1121 assert_non_null(((struct lysc_type_bin*)type)->length);
1122 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1123 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1124 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1125 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1126 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1127 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1128
1129 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 +01001130 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1131 assert_non_null(type);
1132 assert_non_null(((struct lysc_type_bin*)type)->length);
1133 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1134 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1135 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1136 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1137 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1138 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1139
Radek Krejcib31c8992018-11-12 16:29:16 +01001140 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1141 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001142 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1143 assert_non_null(type);
1144 assert_non_null(((struct lysc_type_bin*)type)->length);
1145 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1146 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1147 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1148 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1149
1150 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1151 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001152 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1153 assert_non_null(type);
1154 assert_non_null(((struct lysc_type_bin*)type)->length);
1155 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1156 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1157 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1158 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1159
1160 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1161 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001162 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1163 assert_non_null(type);
1164 assert_non_null(((struct lysc_type_bin*)type)->length);
1165 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1166 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1167 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1168 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1169 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1170 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1171
1172 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 +01001173 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001174 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1175 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001176 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001177 assert_non_null(((struct lysc_type_bin*)type)->length);
1178 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1179 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1180 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001181 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001182 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1183 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001184 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001185 assert_non_null(((struct lysc_type_bin*)type)->length);
1186 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1187 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1188 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1189 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1190
Radek Krejci56aa27c2018-11-13 14:21:59 +01001191 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1192 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001193 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1194 assert_non_null(type);
1195 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001196 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001197 assert_non_null(((struct lysc_type_str*)type)->length);
1198 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1199 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1200 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1201 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001202
Radek Krejci5969f272018-11-23 10:03:58 +01001203 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1204 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001205 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1206 assert_non_null(type);
1207 assert_int_equal(LY_TYPE_STRING, type->basetype);
1208 assert_int_equal(1, type->refcount);
1209 assert_non_null(((struct lysc_type_str*)type)->length);
1210 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1211 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1212 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1213 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1214
Radek Krejci4f28eda2018-11-12 11:46:16 +01001215 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001216 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 +01001217 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001218 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 +01001219 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001220 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 +01001221 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001222 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 +01001223 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001224 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 +01001225 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001226 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 +01001227 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001228 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 +01001229 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001230 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 +01001231 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001232 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 +01001233 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001234 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 +01001235 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001236 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 +01001237 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001238 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 +01001239 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001240
Radek Krejci096235c2019-01-11 11:12:19 +01001241 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1242 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001243 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001244 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1245 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001246 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001247 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1248 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001249 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001250 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1251 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001252 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 +01001253 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 +01001254 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001255 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001256 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 +01001257 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001258 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001259 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 +01001260 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001261 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001262
Radek Krejci096235c2019-01-11 11:12:19 +01001263 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 +01001264 logbuf_assert("Invalid type restrictions for binary type.");
1265
Radek Krejci4f28eda2018-11-12 11:46:16 +01001266 *state = NULL;
1267 ly_ctx_destroy(ctx, NULL);
1268}
1269
Radek Krejcicda74152018-11-13 15:27:32 +01001270static void
1271test_type_pattern(void **state)
1272{
1273 *state = test_type_pattern;
1274
1275 struct ly_ctx *ctx;
1276 struct lys_module *mod;
1277 struct lysc_type *type;
1278
1279 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1280
Radek Krejci027d5802018-11-14 16:57:28 +01001281 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 +01001282 "pattern .* {error-app-tag errortag;error-message error;}"
1283 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001284 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1285 assert_non_null(type);
1286 assert_non_null(((struct lysc_type_str*)type)->patterns);
1287 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1288 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1289 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1290 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1291 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1292 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1293 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1294
1295 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1296 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001297 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1298 assert_non_null(type);
1299 assert_int_equal(LY_TYPE_STRING, type->basetype);
1300 assert_int_equal(1, type->refcount);
1301 assert_non_null(((struct lysc_type_str*)type)->patterns);
1302 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1303 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1304 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1305
Radek Krejci04bc1e92018-11-14 14:28:13 +01001306 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1307 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001308 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1309 assert_non_null(type);
1310 assert_int_equal(LY_TYPE_STRING, type->basetype);
1311 assert_int_equal(1, type->refcount);
1312 assert_non_null(((struct lysc_type_str*)type)->patterns);
1313 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1314 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1315
Radek Krejcicda74152018-11-13 15:27:32 +01001316 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001317 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 +01001318 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001319 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1320 assert_non_null(type);
1321 assert_non_null(((struct lysc_type_str*)type)->patterns);
1322 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1323 /* TODO check some data "^Å™$" */
1324
1325 *state = NULL;
1326 ly_ctx_destroy(ctx, NULL);
1327}
1328
Radek Krejci8b764662018-11-14 14:15:13 +01001329static void
1330test_type_enum(void **state)
1331{
1332 *state = test_type_enum;
1333
1334 struct ly_ctx *ctx;
1335 struct lys_module *mod;
1336 struct lysc_type *type;
1337
1338 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1339
Radek Krejci027d5802018-11-14 16:57:28 +01001340 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 +01001341 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1342 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001343 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1344 assert_non_null(type);
1345 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1346 assert_non_null(((struct lysc_type_enum*)type)->enums);
1347 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1348 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1349 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1350 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1351 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1352 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1353 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1354 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1355 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1356 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1357 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1358 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1359 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1360 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1361
Radek Krejci027d5802018-11-14 16:57:28 +01001362 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 {"
1363 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001364 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1365 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001366 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1367 assert_non_null(type);
1368 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1369 assert_non_null(((struct lysc_type_enum*)type)->enums);
1370 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1371 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1372 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1373 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1374 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1375
1376
1377 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001378 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1379 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1380 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 +01001381 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1382 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1383 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1384 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1385 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1386 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1387 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1388 "enum one; enum one;}}}", LYS_IN_YANG));
1389 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1390 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1391 "enum '';}}}", LYS_IN_YANG));
1392 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1393 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1394 "enum ' x';}}}", LYS_IN_YANG));
1395 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1396 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1397 "enum 'x ';}}}", LYS_IN_YANG));
1398 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1399 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1400 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1401 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1402
Radek Krejci096235c2019-01-11 11:12:19 +01001403 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 +01001404 logbuf_assert("Missing enum substatement for enumeration type.");
1405
Radek Krejci096235c2019-01-11 11:12:19 +01001406 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 +01001407 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001408 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1409
Radek Krejci096235c2019-01-11 11:12:19 +01001410 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 +01001411 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001412 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 +01001413 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 +01001414 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001415 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1416
Radek Krejci096235c2019-01-11 11:12:19 +01001417 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 +01001418 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001419 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1420
Radek Krejci096235c2019-01-11 11:12:19 +01001421 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001422 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001423 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001424
Radek Krejci096235c2019-01-11 11:12:19 +01001425 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 +01001426 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001427 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1428
Radek Krejci8b764662018-11-14 14:15:13 +01001429 *state = NULL;
1430 ly_ctx_destroy(ctx, NULL);
1431}
1432
1433static void
1434test_type_bits(void **state)
1435{
1436 *state = test_type_bits;
1437
1438 struct ly_ctx *ctx;
1439 struct lys_module *mod;
1440 struct lysc_type *type;
1441
1442 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1443
Radek Krejci027d5802018-11-14 16:57:28 +01001444 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 +01001445 "bit automin; bit one {if-feature f; position 1;}"
1446 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001447 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1448 assert_non_null(type);
1449 assert_int_equal(LY_TYPE_BITS, type->basetype);
1450 assert_non_null(((struct lysc_type_bits*)type)->bits);
1451 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1452 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1453 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1454 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1455 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1456 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1457 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1458 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1459 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1460 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1461 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1462 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1463
Radek Krejci027d5802018-11-14 16:57:28 +01001464 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 {"
1465 "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 +01001466 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001467 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1468 assert_non_null(type);
1469 assert_int_equal(LY_TYPE_BITS, type->basetype);
1470 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001471 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1472 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1473 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1474 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1475 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1476 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1477 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001478
1479
1480 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001481 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1482 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1483 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 +01001484 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1485 "bit one {position -1;}}}}", LYS_IN_YANG));
1486 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1487 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1488 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1489 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1490 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1491 "bit one; bit one;}}}", LYS_IN_YANG));
1492 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1493 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1494 "bit '11';}}}", LYS_IN_YANG));
1495 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1496 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1497 "bit 'x1$1';}}}", LYS_IN_YANG));
1498 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1499
Radek Krejci096235c2019-01-11 11:12:19 +01001500 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 +01001501 logbuf_assert("Missing bit substatement for bits type.");
1502
Radek Krejci096235c2019-01-11 11:12:19 +01001503 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 +01001504 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001505 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1506
Radek Krejci096235c2019-01-11 11:12:19 +01001507 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 +01001508 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001509 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 +01001510 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 +01001511 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001512 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1513
Radek Krejci096235c2019-01-11 11:12:19 +01001514 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 +01001515 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001516 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1517
Radek Krejci096235c2019-01-11 11:12:19 +01001518 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001519 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001520 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001521
Radek Krejci096235c2019-01-11 11:12:19 +01001522 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 +01001523 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001524 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1525
Radek Krejci8b764662018-11-14 14:15:13 +01001526 *state = NULL;
1527 ly_ctx_destroy(ctx, NULL);
1528}
1529
Radek Krejci6cba4292018-11-15 17:33:29 +01001530static void
1531test_type_dec64(void **state)
1532{
1533 *state = test_type_dec64;
1534
1535 struct ly_ctx *ctx;
1536 struct lys_module *mod;
1537 struct lysc_type *type;
1538
1539 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1540
1541 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1542 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001543 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1544 assert_non_null(type);
1545 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1546 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1547 assert_non_null(((struct lysc_type_dec*)type)->range);
1548 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1549 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1550 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1551 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1552
1553 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1554 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001555 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1556 assert_non_null(type);
1557 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1558 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1559 assert_non_null(((struct lysc_type_dec*)type)->range);
1560 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1561 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1562 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1563 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1564 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1565 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1566 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1567 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1568
1569 /* invalid cases */
1570 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1571 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1572 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1573 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1574 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1575 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1576
Radek Krejci096235c2019-01-11 11:12:19 +01001577 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 +01001578 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1579
Radek Krejci096235c2019-01-11 11:12:19 +01001580 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 +01001581 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001582
Radek Krejci096235c2019-01-11 11:12:19 +01001583 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 +01001584 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001585 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1586
Radek Krejci096235c2019-01-11 11:12:19 +01001587 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 +01001588 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001589 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1590
Radek Krejci096235c2019-01-11 11:12:19 +01001591 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 +01001592 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001593 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1594
Radek Krejci096235c2019-01-11 11:12:19 +01001595 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 +01001596 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001597 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1598
Radek Krejci6cba4292018-11-15 17:33:29 +01001599 *state = NULL;
1600 ly_ctx_destroy(ctx, NULL);
1601}
1602
Radek Krejci16c0f822018-11-16 10:46:10 +01001603static void
1604test_type_instanceid(void **state)
1605{
1606 *state = test_type_instanceid;
1607
1608 struct ly_ctx *ctx;
1609 struct lys_module *mod;
1610 struct lysc_type *type;
1611
1612 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1613
1614 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1615 "leaf l1 {type instance-identifier {require-instance true;}}"
1616 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001617 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1618 assert_non_null(type);
1619 assert_int_equal(LY_TYPE_INST, type->basetype);
1620 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1621
1622 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1623 assert_non_null(type);
1624 assert_int_equal(LY_TYPE_INST, type->basetype);
1625 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1626
1627 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1628 assert_non_null(type);
1629 assert_int_equal(LY_TYPE_INST, type->basetype);
1630 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1631
1632 /* invalid cases */
1633 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1634 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1635
Radek Krejci096235c2019-01-11 11:12:19 +01001636 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 +01001637 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1638
1639 *state = NULL;
1640 ly_ctx_destroy(ctx, NULL);
1641}
1642
Radek Krejci555cb5b2018-11-16 14:54:33 +01001643static void
1644test_type_identityref(void **state)
1645{
1646 *state = test_type_identityref;
1647
1648 struct ly_ctx *ctx;
1649 struct lys_module *mod;
1650 struct lysc_type *type;
1651
1652 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1653
1654 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;}"
1655 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001656 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001657 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1658 assert_non_null(type);
1659 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1660 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1661 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1662 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1663
1664 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1665 assert_non_null(type);
1666 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1667 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1668 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1669 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1670 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1671
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001672 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1673 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001674 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1675 assert_non_null(type);
1676 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1677 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1678 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1679 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1680 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1681
Radek Krejci555cb5b2018-11-16 14:54:33 +01001682 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001683 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 +01001684 logbuf_assert("Missing base substatement for identityref type.");
1685
Radek Krejci096235c2019-01-11 11:12:19 +01001686 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001687 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001688 logbuf_assert("Missing base substatement for identityref type mytype.");
1689
Radek Krejci096235c2019-01-11 11:12:19 +01001690 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 +01001691 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001692 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001693
Radek Krejci096235c2019-01-11 11:12:19 +01001694 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 +01001695 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001696 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001697
Radek Krejci096235c2019-01-11 11:12:19 +01001698 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001699 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001700 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1701
Radek Krejci096235c2019-01-11 11:12:19 +01001702 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 +01001703 logbuf_assert("Unable to find base (j) of identityref.");
1704
Radek Krejci096235c2019-01-11 11:12:19 +01001705 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 +01001706 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1707
Radek Krejci555cb5b2018-11-16 14:54:33 +01001708 *state = NULL;
1709 ly_ctx_destroy(ctx, NULL);
1710}
1711
Radek Krejcia3045382018-11-22 14:30:31 +01001712static void
1713test_type_leafref(void **state)
1714{
1715 *state = test_type_leafref;
1716
1717 struct ly_ctx *ctx;
1718 struct lys_module *mod;
1719 struct lysc_type *type;
1720 const char *path, *name, *prefix;
1721 size_t prefix_len, name_len;
1722 int parent_times, has_predicate;
1723
1724 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1725
1726 /* lys_path_token() */
1727 path = "invalid_path";
1728 parent_times = 0;
1729 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1730 path = "..";
1731 parent_times = 0;
1732 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1733 path = "..[";
1734 parent_times = 0;
1735 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1736 path = "../";
1737 parent_times = 0;
1738 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1739 path = "/";
1740 parent_times = 0;
1741 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1742
1743 path = "../../pref:id/xxx[predicate]/invalid!!!";
1744 parent_times = 0;
1745 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1746 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1747 assert_int_equal(4, prefix_len);
1748 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1749 assert_int_equal(2, name_len);
1750 assert_int_equal(0, strncmp("id", name, name_len));
1751 assert_int_equal(2, parent_times);
1752 assert_int_equal(0, has_predicate);
1753 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1754 assert_string_equal("[predicate]/invalid!!!", path);
1755 assert_int_equal(0, prefix_len);
1756 assert_null(prefix);
1757 assert_int_equal(3, name_len);
1758 assert_int_equal(0, strncmp("xxx", name, name_len));
1759 assert_int_equal(1, has_predicate);
1760 path += 11;
1761 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1762 assert_string_equal("!!!", path);
1763 assert_int_equal(0, prefix_len);
1764 assert_null(prefix);
1765 assert_int_equal(7, name_len);
1766 assert_int_equal(0, strncmp("invalid", name, name_len));
1767
1768 path = "/absolute/prefix:path";
1769 parent_times = 0;
1770 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1771 assert_string_equal("/prefix:path", path);
1772 assert_int_equal(0, prefix_len);
1773 assert_null(prefix);
1774 assert_int_equal(8, name_len);
1775 assert_int_equal(0, strncmp("absolute", name, name_len));
1776 assert_int_equal(-1, parent_times);
1777 assert_int_equal(0, has_predicate);
1778 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1779 assert_int_equal(0, *path);
1780 assert_int_equal(6, prefix_len);
1781 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1782 assert_int_equal(4, name_len);
1783 assert_int_equal(0, strncmp("path", name, name_len));
1784 assert_int_equal(0, has_predicate);
1785
1786 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001787 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 +01001788 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1789 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001790 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1791 assert_non_null(type);
1792 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1793 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001794 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001795 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1796 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001797 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1798 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1799 assert_non_null(type);
1800 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1801 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001802 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1803 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1804 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001805 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1806
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001807 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 +01001808 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001809 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001810 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1811 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001812 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001813 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1814 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1815 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001816 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1817 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001818 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1819
1820 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001821 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 +01001822 "typedef mytype3 {type c:mytype {require-instance false;}}"
1823 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001824 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1825 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001826 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001827 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1828 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1829 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001830 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1831 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001832 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1833 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1834 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001835 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001836 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1837 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1838 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1839 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1840
Radek Krejci4ce68932018-11-28 12:53:36 +01001841 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1842 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1843 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001844 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1845 assert_non_null(type);
1846 assert_int_equal(1, type->refcount);
1847 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1848 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1849 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1850 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1851 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1852 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1853
Radek Krejci58d171e2018-11-23 13:50:55 +01001854 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001855 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 +01001856 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1857 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001858 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1859 assert_non_null(type);
1860 assert_int_equal(1, type->refcount);
1861 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1862 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1863 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1864 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1865 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1866
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001867 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1868 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1869 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001870 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001871 LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001872 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001873 assert_non_null(type);
1874 assert_int_equal(1, type->refcount);
1875 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001876 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001877 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1878 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1879 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001880
Radek Krejci20e8a182018-12-07 11:43:21 +01001881 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1882 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1883 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1884 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001885 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1886 assert_non_null(type);
1887 assert_int_equal(1, type->refcount);
1888 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1889 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1890 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1891 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1892 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1893
Radek Krejci3d929562019-02-21 11:25:55 +01001894 /* leafref to imported (not yet implemented) module */
1895 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}");
1896 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}"
1897 "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG));
1898 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1899 assert_non_null(type);
1900 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1901 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1902 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1903 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h"));
1904 assert_int_equal(1, mod->implemented);
1905 assert_non_null(mod->compiled->data);
1906 assert_string_equal("h", mod->compiled->data->name);
1907
Radek Krejcia3045382018-11-22 14:30:31 +01001908 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001909 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 +01001910 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001911 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001912 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 +01001913 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001914 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001915 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 +01001916 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001917 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 +01001918 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 +01001919 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001920 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001921 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 +01001922 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001923 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001924 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 +01001925 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001926 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001927 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 +01001928 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001929 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001930 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001931 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001932 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001933 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001934 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001935 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001936 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001937 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001938 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1939 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001940 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001941 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001942 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 +01001943
Radek Krejci096235c2019-01-11 11:12:19 +01001944 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001945 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001946 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001947 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 +01001948 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001949 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1950
Radek Krejci096235c2019-01-11 11:12:19 +01001951 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001952 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1953 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1954 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1955 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001956 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1957
Radek Krejci096235c2019-01-11 11:12:19 +01001958 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001959 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1960 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1961 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1962 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001963 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1964
Radek Krejci096235c2019-01-11 11:12:19 +01001965 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001966 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1967 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1968 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1969 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001970 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1971
Radek Krejci096235c2019-01-11 11:12:19 +01001972 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001973 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1974 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1975 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1976 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001977 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1978
Radek Krejci096235c2019-01-11 11:12:19 +01001979 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001980 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1981 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1982 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1983 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001984 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1985
Radek Krejci096235c2019-01-11 11:12:19 +01001986 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001987 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1988 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1989 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1990 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001991 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1992
Radek Krejci096235c2019-01-11 11:12:19 +01001993 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001994 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1995 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1996 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1997 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001998 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1999
Radek Krejci096235c2019-01-11 11:12:19 +01002000 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002001 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2002 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2003 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
2004 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002005 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
2006
Radek Krejci096235c2019-01-11 11:12:19 +01002007 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002008 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2009 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2010 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
2011 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002012 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
2013
Radek Krejci096235c2019-01-11 11:12:19 +01002014 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002015 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2016 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2017 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
2018 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002019 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
2020
Radek Krejci096235c2019-01-11 11:12:19 +01002021 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002022 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2023 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2024 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
2025 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002026 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
2027
Radek Krejci096235c2019-01-11 11:12:19 +01002028 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002029 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2030 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2031 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
2032 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002033 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
2034
Radek Krejci096235c2019-01-11 11:12:19 +01002035 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002036 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2037 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2038 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
2039 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002040 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
2041
Radek Krejci096235c2019-01-11 11:12:19 +01002042 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2044 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
2045 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
2046 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002047 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
2048
Radek Krejci096235c2019-01-11 11:12:19 +01002049 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002050 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
2051 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2052 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
2053 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002054 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
2055
Radek Krejci412ddfa2018-11-23 11:44:11 +01002056 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01002057 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01002058 "leaf ref1 {type leafref {path /ref2;}}"
2059 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01002060 "leaf ref3 {type leafref {path /ref4;}}"
2061 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01002062 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
2063
Radek Krejcia3045382018-11-22 14:30:31 +01002064 *state = NULL;
2065 ly_ctx_destroy(ctx, NULL);
2066}
2067
Radek Krejci43699232018-11-23 14:59:46 +01002068static void
2069test_type_empty(void **state)
2070{
2071 *state = test_type_empty;
2072
2073 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01002074
2075 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2076
2077 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002078 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01002079 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01002080 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
2081
Radek Krejci096235c2019-01-11 11:12:19 +01002082 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 +01002083 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01002084 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
2085
2086 *state = NULL;
2087 ly_ctx_destroy(ctx, NULL);
2088}
2089
Radek Krejcicdfecd92018-11-26 11:27:32 +01002090
2091static void
2092test_type_union(void **state)
2093{
2094 *state = test_type_union;
2095
2096 struct ly_ctx *ctx;
2097 struct lys_module *mod;
2098 struct lysc_type *type;
2099
2100 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2101
2102 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2103 "typedef mytype {type union {type int8; type mybasetype;}}"
2104 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002105 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2106 assert_non_null(type);
2107 assert_int_equal(2, type->refcount);
2108 assert_int_equal(LY_TYPE_UNION, type->basetype);
2109 assert_non_null(((struct lysc_type_union*)type)->types);
2110 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2111 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2112 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2113
2114 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2115 "typedef mytype {type union {type int8; type mybasetype;}}"
2116 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002117 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2118 assert_non_null(type);
2119 assert_int_equal(1, type->refcount);
2120 assert_int_equal(LY_TYPE_UNION, type->basetype);
2121 assert_non_null(((struct lysc_type_union*)type)->types);
2122 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2123 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2124 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2125 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2126
2127 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2128 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002129 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2130 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002131 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002132 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2133 assert_non_null(type);
2134 assert_int_equal(1, type->refcount);
2135 assert_int_equal(LY_TYPE_UNION, type->basetype);
2136 assert_non_null(((struct lysc_type_union*)type)->types);
2137 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2138 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2139 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2140 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2141 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2142 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2143
Radek Krejci6be5ff12018-11-26 13:18:15 +01002144 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01002145 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002146 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002147 logbuf_assert("Missing type substatement for union type mytype.");
2148
Radek Krejci096235c2019-01-11 11:12:19 +01002149 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2150 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002151
Radek Krejci096235c2019-01-11 11:12:19 +01002152 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 +01002153 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002154 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2155
Radek Krejci096235c2019-01-11 11:12:19 +01002156 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 +01002157 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002158 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2159
Radek Krejcicdfecd92018-11-26 11:27:32 +01002160 *state = NULL;
2161 ly_ctx_destroy(ctx, NULL);
2162}
2163
2164static void
2165test_type_dflt(void **state)
2166{
2167 *state = test_type_union;
2168
2169 struct ly_ctx *ctx;
2170 struct lys_module *mod;
2171 struct lysc_type *type;
2172
2173 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2174
2175 /* default is not inherited from union's types */
2176 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2177 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002178 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2179 assert_non_null(type);
2180 assert_int_equal(1, type->refcount);
2181 assert_int_equal(LY_TYPE_UNION, type->basetype);
2182 assert_non_null(((struct lysc_type_union*)type)->types);
2183 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2184 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2185 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2186 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2187 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2188
2189 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2190 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002191 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2192 assert_non_null(type);
2193 assert_int_equal(2, type->refcount);
2194 assert_int_equal(LY_TYPE_STRING, type->basetype);
2195 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2196 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2197
2198 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2199 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002200 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2201 assert_non_null(type);
2202 assert_int_equal(2, type->refcount);
2203 assert_int_equal(LY_TYPE_STRING, type->basetype);
2204 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2205 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2206
Radek Krejci6be5ff12018-11-26 13:18:15 +01002207 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2208 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2209 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002210 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2211 assert_non_null(type);
2212 assert_int_equal(4, type->refcount);
2213 assert_int_equal(LY_TYPE_STRING, type->basetype);
2214 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2215 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2216 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2217 assert_non_null(type);
2218 assert_int_equal(4, type->refcount);
2219 assert_int_equal(LY_TYPE_STRING, type->basetype);
2220 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2221 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2222
2223 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2224 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002225 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2226 assert_non_null(type);
2227 assert_int_equal(3, type->refcount);
2228 assert_int_equal(LY_TYPE_STRING, type->basetype);
2229 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2230 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2231
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002232 /* mandatory leaf does not takes default value from type */
2233 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2234 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002235 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2236 assert_non_null(type);
2237 assert_int_equal(LY_TYPE_STRING, type->basetype);
2238 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2239 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2240
Radek Krejcicdfecd92018-11-26 11:27:32 +01002241 *state = NULL;
2242 ly_ctx_destroy(ctx, NULL);
2243}
2244
Radek Krejci665421c2018-12-05 08:03:39 +01002245static void
2246test_status(void **state)
2247{
2248 *state = test_status;
2249
2250 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002251
2252 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2253
Radek Krejci096235c2019-01-11 11:12:19 +01002254 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002255 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002256 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2257
Radek Krejci096235c2019-01-11 11:12:19 +01002258 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002259 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002260 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2261
Radek Krejci096235c2019-01-11 11:12:19 +01002262 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002263 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002264 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2265
2266 *state = NULL;
2267 ly_ctx_destroy(ctx, NULL);
2268}
2269
Radek Krejcie86bf772018-12-14 11:39:53 +01002270static void
2271test_uses(void **state)
2272{
2273 *state = test_uses;
2274
2275 struct ly_ctx *ctx;
2276 struct lys_module *mod;
Radek Krejci3641f562019-02-13 15:38:40 +01002277 const struct lysc_node *parent, *child;
Radek Krejcie86bf772018-12-14 11:39:53 +01002278
2279 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2280
Radek Krejci0af46292019-01-11 16:02:31 +01002281 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2282 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002283 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2284 "grouping grp_a_top {leaf a1 {type int8;}}"
2285 "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 +01002286 assert_non_null((parent = mod->compiled->data));
2287 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2288 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2289 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002290 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002291 assert_non_null((child = child->next));
2292 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002293 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002294 assert_non_null((child = child->next));
2295 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002296 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002297 assert_non_null((child = child->next));
2298 assert_string_equal("y", child->name);
2299 assert_non_null(child->iffeatures);
2300 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2301 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2302 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2303 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2304 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2305 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2306
2307 /* make the imported module implemented and enable the feature */
2308 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2309 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2310 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2311 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2312 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002313
Radek Krejci00b874b2019-02-12 10:54:50 +01002314 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {when 1; type string;}}}");
2315 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp {when 2;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002316 assert_non_null(mod->compiled->data);
2317 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2318 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002319 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Radek Krejcib1b59152019-01-07 13:21:56 +01002320
Radek Krejci7f6a3812019-01-07 13:48:57 +01002321 logbuf_clean();
2322 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2323 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2324 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002325 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2326 assert_string_equal("l", mod->compiled->data->name);
2327 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2328 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2329 assert_string_equal("k", mod->compiled->data->next->name);
2330 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2331 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2332
Radek Krejci3641f562019-02-13 15:38:40 +01002333 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}"
2334 "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2335 assert_non_null(mod->compiled->data);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002336 assert_non_null(child = lysc_node_children(mod->compiled->data, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002337 assert_string_equal("g", child->name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002338 assert_non_null(child = lysc_node_children(child, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002339 assert_string_equal("x", child->name);
2340
Radek Krejcie86bf772018-12-14 11:39:53 +01002341 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002342 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 +01002343 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2344
Radek Krejci096235c2019-01-11 11:12:19 +01002345 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002346 "grouping grp {leaf a{type string;}uses grp1;}"
2347 "grouping grp1 {leaf b {type string;}uses grp2;}"
2348 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002349 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2350
Radek Krejci096235c2019-01-11 11:12:19 +01002351 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 +01002352 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2353
Radek Krejci096235c2019-01-11 11:12:19 +01002354 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 +01002355 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002356 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +01002357
Radek Krejci096235c2019-01-11 11:12:19 +01002358 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 +01002359 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002360 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2361
Radek Krejci95710c92019-02-11 15:49:55 +01002362 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2363 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002364 logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002365 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2366 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002367 logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002368
Radek Krejci3641f562019-02-13 15:38:40 +01002369
2370 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}"
2371 "leaf g {type string;}"
2372 "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2373 logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used.");
2374
2375
Radek Krejcie86bf772018-12-14 11:39:53 +01002376 *state = NULL;
2377 ly_ctx_destroy(ctx, NULL);
2378}
2379
Radek Krejci01342af2019-01-03 15:18:08 +01002380static void
2381test_refine(void **state)
2382{
2383 *state = test_refine;
2384
2385 struct ly_ctx *ctx;
2386 struct lys_module *mod;
2387 struct lysc_node *parent, *child;
2388
2389 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2390
Radek Krejci096235c2019-01-11 11:12:19 +01002391 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!;}"
2392 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2393 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2394 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2395 "leaf x {type mytype; mandatory true; must 1;}"
2396 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2397 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002398
Radek Krejcif0089082019-01-07 16:42:01 +01002399 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 +01002400 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002401 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002402 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002403 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2404 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002405 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002406 assert_non_null((parent = mod->compiled->data));
2407 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2408 assert_string_equal("c", parent->name);
2409 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2410 assert_int_equal(LYS_LEAF, child->nodetype);
2411 assert_string_equal("l", child->name);
2412 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2413 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2414 assert_non_null(child = child->next);
2415 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2416 assert_string_equal("ll", child->name);
2417 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2418 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2419 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002420 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2421 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002422 assert_non_null(child = child->next);
2423 assert_int_equal(LYS_CHOICE, child->nodetype);
2424 assert_string_equal("ch", child->name);
2425 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2426 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2427 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002428 assert_non_null(child->iffeatures);
2429 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002430 assert_non_null(child = child->next);
2431 assert_int_equal(LYS_LEAF, child->nodetype);
2432 assert_string_equal("x", child->name);
2433 assert_false(LYS_MAND_TRUE & child->flags);
2434 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002435 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2436 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002437 assert_string_equal("refined", child->dsc);
2438 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002439 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002440 assert_int_equal(LYS_ANYDATA, child->nodetype);
2441 assert_string_equal("a", child->name);
2442 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002443 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2444 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002445 assert_non_null(child->iffeatures);
2446 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002447 assert_string_equal("refined", child->dsc);
2448 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002449 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002450 assert_int_equal(LYS_CONTAINER, child->nodetype);
2451 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002452 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002453 assert_true(LYS_CONFIG_W & child->flags);
2454 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002455
2456 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 +01002457 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002458 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002459 assert_int_equal(LYS_LEAF, child->nodetype);
2460 assert_string_equal("x", child->name);
2461 assert_false(LYS_MAND_TRUE & child->flags);
2462 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2463
2464 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002465 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002466 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002467 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2468
Radek Krejci096235c2019-01-11 11:12:19 +01002469 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002470 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002471 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2472
Radek Krejci096235c2019-01-11 11:12:19 +01002473 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002474 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002475 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2476
Radek Krejci096235c2019-01-11 11:12:19 +01002477 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002478 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002479 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2480
Radek Krejci096235c2019-01-11 11:12:19 +01002481 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002482 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002483 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002484 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002485 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002486 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2487
Radek Krejci096235c2019-01-11 11:12:19 +01002488 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002489 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejcif1421c22019-02-19 13:05:20 +01002490 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" under the default case.");
Radek Krejci01342af2019-01-03 15:18:08 +01002491
Radek Krejci096235c2019-01-11 11:12:19 +01002492 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002493 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002494 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2495
Radek Krejci096235c2019-01-11 11:12:19 +01002496 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002497 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002498 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2499
Radek Krejci096235c2019-01-11 11:12:19 +01002500 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 +01002501 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002502 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2503
Radek Krejci096235c2019-01-11 11:12:19 +01002504 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002505 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002506 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2507
Radek Krejci096235c2019-01-11 11:12:19 +01002508 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002509 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002510 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2511
Radek Krejci096235c2019-01-11 11:12:19 +01002512 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002513 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002514 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2515
Radek Krejci096235c2019-01-11 11:12:19 +01002516 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002517 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002518 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2519
Radek Krejci01342af2019-01-03 15:18:08 +01002520 *state = NULL;
2521 ly_ctx_destroy(ctx, NULL);
2522}
Radek Krejcie86bf772018-12-14 11:39:53 +01002523
Radek Krejci95710c92019-02-11 15:49:55 +01002524static void
2525test_augment(void **state)
2526{
2527 *state = test_augment;
2528
2529 struct ly_ctx *ctx;
2530 struct lys_module *mod;
2531 const struct lysc_node *node;
2532 const struct lysc_node_choice *ch;
2533 const struct lysc_node_case *c;
Radek Krejcif538ce52019-03-05 10:46:14 +01002534 const struct lysc_action *rpc;
Radek Krejci95710c92019-02-11 15:49:55 +01002535
2536 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2537
2538 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2539 "container top {leaf a {type string;}}}");
2540 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2541 "leaf b {type a:atype;}}", LYS_IN_YANG));
2542 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2543 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2544 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2545 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2546 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2547 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2548 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2549 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2550 assert_non_null(node = mod->compiled->data);
2551 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002552 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002553 assert_string_equal(node->name, "a");
2554 assert_non_null(node = node->next);
2555 assert_string_equal(node->name, "c");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002556 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002557 assert_string_equal(node->name, "c");
2558 assert_non_null(node = node->next);
2559 assert_string_equal(node->name, "d");
2560 assert_non_null(node = node->next);
2561 assert_string_equal(node->name, "c");
2562
2563 assert_non_null((mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;choice ch {leaf a {type string;}}"
Radek Krejci00b874b2019-02-12 10:54:50 +01002564 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2565 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002566 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2567 assert_null(mod->compiled->data->next);
2568 assert_string_equal("ch", ch->name);
2569 assert_non_null(c = ch->cases);
2570 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002571 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002572 assert_string_equal("a", c->child->name);
2573 assert_non_null(c = (const struct lysc_node_case*)c->next);
2574 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002575 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002576 assert_string_equal("b", c->child->name);
2577 assert_non_null(c = (const struct lysc_node_case*)c->next);
2578 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002579 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002580 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002581 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002582 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002583 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002584 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2585 assert_null(c->next);
2586
2587 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2588 "container c;"
2589 "augment /c {uses g;}}", LYS_IN_YANG)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002590 assert_non_null(node = lysc_node_children(mod->compiled->data, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002591 assert_string_equal(node->name, "a");
2592
Radek Krejci339f5292019-02-11 16:42:34 +01002593 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2594 "augment /c {container sub;}}");
2595 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2596 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2597 assert_non_null(mod->compiled->data);
2598 assert_string_equal("c", mod->compiled->data->name);
2599 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2600 assert_string_equal("sub", node->name);
2601 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2602 assert_string_equal("main", node->name);
2603
Radek Krejcif538ce52019-03-05 10:46:14 +01002604 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module himp {namespace urn:hi;prefix hi;container top; rpc func;}");
Radek Krejci733988a2019-02-15 15:12:44 +01002605 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;"
2606 "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2607 "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}"
2608 "augment /hi:top {leaf l {type string; mandatory true; config false;}}"
2609 "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG));
2610 assert_non_null(node = mod->compiled->data);
2611 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2612 assert_string_equal("l", node->name);
2613 assert_true(node->flags & LYS_MAND_TRUE);
2614 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
Radek Krejcife909632019-02-12 15:34:42 +01002615 assert_non_null(node = mod->compiled->data);
2616 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2617 assert_string_equal("p", node->name);
Radek Krejci733988a2019-02-15 15:12:44 +01002618 assert_non_null(node = node->next);
2619 assert_string_equal("ll", node->name);
2620 assert_non_null(node = node->next);
2621 assert_string_equal("l", node->name);
2622 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejcife909632019-02-12 15:34:42 +01002623
Radek Krejcif538ce52019-03-05 10:46:14 +01002624 assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}"
2625 "augment /hi:func/input {leaf x {type string;}}"
2626 "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG));
2627 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
2628 assert_non_null(rpc = mod->compiled->rpcs);
2629 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
2630 assert_non_null(rpc->input.data);
2631 assert_string_equal("x", rpc->input.data->name);
2632 assert_null(rpc->input.data->next);
2633 assert_non_null(rpc->output.data);
2634 assert_string_equal("y", rpc->output.data->name);
2635 assert_null(rpc->output.data->next);
2636
Radek Krejci95710c92019-02-11 15:49:55 +01002637 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2638 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
2639 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found.");
2640
2641 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2642 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002643 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002644
2645
Radek Krejci339f5292019-02-11 16:42:34 +01002646 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2647 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
2648 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target.");
2649
2650 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2651 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
2652 logbuf_assert("Invalid augment (/c) of container node which is not allowed to contain case node \"b\".");
2653
Radek Krejci733988a2019-02-15 15:12:44 +01002654 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}"
2655 "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
2656 logbuf_assert("Invalid augment (/hi:top) adding mandatory node \"c\" without making it conditional via when statement.");
Radek Krejcife909632019-02-12 15:34:42 +01002657
Radek Krejci3641f562019-02-13 15:38:40 +01002658 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;"
2659 "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG));
2660 logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\".");
Radek Krejci95710c92019-02-11 15:49:55 +01002661
Radek Krejcif538ce52019-03-05 10:46:14 +01002662 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;"
2663 "augment /func {leaf x {type int8;}}}", LYS_IN_YANG));
2664 logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC/action node which is not an allowed augment's target.");
2665
Radek Krejci95710c92019-02-11 15:49:55 +01002666 *state = NULL;
2667 ly_ctx_destroy(ctx, NULL);
2668}
2669
Radek Krejciccd20f12019-02-15 14:12:27 +01002670static void
2671test_deviation(void **state)
2672{
2673 *state = test_deviation;
2674
2675 struct ly_ctx *ctx;
2676 struct lys_module *mod;
2677 const struct lysc_node *node;
Radek Krejci7af64242019-02-18 13:07:53 +01002678 const struct lysc_node_list *list;
Radek Krejciccd20f12019-02-15 14:12:27 +01002679
2680 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2681
2682 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002683 "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2684 "choice ch {default c; case b {leaf b{type string;}} case a {leaf a{type string;} leaf x {type string;}}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002685 " case c {leaf c{type string;}}}"
2686 "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}"
2687 "rpc func2;}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002688 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002689 "deviation /a:top/a:b {deviate not-supported;}"
2690 "deviation /a:ch/a:a/a:x {deviate not-supported;}"
2691 "deviation /a:ch/a:c/a:c {deviate not-supported;}"
2692 "deviation /a:ch/a:b {deviate not-supported;}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002693 "deviation /a:ch/a:a/a:a {deviate not-supported;}"
2694 "deviation /a:func1/a:input {deviate not-supported;}"
2695 "deviation /a:func1/a:output {deviate not-supported;}"
2696 "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002697 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2698 assert_non_null(node = mod->compiled->data);
2699 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002700 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejciccd20f12019-02-15 14:12:27 +01002701 assert_string_equal(node->name, "a");
2702 assert_non_null(node = node->next);
2703 assert_string_equal(node->name, "c");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002704 assert_null(node = node->next);
2705 assert_non_null(node = mod->compiled->data->next);
2706 assert_string_equal("ch", node->name);
2707 assert_null(((struct lysc_node_choice*)node)->dflt);
2708 assert_null(((struct lysc_node_choice*)node)->cases);
Radek Krejcif538ce52019-03-05 10:46:14 +01002709 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs));
2710 assert_null(mod->compiled->rpcs[0].input.data);
2711 assert_null(mod->compiled->rpcs[0].output.data);
Radek Krejciccd20f12019-02-15 14:12:27 +01002712
2713 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}"
2714 "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}"
2715 "deviation /c1 {deviate add {units meters;}}"
2716 "deviation /c2 {deviate delete {units meters;}}"
2717 "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG));
2718 assert_non_null(node = mod->compiled->data);
2719 assert_string_equal("c1", node->name);
2720 assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units);
2721 assert_non_null(node = node->next);
2722 assert_string_equal("c2", node->name);
2723 assert_null(((struct lysc_node_leaf*)node)->units);
2724 assert_non_null(node = node->next);
2725 assert_string_equal("c3", node->name);
2726 assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units);
2727
2728 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; leaf c1 {type string; must 1;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002729 "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002730 "deviation /c1 {deviate add {must 3;}}"
2731 "deviation /c2 {deviate delete {must 2;}}"
2732 "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG));
2733 assert_non_null(node = mod->compiled->data);
2734 assert_string_equal("c1", node->name);
2735 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts));
2736 assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr);
2737 assert_non_null(node = node->next);
2738 assert_string_equal("c2", node->name);
Radek Krejcib4532d12019-02-15 16:13:29 +01002739 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts));
2740 assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr);
Radek Krejciccd20f12019-02-15 14:12:27 +01002741 assert_non_null(node = node->next);
2742 assert_string_equal("c3", node->name);
2743 assert_null(((struct lysc_node_leaf*)node)->musts);
2744
Radek Krejcib4532d12019-02-15 16:13:29 +01002745 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module e {yang-version 1.1; namespace urn:e;prefix e; typedef mytype {type string; default nothing;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01002746 "choice a {default a;leaf a {type string;} leaf b {type string;} leaf c {type string; mandatory true;}}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002747 "choice b {default a;leaf a {type string;} leaf b {type string;}}"
2748 "leaf c {default hello; type string;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002749 "leaf-list d {default hello; default world; type string;}"
2750 "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002751 assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}"
2752 "deviation /x:a {deviate delete {default a;}}"
2753 "deviation /x:b {deviate delete {default x:a;}}"
2754 "deviation /x:c {deviate delete {default hello;}}"
2755 "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG));
2756 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2757 assert_non_null(node = mod->compiled->data);
2758 assert_null(((struct lysc_node_choice*)node)->dflt);
2759 assert_non_null(node = node->next);
2760 assert_null(((struct lysc_node_choice*)node)->dflt);
2761 assert_non_null(node = node->next);
2762 assert_null(((struct lysc_node_leaf*)node)->dflt);
2763 assert_non_null(node = node->next);
2764 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2765 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002766 assert_non_null(node = node->next);
2767 assert_string_equal("nothing", ((struct lysc_node_leaf*)node)->dflt);
2768 assert_non_null(node = node->next);
2769 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2770 assert_string_equal("nothing", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002771
2772 assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}"
2773 "deviation /x:b {deviate add {default x:b;}}"
2774 "deviation /x:c {deviate add {default bye;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002775 "deviation /x:d {deviate add {default all; default people;}}"
2776 "deviation /x:c2 {deviate add {default hi;}}"
2777 "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002778 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2779 assert_non_null(node = mod->compiled->data);
2780 assert_null(((struct lysc_node_choice*)node)->dflt);
2781 assert_non_null(node = node->next);
2782 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2783 assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name);
2784 assert_non_null(node = node->next);
2785 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2786 assert_string_equal("bye", ((struct lysc_node_leaf*)node)->dflt);
2787 assert_non_null(node = node->next);
2788 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2789 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
2790 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
2791 assert_string_equal("people", ((struct lysc_node_leaflist*)node)->dflts[2]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002792 assert_non_null(node = node->next);
2793 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2794 assert_string_equal("hi", ((struct lysc_node_leaf*)node)->dflt);
2795 assert_non_null(node = node->next);
2796 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2797 assert_string_equal("hi", ((struct lysc_node_leaflist*)node)->dflts[0]);
2798 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002799
2800 assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}"
2801 "deviation /x:b {deviate replace {default x:a;}}"
2802 "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG));
2803 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2804 assert_non_null(node = mod->compiled->data);
2805 assert_null(((struct lysc_node_choice*)node)->dflt);
2806 assert_non_null(node = node->next);
2807 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2808 assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name);
2809 assert_non_null(node = node->next);
2810 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2811 assert_string_equal("hello", ((struct lysc_node_leaf*)node)->dflt);
2812
Radek Krejci7af64242019-02-18 13:07:53 +01002813 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;"
2814 "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2815 "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}"
2816 " leaf c {type string;} leaf d {type string;}}}");
2817 assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}"
2818 "augment /i:l1 {leaf j_c {type string;}}"
2819 "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}"
2820 "deviation /i:l1 {deviate add {unique \"i:c\";}}"
2821 "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG));
2822 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i")));
2823 assert_non_null(list = (struct lysc_node_list*)mod->compiled->data);
2824 assert_string_equal("l1", list->name);
2825 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
2826 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
2827 assert_string_equal("b", list->uniques[0][0]->name);
2828 assert_string_equal("j_c", list->uniques[0][1]->name);
2829 assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1]));
2830 assert_string_equal("c", list->uniques[1][0]->name);
2831 assert_non_null(list = (struct lysc_node_list*)list->next);
2832 assert_string_equal("l2", list->name);
2833 assert_null(list->uniques);
2834
Radek Krejci93dcc392019-02-19 10:43:38 +01002835 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}"
2836 "container top {leaf x {type string;} leaf y {type string; config false;}}"
2837 "deviation /a {deviate add {config false; }}"
2838 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
2839 assert_non_null(node = mod->compiled->data);
2840 assert_string_equal("a", node->name);
2841 assert_true(node->flags & LYS_CONFIG_R);
2842 assert_non_null(node = node->next);
2843 assert_string_equal("top", node->name);
2844 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002845 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002846 assert_string_equal("x", node->name);
2847 assert_true(node->flags & LYS_CONFIG_R);
2848 assert_non_null(node = node->next);
2849 assert_string_equal("y", node->name);
2850 assert_true(node->flags & LYS_CONFIG_R);
2851
2852 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}"
2853 "container top {config false; leaf x {type string;}}"
2854 "deviation /a {deviate replace {config true;}}"
2855 "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG));
2856 assert_non_null(node = mod->compiled->data);
2857 assert_string_equal("a", node->name);
2858 assert_true(node->flags & LYS_CONFIG_W);
2859 assert_non_null(node = node->next);
2860 assert_string_equal("top", node->name);
2861 assert_true(node->flags & LYS_CONFIG_W);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002862 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002863 assert_string_equal("x", node->name);
2864 assert_true(node->flags & LYS_CONFIG_W);
2865
Radek Krejcif1421c22019-02-19 13:05:20 +01002866 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;"
2867 "container a {leaf a {type string;}}"
2868 "container b {leaf b {mandatory true; type string;}}"
2869 "deviation /a/a {deviate add {mandatory true;}}"
2870 "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG));
2871 assert_non_null(node = mod->compiled->data);
2872 assert_string_equal("a", node->name);
2873 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002874 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002875 assert_non_null(node = node->next);
2876 assert_string_equal("b", node->name);
2877 assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002878 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002879
Radek Krejci551b12c2019-02-19 16:11:21 +01002880 assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;"
2881 "leaf a {default test; type string;}"
2882 "leaf b {mandatory true; type string;}"
2883 "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}"
2884 "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG));
2885 assert_non_null(node = mod->compiled->data);
2886 assert_string_equal("a", node->name);
2887 assert_null(((struct lysc_node_leaf*)node)->dflt);
2888 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
2889 assert_non_null(node = node->next);
2890 assert_string_equal("b", node->name);
2891 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2892 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
2893
2894 assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;"
2895 "leaf-list a {type string;}"
2896 "list b {config false;}"
2897 "leaf-list c {min-elements 1; max-elements 10; type string;}"
2898 "list d {min-elements 10; max-elements 100; config false;}"
2899 "deviation /a {deviate add {min-elements 1; max-elements 10;}}"
2900 "deviation /b {deviate add {min-elements 10; max-elements 100;}}"
2901 "deviation /c {deviate replace {min-elements 10; max-elements 100;}}"
2902 "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG));
2903 assert_non_null(node = mod->compiled->data);
2904 assert_string_equal("a", node->name);
2905 assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min);
2906 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max);
2907 assert_non_null(node = node->next);
2908 assert_string_equal("b", node->name);
2909 assert_int_equal(10, ((struct lysc_node_list*)node)->min);
2910 assert_int_equal(100, ((struct lysc_node_list*)node)->max);
2911 assert_non_null(node = node->next);
2912 assert_string_equal("c", node->name);
2913 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min);
2914 assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max);
2915 assert_non_null(node = node->next);
2916 assert_string_equal("d", node->name);
2917 assert_int_equal(1, ((struct lysc_node_list*)node)->min);
2918 assert_int_equal(10, ((struct lysc_node_list*)node)->max);
2919
Radek Krejci33f72892019-02-21 10:36:58 +01002920 assert_non_null(mod = lys_parse_mem(ctx, "module p {yang-version 1.1; namespace urn:p;prefix p; typedef mytype {type int8; default 1;}"
2921 "leaf a {type string; default 10;} leaf-list b {type string;}"
2922 "deviation /a {deviate replace {type mytype;}}"
2923 "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG));
2924 assert_non_null(node = mod->compiled->data);
2925 assert_string_equal("a", node->name);
2926 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf*)node)->type->basetype);
2927 assert_string_equal("10", ((struct lysc_node_leaf*)node)->dflt);
2928 assert_non_null(node = node->next);
2929 assert_string_equal("b", node->name);
2930 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaflist*)node)->type->basetype);
2931 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2932 assert_string_equal("1", ((struct lysc_node_leaflist*)node)->dflts[0]);
2933
Radek Krejci88ef64b2019-02-18 16:02:06 +01002934 assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002935 "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG));
2936 logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002937 assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}"
2938 "deviation /a:top/a:a {deviate not-supported;}"
2939 "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG));
2940 /* warning */
2941 logbuf_assert("Useless multiple (2) deviates on node \"/a:top/a:a\" since the node is not-supported.");
Radek Krejciccd20f12019-02-15 14:12:27 +01002942
2943 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}"
2944 "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG));
2945 logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\".");
2946
2947 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;"
2948 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
2949 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to add \"units\" property.");
2950 assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}"
2951 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
2952 logbuf_assert("Invalid deviation (/c) adding \"units\" property which already exists (with value \"centimeters\").");
2953
2954 assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;"
2955 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
2956 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to delete \"units\" property.");
2957 assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}"
2958 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
2959 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which is not present.");
2960 assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}"
2961 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
2962 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\".");
2963
2964 assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;"
2965 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
2966 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to replace \"units\" property.");
2967 assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}"
2968 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
2969 logbuf_assert("Invalid deviation (/c) replacing \"units\" property \"meters\" which is not present.");
2970
2971 /* the default is already deleted in /e:a byt module f */
2972 assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}"
2973 "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG));
2974 logbuf_assert("Invalid deviation (/e:a) deleting \"default\" property \"x:a\" which is not present.");
2975 assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}"
2976 "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG));
2977 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"x:a\" of choice. "
2978 "The prefix does not match any imported module of the deviation module.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002979 assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}"
2980 "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG));
2981 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\".");
2982 assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}"
2983 "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG));
2984 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module.");
2985 assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;"
2986 "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG));
2987 logbuf_assert("Invalid deviation (/a) of anyxml node - it is not possible to delete \"default\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01002988
2989 assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}"
2990 "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG));
2991 logbuf_assert("Invalid deviation (/e:b) adding \"default\" property which already exists (with value \"a\").");
2992 assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01002993 "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG));
2994 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"x:a\" of choice. "
2995 "The prefix does not match any imported module of the deviation module.");
2996 assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}"
2997 "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG));
2998 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"a\" of choice - the specified case does not exists.");
2999 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003000 "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG));
3001 logbuf_assert("Invalid deviation (/e:c) adding \"default\" property which already exists (with value \"hello\").");
Radek Krejci468a94f2019-02-15 14:52:36 +01003002 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
3003 "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG));
3004 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"e:c\" of choice - mandatory node \"c\" under the default case.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003005 assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}"
3006 "deviation /x {deviate add {default error;}}}", LYS_IN_YANG));
Radek Krejci551b12c2019-02-19 16:11:21 +01003007 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003008
3009 assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}"
3010 "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG));
3011 logbuf_assert("Invalid deviation (/e:d) of leaf-list node - it is not possible to replace \"default\" property.");
3012
Radek Krejci7af64242019-02-18 13:07:53 +01003013 assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}"
3014 "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG));
3015 logbuf_assert("Invalid deviation (/i:l1) deleting \"unique\" property \"x\" which does not match any of the target's property values.");
3016 assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}"
3017 "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG));
3018 logbuf_assert("Invalid deviation (/i:l2) deleting \"unique\" property \"d\" which does not match any of the target's property values.");
3019 assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}"
3020 "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG));
3021 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to delete \"unique\" property.");
3022 assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}"
3023 "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG));
3024 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to add \"unique\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003025
Radek Krejci93dcc392019-02-19 10:43:38 +01003026 assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}"
3027 "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG));
3028 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to add \"config\" property.");
3029 assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}"
3030 "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG));
3031 logbuf_assert("Invalid deviation of config in \"/top/x\" - configuration node cannot be child of any state data node.");
3032 assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}"
3033 "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG));
3034 logbuf_assert("Invalid deviation (/top/x) replacing \"config\" property \"config false\" which is not present.");
3035 assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}"
3036 "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG));
3037 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to replace \"config\" property.");
3038 assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}"
3039 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
3040 logbuf_assert("Invalid deviation of config in \"/top\" - configuration node cannot be child of any state data node.");
Radek Krejcif1421c22019-02-19 13:05:20 +01003041 assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}"
3042 "deviation /x {deviate add {config true;}}}", LYS_IN_YANG));
3043 logbuf_assert("Invalid deviation (/x) adding \"config\" property which already exists (with value \"config false\").");
3044
3045 assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}"
3046 "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG));
3047 logbuf_assert("Invalid deviation of mandatory in \"/top\" - container cannot hold mandatory statement.");
3048 assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}"
3049 "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG));
3050 logbuf_assert("Invalid deviation (/top) replacing \"mandatory\" property \"mandatory true\" which is not present.");
3051 assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}"
3052 "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG));
3053 logbuf_assert("Invalid deviation (/top/x) replacing \"mandatory\" property \"mandatory true\" which is not present.");
3054 assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}"
3055 "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG));
3056 logbuf_assert("Invalid deviation (/x) adding \"mandatory\" property which already exists (with value \"mandatory true\").");
Radek Krejci93dcc392019-02-19 10:43:38 +01003057
Radek Krejci551b12c2019-02-19 16:11:21 +01003058 assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}"
3059 "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG));
3060 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
3061 assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}"
3062 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3063 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf-list.");
3064 assert_null(lys_parse_mem(ctx, "module ll2 {namespace urn:ll2;prefix ll2; choice ch {default a; leaf a {type string;} leaf b {type string;}}"
3065 "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG));
3066 logbuf_assert("Invalid deviation (/ch) combining default case and mandatory choice.");
3067
3068 assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}"
3069 "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG));
3070 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 10 is bigger than max value 5.");
3071 assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}"
3072 "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG));
3073 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
3074 assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}"
3075 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3076 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 5 is bigger than max value 1.");
3077 assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}"
3078 "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG));
3079 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
3080 assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}"
3081 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3082 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
3083 assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}"
3084 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3085 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
3086 assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}"
3087 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
3088 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3089 assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}"
3090 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
3091 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3092 assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}"
3093 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3094 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3095 assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}"
3096 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3097 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3098 assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}"
3099 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3100 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3101 assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }"
3102 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3103 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3104
Radek Krejci33f72892019-02-21 10:36:58 +01003105 assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;"
3106 "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG));
3107 logbuf_assert("Invalid deviation (/x) of anyxml node - it is not possible to replace \"type\" property.");
3108 assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}"
3109 "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG));
3110 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3111
Radek Krejciccd20f12019-02-15 14:12:27 +01003112 *state = NULL;
3113 ly_ctx_destroy(ctx, NULL);
3114}
3115
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003116int main(void)
3117{
3118 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01003119 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
3120 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
3121 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01003122 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
3123 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01003124 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01003125 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
3126 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01003127 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01003128 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01003129 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01003130 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01003131 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01003132 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
3133 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01003134 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01003135 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01003136 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003137 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01003138 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01003139 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01003140 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02003141 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01003142 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01003143 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01003144 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejciccd20f12019-02-15 14:12:27 +01003145 cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003146 };
3147
3148 return cmocka_run_group_tests(tests, NULL, NULL);
3149}