blob: ea088ed782f57a86158fd2bebf5fca670df8fc0d [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
102test_module(void **state)
103{
104 (void) state; /* unused */
105
106 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +0100107 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200108 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +0200109 struct lysc_feature *f;
110 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200111
112 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200113 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100114 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200115
Radek Krejcif8f882a2018-10-31 14:51:15 +0100116 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
117 logbuf_assert("Invalid argument mod (lys_compile()).");
118 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
119 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci313d9902018-11-08 09:42:58 +0100120 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100121 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200122 assert_non_null(mod.compiled);
123 assert_ptr_equal(mod.parsed->name, mod.compiled->name);
124 assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
Radek Krejci151a5b72018-10-19 14:21:44 +0200125 /* features */
126 assert_non_null(mod.compiled->features);
127 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200128 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200129 assert_non_null(f->iffeatures);
130 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200131 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200132 assert_non_null(iff->expr);
133 assert_non_null(iff->features);
134 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200135 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200136
Radek Krejci86d106e2018-10-18 09:53:19 +0200137 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200138
Radek Krejcif8f882a2018-10-31 14:51:15 +0100139 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200140 assert_non_null(mod.compiled);
141 assert_string_equal("test", mod.compiled->name);
142 assert_string_equal("urn:test", mod.compiled->ns);
143
Radek Krejci86d106e2018-10-18 09:53:19 +0200144 lysc_module_free(mod.compiled, NULL);
145 mod.compiled = NULL;
146
Radek Krejci151a5b72018-10-19 14:21:44 +0200147 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200148 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100149 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100150 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
Radek Krejci86d106e2018-10-18 09:53:19 +0200151 logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules.");
152 assert_null(mod.compiled);
Radek Krejci86d106e2018-10-18 09:53:19 +0200153 lysp_module_free(mod.parsed);
Radek Krejci76b3e962018-12-14 17:01:25 +0100154
155 /* data definition name collision in top level */
156 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module aa {namespace urn:aa;prefix aa;"
157 "leaf a {type string;} container a{presence x;}}", &mod.parsed));
158 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
159 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
160 assert_null(mod.compiled);
161 lysp_module_free(mod.parsed);
162
Radek Krejci313d9902018-11-08 09:42:58 +0100163 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200164}
165
Radek Krejci151a5b72018-10-19 14:21:44 +0200166static void
167test_feature(void **state)
168{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100169 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200170
Radek Krejci313d9902018-11-08 09:42:58 +0100171 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100172 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200173 const char *str;
174 struct lysc_feature *f, *f1;
175
176 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
177 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100178 "feature orfeature {if-feature \"f1 or f2\";}\n"
179 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200180 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200181 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100182 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
183 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200184
Radek Krejci313d9902018-11-08 09:42:58 +0100185 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
186 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100187 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200188 assert_non_null(mod.compiled);
189 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100190 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200191 /* all features are disabled by default */
192 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
193 assert_int_equal(0, lysc_feature_value(f));
194 }
195 /* enable f1 */
196 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200197 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200198 assert_int_equal(1, lysc_feature_value(f1));
199
Radek Krejci87616bb2018-10-31 13:30:52 +0100200 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200201 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200202 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100203 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200204 assert_int_equal(1, lysc_feature_value(f));
205
Radek Krejci87616bb2018-10-31 13:30:52 +0100206 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200207 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200208 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100209 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
210 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200211 assert_int_equal(0, lysc_feature_value(f));
212
213 /* first enable f2, so f5 can be enabled then */
214 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100215 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200216 assert_int_equal(1, lysc_feature_value(f));
217
218 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200219 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200220 assert_int_equal(0, lysc_feature_value(f));
221 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
222 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
223 assert_int_equal(0, lysc_feature_value(f));
224
Radek Krejci87616bb2018-10-31 13:30:52 +0100225 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200226 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200227 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
228 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200229 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100230 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200231 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200232 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200233 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200234 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200235
236 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200237 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200238 /* long evaluation of f8 to need to reallocate internal stack for operators */
239 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200240
Radek Krejci87616bb2018-10-31 13:30:52 +0100241 /* double negation of disabled f1 -> disabled */
242 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
243
Radek Krejci38920282018-11-01 09:24:16 +0100244 /* disable all features */
245 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
246 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
247 assert_int_equal(0, lys_feature_value(&mod, f->name));
248 }
249 /* re-setting already set feature */
250 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
251 assert_int_equal(0, lys_feature_value(&mod, "f1"));
252
253 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100254 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
255 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
256 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100257 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
258 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100259 /* test if not changed */
260 assert_int_equal(1, lys_feature_value(&mod, "f1"));
261 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100262
Radek Krejci1aefdf72018-11-01 11:01:39 +0100263 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100264 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
265 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
266
Radek Krejci151a5b72018-10-19 14:21:44 +0200267 lysc_module_free(mod.compiled, NULL);
268 lysp_module_free(mod.parsed);
Radek Krejci87616bb2018-10-31 13:30:52 +0100269
270 /* some invalid expressions */
Radek Krejci313d9902018-11-08 09:42:58 +0100271 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature f1;}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100272 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100273 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
274 lysp_module_free(mod.parsed);
275
Radek Krejci313d9902018-11-08 09:42:58 +0100276 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100277 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100278 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
279 lysp_module_free(mod.parsed);
280
Radek Krejci313d9902018-11-08 09:42:58 +0100281 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100282 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100283 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
284 lysp_module_free(mod.parsed);
285
Radek Krejci313d9902018-11-08 09:42:58 +0100286 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100287 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100288 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
289 lysp_module_free(mod.parsed);
290
Radek Krejci313d9902018-11-08 09:42:58 +0100291 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100292 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100293 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
294 lysp_module_free(mod.parsed);
295
Radek Krejci313d9902018-11-08 09:42:58 +0100296 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod.parsed));
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 \"---\" of if-feature - unable to find feature \"---\".");
299 lysp_module_free(mod.parsed);
300
Radek Krejci313d9902018-11-08 09:42:58 +0100301 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod.parsed));
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 \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
304 lysp_module_free(mod.parsed);
305
Radek Krejcid05cbd92018-12-05 14:26:40 +0100306 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod.parsed));
307 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
308 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
309 lysp_module_free(mod.parsed);
310
311 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sb {belongs-to b {prefix b;} feature f1;}");
312 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b{namespace urn:b; prefix b; include sb;feature f1;}", LYS_IN_YANG));
313 assert_int_equal(LY_EVALID, lys_compile(modp, 0));
314 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
315
Radek Krejci1aefdf72018-11-01 11:01:39 +0100316 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100317 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100318 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
319 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100320 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 +0100321 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
322 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
323 assert_int_equal(0, lys_feature_value(modp, "f1"));
324 assert_int_equal(1, lys_feature_value(modp, "f2"));
325
Radek Krejcid05cbd92018-12-05 14:26:40 +0100326 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100327 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200328}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200329
Radek Krejci478020e2018-10-30 16:02:14 +0100330static void
331test_identity(void **state)
332{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100333 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100334
335 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100336 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100337 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100338 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 +0100339
340 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
341 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
342 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejci7f2a5362018-11-28 13:05:37 +0100343 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100344 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100345
346 assert_non_null(mod1->compiled);
347 assert_non_null(mod1->compiled->identities);
348 assert_non_null(mod2->compiled);
349 assert_non_null(mod2->compiled->identities);
350
351 assert_non_null(mod1->compiled->identities[0].derived);
352 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
353 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
354 assert_non_null(mod2->compiled->identities[0].derived);
355 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
356 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
357 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
358 assert_non_null(mod2->compiled->identities[1].derived);
359 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
360 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
361 assert_non_null(mod2->compiled->identities[2].derived);
362 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
363 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
364
Radek Krejcid05cbd92018-12-05 14:26:40 +0100365 assert_non_null(mod1 = lys_parse_mem(ctx, "module c{namespace urn:c; prefix c; identity i1;identity i1;}", LYS_IN_YANG));
366 assert_int_equal(LY_EVALID, lys_compile(mod1, 0));
367 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
368
369 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sd {belongs-to d {prefix d;} identity i1;}");
370 assert_non_null(mod1 = lys_parse_mem(ctx, "module d{namespace urn:d; prefix d; include sd;identity i1;}", LYS_IN_YANG));
371 assert_int_equal(LY_EVALID, lys_compile(mod1, 0));
372 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
373
Radek Krejci7f2a5362018-11-28 13:05:37 +0100374 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100375 ly_ctx_destroy(ctx, NULL);
376}
377
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100378static void
379test_node_container(void **state)
380{
381 (void) state; /* unused */
382
383 struct ly_ctx *ctx;
384 struct lys_module *mod;
385 struct lysc_node_container *cont;
386
387 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
388 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
389 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
390 assert_non_null(mod->compiled);
391 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
392 assert_int_equal(LYS_CONTAINER, cont->nodetype);
393 assert_string_equal("c", cont->name);
394 assert_true(cont->flags & LYS_CONFIG_W);
395 assert_true(cont->flags & LYS_STATUS_CURR);
396
Radek Krejci98094b32018-11-02 16:21:47 +0100397 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;container c {config false; status deprecated; container child;}}", LYS_IN_YANG));
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100398 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100399 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100400 assert_non_null(mod->compiled);
401 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
402 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100403 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100404 assert_non_null((cont = (struct lysc_node_container*)cont->child));
405 assert_int_equal(LYS_CONTAINER, cont->nodetype);
406 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100407 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100408 assert_string_equal("child", cont->name);
409
410 ly_ctx_destroy(ctx, NULL);
411}
412
Radek Krejci0e5d8382018-11-28 16:37:53 +0100413static void
414test_node_leaflist(void **state)
415{
416 *state = test_node_leaflist;
417
418 struct ly_ctx *ctx;
419 struct lys_module *mod;
420 struct lysc_type *type;
421 struct lysc_node_leaflist *ll;
422
423 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
424
425 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
426 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
427 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
428 "leaf-list ll2 {type leafref {path ../target;}}"
429 "leaf target {type int8;}}",
430 LYS_IN_YANG));
431 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
432 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
433 assert_non_null(type);
434 assert_int_equal(1, type->refcount);
435 assert_int_equal(LY_TYPE_UNION, type->basetype);
436 assert_non_null(((struct lysc_type_union*)type)->types);
437 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
438 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
439 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
440 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
441 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
442 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
443 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
444 assert_non_null(type);
445 assert_int_equal(1, type->refcount);
446 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
447 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
448 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
449
Radek Krejci6bb080b2018-11-28 17:23:41 +0100450 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 +0100451 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
452 assert_non_null(mod->compiled);
453 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
454 assert_int_equal(0, ll->min);
455 assert_int_equal((uint32_t)-1, ll->max);
456
Radek Krejci6bb080b2018-11-28 17:23:41 +0100457 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 +0100458 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100459 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100460 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
461 assert_non_null(mod->compiled);
462 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
463 assert_non_null(ll->dflts);
464 assert_int_equal(3, ll->type->refcount);
465 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
466 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100467 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100468 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100469 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
470 assert_non_null(ll->dflts);
471 assert_int_equal(3, ll->type->refcount);
472 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
473 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100474 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
475
476 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
477 * TODO test also, RPC output parameters and notification content */
478 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
479 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
480 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
481 /* but warning is present: */
482 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
483 assert_non_null(mod->compiled);
484 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
485 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100486
487 /* invalid */
488 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
489 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
490 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
491
492 assert_non_null(mod = lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG));
493 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
494 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
495
496 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
497 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
498 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
499 assert_non_null(mod->compiled);
500 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
501 assert_non_null(ll->dflts);
502 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
503 assert_non_null(mod = lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
504 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
505 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
506 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
507
Radek Krejci0e5d8382018-11-28 16:37:53 +0100508 *state = NULL;
509 ly_ctx_destroy(ctx, NULL);
510}
511
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100512static void
513test_node_list(void **state)
514{
515 *state = test_node_list;
516
517 struct ly_ctx *ctx;
518 struct lys_module *mod;
519 struct lysc_node_list *list;
520
521 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
522
523 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
524 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
525 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
526 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
527 list = (struct lysc_node_list*)mod->compiled->data;
528 assert_non_null(list);
529 assert_non_null(list->keys);
530 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
531 assert_string_equal("x", list->keys[0]->name);
532 assert_string_equal("y", list->keys[1]->name);
533 assert_non_null(list->child);
534 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
535 assert_true(list->child->flags & LYS_KEY);
536 assert_true(list->child->next->flags & LYS_KEY);
537 list = (struct lysc_node_list*)mod->compiled->data->next;
538 assert_non_null(list);
539 assert_null(list->keys);
540 assert_non_null(list->child);
541 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
542 assert_false(list->child->flags & LYS_KEY);
543
544 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
545 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100546 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100547 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
548 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
549 list = (struct lysc_node_list*)mod->compiled->data;
550 assert_non_null(list);
551 assert_string_equal("l", list->name);
552 assert_non_null(list->keys);
553 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
554 assert_string_equal("a", list->keys[0]->name);
555 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100556 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100557 assert_non_null(list->uniques);
558 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
559 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
560 assert_string_equal("a", list->uniques[0][0]->name);
561 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
562 assert_string_equal("b", list->uniques[0][1]->name);
563 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
564 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
565 assert_string_equal("e", list->uniques[1][0]->name);
566 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
567 assert_string_equal("d", list->uniques[1][1]->name);
568 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
569
Radek Krejci665421c2018-12-05 08:03:39 +0100570 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
571 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
572 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
573 list = (struct lysc_node_list*)mod->compiled->data;
574 assert_non_null(list);
575 assert_string_equal("l", list->name);
576 assert_non_null(list->keys);
577 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
578 assert_string_equal("a", list->keys[0]->name);
579 assert_true(list->keys[0]->flags & LYS_KEY);
580 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
581
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100582 /* invalid */
583 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
584 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
585 logbuf_assert("Missing key in list representing configuration data.");
586
587 assert_non_null(mod = lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
588 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
589 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
590 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
591
592 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
593 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
594 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
595 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
596
597 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
598 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
599 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
600 logbuf_assert("Key of the configuration list must not be status leaf.");
601
602 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
603 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
604 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
605 logbuf_assert("Configuration node cannot be child of any state data node.");
606
607 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
608 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
609 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
610 logbuf_assert("The list's key \"x\" not found.");
611
612 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
613 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
614 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
615 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
616
617 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
618 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
619 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
620 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
621
Radek Krejci665421c2018-12-05 08:03:39 +0100622 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
623 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
624 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
625 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
626
627 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
628 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
629 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
630 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
631
632 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
633 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
634 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
635 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
636
637 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
638 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
639 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
640 logbuf_assert("Duplicated key identifier \"x\".");
641
642 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
643 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
644 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
645 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
646
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100647 *state = NULL;
648 ly_ctx_destroy(ctx, NULL);
649}
650
Radek Krejci056d0a82018-12-06 16:57:25 +0100651static void
652test_node_choice(void **state)
653{
654 *state = test_node_choice;
655
656 struct ly_ctx *ctx;
657 struct lys_module *mod;
658 struct lysc_node_choice *ch;
659 struct lysc_node_case *cs;
660
661 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
662
663 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100664 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
665 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100666 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
667 ch = (struct lysc_node_choice*)mod->compiled->data;
668 assert_non_null(ch);
Radek Krejci76b3e962018-12-14 17:01:25 +0100669 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, ch->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +0100670 cs = ch->cases;
671 assert_non_null(cs);
672 assert_string_equal("a", cs->name);
673 assert_ptr_equal(ch, cs->parent);
674 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100675 assert_string_equal("a1", cs->child->name);
676 assert_non_null(cs->child->next);
677 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100678 assert_ptr_equal(cs, cs->child->parent);
679 cs = (struct lysc_node_case*)cs->next;
680 assert_non_null(cs);
681 assert_string_equal("b", cs->name);
682 assert_ptr_equal(ch, cs->parent);
683 assert_non_null(cs->child);
684 assert_string_equal("b", cs->child->name);
685 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100686 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
687 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100688 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100689 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100690
691 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
692 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
693 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
694 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
695 assert_non_null(mod = lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
696 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
697 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
698 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
699 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
700 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
701 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
702 logbuf_assert("Duplicate identifier \"a\" of case statement.");
703 assert_non_null(mod = lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
704 "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
705 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
706 logbuf_assert("Duplicate identifier \"b\" of case statement.");
707
Radek Krejcia9026eb2018-12-12 16:04:47 +0100708 assert_non_null(mod = lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
709 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
710 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
711 logbuf_assert("Default case \"c\" not found.");
712 assert_non_null(mod = lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
713 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
714 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
715 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
716 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
717 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
718 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
719 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
720 /* TODO check with mandatory nodes from augment placed into the case */
721
Radek Krejci056d0a82018-12-06 16:57:25 +0100722 *state = NULL;
723 ly_ctx_destroy(ctx, NULL);
724}
725
Radek Krejci9800fb82018-12-13 14:26:23 +0100726static void
727test_node_anydata(void **state)
728{
729 *state = test_node_anydata;
730
731 struct ly_ctx *ctx;
732 struct lys_module *mod;
733 struct lysc_node_anydata *any;
734
735 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
736
737 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
738 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
739 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
740 any = (struct lysc_node_anydata*)mod->compiled->data;
741 assert_non_null(any);
742 assert_int_equal(LYS_ANYDATA, any->nodetype);
743 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
744
745 logbuf_clean();
746 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
747 "anyxml any;}", LYS_IN_YANG));
748 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
749 any = (struct lysc_node_anydata*)mod->compiled->data;
750 assert_non_null(any);
751 assert_int_equal(LYS_ANYXML, any->nodetype);
752 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
753 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
754
755 /* invalid */
756 assert_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;anydata any;}", LYS_IN_YANG));
757 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
758
759 *state = NULL;
760 ly_ctx_destroy(ctx, NULL);
761}
762
Radek Krejci0f07bed2018-11-13 14:01:40 +0100763/**
764 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
765 * do not test the expression itself
766 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100767static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100768test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100769{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100770 *state = test_type_range;
771
772 struct ly_ctx *ctx;
773 struct lys_module *mod;
774 struct lysc_type *type;
775
776 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
777
778 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));
779 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
780 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
781 assert_non_null(type);
782 assert_int_equal(LY_TYPE_INT8, type->basetype);
783 assert_non_null(((struct lysc_type_num*)type)->range);
784 assert_non_null(((struct lysc_type_num*)type)->range->parts);
785 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
786 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
787 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
788 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
789 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
790
791 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));
792 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
793 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
794 assert_non_null(type);
795 assert_int_equal(LY_TYPE_INT16, type->basetype);
796 assert_non_null(((struct lysc_type_num*)type)->range);
797 assert_non_null(((struct lysc_type_num*)type)->range->parts);
798 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
799 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
800 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
801 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
802 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
803
804 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));
805 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
806 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
807 assert_non_null(type);
808 assert_int_equal(LY_TYPE_INT32, type->basetype);
809 assert_non_null(((struct lysc_type_num*)type)->range);
810 assert_non_null(((struct lysc_type_num*)type)->range->parts);
811 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
812 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
813 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
814 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
815 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
816
817 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));
818 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
819 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
820 assert_non_null(type);
821 assert_int_equal(LY_TYPE_INT64, type->basetype);
822 assert_non_null(((struct lysc_type_num*)type)->range);
823 assert_non_null(((struct lysc_type_num*)type)->range->parts);
824 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
825 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
826 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
827 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
828 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
829
830 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));
831 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
832 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
833 assert_non_null(type);
834 assert_int_equal(LY_TYPE_UINT8, type->basetype);
835 assert_non_null(((struct lysc_type_num*)type)->range);
836 assert_non_null(((struct lysc_type_num*)type)->range->parts);
837 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
838 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
839 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
840 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
841 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
842
843 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));
844 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
845 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
846 assert_non_null(type);
847 assert_int_equal(LY_TYPE_UINT16, type->basetype);
848 assert_non_null(((struct lysc_type_num*)type)->range);
849 assert_non_null(((struct lysc_type_num*)type)->range->parts);
850 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
851 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
852 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
853 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
854 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
855
856 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));
857 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
858 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
859 assert_non_null(type);
860 assert_int_equal(LY_TYPE_UINT32, type->basetype);
861 assert_non_null(((struct lysc_type_num*)type)->range);
862 assert_non_null(((struct lysc_type_num*)type)->range->parts);
863 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
864 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
865 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
866 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
867 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
868
869 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));
870 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
871 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
872 assert_non_null(type);
873 assert_int_equal(LY_TYPE_UINT64, type->basetype);
874 assert_non_null(((struct lysc_type_num*)type)->range);
875 assert_non_null(((struct lysc_type_num*)type)->range->parts);
876 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
877 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
878 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
879 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
880 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
881
882 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
883 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
884 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
885 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
886 assert_non_null(type);
887 assert_int_equal(3, type->refcount);
888 assert_int_equal(LY_TYPE_UINT8, type->basetype);
889 assert_non_null(((struct lysc_type_num*)type)->range);
890 assert_non_null(((struct lysc_type_num*)type)->range->parts);
891 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
892
893 *state = NULL;
894 ly_ctx_destroy(ctx, NULL);
895}
896
897static void
898test_type_length(void **state)
899{
900 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100901
902 struct ly_ctx *ctx;
903 struct lys_module *mod;
904 struct lysc_type *type;
905
906 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
907
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100908 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 +0100909 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
910 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
911 assert_non_null(type);
912 assert_non_null(((struct lysc_type_bin*)type)->length);
913 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100914 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
915 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100916 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
917 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
918 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
919
920 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
921 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
922 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
923 assert_non_null(type);
924 assert_non_null(((struct lysc_type_bin*)type)->length);
925 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
926 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100927 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
928 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100929
930 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));
931 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
932 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
933 assert_non_null(type);
934 assert_non_null(((struct lysc_type_bin*)type)->length);
935 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
936 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
937 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100938 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100939
940 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
941 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
942 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
943 assert_non_null(type);
944 assert_non_null(((struct lysc_type_bin*)type)->length);
945 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
946 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
947 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
948 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
949
950 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));
951 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
952 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
953 assert_non_null(type);
954 assert_non_null(((struct lysc_type_bin*)type)->length);
955 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
956 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
957 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
958 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
959
960 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));
961 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
962 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
963 assert_non_null(type);
964 assert_non_null(((struct lysc_type_bin*)type)->length);
965 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
966 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
967 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
968 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
969 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
970 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
971
972 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));
973 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
974 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
975 assert_non_null(type);
976 assert_non_null(((struct lysc_type_bin*)type)->length);
977 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
978 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
979 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
980 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
981 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
982 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
983
Radek Krejcib31c8992018-11-12 16:29:16 +0100984 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
985 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
986 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
987 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
988 assert_non_null(type);
989 assert_non_null(((struct lysc_type_bin*)type)->length);
990 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
991 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
992 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
993 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
994
995 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
996 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
997 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
998 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
999 assert_non_null(type);
1000 assert_non_null(((struct lysc_type_bin*)type)->length);
1001 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1002 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1003 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1004 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1005
1006 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
1007 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
1008 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1009 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1010 assert_non_null(type);
1011 assert_non_null(((struct lysc_type_bin*)type)->length);
1012 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1013 assert_int_equal(2, 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(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1016 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1017 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1018
1019 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 +01001020 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001021 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1022 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1023 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001024 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001025 assert_non_null(((struct lysc_type_bin*)type)->length);
1026 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1027 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1028 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001029 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001030 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1031 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001032 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001033 assert_non_null(((struct lysc_type_bin*)type)->length);
1034 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1035 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1036 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1037 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1038
Radek Krejci56aa27c2018-11-13 14:21:59 +01001039 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1040 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001041 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1042 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1043 assert_non_null(type);
1044 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001045 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001046 assert_non_null(((struct lysc_type_str*)type)->length);
1047 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1048 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1049 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1050 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001051
Radek Krejci5969f272018-11-23 10:03:58 +01001052 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1053 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
1054 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1055 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1056 assert_non_null(type);
1057 assert_int_equal(LY_TYPE_STRING, type->basetype);
1058 assert_int_equal(1, type->refcount);
1059 assert_non_null(((struct lysc_type_str*)type)->length);
1060 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1061 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1062 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1063 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1064
Radek Krejci4f28eda2018-11-12 11:46:16 +01001065 /* invalid values */
1066 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
1067 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1068 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
1069 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
1070 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1071 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
1072 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG));
1073 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1074 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
1075 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG));
1076 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1077 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
1078 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG));
1079 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1080 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
1081 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
1082 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1083 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001084 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG));
1085 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1086 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
1087 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
1088 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1089 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
1090 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
1091 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1092 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
1093 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
1094 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1095 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
1096 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
1097 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1098 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +01001099 assert_non_null(mod = lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG));
1100 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1101 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001102
1103 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1104 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
1105 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1106 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
1107 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1108 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
1109 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1110 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
1111 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1112 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
1113 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1114 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
1115 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1116 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
1117 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1118 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
1119 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
1120 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
1121 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1122 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
1123 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
1124 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
1125 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1126 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +01001127 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
1128 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
1129 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1130 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001131
Radek Krejci495903e2018-11-13 11:30:45 +01001132 assert_non_null(mod = lys_parse_mem(ctx, "module ss {namespace urn:rr;prefix rr;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG));
1133 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1134 logbuf_assert("Invalid type restrictions for binary type.");
1135
Radek Krejci4f28eda2018-11-12 11:46:16 +01001136 *state = NULL;
1137 ly_ctx_destroy(ctx, NULL);
1138}
1139
Radek Krejcicda74152018-11-13 15:27:32 +01001140static void
1141test_type_pattern(void **state)
1142{
1143 *state = test_type_pattern;
1144
1145 struct ly_ctx *ctx;
1146 struct lys_module *mod;
1147 struct lysc_type *type;
1148
1149 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1150
Radek Krejci027d5802018-11-14 16:57:28 +01001151 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 +01001152 "pattern .* {error-app-tag errortag;error-message error;}"
1153 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
1154 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1155 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1156 assert_non_null(type);
1157 assert_non_null(((struct lysc_type_str*)type)->patterns);
1158 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1159 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1160 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1161 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1162 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1163 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1164 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1165
1166 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1167 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
1168 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1169 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1170 assert_non_null(type);
1171 assert_int_equal(LY_TYPE_STRING, type->basetype);
1172 assert_int_equal(1, type->refcount);
1173 assert_non_null(((struct lysc_type_str*)type)->patterns);
1174 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1175 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1176 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1177
Radek Krejci04bc1e92018-11-14 14:28:13 +01001178 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1179 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
1180 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1181 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1182 assert_non_null(type);
1183 assert_int_equal(LY_TYPE_STRING, type->basetype);
1184 assert_int_equal(1, type->refcount);
1185 assert_non_null(((struct lysc_type_str*)type)->patterns);
1186 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1187 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1188
Radek Krejcicda74152018-11-13 15:27:32 +01001189 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001190 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 +01001191 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
1192 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1193 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1194 assert_non_null(type);
1195 assert_non_null(((struct lysc_type_str*)type)->patterns);
1196 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1197 /* TODO check some data "^Å™$" */
1198
1199 *state = NULL;
1200 ly_ctx_destroy(ctx, NULL);
1201}
1202
Radek Krejci8b764662018-11-14 14:15:13 +01001203static void
1204test_type_enum(void **state)
1205{
1206 *state = test_type_enum;
1207
1208 struct ly_ctx *ctx;
1209 struct lys_module *mod;
1210 struct lysc_type *type;
1211
1212 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1213
Radek Krejci027d5802018-11-14 16:57:28 +01001214 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 +01001215 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1216 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
1217 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1218 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1219 assert_non_null(type);
1220 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1221 assert_non_null(((struct lysc_type_enum*)type)->enums);
1222 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1223 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1224 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1225 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1226 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1227 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1228 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1229 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1230 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1231 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1232 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1233 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1234 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1235 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1236
Radek Krejci027d5802018-11-14 16:57:28 +01001237 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 {"
1238 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001239 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1240 LYS_IN_YANG));
1241 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1242 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1243 assert_non_null(type);
1244 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1245 assert_non_null(((struct lysc_type_enum*)type)->enums);
1246 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1247 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1248 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1249 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1250 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1251
1252
1253 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001254 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1255 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1256 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 +01001257 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1258 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1259 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1260 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1261 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1262 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1263 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1264 "enum one; enum one;}}}", LYS_IN_YANG));
1265 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1266 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1267 "enum '';}}}", LYS_IN_YANG));
1268 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1269 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1270 "enum ' x';}}}", LYS_IN_YANG));
1271 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1272 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1273 "enum 'x ';}}}", LYS_IN_YANG));
1274 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1275 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1276 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1277 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1278
1279 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
1280 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1281 logbuf_assert("Missing enum substatement for enumeration type.");
1282
Radek Krejci027d5802018-11-14 16:57:28 +01001283 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type enumeration {enum one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001284 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
1285 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1286 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1287
Radek Krejci027d5802018-11-14 16:57:28 +01001288 assert_non_null(mod = lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type enumeration {enum one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001289 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
1290 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1291 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
1292 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type enumeration {enum x {value 2147483647;}enum y;}}}",
1293 LYS_IN_YANG));
1294 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1295 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1296
1297 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type enumeration {enum x {value 1;}enum y {value 1;}}}}",
1298 LYS_IN_YANG));
1299 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1300 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1301
Radek Krejci04bc1e92018-11-14 14:28:13 +01001302 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
1303 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1304 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001305 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001306
Radek Krejci027d5802018-11-14 16:57:28 +01001307 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
1308 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1309 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1310 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1311
Radek Krejci8b764662018-11-14 14:15:13 +01001312 *state = NULL;
1313 ly_ctx_destroy(ctx, NULL);
1314}
1315
1316static void
1317test_type_bits(void **state)
1318{
1319 *state = test_type_bits;
1320
1321 struct ly_ctx *ctx;
1322 struct lys_module *mod;
1323 struct lysc_type *type;
1324
1325 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1326
Radek Krejci027d5802018-11-14 16:57:28 +01001327 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 +01001328 "bit automin; bit one {if-feature f; position 1;}"
1329 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
1330 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1331 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1332 assert_non_null(type);
1333 assert_int_equal(LY_TYPE_BITS, type->basetype);
1334 assert_non_null(((struct lysc_type_bits*)type)->bits);
1335 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1336 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1337 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1338 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1339 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1340 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1341 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1342 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1343 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1344 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1345 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1346 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1347
Radek Krejci027d5802018-11-14 16:57:28 +01001348 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 {"
1349 "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 +01001350 LYS_IN_YANG));
1351 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1352 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1353 assert_non_null(type);
1354 assert_int_equal(LY_TYPE_BITS, type->basetype);
1355 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001356 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1357 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1358 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1359 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1360 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1361 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1362 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001363
1364
1365 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001366 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1367 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1368 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 +01001369 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1370 "bit one {position -1;}}}}", LYS_IN_YANG));
1371 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1372 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1373 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1374 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1375 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1376 "bit one; bit one;}}}", LYS_IN_YANG));
1377 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1378 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1379 "bit '11';}}}", LYS_IN_YANG));
1380 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1381 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1382 "bit 'x1$1';}}}", LYS_IN_YANG));
1383 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1384
1385 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
1386 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1387 logbuf_assert("Missing bit substatement for bits type.");
1388
Radek Krejci027d5802018-11-14 16:57:28 +01001389 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type bits {bit one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001390 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
1391 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1392 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1393
Radek Krejci027d5802018-11-14 16:57:28 +01001394 assert_non_null(mod = lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type bits {bit one;}}"
Radek Krejci8b764662018-11-14 14:15:13 +01001395 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
1396 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1397 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
1398 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}",
1399 LYS_IN_YANG));
1400 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1401 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1402
1403 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {value 1;}bit y {value 1;}}}}",
1404 LYS_IN_YANG));
1405 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1406 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1407
Radek Krejci04bc1e92018-11-14 14:28:13 +01001408 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1409 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1410 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001411 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001412
Radek Krejci027d5802018-11-14 16:57:28 +01001413 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1414 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1415 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1416 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1417
Radek Krejci8b764662018-11-14 14:15:13 +01001418 *state = NULL;
1419 ly_ctx_destroy(ctx, NULL);
1420}
1421
Radek Krejci6cba4292018-11-15 17:33:29 +01001422static void
1423test_type_dec64(void **state)
1424{
1425 *state = test_type_dec64;
1426
1427 struct ly_ctx *ctx;
1428 struct lys_module *mod;
1429 struct lysc_type *type;
1430
1431 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1432
1433 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1434 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
1435 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1436 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1437 assert_non_null(type);
1438 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1439 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1440 assert_non_null(((struct lysc_type_dec*)type)->range);
1441 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1442 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1443 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1444 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1445
1446 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1447 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1448 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1449 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1450 assert_non_null(type);
1451 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1452 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1453 assert_non_null(((struct lysc_type_dec*)type)->range);
1454 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1455 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1456 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1457 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1458 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1459 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1460 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1461 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1462
1463 /* invalid cases */
1464 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1465 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1466 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1467 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1468 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1469 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1470
1471 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
1472 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1473 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1474
Radek Krejci643c8242018-11-15 17:51:11 +01001475 assert_non_null(mod = lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
1476 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001477 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001478
Radek Krejci6cba4292018-11-15 17:33:29 +01001479 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
1480 "range '3.142';}}}", LYS_IN_YANG));
1481 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1482 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1483
1484 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
1485 "range '4 | 3.14';}}}", LYS_IN_YANG));
1486 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1487 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1488
Radek Krejci643c8242018-11-15 17:51:11 +01001489 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
1490 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
1491 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1492 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1493
1494 assert_non_null(mod = lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
1495 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1496 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1497 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1498
Radek Krejci6cba4292018-11-15 17:33:29 +01001499 *state = NULL;
1500 ly_ctx_destroy(ctx, NULL);
1501}
1502
Radek Krejci16c0f822018-11-16 10:46:10 +01001503static void
1504test_type_instanceid(void **state)
1505{
1506 *state = test_type_instanceid;
1507
1508 struct ly_ctx *ctx;
1509 struct lys_module *mod;
1510 struct lysc_type *type;
1511
1512 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1513
1514 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1515 "leaf l1 {type instance-identifier {require-instance true;}}"
1516 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
1517 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1518 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1519 assert_non_null(type);
1520 assert_int_equal(LY_TYPE_INST, type->basetype);
1521 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1522
1523 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1524 assert_non_null(type);
1525 assert_int_equal(LY_TYPE_INST, type->basetype);
1526 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1527
1528 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1529 assert_non_null(type);
1530 assert_int_equal(LY_TYPE_INST, type->basetype);
1531 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1532
1533 /* invalid cases */
1534 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1535 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1536
1537 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
1538 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1539 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1540
1541 *state = NULL;
1542 ly_ctx_destroy(ctx, NULL);
1543}
1544
Radek Krejci555cb5b2018-11-16 14:54:33 +01001545static void
1546test_type_identityref(void **state)
1547{
1548 *state = test_type_identityref;
1549
1550 struct ly_ctx *ctx;
1551 struct lys_module *mod;
1552 struct lysc_type *type;
1553
1554 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1555
1556 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;}"
1557 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001558 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001559 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1560 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1561 assert_non_null(type);
1562 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1563 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1564 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1565 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1566
1567 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1568 assert_non_null(type);
1569 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1570 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1571 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1572 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1573 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1574
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001575 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1576 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
1577 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1578 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1579 assert_non_null(type);
1580 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1581 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1582 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1583 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1584 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1585
Radek Krejci555cb5b2018-11-16 14:54:33 +01001586 /* invalid cases */
1587 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
1588 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1589 logbuf_assert("Missing base substatement for identityref type.");
1590
1591 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
1592 "leaf l {type mytype;}}", LYS_IN_YANG));
1593 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1594 logbuf_assert("Missing base substatement for identityref type mytype.");
1595
1596 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
1597 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
1598 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001599 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001600
1601 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
1602 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1603 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001604 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001605
1606 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
1607 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
1608 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1609 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1610
Radek Krejci322614f2018-11-16 15:05:44 +01001611 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
1612 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1613 logbuf_assert("Unable to find base (j) of identityref.");
1614
1615 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
1616 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1617 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1618
Radek Krejci555cb5b2018-11-16 14:54:33 +01001619 *state = NULL;
1620 ly_ctx_destroy(ctx, NULL);
1621}
1622
Radek Krejcia3045382018-11-22 14:30:31 +01001623static void
1624test_type_leafref(void **state)
1625{
1626 *state = test_type_leafref;
1627
1628 struct ly_ctx *ctx;
1629 struct lys_module *mod;
1630 struct lysc_type *type;
1631 const char *path, *name, *prefix;
1632 size_t prefix_len, name_len;
1633 int parent_times, has_predicate;
1634
1635 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1636
1637 /* lys_path_token() */
1638 path = "invalid_path";
1639 parent_times = 0;
1640 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1641 path = "..";
1642 parent_times = 0;
1643 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1644 path = "..[";
1645 parent_times = 0;
1646 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1647 path = "../";
1648 parent_times = 0;
1649 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1650 path = "/";
1651 parent_times = 0;
1652 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1653
1654 path = "../../pref:id/xxx[predicate]/invalid!!!";
1655 parent_times = 0;
1656 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1657 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1658 assert_int_equal(4, prefix_len);
1659 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1660 assert_int_equal(2, name_len);
1661 assert_int_equal(0, strncmp("id", name, name_len));
1662 assert_int_equal(2, parent_times);
1663 assert_int_equal(0, has_predicate);
1664 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1665 assert_string_equal("[predicate]/invalid!!!", path);
1666 assert_int_equal(0, prefix_len);
1667 assert_null(prefix);
1668 assert_int_equal(3, name_len);
1669 assert_int_equal(0, strncmp("xxx", name, name_len));
1670 assert_int_equal(1, has_predicate);
1671 path += 11;
1672 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1673 assert_string_equal("!!!", path);
1674 assert_int_equal(0, prefix_len);
1675 assert_null(prefix);
1676 assert_int_equal(7, name_len);
1677 assert_int_equal(0, strncmp("invalid", name, name_len));
1678
1679 path = "/absolute/prefix:path";
1680 parent_times = 0;
1681 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1682 assert_string_equal("/prefix:path", path);
1683 assert_int_equal(0, prefix_len);
1684 assert_null(prefix);
1685 assert_int_equal(8, name_len);
1686 assert_int_equal(0, strncmp("absolute", name, name_len));
1687 assert_int_equal(-1, parent_times);
1688 assert_int_equal(0, has_predicate);
1689 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1690 assert_int_equal(0, *path);
1691 assert_int_equal(6, prefix_len);
1692 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1693 assert_int_equal(4, name_len);
1694 assert_int_equal(0, strncmp("path", name, name_len));
1695 assert_int_equal(0, has_predicate);
1696
1697 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001698 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 +01001699 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1700 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
1701 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1702 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1703 assert_non_null(type);
1704 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1705 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001706 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001707 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1708 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001709 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1710 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1711 assert_non_null(type);
1712 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1713 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001714 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1715 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1716 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001717 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1718
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001719 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 +01001720 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001721 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001722 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1723 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1724 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001725 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001726 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1727 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1728 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001729 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1730 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001731 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1732
1733 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001734 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 +01001735 "typedef mytype3 {type c:mytype {require-instance false;}}"
1736 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001737 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1738 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1739 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001740 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001741 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1742 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1743 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001744 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1745 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001746 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1747 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1748 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001749 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001750 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1751 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1752 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1753 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1754
Radek Krejci4ce68932018-11-28 12:53:36 +01001755 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1756 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1757 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
1758 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1759 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1760 assert_non_null(type);
1761 assert_int_equal(1, type->refcount);
1762 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1763 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1764 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1765 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1766 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1767 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1768
Radek Krejci58d171e2018-11-23 13:50:55 +01001769 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001770 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 +01001771 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1772 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
1773 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1774 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1775 assert_non_null(type);
1776 assert_int_equal(1, type->refcount);
1777 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1778 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1779 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1780 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1781 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1782
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001783 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1784 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1785 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001786 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001787 LYS_IN_YANG));
1788 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01001789 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001790 assert_non_null(type);
1791 assert_int_equal(1, type->refcount);
1792 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001793 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001794 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1795 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1796 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001797
Radek Krejci20e8a182018-12-07 11:43:21 +01001798 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1799 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1800 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1801 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
1802 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1803 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1804 assert_non_null(type);
1805 assert_int_equal(1, type->refcount);
1806 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1807 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1808 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1809 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1810 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1811
Radek Krejcia3045382018-11-22 14:30:31 +01001812 /* invalid paths */
1813 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
1814 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
1815 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1816 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
1817 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
1818 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
1819 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1820 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
1821 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
1822 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
1823 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1824 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
1825 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf target1 {type string;}container a {leaf target2 {type uint8;}}"
1826 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
1827 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1828 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
1829 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
1830 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
1831 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1832 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
1833 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
1834 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
1835 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1836 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
1837 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
1838 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
1839 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1840 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci2f4a0292018-11-22 15:41:53 +01001841 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
1842 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
1843 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1844 logbuf_assert("Missing path substatement for leafref type.");
1845 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
1846 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
1847 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1848 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001849 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
1850 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
1851 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1852 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1853 "applicable to the leafref itself.");
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001854 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
1855 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
1856 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1857 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 +01001858
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001859 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
1860 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
1861 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1862 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1863 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}"
1864 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
1865 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1866 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1867
Radek Krejcibade82a2018-12-05 10:13:48 +01001868 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
1869 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1870 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1871 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1872 LYS_IN_YANG));
1873 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1874 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1875
1876 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
1877 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1878 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1879 "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}",
1880 LYS_IN_YANG));
1881 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1882 logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination.");
1883
1884 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
1885 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1886 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1887 "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}",
1888 LYS_IN_YANG));
1889 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1890 logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1891
1892 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
1893 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1894 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1895 "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}",
1896 LYS_IN_YANG));
1897 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1898 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1899
1900 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
1901 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1902 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1903 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1904 LYS_IN_YANG));
1905 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1906 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1907
1908 assert_non_null(mod = lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
1909 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1910 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1911 "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}",
1912 LYS_IN_YANG));
1913 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1914 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1915
1916 assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
1917 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1918 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1919 "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}",
1920 LYS_IN_YANG));
1921 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1922 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1923
1924 assert_non_null(mod = lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
1925 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1926 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1927 "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}",
1928 LYS_IN_YANG));
1929 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1930 logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.");
1931
1932 assert_non_null(mod = lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
1933 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1934 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1935 "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}",
1936 LYS_IN_YANG));
1937 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1938 logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1939
1940 assert_non_null(mod = lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
1941 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1942 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1943 "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}",
1944 LYS_IN_YANG));
1945 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1946 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1947
1948 assert_non_null(mod = lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
1949 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1950 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1951 "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}",
1952 LYS_IN_YANG));
1953 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1954 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1955
1956 assert_non_null(mod = lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
1957 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1958 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1959 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1960 LYS_IN_YANG));
1961 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1962 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1963
1964 assert_non_null(mod = lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
1965 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1966 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1967 "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}",
1968 LYS_IN_YANG));
1969 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1970 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1971
1972 assert_non_null(mod = lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
1973 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1974 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
1975 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1976 LYS_IN_YANG));
1977 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1978 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1979
1980 assert_non_null(mod = lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
1981 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1982 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1983 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1984 LYS_IN_YANG));
1985 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1986 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1987
Radek Krejci412ddfa2018-11-23 11:44:11 +01001988 /* circular chain */
1989 assert_non_null(mod = lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
1990 "leaf ref1 {type leafref {path /ref2;}}"
1991 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001992 "leaf ref3 {type leafref {path /ref4;}}"
1993 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001994 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1995 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1996
Radek Krejcia3045382018-11-22 14:30:31 +01001997 *state = NULL;
1998 ly_ctx_destroy(ctx, NULL);
1999}
2000
Radek Krejci43699232018-11-23 14:59:46 +01002001static void
2002test_type_empty(void **state)
2003{
2004 *state = test_type_empty;
2005
2006 struct ly_ctx *ctx;
2007 struct lys_module *mod;
2008
2009 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2010
2011 /* invalid */
2012 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2013 "leaf l {type empty; default x;}}", LYS_IN_YANG));
2014 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2015 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
2016
2017 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
2018 "leaf l {type mytype;}}", LYS_IN_YANG));
2019 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2020 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
2021
2022 *state = NULL;
2023 ly_ctx_destroy(ctx, NULL);
2024}
2025
Radek Krejcicdfecd92018-11-26 11:27:32 +01002026
2027static void
2028test_type_union(void **state)
2029{
2030 *state = test_type_union;
2031
2032 struct ly_ctx *ctx;
2033 struct lys_module *mod;
2034 struct lysc_type *type;
2035
2036 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2037
2038 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2039 "typedef mytype {type union {type int8; type mybasetype;}}"
2040 "leaf l {type mytype;}}", LYS_IN_YANG));
2041 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2042 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2043 assert_non_null(type);
2044 assert_int_equal(2, type->refcount);
2045 assert_int_equal(LY_TYPE_UNION, type->basetype);
2046 assert_non_null(((struct lysc_type_union*)type)->types);
2047 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2048 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2049 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2050
2051 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2052 "typedef mytype {type union {type int8; type mybasetype;}}"
2053 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
2054 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2055 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2056 assert_non_null(type);
2057 assert_int_equal(1, type->refcount);
2058 assert_int_equal(LY_TYPE_UNION, type->basetype);
2059 assert_non_null(((struct lysc_type_union*)type)->types);
2060 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2061 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2062 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2063 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2064
2065 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2066 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002067 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2068 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002069 LYS_IN_YANG));
2070 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2071 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2072 assert_non_null(type);
2073 assert_int_equal(1, type->refcount);
2074 assert_int_equal(LY_TYPE_UNION, type->basetype);
2075 assert_non_null(((struct lysc_type_union*)type)->types);
2076 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2077 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2078 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2079 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2080 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2081 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2082
Radek Krejci6be5ff12018-11-26 13:18:15 +01002083 /* invalid unions */
2084 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
2085 "leaf l {type mytype;}}", LYS_IN_YANG));
2086 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2087 logbuf_assert("Missing type substatement for union type mytype.");
2088
2089 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2090 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2091 logbuf_assert("Missing type substatement for union type.");
2092
2093 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
2094 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
2095 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2096 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2097
2098 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
2099 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
2100 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2101 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2102
Radek Krejcicdfecd92018-11-26 11:27:32 +01002103 *state = NULL;
2104 ly_ctx_destroy(ctx, NULL);
2105}
2106
2107static void
2108test_type_dflt(void **state)
2109{
2110 *state = test_type_union;
2111
2112 struct ly_ctx *ctx;
2113 struct lys_module *mod;
2114 struct lysc_type *type;
2115
2116 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2117
2118 /* default is not inherited from union's types */
2119 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2120 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
2121 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2122 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2123 assert_non_null(type);
2124 assert_int_equal(1, type->refcount);
2125 assert_int_equal(LY_TYPE_UNION, type->basetype);
2126 assert_non_null(((struct lysc_type_union*)type)->types);
2127 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2128 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2129 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2130 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2131 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2132
2133 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2134 "leaf l {type mybasetype;}}", LYS_IN_YANG));
2135 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2136 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2137 assert_non_null(type);
2138 assert_int_equal(2, type->refcount);
2139 assert_int_equal(LY_TYPE_STRING, type->basetype);
2140 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2141 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2142
2143 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2144 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
2145 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2146 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2147 assert_non_null(type);
2148 assert_int_equal(2, type->refcount);
2149 assert_int_equal(LY_TYPE_STRING, type->basetype);
2150 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2151 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2152
Radek Krejci6be5ff12018-11-26 13:18:15 +01002153 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2154 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2155 "leaf l2 {type mytype;}}", LYS_IN_YANG));
2156 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2157 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2158 assert_non_null(type);
2159 assert_int_equal(4, type->refcount);
2160 assert_int_equal(LY_TYPE_STRING, type->basetype);
2161 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2162 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2163 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2164 assert_non_null(type);
2165 assert_int_equal(4, type->refcount);
2166 assert_int_equal(LY_TYPE_STRING, type->basetype);
2167 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2168 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2169
2170 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2171 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
2172 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2173 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2174 assert_non_null(type);
2175 assert_int_equal(3, type->refcount);
2176 assert_int_equal(LY_TYPE_STRING, type->basetype);
2177 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2178 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2179
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002180 /* mandatory leaf does not takes default value from type */
2181 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2182 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
2183 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2184 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2185 assert_non_null(type);
2186 assert_int_equal(LY_TYPE_STRING, type->basetype);
2187 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2188 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2189
Radek Krejcicdfecd92018-11-26 11:27:32 +01002190 *state = NULL;
2191 ly_ctx_destroy(ctx, NULL);
2192}
2193
Radek Krejci665421c2018-12-05 08:03:39 +01002194static void
2195test_status(void **state)
2196{
2197 *state = test_status;
2198
2199 struct ly_ctx *ctx;
2200 struct lys_module *mod;
2201
2202 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2203
2204 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2205 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
2206 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2207 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2208
2209 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
2210 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
2211 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2212 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2213
2214 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
2215 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
2216 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2217 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2218
2219 *state = NULL;
2220 ly_ctx_destroy(ctx, NULL);
2221}
2222
Radek Krejcie86bf772018-12-14 11:39:53 +01002223static void
2224test_uses(void **state)
2225{
2226 *state = test_uses;
2227
2228 struct ly_ctx *ctx;
2229 struct lys_module *mod;
2230 struct lysc_node *parent, *child;
2231
2232 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2233
2234 assert_non_null(mod = lys_parse_mem(ctx, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;}"
2235 "grouping grp {leaf x {type mytype;}}}", LYS_IN_YANG));
2236 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2237
2238
2239 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2240 "grouping grp_a_top {leaf a1 {type int8;}}"
2241 "container a {uses grp_a; uses grp_a_top; uses g:grp; grouping grp_a {leaf a2 {type uint8;}}}}", LYS_IN_YANG));
2242 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2243 assert_non_null((parent = mod->compiled->data));
2244 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2245 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2246 assert_string_equal("a2", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002247 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002248 assert_non_null((child = child->next));
2249 assert_string_equal("a1", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002250 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002251 assert_non_null((child = child->next));
2252 assert_string_equal("x", child->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01002253 assert_ptr_equal(mod, child->module);
Radek Krejcie86bf772018-12-14 11:39:53 +01002254
2255 /* invalid */
2256 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
2257 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2258 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2259
2260 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
2261 "grouping grp {leaf a{type string;}uses grp1;}"
2262 "grouping grp1 {leaf b {type string;}uses grp2;}"
2263 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
2264 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2265 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2266
Radek Krejci76b3e962018-12-14 17:01:25 +01002267 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG));
2268 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2269 logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix).");
2270
2271 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;grouping grp{leaf a{type string;}}"
2272 "leaf a {type string;}uses grp;}", LYS_IN_YANG));
2273 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2274 logbuf_assert("Duplicate identifier \"a\" of data definition statement.");
2275
Radek Krejcie86bf772018-12-14 11:39:53 +01002276 *state = NULL;
2277 ly_ctx_destroy(ctx, NULL);
2278}
2279
2280
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002281int main(void)
2282{
2283 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002284 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2285 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2286 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002287 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2288 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002289 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002290 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2291 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002292 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002293 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002294 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002295 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002296 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002297 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2298 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002299 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002300 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002301 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002302 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002303 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002304 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002305 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002306 };
2307
2308 return cmocka_run_group_tests(tests, NULL, NULL);
2309}