blob: 5ad02daf624b333c06a019061625ea9e56d2990d [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 Krejcidd4e8d42018-10-16 14:55:43 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/common.h"
24#include "../../src/tree_schema_internal.h"
25#include "../../src/xpath.h"
Radek Krejcia1911222019-07-22 17:24:50 +020026#include "../../src/plugins_types.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027
28void lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat);
Michal Vaskob36053d2020-03-26 15:49:30 +010029void yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020030
31LY_ERR lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
32 int *parent_times, int *has_predicate);
Radek Krejcidd4e8d42018-10-16 14:55:43 +020033
34#define BUFSIZE 1024
35char logbuf[BUFSIZE] = {0};
36
37/* set to 0 to printing error messages to stderr instead of checking them in code */
38#define ENABLE_LOGGER_CHECKING 1
39
40#if ENABLE_LOGGER_CHECKING
41static void
42logger(LY_LOG_LEVEL level, const char *msg, const char *path)
43{
44 (void) level; /* unused */
45
Radek Krejci87616bb2018-10-31 13:30:52 +010046 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020047 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
48 } else {
49 strncpy(logbuf, msg, BUFSIZE - 1);
50 }
51}
52#endif
53
54static int
55logger_setup(void **state)
56{
57 (void) state; /* unused */
58#if ENABLE_LOGGER_CHECKING
59 ly_set_log_clb(logger, 1);
60#endif
61 return 0;
62}
63
Radek Krejci4f28eda2018-11-12 11:46:16 +010064static int
65logger_teardown(void **state)
66{
67 (void) state; /* unused */
68#if ENABLE_LOGGER_CHECKING
69 if (*state) {
70 fprintf(stderr, "%s\n", logbuf);
71 }
72#endif
73 return 0;
74}
75
Radek Krejcidd4e8d42018-10-16 14:55:43 +020076void
77logbuf_clean(void)
78{
79 logbuf[0] = '\0';
80}
81
82#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010083# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020084#else
85# define logbuf_assert(str)
86#endif
87
Radek Krejcid05cbd92018-12-05 14:26:40 +010088static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
89 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
90 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
91{
92 *module_data = user_data;
93 *format = LYS_IN_YANG;
94 *free_module_data = NULL;
95 return LY_SUCCESS;
96}
97
Radek Krejcidd4e8d42018-10-16 14:55:43 +020098static void
David Sedlák1b623122019-08-05 15:27:49 +020099reset_mod(struct lys_module *module)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100100{
David Sedlák1b623122019-08-05 15:27:49 +0200101 struct ly_ctx *ctx = module->ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100102 lysc_module_free(module->compiled, NULL);
103 lysp_module_free(module->parsed);
104
David Sedlák1b623122019-08-05 15:27:49 +0200105 FREE_STRING(ctx, module->name);
106 FREE_STRING(ctx, module->ns);
107 FREE_STRING(ctx, module->prefix);
108 FREE_STRING(ctx, module->filepath);
109 FREE_STRING(ctx, module->org);
110 FREE_STRING(ctx, module->contact);
111 FREE_STRING(ctx, module->dsc);
112 FREE_STRING(ctx, module->ref);
113 FREE_ARRAY(ctx, module->off_features, lysc_feature_free);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100114
115 memset(module, 0, sizeof *module);
116 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100117 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100118}
119
120static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200121test_module(void **state)
122{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100123 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200124
125 const char *str;
Michal Vaskob36053d2020-03-26 15:49:30 +0100126 struct lys_yang_parser_ctx *ctx = NULL;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200127 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200128 struct lysc_feature *f;
129 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200130
131 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200132 "feature f1;feature f2 {if-feature f1;}}";
David Sedlák1b623122019-08-05 15:27:49 +0200133 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &mod.ctx));
134 reset_mod(&mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200135
Radek Krejcif8f882a2018-10-31 14:51:15 +0100136 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
137 logbuf_assert("Invalid argument mod (lys_compile()).");
138 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
139 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100140 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Michal Vaskob36053d2020-03-26 15:49:30 +0100141 yang_parser_ctx_free(ctx);
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));
Michal Vaskob36053d2020-03-26 15:49:30 +0100174 yang_parser_ctx_free(ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100175 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
176 assert_null(mod.parsed);
David Sedlák1b623122019-08-05 15:27:49 +0200177 reset_mod(&mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100178
179 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100180 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
181 "leaf a {type string;} container a{presence x;}}", &mod));
Michal Vaskob36053d2020-03-26 15:49:30 +0100182 yang_parser_ctx_free(ctx);
Radek Krejci76b3e962018-12-14 17:01:25 +0100183 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Michal Vaskoa3881362020-01-21 15:57:35 +0100184 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/notification statement. /aa:a");
Radek Krejci76b3e962018-12-14 17:01:25 +0100185 assert_null(mod.compiled);
David Sedlák1b623122019-08-05 15:27:49 +0200186 reset_mod(&mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100187
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100188 *state = NULL;
David Sedlák1b623122019-08-05 15:27:49 +0200189 ly_ctx_destroy(mod.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200190}
191
Radek Krejci151a5b72018-10-19 14:21:44 +0200192static void
193test_feature(void **state)
194{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100195 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200196
Michal Vaskob36053d2020-03-26 15:49:30 +0100197 struct lys_yang_parser_ctx *ctx = NULL;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100198 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200199 const char *str;
200 struct lysc_feature *f, *f1;
201
202 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
203 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100204 "feature orfeature {if-feature \"f1 or f2\";}\n"
205 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200206 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200207 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100208 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
209 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200210
David Sedlák1b623122019-08-05 15:27:49 +0200211 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &mod.ctx));
212 reset_mod(&mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100213
214 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Michal Vaskob36053d2020-03-26 15:49:30 +0100215 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100216 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200217 assert_non_null(mod.compiled);
218 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100219 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200220 /* all features are disabled by default */
221 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
222 assert_int_equal(0, lysc_feature_value(f));
223 }
224 /* enable f1 */
225 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200226 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200227 assert_int_equal(1, lysc_feature_value(f1));
228
Radek Krejci87616bb2018-10-31 13:30:52 +0100229 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200230 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200231 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100232 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200233 assert_int_equal(1, lysc_feature_value(f));
234
Radek Krejci87616bb2018-10-31 13:30:52 +0100235 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200236 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200237 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100238 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
239 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200240 assert_int_equal(0, lysc_feature_value(f));
241
242 /* first enable f2, so f5 can be enabled then */
243 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100244 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200245 assert_int_equal(1, lysc_feature_value(f));
246
247 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200248 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200249 assert_int_equal(0, lysc_feature_value(f));
250 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
251 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
252 assert_int_equal(0, lysc_feature_value(f));
253
Radek Krejci87616bb2018-10-31 13:30:52 +0100254 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200255 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200256 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
257 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200258 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100259 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200260 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200261 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200262 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200263 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200264
265 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200266 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200267 /* long evaluation of f8 to need to reallocate internal stack for operators */
268 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200269
Radek Krejci87616bb2018-10-31 13:30:52 +0100270 /* double negation of disabled f1 -> disabled */
271 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
272
Radek Krejci38920282018-11-01 09:24:16 +0100273 /* disable all features */
274 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
275 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
276 assert_int_equal(0, lys_feature_value(&mod, f->name));
277 }
278 /* re-setting already set feature */
279 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
280 assert_int_equal(0, lys_feature_value(&mod, "f1"));
281
282 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100283 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
284 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
285 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100286 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
287 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100288 /* test if not changed */
289 assert_int_equal(1, lys_feature_value(&mod, "f1"));
290 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100291
David Sedlák1b623122019-08-05 15:27:49 +0200292 assert_non_null(modp = lys_parse_mem(mod.ctx, "module b {namespace urn:b;prefix b;"
Radek Krejci0af46292019-01-11 16:02:31 +0100293 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
294 assert_non_null(modp->compiled);
295 assert_non_null(modp->compiled->features);
296 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
297 assert_non_null(modp->compiled->features[0].iffeatures);
298 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
299 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
300 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
301 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
302 assert_non_null(modp->compiled->features);
303 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
304 assert_non_null(modp->compiled->features[1].depfeatures);
305 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
306 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
307
Radek Krejci1aefdf72018-11-01 11:01:39 +0100308 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100309 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
310 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
311
David Sedlák1b623122019-08-05 15:27:49 +0200312 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100313
314 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100315 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));
Michal Vaskob36053d2020-03-26 15:49:30 +0100316 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100317 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200318 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\". /b:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200319 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100320
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100321 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));
Michal Vaskob36053d2020-03-26 15:49:30 +0100322 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100323 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200324 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression. /b:{feature='f2'}");
David Sedlák1b623122019-08-05 15:27:49 +0200325 reset_mod(&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 f{if-feature 'or';}}", &mod));
Michal Vaskob36053d2020-03-26 15:49:30 +0100328 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100329 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200330 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression. /b:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200331 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100332
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100333 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod));
Michal Vaskob36053d2020-03-26 15:49:30 +0100334 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100335 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200336 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses. /b:{feature='f2'}");
David Sedlák1b623122019-08-05 15:27:49 +0200337 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100338
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100339 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));
Michal Vaskob36053d2020-03-26 15:49:30 +0100340 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100341 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200342 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses. /b:{feature='f2'}");
David Sedlák1b623122019-08-05 15:27:49 +0200343 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100344
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100345 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));
Michal Vaskob36053d2020-03-26 15:49:30 +0100346 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100347 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200348 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\". /b:{feature='f2'}");
David Sedlák1b623122019-08-05 15:27:49 +0200349 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100350
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100351 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));
Michal Vaskob36053d2020-03-26 15:49:30 +0100352 yang_parser_ctx_free(ctx);
Radek Krejcif8f882a2018-10-31 14:51:15 +0100353 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200354 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module. /b:{feature='f2'}");
David Sedlák1b623122019-08-05 15:27:49 +0200355 reset_mod(&mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100356
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100357 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod));
Michal Vaskob36053d2020-03-26 15:49:30 +0100358 yang_parser_ctx_free(ctx);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100359 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200360 logbuf_assert("Duplicate identifier \"f1\" of feature statement. /b:{feature='f1'}");
David Sedlák1b623122019-08-05 15:27:49 +0200361 reset_mod(&mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100362
David Sedlák1b623122019-08-05 15:27:49 +0200363 ly_ctx_set_module_imp_clb(mod.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
364 assert_null(lys_parse_mem(mod.ctx, "module z{namespace urn:z; prefix z; include sz;feature f1;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200365 logbuf_assert("Duplicate identifier \"f1\" of feature statement. /z:{feature='f1'}");
Radek Krejcid05cbd92018-12-05 14:26:40 +0100366
David Sedlák1b623122019-08-05 15:27:49 +0200367 assert_null(lys_parse_mem(mod.ctx, "module aa{namespace urn:aa; prefix aa; feature f1 {if-feature f2;} feature f2 {if-feature f1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200368 logbuf_assert("Feature \"f1\" is indirectly referenced from itself. /aa:{feature='f2'}");
David Sedlák1b623122019-08-05 15:27:49 +0200369 assert_null(lys_parse_mem(mod.ctx, "module ab{namespace urn:ab; prefix ab; feature f1 {if-feature f1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200370 logbuf_assert("Feature \"f1\" is referenced from itself. /ab:{feature='f1'}");
Radek Krejci09a1fc52019-02-13 10:55:17 +0100371
David Sedlák1b623122019-08-05 15:27:49 +0200372 assert_null(lys_parse_mem(mod.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f {if-feature ();}}", LYS_IN_YANG));
Radek Krejci6788abc2019-06-14 13:56:49 +0200373 logbuf_assert("Invalid value \"()\" of if-feature - number of features in expression does not match the required number "
374 "of operands for the operations. /bb:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200375 assert_null(lys_parse_mem(mod.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'f1(';}}", LYS_IN_YANG));
Radek Krejci6788abc2019-06-14 13:56:49 +0200376 logbuf_assert("Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses. /bb:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200377 assert_null(lys_parse_mem(mod.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'and f1';}}", LYS_IN_YANG));
Radek Krejci6788abc2019-06-14 13:56:49 +0200378 logbuf_assert("Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation. /bb:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200379 assert_null(lys_parse_mem(mod.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'f1 not ';}}", LYS_IN_YANG));
Radek Krejci6788abc2019-06-14 13:56:49 +0200380 logbuf_assert("Invalid value \"f1 not \" of if-feature - unexpected end of expression. /bb:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200381 assert_null(lys_parse_mem(mod.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'f1 not not ';}}", LYS_IN_YANG));
Radek Krejci6788abc2019-06-14 13:56:49 +0200382 logbuf_assert("Invalid value \"f1 not not \" of if-feature - unexpected end of expression. /bb:{feature='f'}");
David Sedlák1b623122019-08-05 15:27:49 +0200383 assert_null(lys_parse_mem(mod.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f2; "
Radek Krejci6788abc2019-06-14 13:56:49 +0200384 "feature f {if-feature 'or f1 f2';}}", LYS_IN_YANG));
385 logbuf_assert("Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation. /bb:{feature='f'}");
386
Radek Krejci1aefdf72018-11-01 11:01:39 +0100387 /* import reference */
David Sedlák1b623122019-08-05 15:27:49 +0200388 assert_non_null(modp = lys_parse_mem(mod.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100389 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
David Sedlák1b623122019-08-05 15:27:49 +0200390 assert_non_null(modp = lys_parse_mem(mod.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 +0100391 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
392 assert_int_equal(0, lys_feature_value(modp, "f1"));
393 assert_int_equal(1, lys_feature_value(modp, "f2"));
394
Radek Krejcid05cbd92018-12-05 14:26:40 +0100395 *state = NULL;
David Sedlák1b623122019-08-05 15:27:49 +0200396 ly_ctx_destroy(mod.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200397}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200398
Radek Krejci478020e2018-10-30 16:02:14 +0100399static void
400test_identity(void **state)
401{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100402 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100403
404 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100405 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100406
407 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100408 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
409 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
410 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
411 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100412
413 assert_non_null(mod1->compiled);
414 assert_non_null(mod1->compiled->identities);
415 assert_non_null(mod2->compiled);
416 assert_non_null(mod2->compiled->identities);
417
418 assert_non_null(mod1->compiled->identities[0].derived);
419 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
420 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
421 assert_non_null(mod2->compiled->identities[0].derived);
422 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
423 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
424 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
425 assert_non_null(mod2->compiled->identities[1].derived);
426 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
427 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
428 assert_non_null(mod2->compiled->identities[2].derived);
429 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
430 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
431
Radek Krejci4d483a82019-01-17 13:12:50 +0100432 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
433 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
434 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
435 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
436
437 assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200438 logbuf_assert("Duplicate identifier \"i1\" of identity statement. /aa:{identity='i1'}");
Radek Krejcid05cbd92018-12-05 14:26:40 +0100439
Radek Krejci4d483a82019-01-17 13:12:50 +0100440 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
441 assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200442 logbuf_assert("Duplicate identifier \"i1\" of identity statement. /bb:{identity='i1'}");
Radek Krejcid05cbd92018-12-05 14:26:40 +0100443
Radek Krejci38222632019-02-12 16:55:05 +0100444 assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200445 logbuf_assert("Unable to find base (i2) of identity \"i1\". /cc:{identity='i1'}");
Radek Krejci38222632019-02-12 16:55:05 +0100446
447 assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200448 logbuf_assert("Identity \"i1\" is derived from itself. /dd:{identity='i1'}");
Radek Krejci38222632019-02-12 16:55:05 +0100449 assert_null(lys_parse_mem(ctx, "module de{namespace urn:de; prefix de; identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200450 logbuf_assert("Identity \"i1\" is indirectly derived from itself. /de:{identity='i3'}");
Radek Krejci38222632019-02-12 16:55:05 +0100451
Radek Krejci7f2a5362018-11-28 13:05:37 +0100452 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100453 ly_ctx_destroy(ctx, NULL);
454}
455
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100456static void
457test_node_container(void **state)
458{
459 (void) state; /* unused */
460
461 struct ly_ctx *ctx;
462 struct lys_module *mod;
463 struct lysc_node_container *cont;
464
465 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
466 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 +0100467 assert_non_null(mod->compiled);
468 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
469 assert_int_equal(LYS_CONTAINER, cont->nodetype);
470 assert_string_equal("c", cont->name);
471 assert_true(cont->flags & LYS_CONFIG_W);
472 assert_true(cont->flags & LYS_STATUS_CURR);
473
Radek Krejci98094b32018-11-02 16:21:47 +0100474 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 +0100475 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100476 assert_non_null(mod->compiled);
477 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
478 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100479 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100480 assert_non_null((cont = (struct lysc_node_container*)cont->child));
481 assert_int_equal(LYS_CONTAINER, cont->nodetype);
482 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100483 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100484 assert_string_equal("child", cont->name);
485
486 ly_ctx_destroy(ctx, NULL);
487}
488
Radek Krejci0e5d8382018-11-28 16:37:53 +0100489static void
490test_node_leaflist(void **state)
491{
492 *state = test_node_leaflist;
493
494 struct ly_ctx *ctx;
495 struct lys_module *mod;
496 struct lysc_type *type;
497 struct lysc_node_leaflist *ll;
Radek Krejci1c0c3442019-07-23 16:08:47 +0200498 struct lysc_node_leaf *l;
Radek Krejcia1911222019-07-22 17:24:50 +0200499 const char *dflt;
500 int dynamic;
Radek Krejci0e5d8382018-11-28 16:37:53 +0100501
502 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
503
504 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
505 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
506 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
507 "leaf-list ll2 {type leafref {path ../target;}}"
508 "leaf target {type int8;}}",
509 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100510 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
511 assert_non_null(type);
512 assert_int_equal(1, type->refcount);
513 assert_int_equal(LY_TYPE_UNION, type->basetype);
514 assert_non_null(((struct lysc_type_union*)type)->types);
515 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
516 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
517 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
518 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
519 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
520 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
521 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
522 assert_non_null(type);
523 assert_int_equal(1, type->refcount);
524 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
525 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
526 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
527
Radek Krejci6bb080b2018-11-28 17:23:41 +0100528 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 +0100529 assert_non_null(mod->compiled);
530 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
531 assert_int_equal(0, ll->min);
532 assert_int_equal((uint32_t)-1, ll->max);
533
Radek Krejci6bb080b2018-11-28 17:23:41 +0100534 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 +0100535 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100536 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100537 assert_non_null(mod->compiled);
538 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
539 assert_non_null(ll->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +0200540 assert_int_equal(6, ll->type->refcount); /* 3x type's reference, 3x default value's reference (typedef's default does not reference own type) */
Radek Krejci6bb080b2018-11-28 17:23:41 +0100541 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
Radek Krejcia1911222019-07-22 17:24:50 +0200542 assert_string_equal("1", dflt = ll->dflts[0]->realtype->plugin->print(ll->dflts[0], LYD_XML, NULL, NULL, &dynamic));
543 assert_int_equal(0, dynamic);
544 assert_string_equal("1", dflt = ll->dflts[1]->realtype->plugin->print(ll->dflts[1], LYD_XML, NULL, NULL, &dynamic));
545 assert_int_equal(0, dynamic);
Radek Krejci93dcc392019-02-19 10:43:38 +0100546 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_DFLT | LYS_SET_CONFIG, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100547 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
548 assert_non_null(ll->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +0200549 assert_int_equal(6, ll->type->refcount); /* 3x type's reference, 3x default value's reference */
Radek Krejci6bb080b2018-11-28 17:23:41 +0100550 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
Radek Krejcia1911222019-07-22 17:24:50 +0200551 assert_string_equal("10", dflt = ll->dflts[0]->realtype->plugin->print(ll->dflts[0], LYD_XML, NULL, NULL, &dynamic));
552 assert_int_equal(0, dynamic);
Radek Krejcia6d57732018-11-29 13:40:37 +0100553 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
554
Radek Krejcifc11bd72019-04-11 16:00:05 +0200555 /* ordered-by is ignored for state data, RPC/action output parameters and notification content */
Radek Krejcia6d57732018-11-29 13:40:37 +0100556 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
557 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100558 /* but warning is present: */
Radek Krejci327de162019-06-14 12:52:07 +0200559 logbuf_assert("The ordered-by statement is ignored in lists representing state data (/d:ll).");
Radek Krejcia6d57732018-11-29 13:40:37 +0100560 assert_non_null(mod->compiled);
561 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
Radek Krejci93dcc392019-02-19 10:43:38 +0100562 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200563 logbuf_clean();
564
565 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;"
566 "rpc oper {output {leaf-list ll {type string; ordered-by user;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200567 logbuf_assert("The ordered-by statement is ignored in lists representing RPC/action output parameters (/e:oper/output/ll).");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200568 logbuf_clean();
569
570 assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f;"
571 "notification event {leaf-list ll {type string; ordered-by user;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200572 logbuf_assert("The ordered-by statement is ignored in lists representing notification content (/f:event/ll).");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100573
Radek Krejci1c0c3442019-07-23 16:08:47 +0200574 /* forward reference in default */
575 assert_non_null(mod = lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;"
576 "leaf ref {type instance-identifier {require-instance true;} default \"/g:g\";}"
577 "leaf-list g {type string;}}", LYS_IN_YANG));
578 assert_non_null(l = (struct lysc_node_leaf*)mod->compiled->data);
579 assert_string_equal("ref", l->name);
580 assert_non_null(l->dflt);
Radek Krejci950f6a52019-09-12 17:15:32 +0200581 assert_null(l->dflt->canonical_cache);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200582
Radek Krejci6bb080b2018-11-28 17:23:41 +0100583 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100584 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200585 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules. /aa:ll");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100586
Radek Krejci096235c2019-01-11 11:12:19 +0100587 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 Krejcia1911222019-07-22 17:24:50 +0200588 logbuf_assert("Invalid leaf-lists's default value \"x\" which does not fit the type (Invalid empty value \"x\".). /bb:ll");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100589
590 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
591 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100592 assert_non_null(mod->compiled);
593 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
594 assert_non_null(ll->dflts);
595 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100596 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
597 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200598 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\". /dd:ll");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100599
Radek Krejci1c0c3442019-07-23 16:08:47 +0200600 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;"
601 "leaf ref {type instance-identifier {require-instance true;} default \"/ee:g\";}}", LYS_IN_YANG));
602 logbuf_assert("Invalid default - value does not fit the type "
603 "(Invalid instance-identifier \"/ee:g\" value - path \"/ee:g\" does not exists in the YANG schema.). /ee:ref");
604
Radek Krejci0e5d8382018-11-28 16:37:53 +0100605 *state = NULL;
606 ly_ctx_destroy(ctx, NULL);
607}
608
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100609static void
610test_node_list(void **state)
611{
612 *state = test_node_list;
613
614 struct ly_ctx *ctx;
615 struct lys_module *mod;
616 struct lysc_node_list *list;
Radek Krejci0fe9b512019-07-26 17:51:05 +0200617 struct lysc_node *child;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100618
619 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
620
621 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci0fe9b512019-07-26 17:51:05 +0200622 "list l1 {key \"x y\"; ordered-by user; leaf y{type string;if-feature f;} leaf x {type string; when 1;}}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100623 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100624 list = (struct lysc_node_list*)mod->compiled->data;
625 assert_non_null(list);
Radek Krejci0fe9b512019-07-26 17:51:05 +0200626 assert_non_null(list->child);
627 assert_string_equal("x", list->child->name);
628 assert_true(list->child->flags & LYS_KEY);
629 assert_string_equal("y", list->child->next->name);
630 assert_true(list->child->next->flags & LYS_KEY);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100631 assert_non_null(list->child);
632 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
633 assert_true(list->child->flags & LYS_KEY);
634 assert_true(list->child->next->flags & LYS_KEY);
635 list = (struct lysc_node_list*)mod->compiled->data->next;
636 assert_non_null(list);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100637 assert_non_null(list->child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100638 assert_false(list->child->flags & LYS_KEY);
Radek Krejci0fe9b512019-07-26 17:51:05 +0200639 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG | LYS_KEYLESS, list->flags);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100640
641 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
642 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100643 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100644 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100645 list = (struct lysc_node_list*)mod->compiled->data;
646 assert_non_null(list);
647 assert_string_equal("l", list->name);
Radek Krejci0fe9b512019-07-26 17:51:05 +0200648 assert_string_equal("a", list->child->name);
649 assert_true(list->child->flags & LYS_KEY);
650 assert_null(((struct lysc_node_leaf*)list->child)->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100651 assert_non_null(list->uniques);
652 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
653 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
654 assert_string_equal("a", list->uniques[0][0]->name);
655 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
656 assert_string_equal("b", list->uniques[0][1]->name);
657 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
658 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
659 assert_string_equal("e", list->uniques[1][0]->name);
660 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
661 assert_string_equal("d", list->uniques[1][1]->name);
662 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
663
Radek Krejci665421c2018-12-05 08:03:39 +0100664 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
665 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100666 list = (struct lysc_node_list*)mod->compiled->data;
667 assert_non_null(list);
668 assert_string_equal("l", list->name);
Radek Krejci0fe9b512019-07-26 17:51:05 +0200669 assert_string_equal("a", list->child->name);
670 assert_true(list->child->flags & LYS_KEY);
671 assert_int_equal(LY_TYPE_EMPTY, ((struct lysc_node_leaf*)list->child)->type->basetype);
672
673 /* keys order */
674 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
675 "list l {key \"d b c\";leaf a {type string;} leaf b {type string;} leaf c {type string;} leaf d {type string;}}}", LYS_IN_YANG));
676 list = (struct lysc_node_list*)mod->compiled->data;
677 assert_non_null(list);
678 assert_string_equal("l", list->name);
679 assert_non_null(child = list->child);
680 assert_string_equal("d", child->name);
681 assert_true(child->flags & LYS_KEY);
682 assert_non_null(child = child->next);
683 assert_string_equal("b", child->name);
684 assert_true(child->flags & LYS_KEY);
685 assert_non_null(child = child->next);
686 assert_string_equal("c", child->name);
687 assert_true(child->flags & LYS_KEY);
688 assert_non_null(child = child->next);
689 assert_string_equal("a", child->name);
690 assert_false(child->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100691
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100692 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100693 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200694 logbuf_assert("Missing key in list representing configuration data. /aa:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100695
Radek Krejci096235c2019-01-11 11:12:19 +0100696 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
697 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200698 logbuf_assert("List's key must not have any \"when\" statement. /bb:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100699
Radek Krejci096235c2019-01-11 11:12:19 +0100700 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
701 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200702 logbuf_assert("List's key must not have any \"if-feature\" statement. /cc:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100703
Radek Krejci096235c2019-01-11 11:12:19 +0100704 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
705 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200706 logbuf_assert("Key of the configuration list must not be status leaf. /dd:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100707
Radek Krejci096235c2019-01-11 11:12:19 +0100708 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
709 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200710 logbuf_assert("Configuration node cannot be child of any state data node. /ee:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100711
Radek Krejci096235c2019-01-11 11:12:19 +0100712 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
713 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200714 logbuf_assert("The list's key \"x\" not found. /ff:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100715
Radek Krejci096235c2019-01-11 11:12:19 +0100716 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
717 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200718 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf. /gg:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100719
Radek Krejci096235c2019-01-11 11:12:19 +0100720 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
721 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100722 logbuf_assert("Unique statement \"x y\" refers to leaves with different config type. /hh:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100723
Radek Krejci096235c2019-01-11 11:12:19 +0100724 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
725 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200726 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\". /ii:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100727
Radek Krejci096235c2019-01-11 11:12:19 +0100728 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
729 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200730 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found. /jj:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100731
Radek Krejci096235c2019-01-11 11:12:19 +0100732 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
733 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200734 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator. /kk:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100735
Radek Krejci096235c2019-01-11 11:12:19 +0100736 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
737 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200738 logbuf_assert("Duplicated key identifier \"x\". /ll:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100739
Radek Krejci096235c2019-01-11 11:12:19 +0100740 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
741 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200742 logbuf_assert("List's key cannot be of \"empty\" type until it is in YANG 1.1 module. /mm:l/x");
Radek Krejci665421c2018-12-05 08:03:39 +0100743
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100744 *state = NULL;
745 ly_ctx_destroy(ctx, NULL);
746}
747
Radek Krejci056d0a82018-12-06 16:57:25 +0100748static void
749test_node_choice(void **state)
750{
751 *state = test_node_choice;
752
753 struct ly_ctx *ctx;
754 struct lys_module *mod;
755 struct lysc_node_choice *ch;
756 struct lysc_node_case *cs;
757
758 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
759
760 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Michal Vasko5c4e5892019-11-14 12:31:38 +0100761 "choice ch {default a:b; when \"true()\"; case a {leaf a1 {type string;}leaf a2 {type string;}}"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100762 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100763 ch = (struct lysc_node_choice*)mod->compiled->data;
764 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100765 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Michal Vasko175012e2019-11-06 15:49:14 +0100766 assert_int_equal(1, LY_ARRAY_SIZE(ch->when));
767 assert_null(ch->when[0]->context);
Radek Krejci056d0a82018-12-06 16:57:25 +0100768 cs = ch->cases;
769 assert_non_null(cs);
770 assert_string_equal("a", cs->name);
771 assert_ptr_equal(ch, cs->parent);
772 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100773 assert_string_equal("a1", cs->child->name);
774 assert_non_null(cs->child->next);
775 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100776 assert_ptr_equal(cs, cs->child->parent);
777 cs = (struct lysc_node_case*)cs->next;
778 assert_non_null(cs);
779 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100780 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100781 assert_ptr_equal(ch, cs->parent);
782 assert_non_null(cs->child);
783 assert_string_equal("b", cs->child->name);
784 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100785 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
786 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100787 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100788 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100789
Radek Krejci096235c2019-01-11 11:12:19 +0100790 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
791 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100792 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/notification statement. /aa:ch/x/x");
Radek Krejci096235c2019-01-11 11:12:19 +0100793 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
794 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100795 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/notification statement. /aa2:ch/b/y");
Radek Krejci096235c2019-01-11 11:12:19 +0100796 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
797 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200798 logbuf_assert("Duplicate identifier \"a\" of case statement. /bb:ch/a");
Radek Krejci096235c2019-01-11 11:12:19 +0100799 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
800 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200801 logbuf_assert("Duplicate identifier \"b\" of case statement. /bb2:ch/b");
Radek Krejci056d0a82018-12-06 16:57:25 +0100802
Radek Krejci096235c2019-01-11 11:12:19 +0100803 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
804 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200805 logbuf_assert("Default case \"c\" not found. /ca:ch");
Radek Krejci096235c2019-01-11 11:12:19 +0100806 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
807 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200808 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\"). /cb:ch");
Radek Krejci096235c2019-01-11 11:12:19 +0100809 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
810 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200811 logbuf_assert("Mandatory node \"x\" under the default case \"a\". /cc:ch");
Radek Krejcia9026eb2018-12-12 16:04:47 +0100812 /* TODO check with mandatory nodes from augment placed into the case */
813
Radek Krejci056d0a82018-12-06 16:57:25 +0100814 *state = NULL;
815 ly_ctx_destroy(ctx, NULL);
816}
817
Radek Krejci9800fb82018-12-13 14:26:23 +0100818static void
819test_node_anydata(void **state)
820{
821 *state = test_node_anydata;
822
823 struct ly_ctx *ctx;
824 struct lys_module *mod;
825 struct lysc_node_anydata *any;
826
827 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
828
829 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
830 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100831 any = (struct lysc_node_anydata*)mod->compiled->data;
832 assert_non_null(any);
833 assert_int_equal(LYS_ANYDATA, any->nodetype);
Radek Krejci93dcc392019-02-19 10:43:38 +0100834 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags);
Radek Krejci9800fb82018-12-13 14:26:23 +0100835
836 logbuf_clean();
837 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
838 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100839 any = (struct lysc_node_anydata*)mod->compiled->data;
840 assert_non_null(any);
841 assert_int_equal(LYS_ANYXML, any->nodetype);
842 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
843 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
844
845 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100846 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 +0100847 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
848
849 *state = NULL;
850 ly_ctx_destroy(ctx, NULL);
851}
852
Radek Krejcif538ce52019-03-05 10:46:14 +0100853static void
854test_action(void **state)
855{
856 *state = test_action;
857
858 struct ly_ctx *ctx;
859 struct lys_module *mod;
860 const struct lysc_action *rpc;
861
862 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
863
864 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
865 "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG));
866 rpc = mod->compiled->rpcs;
867 assert_non_null(rpc);
868 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
Michal Vasko1bf09392020-03-27 12:38:10 +0100869 assert_int_equal(LYS_RPC, rpc->nodetype);
Radek Krejcif538ce52019-03-05 10:46:14 +0100870 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
871 assert_string_equal("a", rpc->name);
872
873 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
Michal Vasko5d8756a2019-11-07 15:21:00 +0100874 "action b {input {leaf x {type int8;} leaf y {type int8;}}"
875 "output {must \"result > 25\"; must \"/top\"; leaf result {type int16;}}}}}", LYS_IN_YANG));
Radek Krejcif538ce52019-03-05 10:46:14 +0100876 rpc = lysc_node_actions(mod->compiled->data);
877 assert_non_null(rpc);
878 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
879 assert_int_equal(LYS_ACTION, rpc->nodetype);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100880 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
Radek Krejcif538ce52019-03-05 10:46:14 +0100881 assert_string_equal("b", rpc->name);
Michal Vasko5d8756a2019-11-07 15:21:00 +0100882 assert_null(rpc->input.musts);
883 assert_int_equal(2, LY_ARRAY_SIZE(rpc->output.musts));
Radek Krejcif538ce52019-03-05 10:46:14 +0100884
885 /* invalid */
886 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG));
887 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
888
Radek Krejci8cce8532019-03-05 11:27:45 +0100889 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100890 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/notification statement. /bb:x");
Radek Krejci8cce8532019-03-05 11:27:45 +0100891 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1; namespace urn:cc;prefix cc;container c {leaf y {type string;} action y;}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100892 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/notification statement. /cc:c/y");
Radek Krejci8cce8532019-03-05 11:27:45 +0100893 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1; namespace urn:dd;prefix dd;container c {action z; action z;}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100894 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/notification statement. /dd:c/z");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200895 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} notification w;}");
Radek Krejci8cce8532019-03-05 11:27:45 +0100896 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100897 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/notification statement. /ee:w");
Radek Krejci8cce8532019-03-05 11:27:45 +0100898
Radek Krejcifc11bd72019-04-11 16:00:05 +0200899 assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}"
900 "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200901 logbuf_assert("Action \"invalid\" is placed inside another RPC/action. /ff:{augment='/test/input/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200902
903 assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}"
904 "augment /test/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100905 logbuf_assert("Action \"invalid\" is placed inside notification. /gg:{augment='/test/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200906
907 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; notification test {container a {uses grp;}}"
908 "grouping grp {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100909 logbuf_assert("Action \"invalid\" is placed inside notification. /hh:test/a/{uses='grp'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200910
911 *state = NULL;
912 ly_ctx_destroy(ctx, NULL);
913}
914
915static void
916test_notification(void **state)
917{
918 *state = test_notification;
919
920 struct ly_ctx *ctx;
921 struct lys_module *mod;
922 const struct lysc_notif *notif;
923
924 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
925
926 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
927 "notification a1 {leaf x {type int8;}} notification a2;}", LYS_IN_YANG));
928 notif = mod->compiled->notifs;
929 assert_non_null(notif);
930 assert_int_equal(2, LY_ARRAY_SIZE(notif));
931 assert_int_equal(LYS_NOTIF, notif->nodetype);
932 assert_int_equal(LYS_STATUS_CURR, notif->flags);
933 assert_string_equal("a1", notif->name);
934 assert_non_null(notif->data);
935 assert_string_equal("x", notif->data->name);
936 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
937 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
938 assert_string_equal("a2", notif[1].name);
939 assert_null(notif[1].data);
940
941 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
Michal Vasko5d8756a2019-11-07 15:21:00 +0100942 "notification b1 {leaf x {type int8;}} notification b2 {must \"/top\";}}}", LYS_IN_YANG));
Radek Krejcifc11bd72019-04-11 16:00:05 +0200943 notif = lysc_node_notifs(mod->compiled->data);
944 assert_non_null(notif);
945 assert_int_equal(2, LY_ARRAY_SIZE(notif));
946 assert_int_equal(LYS_NOTIF, notif->nodetype);
947 assert_int_equal(LYS_STATUS_CURR, notif->flags);
948 assert_string_equal("b1", notif->name);
949 assert_non_null(notif->data);
950 assert_string_equal("x", notif->data->name);
951 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
Michal Vaskoecd62de2019-11-13 12:35:11 +0100952 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200953 assert_string_equal("b2", notif[1].name);
954 assert_null(notif[1].data);
Michal Vasko5d8756a2019-11-07 15:21:00 +0100955 assert_int_equal(1, LY_ARRAY_SIZE(notif[1].musts));
Radek Krejcifc11bd72019-04-11 16:00:05 +0200956
957 /* invalid */
958 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {notification x;}}", LYS_IN_YANG));
959 logbuf_assert("Invalid keyword \"notification\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
960
961 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} notification x;}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100962 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/notification statement. /bb:x");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200963 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1; namespace urn:cc;prefix cc;container c {leaf y {type string;} notification y;}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100964 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/notification statement. /cc:c/y");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200965 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1; namespace urn:dd;prefix dd;container c {notification z; notification z;}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100966 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/notification statement. /dd:c/z");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200967 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}");
968 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; notification w;}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100969 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/notification statement. /ee:w");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200970
971 assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}"
972 "augment /test/input/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200973 logbuf_assert("Notification \"invalid\" is placed inside RPC/action. /ff:{augment='/test/input/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200974
975 assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}"
976 "augment /test/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100977 logbuf_assert("Notification \"invalid\" is placed inside another notification. /gg:{augment='/test/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200978
979 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; rpc test {input {container a {uses grp;}}}"
980 "grouping grp {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200981 logbuf_assert("Notification \"invalid\" is placed inside RPC/action. /hh:test/input/a/{uses='grp'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200982
Radek Krejcif538ce52019-03-05 10:46:14 +0100983 *state = NULL;
984 ly_ctx_destroy(ctx, NULL);
985}
986
Radek Krejci0f07bed2018-11-13 14:01:40 +0100987/**
988 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
989 * do not test the expression itself
990 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100991static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100992test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100993{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100994 *state = test_type_range;
995
996 struct ly_ctx *ctx;
997 struct lys_module *mod;
998 struct lysc_type *type;
999
1000 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1001
1002 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 +01001003 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1004 assert_non_null(type);
1005 assert_int_equal(LY_TYPE_INT8, type->basetype);
1006 assert_non_null(((struct lysc_type_num*)type)->range);
1007 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1008 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1009 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
1010 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
1011 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
1012 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
1013
1014 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 +01001015 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1016 assert_non_null(type);
1017 assert_int_equal(LY_TYPE_INT16, type->basetype);
1018 assert_non_null(((struct lysc_type_num*)type)->range);
1019 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1020 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1021 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
1022 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
1023 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
1024 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
1025
1026 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 +01001027 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1028 assert_non_null(type);
1029 assert_int_equal(LY_TYPE_INT32, type->basetype);
1030 assert_non_null(((struct lysc_type_num*)type)->range);
1031 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1032 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1033 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
1034 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
1035 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
1036 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
1037
1038 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 +01001039 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1040 assert_non_null(type);
1041 assert_int_equal(LY_TYPE_INT64, type->basetype);
1042 assert_non_null(((struct lysc_type_num*)type)->range);
1043 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1044 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1045 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
1046 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
1047 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
1048 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
1049
1050 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 +01001051 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1052 assert_non_null(type);
1053 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1054 assert_non_null(((struct lysc_type_num*)type)->range);
1055 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1056 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1057 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1058 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1059 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1060 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1061
1062 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 +01001063 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1064 assert_non_null(type);
1065 assert_int_equal(LY_TYPE_UINT16, type->basetype);
1066 assert_non_null(((struct lysc_type_num*)type)->range);
1067 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1068 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1069 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1070 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1071 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1072 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1073
1074 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 +01001075 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1076 assert_non_null(type);
1077 assert_int_equal(LY_TYPE_UINT32, type->basetype);
1078 assert_non_null(((struct lysc_type_num*)type)->range);
1079 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1080 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1081 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1082 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1083 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1084 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1085
1086 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 +01001087 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1088 assert_non_null(type);
1089 assert_int_equal(LY_TYPE_UINT64, type->basetype);
1090 assert_non_null(((struct lysc_type_num*)type)->range);
1091 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1092 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1093 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1094 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1095 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1096 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1097
1098 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
1099 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +01001100 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1101 assert_non_null(type);
1102 assert_int_equal(3, type->refcount);
1103 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1104 assert_non_null(((struct lysc_type_num*)type)->range);
1105 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1106 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1107
Radek Krejcif1394042019-01-08 10:53:34 +01001108 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
1109 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
1110 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +01001111 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1112 assert_non_null(type);
1113 assert_int_equal(1, type->refcount);
1114 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1115 assert_non_null(((struct lysc_type_num*)type)->range);
1116 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
1117 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
1118 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1119 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1120 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1121 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1122
Radek Krejci0f07bed2018-11-13 14:01:40 +01001123 *state = NULL;
1124 ly_ctx_destroy(ctx, NULL);
1125}
1126
1127static void
1128test_type_length(void **state)
1129{
1130 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +01001131
1132 struct ly_ctx *ctx;
1133 struct lys_module *mod;
1134 struct lysc_type *type;
1135
1136 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1137
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001138 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 +01001139 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1140 assert_non_null(type);
1141 assert_non_null(((struct lysc_type_bin*)type)->length);
1142 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001143 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
1144 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001145 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1146 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1147 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1148
1149 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 +01001150 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1151 assert_non_null(type);
1152 assert_non_null(((struct lysc_type_bin*)type)->length);
1153 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1154 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +01001155 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1156 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001157
1158 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 +01001159 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1160 assert_non_null(type);
1161 assert_non_null(((struct lysc_type_bin*)type)->length);
1162 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1163 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1164 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +01001165 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001166
1167 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 +01001168 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1169 assert_non_null(type);
1170 assert_non_null(((struct lysc_type_bin*)type)->length);
1171 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1172 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1173 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1174 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1175
1176 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 +01001177 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1178 assert_non_null(type);
1179 assert_non_null(((struct lysc_type_bin*)type)->length);
1180 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1181 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1182 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1183 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1184
1185 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 +01001186 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1187 assert_non_null(type);
1188 assert_non_null(((struct lysc_type_bin*)type)->length);
1189 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1190 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1191 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1192 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1193 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1194 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1195
1196 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 +01001197 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1198 assert_non_null(type);
1199 assert_non_null(((struct lysc_type_bin*)type)->length);
1200 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1201 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1202 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1203 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1204 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1205 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1206
Radek Krejcib31c8992018-11-12 16:29:16 +01001207 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1208 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001209 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1210 assert_non_null(type);
1211 assert_non_null(((struct lysc_type_bin*)type)->length);
1212 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1213 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1214 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1215 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1216
1217 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1218 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001219 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1220 assert_non_null(type);
1221 assert_non_null(((struct lysc_type_bin*)type)->length);
1222 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1223 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1224 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1225 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1226
1227 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1228 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001229 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1230 assert_non_null(type);
1231 assert_non_null(((struct lysc_type_bin*)type)->length);
1232 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1233 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1234 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1235 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1236 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1237 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1238
1239 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 +01001240 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001241 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1242 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001243 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001244 assert_non_null(((struct lysc_type_bin*)type)->length);
1245 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1246 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1247 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001248 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001249 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1250 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001251 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001252 assert_non_null(((struct lysc_type_bin*)type)->length);
1253 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1254 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1255 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1256 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1257
Radek Krejci56aa27c2018-11-13 14:21:59 +01001258 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1259 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001260 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1261 assert_non_null(type);
1262 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001263 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001264 assert_non_null(((struct lysc_type_str*)type)->length);
1265 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1266 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1267 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1268 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001269
Radek Krejci5969f272018-11-23 10:03:58 +01001270 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1271 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001272 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1273 assert_non_null(type);
1274 assert_int_equal(LY_TYPE_STRING, type->basetype);
1275 assert_int_equal(1, type->refcount);
1276 assert_non_null(((struct lysc_type_str*)type)->length);
1277 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1278 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1279 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1280 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1281
Radek Krejci4f28eda2018-11-12 11:46:16 +01001282 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001283 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001284 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations. /aa:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001285 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001286 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\". /bb:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001287 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001288 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10). /cc:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001289 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001290 logbuf_assert("Invalid length restriction - values are not in ascending order (10). /dd:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001291 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001292 logbuf_assert("Invalid length restriction - values are not in ascending order (10). /ee:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001293 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001294 logbuf_assert("Invalid length restriction - unexpected data (x). /ff:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001295 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001296 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ). /gg:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001297 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001298 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50). /hh:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001299 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001300 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..). /ii:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001301 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001302 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound. /jj:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001303 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001304 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |). /kk:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001305 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 Krejci327de162019-06-14 12:52:07 +02001306 logbuf_assert("Invalid length restriction - values are not in ascending order (15). /kl:l");
Radek Krejcib31c8992018-11-12 16:29:16 +01001307
Radek Krejci096235c2019-01-11 11:12:19 +01001308 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1309 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001310 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting. /ll:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001311 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1312 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001313 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting. /mm:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001314 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1315 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001316 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting. /nn:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001317 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1318 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001319 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting. /oo:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001320 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 +01001321 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001322 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting. /pp:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001323 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 +01001324 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001325 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting. /qq:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001326 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 +01001327 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001328 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting. /rr:l");
Radek Krejcib31c8992018-11-12 16:29:16 +01001329
Radek Krejci327de162019-06-14 12:52:07 +02001330 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
1331 logbuf_assert("Invalid type restrictions for binary type. /ss:l");
Radek Krejci495903e2018-11-13 11:30:45 +01001332
Radek Krejci4f28eda2018-11-12 11:46:16 +01001333 *state = NULL;
1334 ly_ctx_destroy(ctx, NULL);
1335}
1336
Radek Krejcicda74152018-11-13 15:27:32 +01001337static void
1338test_type_pattern(void **state)
1339{
1340 *state = test_type_pattern;
1341
1342 struct ly_ctx *ctx;
1343 struct lys_module *mod;
1344 struct lysc_type *type;
1345
1346 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1347
Radek Krejci027d5802018-11-14 16:57:28 +01001348 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 +01001349 "pattern .* {error-app-tag errortag;error-message error;}"
1350 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001351 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1352 assert_non_null(type);
1353 assert_non_null(((struct lysc_type_str*)type)->patterns);
1354 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1355 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1356 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001357 assert_string_equal(".*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001358 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1359 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1360 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001361 assert_string_equal("[0-9].*[0-9]", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001362 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1363
1364 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1365 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001366 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1367 assert_non_null(type);
1368 assert_int_equal(LY_TYPE_STRING, type->basetype);
1369 assert_int_equal(1, type->refcount);
1370 assert_non_null(((struct lysc_type_str*)type)->patterns);
1371 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001372 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001373 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
Radek Krejci54579462019-04-30 12:47:06 +02001374 assert_string_equal("[0-4]*", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001375 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1376
Radek Krejci04bc1e92018-11-14 14:28:13 +01001377 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1378 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001379 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1380 assert_non_null(type);
1381 assert_int_equal(LY_TYPE_STRING, type->basetype);
1382 assert_int_equal(1, type->refcount);
1383 assert_non_null(((struct lysc_type_str*)type)->patterns);
1384 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001385 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejci04bc1e92018-11-14 14:28:13 +01001386 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1387
Radek Krejcicda74152018-11-13 15:27:32 +01001388 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001389 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 +01001390 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001391 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1392 assert_non_null(type);
1393 assert_non_null(((struct lysc_type_str*)type)->patterns);
1394 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001395 assert_string_equal("^\\p{IsLatinExtended-A}$", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001396 /* TODO check some data "^ř$" */
1397
1398 *state = NULL;
1399 ly_ctx_destroy(ctx, NULL);
1400}
1401
Radek Krejci8b764662018-11-14 14:15:13 +01001402static void
1403test_type_enum(void **state)
1404{
1405 *state = test_type_enum;
1406
1407 struct ly_ctx *ctx;
1408 struct lys_module *mod;
1409 struct lysc_type *type;
1410
1411 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1412
Radek Krejci027d5802018-11-14 16:57:28 +01001413 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 +01001414 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1415 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001416 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1417 assert_non_null(type);
1418 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1419 assert_non_null(((struct lysc_type_enum*)type)->enums);
1420 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1421 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1422 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1423 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1424 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1425 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1426 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1427 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1428 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1429 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1430 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1431 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1432 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1433 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1434
Radek Krejci027d5802018-11-14 16:57:28 +01001435 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 {"
1436 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001437 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1438 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001439 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1440 assert_non_null(type);
1441 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1442 assert_non_null(((struct lysc_type_enum*)type)->enums);
1443 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1444 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1445 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1446 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1447 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1448
1449
1450 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001451 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1452 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1453 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 +01001454 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1455 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1456 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1457 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1458 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1459 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1460 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1461 "enum one; enum one;}}}", LYS_IN_YANG));
1462 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1463 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1464 "enum '';}}}", LYS_IN_YANG));
1465 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1466 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1467 "enum ' x';}}}", LYS_IN_YANG));
1468 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1469 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1470 "enum 'x ';}}}", LYS_IN_YANG));
1471 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1472 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1473 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1474 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1475
Radek Krejci096235c2019-01-11 11:12:19 +01001476 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001477 logbuf_assert("Missing enum substatement for enumeration type. /bb:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001478
Radek Krejci096235c2019-01-11 11:12:19 +01001479 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 +01001480 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001481 logbuf_assert("Invalid enumeration - derived type adds new item \"two\". /cc:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001482
Radek Krejci096235c2019-01-11 11:12:19 +01001483 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 +01001484 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001485 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type. /dd:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001486 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 +01001487 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001488 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647. /ee:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001489
Radek Krejci096235c2019-01-11 11:12:19 +01001490 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 +01001491 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001492 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\". /ff:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001493
Radek Krejci096235c2019-01-11 11:12:19 +01001494 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001495 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001496 logbuf_assert("Missing enum substatement for enumeration type mytype. /gg:l");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001497
Radek Krejci096235c2019-01-11 11:12:19 +01001498 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 +01001499 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001500 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules. /hh:l");
Radek Krejci027d5802018-11-14 16:57:28 +01001501
Radek Krejci8b764662018-11-14 14:15:13 +01001502 *state = NULL;
1503 ly_ctx_destroy(ctx, NULL);
1504}
1505
1506static void
1507test_type_bits(void **state)
1508{
1509 *state = test_type_bits;
1510
1511 struct ly_ctx *ctx;
1512 struct lys_module *mod;
1513 struct lysc_type *type;
1514
1515 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1516
Radek Krejci027d5802018-11-14 16:57:28 +01001517 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 +01001518 "bit automin; bit one {if-feature f; position 1;}"
1519 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001520 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1521 assert_non_null(type);
1522 assert_int_equal(LY_TYPE_BITS, type->basetype);
1523 assert_non_null(((struct lysc_type_bits*)type)->bits);
1524 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1525 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1526 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1527 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1528 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1529 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1530 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1531 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1532 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1533 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1534 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1535 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1536
Radek Krejci027d5802018-11-14 16:57:28 +01001537 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 {"
David Sedlák9fb515f2019-07-11 10:33:58 +02001538 "bit automin; bit one;bit two; bit seven {position 7;}bit eight;}} leaf l { type mytype {bit eight;bit seven;bit automin;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001539 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001540 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1541 assert_non_null(type);
1542 assert_int_equal(LY_TYPE_BITS, type->basetype);
1543 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001544 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1545 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1546 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1547 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1548 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1549 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1550 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001551
1552
1553 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001554 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1555 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1556 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 +01001557 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1558 "bit one {position -1;}}}}", LYS_IN_YANG));
1559 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1560 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
David Sedlák9fb515f2019-07-11 10:33:58 +02001561 "bit one {position 4294967296;}}}}", LYS_IN_YANG));
1562 logbuf_assert("Invalid value \"4294967296\" of \"position\". Line number 1.");
Radek Krejci8b764662018-11-14 14:15:13 +01001563 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1564 "bit one; bit one;}}}", LYS_IN_YANG));
1565 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1566 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1567 "bit '11';}}}", LYS_IN_YANG));
1568 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1569 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1570 "bit 'x1$1';}}}", LYS_IN_YANG));
1571 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1572
Radek Krejci096235c2019-01-11 11:12:19 +01001573 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001574 logbuf_assert("Missing bit substatement for bits type. /bb:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001575
Radek Krejci096235c2019-01-11 11:12:19 +01001576 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 +01001577 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001578 logbuf_assert("Invalid bits - derived type adds new item \"two\". /cc:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001579
Radek Krejci096235c2019-01-11 11:12:19 +01001580 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 +01001581 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001582 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type. /dd:l");
1583 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}", LYS_IN_YANG));
1584 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295. /ee:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001585
David Sedlák9fb515f2019-07-11 10:33:58 +02001586 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {position 1;}bit y {position 1;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001587 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\". /ff:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001588
Radek Krejci096235c2019-01-11 11:12:19 +01001589 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001590 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001591 logbuf_assert("Missing bit substatement for bits type mytype. /gg:l");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001592
Radek Krejci096235c2019-01-11 11:12:19 +01001593 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 +01001594 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001595 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules. /hh:l");
Radek Krejci027d5802018-11-14 16:57:28 +01001596
Radek Krejci8b764662018-11-14 14:15:13 +01001597 *state = NULL;
1598 ly_ctx_destroy(ctx, NULL);
1599}
1600
Radek Krejci6cba4292018-11-15 17:33:29 +01001601static void
1602test_type_dec64(void **state)
1603{
1604 *state = test_type_dec64;
1605
1606 struct ly_ctx *ctx;
1607 struct lys_module *mod;
1608 struct lysc_type *type;
1609
1610 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1611
1612 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1613 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001614 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1615 assert_non_null(type);
1616 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1617 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1618 assert_non_null(((struct lysc_type_dec*)type)->range);
1619 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1620 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1621 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1622 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1623
1624 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1625 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001626 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1627 assert_non_null(type);
1628 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1629 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1630 assert_non_null(((struct lysc_type_dec*)type)->range);
1631 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1632 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1633 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1634 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1635 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1636 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1637 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1638 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1639
Radek Krejci943177f2019-06-14 16:32:43 +02001640 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type decimal64 {"
1641 "fraction-digits 2;range '1 .. 65535';}}leaf l {type mytype;}}", LYS_IN_YANG));
1642 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1643 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1644 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1645 assert_non_null(((struct lysc_type_dec*)type)->range);
1646 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1647 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1648 assert_int_equal(100, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1649 assert_int_equal(6553500, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1650
Radek Krejci6cba4292018-11-15 17:33:29 +01001651 /* invalid cases */
1652 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1653 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1654 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1655 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1656 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1657 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1658
Radek Krejci096235c2019-01-11 11:12:19 +01001659 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001660 logbuf_assert("Missing fraction-digits substatement for decimal64 type. /aa:l");
Radek Krejci6cba4292018-11-15 17:33:29 +01001661
Radek Krejci096235c2019-01-11 11:12:19 +01001662 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 Krejci327de162019-06-14 12:52:07 +02001663 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype. /ab:l");
Radek Krejci643c8242018-11-15 17:51:11 +01001664
Radek Krejci096235c2019-01-11 11:12:19 +01001665 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 +01001666 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001667 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits. /bb:l");
Radek Krejci6cba4292018-11-15 17:33:29 +01001668
Radek Krejci096235c2019-01-11 11:12:19 +01001669 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 +01001670 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001671 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14). /cc:l");
Radek Krejci6cba4292018-11-15 17:33:29 +01001672
Radek Krejci096235c2019-01-11 11:12:19 +01001673 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 +01001674 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001675 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type. /dd:l");
Radek Krejci643c8242018-11-15 17:51:11 +01001676
Radek Krejci096235c2019-01-11 11:12:19 +01001677 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 +01001678 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001679 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type. /de:l");
Radek Krejci643c8242018-11-15 17:51:11 +01001680
Radek Krejci943177f2019-06-14 16:32:43 +02001681 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:c;prefix c;typedef mytype {type decimal64 {"
1682 "fraction-digits 18;range '-10 .. 0';}}leaf l {type mytype;}}", LYS_IN_YANG));
1683 logbuf_assert("Invalid range restriction - invalid value \"-10000000000000000000\". /ee:l");
1684 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:c;prefix c;typedef mytype {type decimal64 {"
1685 "fraction-digits 18;range '0 .. 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1686 logbuf_assert("Invalid range restriction - invalid value \"10000000000000000000\". /ee:l");
1687
Radek Krejci6cba4292018-11-15 17:33:29 +01001688 *state = NULL;
1689 ly_ctx_destroy(ctx, NULL);
1690}
1691
Radek Krejci16c0f822018-11-16 10:46:10 +01001692static void
1693test_type_instanceid(void **state)
1694{
1695 *state = test_type_instanceid;
1696
1697 struct ly_ctx *ctx;
1698 struct lys_module *mod;
1699 struct lysc_type *type;
1700
1701 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1702
1703 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1704 "leaf l1 {type instance-identifier {require-instance true;}}"
1705 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001706 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1707 assert_non_null(type);
1708 assert_int_equal(LY_TYPE_INST, type->basetype);
1709 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1710
1711 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1712 assert_non_null(type);
1713 assert_int_equal(LY_TYPE_INST, type->basetype);
1714 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1715
1716 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1717 assert_non_null(type);
1718 assert_int_equal(LY_TYPE_INST, type->basetype);
1719 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1720
1721 /* invalid cases */
1722 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1723 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1724
Radek Krejci096235c2019-01-11 11:12:19 +01001725 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 Krejci327de162019-06-14 12:52:07 +02001726 logbuf_assert("Invalid type restrictions for instance-identifier type. /aa:l");
Radek Krejci16c0f822018-11-16 10:46:10 +01001727
1728 *state = NULL;
1729 ly_ctx_destroy(ctx, NULL);
1730}
1731
Radek Krejci555cb5b2018-11-16 14:54:33 +01001732static void
1733test_type_identityref(void **state)
1734{
1735 *state = test_type_identityref;
1736
1737 struct ly_ctx *ctx;
1738 struct lys_module *mod;
1739 struct lysc_type *type;
1740
1741 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1742
1743 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;}"
1744 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001745 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001746 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1747 assert_non_null(type);
1748 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1749 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1750 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1751 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1752
1753 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1754 assert_non_null(type);
1755 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1756 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1757 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1758 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1759 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1760
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001761 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1762 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001763 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1764 assert_non_null(type);
1765 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1766 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1767 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1768 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1769 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1770
Radek Krejci555cb5b2018-11-16 14:54:33 +01001771 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001772 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001773 logbuf_assert("Missing base substatement for identityref type. /aa:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001774
Radek Krejci096235c2019-01-11 11:12:19 +01001775 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001776 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001777 logbuf_assert("Missing base substatement for identityref type mytype. /bb:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001778
Radek Krejci096235c2019-01-11 11:12:19 +01001779 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 +01001780 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001781 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type. /cc:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001782
Radek Krejci096235c2019-01-11 11:12:19 +01001783 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 +01001784 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001785 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type. /dd:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001786
Radek Krejci096235c2019-01-11 11:12:19 +01001787 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001788 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001789 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules. /ee:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001790
Radek Krejci096235c2019-01-11 11:12:19 +01001791 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 Krejci327de162019-06-14 12:52:07 +02001792 logbuf_assert("Unable to find base (j) of identityref. /ff:l");
Radek Krejci322614f2018-11-16 15:05:44 +01001793
Radek Krejci096235c2019-01-11 11:12:19 +01001794 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001795 logbuf_assert("Invalid prefix used for base (x:j) of identityref. /gg:l");
Radek Krejci322614f2018-11-16 15:05:44 +01001796
Radek Krejci555cb5b2018-11-16 14:54:33 +01001797 *state = NULL;
1798 ly_ctx_destroy(ctx, NULL);
1799}
1800
Radek Krejcia3045382018-11-22 14:30:31 +01001801static void
1802test_type_leafref(void **state)
1803{
1804 *state = test_type_leafref;
1805
1806 struct ly_ctx *ctx;
1807 struct lys_module *mod;
1808 struct lysc_type *type;
1809 const char *path, *name, *prefix;
1810 size_t prefix_len, name_len;
1811 int parent_times, has_predicate;
1812
1813 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1814
1815 /* lys_path_token() */
1816 path = "invalid_path";
1817 parent_times = 0;
1818 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1819 path = "..";
1820 parent_times = 0;
1821 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1822 path = "..[";
1823 parent_times = 0;
1824 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1825 path = "../";
1826 parent_times = 0;
1827 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1828 path = "/";
1829 parent_times = 0;
1830 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1831
1832 path = "../../pref:id/xxx[predicate]/invalid!!!";
1833 parent_times = 0;
1834 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1835 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1836 assert_int_equal(4, prefix_len);
1837 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1838 assert_int_equal(2, name_len);
1839 assert_int_equal(0, strncmp("id", name, name_len));
1840 assert_int_equal(2, parent_times);
1841 assert_int_equal(0, has_predicate);
1842 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1843 assert_string_equal("[predicate]/invalid!!!", path);
1844 assert_int_equal(0, prefix_len);
1845 assert_null(prefix);
1846 assert_int_equal(3, name_len);
1847 assert_int_equal(0, strncmp("xxx", name, name_len));
1848 assert_int_equal(1, has_predicate);
1849 path += 11;
1850 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1851 assert_string_equal("!!!", path);
1852 assert_int_equal(0, prefix_len);
1853 assert_null(prefix);
1854 assert_int_equal(7, name_len);
1855 assert_int_equal(0, strncmp("invalid", name, name_len));
1856
1857 path = "/absolute/prefix:path";
1858 parent_times = 0;
1859 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1860 assert_string_equal("/prefix:path", path);
1861 assert_int_equal(0, prefix_len);
1862 assert_null(prefix);
1863 assert_int_equal(8, name_len);
1864 assert_int_equal(0, strncmp("absolute", name, name_len));
1865 assert_int_equal(-1, parent_times);
1866 assert_int_equal(0, has_predicate);
1867 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1868 assert_int_equal(0, *path);
1869 assert_int_equal(6, prefix_len);
1870 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1871 assert_int_equal(4, name_len);
1872 assert_int_equal(0, strncmp("path", name, name_len));
1873 assert_int_equal(0, has_predicate);
1874
1875 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001876 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 +01001877 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1878 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001879 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1880 assert_non_null(type);
1881 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1882 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001883 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001884 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1885 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001886 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1887 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1888 assert_non_null(type);
1889 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1890 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001891 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1892 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1893 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001894 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1895
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001896 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 +01001897 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001898 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001899 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1900 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001901 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001902 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1903 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1904 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001905 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1906 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001907 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1908
1909 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001910 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 +01001911 "typedef mytype3 {type c:mytype {require-instance false;}}"
1912 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001913 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1914 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001915 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001916 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1917 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1918 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001919 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1920 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001921 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1922 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1923 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001924 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001925 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1926 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1927 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1928 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1929
Radek Krejci4ce68932018-11-28 12:53:36 +01001930 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1931 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1932 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001933 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1934 assert_non_null(type);
1935 assert_int_equal(1, type->refcount);
1936 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1937 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1938 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1939 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1940 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1941 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1942
Radek Krejci58d171e2018-11-23 13:50:55 +01001943 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001944 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 +01001945 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1946 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001947 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1948 assert_non_null(type);
1949 assert_int_equal(1, type->refcount);
1950 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1951 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1952 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1953 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1954 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1955
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001956 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1957 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1958 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001959 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001960 LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001961 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001962 assert_non_null(type);
1963 assert_int_equal(1, type->refcount);
1964 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001965 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001966 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1967 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1968 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001969
Radek Krejci20e8a182018-12-07 11:43:21 +01001970 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1971 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1972 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1973 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001974 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1975 assert_non_null(type);
1976 assert_int_equal(1, type->refcount);
1977 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1978 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1979 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1980 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1981 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1982
Radek Krejci3d929562019-02-21 11:25:55 +01001983 /* leafref to imported (not yet implemented) module */
1984 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}");
1985 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}"
1986 "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG));
1987 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1988 assert_non_null(type);
1989 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1990 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1991 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1992 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h"));
1993 assert_int_equal(1, mod->implemented);
1994 assert_non_null(mod->compiled->data);
1995 assert_string_equal("h", mod->compiled->data->name);
1996
Radek Krejci92cc8512019-04-25 09:57:06 +02001997 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module j {namespace urn:j;prefix j; leaf j {type string;}}");
1998 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;import j {prefix j;}"
1999 "leaf i {type leafref {path \"/ilist[name = current()/../j:j]/value\";}}"
2000 "list ilist {key name; leaf name {type string;} leaf value {type uint16;}}}", LYS_IN_YANG));
2001 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2002 assert_non_null(type);
2003 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
2004 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
2005 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
2006 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "j"));
2007 assert_int_equal(1, mod->implemented);
2008 assert_non_null(mod->compiled->data);
2009 assert_string_equal("j", mod->compiled->data->name);
2010
Radek Krejci613bc032020-04-07 15:45:31 +02002011 /* leafref with a default value */
2012 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;"
2013 "leaf source {type leafref {path \"../target\";}default true;}"
2014 "leaf target {type boolean;}}", LYS_IN_YANG));
2015 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2016 assert_non_null(type);
2017 assert_int_equal(1, type->refcount);
2018 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
2019 assert_string_equal("../target", ((struct lysc_type_leafref* )type)->path);
2020 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
2021 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
2022 assert_non_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2023 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_node_leaf*)mod->compiled->data)->dflt->realtype->basetype);
2024 assert_int_equal(1, ((struct lysc_node_leaf*)mod->compiled->data)->dflt->boolean);
2025
Radek Krejcia3045382018-11-22 14:30:31 +01002026 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01002027 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 +01002028 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002029 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\". /aa:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002030 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 +01002031 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002032 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path. /bb:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002033 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 +01002034 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002035 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\". /cc:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002036 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 +01002037 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002038 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container. /dd:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002039 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 +01002040 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002041 logbuf_assert("Invalid leafref path at character 3 (/a!invalid). /ee:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002042 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 +01002043 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002044 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list. /ff:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002045 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 +01002046 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002047 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\". /gg:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002048 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01002049 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002050 logbuf_assert("Missing path substatement for leafref type. /hh:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002051 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01002052 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002053 logbuf_assert("Missing path substatement for leafref type mytype. /ii:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002054 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01002055 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01002056 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
Radek Krejci327de162019-06-14 12:52:07 +02002057 "applicable to the leafref itself. /jj:ref");
Radek Krejci096235c2019-01-11 11:12:19 +01002058 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002059 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002060 logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not. /kk:ref");
Radek Krejci58d171e2018-11-23 13:50:55 +01002061
Radek Krejci096235c2019-01-11 11:12:19 +01002062 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002063 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002064 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules. /ll:ref");
Radek Krejci096235c2019-01-11 11:12:19 +01002065 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 +01002066 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002067 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules. /mm:ref");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002068
Radek Krejci096235c2019-01-11 11:12:19 +01002069 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002070 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2071 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2072 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
2073 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002074 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier. /nn:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002075
Radek Krejci096235c2019-01-11 11:12:19 +01002076 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002077 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2078 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2079 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
2080 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002081 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination. /oo:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002082
Radek Krejci096235c2019-01-11 11:12:19 +01002083 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002084 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2085 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2086 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
2087 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002088 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\". /pp:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002089
Radek Krejci096235c2019-01-11 11:12:19 +01002090 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002091 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2092 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2093 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
2094 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002095 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found. /qq:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002096
Radek Krejci096235c2019-01-11 11:12:19 +01002097 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002098 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2099 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2100 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
2101 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002102 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\". /rr:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002103
Radek Krejci096235c2019-01-11 11:12:19 +01002104 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002105 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2106 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2107 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
2108 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002109 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation. /ss:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002110
Radek Krejci096235c2019-01-11 11:12:19 +01002111 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002112 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2113 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2114 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
2115 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002116 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation. /tt:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002117
Radek Krejci096235c2019-01-11 11:12:19 +01002118 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002119 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2120 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2121 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
2122 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002123 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern. /uu:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002124
Radek Krejci096235c2019-01-11 11:12:19 +01002125 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002126 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2127 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2128 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
2129 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002130 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr. /vv:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002131
Radek Krejci096235c2019-01-11 11:12:19 +01002132 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002133 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2134 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2135 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
2136 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002137 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr. /ww:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002138
Radek Krejci096235c2019-01-11 11:12:19 +01002139 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002140 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2141 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2142 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
2143 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002144 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]). /xx:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002145
Radek Krejci096235c2019-01-11 11:12:19 +01002146 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002147 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2148 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2149 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
2150 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002151 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path-keyexpr. /yy:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002152
Radek Krejci096235c2019-01-11 11:12:19 +01002153 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002154 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2155 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2156 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
2157 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002158 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path-keyexpr. /zz:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002159
Radek Krejci096235c2019-01-11 11:12:19 +01002160 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002161 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2162 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
2163 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
2164 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002165 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path-keyexpr \"current()/../c\" refers container instead of leaf. /zza:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002166
Radek Krejci096235c2019-01-11 11:12:19 +01002167 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002168 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
2169 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2170 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
2171 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002172 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found. /zzb:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002173
Radek Krejci613bc032020-04-07 15:45:31 +02002174 assert_null(lys_parse_mem(ctx, "module zzc {namespace urn:zzc;prefix zzc;"
2175 "leaf source {type leafref {path \"../target\";}default true;}}", LYS_IN_YANG));
2176 logbuf_assert("Invalid leafref path - unable to find \"../target\". /zzc:source");
2177
2178 assert_null(lys_parse_mem(ctx, "module zzd {namespace urn:zzd;prefix zzd;"
2179 "leaf source {type leafref {path \"../target\";}default true;}"
2180 "leaf target {type uint8;}}", LYS_IN_YANG));
2181 logbuf_assert("Invalid default - value does not fit the type (Invalid uint8 value \"true\".). /zzd:source");
2182
Radek Krejci412ddfa2018-11-23 11:44:11 +01002183 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01002184 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01002185 "leaf ref1 {type leafref {path /ref2;}}"
2186 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01002187 "leaf ref3 {type leafref {path /ref4;}}"
2188 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002189 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected. /aaa:ref4");
Radek Krejci412ddfa2018-11-23 11:44:11 +01002190
Radek Krejcia3045382018-11-22 14:30:31 +01002191 *state = NULL;
2192 ly_ctx_destroy(ctx, NULL);
2193}
2194
Radek Krejci43699232018-11-23 14:59:46 +01002195static void
2196test_type_empty(void **state)
2197{
2198 *state = test_type_empty;
2199
2200 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01002201
2202 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2203
2204 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002205 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01002206 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +02002207 logbuf_assert("Invalid leaf's default value \"x\" which does not fit the type (Invalid empty value \"x\".). /aa:l");
Radek Krejci43699232018-11-23 14:59:46 +01002208
Radek Krejci096235c2019-01-11 11:12:19 +01002209 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 +01002210 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002211 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x). /bb:l");
Radek Krejci43699232018-11-23 14:59:46 +01002212
2213 *state = NULL;
2214 ly_ctx_destroy(ctx, NULL);
2215}
2216
Radek Krejcicdfecd92018-11-26 11:27:32 +01002217static void
2218test_type_union(void **state)
2219{
2220 *state = test_type_union;
2221
2222 struct ly_ctx *ctx;
2223 struct lys_module *mod;
2224 struct lysc_type *type;
2225
2226 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2227
2228 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2229 "typedef mytype {type union {type int8; type mybasetype;}}"
2230 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002231 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2232 assert_non_null(type);
2233 assert_int_equal(2, type->refcount);
2234 assert_int_equal(LY_TYPE_UNION, type->basetype);
2235 assert_non_null(((struct lysc_type_union*)type)->types);
2236 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2237 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2238 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2239
2240 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2241 "typedef mytype {type union {type int8; type mybasetype;}}"
2242 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002243 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2244 assert_non_null(type);
2245 assert_int_equal(1, type->refcount);
2246 assert_int_equal(LY_TYPE_UNION, type->basetype);
2247 assert_non_null(((struct lysc_type_union*)type)->types);
2248 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2249 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2250 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2251 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2252
2253 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2254 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002255 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2256 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002257 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002258 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2259 assert_non_null(type);
2260 assert_int_equal(1, type->refcount);
2261 assert_int_equal(LY_TYPE_UNION, type->basetype);
2262 assert_non_null(((struct lysc_type_union*)type)->types);
2263 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2264 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2265 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2266 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2267 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2268 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2269
Radek Krejci6be5ff12018-11-26 13:18:15 +01002270 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01002271 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002272 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002273 logbuf_assert("Missing type substatement for union type mytype. /aa:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002274
Radek Krejci096235c2019-01-11 11:12:19 +01002275 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002276 logbuf_assert("Missing type substatement for union type. /bb:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002277
Radek Krejci096235c2019-01-11 11:12:19 +01002278 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 +01002279 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002280 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type. /cc:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002281
Radek Krejci096235c2019-01-11 11:12:19 +01002282 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 +01002283 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002284 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type. /dd:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002285
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002286 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;typedef mytype {type union{type mytype; type string;}}"
2287 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002288 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected. /ee:l");
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002289 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;typedef mytype {type mytype2;}"
2290 "typedef mytype2 {type mytype;} leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002291 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected. /ef:l");
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002292
Radek Krejcicdfecd92018-11-26 11:27:32 +01002293 *state = NULL;
2294 ly_ctx_destroy(ctx, NULL);
2295}
2296
2297static void
2298test_type_dflt(void **state)
2299{
2300 *state = test_type_union;
2301
2302 struct ly_ctx *ctx;
2303 struct lys_module *mod;
2304 struct lysc_type *type;
Radek Krejcia1911222019-07-22 17:24:50 +02002305 struct lysc_node_leaf *leaf;
2306 int dynamic;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002307
2308 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2309
2310 /* default is not inherited from union's types */
2311 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2312 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002313 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2314 assert_non_null(type);
2315 assert_int_equal(1, type->refcount);
2316 assert_int_equal(LY_TYPE_UNION, type->basetype);
2317 assert_non_null(((struct lysc_type_union*)type)->types);
2318 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2319 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2320 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2321 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2322 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2323
2324 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2325 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002326 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2327 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002328 assert_int_equal(3, type->refcount); /* 2x type reference, 1x default value's reference (typedf's default does not reference own type)*/
Radek Krejcicdfecd92018-11-26 11:27:32 +01002329 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci812a5bf2019-09-09 09:18:05 +02002330 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
Radek Krejcia1911222019-07-22 17:24:50 +02002331 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2332 assert_int_equal(0, dynamic);
2333 assert_string_equal("xxx", leaf->units);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002334
2335 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2336 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002337 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2338 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002339 assert_int_equal(3, type->refcount); /* 2x type reference, 1x default value's reference */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002340 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002341 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2342 assert_string_equal("goodbye", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2343 assert_int_equal(0, dynamic);
2344 assert_string_equal("yyy", leaf->units);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002345
Radek Krejci6be5ff12018-11-26 13:18:15 +01002346 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2347 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2348 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002349 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2350 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002351 assert_int_equal(6, type->refcount); /* 4x type reference, 2x default value's reference (1 shared compiled type of typedefs which default does not reference own type) */
Radek Krejci6be5ff12018-11-26 13:18:15 +01002352 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002353 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2354 assert_string_equal("goodbye", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2355 assert_int_equal(0, dynamic);
2356 assert_string_equal("yyy", leaf->units);
Radek Krejci6be5ff12018-11-26 13:18:15 +01002357 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2358 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002359 assert_int_equal(6, type->refcount); /* 4x type reference, 2x default value's reference (1 shared compiled type of typedefs which default does not reference own type) */
Radek Krejci6be5ff12018-11-26 13:18:15 +01002360 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002361 leaf = (struct lysc_node_leaf*)mod->compiled->data->next;
2362 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2363 assert_int_equal(0, dynamic);
2364 assert_string_equal("xxx", leaf->units);
Radek Krejci6be5ff12018-11-26 13:18:15 +01002365
2366 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2367 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002368 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2369 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002370 assert_int_equal(4, type->refcount); /* 3x type reference, 1x default value's reference (typedef's default does not reference own type) */
Radek Krejci6be5ff12018-11-26 13:18:15 +01002371 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002372 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2373 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2374 assert_int_equal(0, dynamic);
2375 assert_string_equal("xxx", leaf->units);
Radek Krejci6be5ff12018-11-26 13:18:15 +01002376
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002377 /* mandatory leaf does not takes default value from type */
2378 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2379 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002380 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2381 assert_non_null(type);
2382 assert_int_equal(LY_TYPE_STRING, type->basetype);
2383 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2384 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2385
Radek Krejcicdfecd92018-11-26 11:27:32 +01002386 *state = NULL;
2387 ly_ctx_destroy(ctx, NULL);
2388}
2389
Radek Krejci665421c2018-12-05 08:03:39 +01002390static void
2391test_status(void **state)
2392{
2393 *state = test_status;
2394
2395 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002396
2397 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2398
Radek Krejci096235c2019-01-11 11:12:19 +01002399 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002400 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002401 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status. /aa:c/l");
Radek Krejci665421c2018-12-05 08:03:39 +01002402
Radek Krejci096235c2019-01-11 11:12:19 +01002403 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002404 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002405 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status. /bb:c/l");
Radek Krejci665421c2018-12-05 08:03:39 +01002406
Radek Krejci096235c2019-01-11 11:12:19 +01002407 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002408 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002409 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status. /cc:c/l");
Radek Krejci665421c2018-12-05 08:03:39 +01002410
Michal Vaskodd878e22019-11-07 11:12:14 +01002411 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:dd;prefix d;"
2412 "container c {leaf l {status obsolete; type string;}}"
2413 "container d {leaf m {when \"../../c/l\"; type string;}}}", LYS_IN_YANG));
2414 logbuf_assert("A current definition \"m\" is not allowed to reference obsolete definition \"l\". /cc:d/m");
2415
Radek Krejci665421c2018-12-05 08:03:39 +01002416 *state = NULL;
2417 ly_ctx_destroy(ctx, NULL);
2418}
2419
Radek Krejcie86bf772018-12-14 11:39:53 +01002420static void
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002421test_grouping(void **state)
2422{
2423 *state = test_grouping;
2424
2425 struct ly_ctx *ctx;
2426
2427 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2428
2429 /* result ok, but a warning about not used locally scoped grouping printed */
2430 assert_non_null(lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; grouping grp1 {leaf a1 {type string;}}"
2431 "container a {leaf x {type string;} grouping grp2 {leaf a2 {type string;}}}}", LYS_IN_YANG));
2432 logbuf_assert("Locally scoped grouping \"grp2\" not used.");
2433 logbuf_clean();
2434
2435 /* result ok - when statement or leafref target must be checked only at the place where the grouping is really instantiated */
2436 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; grouping grp {"
2437 "leaf ref {type leafref {path \"../name\";}}"
2438 "leaf cond {type string; when \"../name = 'specialone'\";}}}", LYS_IN_YANG));
2439 logbuf_assert("");
2440
2441
2442 /* invalid - error in a non-instantiated grouping */
2443 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2444 "grouping grp {leaf x {type leafref;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002445 logbuf_assert("Missing path substatement for leafref type. /aa:{grouping='grp'}/x");
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002446 logbuf_clean();
2447 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2448 "container a {grouping grp {leaf x {type leafref;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002449 logbuf_assert("Missing path substatement for leafref type. /aa:a/{grouping='grp'}/x");
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002450
2451
2452 *state = NULL;
2453 ly_ctx_destroy(ctx, NULL);
2454}
2455
2456static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002457test_uses(void **state)
2458{
2459 *state = test_uses;
2460
2461 struct ly_ctx *ctx;
2462 struct lys_module *mod;
Radek Krejci3641f562019-02-13 15:38:40 +01002463 const struct lysc_node *parent, *child;
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002464 const struct lysc_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01002465
2466 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2467
Radek Krejci0af46292019-01-11 16:02:31 +01002468 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2469 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002470 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2471 "grouping grp_a_top {leaf a1 {type int8;}}"
2472 "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 +01002473 assert_non_null((parent = mod->compiled->data));
2474 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2475 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2476 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002477 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002478 assert_non_null((child = child->next));
2479 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002480 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002481 assert_non_null((child = child->next));
2482 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002483 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002484 assert_non_null((child = child->next));
2485 assert_string_equal("y", child->name);
2486 assert_non_null(child->iffeatures);
2487 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2488 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2489 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2490 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2491 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2492 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2493
2494 /* make the imported module implemented and enable the feature */
2495 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002496 assert_int_equal(LY_SUCCESS, lys_set_implemented(mod));
Radek Krejci0af46292019-01-11 16:02:31 +01002497 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2498 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2499 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002500
Michal Vasko175012e2019-11-06 15:49:14 +01002501 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {when 1; type string;} leaf c {type string;}}}");
Radek Krejci00b874b2019-02-12 10:54:50 +01002502 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp {when 2;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002503 assert_non_null(mod->compiled->data);
2504 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2505 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002506 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Michal Vasko175012e2019-11-06 15:49:14 +01002507 assert_int_equal(1, mod->compiled->data->when[0]->refcount);
2508 assert_non_null(mod->compiled->data->when[0]->context);
2509 assert_string_equal("b", mod->compiled->data->when[0]->context->name);
2510 assert_int_equal(2, mod->compiled->data->when[1]->refcount);
2511 assert_null(mod->compiled->data->when[1]->context);
2512
2513 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2514 assert_string_equal("c", mod->compiled->data->next->name);
2515 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->data->next->when));
2516 assert_int_equal(2, mod->compiled->data->next->when[0]->refcount);
2517 assert_null(mod->compiled->data->next->when[0]->context);
Radek Krejcib1b59152019-01-07 13:21:56 +01002518
Radek Krejci7f6a3812019-01-07 13:48:57 +01002519 logbuf_clean();
2520 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2521 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2522 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002523 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2524 assert_string_equal("l", mod->compiled->data->name);
2525 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2526 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2527 assert_string_equal("k", mod->compiled->data->next->name);
2528 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2529 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2530
Radek Krejci3641f562019-02-13 15:38:40 +01002531 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}"
2532 "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2533 assert_non_null(mod->compiled->data);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002534 assert_non_null(child = lysc_node_children(mod->compiled->data, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002535 assert_string_equal("g", child->name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002536 assert_non_null(child = lysc_node_children(child, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002537 assert_string_equal("x", child->name);
2538
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002539 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e; grouping grp {action g { description \"super g\";}}"
2540 "container top {action e; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2541 assert_non_null(mod->compiled->data);
2542 cont = (const struct lysc_node_container*)mod->compiled->data;
2543 assert_non_null(cont->actions);
2544 assert_int_equal(2, LY_ARRAY_SIZE(cont->actions));
2545 assert_string_equal("e", cont->actions[1].name);
2546 assert_string_equal("g", cont->actions[0].name);
2547 assert_string_equal("ultra g", cont->actions[0].dsc);
2548
2549 assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f; grouping grp {notification g { description \"super g\";}}"
2550 "container top {notification f; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2551 assert_non_null(mod->compiled->data);
2552 cont = (const struct lysc_node_container*)mod->compiled->data;
2553 assert_non_null(cont->notifs);
2554 assert_int_equal(2, LY_ARRAY_SIZE(cont->notifs));
2555 assert_string_equal("f", cont->notifs[1].name);
2556 assert_string_equal("g", cont->notifs[0].name);
2557 assert_string_equal("ultra g", cont->notifs[0].dsc);
2558
Radek Krejci684faf22019-05-27 14:31:16 +02002559 /* empty grouping */
2560 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g; grouping grp; uses grp;}", LYS_IN_YANG));
2561 assert_null(mod->compiled->data);
2562
Radek Krejcie86bf772018-12-14 11:39:53 +01002563 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002564 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002565 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found. /aa:{uses='missinggrp'}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002566
Radek Krejci096235c2019-01-11 11:12:19 +01002567 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002568 "grouping grp {leaf a{type string;}uses grp1;}"
2569 "grouping grp1 {leaf b {type string;}uses grp2;}"
2570 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002571 logbuf_assert("Grouping \"grp\" references itself through a uses statement. /bb:{uses='grp'}/{uses='grp1'}/{uses='grp2'}/{uses='grp'}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002572
Radek Krejci096235c2019-01-11 11:12:19 +01002573 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002574 logbuf_assert("Invalid prefix used for grouping reference. /cc:{uses='a:missingprefix'}");
Radek Krejci76b3e962018-12-14 17:01:25 +01002575
Radek Krejci096235c2019-01-11 11:12:19 +01002576 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 +01002577 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +01002578 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/notification statement. /dd:{uses='grp'}/dd:a");
Radek Krejci76b3e962018-12-14 17:01:25 +01002579
Radek Krejci096235c2019-01-11 11:12:19 +01002580 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 +01002581 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002582 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status. /ee:{uses='grp'}/ee:l");
Radek Krejci7f6a3812019-01-07 13:48:57 +01002583
Radek Krejci95710c92019-02-11 15:49:55 +01002584 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2585 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +01002586 logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/notification statement. /ff:{uses='grp'}/ff:l");
Radek Krejci95710c92019-02-11 15:49:55 +01002587 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2588 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +01002589 logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/notification statement. /fg:m");
Radek Krejci95710c92019-02-11 15:49:55 +01002590
Radek Krejci3641f562019-02-13 15:38:40 +01002591
2592 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}"
2593 "leaf g {type string;}"
2594 "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002595 logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used. /gg:top/{uses='grp'}/{augment='/g'}");
Radek Krejci3641f562019-02-13 15:38:40 +01002596
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002597 assert_non_null(mod = lys_parse_mem(ctx, "module hh {yang-version 1.1;namespace urn:hh;prefix hh;"
2598 "grouping grp {notification g { description \"super g\";}}"
2599 "container top {notification h; uses grp {refine h {description \"ultra h\";}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002600 logbuf_assert("Invalid descendant-schema-nodeid value \"h\" - target node not found. /hh:top/{uses='grp'}/{refine='h'}");
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002601
2602 assert_non_null(mod = lys_parse_mem(ctx, "module ii {yang-version 1.1;namespace urn:ii;prefix ii;"
2603 "grouping grp {action g { description \"super g\";}}"
2604 "container top {action i; uses grp {refine i {description \"ultra i\";}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002605 logbuf_assert("Invalid descendant-schema-nodeid value \"i\" - target node not found. /ii:top/{uses='grp'}/{refine='i'}");
Radek Krejci3641f562019-02-13 15:38:40 +01002606
Radek Krejci1c54f462020-05-12 17:25:34 +02002607 assert_null(lys_parse_mem(ctx, "module jj {yang-version 1.1;namespace urn:jj;prefix jj;"
2608 "grouping grp {leaf j { when \"1\"; type invalid;}}"
2609 "container top {uses grp;}}", LYS_IN_YANG));
2610 logbuf_assert("Referenced type \"invalid\" not found. /jj:top/{uses='grp'}/j");
2611
Radek Krejcie86bf772018-12-14 11:39:53 +01002612 *state = NULL;
2613 ly_ctx_destroy(ctx, NULL);
2614}
2615
Radek Krejci01342af2019-01-03 15:18:08 +01002616static void
2617test_refine(void **state)
2618{
2619 *state = test_refine;
2620
2621 struct ly_ctx *ctx;
2622 struct lys_module *mod;
2623 struct lysc_node *parent, *child;
Radek Krejcia1911222019-07-22 17:24:50 +02002624 struct lysc_node_leaf *leaf;
2625 struct lysc_node_leaflist *llist;
2626 int dynamic;
Radek Krejci01342af2019-01-03 15:18:08 +01002627
2628 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2629
Radek Krejci096235c2019-01-11 11:12:19 +01002630 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!;}"
2631 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2632 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2633 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2634 "leaf x {type mytype; mandatory true; must 1;}"
2635 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2636 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002637
Radek Krejcif0089082019-01-07 16:42:01 +01002638 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 +01002639 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002640 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002641 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002642 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2643 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002644 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002645 assert_non_null((parent = mod->compiled->data));
2646 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2647 assert_string_equal("c", parent->name);
Radek Krejcia1911222019-07-22 17:24:50 +02002648 assert_non_null((leaf = (struct lysc_node_leaf*)((struct lysc_node_container*)parent)->child));
2649 assert_int_equal(LYS_LEAF, leaf->nodetype);
2650 assert_string_equal("l", leaf->name);
2651 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2652 assert_int_equal(0, dynamic);
2653 assert_int_equal(LYS_CONFIG_R, leaf->flags & LYS_CONFIG_MASK);
2654 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
2655 assert_int_equal(LYS_LEAFLIST, llist->nodetype);
2656 assert_string_equal("ll", llist->name);
2657 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts));
2658 assert_string_equal("hello", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
2659 assert_int_equal(0, dynamic);
2660 assert_string_equal("world", llist->dflts[1]->realtype->plugin->print(llist->dflts[1], LYD_XML, NULL, NULL, &dynamic));
2661 assert_int_equal(0, dynamic);
2662 assert_int_equal(2, llist->min);
2663 assert_int_equal(5, llist->max);
2664 assert_non_null(child = llist->next);
Radek Krejci01342af2019-01-03 15:18:08 +01002665 assert_int_equal(LYS_CHOICE, child->nodetype);
2666 assert_string_equal("ch", child->name);
2667 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2668 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2669 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002670 assert_non_null(child->iffeatures);
2671 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcia1911222019-07-22 17:24:50 +02002672 assert_non_null(leaf = (struct lysc_node_leaf*)child->next);
2673 assert_int_equal(LYS_LEAF, leaf->nodetype);
2674 assert_string_equal("x", leaf->name);
2675 assert_false(LYS_MAND_TRUE & leaf->flags);
2676 assert_string_equal("cheers!", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2677 assert_int_equal(0, dynamic);
2678 assert_non_null(leaf->musts);
2679 assert_int_equal(2, LY_ARRAY_SIZE(leaf->musts));
2680 assert_string_equal("refined", leaf->dsc);
2681 assert_string_equal("refined", leaf->ref);
2682 assert_non_null(child = leaf->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002683 assert_int_equal(LYS_ANYDATA, child->nodetype);
2684 assert_string_equal("a", child->name);
2685 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002686 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2687 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002688 assert_non_null(child->iffeatures);
2689 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002690 assert_string_equal("refined", child->dsc);
2691 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002692 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002693 assert_int_equal(LYS_CONTAINER, child->nodetype);
2694 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002695 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002696 assert_true(LYS_CONFIG_W & child->flags);
2697 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002698
2699 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 +01002700 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +02002701 assert_non_null((leaf = (struct lysc_node_leaf*)((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
2702 assert_int_equal(LYS_LEAF, leaf->nodetype);
2703 assert_string_equal("x", leaf->name);
2704 assert_false(LYS_MAND_TRUE & leaf->flags);
2705 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2706 assert_int_equal(0, dynamic);
Radek Krejci01342af2019-01-03 15:18:08 +01002707
2708 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002709 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002710 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002711 logbuf_assert("Invalid refine of default - container cannot hold default value(s). /aa:{uses='g:grp'}/{refine='c'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002712
Radek Krejci096235c2019-01-11 11:12:19 +01002713 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002714 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002715 logbuf_assert("Invalid refine of default - leaf cannot hold 2 default values. /bb:{uses='g:grp'}/{refine='c/l'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002716
Radek Krejci096235c2019-01-11 11:12:19 +01002717 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002718 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002719 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules. /cc:{uses='g:grp'}/{refine='c/ll'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002720
Radek Krejci096235c2019-01-11 11:12:19 +01002721 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002722 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002723 logbuf_assert("Invalid refine of mandatory - leaf-list cannot hold mandatory statement. /dd:{uses='g:grp'}/{refine='c/ll'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002724
Radek Krejci096235c2019-01-11 11:12:19 +01002725 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002726 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002727 logbuf_assert("Invalid refine of mandatory - leaf already has \"default\" statement. /ee:{uses='g:grp'}/{refine='c/l'}");
Radek Krejci096235c2019-01-11 11:12:19 +01002728 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002729 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002730 logbuf_assert("Invalid refine of mandatory - choice already has \"default\" statement. /ef:{uses='g:grp'}/{refine='c/ch'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002731
Radek Krejci096235c2019-01-11 11:12:19 +01002732 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002733 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002734 logbuf_assert("Invalid refine of mandatory under the default case. /ff:{uses='g:grp'}/{refine='c/ch/a/a'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002735
Radek Krejci096235c2019-01-11 11:12:19 +01002736 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002737 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002738 logbuf_assert("Invalid refine of default - the node is mandatory. /gg:{uses='g:grp'}/{refine='c/x'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002739
Radek Krejci096235c2019-01-11 11:12:19 +01002740 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002741 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002742 logbuf_assert("Invalid refine of config - configuration node cannot be child of any state data node. /hh:{uses='g:grp'}/{refine='c/c/l'}");
Radek Krejcib1b59152019-01-07 13:21:56 +01002743
Radek Krejci096235c2019-01-11 11:12:19 +01002744 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 +01002745 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002746 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status. /ii:{uses='grp'}/ii:l");
Radek Krejcib1b59152019-01-07 13:21:56 +01002747
Radek Krejci096235c2019-01-11 11:12:19 +01002748 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002749 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002750 logbuf_assert("Invalid refine of presence statement - leaf cannot hold the presence statement. /jj:{uses='g:grp'}/{refine='c/x'}");
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002751
Radek Krejci096235c2019-01-11 11:12:19 +01002752 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002753 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002754 logbuf_assert("Invalid refine of must statement - choice cannot hold any must statement. /kk:{uses='g:grp'}/{refine='c/ch'}");
Radek Krejci9a564c92019-01-07 14:53:57 +01002755
Radek Krejci096235c2019-01-11 11:12:19 +01002756 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002757 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002758 logbuf_assert("Invalid refine of min-elements statement - leaf cannot hold this statement. /ll:{uses='g:grp'}/{refine='c/x'}");
Radek Krejci6b22ab72019-01-07 15:39:20 +01002759
Radek Krejci096235c2019-01-11 11:12:19 +01002760 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002761 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002762 logbuf_assert("Invalid refine of min-elements statement - \"min-elements\" is bigger than \"max-elements\". /mm:{uses='g:grp'}/{refine='c/ll'}");
Radek Krejcif2271f12019-01-07 16:42:23 +01002763
Radek Krejci01342af2019-01-03 15:18:08 +01002764 *state = NULL;
2765 ly_ctx_destroy(ctx, NULL);
2766}
Radek Krejcie86bf772018-12-14 11:39:53 +01002767
Radek Krejci95710c92019-02-11 15:49:55 +01002768static void
2769test_augment(void **state)
2770{
2771 *state = test_augment;
2772
2773 struct ly_ctx *ctx;
2774 struct lys_module *mod;
2775 const struct lysc_node *node;
2776 const struct lysc_node_choice *ch;
2777 const struct lysc_node_case *c;
Radek Krejci7c7783d2020-04-08 15:34:39 +02002778 const struct lysc_node_container *cont;
Radek Krejcif538ce52019-03-05 10:46:14 +01002779 const struct lysc_action *rpc;
Radek Krejci7c7783d2020-04-08 15:34:39 +02002780 const struct lysc_notif *notif;
Radek Krejci95710c92019-02-11 15:49:55 +01002781
2782 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2783
2784 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2785 "container top {leaf a {type string;}}}");
2786 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2787 "leaf b {type a:atype;}}", LYS_IN_YANG));
2788 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2789 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2790 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2791 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2792 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2793 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2794 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2795 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2796 assert_non_null(node = mod->compiled->data);
2797 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002798 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002799 assert_string_equal(node->name, "a");
2800 assert_non_null(node = node->next);
2801 assert_string_equal(node->name, "c");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002802 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002803 assert_string_equal(node->name, "c");
2804 assert_non_null(node = node->next);
2805 assert_string_equal(node->name, "d");
2806 assert_non_null(node = node->next);
2807 assert_string_equal(node->name, "c");
2808
2809 assert_non_null((mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;choice ch {leaf a {type string;}}"
Radek Krejci00b874b2019-02-12 10:54:50 +01002810 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2811 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002812 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2813 assert_null(mod->compiled->data->next);
2814 assert_string_equal("ch", ch->name);
2815 assert_non_null(c = ch->cases);
2816 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002817 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002818 assert_string_equal("a", c->child->name);
2819 assert_non_null(c = (const struct lysc_node_case*)c->next);
2820 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002821 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002822 assert_string_equal("b", c->child->name);
2823 assert_non_null(c = (const struct lysc_node_case*)c->next);
2824 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002825 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002826 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002827 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002828 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002829 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002830 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2831 assert_null(c->next);
2832
2833 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2834 "container c;"
2835 "augment /c {uses g;}}", LYS_IN_YANG)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002836 assert_non_null(node = lysc_node_children(mod->compiled->data, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002837 assert_string_equal(node->name, "a");
2838
Radek Krejci339f5292019-02-11 16:42:34 +01002839 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2840 "augment /c {container sub;}}");
2841 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2842 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2843 assert_non_null(mod->compiled->data);
2844 assert_string_equal("c", mod->compiled->data->name);
2845 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2846 assert_string_equal("sub", node->name);
2847 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2848 assert_string_equal("main", node->name);
2849
Radek Krejcif538ce52019-03-05 10:46:14 +01002850 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module himp {namespace urn:hi;prefix hi;container top; rpc func;}");
Radek Krejci733988a2019-02-15 15:12:44 +01002851 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;"
2852 "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2853 "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}"
2854 "augment /hi:top {leaf l {type string; mandatory true; config false;}}"
2855 "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG));
2856 assert_non_null(node = mod->compiled->data);
2857 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2858 assert_string_equal("l", node->name);
2859 assert_true(node->flags & LYS_MAND_TRUE);
2860 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
Radek Krejcife909632019-02-12 15:34:42 +01002861 assert_non_null(node = mod->compiled->data);
2862 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2863 assert_string_equal("p", node->name);
Radek Krejci733988a2019-02-15 15:12:44 +01002864 assert_non_null(node = node->next);
2865 assert_string_equal("ll", node->name);
2866 assert_non_null(node = node->next);
2867 assert_string_equal("l", node->name);
2868 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejcife909632019-02-12 15:34:42 +01002869
Radek Krejcif538ce52019-03-05 10:46:14 +01002870 assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}"
2871 "augment /hi:func/input {leaf x {type string;}}"
2872 "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG));
2873 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
2874 assert_non_null(rpc = mod->compiled->rpcs);
2875 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
2876 assert_non_null(rpc->input.data);
2877 assert_string_equal("x", rpc->input.data->name);
2878 assert_null(rpc->input.data->next);
2879 assert_non_null(rpc->output.data);
2880 assert_string_equal("y", rpc->output.data->name);
2881 assert_null(rpc->output.data->next);
2882
Radek Krejci7c7783d2020-04-08 15:34:39 +02002883 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;yang-version 1.1; container root;"
2884 "grouping grp {notification grp-notif;}"
2885 "augment /root {uses grp;}}", LYS_IN_YANG));
2886 assert_non_null(cont = (const struct lysc_node_container*)mod->compiled->data);
2887 assert_null(cont->child);
2888 assert_non_null(notif = cont->notifs);
2889 assert_int_equal(1, LY_ARRAY_SIZE(notif));
2890
Radek Krejci95710c92019-02-11 15:49:55 +01002891 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2892 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002893 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found. /aa:{augment='/x'}");
Radek Krejci95710c92019-02-11 15:49:55 +01002894
2895 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2896 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +01002897 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/notification statement. /bb:{augment='/c'}/a");
Radek Krejci95710c92019-02-11 15:49:55 +01002898
2899
Radek Krejci339f5292019-02-11 16:42:34 +01002900 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2901 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002902 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target. /cc:{augment='/c/a'}");
Radek Krejci339f5292019-02-11 16:42:34 +01002903
2904 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2905 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002906 logbuf_assert("Invalid augment of container node which is not allowed to contain case node \"b\". /dd:{augment='/c'}");
Radek Krejci339f5292019-02-11 16:42:34 +01002907
Radek Krejci733988a2019-02-15 15:12:44 +01002908 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}"
2909 "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002910 logbuf_assert("Invalid augment adding mandatory node \"c\" without making it conditional via when statement. /ee:{augment='/hi:top'}");
Radek Krejcife909632019-02-12 15:34:42 +01002911
Radek Krejci3641f562019-02-13 15:38:40 +01002912 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;"
2913 "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002914 logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\". /ff:{augment='../top'}");
Radek Krejci95710c92019-02-11 15:49:55 +01002915
Radek Krejcif538ce52019-03-05 10:46:14 +01002916 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;"
2917 "augment /func {leaf x {type int8;}}}", LYS_IN_YANG));
Michal Vasko1bf09392020-03-27 12:38:10 +01002918 logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC node which is not an allowed augment's target. /gg:{augment='/func'}");
Radek Krejcif538ce52019-03-05 10:46:14 +01002919
Radek Krejci95710c92019-02-11 15:49:55 +01002920 *state = NULL;
2921 ly_ctx_destroy(ctx, NULL);
2922}
2923
Radek Krejciccd20f12019-02-15 14:12:27 +01002924static void
2925test_deviation(void **state)
2926{
2927 *state = test_deviation;
2928
2929 struct ly_ctx *ctx;
2930 struct lys_module *mod;
2931 const struct lysc_node *node;
Radek Krejci7af64242019-02-18 13:07:53 +01002932 const struct lysc_node_list *list;
Radek Krejcia1911222019-07-22 17:24:50 +02002933 const struct lysc_node_leaflist *llist;
2934 const struct lysc_node_leaf *leaf;
Radek Krejci474f9b82019-07-24 11:36:37 +02002935 const char *str;
Radek Krejcia1911222019-07-22 17:24:50 +02002936 int dynamic;
Radek Krejciccd20f12019-02-15 14:12:27 +01002937
2938 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2939
2940 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002941 "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2942 "choice ch {default c; case b {leaf b{type string;}} case a {leaf a{type string;} leaf x {type string;}}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002943 " case c {leaf c{type string;}}}"
2944 "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}"
2945 "rpc func2;}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002946 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002947 "deviation /a:top/a:b {deviate not-supported;}"
2948 "deviation /a:ch/a:a/a:x {deviate not-supported;}"
2949 "deviation /a:ch/a:c/a:c {deviate not-supported;}"
2950 "deviation /a:ch/a:b {deviate not-supported;}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002951 "deviation /a:ch/a:a/a:a {deviate not-supported;}"
2952 "deviation /a:func1/a:input {deviate not-supported;}"
2953 "deviation /a:func1/a:output {deviate not-supported;}"
2954 "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002955 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2956 assert_non_null(node = mod->compiled->data);
2957 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002958 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejciccd20f12019-02-15 14:12:27 +01002959 assert_string_equal(node->name, "a");
2960 assert_non_null(node = node->next);
2961 assert_string_equal(node->name, "c");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002962 assert_null(node = node->next);
2963 assert_non_null(node = mod->compiled->data->next);
2964 assert_string_equal("ch", node->name);
2965 assert_null(((struct lysc_node_choice*)node)->dflt);
2966 assert_null(((struct lysc_node_choice*)node)->cases);
Radek Krejcif538ce52019-03-05 10:46:14 +01002967 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs));
2968 assert_null(mod->compiled->rpcs[0].input.data);
2969 assert_null(mod->compiled->rpcs[0].output.data);
Radek Krejciccd20f12019-02-15 14:12:27 +01002970
2971 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}"
2972 "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}"
2973 "deviation /c1 {deviate add {units meters;}}"
2974 "deviation /c2 {deviate delete {units meters;}}"
2975 "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG));
2976 assert_non_null(node = mod->compiled->data);
2977 assert_string_equal("c1", node->name);
2978 assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units);
2979 assert_non_null(node = node->next);
2980 assert_string_equal("c2", node->name);
2981 assert_null(((struct lysc_node_leaf*)node)->units);
2982 assert_non_null(node = node->next);
2983 assert_string_equal("c3", node->name);
2984 assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units);
2985
2986 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; leaf c1 {type string; must 1;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002987 "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002988 "deviation /c1 {deviate add {must 3;}}"
2989 "deviation /c2 {deviate delete {must 2;}}"
2990 "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG));
2991 assert_non_null(node = mod->compiled->data);
2992 assert_string_equal("c1", node->name);
2993 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts));
2994 assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr);
2995 assert_non_null(node = node->next);
2996 assert_string_equal("c2", node->name);
Radek Krejcib4532d12019-02-15 16:13:29 +01002997 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts));
2998 assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr);
Radek Krejciccd20f12019-02-15 14:12:27 +01002999 assert_non_null(node = node->next);
3000 assert_string_equal("c3", node->name);
3001 assert_null(((struct lysc_node_leaf*)node)->musts);
3002
Radek Krejcib4532d12019-02-15 16:13:29 +01003003 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module e {yang-version 1.1; namespace urn:e;prefix e; typedef mytype {type string; default nothing;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01003004 "choice a {default a;leaf a {type string;} leaf b {type string;} leaf c {type string; mandatory true;}}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003005 "choice b {default a;leaf a {type string;} leaf b {type string;}}"
3006 "leaf c {default hello; type string;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01003007 "leaf-list d {default hello; default world; type string;}"
3008 "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003009 assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}"
3010 "deviation /x:a {deviate delete {default a;}}"
3011 "deviation /x:b {deviate delete {default x:a;}}"
3012 "deviation /x:c {deviate delete {default hello;}}"
3013 "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG));
3014 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
3015 assert_non_null(node = mod->compiled->data);
3016 assert_null(((struct lysc_node_choice*)node)->dflt);
3017 assert_non_null(node = node->next);
3018 assert_null(((struct lysc_node_choice*)node)->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02003019 assert_non_null(leaf = (struct lysc_node_leaf*)node->next);
3020 assert_null(leaf->dflt);
3021 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3022 assert_int_equal(1, LY_ARRAY_SIZE(llist->dflts));
3023 assert_string_equal("hello", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
3024 assert_int_equal(0, dynamic);
3025 assert_non_null(leaf = (struct lysc_node_leaf*)llist->next);
3026 assert_string_equal("nothing", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
3027 assert_int_equal(0, dynamic);
3028 assert_int_equal(5, leaf->dflt->realtype->refcount); /* 3x type reference, 2x default value reference (typedef's default does not reference own type) */
3029 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3030 assert_int_equal(1, LY_ARRAY_SIZE(llist->dflts));
3031 assert_string_equal("nothing", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
3032 assert_int_equal(0, dynamic);
Radek Krejciccd20f12019-02-15 14:12:27 +01003033
3034 assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}"
3035 "deviation /x:b {deviate add {default x:b;}}"
3036 "deviation /x:c {deviate add {default bye;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01003037 "deviation /x:d {deviate add {default all; default people;}}"
Radek Krejci462cf252019-07-24 09:49:08 +02003038 "deviation /x:c2 {deviate add {default hi; must 1;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01003039 "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01003040 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
3041 assert_non_null(node = mod->compiled->data);
3042 assert_null(((struct lysc_node_choice*)node)->dflt);
3043 assert_non_null(node = node->next);
3044 assert_non_null(((struct lysc_node_choice*)node)->dflt);
3045 assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name);
Radek Krejcia1911222019-07-22 17:24:50 +02003046 assert_non_null(leaf = (struct lysc_node_leaf*)node->next);
3047 assert_non_null(leaf->dflt);
3048 assert_string_equal("bye", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
3049 assert_int_equal(0, dynamic);
3050 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3051 assert_int_equal(3, LY_ARRAY_SIZE(llist->dflts));
3052 assert_string_equal("hello", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
3053 assert_int_equal(0, dynamic);
3054 assert_string_equal("all", llist->dflts[1]->realtype->plugin->print(llist->dflts[1], LYD_XML, NULL, NULL, &dynamic));
3055 assert_int_equal(0, dynamic);
3056 assert_string_equal("people", llist->dflts[2]->realtype->plugin->print(llist->dflts[2], LYD_XML, NULL, NULL, &dynamic));
3057 assert_int_equal(0, dynamic);
3058 assert_non_null(leaf = (struct lysc_node_leaf*)llist->next);
3059 assert_non_null(leaf->dflt);
3060 assert_string_equal("hi", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
3061 assert_int_equal(0, dynamic);
3062 assert_int_equal(6, leaf->dflt->realtype->refcount); /* 3x type reference, 3x default value reference
3063 - previous type's default values were replaced by node's default values where d2 now has 2 default values */
Radek Krejci462cf252019-07-24 09:49:08 +02003064 assert_int_equal(1, LY_ARRAY_SIZE(leaf->musts));
3065 assert_ptr_equal(leaf->musts[0].module, ly_ctx_get_module_implemented(ctx, "g"));
Radek Krejcia1911222019-07-22 17:24:50 +02003066 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3067 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts));
3068 assert_string_equal("hi", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
3069 assert_int_equal(0, dynamic);
3070 assert_string_equal("all", llist->dflts[1]->realtype->plugin->print(llist->dflts[1], LYD_XML, NULL, NULL, &dynamic));
3071 assert_int_equal(0, dynamic);
Radek Krejciccd20f12019-02-15 14:12:27 +01003072
3073 assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}"
3074 "deviation /x:b {deviate replace {default x:a;}}"
3075 "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG));
3076 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
3077 assert_non_null(node = mod->compiled->data);
3078 assert_null(((struct lysc_node_choice*)node)->dflt);
3079 assert_non_null(node = node->next);
3080 assert_non_null(((struct lysc_node_choice*)node)->dflt);
3081 assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name);
Radek Krejcia1911222019-07-22 17:24:50 +02003082 assert_non_null(leaf = (struct lysc_node_leaf*)node->next);
3083 assert_non_null(leaf->dflt);
3084 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
3085 assert_int_equal(0, dynamic);
Radek Krejciccd20f12019-02-15 14:12:27 +01003086
Radek Krejci7af64242019-02-18 13:07:53 +01003087 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;"
3088 "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
3089 "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}"
3090 " leaf c {type string;} leaf d {type string;}}}");
3091 assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}"
3092 "augment /i:l1 {leaf j_c {type string;}}"
3093 "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}"
3094 "deviation /i:l1 {deviate add {unique \"i:c\";}}"
3095 "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG));
3096 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i")));
3097 assert_non_null(list = (struct lysc_node_list*)mod->compiled->data);
3098 assert_string_equal("l1", list->name);
3099 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
3100 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
3101 assert_string_equal("b", list->uniques[0][0]->name);
3102 assert_string_equal("j_c", list->uniques[0][1]->name);
3103 assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1]));
3104 assert_string_equal("c", list->uniques[1][0]->name);
3105 assert_non_null(list = (struct lysc_node_list*)list->next);
3106 assert_string_equal("l2", list->name);
3107 assert_null(list->uniques);
3108
Radek Krejci93dcc392019-02-19 10:43:38 +01003109 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}"
3110 "container top {leaf x {type string;} leaf y {type string; config false;}}"
3111 "deviation /a {deviate add {config false; }}"
3112 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
3113 assert_non_null(node = mod->compiled->data);
3114 assert_string_equal("a", node->name);
3115 assert_true(node->flags & LYS_CONFIG_R);
3116 assert_non_null(node = node->next);
3117 assert_string_equal("top", node->name);
3118 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003119 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01003120 assert_string_equal("x", node->name);
3121 assert_true(node->flags & LYS_CONFIG_R);
3122 assert_non_null(node = node->next);
3123 assert_string_equal("y", node->name);
3124 assert_true(node->flags & LYS_CONFIG_R);
3125
3126 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}"
3127 "container top {config false; leaf x {type string;}}"
3128 "deviation /a {deviate replace {config true;}}"
3129 "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG));
3130 assert_non_null(node = mod->compiled->data);
3131 assert_string_equal("a", node->name);
3132 assert_true(node->flags & LYS_CONFIG_W);
3133 assert_non_null(node = node->next);
3134 assert_string_equal("top", node->name);
3135 assert_true(node->flags & LYS_CONFIG_W);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003136 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01003137 assert_string_equal("x", node->name);
3138 assert_true(node->flags & LYS_CONFIG_W);
3139
Radek Krejcif1421c22019-02-19 13:05:20 +01003140 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;"
3141 "container a {leaf a {type string;}}"
3142 "container b {leaf b {mandatory true; type string;}}"
3143 "deviation /a/a {deviate add {mandatory true;}}"
3144 "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG));
3145 assert_non_null(node = mod->compiled->data);
3146 assert_string_equal("a", node->name);
3147 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003148 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejcif1421c22019-02-19 13:05:20 +01003149 assert_non_null(node = node->next);
3150 assert_string_equal("b", node->name);
3151 assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003152 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
Radek Krejcif1421c22019-02-19 13:05:20 +01003153
Radek Krejci551b12c2019-02-19 16:11:21 +01003154 assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;"
3155 "leaf a {default test; type string;}"
3156 "leaf b {mandatory true; type string;}"
3157 "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}"
3158 "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG));
3159 assert_non_null(node = mod->compiled->data);
3160 assert_string_equal("a", node->name);
3161 assert_null(((struct lysc_node_leaf*)node)->dflt);
3162 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
3163 assert_non_null(node = node->next);
3164 assert_string_equal("b", node->name);
3165 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
3166 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
3167
3168 assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;"
3169 "leaf-list a {type string;}"
3170 "list b {config false;}"
3171 "leaf-list c {min-elements 1; max-elements 10; type string;}"
3172 "list d {min-elements 10; max-elements 100; config false;}"
3173 "deviation /a {deviate add {min-elements 1; max-elements 10;}}"
3174 "deviation /b {deviate add {min-elements 10; max-elements 100;}}"
3175 "deviation /c {deviate replace {min-elements 10; max-elements 100;}}"
3176 "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG));
3177 assert_non_null(node = mod->compiled->data);
3178 assert_string_equal("a", node->name);
3179 assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min);
3180 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max);
3181 assert_non_null(node = node->next);
3182 assert_string_equal("b", node->name);
3183 assert_int_equal(10, ((struct lysc_node_list*)node)->min);
3184 assert_int_equal(100, ((struct lysc_node_list*)node)->max);
3185 assert_non_null(node = node->next);
3186 assert_string_equal("c", node->name);
3187 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min);
3188 assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max);
3189 assert_non_null(node = node->next);
3190 assert_string_equal("d", node->name);
3191 assert_int_equal(1, ((struct lysc_node_list*)node)->min);
3192 assert_int_equal(10, ((struct lysc_node_list*)node)->max);
3193
Radek Krejci33f72892019-02-21 10:36:58 +01003194 assert_non_null(mod = lys_parse_mem(ctx, "module p {yang-version 1.1; namespace urn:p;prefix p; typedef mytype {type int8; default 1;}"
3195 "leaf a {type string; default 10;} leaf-list b {type string;}"
3196 "deviation /a {deviate replace {type mytype;}}"
3197 "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +02003198 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3199 assert_string_equal("a", leaf->name);
3200 assert_int_equal(LY_TYPE_INT8, leaf->type->basetype);
3201 assert_string_equal("10", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
3202 assert_int_equal(0, dynamic);
3203 assert_int_equal(10, leaf->dflt->uint8);
3204 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3205 assert_string_equal("b", llist->name);
3206 assert_int_equal(LY_TYPE_INT8, llist->type->basetype);
3207 assert_int_equal(1, LY_ARRAY_SIZE(llist->dflts));
3208 assert_string_equal("1", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
3209 assert_int_equal(0, dynamic);
3210 assert_int_equal(1, llist->dflts[0]->uint8);
Radek Krejci33f72892019-02-21 10:36:58 +01003211
Radek Krejci950f6a52019-09-12 17:15:32 +02003212 /* instance-identifiers with NULL canonical_cach are changed to string types with a canonical_cache value equal to the original value */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003213 assert_non_null(mod = lys_parse_mem(ctx, "module q {yang-version 1.1; namespace urn:q;prefix q; import e {prefix e;}"
Radek Krejci1c0c3442019-07-23 16:08:47 +02003214 "leaf q {type instance-identifier; default \"/e:d2\";}"
3215 "leaf-list ql {type instance-identifier; default \"/e:d\"; default \"/e:d2\";}}", LYS_IN_YANG));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003216 assert_non_null(lys_parse_mem(ctx, "module qdev {yang-version 1.1; namespace urn:qdev;prefix qd; import q {prefix q;}"
Radek Krejci1c0c3442019-07-23 16:08:47 +02003217 "deviation /q:q { deviate replace {type string;}}"
3218 "deviation /q:ql { deviate replace {type string;}}}", LYS_IN_YANG));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003219 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3220 assert_int_equal(LY_TYPE_STRING, leaf->dflt->realtype->basetype);
Radek Krejci950f6a52019-09-12 17:15:32 +02003221 assert_non_null(leaf->dflt->canonical_cache);
3222 assert_string_equal("/e:d2", leaf->dflt->canonical_cache);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003223 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3224 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts));
3225 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts_mods));
3226 assert_ptr_equal(llist->dflts_mods[0], mod);
3227 assert_int_equal(LY_TYPE_STRING, llist->dflts[0]->realtype->basetype);
Radek Krejci950f6a52019-09-12 17:15:32 +02003228 assert_string_equal("/e:d", llist->dflts[0]->canonical_cache);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003229 assert_ptr_equal(llist->dflts_mods[1], mod);
3230 assert_int_equal(LY_TYPE_STRING, llist->dflts[0]->realtype->basetype);
Radek Krejci950f6a52019-09-12 17:15:32 +02003231 assert_string_equal("/e:d2", llist->dflts[1]->canonical_cache);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003232
3233 assert_non_null(mod = lys_parse_mem(ctx, "module r {yang-version 1.1; namespace urn:r;prefix r;"
3234 "typedef mytype {type uint8; default 200;}"
3235 "leaf r {type mytype;} leaf-list lr {type mytype;}"
3236 "deviation /r:r {deviate replace {type string;}}"
3237 "deviation /r:lr {deviate replace {type string;}}}", LYS_IN_YANG));
3238 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3239 assert_string_equal("r", leaf->name);
3240 assert_null(leaf->dflt);
3241 assert_null(leaf->dflt_mod);
3242 assert_non_null(llist = (struct lysc_node_leaflist* )leaf->next);
3243 assert_string_equal("lr", llist->name);
3244 assert_null(llist->dflts);
3245 assert_null(llist->dflts_mods);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003246
Radek Krejci474f9b82019-07-24 11:36:37 +02003247 assert_non_null(mod = lys_parse_mem(ctx, "module s {yang-version 1.1; namespace urn:s;prefix s;"
3248 "leaf s {type instance-identifier {require-instance true;} default /s:x;}"
3249 "leaf x {type string;} leaf y {type string;}"
3250 "deviation /s:s {deviate replace {default /s:y;}}}", LYS_IN_YANG));
3251 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3252 assert_string_equal("s", leaf->name);
3253 assert_non_null(leaf->dflt);
3254 assert_non_null(str = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, mod, &dynamic));
3255 assert_string_equal("/s:y", str);
3256 if (dynamic) { free((char*)str); }
3257
Radek Krejci88ef64b2019-02-18 16:02:06 +01003258 assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003259 "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003260 logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found. /aa1:{deviation='/a:top/a:z'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003261 assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}"
3262 "deviation /a:top/a:a {deviate not-supported;}"
3263 "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG));
3264 /* warning */
3265 logbuf_assert("Useless multiple (2) deviates on node \"/a:top/a:a\" since the node is not-supported.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003266
3267 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}"
3268 "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003269 logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\". /bb:{deviation='a:top/a:a'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003270
3271 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;"
3272 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003273 logbuf_assert("Invalid deviation of container node - it is not possible to add \"units\" property. /cc:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003274 assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}"
3275 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003276 logbuf_assert("Invalid deviation adding \"units\" property which already exists (with value \"centimeters\"). /cd:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003277
3278 assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;"
3279 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003280 logbuf_assert("Invalid deviation of container node - it is not possible to delete \"units\" property. /dd1:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003281 assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}"
3282 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003283 logbuf_assert("Invalid deviation deleting \"units\" property \"meters\" which is not present. /dd2:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003284 assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}"
3285 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003286 logbuf_assert("Invalid deviation deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\"."
3287 " /dd3:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003288
3289 assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;"
3290 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003291 logbuf_assert("Invalid deviation of container node - it is not possible to replace \"units\" property. /ee1:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003292 assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}"
3293 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003294 logbuf_assert("Invalid deviation replacing \"units\" property \"meters\" which is not present. /ee2:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003295
3296 /* the default is already deleted in /e:a byt module f */
3297 assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}"
3298 "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003299 logbuf_assert("Invalid deviation deleting \"default\" property \"x:a\" which is not present. /ff1:{deviation='/e:a'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003300 assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}"
3301 "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003302 logbuf_assert("Invalid deviation deleting \"default\" property \"x:a\" of choice. "
3303 "The prefix does not match any imported module of the deviation module. /ff2:{deviation='/e:b'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003304 assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}"
3305 "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003306 logbuf_assert("Invalid deviation deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\". /ff3:{deviation='/e:b'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003307 assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}"
3308 "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003309 logbuf_assert("Invalid deviation deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module. /ff4:{deviation='/e:b'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003310 assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;"
3311 "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003312 logbuf_assert("Invalid deviation of anyxml node - it is not possible to delete \"default\" property. /ff5:{deviation='/a'}");
Radek Krejci1c0c3442019-07-23 16:08:47 +02003313 assert_null(lys_parse_mem(ctx, "module ff6 {namespace urn:ff6;prefix ff6; import e {prefix e;}"
3314 "deviation /e:c {deviate delete {default hi;}}}", LYS_IN_YANG));
3315 logbuf_assert("Invalid deviation deleting \"default\" property \"hi\" which does not match the target's property value \"hello\". /ff6:{deviation='/e:c'}");
3316 assert_null(lys_parse_mem(ctx, "module ff7 {namespace urn:ff7;prefix ff7; import e {prefix e;}"
3317 "deviation /e:d {deviate delete {default hi;}}}", LYS_IN_YANG));
3318 logbuf_assert("Invalid deviation deleting \"default\" property \"hi\" which does not match any of the target's property values. /ff7:{deviation='/e:d'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003319
3320 assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}"
3321 "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003322 logbuf_assert("Invalid deviation adding \"default\" property which already exists (with value \"a\"). /gg1:{deviation='/e:b'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003323 assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01003324 "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003325 logbuf_assert("Invalid deviation adding \"default\" property \"x:a\" of choice. "
3326 "The prefix does not match any imported module of the deviation module. /gg2:{deviation='/e:a'}");
Radek Krejci468a94f2019-02-15 14:52:36 +01003327 assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}"
3328 "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003329 logbuf_assert("Invalid deviation adding \"default\" property \"a\" of choice - the specified case does not exists. /gg3:{deviation='/e:a'}");
Radek Krejci468a94f2019-02-15 14:52:36 +01003330 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003331 "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003332 logbuf_assert("Invalid deviation adding \"default\" property which already exists (with value \"hello\"). /gg4:{deviation='/e:c'}");
Radek Krejci468a94f2019-02-15 14:52:36 +01003333 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
3334 "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003335 logbuf_assert("Invalid deviation adding \"default\" property \"e:c\" of choice - mandatory node \"c\" under the default case. /gg4:{deviation='/e:a'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003336 assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}"
3337 "deviation /x {deviate add {default error;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003338 logbuf_assert("Invalid deviation combining default value and mandatory leaf. /gg5:{deviation='/x'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003339
3340 assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}"
3341 "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003342 logbuf_assert("Invalid deviation of leaf-list node - it is not possible to replace \"default\" property. /hh1:{deviation='/e:d'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003343
Radek Krejci7af64242019-02-18 13:07:53 +01003344 assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}"
3345 "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003346 logbuf_assert("Invalid deviation deleting \"unique\" property \"x\" which does not match any of the target's property values. /ii1:{deviation='/i:l1'}");
Radek Krejci7af64242019-02-18 13:07:53 +01003347 assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}"
3348 "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003349 logbuf_assert("Invalid deviation deleting \"unique\" property \"d\" which does not match any of the target's property values. /ii2:{deviation='/i:l2'}");
Radek Krejci7af64242019-02-18 13:07:53 +01003350 assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}"
3351 "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003352 logbuf_assert("Invalid deviation of leaf node - it is not possible to delete \"unique\" property. /ii3:{deviation='/x'}");
Radek Krejci7af64242019-02-18 13:07:53 +01003353 assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}"
3354 "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003355 logbuf_assert("Invalid deviation of leaf node - it is not possible to add \"unique\" property. /ii4:{deviation='/x'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003356
Radek Krejci93dcc392019-02-19 10:43:38 +01003357 assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}"
3358 "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003359 logbuf_assert("Invalid deviation of case node - it is not possible to add \"config\" property. /jj1:{deviation='/ch/a'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003360 assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}"
3361 "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003362 logbuf_assert("Invalid deviation of config - configuration node cannot be child of any state data node. /jj2:{deviation='/top/x'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003363 assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}"
3364 "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003365 logbuf_assert("Invalid deviation replacing \"config\" property \"config false\" which is not present. /jj3:{deviation='/top/x'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003366 assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}"
3367 "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003368 logbuf_assert("Invalid deviation of case node - it is not possible to replace \"config\" property. /jj4:{deviation='/ch/a'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003369 assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}"
3370 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003371 logbuf_assert("Invalid deviation of config - configuration node cannot be child of any state data node. /jj5:{deviation='/top'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003372 assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}"
3373 "deviation /x {deviate add {config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003374 logbuf_assert("Invalid deviation adding \"config\" property which already exists (with value \"config false\"). /jj6:{deviation='/x'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003375
3376 assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}"
3377 "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003378 logbuf_assert("Invalid deviation of mandatory - container cannot hold mandatory statement. /kk1:{deviation='/top'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003379 assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}"
3380 "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003381 logbuf_assert("Invalid deviation replacing \"mandatory\" property \"mandatory true\" which is not present. /kk2:{deviation='/top'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003382 assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}"
3383 "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003384 logbuf_assert("Invalid deviation replacing \"mandatory\" property \"mandatory true\" which is not present. /kk3:{deviation='/top/x'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003385 assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}"
3386 "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003387 logbuf_assert("Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory true\"). /kk4:{deviation='/x'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003388
Radek Krejci551b12c2019-02-19 16:11:21 +01003389 assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}"
3390 "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003391 logbuf_assert("Invalid deviation combining default value and mandatory leaf. /ll1:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003392 assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}"
3393 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003394 logbuf_assert("Invalid deviation combining default value and mandatory leaf-list. /ll2:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003395 assert_null(lys_parse_mem(ctx, "module ll2 {namespace urn:ll2;prefix ll2; choice ch {default a; leaf a {type string;} leaf b {type string;}}"
3396 "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003397 logbuf_assert("Invalid deviation combining default case and mandatory choice. /ll2:{deviation='/ch'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003398
3399 assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}"
3400 "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003401 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 10 is bigger than max value 5. /mm1:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003402 assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}"
3403 "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003404 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 20 is bigger than max value 10. /mm2:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003405 assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}"
3406 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003407 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 5 is bigger than max value 1. /mm3:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003408 assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}"
3409 "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003410 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 20 is bigger than max value 10. /mm4:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003411 assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}"
3412 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003413 logbuf_assert("Invalid deviation adding \"min-elements\" property which already exists (with value \"5\"). /mm5:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003414 assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}"
3415 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003416 logbuf_assert("Invalid deviation adding \"min-elements\" property which already exists (with value \"5\"). /mm6:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003417 assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}"
3418 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003419 logbuf_assert("Invalid deviation adding \"max-elements\" property which already exists (with value \"5\"). /mm7:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003420 assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}"
3421 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003422 logbuf_assert("Invalid deviation adding \"max-elements\" property which already exists (with value \"5\"). /mm8:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003423 assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}"
3424 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003425 logbuf_assert("Invalid deviation replacing with \"min-elements\" property \"1\" which is not present. /mm9:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003426 assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}"
3427 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003428 logbuf_assert("Invalid deviation replacing with \"min-elements\" property \"1\" which is not present. /mm10:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003429 assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}"
3430 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003431 logbuf_assert("Invalid deviation replacing with \"max-elements\" property \"1\" which is not present. /mm11:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003432 assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }"
3433 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003434 logbuf_assert("Invalid deviation replacing with \"max-elements\" property \"1\" which is not present. /mm12:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003435
Radek Krejci33f72892019-02-21 10:36:58 +01003436 assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;"
3437 "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003438 logbuf_assert("Invalid deviation of anyxml node - it is not possible to replace \"type\" property. /nn1:{deviation='/x'}");
Radek Krejci33f72892019-02-21 10:36:58 +01003439 assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}"
3440 "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003441 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules. /nn2:{deviation='/x'}");
Radek Krejci33f72892019-02-21 10:36:58 +01003442
Radek Krejci1c0c3442019-07-23 16:08:47 +02003443 assert_null(lys_parse_mem(ctx, "module oo1 {namespace urn:oo1;prefix oo1; leaf x {type uint16; default 300;}"
3444 "deviation /x {deviate replace {type uint8;}}}", LYS_IN_YANG));
3445 logbuf_assert("Invalid deviation replacing leaf's type - the leaf's default value \"300\" does not match the type "
3446 "(Value \"300\" is out of uint8's min/max bounds.). /oo1:{deviation='/x'}");
3447 assert_null(lys_parse_mem(ctx, "module oo2 {yang-version 1.1;namespace urn:oo2;prefix oo2; leaf-list x {type uint16; default 10; default 300;}"
3448 "deviation /x {deviate replace {type uint8;}}}", LYS_IN_YANG));
3449 logbuf_assert("Invalid deviation replacing leaf-list's type - the leaf-list's default value \"300\" does not match the type "
3450 "(Value \"300\" is out of uint8's min/max bounds.). /oo2:{deviation='/x'}");
3451 assert_null(lys_parse_mem(ctx, "module oo3 {namespace urn:oo3;prefix oo3; leaf x {type uint8;}"
3452 "deviation /x {deviate add {default 300;}}}", LYS_IN_YANG));
3453 logbuf_assert("Invalid deviation setting \"default\" property \"300\" which does not fit the type "
3454 "(Value \"300\" is out of uint8's min/max bounds.). /oo3:{deviation='/x'}");
3455
Radek Krejci474f9b82019-07-24 11:36:37 +02003456/* TODO recompiling reference object after deviation changes schema tree
3457 assert_non_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp; leaf l { type leafref {path /c/x;}}"
3458 "container c {leaf x {type string;} leaf y {type string;}}}", LYS_IN_YANG));
3459 assert_null(lys_parse_mem(ctx, "module pp1 {namespace urn:pp1;prefix pp1; import pp {prefix pp;}"
3460 "deviation /pp:c/pp:x {deviate not-supported;}}", LYS_IN_YANG));
3461 logbuf_assert("???. /pp:l}");
3462*/
3463
Radek Krejciccd20f12019-02-15 14:12:27 +01003464 *state = NULL;
3465 ly_ctx_destroy(ctx, NULL);
3466}
3467
Michal Vaskoecd62de2019-11-13 12:35:11 +01003468static void
3469test_when(void **state)
3470{
3471 *state = test_when;
3472
3473 struct ly_ctx *ctx;
3474
3475 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
3476
3477 assert_null(lys_parse_mem(ctx,
3478 "module a {"
3479 "namespace urn:a;"
3480 "prefix a;"
3481 "container cont {"
3482 "leaf l {"
3483 "when \"/cont/lst[val='25']\";"
3484 "type empty;"
3485 "}"
3486 "list lst {"
3487 "key \"k\";"
3488 "leaf k {"
3489 "type uint8;"
3490 "}"
3491 "leaf val {"
3492 "when /cont2;"
3493 "type int32;"
3494 "}"
3495 "}"
3496 "}"
3497 "container cont2 {"
3498 "presence \"a\";"
3499 "when ../cont/l;"
3500 "}"
3501 "}"
3502 , LYS_IN_YANG));
Michal Vaskof6e51882019-12-16 09:59:45 +01003503 logbuf_assert("When condition of \"cont2\" includes a self-reference (referenced by when of \"l\"). /a:cont2");
Michal Vaskoecd62de2019-11-13 12:35:11 +01003504
3505 assert_null(lys_parse_mem(ctx,
3506 "module a {"
3507 "namespace urn:a;"
3508 "prefix a;"
3509 "container cont {"
3510 "leaf l {"
3511 "when \"/cont/lst[val='25']\";"
3512 "type empty;"
3513 "}"
3514 "list lst {"
3515 "key \"k\";"
3516 "leaf k {"
3517 "type uint8;"
3518 "}"
3519 "leaf val {"
3520 "when /cont2;"
3521 "type int32;"
3522 "}"
3523 "}"
3524 "}"
3525 "container cont2 {"
3526 "presence \"a\";"
3527 "when ../cont/lst/val;"
3528 "}"
3529 "}"
3530 , LYS_IN_YANG));
Michal Vaskof6e51882019-12-16 09:59:45 +01003531 logbuf_assert("When condition of \"cont2\" includes a self-reference (referenced by when of \"val\"). /a:cont2");
Michal Vaskoecd62de2019-11-13 12:35:11 +01003532
Michal Vasko5c4e5892019-11-14 12:31:38 +01003533 assert_null(lys_parse_mem(ctx,
3534 "module a {"
3535 "namespace urn:a;"
3536 "prefix a;"
3537 "leaf val {"
3538 "type int64;"
3539 "when \"../val='25'\";"
3540 "}"
3541 "}"
3542 , LYS_IN_YANG));
Michal Vaskof6e51882019-12-16 09:59:45 +01003543 logbuf_assert("When condition of \"val\" is accessing its own conditional node. /a:val");
Michal Vasko5c4e5892019-11-14 12:31:38 +01003544
3545 assert_null(lys_parse_mem(ctx,
3546 "module a {"
3547 "namespace urn:a;"
3548 "prefix a;"
3549 "grouping grp {"
3550 "leaf val {"
3551 "type int64;"
3552 "}"
3553 "}"
3554 "uses grp {"
3555 "when \"val='25'\";"
3556 "}"
3557 "}"
3558 , LYS_IN_YANG));
Michal Vaskof6e51882019-12-16 09:59:45 +01003559 logbuf_assert("When condition of \"val\" is accessing its own conditional node. /a:val");
Michal Vasko5c4e5892019-11-14 12:31:38 +01003560
3561 assert_null(lys_parse_mem(ctx,
3562 "module a {"
3563 "namespace urn:a;"
3564 "prefix a;"
3565 "augment /cont {"
3566 "when \"val='25'\";"
3567 "leaf val {"
3568 "type int64;"
3569 "}"
3570 "}"
3571 "container cont;"
3572 "}"
3573 , LYS_IN_YANG));
Michal Vaskof6e51882019-12-16 09:59:45 +01003574 logbuf_assert("When condition of \"val\" is accessing its own conditional node. /a:cont/val");
Michal Vasko5c4e5892019-11-14 12:31:38 +01003575
Michal Vaskoecd62de2019-11-13 12:35:11 +01003576 *state = NULL;
3577 ly_ctx_destroy(ctx, NULL);
3578}
3579
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003580int main(void)
3581{
3582 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01003583 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
3584 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
3585 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01003586 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
3587 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01003588 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01003589 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
3590 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01003591 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01003592 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01003593 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01003594 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01003595 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01003596 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
3597 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01003598 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01003599 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01003600 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003601 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01003602 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01003603 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01003604 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02003605 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcif2de0ed2019-05-02 14:13:18 +02003606 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01003607 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01003608 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01003609 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejciccd20f12019-02-15 14:12:27 +01003610 cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown),
Michal Vaskoecd62de2019-11-13 12:35:11 +01003611 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003612 };
3613
3614 return cmocka_run_group_tests(tests, NULL, NULL);
3615}