blob: 4acbe4d74cc32e13a487cdaab03dfb0c5009e923 [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);
153
154 lysp_module_free(mod.parsed);
Radek Krejci313d9902018-11-08 09:42:58 +0100155 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200156}
157
Radek Krejci151a5b72018-10-19 14:21:44 +0200158static void
159test_feature(void **state)
160{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100161 *state = test_feature;
Radek Krejci151a5b72018-10-19 14:21:44 +0200162
Radek Krejci313d9902018-11-08 09:42:58 +0100163 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100164 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200165 const char *str;
166 struct lysc_feature *f, *f1;
167
168 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
169 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100170 "feature orfeature {if-feature \"f1 or f2\";}\n"
171 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200172 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200173 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100174 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
175 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200176
Radek Krejci313d9902018-11-08 09:42:58 +0100177 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
178 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100179 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200180 assert_non_null(mod.compiled);
181 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100182 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200183 /* all features are disabled by default */
184 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
185 assert_int_equal(0, lysc_feature_value(f));
186 }
187 /* enable f1 */
188 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200189 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200190 assert_int_equal(1, lysc_feature_value(f1));
191
Radek Krejci87616bb2018-10-31 13:30:52 +0100192 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200193 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200194 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100195 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200196 assert_int_equal(1, lysc_feature_value(f));
197
Radek Krejci87616bb2018-10-31 13:30:52 +0100198 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200199 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200200 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100201 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
202 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200203 assert_int_equal(0, lysc_feature_value(f));
204
205 /* first enable f2, so f5 can be enabled then */
206 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100207 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200208 assert_int_equal(1, lysc_feature_value(f));
209
210 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200211 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200212 assert_int_equal(0, lysc_feature_value(f));
213 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
214 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
215 assert_int_equal(0, lysc_feature_value(f));
216
Radek Krejci87616bb2018-10-31 13:30:52 +0100217 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200218 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200219 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
220 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200221 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100222 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200223 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200224 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200225 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200226 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200227
228 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200229 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200230 /* long evaluation of f8 to need to reallocate internal stack for operators */
231 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200232
Radek Krejci87616bb2018-10-31 13:30:52 +0100233 /* double negation of disabled f1 -> disabled */
234 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
235
Radek Krejci38920282018-11-01 09:24:16 +0100236 /* disable all features */
237 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
238 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
239 assert_int_equal(0, lys_feature_value(&mod, f->name));
240 }
241 /* re-setting already set feature */
242 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
243 assert_int_equal(0, lys_feature_value(&mod, "f1"));
244
245 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100246 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
247 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
248 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100249 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
250 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100251 /* test if not changed */
252 assert_int_equal(1, lys_feature_value(&mod, "f1"));
253 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100254
Radek Krejci1aefdf72018-11-01 11:01:39 +0100255 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100256 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
257 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
258
Radek Krejci151a5b72018-10-19 14:21:44 +0200259 lysc_module_free(mod.compiled, NULL);
260 lysp_module_free(mod.parsed);
Radek Krejci87616bb2018-10-31 13:30:52 +0100261
262 /* some invalid expressions */
Radek Krejci313d9902018-11-08 09:42:58 +0100263 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 +0100264 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100265 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
266 lysp_module_free(mod.parsed);
267
Radek Krejci313d9902018-11-08 09:42:58 +0100268 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 +0100269 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100270 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
271 lysp_module_free(mod.parsed);
272
Radek Krejci313d9902018-11-08 09:42:58 +0100273 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 +0100274 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100275 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
276 lysp_module_free(mod.parsed);
277
Radek Krejci313d9902018-11-08 09:42:58 +0100278 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 +0100279 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100280 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
281 lysp_module_free(mod.parsed);
282
Radek Krejci313d9902018-11-08 09:42:58 +0100283 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 +0100284 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100285 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
286 lysp_module_free(mod.parsed);
287
Radek Krejci313d9902018-11-08 09:42:58 +0100288 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 +0100289 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100290 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
291 lysp_module_free(mod.parsed);
292
Radek Krejci313d9902018-11-08 09:42:58 +0100293 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 +0100294 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100295 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
296 lysp_module_free(mod.parsed);
297
Radek Krejcid05cbd92018-12-05 14:26:40 +0100298 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod.parsed));
299 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
300 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
301 lysp_module_free(mod.parsed);
302
303 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sb {belongs-to b {prefix b;} feature f1;}");
304 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b{namespace urn:b; prefix b; include sb;feature f1;}", LYS_IN_YANG));
305 assert_int_equal(LY_EVALID, lys_compile(modp, 0));
306 logbuf_assert("Duplicate identifier \"f1\" of feature statement.");
307
Radek Krejci1aefdf72018-11-01 11:01:39 +0100308 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100309 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100310 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
311 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejcid05cbd92018-12-05 14:26:40 +0100312 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 +0100313 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
314 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
315 assert_int_equal(0, lys_feature_value(modp, "f1"));
316 assert_int_equal(1, lys_feature_value(modp, "f2"));
317
Radek Krejcid05cbd92018-12-05 14:26:40 +0100318 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100319 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200320}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200321
Radek Krejci478020e2018-10-30 16:02:14 +0100322static void
323test_identity(void **state)
324{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100325 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100326
327 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100328 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100329 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100330 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 +0100331
332 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
333 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
334 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejci7f2a5362018-11-28 13:05:37 +0100335 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100336 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100337
338 assert_non_null(mod1->compiled);
339 assert_non_null(mod1->compiled->identities);
340 assert_non_null(mod2->compiled);
341 assert_non_null(mod2->compiled->identities);
342
343 assert_non_null(mod1->compiled->identities[0].derived);
344 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
345 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
346 assert_non_null(mod2->compiled->identities[0].derived);
347 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
348 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
349 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
350 assert_non_null(mod2->compiled->identities[1].derived);
351 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
352 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
353 assert_non_null(mod2->compiled->identities[2].derived);
354 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
355 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
356
Radek Krejcid05cbd92018-12-05 14:26:40 +0100357 assert_non_null(mod1 = lys_parse_mem(ctx, "module c{namespace urn:c; prefix c; identity i1;identity i1;}", LYS_IN_YANG));
358 assert_int_equal(LY_EVALID, lys_compile(mod1, 0));
359 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
360
361 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sd {belongs-to d {prefix d;} identity i1;}");
362 assert_non_null(mod1 = lys_parse_mem(ctx, "module d{namespace urn:d; prefix d; include sd;identity i1;}", LYS_IN_YANG));
363 assert_int_equal(LY_EVALID, lys_compile(mod1, 0));
364 logbuf_assert("Duplicate identifier \"i1\" of identity statement.");
365
Radek Krejci7f2a5362018-11-28 13:05:37 +0100366 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100367 ly_ctx_destroy(ctx, NULL);
368}
369
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100370static void
371test_node_container(void **state)
372{
373 (void) state; /* unused */
374
375 struct ly_ctx *ctx;
376 struct lys_module *mod;
377 struct lysc_node_container *cont;
378
379 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
380 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
381 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
382 assert_non_null(mod->compiled);
383 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
384 assert_int_equal(LYS_CONTAINER, cont->nodetype);
385 assert_string_equal("c", cont->name);
386 assert_true(cont->flags & LYS_CONFIG_W);
387 assert_true(cont->flags & LYS_STATUS_CURR);
388
Radek Krejci98094b32018-11-02 16:21:47 +0100389 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 +0100390 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100391 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100392 assert_non_null(mod->compiled);
393 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
394 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100395 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100396 assert_non_null((cont = (struct lysc_node_container*)cont->child));
397 assert_int_equal(LYS_CONTAINER, cont->nodetype);
398 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100399 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100400 assert_string_equal("child", cont->name);
401
402 ly_ctx_destroy(ctx, NULL);
403}
404
Radek Krejci0e5d8382018-11-28 16:37:53 +0100405static void
406test_node_leaflist(void **state)
407{
408 *state = test_node_leaflist;
409
410 struct ly_ctx *ctx;
411 struct lys_module *mod;
412 struct lysc_type *type;
413 struct lysc_node_leaflist *ll;
414
415 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
416
417 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
418 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
419 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
420 "leaf-list ll2 {type leafref {path ../target;}}"
421 "leaf target {type int8;}}",
422 LYS_IN_YANG));
423 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
424 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
425 assert_non_null(type);
426 assert_int_equal(1, type->refcount);
427 assert_int_equal(LY_TYPE_UNION, type->basetype);
428 assert_non_null(((struct lysc_type_union*)type)->types);
429 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
430 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
431 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
432 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
433 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
434 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
435 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
436 assert_non_null(type);
437 assert_int_equal(1, type->refcount);
438 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
439 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
440 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
441
Radek Krejci6bb080b2018-11-28 17:23:41 +0100442 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 +0100443 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
444 assert_non_null(mod->compiled);
445 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
446 assert_int_equal(0, ll->min);
447 assert_int_equal((uint32_t)-1, ll->max);
448
Radek Krejci6bb080b2018-11-28 17:23:41 +0100449 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 +0100450 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100451 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100452 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
453 assert_non_null(mod->compiled);
454 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
455 assert_non_null(ll->dflts);
456 assert_int_equal(3, ll->type->refcount);
457 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
458 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100459 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100460 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100461 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
462 assert_non_null(ll->dflts);
463 assert_int_equal(3, ll->type->refcount);
464 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
465 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100466 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
467
468 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
469 * TODO test also, RPC output parameters and notification content */
470 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
471 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
472 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
473 /* but warning is present: */
474 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
475 assert_non_null(mod->compiled);
476 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
477 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100478
479 /* invalid */
480 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
481 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
482 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
483
484 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));
485 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
486 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
487
488 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
489 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
490 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
491 assert_non_null(mod->compiled);
492 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
493 assert_non_null(ll->dflts);
494 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
495 assert_non_null(mod = lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
496 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
497 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
498 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
499
Radek Krejci0e5d8382018-11-28 16:37:53 +0100500 *state = NULL;
501 ly_ctx_destroy(ctx, NULL);
502}
503
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100504static void
505test_node_list(void **state)
506{
507 *state = test_node_list;
508
509 struct ly_ctx *ctx;
510 struct lys_module *mod;
511 struct lysc_node_list *list;
512
513 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
514
515 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
516 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
517 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
518 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
519 list = (struct lysc_node_list*)mod->compiled->data;
520 assert_non_null(list);
521 assert_non_null(list->keys);
522 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
523 assert_string_equal("x", list->keys[0]->name);
524 assert_string_equal("y", list->keys[1]->name);
525 assert_non_null(list->child);
526 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
527 assert_true(list->child->flags & LYS_KEY);
528 assert_true(list->child->next->flags & LYS_KEY);
529 list = (struct lysc_node_list*)mod->compiled->data->next;
530 assert_non_null(list);
531 assert_null(list->keys);
532 assert_non_null(list->child);
533 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
534 assert_false(list->child->flags & LYS_KEY);
535
536 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
537 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100538 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100539 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
540 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
541 list = (struct lysc_node_list*)mod->compiled->data;
542 assert_non_null(list);
543 assert_string_equal("l", list->name);
544 assert_non_null(list->keys);
545 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
546 assert_string_equal("a", list->keys[0]->name);
547 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100548 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100549 assert_non_null(list->uniques);
550 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
551 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
552 assert_string_equal("a", list->uniques[0][0]->name);
553 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
554 assert_string_equal("b", list->uniques[0][1]->name);
555 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
556 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
557 assert_string_equal("e", list->uniques[1][0]->name);
558 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
559 assert_string_equal("d", list->uniques[1][1]->name);
560 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
561
Radek Krejci665421c2018-12-05 08:03:39 +0100562 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
563 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
564 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
565 list = (struct lysc_node_list*)mod->compiled->data;
566 assert_non_null(list);
567 assert_string_equal("l", list->name);
568 assert_non_null(list->keys);
569 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
570 assert_string_equal("a", list->keys[0]->name);
571 assert_true(list->keys[0]->flags & LYS_KEY);
572 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
573
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100574 /* invalid */
575 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
576 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
577 logbuf_assert("Missing key in list representing configuration data.");
578
579 assert_non_null(mod = lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
580 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
581 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
582 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
583
584 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
585 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
586 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
587 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
588
589 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
590 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
591 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
592 logbuf_assert("Key of the configuration list must not be status leaf.");
593
594 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
595 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
596 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
597 logbuf_assert("Configuration node cannot be child of any state data node.");
598
599 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
600 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
601 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
602 logbuf_assert("The list's key \"x\" not found.");
603
604 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
605 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
606 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
607 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
608
609 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
610 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
611 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
612 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
613
Radek Krejci665421c2018-12-05 08:03:39 +0100614 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
615 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
616 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
617 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
618
619 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
620 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
621 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
622 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
623
624 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
625 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
626 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
627 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
628
629 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
630 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
631 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
632 logbuf_assert("Duplicated key identifier \"x\".");
633
634 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
635 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
636 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
637 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
638
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100639 *state = NULL;
640 ly_ctx_destroy(ctx, NULL);
641}
642
Radek Krejci056d0a82018-12-06 16:57:25 +0100643static void
644test_node_choice(void **state)
645{
646 *state = test_node_choice;
647
648 struct ly_ctx *ctx;
649 struct lys_module *mod;
650 struct lysc_node_choice *ch;
651 struct lysc_node_case *cs;
652
653 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
654
655 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100656 "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}"
657 "leaf b {type string;}}}", LYS_IN_YANG));
Radek Krejci056d0a82018-12-06 16:57:25 +0100658 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
659 ch = (struct lysc_node_choice*)mod->compiled->data;
660 assert_non_null(ch);
661 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags);
662 cs = ch->cases;
663 assert_non_null(cs);
664 assert_string_equal("a", cs->name);
665 assert_ptr_equal(ch, cs->parent);
666 assert_non_null(cs->child);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100667 assert_string_equal("a1", cs->child->name);
668 assert_non_null(cs->child->next);
669 assert_string_equal("a2", cs->child->next->name);
Radek Krejci056d0a82018-12-06 16:57:25 +0100670 assert_ptr_equal(cs, cs->child->parent);
671 cs = (struct lysc_node_case*)cs->next;
672 assert_non_null(cs);
673 assert_string_equal("b", cs->name);
674 assert_ptr_equal(ch, cs->parent);
675 assert_non_null(cs->child);
676 assert_string_equal("b", cs->child->name);
677 assert_ptr_equal(cs, cs->child->parent);
Radek Krejci18f2b8a2018-12-12 16:41:21 +0100678 assert_ptr_equal(ch->cases->child->next, cs->child->prev);
679 assert_ptr_equal(ch->cases->child->next->next, cs->child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100680 assert_ptr_equal(ch->cases->child->prev, cs->child);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100681 assert_ptr_equal(ch->dflt, cs);
Radek Krejci056d0a82018-12-06 16:57:25 +0100682
683 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
684 "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG));
685 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
686 logbuf_assert("Duplicate identifier \"x\" of data definition statement.");
687 assert_non_null(mod = lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;"
688 "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
689 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
690 logbuf_assert("Duplicate identifier \"y\" of data definition statement.");
691 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
692 "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG));
693 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
694 logbuf_assert("Duplicate identifier \"a\" of case statement.");
695 assert_non_null(mod = lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;"
696 "choice ch {case b {leaf x {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 \"b\" of case statement.");
699
Radek Krejcia9026eb2018-12-12 16:04:47 +0100700 assert_non_null(mod = lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;"
701 "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
702 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
703 logbuf_assert("Default case \"c\" not found.");
704 assert_non_null(mod = lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}"
705 "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG));
706 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
707 logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\").");
708 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
709 "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG));
710 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
711 logbuf_assert("Mandatory node \"x\" under the default case \"a\".");
712 /* TODO check with mandatory nodes from augment placed into the case */
713
Radek Krejci056d0a82018-12-06 16:57:25 +0100714 *state = NULL;
715 ly_ctx_destroy(ctx, NULL);
716}
717
Radek Krejci9800fb82018-12-13 14:26:23 +0100718static void
719test_node_anydata(void **state)
720{
721 *state = test_node_anydata;
722
723 struct ly_ctx *ctx;
724 struct lys_module *mod;
725 struct lysc_node_anydata *any;
726
727 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
728
729 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
730 "anydata any {config false;mandatory true;}}", LYS_IN_YANG));
731 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
732 any = (struct lysc_node_anydata*)mod->compiled->data;
733 assert_non_null(any);
734 assert_int_equal(LYS_ANYDATA, any->nodetype);
735 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
736
737 logbuf_clean();
738 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
739 "anyxml any;}", LYS_IN_YANG));
740 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
741 any = (struct lysc_node_anydata*)mod->compiled->data;
742 assert_non_null(any);
743 assert_int_equal(LYS_ANYXML, any->nodetype);
744 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags);
745 logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */
746
747 /* invalid */
748 assert_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;anydata any;}", LYS_IN_YANG));
749 logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
750
751 *state = NULL;
752 ly_ctx_destroy(ctx, NULL);
753}
754
Radek Krejci0f07bed2018-11-13 14:01:40 +0100755/**
756 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
757 * do not test the expression itself
758 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100759static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100760test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100761{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100762 *state = test_type_range;
763
764 struct ly_ctx *ctx;
765 struct lys_module *mod;
766 struct lysc_type *type;
767
768 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
769
770 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));
771 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
772 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
773 assert_non_null(type);
774 assert_int_equal(LY_TYPE_INT8, type->basetype);
775 assert_non_null(((struct lysc_type_num*)type)->range);
776 assert_non_null(((struct lysc_type_num*)type)->range->parts);
777 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
778 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
779 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
780 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
781 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
782
783 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));
784 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
785 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
786 assert_non_null(type);
787 assert_int_equal(LY_TYPE_INT16, type->basetype);
788 assert_non_null(((struct lysc_type_num*)type)->range);
789 assert_non_null(((struct lysc_type_num*)type)->range->parts);
790 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
791 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
792 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
793 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
794 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
795
796 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));
797 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
798 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
799 assert_non_null(type);
800 assert_int_equal(LY_TYPE_INT32, type->basetype);
801 assert_non_null(((struct lysc_type_num*)type)->range);
802 assert_non_null(((struct lysc_type_num*)type)->range->parts);
803 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
804 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
805 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
806 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
807 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
808
809 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));
810 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
811 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
812 assert_non_null(type);
813 assert_int_equal(LY_TYPE_INT64, type->basetype);
814 assert_non_null(((struct lysc_type_num*)type)->range);
815 assert_non_null(((struct lysc_type_num*)type)->range->parts);
816 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
817 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
818 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
819 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
820 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
821
822 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));
823 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
824 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
825 assert_non_null(type);
826 assert_int_equal(LY_TYPE_UINT8, type->basetype);
827 assert_non_null(((struct lysc_type_num*)type)->range);
828 assert_non_null(((struct lysc_type_num*)type)->range->parts);
829 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
830 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
831 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
832 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
833 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
834
835 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));
836 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
837 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
838 assert_non_null(type);
839 assert_int_equal(LY_TYPE_UINT16, type->basetype);
840 assert_non_null(((struct lysc_type_num*)type)->range);
841 assert_non_null(((struct lysc_type_num*)type)->range->parts);
842 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
843 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
844 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
845 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
846 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
847
848 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));
849 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
850 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
851 assert_non_null(type);
852 assert_int_equal(LY_TYPE_UINT32, type->basetype);
853 assert_non_null(((struct lysc_type_num*)type)->range);
854 assert_non_null(((struct lysc_type_num*)type)->range->parts);
855 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
856 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
857 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
858 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
859 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
860
861 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));
862 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
863 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
864 assert_non_null(type);
865 assert_int_equal(LY_TYPE_UINT64, type->basetype);
866 assert_non_null(((struct lysc_type_num*)type)->range);
867 assert_non_null(((struct lysc_type_num*)type)->range->parts);
868 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
869 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
870 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
871 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
872 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
873
874 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
875 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
876 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
877 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
878 assert_non_null(type);
879 assert_int_equal(3, type->refcount);
880 assert_int_equal(LY_TYPE_UINT8, type->basetype);
881 assert_non_null(((struct lysc_type_num*)type)->range);
882 assert_non_null(((struct lysc_type_num*)type)->range->parts);
883 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
884
885 *state = NULL;
886 ly_ctx_destroy(ctx, NULL);
887}
888
889static void
890test_type_length(void **state)
891{
892 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100893
894 struct ly_ctx *ctx;
895 struct lys_module *mod;
896 struct lysc_type *type;
897
898 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
899
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100900 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 +0100901 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
902 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
903 assert_non_null(type);
904 assert_non_null(((struct lysc_type_bin*)type)->length);
905 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100906 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
907 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100908 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
909 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
910 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
911
912 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
913 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
914 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
915 assert_non_null(type);
916 assert_non_null(((struct lysc_type_bin*)type)->length);
917 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
918 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100919 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
920 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100921
922 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));
923 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
924 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
925 assert_non_null(type);
926 assert_non_null(((struct lysc_type_bin*)type)->length);
927 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
928 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
929 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100930 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100931
932 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
933 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
934 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
935 assert_non_null(type);
936 assert_non_null(((struct lysc_type_bin*)type)->length);
937 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
938 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
939 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
940 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
941
942 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));
943 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
944 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
945 assert_non_null(type);
946 assert_non_null(((struct lysc_type_bin*)type)->length);
947 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
948 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
949 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
950 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
951
952 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));
953 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
954 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
955 assert_non_null(type);
956 assert_non_null(((struct lysc_type_bin*)type)->length);
957 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
958 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
959 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
960 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
961 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
962 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
963
964 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));
965 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
966 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
967 assert_non_null(type);
968 assert_non_null(((struct lysc_type_bin*)type)->length);
969 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
970 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
971 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
972 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
973 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
974 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
975
Radek Krejcib31c8992018-11-12 16:29:16 +0100976 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
977 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
978 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
979 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
980 assert_non_null(type);
981 assert_non_null(((struct lysc_type_bin*)type)->length);
982 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
983 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
984 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
985 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
986
987 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
988 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
989 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
990 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
991 assert_non_null(type);
992 assert_non_null(((struct lysc_type_bin*)type)->length);
993 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
994 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
995 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
996 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
997
998 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
999 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
1000 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1001 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1002 assert_non_null(type);
1003 assert_non_null(((struct lysc_type_bin*)type)->length);
1004 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1005 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1006 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
1007 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1008 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
1009 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
1010
1011 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 +01001012 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +01001013 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1014 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1015 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001016 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +01001017 assert_non_null(((struct lysc_type_bin*)type)->length);
1018 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
1019 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
1020 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001021 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +01001022 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1023 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001024 assert_int_equal(2, 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);
1029 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
1030
Radek Krejci56aa27c2018-11-13 14:21:59 +01001031 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
1032 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +01001033 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1034 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1035 assert_non_null(type);
1036 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +01001037 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +01001038 assert_non_null(((struct lysc_type_str*)type)->length);
1039 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1040 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1041 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1042 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +01001043
Radek Krejci5969f272018-11-23 10:03:58 +01001044 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
1045 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
1046 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1047 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1048 assert_non_null(type);
1049 assert_int_equal(LY_TYPE_STRING, type->basetype);
1050 assert_int_equal(1, type->refcount);
1051 assert_non_null(((struct lysc_type_str*)type)->length);
1052 assert_non_null(((struct lysc_type_str*)type)->length->parts);
1053 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
1054 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
1055 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
1056
Radek Krejci4f28eda2018-11-12 11:46:16 +01001057 /* invalid values */
1058 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
1059 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1060 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
1061 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
1062 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1063 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
1064 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));
1065 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1066 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
1067 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));
1068 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1069 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
1070 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));
1071 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1072 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
1073 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
1074 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1075 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001076 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));
1077 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1078 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
1079 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
1080 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1081 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
1082 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
1083 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1084 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
1085 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
1086 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1087 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
1088 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
1089 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1090 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +01001091 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));
1092 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1093 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +01001094
1095 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
1096 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
1097 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1098 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
1099 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
1100 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
1101 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1102 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
1103 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
1104 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
1105 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1106 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
1107 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
1108 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
1109 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1110 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
1111 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
1112 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
1113 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1114 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
1115 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
1116 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
1117 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1118 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +01001119 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
1120 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
1121 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1122 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +01001123
Radek Krejci495903e2018-11-13 11:30:45 +01001124 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));
1125 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1126 logbuf_assert("Invalid type restrictions for binary type.");
1127
Radek Krejci4f28eda2018-11-12 11:46:16 +01001128 *state = NULL;
1129 ly_ctx_destroy(ctx, NULL);
1130}
1131
Radek Krejcicda74152018-11-13 15:27:32 +01001132static void
1133test_type_pattern(void **state)
1134{
1135 *state = test_type_pattern;
1136
1137 struct ly_ctx *ctx;
1138 struct lys_module *mod;
1139 struct lysc_type *type;
1140
1141 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1142
Radek Krejci027d5802018-11-14 16:57:28 +01001143 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 +01001144 "pattern .* {error-app-tag errortag;error-message error;}"
1145 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
1146 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1147 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1148 assert_non_null(type);
1149 assert_non_null(((struct lysc_type_str*)type)->patterns);
1150 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1151 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1152 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1153 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1154 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1155 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1156 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1157
1158 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1159 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
1160 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1161 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1162 assert_non_null(type);
1163 assert_int_equal(LY_TYPE_STRING, type->basetype);
1164 assert_int_equal(1, type->refcount);
1165 assert_non_null(((struct lysc_type_str*)type)->patterns);
1166 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1167 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1168 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1169
Radek Krejci04bc1e92018-11-14 14:28:13 +01001170 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1171 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
1172 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1173 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1174 assert_non_null(type);
1175 assert_int_equal(LY_TYPE_STRING, type->basetype);
1176 assert_int_equal(1, type->refcount);
1177 assert_non_null(((struct lysc_type_str*)type)->patterns);
1178 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1179 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1180
Radek Krejcicda74152018-11-13 15:27:32 +01001181 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001182 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 +01001183 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
1184 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1185 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1186 assert_non_null(type);
1187 assert_non_null(((struct lysc_type_str*)type)->patterns);
1188 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1189 /* TODO check some data "^Å™$" */
1190
1191 *state = NULL;
1192 ly_ctx_destroy(ctx, NULL);
1193}
1194
Radek Krejci8b764662018-11-14 14:15:13 +01001195static void
1196test_type_enum(void **state)
1197{
1198 *state = test_type_enum;
1199
1200 struct ly_ctx *ctx;
1201 struct lys_module *mod;
1202 struct lysc_type *type;
1203
1204 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1205
Radek Krejci027d5802018-11-14 16:57:28 +01001206 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 +01001207 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1208 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
1209 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1210 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1211 assert_non_null(type);
1212 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1213 assert_non_null(((struct lysc_type_enum*)type)->enums);
1214 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1215 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1216 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1217 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1218 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1219 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1220 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1221 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1222 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1223 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1224 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1225 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1226 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1227 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1228
Radek Krejci027d5802018-11-14 16:57:28 +01001229 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 {"
1230 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001231 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1232 LYS_IN_YANG));
1233 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1234 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1235 assert_non_null(type);
1236 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1237 assert_non_null(((struct lysc_type_enum*)type)->enums);
1238 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1239 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1240 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1241 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1242 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1243
1244
1245 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001246 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1247 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1248 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 +01001249 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1250 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1251 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1252 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1253 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1254 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1255 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1256 "enum one; enum one;}}}", LYS_IN_YANG));
1257 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1258 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1259 "enum '';}}}", LYS_IN_YANG));
1260 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1261 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1262 "enum ' x';}}}", LYS_IN_YANG));
1263 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1264 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1265 "enum 'x ';}}}", LYS_IN_YANG));
1266 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1267 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1268 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1269 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1270
1271 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
1272 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1273 logbuf_assert("Missing enum substatement for enumeration type.");
1274
Radek Krejci027d5802018-11-14 16:57:28 +01001275 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 +01001276 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
1277 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1278 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1279
Radek Krejci027d5802018-11-14 16:57:28 +01001280 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 +01001281 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
1282 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1283 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
1284 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;}}}",
1285 LYS_IN_YANG));
1286 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1287 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1288
1289 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;}}}}",
1290 LYS_IN_YANG));
1291 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1292 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1293
Radek Krejci04bc1e92018-11-14 14:28:13 +01001294 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
1295 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1296 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001297 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001298
Radek Krejci027d5802018-11-14 16:57:28 +01001299 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
1300 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1301 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1302 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1303
Radek Krejci8b764662018-11-14 14:15:13 +01001304 *state = NULL;
1305 ly_ctx_destroy(ctx, NULL);
1306}
1307
1308static void
1309test_type_bits(void **state)
1310{
1311 *state = test_type_bits;
1312
1313 struct ly_ctx *ctx;
1314 struct lys_module *mod;
1315 struct lysc_type *type;
1316
1317 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1318
Radek Krejci027d5802018-11-14 16:57:28 +01001319 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 +01001320 "bit automin; bit one {if-feature f; position 1;}"
1321 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
1322 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1323 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1324 assert_non_null(type);
1325 assert_int_equal(LY_TYPE_BITS, type->basetype);
1326 assert_non_null(((struct lysc_type_bits*)type)->bits);
1327 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1328 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1329 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1330 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1331 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1332 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1333 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1334 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1335 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1336 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1337 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1338 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1339
Radek Krejci027d5802018-11-14 16:57:28 +01001340 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 {"
1341 "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 +01001342 LYS_IN_YANG));
1343 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1344 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1345 assert_non_null(type);
1346 assert_int_equal(LY_TYPE_BITS, type->basetype);
1347 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001348 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1349 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1350 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1351 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1352 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1353 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1354 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001355
1356
1357 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001358 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1359 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1360 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 +01001361 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1362 "bit one {position -1;}}}}", LYS_IN_YANG));
1363 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1364 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1365 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1366 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1367 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1368 "bit one; bit one;}}}", LYS_IN_YANG));
1369 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1370 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1371 "bit '11';}}}", LYS_IN_YANG));
1372 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1373 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1374 "bit 'x1$1';}}}", LYS_IN_YANG));
1375 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1376
1377 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
1378 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1379 logbuf_assert("Missing bit substatement for bits type.");
1380
Radek Krejci027d5802018-11-14 16:57:28 +01001381 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 +01001382 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
1383 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1384 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1385
Radek Krejci027d5802018-11-14 16:57:28 +01001386 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 +01001387 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
1388 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1389 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
1390 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;}}}",
1391 LYS_IN_YANG));
1392 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1393 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1394
1395 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;}}}}",
1396 LYS_IN_YANG));
1397 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1398 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1399
Radek Krejci04bc1e92018-11-14 14:28:13 +01001400 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1401 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1402 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001403 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001404
Radek Krejci027d5802018-11-14 16:57:28 +01001405 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1406 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1407 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1408 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1409
Radek Krejci8b764662018-11-14 14:15:13 +01001410 *state = NULL;
1411 ly_ctx_destroy(ctx, NULL);
1412}
1413
Radek Krejci6cba4292018-11-15 17:33:29 +01001414static void
1415test_type_dec64(void **state)
1416{
1417 *state = test_type_dec64;
1418
1419 struct ly_ctx *ctx;
1420 struct lys_module *mod;
1421 struct lysc_type *type;
1422
1423 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1424
1425 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1426 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
1427 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1428 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1429 assert_non_null(type);
1430 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1431 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1432 assert_non_null(((struct lysc_type_dec*)type)->range);
1433 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1434 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1435 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1436 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1437
1438 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1439 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1440 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1441 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1442 assert_non_null(type);
1443 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1444 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1445 assert_non_null(((struct lysc_type_dec*)type)->range);
1446 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1447 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1448 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1449 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1450 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1451 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1452 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1453 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1454
1455 /* invalid cases */
1456 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1457 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1458 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1459 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1460 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1461 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1462
1463 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
1464 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1465 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1466
Radek Krejci643c8242018-11-15 17:51:11 +01001467 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));
1468 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001469 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001470
Radek Krejci6cba4292018-11-15 17:33:29 +01001471 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
1472 "range '3.142';}}}", LYS_IN_YANG));
1473 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1474 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1475
1476 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
1477 "range '4 | 3.14';}}}", LYS_IN_YANG));
1478 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1479 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1480
Radek Krejci643c8242018-11-15 17:51:11 +01001481 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
1482 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
1483 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1484 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1485
1486 assert_non_null(mod = lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
1487 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1488 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1489 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1490
Radek Krejci6cba4292018-11-15 17:33:29 +01001491 *state = NULL;
1492 ly_ctx_destroy(ctx, NULL);
1493}
1494
Radek Krejci16c0f822018-11-16 10:46:10 +01001495static void
1496test_type_instanceid(void **state)
1497{
1498 *state = test_type_instanceid;
1499
1500 struct ly_ctx *ctx;
1501 struct lys_module *mod;
1502 struct lysc_type *type;
1503
1504 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1505
1506 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1507 "leaf l1 {type instance-identifier {require-instance true;}}"
1508 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
1509 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1510 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1511 assert_non_null(type);
1512 assert_int_equal(LY_TYPE_INST, type->basetype);
1513 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1514
1515 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1516 assert_non_null(type);
1517 assert_int_equal(LY_TYPE_INST, type->basetype);
1518 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1519
1520 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1521 assert_non_null(type);
1522 assert_int_equal(LY_TYPE_INST, type->basetype);
1523 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1524
1525 /* invalid cases */
1526 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1527 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1528
1529 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));
1530 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1531 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1532
1533 *state = NULL;
1534 ly_ctx_destroy(ctx, NULL);
1535}
1536
Radek Krejci555cb5b2018-11-16 14:54:33 +01001537static void
1538test_type_identityref(void **state)
1539{
1540 *state = test_type_identityref;
1541
1542 struct ly_ctx *ctx;
1543 struct lys_module *mod;
1544 struct lysc_type *type;
1545
1546 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1547
1548 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;}"
1549 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001550 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001551 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1552 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1553 assert_non_null(type);
1554 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1555 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1556 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1557 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1558
1559 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1560 assert_non_null(type);
1561 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1562 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1563 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1564 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1565 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1566
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001567 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1568 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
1569 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1570 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1571 assert_non_null(type);
1572 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1573 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1574 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1575 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1576 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1577
Radek Krejci555cb5b2018-11-16 14:54:33 +01001578 /* invalid cases */
1579 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
1580 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1581 logbuf_assert("Missing base substatement for identityref type.");
1582
1583 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
1584 "leaf l {type mytype;}}", LYS_IN_YANG));
1585 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1586 logbuf_assert("Missing base substatement for identityref type mytype.");
1587
1588 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
1589 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
1590 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001591 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001592
1593 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
1594 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1595 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001596 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001597
1598 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
1599 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
1600 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1601 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1602
Radek Krejci322614f2018-11-16 15:05:44 +01001603 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));
1604 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1605 logbuf_assert("Unable to find base (j) of identityref.");
1606
1607 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));
1608 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1609 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1610
Radek Krejci555cb5b2018-11-16 14:54:33 +01001611 *state = NULL;
1612 ly_ctx_destroy(ctx, NULL);
1613}
1614
Radek Krejcia3045382018-11-22 14:30:31 +01001615static void
1616test_type_leafref(void **state)
1617{
1618 *state = test_type_leafref;
1619
1620 struct ly_ctx *ctx;
1621 struct lys_module *mod;
1622 struct lysc_type *type;
1623 const char *path, *name, *prefix;
1624 size_t prefix_len, name_len;
1625 int parent_times, has_predicate;
1626
1627 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1628
1629 /* lys_path_token() */
1630 path = "invalid_path";
1631 parent_times = 0;
1632 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1633 path = "..";
1634 parent_times = 0;
1635 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1636 path = "..[";
1637 parent_times = 0;
1638 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1639 path = "../";
1640 parent_times = 0;
1641 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1642 path = "/";
1643 parent_times = 0;
1644 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1645
1646 path = "../../pref:id/xxx[predicate]/invalid!!!";
1647 parent_times = 0;
1648 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1649 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1650 assert_int_equal(4, prefix_len);
1651 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1652 assert_int_equal(2, name_len);
1653 assert_int_equal(0, strncmp("id", name, name_len));
1654 assert_int_equal(2, parent_times);
1655 assert_int_equal(0, has_predicate);
1656 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1657 assert_string_equal("[predicate]/invalid!!!", path);
1658 assert_int_equal(0, prefix_len);
1659 assert_null(prefix);
1660 assert_int_equal(3, name_len);
1661 assert_int_equal(0, strncmp("xxx", name, name_len));
1662 assert_int_equal(1, has_predicate);
1663 path += 11;
1664 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1665 assert_string_equal("!!!", path);
1666 assert_int_equal(0, prefix_len);
1667 assert_null(prefix);
1668 assert_int_equal(7, name_len);
1669 assert_int_equal(0, strncmp("invalid", name, name_len));
1670
1671 path = "/absolute/prefix:path";
1672 parent_times = 0;
1673 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1674 assert_string_equal("/prefix:path", path);
1675 assert_int_equal(0, prefix_len);
1676 assert_null(prefix);
1677 assert_int_equal(8, name_len);
1678 assert_int_equal(0, strncmp("absolute", name, name_len));
1679 assert_int_equal(-1, parent_times);
1680 assert_int_equal(0, has_predicate);
1681 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1682 assert_int_equal(0, *path);
1683 assert_int_equal(6, prefix_len);
1684 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1685 assert_int_equal(4, name_len);
1686 assert_int_equal(0, strncmp("path", name, name_len));
1687 assert_int_equal(0, has_predicate);
1688
1689 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001690 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 +01001691 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1692 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
1693 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1694 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1695 assert_non_null(type);
1696 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1697 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001698 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001699 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1700 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001701 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1702 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1703 assert_non_null(type);
1704 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1705 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001706 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1707 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1708 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001709 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1710
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001711 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 +01001712 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001713 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001714 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1715 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1716 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001717 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001718 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1719 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1720 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001721 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1722 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001723 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1724
1725 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001726 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 +01001727 "typedef mytype3 {type c:mytype {require-instance false;}}"
1728 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001729 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1730 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1731 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001732 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001733 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1734 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1735 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001736 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1737 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001738 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1739 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1740 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001741 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001742 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1743 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1744 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1745 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1746
Radek Krejci4ce68932018-11-28 12:53:36 +01001747 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1748 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1749 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
1750 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1751 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1752 assert_non_null(type);
1753 assert_int_equal(1, type->refcount);
1754 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1755 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1756 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1757 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1758 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1759 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1760
Radek Krejci58d171e2018-11-23 13:50:55 +01001761 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001762 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 +01001763 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1764 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
1765 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1766 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1767 assert_non_null(type);
1768 assert_int_equal(1, type->refcount);
1769 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1770 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1771 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1772 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1773 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1774
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001775 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1776 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1777 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
Radek Krejcibade82a2018-12-05 10:13:48 +01001778 "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}",
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001779 LYS_IN_YANG));
1780 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01001781 type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type;
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001782 assert_non_null(type);
1783 assert_int_equal(1, type->refcount);
1784 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
Radek Krejcibade82a2018-12-05 10:13:48 +01001785 assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path);
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001786 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1787 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1788 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001789
Radek Krejci20e8a182018-12-07 11:43:21 +01001790 assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;"
1791 "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}"
1792 "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}"
1793 "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG));
1794 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1795 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1796 assert_non_null(type);
1797 assert_int_equal(1, type->refcount);
1798 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1799 assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path);
1800 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1801 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1802 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
1803
Radek Krejcia3045382018-11-22 14:30:31 +01001804 /* invalid paths */
1805 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
1806 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
1807 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1808 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
1809 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
1810 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
1811 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1812 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
1813 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;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 module connected with the prefix of the node \"/a:invalid\".");
1817 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;}}"
1818 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
1819 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1820 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
1821 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;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 at character 3 (/a!invalid).");
1825 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
1826 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
1827 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1828 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
1829 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
1830 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
1831 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1832 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci2f4a0292018-11-22 15:41:53 +01001833 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
1834 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
1835 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1836 logbuf_assert("Missing path substatement for leafref type.");
1837 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
1838 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
1839 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1840 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001841 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
1842 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
1843 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1844 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1845 "applicable to the leafref itself.");
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001846 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
1847 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
1848 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1849 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 +01001850
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001851 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
1852 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
1853 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1854 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1855 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}"
1856 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
1857 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1858 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1859
Radek Krejcibade82a2018-12-05 10:13:48 +01001860 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;"
1861 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1862 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1863 "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}",
1864 LYS_IN_YANG));
1865 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1866 logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier.");
1867
1868 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;"
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=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=current()/../ifname/ip\" - missing predicate termination.");
1875
1876 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;"
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[x: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 \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\".");
1883
1884 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;"
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[id=current()/../ifname]/ip\";}}}",
1888 LYS_IN_YANG));
1889 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1890 logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found.");
1891
1892 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;"
1893 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1894 "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}"
1895 "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}",
1896 LYS_IN_YANG));
1897 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1898 logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\".");
1899
1900 assert_non_null(mod = lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;"
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 = ../ifname]/ip\";}}}",
1904 LYS_IN_YANG));
1905 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1906 logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation.");
1907
1908 assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;"
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 = current()../ifname]/ip\";}}}",
1912 LYS_IN_YANG));
1913 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1914 logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation.");
1915
1916 assert_non_null(mod = lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;"
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 \"/\" in \"../\" rel-path-keyexpr pattern.");
1923
1924 assert_non_null(mod = lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;"
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]\" - at least one \"..\" is expected in rel-path-keyexpr.");
1931
1932 assert_non_null(mod = lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;"
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()/../]/ip\";}}}",
1936 LYS_IN_YANG));
1937 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1938 logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr.");
1939
1940 assert_non_null(mod = lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;"
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()/../$node]/ip\";}}}",
1944 LYS_IN_YANG));
1945 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1946 logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node]).");
1947
1948 assert_non_null(mod = lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;"
1949 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1950 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1951 "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}",
1952 LYS_IN_YANG));
1953 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1954 logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr.");
1955
1956 assert_non_null(mod = lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;"
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()/../xxx]/ip\";}}}",
1960 LYS_IN_YANG));
1961 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1962 logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr.");
1963
1964 assert_non_null(mod = lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;"
1965 "list interface{key name;leaf name{type string;}leaf ip {type string;}}"
1966 "leaf ifname{type leafref{ path \"../interface/name\";}}container c;"
1967 "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}",
1968 LYS_IN_YANG));
1969 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1970 logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf.");
1971
1972 assert_non_null(mod = lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;"
1973 "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}"
1974 "leaf ifname{type leafref{ path \"../interface/name\";}}"
1975 "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}",
1976 LYS_IN_YANG));
1977 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1978 logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found.");
1979
Radek Krejci412ddfa2018-11-23 11:44:11 +01001980 /* circular chain */
1981 assert_non_null(mod = lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
1982 "leaf ref1 {type leafref {path /ref2;}}"
1983 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001984 "leaf ref3 {type leafref {path /ref4;}}"
1985 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001986 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1987 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1988
Radek Krejcia3045382018-11-22 14:30:31 +01001989 *state = NULL;
1990 ly_ctx_destroy(ctx, NULL);
1991}
1992
Radek Krejci43699232018-11-23 14:59:46 +01001993static void
1994test_type_empty(void **state)
1995{
1996 *state = test_type_empty;
1997
1998 struct ly_ctx *ctx;
1999 struct lys_module *mod;
2000
2001 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2002
2003 /* invalid */
2004 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2005 "leaf l {type empty; default x;}}", LYS_IN_YANG));
2006 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2007 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
2008
2009 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
2010 "leaf l {type mytype;}}", LYS_IN_YANG));
2011 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2012 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
2013
2014 *state = NULL;
2015 ly_ctx_destroy(ctx, NULL);
2016}
2017
Radek Krejcicdfecd92018-11-26 11:27:32 +01002018
2019static void
2020test_type_union(void **state)
2021{
2022 *state = test_type_union;
2023
2024 struct ly_ctx *ctx;
2025 struct lys_module *mod;
2026 struct lysc_type *type;
2027
2028 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2029
2030 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
2031 "typedef mytype {type union {type int8; type mybasetype;}}"
2032 "leaf l {type mytype;}}", LYS_IN_YANG));
2033 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2034 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2035 assert_non_null(type);
2036 assert_int_equal(2, type->refcount);
2037 assert_int_equal(LY_TYPE_UNION, type->basetype);
2038 assert_non_null(((struct lysc_type_union*)type)->types);
2039 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2040 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
2041 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2042
2043 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
2044 "typedef mytype {type union {type int8; type mybasetype;}}"
2045 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
2046 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2047 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2048 assert_non_null(type);
2049 assert_int_equal(1, type->refcount);
2050 assert_int_equal(LY_TYPE_UNION, type->basetype);
2051 assert_non_null(((struct lysc_type_union*)type)->types);
2052 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2053 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2054 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
2055 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2056
2057 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
2058 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01002059 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
2060 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01002061 LYS_IN_YANG));
2062 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2063 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2064 assert_non_null(type);
2065 assert_int_equal(1, type->refcount);
2066 assert_int_equal(LY_TYPE_UNION, type->basetype);
2067 assert_non_null(((struct lysc_type_union*)type)->types);
2068 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2069 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2070 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
2071 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
2072 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
2073 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
2074
Radek Krejci6be5ff12018-11-26 13:18:15 +01002075 /* invalid unions */
2076 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
2077 "leaf l {type mytype;}}", LYS_IN_YANG));
2078 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2079 logbuf_assert("Missing type substatement for union type mytype.");
2080
2081 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
2082 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2083 logbuf_assert("Missing type substatement for union type.");
2084
2085 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
2086 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
2087 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2088 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
2089
2090 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
2091 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
2092 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2093 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
2094
Radek Krejcicdfecd92018-11-26 11:27:32 +01002095 *state = NULL;
2096 ly_ctx_destroy(ctx, NULL);
2097}
2098
2099static void
2100test_type_dflt(void **state)
2101{
2102 *state = test_type_union;
2103
2104 struct ly_ctx *ctx;
2105 struct lys_module *mod;
2106 struct lysc_type *type;
2107
2108 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2109
2110 /* default is not inherited from union's types */
2111 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
2112 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
2113 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2114 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2115 assert_non_null(type);
2116 assert_int_equal(1, type->refcount);
2117 assert_int_equal(LY_TYPE_UNION, type->basetype);
2118 assert_non_null(((struct lysc_type_union*)type)->types);
2119 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
2120 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
2121 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
2122 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2123 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
2124
2125 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
2126 "leaf l {type mybasetype;}}", LYS_IN_YANG));
2127 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2128 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2129 assert_non_null(type);
2130 assert_int_equal(2, type->refcount);
2131 assert_int_equal(LY_TYPE_STRING, type->basetype);
2132 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2133 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2134
2135 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
2136 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
2137 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2138 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2139 assert_non_null(type);
2140 assert_int_equal(2, type->refcount);
2141 assert_int_equal(LY_TYPE_STRING, type->basetype);
2142 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2143 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2144
Radek Krejci6be5ff12018-11-26 13:18:15 +01002145 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
2146 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
2147 "leaf l2 {type mytype;}}", LYS_IN_YANG));
2148 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2149 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2150 assert_non_null(type);
2151 assert_int_equal(4, type->refcount);
2152 assert_int_equal(LY_TYPE_STRING, type->basetype);
2153 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2154 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2155 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
2156 assert_non_null(type);
2157 assert_int_equal(4, type->refcount);
2158 assert_int_equal(LY_TYPE_STRING, type->basetype);
2159 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
2160 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
2161
2162 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
2163 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
2164 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2165 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2166 assert_non_null(type);
2167 assert_int_equal(3, type->refcount);
2168 assert_int_equal(LY_TYPE_STRING, type->basetype);
2169 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2170 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2171
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002172 /* mandatory leaf does not takes default value from type */
2173 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
2174 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
2175 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2176 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
2177 assert_non_null(type);
2178 assert_int_equal(LY_TYPE_STRING, type->basetype);
2179 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
2180 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
2181
Radek Krejcicdfecd92018-11-26 11:27:32 +01002182 *state = NULL;
2183 ly_ctx_destroy(ctx, NULL);
2184}
2185
Radek Krejci665421c2018-12-05 08:03:39 +01002186static void
2187test_status(void **state)
2188{
2189 *state = test_status;
2190
2191 struct ly_ctx *ctx;
2192 struct lys_module *mod;
2193
2194 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2195
2196 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
2197 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
2198 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2199 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
2200
2201 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
2202 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
2203 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2204 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
2205
2206 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
2207 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
2208 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2209 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2210
2211 *state = NULL;
2212 ly_ctx_destroy(ctx, NULL);
2213}
2214
Radek Krejcie86bf772018-12-14 11:39:53 +01002215static void
2216test_uses(void **state)
2217{
2218 *state = test_uses;
2219
2220 struct ly_ctx *ctx;
2221 struct lys_module *mod;
2222 struct lysc_node *parent, *child;
2223
2224 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
2225
2226 assert_non_null(mod = lys_parse_mem(ctx, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;}"
2227 "grouping grp {leaf x {type mytype;}}}", LYS_IN_YANG));
2228 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2229
2230
2231 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}"
2232 "grouping grp_a_top {leaf a1 {type int8;}}"
2233 "container a {uses grp_a; uses grp_a_top; uses g:grp; grouping grp_a {leaf a2 {type uint8;}}}}", LYS_IN_YANG));
2234 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
2235 assert_non_null((parent = mod->compiled->data));
2236 assert_int_equal(LYS_CONTAINER, parent->nodetype);
2237 assert_non_null((child = ((struct lysc_node_container*)parent)->child));
2238 assert_string_equal("a2", child->name);
2239 assert_non_null((child = child->next));
2240 assert_string_equal("a1", child->name);
2241 assert_non_null((child = child->next));
2242 assert_string_equal("x", child->name);
2243
2244 /* invalid */
2245 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG));
2246 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2247 logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found.");
2248
2249 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;"
2250 "grouping grp {leaf a{type string;}uses grp1;}"
2251 "grouping grp1 {leaf b {type string;}uses grp2;}"
2252 "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG));
2253 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
2254 logbuf_assert("Grouping \"grp\" references itself through a uses statement.");
2255
2256 *state = NULL;
2257 ly_ctx_destroy(ctx, NULL);
2258}
2259
2260
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002261int main(void)
2262{
2263 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01002264 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
2265 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
2266 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01002267 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
2268 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01002269 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01002270 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
2271 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01002272 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01002273 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01002274 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01002275 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01002276 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01002277 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
2278 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01002279 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01002280 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002281 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002282 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002283 cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002284 cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002285 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02002286 };
2287
2288 return cmocka_run_group_tests(tests, NULL, NULL);
2289}