blob: 3a512881f37d3ee10e00a838fa6da8262fecd72e [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 Krejci0f07bed2018-11-13 14:01:40 +0100364/**
365 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
366 * do not test the expression itself
367 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100368static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100369test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100370{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100371 *state = test_type_range;
372
373 struct ly_ctx *ctx;
374 struct lys_module *mod;
375 struct lysc_type *type;
376
377 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
378
379 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));
380 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
381 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
382 assert_non_null(type);
383 assert_int_equal(LY_TYPE_INT8, type->basetype);
384 assert_non_null(((struct lysc_type_num*)type)->range);
385 assert_non_null(((struct lysc_type_num*)type)->range->parts);
386 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
387 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
388 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
389 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
390 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
391
392 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));
393 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
394 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
395 assert_non_null(type);
396 assert_int_equal(LY_TYPE_INT16, type->basetype);
397 assert_non_null(((struct lysc_type_num*)type)->range);
398 assert_non_null(((struct lysc_type_num*)type)->range->parts);
399 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
400 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
401 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
402 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
403 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
404
405 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));
406 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
407 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
408 assert_non_null(type);
409 assert_int_equal(LY_TYPE_INT32, type->basetype);
410 assert_non_null(((struct lysc_type_num*)type)->range);
411 assert_non_null(((struct lysc_type_num*)type)->range->parts);
412 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
413 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
414 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
415 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
416 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
417
418 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));
419 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
420 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
421 assert_non_null(type);
422 assert_int_equal(LY_TYPE_INT64, type->basetype);
423 assert_non_null(((struct lysc_type_num*)type)->range);
424 assert_non_null(((struct lysc_type_num*)type)->range->parts);
425 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
426 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
427 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
428 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
429 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
430
431 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));
432 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
433 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
434 assert_non_null(type);
435 assert_int_equal(LY_TYPE_UINT8, type->basetype);
436 assert_non_null(((struct lysc_type_num*)type)->range);
437 assert_non_null(((struct lysc_type_num*)type)->range->parts);
438 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
439 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
440 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
441 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
442 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
443
444 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));
445 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
446 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
447 assert_non_null(type);
448 assert_int_equal(LY_TYPE_UINT16, type->basetype);
449 assert_non_null(((struct lysc_type_num*)type)->range);
450 assert_non_null(((struct lysc_type_num*)type)->range->parts);
451 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
452 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
453 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
454 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
455 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
456
457 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));
458 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
459 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
460 assert_non_null(type);
461 assert_int_equal(LY_TYPE_UINT32, type->basetype);
462 assert_non_null(((struct lysc_type_num*)type)->range);
463 assert_non_null(((struct lysc_type_num*)type)->range->parts);
464 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
465 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
466 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
467 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
468 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
469
470 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));
471 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
472 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
473 assert_non_null(type);
474 assert_int_equal(LY_TYPE_UINT64, type->basetype);
475 assert_non_null(((struct lysc_type_num*)type)->range);
476 assert_non_null(((struct lysc_type_num*)type)->range->parts);
477 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
478 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
479 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
480 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
481 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
482
483 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
484 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
485 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
486 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
487 assert_non_null(type);
488 assert_int_equal(3, type->refcount);
489 assert_int_equal(LY_TYPE_UINT8, type->basetype);
490 assert_non_null(((struct lysc_type_num*)type)->range);
491 assert_non_null(((struct lysc_type_num*)type)->range->parts);
492 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
493
494 *state = NULL;
495 ly_ctx_destroy(ctx, NULL);
496}
497
498static void
499test_type_length(void **state)
500{
501 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100502
503 struct ly_ctx *ctx;
504 struct lys_module *mod;
505 struct lysc_type *type;
506
507 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
508
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100509 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 +0100510 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
511 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
512 assert_non_null(type);
513 assert_non_null(((struct lysc_type_bin*)type)->length);
514 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100515 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
516 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100517 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
518 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
519 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
520
521 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
522 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
523 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
524 assert_non_null(type);
525 assert_non_null(((struct lysc_type_bin*)type)->length);
526 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
527 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
528 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
529 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
530
531 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));
532 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
533 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
534 assert_non_null(type);
535 assert_non_null(((struct lysc_type_bin*)type)->length);
536 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
537 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
538 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
539 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
540
541 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
542 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
543 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
544 assert_non_null(type);
545 assert_non_null(((struct lysc_type_bin*)type)->length);
546 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
547 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
548 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
549 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
550
551 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));
552 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
553 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
554 assert_non_null(type);
555 assert_non_null(((struct lysc_type_bin*)type)->length);
556 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
557 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
558 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
559 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
560
561 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));
562 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
563 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
564 assert_non_null(type);
565 assert_non_null(((struct lysc_type_bin*)type)->length);
566 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
567 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
568 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
569 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
570 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
571 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
572
573 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));
574 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
575 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
576 assert_non_null(type);
577 assert_non_null(((struct lysc_type_bin*)type)->length);
578 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
579 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
580 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
581 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
582 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
583 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
584
Radek Krejcib31c8992018-11-12 16:29:16 +0100585 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
586 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
587 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
588 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
589 assert_non_null(type);
590 assert_non_null(((struct lysc_type_bin*)type)->length);
591 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
592 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
593 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
594 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
595
596 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
597 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
598 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
599 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
600 assert_non_null(type);
601 assert_non_null(((struct lysc_type_bin*)type)->length);
602 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
603 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
604 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
605 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
606
607 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
608 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
609 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
610 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
611 assert_non_null(type);
612 assert_non_null(((struct lysc_type_bin*)type)->length);
613 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
614 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
615 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
616 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
617 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
618 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
619
620 assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;typedef mytype {type binary {length 10..100;}}"
621 "leaf l {type mytype {length \"10..100\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
622 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
623 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
624 assert_non_null(type);
625 assert_non_null(((struct lysc_type_bin*)type)->length);
626 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
627 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
628 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
629 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
630 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
631 assert_non_null(type);
632 assert_non_null(((struct lysc_type_bin*)type)->length);
633 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
634 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
635 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
636 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
637
Radek Krejci9a191e62018-11-13 13:19:56 +0100638 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type binary {length 10..100;}}"
639 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
640 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
641 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
642 assert_non_null(type);
643 assert_int_equal(LY_TYPE_BINARY, type->basetype);
644 assert_int_equal(3, type->refcount);
645 assert_non_null(((struct lysc_type_bin*)type)->length);
646 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
647 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
648 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
649 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
650 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10..100;}}"
651 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
652 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
653 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
654 assert_non_null(type);
655 assert_int_equal(LY_TYPE_STRING, type->basetype);
656 assert_int_equal(3, type->refcount);
657 assert_non_null(((struct lysc_type_str*)type)->length);
658 assert_non_null(((struct lysc_type_str*)type)->length->parts);
659 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
660 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
661 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
662
Radek Krejci4f28eda2018-11-12 11:46:16 +0100663 /* invalid values */
664 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
665 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
666 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
667 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
668 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
669 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
670 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));
671 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
672 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
673 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));
674 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
675 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
676 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));
677 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
678 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
679 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
680 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
681 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100682 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));
683 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
684 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
685 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
686 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
687 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
688 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
689 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
690 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
691 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
692 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
693 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
694 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
695 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
696 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100697 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));
698 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
699 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100700
701 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
702 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
703 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
704 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
705 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
706 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
707 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
708 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
709 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
710 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
711 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
712 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
713 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
714 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
715 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
716 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
717 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
718 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
719 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
720 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
721 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
722 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
723 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
724 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100725 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
726 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
727 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
728 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +0100729
Radek Krejci495903e2018-11-13 11:30:45 +0100730 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));
731 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
732 logbuf_assert("Invalid type restrictions for binary type.");
733
734 assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;typedef mytype {type string {length 10;}}"
735 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
736 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
737 logbuf_assert("Invalid length restriction - the derived restriction (min..max) is not equally or more limiting.");
Radek Krejci4f28eda2018-11-12 11:46:16 +0100738
739 *state = NULL;
740 ly_ctx_destroy(ctx, NULL);
741}
742
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200743int main(void)
744{
745 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +0100746 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
747 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
748 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +0100749 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
750 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +0100751 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200752 };
753
754 return cmocka_run_group_tests(tests, NULL, NULL);
755}