blob: fd8c766ac33ddbc92388f1efb2bdd7888c1eabd1 [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010020#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010021#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema.c"
24#include "../../src/context.c"
25#include "../../src/hash_table.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020026
Radek Krejcidd4e8d42018-10-16 14:55:43 +020027#include <stdarg.h>
28#include <stddef.h>
29#include <setjmp.h>
30#include <cmocka.h>
31
32#include <stdio.h>
33#include <string.h>
34
35#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020036
37#define BUFSIZE 1024
38char logbuf[BUFSIZE] = {0};
39
40/* set to 0 to printing error messages to stderr instead of checking them in code */
41#define ENABLE_LOGGER_CHECKING 1
42
43#if ENABLE_LOGGER_CHECKING
44static void
45logger(LY_LOG_LEVEL level, const char *msg, const char *path)
46{
47 (void) level; /* unused */
48
Radek Krejci87616bb2018-10-31 13:30:52 +010049 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020050 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
51 } else {
52 strncpy(logbuf, msg, BUFSIZE - 1);
53 }
54}
55#endif
56
57static int
58logger_setup(void **state)
59{
60 (void) state; /* unused */
61#if ENABLE_LOGGER_CHECKING
62 ly_set_log_clb(logger, 1);
63#endif
64 return 0;
65}
66
Radek Krejci4f28eda2018-11-12 11:46:16 +010067static int
68logger_teardown(void **state)
69{
70 (void) state; /* unused */
71#if ENABLE_LOGGER_CHECKING
72 if (*state) {
73 fprintf(stderr, "%s\n", logbuf);
74 }
75#endif
76 return 0;
77}
78
Radek Krejcidd4e8d42018-10-16 14:55:43 +020079void
80logbuf_clean(void)
81{
82 logbuf[0] = '\0';
83}
84
85#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010086# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020087#else
88# define logbuf_assert(str)
89#endif
90
Radek Krejcid05cbd92018-12-05 14:26:40 +010091static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
92 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
93 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
94{
95 *module_data = user_data;
96 *format = LYS_IN_YANG;
97 *free_module_data = NULL;
98 return LY_SUCCESS;
99}
100
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200101static void
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100102reset_mod(struct ly_ctx *ctx, struct lys_module *module)
103{
104 lysc_module_free(module->compiled, NULL);
105 lysp_module_free(module->parsed);
106
107 FREE_STRING(module->ctx, module->name);
108 FREE_STRING(module->ctx, module->ns);
109 FREE_STRING(module->ctx, module->prefix);
110 FREE_STRING(module->ctx, module->filepath);
111 FREE_STRING(module->ctx, module->org);
112 FREE_STRING(module->ctx, module->contact);
113 FREE_STRING(module->ctx, module->dsc);
114 FREE_STRING(module->ctx, module->ref);
115
116 memset(module, 0, sizeof *module);
117 module->ctx = ctx;
118}
119
120static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200121test_module(void **state)
122{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100123 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200124
125 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +0100126 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200127 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200128 struct lysc_feature *f;
129 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200130
131 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200132 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100133 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100134 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200135
Radek Krejcif8f882a2018-10-31 14:51:15 +0100136 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
137 logbuf_assert("Invalid argument mod (lys_compile()).");
138 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
139 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100140 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100141 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200142 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100143 assert_string_equal("test", mod.name);
144 assert_string_equal("urn:test", mod.ns);
145 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200146 /* features */
147 assert_non_null(mod.compiled->features);
148 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200149 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200150 assert_non_null(f->iffeatures);
151 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200152 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200153 assert_non_null(iff->expr);
154 assert_non_null(iff->features);
155 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200156 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200157
Radek Krejci86d106e2018-10-18 09:53:19 +0200158 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200159
Radek Krejcif8f882a2018-10-31 14:51:15 +0100160 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200161 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200162
Radek Krejci86d106e2018-10-18 09:53:19 +0200163 lysc_module_free(mod.compiled, NULL);
164 mod.compiled = NULL;
165
Radek Krejci151a5b72018-10-19 14:21:44 +0200166 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200167 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100168 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
169 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
170 assert_null(mod.parsed);
171 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100172
173 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100174 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
175 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100176 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
177 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
178 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100179 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100180
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100181 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100182 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200183}
184
Radek Krejci151a5b72018-10-19 14:21:44 +0200185static void
186test_feature(void **state)
187{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100188 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200189
Radek Krejci313d9902018-11-08 09:42:58 +0100190 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100191 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200192 const char *str;
193 struct lysc_feature *f, *f1;
194
195 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
196 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100197 "feature orfeature {if-feature \"f1 or f2\";}\n"
198 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200199 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200200 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100201 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
202 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200203
Radek Krejci313d9902018-11-08 09:42:58 +0100204 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100205 reset_mod(ctx.ctx, &mod);
206
207 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100208 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200209 assert_non_null(mod.compiled);
210 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100211 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200212 /* all features are disabled by default */
213 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
214 assert_int_equal(0, lysc_feature_value(f));
215 }
216 /* enable f1 */
217 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200218 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200219 assert_int_equal(1, lysc_feature_value(f1));
220
Radek Krejci87616bb2018-10-31 13:30:52 +0100221 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200222 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200223 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100224 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200225 assert_int_equal(1, lysc_feature_value(f));
226
Radek Krejci87616bb2018-10-31 13:30:52 +0100227 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200228 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200229 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100230 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
231 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200232 assert_int_equal(0, lysc_feature_value(f));
233
234 /* first enable f2, so f5 can be enabled then */
235 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100236 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200237 assert_int_equal(1, lysc_feature_value(f));
238
239 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200240 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200241 assert_int_equal(0, lysc_feature_value(f));
242 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
243 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
244 assert_int_equal(0, lysc_feature_value(f));
245
Radek Krejci87616bb2018-10-31 13:30:52 +0100246 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200247 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200248 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
249 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200250 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100251 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200252 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200253 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200254 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200255 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200256
257 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200258 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200259 /* long evaluation of f8 to need to reallocate internal stack for operators */
260 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200261
Radek Krejci87616bb2018-10-31 13:30:52 +0100262 /* double negation of disabled f1 -> disabled */
263 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
264
Radek Krejci38920282018-11-01 09:24:16 +0100265 /* disable all features */
266 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
267 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
268 assert_int_equal(0, lys_feature_value(&mod, f->name));
269 }
270 /* re-setting already set feature */
271 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
272 assert_int_equal(0, lys_feature_value(&mod, "f1"));
273
274 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100275 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
276 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
277 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100278 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
279 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100280 /* test if not changed */
281 assert_int_equal(1, lys_feature_value(&mod, "f1"));
282 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100283
Radek Krejci1aefdf72018-11-01 11:01:39 +0100284 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100285 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
286 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
287
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100288 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100289
290 /* some invalid expressions */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100291 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 +0100292 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100293 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100294 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100295
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100296 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 +0100297 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100298 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100299 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100300
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100301 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 +0100302 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100303 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100304 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100305
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100306 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 +0100307 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100308 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100309 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100310
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100311 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 +0100312 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100313 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100314 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100315
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100316 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 +0100317 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100318 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100319 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100320
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100321 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100322 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100323 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100324 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100325
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100326 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 +0100327 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
328 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100329 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100330
331 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sb {belongs-to b {prefix b;} feature f1;}");
332 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b{namespace urn:b; prefix b; include sb;feature f1;}", LYS_IN_YANG));
333 assert_int_equal(LY_EVALID, lys_compile(modp, 0));
334 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
335
Radek Krejci1aefdf72018-11-01 11:01:39 +0100336 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100337 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100338 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
339 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100340 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 +0100341 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
342 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
343 assert_int_equal(0, lys_feature_value(modp, "f1"));
344 assert_int_equal(1, lys_feature_value(modp, "f2"));
345
Radek Krejcid05cbd92018-12-05 14:26:40 +0100346 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100347 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200348}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200349
Radek Krejci478020e2018-10-30 16:02:14 +0100350static void
351test_identity(void **state)
352{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100353 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100354
355 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100356 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100357 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100358 const char *mod2_str = "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;} identity b4 {base b:b1; base b3;}}";
Radek Krejci478020e2018-10-30 16:02:14 +0100359
360 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
361 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
362 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejci7f2a5362018-11-28 13:05:37 +0100363 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100364 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100365
366 assert_non_null(mod1->compiled);
367 assert_non_null(mod1->compiled->identities);
368 assert_non_null(mod2->compiled);
369 assert_non_null(mod2->compiled->identities);
370
371 assert_non_null(mod1->compiled->identities[0].derived);
372 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
373 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
374 assert_non_null(mod2->compiled->identities[0].derived);
375 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
376 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
377 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
378 assert_non_null(mod2->compiled->identities[1].derived);
379 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
380 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
381 assert_non_null(mod2->compiled->identities[2].derived);
382 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
383 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
384
Radek Krejcid05cbd92018-12-05 14:26:40 +0100385 assert_non_null(mod1 = lys_parse_mem(ctx, "module c{namespace urn:c; prefix c; identity i1;identity i1;}", LYS_IN_YANG));
386 assert_int_equal(LY_EVALID, lys_compile(mod1, 0));
387 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
388
389 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sd {belongs-to d {prefix d;} identity i1;}");
390 assert_non_null(mod1 = lys_parse_mem(ctx, "module d{namespace urn:d; prefix d; include sd;identity i1;}", LYS_IN_YANG));
391 assert_int_equal(LY_EVALID, lys_compile(mod1, 0));
392 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
393
Radek Krejci7f2a5362018-11-28 13:05:37 +0100394 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100395 ly_ctx_destroy(ctx, NULL);
396}
397
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100398static void
399test_node_container(void **state)
400{
401 (void) state; /* unused */
402
403 struct ly_ctx *ctx;
404 struct lys_module *mod;
405 struct lysc_node_container *cont;
406
407 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
408 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
409 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
410 assert_non_null(mod->compiled);
411 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
412 assert_int_equal(LYS_CONTAINER, cont->nodetype);
413 assert_string_equal("c", cont->name);
414 assert_true(cont->flags & LYS_CONFIG_W);
415 assert_true(cont->flags & LYS_STATUS_CURR);
416
Radek Krejci98094b32018-11-02 16:21:47 +0100417 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 Krejcibd8d9ba2018-11-02 16:06:26 +0100418 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100419 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100420 assert_non_null(mod->compiled);
421 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
422 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100423 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100424 assert_non_null((cont = (struct lysc_node_container*)cont->child));
425 assert_int_equal(LYS_CONTAINER, cont->nodetype);
426 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100427 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100428 assert_string_equal("child", cont->name);
429
430 ly_ctx_destroy(ctx, NULL);
431}
432
Radek Krejci0e5d8382018-11-28 16:37:53 +0100433static void
434test_node_leaflist(void **state)
435{
436 *state = test_node_leaflist;
437
438 struct ly_ctx *ctx;
439 struct lys_module *mod;
440 struct lysc_type *type;
441 struct lysc_node_leaflist *ll;
442
443 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
444
445 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
446 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
447 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
448 "leaf-list ll2 {type leafref {path ../target;}}"
449 "leaf target {type int8;}}",
450 LYS_IN_YANG));
451 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
452 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
453 assert_non_null(type);
454 assert_int_equal(1, type->refcount);
455 assert_int_equal(LY_TYPE_UNION, type->basetype);
456 assert_non_null(((struct lysc_type_union*)type)->types);
457 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
458 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
459 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
460 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
461 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
462 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
463 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
464 assert_non_null(type);
465 assert_int_equal(1, type->refcount);
466 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
467 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
468 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
469
Radek Krejci6bb080b2018-11-28 17:23:41 +0100470 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 +0100471 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
472 assert_non_null(mod->compiled);
473 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
474 assert_int_equal(0, ll->min);
475 assert_int_equal((uint32_t)-1, ll->max);
476
Radek Krejci6bb080b2018-11-28 17:23:41 +0100477 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 +0100478 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100479 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100480 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
481 assert_non_null(mod->compiled);
482 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
483 assert_non_null(ll->dflts);
484 assert_int_equal(3, ll->type->refcount);
485 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
486 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100487 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100488 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100489 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
490 assert_non_null(ll->dflts);
491 assert_int_equal(3, ll->type->refcount);
492 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
493 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100494 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
495
496 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
497 * TODO test also, RPC output parameters and notification content */
498 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
499 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
500 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
501 /* but warning is present: */
502 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
503 assert_non_null(mod->compiled);
504 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
505 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100506
507 /* invalid */
508 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
509 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
510 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
511
512 assert_non_null(mod = 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));
513 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
514 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
515
516 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
517 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
518 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
519 assert_non_null(mod->compiled);
520 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
521 assert_non_null(ll->dflts);
522 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
523 assert_non_null(mod = lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
524 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
525 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
526 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
527
Radek Krejci0e5d8382018-11-28 16:37:53 +0100528 *state = NULL;
529 ly_ctx_destroy(ctx, NULL);
530}
531
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100532static void
533test_node_list(void **state)
534{
535 *state = test_node_list;
536
537 struct ly_ctx *ctx;
538 struct lys_module *mod;
539 struct lysc_node_list *list;
540
541 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
542
543 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
544 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
545 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
546 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
547 list = (struct lysc_node_list*)mod->compiled->data;
548 assert_non_null(list);
549 assert_non_null(list->keys);
550 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
551 assert_string_equal("x", list->keys[0]->name);
552 assert_string_equal("y", list->keys[1]->name);
553 assert_non_null(list->child);
554 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
555 assert_true(list->child->flags & LYS_KEY);
556 assert_true(list->child->next->flags & LYS_KEY);
557 list = (struct lysc_node_list*)mod->compiled->data->next;
558 assert_non_null(list);
559 assert_null(list->keys);
560 assert_non_null(list->child);
561 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
562 assert_false(list->child->flags & LYS_KEY);
563
564 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
565 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100566 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100567 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
568 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
569 list = (struct lysc_node_list*)mod->compiled->data;
570 assert_non_null(list);
571 assert_string_equal("l", list->name);
572 assert_non_null(list->keys);
573 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
574 assert_string_equal("a", list->keys[0]->name);
575 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100576 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100577 assert_non_null(list->uniques);
578 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
579 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
580 assert_string_equal("a", list->uniques[0][0]->name);
581 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
582 assert_string_equal("b", list->uniques[0][1]->name);
583 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
584 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
585 assert_string_equal("e", list->uniques[1][0]->name);
586 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
587 assert_string_equal("d", list->uniques[1][1]->name);
588 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
589
Radek Krejci665421c2018-12-05 08:03:39 +0100590 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
591 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
592 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
593 list = (struct lysc_node_list*)mod->compiled->data;
594 assert_non_null(list);
595 assert_string_equal("l", list->name);
596 assert_non_null(list->keys);
597 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
598 assert_string_equal("a", list->keys[0]->name);
599 assert_true(list->keys[0]->flags & LYS_KEY);
600 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
601
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100602 /* invalid */
603 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
604 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
605 logbuf_assert("Missing key in list representing configuration data.");
606
607 assert_non_null(mod = lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
608 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
609 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
610 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
611
612 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
613 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
614 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
615 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
616
617 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
618 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
619 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
620 logbuf_assert("Key of the configuration list must not be status leaf.");
621
622 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
623 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
624 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
625 logbuf_assert("Configuration node cannot be child of any state data node.");
626
627 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
628 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
629 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
630 logbuf_assert("The list's key \"x\" not found.");
631
632 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
633 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
634 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
635 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
636
637 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
638 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
639 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
640 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
641
Radek Krejci665421c2018-12-05 08:03:39 +0100642 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
643 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
644 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
645 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
646
647 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
648 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
649 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
650 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
651
652 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
653 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
654 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
655 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
656
657 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
658 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
659 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
660 logbuf_assert("Duplicated key identifier \"x\".");
661
662 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
663 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
664 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
665 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
666
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100667 *state = NULL;
668 ly_ctx_destroy(ctx, NULL);
669}
670
Radek Krejci056d0a82018-12-06 16:57:25 +0100671static void
672test_node_choice(void **state)
673{
674 *state = test_node_choice;
675
676 struct ly_ctx *ctx;
677 struct lys_module *mod;
678 struct lysc_node_choice *ch;
679 struct lysc_node_case *cs;
680
681 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
682
683 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100684 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
685 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100686 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
687 ch = (struct lysc_node_choice*)mod->compiled->data;
688 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100689 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100690 cs = ch->cases;
691 assert_non_null(cs);
692 assert_string_equal("a", cs->name);
693 assert_ptr_equal(ch, cs->parent);
694 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100695 assert_string_equal("a1", cs->child->name);
696 assert_non_null(cs->child->next);
697 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100698 assert_ptr_equal(cs, cs->child->parent);
699 cs = (struct lysc_node_case*)cs->next;
700 assert_non_null(cs);
701 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100702 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100703 assert_ptr_equal(ch, cs->parent);
704 assert_non_null(cs->child);
705 assert_string_equal("b", cs->child->name);
706 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100707 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
708 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100709 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100710 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100711
712 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
713 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
714 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
715 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
716 assert_non_null(mod = lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
717 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
718 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
719 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
720 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
721 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
722 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
723 logbuf_assert("Duplicate identifier \"a\" of case statement.");
724 assert_non_null(mod = lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
725 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
726 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
727 logbuf_assert("Duplicate identifier \"b\" of case statement.");
728
Radek Krejcia9026eb2018-12-12 16:04:47 +0100729 assert_non_null(mod = lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
730 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
731 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
732 logbuf_assert("Default case \"c\" not found.");
733 assert_non_null(mod = lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
734 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
735 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
736 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
737 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
738 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
739 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
740 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
741 /* TODO check with mandatory nodes from augment placed into the case */
742
Radek Krejci056d0a82018-12-06 16:57:25 +0100743 *state = NULL;
744 ly_ctx_destroy(ctx, NULL);
745}
746
Radek Krejci9800fb82018-12-13 14:26:23 +0100747static void
748test_node_anydata(void **state)
749{
750 *state = test_node_anydata;
751
752 struct ly_ctx *ctx;
753 struct lys_module *mod;
754 struct lysc_node_anydata *any;
755
756 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
757
758 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
759 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
760 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
761 any = (struct lysc_node_anydata*)mod->compiled->data;
762 assert_non_null(any);
763 assert_int_equal(LYS_ANYDATA, any->nodetype);
764 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
765
766 logbuf_clean();
767 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
768 "anyxml any;}", LYS_IN_YANG));
769 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
770 any = (struct lysc_node_anydata*)mod->compiled->data;
771 assert_non_null(any);
772 assert_int_equal(LYS_ANYXML, any->nodetype);
773 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
774 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
775
776 /* invalid */
777 assert_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;anydata any;}", LYS_IN_YANG));
778 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
779
780 *state = NULL;
781 ly_ctx_destroy(ctx, NULL);
782}
783
Radek Krejci0f07bed2018-11-13 14:01:40 +0100784/**
785 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
786 * do not test the expression itself
787 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100788static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100789test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100790{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100791 *state = test_type_range;
792
793 struct ly_ctx *ctx;
794 struct lys_module *mod;
795 struct lysc_type *type;
796
797 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
798
799 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));
800 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
801 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
802 assert_non_null(type);
803 assert_int_equal(LY_TYPE_INT8, type->basetype);
804 assert_non_null(((struct lysc_type_num*)type)->range);
805 assert_non_null(((struct lysc_type_num*)type)->range->parts);
806 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
807 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
808 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
809 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
810 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
811
812 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));
813 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
814 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
815 assert_non_null(type);
816 assert_int_equal(LY_TYPE_INT16, type->basetype);
817 assert_non_null(((struct lysc_type_num*)type)->range);
818 assert_non_null(((struct lysc_type_num*)type)->range->parts);
819 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
820 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
821 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
822 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
823 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
824
825 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));
826 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
827 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
828 assert_non_null(type);
829 assert_int_equal(LY_TYPE_INT32, type->basetype);
830 assert_non_null(((struct lysc_type_num*)type)->range);
831 assert_non_null(((struct lysc_type_num*)type)->range->parts);
832 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
833 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
834 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
835 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
836 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
837
838 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));
839 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
840 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
841 assert_non_null(type);
842 assert_int_equal(LY_TYPE_INT64, type->basetype);
843 assert_non_null(((struct lysc_type_num*)type)->range);
844 assert_non_null(((struct lysc_type_num*)type)->range->parts);
845 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
846 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
847 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
848 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
849 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
850
851 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));
852 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
853 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
854 assert_non_null(type);
855 assert_int_equal(LY_TYPE_UINT8, type->basetype);
856 assert_non_null(((struct lysc_type_num*)type)->range);
857 assert_non_null(((struct lysc_type_num*)type)->range->parts);
858 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
859 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
860 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
861 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
862 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
863
864 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));
865 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
866 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
867 assert_non_null(type);
868 assert_int_equal(LY_TYPE_UINT16, type->basetype);
869 assert_non_null(((struct lysc_type_num*)type)->range);
870 assert_non_null(((struct lysc_type_num*)type)->range->parts);
871 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
872 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
873 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
874 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
875 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
876
877 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));
878 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
879 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
880 assert_non_null(type);
881 assert_int_equal(LY_TYPE_UINT32, type->basetype);
882 assert_non_null(((struct lysc_type_num*)type)->range);
883 assert_non_null(((struct lysc_type_num*)type)->range->parts);
884 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
885 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
886 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
887 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
888 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
889
890 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));
891 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
892 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
893 assert_non_null(type);
894 assert_int_equal(LY_TYPE_UINT64, type->basetype);
895 assert_non_null(((struct lysc_type_num*)type)->range);
896 assert_non_null(((struct lysc_type_num*)type)->range->parts);
897 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
898 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
899 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
900 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
901 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
902
903 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
904 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
905 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
906 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
907 assert_non_null(type);
908 assert_int_equal(3, type->refcount);
909 assert_int_equal(LY_TYPE_UINT8, type->basetype);
910 assert_non_null(((struct lysc_type_num*)type)->range);
911 assert_non_null(((struct lysc_type_num*)type)->range->parts);
912 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
913
Radek Krejcif1394042019-01-08 10:53:34 +0100914 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
915 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
916 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
917 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
918 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
919 assert_non_null(type);
920 assert_int_equal(1, type->refcount);
921 assert_int_equal(LY_TYPE_UINT8, type->basetype);
922 assert_non_null(((struct lysc_type_num*)type)->range);
923 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
924 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
925 assert_non_null(((struct lysc_type_num*)type)->range->parts);
926 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
927 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
928 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
929
Radek Krejci0f07bed2018-11-13 14:01:40 +0100930 *state = NULL;
931 ly_ctx_destroy(ctx, NULL);
932}
933
934static void
935test_type_length(void **state)
936{
937 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100938
939 struct ly_ctx *ctx;
940 struct lys_module *mod;
941 struct lysc_type *type;
942
943 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
944
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100945 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 +0100946 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
947 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
948 assert_non_null(type);
949 assert_non_null(((struct lysc_type_bin*)type)->length);
950 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100951 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
952 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100953 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
954 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
955 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
956
957 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
958 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
959 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
960 assert_non_null(type);
961 assert_non_null(((struct lysc_type_bin*)type)->length);
962 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
963 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100964 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
965 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100966
967 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));
968 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
969 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
970 assert_non_null(type);
971 assert_non_null(((struct lysc_type_bin*)type)->length);
972 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
973 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
974 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100975 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100976
977 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
978 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
979 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
980 assert_non_null(type);
981 assert_non_null(((struct lysc_type_bin*)type)->length);
982 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
983 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
984 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
985 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
986
987 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));
988 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
989 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
990 assert_non_null(type);
991 assert_non_null(((struct lysc_type_bin*)type)->length);
992 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
993 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
994 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
995 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
996
997 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));
998 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
999 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1000 assert_non_null(type);
1001 assert_non_null(((struct lysc_type_bin*)type)->length);
1002 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1003 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1004 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1005 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1006 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1007 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1008
1009 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));
1010 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1011 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1012 assert_non_null(type);
1013 assert_non_null(((struct lysc_type_bin*)type)->length);
1014 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1015 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1016 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1017 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1018 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1019 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1020
Radek Krejcib31c8992018-11-12 16:29:16 +01001021 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
1022 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
1023 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1024 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1025 assert_non_null(type);
1026 assert_non_null(((struct lysc_type_bin*)type)->length);
1027 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1028 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1029 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1030 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1031
1032 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
1033 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
1034 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1035 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1036 assert_non_null(type);
1037 assert_non_null(((struct lysc_type_bin*)type)->length);
1038 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1039 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1040 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1041 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1042
1043 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1044 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
1045 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1046 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1047 assert_non_null(type);
1048 assert_non_null(((struct lysc_type_bin*)type)->length);
1049 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1050 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1051 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1052 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1053 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1054 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1055
1056 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 +01001057 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001058 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1059 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1060 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001061 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001062 assert_non_null(((struct lysc_type_bin*)type)->length);
1063 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1064 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1065 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001066 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001067 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1068 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001069 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001070 assert_non_null(((struct lysc_type_bin*)type)->length);
1071 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1072 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1073 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1074 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1075
Radek Krejci56aa27c2018-11-13 14:21:59 +01001076 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1077 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001078 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1079 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1080 assert_non_null(type);
1081 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001082 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001083 assert_non_null(((struct lysc_type_str*)type)->length);
1084 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1085 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1086 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1087 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001088
Radek Krejci5969f272018-11-23 10:03:58 +01001089 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1090 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
1091 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1092 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1093 assert_non_null(type);
1094 assert_int_equal(LY_TYPE_STRING, type->basetype);
1095 assert_int_equal(1, type->refcount);
1096 assert_non_null(((struct lysc_type_str*)type)->length);
1097 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1098 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1099 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1100 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1101
Radek Krejci4f28eda2018-11-12 11:46:16 +01001102 /* invalid values */
1103 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
1104 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1105 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
1106 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
1107 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1108 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
1109 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
1110 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1111 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
1112 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
1113 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1114 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
1115 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
1116 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1117 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
1118 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
1119 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1120 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001121 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
1122 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1123 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
1124 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
1125 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1126 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
1127 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
1128 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1129 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
1130 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
1131 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1132 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
1133 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
1134 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1135 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +01001136 assert_non_null(mod = lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG));
1137 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1138 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001139
1140 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1141 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
1142 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1143 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
1144 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1145 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
1146 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1147 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
1148 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1149 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
1150 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1151 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
1152 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1153 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
1154 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1155 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
1156 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
1157 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
1158 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1159 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
1160 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
1161 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
1162 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1163 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +01001164 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
1165 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
1166 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1167 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001168
Radek Krejci495903e2018-11-13 11:30:45 +01001169 assert_non_null(mod = lys_parse_mem(ctx, "module ss {namespace urn:rr;prefix rr;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
1170 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1171 logbuf_assert("Invalid type restrictions for binary type.");
1172
Radek Krejci4f28eda2018-11-12 11:46:16 +01001173 *state = NULL;
1174 ly_ctx_destroy(ctx, NULL);
1175}
1176
Radek Krejcicda74152018-11-13 15:27:32 +01001177static void
1178test_type_pattern(void **state)
1179{
1180 *state = test_type_pattern;
1181
1182 struct ly_ctx *ctx;
1183 struct lys_module *mod;
1184 struct lysc_type *type;
1185
1186 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1187
Radek Krejci027d5802018-11-14 16:57:28 +01001188 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 +01001189 "pattern .* {error-app-tag errortag;error-message error;}"
1190 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
1191 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1192 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1193 assert_non_null(type);
1194 assert_non_null(((struct lysc_type_str*)type)->patterns);
1195 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1196 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1197 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1198 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1199 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1200 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1201 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1202
1203 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1204 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
1205 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1206 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1207 assert_non_null(type);
1208 assert_int_equal(LY_TYPE_STRING, type->basetype);
1209 assert_int_equal(1, type->refcount);
1210 assert_non_null(((struct lysc_type_str*)type)->patterns);
1211 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1212 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1213 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1214
Radek Krejci04bc1e92018-11-14 14:28:13 +01001215 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1216 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
1217 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1218 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1219 assert_non_null(type);
1220 assert_int_equal(LY_TYPE_STRING, type->basetype);
1221 assert_int_equal(1, type->refcount);
1222 assert_non_null(((struct lysc_type_str*)type)->patterns);
1223 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1224 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1225
Radek Krejcicda74152018-11-13 15:27:32 +01001226 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001227 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 +01001228 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
1229 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1230 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1231 assert_non_null(type);
1232 assert_non_null(((struct lysc_type_str*)type)->patterns);
1233 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1234 /* TODO check some data "^Å™$" */
1235
1236 *state = NULL;
1237 ly_ctx_destroy(ctx, NULL);
1238}
1239
Radek Krejci8b764662018-11-14 14:15:13 +01001240static void
1241test_type_enum(void **state)
1242{
1243 *state = test_type_enum;
1244
1245 struct ly_ctx *ctx;
1246 struct lys_module *mod;
1247 struct lysc_type *type;
1248
1249 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1250
Radek Krejci027d5802018-11-14 16:57:28 +01001251 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 +01001252 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1253 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
1254 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1255 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1256 assert_non_null(type);
1257 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1258 assert_non_null(((struct lysc_type_enum*)type)->enums);
1259 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1260 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1261 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1262 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1263 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1264 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1265 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1266 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1267 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1268 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1269 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1270 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1271 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1272 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1273
Radek Krejci027d5802018-11-14 16:57:28 +01001274 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 {"
1275 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001276 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1277 LYS_IN_YANG));
1278 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1279 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1280 assert_non_null(type);
1281 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1282 assert_non_null(((struct lysc_type_enum*)type)->enums);
1283 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1284 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1285 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1286 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1287 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1288
1289
1290 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001291 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1292 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1293 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 +01001294 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1295 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1296 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1297 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1298 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1299 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1300 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1301 "enum one; enum one;}}}", LYS_IN_YANG));
1302 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1303 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1304 "enum '';}}}", LYS_IN_YANG));
1305 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1306 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1307 "enum ' x';}}}", LYS_IN_YANG));
1308 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1309 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1310 "enum 'x ';}}}", LYS_IN_YANG));
1311 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1312 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1313 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1314 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1315
1316 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
1317 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1318 logbuf_assert("Missing enum substatement for enumeration type.");
1319
Radek Krejci027d5802018-11-14 16:57:28 +01001320 assert_non_null(mod = 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 +01001321 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
1322 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1323 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1324
Radek Krejci027d5802018-11-14 16:57:28 +01001325 assert_non_null(mod = 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 +01001326 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
1327 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1328 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
1329 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type enumeration {enum x {value 2147483647;}enum y;}}}",
1330 LYS_IN_YANG));
1331 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1332 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1333
1334 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type enumeration {enum x {value 1;}enum y {value 1;}}}}",
1335 LYS_IN_YANG));
1336 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1337 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1338
Radek Krejci04bc1e92018-11-14 14:28:13 +01001339 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
1340 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1341 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001342 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001343
Radek Krejci027d5802018-11-14 16:57:28 +01001344 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
1345 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1346 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1347 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1348
Radek Krejci8b764662018-11-14 14:15:13 +01001349 *state = NULL;
1350 ly_ctx_destroy(ctx, NULL);
1351}
1352
1353static void
1354test_type_bits(void **state)
1355{
1356 *state = test_type_bits;
1357
1358 struct ly_ctx *ctx;
1359 struct lys_module *mod;
1360 struct lysc_type *type;
1361
1362 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1363
Radek Krejci027d5802018-11-14 16:57:28 +01001364 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 +01001365 "bit automin; bit one {if-feature f; position 1;}"
1366 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
1367 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1368 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1369 assert_non_null(type);
1370 assert_int_equal(LY_TYPE_BITS, type->basetype);
1371 assert_non_null(((struct lysc_type_bits*)type)->bits);
1372 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1373 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1374 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1375 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1376 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1377 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1378 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1379 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1380 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1381 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1382 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1383 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1384
Radek Krejci027d5802018-11-14 16:57:28 +01001385 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 {"
1386 "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 +01001387 LYS_IN_YANG));
1388 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1389 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1390 assert_non_null(type);
1391 assert_int_equal(LY_TYPE_BITS, type->basetype);
1392 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001393 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1394 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1395 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1396 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1397 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1398 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1399 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001400
1401
1402 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001403 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1404 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1405 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 +01001406 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1407 "bit one {position -1;}}}}", LYS_IN_YANG));
1408 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1409 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1410 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1411 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1412 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1413 "bit one; bit one;}}}", LYS_IN_YANG));
1414 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1415 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1416 "bit '11';}}}", LYS_IN_YANG));
1417 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1418 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1419 "bit 'x1$1';}}}", LYS_IN_YANG));
1420 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1421
1422 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
1423 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1424 logbuf_assert("Missing bit substatement for bits type.");
1425
Radek Krejci027d5802018-11-14 16:57:28 +01001426 assert_non_null(mod = 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 +01001427 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
1428 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1429 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1430
Radek Krejci027d5802018-11-14 16:57:28 +01001431 assert_non_null(mod = 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 +01001432 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
1433 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1434 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
1435 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}",
1436 LYS_IN_YANG));
1437 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1438 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1439
1440 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {value 1;}bit y {value 1;}}}}",
1441 LYS_IN_YANG));
1442 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1443 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1444
Radek Krejci04bc1e92018-11-14 14:28:13 +01001445 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1446 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1447 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001448 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001449
Radek Krejci027d5802018-11-14 16:57:28 +01001450 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1451 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1452 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1453 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1454
Radek Krejci8b764662018-11-14 14:15:13 +01001455 *state = NULL;
1456 ly_ctx_destroy(ctx, NULL);
1457}
1458
Radek Krejci6cba4292018-11-15 17:33:29 +01001459static void
1460test_type_dec64(void **state)
1461{
1462 *state = test_type_dec64;
1463
1464 struct ly_ctx *ctx;
1465 struct lys_module *mod;
1466 struct lysc_type *type;
1467
1468 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1469
1470 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1471 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
1472 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1473 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1474 assert_non_null(type);
1475 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1476 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1477 assert_non_null(((struct lysc_type_dec*)type)->range);
1478 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1479 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1480 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1481 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1482
1483 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1484 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1485 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1486 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1487 assert_non_null(type);
1488 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1489 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1490 assert_non_null(((struct lysc_type_dec*)type)->range);
1491 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1492 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1493 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1494 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1495 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1496 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1497 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1498 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1499
1500 /* invalid cases */
1501 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1502 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1503 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1504 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1505 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1506 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1507
1508 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
1509 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1510 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1511
Radek Krejci643c8242018-11-15 17:51:11 +01001512 assert_non_null(mod = lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
1513 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001514 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001515
Radek Krejci6cba4292018-11-15 17:33:29 +01001516 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
1517 "range '3.142';}}}", LYS_IN_YANG));
1518 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1519 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1520
1521 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
1522 "range '4 | 3.14';}}}", LYS_IN_YANG));
1523 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1524 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1525
Radek Krejci643c8242018-11-15 17:51:11 +01001526 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
1527 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
1528 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1529 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1530
1531 assert_non_null(mod = lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
1532 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1533 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1534 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1535
Radek Krejci6cba4292018-11-15 17:33:29 +01001536 *state = NULL;
1537 ly_ctx_destroy(ctx, NULL);
1538}
1539
Radek Krejci16c0f822018-11-16 10:46:10 +01001540static void
1541test_type_instanceid(void **state)
1542{
1543 *state = test_type_instanceid;
1544
1545 struct ly_ctx *ctx;
1546 struct lys_module *mod;
1547 struct lysc_type *type;
1548
1549 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1550
1551 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1552 "leaf l1 {type instance-identifier {require-instance true;}}"
1553 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
1554 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1555 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1556 assert_non_null(type);
1557 assert_int_equal(LY_TYPE_INST, type->basetype);
1558 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1559
1560 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1561 assert_non_null(type);
1562 assert_int_equal(LY_TYPE_INST, type->basetype);
1563 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1564
1565 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1566 assert_non_null(type);
1567 assert_int_equal(LY_TYPE_INST, type->basetype);
1568 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1569
1570 /* invalid cases */
1571 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1572 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1573
1574 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
1575 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1576 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1577
1578 *state = NULL;
1579 ly_ctx_destroy(ctx, NULL);
1580}
1581
Radek Krejci555cb5b2018-11-16 14:54:33 +01001582static void
1583test_type_identityref(void **state)
1584{
1585 *state = test_type_identityref;
1586
1587 struct ly_ctx *ctx;
1588 struct lys_module *mod;
1589 struct lysc_type *type;
1590
1591 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1592
1593 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;}"
1594 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001595 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001596 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1597 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1598 assert_non_null(type);
1599 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1600 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1601 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1602 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1603
1604 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1605 assert_non_null(type);
1606 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1607 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1608 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1609 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1610 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1611
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001612 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1613 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
1614 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1615 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1616 assert_non_null(type);
1617 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1618 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1619 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1620 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1621 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1622
Radek Krejci555cb5b2018-11-16 14:54:33 +01001623 /* invalid cases */
1624 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
1625 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1626 logbuf_assert("Missing base substatement for identityref type.");
1627
1628 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
1629 "leaf l {type mytype;}}", LYS_IN_YANG));
1630 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1631 logbuf_assert("Missing base substatement for identityref type mytype.");
1632
1633 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
1634 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
1635 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001636 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001637
1638 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
1639 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1640 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001641 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001642
1643 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
1644 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
1645 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1646 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1647
Radek Krejci322614f2018-11-16 15:05:44 +01001648 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
1649 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1650 logbuf_assert("Unable to find base (j) of identityref.");
1651
1652 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
1653 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1654 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1655
Radek Krejci555cb5b2018-11-16 14:54:33 +01001656 *state = NULL;
1657 ly_ctx_destroy(ctx, NULL);
1658}
1659
Radek Krejcia3045382018-11-22 14:30:31 +01001660static void
1661test_type_leafref(void **state)
1662{
1663 *state = test_type_leafref;
1664
1665 struct ly_ctx *ctx;
1666 struct lys_module *mod;
1667 struct lysc_type *type;
1668 const char *path, *name, *prefix;
1669 size_t prefix_len, name_len;
1670 int parent_times, has_predicate;
1671
1672 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1673
1674 /* lys_path_token() */
1675 path = "invalid_path";
1676 parent_times = 0;
1677 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1678 path = "..";
1679 parent_times = 0;
1680 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1681 path = "..[";
1682 parent_times = 0;
1683 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1684 path = "../";
1685 parent_times = 0;
1686 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1687 path = "/";
1688 parent_times = 0;
1689 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1690
1691 path = "../../pref:id/xxx[predicate]/invalid!!!";
1692 parent_times = 0;
1693 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1694 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1695 assert_int_equal(4, prefix_len);
1696 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1697 assert_int_equal(2, name_len);
1698 assert_int_equal(0, strncmp("id", name, name_len));
1699 assert_int_equal(2, parent_times);
1700 assert_int_equal(0, has_predicate);
1701 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1702 assert_string_equal("[predicate]/invalid!!!", path);
1703 assert_int_equal(0, prefix_len);
1704 assert_null(prefix);
1705 assert_int_equal(3, name_len);
1706 assert_int_equal(0, strncmp("xxx", name, name_len));
1707 assert_int_equal(1, has_predicate);
1708 path += 11;
1709 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1710 assert_string_equal("!!!", path);
1711 assert_int_equal(0, prefix_len);
1712 assert_null(prefix);
1713 assert_int_equal(7, name_len);
1714 assert_int_equal(0, strncmp("invalid", name, name_len));
1715
1716 path = "/absolute/prefix:path";
1717 parent_times = 0;
1718 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1719 assert_string_equal("/prefix:path", path);
1720 assert_int_equal(0, prefix_len);
1721 assert_null(prefix);
1722 assert_int_equal(8, name_len);
1723 assert_int_equal(0, strncmp("absolute", name, name_len));
1724 assert_int_equal(-1, parent_times);
1725 assert_int_equal(0, has_predicate);
1726 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1727 assert_int_equal(0, *path);
1728 assert_int_equal(6, prefix_len);
1729 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1730 assert_int_equal(4, name_len);
1731 assert_int_equal(0, strncmp("path", name, name_len));
1732 assert_int_equal(0, has_predicate);
1733
1734 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001735 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 +01001736 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1737 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
1738 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1739 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1740 assert_non_null(type);
1741 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1742 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001743 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001744 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1745 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001746 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1747 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1748 assert_non_null(type);
1749 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1750 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001751 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1752 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1753 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001754 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1755
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001756 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 +01001757 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001758 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001759 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1760 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1761 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001762 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001763 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1764 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1765 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001766 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1767 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001768 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1769
1770 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001771 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 +01001772 "typedef mytype3 {type c:mytype {require-instance false;}}"
1773 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001774 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1775 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1776 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001777 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001778 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1779 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1780 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001781 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1782 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001783 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1784 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1785 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001786 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001787 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1788 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1789 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1790 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1791
Radek Krejci4ce68932018-11-28 12:53:36 +01001792 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1793 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1794 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
1795 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1796 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1797 assert_non_null(type);
1798 assert_int_equal(1, type->refcount);
1799 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1800 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1801 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1802 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1803 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1804 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1805
Radek Krejci58d171e2018-11-23 13:50:55 +01001806 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001807 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 +01001808 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1809 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
1810 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1811 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1812 assert_non_null(type);
1813 assert_int_equal(1, type->refcount);
1814 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1815 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1816 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1817 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1818 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1819
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001820 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1821 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1822 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001823 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001824 LYS_IN_YANG));
1825 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01001826 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001827 assert_non_null(type);
1828 assert_int_equal(1, type->refcount);
1829 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001830 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001831 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1832 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1833 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001834
Radek Krejci20e8a182018-12-07 11:43:21 +01001835 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1836 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1837 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1838 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
1839 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1840 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1841 assert_non_null(type);
1842 assert_int_equal(1, type->refcount);
1843 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1844 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1845 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1846 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1847 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1848
Radek Krejcia3045382018-11-22 14:30:31 +01001849 /* invalid paths */
1850 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
1851 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
1852 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1853 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
1854 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
1855 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
1856 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1857 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
1858 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
1859 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
1860 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1861 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
1862 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf target1 {type string;}container a {leaf target2 {type uint8;}}"
1863 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
1864 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1865 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
1866 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
1867 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
1868 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1869 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
1870 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
1871 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
1872 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1873 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
1874 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
1875 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
1876 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1877 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci2f4a0292018-11-22 15:41:53 +01001878 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
1879 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
1880 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1881 logbuf_assert("Missing path substatement for leafref type.");
1882 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
1883 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
1884 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1885 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001886 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
1887 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
1888 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1889 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1890 "applicable to the leafref itself.");
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001891 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
1892 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
1893 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1894 logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001895
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001896 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
1897 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
1898 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1899 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1900 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}"
1901 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
1902 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1903 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1904
Radek Krejcibade82a2018-12-05 10:13:48 +01001905 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
1906 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1907 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1908 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1909 LYS_IN_YANG));
1910 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1911 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1912
1913 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
1914 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1915 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1916 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1917 LYS_IN_YANG));
1918 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1919 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1920
1921 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
1922 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1923 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1924 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1925 LYS_IN_YANG));
1926 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1927 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1928
1929 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
1930 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1931 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1932 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1933 LYS_IN_YANG));
1934 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1935 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1936
1937 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
1938 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1939 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1940 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1941 LYS_IN_YANG));
1942 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1943 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1944
1945 assert_non_null(mod = lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
1946 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1947 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1948 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1949 LYS_IN_YANG));
1950 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1951 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1952
1953 assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
1954 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1955 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1956 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1957 LYS_IN_YANG));
1958 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1959 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1960
1961 assert_non_null(mod = lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
1962 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1963 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1964 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
1965 LYS_IN_YANG));
1966 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1967 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1968
1969 assert_non_null(mod = lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
1970 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1971 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1972 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
1973 LYS_IN_YANG));
1974 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1975 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1976
1977 assert_non_null(mod = lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
1978 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1979 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1980 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
1981 LYS_IN_YANG));
1982 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1983 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1984
1985 assert_non_null(mod = lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
1986 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1987 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1988 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
1989 LYS_IN_YANG));
1990 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1991 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1992
1993 assert_non_null(mod = lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
1994 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1995 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1996 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1997 LYS_IN_YANG));
1998 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1999 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
2000
2001 assert_non_null(mod = lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
2002 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2003 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2004 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
2005 LYS_IN_YANG));
2006 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2007 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
2008
2009 assert_non_null(mod = lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
2010 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
2011 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
2012 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
2013 LYS_IN_YANG));
2014 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2015 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
2016
2017 assert_non_null(mod = lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
2018 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
2019 "leaf ifname{type leafref{ path \"../interface/name\";}}"
2020 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
2021 LYS_IN_YANG));
2022 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2023 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
2024
Radek Krejci412ddfa2018-11-23 11:44:11 +01002025 /* circular chain */
2026 assert_non_null(mod = lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
2027 "leaf ref1 {type leafref {path /ref2;}}"
2028 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01002029 "leaf ref3 {type leafref {path /ref4;}}"
2030 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01002031 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2032 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
2033
Radek Krejcia3045382018-11-22 14:30:31 +01002034 *state = NULL;
2035 ly_ctx_destroy(ctx, NULL);
2036}
2037
Radek Krejci43699232018-11-23 14:59:46 +01002038static void
2039test_type_empty(void **state)
2040{
2041 *state = test_type_empty;
2042
2043 struct ly_ctx *ctx;
2044 struct lys_module *mod;
2045
2046 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2047
2048 /* invalid */
2049 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2050 "leaf l {type empty; default x;}}", LYS_IN_YANG));
2051 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2052 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
2053
2054 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
2055 "leaf l {type mytype;}}", LYS_IN_YANG));
2056 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2057 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
2058
2059 *state = NULL;
2060 ly_ctx_destroy(ctx, NULL);
2061}
2062
Radek Krejcicdfecd92018-11-26 11:27:32 +01002063
2064static void
2065test_type_union(void **state)
2066{
2067 *state = test_type_union;
2068
2069 struct ly_ctx *ctx;
2070 struct lys_module *mod;
2071 struct lysc_type *type;
2072
2073 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2074
2075 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2076 "typedef mytype {type union {type int8; type mybasetype;}}"
2077 "leaf l {type mytype;}}", LYS_IN_YANG));
2078 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2079 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2080 assert_non_null(type);
2081 assert_int_equal(2, type->refcount);
2082 assert_int_equal(LY_TYPE_UNION, type->basetype);
2083 assert_non_null(((struct lysc_type_union*)type)->types);
2084 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2085 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2086 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2087
2088 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2089 "typedef mytype {type union {type int8; type mybasetype;}}"
2090 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
2091 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2092 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2093 assert_non_null(type);
2094 assert_int_equal(1, type->refcount);
2095 assert_int_equal(LY_TYPE_UNION, type->basetype);
2096 assert_non_null(((struct lysc_type_union*)type)->types);
2097 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2098 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2099 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2100 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2101
2102 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2103 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002104 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2105 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002106 LYS_IN_YANG));
2107 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2108 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2109 assert_non_null(type);
2110 assert_int_equal(1, type->refcount);
2111 assert_int_equal(LY_TYPE_UNION, type->basetype);
2112 assert_non_null(((struct lysc_type_union*)type)->types);
2113 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2114 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2115 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2116 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2117 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2118 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2119
Radek Krejci6be5ff12018-11-26 13:18:15 +01002120 /* invalid unions */
2121 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
2122 "leaf l {type mytype;}}", LYS_IN_YANG));
2123 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2124 logbuf_assert("Missing type substatement for union type mytype.");
2125
2126 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2127 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2128 logbuf_assert("Missing type substatement for union type.");
2129
2130 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
2131 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
2132 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2133 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2134
2135 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
2136 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
2137 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2138 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2139
Radek Krejcicdfecd92018-11-26 11:27:32 +01002140 *state = NULL;
2141 ly_ctx_destroy(ctx, NULL);
2142}
2143
2144static void
2145test_type_dflt(void **state)
2146{
2147 *state = test_type_union;
2148
2149 struct ly_ctx *ctx;
2150 struct lys_module *mod;
2151 struct lysc_type *type;
2152
2153 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2154
2155 /* default is not inherited from union's types */
2156 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2157 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
2158 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2159 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2160 assert_non_null(type);
2161 assert_int_equal(1, type->refcount);
2162 assert_int_equal(LY_TYPE_UNION, type->basetype);
2163 assert_non_null(((struct lysc_type_union*)type)->types);
2164 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2165 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2166 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2167 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2168 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2169
2170 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2171 "leaf l {type mybasetype;}}", LYS_IN_YANG));
2172 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2173 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2174 assert_non_null(type);
2175 assert_int_equal(2, type->refcount);
2176 assert_int_equal(LY_TYPE_STRING, type->basetype);
2177 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2178 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2179
2180 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2181 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
2182 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2183 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2184 assert_non_null(type);
2185 assert_int_equal(2, type->refcount);
2186 assert_int_equal(LY_TYPE_STRING, type->basetype);
2187 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2188 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2189
Radek Krejci6be5ff12018-11-26 13:18:15 +01002190 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2191 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2192 "leaf l2 {type mytype;}}", LYS_IN_YANG));
2193 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2194 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2195 assert_non_null(type);
2196 assert_int_equal(4, type->refcount);
2197 assert_int_equal(LY_TYPE_STRING, type->basetype);
2198 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2199 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2200 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2201 assert_non_null(type);
2202 assert_int_equal(4, type->refcount);
2203 assert_int_equal(LY_TYPE_STRING, type->basetype);
2204 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2205 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2206
2207 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2208 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
2209 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2210 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2211 assert_non_null(type);
2212 assert_int_equal(3, type->refcount);
2213 assert_int_equal(LY_TYPE_STRING, type->basetype);
2214 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2215 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2216
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002217 /* mandatory leaf does not takes default value from type */
2218 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2219 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
2220 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2221 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2222 assert_non_null(type);
2223 assert_int_equal(LY_TYPE_STRING, type->basetype);
2224 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2225 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2226
Radek Krejcicdfecd92018-11-26 11:27:32 +01002227 *state = NULL;
2228 ly_ctx_destroy(ctx, NULL);
2229}
2230
Radek Krejci665421c2018-12-05 08:03:39 +01002231static void
2232test_status(void **state)
2233{
2234 *state = test_status;
2235
2236 struct ly_ctx *ctx;
2237 struct lys_module *mod;
2238
2239 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2240
2241 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2242 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
2243 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2244 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2245
2246 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
2247 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
2248 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2249 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2250
2251 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
2252 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
2253 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2254 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2255
2256 *state = NULL;
2257 ly_ctx_destroy(ctx, NULL);
2258}
2259
Radek Krejcie86bf772018-12-14 11:39:53 +01002260static void
2261test_uses(void **state)
2262{
2263 *state = test_uses;
2264
2265 struct ly_ctx *ctx;
2266 struct lys_module *mod;
2267 struct lysc_node *parent, *child;
2268
2269 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2270
2271 assert_non_null(mod = lys_parse_mem(ctx, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;}"
2272 "grouping grp {leaf x {type mytype;}}}", LYS_IN_YANG));
2273 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2274
2275
2276 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2277 "grouping grp_a_top {leaf a1 {type int8;}}"
2278 "container a {uses grp_a; uses grp_a_top; uses g:grp; grouping grp_a {leaf a2 {type uint8;}}}}", LYS_IN_YANG));
2279 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2280 assert_non_null((parent = mod->compiled->data));
2281 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2282 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2283 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002284 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002285 assert_non_null((child = child->next));
2286 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002287 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002288 assert_non_null((child = child->next));
2289 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002290 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002291
Radek Krejcib1b59152019-01-07 13:21:56 +01002292 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {type string;}}}");
2293 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp;}", LYS_IN_YANG));
2294 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2295 assert_non_null(mod->compiled->data);
2296 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2297 assert_string_equal("b", mod->compiled->data->name);
2298
Radek Krejci7f6a3812019-01-07 13:48:57 +01002299 logbuf_clean();
2300 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2301 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2302 "uses grp {status deprecated;}}", LYS_IN_YANG));
2303 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2304 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2305 assert_string_equal("l", mod->compiled->data->name);
2306 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2307 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2308 assert_string_equal("k", mod->compiled->data->next->name);
2309 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2310 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2311
Radek Krejcie86bf772018-12-14 11:39:53 +01002312 /* invalid */
2313 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
2314 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2315 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2316
2317 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
2318 "grouping grp {leaf a{type string;}uses grp1;}"
2319 "grouping grp1 {leaf b {type string;}uses grp2;}"
2320 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
2321 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2322 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2323
Radek Krejci76b3e962018-12-14 17:01:25 +01002324 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
2325 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2326 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2327
2328 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;grouping grp{leaf a{type string;}}"
2329 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
2330 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2331 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2332
Radek Krejci7f6a3812019-01-07 13:48:57 +01002333 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;grouping grp {leaf l {type string; status deprecated;}}"
2334 "uses grp {status obsolete;}}", LYS_IN_YANG));
2335 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2336 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2337
Radek Krejcie86bf772018-12-14 11:39:53 +01002338 *state = NULL;
2339 ly_ctx_destroy(ctx, NULL);
2340}
2341
Radek Krejci01342af2019-01-03 15:18:08 +01002342static void
2343test_refine(void **state)
2344{
2345 *state = test_refine;
2346
2347 struct ly_ctx *ctx;
2348 struct lys_module *mod;
2349 struct lysc_node *parent, *child;
2350
2351 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2352
Radek Krejcif0089082019-01-07 16:42:01 +01002353 assert_non_null(mod = lys_parse_mem(ctx, "module grp {yang-version 1.1;namespace urn:grp;prefix g; feature f;typedef mytype {type string; default cheers!;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002354 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002355 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002356 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002357 "leaf x {type mytype; mandatory true; must 1;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002358 "anydata a {mandatory false; if-feature f; description original; reference original;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002359 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002360 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2361
Radek Krejcif0089082019-01-07 16:42:01 +01002362 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 +01002363 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002364 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002365 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002366 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2367 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002368 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002369 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2370 assert_non_null((parent = mod->compiled->data));
2371 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2372 assert_string_equal("c", parent->name);
2373 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2374 assert_int_equal(LYS_LEAF, child->nodetype);
2375 assert_string_equal("l", child->name);
2376 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2377 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2378 assert_non_null(child = child->next);
2379 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2380 assert_string_equal("ll", child->name);
2381 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2382 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2383 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002384 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2385 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002386 assert_non_null(child = child->next);
2387 assert_int_equal(LYS_CHOICE, child->nodetype);
2388 assert_string_equal("ch", child->name);
2389 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2390 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2391 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002392 assert_non_null(child->iffeatures);
2393 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002394 assert_non_null(child = child->next);
2395 assert_int_equal(LYS_LEAF, child->nodetype);
2396 assert_string_equal("x", child->name);
2397 assert_false(LYS_MAND_TRUE & child->flags);
2398 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002399 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2400 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002401 assert_string_equal("refined", child->dsc);
2402 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002403 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002404 assert_int_equal(LYS_ANYDATA, child->nodetype);
2405 assert_string_equal("a", child->name);
2406 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002407 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2408 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002409 assert_non_null(child->iffeatures);
2410 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002411 assert_string_equal("refined", child->dsc);
2412 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002413 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002414 assert_int_equal(LYS_CONTAINER, child->nodetype);
2415 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002416 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002417 assert_true(LYS_CONFIG_W & child->flags);
2418 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002419
2420 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 +01002421 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002422 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002423 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002424 assert_int_equal(LYS_LEAF, child->nodetype);
2425 assert_string_equal("x", child->name);
2426 assert_false(LYS_MAND_TRUE & child->flags);
2427 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2428
2429 /* invalid */
2430 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
2431 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
2432 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2433 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2434
2435 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
2436 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
2437 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2438 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2439
2440 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
2441 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
2442 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2443 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2444
2445 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
2446 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
2447 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2448 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2449
2450 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
2451 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
2452 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2453 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
2454 assert_non_null(mod = lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
2455 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
2456 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2457 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2458
2459 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
2460 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
2461 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2462 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" - leaf under the default case.");
2463
2464 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
2465 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
2466 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2467 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2468
Radek Krejcib1b59152019-01-07 13:21:56 +01002469 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
2470 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
2471 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2472 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2473
2474 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;grouping grp {leaf l {type string; status deprecated;}}"
2475 "uses grp {status obsolete;}}", LYS_IN_YANG));
2476 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2477 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2478
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002479 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
2480 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
2481 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2482 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2483
Radek Krejci9a564c92019-01-07 14:53:57 +01002484 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
2485 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
2486 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2487 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2488
Radek Krejci6b22ab72019-01-07 15:39:20 +01002489 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
2490 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
2491 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2492 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2493
Radek Krejcif2271f12019-01-07 16:42:23 +01002494 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
2495 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
2496 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2497 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2498
Radek Krejci01342af2019-01-03 15:18:08 +01002499 *state = NULL;
2500 ly_ctx_destroy(ctx, NULL);
2501}
Radek Krejcie86bf772018-12-14 11:39:53 +01002502
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002503int main(void)
2504{
2505 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002506 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2507 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2508 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002509 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2510 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002511 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002512 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2513 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002514 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002515 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002516 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002517 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002518 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002519 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2520 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002521 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002522 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002523 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002524 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002525 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002526 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002527 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01002528 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002529 };
2530
2531 return cmocka_run_group_tests(tests, NULL, NULL);
2532}