blob: 8f50ead0236c2ce70174c68f3f7e00b2869fac49 [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
524 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
525 * TODO test also, RPC output parameters and notification content */
526 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
527 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100528 /* but warning is present: */
529 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
530 assert_non_null(mod->compiled);
531 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
Radek Krejci93dcc392019-02-19 10:43:38 +0100532 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100533
534 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100535 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 +0100536 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
537
Radek Krejci096235c2019-01-11 11:12:19 +0100538 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 +0100539 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
540
541 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
542 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100543 assert_non_null(mod->compiled);
544 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
545 assert_non_null(ll->dflts);
546 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100547 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
548 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100549 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
550
Radek Krejci0e5d8382018-11-28 16:37:53 +0100551 *state = NULL;
552 ly_ctx_destroy(ctx, NULL);
553}
554
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100555static void
556test_node_list(void **state)
557{
558 *state = test_node_list;
559
560 struct ly_ctx *ctx;
561 struct lys_module *mod;
562 struct lysc_node_list *list;
563
564 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
565
566 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
567 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
568 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100569 list = (struct lysc_node_list*)mod->compiled->data;
570 assert_non_null(list);
571 assert_non_null(list->keys);
572 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
573 assert_string_equal("x", list->keys[0]->name);
574 assert_string_equal("y", list->keys[1]->name);
575 assert_non_null(list->child);
576 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
577 assert_true(list->child->flags & LYS_KEY);
578 assert_true(list->child->next->flags & LYS_KEY);
579 list = (struct lysc_node_list*)mod->compiled->data->next;
580 assert_non_null(list);
581 assert_null(list->keys);
582 assert_non_null(list->child);
Radek Krejci93dcc392019-02-19 10:43:38 +0100583 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, list->flags);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100584 assert_false(list->child->flags & LYS_KEY);
585
586 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
587 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100588 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100589 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100590 list = (struct lysc_node_list*)mod->compiled->data;
591 assert_non_null(list);
592 assert_string_equal("l", list->name);
593 assert_non_null(list->keys);
594 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
595 assert_string_equal("a", list->keys[0]->name);
596 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100597 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100598 assert_non_null(list->uniques);
599 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
600 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
601 assert_string_equal("a", list->uniques[0][0]->name);
602 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
603 assert_string_equal("b", list->uniques[0][1]->name);
604 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
605 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
606 assert_string_equal("e", list->uniques[1][0]->name);
607 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
608 assert_string_equal("d", list->uniques[1][1]->name);
609 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
610
Radek Krejci665421c2018-12-05 08:03:39 +0100611 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
612 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100613 list = (struct lysc_node_list*)mod->compiled->data;
614 assert_non_null(list);
615 assert_string_equal("l", list->name);
616 assert_non_null(list->keys);
617 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
618 assert_string_equal("a", list->keys[0]->name);
619 assert_true(list->keys[0]->flags & LYS_KEY);
620 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
621
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100622 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100623 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 +0100624 logbuf_assert("Missing key in list representing configuration data.");
625
Radek Krejci096235c2019-01-11 11:12:19 +0100626 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
627 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100628 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
629
Radek Krejci096235c2019-01-11 11:12:19 +0100630 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
631 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100632 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
633
Radek Krejci096235c2019-01-11 11:12:19 +0100634 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
635 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100636 logbuf_assert("Key of the configuration list must not be status leaf.");
637
Radek Krejci096235c2019-01-11 11:12:19 +0100638 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
639 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100640 logbuf_assert("Configuration node cannot be child of any state data node.");
641
Radek Krejci096235c2019-01-11 11:12:19 +0100642 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
643 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100644 logbuf_assert("The list's key \"x\" not found.");
645
Radek Krejci096235c2019-01-11 11:12:19 +0100646 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
647 "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 +0100648 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100649
Radek Krejci096235c2019-01-11 11:12:19 +0100650 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
651 "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 +0100652 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
653
Radek Krejci096235c2019-01-11 11:12:19 +0100654 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
655 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100656 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
657
Radek Krejci096235c2019-01-11 11:12:19 +0100658 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
659 "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 +0100660 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
661
Radek Krejci096235c2019-01-11 11:12:19 +0100662 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
663 "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 +0100664 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
665
Radek Krejci096235c2019-01-11 11:12:19 +0100666 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
667 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100668 logbuf_assert("Duplicated key identifier \"x\".");
669
Radek Krejci096235c2019-01-11 11:12:19 +0100670 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
671 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100672 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
673
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100674 *state = NULL;
675 ly_ctx_destroy(ctx, NULL);
676}
677
Radek Krejci056d0a82018-12-06 16:57:25 +0100678static void
679test_node_choice(void **state)
680{
681 *state = test_node_choice;
682
683 struct ly_ctx *ctx;
684 struct lys_module *mod;
685 struct lysc_node_choice *ch;
686 struct lysc_node_case *cs;
687
688 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
689
690 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100691 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
692 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100693 ch = (struct lysc_node_choice*)mod->compiled->data;
694 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100695 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100696 cs = ch->cases;
697 assert_non_null(cs);
698 assert_string_equal("a", cs->name);
699 assert_ptr_equal(ch, cs->parent);
700 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100701 assert_string_equal("a1", cs->child->name);
702 assert_non_null(cs->child->next);
703 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100704 assert_ptr_equal(cs, cs->child->parent);
705 cs = (struct lysc_node_case*)cs->next;
706 assert_non_null(cs);
707 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100708 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100709 assert_ptr_equal(ch, cs->parent);
710 assert_non_null(cs->child);
711 assert_string_equal("b", cs->child->name);
712 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100713 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
714 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100715 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100716 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100717
Radek Krejci096235c2019-01-11 11:12:19 +0100718 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
719 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100720 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100721 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
722 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100723 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100724 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
725 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100726 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100727 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
728 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100729 logbuf_assert("Duplicate identifier \"b\" of case statement.");
730
Radek Krejci096235c2019-01-11 11:12:19 +0100731 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
732 "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 +0100733 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100734 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
735 "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 +0100736 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100737 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
738 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100739 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
740 /* TODO check with mandatory nodes from augment placed into the case */
741
Radek Krejci056d0a82018-12-06 16:57:25 +0100742 *state = NULL;
743 ly_ctx_destroy(ctx, NULL);
744}
745
Radek Krejci9800fb82018-12-13 14:26:23 +0100746static void
747test_node_anydata(void **state)
748{
749 *state = test_node_anydata;
750
751 struct ly_ctx *ctx;
752 struct lys_module *mod;
753 struct lysc_node_anydata *any;
754
755 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
756
757 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
758 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100759 any = (struct lysc_node_anydata*)mod->compiled->data;
760 assert_non_null(any);
761 assert_int_equal(LYS_ANYDATA, any->nodetype);
Radek Krejci93dcc392019-02-19 10:43:38 +0100762 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags);
Radek Krejci9800fb82018-12-13 14:26:23 +0100763
764 logbuf_clean();
765 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
766 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100767 any = (struct lysc_node_anydata*)mod->compiled->data;
768 assert_non_null(any);
769 assert_int_equal(LYS_ANYXML, any->nodetype);
770 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
771 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
772
773 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100774 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 +0100775 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
776
777 *state = NULL;
778 ly_ctx_destroy(ctx, NULL);
779}
780
Radek Krejcif538ce52019-03-05 10:46:14 +0100781static void
782test_action(void **state)
783{
784 *state = test_action;
785
786 struct ly_ctx *ctx;
787 struct lys_module *mod;
788 const struct lysc_action *rpc;
789
790 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
791
792 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
793 "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG));
794 rpc = mod->compiled->rpcs;
795 assert_non_null(rpc);
796 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
797 assert_int_equal(LYS_ACTION, rpc->nodetype);
798 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
799 assert_string_equal("a", rpc->name);
800
801 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
802 "action b {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}}", LYS_IN_YANG));
803 rpc = lysc_node_actions(mod->compiled->data);
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("b", rpc->name);
809
810 /* invalid */
811 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG));
812 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
813
Radek Krejci8cce8532019-03-05 11:27:45 +0100814 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG));
815 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
816 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));
817 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
818 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));
819 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
820 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}");
821 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG));
822 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
823
Radek Krejcif538ce52019-03-05 10:46:14 +0100824 *state = NULL;
825 ly_ctx_destroy(ctx, NULL);
826}
827
Radek Krejci0f07bed2018-11-13 14:01:40 +0100828/**
829 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
830 * do not test the expression itself
831 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100832static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100833test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100834{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100835 *state = test_type_range;
836
837 struct ly_ctx *ctx;
838 struct lys_module *mod;
839 struct lysc_type *type;
840
841 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
842
843 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 +0100844 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
845 assert_non_null(type);
846 assert_int_equal(LY_TYPE_INT8, type->basetype);
847 assert_non_null(((struct lysc_type_num*)type)->range);
848 assert_non_null(((struct lysc_type_num*)type)->range->parts);
849 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
850 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
851 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
852 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
853 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
854
855 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 +0100856 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
857 assert_non_null(type);
858 assert_int_equal(LY_TYPE_INT16, type->basetype);
859 assert_non_null(((struct lysc_type_num*)type)->range);
860 assert_non_null(((struct lysc_type_num*)type)->range->parts);
861 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
862 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
863 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
864 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
865 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
866
867 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 +0100868 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
869 assert_non_null(type);
870 assert_int_equal(LY_TYPE_INT32, type->basetype);
871 assert_non_null(((struct lysc_type_num*)type)->range);
872 assert_non_null(((struct lysc_type_num*)type)->range->parts);
873 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
874 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
875 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
876 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
877 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
878
879 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 +0100880 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
881 assert_non_null(type);
882 assert_int_equal(LY_TYPE_INT64, type->basetype);
883 assert_non_null(((struct lysc_type_num*)type)->range);
884 assert_non_null(((struct lysc_type_num*)type)->range->parts);
885 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
886 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
887 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
888 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
889 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
890
891 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 +0100892 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
893 assert_non_null(type);
894 assert_int_equal(LY_TYPE_UINT8, type->basetype);
895 assert_non_null(((struct lysc_type_num*)type)->range);
896 assert_non_null(((struct lysc_type_num*)type)->range->parts);
897 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
898 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
899 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
900 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
901 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
902
903 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 +0100904 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
905 assert_non_null(type);
906 assert_int_equal(LY_TYPE_UINT16, type->basetype);
907 assert_non_null(((struct lysc_type_num*)type)->range);
908 assert_non_null(((struct lysc_type_num*)type)->range->parts);
909 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
910 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
911 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
912 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
913 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
914
915 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 +0100916 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
917 assert_non_null(type);
918 assert_int_equal(LY_TYPE_UINT32, type->basetype);
919 assert_non_null(((struct lysc_type_num*)type)->range);
920 assert_non_null(((struct lysc_type_num*)type)->range->parts);
921 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
922 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
923 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
924 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
925 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
926
927 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 +0100928 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
929 assert_non_null(type);
930 assert_int_equal(LY_TYPE_UINT64, type->basetype);
931 assert_non_null(((struct lysc_type_num*)type)->range);
932 assert_non_null(((struct lysc_type_num*)type)->range->parts);
933 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
934 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
935 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
936 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
937 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
938
939 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
940 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100941 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
942 assert_non_null(type);
943 assert_int_equal(3, type->refcount);
944 assert_int_equal(LY_TYPE_UINT8, type->basetype);
945 assert_non_null(((struct lysc_type_num*)type)->range);
946 assert_non_null(((struct lysc_type_num*)type)->range->parts);
947 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
948
Radek Krejcif1394042019-01-08 10:53:34 +0100949 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
950 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
951 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +0100952 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
953 assert_non_null(type);
954 assert_int_equal(1, type->refcount);
955 assert_int_equal(LY_TYPE_UINT8, type->basetype);
956 assert_non_null(((struct lysc_type_num*)type)->range);
957 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
958 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
959 assert_non_null(((struct lysc_type_num*)type)->range->parts);
960 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
961 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
962 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
963
Radek Krejci0f07bed2018-11-13 14:01:40 +0100964 *state = NULL;
965 ly_ctx_destroy(ctx, NULL);
966}
967
968static void
969test_type_length(void **state)
970{
971 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100972
973 struct ly_ctx *ctx;
974 struct lys_module *mod;
975 struct lysc_type *type;
976
977 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
978
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100979 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 +0100980 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
981 assert_non_null(type);
982 assert_non_null(((struct lysc_type_bin*)type)->length);
983 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100984 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
985 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100986 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
987 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
988 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
989
990 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 +0100991 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
992 assert_non_null(type);
993 assert_non_null(((struct lysc_type_bin*)type)->length);
994 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
995 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100996 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
997 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100998
999 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 +01001000 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1001 assert_non_null(type);
1002 assert_non_null(((struct lysc_type_bin*)type)->length);
1003 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1004 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1005 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +01001006 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001007
1008 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 +01001009 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1010 assert_non_null(type);
1011 assert_non_null(((struct lysc_type_bin*)type)->length);
1012 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1013 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1014 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1015 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1016
1017 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 +01001018 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1019 assert_non_null(type);
1020 assert_non_null(((struct lysc_type_bin*)type)->length);
1021 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1022 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1023 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1024 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1025
1026 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 +01001027 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1028 assert_non_null(type);
1029 assert_non_null(((struct lysc_type_bin*)type)->length);
1030 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1031 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1032 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1033 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1034 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1035 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1036
1037 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 +01001038 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1039 assert_non_null(type);
1040 assert_non_null(((struct lysc_type_bin*)type)->length);
1041 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1042 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1043 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1044 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1045 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1046 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1047
Radek Krejcib31c8992018-11-12 16:29:16 +01001048 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1049 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001050 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1051 assert_non_null(type);
1052 assert_non_null(((struct lysc_type_bin*)type)->length);
1053 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1054 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1055 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1056 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1057
1058 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1059 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001060 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1061 assert_non_null(type);
1062 assert_non_null(((struct lysc_type_bin*)type)->length);
1063 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1064 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1065 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1066 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1067
1068 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1069 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001070 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1071 assert_non_null(type);
1072 assert_non_null(((struct lysc_type_bin*)type)->length);
1073 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1074 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1075 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1076 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1077 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1078 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1079
1080 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 +01001081 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001082 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1083 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001084 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001085 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));
1088 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001089 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001090 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1091 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001092 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001093 assert_non_null(((struct lysc_type_bin*)type)->length);
1094 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1095 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1096 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1097 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1098
Radek Krejci56aa27c2018-11-13 14:21:59 +01001099 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1100 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001101 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1102 assert_non_null(type);
1103 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001104 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001105 assert_non_null(((struct lysc_type_str*)type)->length);
1106 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1107 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1108 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1109 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001110
Radek Krejci5969f272018-11-23 10:03:58 +01001111 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1112 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001113 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1114 assert_non_null(type);
1115 assert_int_equal(LY_TYPE_STRING, type->basetype);
1116 assert_int_equal(1, type->refcount);
1117 assert_non_null(((struct lysc_type_str*)type)->length);
1118 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1119 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1120 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1121 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1122
Radek Krejci4f28eda2018-11-12 11:46:16 +01001123 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001124 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 +01001125 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001126 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 +01001127 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001128 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 +01001129 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001130 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 +01001131 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001132 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 +01001133 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001134 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 +01001135 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001136 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 +01001137 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001138 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 +01001139 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001140 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 +01001141 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001142 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 +01001143 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001144 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 +01001145 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001146 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 +01001147 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001148
Radek Krejci096235c2019-01-11 11:12:19 +01001149 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1150 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001151 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001152 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1153 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001154 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001155 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1156 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001157 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001158 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1159 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001160 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 +01001161 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 +01001162 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001163 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001164 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 +01001165 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001166 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001167 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 +01001168 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001169 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001170
Radek Krejci096235c2019-01-11 11:12:19 +01001171 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 +01001172 logbuf_assert("Invalid type restrictions for binary type.");
1173
Radek Krejci4f28eda2018-11-12 11:46:16 +01001174 *state = NULL;
1175 ly_ctx_destroy(ctx, NULL);
1176}
1177
Radek Krejcicda74152018-11-13 15:27:32 +01001178static void
1179test_type_pattern(void **state)
1180{
1181 *state = test_type_pattern;
1182
1183 struct ly_ctx *ctx;
1184 struct lys_module *mod;
1185 struct lysc_type *type;
1186
1187 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1188
Radek Krejci027d5802018-11-14 16:57:28 +01001189 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 +01001190 "pattern .* {error-app-tag errortag;error-message error;}"
1191 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001192 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1193 assert_non_null(type);
1194 assert_non_null(((struct lysc_type_str*)type)->patterns);
1195 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1196 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1197 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1198 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1199 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1200 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1201 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1202
1203 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1204 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +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)->patterns);
1210 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1211 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1212 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1213
Radek Krejci04bc1e92018-11-14 14:28:13 +01001214 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1215 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001216 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1217 assert_non_null(type);
1218 assert_int_equal(LY_TYPE_STRING, type->basetype);
1219 assert_int_equal(1, type->refcount);
1220 assert_non_null(((struct lysc_type_str*)type)->patterns);
1221 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1222 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1223
Radek Krejcicda74152018-11-13 15:27:32 +01001224 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001225 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 +01001226 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001227 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1228 assert_non_null(type);
1229 assert_non_null(((struct lysc_type_str*)type)->patterns);
1230 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1231 /* TODO check some data "^Å™$" */
1232
1233 *state = NULL;
1234 ly_ctx_destroy(ctx, NULL);
1235}
1236
Radek Krejci8b764662018-11-14 14:15:13 +01001237static void
1238test_type_enum(void **state)
1239{
1240 *state = test_type_enum;
1241
1242 struct ly_ctx *ctx;
1243 struct lys_module *mod;
1244 struct lysc_type *type;
1245
1246 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1247
Radek Krejci027d5802018-11-14 16:57:28 +01001248 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 +01001249 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1250 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001251 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1252 assert_non_null(type);
1253 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1254 assert_non_null(((struct lysc_type_enum*)type)->enums);
1255 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1256 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1257 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1258 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1259 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1260 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1261 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1262 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1263 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1264 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1265 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1266 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1267 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1268 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1269
Radek Krejci027d5802018-11-14 16:57:28 +01001270 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 {"
1271 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001272 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1273 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001274 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1275 assert_non_null(type);
1276 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1277 assert_non_null(((struct lysc_type_enum*)type)->enums);
1278 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1279 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1280 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1281 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1282 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1283
1284
1285 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001286 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1287 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1288 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 +01001289 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1290 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1291 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1292 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1293 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1294 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1295 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1296 "enum one; enum one;}}}", LYS_IN_YANG));
1297 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1298 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1299 "enum '';}}}", LYS_IN_YANG));
1300 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1301 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1302 "enum ' x';}}}", LYS_IN_YANG));
1303 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1304 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1305 "enum 'x ';}}}", LYS_IN_YANG));
1306 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1307 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1308 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1309 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1310
Radek Krejci096235c2019-01-11 11:12:19 +01001311 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 +01001312 logbuf_assert("Missing enum substatement for enumeration type.");
1313
Radek Krejci096235c2019-01-11 11:12:19 +01001314 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 +01001315 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001316 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1317
Radek Krejci096235c2019-01-11 11:12:19 +01001318 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 +01001319 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001320 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 +01001321 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 +01001322 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001323 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1324
Radek Krejci096235c2019-01-11 11:12:19 +01001325 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 +01001326 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001327 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1328
Radek Krejci096235c2019-01-11 11:12:19 +01001329 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001330 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001331 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001332
Radek Krejci096235c2019-01-11 11:12:19 +01001333 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 +01001334 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001335 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1336
Radek Krejci8b764662018-11-14 14:15:13 +01001337 *state = NULL;
1338 ly_ctx_destroy(ctx, NULL);
1339}
1340
1341static void
1342test_type_bits(void **state)
1343{
1344 *state = test_type_bits;
1345
1346 struct ly_ctx *ctx;
1347 struct lys_module *mod;
1348 struct lysc_type *type;
1349
1350 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1351
Radek Krejci027d5802018-11-14 16:57:28 +01001352 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 +01001353 "bit automin; bit one {if-feature f; position 1;}"
1354 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001355 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1356 assert_non_null(type);
1357 assert_int_equal(LY_TYPE_BITS, type->basetype);
1358 assert_non_null(((struct lysc_type_bits*)type)->bits);
1359 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1360 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1361 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1362 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1363 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1364 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1365 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1366 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1367 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1368 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1369 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1370 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1371
Radek Krejci027d5802018-11-14 16:57:28 +01001372 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 {"
1373 "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 +01001374 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001375 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1376 assert_non_null(type);
1377 assert_int_equal(LY_TYPE_BITS, type->basetype);
1378 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001379 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1380 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1381 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1382 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1383 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1384 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1385 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001386
1387
1388 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001389 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1390 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1391 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 +01001392 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1393 "bit one {position -1;}}}}", LYS_IN_YANG));
1394 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1395 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1396 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1397 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1398 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1399 "bit one; bit one;}}}", LYS_IN_YANG));
1400 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1401 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1402 "bit '11';}}}", LYS_IN_YANG));
1403 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1404 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1405 "bit 'x1$1';}}}", LYS_IN_YANG));
1406 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1407
Radek Krejci096235c2019-01-11 11:12:19 +01001408 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 +01001409 logbuf_assert("Missing bit substatement for bits type.");
1410
Radek Krejci096235c2019-01-11 11:12:19 +01001411 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 +01001412 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001413 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1414
Radek Krejci096235c2019-01-11 11:12:19 +01001415 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 +01001416 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001417 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 +01001418 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 +01001419 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001420 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1421
Radek Krejci096235c2019-01-11 11:12:19 +01001422 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 +01001423 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001424 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1425
Radek Krejci096235c2019-01-11 11:12:19 +01001426 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001427 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001428 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001429
Radek Krejci096235c2019-01-11 11:12:19 +01001430 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 +01001431 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001432 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1433
Radek Krejci8b764662018-11-14 14:15:13 +01001434 *state = NULL;
1435 ly_ctx_destroy(ctx, NULL);
1436}
1437
Radek Krejci6cba4292018-11-15 17:33:29 +01001438static void
1439test_type_dec64(void **state)
1440{
1441 *state = test_type_dec64;
1442
1443 struct ly_ctx *ctx;
1444 struct lys_module *mod;
1445 struct lysc_type *type;
1446
1447 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1448
1449 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1450 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001451 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1452 assert_non_null(type);
1453 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1454 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1455 assert_non_null(((struct lysc_type_dec*)type)->range);
1456 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1457 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1458 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1459 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1460
1461 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1462 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001463 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1464 assert_non_null(type);
1465 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1466 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1467 assert_non_null(((struct lysc_type_dec*)type)->range);
1468 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1469 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1470 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1471 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1472 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1473 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1474 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1475 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1476
1477 /* invalid cases */
1478 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1479 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1480 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1481 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1482 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1483 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1484
Radek Krejci096235c2019-01-11 11:12:19 +01001485 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 +01001486 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1487
Radek Krejci096235c2019-01-11 11:12:19 +01001488 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 +01001489 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001490
Radek Krejci096235c2019-01-11 11:12:19 +01001491 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 +01001492 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001493 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1494
Radek Krejci096235c2019-01-11 11:12:19 +01001495 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 +01001496 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001497 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1498
Radek Krejci096235c2019-01-11 11:12:19 +01001499 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 +01001500 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001501 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1502
Radek Krejci096235c2019-01-11 11:12:19 +01001503 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 +01001504 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001505 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1506
Radek Krejci6cba4292018-11-15 17:33:29 +01001507 *state = NULL;
1508 ly_ctx_destroy(ctx, NULL);
1509}
1510
Radek Krejci16c0f822018-11-16 10:46:10 +01001511static void
1512test_type_instanceid(void **state)
1513{
1514 *state = test_type_instanceid;
1515
1516 struct ly_ctx *ctx;
1517 struct lys_module *mod;
1518 struct lysc_type *type;
1519
1520 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1521
1522 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1523 "leaf l1 {type instance-identifier {require-instance true;}}"
1524 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001525 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1526 assert_non_null(type);
1527 assert_int_equal(LY_TYPE_INST, type->basetype);
1528 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1529
1530 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1531 assert_non_null(type);
1532 assert_int_equal(LY_TYPE_INST, type->basetype);
1533 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1534
1535 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1536 assert_non_null(type);
1537 assert_int_equal(LY_TYPE_INST, type->basetype);
1538 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1539
1540 /* invalid cases */
1541 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1542 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1543
Radek Krejci096235c2019-01-11 11:12:19 +01001544 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 +01001545 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1546
1547 *state = NULL;
1548 ly_ctx_destroy(ctx, NULL);
1549}
1550
Radek Krejci555cb5b2018-11-16 14:54:33 +01001551static void
1552test_type_identityref(void **state)
1553{
1554 *state = test_type_identityref;
1555
1556 struct ly_ctx *ctx;
1557 struct lys_module *mod;
1558 struct lysc_type *type;
1559
1560 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1561
1562 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;}"
1563 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001564 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001565 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1566 assert_non_null(type);
1567 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1568 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1569 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1570 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1571
1572 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1573 assert_non_null(type);
1574 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1575 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1576 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1577 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1578 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1579
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001580 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1581 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001582 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1583 assert_non_null(type);
1584 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1585 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1586 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1587 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1588 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1589
Radek Krejci555cb5b2018-11-16 14:54:33 +01001590 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001591 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 +01001592 logbuf_assert("Missing base substatement for identityref type.");
1593
Radek Krejci096235c2019-01-11 11:12:19 +01001594 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001595 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001596 logbuf_assert("Missing base substatement for identityref type mytype.");
1597
Radek Krejci096235c2019-01-11 11:12:19 +01001598 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 +01001599 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001600 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001601
Radek Krejci096235c2019-01-11 11:12:19 +01001602 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 +01001603 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001604 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001605
Radek Krejci096235c2019-01-11 11:12:19 +01001606 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001607 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001608 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1609
Radek Krejci096235c2019-01-11 11:12:19 +01001610 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 +01001611 logbuf_assert("Unable to find base (j) of identityref.");
1612
Radek Krejci096235c2019-01-11 11:12:19 +01001613 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 +01001614 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1615
Radek Krejci555cb5b2018-11-16 14:54:33 +01001616 *state = NULL;
1617 ly_ctx_destroy(ctx, NULL);
1618}
1619
Radek Krejcia3045382018-11-22 14:30:31 +01001620static void
1621test_type_leafref(void **state)
1622{
1623 *state = test_type_leafref;
1624
1625 struct ly_ctx *ctx;
1626 struct lys_module *mod;
1627 struct lysc_type *type;
1628 const char *path, *name, *prefix;
1629 size_t prefix_len, name_len;
1630 int parent_times, has_predicate;
1631
1632 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1633
1634 /* lys_path_token() */
1635 path = "invalid_path";
1636 parent_times = 0;
1637 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1638 path = "..";
1639 parent_times = 0;
1640 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1641 path = "..[";
1642 parent_times = 0;
1643 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1644 path = "../";
1645 parent_times = 0;
1646 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1647 path = "/";
1648 parent_times = 0;
1649 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1650
1651 path = "../../pref:id/xxx[predicate]/invalid!!!";
1652 parent_times = 0;
1653 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1654 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1655 assert_int_equal(4, prefix_len);
1656 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1657 assert_int_equal(2, name_len);
1658 assert_int_equal(0, strncmp("id", name, name_len));
1659 assert_int_equal(2, parent_times);
1660 assert_int_equal(0, has_predicate);
1661 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1662 assert_string_equal("[predicate]/invalid!!!", path);
1663 assert_int_equal(0, prefix_len);
1664 assert_null(prefix);
1665 assert_int_equal(3, name_len);
1666 assert_int_equal(0, strncmp("xxx", name, name_len));
1667 assert_int_equal(1, has_predicate);
1668 path += 11;
1669 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1670 assert_string_equal("!!!", path);
1671 assert_int_equal(0, prefix_len);
1672 assert_null(prefix);
1673 assert_int_equal(7, name_len);
1674 assert_int_equal(0, strncmp("invalid", name, name_len));
1675
1676 path = "/absolute/prefix:path";
1677 parent_times = 0;
1678 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1679 assert_string_equal("/prefix:path", path);
1680 assert_int_equal(0, prefix_len);
1681 assert_null(prefix);
1682 assert_int_equal(8, name_len);
1683 assert_int_equal(0, strncmp("absolute", name, name_len));
1684 assert_int_equal(-1, parent_times);
1685 assert_int_equal(0, has_predicate);
1686 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1687 assert_int_equal(0, *path);
1688 assert_int_equal(6, prefix_len);
1689 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1690 assert_int_equal(4, name_len);
1691 assert_int_equal(0, strncmp("path", name, name_len));
1692 assert_int_equal(0, has_predicate);
1693
1694 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001695 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 +01001696 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1697 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001698 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1699 assert_non_null(type);
1700 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1701 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001702 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001703 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1704 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001705 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1706 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1707 assert_non_null(type);
1708 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1709 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001710 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1711 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1712 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001713 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1714
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001715 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 +01001716 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001717 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001718 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1719 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001720 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001721 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1722 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1723 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001724 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1725 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001726 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1727
1728 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001729 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 +01001730 "typedef mytype3 {type c:mytype {require-instance false;}}"
1731 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001732 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1733 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001734 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001735 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1736 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1737 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001738 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1739 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001740 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1741 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1742 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001743 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001744 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1745 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1746 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1747 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1748
Radek Krejci4ce68932018-11-28 12:53:36 +01001749 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1750 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1751 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001752 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1753 assert_non_null(type);
1754 assert_int_equal(1, type->refcount);
1755 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1756 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1757 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1758 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1759 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1760 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1761
Radek Krejci58d171e2018-11-23 13:50:55 +01001762 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001763 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 +01001764 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1765 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001766 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1767 assert_non_null(type);
1768 assert_int_equal(1, type->refcount);
1769 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1770 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1771 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1772 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1773 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1774
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001775 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1776 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1777 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001778 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001779 LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001780 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001781 assert_non_null(type);
1782 assert_int_equal(1, type->refcount);
1783 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001784 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001785 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1786 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1787 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001788
Radek Krejci20e8a182018-12-07 11:43:21 +01001789 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1790 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1791 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1792 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001793 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1794 assert_non_null(type);
1795 assert_int_equal(1, type->refcount);
1796 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1797 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1798 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1799 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1800 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1801
Radek Krejci3d929562019-02-21 11:25:55 +01001802 /* leafref to imported (not yet implemented) module */
1803 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}");
1804 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}"
1805 "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG));
1806 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1807 assert_non_null(type);
1808 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1809 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1810 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1811 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h"));
1812 assert_int_equal(1, mod->implemented);
1813 assert_non_null(mod->compiled->data);
1814 assert_string_equal("h", mod->compiled->data->name);
1815
Radek Krejcia3045382018-11-22 14:30:31 +01001816 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001817 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 +01001818 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001819 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001820 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 +01001821 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001822 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001823 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 +01001824 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001825 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 +01001826 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 +01001827 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001828 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001829 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 +01001830 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001831 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001832 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 +01001833 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001834 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001835 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 +01001836 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001837 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001838 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001839 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001840 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001841 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001842 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001843 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001844 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001845 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001846 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1847 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001848 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001849 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001850 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 +01001851
Radek Krejci096235c2019-01-11 11:12:19 +01001852 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001853 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001854 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001855 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 +01001856 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001857 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1858
Radek Krejci096235c2019-01-11 11:12:19 +01001859 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001860 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1861 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1862 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1863 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001864 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1865
Radek Krejci096235c2019-01-11 11:12:19 +01001866 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001867 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1868 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1869 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1870 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001871 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1872
Radek Krejci096235c2019-01-11 11:12:19 +01001873 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001874 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1875 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1876 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1877 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001878 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1879
Radek Krejci096235c2019-01-11 11:12:19 +01001880 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001881 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1882 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1883 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1884 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001885 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1886
Radek Krejci096235c2019-01-11 11:12:19 +01001887 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001888 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1889 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1890 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1891 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001892 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1893
Radek Krejci096235c2019-01-11 11:12:19 +01001894 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001895 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1896 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1897 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1898 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001899 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1900
Radek Krejci096235c2019-01-11 11:12:19 +01001901 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001902 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1903 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1904 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1905 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001906 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1907
Radek Krejci096235c2019-01-11 11:12:19 +01001908 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001909 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1910 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1911 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
1912 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001913 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1914
Radek Krejci096235c2019-01-11 11:12:19 +01001915 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001916 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1917 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1918 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
1919 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001920 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1921
Radek Krejci096235c2019-01-11 11:12:19 +01001922 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001923 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1924 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1925 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
1926 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001927 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1928
Radek Krejci096235c2019-01-11 11:12:19 +01001929 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001930 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1931 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1932 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
1933 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001934 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1935
Radek Krejci096235c2019-01-11 11:12:19 +01001936 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001937 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1938 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1939 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1940 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001941 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1942
Radek Krejci096235c2019-01-11 11:12:19 +01001943 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001944 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1945 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1946 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
1947 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001948 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1949
Radek Krejci096235c2019-01-11 11:12:19 +01001950 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001951 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1952 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
1953 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1954 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001955 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1956
Radek Krejci096235c2019-01-11 11:12:19 +01001957 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001958 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1959 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1960 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1961 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001962 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1963
Radek Krejci412ddfa2018-11-23 11:44:11 +01001964 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01001965 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01001966 "leaf ref1 {type leafref {path /ref2;}}"
1967 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001968 "leaf ref3 {type leafref {path /ref4;}}"
1969 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001970 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1971
Radek Krejcia3045382018-11-22 14:30:31 +01001972 *state = NULL;
1973 ly_ctx_destroy(ctx, NULL);
1974}
1975
Radek Krejci43699232018-11-23 14:59:46 +01001976static void
1977test_type_empty(void **state)
1978{
1979 *state = test_type_empty;
1980
1981 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01001982
1983 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1984
1985 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01001986 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01001987 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001988 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1989
Radek Krejci096235c2019-01-11 11:12:19 +01001990 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 +01001991 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001992 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1993
1994 *state = NULL;
1995 ly_ctx_destroy(ctx, NULL);
1996}
1997
Radek Krejcicdfecd92018-11-26 11:27:32 +01001998
1999static void
2000test_type_union(void **state)
2001{
2002 *state = test_type_union;
2003
2004 struct ly_ctx *ctx;
2005 struct lys_module *mod;
2006 struct lysc_type *type;
2007
2008 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2009
2010 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2011 "typedef mytype {type union {type int8; type mybasetype;}}"
2012 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002013 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2014 assert_non_null(type);
2015 assert_int_equal(2, type->refcount);
2016 assert_int_equal(LY_TYPE_UNION, type->basetype);
2017 assert_non_null(((struct lysc_type_union*)type)->types);
2018 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2019 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2020 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2021
2022 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2023 "typedef mytype {type union {type int8; type mybasetype;}}"
2024 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002025 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2026 assert_non_null(type);
2027 assert_int_equal(1, type->refcount);
2028 assert_int_equal(LY_TYPE_UNION, type->basetype);
2029 assert_non_null(((struct lysc_type_union*)type)->types);
2030 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2031 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2032 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2033 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2034
2035 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2036 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002037 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2038 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002039 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002040 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2041 assert_non_null(type);
2042 assert_int_equal(1, type->refcount);
2043 assert_int_equal(LY_TYPE_UNION, type->basetype);
2044 assert_non_null(((struct lysc_type_union*)type)->types);
2045 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2046 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2047 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2048 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2049 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2050 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2051
Radek Krejci6be5ff12018-11-26 13:18:15 +01002052 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01002053 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002054 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002055 logbuf_assert("Missing type substatement for union type mytype.");
2056
Radek Krejci096235c2019-01-11 11:12:19 +01002057 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2058 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002059
Radek Krejci096235c2019-01-11 11:12:19 +01002060 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 +01002061 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002062 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2063
Radek Krejci096235c2019-01-11 11:12:19 +01002064 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 +01002065 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002066 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2067
Radek Krejcicdfecd92018-11-26 11:27:32 +01002068 *state = NULL;
2069 ly_ctx_destroy(ctx, NULL);
2070}
2071
2072static void
2073test_type_dflt(void **state)
2074{
2075 *state = test_type_union;
2076
2077 struct ly_ctx *ctx;
2078 struct lys_module *mod;
2079 struct lysc_type *type;
2080
2081 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2082
2083 /* default is not inherited from union's types */
2084 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2085 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002086 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2087 assert_non_null(type);
2088 assert_int_equal(1, type->refcount);
2089 assert_int_equal(LY_TYPE_UNION, type->basetype);
2090 assert_non_null(((struct lysc_type_union*)type)->types);
2091 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2092 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2093 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2094 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2095 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2096
2097 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2098 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002099 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2100 assert_non_null(type);
2101 assert_int_equal(2, type->refcount);
2102 assert_int_equal(LY_TYPE_STRING, type->basetype);
2103 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2104 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2105
2106 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2107 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002108 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2109 assert_non_null(type);
2110 assert_int_equal(2, type->refcount);
2111 assert_int_equal(LY_TYPE_STRING, type->basetype);
2112 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2113 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2114
Radek Krejci6be5ff12018-11-26 13:18:15 +01002115 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2116 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2117 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002118 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2119 assert_non_null(type);
2120 assert_int_equal(4, type->refcount);
2121 assert_int_equal(LY_TYPE_STRING, type->basetype);
2122 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2123 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2124 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2125 assert_non_null(type);
2126 assert_int_equal(4, type->refcount);
2127 assert_int_equal(LY_TYPE_STRING, type->basetype);
2128 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2129 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2130
2131 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2132 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002133 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2134 assert_non_null(type);
2135 assert_int_equal(3, type->refcount);
2136 assert_int_equal(LY_TYPE_STRING, type->basetype);
2137 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2138 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2139
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002140 /* mandatory leaf does not takes default value from type */
2141 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2142 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002143 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2144 assert_non_null(type);
2145 assert_int_equal(LY_TYPE_STRING, type->basetype);
2146 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2147 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2148
Radek Krejcicdfecd92018-11-26 11:27:32 +01002149 *state = NULL;
2150 ly_ctx_destroy(ctx, NULL);
2151}
2152
Radek Krejci665421c2018-12-05 08:03:39 +01002153static void
2154test_status(void **state)
2155{
2156 *state = test_status;
2157
2158 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002159
2160 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2161
Radek Krejci096235c2019-01-11 11:12:19 +01002162 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002163 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002164 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2165
Radek Krejci096235c2019-01-11 11:12:19 +01002166 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002167 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002168 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2169
Radek Krejci096235c2019-01-11 11:12:19 +01002170 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002171 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002172 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2173
2174 *state = NULL;
2175 ly_ctx_destroy(ctx, NULL);
2176}
2177
Radek Krejcie86bf772018-12-14 11:39:53 +01002178static void
2179test_uses(void **state)
2180{
2181 *state = test_uses;
2182
2183 struct ly_ctx *ctx;
2184 struct lys_module *mod;
Radek Krejci3641f562019-02-13 15:38:40 +01002185 const struct lysc_node *parent, *child;
Radek Krejcie86bf772018-12-14 11:39:53 +01002186
2187 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2188
Radek Krejci0af46292019-01-11 16:02:31 +01002189 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2190 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002191 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2192 "grouping grp_a_top {leaf a1 {type int8;}}"
2193 "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 +01002194 assert_non_null((parent = mod->compiled->data));
2195 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2196 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2197 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002198 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002199 assert_non_null((child = child->next));
2200 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002201 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002202 assert_non_null((child = child->next));
2203 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002204 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002205 assert_non_null((child = child->next));
2206 assert_string_equal("y", child->name);
2207 assert_non_null(child->iffeatures);
2208 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2209 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2210 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2211 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2212 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2213 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2214
2215 /* make the imported module implemented and enable the feature */
2216 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2217 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2218 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2219 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2220 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002221
Radek Krejci00b874b2019-02-12 10:54:50 +01002222 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;}}}");
2223 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 +01002224 assert_non_null(mod->compiled->data);
2225 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2226 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002227 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Radek Krejcib1b59152019-01-07 13:21:56 +01002228
Radek Krejci7f6a3812019-01-07 13:48:57 +01002229 logbuf_clean();
2230 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2231 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2232 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002233 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2234 assert_string_equal("l", mod->compiled->data->name);
2235 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2236 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2237 assert_string_equal("k", mod->compiled->data->next->name);
2238 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2239 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2240
Radek Krejci3641f562019-02-13 15:38:40 +01002241 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}"
2242 "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2243 assert_non_null(mod->compiled->data);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002244 assert_non_null(child = lysc_node_children(mod->compiled->data, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002245 assert_string_equal("g", child->name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002246 assert_non_null(child = lysc_node_children(child, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002247 assert_string_equal("x", child->name);
2248
Radek Krejcie86bf772018-12-14 11:39:53 +01002249 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002250 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 +01002251 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2252
Radek Krejci096235c2019-01-11 11:12:19 +01002253 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002254 "grouping grp {leaf a{type string;}uses grp1;}"
2255 "grouping grp1 {leaf b {type string;}uses grp2;}"
2256 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002257 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2258
Radek Krejci096235c2019-01-11 11:12:19 +01002259 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 +01002260 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2261
Radek Krejci096235c2019-01-11 11:12:19 +01002262 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 +01002263 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002264 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +01002265
Radek Krejci096235c2019-01-11 11:12:19 +01002266 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 +01002267 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002268 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2269
Radek Krejci95710c92019-02-11 15:49:55 +01002270 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2271 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002272 logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002273 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2274 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002275 logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002276
Radek Krejci3641f562019-02-13 15:38:40 +01002277
2278 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}"
2279 "leaf g {type string;}"
2280 "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2281 logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used.");
2282
2283
Radek Krejcie86bf772018-12-14 11:39:53 +01002284 *state = NULL;
2285 ly_ctx_destroy(ctx, NULL);
2286}
2287
Radek Krejci01342af2019-01-03 15:18:08 +01002288static void
2289test_refine(void **state)
2290{
2291 *state = test_refine;
2292
2293 struct ly_ctx *ctx;
2294 struct lys_module *mod;
2295 struct lysc_node *parent, *child;
2296
2297 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2298
Radek Krejci096235c2019-01-11 11:12:19 +01002299 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!;}"
2300 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2301 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2302 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2303 "leaf x {type mytype; mandatory true; must 1;}"
2304 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2305 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002306
Radek Krejcif0089082019-01-07 16:42:01 +01002307 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 +01002308 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002309 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002310 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002311 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2312 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002313 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002314 assert_non_null((parent = mod->compiled->data));
2315 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2316 assert_string_equal("c", parent->name);
2317 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2318 assert_int_equal(LYS_LEAF, child->nodetype);
2319 assert_string_equal("l", child->name);
2320 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2321 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2322 assert_non_null(child = child->next);
2323 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2324 assert_string_equal("ll", child->name);
2325 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2326 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2327 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002328 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2329 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002330 assert_non_null(child = child->next);
2331 assert_int_equal(LYS_CHOICE, child->nodetype);
2332 assert_string_equal("ch", child->name);
2333 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2334 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2335 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002336 assert_non_null(child->iffeatures);
2337 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002338 assert_non_null(child = child->next);
2339 assert_int_equal(LYS_LEAF, child->nodetype);
2340 assert_string_equal("x", child->name);
2341 assert_false(LYS_MAND_TRUE & child->flags);
2342 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002343 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2344 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002345 assert_string_equal("refined", child->dsc);
2346 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002347 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002348 assert_int_equal(LYS_ANYDATA, child->nodetype);
2349 assert_string_equal("a", child->name);
2350 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002351 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2352 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002353 assert_non_null(child->iffeatures);
2354 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002355 assert_string_equal("refined", child->dsc);
2356 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002357 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002358 assert_int_equal(LYS_CONTAINER, child->nodetype);
2359 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002360 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002361 assert_true(LYS_CONFIG_W & child->flags);
2362 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002363
2364 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 +01002365 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002366 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002367 assert_int_equal(LYS_LEAF, child->nodetype);
2368 assert_string_equal("x", child->name);
2369 assert_false(LYS_MAND_TRUE & child->flags);
2370 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2371
2372 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002373 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002374 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002375 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2376
Radek Krejci096235c2019-01-11 11:12:19 +01002377 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002378 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002379 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2380
Radek Krejci096235c2019-01-11 11:12:19 +01002381 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002382 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002383 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2384
Radek Krejci096235c2019-01-11 11:12:19 +01002385 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002386 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002387 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2388
Radek Krejci096235c2019-01-11 11:12:19 +01002389 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002390 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002391 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002392 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002393 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002394 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2395
Radek Krejci096235c2019-01-11 11:12:19 +01002396 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002397 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejcif1421c22019-02-19 13:05:20 +01002398 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" under the default case.");
Radek Krejci01342af2019-01-03 15:18:08 +01002399
Radek Krejci096235c2019-01-11 11:12:19 +01002400 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002401 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002402 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2403
Radek Krejci096235c2019-01-11 11:12:19 +01002404 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002405 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002406 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2407
Radek Krejci096235c2019-01-11 11:12:19 +01002408 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 +01002409 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002410 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2411
Radek Krejci096235c2019-01-11 11:12:19 +01002412 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002413 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002414 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2415
Radek Krejci096235c2019-01-11 11:12:19 +01002416 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002417 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002418 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2419
Radek Krejci096235c2019-01-11 11:12:19 +01002420 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002421 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002422 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2423
Radek Krejci096235c2019-01-11 11:12:19 +01002424 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002425 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002426 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2427
Radek Krejci01342af2019-01-03 15:18:08 +01002428 *state = NULL;
2429 ly_ctx_destroy(ctx, NULL);
2430}
Radek Krejcie86bf772018-12-14 11:39:53 +01002431
Radek Krejci95710c92019-02-11 15:49:55 +01002432static void
2433test_augment(void **state)
2434{
2435 *state = test_augment;
2436
2437 struct ly_ctx *ctx;
2438 struct lys_module *mod;
2439 const struct lysc_node *node;
2440 const struct lysc_node_choice *ch;
2441 const struct lysc_node_case *c;
Radek Krejcif538ce52019-03-05 10:46:14 +01002442 const struct lysc_action *rpc;
Radek Krejci95710c92019-02-11 15:49:55 +01002443
2444 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2445
2446 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2447 "container top {leaf a {type string;}}}");
2448 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2449 "leaf b {type a:atype;}}", LYS_IN_YANG));
2450 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2451 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2452 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2453 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2454 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2455 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2456 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2457 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2458 assert_non_null(node = mod->compiled->data);
2459 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002460 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002461 assert_string_equal(node->name, "a");
2462 assert_non_null(node = node->next);
2463 assert_string_equal(node->name, "c");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002464 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002465 assert_string_equal(node->name, "c");
2466 assert_non_null(node = node->next);
2467 assert_string_equal(node->name, "d");
2468 assert_non_null(node = node->next);
2469 assert_string_equal(node->name, "c");
2470
2471 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 +01002472 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2473 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002474 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2475 assert_null(mod->compiled->data->next);
2476 assert_string_equal("ch", ch->name);
2477 assert_non_null(c = ch->cases);
2478 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002479 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002480 assert_string_equal("a", c->child->name);
2481 assert_non_null(c = (const struct lysc_node_case*)c->next);
2482 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002483 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002484 assert_string_equal("b", c->child->name);
2485 assert_non_null(c = (const struct lysc_node_case*)c->next);
2486 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002487 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002488 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002489 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002490 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002491 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002492 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2493 assert_null(c->next);
2494
2495 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2496 "container c;"
2497 "augment /c {uses g;}}", LYS_IN_YANG)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002498 assert_non_null(node = lysc_node_children(mod->compiled->data, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002499 assert_string_equal(node->name, "a");
2500
Radek Krejci339f5292019-02-11 16:42:34 +01002501 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2502 "augment /c {container sub;}}");
2503 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2504 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2505 assert_non_null(mod->compiled->data);
2506 assert_string_equal("c", mod->compiled->data->name);
2507 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2508 assert_string_equal("sub", node->name);
2509 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2510 assert_string_equal("main", node->name);
2511
Radek Krejcif538ce52019-03-05 10:46:14 +01002512 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 +01002513 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;"
2514 "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2515 "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}"
2516 "augment /hi:top {leaf l {type string; mandatory true; config false;}}"
2517 "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG));
2518 assert_non_null(node = mod->compiled->data);
2519 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2520 assert_string_equal("l", node->name);
2521 assert_true(node->flags & LYS_MAND_TRUE);
2522 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
Radek Krejcife909632019-02-12 15:34:42 +01002523 assert_non_null(node = mod->compiled->data);
2524 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2525 assert_string_equal("p", node->name);
Radek Krejci733988a2019-02-15 15:12:44 +01002526 assert_non_null(node = node->next);
2527 assert_string_equal("ll", node->name);
2528 assert_non_null(node = node->next);
2529 assert_string_equal("l", node->name);
2530 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejcife909632019-02-12 15:34:42 +01002531
Radek Krejcif538ce52019-03-05 10:46:14 +01002532 assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}"
2533 "augment /hi:func/input {leaf x {type string;}}"
2534 "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG));
2535 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
2536 assert_non_null(rpc = mod->compiled->rpcs);
2537 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
2538 assert_non_null(rpc->input.data);
2539 assert_string_equal("x", rpc->input.data->name);
2540 assert_null(rpc->input.data->next);
2541 assert_non_null(rpc->output.data);
2542 assert_string_equal("y", rpc->output.data->name);
2543 assert_null(rpc->output.data->next);
2544
Radek Krejci95710c92019-02-11 15:49:55 +01002545 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2546 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
2547 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found.");
2548
2549 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2550 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002551 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002552
2553
Radek Krejci339f5292019-02-11 16:42:34 +01002554 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2555 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
2556 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target.");
2557
2558 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2559 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
2560 logbuf_assert("Invalid augment (/c) of container node which is not allowed to contain case node \"b\".");
2561
Radek Krejci733988a2019-02-15 15:12:44 +01002562 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}"
2563 "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
2564 logbuf_assert("Invalid augment (/hi:top) adding mandatory node \"c\" without making it conditional via when statement.");
Radek Krejcife909632019-02-12 15:34:42 +01002565
Radek Krejci3641f562019-02-13 15:38:40 +01002566 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;"
2567 "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG));
2568 logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\".");
Radek Krejci95710c92019-02-11 15:49:55 +01002569
Radek Krejcif538ce52019-03-05 10:46:14 +01002570 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;"
2571 "augment /func {leaf x {type int8;}}}", LYS_IN_YANG));
2572 logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC/action node which is not an allowed augment's target.");
2573
Radek Krejci95710c92019-02-11 15:49:55 +01002574 *state = NULL;
2575 ly_ctx_destroy(ctx, NULL);
2576}
2577
Radek Krejciccd20f12019-02-15 14:12:27 +01002578static void
2579test_deviation(void **state)
2580{
2581 *state = test_deviation;
2582
2583 struct ly_ctx *ctx;
2584 struct lys_module *mod;
2585 const struct lysc_node *node;
Radek Krejci7af64242019-02-18 13:07:53 +01002586 const struct lysc_node_list *list;
Radek Krejciccd20f12019-02-15 14:12:27 +01002587
2588 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2589
2590 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002591 "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2592 "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 +01002593 " case c {leaf c{type string;}}}"
2594 "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}"
2595 "rpc func2;}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002596 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 +01002597 "deviation /a:top/a:b {deviate not-supported;}"
2598 "deviation /a:ch/a:a/a:x {deviate not-supported;}"
2599 "deviation /a:ch/a:c/a:c {deviate not-supported;}"
2600 "deviation /a:ch/a:b {deviate not-supported;}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002601 "deviation /a:ch/a:a/a:a {deviate not-supported;}"
2602 "deviation /a:func1/a:input {deviate not-supported;}"
2603 "deviation /a:func1/a:output {deviate not-supported;}"
2604 "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002605 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2606 assert_non_null(node = mod->compiled->data);
2607 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002608 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejciccd20f12019-02-15 14:12:27 +01002609 assert_string_equal(node->name, "a");
2610 assert_non_null(node = node->next);
2611 assert_string_equal(node->name, "c");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002612 assert_null(node = node->next);
2613 assert_non_null(node = mod->compiled->data->next);
2614 assert_string_equal("ch", node->name);
2615 assert_null(((struct lysc_node_choice*)node)->dflt);
2616 assert_null(((struct lysc_node_choice*)node)->cases);
Radek Krejcif538ce52019-03-05 10:46:14 +01002617 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs));
2618 assert_null(mod->compiled->rpcs[0].input.data);
2619 assert_null(mod->compiled->rpcs[0].output.data);
Radek Krejciccd20f12019-02-15 14:12:27 +01002620
2621 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}"
2622 "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}"
2623 "deviation /c1 {deviate add {units meters;}}"
2624 "deviation /c2 {deviate delete {units meters;}}"
2625 "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG));
2626 assert_non_null(node = mod->compiled->data);
2627 assert_string_equal("c1", node->name);
2628 assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units);
2629 assert_non_null(node = node->next);
2630 assert_string_equal("c2", node->name);
2631 assert_null(((struct lysc_node_leaf*)node)->units);
2632 assert_non_null(node = node->next);
2633 assert_string_equal("c3", node->name);
2634 assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units);
2635
2636 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 +01002637 "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002638 "deviation /c1 {deviate add {must 3;}}"
2639 "deviation /c2 {deviate delete {must 2;}}"
2640 "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG));
2641 assert_non_null(node = mod->compiled->data);
2642 assert_string_equal("c1", node->name);
2643 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts));
2644 assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr);
2645 assert_non_null(node = node->next);
2646 assert_string_equal("c2", node->name);
Radek Krejcib4532d12019-02-15 16:13:29 +01002647 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts));
2648 assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr);
Radek Krejciccd20f12019-02-15 14:12:27 +01002649 assert_non_null(node = node->next);
2650 assert_string_equal("c3", node->name);
2651 assert_null(((struct lysc_node_leaf*)node)->musts);
2652
Radek Krejcib4532d12019-02-15 16:13:29 +01002653 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 +01002654 "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 +01002655 "choice b {default a;leaf a {type string;} leaf b {type string;}}"
2656 "leaf c {default hello; type string;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002657 "leaf-list d {default hello; default world; type string;}"
2658 "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002659 assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}"
2660 "deviation /x:a {deviate delete {default a;}}"
2661 "deviation /x:b {deviate delete {default x:a;}}"
2662 "deviation /x:c {deviate delete {default hello;}}"
2663 "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG));
2664 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2665 assert_non_null(node = mod->compiled->data);
2666 assert_null(((struct lysc_node_choice*)node)->dflt);
2667 assert_non_null(node = node->next);
2668 assert_null(((struct lysc_node_choice*)node)->dflt);
2669 assert_non_null(node = node->next);
2670 assert_null(((struct lysc_node_leaf*)node)->dflt);
2671 assert_non_null(node = node->next);
2672 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2673 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002674 assert_non_null(node = node->next);
2675 assert_string_equal("nothing", ((struct lysc_node_leaf*)node)->dflt);
2676 assert_non_null(node = node->next);
2677 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2678 assert_string_equal("nothing", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002679
2680 assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}"
2681 "deviation /x:b {deviate add {default x:b;}}"
2682 "deviation /x:c {deviate add {default bye;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002683 "deviation /x:d {deviate add {default all; default people;}}"
2684 "deviation /x:c2 {deviate add {default hi;}}"
2685 "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002686 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2687 assert_non_null(node = mod->compiled->data);
2688 assert_null(((struct lysc_node_choice*)node)->dflt);
2689 assert_non_null(node = node->next);
2690 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2691 assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name);
2692 assert_non_null(node = node->next);
2693 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2694 assert_string_equal("bye", ((struct lysc_node_leaf*)node)->dflt);
2695 assert_non_null(node = node->next);
2696 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2697 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
2698 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
2699 assert_string_equal("people", ((struct lysc_node_leaflist*)node)->dflts[2]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002700 assert_non_null(node = node->next);
2701 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2702 assert_string_equal("hi", ((struct lysc_node_leaf*)node)->dflt);
2703 assert_non_null(node = node->next);
2704 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2705 assert_string_equal("hi", ((struct lysc_node_leaflist*)node)->dflts[0]);
2706 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002707
2708 assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}"
2709 "deviation /x:b {deviate replace {default x:a;}}"
2710 "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG));
2711 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2712 assert_non_null(node = mod->compiled->data);
2713 assert_null(((struct lysc_node_choice*)node)->dflt);
2714 assert_non_null(node = node->next);
2715 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2716 assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name);
2717 assert_non_null(node = node->next);
2718 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2719 assert_string_equal("hello", ((struct lysc_node_leaf*)node)->dflt);
2720
Radek Krejci7af64242019-02-18 13:07:53 +01002721 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;"
2722 "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2723 "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}"
2724 " leaf c {type string;} leaf d {type string;}}}");
2725 assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}"
2726 "augment /i:l1 {leaf j_c {type string;}}"
2727 "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}"
2728 "deviation /i:l1 {deviate add {unique \"i:c\";}}"
2729 "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG));
2730 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i")));
2731 assert_non_null(list = (struct lysc_node_list*)mod->compiled->data);
2732 assert_string_equal("l1", list->name);
2733 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
2734 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
2735 assert_string_equal("b", list->uniques[0][0]->name);
2736 assert_string_equal("j_c", list->uniques[0][1]->name);
2737 assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1]));
2738 assert_string_equal("c", list->uniques[1][0]->name);
2739 assert_non_null(list = (struct lysc_node_list*)list->next);
2740 assert_string_equal("l2", list->name);
2741 assert_null(list->uniques);
2742
Radek Krejci93dcc392019-02-19 10:43:38 +01002743 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}"
2744 "container top {leaf x {type string;} leaf y {type string; config false;}}"
2745 "deviation /a {deviate add {config false; }}"
2746 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
2747 assert_non_null(node = mod->compiled->data);
2748 assert_string_equal("a", node->name);
2749 assert_true(node->flags & LYS_CONFIG_R);
2750 assert_non_null(node = node->next);
2751 assert_string_equal("top", node->name);
2752 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002753 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002754 assert_string_equal("x", node->name);
2755 assert_true(node->flags & LYS_CONFIG_R);
2756 assert_non_null(node = node->next);
2757 assert_string_equal("y", node->name);
2758 assert_true(node->flags & LYS_CONFIG_R);
2759
2760 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}"
2761 "container top {config false; leaf x {type string;}}"
2762 "deviation /a {deviate replace {config true;}}"
2763 "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG));
2764 assert_non_null(node = mod->compiled->data);
2765 assert_string_equal("a", node->name);
2766 assert_true(node->flags & LYS_CONFIG_W);
2767 assert_non_null(node = node->next);
2768 assert_string_equal("top", node->name);
2769 assert_true(node->flags & LYS_CONFIG_W);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002770 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002771 assert_string_equal("x", node->name);
2772 assert_true(node->flags & LYS_CONFIG_W);
2773
Radek Krejcif1421c22019-02-19 13:05:20 +01002774 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;"
2775 "container a {leaf a {type string;}}"
2776 "container b {leaf b {mandatory true; type string;}}"
2777 "deviation /a/a {deviate add {mandatory true;}}"
2778 "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG));
2779 assert_non_null(node = mod->compiled->data);
2780 assert_string_equal("a", node->name);
2781 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002782 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002783 assert_non_null(node = node->next);
2784 assert_string_equal("b", node->name);
2785 assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002786 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002787
Radek Krejci551b12c2019-02-19 16:11:21 +01002788 assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;"
2789 "leaf a {default test; type string;}"
2790 "leaf b {mandatory true; type string;}"
2791 "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}"
2792 "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG));
2793 assert_non_null(node = mod->compiled->data);
2794 assert_string_equal("a", node->name);
2795 assert_null(((struct lysc_node_leaf*)node)->dflt);
2796 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
2797 assert_non_null(node = node->next);
2798 assert_string_equal("b", node->name);
2799 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2800 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
2801
2802 assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;"
2803 "leaf-list a {type string;}"
2804 "list b {config false;}"
2805 "leaf-list c {min-elements 1; max-elements 10; type string;}"
2806 "list d {min-elements 10; max-elements 100; config false;}"
2807 "deviation /a {deviate add {min-elements 1; max-elements 10;}}"
2808 "deviation /b {deviate add {min-elements 10; max-elements 100;}}"
2809 "deviation /c {deviate replace {min-elements 10; max-elements 100;}}"
2810 "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG));
2811 assert_non_null(node = mod->compiled->data);
2812 assert_string_equal("a", node->name);
2813 assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min);
2814 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max);
2815 assert_non_null(node = node->next);
2816 assert_string_equal("b", node->name);
2817 assert_int_equal(10, ((struct lysc_node_list*)node)->min);
2818 assert_int_equal(100, ((struct lysc_node_list*)node)->max);
2819 assert_non_null(node = node->next);
2820 assert_string_equal("c", node->name);
2821 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min);
2822 assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max);
2823 assert_non_null(node = node->next);
2824 assert_string_equal("d", node->name);
2825 assert_int_equal(1, ((struct lysc_node_list*)node)->min);
2826 assert_int_equal(10, ((struct lysc_node_list*)node)->max);
2827
Radek Krejci33f72892019-02-21 10:36:58 +01002828 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;}"
2829 "leaf a {type string; default 10;} leaf-list b {type string;}"
2830 "deviation /a {deviate replace {type mytype;}}"
2831 "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG));
2832 assert_non_null(node = mod->compiled->data);
2833 assert_string_equal("a", node->name);
2834 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf*)node)->type->basetype);
2835 assert_string_equal("10", ((struct lysc_node_leaf*)node)->dflt);
2836 assert_non_null(node = node->next);
2837 assert_string_equal("b", node->name);
2838 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaflist*)node)->type->basetype);
2839 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2840 assert_string_equal("1", ((struct lysc_node_leaflist*)node)->dflts[0]);
2841
Radek Krejci88ef64b2019-02-18 16:02:06 +01002842 assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002843 "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG));
2844 logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002845 assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}"
2846 "deviation /a:top/a:a {deviate not-supported;}"
2847 "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG));
2848 /* warning */
2849 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 +01002850
2851 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}"
2852 "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG));
2853 logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\".");
2854
2855 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;"
2856 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
2857 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to add \"units\" property.");
2858 assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}"
2859 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
2860 logbuf_assert("Invalid deviation (/c) adding \"units\" property which already exists (with value \"centimeters\").");
2861
2862 assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;"
2863 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
2864 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to delete \"units\" property.");
2865 assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}"
2866 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
2867 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which is not present.");
2868 assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}"
2869 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
2870 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\".");
2871
2872 assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;"
2873 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
2874 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to replace \"units\" property.");
2875 assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}"
2876 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
2877 logbuf_assert("Invalid deviation (/c) replacing \"units\" property \"meters\" which is not present.");
2878
2879 /* the default is already deleted in /e:a byt module f */
2880 assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}"
2881 "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG));
2882 logbuf_assert("Invalid deviation (/e:a) deleting \"default\" property \"x:a\" which is not present.");
2883 assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}"
2884 "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG));
2885 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"x:a\" of choice. "
2886 "The prefix does not match any imported module of the deviation module.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002887 assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}"
2888 "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG));
2889 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\".");
2890 assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}"
2891 "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG));
2892 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module.");
2893 assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;"
2894 "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG));
2895 logbuf_assert("Invalid deviation (/a) of anyxml node - it is not possible to delete \"default\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01002896
2897 assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}"
2898 "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG));
2899 logbuf_assert("Invalid deviation (/e:b) adding \"default\" property which already exists (with value \"a\").");
2900 assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01002901 "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG));
2902 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"x:a\" of choice. "
2903 "The prefix does not match any imported module of the deviation module.");
2904 assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}"
2905 "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG));
2906 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"a\" of choice - the specified case does not exists.");
2907 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002908 "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG));
2909 logbuf_assert("Invalid deviation (/e:c) adding \"default\" property which already exists (with value \"hello\").");
Radek Krejci468a94f2019-02-15 14:52:36 +01002910 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
2911 "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG));
2912 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 +01002913 assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}"
2914 "deviation /x {deviate add {default error;}}}", LYS_IN_YANG));
Radek Krejci551b12c2019-02-19 16:11:21 +01002915 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
Radek Krejciccd20f12019-02-15 14:12:27 +01002916
2917 assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}"
2918 "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG));
2919 logbuf_assert("Invalid deviation (/e:d) of leaf-list node - it is not possible to replace \"default\" property.");
2920
Radek Krejci7af64242019-02-18 13:07:53 +01002921 assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}"
2922 "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG));
2923 logbuf_assert("Invalid deviation (/i:l1) deleting \"unique\" property \"x\" which does not match any of the target's property values.");
2924 assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}"
2925 "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG));
2926 logbuf_assert("Invalid deviation (/i:l2) deleting \"unique\" property \"d\" which does not match any of the target's property values.");
2927 assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}"
2928 "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG));
2929 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to delete \"unique\" property.");
2930 assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}"
2931 "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG));
2932 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to add \"unique\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01002933
Radek Krejci93dcc392019-02-19 10:43:38 +01002934 assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}"
2935 "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG));
2936 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to add \"config\" property.");
2937 assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}"
2938 "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG));
2939 logbuf_assert("Invalid deviation of config in \"/top/x\" - configuration node cannot be child of any state data node.");
2940 assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}"
2941 "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG));
2942 logbuf_assert("Invalid deviation (/top/x) replacing \"config\" property \"config false\" which is not present.");
2943 assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}"
2944 "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG));
2945 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to replace \"config\" property.");
2946 assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}"
2947 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
2948 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 +01002949 assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}"
2950 "deviation /x {deviate add {config true;}}}", LYS_IN_YANG));
2951 logbuf_assert("Invalid deviation (/x) adding \"config\" property which already exists (with value \"config false\").");
2952
2953 assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}"
2954 "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG));
2955 logbuf_assert("Invalid deviation of mandatory in \"/top\" - container cannot hold mandatory statement.");
2956 assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}"
2957 "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG));
2958 logbuf_assert("Invalid deviation (/top) replacing \"mandatory\" property \"mandatory true\" which is not present.");
2959 assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}"
2960 "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG));
2961 logbuf_assert("Invalid deviation (/top/x) replacing \"mandatory\" property \"mandatory true\" which is not present.");
2962 assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}"
2963 "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG));
2964 logbuf_assert("Invalid deviation (/x) adding \"mandatory\" property which already exists (with value \"mandatory true\").");
Radek Krejci93dcc392019-02-19 10:43:38 +01002965
Radek Krejci551b12c2019-02-19 16:11:21 +01002966 assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}"
2967 "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG));
2968 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
2969 assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}"
2970 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
2971 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf-list.");
2972 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;}}"
2973 "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG));
2974 logbuf_assert("Invalid deviation (/ch) combining default case and mandatory choice.");
2975
2976 assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}"
2977 "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG));
2978 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 10 is bigger than max value 5.");
2979 assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}"
2980 "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG));
2981 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
2982 assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}"
2983 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
2984 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 5 is bigger than max value 1.");
2985 assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}"
2986 "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG));
2987 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
2988 assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}"
2989 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
2990 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
2991 assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}"
2992 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
2993 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
2994 assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}"
2995 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
2996 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
2997 assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}"
2998 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
2999 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3000 assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}"
3001 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3002 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3003 assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}"
3004 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3005 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3006 assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}"
3007 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3008 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3009 assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }"
3010 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3011 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3012
Radek Krejci33f72892019-02-21 10:36:58 +01003013 assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;"
3014 "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG));
3015 logbuf_assert("Invalid deviation (/x) of anyxml node - it is not possible to replace \"type\" property.");
3016 assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}"
3017 "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG));
3018 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3019
Radek Krejci05b774b2019-02-25 13:26:18 +01003020 assert_null(lys_parse_mem(ctx, "module oo {yang-version 1.1; namespace urn:oo;prefix oo; rpc test {input {container a {leaf b {type string;}}}}"
3021 "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
3022 logbuf_assert("Action \"invalid\" is placed inside another RPC/action.");
3023
Radek Krejciccd20f12019-02-15 14:12:27 +01003024 *state = NULL;
3025 ly_ctx_destroy(ctx, NULL);
3026}
3027
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003028int main(void)
3029{
3030 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01003031 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
3032 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
3033 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01003034 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
3035 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01003036 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01003037 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
3038 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01003039 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01003040 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01003041 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01003042 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01003043 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01003044 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
3045 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01003046 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01003047 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01003048 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003049 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01003050 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01003051 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01003052 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01003053 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01003054 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01003055 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejciccd20f12019-02-15 14:12:27 +01003056 cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003057 };
3058
3059 return cmocka_run_group_tests(tests, NULL, NULL);
3060}