blob: 3894f7053fab608214736ca55cd73738a212726e [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010020#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010021#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema.c"
24#include "../../src/context.c"
25#include "../../src/hash_table.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020026
Radek Krejcidd4e8d42018-10-16 14:55:43 +020027#include <stdarg.h>
28#include <stddef.h>
29#include <setjmp.h>
30#include <cmocka.h>
31
32#include <stdio.h>
33#include <string.h>
34
35#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020036
37#define BUFSIZE 1024
38char logbuf[BUFSIZE] = {0};
39
40/* set to 0 to printing error messages to stderr instead of checking them in code */
41#define ENABLE_LOGGER_CHECKING 1
42
43#if ENABLE_LOGGER_CHECKING
44static void
45logger(LY_LOG_LEVEL level, const char *msg, const char *path)
46{
47 (void) level; /* unused */
48
Radek Krejci87616bb2018-10-31 13:30:52 +010049 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020050 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
51 } else {
52 strncpy(logbuf, msg, BUFSIZE - 1);
53 }
54}
55#endif
56
57static int
58logger_setup(void **state)
59{
60 (void) state; /* unused */
61#if ENABLE_LOGGER_CHECKING
62 ly_set_log_clb(logger, 1);
63#endif
64 return 0;
65}
66
Radek Krejci4f28eda2018-11-12 11:46:16 +010067static int
68logger_teardown(void **state)
69{
70 (void) state; /* unused */
71#if ENABLE_LOGGER_CHECKING
72 if (*state) {
73 fprintf(stderr, "%s\n", logbuf);
74 }
75#endif
76 return 0;
77}
78
Radek Krejcidd4e8d42018-10-16 14:55:43 +020079void
80logbuf_clean(void)
81{
82 logbuf[0] = '\0';
83}
84
85#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010086# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020087#else
88# define logbuf_assert(str)
89#endif
90
Radek Krejcid05cbd92018-12-05 14:26:40 +010091static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
92 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
93 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
94{
95 *module_data = user_data;
96 *format = LYS_IN_YANG;
97 *free_module_data = NULL;
98 return LY_SUCCESS;
99}
100
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200101static void
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100102reset_mod(struct ly_ctx *ctx, struct lys_module *module)
103{
104 lysc_module_free(module->compiled, NULL);
105 lysp_module_free(module->parsed);
106
107 FREE_STRING(module->ctx, module->name);
108 FREE_STRING(module->ctx, module->ns);
109 FREE_STRING(module->ctx, module->prefix);
110 FREE_STRING(module->ctx, module->filepath);
111 FREE_STRING(module->ctx, module->org);
112 FREE_STRING(module->ctx, module->contact);
113 FREE_STRING(module->ctx, module->dsc);
114 FREE_STRING(module->ctx, module->ref);
Radek Krejci95710c92019-02-11 15:49:55 +0100115 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100116
117 memset(module, 0, sizeof *module);
118 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100119 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100120}
121
122static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200123test_module(void **state)
124{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100125 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200126
127 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +0100128 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200129 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200130 struct lysc_feature *f;
131 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200132
133 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200134 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100135 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100136 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200137
Radek Krejcif8f882a2018-10-31 14:51:15 +0100138 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
139 logbuf_assert("Invalid argument mod (lys_compile()).");
140 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
141 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100142 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100143 mod.implemented = 0;
144 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
145 assert_null(mod.compiled);
146 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100147 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200148 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100149 assert_string_equal("test", mod.name);
150 assert_string_equal("urn:test", mod.ns);
151 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200152 /* features */
153 assert_non_null(mod.compiled->features);
154 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200155 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200156 assert_non_null(f->iffeatures);
157 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200158 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200159 assert_non_null(iff->expr);
160 assert_non_null(iff->features);
161 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200162 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200163
Radek Krejci86d106e2018-10-18 09:53:19 +0200164 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200165
Radek Krejcif8f882a2018-10-31 14:51:15 +0100166 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200167 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200168
Radek Krejci86d106e2018-10-18 09:53:19 +0200169 lysc_module_free(mod.compiled, NULL);
170 mod.compiled = NULL;
171
Radek Krejci151a5b72018-10-19 14:21:44 +0200172 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200173 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100174 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
175 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
176 assert_null(mod.parsed);
177 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100178
179 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100180 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
181 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100182 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
183 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
184 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100185 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100186
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100187 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100188 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200189}
190
Radek Krejci151a5b72018-10-19 14:21:44 +0200191static void
192test_feature(void **state)
193{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100194 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200195
Radek Krejci313d9902018-11-08 09:42:58 +0100196 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100197 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200198 const char *str;
199 struct lysc_feature *f, *f1;
200
201 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
202 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100203 "feature orfeature {if-feature \"f1 or f2\";}\n"
204 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200205 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200206 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100207 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
208 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200209
Radek Krejci313d9902018-11-08 09:42:58 +0100210 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100211 reset_mod(ctx.ctx, &mod);
212
213 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100214 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200215 assert_non_null(mod.compiled);
216 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100217 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200218 /* all features are disabled by default */
219 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
220 assert_int_equal(0, lysc_feature_value(f));
221 }
222 /* enable f1 */
223 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200224 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200225 assert_int_equal(1, lysc_feature_value(f1));
226
Radek Krejci87616bb2018-10-31 13:30:52 +0100227 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200228 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200229 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100230 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200231 assert_int_equal(1, lysc_feature_value(f));
232
Radek Krejci87616bb2018-10-31 13:30:52 +0100233 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200234 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200235 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100236 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
237 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200238 assert_int_equal(0, lysc_feature_value(f));
239
240 /* first enable f2, so f5 can be enabled then */
241 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100242 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200243 assert_int_equal(1, lysc_feature_value(f));
244
245 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200246 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200247 assert_int_equal(0, lysc_feature_value(f));
248 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
249 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
250 assert_int_equal(0, lysc_feature_value(f));
251
Radek Krejci87616bb2018-10-31 13:30:52 +0100252 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200253 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200254 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
255 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200256 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100257 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200258 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200259 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200260 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200261 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200262
263 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200264 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200265 /* long evaluation of f8 to need to reallocate internal stack for operators */
266 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200267
Radek Krejci87616bb2018-10-31 13:30:52 +0100268 /* double negation of disabled f1 -> disabled */
269 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
270
Radek Krejci38920282018-11-01 09:24:16 +0100271 /* disable all features */
272 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
273 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
274 assert_int_equal(0, lys_feature_value(&mod, f->name));
275 }
276 /* re-setting already set feature */
277 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
278 assert_int_equal(0, lys_feature_value(&mod, "f1"));
279
280 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100281 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
282 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
283 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100284 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
285 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100286 /* test if not changed */
287 assert_int_equal(1, lys_feature_value(&mod, "f1"));
288 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100289
Radek Krejci0af46292019-01-11 16:02:31 +0100290 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;"
291 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
292 assert_non_null(modp->compiled);
293 assert_non_null(modp->compiled->features);
294 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
295 assert_non_null(modp->compiled->features[0].iffeatures);
296 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
297 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
298 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
299 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
300 assert_non_null(modp->compiled->features);
301 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
302 assert_non_null(modp->compiled->features[1].depfeatures);
303 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
304 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
305
Radek Krejci1aefdf72018-11-01 11:01:39 +0100306 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100307 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
308 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
309
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100310 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100311
312 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100313 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature f1;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100314 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100315 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100316 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100317
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100318 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100319 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100320 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100321 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100322
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100323 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100324 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100325 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100326 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100327
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100328 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100329 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100330 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100331 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100332
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100333 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100334 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100335 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100336 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100337
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100338 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100339 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100340 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100341 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100342
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100343 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100344 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100345 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100346 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100347
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100348 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100349 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
350 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100352
Radek Krejci0af46292019-01-11 16:02:31 +0100353 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
354 assert_null(lys_parse_mem(ctx.ctx, "module z{namespace urn:z; prefix z; include sz;feature f1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100355 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
356
Radek Krejci1aefdf72018-11-01 11:01:39 +0100357 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100358 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100359 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100360 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module c{namespace urn:c; prefix c; import a {prefix a;} feature f1; feature f2{if-feature 'a:f1';}}", LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100361 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
362 assert_int_equal(0, lys_feature_value(modp, "f1"));
363 assert_int_equal(1, lys_feature_value(modp, "f2"));
364
Radek Krejcid05cbd92018-12-05 14:26:40 +0100365 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100366 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200367}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200368
Radek Krejci478020e2018-10-30 16:02:14 +0100369static void
370test_identity(void **state)
371{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100372 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100373
374 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100375 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100376
377 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100378 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
379 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
380 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
381 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100382
383 assert_non_null(mod1->compiled);
384 assert_non_null(mod1->compiled->identities);
385 assert_non_null(mod2->compiled);
386 assert_non_null(mod2->compiled->identities);
387
388 assert_non_null(mod1->compiled->identities[0].derived);
389 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
390 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
391 assert_non_null(mod2->compiled->identities[0].derived);
392 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
393 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
394 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
395 assert_non_null(mod2->compiled->identities[1].derived);
396 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
397 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
398 assert_non_null(mod2->compiled->identities[2].derived);
399 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
400 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
401
Radek Krejci4d483a82019-01-17 13:12:50 +0100402 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
403 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
404 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
405 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
406
407 assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100408 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
409
Radek Krejci4d483a82019-01-17 13:12:50 +0100410 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
411 assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100412 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
413
Radek Krejci38222632019-02-12 16:55:05 +0100414 assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG));
415 logbuf_assert("Unable to find base (i2) of identity \"i1\".");
416
417 assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG));
418 logbuf_assert("Identity \"i1\" is derived from itself.");
419 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));
420 logbuf_assert("Identity \"i1\" is indirectly derived from itself.");
421
Radek Krejci7f2a5362018-11-28 13:05:37 +0100422 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100423 ly_ctx_destroy(ctx, NULL);
424}
425
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100426static void
427test_node_container(void **state)
428{
429 (void) state; /* unused */
430
431 struct ly_ctx *ctx;
432 struct lys_module *mod;
433 struct lysc_node_container *cont;
434
435 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
436 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 +0100437 assert_non_null(mod->compiled);
438 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
439 assert_int_equal(LYS_CONTAINER, cont->nodetype);
440 assert_string_equal("c", cont->name);
441 assert_true(cont->flags & LYS_CONFIG_W);
442 assert_true(cont->flags & LYS_STATUS_CURR);
443
Radek Krejci98094b32018-11-02 16:21:47 +0100444 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 +0100445 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100446 assert_non_null(mod->compiled);
447 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
448 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100449 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100450 assert_non_null((cont = (struct lysc_node_container*)cont->child));
451 assert_int_equal(LYS_CONTAINER, cont->nodetype);
452 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100453 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100454 assert_string_equal("child", cont->name);
455
456 ly_ctx_destroy(ctx, NULL);
457}
458
Radek Krejci0e5d8382018-11-28 16:37:53 +0100459static void
460test_node_leaflist(void **state)
461{
462 *state = test_node_leaflist;
463
464 struct ly_ctx *ctx;
465 struct lys_module *mod;
466 struct lysc_type *type;
467 struct lysc_node_leaflist *ll;
468
469 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
470
471 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
472 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
473 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
474 "leaf-list ll2 {type leafref {path ../target;}}"
475 "leaf target {type int8;}}",
476 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100477 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
478 assert_non_null(type);
479 assert_int_equal(1, type->refcount);
480 assert_int_equal(LY_TYPE_UNION, type->basetype);
481 assert_non_null(((struct lysc_type_union*)type)->types);
482 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
483 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
484 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
485 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
486 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
487 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
488 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
489 assert_non_null(type);
490 assert_int_equal(1, type->refcount);
491 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
492 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
493 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
494
Radek Krejci6bb080b2018-11-28 17:23:41 +0100495 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 +0100496 assert_non_null(mod->compiled);
497 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
498 assert_int_equal(0, ll->min);
499 assert_int_equal((uint32_t)-1, ll->max);
500
Radek Krejci6bb080b2018-11-28 17:23:41 +0100501 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 +0100502 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100503 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100504 assert_non_null(mod->compiled);
505 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
506 assert_non_null(ll->dflts);
507 assert_int_equal(3, ll->type->refcount);
508 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
509 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100510 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100511 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100512 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
513 assert_non_null(ll->dflts);
514 assert_int_equal(3, ll->type->refcount);
515 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
516 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100517 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
518
519 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
520 * TODO test also, RPC output parameters and notification content */
521 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
522 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100523 /* but warning is present: */
524 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
525 assert_non_null(mod->compiled);
526 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
527 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100528
529 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100530 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 +0100531 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
532
Radek Krejci096235c2019-01-11 11:12:19 +0100533 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 +0100534 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
535
536 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
537 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100538 assert_non_null(mod->compiled);
539 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
540 assert_non_null(ll->dflts);
541 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100542 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
543 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100544 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
545
Radek Krejci0e5d8382018-11-28 16:37:53 +0100546 *state = NULL;
547 ly_ctx_destroy(ctx, NULL);
548}
549
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100550static void
551test_node_list(void **state)
552{
553 *state = test_node_list;
554
555 struct ly_ctx *ctx;
556 struct lys_module *mod;
557 struct lysc_node_list *list;
558
559 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
560
561 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
562 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
563 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100564 list = (struct lysc_node_list*)mod->compiled->data;
565 assert_non_null(list);
566 assert_non_null(list->keys);
567 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
568 assert_string_equal("x", list->keys[0]->name);
569 assert_string_equal("y", list->keys[1]->name);
570 assert_non_null(list->child);
571 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
572 assert_true(list->child->flags & LYS_KEY);
573 assert_true(list->child->next->flags & LYS_KEY);
574 list = (struct lysc_node_list*)mod->compiled->data->next;
575 assert_non_null(list);
576 assert_null(list->keys);
577 assert_non_null(list->child);
578 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
579 assert_false(list->child->flags & LYS_KEY);
580
581 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
582 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100583 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100584 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100585 list = (struct lysc_node_list*)mod->compiled->data;
586 assert_non_null(list);
587 assert_string_equal("l", list->name);
588 assert_non_null(list->keys);
589 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
590 assert_string_equal("a", list->keys[0]->name);
591 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100592 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100593 assert_non_null(list->uniques);
594 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
595 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
596 assert_string_equal("a", list->uniques[0][0]->name);
597 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
598 assert_string_equal("b", list->uniques[0][1]->name);
599 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
600 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
601 assert_string_equal("e", list->uniques[1][0]->name);
602 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
603 assert_string_equal("d", list->uniques[1][1]->name);
604 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
605
Radek Krejci665421c2018-12-05 08:03:39 +0100606 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
607 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100608 list = (struct lysc_node_list*)mod->compiled->data;
609 assert_non_null(list);
610 assert_string_equal("l", list->name);
611 assert_non_null(list->keys);
612 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
613 assert_string_equal("a", list->keys[0]->name);
614 assert_true(list->keys[0]->flags & LYS_KEY);
615 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
616
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100617 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100618 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 +0100619 logbuf_assert("Missing key in list representing configuration data.");
620
Radek Krejci096235c2019-01-11 11:12:19 +0100621 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
622 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100623 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
624
Radek Krejci096235c2019-01-11 11:12:19 +0100625 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
626 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100627 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
628
Radek Krejci096235c2019-01-11 11:12:19 +0100629 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
630 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100631 logbuf_assert("Key of the configuration list must not be status leaf.");
632
Radek Krejci096235c2019-01-11 11:12:19 +0100633 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
634 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100635 logbuf_assert("Configuration node cannot be child of any state data node.");
636
Radek Krejci096235c2019-01-11 11:12:19 +0100637 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
638 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100639 logbuf_assert("The list's key \"x\" not found.");
640
Radek Krejci096235c2019-01-11 11:12:19 +0100641 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
642 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100643 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
644
Radek Krejci096235c2019-01-11 11:12:19 +0100645 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
646 "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 +0100647 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
648
Radek Krejci096235c2019-01-11 11:12:19 +0100649 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
650 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100651 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
652
Radek Krejci096235c2019-01-11 11:12:19 +0100653 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
654 "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 +0100655 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
656
Radek Krejci096235c2019-01-11 11:12:19 +0100657 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
658 "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 +0100659 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
660
Radek Krejci096235c2019-01-11 11:12:19 +0100661 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
662 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100663 logbuf_assert("Duplicated key identifier \"x\".");
664
Radek Krejci096235c2019-01-11 11:12:19 +0100665 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
666 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100667 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
668
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100669 *state = NULL;
670 ly_ctx_destroy(ctx, NULL);
671}
672
Radek Krejci056d0a82018-12-06 16:57:25 +0100673static void
674test_node_choice(void **state)
675{
676 *state = test_node_choice;
677
678 struct ly_ctx *ctx;
679 struct lys_module *mod;
680 struct lysc_node_choice *ch;
681 struct lysc_node_case *cs;
682
683 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
684
685 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100686 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
687 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100688 ch = (struct lysc_node_choice*)mod->compiled->data;
689 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100690 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100691 cs = ch->cases;
692 assert_non_null(cs);
693 assert_string_equal("a", cs->name);
694 assert_ptr_equal(ch, cs->parent);
695 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100696 assert_string_equal("a1", cs->child->name);
697 assert_non_null(cs->child->next);
698 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100699 assert_ptr_equal(cs, cs->child->parent);
700 cs = (struct lysc_node_case*)cs->next;
701 assert_non_null(cs);
702 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100703 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100704 assert_ptr_equal(ch, cs->parent);
705 assert_non_null(cs->child);
706 assert_string_equal("b", cs->child->name);
707 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100708 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
709 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100710 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100711 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100712
Radek Krejci096235c2019-01-11 11:12:19 +0100713 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
714 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100715 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100716 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
717 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100718 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100719 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
720 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100721 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100722 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
723 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100724 logbuf_assert("Duplicate identifier \"b\" of case statement.");
725
Radek Krejci096235c2019-01-11 11:12:19 +0100726 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
727 "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 +0100728 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100729 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
730 "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 +0100731 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100732 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
733 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100734 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
735 /* TODO check with mandatory nodes from augment placed into the case */
736
Radek Krejci056d0a82018-12-06 16:57:25 +0100737 *state = NULL;
738 ly_ctx_destroy(ctx, NULL);
739}
740
Radek Krejci9800fb82018-12-13 14:26:23 +0100741static void
742test_node_anydata(void **state)
743{
744 *state = test_node_anydata;
745
746 struct ly_ctx *ctx;
747 struct lys_module *mod;
748 struct lysc_node_anydata *any;
749
750 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
751
752 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
753 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100754 any = (struct lysc_node_anydata*)mod->compiled->data;
755 assert_non_null(any);
756 assert_int_equal(LYS_ANYDATA, any->nodetype);
757 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
758
759 logbuf_clean();
760 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
761 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100762 any = (struct lysc_node_anydata*)mod->compiled->data;
763 assert_non_null(any);
764 assert_int_equal(LYS_ANYXML, any->nodetype);
765 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
766 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
767
768 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100769 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 +0100770 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
771
772 *state = NULL;
773 ly_ctx_destroy(ctx, NULL);
774}
775
Radek Krejci0f07bed2018-11-13 14:01:40 +0100776/**
777 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
778 * do not test the expression itself
779 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100780static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100781test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100782{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100783 *state = test_type_range;
784
785 struct ly_ctx *ctx;
786 struct lys_module *mod;
787 struct lysc_type *type;
788
789 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
790
791 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 +0100792 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
793 assert_non_null(type);
794 assert_int_equal(LY_TYPE_INT8, type->basetype);
795 assert_non_null(((struct lysc_type_num*)type)->range);
796 assert_non_null(((struct lysc_type_num*)type)->range->parts);
797 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
798 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
799 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
800 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
801 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
802
803 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 +0100804 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
805 assert_non_null(type);
806 assert_int_equal(LY_TYPE_INT16, type->basetype);
807 assert_non_null(((struct lysc_type_num*)type)->range);
808 assert_non_null(((struct lysc_type_num*)type)->range->parts);
809 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
810 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
811 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
812 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
813 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
814
815 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 +0100816 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
817 assert_non_null(type);
818 assert_int_equal(LY_TYPE_INT32, type->basetype);
819 assert_non_null(((struct lysc_type_num*)type)->range);
820 assert_non_null(((struct lysc_type_num*)type)->range->parts);
821 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
822 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
823 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
824 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
825 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
826
827 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 +0100828 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
829 assert_non_null(type);
830 assert_int_equal(LY_TYPE_INT64, type->basetype);
831 assert_non_null(((struct lysc_type_num*)type)->range);
832 assert_non_null(((struct lysc_type_num*)type)->range->parts);
833 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
834 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
835 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
836 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
837 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
838
839 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 +0100840 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
841 assert_non_null(type);
842 assert_int_equal(LY_TYPE_UINT8, type->basetype);
843 assert_non_null(((struct lysc_type_num*)type)->range);
844 assert_non_null(((struct lysc_type_num*)type)->range->parts);
845 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
846 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
847 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
848 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
849 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
850
851 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 +0100852 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
853 assert_non_null(type);
854 assert_int_equal(LY_TYPE_UINT16, type->basetype);
855 assert_non_null(((struct lysc_type_num*)type)->range);
856 assert_non_null(((struct lysc_type_num*)type)->range->parts);
857 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
858 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
859 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
860 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
861 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
862
863 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 +0100864 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
865 assert_non_null(type);
866 assert_int_equal(LY_TYPE_UINT32, type->basetype);
867 assert_non_null(((struct lysc_type_num*)type)->range);
868 assert_non_null(((struct lysc_type_num*)type)->range->parts);
869 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
870 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
871 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
872 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
873 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
874
875 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 +0100876 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
877 assert_non_null(type);
878 assert_int_equal(LY_TYPE_UINT64, type->basetype);
879 assert_non_null(((struct lysc_type_num*)type)->range);
880 assert_non_null(((struct lysc_type_num*)type)->range->parts);
881 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
882 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
883 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
884 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
885 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
886
887 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
888 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100889 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
890 assert_non_null(type);
891 assert_int_equal(3, type->refcount);
892 assert_int_equal(LY_TYPE_UINT8, type->basetype);
893 assert_non_null(((struct lysc_type_num*)type)->range);
894 assert_non_null(((struct lysc_type_num*)type)->range->parts);
895 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
896
Radek Krejcif1394042019-01-08 10:53:34 +0100897 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
898 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
899 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +0100900 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
901 assert_non_null(type);
902 assert_int_equal(1, type->refcount);
903 assert_int_equal(LY_TYPE_UINT8, type->basetype);
904 assert_non_null(((struct lysc_type_num*)type)->range);
905 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
906 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
907 assert_non_null(((struct lysc_type_num*)type)->range->parts);
908 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
909 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
910 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
911
Radek Krejci0f07bed2018-11-13 14:01:40 +0100912 *state = NULL;
913 ly_ctx_destroy(ctx, NULL);
914}
915
916static void
917test_type_length(void **state)
918{
919 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100920
921 struct ly_ctx *ctx;
922 struct lys_module *mod;
923 struct lysc_type *type;
924
925 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
926
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100927 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 +0100928 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
929 assert_non_null(type);
930 assert_non_null(((struct lysc_type_bin*)type)->length);
931 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100932 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
933 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100934 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
935 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
936 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
937
938 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 +0100939 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
940 assert_non_null(type);
941 assert_non_null(((struct lysc_type_bin*)type)->length);
942 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
943 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100944 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
945 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100946
947 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 +0100948 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
949 assert_non_null(type);
950 assert_non_null(((struct lysc_type_bin*)type)->length);
951 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
952 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
953 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100954 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100955
956 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 +0100957 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
958 assert_non_null(type);
959 assert_non_null(((struct lysc_type_bin*)type)->length);
960 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
961 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
962 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
963 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
964
965 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 +0100966 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
967 assert_non_null(type);
968 assert_non_null(((struct lysc_type_bin*)type)->length);
969 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
970 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
971 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
972 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
973
974 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 +0100975 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
976 assert_non_null(type);
977 assert_non_null(((struct lysc_type_bin*)type)->length);
978 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
979 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
980 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
981 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
982 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
983 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
984
985 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 +0100986 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
987 assert_non_null(type);
988 assert_non_null(((struct lysc_type_bin*)type)->length);
989 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
990 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
991 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
992 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
993 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
994 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
995
Radek Krejcib31c8992018-11-12 16:29:16 +0100996 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
997 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100998 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
999 assert_non_null(type);
1000 assert_non_null(((struct lysc_type_bin*)type)->length);
1001 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1002 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1003 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1004 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1005
1006 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1007 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001008 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1009 assert_non_null(type);
1010 assert_non_null(((struct lysc_type_bin*)type)->length);
1011 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1012 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1013 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1014 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1015
1016 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1017 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29: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(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1023 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1024 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1025 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1026 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1027
1028 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 +01001029 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001030 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1031 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001032 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001033 assert_non_null(((struct lysc_type_bin*)type)->length);
1034 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1035 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1036 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001037 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001038 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1039 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001040 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001041 assert_non_null(((struct lysc_type_bin*)type)->length);
1042 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1043 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1044 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1045 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1046
Radek Krejci56aa27c2018-11-13 14:21:59 +01001047 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1048 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001049 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1050 assert_non_null(type);
1051 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001052 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001053 assert_non_null(((struct lysc_type_str*)type)->length);
1054 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1055 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1056 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1057 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001058
Radek Krejci5969f272018-11-23 10:03:58 +01001059 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1060 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001061 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1062 assert_non_null(type);
1063 assert_int_equal(LY_TYPE_STRING, type->basetype);
1064 assert_int_equal(1, type->refcount);
1065 assert_non_null(((struct lysc_type_str*)type)->length);
1066 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1067 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1068 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1069 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1070
Radek Krejci4f28eda2018-11-12 11:46:16 +01001071 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001072 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 +01001073 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001074 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 +01001075 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001076 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 +01001077 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001078 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 +01001079 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001080 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 +01001081 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001082 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 +01001083 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001084 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 +01001085 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001086 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 +01001087 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001088 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 +01001089 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001090 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 +01001091 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001092 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 +01001093 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001094 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 +01001095 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001096
Radek Krejci096235c2019-01-11 11:12:19 +01001097 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1098 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001099 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001100 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1101 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001102 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001103 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1104 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001105 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001106 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1107 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001108 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 +01001109 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 +01001110 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001111 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001112 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 +01001113 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001114 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001115 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 +01001116 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001117 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001118
Radek Krejci096235c2019-01-11 11:12:19 +01001119 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 +01001120 logbuf_assert("Invalid type restrictions for binary type.");
1121
Radek Krejci4f28eda2018-11-12 11:46:16 +01001122 *state = NULL;
1123 ly_ctx_destroy(ctx, NULL);
1124}
1125
Radek Krejcicda74152018-11-13 15:27:32 +01001126static void
1127test_type_pattern(void **state)
1128{
1129 *state = test_type_pattern;
1130
1131 struct ly_ctx *ctx;
1132 struct lys_module *mod;
1133 struct lysc_type *type;
1134
1135 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1136
Radek Krejci027d5802018-11-14 16:57:28 +01001137 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 +01001138 "pattern .* {error-app-tag errortag;error-message error;}"
1139 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001140 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1141 assert_non_null(type);
1142 assert_non_null(((struct lysc_type_str*)type)->patterns);
1143 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1144 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1145 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1146 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1147 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1148 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1149 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1150
1151 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1152 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001153 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1154 assert_non_null(type);
1155 assert_int_equal(LY_TYPE_STRING, type->basetype);
1156 assert_int_equal(1, type->refcount);
1157 assert_non_null(((struct lysc_type_str*)type)->patterns);
1158 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1159 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1160 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1161
Radek Krejci04bc1e92018-11-14 14:28:13 +01001162 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1163 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001164 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1165 assert_non_null(type);
1166 assert_int_equal(LY_TYPE_STRING, type->basetype);
1167 assert_int_equal(1, type->refcount);
1168 assert_non_null(((struct lysc_type_str*)type)->patterns);
1169 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1170 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1171
Radek Krejcicda74152018-11-13 15:27:32 +01001172 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001173 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 +01001174 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001175 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1176 assert_non_null(type);
1177 assert_non_null(((struct lysc_type_str*)type)->patterns);
1178 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1179 /* TODO check some data "^Å™$" */
1180
1181 *state = NULL;
1182 ly_ctx_destroy(ctx, NULL);
1183}
1184
Radek Krejci8b764662018-11-14 14:15:13 +01001185static void
1186test_type_enum(void **state)
1187{
1188 *state = test_type_enum;
1189
1190 struct ly_ctx *ctx;
1191 struct lys_module *mod;
1192 struct lysc_type *type;
1193
1194 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1195
Radek Krejci027d5802018-11-14 16:57:28 +01001196 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 +01001197 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1198 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001199 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1200 assert_non_null(type);
1201 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1202 assert_non_null(((struct lysc_type_enum*)type)->enums);
1203 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1204 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1205 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1206 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1207 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1208 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1209 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1210 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1211 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1212 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1213 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1214 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1215 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1216 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1217
Radek Krejci027d5802018-11-14 16:57:28 +01001218 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 {"
1219 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001220 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1221 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001222 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1223 assert_non_null(type);
1224 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1225 assert_non_null(((struct lysc_type_enum*)type)->enums);
1226 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1227 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1228 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1229 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1230 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1231
1232
1233 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001234 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1235 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1236 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 +01001237 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1238 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1239 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1240 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1241 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1242 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1243 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1244 "enum one; enum one;}}}", LYS_IN_YANG));
1245 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1246 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1247 "enum '';}}}", LYS_IN_YANG));
1248 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1249 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1250 "enum ' x';}}}", LYS_IN_YANG));
1251 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1252 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1253 "enum 'x ';}}}", LYS_IN_YANG));
1254 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1255 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1256 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1257 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1258
Radek Krejci096235c2019-01-11 11:12:19 +01001259 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 +01001260 logbuf_assert("Missing enum substatement for enumeration type.");
1261
Radek Krejci096235c2019-01-11 11:12:19 +01001262 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 +01001263 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001264 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1265
Radek Krejci096235c2019-01-11 11:12:19 +01001266 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 +01001267 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001268 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 +01001269 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 +01001270 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001271 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1272
Radek Krejci096235c2019-01-11 11:12:19 +01001273 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 +01001274 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001275 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1276
Radek Krejci096235c2019-01-11 11:12:19 +01001277 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001278 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001279 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001280
Radek Krejci096235c2019-01-11 11:12:19 +01001281 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 +01001282 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001283 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1284
Radek Krejci8b764662018-11-14 14:15:13 +01001285 *state = NULL;
1286 ly_ctx_destroy(ctx, NULL);
1287}
1288
1289static void
1290test_type_bits(void **state)
1291{
1292 *state = test_type_bits;
1293
1294 struct ly_ctx *ctx;
1295 struct lys_module *mod;
1296 struct lysc_type *type;
1297
1298 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1299
Radek Krejci027d5802018-11-14 16:57:28 +01001300 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 +01001301 "bit automin; bit one {if-feature f; position 1;}"
1302 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001303 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1304 assert_non_null(type);
1305 assert_int_equal(LY_TYPE_BITS, type->basetype);
1306 assert_non_null(((struct lysc_type_bits*)type)->bits);
1307 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1308 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1309 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1310 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1311 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1312 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1313 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1314 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1315 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1316 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1317 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1318 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1319
Radek Krejci027d5802018-11-14 16:57:28 +01001320 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 {"
1321 "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 +01001322 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001323 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1324 assert_non_null(type);
1325 assert_int_equal(LY_TYPE_BITS, type->basetype);
1326 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001327 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1328 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1329 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1330 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1331 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1332 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1333 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001334
1335
1336 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001337 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1338 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1339 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 +01001340 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1341 "bit one {position -1;}}}}", LYS_IN_YANG));
1342 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1343 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1344 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1345 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1346 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1347 "bit one; bit one;}}}", LYS_IN_YANG));
1348 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1349 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1350 "bit '11';}}}", LYS_IN_YANG));
1351 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1352 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1353 "bit 'x1$1';}}}", LYS_IN_YANG));
1354 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1355
Radek Krejci096235c2019-01-11 11:12:19 +01001356 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 +01001357 logbuf_assert("Missing bit substatement for bits type.");
1358
Radek Krejci096235c2019-01-11 11:12:19 +01001359 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 +01001360 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001361 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1362
Radek Krejci096235c2019-01-11 11:12:19 +01001363 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 +01001364 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001365 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 +01001366 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 +01001367 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001368 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1369
Radek Krejci096235c2019-01-11 11:12:19 +01001370 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 +01001371 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001372 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1373
Radek Krejci096235c2019-01-11 11:12:19 +01001374 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001375 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001376 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001377
Radek Krejci096235c2019-01-11 11:12:19 +01001378 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 +01001379 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001380 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1381
Radek Krejci8b764662018-11-14 14:15:13 +01001382 *state = NULL;
1383 ly_ctx_destroy(ctx, NULL);
1384}
1385
Radek Krejci6cba4292018-11-15 17:33:29 +01001386static void
1387test_type_dec64(void **state)
1388{
1389 *state = test_type_dec64;
1390
1391 struct ly_ctx *ctx;
1392 struct lys_module *mod;
1393 struct lysc_type *type;
1394
1395 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1396
1397 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1398 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001399 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1400 assert_non_null(type);
1401 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1402 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1403 assert_non_null(((struct lysc_type_dec*)type)->range);
1404 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1405 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1406 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1407 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1408
1409 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1410 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001411 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1412 assert_non_null(type);
1413 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1414 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1415 assert_non_null(((struct lysc_type_dec*)type)->range);
1416 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1417 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1418 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1419 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1420 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1421 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1422 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1423 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1424
1425 /* invalid cases */
1426 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1427 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1428 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1429 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1430 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1431 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1432
Radek Krejci096235c2019-01-11 11:12:19 +01001433 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 +01001434 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1435
Radek Krejci096235c2019-01-11 11:12:19 +01001436 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 +01001437 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001438
Radek Krejci096235c2019-01-11 11:12:19 +01001439 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 +01001440 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001441 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1442
Radek Krejci096235c2019-01-11 11:12:19 +01001443 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 +01001444 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001445 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1446
Radek Krejci096235c2019-01-11 11:12:19 +01001447 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 +01001448 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001449 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1450
Radek Krejci096235c2019-01-11 11:12:19 +01001451 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 +01001452 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001453 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1454
Radek Krejci6cba4292018-11-15 17:33:29 +01001455 *state = NULL;
1456 ly_ctx_destroy(ctx, NULL);
1457}
1458
Radek Krejci16c0f822018-11-16 10:46:10 +01001459static void
1460test_type_instanceid(void **state)
1461{
1462 *state = test_type_instanceid;
1463
1464 struct ly_ctx *ctx;
1465 struct lys_module *mod;
1466 struct lysc_type *type;
1467
1468 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1469
1470 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1471 "leaf l1 {type instance-identifier {require-instance true;}}"
1472 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001473 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1474 assert_non_null(type);
1475 assert_int_equal(LY_TYPE_INST, type->basetype);
1476 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1477
1478 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1479 assert_non_null(type);
1480 assert_int_equal(LY_TYPE_INST, type->basetype);
1481 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1482
1483 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1484 assert_non_null(type);
1485 assert_int_equal(LY_TYPE_INST, type->basetype);
1486 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1487
1488 /* invalid cases */
1489 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1490 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1491
Radek Krejci096235c2019-01-11 11:12:19 +01001492 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 +01001493 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1494
1495 *state = NULL;
1496 ly_ctx_destroy(ctx, NULL);
1497}
1498
Radek Krejci555cb5b2018-11-16 14:54:33 +01001499static void
1500test_type_identityref(void **state)
1501{
1502 *state = test_type_identityref;
1503
1504 struct ly_ctx *ctx;
1505 struct lys_module *mod;
1506 struct lysc_type *type;
1507
1508 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1509
1510 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;}"
1511 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001512 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001513 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1514 assert_non_null(type);
1515 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1516 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1517 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1518 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1519
1520 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1521 assert_non_null(type);
1522 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1523 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1524 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1525 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1526 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1527
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001528 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1529 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001530 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1531 assert_non_null(type);
1532 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1533 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1534 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1535 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1536 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1537
Radek Krejci555cb5b2018-11-16 14:54:33 +01001538 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001539 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 +01001540 logbuf_assert("Missing base substatement for identityref type.");
1541
Radek Krejci096235c2019-01-11 11:12:19 +01001542 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001543 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001544 logbuf_assert("Missing base substatement for identityref type mytype.");
1545
Radek Krejci096235c2019-01-11 11:12:19 +01001546 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 +01001547 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001548 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001549
Radek Krejci096235c2019-01-11 11:12:19 +01001550 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 +01001551 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001552 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001553
Radek Krejci096235c2019-01-11 11:12:19 +01001554 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001555 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001556 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1557
Radek Krejci096235c2019-01-11 11:12:19 +01001558 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 +01001559 logbuf_assert("Unable to find base (j) of identityref.");
1560
Radek Krejci096235c2019-01-11 11:12:19 +01001561 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 +01001562 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1563
Radek Krejci555cb5b2018-11-16 14:54:33 +01001564 *state = NULL;
1565 ly_ctx_destroy(ctx, NULL);
1566}
1567
Radek Krejcia3045382018-11-22 14:30:31 +01001568static void
1569test_type_leafref(void **state)
1570{
1571 *state = test_type_leafref;
1572
1573 struct ly_ctx *ctx;
1574 struct lys_module *mod;
1575 struct lysc_type *type;
1576 const char *path, *name, *prefix;
1577 size_t prefix_len, name_len;
1578 int parent_times, has_predicate;
1579
1580 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1581
1582 /* lys_path_token() */
1583 path = "invalid_path";
1584 parent_times = 0;
1585 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1586 path = "..";
1587 parent_times = 0;
1588 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1589 path = "..[";
1590 parent_times = 0;
1591 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1592 path = "../";
1593 parent_times = 0;
1594 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1595 path = "/";
1596 parent_times = 0;
1597 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1598
1599 path = "../../pref:id/xxx[predicate]/invalid!!!";
1600 parent_times = 0;
1601 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1602 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1603 assert_int_equal(4, prefix_len);
1604 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1605 assert_int_equal(2, name_len);
1606 assert_int_equal(0, strncmp("id", name, name_len));
1607 assert_int_equal(2, parent_times);
1608 assert_int_equal(0, has_predicate);
1609 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1610 assert_string_equal("[predicate]/invalid!!!", path);
1611 assert_int_equal(0, prefix_len);
1612 assert_null(prefix);
1613 assert_int_equal(3, name_len);
1614 assert_int_equal(0, strncmp("xxx", name, name_len));
1615 assert_int_equal(1, has_predicate);
1616 path += 11;
1617 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1618 assert_string_equal("!!!", path);
1619 assert_int_equal(0, prefix_len);
1620 assert_null(prefix);
1621 assert_int_equal(7, name_len);
1622 assert_int_equal(0, strncmp("invalid", name, name_len));
1623
1624 path = "/absolute/prefix:path";
1625 parent_times = 0;
1626 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1627 assert_string_equal("/prefix:path", path);
1628 assert_int_equal(0, prefix_len);
1629 assert_null(prefix);
1630 assert_int_equal(8, name_len);
1631 assert_int_equal(0, strncmp("absolute", name, name_len));
1632 assert_int_equal(-1, parent_times);
1633 assert_int_equal(0, has_predicate);
1634 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1635 assert_int_equal(0, *path);
1636 assert_int_equal(6, prefix_len);
1637 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1638 assert_int_equal(4, name_len);
1639 assert_int_equal(0, strncmp("path", name, name_len));
1640 assert_int_equal(0, has_predicate);
1641
1642 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001643 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 +01001644 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1645 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001646 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1647 assert_non_null(type);
1648 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1649 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001650 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001651 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1652 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001653 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1654 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1655 assert_non_null(type);
1656 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1657 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001658 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1659 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1660 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001661 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1662
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001663 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 +01001664 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001665 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001666 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1667 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001668 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001669 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1670 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1671 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001672 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1673 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001674 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1675
1676 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001677 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 +01001678 "typedef mytype3 {type c:mytype {require-instance false;}}"
1679 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001680 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1681 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001682 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001683 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1684 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1685 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001686 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1687 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001688 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1689 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1690 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001691 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001692 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1693 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1694 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1695 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1696
Radek Krejci4ce68932018-11-28 12:53:36 +01001697 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1698 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1699 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001700 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1701 assert_non_null(type);
1702 assert_int_equal(1, type->refcount);
1703 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1704 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1705 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1706 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1707 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1708 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1709
Radek Krejci58d171e2018-11-23 13:50:55 +01001710 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001711 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 +01001712 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1713 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001714 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1715 assert_non_null(type);
1716 assert_int_equal(1, type->refcount);
1717 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1718 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1719 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1720 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1721 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1722
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001723 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1724 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1725 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001726 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001727 LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +01001728 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001729 assert_non_null(type);
1730 assert_int_equal(1, type->refcount);
1731 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001732 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001733 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1734 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1735 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001736
Radek Krejci20e8a182018-12-07 11:43:21 +01001737 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1738 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1739 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1740 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001741 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1742 assert_non_null(type);
1743 assert_int_equal(1, type->refcount);
1744 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1745 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1746 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1747 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1748 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1749
Radek Krejcia3045382018-11-22 14:30:31 +01001750 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001751 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 +01001752 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001753 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001754 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 +01001755 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001756 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001757 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 +01001758 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001759 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 +01001760 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 +01001761 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001762 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001763 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 +01001764 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001765 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001766 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 +01001767 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001768 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001769 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 +01001770 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001771 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001772 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001773 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001774 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001775 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001776 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001777 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001778 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001779 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001780 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1781 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001782 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001783 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001784 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 +01001785
Radek Krejci096235c2019-01-11 11:12:19 +01001786 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001787 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001788 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001789 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 +01001790 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001791 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1792
Radek Krejci096235c2019-01-11 11:12:19 +01001793 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001794 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1795 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1796 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1797 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001798 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1799
Radek Krejci096235c2019-01-11 11:12:19 +01001800 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001801 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1802 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1803 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1804 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001805 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1806
Radek Krejci096235c2019-01-11 11:12:19 +01001807 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001808 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1809 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1810 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1811 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001812 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1813
Radek Krejci096235c2019-01-11 11:12:19 +01001814 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001815 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1816 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1817 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1818 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001819 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1820
Radek Krejci096235c2019-01-11 11:12:19 +01001821 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001822 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1823 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1824 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1825 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001826 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1827
Radek Krejci096235c2019-01-11 11:12:19 +01001828 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001829 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1830 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1831 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1832 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001833 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1834
Radek Krejci096235c2019-01-11 11:12:19 +01001835 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001836 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1837 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1838 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1839 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001840 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1841
Radek Krejci096235c2019-01-11 11:12:19 +01001842 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001843 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1844 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1845 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
1846 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001847 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1848
Radek Krejci096235c2019-01-11 11:12:19 +01001849 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001850 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1851 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1852 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
1853 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001854 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1855
Radek Krejci096235c2019-01-11 11:12:19 +01001856 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001857 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1858 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1859 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
1860 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001861 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1862
Radek Krejci096235c2019-01-11 11:12:19 +01001863 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001864 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1865 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1866 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
1867 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001868 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1869
Radek Krejci096235c2019-01-11 11:12:19 +01001870 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001871 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1872 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1873 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1874 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001875 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1876
Radek Krejci096235c2019-01-11 11:12:19 +01001877 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001878 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1879 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1880 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
1881 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001882 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1883
Radek Krejci096235c2019-01-11 11:12:19 +01001884 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001885 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1886 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
1887 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1888 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001889 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1890
Radek Krejci096235c2019-01-11 11:12:19 +01001891 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001892 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1893 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1894 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1895 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001896 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1897
Radek Krejci412ddfa2018-11-23 11:44:11 +01001898 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01001899 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01001900 "leaf ref1 {type leafref {path /ref2;}}"
1901 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001902 "leaf ref3 {type leafref {path /ref4;}}"
1903 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001904 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1905
Radek Krejcia3045382018-11-22 14:30:31 +01001906 *state = NULL;
1907 ly_ctx_destroy(ctx, NULL);
1908}
1909
Radek Krejci43699232018-11-23 14:59:46 +01001910static void
1911test_type_empty(void **state)
1912{
1913 *state = test_type_empty;
1914
1915 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01001916
1917 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1918
1919 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01001920 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01001921 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001922 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1923
Radek Krejci096235c2019-01-11 11:12:19 +01001924 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 +01001925 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001926 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1927
1928 *state = NULL;
1929 ly_ctx_destroy(ctx, NULL);
1930}
1931
Radek Krejcicdfecd92018-11-26 11:27:32 +01001932
1933static void
1934test_type_union(void **state)
1935{
1936 *state = test_type_union;
1937
1938 struct ly_ctx *ctx;
1939 struct lys_module *mod;
1940 struct lysc_type *type;
1941
1942 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1943
1944 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
1945 "typedef mytype {type union {type int8; type mybasetype;}}"
1946 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001947 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1948 assert_non_null(type);
1949 assert_int_equal(2, type->refcount);
1950 assert_int_equal(LY_TYPE_UNION, type->basetype);
1951 assert_non_null(((struct lysc_type_union*)type)->types);
1952 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1953 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
1954 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1955
1956 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
1957 "typedef mytype {type union {type int8; type mybasetype;}}"
1958 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001959 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1960 assert_non_null(type);
1961 assert_int_equal(1, type->refcount);
1962 assert_int_equal(LY_TYPE_UNION, type->basetype);
1963 assert_non_null(((struct lysc_type_union*)type)->types);
1964 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1965 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1966 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
1967 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1968
1969 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
1970 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001971 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
1972 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01001973 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001974 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1975 assert_non_null(type);
1976 assert_int_equal(1, type->refcount);
1977 assert_int_equal(LY_TYPE_UNION, type->basetype);
1978 assert_non_null(((struct lysc_type_union*)type)->types);
1979 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1980 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1981 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
1982 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1983 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
1984 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
1985
Radek Krejci6be5ff12018-11-26 13:18:15 +01001986 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01001987 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001988 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001989 logbuf_assert("Missing type substatement for union type mytype.");
1990
Radek Krejci096235c2019-01-11 11:12:19 +01001991 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
1992 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01001993
Radek Krejci096235c2019-01-11 11:12:19 +01001994 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 +01001995 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001996 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
1997
Radek Krejci096235c2019-01-11 11:12:19 +01001998 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 +01001999 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002000 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2001
Radek Krejcicdfecd92018-11-26 11:27:32 +01002002 *state = NULL;
2003 ly_ctx_destroy(ctx, NULL);
2004}
2005
2006static void
2007test_type_dflt(void **state)
2008{
2009 *state = test_type_union;
2010
2011 struct ly_ctx *ctx;
2012 struct lys_module *mod;
2013 struct lysc_type *type;
2014
2015 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2016
2017 /* default is not inherited from union's types */
2018 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2019 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002020 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2021 assert_non_null(type);
2022 assert_int_equal(1, type->refcount);
2023 assert_int_equal(LY_TYPE_UNION, type->basetype);
2024 assert_non_null(((struct lysc_type_union*)type)->types);
2025 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2026 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2027 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2028 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2029 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2030
2031 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2032 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002033 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2034 assert_non_null(type);
2035 assert_int_equal(2, type->refcount);
2036 assert_int_equal(LY_TYPE_STRING, type->basetype);
2037 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2038 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2039
2040 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2041 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002042 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2043 assert_non_null(type);
2044 assert_int_equal(2, type->refcount);
2045 assert_int_equal(LY_TYPE_STRING, type->basetype);
2046 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2047 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2048
Radek Krejci6be5ff12018-11-26 13:18:15 +01002049 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2050 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2051 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002052 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2053 assert_non_null(type);
2054 assert_int_equal(4, type->refcount);
2055 assert_int_equal(LY_TYPE_STRING, type->basetype);
2056 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2057 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2058 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2059 assert_non_null(type);
2060 assert_int_equal(4, type->refcount);
2061 assert_int_equal(LY_TYPE_STRING, type->basetype);
2062 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2063 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2064
2065 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2066 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002067 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2068 assert_non_null(type);
2069 assert_int_equal(3, type->refcount);
2070 assert_int_equal(LY_TYPE_STRING, type->basetype);
2071 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2072 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2073
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002074 /* mandatory leaf does not takes default value from type */
2075 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2076 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002077 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2078 assert_non_null(type);
2079 assert_int_equal(LY_TYPE_STRING, type->basetype);
2080 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2081 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2082
Radek Krejcicdfecd92018-11-26 11:27:32 +01002083 *state = NULL;
2084 ly_ctx_destroy(ctx, NULL);
2085}
2086
Radek Krejci665421c2018-12-05 08:03:39 +01002087static void
2088test_status(void **state)
2089{
2090 *state = test_status;
2091
2092 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002093
2094 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2095
Radek Krejci096235c2019-01-11 11:12:19 +01002096 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002097 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002098 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2099
Radek Krejci096235c2019-01-11 11:12:19 +01002100 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002101 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002102 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2103
Radek Krejci096235c2019-01-11 11:12:19 +01002104 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002105 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002106 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2107
2108 *state = NULL;
2109 ly_ctx_destroy(ctx, NULL);
2110}
2111
Radek Krejcie86bf772018-12-14 11:39:53 +01002112static void
2113test_uses(void **state)
2114{
2115 *state = test_uses;
2116
2117 struct ly_ctx *ctx;
2118 struct lys_module *mod;
2119 struct lysc_node *parent, *child;
2120
2121 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2122
Radek Krejci0af46292019-01-11 16:02:31 +01002123 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2124 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002125 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2126 "grouping grp_a_top {leaf a1 {type int8;}}"
2127 "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 +01002128 assert_non_null((parent = mod->compiled->data));
2129 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2130 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2131 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002132 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002133 assert_non_null((child = child->next));
2134 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002135 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002136 assert_non_null((child = child->next));
2137 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002138 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002139 assert_non_null((child = child->next));
2140 assert_string_equal("y", child->name);
2141 assert_non_null(child->iffeatures);
2142 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2143 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2144 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2145 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2146 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2147 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2148
2149 /* make the imported module implemented and enable the feature */
2150 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2151 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2152 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2153 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2154 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002155
Radek Krejci00b874b2019-02-12 10:54:50 +01002156 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;}}}");
2157 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 +01002158 assert_non_null(mod->compiled->data);
2159 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2160 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002161 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Radek Krejcib1b59152019-01-07 13:21:56 +01002162
Radek Krejci7f6a3812019-01-07 13:48:57 +01002163 logbuf_clean();
2164 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2165 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2166 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002167 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2168 assert_string_equal("l", mod->compiled->data->name);
2169 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2170 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2171 assert_string_equal("k", mod->compiled->data->next->name);
2172 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2173 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2174
Radek Krejcie86bf772018-12-14 11:39:53 +01002175 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002176 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 +01002177 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2178
Radek Krejci096235c2019-01-11 11:12:19 +01002179 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002180 "grouping grp {leaf a{type string;}uses grp1;}"
2181 "grouping grp1 {leaf b {type string;}uses grp2;}"
2182 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002183 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2184
Radek Krejci096235c2019-01-11 11:12:19 +01002185 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 +01002186 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2187
Radek Krejci096235c2019-01-11 11:12:19 +01002188 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 +01002189 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002190 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2191
Radek Krejci096235c2019-01-11 11:12:19 +01002192 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 +01002193 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002194 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2195
Radek Krejci95710c92019-02-11 15:49:55 +01002196 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2197 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
2198 logbuf_assert("Duplicate identifier \"l\" of data definition statement.");
2199 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2200 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
2201 logbuf_assert("Duplicate identifier \"m\" of data definition statement.");
2202
Radek Krejcie86bf772018-12-14 11:39:53 +01002203 *state = NULL;
2204 ly_ctx_destroy(ctx, NULL);
2205}
2206
Radek Krejci01342af2019-01-03 15:18:08 +01002207static void
2208test_refine(void **state)
2209{
2210 *state = test_refine;
2211
2212 struct ly_ctx *ctx;
2213 struct lys_module *mod;
2214 struct lysc_node *parent, *child;
2215
2216 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2217
Radek Krejci096235c2019-01-11 11:12:19 +01002218 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!;}"
2219 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2220 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2221 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2222 "leaf x {type mytype; mandatory true; must 1;}"
2223 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2224 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002225
Radek Krejcif0089082019-01-07 16:42:01 +01002226 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 +01002227 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002228 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002229 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002230 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2231 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002232 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002233 assert_non_null((parent = mod->compiled->data));
2234 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2235 assert_string_equal("c", parent->name);
2236 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2237 assert_int_equal(LYS_LEAF, child->nodetype);
2238 assert_string_equal("l", child->name);
2239 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2240 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2241 assert_non_null(child = child->next);
2242 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2243 assert_string_equal("ll", child->name);
2244 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2245 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2246 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002247 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2248 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002249 assert_non_null(child = child->next);
2250 assert_int_equal(LYS_CHOICE, child->nodetype);
2251 assert_string_equal("ch", child->name);
2252 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2253 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2254 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002255 assert_non_null(child->iffeatures);
2256 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002257 assert_non_null(child = child->next);
2258 assert_int_equal(LYS_LEAF, child->nodetype);
2259 assert_string_equal("x", child->name);
2260 assert_false(LYS_MAND_TRUE & child->flags);
2261 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002262 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2263 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002264 assert_string_equal("refined", child->dsc);
2265 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002266 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002267 assert_int_equal(LYS_ANYDATA, child->nodetype);
2268 assert_string_equal("a", child->name);
2269 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002270 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2271 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002272 assert_non_null(child->iffeatures);
2273 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002274 assert_string_equal("refined", child->dsc);
2275 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002276 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002277 assert_int_equal(LYS_CONTAINER, child->nodetype);
2278 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002279 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002280 assert_true(LYS_CONFIG_W & child->flags);
2281 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002282
2283 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 +01002284 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002285 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002286 assert_int_equal(LYS_LEAF, child->nodetype);
2287 assert_string_equal("x", child->name);
2288 assert_false(LYS_MAND_TRUE & child->flags);
2289 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2290
2291 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002292 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002293 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002294 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2295
Radek Krejci096235c2019-01-11 11:12:19 +01002296 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002297 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002298 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2299
Radek Krejci096235c2019-01-11 11:12:19 +01002300 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002301 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002302 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2303
Radek Krejci096235c2019-01-11 11:12:19 +01002304 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002305 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002306 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2307
Radek Krejci096235c2019-01-11 11:12:19 +01002308 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002309 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002310 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002311 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002312 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002313 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2314
Radek Krejci096235c2019-01-11 11:12:19 +01002315 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002316 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002317 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" - leaf under the default case.");
2318
Radek Krejci096235c2019-01-11 11:12:19 +01002319 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002320 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002321 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2322
Radek Krejci096235c2019-01-11 11:12:19 +01002323 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002324 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002325 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2326
Radek Krejci096235c2019-01-11 11:12:19 +01002327 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 +01002328 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002329 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2330
Radek Krejci096235c2019-01-11 11:12:19 +01002331 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002332 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002333 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2334
Radek Krejci096235c2019-01-11 11:12:19 +01002335 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002336 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002337 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2338
Radek Krejci096235c2019-01-11 11:12:19 +01002339 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002340 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002341 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2342
Radek Krejci096235c2019-01-11 11:12:19 +01002343 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002344 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002345 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2346
Radek Krejci01342af2019-01-03 15:18:08 +01002347 *state = NULL;
2348 ly_ctx_destroy(ctx, NULL);
2349}
Radek Krejcie86bf772018-12-14 11:39:53 +01002350
Radek Krejci95710c92019-02-11 15:49:55 +01002351static void
2352test_augment(void **state)
2353{
2354 *state = test_augment;
2355
2356 struct ly_ctx *ctx;
2357 struct lys_module *mod;
2358 const struct lysc_node *node;
2359 const struct lysc_node_choice *ch;
2360 const struct lysc_node_case *c;
2361
2362 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2363
2364 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2365 "container top {leaf a {type string;}}}");
2366 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2367 "leaf b {type a:atype;}}", LYS_IN_YANG));
2368 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2369 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2370 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2371 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2372 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2373 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2374 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2375 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2376 assert_non_null(node = mod->compiled->data);
2377 assert_string_equal(node->name, "top");
2378 assert_non_null(node = lysc_node_children(node));
2379 assert_string_equal(node->name, "a");
2380 assert_non_null(node = node->next);
2381 assert_string_equal(node->name, "c");
2382 assert_non_null(node = lysc_node_children(node));
2383 assert_string_equal(node->name, "c");
2384 assert_non_null(node = node->next);
2385 assert_string_equal(node->name, "d");
2386 assert_non_null(node = node->next);
2387 assert_string_equal(node->name, "c");
2388
2389 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 +01002390 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2391 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002392 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2393 assert_null(mod->compiled->data->next);
2394 assert_string_equal("ch", ch->name);
2395 assert_non_null(c = ch->cases);
2396 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002397 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002398 assert_string_equal("a", c->child->name);
2399 assert_non_null(c = (const struct lysc_node_case*)c->next);
2400 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002401 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002402 assert_string_equal("b", c->child->name);
2403 assert_non_null(c = (const struct lysc_node_case*)c->next);
2404 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002405 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002406 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002407 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002408 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002409 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002410 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2411 assert_null(c->next);
2412
2413 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2414 "container c;"
2415 "augment /c {uses g;}}", LYS_IN_YANG)));
2416 assert_non_null(node = lysc_node_children(mod->compiled->data));
2417 assert_string_equal(node->name, "a");
2418
Radek Krejci339f5292019-02-11 16:42:34 +01002419 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2420 "augment /c {container sub;}}");
2421 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2422 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2423 assert_non_null(mod->compiled->data);
2424 assert_string_equal("c", mod->compiled->data->name);
2425 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2426 assert_string_equal("sub", node->name);
2427 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2428 assert_string_equal("main", node->name);
2429
Radek Krejcife909632019-02-12 15:34:42 +01002430 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;container top;"
2431 "augment /top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2432 "augment /top {list l {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}}", LYS_IN_YANG));
2433 assert_non_null(node = mod->compiled->data);
2434 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2435 assert_string_equal("p", node->name);
2436 assert_non_null(node->next);
2437 assert_string_equal("l", node->next->name);
2438
Radek Krejci95710c92019-02-11 15:49:55 +01002439 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2440 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
2441 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found.");
2442
2443 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2444 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
2445 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2446
2447
Radek Krejci339f5292019-02-11 16:42:34 +01002448 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2449 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
2450 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target.");
2451
2452 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2453 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
2454 logbuf_assert("Invalid augment (/c) of container node which is not allowed to contain case node \"b\".");
2455
2456
Radek Krejcife909632019-02-12 15:34:42 +01002457 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; container top;"
2458 "augment /top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
2459 logbuf_assert("Invalid augment (/top) adding mandatory node \"c\" without making it conditional via when statement.");
2460
Radek Krejci339f5292019-02-11 16:42:34 +01002461
Radek Krejci95710c92019-02-11 15:49:55 +01002462
2463 *state = NULL;
2464 ly_ctx_destroy(ctx, NULL);
2465}
2466
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002467int main(void)
2468{
2469 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002470 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2471 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2472 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002473 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2474 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002475 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002476 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2477 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002478 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002479 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002480 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002481 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002482 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002483 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2484 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002485 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002486 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002487 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002488 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002489 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002490 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002491 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01002492 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01002493 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002494 };
2495
2496 return cmocka_run_group_tests(tests, NULL, NULL);
2497}