blob: 1288ee32c33695ec8701db372a4bc74704045d95 [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);
115
116 memset(module, 0, sizeof *module);
117 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100118 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100119}
120
121static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200122test_module(void **state)
123{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100124 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200125
126 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +0100127 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200128 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200129 struct lysc_feature *f;
130 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200131
132 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200133 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100134 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100135 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200136
Radek Krejcif8f882a2018-10-31 14:51:15 +0100137 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
138 logbuf_assert("Invalid argument mod (lys_compile()).");
139 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
140 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100141 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100142 mod.implemented = 0;
143 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
144 assert_null(mod.compiled);
145 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100146 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200147 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100148 assert_string_equal("test", mod.name);
149 assert_string_equal("urn:test", mod.ns);
150 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200151 /* features */
152 assert_non_null(mod.compiled->features);
153 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200154 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200155 assert_non_null(f->iffeatures);
156 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200157 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200158 assert_non_null(iff->expr);
159 assert_non_null(iff->features);
160 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200161 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200162
Radek Krejci86d106e2018-10-18 09:53:19 +0200163 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200164
Radek Krejcif8f882a2018-10-31 14:51:15 +0100165 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200166 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200167
Radek Krejci86d106e2018-10-18 09:53:19 +0200168 lysc_module_free(mod.compiled, NULL);
169 mod.compiled = NULL;
170
Radek Krejci151a5b72018-10-19 14:21:44 +0200171 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200172 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100173 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
174 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
175 assert_null(mod.parsed);
176 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100177
178 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100179 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
180 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100181 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
182 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
183 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100184 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100185
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100186 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100187 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200188}
189
Radek Krejci151a5b72018-10-19 14:21:44 +0200190static void
191test_feature(void **state)
192{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100193 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200194
Radek Krejci313d9902018-11-08 09:42:58 +0100195 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100196 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200197 const char *str;
198 struct lysc_feature *f, *f1;
199
200 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
201 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100202 "feature orfeature {if-feature \"f1 or f2\";}\n"
203 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200204 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200205 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100206 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
207 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200208
Radek Krejci313d9902018-11-08 09:42:58 +0100209 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100210 reset_mod(ctx.ctx, &mod);
211
212 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100213 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200214 assert_non_null(mod.compiled);
215 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100216 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200217 /* all features are disabled by default */
218 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
219 assert_int_equal(0, lysc_feature_value(f));
220 }
221 /* enable f1 */
222 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200223 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200224 assert_int_equal(1, lysc_feature_value(f1));
225
Radek Krejci87616bb2018-10-31 13:30:52 +0100226 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200227 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200228 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100229 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200230 assert_int_equal(1, lysc_feature_value(f));
231
Radek Krejci87616bb2018-10-31 13:30:52 +0100232 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200233 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200234 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100235 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
236 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200237 assert_int_equal(0, lysc_feature_value(f));
238
239 /* first enable f2, so f5 can be enabled then */
240 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100241 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200242 assert_int_equal(1, lysc_feature_value(f));
243
244 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200245 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200246 assert_int_equal(0, lysc_feature_value(f));
247 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
248 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
249 assert_int_equal(0, lysc_feature_value(f));
250
Radek Krejci87616bb2018-10-31 13:30:52 +0100251 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200252 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200253 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
254 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200255 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100256 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200257 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200258 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200259 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200260 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200261
262 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200263 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200264 /* long evaluation of f8 to need to reallocate internal stack for operators */
265 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200266
Radek Krejci87616bb2018-10-31 13:30:52 +0100267 /* double negation of disabled f1 -> disabled */
268 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
269
Radek Krejci38920282018-11-01 09:24:16 +0100270 /* disable all features */
271 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
272 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
273 assert_int_equal(0, lys_feature_value(&mod, f->name));
274 }
275 /* re-setting already set feature */
276 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
277 assert_int_equal(0, lys_feature_value(&mod, "f1"));
278
279 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100280 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
281 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
282 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100283 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
284 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100285 /* test if not changed */
286 assert_int_equal(1, lys_feature_value(&mod, "f1"));
287 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100288
Radek Krejci0af46292019-01-11 16:02:31 +0100289 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;"
290 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
291 assert_non_null(modp->compiled);
292 assert_non_null(modp->compiled->features);
293 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
294 assert_non_null(modp->compiled->features[0].iffeatures);
295 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
296 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
297 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
298 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
299 assert_non_null(modp->compiled->features);
300 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
301 assert_non_null(modp->compiled->features[1].depfeatures);
302 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
303 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
304
Radek Krejci1aefdf72018-11-01 11:01:39 +0100305 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100306 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
307 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
308
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100309 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100310
311 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100312 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 +0100313 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100314 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100315 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100316
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100317 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 +0100318 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100319 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100320 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100321
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100322 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 +0100323 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100324 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100326
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100327 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 +0100328 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100329 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100330 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100331
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100332 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 +0100333 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100334 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100335 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100336
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100337 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 +0100338 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100339 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100340 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100341
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100342 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 +0100343 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100344 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 +0100345 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100346
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100347 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 +0100348 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
349 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100350 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100351
Radek Krejci0af46292019-01-11 16:02:31 +0100352 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
353 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 +0100354 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
355
Radek Krejci1aefdf72018-11-01 11:01:39 +0100356 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100357 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100358 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100359 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 +0100360 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
361 assert_int_equal(0, lys_feature_value(modp, "f1"));
362 assert_int_equal(1, lys_feature_value(modp, "f2"));
363
Radek Krejcid05cbd92018-12-05 14:26:40 +0100364 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100365 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200366}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200367
Radek Krejci478020e2018-10-30 16:02:14 +0100368static void
369test_identity(void **state)
370{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100371 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100372
373 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100374 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100375 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100376 const char *mod2_str = "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;} identity b4 {base b:b1; base b3;}}";
Radek Krejci478020e2018-10-30 16:02:14 +0100377
378 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
379 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
380 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100381
382 assert_non_null(mod1->compiled);
383 assert_non_null(mod1->compiled->identities);
384 assert_non_null(mod2->compiled);
385 assert_non_null(mod2->compiled->identities);
386
387 assert_non_null(mod1->compiled->identities[0].derived);
388 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
389 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
390 assert_non_null(mod2->compiled->identities[0].derived);
391 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
392 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
393 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
394 assert_non_null(mod2->compiled->identities[1].derived);
395 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
396 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
397 assert_non_null(mod2->compiled->identities[2].derived);
398 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
399 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
400
Radek Krejci096235c2019-01-11 11:12:19 +0100401 assert_null(lys_parse_mem(ctx, "module c{namespace urn:c; prefix c; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100402 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
403
404 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sd {belongs-to d {prefix d;} identity i1;}");
Radek Krejci096235c2019-01-11 11:12:19 +0100405 assert_null(lys_parse_mem(ctx, "module d{namespace urn:d; prefix d; include sd;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100406 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
407
Radek Krejci7f2a5362018-11-28 13:05:37 +0100408 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100409 ly_ctx_destroy(ctx, NULL);
410}
411
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100412static void
413test_node_container(void **state)
414{
415 (void) state; /* unused */
416
417 struct ly_ctx *ctx;
418 struct lys_module *mod;
419 struct lysc_node_container *cont;
420
421 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
422 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 +0100423 assert_non_null(mod->compiled);
424 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
425 assert_int_equal(LYS_CONTAINER, cont->nodetype);
426 assert_string_equal("c", cont->name);
427 assert_true(cont->flags & LYS_CONFIG_W);
428 assert_true(cont->flags & LYS_STATUS_CURR);
429
Radek Krejci98094b32018-11-02 16:21:47 +0100430 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 +0100431 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100432 assert_non_null(mod->compiled);
433 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
434 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100435 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100436 assert_non_null((cont = (struct lysc_node_container*)cont->child));
437 assert_int_equal(LYS_CONTAINER, cont->nodetype);
438 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100439 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100440 assert_string_equal("child", cont->name);
441
442 ly_ctx_destroy(ctx, NULL);
443}
444
Radek Krejci0e5d8382018-11-28 16:37:53 +0100445static void
446test_node_leaflist(void **state)
447{
448 *state = test_node_leaflist;
449
450 struct ly_ctx *ctx;
451 struct lys_module *mod;
452 struct lysc_type *type;
453 struct lysc_node_leaflist *ll;
454
455 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
456
457 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
458 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
459 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
460 "leaf-list ll2 {type leafref {path ../target;}}"
461 "leaf target {type int8;}}",
462 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100463 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
464 assert_non_null(type);
465 assert_int_equal(1, type->refcount);
466 assert_int_equal(LY_TYPE_UNION, type->basetype);
467 assert_non_null(((struct lysc_type_union*)type)->types);
468 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
469 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
470 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
471 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
472 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
473 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
474 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
475 assert_non_null(type);
476 assert_int_equal(1, type->refcount);
477 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
478 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
479 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
480
Radek Krejci6bb080b2018-11-28 17:23:41 +0100481 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 +0100482 assert_non_null(mod->compiled);
483 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
484 assert_int_equal(0, ll->min);
485 assert_int_equal((uint32_t)-1, ll->max);
486
Radek Krejci6bb080b2018-11-28 17:23:41 +0100487 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 +0100488 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100489 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100490 assert_non_null(mod->compiled);
491 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
492 assert_non_null(ll->dflts);
493 assert_int_equal(3, ll->type->refcount);
494 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
495 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100496 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100497 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100498 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
499 assert_non_null(ll->dflts);
500 assert_int_equal(3, ll->type->refcount);
501 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
502 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100503 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
504
505 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
506 * TODO test also, RPC output parameters and notification content */
507 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
508 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100509 /* but warning is present: */
510 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
511 assert_non_null(mod->compiled);
512 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
513 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100514
515 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100516 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 +0100517 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
518
Radek Krejci096235c2019-01-11 11:12:19 +0100519 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 +0100520 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
521
522 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
523 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100524 assert_non_null(mod->compiled);
525 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
526 assert_non_null(ll->dflts);
527 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100528 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
529 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100530 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
531
Radek Krejci0e5d8382018-11-28 16:37:53 +0100532 *state = NULL;
533 ly_ctx_destroy(ctx, NULL);
534}
535
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100536static void
537test_node_list(void **state)
538{
539 *state = test_node_list;
540
541 struct ly_ctx *ctx;
542 struct lys_module *mod;
543 struct lysc_node_list *list;
544
545 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
546
547 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
548 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
549 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100550 list = (struct lysc_node_list*)mod->compiled->data;
551 assert_non_null(list);
552 assert_non_null(list->keys);
553 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
554 assert_string_equal("x", list->keys[0]->name);
555 assert_string_equal("y", list->keys[1]->name);
556 assert_non_null(list->child);
557 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
558 assert_true(list->child->flags & LYS_KEY);
559 assert_true(list->child->next->flags & LYS_KEY);
560 list = (struct lysc_node_list*)mod->compiled->data->next;
561 assert_non_null(list);
562 assert_null(list->keys);
563 assert_non_null(list->child);
564 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
565 assert_false(list->child->flags & LYS_KEY);
566
567 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
568 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100569 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100570 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100571 list = (struct lysc_node_list*)mod->compiled->data;
572 assert_non_null(list);
573 assert_string_equal("l", list->name);
574 assert_non_null(list->keys);
575 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
576 assert_string_equal("a", list->keys[0]->name);
577 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100578 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100579 assert_non_null(list->uniques);
580 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
581 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
582 assert_string_equal("a", list->uniques[0][0]->name);
583 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
584 assert_string_equal("b", list->uniques[0][1]->name);
585 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
586 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
587 assert_string_equal("e", list->uniques[1][0]->name);
588 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
589 assert_string_equal("d", list->uniques[1][1]->name);
590 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
591
Radek Krejci665421c2018-12-05 08:03:39 +0100592 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
593 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100594 list = (struct lysc_node_list*)mod->compiled->data;
595 assert_non_null(list);
596 assert_string_equal("l", list->name);
597 assert_non_null(list->keys);
598 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
599 assert_string_equal("a", list->keys[0]->name);
600 assert_true(list->keys[0]->flags & LYS_KEY);
601 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
602
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100603 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100604 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 +0100605 logbuf_assert("Missing key in list representing configuration data.");
606
Radek Krejci096235c2019-01-11 11:12:19 +0100607 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
608 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100609 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
610
Radek Krejci096235c2019-01-11 11:12:19 +0100611 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
612 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100613 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
614
Radek Krejci096235c2019-01-11 11:12:19 +0100615 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
616 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100617 logbuf_assert("Key of the configuration list must not be status leaf.");
618
Radek Krejci096235c2019-01-11 11:12:19 +0100619 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
620 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100621 logbuf_assert("Configuration node cannot be child of any state data node.");
622
Radek Krejci096235c2019-01-11 11:12:19 +0100623 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
624 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100625 logbuf_assert("The list's key \"x\" not found.");
626
Radek Krejci096235c2019-01-11 11:12:19 +0100627 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
628 "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 +0100629 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
630
Radek Krejci096235c2019-01-11 11:12:19 +0100631 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
632 "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 +0100633 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
634
Radek Krejci096235c2019-01-11 11:12:19 +0100635 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
636 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100637 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
638
Radek Krejci096235c2019-01-11 11:12:19 +0100639 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
640 "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 +0100641 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
642
Radek Krejci096235c2019-01-11 11:12:19 +0100643 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
644 "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 +0100645 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
646
Radek Krejci096235c2019-01-11 11:12:19 +0100647 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
648 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100649 logbuf_assert("Duplicated key identifier \"x\".");
650
Radek Krejci096235c2019-01-11 11:12:19 +0100651 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
652 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100653 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
654
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100655 *state = NULL;
656 ly_ctx_destroy(ctx, NULL);
657}
658
Radek Krejci056d0a82018-12-06 16:57:25 +0100659static void
660test_node_choice(void **state)
661{
662 *state = test_node_choice;
663
664 struct ly_ctx *ctx;
665 struct lys_module *mod;
666 struct lysc_node_choice *ch;
667 struct lysc_node_case *cs;
668
669 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
670
671 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100672 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
673 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100674 ch = (struct lysc_node_choice*)mod->compiled->data;
675 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100676 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100677 cs = ch->cases;
678 assert_non_null(cs);
679 assert_string_equal("a", cs->name);
680 assert_ptr_equal(ch, cs->parent);
681 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100682 assert_string_equal("a1", cs->child->name);
683 assert_non_null(cs->child->next);
684 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100685 assert_ptr_equal(cs, cs->child->parent);
686 cs = (struct lysc_node_case*)cs->next;
687 assert_non_null(cs);
688 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100689 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100690 assert_ptr_equal(ch, cs->parent);
691 assert_non_null(cs->child);
692 assert_string_equal("b", cs->child->name);
693 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100694 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
695 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100696 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100697 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100698
Radek Krejci096235c2019-01-11 11:12:19 +0100699 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
700 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100701 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100702 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
703 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100704 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100705 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
706 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100707 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100708 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
709 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100710 logbuf_assert("Duplicate identifier \"b\" of case statement.");
711
Radek Krejci096235c2019-01-11 11:12:19 +0100712 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
713 "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 +0100714 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100715 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
716 "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 +0100717 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100718 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
719 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100720 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
721 /* TODO check with mandatory nodes from augment placed into the case */
722
Radek Krejci056d0a82018-12-06 16:57:25 +0100723 *state = NULL;
724 ly_ctx_destroy(ctx, NULL);
725}
726
Radek Krejci9800fb82018-12-13 14:26:23 +0100727static void
728test_node_anydata(void **state)
729{
730 *state = test_node_anydata;
731
732 struct ly_ctx *ctx;
733 struct lys_module *mod;
734 struct lysc_node_anydata *any;
735
736 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
737
738 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
739 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100740 any = (struct lysc_node_anydata*)mod->compiled->data;
741 assert_non_null(any);
742 assert_int_equal(LYS_ANYDATA, any->nodetype);
743 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
744
745 logbuf_clean();
746 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
747 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100748 any = (struct lysc_node_anydata*)mod->compiled->data;
749 assert_non_null(any);
750 assert_int_equal(LYS_ANYXML, any->nodetype);
751 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
752 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
753
754 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100755 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 +0100756 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
757
758 *state = NULL;
759 ly_ctx_destroy(ctx, NULL);
760}
761
Radek Krejci0f07bed2018-11-13 14:01:40 +0100762/**
763 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
764 * do not test the expression itself
765 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100766static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100767test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100768{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100769 *state = test_type_range;
770
771 struct ly_ctx *ctx;
772 struct lys_module *mod;
773 struct lysc_type *type;
774
775 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
776
777 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 +0100778 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
779 assert_non_null(type);
780 assert_int_equal(LY_TYPE_INT8, type->basetype);
781 assert_non_null(((struct lysc_type_num*)type)->range);
782 assert_non_null(((struct lysc_type_num*)type)->range->parts);
783 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
784 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
785 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
786 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
787 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
788
789 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 +0100790 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
791 assert_non_null(type);
792 assert_int_equal(LY_TYPE_INT16, type->basetype);
793 assert_non_null(((struct lysc_type_num*)type)->range);
794 assert_non_null(((struct lysc_type_num*)type)->range->parts);
795 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
796 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
797 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
798 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
799 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
800
801 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 +0100802 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
803 assert_non_null(type);
804 assert_int_equal(LY_TYPE_INT32, type->basetype);
805 assert_non_null(((struct lysc_type_num*)type)->range);
806 assert_non_null(((struct lysc_type_num*)type)->range->parts);
807 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
808 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
809 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
810 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
811 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
812
813 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 +0100814 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
815 assert_non_null(type);
816 assert_int_equal(LY_TYPE_INT64, type->basetype);
817 assert_non_null(((struct lysc_type_num*)type)->range);
818 assert_non_null(((struct lysc_type_num*)type)->range->parts);
819 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
820 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
821 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
822 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
823 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
824
825 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 +0100826 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
827 assert_non_null(type);
828 assert_int_equal(LY_TYPE_UINT8, type->basetype);
829 assert_non_null(((struct lysc_type_num*)type)->range);
830 assert_non_null(((struct lysc_type_num*)type)->range->parts);
831 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
832 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
833 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
834 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
835 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
836
837 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 +0100838 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
839 assert_non_null(type);
840 assert_int_equal(LY_TYPE_UINT16, type->basetype);
841 assert_non_null(((struct lysc_type_num*)type)->range);
842 assert_non_null(((struct lysc_type_num*)type)->range->parts);
843 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
844 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
845 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
846 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
847 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
848
849 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 +0100850 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
851 assert_non_null(type);
852 assert_int_equal(LY_TYPE_UINT32, type->basetype);
853 assert_non_null(((struct lysc_type_num*)type)->range);
854 assert_non_null(((struct lysc_type_num*)type)->range->parts);
855 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
856 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
857 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
858 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
859 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
860
861 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 +0100862 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
863 assert_non_null(type);
864 assert_int_equal(LY_TYPE_UINT64, type->basetype);
865 assert_non_null(((struct lysc_type_num*)type)->range);
866 assert_non_null(((struct lysc_type_num*)type)->range->parts);
867 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
868 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
869 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
870 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
871 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
872
873 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
874 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100875 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
876 assert_non_null(type);
877 assert_int_equal(3, type->refcount);
878 assert_int_equal(LY_TYPE_UINT8, 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(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
882
Radek Krejcif1394042019-01-08 10:53:34 +0100883 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
884 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
885 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +0100886 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
887 assert_non_null(type);
888 assert_int_equal(1, type->refcount);
889 assert_int_equal(LY_TYPE_UINT8, type->basetype);
890 assert_non_null(((struct lysc_type_num*)type)->range);
891 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
892 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
893 assert_non_null(((struct lysc_type_num*)type)->range->parts);
894 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
895 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
896 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
897
Radek Krejci0f07bed2018-11-13 14:01:40 +0100898 *state = NULL;
899 ly_ctx_destroy(ctx, NULL);
900}
901
902static void
903test_type_length(void **state)
904{
905 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100906
907 struct ly_ctx *ctx;
908 struct lys_module *mod;
909 struct lysc_type *type;
910
911 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
912
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100913 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 +0100914 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
915 assert_non_null(type);
916 assert_non_null(((struct lysc_type_bin*)type)->length);
917 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100918 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
919 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100920 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
921 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
922 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
923
924 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 +0100925 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
926 assert_non_null(type);
927 assert_non_null(((struct lysc_type_bin*)type)->length);
928 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
929 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100930 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
931 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100932
933 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 +0100934 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
935 assert_non_null(type);
936 assert_non_null(((struct lysc_type_bin*)type)->length);
937 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
938 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
939 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100940 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100941
942 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 +0100943 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
944 assert_non_null(type);
945 assert_non_null(((struct lysc_type_bin*)type)->length);
946 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
947 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
948 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
949 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
950
951 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 +0100952 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
953 assert_non_null(type);
954 assert_non_null(((struct lysc_type_bin*)type)->length);
955 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
956 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
957 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
958 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
959
960 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 +0100961 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
962 assert_non_null(type);
963 assert_non_null(((struct lysc_type_bin*)type)->length);
964 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
965 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
966 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
967 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
968 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
969 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
970
971 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 +0100972 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
973 assert_non_null(type);
974 assert_non_null(((struct lysc_type_bin*)type)->length);
975 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
976 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
977 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
978 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
979 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
980 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
981
Radek Krejcib31c8992018-11-12 16:29:16 +0100982 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
983 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100984 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
985 assert_non_null(type);
986 assert_non_null(((struct lysc_type_bin*)type)->length);
987 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
988 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
989 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
990 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
991
992 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
993 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100994 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
995 assert_non_null(type);
996 assert_non_null(((struct lysc_type_bin*)type)->length);
997 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
998 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
999 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1000 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1001
1002 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1003 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001004 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1005 assert_non_null(type);
1006 assert_non_null(((struct lysc_type_bin*)type)->length);
1007 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1008 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1009 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1010 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1011 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1012 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1013
1014 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 +01001015 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001016 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1017 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001018 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001019 assert_non_null(((struct lysc_type_bin*)type)->length);
1020 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1021 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1022 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001023 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001024 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1025 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001026 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001027 assert_non_null(((struct lysc_type_bin*)type)->length);
1028 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1029 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1030 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1031 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1032
Radek Krejci56aa27c2018-11-13 14:21:59 +01001033 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1034 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001035 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1036 assert_non_null(type);
1037 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001038 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001039 assert_non_null(((struct lysc_type_str*)type)->length);
1040 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1041 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1042 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1043 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001044
Radek Krejci5969f272018-11-23 10:03:58 +01001045 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1046 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001047 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1048 assert_non_null(type);
1049 assert_int_equal(LY_TYPE_STRING, type->basetype);
1050 assert_int_equal(1, type->refcount);
1051 assert_non_null(((struct lysc_type_str*)type)->length);
1052 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1053 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1054 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1055 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1056
Radek Krejci4f28eda2018-11-12 11:46:16 +01001057 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001058 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 +01001059 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001060 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 +01001061 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001062 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 +01001063 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001064 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 +01001065 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001066 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 +01001067 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001068 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 +01001069 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001070 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 +01001071 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001072 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 +01001073 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001074 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 +01001075 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001076 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 +01001077 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001078 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 +01001079 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001080 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 +01001081 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001082
Radek Krejci096235c2019-01-11 11:12:19 +01001083 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1084 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001085 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001086 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1087 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001088 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001089 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1090 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001091 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001092 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1093 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001094 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 +01001095 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 +01001096 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001097 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001098 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 +01001099 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001100 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001101 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 +01001102 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001103 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001104
Radek Krejci096235c2019-01-11 11:12:19 +01001105 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 +01001106 logbuf_assert("Invalid type restrictions for binary type.");
1107
Radek Krejci4f28eda2018-11-12 11:46:16 +01001108 *state = NULL;
1109 ly_ctx_destroy(ctx, NULL);
1110}
1111
Radek Krejcicda74152018-11-13 15:27:32 +01001112static void
1113test_type_pattern(void **state)
1114{
1115 *state = test_type_pattern;
1116
1117 struct ly_ctx *ctx;
1118 struct lys_module *mod;
1119 struct lysc_type *type;
1120
1121 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1122
Radek Krejci027d5802018-11-14 16:57:28 +01001123 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 +01001124 "pattern .* {error-app-tag errortag;error-message error;}"
1125 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001126 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1127 assert_non_null(type);
1128 assert_non_null(((struct lysc_type_str*)type)->patterns);
1129 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1130 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1131 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1132 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1133 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1134 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1135 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1136
1137 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1138 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001139 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1140 assert_non_null(type);
1141 assert_int_equal(LY_TYPE_STRING, type->basetype);
1142 assert_int_equal(1, type->refcount);
1143 assert_non_null(((struct lysc_type_str*)type)->patterns);
1144 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1145 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1146 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1147
Radek Krejci04bc1e92018-11-14 14:28:13 +01001148 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1149 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001150 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1151 assert_non_null(type);
1152 assert_int_equal(LY_TYPE_STRING, type->basetype);
1153 assert_int_equal(1, type->refcount);
1154 assert_non_null(((struct lysc_type_str*)type)->patterns);
1155 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1156 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1157
Radek Krejcicda74152018-11-13 15:27:32 +01001158 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001159 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 +01001160 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001161 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1162 assert_non_null(type);
1163 assert_non_null(((struct lysc_type_str*)type)->patterns);
1164 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1165 /* TODO check some data "^Å™$" */
1166
1167 *state = NULL;
1168 ly_ctx_destroy(ctx, NULL);
1169}
1170
Radek Krejci8b764662018-11-14 14:15:13 +01001171static void
1172test_type_enum(void **state)
1173{
1174 *state = test_type_enum;
1175
1176 struct ly_ctx *ctx;
1177 struct lys_module *mod;
1178 struct lysc_type *type;
1179
1180 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1181
Radek Krejci027d5802018-11-14 16:57:28 +01001182 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 +01001183 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1184 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001185 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1186 assert_non_null(type);
1187 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1188 assert_non_null(((struct lysc_type_enum*)type)->enums);
1189 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1190 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1191 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1192 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1193 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1194 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1195 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1196 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1197 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1198 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1199 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1200 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1201 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1202 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1203
Radek Krejci027d5802018-11-14 16:57:28 +01001204 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 {"
1205 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001206 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1207 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001208 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1209 assert_non_null(type);
1210 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1211 assert_non_null(((struct lysc_type_enum*)type)->enums);
1212 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1213 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1214 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1215 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1216 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1217
1218
1219 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001220 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1221 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1222 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 +01001223 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1224 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1225 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1226 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1227 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1228 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1229 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1230 "enum one; enum one;}}}", LYS_IN_YANG));
1231 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1232 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1233 "enum '';}}}", LYS_IN_YANG));
1234 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1235 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1236 "enum ' x';}}}", LYS_IN_YANG));
1237 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1238 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1239 "enum 'x ';}}}", LYS_IN_YANG));
1240 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1241 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1242 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1243 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1244
Radek Krejci096235c2019-01-11 11:12:19 +01001245 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 +01001246 logbuf_assert("Missing enum substatement for enumeration type.");
1247
Radek Krejci096235c2019-01-11 11:12:19 +01001248 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 +01001249 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001250 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1251
Radek Krejci096235c2019-01-11 11:12:19 +01001252 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 +01001253 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001254 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 +01001255 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 +01001256 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001257 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1258
Radek Krejci096235c2019-01-11 11:12:19 +01001259 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 +01001260 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001261 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1262
Radek Krejci096235c2019-01-11 11:12:19 +01001263 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001264 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001265 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001266
Radek Krejci096235c2019-01-11 11:12:19 +01001267 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 +01001268 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001269 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1270
Radek Krejci8b764662018-11-14 14:15:13 +01001271 *state = NULL;
1272 ly_ctx_destroy(ctx, NULL);
1273}
1274
1275static void
1276test_type_bits(void **state)
1277{
1278 *state = test_type_bits;
1279
1280 struct ly_ctx *ctx;
1281 struct lys_module *mod;
1282 struct lysc_type *type;
1283
1284 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1285
Radek Krejci027d5802018-11-14 16:57:28 +01001286 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 +01001287 "bit automin; bit one {if-feature f; position 1;}"
1288 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001289 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1290 assert_non_null(type);
1291 assert_int_equal(LY_TYPE_BITS, type->basetype);
1292 assert_non_null(((struct lysc_type_bits*)type)->bits);
1293 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1294 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1295 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1296 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1297 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1298 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1299 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1300 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1301 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1302 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1303 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1304 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1305
Radek Krejci027d5802018-11-14 16:57:28 +01001306 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 {"
1307 "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 +01001308 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001309 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1310 assert_non_null(type);
1311 assert_int_equal(LY_TYPE_BITS, type->basetype);
1312 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001313 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1314 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1315 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1316 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1317 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1318 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1319 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001320
1321
1322 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001323 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1324 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1325 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 +01001326 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1327 "bit one {position -1;}}}}", LYS_IN_YANG));
1328 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1329 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1330 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1331 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1332 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1333 "bit one; bit one;}}}", LYS_IN_YANG));
1334 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1335 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1336 "bit '11';}}}", LYS_IN_YANG));
1337 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1338 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1339 "bit 'x1$1';}}}", LYS_IN_YANG));
1340 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1341
Radek Krejci096235c2019-01-11 11:12:19 +01001342 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 +01001343 logbuf_assert("Missing bit substatement for bits type.");
1344
Radek Krejci096235c2019-01-11 11:12:19 +01001345 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 +01001346 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001347 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1348
Radek Krejci096235c2019-01-11 11:12:19 +01001349 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 +01001350 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001351 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 +01001352 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 +01001353 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001354 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1355
Radek Krejci096235c2019-01-11 11:12:19 +01001356 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 +01001357 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001358 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1359
Radek Krejci096235c2019-01-11 11:12:19 +01001360 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001361 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001362 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001363
Radek Krejci096235c2019-01-11 11:12:19 +01001364 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 +01001365 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001366 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1367
Radek Krejci8b764662018-11-14 14:15:13 +01001368 *state = NULL;
1369 ly_ctx_destroy(ctx, NULL);
1370}
1371
Radek Krejci6cba4292018-11-15 17:33:29 +01001372static void
1373test_type_dec64(void **state)
1374{
1375 *state = test_type_dec64;
1376
1377 struct ly_ctx *ctx;
1378 struct lys_module *mod;
1379 struct lysc_type *type;
1380
1381 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1382
1383 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1384 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001385 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1386 assert_non_null(type);
1387 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1388 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1389 assert_non_null(((struct lysc_type_dec*)type)->range);
1390 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1391 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1392 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1393 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1394
1395 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1396 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001397 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1398 assert_non_null(type);
1399 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1400 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1401 assert_non_null(((struct lysc_type_dec*)type)->range);
1402 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1403 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1404 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1405 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1406 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1407 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1408 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1409 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1410
1411 /* invalid cases */
1412 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1413 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1414 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1415 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1416 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1417 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1418
Radek Krejci096235c2019-01-11 11:12:19 +01001419 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 +01001420 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1421
Radek Krejci096235c2019-01-11 11:12:19 +01001422 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 +01001423 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001424
Radek Krejci096235c2019-01-11 11:12:19 +01001425 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 +01001426 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001427 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1428
Radek Krejci096235c2019-01-11 11:12:19 +01001429 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 +01001430 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001431 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1432
Radek Krejci096235c2019-01-11 11:12:19 +01001433 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 +01001434 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001435 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1436
Radek Krejci096235c2019-01-11 11:12:19 +01001437 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 +01001438 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001439 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1440
Radek Krejci6cba4292018-11-15 17:33:29 +01001441 *state = NULL;
1442 ly_ctx_destroy(ctx, NULL);
1443}
1444
Radek Krejci16c0f822018-11-16 10:46:10 +01001445static void
1446test_type_instanceid(void **state)
1447{
1448 *state = test_type_instanceid;
1449
1450 struct ly_ctx *ctx;
1451 struct lys_module *mod;
1452 struct lysc_type *type;
1453
1454 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1455
1456 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1457 "leaf l1 {type instance-identifier {require-instance true;}}"
1458 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001459 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1460 assert_non_null(type);
1461 assert_int_equal(LY_TYPE_INST, type->basetype);
1462 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1463
1464 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1465 assert_non_null(type);
1466 assert_int_equal(LY_TYPE_INST, type->basetype);
1467 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1468
1469 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1470 assert_non_null(type);
1471 assert_int_equal(LY_TYPE_INST, type->basetype);
1472 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1473
1474 /* invalid cases */
1475 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1476 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1477
Radek Krejci096235c2019-01-11 11:12:19 +01001478 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 +01001479 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1480
1481 *state = NULL;
1482 ly_ctx_destroy(ctx, NULL);
1483}
1484
Radek Krejci555cb5b2018-11-16 14:54:33 +01001485static void
1486test_type_identityref(void **state)
1487{
1488 *state = test_type_identityref;
1489
1490 struct ly_ctx *ctx;
1491 struct lys_module *mod;
1492 struct lysc_type *type;
1493
1494 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1495
1496 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;}"
1497 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001498 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001499 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1500 assert_non_null(type);
1501 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1502 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1503 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1504 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1505
1506 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1507 assert_non_null(type);
1508 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1509 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1510 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1511 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1512 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1513
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001514 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1515 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001516 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1517 assert_non_null(type);
1518 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1519 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1520 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1521 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1522 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1523
Radek Krejci555cb5b2018-11-16 14:54:33 +01001524 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001525 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 +01001526 logbuf_assert("Missing base substatement for identityref type.");
1527
Radek Krejci096235c2019-01-11 11:12:19 +01001528 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001529 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001530 logbuf_assert("Missing base substatement for identityref type mytype.");
1531
Radek Krejci096235c2019-01-11 11:12:19 +01001532 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 +01001533 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001534 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001535
Radek Krejci096235c2019-01-11 11:12:19 +01001536 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 +01001537 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001538 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001539
Radek Krejci096235c2019-01-11 11:12:19 +01001540 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001541 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001542 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1543
Radek Krejci096235c2019-01-11 11:12:19 +01001544 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 +01001545 logbuf_assert("Unable to find base (j) of identityref.");
1546
Radek Krejci096235c2019-01-11 11:12:19 +01001547 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 +01001548 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1549
Radek Krejci555cb5b2018-11-16 14:54:33 +01001550 *state = NULL;
1551 ly_ctx_destroy(ctx, NULL);
1552}
1553
Radek Krejcia3045382018-11-22 14:30:31 +01001554static void
1555test_type_leafref(void **state)
1556{
1557 *state = test_type_leafref;
1558
1559 struct ly_ctx *ctx;
1560 struct lys_module *mod;
1561 struct lysc_type *type;
1562 const char *path, *name, *prefix;
1563 size_t prefix_len, name_len;
1564 int parent_times, has_predicate;
1565
1566 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1567
1568 /* lys_path_token() */
1569 path = "invalid_path";
1570 parent_times = 0;
1571 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1572 path = "..";
1573 parent_times = 0;
1574 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1575 path = "..[";
1576 parent_times = 0;
1577 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1578 path = "../";
1579 parent_times = 0;
1580 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1581 path = "/";
1582 parent_times = 0;
1583 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1584
1585 path = "../../pref:id/xxx[predicate]/invalid!!!";
1586 parent_times = 0;
1587 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1588 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1589 assert_int_equal(4, prefix_len);
1590 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1591 assert_int_equal(2, name_len);
1592 assert_int_equal(0, strncmp("id", name, name_len));
1593 assert_int_equal(2, parent_times);
1594 assert_int_equal(0, has_predicate);
1595 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1596 assert_string_equal("[predicate]/invalid!!!", path);
1597 assert_int_equal(0, prefix_len);
1598 assert_null(prefix);
1599 assert_int_equal(3, name_len);
1600 assert_int_equal(0, strncmp("xxx", name, name_len));
1601 assert_int_equal(1, has_predicate);
1602 path += 11;
1603 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1604 assert_string_equal("!!!", path);
1605 assert_int_equal(0, prefix_len);
1606 assert_null(prefix);
1607 assert_int_equal(7, name_len);
1608 assert_int_equal(0, strncmp("invalid", name, name_len));
1609
1610 path = "/absolute/prefix:path";
1611 parent_times = 0;
1612 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1613 assert_string_equal("/prefix:path", path);
1614 assert_int_equal(0, prefix_len);
1615 assert_null(prefix);
1616 assert_int_equal(8, name_len);
1617 assert_int_equal(0, strncmp("absolute", name, name_len));
1618 assert_int_equal(-1, parent_times);
1619 assert_int_equal(0, has_predicate);
1620 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1621 assert_int_equal(0, *path);
1622 assert_int_equal(6, prefix_len);
1623 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1624 assert_int_equal(4, name_len);
1625 assert_int_equal(0, strncmp("path", name, name_len));
1626 assert_int_equal(0, has_predicate);
1627
1628 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001629 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 +01001630 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1631 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001632 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1633 assert_non_null(type);
1634 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1635 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001636 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001637 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1638 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001639 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1640 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1641 assert_non_null(type);
1642 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1643 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001644 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1645 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1646 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001647 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1648
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001649 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 +01001650 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001651 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001652 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1653 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001654 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001655 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1656 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1657 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001658 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1659 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001660 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1661
1662 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001663 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 +01001664 "typedef mytype3 {type c:mytype {require-instance false;}}"
1665 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", 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 Krejci2f4a0292018-11-22 15:41:53 +01001669 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1670 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1671 assert_ptr_not_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 Krejci2f4a0292018-11-22 15:41:53 +01001674 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1675 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1676 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001677 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001678 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1679 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1680 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1681 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1682
Radek Krejci4ce68932018-11-28 12:53:36 +01001683 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1684 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1685 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001686 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1687 assert_non_null(type);
1688 assert_int_equal(1, type->refcount);
1689 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1690 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1691 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1692 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1693 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1694 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1695
Radek Krejci58d171e2018-11-23 13:50:55 +01001696 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001697 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 +01001698 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1699 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +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_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_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1708
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001709 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1710 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1711 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001712 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001713 LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +01001714 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001715 assert_non_null(type);
1716 assert_int_equal(1, type->refcount);
1717 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001718 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001719 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_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001722
Radek Krejci20e8a182018-12-07 11:43:21 +01001723 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1724 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1725 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1726 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001727 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1728 assert_non_null(type);
1729 assert_int_equal(1, type->refcount);
1730 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1731 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1732 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1733 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1734 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1735
Radek Krejcia3045382018-11-22 14:30:31 +01001736 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001737 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 +01001738 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001739 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001740 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 +01001741 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001742 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001743 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 +01001744 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001745 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001746 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 +01001747 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001748 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001749 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 +01001750 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001751 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001752 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 +01001753 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001754 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001755 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 +01001756 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001757 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001758 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001759 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001760 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001761 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001762 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001763 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001764 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001765 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001766 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1767 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001768 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001769 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001770 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 +01001771
Radek Krejci096235c2019-01-11 11:12:19 +01001772 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001773 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001774 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001775 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 +01001776 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001777 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1778
Radek Krejci096235c2019-01-11 11:12:19 +01001779 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001780 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1781 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1782 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1783 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001784 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1785
Radek Krejci096235c2019-01-11 11:12:19 +01001786 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001787 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1788 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1789 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1790 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001791 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1792
Radek Krejci096235c2019-01-11 11:12:19 +01001793 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
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[x:name=current()/../ifname]/ip\";}}}",
1797 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001798 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1799
Radek Krejci096235c2019-01-11 11:12:19 +01001800 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
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[id=current()/../ifname]/ip\";}}}",
1804 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001805 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1806
Radek Krejci096235c2019-01-11 11:12:19 +01001807 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
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\";}}leaf test{type string;}"
1810 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1811 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001812 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1813
Radek Krejci096235c2019-01-11 11:12:19 +01001814 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
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\";}}leaf test{type string;}"
1817 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1818 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001819 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1820
Radek Krejci096235c2019-01-11 11:12:19 +01001821 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
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]/ip\";}}}",
1825 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001826 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1827
Radek Krejci096235c2019-01-11 11:12:19 +01001828 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
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 = current()/..ifname]/ip\";}}}",
1832 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001833 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1834
Radek Krejci096235c2019-01-11 11:12:19 +01001835 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
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]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1841
Radek Krejci096235c2019-01-11 11:12:19 +01001842 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
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()/../]/ip\";}}}",
1846 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001847 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1848
Radek Krejci096235c2019-01-11 11:12:19 +01001849 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
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()/../$node]/ip\";}}}",
1853 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001854 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1855
Radek Krejci096235c2019-01-11 11:12:19 +01001856 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
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\";}}"
1859 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1860 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001861 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1862
Radek Krejci096235c2019-01-11 11:12:19 +01001863 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
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\";}}"
1866 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
1867 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001868 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1869
Radek Krejci096235c2019-01-11 11:12:19 +01001870 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
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\";}}container c;"
1873 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1874 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001875 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1876
Radek Krejci096235c2019-01-11 11:12:19 +01001877 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001878 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1879 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1880 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1881 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001882 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1883
Radek Krejci412ddfa2018-11-23 11:44:11 +01001884 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01001885 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01001886 "leaf ref1 {type leafref {path /ref2;}}"
1887 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001888 "leaf ref3 {type leafref {path /ref4;}}"
1889 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001890 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1891
Radek Krejcia3045382018-11-22 14:30:31 +01001892 *state = NULL;
1893 ly_ctx_destroy(ctx, NULL);
1894}
1895
Radek Krejci43699232018-11-23 14:59:46 +01001896static void
1897test_type_empty(void **state)
1898{
1899 *state = test_type_empty;
1900
1901 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01001902
1903 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1904
1905 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01001906 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01001907 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001908 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1909
Radek Krejci096235c2019-01-11 11:12:19 +01001910 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 +01001911 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001912 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1913
1914 *state = NULL;
1915 ly_ctx_destroy(ctx, NULL);
1916}
1917
Radek Krejcicdfecd92018-11-26 11:27:32 +01001918
1919static void
1920test_type_union(void **state)
1921{
1922 *state = test_type_union;
1923
1924 struct ly_ctx *ctx;
1925 struct lys_module *mod;
1926 struct lysc_type *type;
1927
1928 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1929
1930 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
1931 "typedef mytype {type union {type int8; type mybasetype;}}"
1932 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001933 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1934 assert_non_null(type);
1935 assert_int_equal(2, type->refcount);
1936 assert_int_equal(LY_TYPE_UNION, type->basetype);
1937 assert_non_null(((struct lysc_type_union*)type)->types);
1938 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1939 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
1940 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1941
1942 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
1943 "typedef mytype {type union {type int8; type mybasetype;}}"
1944 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001945 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1946 assert_non_null(type);
1947 assert_int_equal(1, type->refcount);
1948 assert_int_equal(LY_TYPE_UNION, type->basetype);
1949 assert_non_null(((struct lysc_type_union*)type)->types);
1950 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1951 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1952 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
1953 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1954
1955 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
1956 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001957 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
1958 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01001959 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001960 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1961 assert_non_null(type);
1962 assert_int_equal(1, type->refcount);
1963 assert_int_equal(LY_TYPE_UNION, type->basetype);
1964 assert_non_null(((struct lysc_type_union*)type)->types);
1965 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1966 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1967 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
1968 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1969 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
1970 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
1971
Radek Krejci6be5ff12018-11-26 13:18:15 +01001972 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01001973 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001974 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001975 logbuf_assert("Missing type substatement for union type mytype.");
1976
Radek Krejci096235c2019-01-11 11:12:19 +01001977 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
1978 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01001979
Radek Krejci096235c2019-01-11 11:12:19 +01001980 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 +01001981 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001982 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
1983
Radek Krejci096235c2019-01-11 11:12:19 +01001984 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 +01001985 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001986 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
1987
Radek Krejcicdfecd92018-11-26 11:27:32 +01001988 *state = NULL;
1989 ly_ctx_destroy(ctx, NULL);
1990}
1991
1992static void
1993test_type_dflt(void **state)
1994{
1995 *state = test_type_union;
1996
1997 struct ly_ctx *ctx;
1998 struct lys_module *mod;
1999 struct lysc_type *type;
2000
2001 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2002
2003 /* default is not inherited from union's types */
2004 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2005 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002006 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2007 assert_non_null(type);
2008 assert_int_equal(1, type->refcount);
2009 assert_int_equal(LY_TYPE_UNION, type->basetype);
2010 assert_non_null(((struct lysc_type_union*)type)->types);
2011 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2012 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2013 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2014 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2015 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2016
2017 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2018 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002019 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2020 assert_non_null(type);
2021 assert_int_equal(2, type->refcount);
2022 assert_int_equal(LY_TYPE_STRING, type->basetype);
2023 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2024 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2025
2026 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2027 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002028 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2029 assert_non_null(type);
2030 assert_int_equal(2, type->refcount);
2031 assert_int_equal(LY_TYPE_STRING, type->basetype);
2032 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2033 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2034
Radek Krejci6be5ff12018-11-26 13:18:15 +01002035 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2036 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2037 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002038 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2039 assert_non_null(type);
2040 assert_int_equal(4, type->refcount);
2041 assert_int_equal(LY_TYPE_STRING, type->basetype);
2042 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2043 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2044 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2045 assert_non_null(type);
2046 assert_int_equal(4, type->refcount);
2047 assert_int_equal(LY_TYPE_STRING, type->basetype);
2048 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2049 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2050
2051 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2052 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002053 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2054 assert_non_null(type);
2055 assert_int_equal(3, type->refcount);
2056 assert_int_equal(LY_TYPE_STRING, type->basetype);
2057 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2058 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2059
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002060 /* mandatory leaf does not takes default value from type */
2061 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2062 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002063 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2064 assert_non_null(type);
2065 assert_int_equal(LY_TYPE_STRING, type->basetype);
2066 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2067 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2068
Radek Krejcicdfecd92018-11-26 11:27:32 +01002069 *state = NULL;
2070 ly_ctx_destroy(ctx, NULL);
2071}
2072
Radek Krejci665421c2018-12-05 08:03:39 +01002073static void
2074test_status(void **state)
2075{
2076 *state = test_status;
2077
2078 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002079
2080 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2081
Radek Krejci096235c2019-01-11 11:12:19 +01002082 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002083 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002084 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2085
Radek Krejci096235c2019-01-11 11:12:19 +01002086 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002087 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002088 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2089
Radek Krejci096235c2019-01-11 11:12:19 +01002090 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002091 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002092 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2093
2094 *state = NULL;
2095 ly_ctx_destroy(ctx, NULL);
2096}
2097
Radek Krejcie86bf772018-12-14 11:39:53 +01002098static void
2099test_uses(void **state)
2100{
2101 *state = test_uses;
2102
2103 struct ly_ctx *ctx;
2104 struct lys_module *mod;
2105 struct lysc_node *parent, *child;
2106
2107 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2108
Radek Krejci0af46292019-01-11 16:02:31 +01002109 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2110 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002111 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2112 "grouping grp_a_top {leaf a1 {type int8;}}"
2113 "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 +01002114 assert_non_null((parent = mod->compiled->data));
2115 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2116 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2117 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002118 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002119 assert_non_null((child = child->next));
2120 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002121 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002122 assert_non_null((child = child->next));
2123 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002124 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002125 assert_non_null((child = child->next));
2126 assert_string_equal("y", child->name);
2127 assert_non_null(child->iffeatures);
2128 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2129 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2130 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2131 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2132 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2133 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2134
2135 /* make the imported module implemented and enable the feature */
2136 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2137 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2138 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2139 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2140 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002141
Radek Krejcib1b59152019-01-07 13:21:56 +01002142 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {type string;}}}");
2143 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp;}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002144 assert_non_null(mod->compiled->data);
2145 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2146 assert_string_equal("b", mod->compiled->data->name);
2147
Radek Krejci7f6a3812019-01-07 13:48:57 +01002148 logbuf_clean();
2149 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2150 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2151 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002152 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2153 assert_string_equal("l", mod->compiled->data->name);
2154 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2155 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2156 assert_string_equal("k", mod->compiled->data->next->name);
2157 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2158 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2159
Radek Krejcie86bf772018-12-14 11:39:53 +01002160 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002161 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 +01002162 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2163
Radek Krejci096235c2019-01-11 11:12:19 +01002164 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002165 "grouping grp {leaf a{type string;}uses grp1;}"
2166 "grouping grp1 {leaf b {type string;}uses grp2;}"
2167 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002168 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2169
Radek Krejci096235c2019-01-11 11:12:19 +01002170 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 +01002171 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2172
Radek Krejci096235c2019-01-11 11:12:19 +01002173 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 +01002174 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002175 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2176
Radek Krejci096235c2019-01-11 11:12:19 +01002177 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 +01002178 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002179 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2180
Radek Krejcie86bf772018-12-14 11:39:53 +01002181 *state = NULL;
2182 ly_ctx_destroy(ctx, NULL);
2183}
2184
Radek Krejci01342af2019-01-03 15:18:08 +01002185static void
2186test_refine(void **state)
2187{
2188 *state = test_refine;
2189
2190 struct ly_ctx *ctx;
2191 struct lys_module *mod;
2192 struct lysc_node *parent, *child;
2193
2194 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2195
Radek Krejci096235c2019-01-11 11:12:19 +01002196 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!;}"
2197 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2198 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2199 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2200 "leaf x {type mytype; mandatory true; must 1;}"
2201 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2202 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002203
Radek Krejcif0089082019-01-07 16:42:01 +01002204 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 +01002205 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002206 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002207 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002208 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2209 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002210 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002211 assert_non_null((parent = mod->compiled->data));
2212 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2213 assert_string_equal("c", parent->name);
2214 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2215 assert_int_equal(LYS_LEAF, child->nodetype);
2216 assert_string_equal("l", child->name);
2217 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2218 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2219 assert_non_null(child = child->next);
2220 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2221 assert_string_equal("ll", child->name);
2222 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2223 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2224 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002225 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2226 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002227 assert_non_null(child = child->next);
2228 assert_int_equal(LYS_CHOICE, child->nodetype);
2229 assert_string_equal("ch", child->name);
2230 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2231 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2232 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002233 assert_non_null(child->iffeatures);
2234 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002235 assert_non_null(child = child->next);
2236 assert_int_equal(LYS_LEAF, child->nodetype);
2237 assert_string_equal("x", child->name);
2238 assert_false(LYS_MAND_TRUE & child->flags);
2239 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002240 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2241 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002242 assert_string_equal("refined", child->dsc);
2243 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002244 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002245 assert_int_equal(LYS_ANYDATA, child->nodetype);
2246 assert_string_equal("a", child->name);
2247 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002248 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2249 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002250 assert_non_null(child->iffeatures);
2251 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002252 assert_string_equal("refined", child->dsc);
2253 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002254 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002255 assert_int_equal(LYS_CONTAINER, child->nodetype);
2256 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002257 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002258 assert_true(LYS_CONFIG_W & child->flags);
2259 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002260
2261 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 +01002262 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002263 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002264 assert_int_equal(LYS_LEAF, child->nodetype);
2265 assert_string_equal("x", child->name);
2266 assert_false(LYS_MAND_TRUE & child->flags);
2267 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2268
2269 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002270 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002271 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002272 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2273
Radek Krejci096235c2019-01-11 11:12:19 +01002274 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002275 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002276 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2277
Radek Krejci096235c2019-01-11 11:12:19 +01002278 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002279 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002280 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2281
Radek Krejci096235c2019-01-11 11:12:19 +01002282 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002283 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002284 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2285
Radek Krejci096235c2019-01-11 11:12:19 +01002286 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002287 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002288 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002289 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002290 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002291 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2292
Radek Krejci096235c2019-01-11 11:12:19 +01002293 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002294 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002295 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" - leaf under the default case.");
2296
Radek Krejci096235c2019-01-11 11:12:19 +01002297 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002298 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002299 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2300
Radek Krejci096235c2019-01-11 11:12:19 +01002301 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002302 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002303 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2304
Radek Krejci096235c2019-01-11 11:12:19 +01002305 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 +01002306 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002307 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2308
Radek Krejci096235c2019-01-11 11:12:19 +01002309 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002310 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002311 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2312
Radek Krejci096235c2019-01-11 11:12:19 +01002313 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002314 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002315 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2316
Radek Krejci096235c2019-01-11 11:12:19 +01002317 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002318 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002319 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2320
Radek Krejci096235c2019-01-11 11:12:19 +01002321 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002322 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002323 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2324
Radek Krejci01342af2019-01-03 15:18:08 +01002325 *state = NULL;
2326 ly_ctx_destroy(ctx, NULL);
2327}
Radek Krejcie86bf772018-12-14 11:39:53 +01002328
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002329int main(void)
2330{
2331 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002332 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2333 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2334 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002335 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2336 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002337 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002338 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2339 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002340 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002341 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002342 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002343 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002344 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002345 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2346 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002347 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002348 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002349 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002350 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002351 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002352 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002353 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01002354 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002355 };
2356
2357 return cmocka_run_group_tests(tests, NULL, NULL);
2358}