blob: b3e491716820ecbdf608f6b477cdd20404c2f1f9 [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;
Radek Krejci096235c2019-01-11 11:12:19 +0100118 module->implemented = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100119}
120
121static void
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200122test_module(void **state)
123{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100124 *state = test_module;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200125
126 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +0100127 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200128 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200129 struct lysc_feature *f;
130 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200131
132 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200133 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100134 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100135 reset_mod(ctx.ctx, &mod);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200136
Radek Krejcif8f882a2018-10-31 14:51:15 +0100137 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
138 logbuf_assert("Invalid argument mod (lys_compile()).");
139 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
140 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100141 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +0100142 mod.implemented = 0;
143 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
144 assert_null(mod.compiled);
145 mod.implemented = 1;
Radek Krejcif8f882a2018-10-31 14:51:15 +0100146 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200147 assert_non_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100148 assert_string_equal("test", mod.name);
149 assert_string_equal("urn:test", mod.ns);
150 assert_string_equal("t", mod.prefix);
Radek Krejci151a5b72018-10-19 14:21:44 +0200151 /* features */
152 assert_non_null(mod.compiled->features);
153 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200154 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200155 assert_non_null(f->iffeatures);
156 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200157 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200158 assert_non_null(iff->expr);
159 assert_non_null(iff->features);
160 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200161 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200162
Radek Krejci86d106e2018-10-18 09:53:19 +0200163 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200164
Radek Krejcif8f882a2018-10-31 14:51:15 +0100165 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200166 assert_non_null(mod.compiled);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200167
Radek Krejci86d106e2018-10-18 09:53:19 +0200168 lysc_module_free(mod.compiled, NULL);
169 mod.compiled = NULL;
170
Radek Krejci151a5b72018-10-19 14:21:44 +0200171 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200172 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100173 assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod));
174 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
175 assert_null(mod.parsed);
176 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100177
178 /* data definition name collision in top level */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100179 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;"
180 "leaf a {type string;} container a{presence x;}}", &mod));
Radek Krejci76b3e962018-12-14 17:01:25 +0100181 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
182 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
183 assert_null(mod.compiled);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100184 reset_mod(ctx.ctx, &mod);
Radek Krejci76b3e962018-12-14 17:01:25 +0100185
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100186 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100187 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200188}
189
Radek Krejci151a5b72018-10-19 14:21:44 +0200190static void
191test_feature(void **state)
192{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100193 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200194
Radek Krejci313d9902018-11-08 09:42:58 +0100195 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100196 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200197 const char *str;
198 struct lysc_feature *f, *f1;
199
200 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
201 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100202 "feature orfeature {if-feature \"f1 or f2\";}\n"
203 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200204 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200205 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100206 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
207 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200208
Radek Krejci313d9902018-11-08 09:42:58 +0100209 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100210 reset_mod(ctx.ctx, &mod);
211
212 assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100213 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200214 assert_non_null(mod.compiled);
215 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100216 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200217 /* all features are disabled by default */
218 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
219 assert_int_equal(0, lysc_feature_value(f));
220 }
221 /* enable f1 */
222 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200223 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200224 assert_int_equal(1, lysc_feature_value(f1));
225
Radek Krejci87616bb2018-10-31 13:30:52 +0100226 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200227 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200228 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100229 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200230 assert_int_equal(1, lysc_feature_value(f));
231
Radek Krejci87616bb2018-10-31 13:30:52 +0100232 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200233 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200234 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100235 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
236 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200237 assert_int_equal(0, lysc_feature_value(f));
238
239 /* first enable f2, so f5 can be enabled then */
240 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100241 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200242 assert_int_equal(1, lysc_feature_value(f));
243
244 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200245 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200246 assert_int_equal(0, lysc_feature_value(f));
247 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
248 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
249 assert_int_equal(0, lysc_feature_value(f));
250
Radek Krejci87616bb2018-10-31 13:30:52 +0100251 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200252 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200253 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
254 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200255 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100256 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200257 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200258 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200259 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200260 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200261
262 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200263 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200264 /* long evaluation of f8 to need to reallocate internal stack for operators */
265 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200266
Radek Krejci87616bb2018-10-31 13:30:52 +0100267 /* double negation of disabled f1 -> disabled */
268 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
269
Radek Krejci38920282018-11-01 09:24:16 +0100270 /* disable all features */
271 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
272 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
273 assert_int_equal(0, lys_feature_value(&mod, f->name));
274 }
275 /* re-setting already set feature */
276 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
277 assert_int_equal(0, lys_feature_value(&mod, "f1"));
278
279 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100280 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
281 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
282 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100283 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
284 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100285 /* test if not changed */
286 assert_int_equal(1, lys_feature_value(&mod, "f1"));
287 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100288
Radek Krejci1aefdf72018-11-01 11:01:39 +0100289 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100290 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
291 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
292
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100293 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100294
295 /* some invalid expressions */
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 f{if-feature f1;}}", &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 \"f1\" of if-feature - unable to find feature \"f1\".");
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 f1; feature f2{if-feature 'f and';}}", &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 \"f and\" 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 f{if-feature 'or';}}", &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 \"or\" of if-feature - unexpected end of expression.");
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 'f1)';}}", &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 \"f1)\" of if-feature - non-matching opening and closing parentheses.");
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{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &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 \"---\" of if-feature - unable to find feature \"---\".");
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 f2{if-feature 'not f1';}}", &mod));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100327 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100328 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 +0100329 reset_mod(ctx.ctx, &mod);
Radek Krejci87616bb2018-10-31 13:30:52 +0100330
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100331 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 +0100332 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
333 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100334 reset_mod(ctx.ctx, &mod);
Radek Krejcid05cbd92018-12-05 14:26:40 +0100335
336 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sb {belongs-to b {prefix b;} feature f1;}");
Radek Krejci096235c2019-01-11 11:12:19 +0100337 assert_null(lys_parse_mem(ctx.ctx, "module b{namespace urn:b; prefix b; include sb;feature f1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100338 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
339
Radek Krejci1aefdf72018-11-01 11:01:39 +0100340 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100341 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100342 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100343 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 +0100344 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
345 assert_int_equal(0, lys_feature_value(modp, "f1"));
346 assert_int_equal(1, lys_feature_value(modp, "f2"));
347
Radek Krejcid05cbd92018-12-05 14:26:40 +0100348 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100349 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200350}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200351
Radek Krejci478020e2018-10-30 16:02:14 +0100352static void
353test_identity(void **state)
354{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100355 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100356
357 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100358 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100359 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100360 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 +0100361
362 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
363 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
364 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
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 Krejci096235c2019-01-11 11:12:19 +0100385 assert_null(lys_parse_mem(ctx, "module c{namespace urn:c; prefix c; identity i1;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100386 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
387
388 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sd {belongs-to d {prefix d;} identity i1;}");
Radek Krejci096235c2019-01-11 11:12:19 +0100389 assert_null(lys_parse_mem(ctx, "module d{namespace urn:d; prefix d; include sd;identity i1;}", LYS_IN_YANG));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100390 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
391
Radek Krejci7f2a5362018-11-28 13:05:37 +0100392 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100393 ly_ctx_destroy(ctx, NULL);
394}
395
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100396static void
397test_node_container(void **state)
398{
399 (void) state; /* unused */
400
401 struct ly_ctx *ctx;
402 struct lys_module *mod;
403 struct lysc_node_container *cont;
404
405 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
406 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100407 assert_non_null(mod->compiled);
408 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
409 assert_int_equal(LYS_CONTAINER, cont->nodetype);
410 assert_string_equal("c", cont->name);
411 assert_true(cont->flags & LYS_CONFIG_W);
412 assert_true(cont->flags & LYS_STATUS_CURR);
413
Radek Krejci98094b32018-11-02 16:21:47 +0100414 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;container c {config false; status deprecated; container child;}}", LYS_IN_YANG));
Radek Krejci98094b32018-11-02 16:21:47 +0100415 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100416 assert_non_null(mod->compiled);
417 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
418 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100419 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100420 assert_non_null((cont = (struct lysc_node_container*)cont->child));
421 assert_int_equal(LYS_CONTAINER, cont->nodetype);
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_string_equal("child", cont->name);
425
426 ly_ctx_destroy(ctx, NULL);
427}
428
Radek Krejci0e5d8382018-11-28 16:37:53 +0100429static void
430test_node_leaflist(void **state)
431{
432 *state = test_node_leaflist;
433
434 struct ly_ctx *ctx;
435 struct lys_module *mod;
436 struct lysc_type *type;
437 struct lysc_node_leaflist *ll;
438
439 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
440
441 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
442 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
443 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
444 "leaf-list ll2 {type leafref {path ../target;}}"
445 "leaf target {type int8;}}",
446 LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100447 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
448 assert_non_null(type);
449 assert_int_equal(1, type->refcount);
450 assert_int_equal(LY_TYPE_UNION, type->basetype);
451 assert_non_null(((struct lysc_type_union*)type)->types);
452 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
453 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
454 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
455 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
456 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
457 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
458 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
459 assert_non_null(type);
460 assert_int_equal(1, type->refcount);
461 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
462 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
463 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
464
Radek Krejci6bb080b2018-11-28 17:23:41 +0100465 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 +0100466 assert_non_null(mod->compiled);
467 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
468 assert_int_equal(0, ll->min);
469 assert_int_equal((uint32_t)-1, ll->max);
470
Radek Krejci6bb080b2018-11-28 17:23:41 +0100471 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 +0100472 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100473 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100474 assert_non_null(mod->compiled);
475 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
476 assert_non_null(ll->dflts);
477 assert_int_equal(3, ll->type->refcount);
478 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
479 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100480 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100481 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100482 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
483 assert_non_null(ll->dflts);
484 assert_int_equal(3, ll->type->refcount);
485 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
486 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100487 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
488
489 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
490 * TODO test also, RPC output parameters and notification content */
491 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
492 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
Radek Krejcia6d57732018-11-29 13:40:37 +0100493 /* but warning is present: */
494 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
495 assert_non_null(mod->compiled);
496 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
497 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100498
499 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100500 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100501 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
502
Radek Krejci096235c2019-01-11 11:12:19 +0100503 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100504 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
505
506 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
507 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100508 assert_non_null(mod->compiled);
509 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
510 assert_non_null(ll->dflts);
511 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
Radek Krejci096235c2019-01-11 11:12:19 +0100512 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
513 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100514 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
515
Radek Krejci0e5d8382018-11-28 16:37:53 +0100516 *state = NULL;
517 ly_ctx_destroy(ctx, NULL);
518}
519
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100520static void
521test_node_list(void **state)
522{
523 *state = test_node_list;
524
525 struct ly_ctx *ctx;
526 struct lys_module *mod;
527 struct lysc_node_list *list;
528
529 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
530
531 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
532 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
533 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100534 list = (struct lysc_node_list*)mod->compiled->data;
535 assert_non_null(list);
536 assert_non_null(list->keys);
537 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
538 assert_string_equal("x", list->keys[0]->name);
539 assert_string_equal("y", list->keys[1]->name);
540 assert_non_null(list->child);
541 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
542 assert_true(list->child->flags & LYS_KEY);
543 assert_true(list->child->next->flags & LYS_KEY);
544 list = (struct lysc_node_list*)mod->compiled->data->next;
545 assert_non_null(list);
546 assert_null(list->keys);
547 assert_non_null(list->child);
548 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
549 assert_false(list->child->flags & LYS_KEY);
550
551 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
552 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100553 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100554 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100555 list = (struct lysc_node_list*)mod->compiled->data;
556 assert_non_null(list);
557 assert_string_equal("l", list->name);
558 assert_non_null(list->keys);
559 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
560 assert_string_equal("a", list->keys[0]->name);
561 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100562 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100563 assert_non_null(list->uniques);
564 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
565 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
566 assert_string_equal("a", list->uniques[0][0]->name);
567 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
568 assert_string_equal("b", list->uniques[0][1]->name);
569 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
570 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
571 assert_string_equal("e", list->uniques[1][0]->name);
572 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
573 assert_string_equal("d", list->uniques[1][1]->name);
574 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
575
Radek Krejci665421c2018-12-05 08:03:39 +0100576 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
577 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100578 list = (struct lysc_node_list*)mod->compiled->data;
579 assert_non_null(list);
580 assert_string_equal("l", list->name);
581 assert_non_null(list->keys);
582 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
583 assert_string_equal("a", list->keys[0]->name);
584 assert_true(list->keys[0]->flags & LYS_KEY);
585 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
586
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100587 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100588 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100589 logbuf_assert("Missing key in list representing configuration data.");
590
Radek Krejci096235c2019-01-11 11:12:19 +0100591 assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
592 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100593 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
594
Radek Krejci096235c2019-01-11 11:12:19 +0100595 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
596 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100597 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
598
Radek Krejci096235c2019-01-11 11:12:19 +0100599 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
600 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100601 logbuf_assert("Key of the configuration list must not be status leaf.");
602
Radek Krejci096235c2019-01-11 11:12:19 +0100603 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
604 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100605 logbuf_assert("Configuration node cannot be child of any state data node.");
606
Radek Krejci096235c2019-01-11 11:12:19 +0100607 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
608 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100609 logbuf_assert("The list's key \"x\" not found.");
610
Radek Krejci096235c2019-01-11 11:12:19 +0100611 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
612 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100613 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
614
Radek Krejci096235c2019-01-11 11:12:19 +0100615 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
616 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100617 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
618
Radek Krejci096235c2019-01-11 11:12:19 +0100619 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
620 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100621 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
622
Radek Krejci096235c2019-01-11 11:12:19 +0100623 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
624 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100625 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
626
Radek Krejci096235c2019-01-11 11:12:19 +0100627 assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
628 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100629 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
630
Radek Krejci096235c2019-01-11 11:12:19 +0100631 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
632 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100633 logbuf_assert("Duplicated key identifier \"x\".");
634
Radek Krejci096235c2019-01-11 11:12:19 +0100635 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
636 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +0100637 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
638
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100639 *state = NULL;
640 ly_ctx_destroy(ctx, NULL);
641}
642
Radek Krejci056d0a82018-12-06 16:57:25 +0100643static void
644test_node_choice(void **state)
645{
646 *state = test_node_choice;
647
648 struct ly_ctx *ctx;
649 struct lys_module *mod;
650 struct lysc_node_choice *ch;
651 struct lysc_node_case *cs;
652
653 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
654
655 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100656 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
657 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100658 ch = (struct lysc_node_choice*)mod->compiled->data;
659 assert_non_null(ch);
Radek Krejci01342af2019-01-03 15:18:08 +0100660 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100661 cs = ch->cases;
662 assert_non_null(cs);
663 assert_string_equal("a", cs->name);
664 assert_ptr_equal(ch, cs->parent);
665 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100666 assert_string_equal("a1", cs->child->name);
667 assert_non_null(cs->child->next);
668 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100669 assert_ptr_equal(cs, cs->child->parent);
670 cs = (struct lysc_node_case*)cs->next;
671 assert_non_null(cs);
672 assert_string_equal("b", cs->name);
Radek Krejci01342af2019-01-03 15:18:08 +0100673 assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100674 assert_ptr_equal(ch, cs->parent);
675 assert_non_null(cs->child);
676 assert_string_equal("b", cs->child->name);
677 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100678 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
679 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100680 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100681 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100682
Radek Krejci096235c2019-01-11 11:12:19 +0100683 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
684 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100685 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100686 assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
687 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100688 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100689 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
690 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100691 logbuf_assert("Duplicate identifier \"a\" of case statement.");
Radek Krejci096235c2019-01-11 11:12:19 +0100692 assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
693 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100694 logbuf_assert("Duplicate identifier \"b\" of case statement.");
695
Radek Krejci096235c2019-01-11 11:12:19 +0100696 assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
697 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100698 logbuf_assert("Default case \"c\" not found.");
Radek Krejci096235c2019-01-11 11:12:19 +0100699 assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
700 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100701 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
Radek Krejci096235c2019-01-11 11:12:19 +0100702 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
703 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
Radek Krejcia9026eb2018-12-12 16:04:47 +0100704 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
705 /* TODO check with mandatory nodes from augment placed into the case */
706
Radek Krejci056d0a82018-12-06 16:57:25 +0100707 *state = NULL;
708 ly_ctx_destroy(ctx, NULL);
709}
710
Radek Krejci9800fb82018-12-13 14:26:23 +0100711static void
712test_node_anydata(void **state)
713{
714 *state = test_node_anydata;
715
716 struct ly_ctx *ctx;
717 struct lys_module *mod;
718 struct lysc_node_anydata *any;
719
720 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
721
722 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
723 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100724 any = (struct lysc_node_anydata*)mod->compiled->data;
725 assert_non_null(any);
726 assert_int_equal(LYS_ANYDATA, any->nodetype);
727 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
728
729 logbuf_clean();
730 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
731 "anyxml any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100732 any = (struct lysc_node_anydata*)mod->compiled->data;
733 assert_non_null(any);
734 assert_int_equal(LYS_ANYXML, any->nodetype);
735 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
736 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
737
738 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +0100739 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;anydata any;}", LYS_IN_YANG));
Radek Krejci9800fb82018-12-13 14:26:23 +0100740 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
741
742 *state = NULL;
743 ly_ctx_destroy(ctx, NULL);
744}
745
Radek Krejci0f07bed2018-11-13 14:01:40 +0100746/**
747 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
748 * do not test the expression itself
749 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100750static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100751test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100752{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100753 *state = test_type_range;
754
755 struct ly_ctx *ctx;
756 struct lys_module *mod;
757 struct lysc_type *type;
758
759 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
760
761 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type int8 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100762 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
763 assert_non_null(type);
764 assert_int_equal(LY_TYPE_INT8, type->basetype);
765 assert_non_null(((struct lysc_type_num*)type)->range);
766 assert_non_null(((struct lysc_type_num*)type)->range->parts);
767 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
768 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
769 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
770 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
771 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
772
773 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type int16 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100774 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
775 assert_non_null(type);
776 assert_int_equal(LY_TYPE_INT16, type->basetype);
777 assert_non_null(((struct lysc_type_num*)type)->range);
778 assert_non_null(((struct lysc_type_num*)type)->range->parts);
779 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
780 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
781 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
782 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
783 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
784
785 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type int32 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100786 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
787 assert_non_null(type);
788 assert_int_equal(LY_TYPE_INT32, type->basetype);
789 assert_non_null(((struct lysc_type_num*)type)->range);
790 assert_non_null(((struct lysc_type_num*)type)->range->parts);
791 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
792 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
793 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
794 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
795 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
796
797 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type int64 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100798 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
799 assert_non_null(type);
800 assert_int_equal(LY_TYPE_INT64, type->basetype);
801 assert_non_null(((struct lysc_type_num*)type)->range);
802 assert_non_null(((struct lysc_type_num*)type)->range->parts);
803 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
804 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
805 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
806 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
807 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
808
809 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;leaf l {type uint8 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100810 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
811 assert_non_null(type);
812 assert_int_equal(LY_TYPE_UINT8, type->basetype);
813 assert_non_null(((struct lysc_type_num*)type)->range);
814 assert_non_null(((struct lysc_type_num*)type)->range->parts);
815 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
816 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
817 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
818 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
819 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
820
821 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;leaf l {type uint16 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100822 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
823 assert_non_null(type);
824 assert_int_equal(LY_TYPE_UINT16, type->basetype);
825 assert_non_null(((struct lysc_type_num*)type)->range);
826 assert_non_null(((struct lysc_type_num*)type)->range->parts);
827 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
828 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
829 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
830 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
831 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
832
833 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;leaf l {type uint32 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100834 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
835 assert_non_null(type);
836 assert_int_equal(LY_TYPE_UINT32, type->basetype);
837 assert_non_null(((struct lysc_type_num*)type)->range);
838 assert_non_null(((struct lysc_type_num*)type)->range->parts);
839 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
840 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
841 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
842 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
843 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
844
845 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;leaf l {type uint64 {range min..10|max;}}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100846 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
847 assert_non_null(type);
848 assert_int_equal(LY_TYPE_UINT64, type->basetype);
849 assert_non_null(((struct lysc_type_num*)type)->range);
850 assert_non_null(((struct lysc_type_num*)type)->range->parts);
851 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
852 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
853 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
854 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
855 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
856
857 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
858 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci0f07bed2018-11-13 14:01:40 +0100859 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
860 assert_non_null(type);
861 assert_int_equal(3, type->refcount);
862 assert_int_equal(LY_TYPE_UINT8, type->basetype);
863 assert_non_null(((struct lysc_type_num*)type)->range);
864 assert_non_null(((struct lysc_type_num*)type)->range->parts);
865 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
866
Radek Krejcif1394042019-01-08 10:53:34 +0100867 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;"
868 "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}"
869 "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG));
Radek Krejcif1394042019-01-08 10:53:34 +0100870 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
871 assert_non_null(type);
872 assert_int_equal(1, type->refcount);
873 assert_int_equal(LY_TYPE_UINT8, type->basetype);
874 assert_non_null(((struct lysc_type_num*)type)->range);
875 assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc);
876 assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref);
877 assert_non_null(((struct lysc_type_num*)type)->range->parts);
878 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
879 assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
880 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
881
Radek Krejci0f07bed2018-11-13 14:01:40 +0100882 *state = NULL;
883 ly_ctx_destroy(ctx, NULL);
884}
885
886static void
887test_type_length(void **state)
888{
889 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100890
891 struct ly_ctx *ctx;
892 struct lys_module *mod;
893 struct lysc_type *type;
894
895 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
896
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100897 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 +0100898 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
899 assert_non_null(type);
900 assert_non_null(((struct lysc_type_bin*)type)->length);
901 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100902 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
903 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100904 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
905 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
906 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
907
908 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100909 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
910 assert_non_null(type);
911 assert_non_null(((struct lysc_type_bin*)type)->length);
912 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
913 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100914 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
915 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100916
917 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type binary {length min..max;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100918 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
919 assert_non_null(type);
920 assert_non_null(((struct lysc_type_bin*)type)->length);
921 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
922 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
923 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100924 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100925
926 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100927 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
928 assert_non_null(type);
929 assert_non_null(((struct lysc_type_bin*)type)->length);
930 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
931 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
932 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
933 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
934
935 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;leaf l {type binary {length 1..10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100936 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
937 assert_non_null(type);
938 assert_non_null(((struct lysc_type_bin*)type)->length);
939 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
940 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
941 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
942 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
943
944 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;leaf l {type binary {length 1..10|20..30;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100945 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
946 assert_non_null(type);
947 assert_non_null(((struct lysc_type_bin*)type)->length);
948 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
949 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
950 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
951 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
952 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
953 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
954
955 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;leaf l {type binary {length \"16 | 32\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +0100956 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
957 assert_non_null(type);
958 assert_non_null(((struct lysc_type_bin*)type)->length);
959 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
960 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
961 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
962 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
963 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
964 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
965
Radek Krejcib31c8992018-11-12 16:29:16 +0100966 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
967 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100968 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
969 assert_non_null(type);
970 assert_non_null(((struct lysc_type_bin*)type)->length);
971 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
972 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
973 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
974 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
975
976 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
977 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100978 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
979 assert_non_null(type);
980 assert_non_null(((struct lysc_type_bin*)type)->length);
981 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
982 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
983 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
984 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
985
986 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
987 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100988 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
989 assert_non_null(type);
990 assert_non_null(((struct lysc_type_bin*)type)->length);
991 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
992 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
993 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
994 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
995 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
996 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
997
998 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 +0100999 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001000 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1001 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001002 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001003 assert_non_null(((struct lysc_type_bin*)type)->length);
1004 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1005 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1006 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001007 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001008 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1009 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001010 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001011 assert_non_null(((struct lysc_type_bin*)type)->length);
1012 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1013 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1014 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1015 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1016
Radek Krejci56aa27c2018-11-13 14:21:59 +01001017 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1018 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001019 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1020 assert_non_null(type);
1021 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001022 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001023 assert_non_null(((struct lysc_type_str*)type)->length);
1024 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1025 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1026 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1027 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001028
Radek Krejci5969f272018-11-23 10:03:58 +01001029 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1030 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
Radek Krejci5969f272018-11-23 10:03:58 +01001031 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1032 assert_non_null(type);
1033 assert_int_equal(LY_TYPE_STRING, type->basetype);
1034 assert_int_equal(1, type->refcount);
1035 assert_non_null(((struct lysc_type_str*)type)->length);
1036 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1037 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1038 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1039 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1040
Radek Krejci4f28eda2018-11-12 11:46:16 +01001041 /* invalid values */
Radek Krejci096235c2019-01-11 11:12:19 +01001042 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001043 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
Radek Krejci096235c2019-01-11 11:12:19 +01001044 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001045 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001046 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001047 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001048 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001049 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001050 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001051 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
Radek Krejci096235c2019-01-11 11:12:19 +01001052 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
Radek Krejci4f28eda2018-11-12 11:46:16 +01001053 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejci096235c2019-01-11 11:12:19 +01001054 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001055 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
Radek Krejci096235c2019-01-11 11:12:19 +01001056 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001057 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
Radek Krejci096235c2019-01-11 11:12:19 +01001058 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001059 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
Radek Krejci096235c2019-01-11 11:12:19 +01001060 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001061 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
Radek Krejci096235c2019-01-11 11:12:19 +01001062 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001063 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci096235c2019-01-11 11:12:19 +01001064 assert_null(lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001065 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001066
Radek Krejci096235c2019-01-11 11:12:19 +01001067 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1068 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001069 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001070 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1071 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001072 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001073 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1074 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001075 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001076 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1077 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001078 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001079 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
Radek Krejcib31c8992018-11-12 16:29:16 +01001080 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001081 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001082 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
Radek Krejcib31c8992018-11-12 16:29:16 +01001083 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001084 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci096235c2019-01-11 11:12:19 +01001085 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
Radek Krejci6489bbe2018-11-12 17:05:19 +01001086 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
Radek Krejci6489bbe2018-11-12 17:05:19 +01001087 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001088
Radek Krejci096235c2019-01-11 11:12:19 +01001089 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:rr;prefix rr;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
Radek Krejci495903e2018-11-13 11:30:45 +01001090 logbuf_assert("Invalid type restrictions for binary type.");
1091
Radek Krejci4f28eda2018-11-12 11:46:16 +01001092 *state = NULL;
1093 ly_ctx_destroy(ctx, NULL);
1094}
1095
Radek Krejcicda74152018-11-13 15:27:32 +01001096static void
1097test_type_pattern(void **state)
1098{
1099 *state = test_type_pattern;
1100
1101 struct ly_ctx *ctx;
1102 struct lys_module *mod;
1103 struct lysc_type *type;
1104
1105 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1106
Radek Krejci027d5802018-11-14 16:57:28 +01001107 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 +01001108 "pattern .* {error-app-tag errortag;error-message error;}"
1109 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001110 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1111 assert_non_null(type);
1112 assert_non_null(((struct lysc_type_str*)type)->patterns);
1113 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1114 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1115 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1116 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1117 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1118 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1119 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1120
1121 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1122 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001123 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1124 assert_non_null(type);
1125 assert_int_equal(LY_TYPE_STRING, type->basetype);
1126 assert_int_equal(1, type->refcount);
1127 assert_non_null(((struct lysc_type_str*)type)->patterns);
1128 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1129 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1130 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1131
Radek Krejci04bc1e92018-11-14 14:28:13 +01001132 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1133 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
Radek Krejci04bc1e92018-11-14 14:28:13 +01001134 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1135 assert_non_null(type);
1136 assert_int_equal(LY_TYPE_STRING, type->basetype);
1137 assert_int_equal(1, type->refcount);
1138 assert_non_null(((struct lysc_type_str*)type)->patterns);
1139 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1140 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1141
Radek Krejcicda74152018-11-13 15:27:32 +01001142 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001143 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 +01001144 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
Radek Krejcicda74152018-11-13 15:27:32 +01001145 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1146 assert_non_null(type);
1147 assert_non_null(((struct lysc_type_str*)type)->patterns);
1148 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1149 /* TODO check some data "^Å™$" */
1150
1151 *state = NULL;
1152 ly_ctx_destroy(ctx, NULL);
1153}
1154
Radek Krejci8b764662018-11-14 14:15:13 +01001155static void
1156test_type_enum(void **state)
1157{
1158 *state = test_type_enum;
1159
1160 struct ly_ctx *ctx;
1161 struct lys_module *mod;
1162 struct lysc_type *type;
1163
1164 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1165
Radek Krejci027d5802018-11-14 16:57:28 +01001166 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 +01001167 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1168 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001169 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1170 assert_non_null(type);
1171 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1172 assert_non_null(((struct lysc_type_enum*)type)->enums);
1173 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1174 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1175 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1176 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1177 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1178 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1179 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1180 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1181 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1182 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1183 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1184 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1185 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1186 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1187
Radek Krejci027d5802018-11-14 16:57:28 +01001188 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 {"
1189 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001190 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1191 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001192 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1193 assert_non_null(type);
1194 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1195 assert_non_null(((struct lysc_type_enum*)type)->enums);
1196 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1197 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1198 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1199 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1200 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1201
1202
1203 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001204 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1205 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1206 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 +01001207 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1208 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1209 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1210 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1211 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1212 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1213 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1214 "enum one; enum one;}}}", LYS_IN_YANG));
1215 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1216 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1217 "enum '';}}}", LYS_IN_YANG));
1218 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1219 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1220 "enum ' x';}}}", LYS_IN_YANG));
1221 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1222 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1223 "enum 'x ';}}}", LYS_IN_YANG));
1224 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1225 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1226 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1227 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1228
Radek Krejci096235c2019-01-11 11:12:19 +01001229 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001230 logbuf_assert("Missing enum substatement for enumeration type.");
1231
Radek Krejci096235c2019-01-11 11:12:19 +01001232 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type enumeration {enum one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001233 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001234 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1235
Radek Krejci096235c2019-01-11 11:12:19 +01001236 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type enumeration {enum one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001237 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001238 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001239 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type enumeration {enum x {value 2147483647;}enum y;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001240 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001241 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1242
Radek Krejci096235c2019-01-11 11:12:19 +01001243 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type enumeration {enum x {value 1;}enum y {value 1;}}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001244 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001245 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1246
Radek Krejci096235c2019-01-11 11:12:19 +01001247 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001248 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001249 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001250
Radek Krejci096235c2019-01-11 11:12:19 +01001251 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
Radek Krejci027d5802018-11-14 16:57:28 +01001252 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001253 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1254
Radek Krejci8b764662018-11-14 14:15:13 +01001255 *state = NULL;
1256 ly_ctx_destroy(ctx, NULL);
1257}
1258
1259static void
1260test_type_bits(void **state)
1261{
1262 *state = test_type_bits;
1263
1264 struct ly_ctx *ctx;
1265 struct lys_module *mod;
1266 struct lysc_type *type;
1267
1268 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1269
Radek Krejci027d5802018-11-14 16:57:28 +01001270 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 +01001271 "bit automin; bit one {if-feature f; position 1;}"
1272 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001273 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1274 assert_non_null(type);
1275 assert_int_equal(LY_TYPE_BITS, type->basetype);
1276 assert_non_null(((struct lysc_type_bits*)type)->bits);
1277 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1278 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1279 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1280 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1281 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1282 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1283 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1284 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1285 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1286 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1287 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1288 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1289
Radek Krejci027d5802018-11-14 16:57:28 +01001290 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 {"
1291 "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 +01001292 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001293 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1294 assert_non_null(type);
1295 assert_int_equal(LY_TYPE_BITS, type->basetype);
1296 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001297 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1298 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1299 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1300 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1301 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1302 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1303 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001304
1305
1306 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001307 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1308 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1309 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 +01001310 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1311 "bit one {position -1;}}}}", LYS_IN_YANG));
1312 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1313 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1314 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1315 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1316 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1317 "bit one; bit one;}}}", LYS_IN_YANG));
1318 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1319 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1320 "bit '11';}}}", LYS_IN_YANG));
1321 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1322 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1323 "bit 'x1$1';}}}", LYS_IN_YANG));
1324 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1325
Radek Krejci096235c2019-01-11 11:12:19 +01001326 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001327 logbuf_assert("Missing bit substatement for bits type.");
1328
Radek Krejci096235c2019-01-11 11:12:19 +01001329 assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type bits {bit one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001330 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001331 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1332
Radek Krejci096235c2019-01-11 11:12:19 +01001333 assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type bits {bit one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001334 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001335 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001336 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001337 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001338 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1339
Radek Krejci096235c2019-01-11 11:12:19 +01001340 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {value 1;}bit y {value 1;}}}}",
Radek Krejci8b764662018-11-14 14:15:13 +01001341 LYS_IN_YANG));
Radek Krejci8b764662018-11-14 14:15:13 +01001342 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1343
Radek Krejci096235c2019-01-11 11:12:19 +01001344 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
Radek Krejci04bc1e92018-11-14 14:28:13 +01001345 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001346 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001347
Radek Krejci096235c2019-01-11 11:12:19 +01001348 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
Radek Krejci027d5802018-11-14 16:57:28 +01001349 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
Radek Krejci027d5802018-11-14 16:57:28 +01001350 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1351
Radek Krejci8b764662018-11-14 14:15:13 +01001352 *state = NULL;
1353 ly_ctx_destroy(ctx, NULL);
1354}
1355
Radek Krejci6cba4292018-11-15 17:33:29 +01001356static void
1357test_type_dec64(void **state)
1358{
1359 *state = test_type_dec64;
1360
1361 struct ly_ctx *ctx;
1362 struct lys_module *mod;
1363 struct lysc_type *type;
1364
1365 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1366
1367 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1368 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001369 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1370 assert_non_null(type);
1371 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1372 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1373 assert_non_null(((struct lysc_type_dec*)type)->range);
1374 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1375 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1376 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1377 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1378
1379 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1380 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001381 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1382 assert_non_null(type);
1383 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1384 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1385 assert_non_null(((struct lysc_type_dec*)type)->range);
1386 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1387 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1388 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1389 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1390 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1391 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1392 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1393 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1394
1395 /* invalid cases */
1396 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1397 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1398 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1399 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1400 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1401 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1402
Radek Krejci096235c2019-01-11 11:12:19 +01001403 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001404 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1405
Radek Krejci096235c2019-01-11 11:12:19 +01001406 assert_null(lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001407 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001408
Radek Krejci096235c2019-01-11 11:12:19 +01001409 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
Radek Krejci6cba4292018-11-15 17:33:29 +01001410 "range '3.142';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001411 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1412
Radek Krejci096235c2019-01-11 11:12:19 +01001413 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
Radek Krejci6cba4292018-11-15 17:33:29 +01001414 "range '4 | 3.14';}}}", LYS_IN_YANG));
Radek Krejci6cba4292018-11-15 17:33:29 +01001415 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1416
Radek Krejci096235c2019-01-11 11:12:19 +01001417 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
Radek Krejci643c8242018-11-15 17:51:11 +01001418 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001419 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1420
Radek Krejci096235c2019-01-11 11:12:19 +01001421 assert_null(lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
Radek Krejci643c8242018-11-15 17:51:11 +01001422 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci643c8242018-11-15 17:51:11 +01001423 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1424
Radek Krejci6cba4292018-11-15 17:33:29 +01001425 *state = NULL;
1426 ly_ctx_destroy(ctx, NULL);
1427}
1428
Radek Krejci16c0f822018-11-16 10:46:10 +01001429static void
1430test_type_instanceid(void **state)
1431{
1432 *state = test_type_instanceid;
1433
1434 struct ly_ctx *ctx;
1435 struct lys_module *mod;
1436 struct lysc_type *type;
1437
1438 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1439
1440 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1441 "leaf l1 {type instance-identifier {require-instance true;}}"
1442 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001443 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1444 assert_non_null(type);
1445 assert_int_equal(LY_TYPE_INST, type->basetype);
1446 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1447
1448 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1449 assert_non_null(type);
1450 assert_int_equal(LY_TYPE_INST, type->basetype);
1451 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1452
1453 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1454 assert_non_null(type);
1455 assert_int_equal(LY_TYPE_INST, type->basetype);
1456 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1457
1458 /* invalid cases */
1459 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1460 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1461
Radek Krejci096235c2019-01-11 11:12:19 +01001462 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
Radek Krejci16c0f822018-11-16 10:46:10 +01001463 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1464
1465 *state = NULL;
1466 ly_ctx_destroy(ctx, NULL);
1467}
1468
Radek Krejci555cb5b2018-11-16 14:54:33 +01001469static void
1470test_type_identityref(void **state)
1471{
1472 *state = test_type_identityref;
1473
1474 struct ly_ctx *ctx;
1475 struct lys_module *mod;
1476 struct lysc_type *type;
1477
1478 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1479
1480 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;}"
1481 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001482 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001483 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1484 assert_non_null(type);
1485 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1486 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1487 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1488 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1489
1490 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1491 assert_non_null(type);
1492 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1493 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1494 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1495 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1496 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1497
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001498 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1499 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001500 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1501 assert_non_null(type);
1502 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1503 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1504 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1505 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1506 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1507
Radek Krejci555cb5b2018-11-16 14:54:33 +01001508 /* invalid cases */
Radek Krejci096235c2019-01-11 11:12:19 +01001509 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001510 logbuf_assert("Missing base substatement for identityref type.");
1511
Radek Krejci096235c2019-01-11 11:12:19 +01001512 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001513 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001514 logbuf_assert("Missing base substatement for identityref type mytype.");
1515
Radek Krejci096235c2019-01-11 11:12:19 +01001516 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001517 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001518 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001519
Radek Krejci096235c2019-01-11 11:12:19 +01001520 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001521 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001522 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001523
Radek Krejci096235c2019-01-11 11:12:19 +01001524 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
Radek Krejci555cb5b2018-11-16 14:54:33 +01001525 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001526 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1527
Radek Krejci096235c2019-01-11 11:12:19 +01001528 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
Radek Krejci322614f2018-11-16 15:05:44 +01001529 logbuf_assert("Unable to find base (j) of identityref.");
1530
Radek Krejci096235c2019-01-11 11:12:19 +01001531 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
Radek Krejci322614f2018-11-16 15:05:44 +01001532 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1533
Radek Krejci555cb5b2018-11-16 14:54:33 +01001534 *state = NULL;
1535 ly_ctx_destroy(ctx, NULL);
1536}
1537
Radek Krejcia3045382018-11-22 14:30:31 +01001538static void
1539test_type_leafref(void **state)
1540{
1541 *state = test_type_leafref;
1542
1543 struct ly_ctx *ctx;
1544 struct lys_module *mod;
1545 struct lysc_type *type;
1546 const char *path, *name, *prefix;
1547 size_t prefix_len, name_len;
1548 int parent_times, has_predicate;
1549
1550 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1551
1552 /* lys_path_token() */
1553 path = "invalid_path";
1554 parent_times = 0;
1555 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1556 path = "..";
1557 parent_times = 0;
1558 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1559 path = "..[";
1560 parent_times = 0;
1561 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1562 path = "../";
1563 parent_times = 0;
1564 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1565 path = "/";
1566 parent_times = 0;
1567 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1568
1569 path = "../../pref:id/xxx[predicate]/invalid!!!";
1570 parent_times = 0;
1571 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1572 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1573 assert_int_equal(4, prefix_len);
1574 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1575 assert_int_equal(2, name_len);
1576 assert_int_equal(0, strncmp("id", name, name_len));
1577 assert_int_equal(2, parent_times);
1578 assert_int_equal(0, has_predicate);
1579 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1580 assert_string_equal("[predicate]/invalid!!!", path);
1581 assert_int_equal(0, prefix_len);
1582 assert_null(prefix);
1583 assert_int_equal(3, name_len);
1584 assert_int_equal(0, strncmp("xxx", name, name_len));
1585 assert_int_equal(1, has_predicate);
1586 path += 11;
1587 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1588 assert_string_equal("!!!", path);
1589 assert_int_equal(0, prefix_len);
1590 assert_null(prefix);
1591 assert_int_equal(7, name_len);
1592 assert_int_equal(0, strncmp("invalid", name, name_len));
1593
1594 path = "/absolute/prefix:path";
1595 parent_times = 0;
1596 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1597 assert_string_equal("/prefix:path", path);
1598 assert_int_equal(0, prefix_len);
1599 assert_null(prefix);
1600 assert_int_equal(8, name_len);
1601 assert_int_equal(0, strncmp("absolute", name, name_len));
1602 assert_int_equal(-1, parent_times);
1603 assert_int_equal(0, has_predicate);
1604 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1605 assert_int_equal(0, *path);
1606 assert_int_equal(6, prefix_len);
1607 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1608 assert_int_equal(4, name_len);
1609 assert_int_equal(0, strncmp("path", name, name_len));
1610 assert_int_equal(0, has_predicate);
1611
1612 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001613 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 +01001614 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1615 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001616 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1617 assert_non_null(type);
1618 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1619 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001620 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001621 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1622 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001623 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1624 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1625 assert_non_null(type);
1626 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1627 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001628 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1629 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1630 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001631 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1632
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001633 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 +01001634 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001635 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001636 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1637 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001638 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001639 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1640 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1641 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001642 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1643 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001644 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1645
1646 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001647 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 +01001648 "typedef mytype3 {type c:mytype {require-instance false;}}"
1649 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001650 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1651 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001652 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001653 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1654 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1655 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001656 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1657 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001658 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1659 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1660 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001661 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001662 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1663 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1664 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1665 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1666
Radek Krejci4ce68932018-11-28 12:53:36 +01001667 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1668 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1669 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
Radek Krejci4ce68932018-11-28 12:53:36 +01001670 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1671 assert_non_null(type);
1672 assert_int_equal(1, type->refcount);
1673 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1674 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1675 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1676 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1677 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1678 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1679
Radek Krejci58d171e2018-11-23 13:50:55 +01001680 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001681 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 +01001682 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1683 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001684 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1685 assert_non_null(type);
1686 assert_int_equal(1, type->refcount);
1687 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1688 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1689 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1690 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1691 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1692
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001693 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1694 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1695 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001696 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001697 LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +01001698 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001699 assert_non_null(type);
1700 assert_int_equal(1, type->refcount);
1701 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001702 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001703 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1704 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1705 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001706
Radek Krejci20e8a182018-12-07 11:43:21 +01001707 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1708 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1709 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1710 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
Radek Krejci20e8a182018-12-07 11:43:21 +01001711 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1712 assert_non_null(type);
1713 assert_int_equal(1, type->refcount);
1714 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1715 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1716 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1717 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1718 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1719
Radek Krejcia3045382018-11-22 14:30:31 +01001720 /* invalid paths */
Radek Krejci096235c2019-01-11 11:12:19 +01001721 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001722 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001723 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001724 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001725 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001726 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
Radek Krejci096235c2019-01-11 11:12:19 +01001727 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001728 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001729 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001730 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf target1 {type string;}container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001731 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001732 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
Radek Krejci096235c2019-01-11 11:12:19 +01001733 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001734 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001735 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
Radek Krejci096235c2019-01-11 11:12:19 +01001736 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001737 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001738 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
Radek Krejci096235c2019-01-11 11:12:19 +01001739 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
Radek Krejcia3045382018-11-22 14:30:31 +01001740 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
Radek Krejcia3045382018-11-22 14:30:31 +01001741 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci096235c2019-01-11 11:12:19 +01001742 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001743 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001744 logbuf_assert("Missing path substatement for leafref type.");
Radek Krejci096235c2019-01-11 11:12:19 +01001745 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001746 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
Radek Krejci2f4a0292018-11-22 15:41:53 +01001747 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci096235c2019-01-11 11:12:19 +01001748 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001749 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
Radek Krejci58d171e2018-11-23 13:50:55 +01001750 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1751 "applicable to the leafref itself.");
Radek Krejci096235c2019-01-11 11:12:19 +01001752 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001753 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001754 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 +01001755
Radek Krejci096235c2019-01-11 11:12:19 +01001756 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001757 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001758 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
Radek Krejci096235c2019-01-11 11:12:19 +01001759 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001760 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001761 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1762
Radek Krejci096235c2019-01-11 11:12:19 +01001763 assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001764 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1765 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1766 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1767 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001768 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1769
Radek Krejci096235c2019-01-11 11:12:19 +01001770 assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001771 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1772 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1773 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1774 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001775 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1776
Radek Krejci096235c2019-01-11 11:12:19 +01001777 assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001778 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1779 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1780 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1781 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001782 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1783
Radek Krejci096235c2019-01-11 11:12:19 +01001784 assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001785 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1786 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1787 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1788 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001789 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1790
Radek Krejci096235c2019-01-11 11:12:19 +01001791 assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001792 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1793 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1794 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1795 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001796 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1797
Radek Krejci096235c2019-01-11 11:12:19 +01001798 assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001799 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1800 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1801 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1802 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001803 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1804
Radek Krejci096235c2019-01-11 11:12:19 +01001805 assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001806 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1807 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1808 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1809 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001810 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1811
Radek Krejci096235c2019-01-11 11:12:19 +01001812 assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001813 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1814 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1815 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
1816 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001817 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1818
Radek Krejci096235c2019-01-11 11:12:19 +01001819 assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001820 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1821 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1822 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
1823 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001824 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1825
Radek Krejci096235c2019-01-11 11:12:19 +01001826 assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001827 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1828 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1829 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
1830 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001831 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1832
Radek Krejci096235c2019-01-11 11:12:19 +01001833 assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001834 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1835 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1836 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
1837 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001838 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1839
Radek Krejci096235c2019-01-11 11:12:19 +01001840 assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001841 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1842 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1843 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1844 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001845 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1846
Radek Krejci096235c2019-01-11 11:12:19 +01001847 assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001848 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1849 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1850 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
1851 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001852 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1853
Radek Krejci096235c2019-01-11 11:12:19 +01001854 assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001855 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1856 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
1857 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1858 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001859 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1860
Radek Krejci096235c2019-01-11 11:12:19 +01001861 assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
Radek Krejcibade82a2018-12-05 10:13:48 +01001862 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1863 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1864 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1865 LYS_IN_YANG));
Radek Krejcibade82a2018-12-05 10:13:48 +01001866 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1867
Radek Krejci412ddfa2018-11-23 11:44:11 +01001868 /* circular chain */
Radek Krejci096235c2019-01-11 11:12:19 +01001869 assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
Radek Krejci412ddfa2018-11-23 11:44:11 +01001870 "leaf ref1 {type leafref {path /ref2;}}"
1871 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001872 "leaf ref3 {type leafref {path /ref4;}}"
1873 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001874 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1875
Radek Krejcia3045382018-11-22 14:30:31 +01001876 *state = NULL;
1877 ly_ctx_destroy(ctx, NULL);
1878}
1879
Radek Krejci43699232018-11-23 14:59:46 +01001880static void
1881test_type_empty(void **state)
1882{
1883 *state = test_type_empty;
1884
1885 struct ly_ctx *ctx;
Radek Krejci43699232018-11-23 14:59:46 +01001886
1887 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1888
1889 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01001890 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci43699232018-11-23 14:59:46 +01001891 "leaf l {type empty; default x;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001892 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1893
Radek Krejci096235c2019-01-11 11:12:19 +01001894 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
Radek Krejci43699232018-11-23 14:59:46 +01001895 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci43699232018-11-23 14:59:46 +01001896 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1897
1898 *state = NULL;
1899 ly_ctx_destroy(ctx, NULL);
1900}
1901
Radek Krejcicdfecd92018-11-26 11:27:32 +01001902
1903static void
1904test_type_union(void **state)
1905{
1906 *state = test_type_union;
1907
1908 struct ly_ctx *ctx;
1909 struct lys_module *mod;
1910 struct lysc_type *type;
1911
1912 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1913
1914 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
1915 "typedef mytype {type union {type int8; type mybasetype;}}"
1916 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001917 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1918 assert_non_null(type);
1919 assert_int_equal(2, type->refcount);
1920 assert_int_equal(LY_TYPE_UNION, type->basetype);
1921 assert_non_null(((struct lysc_type_union*)type)->types);
1922 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1923 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
1924 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1925
1926 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
1927 "typedef mytype {type union {type int8; type mybasetype;}}"
1928 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001929 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1930 assert_non_null(type);
1931 assert_int_equal(1, type->refcount);
1932 assert_int_equal(LY_TYPE_UNION, type->basetype);
1933 assert_non_null(((struct lysc_type_union*)type)->types);
1934 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1935 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1936 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
1937 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1938
1939 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
1940 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001941 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
1942 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01001943 LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001944 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1945 assert_non_null(type);
1946 assert_int_equal(1, type->refcount);
1947 assert_int_equal(LY_TYPE_UNION, type->basetype);
1948 assert_non_null(((struct lysc_type_union*)type)->types);
1949 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1950 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1951 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
1952 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1953 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
1954 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
1955
Radek Krejci6be5ff12018-11-26 13:18:15 +01001956 /* invalid unions */
Radek Krejci096235c2019-01-11 11:12:19 +01001957 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001958 "leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001959 logbuf_assert("Missing type substatement for union type mytype.");
1960
Radek Krejci096235c2019-01-11 11:12:19 +01001961 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
1962 logbuf_assert("Missing type substatement for union type.");
Radek Krejci6be5ff12018-11-26 13:18:15 +01001963
Radek Krejci096235c2019-01-11 11:12:19 +01001964 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001965 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001966 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
1967
Radek Krejci096235c2019-01-11 11:12:19 +01001968 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001969 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01001970 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
1971
Radek Krejcicdfecd92018-11-26 11:27:32 +01001972 *state = NULL;
1973 ly_ctx_destroy(ctx, NULL);
1974}
1975
1976static void
1977test_type_dflt(void **state)
1978{
1979 *state = test_type_union;
1980
1981 struct ly_ctx *ctx;
1982 struct lys_module *mod;
1983 struct lysc_type *type;
1984
1985 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1986
1987 /* default is not inherited from union's types */
1988 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
1989 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001990 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1991 assert_non_null(type);
1992 assert_int_equal(1, type->refcount);
1993 assert_int_equal(LY_TYPE_UNION, type->basetype);
1994 assert_non_null(((struct lysc_type_union*)type)->types);
1995 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1996 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1997 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1998 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1999 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2000
2001 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2002 "leaf l {type mybasetype;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002003 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2004 assert_non_null(type);
2005 assert_int_equal(2, type->refcount);
2006 assert_int_equal(LY_TYPE_STRING, type->basetype);
2007 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2008 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2009
2010 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2011 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002012 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2013 assert_non_null(type);
2014 assert_int_equal(2, type->refcount);
2015 assert_int_equal(LY_TYPE_STRING, type->basetype);
2016 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2017 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2018
Radek Krejci6be5ff12018-11-26 13:18:15 +01002019 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2020 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2021 "leaf l2 {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002022 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2023 assert_non_null(type);
2024 assert_int_equal(4, type->refcount);
2025 assert_int_equal(LY_TYPE_STRING, type->basetype);
2026 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2027 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2028 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2029 assert_non_null(type);
2030 assert_int_equal(4, type->refcount);
2031 assert_int_equal(LY_TYPE_STRING, type->basetype);
2032 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2033 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2034
2035 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2036 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
Radek Krejci6be5ff12018-11-26 13:18:15 +01002037 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2038 assert_non_null(type);
2039 assert_int_equal(3, type->refcount);
2040 assert_int_equal(LY_TYPE_STRING, type->basetype);
2041 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2042 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2043
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002044 /* mandatory leaf does not takes default value from type */
2045 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2046 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002047 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2048 assert_non_null(type);
2049 assert_int_equal(LY_TYPE_STRING, type->basetype);
2050 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2051 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2052
Radek Krejcicdfecd92018-11-26 11:27:32 +01002053 *state = NULL;
2054 ly_ctx_destroy(ctx, NULL);
2055}
2056
Radek Krejci665421c2018-12-05 08:03:39 +01002057static void
2058test_status(void **state)
2059{
2060 *state = test_status;
2061
2062 struct ly_ctx *ctx;
Radek Krejci665421c2018-12-05 08:03:39 +01002063
2064 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2065
Radek Krejci096235c2019-01-11 11:12:19 +01002066 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
Radek Krejci665421c2018-12-05 08:03:39 +01002067 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002068 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2069
Radek Krejci096235c2019-01-11 11:12:19 +01002070 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
Radek Krejci665421c2018-12-05 08:03:39 +01002071 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002072 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2073
Radek Krejci096235c2019-01-11 11:12:19 +01002074 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
Radek Krejci665421c2018-12-05 08:03:39 +01002075 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
Radek Krejci665421c2018-12-05 08:03:39 +01002076 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2077
2078 *state = NULL;
2079 ly_ctx_destroy(ctx, NULL);
2080}
2081
Radek Krejcie86bf772018-12-14 11:39:53 +01002082static void
2083test_uses(void **state)
2084{
2085 *state = test_uses;
2086
2087 struct ly_ctx *ctx;
2088 struct lys_module *mod;
2089 struct lysc_node *parent, *child;
2090
2091 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2092
Radek Krejci096235c2019-01-11 11:12:19 +01002093 assert_non_null(lys_parse_mem(ctx, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;}"
2094 "grouping grp {leaf x {type mytype;}}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002095
2096
2097 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2098 "grouping grp_a_top {leaf a1 {type int8;}}"
2099 "container a {uses grp_a; uses grp_a_top; uses g:grp; grouping grp_a {leaf a2 {type uint8;}}}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002100 assert_non_null((parent = mod->compiled->data));
2101 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2102 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2103 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002104 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002105 assert_non_null((child = child->next));
2106 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002107 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002108 assert_non_null((child = child->next));
2109 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002110 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002111
Radek Krejcib1b59152019-01-07 13:21:56 +01002112 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {type string;}}}");
2113 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp;}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002114 assert_non_null(mod->compiled->data);
2115 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2116 assert_string_equal("b", mod->compiled->data->name);
2117
Radek Krejci7f6a3812019-01-07 13:48:57 +01002118 logbuf_clean();
2119 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;"
2120 "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}"
2121 "uses grp {status deprecated;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002122 assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype);
2123 assert_string_equal("l", mod->compiled->data->name);
2124 assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags);
2125 assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype);
2126 assert_string_equal("k", mod->compiled->data->next->name);
2127 assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags);
2128 logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */
2129
Radek Krejcie86bf772018-12-14 11:39:53 +01002130 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002131 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002132 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2133
Radek Krejci096235c2019-01-11 11:12:19 +01002134 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
Radek Krejcie86bf772018-12-14 11:39:53 +01002135 "grouping grp {leaf a{type string;}uses grp1;}"
2136 "grouping grp1 {leaf b {type string;}uses grp2;}"
2137 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
Radek Krejcie86bf772018-12-14 11:39:53 +01002138 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2139
Radek Krejci096235c2019-01-11 11:12:19 +01002140 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002141 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2142
Radek Krejci096235c2019-01-11 11:12:19 +01002143 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;grouping grp{leaf a{type string;}}"
Radek Krejci76b3e962018-12-14 17:01:25 +01002144 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
Radek Krejci76b3e962018-12-14 17:01:25 +01002145 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2146
Radek Krejci096235c2019-01-11 11:12:19 +01002147 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;grouping grp {leaf l {type string; status deprecated;}}"
Radek Krejci7f6a3812019-01-07 13:48:57 +01002148 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002149 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2150
Radek Krejcie86bf772018-12-14 11:39:53 +01002151 *state = NULL;
2152 ly_ctx_destroy(ctx, NULL);
2153}
2154
Radek Krejci01342af2019-01-03 15:18:08 +01002155static void
2156test_refine(void **state)
2157{
2158 *state = test_refine;
2159
2160 struct ly_ctx *ctx;
2161 struct lys_module *mod;
2162 struct lysc_node *parent, *child;
2163
2164 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2165
Radek Krejci096235c2019-01-11 11:12:19 +01002166 assert_non_null(lys_parse_mem(ctx, "module grp {yang-version 1.1;namespace urn:grp;prefix g; feature f;typedef mytype {type string; default cheers!;}"
2167 "grouping grp {container c {leaf l {type mytype; default goodbye;}"
2168 "leaf-list ll {type mytype; default goodbye; max-elements 6;}"
2169 "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}"
2170 "leaf x {type mytype; mandatory true; must 1;}"
2171 "anydata a {mandatory false; if-feature f; description original; reference original;}"
2172 "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002173
Radek Krejcif0089082019-01-07 16:42:01 +01002174 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 +01002175 "uses g:grp {refine c/l {default hello; config false;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002176 "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}"
Radek Krejcif0089082019-01-07 16:42:01 +01002177 "refine c/ch {default b;config true; if-feature fa;}"
Radek Krejcif3404542019-01-08 13:28:42 +01002178 "refine c/x {mandatory false; must ../ll;description refined; reference refined;}"
2179 "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002180 "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002181 assert_non_null((parent = mod->compiled->data));
2182 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2183 assert_string_equal("c", parent->name);
2184 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2185 assert_int_equal(LYS_LEAF, child->nodetype);
2186 assert_string_equal("l", child->name);
2187 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2188 assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK);
2189 assert_non_null(child = child->next);
2190 assert_int_equal(LYS_LEAFLIST, child->nodetype);
2191 assert_string_equal("ll", child->name);
2192 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts));
2193 assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]);
2194 assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]);
Radek Krejci6b22ab72019-01-07 15:39:20 +01002195 assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min);
2196 assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max);
Radek Krejci01342af2019-01-03 15:18:08 +01002197 assert_non_null(child = child->next);
2198 assert_int_equal(LYS_CHOICE, child->nodetype);
2199 assert_string_equal("ch", child->name);
2200 assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name);
2201 assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags);
2202 assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags);
Radek Krejcif0089082019-01-07 16:42:01 +01002203 assert_non_null(child->iffeatures);
2204 assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejci01342af2019-01-03 15:18:08 +01002205 assert_non_null(child = child->next);
2206 assert_int_equal(LYS_LEAF, child->nodetype);
2207 assert_string_equal("x", child->name);
2208 assert_false(LYS_MAND_TRUE & child->flags);
2209 assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt);
Radek Krejci9a564c92019-01-07 14:53:57 +01002210 assert_non_null(((struct lysc_node_leaf*)child)->musts);
2211 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts));
Radek Krejcif3404542019-01-08 13:28:42 +01002212 assert_string_equal("refined", child->dsc);
2213 assert_string_equal("refined", child->ref);
Radek Krejcib1b59152019-01-07 13:21:56 +01002214 assert_non_null(child = child->next);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002215 assert_int_equal(LYS_ANYDATA, child->nodetype);
2216 assert_string_equal("a", child->name);
2217 assert_true(LYS_MAND_TRUE & child->flags);
Radek Krejci9a564c92019-01-07 14:53:57 +01002218 assert_non_null(((struct lysc_node_anydata*)child)->musts);
2219 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts));
Radek Krejcif0089082019-01-07 16:42:01 +01002220 assert_non_null(child->iffeatures);
2221 assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures));
Radek Krejcif3404542019-01-08 13:28:42 +01002222 assert_string_equal("refined", child->dsc);
2223 assert_string_equal("refined", child->ref);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002224 assert_non_null(child = child->next);
Radek Krejcib1b59152019-01-07 13:21:56 +01002225 assert_int_equal(LYS_CONTAINER, child->nodetype);
2226 assert_string_equal("c", child->name);
Radek Krejci7f6a3812019-01-07 13:48:57 +01002227 assert_true(LYS_PRESENCE & child->flags);
Radek Krejcib1b59152019-01-07 13:21:56 +01002228 assert_true(LYS_CONFIG_W & child->flags);
2229 assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags);
Radek Krejci01342af2019-01-03 15:18:08 +01002230
2231 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 +01002232 "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG));
Radek Krejci7f6a3812019-01-07 13:48:57 +01002233 assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev));
Radek Krejci01342af2019-01-03 15:18:08 +01002234 assert_int_equal(LYS_LEAF, child->nodetype);
2235 assert_string_equal("x", child->name);
2236 assert_false(LYS_MAND_TRUE & child->flags);
2237 assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt);
2238
2239 /* invalid */
Radek Krejci096235c2019-01-11 11:12:19 +01002240 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002241 "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002242 logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s).");
2243
Radek Krejci096235c2019-01-11 11:12:19 +01002244 assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002245 "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002246 logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values.");
2247
Radek Krejci096235c2019-01-11 11:12:19 +01002248 assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002249 "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002250 logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
2251
Radek Krejci096235c2019-01-11 11:12:19 +01002252 assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002253 "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002254 logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement.");
2255
Radek Krejci096235c2019-01-11 11:12:19 +01002256 assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002257 "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002258 logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement.");
Radek Krejci096235c2019-01-11 11:12:19 +01002259 assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002260 "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002261 logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement.");
2262
Radek Krejci096235c2019-01-11 11:12:19 +01002263 assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002264 "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002265 logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" - leaf under the default case.");
2266
Radek Krejci096235c2019-01-11 11:12:19 +01002267 assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}"
Radek Krejci01342af2019-01-03 15:18:08 +01002268 "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG));
Radek Krejci01342af2019-01-03 15:18:08 +01002269 logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory.");
2270
Radek Krejci096235c2019-01-11 11:12:19 +01002271 assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002272 "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002273 logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node.");
2274
Radek Krejci096235c2019-01-11 11:12:19 +01002275 assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;grouping grp {leaf l {type string; status deprecated;}}"
Radek Krejcib1b59152019-01-07 13:21:56 +01002276 "uses grp {status obsolete;}}", LYS_IN_YANG));
Radek Krejcib1b59152019-01-07 13:21:56 +01002277 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2278
Radek Krejci096235c2019-01-11 11:12:19 +01002279 assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}"
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002280 "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG));
Radek Krejci9a54f1f2019-01-07 13:47:55 +01002281 logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement.");
2282
Radek Krejci096235c2019-01-11 11:12:19 +01002283 assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}"
Radek Krejci9a564c92019-01-07 14:53:57 +01002284 "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG));
Radek Krejci9a564c92019-01-07 14:53:57 +01002285 logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement.");
2286
Radek Krejci096235c2019-01-11 11:12:19 +01002287 assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}"
Radek Krejci6b22ab72019-01-07 15:39:20 +01002288 "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG));
Radek Krejci6b22ab72019-01-07 15:39:20 +01002289 logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement.");
2290
Radek Krejci096235c2019-01-11 11:12:19 +01002291 assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}"
Radek Krejcif2271f12019-01-07 16:42:23 +01002292 "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG));
Radek Krejcif2271f12019-01-07 16:42:23 +01002293 logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\".");
2294
Radek Krejci01342af2019-01-03 15:18:08 +01002295 *state = NULL;
2296 ly_ctx_destroy(ctx, NULL);
2297}
Radek Krejcie86bf772018-12-14 11:39:53 +01002298
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002299int main(void)
2300{
2301 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002302 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2303 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2304 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002305 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2306 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002307 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002308 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2309 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002310 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002311 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002312 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002313 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002314 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002315 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2316 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002317 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002318 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002319 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002320 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002321 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002322 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002323 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci01342af2019-01-03 15:18:08 +01002324 cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002325 };
2326
2327 return cmocka_run_group_tests(tests, NULL, NULL);
2328}