blob: 3cff5248b385c7b91ec4d32518ee9ecbfe93e948 [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcidd4e8d42018-10-16 14:55:43 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/common.h"
24#include "../../src/tree_schema_internal.h"
25#include "../../src/xpath.h"
26
27void lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat);
28
29LY_ERR lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
30 int *parent_times, int *has_predicate);
Radek Krejcidd4e8d42018-10-16 14:55:43 +020031
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34
35/* set to 0 to printing error messages to stderr instead of checking them in code */
36#define ENABLE_LOGGER_CHECKING 1
37
38#if ENABLE_LOGGER_CHECKING
39static void
40logger(LY_LOG_LEVEL level, const char *msg, const char *path)
41{
42 (void) level; /* unused */
43
Radek Krejci87616bb2018-10-31 13:30:52 +010044 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020045 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
46 } else {
47 strncpy(logbuf, msg, BUFSIZE - 1);
48 }
49}
50#endif
51
52static int
53logger_setup(void **state)
54{
55 (void) state; /* unused */
56#if ENABLE_LOGGER_CHECKING
57 ly_set_log_clb(logger, 1);
58#endif
59 return 0;
60}
61
Radek Krejci4f28eda2018-11-12 11:46:16 +010062static int
63logger_teardown(void **state)
64{
65 (void) state; /* unused */
66#if ENABLE_LOGGER_CHECKING
67 if (*state) {
68 fprintf(stderr, "%s\n", logbuf);
69 }
70#endif
71 return 0;
72}
73
Radek Krejcidd4e8d42018-10-16 14:55:43 +020074void
75logbuf_clean(void)
76{
77 logbuf[0] = '\0';
78}
79
80#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010081# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020082#else
83# define logbuf_assert(str)
84#endif
85
Radek Krejcid05cbd92018-12-05 14:26:40 +010086static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
87 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
88 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
89{
90 *module_data = user_data;
91 *format = LYS_IN_YANG;
92 *free_module_data = NULL;
93 return LY_SUCCESS;
94}
95
Radek Krejcidd4e8d42018-10-16 14:55:43 +020096static void
Radek Krejci0bcdaed2019-01-10 10:21:34 +010097reset_mod(struct ly_ctx *ctx, struct lys_module *module)
98{
99 lysc_module_free(module->compiled, NULL);
100 lysp_module_free(module->parsed);
101
102 FREE_STRING(module->ctx, module->name);
103 FREE_STRING(module->ctx, module->ns);
104 FREE_STRING(module->ctx, module->prefix);
105 FREE_STRING(module->ctx, module->filepath);
106 FREE_STRING(module->ctx, module->org);
107 FREE_STRING(module->ctx, module->contact);
108 FREE_STRING(module->ctx, module->dsc);
109 FREE_STRING(module->ctx, module->ref);
Radek Krejci95710c92019-02-11 15:49:55 +0100110 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100111
112 memset(module, 0, sizeof *module);
113 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100114 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100115}
116
117static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200118test_module(void **state)
119{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100120 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200121
122 const char *str;
Radek Krejcie7b95092019-05-15 11:03:07 +0200123 struct lys_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200124 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200125 struct lysc_feature *f;
126 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200127
128 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200129 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100130 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100131 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200132
Radek Krejcif8f882a2018-10-31 14:51:15 +0100133 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
134 logbuf_assert("Invalid argument mod (lys_compile()).");
135 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
136 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100137 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100138 mod.implemented = 0;
139 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
140 assert_null(mod.compiled);
141 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100142 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200143 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100144 assert_string_equal("test", mod.name);
145 assert_string_equal("urn:test", mod.ns);
146 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200147 /* features */
148 assert_non_null(mod.compiled->features);
149 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200150 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200151 assert_non_null(f->iffeatures);
152 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200153 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200154 assert_non_null(iff->expr);
155 assert_non_null(iff->features);
156 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200157 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200158
Radek Krejci86d106e2018-10-18 09:53:19 +0200159 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200160
Radek Krejcif8f882a2018-10-31 14:51:15 +0100161 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200162 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200163
Radek Krejci86d106e2018-10-18 09:53:19 +0200164 lysc_module_free(mod.compiled, NULL);
165 mod.compiled = NULL;
166
Radek Krejci151a5b72018-10-19 14:21:44 +0200167 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200168 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100169 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
170 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
171 assert_null(mod.parsed);
172 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100173
174 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100175 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
176 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100177 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci8cce8532019-03-05 11:27:45 +0100178 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +0100179 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100180 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100181
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100182 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100183 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200184}
185
Radek Krejci151a5b72018-10-19 14:21:44 +0200186static void
187test_feature(void **state)
188{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100189 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200190
Radek Krejcie7b95092019-05-15 11:03:07 +0200191 struct lys_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100192 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200193 const char *str;
194 struct lysc_feature *f, *f1;
195
196 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
197 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100198 "feature orfeature {if-feature \"f1 or f2\";}\n"
199 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200200 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200201 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100202 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
203 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200204
Radek Krejci313d9902018-11-08 09:42:58 +0100205 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100206 reset_mod(ctx.ctx, &mod);
207
208 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100209 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200210 assert_non_null(mod.compiled);
211 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100212 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200213 /* all features are disabled by default */
214 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
215 assert_int_equal(0, lysc_feature_value(f));
216 }
217 /* enable f1 */
218 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200219 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200220 assert_int_equal(1, lysc_feature_value(f1));
221
Radek Krejci87616bb2018-10-31 13:30:52 +0100222 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200223 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200224 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100225 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200226 assert_int_equal(1, lysc_feature_value(f));
227
Radek Krejci87616bb2018-10-31 13:30:52 +0100228 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200229 f = &mod.compiled->features[4];
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_EDENIED, lys_feature_enable(&mod, "andfeature"));
232 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200233 assert_int_equal(0, lysc_feature_value(f));
234
235 /* first enable f2, so f5 can be enabled then */
236 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100237 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200238 assert_int_equal(1, lysc_feature_value(f));
239
240 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200241 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200242 assert_int_equal(0, lysc_feature_value(f));
243 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
244 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
245 assert_int_equal(0, lysc_feature_value(f));
246
Radek Krejci87616bb2018-10-31 13:30:52 +0100247 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200248 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200249 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
250 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200251 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100252 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200253 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200254 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200255 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200256 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200257
258 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200259 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200260 /* long evaluation of f8 to need to reallocate internal stack for operators */
261 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200262
Radek Krejci87616bb2018-10-31 13:30:52 +0100263 /* double negation of disabled f1 -> disabled */
264 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
265
Radek Krejci38920282018-11-01 09:24:16 +0100266 /* disable all features */
267 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
268 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
269 assert_int_equal(0, lys_feature_value(&mod, f->name));
270 }
271 /* re-setting already set feature */
272 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
273 assert_int_equal(0, lys_feature_value(&mod, "f1"));
274
275 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100276 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
277 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
278 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100279 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
280 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100281 /* test if not changed */
282 assert_int_equal(1, lys_feature_value(&mod, "f1"));
283 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100284
Radek Krejci0af46292019-01-11 16:02:31 +0100285 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;"
286 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
287 assert_non_null(modp->compiled);
288 assert_non_null(modp->compiled->features);
289 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
290 assert_non_null(modp->compiled->features[0].iffeatures);
291 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
292 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
293 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
294 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
295 assert_non_null(modp->compiled->features);
296 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
297 assert_non_null(modp->compiled->features[1].depfeatures);
298 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
299 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
300
Radek Krejci1aefdf72018-11-01 11:01:39 +0100301 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100302 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
303 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
304
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100305 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100306
307 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100308 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 +0100309 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100310 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100311 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100312
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100313 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100314 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100315 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100316 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100317
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100318 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100319 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100320 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100321 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100322
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100323 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100324 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100325 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100326 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100327
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100328 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100329 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100330 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100331 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100332
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100333 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100334 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100335 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100336 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100337
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100338 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100339 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100340 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100341 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100342
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100343 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100344 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
345 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100346 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100347
Radek Krejci0af46292019-01-11 16:02:31 +0100348 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
349 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 +0100350 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
351
Radek Krejci09a1fc52019-02-13 10:55:17 +0100352 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));
353 logbuf_assert("Feature \"f1\" is indirectly referenced from itself.");
354 assert_null(lys_parse_mem(ctx.ctx, "module ab{namespace urn:ab; prefix ab; feature f1 {if-feature f1;}}", LYS_IN_YANG));
355 logbuf_assert("Feature \"f1\" is referenced from itself.");
356
Radek Krejci1aefdf72018-11-01 11:01:39 +0100357 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100358 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100359 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100360 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module c{namespace urn:c; prefix c; import a {prefix a;} feature f1; feature f2{if-feature 'a:f1';}}", LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100361 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
362 assert_int_equal(0, lys_feature_value(modp, "f1"));
363 assert_int_equal(1, lys_feature_value(modp, "f2"));
364
Radek Krejcid05cbd92018-12-05 14:26:40 +0100365 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100366 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200367}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200368
Radek Krejci478020e2018-10-30 16:02:14 +0100369static void
370test_identity(void **state)
371{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100372 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100373
374 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100375 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100376
377 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100378 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
379 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
380 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
381 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100382
383 assert_non_null(mod1->compiled);
384 assert_non_null(mod1->compiled->identities);
385 assert_non_null(mod2->compiled);
386 assert_non_null(mod2->compiled->identities);
387
388 assert_non_null(mod1->compiled->identities[0].derived);
389 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
390 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
391 assert_non_null(mod2->compiled->identities[0].derived);
392 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
393 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
394 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
395 assert_non_null(mod2->compiled->identities[1].derived);
396 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
397 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
398 assert_non_null(mod2->compiled->identities[2].derived);
399 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
400 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
401
Radek Krejci4d483a82019-01-17 13:12:50 +0100402 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
403 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
404 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
405 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
406
407 assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100408 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
409
Radek Krejci4d483a82019-01-17 13:12:50 +0100410 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
411 assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100412 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
413
Radek Krejci38222632019-02-12 16:55:05 +0100414 assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG));
415 logbuf_assert("Unable to find base (i2) of identity \"i1\".");
416
417 assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG));
418 logbuf_assert("Identity \"i1\" is derived from itself.");
419 assert_null(lys_parse_mem(ctx, "module de{namespace urn:de; prefix de; identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}}", LYS_IN_YANG));
420 logbuf_assert("Identity \"i1\" is indirectly derived from itself.");
421
Radek Krejci7f2a5362018-11-28 13:05:37 +0100422 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100423 ly_ctx_destroy(ctx, NULL);
424}
425
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100426static void
427test_node_container(void **state)
428{
429 (void) state; /* unused */
430
431 struct ly_ctx *ctx;
432 struct lys_module *mod;
433 struct lysc_node_container *cont;
434
435 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
436 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100437 assert_non_null(mod->compiled);
438 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
439 assert_int_equal(LYS_CONTAINER, cont->nodetype);
440 assert_string_equal("c", cont->name);
441 assert_true(cont->flags & LYS_CONFIG_W);
442 assert_true(cont->flags & LYS_STATUS_CURR);
443
Radek Krejci98094b32018-11-02 16:21:47 +0100444 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;container c {config false; status deprecated; container child;}}", LYS_IN_YANG));
Radek Krejci98094b32018-11-02 16:21:47 +0100445 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100446 assert_non_null(mod->compiled);
447 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
448 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100449 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100450 assert_non_null((cont = (struct lysc_node_container*)cont->child));
451 assert_int_equal(LYS_CONTAINER, cont->nodetype);
452 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100453 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100454 assert_string_equal("child", cont->name);
455
456 ly_ctx_destroy(ctx, NULL);
457}
458
Radek Krejci0e5d8382018-11-28 16:37:53 +0100459static void
460test_node_leaflist(void **state)
461{
462 *state = test_node_leaflist;
463
464 struct ly_ctx *ctx;
465 struct lys_module *mod;
466 struct lysc_type *type;
467 struct lysc_node_leaflist *ll;
468
469 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
470
471 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
472 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
473 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
474 "leaf-list ll2 {type leafref {path ../target;}}"
475 "leaf target {type int8;}}",
476 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100477 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
478 assert_non_null(type);
479 assert_int_equal(1, type->refcount);
480 assert_int_equal(LY_TYPE_UNION, type->basetype);
481 assert_non_null(((struct lysc_type_union*)type)->types);
482 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
483 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
484 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
485 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
486 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
487 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
488 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
489 assert_non_null(type);
490 assert_int_equal(1, type->refcount);
491 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
492 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
493 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
494
Radek Krejci6bb080b2018-11-28 17:23:41 +0100495 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf-list ll {type string;}}", LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100496 assert_non_null(mod->compiled);
497 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
498 assert_int_equal(0, ll->min);
499 assert_int_equal((uint32_t)-1, ll->max);
500
Radek Krejci6bb080b2018-11-28 17:23:41 +0100501 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;typedef mytype {type int8;default 10;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100502 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100503 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100504 assert_non_null(mod->compiled);
505 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
506 assert_non_null(ll->dflts);
507 assert_int_equal(3, ll->type->refcount);
508 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
509 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100510 assert_string_equal("1", ll->dflts[1]);
Radek Krejci93dcc392019-02-19 10:43:38 +0100511 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 +0100512 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
513 assert_non_null(ll->dflts);
514 assert_int_equal(3, ll->type->refcount);
515 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
516 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100517 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
518
Radek Krejcifc11bd72019-04-11 16:00:05 +0200519 /* ordered-by is ignored for state data, RPC/action output parameters and notification content */
Radek Krejcia6d57732018-11-29 13:40:37 +0100520 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
521 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100522 /* but warning is present: */
Radek Krejcifc11bd72019-04-11 16:00:05 +0200523 logbuf_assert("The ordered-by statement is ignored in lists representing state data ().");
Radek Krejcia6d57732018-11-29 13:40:37 +0100524 assert_non_null(mod->compiled);
525 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
Radek Krejci93dcc392019-02-19 10:43:38 +0100526 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200527 logbuf_clean();
528
529 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;"
530 "rpc oper {output {leaf-list ll {type string; ordered-by user;}}}}", LYS_IN_YANG));
531 logbuf_assert("The ordered-by statement is ignored in lists representing RPC/action output parameters ().");
532 logbuf_clean();
533
534 assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f;"
535 "notification event {leaf-list ll {type string; ordered-by user;}}}", LYS_IN_YANG));
536 logbuf_assert("The ordered-by statement is ignored in lists representing notification content ().");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100537
538 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100539 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 +0100540 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
541
Radek Krejci096235c2019-01-11 11:12:19 +0100542 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 +0100543 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
544
545 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
546 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100547 assert_non_null(mod->compiled);
548 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
549 assert_non_null(ll->dflts);
550 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100551 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
552 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100553 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
554
Radek Krejci0e5d8382018-11-28 16:37:53 +0100555 *state = NULL;
556 ly_ctx_destroy(ctx, NULL);
557}
558
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100559static void
560test_node_list(void **state)
561{
562 *state = test_node_list;
563
564 struct ly_ctx *ctx;
565 struct lys_module *mod;
566 struct lysc_node_list *list;
567
568 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
569
570 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
571 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
572 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100573 list = (struct lysc_node_list*)mod->compiled->data;
574 assert_non_null(list);
575 assert_non_null(list->keys);
576 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
577 assert_string_equal("x", list->keys[0]->name);
578 assert_string_equal("y", list->keys[1]->name);
579 assert_non_null(list->child);
580 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
581 assert_true(list->child->flags & LYS_KEY);
582 assert_true(list->child->next->flags & LYS_KEY);
583 list = (struct lysc_node_list*)mod->compiled->data->next;
584 assert_non_null(list);
585 assert_null(list->keys);
586 assert_non_null(list->child);
Radek Krejci93dcc392019-02-19 10:43:38 +0100587 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, list->flags);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100588 assert_false(list->child->flags & LYS_KEY);
589
590 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
591 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100592 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100593 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100594 list = (struct lysc_node_list*)mod->compiled->data;
595 assert_non_null(list);
596 assert_string_equal("l", list->name);
597 assert_non_null(list->keys);
598 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
599 assert_string_equal("a", list->keys[0]->name);
600 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100601 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100602 assert_non_null(list->uniques);
603 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
604 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
605 assert_string_equal("a", list->uniques[0][0]->name);
606 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
607 assert_string_equal("b", list->uniques[0][1]->name);
608 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
609 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
610 assert_string_equal("e", list->uniques[1][0]->name);
611 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
612 assert_string_equal("d", list->uniques[1][1]->name);
613 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
614
Radek Krejci665421c2018-12-05 08:03:39 +0100615 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
616 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100617 list = (struct lysc_node_list*)mod->compiled->data;
618 assert_non_null(list);
619 assert_string_equal("l", list->name);
620 assert_non_null(list->keys);
621 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
622 assert_string_equal("a", list->keys[0]->name);
623 assert_true(list->keys[0]->flags & LYS_KEY);
624 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
625
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100626 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100627 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 +0100628 logbuf_assert("Missing key in list representing configuration data.");
629
Radek Krejci096235c2019-01-11 11:12:19 +0100630 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
631 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100632 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
633
Radek Krejci096235c2019-01-11 11:12:19 +0100634 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
635 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100636 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
637
Radek Krejci096235c2019-01-11 11:12:19 +0100638 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
639 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100640 logbuf_assert("Key of the configuration list must not be status leaf.");
641
Radek Krejci096235c2019-01-11 11:12:19 +0100642 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
643 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100644 logbuf_assert("Configuration node cannot be child of any state data node.");
645
Radek Krejci096235c2019-01-11 11:12:19 +0100646 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
647 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100648 logbuf_assert("The list's key \"x\" not found.");
649
Radek Krejci096235c2019-01-11 11:12:19 +0100650 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
651 "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 +0100652 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100653
Radek Krejci096235c2019-01-11 11:12:19 +0100654 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
655 "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 +0100656 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
657
Radek Krejci096235c2019-01-11 11:12:19 +0100658 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
659 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100660 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
661
Radek Krejci096235c2019-01-11 11:12:19 +0100662 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
663 "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 +0100664 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
665
Radek Krejci096235c2019-01-11 11:12:19 +0100666 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
667 "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 +0100668 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
669
Radek Krejci096235c2019-01-11 11:12:19 +0100670 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
671 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100672 logbuf_assert("Duplicated key identifier \"x\".");
673
Radek Krejci096235c2019-01-11 11:12:19 +0100674 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
675 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100676 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
677
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100678 *state = NULL;
679 ly_ctx_destroy(ctx, NULL);
680}
681
Radek Krejci056d0a82018-12-06 16:57:25 +0100682static void
683test_node_choice(void **state)
684{
685 *state = test_node_choice;
686
687 struct ly_ctx *ctx;
688 struct lys_module *mod;
689 struct lysc_node_choice *ch;
690 struct lysc_node_case *cs;
691
692 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
693
694 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100695 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
696 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100697 ch = (struct lysc_node_choice*)mod->compiled->data;
698 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100699 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100700 cs = ch->cases;
701 assert_non_null(cs);
702 assert_string_equal("a", cs->name);
703 assert_ptr_equal(ch, cs->parent);
704 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100705 assert_string_equal("a1", cs->child->name);
706 assert_non_null(cs->child->next);
707 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100708 assert_ptr_equal(cs, cs->child->parent);
709 cs = (struct lysc_node_case*)cs->next;
710 assert_non_null(cs);
711 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100712 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100713 assert_ptr_equal(ch, cs->parent);
714 assert_non_null(cs->child);
715 assert_string_equal("b", cs->child->name);
716 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100717 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
718 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100719 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100720 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100721
Radek Krejci096235c2019-01-11 11:12:19 +0100722 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
723 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100724 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100725 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
726 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +0100727 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100728 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
729 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100730 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100731 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
732 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100733 logbuf_assert("Duplicate identifier \"b\" of case statement.");
734
Radek Krejci096235c2019-01-11 11:12:19 +0100735 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
736 "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 +0100737 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100738 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
739 "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 +0100740 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100741 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
742 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100743 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
744 /* TODO check with mandatory nodes from augment placed into the case */
745
Radek Krejci056d0a82018-12-06 16:57:25 +0100746 *state = NULL;
747 ly_ctx_destroy(ctx, NULL);
748}
749
Radek Krejci9800fb82018-12-13 14:26:23 +0100750static void
751test_node_anydata(void **state)
752{
753 *state = test_node_anydata;
754
755 struct ly_ctx *ctx;
756 struct lys_module *mod;
757 struct lysc_node_anydata *any;
758
759 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
760
761 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
762 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100763 any = (struct lysc_node_anydata*)mod->compiled->data;
764 assert_non_null(any);
765 assert_int_equal(LYS_ANYDATA, any->nodetype);
Radek Krejci93dcc392019-02-19 10:43:38 +0100766 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags);
Radek Krejci9800fb82018-12-13 14:26:23 +0100767
768 logbuf_clean();
769 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
770 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100771 any = (struct lysc_node_anydata*)mod->compiled->data;
772 assert_non_null(any);
773 assert_int_equal(LYS_ANYXML, any->nodetype);
774 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
775 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
776
777 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100778 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 +0100779 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
780
781 *state = NULL;
782 ly_ctx_destroy(ctx, NULL);
783}
784
Radek Krejcif538ce52019-03-05 10:46:14 +0100785static void
786test_action(void **state)
787{
788 *state = test_action;
789
790 struct ly_ctx *ctx;
791 struct lys_module *mod;
792 const struct lysc_action *rpc;
793
794 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
795
796 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
797 "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG));
798 rpc = mod->compiled->rpcs;
799 assert_non_null(rpc);
800 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
801 assert_int_equal(LYS_ACTION, rpc->nodetype);
802 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
803 assert_string_equal("a", rpc->name);
804
805 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
806 "action b {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}}", LYS_IN_YANG));
807 rpc = lysc_node_actions(mod->compiled->data);
808 assert_non_null(rpc);
809 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
810 assert_int_equal(LYS_ACTION, rpc->nodetype);
811 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
812 assert_string_equal("b", rpc->name);
813
814 /* invalid */
815 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG));
816 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
817
Radek Krejci8cce8532019-03-05 11:27:45 +0100818 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG));
819 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
820 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));
821 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
822 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));
823 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200824 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 +0100825 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG));
826 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
827
Radek Krejcifc11bd72019-04-11 16:00:05 +0200828 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;}}}}"
829 "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
830 logbuf_assert("Action \"invalid\" is placed inside another RPC/action.");
831
832 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;}}}"
833 "augment /test/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
834 logbuf_assert("Action \"invalid\" is placed inside Notification.");
835
836 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; notification test {container a {uses grp;}}"
837 "grouping grp {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
838 logbuf_assert("Action \"invalid\" is placed inside Notification.");
839
840 *state = NULL;
841 ly_ctx_destroy(ctx, NULL);
842}
843
844static void
845test_notification(void **state)
846{
847 *state = test_notification;
848
849 struct ly_ctx *ctx;
850 struct lys_module *mod;
851 const struct lysc_notif *notif;
852
853 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
854
855 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
856 "notification a1 {leaf x {type int8;}} notification a2;}", LYS_IN_YANG));
857 notif = mod->compiled->notifs;
858 assert_non_null(notif);
859 assert_int_equal(2, LY_ARRAY_SIZE(notif));
860 assert_int_equal(LYS_NOTIF, notif->nodetype);
861 assert_int_equal(LYS_STATUS_CURR, notif->flags);
862 assert_string_equal("a1", notif->name);
863 assert_non_null(notif->data);
864 assert_string_equal("x", notif->data->name);
865 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
866 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
867 assert_string_equal("a2", notif[1].name);
868 assert_null(notif[1].data);
869
870 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
871 "notification b1 {leaf x {type int8;}} notification b2;}}", LYS_IN_YANG));
872 notif = lysc_node_notifs(mod->compiled->data);
873 assert_non_null(notif);
874 assert_int_equal(2, LY_ARRAY_SIZE(notif));
875 assert_int_equal(LYS_NOTIF, notif->nodetype);
876 assert_int_equal(LYS_STATUS_CURR, notif->flags);
877 assert_string_equal("b1", notif->name);
878 assert_non_null(notif->data);
879 assert_string_equal("x", notif->data->name);
880 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
881 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
882 assert_string_equal("b2", notif[1].name);
883 assert_null(notif[1].data);
884
885 /* invalid */
886 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {notification x;}}", LYS_IN_YANG));
887 logbuf_assert("Invalid keyword \"notification\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
888
889 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} notification x;}", LYS_IN_YANG));
890 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement.");
891 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));
892 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement.");
893 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));
894 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement.");
895 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}");
896 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; notification w;}", LYS_IN_YANG));
897 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement.");
898
899 assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}"
900 "augment /test/input/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
901 logbuf_assert("Notification \"invalid\" is placed inside RPC/action.");
902
903 assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}"
904 "augment /test/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
905 logbuf_assert("Notification \"invalid\" is placed inside another Notification.");
906
907 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; rpc test {input {container a {uses grp;}}}"
908 "grouping grp {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
909 logbuf_assert("Notification \"invalid\" is placed inside RPC/action.");
910
Radek Krejcif538ce52019-03-05 10:46:14 +0100911 *state = NULL;
912 ly_ctx_destroy(ctx, NULL);
913}
914
Radek Krejci0f07bed2018-11-13 14:01:40 +0100915/**
916 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
917 * do not test the expression itself
918 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100919static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100920test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100921{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100922 *state = test_type_range;
923
924 struct ly_ctx *ctx;
925 struct lys_module *mod;
926 struct lysc_type *type;
927
928 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
929
930 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 +0100931 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
932 assert_non_null(type);
933 assert_int_equal(LY_TYPE_INT8, type->basetype);
934 assert_non_null(((struct lysc_type_num*)type)->range);
935 assert_non_null(((struct lysc_type_num*)type)->range->parts);
936 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
937 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
938 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
939 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
940 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
941
942 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 +0100943 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
944 assert_non_null(type);
945 assert_int_equal(LY_TYPE_INT16, type->basetype);
946 assert_non_null(((struct lysc_type_num*)type)->range);
947 assert_non_null(((struct lysc_type_num*)type)->range->parts);
948 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
949 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
950 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
951 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
952 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
953
954 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 +0100955 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
956 assert_non_null(type);
957 assert_int_equal(LY_TYPE_INT32, type->basetype);
958 assert_non_null(((struct lysc_type_num*)type)->range);
959 assert_non_null(((struct lysc_type_num*)type)->range->parts);
960 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
961 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
962 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
963 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
964 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
965
966 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 +0100967 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
968 assert_non_null(type);
969 assert_int_equal(LY_TYPE_INT64, type->basetype);
970 assert_non_null(((struct lysc_type_num*)type)->range);
971 assert_non_null(((struct lysc_type_num*)type)->range->parts);
972 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
973 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
974 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
975 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
976 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
977
978 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 +0100979 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
980 assert_non_null(type);
981 assert_int_equal(LY_TYPE_UINT8, type->basetype);
982 assert_non_null(((struct lysc_type_num*)type)->range);
983 assert_non_null(((struct lysc_type_num*)type)->range->parts);
984 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
985 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
986 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
987 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
988 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
989
990 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 +0100991 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
992 assert_non_null(type);
993 assert_int_equal(LY_TYPE_UINT16, type->basetype);
994 assert_non_null(((struct lysc_type_num*)type)->range);
995 assert_non_null(((struct lysc_type_num*)type)->range->parts);
996 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
997 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
998 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
999 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1000 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1001
1002 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 +01001003 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1004 assert_non_null(type);
1005 assert_int_equal(LY_TYPE_UINT32, type->basetype);
1006 assert_non_null(((struct lysc_type_num*)type)->range);
1007 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1008 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1009 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1010 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1011 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1012 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1013
1014 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 +01001015 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1016 assert_non_null(type);
1017 assert_int_equal(LY_TYPE_UINT64, type->basetype);
1018 assert_non_null(((struct lysc_type_num*)type)->range);
1019 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1020 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1021 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1022 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1023 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1024 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1025
1026 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
1027 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +01001028 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1029 assert_non_null(type);
1030 assert_int_equal(3, type->refcount);
1031 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1032 assert_non_null(((struct lysc_type_num*)type)->range);
1033 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1034 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1035
Radek Krejcif1394042019-01-08 10:53:34 +01001036 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
1037 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
1038 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +01001039 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1040 assert_non_null(type);
1041 assert_int_equal(1, type->refcount);
1042 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1043 assert_non_null(((struct lysc_type_num*)type)->range);
1044 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
1045 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
1046 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1047 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1048 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1049 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1050
Radek Krejci0f07bed2018-11-13 14:01:40 +01001051 *state = NULL;
1052 ly_ctx_destroy(ctx, NULL);
1053}
1054
1055static void
1056test_type_length(void **state)
1057{
1058 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +01001059
1060 struct ly_ctx *ctx;
1061 struct lys_module *mod;
1062 struct lysc_type *type;
1063
1064 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1065
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001066 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 +01001067 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1068 assert_non_null(type);
1069 assert_non_null(((struct lysc_type_bin*)type)->length);
1070 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001071 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
1072 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001073 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1074 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1075 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1076
1077 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 +01001078 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1079 assert_non_null(type);
1080 assert_non_null(((struct lysc_type_bin*)type)->length);
1081 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1082 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +01001083 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1084 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001085
1086 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 +01001087 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1088 assert_non_null(type);
1089 assert_non_null(((struct lysc_type_bin*)type)->length);
1090 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1091 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1092 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +01001093 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001094
1095 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 +01001096 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1097 assert_non_null(type);
1098 assert_non_null(((struct lysc_type_bin*)type)->length);
1099 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1100 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1101 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1102 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1103
1104 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 +01001105 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1106 assert_non_null(type);
1107 assert_non_null(((struct lysc_type_bin*)type)->length);
1108 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1109 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1110 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1111 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1112
1113 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 +01001114 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1115 assert_non_null(type);
1116 assert_non_null(((struct lysc_type_bin*)type)->length);
1117 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1118 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1119 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1120 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1121 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1122 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1123
1124 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 +01001125 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1126 assert_non_null(type);
1127 assert_non_null(((struct lysc_type_bin*)type)->length);
1128 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1129 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1130 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1131 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1132 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1133 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1134
Radek Krejcib31c8992018-11-12 16:29:16 +01001135 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1136 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001137 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1138 assert_non_null(type);
1139 assert_non_null(((struct lysc_type_bin*)type)->length);
1140 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1141 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1142 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1143 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1144
1145 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1146 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001147 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1148 assert_non_null(type);
1149 assert_non_null(((struct lysc_type_bin*)type)->length);
1150 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1151 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1152 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1153 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1154
1155 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1156 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001157 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1158 assert_non_null(type);
1159 assert_non_null(((struct lysc_type_bin*)type)->length);
1160 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1161 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1162 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1163 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1164 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1165 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1166
1167 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 +01001168 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001169 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1170 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001171 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001172 assert_non_null(((struct lysc_type_bin*)type)->length);
1173 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1174 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1175 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001176 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001177 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1178 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001179 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001180 assert_non_null(((struct lysc_type_bin*)type)->length);
1181 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1182 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1183 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1184 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1185
Radek Krejci56aa27c2018-11-13 14:21:59 +01001186 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1187 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001188 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1189 assert_non_null(type);
1190 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001191 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001192 assert_non_null(((struct lysc_type_str*)type)->length);
1193 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1194 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1195 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1196 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001197
Radek Krejci5969f272018-11-23 10:03:58 +01001198 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1199 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001200 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1201 assert_non_null(type);
1202 assert_int_equal(LY_TYPE_STRING, type->basetype);
1203 assert_int_equal(1, type->refcount);
1204 assert_non_null(((struct lysc_type_str*)type)->length);
1205 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1206 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1207 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1208 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1209
Radek Krejci4f28eda2018-11-12 11:46:16 +01001210 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001211 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 +01001212 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001213 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 +01001214 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001215 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 +01001216 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001217 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 +01001218 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001219 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 +01001220 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001221 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 +01001222 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001223 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 +01001224 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001225 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 +01001226 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001227 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 +01001228 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001229 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 +01001230 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001231 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 +01001232 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001233 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 +01001234 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001235
Radek Krejci096235c2019-01-11 11:12:19 +01001236 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1237 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001238 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001239 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1240 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001241 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001242 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1243 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001244 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001245 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1246 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001247 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 +01001248 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 +01001249 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001250 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001251 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 +01001252 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001253 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001254 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 +01001255 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001256 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001257
Radek Krejci096235c2019-01-11 11:12:19 +01001258 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 +01001259 logbuf_assert("Invalid type restrictions for binary type.");
1260
Radek Krejci4f28eda2018-11-12 11:46:16 +01001261 *state = NULL;
1262 ly_ctx_destroy(ctx, NULL);
1263}
1264
Radek Krejcicda74152018-11-13 15:27:32 +01001265static void
1266test_type_pattern(void **state)
1267{
1268 *state = test_type_pattern;
1269
1270 struct ly_ctx *ctx;
1271 struct lys_module *mod;
1272 struct lysc_type *type;
1273
1274 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1275
Radek Krejci027d5802018-11-14 16:57:28 +01001276 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 +01001277 "pattern .* {error-app-tag errortag;error-message error;}"
1278 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001279 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1280 assert_non_null(type);
1281 assert_non_null(((struct lysc_type_str*)type)->patterns);
1282 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1283 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1284 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001285 assert_string_equal(".*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001286 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1287 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1288 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001289 assert_string_equal("[0-9].*[0-9]", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001290 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1291
1292 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1293 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001294 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1295 assert_non_null(type);
1296 assert_int_equal(LY_TYPE_STRING, type->basetype);
1297 assert_int_equal(1, type->refcount);
1298 assert_non_null(((struct lysc_type_str*)type)->patterns);
1299 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001300 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001301 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
Radek Krejci54579462019-04-30 12:47:06 +02001302 assert_string_equal("[0-4]*", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001303 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1304
Radek Krejci04bc1e92018-11-14 14:28:13 +01001305 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1306 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001307 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1308 assert_non_null(type);
1309 assert_int_equal(LY_TYPE_STRING, type->basetype);
1310 assert_int_equal(1, type->refcount);
1311 assert_non_null(((struct lysc_type_str*)type)->patterns);
1312 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001313 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejci04bc1e92018-11-14 14:28:13 +01001314 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1315
Radek Krejcicda74152018-11-13 15:27:32 +01001316 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001317 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 +01001318 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001319 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1320 assert_non_null(type);
1321 assert_non_null(((struct lysc_type_str*)type)->patterns);
1322 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001323 assert_string_equal("^\\p{IsLatinExtended-A}$", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001324 /* TODO check some data "^Å™$" */
1325
1326 *state = NULL;
1327 ly_ctx_destroy(ctx, NULL);
1328}
1329
Radek Krejci8b764662018-11-14 14:15:13 +01001330static void
1331test_type_enum(void **state)
1332{
1333 *state = test_type_enum;
1334
1335 struct ly_ctx *ctx;
1336 struct lys_module *mod;
1337 struct lysc_type *type;
1338
1339 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1340
Radek Krejci027d5802018-11-14 16:57:28 +01001341 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 +01001342 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1343 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001344 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1345 assert_non_null(type);
1346 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1347 assert_non_null(((struct lysc_type_enum*)type)->enums);
1348 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1349 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1350 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1351 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1352 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1353 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1354 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1355 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1356 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1357 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1358 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1359 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1360 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1361 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1362
Radek Krejci027d5802018-11-14 16:57:28 +01001363 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 {"
1364 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001365 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1366 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001367 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1368 assert_non_null(type);
1369 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1370 assert_non_null(((struct lysc_type_enum*)type)->enums);
1371 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1372 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1373 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1374 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1375 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1376
1377
1378 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001379 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1380 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1381 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 +01001382 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1383 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1384 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1385 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1386 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1387 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1388 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1389 "enum one; enum one;}}}", LYS_IN_YANG));
1390 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1391 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1392 "enum '';}}}", LYS_IN_YANG));
1393 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1394 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1395 "enum ' x';}}}", LYS_IN_YANG));
1396 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1397 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1398 "enum 'x ';}}}", LYS_IN_YANG));
1399 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1400 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1401 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1402 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1403
Radek Krejci096235c2019-01-11 11:12:19 +01001404 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 +01001405 logbuf_assert("Missing enum substatement for enumeration type.");
1406
Radek Krejci096235c2019-01-11 11:12:19 +01001407 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 +01001408 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001409 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1410
Radek Krejci096235c2019-01-11 11:12:19 +01001411 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 +01001412 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001413 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 +01001414 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 +01001415 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001416 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1417
Radek Krejci096235c2019-01-11 11:12:19 +01001418 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 +01001419 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001420 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1421
Radek Krejci096235c2019-01-11 11:12:19 +01001422 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001423 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001424 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001425
Radek Krejci096235c2019-01-11 11:12:19 +01001426 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 +01001427 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001428 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1429
Radek Krejci8b764662018-11-14 14:15:13 +01001430 *state = NULL;
1431 ly_ctx_destroy(ctx, NULL);
1432}
1433
1434static void
1435test_type_bits(void **state)
1436{
1437 *state = test_type_bits;
1438
1439 struct ly_ctx *ctx;
1440 struct lys_module *mod;
1441 struct lysc_type *type;
1442
1443 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1444
Radek Krejci027d5802018-11-14 16:57:28 +01001445 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 +01001446 "bit automin; bit one {if-feature f; position 1;}"
1447 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001448 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1449 assert_non_null(type);
1450 assert_int_equal(LY_TYPE_BITS, type->basetype);
1451 assert_non_null(((struct lysc_type_bits*)type)->bits);
1452 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1453 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1454 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1455 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1456 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1457 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1458 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1459 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1460 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1461 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1462 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1463 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1464
Radek Krejci027d5802018-11-14 16:57:28 +01001465 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 {"
1466 "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 +01001467 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001468 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1469 assert_non_null(type);
1470 assert_int_equal(LY_TYPE_BITS, type->basetype);
1471 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001472 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1473 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1474 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1475 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1476 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1477 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1478 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001479
1480
1481 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001482 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1483 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1484 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 +01001485 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1486 "bit one {position -1;}}}}", LYS_IN_YANG));
1487 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1488 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1489 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1490 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1491 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1492 "bit one; bit one;}}}", LYS_IN_YANG));
1493 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1494 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1495 "bit '11';}}}", LYS_IN_YANG));
1496 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1497 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1498 "bit 'x1$1';}}}", LYS_IN_YANG));
1499 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1500
Radek Krejci096235c2019-01-11 11:12:19 +01001501 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 +01001502 logbuf_assert("Missing bit substatement for bits type.");
1503
Radek Krejci096235c2019-01-11 11:12:19 +01001504 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 +01001505 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001506 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1507
Radek Krejci096235c2019-01-11 11:12:19 +01001508 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 +01001509 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001510 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 +01001511 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 +01001512 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001513 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1514
Radek Krejci096235c2019-01-11 11:12:19 +01001515 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 +01001516 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001517 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1518
Radek Krejci096235c2019-01-11 11:12:19 +01001519 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001520 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001521 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001522
Radek Krejci096235c2019-01-11 11:12:19 +01001523 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 +01001524 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001525 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1526
Radek Krejci8b764662018-11-14 14:15:13 +01001527 *state = NULL;
1528 ly_ctx_destroy(ctx, NULL);
1529}
1530
Radek Krejci6cba4292018-11-15 17:33:29 +01001531static void
1532test_type_dec64(void **state)
1533{
1534 *state = test_type_dec64;
1535
1536 struct ly_ctx *ctx;
1537 struct lys_module *mod;
1538 struct lysc_type *type;
1539
1540 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1541
1542 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1543 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001544 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1545 assert_non_null(type);
1546 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1547 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1548 assert_non_null(((struct lysc_type_dec*)type)->range);
1549 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1550 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1551 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1552 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1553
1554 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1555 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001556 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1557 assert_non_null(type);
1558 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1559 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1560 assert_non_null(((struct lysc_type_dec*)type)->range);
1561 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1562 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1563 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1564 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1565 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1566 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1567 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1568 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1569
1570 /* invalid cases */
1571 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1572 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1573 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1574 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1575 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1576 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1577
Radek Krejci096235c2019-01-11 11:12:19 +01001578 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 +01001579 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1580
Radek Krejci096235c2019-01-11 11:12:19 +01001581 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 +01001582 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001583
Radek Krejci096235c2019-01-11 11:12:19 +01001584 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 +01001585 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001586 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1587
Radek Krejci096235c2019-01-11 11:12:19 +01001588 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 +01001589 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001590 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1591
Radek Krejci096235c2019-01-11 11:12:19 +01001592 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 +01001593 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001594 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1595
Radek Krejci096235c2019-01-11 11:12:19 +01001596 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 +01001597 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001598 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1599
Radek Krejci6cba4292018-11-15 17:33:29 +01001600 *state = NULL;
1601 ly_ctx_destroy(ctx, NULL);
1602}
1603
Radek Krejci16c0f822018-11-16 10:46:10 +01001604static void
1605test_type_instanceid(void **state)
1606{
1607 *state = test_type_instanceid;
1608
1609 struct ly_ctx *ctx;
1610 struct lys_module *mod;
1611 struct lysc_type *type;
1612
1613 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1614
1615 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1616 "leaf l1 {type instance-identifier {require-instance true;}}"
1617 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001618 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1619 assert_non_null(type);
1620 assert_int_equal(LY_TYPE_INST, type->basetype);
1621 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1622
1623 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1624 assert_non_null(type);
1625 assert_int_equal(LY_TYPE_INST, type->basetype);
1626 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1627
1628 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1629 assert_non_null(type);
1630 assert_int_equal(LY_TYPE_INST, type->basetype);
1631 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1632
1633 /* invalid cases */
1634 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1635 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1636
Radek Krejci096235c2019-01-11 11:12:19 +01001637 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 +01001638 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1639
1640 *state = NULL;
1641 ly_ctx_destroy(ctx, NULL);
1642}
1643
Radek Krejci555cb5b2018-11-16 14:54:33 +01001644static void
1645test_type_identityref(void **state)
1646{
1647 *state = test_type_identityref;
1648
1649 struct ly_ctx *ctx;
1650 struct lys_module *mod;
1651 struct lysc_type *type;
1652
1653 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1654
1655 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;}"
1656 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001657 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001658 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1659 assert_non_null(type);
1660 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1661 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1662 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1663 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1664
1665 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1666 assert_non_null(type);
1667 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1668 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1669 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1670 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1671 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1672
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001673 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1674 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001675 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1676 assert_non_null(type);
1677 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1678 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1679 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1680 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1681 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1682
Radek Krejci555cb5b2018-11-16 14:54:33 +01001683 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001684 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 +01001685 logbuf_assert("Missing base substatement for identityref type.");
1686
Radek Krejci096235c2019-01-11 11:12:19 +01001687 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001688 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001689 logbuf_assert("Missing base substatement for identityref type mytype.");
1690
Radek Krejci096235c2019-01-11 11:12:19 +01001691 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 +01001692 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001693 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001694
Radek Krejci096235c2019-01-11 11:12:19 +01001695 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 +01001696 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001697 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001698
Radek Krejci096235c2019-01-11 11:12:19 +01001699 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001700 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001701 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1702
Radek Krejci096235c2019-01-11 11:12:19 +01001703 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 +01001704 logbuf_assert("Unable to find base (j) of identityref.");
1705
Radek Krejci096235c2019-01-11 11:12:19 +01001706 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 +01001707 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1708
Radek Krejci555cb5b2018-11-16 14:54:33 +01001709 *state = NULL;
1710 ly_ctx_destroy(ctx, NULL);
1711}
1712
Radek Krejcia3045382018-11-22 14:30:31 +01001713static void
1714test_type_leafref(void **state)
1715{
1716 *state = test_type_leafref;
1717
1718 struct ly_ctx *ctx;
1719 struct lys_module *mod;
1720 struct lysc_type *type;
1721 const char *path, *name, *prefix;
1722 size_t prefix_len, name_len;
1723 int parent_times, has_predicate;
1724
1725 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1726
1727 /* lys_path_token() */
1728 path = "invalid_path";
1729 parent_times = 0;
1730 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1731 path = "..";
1732 parent_times = 0;
1733 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1734 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
1744 path = "../../pref:id/xxx[predicate]/invalid!!!";
1745 parent_times = 0;
1746 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1747 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1748 assert_int_equal(4, prefix_len);
1749 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1750 assert_int_equal(2, name_len);
1751 assert_int_equal(0, strncmp("id", name, name_len));
1752 assert_int_equal(2, parent_times);
1753 assert_int_equal(0, has_predicate);
1754 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1755 assert_string_equal("[predicate]/invalid!!!", path);
1756 assert_int_equal(0, prefix_len);
1757 assert_null(prefix);
1758 assert_int_equal(3, name_len);
1759 assert_int_equal(0, strncmp("xxx", name, name_len));
1760 assert_int_equal(1, has_predicate);
1761 path += 11;
1762 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1763 assert_string_equal("!!!", path);
1764 assert_int_equal(0, prefix_len);
1765 assert_null(prefix);
1766 assert_int_equal(7, name_len);
1767 assert_int_equal(0, strncmp("invalid", name, name_len));
1768
1769 path = "/absolute/prefix:path";
1770 parent_times = 0;
1771 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1772 assert_string_equal("/prefix:path", path);
1773 assert_int_equal(0, prefix_len);
1774 assert_null(prefix);
1775 assert_int_equal(8, name_len);
1776 assert_int_equal(0, strncmp("absolute", name, name_len));
1777 assert_int_equal(-1, parent_times);
1778 assert_int_equal(0, has_predicate);
1779 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1780 assert_int_equal(0, *path);
1781 assert_int_equal(6, prefix_len);
1782 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1783 assert_int_equal(4, name_len);
1784 assert_int_equal(0, strncmp("path", name, name_len));
1785 assert_int_equal(0, has_predicate);
1786
1787 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001788 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 +01001789 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1790 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001791 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1792 assert_non_null(type);
1793 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1794 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001795 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001796 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1797 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001798 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1799 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1800 assert_non_null(type);
1801 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1802 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001803 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1804 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1805 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001806 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1807
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001808 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 +01001809 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001810 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001811 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1812 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001813 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001814 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1815 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1816 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001817 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1818 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001819 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1820
1821 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001822 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 +01001823 "typedef mytype3 {type c:mytype {require-instance false;}}"
1824 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001825 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1826 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001827 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001828 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1829 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1830 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001831 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1832 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001833 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1834 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1835 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001836 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001837 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1838 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1839 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1840 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1841
Radek Krejci4ce68932018-11-28 12:53:36 +01001842 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1843 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1844 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001845 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1846 assert_non_null(type);
1847 assert_int_equal(1, type->refcount);
1848 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1849 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1850 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1851 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1852 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1853 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1854
Radek Krejci58d171e2018-11-23 13:50:55 +01001855 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001856 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 +01001857 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1858 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001859 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1860 assert_non_null(type);
1861 assert_int_equal(1, type->refcount);
1862 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1863 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1864 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1865 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1866 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1867
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001868 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1869 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1870 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001871 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001872 LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001873 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001874 assert_non_null(type);
1875 assert_int_equal(1, type->refcount);
1876 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001877 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001878 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1879 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1880 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001881
Radek Krejci20e8a182018-12-07 11:43:21 +01001882 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1883 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1884 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1885 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001886 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1887 assert_non_null(type);
1888 assert_int_equal(1, type->refcount);
1889 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1890 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1891 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1892 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1893 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1894
Radek Krejci3d929562019-02-21 11:25:55 +01001895 /* leafref to imported (not yet implemented) module */
1896 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}");
1897 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}"
1898 "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG));
1899 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1900 assert_non_null(type);
1901 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1902 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1903 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1904 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h"));
1905 assert_int_equal(1, mod->implemented);
1906 assert_non_null(mod->compiled->data);
1907 assert_string_equal("h", mod->compiled->data->name);
1908
Radek Krejci92cc8512019-04-25 09:57:06 +02001909 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module j {namespace urn:j;prefix j; leaf j {type string;}}");
1910 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;import j {prefix j;}"
1911 "leaf i {type leafref {path \"/ilist[name = current()/../j:j]/value\";}}"
1912 "list ilist {key name; leaf name {type string;} leaf value {type uint16;}}}", LYS_IN_YANG));
1913 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1914 assert_non_null(type);
1915 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1916 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1917 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1918 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "j"));
1919 assert_int_equal(1, mod->implemented);
1920 assert_non_null(mod->compiled->data);
1921 assert_string_equal("j", mod->compiled->data->name);
1922
Radek Krejcia3045382018-11-22 14:30:31 +01001923 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001924 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 +01001925 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001926 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001927 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 +01001928 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001929 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001930 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 +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 module connected with the prefix of the node \"/a:invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001933 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 +01001934 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001935 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001936 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 +01001937 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001938 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001939 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 +01001940 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001941 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001942 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 +01001943 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001944 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001945 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001946 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001947 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001948 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001949 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001950 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001951 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001952 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001953 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1954 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001955 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001956 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001957 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 +01001958
Radek Krejci096235c2019-01-11 11:12:19 +01001959 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001960 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001961 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001962 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 +01001963 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001964 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1965
Radek Krejci096235c2019-01-11 11:12:19 +01001966 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001967 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1968 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1969 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1970 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001971 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1972
Radek Krejci096235c2019-01-11 11:12:19 +01001973 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001974 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1975 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1976 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1977 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001978 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1979
Radek Krejci096235c2019-01-11 11:12:19 +01001980 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001981 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1982 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1983 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1984 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001985 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1986
Radek Krejci096235c2019-01-11 11:12:19 +01001987 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001988 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1989 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1990 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1991 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001992 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1993
Radek Krejci096235c2019-01-11 11:12:19 +01001994 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001995 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1996 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1997 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1998 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001999 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
2000
Radek Krejci096235c2019-01-11 11:12:19 +01002001 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002002 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2003 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2004 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
2005 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002006 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
2007
Radek Krejci096235c2019-01-11 11:12:19 +01002008 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002009 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2010 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2011 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
2012 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002013 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
2014
Radek Krejci096235c2019-01-11 11:12:19 +01002015 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002016 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2017 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2018 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
2019 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002020 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
2021
Radek Krejci096235c2019-01-11 11:12:19 +01002022 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002023 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2024 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2025 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
2026 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002027 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
2028
Radek Krejci096235c2019-01-11 11:12:19 +01002029 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002030 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2031 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2032 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
2033 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002034 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
2035
Radek Krejci096235c2019-01-11 11:12:19 +01002036 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002037 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2038 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2039 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
2040 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002041 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
2042
Radek Krejci096235c2019-01-11 11:12:19 +01002043 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002044 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2045 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2046 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
2047 LYS_IN_YANG));
Radek Krejci92cc8512019-04-25 09:57:06 +02002048 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 +01002049
Radek Krejci096235c2019-01-11 11:12:19 +01002050 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002051 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2052 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2053 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
2054 LYS_IN_YANG));
Radek Krejci92cc8512019-04-25 09:57:06 +02002055 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 +01002056
Radek Krejci096235c2019-01-11 11:12:19 +01002057 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002058 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2059 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
2060 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
2061 LYS_IN_YANG));
Radek Krejci92cc8512019-04-25 09:57:06 +02002062 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 +01002063
Radek Krejci096235c2019-01-11 11:12:19 +01002064 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002065 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
2066 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2067 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
2068 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01002069 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
2070
Radek Krejci412ddfa2018-11-23 11:44:11 +01002071 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01002072 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01002073 "leaf ref1 {type leafref {path /ref2;}}"
2074 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01002075 "leaf ref3 {type leafref {path /ref4;}}"
2076 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01002077 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
2078
Radek Krejcia3045382018-11-22 14:30:31 +01002079 *state = NULL;
2080 ly_ctx_destroy(ctx, NULL);
2081}
2082
Radek Krejci43699232018-11-23 14:59:46 +01002083static void
2084test_type_empty(void **state)
2085{
2086 *state = test_type_empty;
2087
2088 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01002089
2090 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2091
2092 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002093 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01002094 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01002095 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
2096
Radek Krejci096235c2019-01-11 11:12:19 +01002097 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 +01002098 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01002099 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
2100
2101 *state = NULL;
2102 ly_ctx_destroy(ctx, NULL);
2103}
2104
Radek Krejcicdfecd92018-11-26 11:27:32 +01002105
2106static void
2107test_type_union(void **state)
2108{
2109 *state = test_type_union;
2110
2111 struct ly_ctx *ctx;
2112 struct lys_module *mod;
2113 struct lysc_type *type;
2114
2115 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2116
2117 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2118 "typedef mytype {type union {type int8; type mybasetype;}}"
2119 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002120 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2121 assert_non_null(type);
2122 assert_int_equal(2, type->refcount);
2123 assert_int_equal(LY_TYPE_UNION, type->basetype);
2124 assert_non_null(((struct lysc_type_union*)type)->types);
2125 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2126 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2127 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2128
2129 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2130 "typedef mytype {type union {type int8; type mybasetype;}}"
2131 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002132 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2133 assert_non_null(type);
2134 assert_int_equal(1, type->refcount);
2135 assert_int_equal(LY_TYPE_UNION, type->basetype);
2136 assert_non_null(((struct lysc_type_union*)type)->types);
2137 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2138 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2139 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2140 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2141
2142 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2143 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002144 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2145 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002146 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002147 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2148 assert_non_null(type);
2149 assert_int_equal(1, type->refcount);
2150 assert_int_equal(LY_TYPE_UNION, type->basetype);
2151 assert_non_null(((struct lysc_type_union*)type)->types);
2152 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2153 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2154 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2155 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2156 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2157 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2158
Radek Krejci6be5ff12018-11-26 13:18:15 +01002159 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01002160 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002161 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002162 logbuf_assert("Missing type substatement for union type mytype.");
2163
Radek Krejci096235c2019-01-11 11:12:19 +01002164 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2165 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002166
Radek Krejci096235c2019-01-11 11:12:19 +01002167 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 +01002168 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002169 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2170
Radek Krejci096235c2019-01-11 11:12:19 +01002171 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 +01002172 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002173 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2174
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002175 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;typedef mytype {type union{type mytype; type string;}}"
2176 "leaf l {type mytype;}}", LYS_IN_YANG));
2177 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected.");
2178 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;typedef mytype {type mytype2;}"
2179 "typedef mytype2 {type mytype;} leaf l {type mytype;}}", LYS_IN_YANG));
2180 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected.");
2181
Radek Krejcicdfecd92018-11-26 11:27:32 +01002182 *state = NULL;
2183 ly_ctx_destroy(ctx, NULL);
2184}
2185
2186static void
2187test_type_dflt(void **state)
2188{
2189 *state = test_type_union;
2190
2191 struct ly_ctx *ctx;
2192 struct lys_module *mod;
2193 struct lysc_type *type;
2194
2195 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2196
2197 /* default is not inherited from union's types */
2198 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2199 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002200 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2201 assert_non_null(type);
2202 assert_int_equal(1, type->refcount);
2203 assert_int_equal(LY_TYPE_UNION, type->basetype);
2204 assert_non_null(((struct lysc_type_union*)type)->types);
2205 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2206 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2207 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2208 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2209 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2210
2211 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2212 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002213 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2214 assert_non_null(type);
2215 assert_int_equal(2, type->refcount);
2216 assert_int_equal(LY_TYPE_STRING, type->basetype);
2217 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2218 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2219
2220 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2221 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002222 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2223 assert_non_null(type);
2224 assert_int_equal(2, type->refcount);
2225 assert_int_equal(LY_TYPE_STRING, type->basetype);
2226 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2227 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2228
Radek Krejci6be5ff12018-11-26 13:18:15 +01002229 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2230 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2231 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002232 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2233 assert_non_null(type);
2234 assert_int_equal(4, type->refcount);
2235 assert_int_equal(LY_TYPE_STRING, type->basetype);
2236 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2237 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2238 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->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("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2243 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2244
2245 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2246 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002247 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2248 assert_non_null(type);
2249 assert_int_equal(3, type->refcount);
2250 assert_int_equal(LY_TYPE_STRING, type->basetype);
2251 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2252 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2253
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002254 /* mandatory leaf does not takes default value from type */
2255 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2256 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002257 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2258 assert_non_null(type);
2259 assert_int_equal(LY_TYPE_STRING, type->basetype);
2260 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2261 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2262
Radek Krejcicdfecd92018-11-26 11:27:32 +01002263 *state = NULL;
2264 ly_ctx_destroy(ctx, NULL);
2265}
2266
Radek Krejci665421c2018-12-05 08:03:39 +01002267static void
2268test_status(void **state)
2269{
2270 *state = test_status;
2271
2272 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002273
2274 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2275
Radek Krejci096235c2019-01-11 11:12:19 +01002276 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002277 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002278 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2279
Radek Krejci096235c2019-01-11 11:12:19 +01002280 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002281 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002282 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2283
Radek Krejci096235c2019-01-11 11:12:19 +01002284 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002285 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002286 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2287
2288 *state = NULL;
2289 ly_ctx_destroy(ctx, NULL);
2290}
2291
Radek Krejcie86bf772018-12-14 11:39:53 +01002292static void
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002293test_grouping(void **state)
2294{
2295 *state = test_grouping;
2296
2297 struct ly_ctx *ctx;
2298
2299 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2300
2301 /* result ok, but a warning about not used locally scoped grouping printed */
2302 assert_non_null(lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; grouping grp1 {leaf a1 {type string;}}"
2303 "container a {leaf x {type string;} grouping grp2 {leaf a2 {type string;}}}}", LYS_IN_YANG));
2304 logbuf_assert("Locally scoped grouping \"grp2\" not used.");
2305 logbuf_clean();
2306
2307 /* result ok - when statement or leafref target must be checked only at the place where the grouping is really instantiated */
2308 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; grouping grp {"
2309 "leaf ref {type leafref {path \"../name\";}}"
2310 "leaf cond {type string; when \"../name = 'specialone'\";}}}", LYS_IN_YANG));
2311 logbuf_assert("");
2312
2313
2314 /* invalid - error in a non-instantiated grouping */
2315 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2316 "grouping grp {leaf x {type leafref;}}}", LYS_IN_YANG));
2317 logbuf_assert("Missing path substatement for leafref type.");
2318 logbuf_clean();
2319 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2320 "container a {grouping grp {leaf x {type leafref;}}}}", LYS_IN_YANG));
2321 logbuf_assert("Missing path substatement for leafref type.");
2322
2323
2324 *state = NULL;
2325 ly_ctx_destroy(ctx, NULL);
2326}
2327
2328static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002329test_uses(void **state)
2330{
2331 *state = test_uses;
2332
2333 struct ly_ctx *ctx;
2334 struct lys_module *mod;
Radek Krejci3641f562019-02-13 15:38:40 +01002335 const struct lysc_node *parent, *child;
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002336 const struct lysc_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01002337
2338 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2339
Radek Krejci0af46292019-01-11 16:02:31 +01002340 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2341 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002342 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2343 "grouping grp_a_top {leaf a1 {type int8;}}"
2344 "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 +01002345 assert_non_null((parent = mod->compiled->data));
2346 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2347 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2348 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002349 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002350 assert_non_null((child = child->next));
2351 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002352 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002353 assert_non_null((child = child->next));
2354 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002355 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002356 assert_non_null((child = child->next));
2357 assert_string_equal("y", child->name);
2358 assert_non_null(child->iffeatures);
2359 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2360 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2361 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2362 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2363 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2364 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2365
2366 /* make the imported module implemented and enable the feature */
2367 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2368 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2369 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2370 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2371 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002372
Radek Krejci00b874b2019-02-12 10:54:50 +01002373 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;}}}");
2374 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 +01002375 assert_non_null(mod->compiled->data);
2376 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2377 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002378 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Radek Krejcib1b59152019-01-07 13:21:56 +01002379
Radek Krejci7f6a3812019-01-07 13:48:57 +01002380 logbuf_clean();
2381 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2382 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2383 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002384 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2385 assert_string_equal("l", mod->compiled->data->name);
2386 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2387 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2388 assert_string_equal("k", mod->compiled->data->next->name);
2389 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2390 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2391
Radek Krejci3641f562019-02-13 15:38:40 +01002392 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}"
2393 "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2394 assert_non_null(mod->compiled->data);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002395 assert_non_null(child = lysc_node_children(mod->compiled->data, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002396 assert_string_equal("g", child->name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002397 assert_non_null(child = lysc_node_children(child, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002398 assert_string_equal("x", child->name);
2399
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002400 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\";}}"
2401 "container top {action e; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2402 assert_non_null(mod->compiled->data);
2403 cont = (const struct lysc_node_container*)mod->compiled->data;
2404 assert_non_null(cont->actions);
2405 assert_int_equal(2, LY_ARRAY_SIZE(cont->actions));
2406 assert_string_equal("e", cont->actions[1].name);
2407 assert_string_equal("g", cont->actions[0].name);
2408 assert_string_equal("ultra g", cont->actions[0].dsc);
2409
2410 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\";}}"
2411 "container top {notification f; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2412 assert_non_null(mod->compiled->data);
2413 cont = (const struct lysc_node_container*)mod->compiled->data;
2414 assert_non_null(cont->notifs);
2415 assert_int_equal(2, LY_ARRAY_SIZE(cont->notifs));
2416 assert_string_equal("f", cont->notifs[1].name);
2417 assert_string_equal("g", cont->notifs[0].name);
2418 assert_string_equal("ultra g", cont->notifs[0].dsc);
2419
Radek Krejci684faf22019-05-27 14:31:16 +02002420 /* empty grouping */
2421 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g; grouping grp; uses grp;}", LYS_IN_YANG));
2422 assert_null(mod->compiled->data);
2423
Radek Krejcie86bf772018-12-14 11:39:53 +01002424 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002425 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 +01002426 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2427
Radek Krejci096235c2019-01-11 11:12:19 +01002428 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002429 "grouping grp {leaf a{type string;}uses grp1;}"
2430 "grouping grp1 {leaf b {type string;}uses grp2;}"
2431 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002432 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2433
Radek Krejci096235c2019-01-11 11:12:19 +01002434 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 +01002435 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2436
Radek Krejci096235c2019-01-11 11:12:19 +01002437 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 +01002438 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002439 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci76b3e962018-12-14 17:01:25 +01002440
Radek Krejci096235c2019-01-11 11:12:19 +01002441 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 +01002442 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002443 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2444
Radek Krejci95710c92019-02-11 15:49:55 +01002445 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2446 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002447 logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002448 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2449 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002450 logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002451
Radek Krejci3641f562019-02-13 15:38:40 +01002452
2453 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}"
2454 "leaf g {type string;}"
2455 "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2456 logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used.");
2457
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002458 assert_non_null(mod = lys_parse_mem(ctx, "module hh {yang-version 1.1;namespace urn:hh;prefix hh;"
2459 "grouping grp {notification g { description \"super g\";}}"
2460 "container top {notification h; uses grp {refine h {description \"ultra h\";}}}}", LYS_IN_YANG));
2461 logbuf_assert("Invalid descendant-schema-nodeid value \"h\" - target node not found.");
2462
2463 assert_non_null(mod = lys_parse_mem(ctx, "module ii {yang-version 1.1;namespace urn:ii;prefix ii;"
2464 "grouping grp {action g { description \"super g\";}}"
2465 "container top {action i; uses grp {refine i {description \"ultra i\";}}}}", LYS_IN_YANG));
2466 logbuf_assert("Invalid descendant-schema-nodeid value \"i\" - target node not found.");
Radek Krejci3641f562019-02-13 15:38:40 +01002467
Radek Krejcie86bf772018-12-14 11:39:53 +01002468 *state = NULL;
2469 ly_ctx_destroy(ctx, NULL);
2470}
2471
Radek Krejci01342af2019-01-03 15:18:08 +01002472static void
2473test_refine(void **state)
2474{
2475 *state = test_refine;
2476
2477 struct ly_ctx *ctx;
2478 struct lys_module *mod;
2479 struct lysc_node *parent, *child;
2480
2481 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2482
Radek Krejci096235c2019-01-11 11:12:19 +01002483 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!;}"
2484 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2485 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2486 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2487 "leaf x {type mytype; mandatory true; must 1;}"
2488 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2489 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002490
Radek Krejcif0089082019-01-07 16:42:01 +01002491 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 +01002492 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002493 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002494 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002495 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2496 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002497 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002498 assert_non_null((parent = mod->compiled->data));
2499 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2500 assert_string_equal("c", parent->name);
2501 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2502 assert_int_equal(LYS_LEAF, child->nodetype);
2503 assert_string_equal("l", child->name);
2504 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2505 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2506 assert_non_null(child = child->next);
2507 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2508 assert_string_equal("ll", child->name);
2509 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2510 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2511 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002512 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2513 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002514 assert_non_null(child = child->next);
2515 assert_int_equal(LYS_CHOICE, child->nodetype);
2516 assert_string_equal("ch", child->name);
2517 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2518 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2519 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002520 assert_non_null(child->iffeatures);
2521 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002522 assert_non_null(child = child->next);
2523 assert_int_equal(LYS_LEAF, child->nodetype);
2524 assert_string_equal("x", child->name);
2525 assert_false(LYS_MAND_TRUE & child->flags);
2526 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002527 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2528 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002529 assert_string_equal("refined", child->dsc);
2530 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002531 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002532 assert_int_equal(LYS_ANYDATA, child->nodetype);
2533 assert_string_equal("a", child->name);
2534 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002535 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2536 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002537 assert_non_null(child->iffeatures);
2538 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002539 assert_string_equal("refined", child->dsc);
2540 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002541 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002542 assert_int_equal(LYS_CONTAINER, child->nodetype);
2543 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002544 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002545 assert_true(LYS_CONFIG_W & child->flags);
2546 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002547
2548 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 +01002549 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002550 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002551 assert_int_equal(LYS_LEAF, child->nodetype);
2552 assert_string_equal("x", child->name);
2553 assert_false(LYS_MAND_TRUE & child->flags);
2554 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2555
2556 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002557 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002558 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002559 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2560
Radek Krejci096235c2019-01-11 11:12:19 +01002561 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002562 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002563 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2564
Radek Krejci096235c2019-01-11 11:12:19 +01002565 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002566 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002567 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2568
Radek Krejci096235c2019-01-11 11:12:19 +01002569 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002570 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002571 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2572
Radek Krejci096235c2019-01-11 11:12:19 +01002573 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002574 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002575 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002576 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002577 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002578 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2579
Radek Krejci096235c2019-01-11 11:12:19 +01002580 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002581 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejcif1421c22019-02-19 13:05:20 +01002582 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" under the default case.");
Radek Krejci01342af2019-01-03 15:18:08 +01002583
Radek Krejci096235c2019-01-11 11:12:19 +01002584 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002585 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002586 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2587
Radek Krejci096235c2019-01-11 11:12:19 +01002588 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002589 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002590 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2591
Radek Krejci096235c2019-01-11 11:12:19 +01002592 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 +01002593 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002594 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2595
Radek Krejci096235c2019-01-11 11:12:19 +01002596 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002597 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002598 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2599
Radek Krejci096235c2019-01-11 11:12:19 +01002600 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002601 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002602 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2603
Radek Krejci096235c2019-01-11 11:12:19 +01002604 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002605 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002606 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2607
Radek Krejci096235c2019-01-11 11:12:19 +01002608 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002609 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002610 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2611
Radek Krejci01342af2019-01-03 15:18:08 +01002612 *state = NULL;
2613 ly_ctx_destroy(ctx, NULL);
2614}
Radek Krejcie86bf772018-12-14 11:39:53 +01002615
Radek Krejci95710c92019-02-11 15:49:55 +01002616static void
2617test_augment(void **state)
2618{
2619 *state = test_augment;
2620
2621 struct ly_ctx *ctx;
2622 struct lys_module *mod;
2623 const struct lysc_node *node;
2624 const struct lysc_node_choice *ch;
2625 const struct lysc_node_case *c;
Radek Krejcif538ce52019-03-05 10:46:14 +01002626 const struct lysc_action *rpc;
Radek Krejci95710c92019-02-11 15:49:55 +01002627
2628 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2629
2630 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2631 "container top {leaf a {type string;}}}");
2632 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2633 "leaf b {type a:atype;}}", LYS_IN_YANG));
2634 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2635 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2636 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2637 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2638 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2639 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2640 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2641 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2642 assert_non_null(node = mod->compiled->data);
2643 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002644 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002645 assert_string_equal(node->name, "a");
2646 assert_non_null(node = node->next);
2647 assert_string_equal(node->name, "c");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002648 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002649 assert_string_equal(node->name, "c");
2650 assert_non_null(node = node->next);
2651 assert_string_equal(node->name, "d");
2652 assert_non_null(node = node->next);
2653 assert_string_equal(node->name, "c");
2654
2655 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 +01002656 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2657 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002658 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2659 assert_null(mod->compiled->data->next);
2660 assert_string_equal("ch", ch->name);
2661 assert_non_null(c = ch->cases);
2662 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002663 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002664 assert_string_equal("a", c->child->name);
2665 assert_non_null(c = (const struct lysc_node_case*)c->next);
2666 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002667 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002668 assert_string_equal("b", c->child->name);
2669 assert_non_null(c = (const struct lysc_node_case*)c->next);
2670 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002671 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002672 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002673 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002674 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002675 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002676 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2677 assert_null(c->next);
2678
2679 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2680 "container c;"
2681 "augment /c {uses g;}}", LYS_IN_YANG)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002682 assert_non_null(node = lysc_node_children(mod->compiled->data, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002683 assert_string_equal(node->name, "a");
2684
Radek Krejci339f5292019-02-11 16:42:34 +01002685 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2686 "augment /c {container sub;}}");
2687 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2688 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2689 assert_non_null(mod->compiled->data);
2690 assert_string_equal("c", mod->compiled->data->name);
2691 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2692 assert_string_equal("sub", node->name);
2693 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2694 assert_string_equal("main", node->name);
2695
Radek Krejcif538ce52019-03-05 10:46:14 +01002696 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 +01002697 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;"
2698 "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2699 "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}"
2700 "augment /hi:top {leaf l {type string; mandatory true; config false;}}"
2701 "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG));
2702 assert_non_null(node = mod->compiled->data);
2703 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2704 assert_string_equal("l", node->name);
2705 assert_true(node->flags & LYS_MAND_TRUE);
2706 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
Radek Krejcife909632019-02-12 15:34:42 +01002707 assert_non_null(node = mod->compiled->data);
2708 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2709 assert_string_equal("p", node->name);
Radek Krejci733988a2019-02-15 15:12:44 +01002710 assert_non_null(node = node->next);
2711 assert_string_equal("ll", node->name);
2712 assert_non_null(node = node->next);
2713 assert_string_equal("l", node->name);
2714 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejcife909632019-02-12 15:34:42 +01002715
Radek Krejcif538ce52019-03-05 10:46:14 +01002716 assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}"
2717 "augment /hi:func/input {leaf x {type string;}}"
2718 "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG));
2719 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
2720 assert_non_null(rpc = mod->compiled->rpcs);
2721 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
2722 assert_non_null(rpc->input.data);
2723 assert_string_equal("x", rpc->input.data->name);
2724 assert_null(rpc->input.data->next);
2725 assert_non_null(rpc->output.data);
2726 assert_string_equal("y", rpc->output.data->name);
2727 assert_null(rpc->output.data->next);
2728
Radek Krejci95710c92019-02-11 15:49:55 +01002729 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2730 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
2731 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found.");
2732
2733 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2734 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci8cce8532019-03-05 11:27:45 +01002735 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement.");
Radek Krejci95710c92019-02-11 15:49:55 +01002736
2737
Radek Krejci339f5292019-02-11 16:42:34 +01002738 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2739 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
2740 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target.");
2741
2742 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2743 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
2744 logbuf_assert("Invalid augment (/c) of container node which is not allowed to contain case node \"b\".");
2745
Radek Krejci733988a2019-02-15 15:12:44 +01002746 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}"
2747 "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
2748 logbuf_assert("Invalid augment (/hi:top) adding mandatory node \"c\" without making it conditional via when statement.");
Radek Krejcife909632019-02-12 15:34:42 +01002749
Radek Krejci3641f562019-02-13 15:38:40 +01002750 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;"
2751 "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG));
2752 logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\".");
Radek Krejci95710c92019-02-11 15:49:55 +01002753
Radek Krejcif538ce52019-03-05 10:46:14 +01002754 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;"
2755 "augment /func {leaf x {type int8;}}}", LYS_IN_YANG));
2756 logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC/action node which is not an allowed augment's target.");
2757
Radek Krejci95710c92019-02-11 15:49:55 +01002758 *state = NULL;
2759 ly_ctx_destroy(ctx, NULL);
2760}
2761
Radek Krejciccd20f12019-02-15 14:12:27 +01002762static void
2763test_deviation(void **state)
2764{
2765 *state = test_deviation;
2766
2767 struct ly_ctx *ctx;
2768 struct lys_module *mod;
2769 const struct lysc_node *node;
Radek Krejci7af64242019-02-18 13:07:53 +01002770 const struct lysc_node_list *list;
Radek Krejciccd20f12019-02-15 14:12:27 +01002771
2772 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2773
2774 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002775 "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2776 "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 +01002777 " case c {leaf c{type string;}}}"
2778 "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}"
2779 "rpc func2;}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002780 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 +01002781 "deviation /a:top/a:b {deviate not-supported;}"
2782 "deviation /a:ch/a:a/a:x {deviate not-supported;}"
2783 "deviation /a:ch/a:c/a:c {deviate not-supported;}"
2784 "deviation /a:ch/a:b {deviate not-supported;}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002785 "deviation /a:ch/a:a/a:a {deviate not-supported;}"
2786 "deviation /a:func1/a:input {deviate not-supported;}"
2787 "deviation /a:func1/a:output {deviate not-supported;}"
2788 "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002789 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2790 assert_non_null(node = mod->compiled->data);
2791 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002792 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejciccd20f12019-02-15 14:12:27 +01002793 assert_string_equal(node->name, "a");
2794 assert_non_null(node = node->next);
2795 assert_string_equal(node->name, "c");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002796 assert_null(node = node->next);
2797 assert_non_null(node = mod->compiled->data->next);
2798 assert_string_equal("ch", node->name);
2799 assert_null(((struct lysc_node_choice*)node)->dflt);
2800 assert_null(((struct lysc_node_choice*)node)->cases);
Radek Krejcif538ce52019-03-05 10:46:14 +01002801 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs));
2802 assert_null(mod->compiled->rpcs[0].input.data);
2803 assert_null(mod->compiled->rpcs[0].output.data);
Radek Krejciccd20f12019-02-15 14:12:27 +01002804
2805 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}"
2806 "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}"
2807 "deviation /c1 {deviate add {units meters;}}"
2808 "deviation /c2 {deviate delete {units meters;}}"
2809 "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG));
2810 assert_non_null(node = mod->compiled->data);
2811 assert_string_equal("c1", node->name);
2812 assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units);
2813 assert_non_null(node = node->next);
2814 assert_string_equal("c2", node->name);
2815 assert_null(((struct lysc_node_leaf*)node)->units);
2816 assert_non_null(node = node->next);
2817 assert_string_equal("c3", node->name);
2818 assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units);
2819
2820 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 +01002821 "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002822 "deviation /c1 {deviate add {must 3;}}"
2823 "deviation /c2 {deviate delete {must 2;}}"
2824 "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG));
2825 assert_non_null(node = mod->compiled->data);
2826 assert_string_equal("c1", node->name);
2827 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts));
2828 assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr);
2829 assert_non_null(node = node->next);
2830 assert_string_equal("c2", node->name);
Radek Krejcib4532d12019-02-15 16:13:29 +01002831 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts));
2832 assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr);
Radek Krejciccd20f12019-02-15 14:12:27 +01002833 assert_non_null(node = node->next);
2834 assert_string_equal("c3", node->name);
2835 assert_null(((struct lysc_node_leaf*)node)->musts);
2836
Radek Krejcib4532d12019-02-15 16:13:29 +01002837 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 +01002838 "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 +01002839 "choice b {default a;leaf a {type string;} leaf b {type string;}}"
2840 "leaf c {default hello; type string;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002841 "leaf-list d {default hello; default world; type string;}"
2842 "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002843 assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}"
2844 "deviation /x:a {deviate delete {default a;}}"
2845 "deviation /x:b {deviate delete {default x:a;}}"
2846 "deviation /x:c {deviate delete {default hello;}}"
2847 "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG));
2848 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2849 assert_non_null(node = mod->compiled->data);
2850 assert_null(((struct lysc_node_choice*)node)->dflt);
2851 assert_non_null(node = node->next);
2852 assert_null(((struct lysc_node_choice*)node)->dflt);
2853 assert_non_null(node = node->next);
2854 assert_null(((struct lysc_node_leaf*)node)->dflt);
2855 assert_non_null(node = node->next);
2856 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2857 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002858 assert_non_null(node = node->next);
2859 assert_string_equal("nothing", ((struct lysc_node_leaf*)node)->dflt);
2860 assert_non_null(node = node->next);
2861 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2862 assert_string_equal("nothing", ((struct lysc_node_leaflist*)node)->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002863
2864 assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}"
2865 "deviation /x:b {deviate add {default x:b;}}"
2866 "deviation /x:c {deviate add {default bye;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002867 "deviation /x:d {deviate add {default all; default people;}}"
2868 "deviation /x:c2 {deviate add {default hi;}}"
2869 "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002870 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2871 assert_non_null(node = mod->compiled->data);
2872 assert_null(((struct lysc_node_choice*)node)->dflt);
2873 assert_non_null(node = node->next);
2874 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2875 assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name);
2876 assert_non_null(node = node->next);
2877 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2878 assert_string_equal("bye", ((struct lysc_node_leaf*)node)->dflt);
2879 assert_non_null(node = node->next);
2880 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2881 assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]);
2882 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
2883 assert_string_equal("people", ((struct lysc_node_leaflist*)node)->dflts[2]);
Radek Krejcib4532d12019-02-15 16:13:29 +01002884 assert_non_null(node = node->next);
2885 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2886 assert_string_equal("hi", ((struct lysc_node_leaf*)node)->dflt);
2887 assert_non_null(node = node->next);
2888 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
2889 assert_string_equal("hi", ((struct lysc_node_leaflist*)node)->dflts[0]);
2890 assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]);
Radek Krejciccd20f12019-02-15 14:12:27 +01002891
2892 assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}"
2893 "deviation /x:b {deviate replace {default x:a;}}"
2894 "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG));
2895 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2896 assert_non_null(node = mod->compiled->data);
2897 assert_null(((struct lysc_node_choice*)node)->dflt);
2898 assert_non_null(node = node->next);
2899 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2900 assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name);
2901 assert_non_null(node = node->next);
2902 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2903 assert_string_equal("hello", ((struct lysc_node_leaf*)node)->dflt);
2904
Radek Krejci7af64242019-02-18 13:07:53 +01002905 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;"
2906 "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2907 "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}"
2908 " leaf c {type string;} leaf d {type string;}}}");
2909 assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}"
2910 "augment /i:l1 {leaf j_c {type string;}}"
2911 "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}"
2912 "deviation /i:l1 {deviate add {unique \"i:c\";}}"
2913 "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG));
2914 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i")));
2915 assert_non_null(list = (struct lysc_node_list*)mod->compiled->data);
2916 assert_string_equal("l1", list->name);
2917 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
2918 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
2919 assert_string_equal("b", list->uniques[0][0]->name);
2920 assert_string_equal("j_c", list->uniques[0][1]->name);
2921 assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1]));
2922 assert_string_equal("c", list->uniques[1][0]->name);
2923 assert_non_null(list = (struct lysc_node_list*)list->next);
2924 assert_string_equal("l2", list->name);
2925 assert_null(list->uniques);
2926
Radek Krejci93dcc392019-02-19 10:43:38 +01002927 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}"
2928 "container top {leaf x {type string;} leaf y {type string; config false;}}"
2929 "deviation /a {deviate add {config false; }}"
2930 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
2931 assert_non_null(node = mod->compiled->data);
2932 assert_string_equal("a", node->name);
2933 assert_true(node->flags & LYS_CONFIG_R);
2934 assert_non_null(node = node->next);
2935 assert_string_equal("top", node->name);
2936 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002937 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002938 assert_string_equal("x", node->name);
2939 assert_true(node->flags & LYS_CONFIG_R);
2940 assert_non_null(node = node->next);
2941 assert_string_equal("y", node->name);
2942 assert_true(node->flags & LYS_CONFIG_R);
2943
2944 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}"
2945 "container top {config false; leaf x {type string;}}"
2946 "deviation /a {deviate replace {config true;}}"
2947 "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG));
2948 assert_non_null(node = mod->compiled->data);
2949 assert_string_equal("a", node->name);
2950 assert_true(node->flags & LYS_CONFIG_W);
2951 assert_non_null(node = node->next);
2952 assert_string_equal("top", node->name);
2953 assert_true(node->flags & LYS_CONFIG_W);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002954 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01002955 assert_string_equal("x", node->name);
2956 assert_true(node->flags & LYS_CONFIG_W);
2957
Radek Krejcif1421c22019-02-19 13:05:20 +01002958 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;"
2959 "container a {leaf a {type string;}}"
2960 "container b {leaf b {mandatory true; type string;}}"
2961 "deviation /a/a {deviate add {mandatory true;}}"
2962 "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG));
2963 assert_non_null(node = mod->compiled->data);
2964 assert_string_equal("a", node->name);
2965 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002966 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002967 assert_non_null(node = node->next);
2968 assert_string_equal("b", node->name);
2969 assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002970 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
Radek Krejcif1421c22019-02-19 13:05:20 +01002971
Radek Krejci551b12c2019-02-19 16:11:21 +01002972 assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;"
2973 "leaf a {default test; type string;}"
2974 "leaf b {mandatory true; type string;}"
2975 "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}"
2976 "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG));
2977 assert_non_null(node = mod->compiled->data);
2978 assert_string_equal("a", node->name);
2979 assert_null(((struct lysc_node_leaf*)node)->dflt);
2980 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
2981 assert_non_null(node = node->next);
2982 assert_string_equal("b", node->name);
2983 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
2984 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
2985
2986 assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;"
2987 "leaf-list a {type string;}"
2988 "list b {config false;}"
2989 "leaf-list c {min-elements 1; max-elements 10; type string;}"
2990 "list d {min-elements 10; max-elements 100; config false;}"
2991 "deviation /a {deviate add {min-elements 1; max-elements 10;}}"
2992 "deviation /b {deviate add {min-elements 10; max-elements 100;}}"
2993 "deviation /c {deviate replace {min-elements 10; max-elements 100;}}"
2994 "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG));
2995 assert_non_null(node = mod->compiled->data);
2996 assert_string_equal("a", node->name);
2997 assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min);
2998 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max);
2999 assert_non_null(node = node->next);
3000 assert_string_equal("b", node->name);
3001 assert_int_equal(10, ((struct lysc_node_list*)node)->min);
3002 assert_int_equal(100, ((struct lysc_node_list*)node)->max);
3003 assert_non_null(node = node->next);
3004 assert_string_equal("c", node->name);
3005 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min);
3006 assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max);
3007 assert_non_null(node = node->next);
3008 assert_string_equal("d", node->name);
3009 assert_int_equal(1, ((struct lysc_node_list*)node)->min);
3010 assert_int_equal(10, ((struct lysc_node_list*)node)->max);
3011
Radek Krejci33f72892019-02-21 10:36:58 +01003012 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;}"
3013 "leaf a {type string; default 10;} leaf-list b {type string;}"
3014 "deviation /a {deviate replace {type mytype;}}"
3015 "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG));
3016 assert_non_null(node = mod->compiled->data);
3017 assert_string_equal("a", node->name);
3018 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf*)node)->type->basetype);
3019 assert_string_equal("10", ((struct lysc_node_leaf*)node)->dflt);
3020 assert_non_null(node = node->next);
3021 assert_string_equal("b", node->name);
3022 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaflist*)node)->type->basetype);
3023 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts));
3024 assert_string_equal("1", ((struct lysc_node_leaflist*)node)->dflts[0]);
3025
Radek Krejci88ef64b2019-02-18 16:02:06 +01003026 assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003027 "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG));
3028 logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003029 assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}"
3030 "deviation /a:top/a:a {deviate not-supported;}"
3031 "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG));
3032 /* warning */
3033 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 +01003034
3035 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}"
3036 "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG));
3037 logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\".");
3038
3039 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;"
3040 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
3041 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to add \"units\" property.");
3042 assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}"
3043 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
3044 logbuf_assert("Invalid deviation (/c) adding \"units\" property which already exists (with value \"centimeters\").");
3045
3046 assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;"
3047 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
3048 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to delete \"units\" property.");
3049 assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}"
3050 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
3051 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which is not present.");
3052 assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}"
3053 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
3054 logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\".");
3055
3056 assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;"
3057 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
3058 logbuf_assert("Invalid deviation (/c) of container node - it is not possible to replace \"units\" property.");
3059 assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}"
3060 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
3061 logbuf_assert("Invalid deviation (/c) replacing \"units\" property \"meters\" which is not present.");
3062
3063 /* the default is already deleted in /e:a byt module f */
3064 assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}"
3065 "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG));
3066 logbuf_assert("Invalid deviation (/e:a) deleting \"default\" property \"x:a\" which is not present.");
3067 assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}"
3068 "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG));
3069 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"x:a\" of choice. "
3070 "The prefix does not match any imported module of the deviation module.");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003071 assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}"
3072 "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG));
3073 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\".");
3074 assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}"
3075 "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG));
3076 logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module.");
3077 assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;"
3078 "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG));
3079 logbuf_assert("Invalid deviation (/a) of anyxml node - it is not possible to delete \"default\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003080
3081 assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}"
3082 "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG));
3083 logbuf_assert("Invalid deviation (/e:b) adding \"default\" property which already exists (with value \"a\").");
3084 assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01003085 "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG));
3086 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"x:a\" of choice. "
3087 "The prefix does not match any imported module of the deviation module.");
3088 assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}"
3089 "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG));
3090 logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"a\" of choice - the specified case does not exists.");
3091 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003092 "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG));
3093 logbuf_assert("Invalid deviation (/e:c) adding \"default\" property which already exists (with value \"hello\").");
Radek Krejci468a94f2019-02-15 14:52:36 +01003094 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
3095 "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG));
3096 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 +01003097 assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}"
3098 "deviation /x {deviate add {default error;}}}", LYS_IN_YANG));
Radek Krejci551b12c2019-02-19 16:11:21 +01003099 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003100
3101 assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}"
3102 "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG));
3103 logbuf_assert("Invalid deviation (/e:d) of leaf-list node - it is not possible to replace \"default\" property.");
3104
Radek Krejci7af64242019-02-18 13:07:53 +01003105 assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}"
3106 "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG));
3107 logbuf_assert("Invalid deviation (/i:l1) deleting \"unique\" property \"x\" which does not match any of the target's property values.");
3108 assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}"
3109 "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG));
3110 logbuf_assert("Invalid deviation (/i:l2) deleting \"unique\" property \"d\" which does not match any of the target's property values.");
3111 assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}"
3112 "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG));
3113 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to delete \"unique\" property.");
3114 assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}"
3115 "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG));
3116 logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to add \"unique\" property.");
Radek Krejciccd20f12019-02-15 14:12:27 +01003117
Radek Krejci93dcc392019-02-19 10:43:38 +01003118 assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}"
3119 "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG));
3120 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to add \"config\" property.");
3121 assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}"
3122 "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG));
3123 logbuf_assert("Invalid deviation of config in \"/top/x\" - configuration node cannot be child of any state data node.");
3124 assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}"
3125 "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG));
3126 logbuf_assert("Invalid deviation (/top/x) replacing \"config\" property \"config false\" which is not present.");
3127 assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}"
3128 "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG));
3129 logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to replace \"config\" property.");
3130 assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}"
3131 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
3132 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 +01003133 assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}"
3134 "deviation /x {deviate add {config true;}}}", LYS_IN_YANG));
3135 logbuf_assert("Invalid deviation (/x) adding \"config\" property which already exists (with value \"config false\").");
3136
3137 assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}"
3138 "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG));
3139 logbuf_assert("Invalid deviation of mandatory in \"/top\" - container cannot hold mandatory statement.");
3140 assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}"
3141 "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG));
3142 logbuf_assert("Invalid deviation (/top) replacing \"mandatory\" property \"mandatory true\" which is not present.");
3143 assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}"
3144 "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG));
3145 logbuf_assert("Invalid deviation (/top/x) replacing \"mandatory\" property \"mandatory true\" which is not present.");
3146 assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}"
3147 "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG));
3148 logbuf_assert("Invalid deviation (/x) adding \"mandatory\" property which already exists (with value \"mandatory true\").");
Radek Krejci93dcc392019-02-19 10:43:38 +01003149
Radek Krejci551b12c2019-02-19 16:11:21 +01003150 assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}"
3151 "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG));
3152 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf.");
3153 assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}"
3154 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3155 logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf-list.");
3156 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;}}"
3157 "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG));
3158 logbuf_assert("Invalid deviation (/ch) combining default case and mandatory choice.");
3159
3160 assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}"
3161 "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG));
3162 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 10 is bigger than max value 5.");
3163 assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}"
3164 "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG));
3165 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
3166 assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}"
3167 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3168 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 5 is bigger than max value 1.");
3169 assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}"
3170 "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG));
3171 logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10.");
3172 assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}"
3173 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3174 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
3175 assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}"
3176 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
3177 logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\").");
3178 assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}"
3179 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
3180 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3181 assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}"
3182 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
3183 logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\").");
3184 assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}"
3185 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3186 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3187 assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}"
3188 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
3189 logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present.");
3190 assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}"
3191 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3192 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3193 assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }"
3194 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
3195 logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present.");
3196
Radek Krejci33f72892019-02-21 10:36:58 +01003197 assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;"
3198 "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG));
3199 logbuf_assert("Invalid deviation (/x) of anyxml node - it is not possible to replace \"type\" property.");
3200 assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}"
3201 "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG));
3202 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3203
Radek Krejciccd20f12019-02-15 14:12:27 +01003204 *state = NULL;
3205 ly_ctx_destroy(ctx, NULL);
3206}
3207
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003208int main(void)
3209{
3210 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01003211 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
3212 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
3213 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01003214 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
3215 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01003216 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01003217 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
3218 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01003219 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01003220 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01003221 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01003222 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01003223 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01003224 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
3225 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01003226 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01003227 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01003228 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003229 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01003230 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01003231 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01003232 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02003233 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcif2de0ed2019-05-02 14:13:18 +02003234 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01003235 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01003236 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01003237 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejciccd20f12019-02-15 14:12:27 +01003238 cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003239 };
3240
3241 return cmocka_run_group_tests(tests, NULL, NULL);
3242}