blob: 9f93ae14995c61014b23519bf5117405ad9e8a7b [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcidd4e8d42018-10-16 14:55:43 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/common.h"
24#include "../../src/tree_schema_internal.h"
25#include "../../src/xpath.h"
Radek Krejcia1911222019-07-22 17:24:50 +020026#include "../../src/plugins_types.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027
28void lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat);
29
30LY_ERR lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
31 int *parent_times, int *has_predicate);
Radek Krejcidd4e8d42018-10-16 14:55:43 +020032
33#define BUFSIZE 1024
34char logbuf[BUFSIZE] = {0};
35
36/* set to 0 to printing error messages to stderr instead of checking them in code */
37#define ENABLE_LOGGER_CHECKING 1
38
39#if ENABLE_LOGGER_CHECKING
40static void
41logger(LY_LOG_LEVEL level, const char *msg, const char *path)
42{
43 (void) level; /* unused */
44
Radek Krejci87616bb2018-10-31 13:30:52 +010045 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020046 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
47 } else {
48 strncpy(logbuf, msg, BUFSIZE - 1);
49 }
50}
51#endif
52
53static int
54logger_setup(void **state)
55{
56 (void) state; /* unused */
57#if ENABLE_LOGGER_CHECKING
58 ly_set_log_clb(logger, 1);
59#endif
60 return 0;
61}
62
Radek Krejci4f28eda2018-11-12 11:46:16 +010063static int
64logger_teardown(void **state)
65{
66 (void) state; /* unused */
67#if ENABLE_LOGGER_CHECKING
68 if (*state) {
69 fprintf(stderr, "%s\n", logbuf);
70 }
71#endif
72 return 0;
73}
74
Radek Krejcidd4e8d42018-10-16 14:55:43 +020075void
76logbuf_clean(void)
77{
78 logbuf[0] = '\0';
79}
80
81#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010082# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020083#else
84# define logbuf_assert(str)
85#endif
86
Radek Krejcid05cbd92018-12-05 14:26:40 +010087static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
88 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
89 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
90{
91 *module_data = user_data;
92 *format = LYS_IN_YANG;
93 *free_module_data = NULL;
94 return LY_SUCCESS;
95}
96
Radek Krejcidd4e8d42018-10-16 14:55:43 +020097static void
Radek Krejci0bcdaed2019-01-10 10:21:34 +010098reset_mod(struct ly_ctx *ctx, struct lys_module *module)
99{
100 lysc_module_free(module->compiled, NULL);
101 lysp_module_free(module->parsed);
102
103 FREE_STRING(module->ctx, module->name);
104 FREE_STRING(module->ctx, module->ns);
105 FREE_STRING(module->ctx, module->prefix);
106 FREE_STRING(module->ctx, module->filepath);
107 FREE_STRING(module->ctx, module->org);
108 FREE_STRING(module->ctx, module->contact);
109 FREE_STRING(module->ctx, module->dsc);
110 FREE_STRING(module->ctx, module->ref);
Radek Krejci95710c92019-02-11 15:49:55 +0100111 FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100112
113 memset(module, 0, sizeof *module);
114 module->ctx = ctx;
Radek Krejci096235c2019-01-11 11:12:19 +0100115 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100116}
117
118static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200119test_module(void **state)
120{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100121 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200122
123 const char *str;
Radek Krejcie7b95092019-05-15 11:03:07 +0200124 struct lys_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200125 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200126 struct lysc_feature *f;
127 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200128
129 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200130 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100131 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100132 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200133
Radek Krejcif8f882a2018-10-31 14:51:15 +0100134 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
135 logbuf_assert("Invalid argument mod (lys_compile()).");
136 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
137 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100138 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100139 mod.implemented = 0;
140 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
141 assert_null(mod.compiled);
142 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100143 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200144 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100145 assert_string_equal("test", mod.name);
146 assert_string_equal("urn:test", mod.ns);
147 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200148 /* features */
149 assert_non_null(mod.compiled->features);
150 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200151 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200152 assert_non_null(f->iffeatures);
153 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200154 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200155 assert_non_null(iff->expr);
156 assert_non_null(iff->features);
157 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200158 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200159
Radek Krejci86d106e2018-10-18 09:53:19 +0200160 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200161
Radek Krejcif8f882a2018-10-31 14:51:15 +0100162 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200163 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200164
Radek Krejci86d106e2018-10-18 09:53:19 +0200165 lysc_module_free(mod.compiled, NULL);
166 mod.compiled = NULL;
167
Radek Krejci151a5b72018-10-19 14:21:44 +0200168 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200169 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100170 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
171 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
172 assert_null(mod.parsed);
173 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100174
175 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100176 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
177 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100178 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200179 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement. /aa:a");
Radek Krejci76b3e962018-12-14 17:01:25 +0100180 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100181 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100182
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100183 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100184 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200185}
186
Radek Krejci151a5b72018-10-19 14:21:44 +0200187static void
188test_feature(void **state)
189{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100190 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200191
Radek Krejcie7b95092019-05-15 11:03:07 +0200192 struct lys_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100193 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200194 const char *str;
195 struct lysc_feature *f, *f1;
196
197 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
198 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100199 "feature orfeature {if-feature \"f1 or f2\";}\n"
200 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200201 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200202 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100203 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
204 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200205
Radek Krejci313d9902018-11-08 09:42:58 +0100206 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100207 reset_mod(ctx.ctx, &mod);
208
209 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100210 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200211 assert_non_null(mod.compiled);
212 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100213 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200214 /* all features are disabled by default */
215 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
216 assert_int_equal(0, lysc_feature_value(f));
217 }
218 /* enable f1 */
219 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200220 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200221 assert_int_equal(1, lysc_feature_value(f1));
222
Radek Krejci87616bb2018-10-31 13:30:52 +0100223 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200224 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200225 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100226 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200227 assert_int_equal(1, lysc_feature_value(f));
228
Radek Krejci87616bb2018-10-31 13:30:52 +0100229 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200230 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200231 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100232 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
233 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200234 assert_int_equal(0, lysc_feature_value(f));
235
236 /* first enable f2, so f5 can be enabled then */
237 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100238 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200239 assert_int_equal(1, lysc_feature_value(f));
240
241 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200242 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200243 assert_int_equal(0, lysc_feature_value(f));
244 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
245 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
246 assert_int_equal(0, lysc_feature_value(f));
247
Radek Krejci87616bb2018-10-31 13:30:52 +0100248 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200249 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200250 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
251 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200252 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100253 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200254 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200255 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200256 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200257 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200258
259 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200260 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200261 /* long evaluation of f8 to need to reallocate internal stack for operators */
262 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200263
Radek Krejci87616bb2018-10-31 13:30:52 +0100264 /* double negation of disabled f1 -> disabled */
265 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
266
Radek Krejci38920282018-11-01 09:24:16 +0100267 /* disable all features */
268 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
269 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
270 assert_int_equal(0, lys_feature_value(&mod, f->name));
271 }
272 /* re-setting already set feature */
273 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
274 assert_int_equal(0, lys_feature_value(&mod, "f1"));
275
276 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100277 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
278 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
279 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100280 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
281 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100282 /* test if not changed */
283 assert_int_equal(1, lys_feature_value(&mod, "f1"));
284 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100285
Radek Krejci0af46292019-01-11 16:02:31 +0100286 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;"
287 "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG));
288 assert_non_null(modp->compiled);
289 assert_non_null(modp->compiled->features);
290 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
291 assert_non_null(modp->compiled->features[0].iffeatures);
292 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures));
293 assert_non_null(modp->compiled->features[0].iffeatures[0].features);
294 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features));
295 assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]);
296 assert_non_null(modp->compiled->features);
297 assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features));
298 assert_non_null(modp->compiled->features[1].depfeatures);
299 assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures));
300 assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]);
301
Radek Krejci1aefdf72018-11-01 11:01:39 +0100302 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100303 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
304 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
305
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100306 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100307
308 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100309 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 +0100310 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200311 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\". /b:{feature='f'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100312 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100313
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100314 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100315 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200316 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression. /b:{feature='f2'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100317 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100318
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100319 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100320 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200321 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression. /b:{feature='f'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100322 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100323
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100324 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100325 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200326 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses. /b:{feature='f2'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100327 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100328
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100329 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100330 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200331 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses. /b:{feature='f2'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100332 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100333
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100334 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100335 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200336 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\". /b:{feature='f2'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100337 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100338
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100339 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100340 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200341 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module. /b:{feature='f2'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100342 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100343
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100344 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100345 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci327de162019-06-14 12:52:07 +0200346 logbuf_assert("Duplicate identifier \"f1\" of feature statement. /b:{feature='f1'}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100347 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100348
Radek Krejci0af46292019-01-11 16:02:31 +0100349 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}");
350 assert_null(lys_parse_mem(ctx.ctx, "module z{namespace urn:z; prefix z; include sz;feature f1;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200351 logbuf_assert("Duplicate identifier \"f1\" of feature statement. /z:{feature='f1'}");
Radek Krejcid05cbd92018-12-05 14:26:40 +0100352
Radek Krejci09a1fc52019-02-13 10:55:17 +0100353 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));
Radek Krejci327de162019-06-14 12:52:07 +0200354 logbuf_assert("Feature \"f1\" is indirectly referenced from itself. /aa:{feature='f2'}");
Radek Krejci09a1fc52019-02-13 10:55:17 +0100355 assert_null(lys_parse_mem(ctx.ctx, "module ab{namespace urn:ab; prefix ab; feature f1 {if-feature f1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200356 logbuf_assert("Feature \"f1\" is referenced from itself. /ab:{feature='f1'}");
Radek Krejci09a1fc52019-02-13 10:55:17 +0100357
Radek Krejci6788abc2019-06-14 13:56:49 +0200358 assert_null(lys_parse_mem(ctx.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f {if-feature ();}}", LYS_IN_YANG));
359 logbuf_assert("Invalid value \"()\" of if-feature - number of features in expression does not match the required number "
360 "of operands for the operations. /bb:{feature='f'}");
361 assert_null(lys_parse_mem(ctx.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'f1(';}}", LYS_IN_YANG));
362 logbuf_assert("Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses. /bb:{feature='f'}");
363 assert_null(lys_parse_mem(ctx.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'and f1';}}", LYS_IN_YANG));
364 logbuf_assert("Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation. /bb:{feature='f'}");
365 assert_null(lys_parse_mem(ctx.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'f1 not ';}}", LYS_IN_YANG));
366 logbuf_assert("Invalid value \"f1 not \" of if-feature - unexpected end of expression. /bb:{feature='f'}");
367 assert_null(lys_parse_mem(ctx.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f {if-feature 'f1 not not ';}}", LYS_IN_YANG));
368 logbuf_assert("Invalid value \"f1 not not \" of if-feature - unexpected end of expression. /bb:{feature='f'}");
369 assert_null(lys_parse_mem(ctx.ctx, "module bb{yang-version 1.1; namespace urn:bb; prefix bb; feature f1; feature f2; "
370 "feature f {if-feature 'or f1 f2';}}", LYS_IN_YANG));
371 logbuf_assert("Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation. /bb:{feature='f'}");
372
Radek Krejci1aefdf72018-11-01 11:01:39 +0100373 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100374 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100375 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100376 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 +0100377 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
378 assert_int_equal(0, lys_feature_value(modp, "f1"));
379 assert_int_equal(1, lys_feature_value(modp, "f2"));
380
Radek Krejcid05cbd92018-12-05 14:26:40 +0100381 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100382 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200383}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200384
Radek Krejci478020e2018-10-30 16:02:14 +0100385static void
386test_identity(void **state)
387{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100388 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100389
390 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100391 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100392
393 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci4d483a82019-01-17 13:12:50 +0100394 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG));
395 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}"
396 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
397 "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG));
Radek Krejci478020e2018-10-30 16:02:14 +0100398
399 assert_non_null(mod1->compiled);
400 assert_non_null(mod1->compiled->identities);
401 assert_non_null(mod2->compiled);
402 assert_non_null(mod2->compiled->identities);
403
404 assert_non_null(mod1->compiled->identities[0].derived);
405 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
406 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
407 assert_non_null(mod2->compiled->identities[0].derived);
408 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
409 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
410 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
411 assert_non_null(mod2->compiled->identities[1].derived);
412 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
413 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
414 assert_non_null(mod2->compiled->identities[2].derived);
415 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
416 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
417
Radek Krejci4d483a82019-01-17 13:12:50 +0100418 assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
419 "identity c2 {base c1;} identity c1;}", LYS_IN_YANG));
420 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
421 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]);
422
423 assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200424 logbuf_assert("Duplicate identifier \"i1\" of identity statement. /aa:{identity='i1'}");
Radek Krejcid05cbd92018-12-05 14:26:40 +0100425
Radek Krejci4d483a82019-01-17 13:12:50 +0100426 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}");
427 assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200428 logbuf_assert("Duplicate identifier \"i1\" of identity statement. /bb:{identity='i1'}");
Radek Krejcid05cbd92018-12-05 14:26:40 +0100429
Radek Krejci38222632019-02-12 16:55:05 +0100430 assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200431 logbuf_assert("Unable to find base (i2) of identity \"i1\". /cc:{identity='i1'}");
Radek Krejci38222632019-02-12 16:55:05 +0100432
433 assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200434 logbuf_assert("Identity \"i1\" is derived from itself. /dd:{identity='i1'}");
Radek Krejci38222632019-02-12 16:55:05 +0100435 assert_null(lys_parse_mem(ctx, "module de{namespace urn:de; prefix de; identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200436 logbuf_assert("Identity \"i1\" is indirectly derived from itself. /de:{identity='i3'}");
Radek Krejci38222632019-02-12 16:55:05 +0100437
Radek Krejci7f2a5362018-11-28 13:05:37 +0100438 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100439 ly_ctx_destroy(ctx, NULL);
440}
441
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100442static void
443test_node_container(void **state)
444{
445 (void) state; /* unused */
446
447 struct ly_ctx *ctx;
448 struct lys_module *mod;
449 struct lysc_node_container *cont;
450
451 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
452 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 +0100453 assert_non_null(mod->compiled);
454 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
455 assert_int_equal(LYS_CONTAINER, cont->nodetype);
456 assert_string_equal("c", cont->name);
457 assert_true(cont->flags & LYS_CONFIG_W);
458 assert_true(cont->flags & LYS_STATUS_CURR);
459
Radek Krejci98094b32018-11-02 16:21:47 +0100460 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 +0100461 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100462 assert_non_null(mod->compiled);
463 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
464 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100465 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100466 assert_non_null((cont = (struct lysc_node_container*)cont->child));
467 assert_int_equal(LYS_CONTAINER, cont->nodetype);
468 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100469 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100470 assert_string_equal("child", cont->name);
471
472 ly_ctx_destroy(ctx, NULL);
473}
474
Radek Krejci0e5d8382018-11-28 16:37:53 +0100475static void
476test_node_leaflist(void **state)
477{
478 *state = test_node_leaflist;
479
480 struct ly_ctx *ctx;
481 struct lys_module *mod;
482 struct lysc_type *type;
483 struct lysc_node_leaflist *ll;
Radek Krejci1c0c3442019-07-23 16:08:47 +0200484 struct lysc_node_leaf *l;
Radek Krejcia1911222019-07-22 17:24:50 +0200485 const char *dflt;
486 int dynamic;
Radek Krejci0e5d8382018-11-28 16:37:53 +0100487
488 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
489
490 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
491 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
492 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
493 "leaf-list ll2 {type leafref {path ../target;}}"
494 "leaf target {type int8;}}",
495 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100496 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
497 assert_non_null(type);
498 assert_int_equal(1, type->refcount);
499 assert_int_equal(LY_TYPE_UNION, type->basetype);
500 assert_non_null(((struct lysc_type_union*)type)->types);
501 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
502 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
503 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
504 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
505 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
506 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
507 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
508 assert_non_null(type);
509 assert_int_equal(1, type->refcount);
510 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
511 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
512 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
513
Radek Krejci6bb080b2018-11-28 17:23:41 +0100514 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 +0100515 assert_non_null(mod->compiled);
516 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
517 assert_int_equal(0, ll->min);
518 assert_int_equal((uint32_t)-1, ll->max);
519
Radek Krejci6bb080b2018-11-28 17:23:41 +0100520 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 +0100521 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100522 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100523 assert_non_null(mod->compiled);
524 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
525 assert_non_null(ll->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +0200526 assert_int_equal(6, ll->type->refcount); /* 3x type's reference, 3x default value's reference (typedef's default does not reference own type) */
Radek Krejci6bb080b2018-11-28 17:23:41 +0100527 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
Radek Krejcia1911222019-07-22 17:24:50 +0200528 assert_string_equal("1", dflt = ll->dflts[0]->realtype->plugin->print(ll->dflts[0], LYD_XML, NULL, NULL, &dynamic));
529 assert_int_equal(0, dynamic);
530 assert_string_equal("1", dflt = ll->dflts[1]->realtype->plugin->print(ll->dflts[1], LYD_XML, NULL, NULL, &dynamic));
531 assert_int_equal(0, dynamic);
Radek Krejci93dcc392019-02-19 10:43:38 +0100532 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 +0100533 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
534 assert_non_null(ll->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +0200535 assert_int_equal(6, ll->type->refcount); /* 3x type's reference, 3x default value's reference */
Radek Krejci6bb080b2018-11-28 17:23:41 +0100536 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
Radek Krejcia1911222019-07-22 17:24:50 +0200537 assert_string_equal("10", dflt = ll->dflts[0]->realtype->plugin->print(ll->dflts[0], LYD_XML, NULL, NULL, &dynamic));
538 assert_int_equal(0, dynamic);
Radek Krejcia6d57732018-11-29 13:40:37 +0100539 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
540
Radek Krejcifc11bd72019-04-11 16:00:05 +0200541 /* ordered-by is ignored for state data, RPC/action output parameters and notification content */
Radek Krejcia6d57732018-11-29 13:40:37 +0100542 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
543 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100544 /* but warning is present: */
Radek Krejci327de162019-06-14 12:52:07 +0200545 logbuf_assert("The ordered-by statement is ignored in lists representing state data (/d:ll).");
Radek Krejcia6d57732018-11-29 13:40:37 +0100546 assert_non_null(mod->compiled);
547 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
Radek Krejci93dcc392019-02-19 10:43:38 +0100548 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200549 logbuf_clean();
550
551 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;"
552 "rpc oper {output {leaf-list ll {type string; ordered-by user;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200553 logbuf_assert("The ordered-by statement is ignored in lists representing RPC/action output parameters (/e:oper/output/ll).");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200554 logbuf_clean();
555
556 assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f;"
557 "notification event {leaf-list ll {type string; ordered-by user;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200558 logbuf_assert("The ordered-by statement is ignored in lists representing notification content (/f:event/ll).");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100559
Radek Krejci1c0c3442019-07-23 16:08:47 +0200560 /* forward reference in default */
561 assert_non_null(mod = lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;"
562 "leaf ref {type instance-identifier {require-instance true;} default \"/g:g\";}"
563 "leaf-list g {type string;}}", LYS_IN_YANG));
564 assert_non_null(l = (struct lysc_node_leaf*)mod->compiled->data);
565 assert_string_equal("ref", l->name);
566 assert_non_null(l->dflt);
567 assert_null(l->dflt->canonized);
568
Radek Krejci6bb080b2018-11-28 17:23:41 +0100569 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100570 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200571 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules. /aa:ll");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100572
Radek Krejci096235c2019-01-11 11:12:19 +0100573 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +0200574 logbuf_assert("Invalid leaf-lists's default value \"x\" which does not fit the type (Invalid empty value \"x\".). /bb:ll");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100575
576 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
577 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100578 assert_non_null(mod->compiled);
579 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
580 assert_non_null(ll->dflts);
581 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100582 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
583 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200584 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\". /dd:ll");
Radek Krejci6bb080b2018-11-28 17:23:41 +0100585
Radek Krejci1c0c3442019-07-23 16:08:47 +0200586 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;"
587 "leaf ref {type instance-identifier {require-instance true;} default \"/ee:g\";}}", LYS_IN_YANG));
588 logbuf_assert("Invalid default - value does not fit the type "
589 "(Invalid instance-identifier \"/ee:g\" value - path \"/ee:g\" does not exists in the YANG schema.). /ee:ref");
590
Radek Krejci0e5d8382018-11-28 16:37:53 +0100591 *state = NULL;
592 ly_ctx_destroy(ctx, NULL);
593}
594
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100595static void
596test_node_list(void **state)
597{
598 *state = test_node_list;
599
600 struct ly_ctx *ctx;
601 struct lys_module *mod;
602 struct lysc_node_list *list;
603
604 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
605
606 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
607 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
608 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100609 list = (struct lysc_node_list*)mod->compiled->data;
610 assert_non_null(list);
611 assert_non_null(list->keys);
612 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
613 assert_string_equal("x", list->keys[0]->name);
614 assert_string_equal("y", list->keys[1]->name);
615 assert_non_null(list->child);
616 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
617 assert_true(list->child->flags & LYS_KEY);
618 assert_true(list->child->next->flags & LYS_KEY);
619 list = (struct lysc_node_list*)mod->compiled->data->next;
620 assert_non_null(list);
621 assert_null(list->keys);
622 assert_non_null(list->child);
Radek Krejci93dcc392019-02-19 10:43:38 +0100623 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, list->flags);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100624 assert_false(list->child->flags & LYS_KEY);
625
626 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
627 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100628 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100629 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100630 list = (struct lysc_node_list*)mod->compiled->data;
631 assert_non_null(list);
632 assert_string_equal("l", list->name);
633 assert_non_null(list->keys);
634 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
635 assert_string_equal("a", list->keys[0]->name);
636 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100637 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100638 assert_non_null(list->uniques);
639 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
640 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
641 assert_string_equal("a", list->uniques[0][0]->name);
642 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
643 assert_string_equal("b", list->uniques[0][1]->name);
644 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
645 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
646 assert_string_equal("e", list->uniques[1][0]->name);
647 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
648 assert_string_equal("d", list->uniques[1][1]->name);
649 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
650
Radek Krejci665421c2018-12-05 08:03:39 +0100651 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
652 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100653 list = (struct lysc_node_list*)mod->compiled->data;
654 assert_non_null(list);
655 assert_string_equal("l", list->name);
656 assert_non_null(list->keys);
657 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
658 assert_string_equal("a", list->keys[0]->name);
659 assert_true(list->keys[0]->flags & LYS_KEY);
660 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
661
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100662 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100663 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200664 logbuf_assert("Missing key in list representing configuration data. /aa:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100665
Radek Krejci096235c2019-01-11 11:12:19 +0100666 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
667 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200668 logbuf_assert("List's key must not have any \"when\" statement. /bb:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100669
Radek Krejci096235c2019-01-11 11:12:19 +0100670 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
671 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200672 logbuf_assert("List's key must not have any \"if-feature\" statement. /cc:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100673
Radek Krejci096235c2019-01-11 11:12:19 +0100674 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
675 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200676 logbuf_assert("Key of the configuration list must not be status leaf. /dd:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100677
Radek Krejci096235c2019-01-11 11:12:19 +0100678 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
679 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200680 logbuf_assert("Configuration node cannot be child of any state data node. /ee:l/x");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100681
Radek Krejci096235c2019-01-11 11:12:19 +0100682 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
683 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200684 logbuf_assert("The list's key \"x\" not found. /ff:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100685
Radek Krejci096235c2019-01-11 11:12:19 +0100686 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
687 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200688 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf. /gg:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100689
Radek Krejci096235c2019-01-11 11:12:19 +0100690 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
691 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200692 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type. /hh:l");
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100693
Radek Krejci096235c2019-01-11 11:12:19 +0100694 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
695 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200696 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\". /ii:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100697
Radek Krejci096235c2019-01-11 11:12:19 +0100698 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
699 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200700 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found. /jj:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100701
Radek Krejci096235c2019-01-11 11:12:19 +0100702 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
703 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200704 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator. /kk:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100705
Radek Krejci096235c2019-01-11 11:12:19 +0100706 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
707 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200708 logbuf_assert("Duplicated key identifier \"x\". /ll:l");
Radek Krejci665421c2018-12-05 08:03:39 +0100709
Radek Krejci096235c2019-01-11 11:12:19 +0100710 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
711 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200712 logbuf_assert("List's key cannot be of \"empty\" type until it is in YANG 1.1 module. /mm:l/x");
Radek Krejci665421c2018-12-05 08:03:39 +0100713
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100714 *state = NULL;
715 ly_ctx_destroy(ctx, NULL);
716}
717
Radek Krejci056d0a82018-12-06 16:57:25 +0100718static void
719test_node_choice(void **state)
720{
721 *state = test_node_choice;
722
723 struct ly_ctx *ctx;
724 struct lys_module *mod;
725 struct lysc_node_choice *ch;
726 struct lysc_node_case *cs;
727
728 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
729
730 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100731 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
732 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100733 ch = (struct lysc_node_choice*)mod->compiled->data;
734 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100735 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100736 cs = ch->cases;
737 assert_non_null(cs);
738 assert_string_equal("a", cs->name);
739 assert_ptr_equal(ch, cs->parent);
740 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100741 assert_string_equal("a1", cs->child->name);
742 assert_non_null(cs->child->next);
743 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100744 assert_ptr_equal(cs, cs->child->parent);
745 cs = (struct lysc_node_case*)cs->next;
746 assert_non_null(cs);
747 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100748 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100749 assert_ptr_equal(ch, cs->parent);
750 assert_non_null(cs->child);
751 assert_string_equal("b", cs->child->name);
752 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100753 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
754 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100755 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100756 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100757
Radek Krejci096235c2019-01-11 11:12:19 +0100758 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
759 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200760 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement. /aa:ch/x/x");
Radek Krejci096235c2019-01-11 11:12:19 +0100761 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
762 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200763 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement. /aa2:ch/b/y");
Radek Krejci096235c2019-01-11 11:12:19 +0100764 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
765 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200766 logbuf_assert("Duplicate identifier \"a\" of case statement. /bb:ch/a");
Radek Krejci096235c2019-01-11 11:12:19 +0100767 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
768 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200769 logbuf_assert("Duplicate identifier \"b\" of case statement. /bb2:ch/b");
Radek Krejci056d0a82018-12-06 16:57:25 +0100770
Radek Krejci096235c2019-01-11 11:12:19 +0100771 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
772 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200773 logbuf_assert("Default case \"c\" not found. /ca:ch");
Radek Krejci096235c2019-01-11 11:12:19 +0100774 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
775 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200776 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\"). /cb:ch");
Radek Krejci096235c2019-01-11 11:12:19 +0100777 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
778 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200779 logbuf_assert("Mandatory node \"x\" under the default case \"a\". /cc:ch");
Radek Krejcia9026eb2018-12-12 16:04:47 +0100780 /* TODO check with mandatory nodes from augment placed into the case */
781
Radek Krejci056d0a82018-12-06 16:57:25 +0100782 *state = NULL;
783 ly_ctx_destroy(ctx, NULL);
784}
785
Radek Krejci9800fb82018-12-13 14:26:23 +0100786static void
787test_node_anydata(void **state)
788{
789 *state = test_node_anydata;
790
791 struct ly_ctx *ctx;
792 struct lys_module *mod;
793 struct lysc_node_anydata *any;
794
795 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
796
797 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
798 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100799 any = (struct lysc_node_anydata*)mod->compiled->data;
800 assert_non_null(any);
801 assert_int_equal(LYS_ANYDATA, any->nodetype);
Radek Krejci93dcc392019-02-19 10:43:38 +0100802 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags);
Radek Krejci9800fb82018-12-13 14:26:23 +0100803
804 logbuf_clean();
805 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
806 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100807 any = (struct lysc_node_anydata*)mod->compiled->data;
808 assert_non_null(any);
809 assert_int_equal(LYS_ANYXML, any->nodetype);
810 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
811 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
812
813 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100814 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 +0100815 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
816
817 *state = NULL;
818 ly_ctx_destroy(ctx, NULL);
819}
820
Radek Krejcif538ce52019-03-05 10:46:14 +0100821static void
822test_action(void **state)
823{
824 *state = test_action;
825
826 struct ly_ctx *ctx;
827 struct lys_module *mod;
828 const struct lysc_action *rpc;
829
830 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
831
832 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
833 "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG));
834 rpc = mod->compiled->rpcs;
835 assert_non_null(rpc);
836 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
837 assert_int_equal(LYS_ACTION, rpc->nodetype);
838 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
839 assert_string_equal("a", rpc->name);
840
841 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
842 "action b {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}}", LYS_IN_YANG));
843 rpc = lysc_node_actions(mod->compiled->data);
844 assert_non_null(rpc);
845 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
846 assert_int_equal(LYS_ACTION, rpc->nodetype);
847 assert_int_equal(LYS_STATUS_CURR, rpc->flags);
848 assert_string_equal("b", rpc->name);
849
850 /* invalid */
851 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG));
852 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
853
Radek Krejci8cce8532019-03-05 11:27:45 +0100854 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200855 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement. /bb:x");
Radek Krejci8cce8532019-03-05 11:27:45 +0100856 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));
Radek Krejci327de162019-06-14 12:52:07 +0200857 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement. /cc:c/y");
Radek Krejci8cce8532019-03-05 11:27:45 +0100858 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));
Radek Krejci327de162019-06-14 12:52:07 +0200859 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement. /dd:c/z");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200860 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 +0100861 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200862 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement. /ee:w");
Radek Krejci8cce8532019-03-05 11:27:45 +0100863
Radek Krejcifc11bd72019-04-11 16:00:05 +0200864 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;}}}}"
865 "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200866 logbuf_assert("Action \"invalid\" is placed inside another RPC/action. /ff:{augment='/test/input/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200867
868 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;}}}"
869 "augment /test/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200870 logbuf_assert("Action \"invalid\" is placed inside Notification. /gg:{augment='/test/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200871
872 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; notification test {container a {uses grp;}}"
873 "grouping grp {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200874 logbuf_assert("Action \"invalid\" is placed inside Notification. /hh:test/a/{uses='grp'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200875
876 *state = NULL;
877 ly_ctx_destroy(ctx, NULL);
878}
879
880static void
881test_notification(void **state)
882{
883 *state = test_notification;
884
885 struct ly_ctx *ctx;
886 struct lys_module *mod;
887 const struct lysc_notif *notif;
888
889 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
890
891 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
892 "notification a1 {leaf x {type int8;}} notification a2;}", LYS_IN_YANG));
893 notif = mod->compiled->notifs;
894 assert_non_null(notif);
895 assert_int_equal(2, LY_ARRAY_SIZE(notif));
896 assert_int_equal(LYS_NOTIF, notif->nodetype);
897 assert_int_equal(LYS_STATUS_CURR, notif->flags);
898 assert_string_equal("a1", notif->name);
899 assert_non_null(notif->data);
900 assert_string_equal("x", notif->data->name);
901 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
902 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
903 assert_string_equal("a2", notif[1].name);
904 assert_null(notif[1].data);
905
906 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {"
907 "notification b1 {leaf x {type int8;}} notification b2;}}", LYS_IN_YANG));
908 notif = lysc_node_notifs(mod->compiled->data);
909 assert_non_null(notif);
910 assert_int_equal(2, LY_ARRAY_SIZE(notif));
911 assert_int_equal(LYS_NOTIF, notif->nodetype);
912 assert_int_equal(LYS_STATUS_CURR, notif->flags);
913 assert_string_equal("b1", notif->name);
914 assert_non_null(notif->data);
915 assert_string_equal("x", notif->data->name);
916 assert_int_equal(LYS_NOTIF, notif[1].nodetype);
917 assert_int_equal(LYS_STATUS_CURR, notif[1].flags);
918 assert_string_equal("b2", notif[1].name);
919 assert_null(notif[1].data);
920
921 /* invalid */
922 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {notification x;}}", LYS_IN_YANG));
923 logbuf_assert("Invalid keyword \"notification\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
924
925 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} notification x;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200926 logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement. /bb:x");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200927 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));
Radek Krejci327de162019-06-14 12:52:07 +0200928 logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement. /cc:c/y");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200929 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));
Radek Krejci327de162019-06-14 12:52:07 +0200930 logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement. /dd:c/z");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200931 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}");
932 assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; notification w;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200933 logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement. /ee:w");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200934
935 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;}}}}"
936 "augment /test/input/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200937 logbuf_assert("Notification \"invalid\" is placed inside RPC/action. /ff:{augment='/test/input/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200938
939 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;}}}"
940 "augment /test/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200941 logbuf_assert("Notification \"invalid\" is placed inside another Notification. /gg:{augment='/test/a'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200942
943 assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; rpc test {input {container a {uses grp;}}}"
944 "grouping grp {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +0200945 logbuf_assert("Notification \"invalid\" is placed inside RPC/action. /hh:test/input/a/{uses='grp'}/invalid");
Radek Krejcifc11bd72019-04-11 16:00:05 +0200946
Radek Krejcif538ce52019-03-05 10:46:14 +0100947 *state = NULL;
948 ly_ctx_destroy(ctx, NULL);
949}
950
Radek Krejci0f07bed2018-11-13 14:01:40 +0100951/**
952 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
953 * do not test the expression itself
954 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100955static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100956test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100957{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100958 *state = test_type_range;
959
960 struct ly_ctx *ctx;
961 struct lys_module *mod;
962 struct lysc_type *type;
963
964 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
965
966 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 +0100967 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
968 assert_non_null(type);
969 assert_int_equal(LY_TYPE_INT8, 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(-128, ((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(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
976 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
977
978 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 +0100979 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
980 assert_non_null(type);
981 assert_int_equal(LY_TYPE_INT16, 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(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
986 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
987 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
988 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
989
990 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 +0100991 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
992 assert_non_null(type);
993 assert_int_equal(LY_TYPE_INT32, 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(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
998 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
999 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
1000 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
1001
1002 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 +01001003 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1004 assert_non_null(type);
1005 assert_int_equal(LY_TYPE_INT64, 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(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
1010 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
1011 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
1012 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
1013
1014 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 +01001015 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1016 assert_non_null(type);
1017 assert_int_equal(LY_TYPE_UINT8, 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(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1024 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1025
1026 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 +01001027 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1028 assert_non_null(type);
1029 assert_int_equal(LY_TYPE_UINT16, type->basetype);
1030 assert_non_null(((struct lysc_type_num*)type)->range);
1031 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1032 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1033 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1034 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1035 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1036 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1037
1038 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 +01001039 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1040 assert_non_null(type);
1041 assert_int_equal(LY_TYPE_UINT32, type->basetype);
1042 assert_non_null(((struct lysc_type_num*)type)->range);
1043 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1044 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1045 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1046 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1047 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1048 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1049
1050 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 +01001051 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1052 assert_non_null(type);
1053 assert_int_equal(LY_TYPE_UINT64, type->basetype);
1054 assert_non_null(((struct lysc_type_num*)type)->range);
1055 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1056 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1057 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1058 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1059 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
1060 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
1061
1062 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
1063 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +01001064 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1065 assert_non_null(type);
1066 assert_int_equal(3, type->refcount);
1067 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1068 assert_non_null(((struct lysc_type_num*)type)->range);
1069 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1070 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1071
Radek Krejcif1394042019-01-08 10:53:34 +01001072 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
1073 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
1074 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +01001075 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1076 assert_non_null(type);
1077 assert_int_equal(1, type->refcount);
1078 assert_int_equal(LY_TYPE_UINT8, type->basetype);
1079 assert_non_null(((struct lysc_type_num*)type)->range);
1080 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
1081 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
1082 assert_non_null(((struct lysc_type_num*)type)->range->parts);
1083 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
1084 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
1085 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
1086
Radek Krejci0f07bed2018-11-13 14:01:40 +01001087 *state = NULL;
1088 ly_ctx_destroy(ctx, NULL);
1089}
1090
1091static void
1092test_type_length(void **state)
1093{
1094 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +01001095
1096 struct ly_ctx *ctx;
1097 struct lys_module *mod;
1098 struct lysc_type *type;
1099
1100 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1101
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001102 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 +01001103 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1104 assert_non_null(type);
1105 assert_non_null(((struct lysc_type_bin*)type)->length);
1106 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +01001107 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
1108 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001109 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1110 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1111 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1112
1113 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 +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(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +01001119 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1120 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001121
1122 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 +01001123 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1124 assert_non_null(type);
1125 assert_non_null(((struct lysc_type_bin*)type)->length);
1126 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1127 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1128 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +01001129 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001130
1131 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 +01001132 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1133 assert_non_null(type);
1134 assert_non_null(((struct lysc_type_bin*)type)->length);
1135 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1136 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1137 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1138 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1139
1140 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 +01001141 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1142 assert_non_null(type);
1143 assert_non_null(((struct lysc_type_bin*)type)->length);
1144 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1145 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1146 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1147 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1148
1149 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 +01001150 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1151 assert_non_null(type);
1152 assert_non_null(((struct lysc_type_bin*)type)->length);
1153 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1154 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1155 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1156 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1157 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1158 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1159
1160 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 +01001161 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1162 assert_non_null(type);
1163 assert_non_null(((struct lysc_type_bin*)type)->length);
1164 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1165 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1166 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1167 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1168 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1169 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1170
Radek Krejcib31c8992018-11-12 16:29:16 +01001171 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1172 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001173 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1174 assert_non_null(type);
1175 assert_non_null(((struct lysc_type_bin*)type)->length);
1176 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1177 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1178 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1179 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1180
1181 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1182 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001183 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1184 assert_non_null(type);
1185 assert_non_null(((struct lysc_type_bin*)type)->length);
1186 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1187 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1188 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1189 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1190
1191 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1192 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001193 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1194 assert_non_null(type);
1195 assert_non_null(((struct lysc_type_bin*)type)->length);
1196 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1197 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1198 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1199 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1200 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1201 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1202
1203 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 +01001204 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001205 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1206 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001207 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001208 assert_non_null(((struct lysc_type_bin*)type)->length);
1209 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1210 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1211 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001212 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001213 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1214 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001215 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001216 assert_non_null(((struct lysc_type_bin*)type)->length);
1217 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1218 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1219 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1220 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1221
Radek Krejci56aa27c2018-11-13 14:21:59 +01001222 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1223 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001224 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1225 assert_non_null(type);
1226 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001227 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001228 assert_non_null(((struct lysc_type_str*)type)->length);
1229 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1230 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1231 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1232 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001233
Radek Krejci5969f272018-11-23 10:03:58 +01001234 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1235 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001236 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1237 assert_non_null(type);
1238 assert_int_equal(LY_TYPE_STRING, type->basetype);
1239 assert_int_equal(1, type->refcount);
1240 assert_non_null(((struct lysc_type_str*)type)->length);
1241 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1242 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1243 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1244 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1245
Radek Krejci4f28eda2018-11-12 11:46:16 +01001246 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001247 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001248 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations. /aa:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001249 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001250 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\". /bb:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001251 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001252 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10). /cc:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001253 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001254 logbuf_assert("Invalid length restriction - values are not in ascending order (10). /dd:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001255 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001256 logbuf_assert("Invalid length restriction - values are not in ascending order (10). /ee:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001257 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001258 logbuf_assert("Invalid length restriction - unexpected data (x). /ff:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001259 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001260 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ). /gg:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001261 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001262 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50). /hh:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001263 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001264 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..). /ii:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001265 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001266 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound. /jj:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001267 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001268 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |). /kk:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001269 assert_null(lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001270 logbuf_assert("Invalid length restriction - values are not in ascending order (15). /kl:l");
Radek Krejcib31c8992018-11-12 16:29:16 +01001271
Radek Krejci096235c2019-01-11 11:12:19 +01001272 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1273 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001274 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting. /ll:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001275 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1276 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001277 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting. /mm:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001278 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1279 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001280 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting. /nn:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001281 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1282 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001283 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting. /oo:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001284 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 +01001285 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001286 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting. /pp:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001287 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 +01001288 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001289 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting. /qq:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001290 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 +01001291 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001292 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting. /rr:l");
Radek Krejcib31c8992018-11-12 16:29:16 +01001293
Radek Krejci327de162019-06-14 12:52:07 +02001294 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
1295 logbuf_assert("Invalid type restrictions for binary type. /ss:l");
Radek Krejci495903e2018-11-13 11:30:45 +01001296
Radek Krejci4f28eda2018-11-12 11:46:16 +01001297 *state = NULL;
1298 ly_ctx_destroy(ctx, NULL);
1299}
1300
Radek Krejcicda74152018-11-13 15:27:32 +01001301static void
1302test_type_pattern(void **state)
1303{
1304 *state = test_type_pattern;
1305
1306 struct ly_ctx *ctx;
1307 struct lys_module *mod;
1308 struct lysc_type *type;
1309
1310 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1311
Radek Krejci027d5802018-11-14 16:57:28 +01001312 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 +01001313 "pattern .* {error-app-tag errortag;error-message error;}"
1314 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001315 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1316 assert_non_null(type);
1317 assert_non_null(((struct lysc_type_str*)type)->patterns);
1318 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1319 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1320 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001321 assert_string_equal(".*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001322 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1323 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1324 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
Radek Krejci54579462019-04-30 12:47:06 +02001325 assert_string_equal("[0-9].*[0-9]", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001326 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1327
1328 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1329 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001330 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1331 assert_non_null(type);
1332 assert_int_equal(LY_TYPE_STRING, type->basetype);
1333 assert_int_equal(1, type->refcount);
1334 assert_non_null(((struct lysc_type_str*)type)->patterns);
1335 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001336 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001337 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
Radek Krejci54579462019-04-30 12:47:06 +02001338 assert_string_equal("[0-4]*", ((struct lysc_type_str*)type)->patterns[1]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001339 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1340
Radek Krejci04bc1e92018-11-14 14:28:13 +01001341 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1342 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001343 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1344 assert_non_null(type);
1345 assert_int_equal(LY_TYPE_STRING, type->basetype);
1346 assert_int_equal(1, type->refcount);
1347 assert_non_null(((struct lysc_type_str*)type)->patterns);
1348 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001349 assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejci04bc1e92018-11-14 14:28:13 +01001350 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1351
Radek Krejcicda74152018-11-13 15:27:32 +01001352 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001353 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 +01001354 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001355 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1356 assert_non_null(type);
1357 assert_non_null(((struct lysc_type_str*)type)->patterns);
1358 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
Radek Krejci54579462019-04-30 12:47:06 +02001359 assert_string_equal("^\\p{IsLatinExtended-A}$", ((struct lysc_type_str*)type)->patterns[0]->expr);
Radek Krejcicda74152018-11-13 15:27:32 +01001360 /* TODO check some data "^Å™$" */
1361
1362 *state = NULL;
1363 ly_ctx_destroy(ctx, NULL);
1364}
1365
Radek Krejci8b764662018-11-14 14:15:13 +01001366static void
1367test_type_enum(void **state)
1368{
1369 *state = test_type_enum;
1370
1371 struct ly_ctx *ctx;
1372 struct lys_module *mod;
1373 struct lysc_type *type;
1374
1375 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1376
Radek Krejci027d5802018-11-14 16:57:28 +01001377 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 +01001378 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1379 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001380 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1381 assert_non_null(type);
1382 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1383 assert_non_null(((struct lysc_type_enum*)type)->enums);
1384 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1385 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1386 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1387 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1388 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1389 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1390 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1391 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1392 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1393 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1394 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1395 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1396 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1397 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1398
Radek Krejci027d5802018-11-14 16:57:28 +01001399 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 {"
1400 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001401 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1402 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001403 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1404 assert_non_null(type);
1405 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1406 assert_non_null(((struct lysc_type_enum*)type)->enums);
1407 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1408 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1409 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1410 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1411 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1412
1413
1414 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001415 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1416 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1417 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 +01001418 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1419 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1420 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1421 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1422 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1423 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1424 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1425 "enum one; enum one;}}}", LYS_IN_YANG));
1426 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1427 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1428 "enum '';}}}", LYS_IN_YANG));
1429 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1430 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1431 "enum ' x';}}}", LYS_IN_YANG));
1432 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1433 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1434 "enum 'x ';}}}", LYS_IN_YANG));
1435 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1436 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1437 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1438 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1439
Radek Krejci096235c2019-01-11 11:12:19 +01001440 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001441 logbuf_assert("Missing enum substatement for enumeration type. /bb:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001442
Radek Krejci096235c2019-01-11 11:12:19 +01001443 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 +01001444 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001445 logbuf_assert("Invalid enumeration - derived type adds new item \"two\". /cc:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001446
Radek Krejci096235c2019-01-11 11:12:19 +01001447 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 +01001448 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001449 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type. /dd:l");
Radek Krejci096235c2019-01-11 11:12:19 +01001450 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 +01001451 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001452 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647. /ee:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001453
Radek Krejci096235c2019-01-11 11:12:19 +01001454 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 +01001455 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001456 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\". /ff:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001457
Radek Krejci096235c2019-01-11 11:12:19 +01001458 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001459 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001460 logbuf_assert("Missing enum substatement for enumeration type mytype. /gg:l");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001461
Radek Krejci096235c2019-01-11 11:12:19 +01001462 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 +01001463 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001464 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules. /hh:l");
Radek Krejci027d5802018-11-14 16:57:28 +01001465
Radek Krejci8b764662018-11-14 14:15:13 +01001466 *state = NULL;
1467 ly_ctx_destroy(ctx, NULL);
1468}
1469
1470static void
1471test_type_bits(void **state)
1472{
1473 *state = test_type_bits;
1474
1475 struct ly_ctx *ctx;
1476 struct lys_module *mod;
1477 struct lysc_type *type;
1478
1479 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1480
Radek Krejci027d5802018-11-14 16:57:28 +01001481 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 +01001482 "bit automin; bit one {if-feature f; position 1;}"
1483 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001484 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1485 assert_non_null(type);
1486 assert_int_equal(LY_TYPE_BITS, type->basetype);
1487 assert_non_null(((struct lysc_type_bits*)type)->bits);
1488 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1489 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1490 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1491 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1492 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1493 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1494 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1495 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1496 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1497 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1498 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1499 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1500
Radek Krejci027d5802018-11-14 16:57:28 +01001501 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 {"
1502 "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 +01001503 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001504 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1505 assert_non_null(type);
1506 assert_int_equal(LY_TYPE_BITS, type->basetype);
1507 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001508 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1509 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1510 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1511 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1512 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1513 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1514 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001515
1516
1517 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001518 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1519 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1520 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 +01001521 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1522 "bit one {position -1;}}}}", LYS_IN_YANG));
1523 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1524 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1525 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1526 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1527 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1528 "bit one; bit one;}}}", LYS_IN_YANG));
1529 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1530 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1531 "bit '11';}}}", LYS_IN_YANG));
1532 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1533 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1534 "bit 'x1$1';}}}", LYS_IN_YANG));
1535 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1536
Radek Krejci096235c2019-01-11 11:12:19 +01001537 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001538 logbuf_assert("Missing bit substatement for bits type. /bb:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001539
Radek Krejci096235c2019-01-11 11:12:19 +01001540 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 +01001541 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001542 logbuf_assert("Invalid bits - derived type adds new item \"two\". /cc:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001543
Radek Krejci096235c2019-01-11 11:12:19 +01001544 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 +01001545 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001546 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type. /dd:l");
1547 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}", LYS_IN_YANG));
1548 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295. /ee:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001549
Radek Krejci327de162019-06-14 12:52:07 +02001550 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;}}}}", LYS_IN_YANG));
1551 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\". /ff:l");
Radek Krejci8b764662018-11-14 14:15:13 +01001552
Radek Krejci096235c2019-01-11 11:12:19 +01001553 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001554 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001555 logbuf_assert("Missing bit substatement for bits type mytype. /gg:l");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001556
Radek Krejci096235c2019-01-11 11:12:19 +01001557 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 +01001558 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001559 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules. /hh:l");
Radek Krejci027d5802018-11-14 16:57:28 +01001560
Radek Krejci8b764662018-11-14 14:15:13 +01001561 *state = NULL;
1562 ly_ctx_destroy(ctx, NULL);
1563}
1564
Radek Krejci6cba4292018-11-15 17:33:29 +01001565static void
1566test_type_dec64(void **state)
1567{
1568 *state = test_type_dec64;
1569
1570 struct ly_ctx *ctx;
1571 struct lys_module *mod;
1572 struct lysc_type *type;
1573
1574 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1575
1576 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1577 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001578 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1579 assert_non_null(type);
1580 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1581 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1582 assert_non_null(((struct lysc_type_dec*)type)->range);
1583 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1584 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1585 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1586 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1587
1588 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1589 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001590 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1591 assert_non_null(type);
1592 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1593 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1594 assert_non_null(((struct lysc_type_dec*)type)->range);
1595 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1596 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1597 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1598 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1599 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1600 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1601 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1602 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1603
Radek Krejci943177f2019-06-14 16:32:43 +02001604 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type decimal64 {"
1605 "fraction-digits 2;range '1 .. 65535';}}leaf l {type mytype;}}", LYS_IN_YANG));
1606 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1607 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1608 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1609 assert_non_null(((struct lysc_type_dec*)type)->range);
1610 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1611 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1612 assert_int_equal(100, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1613 assert_int_equal(6553500, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1614
Radek Krejci6cba4292018-11-15 17:33:29 +01001615 /* invalid cases */
1616 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1617 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1618 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1619 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1620 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1621 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1622
Radek Krejci096235c2019-01-11 11:12:19 +01001623 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001624 logbuf_assert("Missing fraction-digits substatement for decimal64 type. /aa:l");
Radek Krejci6cba4292018-11-15 17:33:29 +01001625
Radek Krejci096235c2019-01-11 11:12:19 +01001626 assert_null(lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001627 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype. /ab:l");
Radek Krejci643c8242018-11-15 17:51:11 +01001628
Radek Krejci096235c2019-01-11 11:12:19 +01001629 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 +01001630 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001631 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits. /bb:l");
Radek Krejci6cba4292018-11-15 17:33:29 +01001632
Radek Krejci096235c2019-01-11 11:12:19 +01001633 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 +01001634 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001635 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14). /cc:l");
Radek Krejci6cba4292018-11-15 17:33:29 +01001636
Radek Krejci096235c2019-01-11 11:12:19 +01001637 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 +01001638 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001639 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type. /dd:l");
Radek Krejci643c8242018-11-15 17:51:11 +01001640
Radek Krejci096235c2019-01-11 11:12:19 +01001641 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 +01001642 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001643 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type. /de:l");
Radek Krejci643c8242018-11-15 17:51:11 +01001644
Radek Krejci943177f2019-06-14 16:32:43 +02001645 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:c;prefix c;typedef mytype {type decimal64 {"
1646 "fraction-digits 18;range '-10 .. 0';}}leaf l {type mytype;}}", LYS_IN_YANG));
1647 logbuf_assert("Invalid range restriction - invalid value \"-10000000000000000000\". /ee:l");
1648 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:c;prefix c;typedef mytype {type decimal64 {"
1649 "fraction-digits 18;range '0 .. 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1650 logbuf_assert("Invalid range restriction - invalid value \"10000000000000000000\". /ee:l");
1651
Radek Krejci6cba4292018-11-15 17:33:29 +01001652 *state = NULL;
1653 ly_ctx_destroy(ctx, NULL);
1654}
1655
Radek Krejci16c0f822018-11-16 10:46:10 +01001656static void
1657test_type_instanceid(void **state)
1658{
1659 *state = test_type_instanceid;
1660
1661 struct ly_ctx *ctx;
1662 struct lys_module *mod;
1663 struct lysc_type *type;
1664
1665 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1666
1667 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1668 "leaf l1 {type instance-identifier {require-instance true;}}"
1669 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001670 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1671 assert_non_null(type);
1672 assert_int_equal(LY_TYPE_INST, type->basetype);
1673 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1674
1675 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1676 assert_non_null(type);
1677 assert_int_equal(LY_TYPE_INST, type->basetype);
1678 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1679
1680 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1681 assert_non_null(type);
1682 assert_int_equal(LY_TYPE_INST, type->basetype);
1683 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1684
1685 /* invalid cases */
1686 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1687 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1688
Radek Krejci096235c2019-01-11 11:12:19 +01001689 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001690 logbuf_assert("Invalid type restrictions for instance-identifier type. /aa:l");
Radek Krejci16c0f822018-11-16 10:46:10 +01001691
1692 *state = NULL;
1693 ly_ctx_destroy(ctx, NULL);
1694}
1695
Radek Krejci555cb5b2018-11-16 14:54:33 +01001696static void
1697test_type_identityref(void **state)
1698{
1699 *state = test_type_identityref;
1700
1701 struct ly_ctx *ctx;
1702 struct lys_module *mod;
1703 struct lysc_type *type;
1704
1705 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1706
1707 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;}"
1708 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001709 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001710 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1711 assert_non_null(type);
1712 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1713 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1714 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1715 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1716
1717 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1718 assert_non_null(type);
1719 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1720 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1721 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1722 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1723 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1724
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001725 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1726 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001727 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1728 assert_non_null(type);
1729 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1730 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1731 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1732 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1733 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1734
Radek Krejci555cb5b2018-11-16 14:54:33 +01001735 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001736 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001737 logbuf_assert("Missing base substatement for identityref type. /aa:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001738
Radek Krejci096235c2019-01-11 11:12:19 +01001739 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001740 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001741 logbuf_assert("Missing base substatement for identityref type mytype. /bb:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001742
Radek Krejci096235c2019-01-11 11:12:19 +01001743 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 +01001744 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001745 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type. /cc:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001746
Radek Krejci096235c2019-01-11 11:12:19 +01001747 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 +01001748 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001749 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type. /dd:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001750
Radek Krejci096235c2019-01-11 11:12:19 +01001751 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001752 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001753 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules. /ee:l");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001754
Radek Krejci096235c2019-01-11 11:12:19 +01001755 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001756 logbuf_assert("Unable to find base (j) of identityref. /ff:l");
Radek Krejci322614f2018-11-16 15:05:44 +01001757
Radek Krejci096235c2019-01-11 11:12:19 +01001758 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001759 logbuf_assert("Invalid prefix used for base (x:j) of identityref. /gg:l");
Radek Krejci322614f2018-11-16 15:05:44 +01001760
Radek Krejci555cb5b2018-11-16 14:54:33 +01001761 *state = NULL;
1762 ly_ctx_destroy(ctx, NULL);
1763}
1764
Radek Krejcia3045382018-11-22 14:30:31 +01001765static void
1766test_type_leafref(void **state)
1767{
1768 *state = test_type_leafref;
1769
1770 struct ly_ctx *ctx;
1771 struct lys_module *mod;
1772 struct lysc_type *type;
1773 const char *path, *name, *prefix;
1774 size_t prefix_len, name_len;
1775 int parent_times, has_predicate;
1776
1777 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1778
1779 /* lys_path_token() */
1780 path = "invalid_path";
1781 parent_times = 0;
1782 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1783 path = "..";
1784 parent_times = 0;
1785 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1786 path = "..[";
1787 parent_times = 0;
1788 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1789 path = "../";
1790 parent_times = 0;
1791 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1792 path = "/";
1793 parent_times = 0;
1794 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1795
1796 path = "../../pref:id/xxx[predicate]/invalid!!!";
1797 parent_times = 0;
1798 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1799 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1800 assert_int_equal(4, prefix_len);
1801 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1802 assert_int_equal(2, name_len);
1803 assert_int_equal(0, strncmp("id", name, name_len));
1804 assert_int_equal(2, parent_times);
1805 assert_int_equal(0, has_predicate);
1806 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1807 assert_string_equal("[predicate]/invalid!!!", path);
1808 assert_int_equal(0, prefix_len);
1809 assert_null(prefix);
1810 assert_int_equal(3, name_len);
1811 assert_int_equal(0, strncmp("xxx", name, name_len));
1812 assert_int_equal(1, has_predicate);
1813 path += 11;
1814 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1815 assert_string_equal("!!!", path);
1816 assert_int_equal(0, prefix_len);
1817 assert_null(prefix);
1818 assert_int_equal(7, name_len);
1819 assert_int_equal(0, strncmp("invalid", name, name_len));
1820
1821 path = "/absolute/prefix:path";
1822 parent_times = 0;
1823 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1824 assert_string_equal("/prefix:path", path);
1825 assert_int_equal(0, prefix_len);
1826 assert_null(prefix);
1827 assert_int_equal(8, name_len);
1828 assert_int_equal(0, strncmp("absolute", name, name_len));
1829 assert_int_equal(-1, parent_times);
1830 assert_int_equal(0, has_predicate);
1831 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1832 assert_int_equal(0, *path);
1833 assert_int_equal(6, prefix_len);
1834 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1835 assert_int_equal(4, name_len);
1836 assert_int_equal(0, strncmp("path", name, name_len));
1837 assert_int_equal(0, has_predicate);
1838
1839 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001840 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 +01001841 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1842 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001843 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1844 assert_non_null(type);
1845 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1846 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001847 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001848 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1849 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001850 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1851 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1852 assert_non_null(type);
1853 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1854 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001855 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1856 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1857 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001858 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1859
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001860 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 +01001861 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001862 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001863 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1864 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001865 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001866 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1867 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1868 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001869 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1870 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001871 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1872
1873 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001874 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 +01001875 "typedef mytype3 {type c:mytype {require-instance false;}}"
1876 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001877 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1878 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001879 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001880 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1881 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1882 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001883 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1884 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001885 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1886 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1887 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001888 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001889 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1890 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1891 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1892 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1893
Radek Krejci4ce68932018-11-28 12:53:36 +01001894 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1895 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1896 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001897 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1898 assert_non_null(type);
1899 assert_int_equal(1, type->refcount);
1900 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1901 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1902 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1903 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1904 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1905 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1906
Radek Krejci58d171e2018-11-23 13:50:55 +01001907 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001908 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 +01001909 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1910 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001911 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1912 assert_non_null(type);
1913 assert_int_equal(1, type->refcount);
1914 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1915 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1916 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1917 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1918 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1919
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001920 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1921 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1922 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001923 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001924 LYS_IN_YANG));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01001925 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001926 assert_non_null(type);
1927 assert_int_equal(1, type->refcount);
1928 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001929 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001930 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1931 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1932 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001933
Radek Krejci20e8a182018-12-07 11:43:21 +01001934 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1935 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1936 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1937 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001938 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1939 assert_non_null(type);
1940 assert_int_equal(1, type->refcount);
1941 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1942 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1943 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1944 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1945 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1946
Radek Krejci3d929562019-02-21 11:25:55 +01001947 /* leafref to imported (not yet implemented) module */
1948 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}");
1949 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}"
1950 "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG));
1951 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1952 assert_non_null(type);
1953 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1954 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1955 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1956 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h"));
1957 assert_int_equal(1, mod->implemented);
1958 assert_non_null(mod->compiled->data);
1959 assert_string_equal("h", mod->compiled->data->name);
1960
Radek Krejci92cc8512019-04-25 09:57:06 +02001961 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module j {namespace urn:j;prefix j; leaf j {type string;}}");
1962 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;import j {prefix j;}"
1963 "leaf i {type leafref {path \"/ilist[name = current()/../j:j]/value\";}}"
1964 "list ilist {key name; leaf name {type string;} leaf value {type uint16;}}}", LYS_IN_YANG));
1965 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1966 assert_non_null(type);
1967 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1968 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1969 assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype);
1970 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "j"));
1971 assert_int_equal(1, mod->implemented);
1972 assert_non_null(mod->compiled->data);
1973 assert_string_equal("j", mod->compiled->data->name);
1974
Radek Krejcia3045382018-11-22 14:30:31 +01001975 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001976 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 +01001977 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001978 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\". /aa:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001979 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 +01001980 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001981 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path. /bb:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001982 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 +01001983 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001984 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\". /cc:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001985 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 +01001986 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001987 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container. /dd:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001988 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 +01001989 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001990 logbuf_assert("Invalid leafref path at character 3 (/a!invalid). /ee:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001991 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 +01001992 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001993 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list. /ff:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001994 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 +01001995 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001996 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\". /gg:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01001997 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001998 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02001999 logbuf_assert("Missing path substatement for leafref type. /hh:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002000 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01002001 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002002 logbuf_assert("Missing path substatement for leafref type mytype. /ii:ref1");
Radek Krejci096235c2019-01-11 11:12:19 +01002003 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01002004 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01002005 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
Radek Krejci327de162019-06-14 12:52:07 +02002006 "applicable to the leafref itself. /jj:ref");
Radek Krejci096235c2019-01-11 11:12:19 +01002007 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002008 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002009 logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not. /kk:ref");
Radek Krejci58d171e2018-11-23 13:50:55 +01002010
Radek Krejci096235c2019-01-11 11:12:19 +01002011 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002012 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002013 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules. /ll:ref");
Radek Krejci096235c2019-01-11 11:12:19 +01002014 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 +01002015 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002016 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules. /mm:ref");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002017
Radek Krejci096235c2019-01-11 11:12:19 +01002018 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002019 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2020 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2021 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
2022 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002023 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier. /nn:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002024
Radek Krejci096235c2019-01-11 11:12:19 +01002025 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002026 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2027 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2028 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
2029 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002030 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination. /oo:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002031
Radek Krejci096235c2019-01-11 11:12:19 +01002032 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002033 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2034 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2035 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
2036 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002037 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\". /pp:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002038
Radek Krejci096235c2019-01-11 11:12:19 +01002039 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002040 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2041 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2042 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
2043 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002044 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found. /qq:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002045
Radek Krejci096235c2019-01-11 11:12:19 +01002046 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002047 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2048 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2049 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
2050 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002051 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\". /rr:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002052
Radek Krejci096235c2019-01-11 11:12:19 +01002053 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002054 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2055 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2056 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
2057 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002058 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation. /ss:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002059
Radek Krejci096235c2019-01-11 11:12:19 +01002060 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002061 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2062 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2063 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
2064 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002065 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation. /tt:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002066
Radek Krejci096235c2019-01-11 11:12:19 +01002067 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002068 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2069 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2070 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
2071 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002072 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern. /uu:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002073
Radek Krejci096235c2019-01-11 11:12:19 +01002074 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002075 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2076 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2077 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
2078 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002079 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr. /vv:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002080
Radek Krejci096235c2019-01-11 11:12:19 +01002081 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002082 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2083 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2084 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
2085 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002086 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr. /ww:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002087
Radek Krejci096235c2019-01-11 11:12:19 +01002088 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002089 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2090 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
2091 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
2092 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002093 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]). /xx:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002094
Radek Krejci096235c2019-01-11 11:12:19 +01002095 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002096 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2097 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2098 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
2099 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002100 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path-keyexpr. /yy:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002101
Radek Krejci096235c2019-01-11 11:12:19 +01002102 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002103 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2104 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2105 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
2106 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002107 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path-keyexpr. /zz:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002108
Radek Krejci096235c2019-01-11 11:12:19 +01002109 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002110 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2111 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
2112 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
2113 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002114 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path-keyexpr \"current()/../c\" refers container instead of leaf. /zza:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002115
Radek Krejci096235c2019-01-11 11:12:19 +01002116 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01002117 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
2118 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2119 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
2120 LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002121 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found. /zzb:address");
Radek Krejcibade82a2018-12-05 10:13:48 +01002122
Radek Krejci412ddfa2018-11-23 11:44:11 +01002123 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01002124 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01002125 "leaf ref1 {type leafref {path /ref2;}}"
2126 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01002127 "leaf ref3 {type leafref {path /ref4;}}"
2128 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002129 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected. /aaa:ref4");
Radek Krejci412ddfa2018-11-23 11:44:11 +01002130
Radek Krejcia3045382018-11-22 14:30:31 +01002131 *state = NULL;
2132 ly_ctx_destroy(ctx, NULL);
2133}
2134
Radek Krejci43699232018-11-23 14:59:46 +01002135static void
2136test_type_empty(void **state)
2137{
2138 *state = test_type_empty;
2139
2140 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01002141
2142 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2143
2144 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002145 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01002146 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +02002147 logbuf_assert("Invalid leaf's default value \"x\" which does not fit the type (Invalid empty value \"x\".). /aa:l");
Radek Krejci43699232018-11-23 14:59:46 +01002148
Radek Krejci096235c2019-01-11 11:12:19 +01002149 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 +01002150 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002151 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x). /bb:l");
Radek Krejci43699232018-11-23 14:59:46 +01002152
2153 *state = NULL;
2154 ly_ctx_destroy(ctx, NULL);
2155}
2156
Radek Krejcicdfecd92018-11-26 11:27:32 +01002157
2158static void
2159test_type_union(void **state)
2160{
2161 *state = test_type_union;
2162
2163 struct ly_ctx *ctx;
2164 struct lys_module *mod;
2165 struct lysc_type *type;
2166
2167 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2168
2169 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2170 "typedef mytype {type union {type int8; type mybasetype;}}"
2171 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002172 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2173 assert_non_null(type);
2174 assert_int_equal(2, type->refcount);
2175 assert_int_equal(LY_TYPE_UNION, type->basetype);
2176 assert_non_null(((struct lysc_type_union*)type)->types);
2177 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2178 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2179 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2180
2181 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2182 "typedef mytype {type union {type int8; type mybasetype;}}"
2183 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002184 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2185 assert_non_null(type);
2186 assert_int_equal(1, type->refcount);
2187 assert_int_equal(LY_TYPE_UNION, type->basetype);
2188 assert_non_null(((struct lysc_type_union*)type)->types);
2189 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2190 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2191 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2192 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2193
2194 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2195 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002196 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2197 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002198 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002199 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2200 assert_non_null(type);
2201 assert_int_equal(1, type->refcount);
2202 assert_int_equal(LY_TYPE_UNION, type->basetype);
2203 assert_non_null(((struct lysc_type_union*)type)->types);
2204 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2205 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2206 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2207 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2208 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2209 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2210
Radek Krejci6be5ff12018-11-26 13:18:15 +01002211 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01002212 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002213 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002214 logbuf_assert("Missing type substatement for union type mytype. /aa:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002215
Radek Krejci096235c2019-01-11 11:12:19 +01002216 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002217 logbuf_assert("Missing type substatement for union type. /bb:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002218
Radek Krejci096235c2019-01-11 11:12:19 +01002219 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 +01002220 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002221 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type. /cc:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002222
Radek Krejci096235c2019-01-11 11:12:19 +01002223 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 +01002224 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002225 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type. /dd:l");
Radek Krejci6be5ff12018-11-26 13:18:15 +01002226
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002227 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;typedef mytype {type union{type mytype; type string;}}"
2228 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002229 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected. /ee:l");
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002230 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;typedef mytype {type mytype2;}"
2231 "typedef mytype2 {type mytype;} leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002232 logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected. /ef:l");
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002233
Radek Krejcicdfecd92018-11-26 11:27:32 +01002234 *state = NULL;
2235 ly_ctx_destroy(ctx, NULL);
2236}
2237
2238static void
2239test_type_dflt(void **state)
2240{
2241 *state = test_type_union;
2242
2243 struct ly_ctx *ctx;
2244 struct lys_module *mod;
2245 struct lysc_type *type;
Radek Krejcia1911222019-07-22 17:24:50 +02002246 struct lysc_node_leaf *leaf;
2247 int dynamic;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002248
2249 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2250
2251 /* default is not inherited from union's types */
2252 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2253 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002254 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2255 assert_non_null(type);
2256 assert_int_equal(1, type->refcount);
2257 assert_int_equal(LY_TYPE_UNION, type->basetype);
2258 assert_non_null(((struct lysc_type_union*)type)->types);
2259 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2260 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2261 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2262 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2263 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2264
2265 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2266 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002267 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2268 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002269 assert_int_equal(3, type->refcount); /* 2x type reference, 1x default value's reference (typedf's default does not reference own type)*/
Radek Krejcicdfecd92018-11-26 11:27:32 +01002270 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002271 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2272 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2273 assert_int_equal(0, dynamic);
2274 assert_string_equal("xxx", leaf->units);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002275
2276 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2277 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002278 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2279 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002280 assert_int_equal(3, type->refcount); /* 2x type reference, 1x default value's reference */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002281 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002282 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2283 assert_string_equal("goodbye", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2284 assert_int_equal(0, dynamic);
2285 assert_string_equal("yyy", leaf->units);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002286
Radek Krejci6be5ff12018-11-26 13:18:15 +01002287 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2288 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2289 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002290 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2291 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002292 assert_int_equal(6, type->refcount); /* 4x type reference, 2x default value's reference (1 shared compiled type of typedefs which default does not reference own type) */
Radek Krejci6be5ff12018-11-26 13:18:15 +01002293 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002294 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2295 assert_string_equal("goodbye", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2296 assert_int_equal(0, dynamic);
2297 assert_string_equal("yyy", leaf->units);
Radek Krejci6be5ff12018-11-26 13:18:15 +01002298 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2299 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002300 assert_int_equal(6, type->refcount); /* 4x type reference, 2x default value's reference (1 shared compiled type of typedefs which default does not reference own type) */
Radek Krejci6be5ff12018-11-26 13:18:15 +01002301 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002302 leaf = (struct lysc_node_leaf*)mod->compiled->data->next;
2303 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2304 assert_int_equal(0, dynamic);
2305 assert_string_equal("xxx", leaf->units);
Radek Krejci6be5ff12018-11-26 13:18:15 +01002306
2307 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2308 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002309 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2310 assert_non_null(type);
Radek Krejcia1911222019-07-22 17:24:50 +02002311 assert_int_equal(4, type->refcount); /* 3x type reference, 1x default value's reference (typedef's default does not reference own type) */
Radek Krejci6be5ff12018-11-26 13:18:15 +01002312 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejcia1911222019-07-22 17:24:50 +02002313 leaf = (struct lysc_node_leaf*)mod->compiled->data;
2314 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2315 assert_int_equal(0, dynamic);
2316 assert_string_equal("xxx", leaf->units);
Radek Krejci6be5ff12018-11-26 13:18:15 +01002317
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002318 /* mandatory leaf does not takes default value from type */
2319 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2320 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002321 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2322 assert_non_null(type);
2323 assert_int_equal(LY_TYPE_STRING, type->basetype);
2324 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2325 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2326
Radek Krejcicdfecd92018-11-26 11:27:32 +01002327 *state = NULL;
2328 ly_ctx_destroy(ctx, NULL);
2329}
2330
Radek Krejci665421c2018-12-05 08:03:39 +01002331static void
2332test_status(void **state)
2333{
2334 *state = test_status;
2335
2336 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002337
2338 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2339
Radek Krejci096235c2019-01-11 11:12:19 +01002340 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002341 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002342 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status. /aa:c/l");
Radek Krejci665421c2018-12-05 08:03:39 +01002343
Radek Krejci096235c2019-01-11 11:12:19 +01002344 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002345 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002346 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status. /bb:c/l");
Radek Krejci665421c2018-12-05 08:03:39 +01002347
Radek Krejci096235c2019-01-11 11:12:19 +01002348 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002349 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002350 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status. /cc:c/l");
Radek Krejci665421c2018-12-05 08:03:39 +01002351
2352 *state = NULL;
2353 ly_ctx_destroy(ctx, NULL);
2354}
2355
Radek Krejcie86bf772018-12-14 11:39:53 +01002356static void
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002357test_grouping(void **state)
2358{
2359 *state = test_grouping;
2360
2361 struct ly_ctx *ctx;
2362
2363 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2364
2365 /* result ok, but a warning about not used locally scoped grouping printed */
2366 assert_non_null(lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; grouping grp1 {leaf a1 {type string;}}"
2367 "container a {leaf x {type string;} grouping grp2 {leaf a2 {type string;}}}}", LYS_IN_YANG));
2368 logbuf_assert("Locally scoped grouping \"grp2\" not used.");
2369 logbuf_clean();
2370
2371 /* result ok - when statement or leafref target must be checked only at the place where the grouping is really instantiated */
2372 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; grouping grp {"
2373 "leaf ref {type leafref {path \"../name\";}}"
2374 "leaf cond {type string; when \"../name = 'specialone'\";}}}", LYS_IN_YANG));
2375 logbuf_assert("");
2376
2377
2378 /* invalid - error in a non-instantiated grouping */
2379 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2380 "grouping grp {leaf x {type leafref;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002381 logbuf_assert("Missing path substatement for leafref type. /aa:{grouping='grp'}/x");
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002382 logbuf_clean();
2383 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2384 "container a {grouping grp {leaf x {type leafref;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002385 logbuf_assert("Missing path substatement for leafref type. /aa:a/{grouping='grp'}/x");
Radek Krejcif2de0ed2019-05-02 14:13:18 +02002386
2387
2388 *state = NULL;
2389 ly_ctx_destroy(ctx, NULL);
2390}
2391
2392static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002393test_uses(void **state)
2394{
2395 *state = test_uses;
2396
2397 struct ly_ctx *ctx;
2398 struct lys_module *mod;
Radek Krejci3641f562019-02-13 15:38:40 +01002399 const struct lysc_node *parent, *child;
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002400 const struct lysc_node_container *cont;
Radek Krejcie86bf772018-12-14 11:39:53 +01002401
2402 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2403
Radek Krejci0af46292019-01-11 16:02:31 +01002404 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;"
2405 "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002406 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2407 "grouping grp_a_top {leaf a1 {type int8;}}"
2408 "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 +01002409 assert_non_null((parent = mod->compiled->data));
2410 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2411 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2412 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002413 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002414 assert_non_null((child = child->next));
2415 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002416 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002417 assert_non_null((child = child->next));
2418 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002419 assert_ptr_equal(mod, child->module);
Radek Krejci0af46292019-01-11 16:02:31 +01002420 assert_non_null((child = child->next));
2421 assert_string_equal("y", child->name);
2422 assert_non_null(child->iffeatures);
2423 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
2424 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features));
2425 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2426 assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
2427 logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
2428 assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
2429
2430 /* make the imported module implemented and enable the feature */
2431 assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
2432 assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod));
2433 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
2434 assert_string_equal("f", child->iffeatures[0].features[0]->name);
2435 assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
Radek Krejcie86bf772018-12-14 11:39:53 +01002436
Radek Krejci00b874b2019-02-12 10:54:50 +01002437 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;}}}");
2438 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 +01002439 assert_non_null(mod->compiled->data);
2440 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2441 assert_string_equal("b", mod->compiled->data->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002442 assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when));
Radek Krejcib1b59152019-01-07 13:21:56 +01002443
Radek Krejci7f6a3812019-01-07 13:48:57 +01002444 logbuf_clean();
2445 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2446 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2447 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002448 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2449 assert_string_equal("l", mod->compiled->data->name);
2450 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2451 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2452 assert_string_equal("k", mod->compiled->data->next->name);
2453 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2454 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2455
Radek Krejci3641f562019-02-13 15:38:40 +01002456 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}"
2457 "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG));
2458 assert_non_null(mod->compiled->data);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002459 assert_non_null(child = lysc_node_children(mod->compiled->data, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002460 assert_string_equal("g", child->name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002461 assert_non_null(child = lysc_node_children(child, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01002462 assert_string_equal("x", child->name);
2463
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002464 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\";}}"
2465 "container top {action e; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2466 assert_non_null(mod->compiled->data);
2467 cont = (const struct lysc_node_container*)mod->compiled->data;
2468 assert_non_null(cont->actions);
2469 assert_int_equal(2, LY_ARRAY_SIZE(cont->actions));
2470 assert_string_equal("e", cont->actions[1].name);
2471 assert_string_equal("g", cont->actions[0].name);
2472 assert_string_equal("ultra g", cont->actions[0].dsc);
2473
2474 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\";}}"
2475 "container top {notification f; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG));
2476 assert_non_null(mod->compiled->data);
2477 cont = (const struct lysc_node_container*)mod->compiled->data;
2478 assert_non_null(cont->notifs);
2479 assert_int_equal(2, LY_ARRAY_SIZE(cont->notifs));
2480 assert_string_equal("f", cont->notifs[1].name);
2481 assert_string_equal("g", cont->notifs[0].name);
2482 assert_string_equal("ultra g", cont->notifs[0].dsc);
2483
Radek Krejci684faf22019-05-27 14:31:16 +02002484 /* empty grouping */
2485 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g; grouping grp; uses grp;}", LYS_IN_YANG));
2486 assert_null(mod->compiled->data);
2487
Radek Krejcie86bf772018-12-14 11:39:53 +01002488 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002489 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002490 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found. /aa:{uses='missinggrp'}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002491
Radek Krejci096235c2019-01-11 11:12:19 +01002492 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002493 "grouping grp {leaf a{type string;}uses grp1;}"
2494 "grouping grp1 {leaf b {type string;}uses grp2;}"
2495 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002496 logbuf_assert("Grouping \"grp\" references itself through a uses statement. /bb:{uses='grp'}/{uses='grp1'}/{uses='grp2'}/{uses='grp'}");
Radek Krejcie86bf772018-12-14 11:39:53 +01002497
Radek Krejci096235c2019-01-11 11:12:19 +01002498 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002499 logbuf_assert("Invalid prefix used for grouping reference. /cc:{uses='a:missingprefix'}");
Radek Krejci76b3e962018-12-14 17:01:25 +01002500
Radek Krejci096235c2019-01-11 11:12:19 +01002501 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 +01002502 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002503 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement. /dd:{uses='grp'}/dd:a");
Radek Krejci76b3e962018-12-14 17:01:25 +01002504
Radek Krejci096235c2019-01-11 11:12:19 +01002505 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 +01002506 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002507 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status. /ee:{uses='grp'}/ee:l");
Radek Krejci7f6a3812019-01-07 13:48:57 +01002508
Radek Krejci95710c92019-02-11 15:49:55 +01002509 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}"
2510 "leaf l {type int8;}uses grp;}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002511 logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/Notification statement. /ff:{uses='grp'}/ff:l");
Radek Krejci95710c92019-02-11 15:49:55 +01002512 assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}"
2513 "uses grp;leaf m {type int8;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002514 logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/Notification statement. /fg:m");
Radek Krejci95710c92019-02-11 15:49:55 +01002515
Radek Krejci3641f562019-02-13 15:38:40 +01002516
2517 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}"
2518 "leaf g {type string;}"
2519 "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002520 logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used. /gg:top/{uses='grp'}/{augment='/g'}");
Radek Krejci3641f562019-02-13 15:38:40 +01002521
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002522 assert_non_null(mod = lys_parse_mem(ctx, "module hh {yang-version 1.1;namespace urn:hh;prefix hh;"
2523 "grouping grp {notification g { description \"super g\";}}"
2524 "container top {notification h; uses grp {refine h {description \"ultra h\";}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002525 logbuf_assert("Invalid descendant-schema-nodeid value \"h\" - target node not found. /hh:top/{uses='grp'}/{refine='h'}");
Radek Krejci32b6ecd2019-04-12 10:41:24 +02002526
2527 assert_non_null(mod = lys_parse_mem(ctx, "module ii {yang-version 1.1;namespace urn:ii;prefix ii;"
2528 "grouping grp {action g { description \"super g\";}}"
2529 "container top {action i; uses grp {refine i {description \"ultra i\";}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002530 logbuf_assert("Invalid descendant-schema-nodeid value \"i\" - target node not found. /ii:top/{uses='grp'}/{refine='i'}");
Radek Krejci3641f562019-02-13 15:38:40 +01002531
Radek Krejcie86bf772018-12-14 11:39:53 +01002532 *state = NULL;
2533 ly_ctx_destroy(ctx, NULL);
2534}
2535
Radek Krejci01342af2019-01-03 15:18:08 +01002536static void
2537test_refine(void **state)
2538{
2539 *state = test_refine;
2540
2541 struct ly_ctx *ctx;
2542 struct lys_module *mod;
2543 struct lysc_node *parent, *child;
Radek Krejcia1911222019-07-22 17:24:50 +02002544 struct lysc_node_leaf *leaf;
2545 struct lysc_node_leaflist *llist;
2546 int dynamic;
Radek Krejci01342af2019-01-03 15:18:08 +01002547
2548 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2549
Radek Krejci096235c2019-01-11 11:12:19 +01002550 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!;}"
2551 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2552 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2553 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2554 "leaf x {type mytype; mandatory true; must 1;}"
2555 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2556 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002557
Radek Krejcif0089082019-01-07 16:42:01 +01002558 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 +01002559 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002560 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002561 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002562 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2563 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002564 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002565 assert_non_null((parent = mod->compiled->data));
2566 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2567 assert_string_equal("c", parent->name);
Radek Krejcia1911222019-07-22 17:24:50 +02002568 assert_non_null((leaf = (struct lysc_node_leaf*)((struct lysc_node_container*)parent)->child));
2569 assert_int_equal(LYS_LEAF, leaf->nodetype);
2570 assert_string_equal("l", leaf->name);
2571 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2572 assert_int_equal(0, dynamic);
2573 assert_int_equal(LYS_CONFIG_R, leaf->flags & LYS_CONFIG_MASK);
2574 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
2575 assert_int_equal(LYS_LEAFLIST, llist->nodetype);
2576 assert_string_equal("ll", llist->name);
2577 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts));
2578 assert_string_equal("hello", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
2579 assert_int_equal(0, dynamic);
2580 assert_string_equal("world", llist->dflts[1]->realtype->plugin->print(llist->dflts[1], LYD_XML, NULL, NULL, &dynamic));
2581 assert_int_equal(0, dynamic);
2582 assert_int_equal(2, llist->min);
2583 assert_int_equal(5, llist->max);
2584 assert_non_null(child = llist->next);
Radek Krejci01342af2019-01-03 15:18:08 +01002585 assert_int_equal(LYS_CHOICE, child->nodetype);
2586 assert_string_equal("ch", child->name);
2587 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2588 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2589 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002590 assert_non_null(child->iffeatures);
2591 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcia1911222019-07-22 17:24:50 +02002592 assert_non_null(leaf = (struct lysc_node_leaf*)child->next);
2593 assert_int_equal(LYS_LEAF, leaf->nodetype);
2594 assert_string_equal("x", leaf->name);
2595 assert_false(LYS_MAND_TRUE & leaf->flags);
2596 assert_string_equal("cheers!", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2597 assert_int_equal(0, dynamic);
2598 assert_non_null(leaf->musts);
2599 assert_int_equal(2, LY_ARRAY_SIZE(leaf->musts));
2600 assert_string_equal("refined", leaf->dsc);
2601 assert_string_equal("refined", leaf->ref);
2602 assert_non_null(child = leaf->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002603 assert_int_equal(LYS_ANYDATA, child->nodetype);
2604 assert_string_equal("a", child->name);
2605 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002606 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2607 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002608 assert_non_null(child->iffeatures);
2609 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002610 assert_string_equal("refined", child->dsc);
2611 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002612 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002613 assert_int_equal(LYS_CONTAINER, child->nodetype);
2614 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002615 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002616 assert_true(LYS_CONFIG_W & child->flags);
2617 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002618
2619 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 +01002620 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +02002621 assert_non_null((leaf = (struct lysc_node_leaf*)((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
2622 assert_int_equal(LYS_LEAF, leaf->nodetype);
2623 assert_string_equal("x", leaf->name);
2624 assert_false(LYS_MAND_TRUE & leaf->flags);
2625 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2626 assert_int_equal(0, dynamic);
Radek Krejci01342af2019-01-03 15:18:08 +01002627
2628 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002629 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002630 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002631 logbuf_assert("Invalid refine of default - container cannot hold default value(s). /aa:{uses='g:grp'}/{refine='c'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002632
Radek Krejci096235c2019-01-11 11:12:19 +01002633 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002634 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002635 logbuf_assert("Invalid refine of default - leaf cannot hold 2 default values. /bb:{uses='g:grp'}/{refine='c/l'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002636
Radek Krejci096235c2019-01-11 11:12:19 +01002637 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002638 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002639 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules. /cc:{uses='g:grp'}/{refine='c/ll'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002640
Radek Krejci096235c2019-01-11 11:12:19 +01002641 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002642 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002643 logbuf_assert("Invalid refine of mandatory - leaf-list cannot hold mandatory statement. /dd:{uses='g:grp'}/{refine='c/ll'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002644
Radek Krejci096235c2019-01-11 11:12:19 +01002645 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002646 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002647 logbuf_assert("Invalid refine of mandatory - leaf already has \"default\" statement. /ee:{uses='g:grp'}/{refine='c/l'}");
Radek Krejci096235c2019-01-11 11:12:19 +01002648 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002649 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002650 logbuf_assert("Invalid refine of mandatory - choice already has \"default\" statement. /ef:{uses='g:grp'}/{refine='c/ch'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002651
Radek Krejci096235c2019-01-11 11:12:19 +01002652 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002653 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002654 logbuf_assert("Invalid refine of mandatory under the default case. /ff:{uses='g:grp'}/{refine='c/ch/a/a'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002655
Radek Krejci096235c2019-01-11 11:12:19 +01002656 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002657 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002658 logbuf_assert("Invalid refine of default - the node is mandatory. /gg:{uses='g:grp'}/{refine='c/x'}");
Radek Krejci01342af2019-01-03 15:18:08 +01002659
Radek Krejci096235c2019-01-11 11:12:19 +01002660 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002661 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002662 logbuf_assert("Invalid refine of config - configuration node cannot be child of any state data node. /hh:{uses='g:grp'}/{refine='c/c/l'}");
Radek Krejcib1b59152019-01-07 13:21:56 +01002663
Radek Krejci096235c2019-01-11 11:12:19 +01002664 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 +01002665 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002666 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status. /ii:{uses='grp'}/ii:l");
Radek Krejcib1b59152019-01-07 13:21:56 +01002667
Radek Krejci096235c2019-01-11 11:12:19 +01002668 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002669 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002670 logbuf_assert("Invalid refine of presence statement - leaf cannot hold the presence statement. /jj:{uses='g:grp'}/{refine='c/x'}");
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002671
Radek Krejci096235c2019-01-11 11:12:19 +01002672 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002673 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002674 logbuf_assert("Invalid refine of must statement - choice cannot hold any must statement. /kk:{uses='g:grp'}/{refine='c/ch'}");
Radek Krejci9a564c92019-01-07 14:53:57 +01002675
Radek Krejci096235c2019-01-11 11:12:19 +01002676 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002677 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002678 logbuf_assert("Invalid refine of min-elements statement - leaf cannot hold this statement. /ll:{uses='g:grp'}/{refine='c/x'}");
Radek Krejci6b22ab72019-01-07 15:39:20 +01002679
Radek Krejci096235c2019-01-11 11:12:19 +01002680 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002681 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002682 logbuf_assert("Invalid refine of min-elements statement - \"min-elements\" is bigger than \"max-elements\". /mm:{uses='g:grp'}/{refine='c/ll'}");
Radek Krejcif2271f12019-01-07 16:42:23 +01002683
Radek Krejci01342af2019-01-03 15:18:08 +01002684 *state = NULL;
2685 ly_ctx_destroy(ctx, NULL);
2686}
Radek Krejcie86bf772018-12-14 11:39:53 +01002687
Radek Krejci95710c92019-02-11 15:49:55 +01002688static void
2689test_augment(void **state)
2690{
2691 *state = test_augment;
2692
2693 struct ly_ctx *ctx;
2694 struct lys_module *mod;
2695 const struct lysc_node *node;
2696 const struct lysc_node_choice *ch;
2697 const struct lysc_node_case *c;
Radek Krejcif538ce52019-03-05 10:46:14 +01002698 const struct lysc_action *rpc;
Radek Krejci95710c92019-02-11 15:49:55 +01002699
2700 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2701
2702 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}"
2703 "container top {leaf a {type string;}}}");
2704 assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}"
2705 "leaf b {type a:atype;}}", LYS_IN_YANG));
2706 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}"
2707 "augment /a:top/ { container c {leaf c {type a:atype;}}}}");
2708 assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}"
2709 "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG));
2710 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2711 assert_non_null(ly_ctx_get_module_implemented(ctx, "b"));
2712 assert_non_null(ly_ctx_get_module_implemented(ctx, "c"));
2713 assert_non_null(ly_ctx_get_module_implemented(ctx, "d"));
2714 assert_non_null(node = mod->compiled->data);
2715 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002716 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002717 assert_string_equal(node->name, "a");
2718 assert_non_null(node = node->next);
2719 assert_string_equal(node->name, "c");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002720 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002721 assert_string_equal(node->name, "c");
2722 assert_non_null(node = node->next);
2723 assert_string_equal(node->name, "d");
2724 assert_non_null(node = node->next);
2725 assert_string_equal(node->name, "c");
2726
2727 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 +01002728 "augment /ch/c {when 1; leaf lc2 {type uint16;}}"
2729 "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG)));
Radek Krejci95710c92019-02-11 15:49:55 +01002730 assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data));
2731 assert_null(mod->compiled->data->next);
2732 assert_string_equal("ch", ch->name);
2733 assert_non_null(c = ch->cases);
2734 assert_string_equal("a", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002735 assert_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002736 assert_string_equal("a", c->child->name);
2737 assert_non_null(c = (const struct lysc_node_case*)c->next);
2738 assert_string_equal("b", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002739 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002740 assert_string_equal("b", c->child->name);
2741 assert_non_null(c = (const struct lysc_node_case*)c->next);
2742 assert_string_equal("c", c->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002743 assert_non_null(c->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002744 assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002745 assert_null(((const struct lysc_node_case*)c)->child->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002746 assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name);
Radek Krejci00b874b2019-02-12 10:54:50 +01002747 assert_non_null(((const struct lysc_node_case*)c)->child->next->when);
Radek Krejci95710c92019-02-11 15:49:55 +01002748 assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next);
2749 assert_null(c->next);
2750
2751 assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}"
2752 "container c;"
2753 "augment /c {uses g;}}", LYS_IN_YANG)));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002754 assert_non_null(node = lysc_node_children(mod->compiled->data, 0));
Radek Krejci95710c92019-02-11 15:49:55 +01002755 assert_string_equal(node->name, "a");
2756
Radek Krejci339f5292019-02-11 16:42:34 +01002757 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}"
2758 "augment /c {container sub;}}");
2759 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;"
2760 "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG));
2761 assert_non_null(mod->compiled->data);
2762 assert_string_equal("c", mod->compiled->data->name);
2763 assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child);
2764 assert_string_equal("sub", node->name);
2765 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2766 assert_string_equal("main", node->name);
2767
Radek Krejcif538ce52019-03-05 10:46:14 +01002768 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 +01002769 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;"
2770 "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}"
2771 "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}"
2772 "augment /hi:top {leaf l {type string; mandatory true; config false;}}"
2773 "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG));
2774 assert_non_null(node = mod->compiled->data);
2775 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2776 assert_string_equal("l", node->name);
2777 assert_true(node->flags & LYS_MAND_TRUE);
2778 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
Radek Krejcife909632019-02-12 15:34:42 +01002779 assert_non_null(node = mod->compiled->data);
2780 assert_non_null(node = ((struct lysc_node_container*)node)->child);
2781 assert_string_equal("p", node->name);
Radek Krejci733988a2019-02-15 15:12:44 +01002782 assert_non_null(node = node->next);
2783 assert_string_equal("ll", node->name);
2784 assert_non_null(node = node->next);
2785 assert_string_equal("l", node->name);
2786 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejcife909632019-02-12 15:34:42 +01002787
Radek Krejcif538ce52019-03-05 10:46:14 +01002788 assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}"
2789 "augment /hi:func/input {leaf x {type string;}}"
2790 "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG));
2791 assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp"));
2792 assert_non_null(rpc = mod->compiled->rpcs);
2793 assert_int_equal(1, LY_ARRAY_SIZE(rpc));
2794 assert_non_null(rpc->input.data);
2795 assert_string_equal("x", rpc->input.data->name);
2796 assert_null(rpc->input.data->next);
2797 assert_non_null(rpc->output.data);
2798 assert_string_equal("y", rpc->output.data->name);
2799 assert_null(rpc->output.data->next);
2800
Radek Krejci95710c92019-02-11 15:49:55 +01002801 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}"
2802 "augment /x {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002803 logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found. /aa:{augment='/x'}");
Radek Krejci95710c92019-02-11 15:49:55 +01002804
2805 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}"
2806 "augment /c {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002807 logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement. /bb:{augment='/c'}/a");
Radek Krejci95710c92019-02-11 15:49:55 +01002808
2809
Radek Krejci339f5292019-02-11 16:42:34 +01002810 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}"
2811 "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002812 logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target. /cc:{augment='/c/a'}");
Radek Krejci339f5292019-02-11 16:42:34 +01002813
2814 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}"
2815 "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002816 logbuf_assert("Invalid augment of container node which is not allowed to contain case node \"b\". /dd:{augment='/c'}");
Radek Krejci339f5292019-02-11 16:42:34 +01002817
Radek Krejci733988a2019-02-15 15:12:44 +01002818 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}"
2819 "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002820 logbuf_assert("Invalid augment adding mandatory node \"c\" without making it conditional via when statement. /ee:{augment='/hi:top'}");
Radek Krejcife909632019-02-12 15:34:42 +01002821
Radek Krejci3641f562019-02-13 15:38:40 +01002822 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;"
2823 "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002824 logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\". /ff:{augment='../top'}");
Radek Krejci95710c92019-02-11 15:49:55 +01002825
Radek Krejcif538ce52019-03-05 10:46:14 +01002826 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;"
2827 "augment /func {leaf x {type int8;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02002828 logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC/action node which is not an allowed augment's target. /gg:{augment='/func'}");
Radek Krejcif538ce52019-03-05 10:46:14 +01002829
Radek Krejci95710c92019-02-11 15:49:55 +01002830 *state = NULL;
2831 ly_ctx_destroy(ctx, NULL);
2832}
2833
Radek Krejciccd20f12019-02-15 14:12:27 +01002834static void
2835test_deviation(void **state)
2836{
2837 *state = test_deviation;
2838
2839 struct ly_ctx *ctx;
2840 struct lys_module *mod;
2841 const struct lysc_node *node;
Radek Krejci7af64242019-02-18 13:07:53 +01002842 const struct lysc_node_list *list;
Radek Krejcia1911222019-07-22 17:24:50 +02002843 const struct lysc_node_leaflist *llist;
2844 const struct lysc_node_leaf *leaf;
Radek Krejci474f9b82019-07-24 11:36:37 +02002845 const char *str;
Radek Krejcia1911222019-07-22 17:24:50 +02002846 int dynamic;
Radek Krejciccd20f12019-02-15 14:12:27 +01002847
2848 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2849
2850 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;"
Radek Krejci88ef64b2019-02-18 16:02:06 +01002851 "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2852 "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 +01002853 " case c {leaf c{type string;}}}"
2854 "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}"
2855 "rpc func2;}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002856 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 +01002857 "deviation /a:top/a:b {deviate not-supported;}"
2858 "deviation /a:ch/a:a/a:x {deviate not-supported;}"
2859 "deviation /a:ch/a:c/a:c {deviate not-supported;}"
2860 "deviation /a:ch/a:b {deviate not-supported;}"
Radek Krejcif538ce52019-03-05 10:46:14 +01002861 "deviation /a:ch/a:a/a:a {deviate not-supported;}"
2862 "deviation /a:func1/a:input {deviate not-supported;}"
2863 "deviation /a:func1/a:output {deviate not-supported;}"
2864 "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002865 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a")));
2866 assert_non_null(node = mod->compiled->data);
2867 assert_string_equal(node->name, "top");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01002868 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejciccd20f12019-02-15 14:12:27 +01002869 assert_string_equal(node->name, "a");
2870 assert_non_null(node = node->next);
2871 assert_string_equal(node->name, "c");
Radek Krejci88ef64b2019-02-18 16:02:06 +01002872 assert_null(node = node->next);
2873 assert_non_null(node = mod->compiled->data->next);
2874 assert_string_equal("ch", node->name);
2875 assert_null(((struct lysc_node_choice*)node)->dflt);
2876 assert_null(((struct lysc_node_choice*)node)->cases);
Radek Krejcif538ce52019-03-05 10:46:14 +01002877 assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs));
2878 assert_null(mod->compiled->rpcs[0].input.data);
2879 assert_null(mod->compiled->rpcs[0].output.data);
Radek Krejciccd20f12019-02-15 14:12:27 +01002880
2881 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}"
2882 "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}"
2883 "deviation /c1 {deviate add {units meters;}}"
2884 "deviation /c2 {deviate delete {units meters;}}"
2885 "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG));
2886 assert_non_null(node = mod->compiled->data);
2887 assert_string_equal("c1", node->name);
2888 assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units);
2889 assert_non_null(node = node->next);
2890 assert_string_equal("c2", node->name);
2891 assert_null(((struct lysc_node_leaf*)node)->units);
2892 assert_non_null(node = node->next);
2893 assert_string_equal("c3", node->name);
2894 assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units);
2895
2896 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 +01002897 "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01002898 "deviation /c1 {deviate add {must 3;}}"
2899 "deviation /c2 {deviate delete {must 2;}}"
2900 "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG));
2901 assert_non_null(node = mod->compiled->data);
2902 assert_string_equal("c1", node->name);
2903 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts));
2904 assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr);
2905 assert_non_null(node = node->next);
2906 assert_string_equal("c2", node->name);
Radek Krejcib4532d12019-02-15 16:13:29 +01002907 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts));
2908 assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr);
Radek Krejciccd20f12019-02-15 14:12:27 +01002909 assert_non_null(node = node->next);
2910 assert_string_equal("c3", node->name);
2911 assert_null(((struct lysc_node_leaf*)node)->musts);
2912
Radek Krejcib4532d12019-02-15 16:13:29 +01002913 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 +01002914 "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 +01002915 "choice b {default a;leaf a {type string;} leaf b {type string;}}"
2916 "leaf c {default hello; type string;}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002917 "leaf-list d {default hello; default world; type string;}"
2918 "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}");
Radek Krejciccd20f12019-02-15 14:12:27 +01002919 assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}"
2920 "deviation /x:a {deviate delete {default a;}}"
2921 "deviation /x:b {deviate delete {default x:a;}}"
2922 "deviation /x:c {deviate delete {default hello;}}"
2923 "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG));
2924 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2925 assert_non_null(node = mod->compiled->data);
2926 assert_null(((struct lysc_node_choice*)node)->dflt);
2927 assert_non_null(node = node->next);
2928 assert_null(((struct lysc_node_choice*)node)->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02002929 assert_non_null(leaf = (struct lysc_node_leaf*)node->next);
2930 assert_null(leaf->dflt);
2931 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
2932 assert_int_equal(1, LY_ARRAY_SIZE(llist->dflts));
2933 assert_string_equal("hello", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
2934 assert_int_equal(0, dynamic);
2935 assert_non_null(leaf = (struct lysc_node_leaf*)llist->next);
2936 assert_string_equal("nothing", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2937 assert_int_equal(0, dynamic);
2938 assert_int_equal(5, leaf->dflt->realtype->refcount); /* 3x type reference, 2x default value reference (typedef's default does not reference own type) */
2939 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
2940 assert_int_equal(1, LY_ARRAY_SIZE(llist->dflts));
2941 assert_string_equal("nothing", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
2942 assert_int_equal(0, dynamic);
Radek Krejciccd20f12019-02-15 14:12:27 +01002943
2944 assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}"
2945 "deviation /x:b {deviate add {default x:b;}}"
2946 "deviation /x:c {deviate add {default bye;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002947 "deviation /x:d {deviate add {default all; default people;}}"
Radek Krejci462cf252019-07-24 09:49:08 +02002948 "deviation /x:c2 {deviate add {default hi; must 1;}}"
Radek Krejcib4532d12019-02-15 16:13:29 +01002949 "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG));
Radek Krejciccd20f12019-02-15 14:12:27 +01002950 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2951 assert_non_null(node = mod->compiled->data);
2952 assert_null(((struct lysc_node_choice*)node)->dflt);
2953 assert_non_null(node = node->next);
2954 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2955 assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name);
Radek Krejcia1911222019-07-22 17:24:50 +02002956 assert_non_null(leaf = (struct lysc_node_leaf*)node->next);
2957 assert_non_null(leaf->dflt);
2958 assert_string_equal("bye", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2959 assert_int_equal(0, dynamic);
2960 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
2961 assert_int_equal(3, LY_ARRAY_SIZE(llist->dflts));
2962 assert_string_equal("hello", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
2963 assert_int_equal(0, dynamic);
2964 assert_string_equal("all", llist->dflts[1]->realtype->plugin->print(llist->dflts[1], LYD_XML, NULL, NULL, &dynamic));
2965 assert_int_equal(0, dynamic);
2966 assert_string_equal("people", llist->dflts[2]->realtype->plugin->print(llist->dflts[2], LYD_XML, NULL, NULL, &dynamic));
2967 assert_int_equal(0, dynamic);
2968 assert_non_null(leaf = (struct lysc_node_leaf*)llist->next);
2969 assert_non_null(leaf->dflt);
2970 assert_string_equal("hi", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2971 assert_int_equal(0, dynamic);
2972 assert_int_equal(6, leaf->dflt->realtype->refcount); /* 3x type reference, 3x default value reference
2973 - previous type's default values were replaced by node's default values where d2 now has 2 default values */
Radek Krejci462cf252019-07-24 09:49:08 +02002974 assert_int_equal(1, LY_ARRAY_SIZE(leaf->musts));
2975 assert_ptr_equal(leaf->musts[0].module, ly_ctx_get_module_implemented(ctx, "g"));
Radek Krejcia1911222019-07-22 17:24:50 +02002976 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
2977 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts));
2978 assert_string_equal("hi", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
2979 assert_int_equal(0, dynamic);
2980 assert_string_equal("all", llist->dflts[1]->realtype->plugin->print(llist->dflts[1], LYD_XML, NULL, NULL, &dynamic));
2981 assert_int_equal(0, dynamic);
Radek Krejciccd20f12019-02-15 14:12:27 +01002982
2983 assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}"
2984 "deviation /x:b {deviate replace {default x:a;}}"
2985 "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG));
2986 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e")));
2987 assert_non_null(node = mod->compiled->data);
2988 assert_null(((struct lysc_node_choice*)node)->dflt);
2989 assert_non_null(node = node->next);
2990 assert_non_null(((struct lysc_node_choice*)node)->dflt);
2991 assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name);
Radek Krejcia1911222019-07-22 17:24:50 +02002992 assert_non_null(leaf = (struct lysc_node_leaf*)node->next);
2993 assert_non_null(leaf->dflt);
2994 assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
2995 assert_int_equal(0, dynamic);
Radek Krejciccd20f12019-02-15 14:12:27 +01002996
Radek Krejci7af64242019-02-18 13:07:53 +01002997 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;"
2998 "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
2999 "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}"
3000 " leaf c {type string;} leaf d {type string;}}}");
3001 assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}"
3002 "augment /i:l1 {leaf j_c {type string;}}"
3003 "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}"
3004 "deviation /i:l1 {deviate add {unique \"i:c\";}}"
3005 "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG));
3006 assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i")));
3007 assert_non_null(list = (struct lysc_node_list*)mod->compiled->data);
3008 assert_string_equal("l1", list->name);
3009 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
3010 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
3011 assert_string_equal("b", list->uniques[0][0]->name);
3012 assert_string_equal("j_c", list->uniques[0][1]->name);
3013 assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1]));
3014 assert_string_equal("c", list->uniques[1][0]->name);
3015 assert_non_null(list = (struct lysc_node_list*)list->next);
3016 assert_string_equal("l2", list->name);
3017 assert_null(list->uniques);
3018
Radek Krejci93dcc392019-02-19 10:43:38 +01003019 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}"
3020 "container top {leaf x {type string;} leaf y {type string; config false;}}"
3021 "deviation /a {deviate add {config false; }}"
3022 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
3023 assert_non_null(node = mod->compiled->data);
3024 assert_string_equal("a", node->name);
3025 assert_true(node->flags & LYS_CONFIG_R);
3026 assert_non_null(node = node->next);
3027 assert_string_equal("top", node->name);
3028 assert_true(node->flags & LYS_CONFIG_R);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003029 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01003030 assert_string_equal("x", node->name);
3031 assert_true(node->flags & LYS_CONFIG_R);
3032 assert_non_null(node = node->next);
3033 assert_string_equal("y", node->name);
3034 assert_true(node->flags & LYS_CONFIG_R);
3035
3036 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}"
3037 "container top {config false; leaf x {type string;}}"
3038 "deviation /a {deviate replace {config true;}}"
3039 "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG));
3040 assert_non_null(node = mod->compiled->data);
3041 assert_string_equal("a", node->name);
3042 assert_true(node->flags & LYS_CONFIG_W);
3043 assert_non_null(node = node->next);
3044 assert_string_equal("top", node->name);
3045 assert_true(node->flags & LYS_CONFIG_W);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003046 assert_non_null(node = lysc_node_children(node, 0));
Radek Krejci93dcc392019-02-19 10:43:38 +01003047 assert_string_equal("x", node->name);
3048 assert_true(node->flags & LYS_CONFIG_W);
3049
Radek Krejcif1421c22019-02-19 13:05:20 +01003050 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;"
3051 "container a {leaf a {type string;}}"
3052 "container b {leaf b {mandatory true; type string;}}"
3053 "deviation /a/a {deviate add {mandatory true;}}"
3054 "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG));
3055 assert_non_null(node = mod->compiled->data);
3056 assert_string_equal("a", node->name);
3057 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003058 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
Radek Krejcif1421c22019-02-19 13:05:20 +01003059 assert_non_null(node = node->next);
3060 assert_string_equal("b", node->name);
3061 assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003062 assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
Radek Krejcif1421c22019-02-19 13:05:20 +01003063
Radek Krejci551b12c2019-02-19 16:11:21 +01003064 assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;"
3065 "leaf a {default test; type string;}"
3066 "leaf b {mandatory true; type string;}"
3067 "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}"
3068 "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG));
3069 assert_non_null(node = mod->compiled->data);
3070 assert_string_equal("a", node->name);
3071 assert_null(((struct lysc_node_leaf*)node)->dflt);
3072 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE);
3073 assert_non_null(node = node->next);
3074 assert_string_equal("b", node->name);
3075 assert_non_null(((struct lysc_node_leaf*)node)->dflt);
3076 assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE);
3077
3078 assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;"
3079 "leaf-list a {type string;}"
3080 "list b {config false;}"
3081 "leaf-list c {min-elements 1; max-elements 10; type string;}"
3082 "list d {min-elements 10; max-elements 100; config false;}"
3083 "deviation /a {deviate add {min-elements 1; max-elements 10;}}"
3084 "deviation /b {deviate add {min-elements 10; max-elements 100;}}"
3085 "deviation /c {deviate replace {min-elements 10; max-elements 100;}}"
3086 "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG));
3087 assert_non_null(node = mod->compiled->data);
3088 assert_string_equal("a", node->name);
3089 assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min);
3090 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max);
3091 assert_non_null(node = node->next);
3092 assert_string_equal("b", node->name);
3093 assert_int_equal(10, ((struct lysc_node_list*)node)->min);
3094 assert_int_equal(100, ((struct lysc_node_list*)node)->max);
3095 assert_non_null(node = node->next);
3096 assert_string_equal("c", node->name);
3097 assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min);
3098 assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max);
3099 assert_non_null(node = node->next);
3100 assert_string_equal("d", node->name);
3101 assert_int_equal(1, ((struct lysc_node_list*)node)->min);
3102 assert_int_equal(10, ((struct lysc_node_list*)node)->max);
3103
Radek Krejci33f72892019-02-21 10:36:58 +01003104 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;}"
3105 "leaf a {type string; default 10;} leaf-list b {type string;}"
3106 "deviation /a {deviate replace {type mytype;}}"
3107 "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG));
Radek Krejcia1911222019-07-22 17:24:50 +02003108 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3109 assert_string_equal("a", leaf->name);
3110 assert_int_equal(LY_TYPE_INT8, leaf->type->basetype);
3111 assert_string_equal("10", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
3112 assert_int_equal(0, dynamic);
3113 assert_int_equal(10, leaf->dflt->uint8);
3114 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3115 assert_string_equal("b", llist->name);
3116 assert_int_equal(LY_TYPE_INT8, llist->type->basetype);
3117 assert_int_equal(1, LY_ARRAY_SIZE(llist->dflts));
3118 assert_string_equal("1", llist->dflts[0]->realtype->plugin->print(llist->dflts[0], LYD_XML, NULL, NULL, &dynamic));
3119 assert_int_equal(0, dynamic);
3120 assert_int_equal(1, llist->dflts[0]->uint8);
Radek Krejci33f72892019-02-21 10:36:58 +01003121
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003122 assert_non_null(mod = lys_parse_mem(ctx, "module q {yang-version 1.1; namespace urn:q;prefix q; import e {prefix e;}"
Radek Krejci1c0c3442019-07-23 16:08:47 +02003123 "leaf q {type instance-identifier; default \"/e:d2\";}"
3124 "leaf-list ql {type instance-identifier; default \"/e:d\"; default \"/e:d2\";}}", LYS_IN_YANG));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003125 assert_non_null(lys_parse_mem(ctx, "module qdev {yang-version 1.1; namespace urn:qdev;prefix qd; import q {prefix q;}"
Radek Krejci1c0c3442019-07-23 16:08:47 +02003126 "deviation /q:q { deviate replace {type string;}}"
3127 "deviation /q:ql { deviate replace {type string;}}}", LYS_IN_YANG));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003128 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3129 assert_int_equal(LY_TYPE_STRING, leaf->dflt->realtype->basetype);
3130 assert_non_null(leaf->dflt->canonized);
3131 assert_string_equal("/e:d2", leaf->dflt->canonized);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003132 assert_non_null(llist = (struct lysc_node_leaflist*)leaf->next);
3133 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts));
3134 assert_int_equal(2, LY_ARRAY_SIZE(llist->dflts_mods));
3135 assert_ptr_equal(llist->dflts_mods[0], mod);
3136 assert_int_equal(LY_TYPE_STRING, llist->dflts[0]->realtype->basetype);
3137 assert_string_equal("/e:d", llist->dflts[0]->canonized);
3138 assert_ptr_equal(llist->dflts_mods[1], mod);
3139 assert_int_equal(LY_TYPE_STRING, llist->dflts[0]->realtype->basetype);
3140 assert_string_equal("/e:d2", llist->dflts[1]->canonized);
3141
3142 assert_non_null(mod = lys_parse_mem(ctx, "module r {yang-version 1.1; namespace urn:r;prefix r;"
3143 "typedef mytype {type uint8; default 200;}"
3144 "leaf r {type mytype;} leaf-list lr {type mytype;}"
3145 "deviation /r:r {deviate replace {type string;}}"
3146 "deviation /r:lr {deviate replace {type string;}}}", LYS_IN_YANG));
3147 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3148 assert_string_equal("r", leaf->name);
3149 assert_null(leaf->dflt);
3150 assert_null(leaf->dflt_mod);
3151 assert_non_null(llist = (struct lysc_node_leaflist* )leaf->next);
3152 assert_string_equal("lr", llist->name);
3153 assert_null(llist->dflts);
3154 assert_null(llist->dflts_mods);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003155
Radek Krejci474f9b82019-07-24 11:36:37 +02003156 assert_non_null(mod = lys_parse_mem(ctx, "module s {yang-version 1.1; namespace urn:s;prefix s;"
3157 "leaf s {type instance-identifier {require-instance true;} default /s:x;}"
3158 "leaf x {type string;} leaf y {type string;}"
3159 "deviation /s:s {deviate replace {default /s:y;}}}", LYS_IN_YANG));
3160 assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
3161 assert_string_equal("s", leaf->name);
3162 assert_non_null(leaf->dflt);
3163 assert_non_null(str = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, mod, &dynamic));
3164 assert_string_equal("/s:y", str);
3165 if (dynamic) { free((char*)str); }
3166
Radek Krejci88ef64b2019-02-18 16:02:06 +01003167 assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003168 "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003169 logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found. /aa1:{deviation='/a:top/a:z'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003170 assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}"
3171 "deviation /a:top/a:a {deviate not-supported;}"
3172 "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG));
3173 /* warning */
3174 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 +01003175
3176 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}"
3177 "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003178 logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\". /bb:{deviation='a:top/a:a'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003179
3180 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;"
3181 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003182 logbuf_assert("Invalid deviation of container node - it is not possible to add \"units\" property. /cc:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003183 assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}"
3184 "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003185 logbuf_assert("Invalid deviation adding \"units\" property which already exists (with value \"centimeters\"). /cd:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003186
3187 assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;"
3188 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003189 logbuf_assert("Invalid deviation of container node - it is not possible to delete \"units\" property. /dd1:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003190 assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}"
3191 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003192 logbuf_assert("Invalid deviation deleting \"units\" property \"meters\" which is not present. /dd2:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003193 assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}"
3194 "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003195 logbuf_assert("Invalid deviation deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\"."
3196 " /dd3:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003197
3198 assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;"
3199 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003200 logbuf_assert("Invalid deviation of container node - it is not possible to replace \"units\" property. /ee1:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003201 assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}"
3202 "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003203 logbuf_assert("Invalid deviation replacing \"units\" property \"meters\" which is not present. /ee2:{deviation='/c'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003204
3205 /* the default is already deleted in /e:a byt module f */
3206 assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}"
3207 "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003208 logbuf_assert("Invalid deviation deleting \"default\" property \"x:a\" which is not present. /ff1:{deviation='/e:a'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003209 assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}"
3210 "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003211 logbuf_assert("Invalid deviation deleting \"default\" property \"x:a\" of choice. "
3212 "The prefix does not match any imported module of the deviation module. /ff2:{deviation='/e:b'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003213 assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}"
3214 "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003215 logbuf_assert("Invalid deviation deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\". /ff3:{deviation='/e:b'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003216 assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}"
3217 "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003218 logbuf_assert("Invalid deviation deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module. /ff4:{deviation='/e:b'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003219 assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;"
3220 "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003221 logbuf_assert("Invalid deviation of anyxml node - it is not possible to delete \"default\" property. /ff5:{deviation='/a'}");
Radek Krejci1c0c3442019-07-23 16:08:47 +02003222 assert_null(lys_parse_mem(ctx, "module ff6 {namespace urn:ff6;prefix ff6; import e {prefix e;}"
3223 "deviation /e:c {deviate delete {default hi;}}}", LYS_IN_YANG));
3224 logbuf_assert("Invalid deviation deleting \"default\" property \"hi\" which does not match the target's property value \"hello\". /ff6:{deviation='/e:c'}");
3225 assert_null(lys_parse_mem(ctx, "module ff7 {namespace urn:ff7;prefix ff7; import e {prefix e;}"
3226 "deviation /e:d {deviate delete {default hi;}}}", LYS_IN_YANG));
3227 logbuf_assert("Invalid deviation deleting \"default\" property \"hi\" which does not match any of the target's property values. /ff7:{deviation='/e:d'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003228
3229 assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}"
3230 "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003231 logbuf_assert("Invalid deviation adding \"default\" property which already exists (with value \"a\"). /gg1:{deviation='/e:b'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003232 assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}"
Radek Krejci468a94f2019-02-15 14:52:36 +01003233 "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003234 logbuf_assert("Invalid deviation adding \"default\" property \"x:a\" of choice. "
3235 "The prefix does not match any imported module of the deviation module. /gg2:{deviation='/e:a'}");
Radek Krejci468a94f2019-02-15 14:52:36 +01003236 assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}"
3237 "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003238 logbuf_assert("Invalid deviation adding \"default\" property \"a\" of choice - the specified case does not exists. /gg3:{deviation='/e:a'}");
Radek Krejci468a94f2019-02-15 14:52:36 +01003239 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
Radek Krejciccd20f12019-02-15 14:12:27 +01003240 "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003241 logbuf_assert("Invalid deviation adding \"default\" property which already exists (with value \"hello\"). /gg4:{deviation='/e:c'}");
Radek Krejci468a94f2019-02-15 14:52:36 +01003242 assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}"
3243 "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003244 logbuf_assert("Invalid deviation adding \"default\" property \"e:c\" of choice - mandatory node \"c\" under the default case. /gg4:{deviation='/e:a'}");
Radek Krejci88ef64b2019-02-18 16:02:06 +01003245 assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}"
3246 "deviation /x {deviate add {default error;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003247 logbuf_assert("Invalid deviation combining default value and mandatory leaf. /gg5:{deviation='/x'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003248
3249 assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}"
3250 "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003251 logbuf_assert("Invalid deviation of leaf-list node - it is not possible to replace \"default\" property. /hh1:{deviation='/e:d'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003252
Radek Krejci7af64242019-02-18 13:07:53 +01003253 assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}"
3254 "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003255 logbuf_assert("Invalid deviation deleting \"unique\" property \"x\" which does not match any of the target's property values. /ii1:{deviation='/i:l1'}");
Radek Krejci7af64242019-02-18 13:07:53 +01003256 assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}"
3257 "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003258 logbuf_assert("Invalid deviation deleting \"unique\" property \"d\" which does not match any of the target's property values. /ii2:{deviation='/i:l2'}");
Radek Krejci7af64242019-02-18 13:07:53 +01003259 assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}"
3260 "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003261 logbuf_assert("Invalid deviation of leaf node - it is not possible to delete \"unique\" property. /ii3:{deviation='/x'}");
Radek Krejci7af64242019-02-18 13:07:53 +01003262 assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}"
3263 "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003264 logbuf_assert("Invalid deviation of leaf node - it is not possible to add \"unique\" property. /ii4:{deviation='/x'}");
Radek Krejciccd20f12019-02-15 14:12:27 +01003265
Radek Krejci93dcc392019-02-19 10:43:38 +01003266 assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}"
3267 "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003268 logbuf_assert("Invalid deviation of case node - it is not possible to add \"config\" property. /jj1:{deviation='/ch/a'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003269 assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}"
3270 "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003271 logbuf_assert("Invalid deviation of config - configuration node cannot be child of any state data node. /jj2:{deviation='/top/x'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003272 assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}"
3273 "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003274 logbuf_assert("Invalid deviation replacing \"config\" property \"config false\" which is not present. /jj3:{deviation='/top/x'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003275 assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}"
3276 "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003277 logbuf_assert("Invalid deviation of case node - it is not possible to replace \"config\" property. /jj4:{deviation='/ch/a'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003278 assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}"
3279 "deviation /top {deviate add {config false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003280 logbuf_assert("Invalid deviation of config - configuration node cannot be child of any state data node. /jj5:{deviation='/top'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003281 assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}"
3282 "deviation /x {deviate add {config true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003283 logbuf_assert("Invalid deviation adding \"config\" property which already exists (with value \"config false\"). /jj6:{deviation='/x'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003284
3285 assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}"
3286 "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003287 logbuf_assert("Invalid deviation of mandatory - container cannot hold mandatory statement. /kk1:{deviation='/top'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003288 assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}"
3289 "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003290 logbuf_assert("Invalid deviation replacing \"mandatory\" property \"mandatory true\" which is not present. /kk2:{deviation='/top'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003291 assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}"
3292 "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003293 logbuf_assert("Invalid deviation replacing \"mandatory\" property \"mandatory true\" which is not present. /kk3:{deviation='/top/x'}");
Radek Krejcif1421c22019-02-19 13:05:20 +01003294 assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}"
3295 "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003296 logbuf_assert("Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory true\"). /kk4:{deviation='/x'}");
Radek Krejci93dcc392019-02-19 10:43:38 +01003297
Radek Krejci551b12c2019-02-19 16:11:21 +01003298 assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}"
3299 "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003300 logbuf_assert("Invalid deviation combining default value and mandatory leaf. /ll1:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003301 assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}"
3302 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003303 logbuf_assert("Invalid deviation combining default value and mandatory leaf-list. /ll2:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003304 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;}}"
3305 "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003306 logbuf_assert("Invalid deviation combining default case and mandatory choice. /ll2:{deviation='/ch'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003307
3308 assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}"
3309 "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003310 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 10 is bigger than max value 5. /mm1:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003311 assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}"
3312 "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003313 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 20 is bigger than max value 10. /mm2:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003314 assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}"
3315 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003316 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 5 is bigger than max value 1. /mm3:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003317 assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}"
3318 "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003319 logbuf_assert("Invalid combination of min-elements and max-elements after deviation: min value 20 is bigger than max value 10. /mm4:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003320 assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}"
3321 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003322 logbuf_assert("Invalid deviation adding \"min-elements\" property which already exists (with value \"5\"). /mm5:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003323 assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}"
3324 "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003325 logbuf_assert("Invalid deviation adding \"min-elements\" property which already exists (with value \"5\"). /mm6:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003326 assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}"
3327 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003328 logbuf_assert("Invalid deviation adding \"max-elements\" property which already exists (with value \"5\"). /mm7:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003329 assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}"
3330 "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003331 logbuf_assert("Invalid deviation adding \"max-elements\" property which already exists (with value \"5\"). /mm8:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003332 assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}"
3333 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003334 logbuf_assert("Invalid deviation replacing with \"min-elements\" property \"1\" which is not present. /mm9:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003335 assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}"
3336 "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003337 logbuf_assert("Invalid deviation replacing with \"min-elements\" property \"1\" which is not present. /mm10:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003338 assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}"
3339 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003340 logbuf_assert("Invalid deviation replacing with \"max-elements\" property \"1\" which is not present. /mm11:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003341 assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }"
3342 "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003343 logbuf_assert("Invalid deviation replacing with \"max-elements\" property \"1\" which is not present. /mm12:{deviation='/x'}");
Radek Krejci551b12c2019-02-19 16:11:21 +01003344
Radek Krejci33f72892019-02-21 10:36:58 +01003345 assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;"
3346 "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003347 logbuf_assert("Invalid deviation of anyxml node - it is not possible to replace \"type\" property. /nn1:{deviation='/x'}");
Radek Krejci33f72892019-02-21 10:36:58 +01003348 assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}"
3349 "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG));
Radek Krejci327de162019-06-14 12:52:07 +02003350 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules. /nn2:{deviation='/x'}");
Radek Krejci33f72892019-02-21 10:36:58 +01003351
Radek Krejci1c0c3442019-07-23 16:08:47 +02003352 assert_null(lys_parse_mem(ctx, "module oo1 {namespace urn:oo1;prefix oo1; leaf x {type uint16; default 300;}"
3353 "deviation /x {deviate replace {type uint8;}}}", LYS_IN_YANG));
3354 logbuf_assert("Invalid deviation replacing leaf's type - the leaf's default value \"300\" does not match the type "
3355 "(Value \"300\" is out of uint8's min/max bounds.). /oo1:{deviation='/x'}");
3356 assert_null(lys_parse_mem(ctx, "module oo2 {yang-version 1.1;namespace urn:oo2;prefix oo2; leaf-list x {type uint16; default 10; default 300;}"
3357 "deviation /x {deviate replace {type uint8;}}}", LYS_IN_YANG));
3358 logbuf_assert("Invalid deviation replacing leaf-list's type - the leaf-list's default value \"300\" does not match the type "
3359 "(Value \"300\" is out of uint8's min/max bounds.). /oo2:{deviation='/x'}");
3360 assert_null(lys_parse_mem(ctx, "module oo3 {namespace urn:oo3;prefix oo3; leaf x {type uint8;}"
3361 "deviation /x {deviate add {default 300;}}}", LYS_IN_YANG));
3362 logbuf_assert("Invalid deviation setting \"default\" property \"300\" which does not fit the type "
3363 "(Value \"300\" is out of uint8's min/max bounds.). /oo3:{deviation='/x'}");
3364
Radek Krejci474f9b82019-07-24 11:36:37 +02003365/* TODO recompiling reference object after deviation changes schema tree
3366 assert_non_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp; leaf l { type leafref {path /c/x;}}"
3367 "container c {leaf x {type string;} leaf y {type string;}}}", LYS_IN_YANG));
3368 assert_null(lys_parse_mem(ctx, "module pp1 {namespace urn:pp1;prefix pp1; import pp {prefix pp;}"
3369 "deviation /pp:c/pp:x {deviate not-supported;}}", LYS_IN_YANG));
3370 logbuf_assert("???. /pp:l}");
3371*/
3372
Radek Krejciccd20f12019-02-15 14:12:27 +01003373 *state = NULL;
3374 ly_ctx_destroy(ctx, NULL);
3375}
3376
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003377int main(void)
3378{
3379 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01003380 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
3381 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
3382 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01003383 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
3384 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01003385 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01003386 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
3387 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01003388 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01003389 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01003390 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01003391 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01003392 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01003393 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
3394 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01003395 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01003396 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01003397 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003398 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01003399 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01003400 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01003401 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02003402 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcif2de0ed2019-05-02 14:13:18 +02003403 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01003404 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01003405 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejci95710c92019-02-11 15:49:55 +01003406 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejciccd20f12019-02-15 14:12:27 +01003407 cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02003408 };
3409
3410 return cmocka_run_group_tests(tests, NULL, NULL);
3411}