blob: a0d5cd1c5a2f07780209befe5a8fa88f1ea12162 [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"
21#include "../../src/tree_schema.c"
22#include "../../src/context.c"
23#include "../../src/hash_table.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020024
Radek Krejcidd4e8d42018-10-16 14:55:43 +020025#include <stdarg.h>
26#include <stddef.h>
27#include <setjmp.h>
28#include <cmocka.h>
29
30#include <stdio.h>
31#include <string.h>
32
33#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020034
35#define BUFSIZE 1024
36char logbuf[BUFSIZE] = {0};
37
38/* set to 0 to printing error messages to stderr instead of checking them in code */
39#define ENABLE_LOGGER_CHECKING 1
40
41#if ENABLE_LOGGER_CHECKING
42static void
43logger(LY_LOG_LEVEL level, const char *msg, const char *path)
44{
45 (void) level; /* unused */
46
Radek Krejci87616bb2018-10-31 13:30:52 +010047 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020048 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
49 } else {
50 strncpy(logbuf, msg, BUFSIZE - 1);
51 }
52}
53#endif
54
55static int
56logger_setup(void **state)
57{
58 (void) state; /* unused */
59#if ENABLE_LOGGER_CHECKING
60 ly_set_log_clb(logger, 1);
61#endif
62 return 0;
63}
64
Radek Krejci4f28eda2018-11-12 11:46:16 +010065static int
66logger_teardown(void **state)
67{
68 (void) state; /* unused */
69#if ENABLE_LOGGER_CHECKING
70 if (*state) {
71 fprintf(stderr, "%s\n", logbuf);
72 }
73#endif
74 return 0;
75}
76
Radek Krejcidd4e8d42018-10-16 14:55:43 +020077void
78logbuf_clean(void)
79{
80 logbuf[0] = '\0';
81}
82
83#if ENABLE_LOGGER_CHECKING
84# define logbuf_assert(str) assert_string_equal(logbuf, str)
85#else
86# define logbuf_assert(str)
87#endif
88
89static void
90test_module(void **state)
91{
92 (void) state; /* unused */
93
94 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +010095 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +020096 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +020097 struct lysc_feature *f;
98 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +020099
100 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200101 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100102 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200103
Radek Krejcif8f882a2018-10-31 14:51:15 +0100104 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
105 logbuf_assert("Invalid argument mod (lys_compile()).");
106 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
107 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci313d9902018-11-08 09:42:58 +0100108 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100109 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200110 assert_non_null(mod.compiled);
111 assert_ptr_equal(mod.parsed->name, mod.compiled->name);
112 assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
Radek Krejci151a5b72018-10-19 14:21:44 +0200113 /* features */
114 assert_non_null(mod.compiled->features);
115 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200116 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200117 assert_non_null(f->iffeatures);
118 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200119 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200120 assert_non_null(iff->expr);
121 assert_non_null(iff->features);
122 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200123 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200124
Radek Krejci86d106e2018-10-18 09:53:19 +0200125 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200126
Radek Krejcif8f882a2018-10-31 14:51:15 +0100127 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200128 assert_non_null(mod.compiled);
129 assert_string_equal("test", mod.compiled->name);
130 assert_string_equal("urn:test", mod.compiled->ns);
131
Radek Krejci86d106e2018-10-18 09:53:19 +0200132 lysc_module_free(mod.compiled, NULL);
133 mod.compiled = NULL;
134
Radek Krejci151a5b72018-10-19 14:21:44 +0200135 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200136 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100137 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100138 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
Radek Krejci86d106e2018-10-18 09:53:19 +0200139 logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules.");
140 assert_null(mod.compiled);
141
142 lysp_module_free(mod.parsed);
Radek Krejci313d9902018-11-08 09:42:58 +0100143 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200144}
145
Radek Krejci151a5b72018-10-19 14:21:44 +0200146static void
147test_feature(void **state)
148{
149 (void) state; /* unused */
150
Radek Krejci313d9902018-11-08 09:42:58 +0100151 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100152 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200153 const char *str;
154 struct lysc_feature *f, *f1;
155
156 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
157 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100158 "feature orfeature {if-feature \"f1 or f2\";}\n"
159 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200160 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200161 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100162 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
163 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200164
Radek Krejci313d9902018-11-08 09:42:58 +0100165 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
166 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100167 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200168 assert_non_null(mod.compiled);
169 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100170 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200171 /* all features are disabled by default */
172 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
173 assert_int_equal(0, lysc_feature_value(f));
174 }
175 /* enable f1 */
176 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200177 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200178 assert_int_equal(1, lysc_feature_value(f1));
179
Radek Krejci87616bb2018-10-31 13:30:52 +0100180 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200181 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200182 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100183 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200184 assert_int_equal(1, lysc_feature_value(f));
185
Radek Krejci87616bb2018-10-31 13:30:52 +0100186 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200187 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200188 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100189 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
190 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200191 assert_int_equal(0, lysc_feature_value(f));
192
193 /* first enable f2, so f5 can be enabled then */
194 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100195 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200196 assert_int_equal(1, lysc_feature_value(f));
197
198 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200199 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200200 assert_int_equal(0, lysc_feature_value(f));
201 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
202 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
203 assert_int_equal(0, lysc_feature_value(f));
204
Radek Krejci87616bb2018-10-31 13:30:52 +0100205 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200206 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200207 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
208 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200209 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100210 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200211 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200212 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200213 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200214 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200215
216 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200217 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200218 /* long evaluation of f8 to need to reallocate internal stack for operators */
219 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200220
Radek Krejci87616bb2018-10-31 13:30:52 +0100221 /* double negation of disabled f1 -> disabled */
222 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
223
Radek Krejci38920282018-11-01 09:24:16 +0100224 /* disable all features */
225 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
226 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
227 assert_int_equal(0, lys_feature_value(&mod, f->name));
228 }
229 /* re-setting already set feature */
230 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
231 assert_int_equal(0, lys_feature_value(&mod, "f1"));
232
233 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100234 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
235 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
236 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100237 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
238 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100239 /* test if not changed */
240 assert_int_equal(1, lys_feature_value(&mod, "f1"));
241 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100242
Radek Krejci1aefdf72018-11-01 11:01:39 +0100243 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100244 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
245 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
246
Radek Krejci151a5b72018-10-19 14:21:44 +0200247 lysc_module_free(mod.compiled, NULL);
248 lysp_module_free(mod.parsed);
Radek Krejci87616bb2018-10-31 13:30:52 +0100249
250 /* some invalid expressions */
Radek Krejci313d9902018-11-08 09:42:58 +0100251 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 +0100252 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100253 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
254 lysp_module_free(mod.parsed);
255
Radek Krejci313d9902018-11-08 09:42:58 +0100256 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 +0100257 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100258 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
259 lysp_module_free(mod.parsed);
260
Radek Krejci313d9902018-11-08 09:42:58 +0100261 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 +0100262 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100263 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
264 lysp_module_free(mod.parsed);
265
Radek Krejci313d9902018-11-08 09:42:58 +0100266 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 +0100267 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100268 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
269 lysp_module_free(mod.parsed);
270
Radek Krejci313d9902018-11-08 09:42:58 +0100271 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100272 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100273 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
274 lysp_module_free(mod.parsed);
275
Radek Krejci313d9902018-11-08 09:42:58 +0100276 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100277 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100278 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
279 lysp_module_free(mod.parsed);
280
Radek Krejci313d9902018-11-08 09:42:58 +0100281 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100282 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100283 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
284 lysp_module_free(mod.parsed);
285
Radek Krejci1aefdf72018-11-01 11:01:39 +0100286 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100287 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100288 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
289 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejci313d9902018-11-08 09:42:58 +0100290 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 +0100291 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
292 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
293 assert_int_equal(0, lys_feature_value(modp, "f1"));
294 assert_int_equal(1, lys_feature_value(modp, "f2"));
295
Radek Krejci313d9902018-11-08 09:42:58 +0100296 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200297}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200298
Radek Krejci478020e2018-10-30 16:02:14 +0100299static void
300test_identity(void **state)
301{
302 (void) state; /* unused */
303
304 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100305 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100306 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100307 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 +0100308
309 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
310 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
311 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100312 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100313
314 assert_non_null(mod1->compiled);
315 assert_non_null(mod1->compiled->identities);
316 assert_non_null(mod2->compiled);
317 assert_non_null(mod2->compiled->identities);
318
319 assert_non_null(mod1->compiled->identities[0].derived);
320 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
321 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
322 assert_non_null(mod2->compiled->identities[0].derived);
323 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
324 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
325 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
326 assert_non_null(mod2->compiled->identities[1].derived);
327 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
328 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
329 assert_non_null(mod2->compiled->identities[2].derived);
330 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
331 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
332
Radek Krejci478020e2018-10-30 16:02:14 +0100333 ly_ctx_destroy(ctx, NULL);
334}
335
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100336static void
337test_node_container(void **state)
338{
339 (void) state; /* unused */
340
341 struct ly_ctx *ctx;
342 struct lys_module *mod;
343 struct lysc_node_container *cont;
344
345 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
346 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
347 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
348 assert_non_null(mod->compiled);
349 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
350 assert_int_equal(LYS_CONTAINER, cont->nodetype);
351 assert_string_equal("c", cont->name);
352 assert_true(cont->flags & LYS_CONFIG_W);
353 assert_true(cont->flags & LYS_STATUS_CURR);
354
Radek Krejci98094b32018-11-02 16:21:47 +0100355 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 +0100356 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100357 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100358 assert_non_null(mod->compiled);
359 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
360 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100361 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100362 assert_non_null((cont = (struct lysc_node_container*)cont->child));
363 assert_int_equal(LYS_CONTAINER, cont->nodetype);
364 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100365 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100366 assert_string_equal("child", cont->name);
367
368 ly_ctx_destroy(ctx, NULL);
369}
370
Radek Krejci0f07bed2018-11-13 14:01:40 +0100371/**
372 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
373 * do not test the expression itself
374 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100375static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100376test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100377{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100378 *state = test_type_range;
379
380 struct ly_ctx *ctx;
381 struct lys_module *mod;
382 struct lysc_type *type;
383
384 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
385
386 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));
387 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
388 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
389 assert_non_null(type);
390 assert_int_equal(LY_TYPE_INT8, type->basetype);
391 assert_non_null(((struct lysc_type_num*)type)->range);
392 assert_non_null(((struct lysc_type_num*)type)->range->parts);
393 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
394 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
395 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
396 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
397 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
398
399 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));
400 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
401 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
402 assert_non_null(type);
403 assert_int_equal(LY_TYPE_INT16, type->basetype);
404 assert_non_null(((struct lysc_type_num*)type)->range);
405 assert_non_null(((struct lysc_type_num*)type)->range->parts);
406 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
407 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
408 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
409 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
410 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
411
412 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));
413 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
414 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
415 assert_non_null(type);
416 assert_int_equal(LY_TYPE_INT32, type->basetype);
417 assert_non_null(((struct lysc_type_num*)type)->range);
418 assert_non_null(((struct lysc_type_num*)type)->range->parts);
419 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
420 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
421 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
422 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
423 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
424
425 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));
426 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
427 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
428 assert_non_null(type);
429 assert_int_equal(LY_TYPE_INT64, type->basetype);
430 assert_non_null(((struct lysc_type_num*)type)->range);
431 assert_non_null(((struct lysc_type_num*)type)->range->parts);
432 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
433 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
434 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
435 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
436 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
437
438 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));
439 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
440 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
441 assert_non_null(type);
442 assert_int_equal(LY_TYPE_UINT8, type->basetype);
443 assert_non_null(((struct lysc_type_num*)type)->range);
444 assert_non_null(((struct lysc_type_num*)type)->range->parts);
445 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
446 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
447 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
448 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
449 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
450
451 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));
452 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
453 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
454 assert_non_null(type);
455 assert_int_equal(LY_TYPE_UINT16, type->basetype);
456 assert_non_null(((struct lysc_type_num*)type)->range);
457 assert_non_null(((struct lysc_type_num*)type)->range->parts);
458 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
459 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
460 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
461 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
462 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
463
464 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));
465 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
466 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
467 assert_non_null(type);
468 assert_int_equal(LY_TYPE_UINT32, type->basetype);
469 assert_non_null(((struct lysc_type_num*)type)->range);
470 assert_non_null(((struct lysc_type_num*)type)->range->parts);
471 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
472 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
473 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
474 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
475 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
476
477 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));
478 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
479 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
480 assert_non_null(type);
481 assert_int_equal(LY_TYPE_UINT64, type->basetype);
482 assert_non_null(((struct lysc_type_num*)type)->range);
483 assert_non_null(((struct lysc_type_num*)type)->range->parts);
484 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
485 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
486 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
487 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
488 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
489
490 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
491 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
492 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
493 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
494 assert_non_null(type);
495 assert_int_equal(3, type->refcount);
496 assert_int_equal(LY_TYPE_UINT8, type->basetype);
497 assert_non_null(((struct lysc_type_num*)type)->range);
498 assert_non_null(((struct lysc_type_num*)type)->range->parts);
499 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
500
501 *state = NULL;
502 ly_ctx_destroy(ctx, NULL);
503}
504
505static void
506test_type_length(void **state)
507{
508 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100509
510 struct ly_ctx *ctx;
511 struct lys_module *mod;
512 struct lysc_type *type;
513
514 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
515
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100516 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 +0100517 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
518 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
519 assert_non_null(type);
520 assert_non_null(((struct lysc_type_bin*)type)->length);
521 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100522 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
523 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100524 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
525 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
526 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
527
528 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
529 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
530 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
531 assert_non_null(type);
532 assert_non_null(((struct lysc_type_bin*)type)->length);
533 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
534 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
535 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
536 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
537
538 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));
539 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
540 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
541 assert_non_null(type);
542 assert_non_null(((struct lysc_type_bin*)type)->length);
543 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
544 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
545 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
546 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
547
548 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
549 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
550 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
551 assert_non_null(type);
552 assert_non_null(((struct lysc_type_bin*)type)->length);
553 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
554 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
555 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
556 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
557
558 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));
559 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
560 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
561 assert_non_null(type);
562 assert_non_null(((struct lysc_type_bin*)type)->length);
563 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
564 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
565 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
566 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
567
568 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));
569 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
570 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
571 assert_non_null(type);
572 assert_non_null(((struct lysc_type_bin*)type)->length);
573 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
574 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
575 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
576 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
577 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
578 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
579
580 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));
581 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
582 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
583 assert_non_null(type);
584 assert_non_null(((struct lysc_type_bin*)type)->length);
585 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
586 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
587 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
588 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
589 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
590 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
591
Radek Krejcib31c8992018-11-12 16:29:16 +0100592 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
593 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
594 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
595 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
596 assert_non_null(type);
597 assert_non_null(((struct lysc_type_bin*)type)->length);
598 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
599 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
600 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
601 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
602
603 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
604 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
605 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
606 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
607 assert_non_null(type);
608 assert_non_null(((struct lysc_type_bin*)type)->length);
609 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
610 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
611 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
612 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
613
614 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
615 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
616 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
617 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
618 assert_non_null(type);
619 assert_non_null(((struct lysc_type_bin*)type)->length);
620 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
621 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
622 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
623 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
624 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
625 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
626
627 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 +0100628 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100629 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
630 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
631 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100632 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100633 assert_non_null(((struct lysc_type_bin*)type)->length);
634 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
635 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
636 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100637 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +0100638 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
639 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100640 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100641 assert_non_null(((struct lysc_type_bin*)type)->length);
642 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
643 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
644 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
645 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
646
Radek Krejci56aa27c2018-11-13 14:21:59 +0100647 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
648 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +0100649 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
650 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
651 assert_non_null(type);
652 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100653 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +0100654 assert_non_null(((struct lysc_type_str*)type)->length);
655 assert_non_null(((struct lysc_type_str*)type)->length->parts);
656 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
657 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
658 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +0100659
Radek Krejci4f28eda2018-11-12 11:46:16 +0100660 /* invalid values */
661 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
662 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
663 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
664 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
665 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
666 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
667 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));
668 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
669 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
670 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));
671 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
672 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
673 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));
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 ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
677 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
678 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100679 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));
680 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
681 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
682 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
683 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
684 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
685 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
686 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
687 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
688 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;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 \"..\" without a lower bound.");
691 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;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 end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100694 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));
695 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
696 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100697
698 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
699 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
700 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
701 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
702 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
703 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
704 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
705 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
706 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
707 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
708 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
709 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
710 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
711 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
712 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
713 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
714 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
715 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
716 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
717 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
718 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
719 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
720 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
721 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100722 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
723 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
724 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
725 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +0100726
Radek Krejci495903e2018-11-13 11:30:45 +0100727 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));
728 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
729 logbuf_assert("Invalid type restrictions for binary type.");
730
731 assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;typedef mytype {type string {length 10;}}"
732 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
733 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
734 logbuf_assert("Invalid length restriction - the derived restriction (min..max) is not equally or more limiting.");
Radek Krejci4f28eda2018-11-12 11:46:16 +0100735
736 *state = NULL;
737 ly_ctx_destroy(ctx, NULL);
738}
739
Radek Krejcicda74152018-11-13 15:27:32 +0100740static void
741test_type_pattern(void **state)
742{
743 *state = test_type_pattern;
744
745 struct ly_ctx *ctx;
746 struct lys_module *mod;
747 struct lysc_type *type;
748
749 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
750
Radek Krejci027d5802018-11-14 16:57:28 +0100751 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 +0100752 "pattern .* {error-app-tag errortag;error-message error;}"
753 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
754 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
755 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
756 assert_non_null(type);
757 assert_non_null(((struct lysc_type_str*)type)->patterns);
758 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
759 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
760 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
761 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
762 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
763 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
764 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
765
766 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
767 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
768 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
769 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
770 assert_non_null(type);
771 assert_int_equal(LY_TYPE_STRING, type->basetype);
772 assert_int_equal(1, type->refcount);
773 assert_non_null(((struct lysc_type_str*)type)->patterns);
774 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
775 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
776 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
777
Radek Krejci04bc1e92018-11-14 14:28:13 +0100778 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
779 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
780 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
781 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
782 assert_non_null(type);
783 assert_int_equal(LY_TYPE_STRING, type->basetype);
784 assert_int_equal(1, type->refcount);
785 assert_non_null(((struct lysc_type_str*)type)->patterns);
786 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
787 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
788
Radek Krejcicda74152018-11-13 15:27:32 +0100789 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +0100790 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 +0100791 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
792 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
793 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
794 assert_non_null(type);
795 assert_non_null(((struct lysc_type_str*)type)->patterns);
796 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
797 /* TODO check some data "^Å™$" */
798
799 *state = NULL;
800 ly_ctx_destroy(ctx, NULL);
801}
802
Radek Krejci8b764662018-11-14 14:15:13 +0100803static void
804test_type_enum(void **state)
805{
806 *state = test_type_enum;
807
808 struct ly_ctx *ctx;
809 struct lys_module *mod;
810 struct lysc_type *type;
811
812 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
813
Radek Krejci027d5802018-11-14 16:57:28 +0100814 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 +0100815 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
816 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
817 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
818 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
819 assert_non_null(type);
820 assert_int_equal(LY_TYPE_ENUM, type->basetype);
821 assert_non_null(((struct lysc_type_enum*)type)->enums);
822 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
823 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
824 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
825 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
826 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
827 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
828 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
829 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
830 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
831 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
832 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
833 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
834 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
835 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
836
Radek Krejci027d5802018-11-14 16:57:28 +0100837 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 {"
838 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +0100839 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
840 LYS_IN_YANG));
841 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
842 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
843 assert_non_null(type);
844 assert_int_equal(LY_TYPE_ENUM, type->basetype);
845 assert_non_null(((struct lysc_type_enum*)type)->enums);
846 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
847 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
848 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
849 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
850 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
851
852
853 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +0100854 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
855 "enum one {if-feature f;}}}}", LYS_IN_YANG));
856 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 +0100857 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
858 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
859 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
860 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
861 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
862 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
863 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
864 "enum one; enum one;}}}", LYS_IN_YANG));
865 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
866 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
867 "enum '';}}}", LYS_IN_YANG));
868 logbuf_assert("Enum name must not be zero-length. Line number 1.");
869 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
870 "enum ' x';}}}", LYS_IN_YANG));
871 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
872 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
873 "enum 'x ';}}}", LYS_IN_YANG));
874 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
875 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
876 "enum 'inva\nlid';}}}", LYS_IN_YANG));
877 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
878
879 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
880 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
881 logbuf_assert("Missing enum substatement for enumeration type.");
882
Radek Krejci027d5802018-11-14 16:57:28 +0100883 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 +0100884 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
885 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
886 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
887
Radek Krejci027d5802018-11-14 16:57:28 +0100888 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 +0100889 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
890 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
891 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
892 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;}}}",
893 LYS_IN_YANG));
894 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
895 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
896
897 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;}}}}",
898 LYS_IN_YANG));
899 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
900 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
901
Radek Krejci04bc1e92018-11-14 14:28:13 +0100902 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
903 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
904 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
905 logbuf_assert("Missing enum substatement for enumeration type \"mytype\".");
906
Radek Krejci027d5802018-11-14 16:57:28 +0100907 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
908 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
909 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
910 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
911
Radek Krejci8b764662018-11-14 14:15:13 +0100912 *state = NULL;
913 ly_ctx_destroy(ctx, NULL);
914}
915
916static void
917test_type_bits(void **state)
918{
919 *state = test_type_bits;
920
921 struct ly_ctx *ctx;
922 struct lys_module *mod;
923 struct lysc_type *type;
924
925 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
926
Radek Krejci027d5802018-11-14 16:57:28 +0100927 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 +0100928 "bit automin; bit one {if-feature f; position 1;}"
929 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
930 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
931 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
932 assert_non_null(type);
933 assert_int_equal(LY_TYPE_BITS, type->basetype);
934 assert_non_null(((struct lysc_type_bits*)type)->bits);
935 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
936 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
937 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
938 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
939 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
940 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
941 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
942 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
943 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
944 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
945 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
946 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
947
Radek Krejci027d5802018-11-14 16:57:28 +0100948 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 {"
949 "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 +0100950 LYS_IN_YANG));
951 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
952 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
953 assert_non_null(type);
954 assert_int_equal(LY_TYPE_BITS, type->basetype);
955 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +0100956 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
957 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
958 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
959 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
960 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
961 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
962 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +0100963
964
965 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +0100966 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
967 "bit one {if-feature f;}}}}", LYS_IN_YANG));
968 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 +0100969 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
970 "bit one {position -1;}}}}", LYS_IN_YANG));
971 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
972 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
973 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
974 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
975 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
976 "bit one; bit one;}}}", LYS_IN_YANG));
977 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
978 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
979 "bit '11';}}}", LYS_IN_YANG));
980 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
981 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
982 "bit 'x1$1';}}}", LYS_IN_YANG));
983 logbuf_assert("Invalid identifier character '$'. Line number 1.");
984
985 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
986 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
987 logbuf_assert("Missing bit substatement for bits type.");
988
Radek Krejci027d5802018-11-14 16:57:28 +0100989 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 +0100990 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
991 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
992 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
993
Radek Krejci027d5802018-11-14 16:57:28 +0100994 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 +0100995 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
996 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
997 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
998 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;}}}",
999 LYS_IN_YANG));
1000 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1001 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1002
1003 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;}}}}",
1004 LYS_IN_YANG));
1005 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1006 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1007
Radek Krejci04bc1e92018-11-14 14:28:13 +01001008 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1009 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1010 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1011 logbuf_assert("Missing bit substatement for bits type \"mytype\".");
1012
Radek Krejci027d5802018-11-14 16:57:28 +01001013 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1014 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1015 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1016 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1017
Radek Krejci8b764662018-11-14 14:15:13 +01001018 *state = NULL;
1019 ly_ctx_destroy(ctx, NULL);
1020}
1021
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001022int main(void)
1023{
1024 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01001025 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
1026 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
1027 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01001028 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
1029 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01001030 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01001031 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
1032 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01001033 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001034 };
1035
1036 return cmocka_run_group_tests(tests, NULL, NULL);
1037}