blob: ce19c07f2fd8feff84a3f6148c8bb98566b855c0 [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 Krejci86d106e2018-10-18 09:53:19 +020015#include "../../src/tree_schema.c"
16#include "../../src/parser_yang.c"
17
Radek Krejcidd4e8d42018-10-16 14:55:43 +020018#include <stdarg.h>
19#include <stddef.h>
20#include <setjmp.h>
21#include <cmocka.h>
22
23#include <stdio.h>
24#include <string.h>
25
26#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020027
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
34#if ENABLE_LOGGER_CHECKING
35static void
36logger(LY_LOG_LEVEL level, const char *msg, const char *path)
37{
38 (void) level; /* unused */
39
Radek Krejci87616bb2018-10-31 13:30:52 +010040 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020041 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
42 } else {
43 strncpy(logbuf, msg, BUFSIZE - 1);
44 }
45}
46#endif
47
48static int
49logger_setup(void **state)
50{
51 (void) state; /* unused */
52#if ENABLE_LOGGER_CHECKING
53 ly_set_log_clb(logger, 1);
54#endif
55 return 0;
56}
57
Radek Krejci4f28eda2018-11-12 11:46:16 +010058static int
59logger_teardown(void **state)
60{
61 (void) state; /* unused */
62#if ENABLE_LOGGER_CHECKING
63 if (*state) {
64 fprintf(stderr, "%s\n", logbuf);
65 }
66#endif
67 return 0;
68}
69
Radek Krejcidd4e8d42018-10-16 14:55:43 +020070void
71logbuf_clean(void)
72{
73 logbuf[0] = '\0';
74}
75
76#if ENABLE_LOGGER_CHECKING
77# define logbuf_assert(str) assert_string_equal(logbuf, str)
78#else
79# define logbuf_assert(str)
80#endif
81
82static void
83test_module(void **state)
84{
85 (void) state; /* unused */
86
87 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +010088 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +020089 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +020090 struct lysc_feature *f;
91 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +020092
93 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +020094 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +010095 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejcidd4e8d42018-10-16 14:55:43 +020096
Radek Krejcif8f882a2018-10-31 14:51:15 +010097 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
98 logbuf_assert("Invalid argument mod (lys_compile()).");
99 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
100 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci313d9902018-11-08 09:42:58 +0100101 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100102 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200103 assert_non_null(mod.compiled);
104 assert_ptr_equal(mod.parsed->name, mod.compiled->name);
105 assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
Radek Krejci151a5b72018-10-19 14:21:44 +0200106 /* features */
107 assert_non_null(mod.compiled->features);
108 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200109 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200110 assert_non_null(f->iffeatures);
111 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200112 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200113 assert_non_null(iff->expr);
114 assert_non_null(iff->features);
115 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200116 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200117
Radek Krejci86d106e2018-10-18 09:53:19 +0200118 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200119
Radek Krejcif8f882a2018-10-31 14:51:15 +0100120 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200121 assert_non_null(mod.compiled);
122 assert_string_equal("test", mod.compiled->name);
123 assert_string_equal("urn:test", mod.compiled->ns);
124
Radek Krejci86d106e2018-10-18 09:53:19 +0200125 lysc_module_free(mod.compiled, NULL);
126 mod.compiled = NULL;
127
Radek Krejci151a5b72018-10-19 14:21:44 +0200128 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200129 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100130 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100131 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
Radek Krejci86d106e2018-10-18 09:53:19 +0200132 logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules.");
133 assert_null(mod.compiled);
134
135 lysp_module_free(mod.parsed);
Radek Krejci313d9902018-11-08 09:42:58 +0100136 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200137}
138
Radek Krejci151a5b72018-10-19 14:21:44 +0200139static void
140test_feature(void **state)
141{
142 (void) state; /* unused */
143
Radek Krejci313d9902018-11-08 09:42:58 +0100144 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100145 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200146 const char *str;
147 struct lysc_feature *f, *f1;
148
149 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
150 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100151 "feature orfeature {if-feature \"f1 or f2\";}\n"
152 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200153 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200154 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100155 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
156 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200157
Radek Krejci313d9902018-11-08 09:42:58 +0100158 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
159 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100160 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200161 assert_non_null(mod.compiled);
162 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100163 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200164 /* all features are disabled by default */
165 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
166 assert_int_equal(0, lysc_feature_value(f));
167 }
168 /* enable f1 */
169 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200170 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200171 assert_int_equal(1, lysc_feature_value(f1));
172
Radek Krejci87616bb2018-10-31 13:30:52 +0100173 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200174 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200175 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100176 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200177 assert_int_equal(1, lysc_feature_value(f));
178
Radek Krejci87616bb2018-10-31 13:30:52 +0100179 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200180 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200181 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100182 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
183 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200184 assert_int_equal(0, lysc_feature_value(f));
185
186 /* first enable f2, so f5 can be enabled then */
187 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100188 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200189 assert_int_equal(1, lysc_feature_value(f));
190
191 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200192 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200193 assert_int_equal(0, lysc_feature_value(f));
194 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
195 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
196 assert_int_equal(0, lysc_feature_value(f));
197
Radek Krejci87616bb2018-10-31 13:30:52 +0100198 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200199 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200200 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
201 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200202 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100203 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200204 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200205 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200206 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200207 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200208
209 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200210 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200211 /* long evaluation of f8 to need to reallocate internal stack for operators */
212 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200213
Radek Krejci87616bb2018-10-31 13:30:52 +0100214 /* double negation of disabled f1 -> disabled */
215 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
216
Radek Krejci38920282018-11-01 09:24:16 +0100217 /* disable all features */
218 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
219 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
220 assert_int_equal(0, lys_feature_value(&mod, f->name));
221 }
222 /* re-setting already set feature */
223 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
224 assert_int_equal(0, lys_feature_value(&mod, "f1"));
225
226 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100227 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
228 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
229 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100230 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
231 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100232 /* test if not changed */
233 assert_int_equal(1, lys_feature_value(&mod, "f1"));
234 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100235
Radek Krejci1aefdf72018-11-01 11:01:39 +0100236 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100237 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
238 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
239
Radek Krejci151a5b72018-10-19 14:21:44 +0200240 lysc_module_free(mod.compiled, NULL);
241 lysp_module_free(mod.parsed);
Radek Krejci87616bb2018-10-31 13:30:52 +0100242
243 /* some invalid expressions */
Radek Krejci313d9902018-11-08 09:42:58 +0100244 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 +0100245 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100246 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
247 lysp_module_free(mod.parsed);
248
Radek Krejci313d9902018-11-08 09:42:58 +0100249 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 +0100250 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100251 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
252 lysp_module_free(mod.parsed);
253
Radek Krejci313d9902018-11-08 09:42:58 +0100254 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 +0100255 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100256 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
257 lysp_module_free(mod.parsed);
258
Radek Krejci313d9902018-11-08 09:42:58 +0100259 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 +0100260 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100261 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
262 lysp_module_free(mod.parsed);
263
Radek Krejci313d9902018-11-08 09:42:58 +0100264 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 +0100265 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100266 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
267 lysp_module_free(mod.parsed);
268
Radek Krejci313d9902018-11-08 09:42:58 +0100269 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 +0100270 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100271 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
272 lysp_module_free(mod.parsed);
273
Radek Krejci313d9902018-11-08 09:42:58 +0100274 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 +0100275 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100276 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
277 lysp_module_free(mod.parsed);
278
Radek Krejci1aefdf72018-11-01 11:01:39 +0100279 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100280 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100281 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
282 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejci313d9902018-11-08 09:42:58 +0100283 assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b{namespace urn:b; prefix b; import a {prefix a;} feature f1; feature f2{if-feature 'a:f1';}}", LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100284 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
285 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
286 assert_int_equal(0, lys_feature_value(modp, "f1"));
287 assert_int_equal(1, lys_feature_value(modp, "f2"));
288
Radek Krejci313d9902018-11-08 09:42:58 +0100289 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200290}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200291
Radek Krejci478020e2018-10-30 16:02:14 +0100292static void
293test_identity(void **state)
294{
295 (void) state; /* unused */
296
297 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100298 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100299 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
300 const char *mod2_str = "module b {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;}}";
301
302 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
303 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
304 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100305 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100306
307 assert_non_null(mod1->compiled);
308 assert_non_null(mod1->compiled->identities);
309 assert_non_null(mod2->compiled);
310 assert_non_null(mod2->compiled->identities);
311
312 assert_non_null(mod1->compiled->identities[0].derived);
313 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
314 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
315 assert_non_null(mod2->compiled->identities[0].derived);
316 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
317 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
318 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
319 assert_non_null(mod2->compiled->identities[1].derived);
320 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
321 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
322 assert_non_null(mod2->compiled->identities[2].derived);
323 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
324 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
325
Radek Krejci478020e2018-10-30 16:02:14 +0100326 ly_ctx_destroy(ctx, NULL);
327}
328
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100329static void
330test_node_container(void **state)
331{
332 (void) state; /* unused */
333
334 struct ly_ctx *ctx;
335 struct lys_module *mod;
336 struct lysc_node_container *cont;
337
338 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
339 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
340 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
341 assert_non_null(mod->compiled);
342 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
343 assert_int_equal(LYS_CONTAINER, cont->nodetype);
344 assert_string_equal("c", cont->name);
345 assert_true(cont->flags & LYS_CONFIG_W);
346 assert_true(cont->flags & LYS_STATUS_CURR);
347
Radek Krejci98094b32018-11-02 16:21:47 +0100348 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 +0100349 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100350 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100351 assert_non_null(mod->compiled);
352 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
353 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100354 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100355 assert_non_null((cont = (struct lysc_node_container*)cont->child));
356 assert_int_equal(LYS_CONTAINER, cont->nodetype);
357 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100358 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100359 assert_string_equal("child", cont->name);
360
361 ly_ctx_destroy(ctx, NULL);
362}
363
Radek Krejci4f28eda2018-11-12 11:46:16 +0100364static void
Radek Krejcib31c8992018-11-12 16:29:16 +0100365test_node_type_length(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100366{
Radek Krejcib31c8992018-11-12 16:29:16 +0100367 *state = test_node_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100368
369 struct ly_ctx *ctx;
370 struct lys_module *mod;
371 struct lysc_type *type;
372
373 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
374
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100375 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 +0100376 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
377 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
378 assert_non_null(type);
379 assert_non_null(((struct lysc_type_bin*)type)->length);
380 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100381 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
382 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100383 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
384 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
385 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
386
387 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
388 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
389 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
390 assert_non_null(type);
391 assert_non_null(((struct lysc_type_bin*)type)->length);
392 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
393 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
394 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
395 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
396
397 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));
398 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
399 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
400 assert_non_null(type);
401 assert_non_null(((struct lysc_type_bin*)type)->length);
402 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
403 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
404 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
405 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
406
407 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
408 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
409 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
410 assert_non_null(type);
411 assert_non_null(((struct lysc_type_bin*)type)->length);
412 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
413 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
414 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
415 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
416
417 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));
418 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
419 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
420 assert_non_null(type);
421 assert_non_null(((struct lysc_type_bin*)type)->length);
422 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
423 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
424 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
425 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
426
427 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));
428 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
429 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
430 assert_non_null(type);
431 assert_non_null(((struct lysc_type_bin*)type)->length);
432 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
433 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
434 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
435 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
436 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
437 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
438
439 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));
440 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
441 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
442 assert_non_null(type);
443 assert_non_null(((struct lysc_type_bin*)type)->length);
444 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
445 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
446 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
447 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
448 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
449 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
450
Radek Krejcib31c8992018-11-12 16:29:16 +0100451 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
452 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
453 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
454 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
455 assert_non_null(type);
456 assert_non_null(((struct lysc_type_bin*)type)->length);
457 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
458 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
459 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
460 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
461
462 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
463 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
464 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
465 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
466 assert_non_null(type);
467 assert_non_null(((struct lysc_type_bin*)type)->length);
468 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
469 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
470 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
471 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
472
473 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
474 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
475 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
476 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
477 assert_non_null(type);
478 assert_non_null(((struct lysc_type_bin*)type)->length);
479 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
480 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
481 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
482 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
483 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
484 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
485
486 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;typedef mytype {type binary {length 10..100;}}"
487 "leaf l {type mytype {length \"10..100\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
488 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
489 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
490 assert_non_null(type);
491 assert_non_null(((struct lysc_type_bin*)type)->length);
492 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
493 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
494 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
495 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
496 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
497 assert_non_null(type);
498 assert_non_null(((struct lysc_type_bin*)type)->length);
499 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
500 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
501 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
502 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
503
Radek Krejci9a191e62018-11-13 13:19:56 +0100504 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type binary {length 10..100;}}"
505 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
506 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
507 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
508 assert_non_null(type);
509 assert_int_equal(LY_TYPE_BINARY, type->basetype);
510 assert_int_equal(3, type->refcount);
511 assert_non_null(((struct lysc_type_bin*)type)->length);
512 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
513 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
514 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
515 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
516 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10..100;}}"
517 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
518 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
519 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
520 assert_non_null(type);
521 assert_int_equal(LY_TYPE_STRING, type->basetype);
522 assert_int_equal(3, type->refcount);
523 assert_non_null(((struct lysc_type_str*)type)->length);
524 assert_non_null(((struct lysc_type_str*)type)->length->parts);
525 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
526 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
527 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
528
Radek Krejci4f28eda2018-11-12 11:46:16 +0100529 /* invalid values */
530 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
531 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
532 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
533 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
534 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
535 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
536 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));
537 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
538 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
539 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));
540 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
541 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
542 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));
543 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
544 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
545 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
546 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
547 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100548 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));
549 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
550 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
551 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
552 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
553 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
554 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
555 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
556 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
557 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
558 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
559 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
560 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
561 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
562 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100563 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));
564 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
565 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100566
567 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
568 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
569 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
570 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
571 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
572 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
573 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
574 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
575 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
576 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
577 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
578 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
579 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
580 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
581 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
582 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
583 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
584 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
585 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
586 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
587 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
588 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
589 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
590 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100591 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
592 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
593 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
594 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +0100595
Radek Krejci495903e2018-11-13 11:30:45 +0100596 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));
597 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
598 logbuf_assert("Invalid type restrictions for binary type.");
599
600 assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;typedef mytype {type string {length 10;}}"
601 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
602 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
603 logbuf_assert("Invalid length restriction - the derived restriction (min..max) is not equally or more limiting.");
Radek Krejci4f28eda2018-11-12 11:46:16 +0100604
605 *state = NULL;
606 ly_ctx_destroy(ctx, NULL);
607}
608
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200609int main(void)
610{
611 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100612 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
613 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
614 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejcib31c8992018-11-12 16:29:16 +0100615 cmocka_unit_test_setup_teardown(test_node_type_length, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +0100616 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200617 };
618
619 return cmocka_run_group_tests(tests, NULL, NULL);
620}