blob: 8954d0d86e6461f0147ca588deede5161f45ba7a [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010020#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010021#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema.c"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "../../src/plugins_types.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010025#include "../../src/context.c"
26#include "../../src/hash_table.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020027
Radek Krejcidd4e8d42018-10-16 14:55:43 +020028#include <stdarg.h>
29#include <stddef.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include <stdio.h>
34#include <string.h>
35
36#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020037
38#define BUFSIZE 1024
39char logbuf[BUFSIZE] = {0};
40
41/* set to 0 to printing error messages to stderr instead of checking them in code */
42#define ENABLE_LOGGER_CHECKING 1
43
44#if ENABLE_LOGGER_CHECKING
45static void
46logger(LY_LOG_LEVEL level, const char *msg, const char *path)
47{
48 (void) level; /* unused */
49
Radek Krejci87616bb2018-10-31 13:30:52 +010050 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020051 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
52 } else {
53 strncpy(logbuf, msg, BUFSIZE - 1);
54 }
55}
56#endif
57
58static int
59logger_setup(void **state)
60{
61 (void) state; /* unused */
62#if ENABLE_LOGGER_CHECKING
63 ly_set_log_clb(logger, 1);
64#endif
65 return 0;
66}
67
Radek Krejci4f28eda2018-11-12 11:46:16 +010068static int
69logger_teardown(void **state)
70{
71 (void) state; /* unused */
72#if ENABLE_LOGGER_CHECKING
73 if (*state) {
74 fprintf(stderr, "%s\n", logbuf);
75 }
76#endif
77 return 0;
78}
79
Radek Krejcidd4e8d42018-10-16 14:55:43 +020080void
81logbuf_clean(void)
82{
83 logbuf[0] = '\0';
84}
85
86#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010087# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020088#else
89# define logbuf_assert(str)
90#endif
91
Radek Krejcid05cbd92018-12-05 14:26:40 +010092static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
93 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
94 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
95{
96 *module_data = user_data;
97 *format = LYS_IN_YANG;
98 *free_module_data = NULL;
99 return LY_SUCCESS;
100}
101
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200102static void
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100103reset_mod(struct ly_ctx *ctx, struct lys_module *module)
104{
105 lysc_module_free(module->compiled, NULL);
106 lysp_module_free(module->parsed);
107
108 FREE_STRING(module->ctx, module->name);
109 FREE_STRING(module->ctx, module->ns);
110 FREE_STRING(module->ctx, module->prefix);
111 FREE_STRING(module->ctx, module->filepath);
112 FREE_STRING(module->ctx, module->org);
113 FREE_STRING(module->ctx, module->contact);
114 FREE_STRING(module->ctx, module->dsc);
115 FREE_STRING(module->ctx, module->ref);
Radek Krejci95710c92019-02-11 15:49:55 +0100116 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100117
118 memset(module, 0, sizeof *module);
119 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100120 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100121}
122
123static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200124test_module(void **state)
125{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100126 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200127
128 const char *str;
Radek Krejcie7b95092019-05-15 11:03:07 +0200129 struct lys_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200130 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200131 struct lysc_feature *f;
132 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200133
134 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200135 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100136 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100137 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200138
Radek Krejcif8f882a2018-10-31 14:51:15 +0100139 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
140 logbuf_assert("Invalid argument mod (lys_compile()).");
141 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
142 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100143 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100144 mod.implemented = 0;
145 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
146 assert_null(mod.compiled);
147 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100148 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200149 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100150 assert_string_equal("test", mod.name);
151 assert_string_equal("urn:test", mod.ns);
152 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200153 /* features */
154 assert_non_null(mod.compiled->features);
155 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200156 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200157 assert_non_null(f->iffeatures);
158 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200159 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200160 assert_non_null(iff->expr);
161 assert_non_null(iff->features);
162 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200163 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200164
Radek Krejci86d106e2018-10-18 09:53:19 +0200165 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200166
Radek Krejcif8f882a2018-10-31 14:51:15 +0100167 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200168 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200169
Radek Krejci86d106e2018-10-18 09:53:19 +0200170 lysc_module_free(mod.compiled, NULL);
171 mod.compiled = NULL;
172
Radek Krejci151a5b72018-10-19 14:21:44 +0200173 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200174 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100175 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
176 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
177 assert_null(mod.parsed);
178 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100179
180 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100181 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
182 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100183 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci8cce8532019-03-05 11:27:45 +0100184 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +0100185 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100186 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100187
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100188 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100189 ly_ctx_destroy(ctx.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
Radek Krejcie7b95092019-05-15 11:03:07 +0200197 struct lys_parser_ctx ctx = {0};
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
Radek Krejci313d9902018-11-08 09:42:58 +0100211 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100212 reset_mod(ctx.ctx, &mod);
213
214 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100215 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200216 assert_non_null(mod.compiled);
217 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100218 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200219 /* all features are disabled by default */
220 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
221 assert_int_equal(0, lysc_feature_value(f));
222 }
223 /* enable f1 */
224 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200225 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200226 assert_int_equal(1, lysc_feature_value(f1));
227
Radek Krejci87616bb2018-10-31 13:30:52 +0100228 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200229 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200230 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100231 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200232 assert_int_equal(1, lysc_feature_value(f));
233
Radek Krejci87616bb2018-10-31 13:30:52 +0100234 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200235 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200236 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100237 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
238 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200239 assert_int_equal(0, lysc_feature_value(f));
240
241 /* first enable f2, so f5 can be enabled then */
242 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100243 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200244 assert_int_equal(1, lysc_feature_value(f));
245
246 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200247 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200248 assert_int_equal(0, lysc_feature_value(f));
249 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
250 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
251 assert_int_equal(0, lysc_feature_value(f));
252
Radek Krejci87616bb2018-10-31 13:30:52 +0100253 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200254 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200255 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
256 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200257 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100258 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200259 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200260 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200261 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200262 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200263
264 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200265 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200266 /* long evaluation of f8 to need to reallocate internal stack for operators */
267 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200268
Radek Krejci87616bb2018-10-31 13:30:52 +0100269 /* double negation of disabled f1 -> disabled */
270 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
271
Radek Krejci38920282018-11-01 09:24:16 +0100272 /* disable all features */
273 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
274 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
275 assert_int_equal(0, lys_feature_value(&mod, f->name));
276 }
277 /* re-setting already set feature */
278 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
279 assert_int_equal(0, lys_feature_value(&mod, "f1"));
280
281 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100282 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
283 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
284 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100285 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
286 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100287 /* test if not changed */
288 assert_int_equal(1, lys_feature_value(&mod, "f1"));
289 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100290
Radek Krejci0af46292019-01-11 16:02:31 +0100291 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;"
292 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
293 assert_non_null(modp->compiled);
294 assert_non_null(modp->compiled->features);
295 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
296 assert_non_null(modp->compiled->features[0].iffeatures);
297 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
298 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
299 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
300 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
301 assert_non_null(modp->compiled->features);
302 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
303 assert_non_null(modp->compiled->features[1].depfeatures);
304 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
305 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
306
Radek Krejci1aefdf72018-11-01 11:01:39 +0100307 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100308 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
309 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
310
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100311 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100312
313 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100314 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature f1;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100315 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100316 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100317 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100318
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100319 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100320 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100321 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100322 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100323
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100324 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100325 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100326 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100327 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100328
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100329 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100330 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100331 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100332 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100333
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100334 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100335 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100336 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100337 reset_mod(ctx.ctx, &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 ---;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100340 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100341 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100342 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100343
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100344 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100345 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100346 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100347 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100348
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100349 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100350 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
351 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100352 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100353
Radek Krejci0af46292019-01-11 16:02:31 +0100354 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
355 assert_null(lys_parse_mem(ctx.ctx, "module z{namespace urn:z; prefix z; include sz;feature f1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100356 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
357
Radek Krejci09a1fc52019-02-13 10:55:17 +0100358 assert_null(lys_parse_mem(ctx.ctx, "module aa{namespace urn:aa; prefix aa; feature f1 {if-feature f2;} feature f2 {if-feature f1;}}", LYS_IN_YANG));
359 logbuf_assert("Feature \"f1\" is indirectly referenced from itself.");
360 assert_null(lys_parse_mem(ctx.ctx, "module ab{namespace urn:ab; prefix ab; feature f1 {if-feature f1;}}", LYS_IN_YANG));
361 logbuf_assert("Feature \"f1\" is referenced from itself.");
362
Radek Krejci1aefdf72018-11-01 11:01:39 +0100363 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100364 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100365 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100366 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module c{namespace urn:c; prefix c; import a {prefix a;} feature f1; feature f2{if-feature 'a:f1';}}", LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100367 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
368 assert_int_equal(0, lys_feature_value(modp, "f1"));
369 assert_int_equal(1, lys_feature_value(modp, "f2"));
370
Radek Krejcid05cbd92018-12-05 14:26:40 +0100371 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100372 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200373}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200374
Radek Krejci478020e2018-10-30 16:02:14 +0100375static void
376test_identity(void **state)
377{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100378 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100379
380 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100381 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100382
383 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100384 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
385 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
386 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
387 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100388
389 assert_non_null(mod1->compiled);
390 assert_non_null(mod1->compiled->identities);
391 assert_non_null(mod2->compiled);
392 assert_non_null(mod2->compiled->identities);
393
394 assert_non_null(mod1->compiled->identities[0].derived);
395 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
396 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
397 assert_non_null(mod2->compiled->identities[0].derived);
398 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
399 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
400 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
401 assert_non_null(mod2->compiled->identities[1].derived);
402 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
403 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
404 assert_non_null(mod2->compiled->identities[2].derived);
405 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
406 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
407
Radek Krejci4d483a82019-01-17 13:12:50 +0100408 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
409 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
410 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
411 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
412
413 assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100414 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
415
Radek Krejci4d483a82019-01-17 13:12:50 +0100416 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
417 assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100418 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
419
Radek Krejci38222632019-02-12 16:55:05 +0100420 assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG));
421 logbuf_assert("Unable to find base (i2) of identity \"i1\".");
422
423 assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG));
424 logbuf_assert("Identity \"i1\" is derived from itself.");
425 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));
426 logbuf_assert("Identity \"i1\" is indirectly derived from itself.");
427
Radek Krejci7f2a5362018-11-28 13:05:37 +0100428 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100429 ly_ctx_destroy(ctx, NULL);
430}
431
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100432static void
433test_node_container(void **state)
434{
435 (void) state; /* unused */
436
437 struct ly_ctx *ctx;
438 struct lys_module *mod;
439 struct lysc_node_container *cont;
440
441 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
442 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 +0100443 assert_non_null(mod->compiled);
444 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
445 assert_int_equal(LYS_CONTAINER, cont->nodetype);
446 assert_string_equal("c", cont->name);
447 assert_true(cont->flags & LYS_CONFIG_W);
448 assert_true(cont->flags & LYS_STATUS_CURR);
449
Radek Krejci98094b32018-11-02 16:21:47 +0100450 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 +0100451 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100452 assert_non_null(mod->compiled);
453 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
454 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100455 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100456 assert_non_null((cont = (struct lysc_node_container*)cont->child));
457 assert_int_equal(LYS_CONTAINER, cont->nodetype);
458 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100459 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100460 assert_string_equal("child", cont->name);
461
462 ly_ctx_destroy(ctx, NULL);
463}
464
Radek Krejci0e5d8382018-11-28 16:37:53 +0100465static void
466test_node_leaflist(void **state)
467{
468 *state = test_node_leaflist;
469
470 struct ly_ctx *ctx;
471 struct lys_module *mod;
472 struct lysc_type *type;
473 struct lysc_node_leaflist *ll;
474
475 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
476
477 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
478 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
479 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
480 "leaf-list ll2 {type leafref {path ../target;}}"
481 "leaf target {type int8;}}",
482 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100483 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
484 assert_non_null(type);
485 assert_int_equal(1, type->refcount);
486 assert_int_equal(LY_TYPE_UNION, type->basetype);
487 assert_non_null(((struct lysc_type_union*)type)->types);
488 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
489 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
490 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
491 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
492 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
493 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
494 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
495 assert_non_null(type);
496 assert_int_equal(1, type->refcount);
497 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
498 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
499 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
500
Radek Krejci6bb080b2018-11-28 17:23:41 +0100501 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 +0100502 assert_non_null(mod->compiled);
503 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
504 assert_int_equal(0, ll->min);
505 assert_int_equal((uint32_t)-1, ll->max);
506
Radek Krejci6bb080b2018-11-28 17:23:41 +0100507 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 +0100508 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100509 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100510 assert_non_null(mod->compiled);
511 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
512 assert_non_null(ll->dflts);
513 assert_int_equal(3, ll->type->refcount);
514 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
515 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100516 assert_string_equal("1", ll->dflts[1]);
Radek Krejci93dcc392019-02-19 10:43:38 +0100517 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 +0100518 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
519 assert_non_null(ll->dflts);
520 assert_int_equal(3, ll->type->refcount);
521 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
522 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100523 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
524
Radek Krejcifc11bd72019-04-11 16:00:05 +0200525 /* ordered-by is ignored for state data, RPC/action output parameters and notification content */
Radek Krejcia6d57732018-11-29 13:40:37 +0100526 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
527 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100528 /* but warning is present: */
Radek Krejcifc11bd72019-04-11 16:00:05 +0200529 logbuf_assert("The ordered-by statement is ignored in lists representing state data ().");
Radek Krejcia6d57732018-11-29 13:40:37 +0100530 assert_non_null(mod->compiled);
531 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
Radek Krejci93dcc392019-02-19 10:43:38 +0100532 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200533 logbuf_clean();
534
535 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;"
536 "rpc oper {output {leaf-list ll {type string; ordered-by user;}}}}", LYS_IN_YANG));
537 logbuf_assert("The ordered-by statement is ignored in lists representing RPC/action output parameters ().");
538 logbuf_clean();
539
540 assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f;"
541 "notification event {leaf-list ll {type string; ordered-by user;}}}", LYS_IN_YANG));
542 logbuf_assert("The ordered-by statement is ignored in lists representing notification content ().");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100543
544 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100545 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100546 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
547
Radek Krejci096235c2019-01-11 11:12:19 +0100548 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100549 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
550
551 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
552 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100553 assert_non_null(mod->compiled);
554 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
555 assert_non_null(ll->dflts);
556 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100557 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
558 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100559 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
560
Radek Krejci0e5d8382018-11-28 16:37:53 +0100561 *state = NULL;
562 ly_ctx_destroy(ctx, NULL);
563}
564
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100565static void
566test_node_list(void **state)
567{
568 *state = test_node_list;
569
570 struct ly_ctx *ctx;
571 struct lys_module *mod;
572 struct lysc_node_list *list;
573
574 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
575
576 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
577 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
578 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100579 list = (struct lysc_node_list*)mod->compiled->data;
580 assert_non_null(list);
581 assert_non_null(list->keys);
582 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
583 assert_string_equal("x", list->keys[0]->name);
584 assert_string_equal("y", list->keys[1]->name);
585 assert_non_null(list->child);
586 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
587 assert_true(list->child->flags & LYS_KEY);
588 assert_true(list->child->next->flags & LYS_KEY);
589 list = (struct lysc_node_list*)mod->compiled->data->next;
590 assert_non_null(list);
591 assert_null(list->keys);
592 assert_non_null(list->child);
Radek Krejci93dcc392019-02-19 10:43:38 +0100593 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, list->flags);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100594 assert_false(list->child->flags & LYS_KEY);
595
596 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
597 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100598 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100599 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100600 list = (struct lysc_node_list*)mod->compiled->data;
601 assert_non_null(list);
602 assert_string_equal("l", list->name);
603 assert_non_null(list->keys);
604 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
605 assert_string_equal("a", list->keys[0]->name);
606 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100607 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100608 assert_non_null(list->uniques);
609 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
610 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
611 assert_string_equal("a", list->uniques[0][0]->name);
612 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
613 assert_string_equal("b", list->uniques[0][1]->name);
614 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
615 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
616 assert_string_equal("e", list->uniques[1][0]->name);
617 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
618 assert_string_equal("d", list->uniques[1][1]->name);
619 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
620
Radek Krejci665421c2018-12-05 08:03:39 +0100621 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
622 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100623 list = (struct lysc_node_list*)mod->compiled->data;
624 assert_non_null(list);
625 assert_string_equal("l", list->name);
626 assert_non_null(list->keys);
627 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
628 assert_string_equal("a", list->keys[0]->name);
629 assert_true(list->keys[0]->flags & LYS_KEY);
630 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
631
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100632 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100633 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100634 logbuf_assert("Missing key in list representing configuration data.");
635
Radek Krejci096235c2019-01-11 11:12:19 +0100636 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
637 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100638 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
639
Radek Krejci096235c2019-01-11 11:12:19 +0100640 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
641 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100642 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
643
Radek Krejci096235c2019-01-11 11:12:19 +0100644 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
645 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100646 logbuf_assert("Key of the configuration list must not be status leaf.");
647
Radek Krejci096235c2019-01-11 11:12:19 +0100648 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
649 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100650 logbuf_assert("Configuration node cannot be child of any state data node.");
651
Radek Krejci096235c2019-01-11 11:12:19 +0100652 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
653 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100654 logbuf_assert("The list's key \"x\" not found.");
655
Radek Krejci096235c2019-01-11 11:12:19 +0100656 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
657 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100658 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100659
Radek Krejci096235c2019-01-11 11:12:19 +0100660 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
661 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100662 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
663
Radek Krejci096235c2019-01-11 11:12:19 +0100664 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
665 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100666 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
667
Radek Krejci096235c2019-01-11 11:12:19 +0100668 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
669 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100670 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
671
Radek Krejci096235c2019-01-11 11:12:19 +0100672 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
673 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100674 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
675
Radek Krejci096235c2019-01-11 11:12:19 +0100676 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
677 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100678 logbuf_assert("Duplicated key identifier \"x\".");
679
Radek Krejci096235c2019-01-11 11:12:19 +0100680 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
681 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100682 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
683
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100684 *state = NULL;
685 ly_ctx_destroy(ctx, NULL);
686}
687
Radek Krejci056d0a82018-12-06 16:57:25 +0100688static void
689test_node_choice(void **state)
690{
691 *state = test_node_choice;
692
693 struct ly_ctx *ctx;
694 struct lys_module *mod;
695 struct lysc_node_choice *ch;
696 struct lysc_node_case *cs;
697
698 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
699
700 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100701 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
702 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100703 ch = (struct lysc_node_choice*)mod->compiled->data;
704 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100705 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100706 cs = ch->cases;
707 assert_non_null(cs);
708 assert_string_equal("a", cs->name);
709 assert_ptr_equal(ch, cs->parent);
710 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100711 assert_string_equal("a1", cs->child->name);
712 assert_non_null(cs->child->next);
713 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100714 assert_ptr_equal(cs, cs->child->parent);
715 cs = (struct lysc_node_case*)cs->next;
716 assert_non_null(cs);
717 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100718 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100719 assert_ptr_equal(ch, cs->parent);
720 assert_non_null(cs->child);
721 assert_string_equal("b", cs->child->name);
722 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100723 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
724 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100725 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100726 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100727
Radek Krejci096235c2019-01-11 11:12:19 +0100728 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
729 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100730 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100731 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
732 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100733 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100734 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
735 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100736 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100737 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
738 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100739 logbuf_assert("Duplicate identifier \"b\" of case statement.");
740
Radek Krejci096235c2019-01-11 11:12:19 +0100741 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
742 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100743 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100744 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
745 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100746 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100747 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
748 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100749 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
750 /* TODO check with mandatory nodes from augment placed into the case */
751
Radek Krejci056d0a82018-12-06 16:57:25 +0100752 *state = NULL;
753 ly_ctx_destroy(ctx, NULL);
754}
755
Radek Krejci9800fb82018-12-13 14:26:23 +0100756static void
757test_node_anydata(void **state)
758{
759 *state = test_node_anydata;
760
761 struct ly_ctx *ctx;
762 struct lys_module *mod;
763 struct lysc_node_anydata *any;
764
765 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
766
767 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
768 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100769 any = (struct lysc_node_anydata*)mod->compiled->data;
770 assert_non_null(any);
771 assert_int_equal(LYS_ANYDATA, any->nodetype);
Radek Krejci93dcc392019-02-19 10:43:38 +0100772 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags);
Radek Krejci9800fb82018-12-13 14:26:23 +0100773
774 logbuf_clean();
775 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
776 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100777 any = (struct lysc_node_anydata*)mod->compiled->data;
778 assert_non_null(any);
779 assert_int_equal(LYS_ANYXML, any->nodetype);
780 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
781 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
782
783 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100784 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 +0100785 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
786
787 *state = NULL;
788 ly_ctx_destroy(ctx, NULL);
789}
790
Radek Krejcif538ce52019-03-05 10:46:14 +0100791static void
792test_action(void **state)
793{
794 *state = test_action;
795
796 struct ly_ctx *ctx;
797 struct lys_module *mod;
798 const struct lysc_action *rpc;
799
800 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
801
802 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
803 "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG));
804 rpc = mod->compiled->rpcs;
805 assert_non_null(rpc);
806 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
807 assert_int_equal(LYS_ACTION, rpc->nodetype);
808 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
809 assert_string_equal("a", rpc->name);
810
811 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
812 "action b {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}}", LYS_IN_YANG));
813 rpc = lysc_node_actions(mod->compiled->data);
814 assert_non_null(rpc);
815 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
816 assert_int_equal(LYS_ACTION, rpc->nodetype);
817 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
818 assert_string_equal("b", rpc->name);
819
820 /* invalid */
821 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG));
822 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
823
Radek Krejci8cce8532019-03-05 11:27:45 +0100824 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG));
825 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
826 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));
827 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
828 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));
829 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200830 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 +0100831 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG));
832 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
833
Radek Krejcifc11bd72019-04-11 16:00:05 +0200834 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;}}}}"
835 "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
836 logbuf_assert("Action \"invalid\" is placed inside another RPC/action.");
837
838 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;}}}"
839 "augment /test/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
840 logbuf_assert("Action \"invalid\" is placed inside Notification.");
841
842 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; notification test {container a {uses grp;}}"
843 "grouping grp {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
844 logbuf_assert("Action \"invalid\" is placed inside Notification.");
845
846 *state = NULL;
847 ly_ctx_destroy(ctx, NULL);
848}
849
850static void
851test_notification(void **state)
852{
853 *state = test_notification;
854
855 struct ly_ctx *ctx;
856 struct lys_module *mod;
857 const struct lysc_notif *notif;
858
859 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
860
861 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
862 "notification a1 {leaf x {type int8;}} notification a2;}", LYS_IN_YANG));
863 notif = mod->compiled->notifs;
864 assert_non_null(notif);
865 assert_int_equal(2, LY_ARRAY_SIZE(notif));
866 assert_int_equal(LYS_NOTIF, notif->nodetype);
867 assert_int_equal(LYS_STATUS_CURR, notif->flags);
868 assert_string_equal("a1", notif->name);
869 assert_non_null(notif->data);
870 assert_string_equal("x", notif->data->name);
871 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
872 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
873 assert_string_equal("a2", notif[1].name);
874 assert_null(notif[1].data);
875
876 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
877 "notification b1 {leaf x {type int8;}} notification b2;}}", LYS_IN_YANG));
878 notif = lysc_node_notifs(mod->compiled->data);
879 assert_non_null(notif);
880 assert_int_equal(2, LY_ARRAY_SIZE(notif));
881 assert_int_equal(LYS_NOTIF, notif->nodetype);
882 assert_int_equal(LYS_STATUS_CURR, notif->flags);
883 assert_string_equal("b1", notif->name);
884 assert_non_null(notif->data);
885 assert_string_equal("x", notif->data->name);
886 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
887 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
888 assert_string_equal("b2", notif[1].name);
889 assert_null(notif[1].data);
890
891 /* invalid */
892 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {notification x;}}", LYS_IN_YANG));
893 logbuf_assert("Invalid keyword \"notification\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
894
895 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} notification x;}", LYS_IN_YANG));
896 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
897 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));
898 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
899 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));
900 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
901 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}");
902 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; notification w;}", LYS_IN_YANG));
903 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
904
905 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;}}}}"
906 "augment /test/input/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
907 logbuf_assert("Notification \"invalid\" is placed inside RPC/action.");
908
909 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;}}}"
910 "augment /test/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
911 logbuf_assert("Notification \"invalid\" is placed inside another Notification.");
912
913 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; rpc test {input {container a {uses grp;}}}"
914 "grouping grp {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
915 logbuf_assert("Notification \"invalid\" is placed inside RPC/action.");
916
Radek Krejcif538ce52019-03-05 10:46:14 +0100917 *state = NULL;
918 ly_ctx_destroy(ctx, NULL);
919}
920
Radek Krejci0f07bed2018-11-13 14:01:40 +0100921/**
922 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
923 * do not test the expression itself
924 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100925static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100926test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100927{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100928 *state = test_type_range;
929
930 struct ly_ctx *ctx;
931 struct lys_module *mod;
932 struct lysc_type *type;
933
934 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
935
936 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 +0100937 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
938 assert_non_null(type);
939 assert_int_equal(LY_TYPE_INT8, type->basetype);
940 assert_non_null(((struct lysc_type_num*)type)->range);
941 assert_non_null(((struct lysc_type_num*)type)->range->parts);
942 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
943 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
944 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
945 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
946 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
947
948 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 +0100949 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
950 assert_non_null(type);
951 assert_int_equal(LY_TYPE_INT16, type->basetype);
952 assert_non_null(((struct lysc_type_num*)type)->range);
953 assert_non_null(((struct lysc_type_num*)type)->range->parts);
954 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
955 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
956 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
957 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
958 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
959
960 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 +0100961 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
962 assert_non_null(type);
963 assert_int_equal(LY_TYPE_INT32, type->basetype);
964 assert_non_null(((struct lysc_type_num*)type)->range);
965 assert_non_null(((struct lysc_type_num*)type)->range->parts);
966 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
967 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
968 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
969 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
970 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
971
972 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 +0100973 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
974 assert_non_null(type);
975 assert_int_equal(LY_TYPE_INT64, type->basetype);
976 assert_non_null(((struct lysc_type_num*)type)->range);
977 assert_non_null(((struct lysc_type_num*)type)->range->parts);
978 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
979 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
980 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
981 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
982 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
983
984 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 +0100985 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
986 assert_non_null(type);
987 assert_int_equal(LY_TYPE_UINT8, type->basetype);
988 assert_non_null(((struct lysc_type_num*)type)->range);
989 assert_non_null(((struct lysc_type_num*)type)->range->parts);
990 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
991 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
992 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
993 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
994 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
995
996 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 +0100997 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
998 assert_non_null(type);
999 assert_int_equal(LY_TYPE_UINT16, type->basetype);
1000 assert_non_null(((struct lysc_type_num*)type)->range);
1001 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1002 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1003 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1004 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1005 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1006 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1007
1008 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 +01001009 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1010 assert_non_null(type);
1011 assert_int_equal(LY_TYPE_UINT32, type->basetype);
1012 assert_non_null(((struct lysc_type_num*)type)->range);
1013 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1014 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1015 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1016 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1017 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1018 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1019
1020 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 +01001021 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1022 assert_non_null(type);
1023 assert_int_equal(LY_TYPE_UINT64, type->basetype);
1024 assert_non_null(((struct lysc_type_num*)type)->range);
1025 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1026 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1027 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1028 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1029 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1030 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1031
1032 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
1033 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +01001034 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1035 assert_non_null(type);
1036 assert_int_equal(3, type->refcount);
1037 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1038 assert_non_null(((struct lysc_type_num*)type)->range);
1039 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1040 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1041
Radek Krejcif1394042019-01-08 10:53:34 +01001042 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
1043 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
1044 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +01001045 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1046 assert_non_null(type);
1047 assert_int_equal(1, type->refcount);
1048 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1049 assert_non_null(((struct lysc_type_num*)type)->range);
1050 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
1051 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
1052 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1053 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1054 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1055 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1056
Radek Krejci0f07bed2018-11-13 14:01:40 +01001057 *state = NULL;
1058 ly_ctx_destroy(ctx, NULL);
1059}
1060
1061static void
1062test_type_length(void **state)
1063{
1064 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +01001065
1066 struct ly_ctx *ctx;
1067 struct lys_module *mod;
1068 struct lysc_type *type;
1069
1070 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1071
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001072 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 +01001073 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1074 assert_non_null(type);
1075 assert_non_null(((struct lysc_type_bin*)type)->length);
1076 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001077 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
1078 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001079 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1080 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1081 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1082
1083 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 +01001084 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1085 assert_non_null(type);
1086 assert_non_null(((struct lysc_type_bin*)type)->length);
1087 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1088 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +01001089 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1090 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001091
1092 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 +01001093 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1094 assert_non_null(type);
1095 assert_non_null(((struct lysc_type_bin*)type)->length);
1096 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1097 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1098 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +01001099 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001100
1101 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 +01001102 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1103 assert_non_null(type);
1104 assert_non_null(((struct lysc_type_bin*)type)->length);
1105 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1106 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1107 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1108 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1109
1110 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 +01001111 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1112 assert_non_null(type);
1113 assert_non_null(((struct lysc_type_bin*)type)->length);
1114 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1115 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1116 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1117 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1118
1119 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 +01001120 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1121 assert_non_null(type);
1122 assert_non_null(((struct lysc_type_bin*)type)->length);
1123 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1124 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1125 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1126 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1127 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1128 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1129
1130 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 +01001131 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1132 assert_non_null(type);
1133 assert_non_null(((struct lysc_type_bin*)type)->length);
1134 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1135 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1136 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1137 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1138 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1139 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1140
Radek Krejcib31c8992018-11-12 16:29:16 +01001141 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1142 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001143 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1144 assert_non_null(type);
1145 assert_non_null(((struct lysc_type_bin*)type)->length);
1146 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1147 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1148 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1149 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1150
1151 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1152 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001153 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1154 assert_non_null(type);
1155 assert_non_null(((struct lysc_type_bin*)type)->length);
1156 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1157 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1158 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1159 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1160
1161 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1162 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001163 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1164 assert_non_null(type);
1165 assert_non_null(((struct lysc_type_bin*)type)->length);
1166 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1167 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1168 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1169 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1170 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1171 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1172
1173 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 +01001174 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001175 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1176 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001177 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001178 assert_non_null(((struct lysc_type_bin*)type)->length);
1179 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1180 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1181 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001182 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001183 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1184 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001185 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001186 assert_non_null(((struct lysc_type_bin*)type)->length);
1187 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1188 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1189 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1190 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1191
Radek Krejci56aa27c2018-11-13 14:21:59 +01001192 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1193 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001194 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1195 assert_non_null(type);
1196 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001197 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001198 assert_non_null(((struct lysc_type_str*)type)->length);
1199 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1200 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1201 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1202 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001203
Radek Krejci5969f272018-11-23 10:03:58 +01001204 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1205 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001206 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1207 assert_non_null(type);
1208 assert_int_equal(LY_TYPE_STRING, type->basetype);
1209 assert_int_equal(1, type->refcount);
1210 assert_non_null(((struct lysc_type_str*)type)->length);
1211 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1212 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1213 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1214 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1215
Radek Krejci4f28eda2018-11-12 11:46:16 +01001216 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001217 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001218 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001219 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001220 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001221 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001222 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001223 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001224 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001225 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001226 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001227 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001228 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001229 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001230 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001231 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001232 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001233 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001234 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001235 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001236 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001237 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001238 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001239 assert_null(lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001240 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001241
Radek Krejci096235c2019-01-11 11:12:19 +01001242 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1243 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001244 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001245 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1246 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001247 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001248 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1249 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001250 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001251 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1252 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001253 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001254 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 +01001255 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001256 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001257 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 +01001258 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001259 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001260 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 +01001261 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001262 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001263
Radek Krejci096235c2019-01-11 11:12:19 +01001264 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:rr;prefix rr;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
Radek Krejci495903e2018-11-13 11:30:45 +01001265 logbuf_assert("Invalid type restrictions for binary type.");
1266
Radek Krejci4f28eda2018-11-12 11:46:16 +01001267 *state = NULL;
1268 ly_ctx_destroy(ctx, NULL);
1269}
1270
Radek Krejcicda74152018-11-13 15:27:32 +01001271static void
1272test_type_pattern(void **state)
1273{
1274 *state = test_type_pattern;
1275
1276 struct ly_ctx *ctx;
1277 struct lys_module *mod;
1278 struct lysc_type *type;
1279
1280 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1281
Radek Krejci027d5802018-11-14 16:57:28 +01001282 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 +01001283 "pattern .* {error-app-tag errortag;error-message error;}"
1284 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001285 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1286 assert_non_null(type);
1287 assert_non_null(((struct lysc_type_str*)type)->patterns);
1288 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1289 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1290 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001291 assert_string_equal(".*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001292 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1293 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1294 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001295 assert_string_equal("[0-9].*[0-9]", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001296 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1297
1298 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1299 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001300 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1301 assert_non_null(type);
1302 assert_int_equal(LY_TYPE_STRING, type->basetype);
1303 assert_int_equal(1, type->refcount);
1304 assert_non_null(((struct lysc_type_str*)type)->patterns);
1305 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001306 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001307 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
Radek Krejci54579462019-04-30 12:47:06 +02001308 assert_string_equal("[0-4]*", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001309 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1310
Radek Krejci04bc1e92018-11-14 14:28:13 +01001311 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1312 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001313 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1314 assert_non_null(type);
1315 assert_int_equal(LY_TYPE_STRING, type->basetype);
1316 assert_int_equal(1, type->refcount);
1317 assert_non_null(((struct lysc_type_str*)type)->patterns);
1318 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001319 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejci04bc1e92018-11-14 14:28:13 +01001320 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1321
Radek Krejcicda74152018-11-13 15:27:32 +01001322 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001323 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 +01001324 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001325 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1326 assert_non_null(type);
1327 assert_non_null(((struct lysc_type_str*)type)->patterns);
1328 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001329 assert_string_equal("^\\p{IsLatinExtended-A}$", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001330 /* TODO check some data "^Å™$" */
1331
1332 *state = NULL;
1333 ly_ctx_destroy(ctx, NULL);
1334}
1335
Radek Krejci8b764662018-11-14 14:15:13 +01001336static void
1337test_type_enum(void **state)
1338{
1339 *state = test_type_enum;
1340
1341 struct ly_ctx *ctx;
1342 struct lys_module *mod;
1343 struct lysc_type *type;
1344
1345 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1346
Radek Krejci027d5802018-11-14 16:57:28 +01001347 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 +01001348 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1349 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001350 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1351 assert_non_null(type);
1352 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1353 assert_non_null(((struct lysc_type_enum*)type)->enums);
1354 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1355 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1356 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1357 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1358 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1359 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1360 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1361 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1362 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1363 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1364 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1365 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1366 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1367 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1368
Radek Krejci027d5802018-11-14 16:57:28 +01001369 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 {"
1370 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001371 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1372 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001373 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1374 assert_non_null(type);
1375 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1376 assert_non_null(((struct lysc_type_enum*)type)->enums);
1377 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1378 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1379 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1380 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1381 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1382
1383
1384 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001385 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1386 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1387 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 +01001388 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1389 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1390 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1391 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1392 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1393 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1394 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1395 "enum one; enum one;}}}", LYS_IN_YANG));
1396 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1397 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1398 "enum '';}}}", LYS_IN_YANG));
1399 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1400 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1401 "enum ' x';}}}", LYS_IN_YANG));
1402 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1403 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1404 "enum 'x ';}}}", LYS_IN_YANG));
1405 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1406 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1407 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1408 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1409
Radek Krejci096235c2019-01-11 11:12:19 +01001410 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001411 logbuf_assert("Missing enum substatement for enumeration type.");
1412
Radek Krejci096235c2019-01-11 11:12:19 +01001413 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 +01001414 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001415 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1416
Radek Krejci096235c2019-01-11 11:12:19 +01001417 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 +01001418 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001419 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001420 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 +01001421 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001422 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1423
Radek Krejci096235c2019-01-11 11:12:19 +01001424 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 +01001425 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001426 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1427
Radek Krejci096235c2019-01-11 11:12:19 +01001428 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001429 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001430 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001431
Radek Krejci096235c2019-01-11 11:12:19 +01001432 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 +01001433 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001434 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1435
Radek Krejci8b764662018-11-14 14:15:13 +01001436 *state = NULL;
1437 ly_ctx_destroy(ctx, NULL);
1438}
1439
1440static void
1441test_type_bits(void **state)
1442{
1443 *state = test_type_bits;
1444
1445 struct ly_ctx *ctx;
1446 struct lys_module *mod;
1447 struct lysc_type *type;
1448
1449 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1450
Radek Krejci027d5802018-11-14 16:57:28 +01001451 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 +01001452 "bit automin; bit one {if-feature f; position 1;}"
1453 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001454 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1455 assert_non_null(type);
1456 assert_int_equal(LY_TYPE_BITS, type->basetype);
1457 assert_non_null(((struct lysc_type_bits*)type)->bits);
1458 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1459 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1460 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1461 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1462 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1463 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1464 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1465 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1466 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1467 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1468 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1469 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1470
Radek Krejci027d5802018-11-14 16:57:28 +01001471 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 {"
1472 "bit automin; bit one;bit two; bit seven {value 7;}bit eight;}} leaf l { type mytype {bit eight;bit seven;bit automin;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001473 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001474 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1475 assert_non_null(type);
1476 assert_int_equal(LY_TYPE_BITS, type->basetype);
1477 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001478 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1479 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1480 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1481 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1482 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1483 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1484 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001485
1486
1487 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001488 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1489 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1490 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 +01001491 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1492 "bit one {position -1;}}}}", LYS_IN_YANG));
1493 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1494 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1495 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1496 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1497 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1498 "bit one; bit one;}}}", LYS_IN_YANG));
1499 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1500 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1501 "bit '11';}}}", LYS_IN_YANG));
1502 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1503 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1504 "bit 'x1$1';}}}", LYS_IN_YANG));
1505 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1506
Radek Krejci096235c2019-01-11 11:12:19 +01001507 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001508 logbuf_assert("Missing bit substatement for bits type.");
1509
Radek Krejci096235c2019-01-11 11:12:19 +01001510 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 +01001511 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001512 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1513
Radek Krejci096235c2019-01-11 11:12:19 +01001514 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 +01001515 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001516 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001517 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001518 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001519 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1520
Radek Krejci096235c2019-01-11 11:12:19 +01001521 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {value 1;}bit y {value 1;}}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001522 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001523 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1524
Radek Krejci096235c2019-01-11 11:12:19 +01001525 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001526 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001527 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001528
Radek Krejci096235c2019-01-11 11:12:19 +01001529 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 +01001530 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001531 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1532
Radek Krejci8b764662018-11-14 14:15:13 +01001533 *state = NULL;
1534 ly_ctx_destroy(ctx, NULL);
1535}
1536
Radek Krejci6cba4292018-11-15 17:33:29 +01001537static void
1538test_type_dec64(void **state)
1539{
1540 *state = test_type_dec64;
1541
1542 struct ly_ctx *ctx;
1543 struct lys_module *mod;
1544 struct lysc_type *type;
1545
1546 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1547
1548 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1549 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001550 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1551 assert_non_null(type);
1552 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1553 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1554 assert_non_null(((struct lysc_type_dec*)type)->range);
1555 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1556 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1557 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1558 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1559
1560 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1561 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001562 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1563 assert_non_null(type);
1564 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1565 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1566 assert_non_null(((struct lysc_type_dec*)type)->range);
1567 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1568 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1569 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1570 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1571 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1572 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1573 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1574 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1575
1576 /* invalid cases */
1577 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1578 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1579 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1580 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1581 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1582 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1583
Radek Krejci096235c2019-01-11 11:12:19 +01001584 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001585 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1586
Radek Krejci096235c2019-01-11 11:12:19 +01001587 assert_null(lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001588 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001589
Radek Krejci096235c2019-01-11 11:12:19 +01001590 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 +01001591 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001592 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1593
Radek Krejci096235c2019-01-11 11:12:19 +01001594 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 +01001595 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001596 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1597
Radek Krejci096235c2019-01-11 11:12:19 +01001598 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 +01001599 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001600 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1601
Radek Krejci096235c2019-01-11 11:12:19 +01001602 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 +01001603 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001604 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1605
Radek Krejci6cba4292018-11-15 17:33:29 +01001606 *state = NULL;
1607 ly_ctx_destroy(ctx, NULL);
1608}
1609
Radek Krejci16c0f822018-11-16 10:46:10 +01001610static void
1611test_type_instanceid(void **state)
1612{
1613 *state = test_type_instanceid;
1614
1615 struct ly_ctx *ctx;
1616 struct lys_module *mod;
1617 struct lysc_type *type;
1618
1619 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1620
1621 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1622 "leaf l1 {type instance-identifier {require-instance true;}}"
1623 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001624 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1625 assert_non_null(type);
1626 assert_int_equal(LY_TYPE_INST, type->basetype);
1627 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1628
1629 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1630 assert_non_null(type);
1631 assert_int_equal(LY_TYPE_INST, type->basetype);
1632 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1633
1634 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1635 assert_non_null(type);
1636 assert_int_equal(LY_TYPE_INST, type->basetype);
1637 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1638
1639 /* invalid cases */
1640 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1641 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1642
Radek Krejci096235c2019-01-11 11:12:19 +01001643 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001644 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1645
1646 *state = NULL;
1647 ly_ctx_destroy(ctx, NULL);
1648}
1649
Radek Krejci555cb5b2018-11-16 14:54:33 +01001650static void
1651test_type_identityref(void **state)
1652{
1653 *state = test_type_identityref;
1654
1655 struct ly_ctx *ctx;
1656 struct lys_module *mod;
1657 struct lysc_type *type;
1658
1659 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1660
1661 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;}"
1662 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001663 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001664 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1665 assert_non_null(type);
1666 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1667 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1668 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1669 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1670
1671 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1672 assert_non_null(type);
1673 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1674 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1675 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1676 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1677 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1678
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001679 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1680 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001681 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1682 assert_non_null(type);
1683 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1684 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1685 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1686 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1687 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1688
Radek Krejci555cb5b2018-11-16 14:54:33 +01001689 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001690 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001691 logbuf_assert("Missing base substatement for identityref type.");
1692
Radek Krejci096235c2019-01-11 11:12:19 +01001693 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001694 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001695 logbuf_assert("Missing base substatement for identityref type mytype.");
1696
Radek Krejci096235c2019-01-11 11:12:19 +01001697 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 +01001698 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001699 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001700
Radek Krejci096235c2019-01-11 11:12:19 +01001701 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 +01001702 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001703 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001704
Radek Krejci096235c2019-01-11 11:12:19 +01001705 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001706 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001707 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1708
Radek Krejci096235c2019-01-11 11:12:19 +01001709 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
Radek Krejci322614f2018-11-16 15:05:44 +01001710 logbuf_assert("Unable to find base (j) of identityref.");
1711
Radek Krejci096235c2019-01-11 11:12:19 +01001712 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
Radek Krejci322614f2018-11-16 15:05:44 +01001713 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1714
Radek Krejci555cb5b2018-11-16 14:54:33 +01001715 *state = NULL;
1716 ly_ctx_destroy(ctx, NULL);
1717}
1718
Radek Krejcia3045382018-11-22 14:30:31 +01001719static void
1720test_type_leafref(void **state)
1721{
1722 *state = test_type_leafref;
1723
1724 struct ly_ctx *ctx;
1725 struct lys_module *mod;
1726 struct lysc_type *type;
1727 const char *path, *name, *prefix;
1728 size_t prefix_len, name_len;
1729 int parent_times, has_predicate;
1730
1731 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1732
1733 /* lys_path_token() */
1734 path = "invalid_path";
1735 parent_times = 0;
1736 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1737 path = "..";
1738 parent_times = 0;
1739 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1740 path = "..[";
1741 parent_times = 0;
1742 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1743 path = "../";
1744 parent_times = 0;
1745 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1746 path = "/";
1747 parent_times = 0;
1748 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1749
1750 path = "../../pref:id/xxx[predicate]/invalid!!!";
1751 parent_times = 0;
1752 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1753 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1754 assert_int_equal(4, prefix_len);
1755 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1756 assert_int_equal(2, name_len);
1757 assert_int_equal(0, strncmp("id", name, name_len));
1758 assert_int_equal(2, parent_times);
1759 assert_int_equal(0, has_predicate);
1760 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1761 assert_string_equal("[predicate]/invalid!!!", path);
1762 assert_int_equal(0, prefix_len);
1763 assert_null(prefix);
1764 assert_int_equal(3, name_len);
1765 assert_int_equal(0, strncmp("xxx", name, name_len));
1766 assert_int_equal(1, has_predicate);
1767 path += 11;
1768 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1769 assert_string_equal("!!!", path);
1770 assert_int_equal(0, prefix_len);
1771 assert_null(prefix);
1772 assert_int_equal(7, name_len);
1773 assert_int_equal(0, strncmp("invalid", name, name_len));
1774
1775 path = "/absolute/prefix:path";
1776 parent_times = 0;
1777 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1778 assert_string_equal("/prefix:path", path);
1779 assert_int_equal(0, prefix_len);
1780 assert_null(prefix);
1781 assert_int_equal(8, name_len);
1782 assert_int_equal(0, strncmp("absolute", name, name_len));
1783 assert_int_equal(-1, parent_times);
1784 assert_int_equal(0, has_predicate);
1785 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1786 assert_int_equal(0, *path);
1787 assert_int_equal(6, prefix_len);
1788 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1789 assert_int_equal(4, name_len);
1790 assert_int_equal(0, strncmp("path", name, name_len));
1791 assert_int_equal(0, has_predicate);
1792
1793 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001794 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 +01001795 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1796 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001797 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1798 assert_non_null(type);
1799 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1800 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001801 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001802 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1803 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001804 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1805 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1806 assert_non_null(type);
1807 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1808 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001809 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1810 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1811 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001812 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1813
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001814 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 +01001815 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001816 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001817 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1818 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001819 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001820 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1821 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1822 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001823 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1824 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001825 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1826
1827 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001828 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 +01001829 "typedef mytype3 {type c:mytype {require-instance false;}}"
1830 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001831 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1832 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001833 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001834 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1835 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1836 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001837 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1838 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001839 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1840 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1841 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001842 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001843 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1844 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1845 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1846 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1847
Radek Krejci4ce68932018-11-28 12:53:36 +01001848 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1849 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1850 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001851 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1852 assert_non_null(type);
1853 assert_int_equal(1, type->refcount);
1854 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1855 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1856 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1857 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1858 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1859 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1860
Radek Krejci58d171e2018-11-23 13:50:55 +01001861 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001862 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 +01001863 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1864 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001865 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1866 assert_non_null(type);
1867 assert_int_equal(1, type->refcount);
1868 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1869 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1870 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1871 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1872 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1873
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001874 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1875 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1876 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001877 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001878 LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001879 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001880 assert_non_null(type);
1881 assert_int_equal(1, type->refcount);
1882 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001883 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001884 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1885 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1886 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001887
Radek Krejci20e8a182018-12-07 11:43:21 +01001888 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1889 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1890 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1891 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001892 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1893 assert_non_null(type);
1894 assert_int_equal(1, type->refcount);
1895 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1896 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1897 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1898 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1899 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1900
Radek Krejci3d929562019-02-21 11:25:55 +01001901 /* leafref to imported (not yet implemented) module */
1902 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}");
1903 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}"
1904 "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG));
1905 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1906 assert_non_null(type);
1907 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1908 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1909 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1910 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h"));
1911 assert_int_equal(1, mod->implemented);
1912 assert_non_null(mod->compiled->data);
1913 assert_string_equal("h", mod->compiled->data->name);
1914
Radek Krejci92cc8512019-04-25 09:57:06 +02001915 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module j {namespace urn:j;prefix j; leaf j {type string;}}");
1916 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;import j {prefix j;}"
1917 "leaf i {type leafref {path \"/ilist[name = current()/../j:j]/value\";}}"
1918 "list ilist {key name; leaf name {type string;} leaf value {type uint16;}}}", LYS_IN_YANG));
1919 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1920 assert_non_null(type);
1921 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1922 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1923 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1924 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "j"));
1925 assert_int_equal(1, mod->implemented);
1926 assert_non_null(mod->compiled->data);
1927 assert_string_equal("j", mod->compiled->data->name);
1928
Radek Krejcia3045382018-11-22 14:30:31 +01001929 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001930 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 +01001931 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001932 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001933 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 +01001934 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001935 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001936 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 +01001937 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001938 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001939 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 +01001940 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001941 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001942 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 +01001943 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001944 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001945 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 +01001946 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001947 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001948 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 +01001949 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001950 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001951 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001952 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001953 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001954 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001955 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001956 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001957 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001958 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001959 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1960 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001961 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001962 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001963 logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001964
Radek Krejci096235c2019-01-11 11:12:19 +01001965 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001966 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001967 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001968 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 +01001969 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001970 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1971
Radek Krejci096235c2019-01-11 11:12:19 +01001972 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001973 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1974 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1975 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1976 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001977 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1978
Radek Krejci096235c2019-01-11 11:12:19 +01001979 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001980 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1981 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1982 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1983 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001984 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1985
Radek Krejci096235c2019-01-11 11:12:19 +01001986 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001987 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1988 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1989 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1990 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001991 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1992
Radek Krejci096235c2019-01-11 11:12:19 +01001993 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001994 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1995 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1996 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1997 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001998 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1999
Radek Krejci096235c2019-01-11 11:12:19 +01002000 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002001 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2002 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2003 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
2004 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002005 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
2006
Radek Krejci096235c2019-01-11 11:12:19 +01002007 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002008 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2009 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2010 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
2011 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002012 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
2013
Radek Krejci096235c2019-01-11 11:12:19 +01002014 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002015 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2016 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2017 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
2018 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002019 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
2020
Radek Krejci096235c2019-01-11 11:12:19 +01002021 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002022 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2023 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2024 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
2025 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002026 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
2027
Radek Krejci096235c2019-01-11 11:12:19 +01002028 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002029 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2030 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2031 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
2032 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002033 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
2034
Radek Krejci096235c2019-01-11 11:12:19 +01002035 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002036 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2037 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2038 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
2039 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002040 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
2041
Radek Krejci096235c2019-01-11 11:12:19 +01002042 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2044 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2045 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
2046 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002047 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
2048
Radek Krejci096235c2019-01-11 11:12:19 +01002049 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002050 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2051 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2052 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
2053 LYS_IN_YANG));
Radek Krejci92cc8512019-04-25 09:57:06 +02002054 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path-keyexpr.");
Radek Krejcibade82a2018-12-05 10:13:48 +01002055
Radek Krejci096235c2019-01-11 11:12:19 +01002056 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002057 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2058 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2059 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
2060 LYS_IN_YANG));
Radek Krejci92cc8512019-04-25 09:57:06 +02002061 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path-keyexpr.");
Radek Krejcibade82a2018-12-05 10:13:48 +01002062
Radek Krejci096235c2019-01-11 11:12:19 +01002063 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002064 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2065 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
2066 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
2067 LYS_IN_YANG));
Radek Krejci92cc8512019-04-25 09:57:06 +02002068 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path-keyexpr \"current()/../c\" refers container instead of leaf.");
Radek Krejcibade82a2018-12-05 10:13:48 +01002069
Radek Krejci096235c2019-01-11 11:12:19 +01002070 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002071 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
2072 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2073 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
2074 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002075 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
2076
Radek Krejci412ddfa2018-11-23 11:44:11 +01002077 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01002078 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01002079 "leaf ref1 {type leafref {path /ref2;}}"
2080 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01002081 "leaf ref3 {type leafref {path /ref4;}}"
2082 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01002083 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
2084
Radek Krejcia3045382018-11-22 14:30:31 +01002085 *state = NULL;
2086 ly_ctx_destroy(ctx, NULL);
2087}
2088
Radek Krejci43699232018-11-23 14:59:46 +01002089static void
2090test_type_empty(void **state)
2091{
2092 *state = test_type_empty;
2093
2094 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01002095
2096 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2097
2098 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002099 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01002100 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01002101 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
2102
Radek Krejci096235c2019-01-11 11:12:19 +01002103 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 +01002104 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01002105 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
2106
2107 *state = NULL;
2108 ly_ctx_destroy(ctx, NULL);
2109}
2110
Radek Krejcicdfecd92018-11-26 11:27:32 +01002111
2112static void
2113test_type_union(void **state)
2114{
2115 *state = test_type_union;
2116
2117 struct ly_ctx *ctx;
2118 struct lys_module *mod;
2119 struct lysc_type *type;
2120
2121 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2122
2123 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2124 "typedef mytype {type union {type int8; type mybasetype;}}"
2125 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002126 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2127 assert_non_null(type);
2128 assert_int_equal(2, type->refcount);
2129 assert_int_equal(LY_TYPE_UNION, type->basetype);
2130 assert_non_null(((struct lysc_type_union*)type)->types);
2131 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2132 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2133 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2134
2135 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2136 "typedef mytype {type union {type int8; type mybasetype;}}"
2137 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002138 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2139 assert_non_null(type);
2140 assert_int_equal(1, type->refcount);
2141 assert_int_equal(LY_TYPE_UNION, type->basetype);
2142 assert_non_null(((struct lysc_type_union*)type)->types);
2143 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2144 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2145 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2146 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2147
2148 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2149 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002150 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2151 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002152 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002153 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2154 assert_non_null(type);
2155 assert_int_equal(1, type->refcount);
2156 assert_int_equal(LY_TYPE_UNION, type->basetype);
2157 assert_non_null(((struct lysc_type_union*)type)->types);
2158 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2159 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2160 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2161 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2162 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2163 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2164
Radek Krejci6be5ff12018-11-26 13:18:15 +01002165 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01002166 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002167 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002168 logbuf_assert("Missing type substatement for union type mytype.");
2169
Radek Krejci096235c2019-01-11 11:12:19 +01002170 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2171 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002172
Radek Krejci096235c2019-01-11 11:12:19 +01002173 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 +01002174 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002175 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2176
Radek Krejci096235c2019-01-11 11:12:19 +01002177 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 +01002178 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002179 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2180
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002181 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;typedef mytype {type union{type mytype; type string;}}"
2182 "leaf l {type mytype;}}", LYS_IN_YANG));
2183 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected.");
2184 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;typedef mytype {type mytype2;}"
2185 "typedef mytype2 {type mytype;} leaf l {type mytype;}}", LYS_IN_YANG));
2186 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected.");
2187
Radek Krejcicdfecd92018-11-26 11:27:32 +01002188 *state = NULL;
2189 ly_ctx_destroy(ctx, NULL);
2190}
2191
2192static void
2193test_type_dflt(void **state)
2194{
2195 *state = test_type_union;
2196
2197 struct ly_ctx *ctx;
2198 struct lys_module *mod;
2199 struct lysc_type *type;
2200
2201 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2202
2203 /* default is not inherited from union's types */
2204 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2205 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002206 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2207 assert_non_null(type);
2208 assert_int_equal(1, type->refcount);
2209 assert_int_equal(LY_TYPE_UNION, type->basetype);
2210 assert_non_null(((struct lysc_type_union*)type)->types);
2211 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2212 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2213 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2214 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2215 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2216
2217 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2218 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002219 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2220 assert_non_null(type);
2221 assert_int_equal(2, type->refcount);
2222 assert_int_equal(LY_TYPE_STRING, type->basetype);
2223 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2224 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2225
2226 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2227 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002228 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2229 assert_non_null(type);
2230 assert_int_equal(2, type->refcount);
2231 assert_int_equal(LY_TYPE_STRING, type->basetype);
2232 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2233 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2234
Radek Krejci6be5ff12018-11-26 13:18:15 +01002235 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2236 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2237 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002238 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2239 assert_non_null(type);
2240 assert_int_equal(4, type->refcount);
2241 assert_int_equal(LY_TYPE_STRING, type->basetype);
2242 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2243 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2244 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2245 assert_non_null(type);
2246 assert_int_equal(4, type->refcount);
2247 assert_int_equal(LY_TYPE_STRING, type->basetype);
2248 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2249 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2250
2251 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2252 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002253 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2254 assert_non_null(type);
2255 assert_int_equal(3, type->refcount);
2256 assert_int_equal(LY_TYPE_STRING, type->basetype);
2257 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2258 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2259
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002260 /* mandatory leaf does not takes default value from type */
2261 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2262 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002263 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2264 assert_non_null(type);
2265 assert_int_equal(LY_TYPE_STRING, type->basetype);
2266 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2267 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2268
Radek Krejcicdfecd92018-11-26 11:27:32 +01002269 *state = NULL;
2270 ly_ctx_destroy(ctx, NULL);
2271}
2272
Radek Krejci665421c2018-12-05 08:03:39 +01002273static void
2274test_status(void **state)
2275{
2276 *state = test_status;
2277
2278 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002279
2280 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2281
Radek Krejci096235c2019-01-11 11:12:19 +01002282 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002283 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002284 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2285
Radek Krejci096235c2019-01-11 11:12:19 +01002286 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002287 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002288 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2289
Radek Krejci096235c2019-01-11 11:12:19 +01002290 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002291 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002292 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2293
2294 *state = NULL;
2295 ly_ctx_destroy(ctx, NULL);
2296}
2297
Radek Krejcie86bf772018-12-14 11:39:53 +01002298static void
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002299test_grouping(void **state)
2300{
2301 *state = test_grouping;
2302
2303 struct ly_ctx *ctx;
2304
2305 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2306
2307 /* result ok, but a warning about not used locally scoped grouping printed */
2308 assert_non_null(lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; grouping grp1 {leaf a1 {type string;}}"
2309 "container a {leaf x {type string;} grouping grp2 {leaf a2 {type string;}}}}", LYS_IN_YANG));
2310 logbuf_assert("Locally scoped grouping \"grp2\" not used.");
2311 logbuf_clean();
2312
2313 /* result ok - when statement or leafref target must be checked only at the place where the grouping is really instantiated */
2314 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; grouping grp {"
2315 "leaf ref {type leafref {path \"../name\";}}"
2316 "leaf cond {type string; when \"../name = 'specialone'\";}}}", LYS_IN_YANG));
2317 logbuf_assert("");
2318
2319
2320 /* invalid - error in a non-instantiated grouping */
2321 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2322 "grouping grp {leaf x {type leafref;}}}", LYS_IN_YANG));
2323 logbuf_assert("Missing path substatement for leafref type.");
2324 logbuf_clean();
2325 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2326 "container a {grouping grp {leaf x {type leafref;}}}}", LYS_IN_YANG));
2327 logbuf_assert("Missing path substatement for leafref type.");
2328
2329
2330 *state = NULL;
2331 ly_ctx_destroy(ctx, NULL);
2332}
2333
2334static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002335test_uses(void **state)
2336{
2337 *state = test_uses;
2338
2339 struct ly_ctx *ctx;
2340 struct lys_module *mod;
Radek Krejci3641f562019-02-13 15:38:40 +01002341 const struct lysc_node *parent, *child;
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002342 const struct lysc_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01002343
2344 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2345
Radek Krejci0af46292019-01-11 16:02:31 +01002346 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2347 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002348 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2349 "grouping grp_a_top {leaf a1 {type int8;}}"
2350 "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 +01002351 assert_non_null((parent = mod->compiled->data));
2352 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2353 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2354 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002355 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002356 assert_non_null((child = child->next));
2357 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002358 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002359 assert_non_null((child = child->next));
2360 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002361 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002362 assert_non_null((child = child->next));
2363 assert_string_equal("y", child->name);
2364 assert_non_null(child->iffeatures);
2365 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2366 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2367 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2368 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2369 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2370 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2371
2372 /* make the imported module implemented and enable the feature */
2373 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2374 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2375 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2376 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2377 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002378
Radek Krejci00b874b2019-02-12 10:54:50 +01002379 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;}}}");
2380 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 +01002381 assert_non_null(mod->compiled->data);
2382 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2383 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002384 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Radek Krejcib1b59152019-01-07 13:21:56 +01002385
Radek Krejci7f6a3812019-01-07 13:48:57 +01002386 logbuf_clean();
2387 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2388 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2389 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002390 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2391 assert_string_equal("l", mod->compiled->data->name);
2392 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2393 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2394 assert_string_equal("k", mod->compiled->data->next->name);
2395 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2396 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2397
Radek Krejci3641f562019-02-13 15:38:40 +01002398 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}"
2399 "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2400 assert_non_null(mod->compiled->data);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002401 assert_non_null(child = lysc_node_children(mod->compiled->data, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002402 assert_string_equal("g", child->name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002403 assert_non_null(child = lysc_node_children(child, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002404 assert_string_equal("x", child->name);
2405
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002406 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\";}}"
2407 "container top {action e; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2408 assert_non_null(mod->compiled->data);
2409 cont = (const struct lysc_node_container*)mod->compiled->data;
2410 assert_non_null(cont->actions);
2411 assert_int_equal(2, LY_ARRAY_SIZE(cont->actions));
2412 assert_string_equal("e", cont->actions[1].name);
2413 assert_string_equal("g", cont->actions[0].name);
2414 assert_string_equal("ultra g", cont->actions[0].dsc);
2415
2416 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\";}}"
2417 "container top {notification f; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2418 assert_non_null(mod->compiled->data);
2419 cont = (const struct lysc_node_container*)mod->compiled->data;
2420 assert_non_null(cont->notifs);
2421 assert_int_equal(2, LY_ARRAY_SIZE(cont->notifs));
2422 assert_string_equal("f", cont->notifs[1].name);
2423 assert_string_equal("g", cont->notifs[0].name);
2424 assert_string_equal("ultra g", cont->notifs[0].dsc);
2425
Radek Krejcie86bf772018-12-14 11:39:53 +01002426 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002427 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002428 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2429
Radek Krejci096235c2019-01-11 11:12:19 +01002430 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002431 "grouping grp {leaf a{type string;}uses grp1;}"
2432 "grouping grp1 {leaf b {type string;}uses grp2;}"
2433 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002434 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2435
Radek Krejci096235c2019-01-11 11:12:19 +01002436 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002437 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2438
Radek Krejci096235c2019-01-11 11:12:19 +01002439 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 +01002440 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002441 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +01002442
Radek Krejci096235c2019-01-11 11:12:19 +01002443 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 +01002444 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002445 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2446
Radek Krejci95710c92019-02-11 15:49:55 +01002447 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2448 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002449 logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002450 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2451 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002452 logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002453
Radek Krejci3641f562019-02-13 15:38:40 +01002454
2455 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}"
2456 "leaf g {type string;}"
2457 "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2458 logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used.");
2459
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002460 assert_non_null(mod = lys_parse_mem(ctx, "module hh {yang-version 1.1;namespace urn:hh;prefix hh;"
2461 "grouping grp {notification g { description \"super g\";}}"
2462 "container top {notification h; uses grp {refine h {description \"ultra h\";}}}}", LYS_IN_YANG));
2463 logbuf_assert("Invalid descendant-schema-nodeid value \"h\" - target node not found.");
2464
2465 assert_non_null(mod = lys_parse_mem(ctx, "module ii {yang-version 1.1;namespace urn:ii;prefix ii;"
2466 "grouping grp {action g { description \"super g\";}}"
2467 "container top {action i; uses grp {refine i {description \"ultra i\";}}}}", LYS_IN_YANG));
2468 logbuf_assert("Invalid descendant-schema-nodeid value \"i\" - target node not found.");
Radek Krejci3641f562019-02-13 15:38:40 +01002469
Radek Krejcie86bf772018-12-14 11:39:53 +01002470 *state = NULL;
2471 ly_ctx_destroy(ctx, NULL);
2472}
2473
Radek Krejci01342af2019-01-03 15:18:08 +01002474static void
2475test_refine(void **state)
2476{
2477 *state = test_refine;
2478
2479 struct ly_ctx *ctx;
2480 struct lys_module *mod;
2481 struct lysc_node *parent, *child;
2482
2483 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2484
Radek Krejci096235c2019-01-11 11:12:19 +01002485 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!;}"
2486 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2487 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2488 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2489 "leaf x {type mytype; mandatory true; must 1;}"
2490 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2491 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002492
Radek Krejcif0089082019-01-07 16:42:01 +01002493 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 +01002494 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002495 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002496 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002497 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2498 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002499 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002500 assert_non_null((parent = mod->compiled->data));
2501 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2502 assert_string_equal("c", parent->name);
2503 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2504 assert_int_equal(LYS_LEAF, child->nodetype);
2505 assert_string_equal("l", child->name);
2506 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2507 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2508 assert_non_null(child = child->next);
2509 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2510 assert_string_equal("ll", child->name);
2511 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2512 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2513 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002514 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2515 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002516 assert_non_null(child = child->next);
2517 assert_int_equal(LYS_CHOICE, child->nodetype);
2518 assert_string_equal("ch", child->name);
2519 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2520 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2521 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002522 assert_non_null(child->iffeatures);
2523 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002524 assert_non_null(child = child->next);
2525 assert_int_equal(LYS_LEAF, child->nodetype);
2526 assert_string_equal("x", child->name);
2527 assert_false(LYS_MAND_TRUE & child->flags);
2528 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002529 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2530 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002531 assert_string_equal("refined", child->dsc);
2532 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002533 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002534 assert_int_equal(LYS_ANYDATA, child->nodetype);
2535 assert_string_equal("a", child->name);
2536 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002537 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2538 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002539 assert_non_null(child->iffeatures);
2540 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002541 assert_string_equal("refined", child->dsc);
2542 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002543 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002544 assert_int_equal(LYS_CONTAINER, child->nodetype);
2545 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002546 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002547 assert_true(LYS_CONFIG_W & child->flags);
2548 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002549
2550 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 +01002551 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002552 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002553 assert_int_equal(LYS_LEAF, child->nodetype);
2554 assert_string_equal("x", child->name);
2555 assert_false(LYS_MAND_TRUE & child->flags);
2556 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2557
2558 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002559 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002560 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002561 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2562
Radek Krejci096235c2019-01-11 11:12:19 +01002563 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002564 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002565 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2566
Radek Krejci096235c2019-01-11 11:12:19 +01002567 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002568 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002569 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2570
Radek Krejci096235c2019-01-11 11:12:19 +01002571 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002572 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002573 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2574
Radek Krejci096235c2019-01-11 11:12:19 +01002575 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002576 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002577 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002578 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002579 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002580 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2581
Radek Krejci096235c2019-01-11 11:12:19 +01002582 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002583 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejcif1421c22019-02-19 13:05:20 +01002584 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" under the default case.");
Radek Krejci01342af2019-01-03 15:18:08 +01002585
Radek Krejci096235c2019-01-11 11:12:19 +01002586 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002587 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002588 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2589
Radek Krejci096235c2019-01-11 11:12:19 +01002590 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002591 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002592 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2593
Radek Krejci096235c2019-01-11 11:12:19 +01002594 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 +01002595 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002596 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2597
Radek Krejci096235c2019-01-11 11:12:19 +01002598 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002599 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002600 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2601
Radek Krejci096235c2019-01-11 11:12:19 +01002602 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002603 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002604 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2605
Radek Krejci096235c2019-01-11 11:12:19 +01002606 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002607 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002608 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2609
Radek Krejci096235c2019-01-11 11:12:19 +01002610 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002611 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002612 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2613
Radek Krejci01342af2019-01-03 15:18:08 +01002614 *state = NULL;
2615 ly_ctx_destroy(ctx, NULL);
2616}
Radek Krejcie86bf772018-12-14 11:39:53 +01002617
Radek Krejci95710c92019-02-11 15:49:55 +01002618static void
2619test_augment(void **state)
2620{
2621 *state = test_augment;
2622
2623 struct ly_ctx *ctx;
2624 struct lys_module *mod;
2625 const struct lysc_node *node;
2626 const struct lysc_node_choice *ch;
2627 const struct lysc_node_case *c;
Radek Krejcif538ce52019-03-05 10:46:14 +01002628 const struct lysc_action *rpc;
Radek Krejci95710c92019-02-11 15:49:55 +01002629
2630 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2631
2632 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2633 "container top {leaf a {type string;}}}");
2634 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2635 "leaf b {type a:atype;}}", LYS_IN_YANG));
2636 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2637 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2638 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2639 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2640 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2641 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2642 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2643 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2644 assert_non_null(node = mod->compiled->data);
2645 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002646 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002647 assert_string_equal(node->name, "a");
2648 assert_non_null(node = node->next);
2649 assert_string_equal(node->name, "c");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002650 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002651 assert_string_equal(node->name, "c");
2652 assert_non_null(node = node->next);
2653 assert_string_equal(node->name, "d");
2654 assert_non_null(node = node->next);
2655 assert_string_equal(node->name, "c");
2656
2657 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 +01002658 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2659 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002660 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2661 assert_null(mod->compiled->data->next);
2662 assert_string_equal("ch", ch->name);
2663 assert_non_null(c = ch->cases);
2664 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002665 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002666 assert_string_equal("a", c->child->name);
2667 assert_non_null(c = (const struct lysc_node_case*)c->next);
2668 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002669 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002670 assert_string_equal("b", c->child->name);
2671 assert_non_null(c = (const struct lysc_node_case*)c->next);
2672 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002673 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002674 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002675 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002676 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002677 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002678 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2679 assert_null(c->next);
2680
2681 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2682 "container c;"
2683 "augment /c {uses g;}}", LYS_IN_YANG)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002684 assert_non_null(node = lysc_node_children(mod->compiled->data, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002685 assert_string_equal(node->name, "a");
2686
Radek Krejci339f5292019-02-11 16:42:34 +01002687 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2688 "augment /c {container sub;}}");
2689 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2690 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2691 assert_non_null(mod->compiled->data);
2692 assert_string_equal("c", mod->compiled->data->name);
2693 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2694 assert_string_equal("sub", node->name);
2695 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2696 assert_string_equal("main", node->name);
2697
Radek Krejcif538ce52019-03-05 10:46:14 +01002698 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 +01002699 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;"
2700 "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2701 "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}"
2702 "augment /hi:top {leaf l {type string; mandatory true; config false;}}"
2703 "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG));
2704 assert_non_null(node = mod->compiled->data);
2705 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2706 assert_string_equal("l", node->name);
2707 assert_true(node->flags & LYS_MAND_TRUE);
2708 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
Radek Krejcife909632019-02-12 15:34:42 +01002709 assert_non_null(node = mod->compiled->data);
2710 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2711 assert_string_equal("p", node->name);
Radek Krejci733988a2019-02-15 15:12:44 +01002712 assert_non_null(node = node->next);
2713 assert_string_equal("ll", node->name);
2714 assert_non_null(node = node->next);
2715 assert_string_equal("l", node->name);
2716 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejcife909632019-02-12 15:34:42 +01002717
Radek Krejcif538ce52019-03-05 10:46:14 +01002718 assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}"
2719 "augment /hi:func/input {leaf x {type string;}}"
2720 "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG));
2721 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
2722 assert_non_null(rpc = mod->compiled->rpcs);
2723 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
2724 assert_non_null(rpc->input.data);
2725 assert_string_equal("x", rpc->input.data->name);
2726 assert_null(rpc->input.data->next);
2727 assert_non_null(rpc->output.data);
2728 assert_string_equal("y", rpc->output.data->name);
2729 assert_null(rpc->output.data->next);
2730
Radek Krejci95710c92019-02-11 15:49:55 +01002731 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2732 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
2733 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found.");
2734
2735 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2736 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002737 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002738
2739
Radek Krejci339f5292019-02-11 16:42:34 +01002740 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2741 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
2742 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target.");
2743
2744 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2745 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
2746 logbuf_assert("Invalid augment (/c) of container node which is not allowed to contain case node \"b\".");
2747
Radek Krejci733988a2019-02-15 15:12:44 +01002748 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}"
2749 "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
2750 logbuf_assert("Invalid augment (/hi:top) adding mandatory node \"c\" without making it conditional via when statement.");
Radek Krejcife909632019-02-12 15:34:42 +01002751
Radek Krejci3641f562019-02-13 15:38:40 +01002752 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;"
2753 "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG));
2754 logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\".");
Radek Krejci95710c92019-02-11 15:49:55 +01002755
Radek Krejcif538ce52019-03-05 10:46:14 +01002756 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;"
2757 "augment /func {leaf x {type int8;}}}", LYS_IN_YANG));
2758 logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC/action node which is not an allowed augment's target.");
2759
Radek Krejci95710c92019-02-11 15:49:55 +01002760 *state = NULL;
2761 ly_ctx_destroy(ctx, NULL);
2762}
2763
Radek Krejciccd20f12019-02-15 14:12:27 +01002764static void
2765test_deviation(void **state)
2766{
2767 *state = test_deviation;
2768
2769 struct ly_ctx *ctx;
2770 struct lys_module *mod;
2771 const struct lysc_node *node;
Radek Krejci7af64242019-02-18 13:07:53 +01002772 const struct lysc_node_list *list;
Radek Krejciccd20f12019-02-15 14:12:27 +01002773
2774 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2775
2776 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002777 "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2778 "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 +01002779 " case c {leaf c{type string;}}}"
2780 "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}"
2781 "rpc func2;}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002782 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 +01002783 "deviation /a:top/a:b {deviate not-supported;}"
2784 "deviation /a:ch/a:a/a:x {deviate not-supported;}"
2785 "deviation /a:ch/a:c/a:c {deviate not-supported;}"
2786 "deviation /a:ch/a:b {deviate not-supported;}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002787 "deviation /a:ch/a:a/a:a {deviate not-supported;}"
2788 "deviation /a:func1/a:input {deviate not-supported;}"
2789 "deviation /a:func1/a:output {deviate not-supported;}"
2790 "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002791 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2792 assert_non_null(node = mod->compiled->data);
2793 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002794 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejciccd20f12019-02-15 14:12:27 +01002795 assert_string_equal(node->name, "a");
2796 assert_non_null(node = node->next);
2797 assert_string_equal(node->name, "c");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002798 assert_null(node = node->next);
2799 assert_non_null(node = mod->compiled->data->next);
2800 assert_string_equal("ch", node->name);
2801 assert_null(((struct lysc_node_choice*)node)->dflt);
2802 assert_null(((struct lysc_node_choice*)node)->cases);
Radek Krejcif538ce52019-03-05 10:46:14 +01002803 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs));
2804 assert_null(mod->compiled->rpcs[0].input.data);
2805 assert_null(mod->compiled->rpcs[0].output.data);
Radek Krejciccd20f12019-02-15 14:12:27 +01002806
2807 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}"
2808 "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}"
2809 "deviation /c1 {deviate add {units meters;}}"
2810 "deviation /c2 {deviate delete {units meters;}}"
2811 "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG));
2812 assert_non_null(node = mod->compiled->data);
2813 assert_string_equal("c1", node->name);
2814 assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units);
2815 assert_non_null(node = node->next);
2816 assert_string_equal("c2", node->name);
2817 assert_null(((struct lysc_node_leaf*)node)->units);
2818 assert_non_null(node = node->next);
2819 assert_string_equal("c3", node->name);
2820 assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units);
2821
2822 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 +01002823 "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002824 "deviation /c1 {deviate add {must 3;}}"
2825 "deviation /c2 {deviate delete {must 2;}}"
2826 "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG));
2827 assert_non_null(node = mod->compiled->data);
2828 assert_string_equal("c1", node->name);
2829 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts));
2830 assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr);
2831 assert_non_null(node = node->next);
2832 assert_string_equal("c2", node->name);
Radek Krejcib4532d12019-02-15 16:13:29 +01002833 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts));
2834 assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr);
Radek Krejciccd20f12019-02-15 14:12:27 +01002835 assert_non_null(node = node->next);
2836 assert_string_equal("c3", node->name);
2837 assert_null(((struct lysc_node_leaf*)node)->musts);
2838
Radek Krejcib4532d12019-02-15 16:13:29 +01002839 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 +01002840 "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 +01002841 "choice b {default a;leaf a {type string;} leaf b {type string;}}"
2842 "leaf c {default hello; type string;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002843 "leaf-list d {default hello; default world; type string;}"
2844 "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002845 assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}"
2846 "deviation /x:a {deviate delete {default a;}}"
2847 "deviation /x:b {deviate delete {default x:a;}}"
2848 "deviation /x:c {deviate delete {default hello;}}"
2849 "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG));
2850 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2851 assert_non_null(node = mod->compiled->data);
2852 assert_null(((struct lysc_node_choice*)node)->dflt);
2853 assert_non_null(node = node->next);
2854 assert_null(((struct lysc_node_choice*)node)->dflt);
2855 assert_non_null(node = node->next);
2856 assert_null(((struct lysc_node_leaf*)node)->dflt);
2857 assert_non_null(node = node->next);
2858 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2859 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002860 assert_non_null(node = node->next);
2861 assert_string_equal("nothing", ((struct lysc_node_leaf*)node)->dflt);
2862 assert_non_null(node = node->next);
2863 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2864 assert_string_equal("nothing", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002865
2866 assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}"
2867 "deviation /x:b {deviate add {default x:b;}}"
2868 "deviation /x:c {deviate add {default bye;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002869 "deviation /x:d {deviate add {default all; default people;}}"
2870 "deviation /x:c2 {deviate add {default hi;}}"
2871 "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002872 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2873 assert_non_null(node = mod->compiled->data);
2874 assert_null(((struct lysc_node_choice*)node)->dflt);
2875 assert_non_null(node = node->next);
2876 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2877 assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name);
2878 assert_non_null(node = node->next);
2879 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2880 assert_string_equal("bye", ((struct lysc_node_leaf*)node)->dflt);
2881 assert_non_null(node = node->next);
2882 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2883 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
2884 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
2885 assert_string_equal("people", ((struct lysc_node_leaflist*)node)->dflts[2]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002886 assert_non_null(node = node->next);
2887 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2888 assert_string_equal("hi", ((struct lysc_node_leaf*)node)->dflt);
2889 assert_non_null(node = node->next);
2890 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2891 assert_string_equal("hi", ((struct lysc_node_leaflist*)node)->dflts[0]);
2892 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002893
2894 assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}"
2895 "deviation /x:b {deviate replace {default x:a;}}"
2896 "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG));
2897 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2898 assert_non_null(node = mod->compiled->data);
2899 assert_null(((struct lysc_node_choice*)node)->dflt);
2900 assert_non_null(node = node->next);
2901 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2902 assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name);
2903 assert_non_null(node = node->next);
2904 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2905 assert_string_equal("hello", ((struct lysc_node_leaf*)node)->dflt);
2906
Radek Krejci7af64242019-02-18 13:07:53 +01002907 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;"
2908 "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2909 "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}"
2910 " leaf c {type string;} leaf d {type string;}}}");
2911 assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}"
2912 "augment /i:l1 {leaf j_c {type string;}}"
2913 "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}"
2914 "deviation /i:l1 {deviate add {unique \"i:c\";}}"
2915 "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG));
2916 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i")));
2917 assert_non_null(list = (struct lysc_node_list*)mod->compiled->data);
2918 assert_string_equal("l1", list->name);
2919 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
2920 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
2921 assert_string_equal("b", list->uniques[0][0]->name);
2922 assert_string_equal("j_c", list->uniques[0][1]->name);
2923 assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1]));
2924 assert_string_equal("c", list->uniques[1][0]->name);
2925 assert_non_null(list = (struct lysc_node_list*)list->next);
2926 assert_string_equal("l2", list->name);
2927 assert_null(list->uniques);
2928
Radek Krejci93dcc392019-02-19 10:43:38 +01002929 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}"
2930 "container top {leaf x {type string;} leaf y {type string; config false;}}"
2931 "deviation /a {deviate add {config false; }}"
2932 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
2933 assert_non_null(node = mod->compiled->data);
2934 assert_string_equal("a", node->name);
2935 assert_true(node->flags & LYS_CONFIG_R);
2936 assert_non_null(node = node->next);
2937 assert_string_equal("top", node->name);
2938 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002939 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002940 assert_string_equal("x", node->name);
2941 assert_true(node->flags & LYS_CONFIG_R);
2942 assert_non_null(node = node->next);
2943 assert_string_equal("y", node->name);
2944 assert_true(node->flags & LYS_CONFIG_R);
2945
2946 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}"
2947 "container top {config false; leaf x {type string;}}"
2948 "deviation /a {deviate replace {config true;}}"
2949 "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG));
2950 assert_non_null(node = mod->compiled->data);
2951 assert_string_equal("a", node->name);
2952 assert_true(node->flags & LYS_CONFIG_W);
2953 assert_non_null(node = node->next);
2954 assert_string_equal("top", node->name);
2955 assert_true(node->flags & LYS_CONFIG_W);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002956 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002957 assert_string_equal("x", node->name);
2958 assert_true(node->flags & LYS_CONFIG_W);
2959
Radek Krejcif1421c22019-02-19 13:05:20 +01002960 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;"
2961 "container a {leaf a {type string;}}"
2962 "container b {leaf b {mandatory true; type string;}}"
2963 "deviation /a/a {deviate add {mandatory true;}}"
2964 "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG));
2965 assert_non_null(node = mod->compiled->data);
2966 assert_string_equal("a", node->name);
2967 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002968 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002969 assert_non_null(node = node->next);
2970 assert_string_equal("b", node->name);
2971 assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002972 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002973
Radek Krejci551b12c2019-02-19 16:11:21 +01002974 assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;"
2975 "leaf a {default test; type string;}"
2976 "leaf b {mandatory true; type string;}"
2977 "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}"
2978 "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG));
2979 assert_non_null(node = mod->compiled->data);
2980 assert_string_equal("a", node->name);
2981 assert_null(((struct lysc_node_leaf*)node)->dflt);
2982 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
2983 assert_non_null(node = node->next);
2984 assert_string_equal("b", node->name);
2985 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2986 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
2987
2988 assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;"
2989 "leaf-list a {type string;}"
2990 "list b {config false;}"
2991 "leaf-list c {min-elements 1; max-elements 10; type string;}"
2992 "list d {min-elements 10; max-elements 100; config false;}"
2993 "deviation /a {deviate add {min-elements 1; max-elements 10;}}"
2994 "deviation /b {deviate add {min-elements 10; max-elements 100;}}"
2995 "deviation /c {deviate replace {min-elements 10; max-elements 100;}}"
2996 "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG));
2997 assert_non_null(node = mod->compiled->data);
2998 assert_string_equal("a", node->name);
2999 assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min);
3000 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max);
3001 assert_non_null(node = node->next);
3002 assert_string_equal("b", node->name);
3003 assert_int_equal(10, ((struct lysc_node_list*)node)->min);
3004 assert_int_equal(100, ((struct lysc_node_list*)node)->max);
3005 assert_non_null(node = node->next);
3006 assert_string_equal("c", node->name);
3007 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min);
3008 assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max);
3009 assert_non_null(node = node->next);
3010 assert_string_equal("d", node->name);
3011 assert_int_equal(1, ((struct lysc_node_list*)node)->min);
3012 assert_int_equal(10, ((struct lysc_node_list*)node)->max);
3013
Radek Krejci33f72892019-02-21 10:36:58 +01003014 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;}"
3015 "leaf a {type string; default 10;} leaf-list b {type string;}"
3016 "deviation /a {deviate replace {type mytype;}}"
3017 "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG));
3018 assert_non_null(node = mod->compiled->data);
3019 assert_string_equal("a", node->name);
3020 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf*)node)->type->basetype);
3021 assert_string_equal("10", ((struct lysc_node_leaf*)node)->dflt);
3022 assert_non_null(node = node->next);
3023 assert_string_equal("b", node->name);
3024 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaflist*)node)->type->basetype);
3025 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
3026 assert_string_equal("1", ((struct lysc_node_leaflist*)node)->dflts[0]);
3027
Radek Krejci88ef64b2019-02-18 16:02:06 +01003028 assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003029 "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG));
3030 logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003031 assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}"
3032 "deviation /a:top/a:a {deviate not-supported;}"
3033 "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG));
3034 /* warning */
3035 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 +01003036
3037 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}"
3038 "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG));
3039 logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\".");
3040
3041 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;"
3042 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
3043 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to add \"units\" property.");
3044 assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}"
3045 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
3046 logbuf_assert("Invalid deviation (/c) adding \"units\" property which already exists (with value \"centimeters\").");
3047
3048 assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;"
3049 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
3050 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to delete \"units\" property.");
3051 assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}"
3052 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
3053 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which is not present.");
3054 assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}"
3055 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
3056 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\".");
3057
3058 assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;"
3059 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
3060 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to replace \"units\" property.");
3061 assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}"
3062 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
3063 logbuf_assert("Invalid deviation (/c) replacing \"units\" property \"meters\" which is not present.");
3064
3065 /* the default is already deleted in /e:a byt module f */
3066 assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}"
3067 "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG));
3068 logbuf_assert("Invalid deviation (/e:a) deleting \"default\" property \"x:a\" which is not present.");
3069 assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}"
3070 "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG));
3071 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"x:a\" of choice. "
3072 "The prefix does not match any imported module of the deviation module.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003073 assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}"
3074 "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG));
3075 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\".");
3076 assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}"
3077 "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG));
3078 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module.");
3079 assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;"
3080 "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG));
3081 logbuf_assert("Invalid deviation (/a) of anyxml node - it is not possible to delete \"default\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003082
3083 assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}"
3084 "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG));
3085 logbuf_assert("Invalid deviation (/e:b) adding \"default\" property which already exists (with value \"a\").");
3086 assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01003087 "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG));
3088 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"x:a\" of choice. "
3089 "The prefix does not match any imported module of the deviation module.");
3090 assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}"
3091 "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG));
3092 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"a\" of choice - the specified case does not exists.");
3093 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003094 "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG));
3095 logbuf_assert("Invalid deviation (/e:c) adding \"default\" property which already exists (with value \"hello\").");
Radek Krejci468a94f2019-02-15 14:52:36 +01003096 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
3097 "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG));
3098 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"e:c\" of choice - mandatory node \"c\" under the default case.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003099 assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}"
3100 "deviation /x {deviate add {default error;}}}", LYS_IN_YANG));
Radek Krejci551b12c2019-02-19 16:11:21 +01003101 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003102
3103 assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}"
3104 "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG));
3105 logbuf_assert("Invalid deviation (/e:d) of leaf-list node - it is not possible to replace \"default\" property.");
3106
Radek Krejci7af64242019-02-18 13:07:53 +01003107 assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}"
3108 "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG));
3109 logbuf_assert("Invalid deviation (/i:l1) deleting \"unique\" property \"x\" which does not match any of the target's property values.");
3110 assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}"
3111 "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG));
3112 logbuf_assert("Invalid deviation (/i:l2) deleting \"unique\" property \"d\" which does not match any of the target's property values.");
3113 assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}"
3114 "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG));
3115 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to delete \"unique\" property.");
3116 assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}"
3117 "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG));
3118 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to add \"unique\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003119
Radek Krejci93dcc392019-02-19 10:43:38 +01003120 assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}"
3121 "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG));
3122 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to add \"config\" property.");
3123 assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}"
3124 "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG));
3125 logbuf_assert("Invalid deviation of config in \"/top/x\" - configuration node cannot be child of any state data node.");
3126 assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}"
3127 "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG));
3128 logbuf_assert("Invalid deviation (/top/x) replacing \"config\" property \"config false\" which is not present.");
3129 assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}"
3130 "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG));
3131 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to replace \"config\" property.");
3132 assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}"
3133 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
3134 logbuf_assert("Invalid deviation of config in \"/top\" - configuration node cannot be child of any state data node.");
Radek Krejcif1421c22019-02-19 13:05:20 +01003135 assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}"
3136 "deviation /x {deviate add {config true;}}}", LYS_IN_YANG));
3137 logbuf_assert("Invalid deviation (/x) adding \"config\" property which already exists (with value \"config false\").");
3138
3139 assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}"
3140 "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG));
3141 logbuf_assert("Invalid deviation of mandatory in \"/top\" - container cannot hold mandatory statement.");
3142 assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}"
3143 "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG));
3144 logbuf_assert("Invalid deviation (/top) replacing \"mandatory\" property \"mandatory true\" which is not present.");
3145 assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}"
3146 "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG));
3147 logbuf_assert("Invalid deviation (/top/x) replacing \"mandatory\" property \"mandatory true\" which is not present.");
3148 assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}"
3149 "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG));
3150 logbuf_assert("Invalid deviation (/x) adding \"mandatory\" property which already exists (with value \"mandatory true\").");
Radek Krejci93dcc392019-02-19 10:43:38 +01003151
Radek Krejci551b12c2019-02-19 16:11:21 +01003152 assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}"
3153 "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG));
3154 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
3155 assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}"
3156 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3157 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf-list.");
3158 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;}}"
3159 "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG));
3160 logbuf_assert("Invalid deviation (/ch) combining default case and mandatory choice.");
3161
3162 assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}"
3163 "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG));
3164 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 10 is bigger than max value 5.");
3165 assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}"
3166 "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG));
3167 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
3168 assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}"
3169 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3170 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 5 is bigger than max value 1.");
3171 assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}"
3172 "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG));
3173 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
3174 assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}"
3175 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3176 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
3177 assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}"
3178 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3179 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
3180 assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}"
3181 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
3182 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3183 assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}"
3184 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
3185 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3186 assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}"
3187 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3188 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3189 assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}"
3190 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3191 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3192 assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}"
3193 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3194 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3195 assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }"
3196 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3197 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3198
Radek Krejci33f72892019-02-21 10:36:58 +01003199 assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;"
3200 "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG));
3201 logbuf_assert("Invalid deviation (/x) of anyxml node - it is not possible to replace \"type\" property.");
3202 assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}"
3203 "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG));
3204 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3205
Radek Krejciccd20f12019-02-15 14:12:27 +01003206 *state = NULL;
3207 ly_ctx_destroy(ctx, NULL);
3208}
3209
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003210int main(void)
3211{
3212 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01003213 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
3214 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
3215 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01003216 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
3217 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01003218 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01003219 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
3220 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01003221 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01003222 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01003223 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01003224 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01003225 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01003226 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
3227 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01003228 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01003229 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01003230 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003231 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01003232 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01003233 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01003234 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02003235 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcif2de0ed2019-05-02 14:13:18 +02003236 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01003237 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01003238 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01003239 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejciccd20f12019-02-15 14:12:27 +01003240 cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003241 };
3242
3243 return cmocka_run_group_tests(tests, NULL, NULL);
3244}