blob: dde26edb32cbacb6994d76e9b819ca3c9240fe4a [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010020#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010021#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema.c"
24#include "../../src/context.c"
25#include "../../src/hash_table.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020026
Radek Krejcidd4e8d42018-10-16 14:55:43 +020027#include <stdarg.h>
28#include <stddef.h>
29#include <setjmp.h>
30#include <cmocka.h>
31
32#include <stdio.h>
33#include <string.h>
34
35#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020036
37#define BUFSIZE 1024
38char logbuf[BUFSIZE] = {0};
39
40/* set to 0 to printing error messages to stderr instead of checking them in code */
41#define ENABLE_LOGGER_CHECKING 1
42
43#if ENABLE_LOGGER_CHECKING
44static void
45logger(LY_LOG_LEVEL level, const char *msg, const char *path)
46{
47 (void) level; /* unused */
48
Radek Krejci87616bb2018-10-31 13:30:52 +010049 if (path && path[0]) {
Radek Krejcidd4e8d42018-10-16 14:55:43 +020050 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
51 } else {
52 strncpy(logbuf, msg, BUFSIZE - 1);
53 }
54}
55#endif
56
57static int
58logger_setup(void **state)
59{
60 (void) state; /* unused */
61#if ENABLE_LOGGER_CHECKING
62 ly_set_log_clb(logger, 1);
63#endif
64 return 0;
65}
66
Radek Krejci4f28eda2018-11-12 11:46:16 +010067static int
68logger_teardown(void **state)
69{
70 (void) state; /* unused */
71#if ENABLE_LOGGER_CHECKING
72 if (*state) {
73 fprintf(stderr, "%s\n", logbuf);
74 }
75#endif
76 return 0;
77}
78
Radek Krejcidd4e8d42018-10-16 14:55:43 +020079void
80logbuf_clean(void)
81{
82 logbuf[0] = '\0';
83}
84
85#if ENABLE_LOGGER_CHECKING
Radek Krejci16c0f822018-11-16 10:46:10 +010086# define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean()
Radek Krejcidd4e8d42018-10-16 14:55:43 +020087#else
88# define logbuf_assert(str)
89#endif
90
91static void
92test_module(void **state)
93{
94 (void) state; /* unused */
95
96 const char *str;
Radek Krejci313d9902018-11-08 09:42:58 +010097 struct ly_parser_ctx ctx = {0};
Radek Krejcidd4e8d42018-10-16 14:55:43 +020098 struct lys_module mod = {0};
Radek Krejci151a5b72018-10-19 14:21:44 +020099 struct lysc_feature *f;
100 struct lysc_iffeature *iff;
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200101
102 str = "module test {namespace urn:test; prefix t;"
Radek Krejci151a5b72018-10-19 14:21:44 +0200103 "feature f1;feature f2 {if-feature f1;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100104 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200105
Radek Krejcif8f882a2018-10-31 14:51:15 +0100106 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0));
107 logbuf_assert("Invalid argument mod (lys_compile()).");
108 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
109 logbuf_assert("Invalid argument mod->parsed (lys_compile()).");
Radek Krejci313d9902018-11-08 09:42:58 +0100110 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100111 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200112 assert_non_null(mod.compiled);
113 assert_ptr_equal(mod.parsed->name, mod.compiled->name);
114 assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
Radek Krejci151a5b72018-10-19 14:21:44 +0200115 /* features */
116 assert_non_null(mod.compiled->features);
117 assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200118 f = &mod.compiled->features[1];
Radek Krejci151a5b72018-10-19 14:21:44 +0200119 assert_non_null(f->iffeatures);
120 assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200121 iff = &f->iffeatures[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200122 assert_non_null(iff->expr);
123 assert_non_null(iff->features);
124 assert_int_equal(1, LY_ARRAY_SIZE(iff->features));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200125 assert_ptr_equal(&mod.compiled->features[0], iff->features[0]);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200126
Radek Krejci86d106e2018-10-18 09:53:19 +0200127 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200128
Radek Krejcif8f882a2018-10-31 14:51:15 +0100129 assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP));
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200130 assert_non_null(mod.compiled);
131 assert_string_equal("test", mod.compiled->name);
132 assert_string_equal("urn:test", mod.compiled->ns);
133
Radek Krejci86d106e2018-10-18 09:53:19 +0200134 lysc_module_free(mod.compiled, NULL);
135 mod.compiled = NULL;
136
Radek Krejci151a5b72018-10-19 14:21:44 +0200137 /* submodules cannot be compiled directly */
Radek Krejci86d106e2018-10-18 09:53:19 +0200138 str = "submodule test {belongs-to xxx {prefix x;}}";
Radek Krejci313d9902018-11-08 09:42:58 +0100139 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100140 assert_int_equal(LY_EINVAL, lys_compile(&mod, 0));
Radek Krejci86d106e2018-10-18 09:53:19 +0200141 logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules.");
142 assert_null(mod.compiled);
143
144 lysp_module_free(mod.parsed);
Radek Krejci313d9902018-11-08 09:42:58 +0100145 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200146}
147
Radek Krejci151a5b72018-10-19 14:21:44 +0200148static void
149test_feature(void **state)
150{
151 (void) state; /* unused */
152
Radek Krejci313d9902018-11-08 09:42:58 +0100153 struct ly_parser_ctx ctx = {0};
Radek Krejci1aefdf72018-11-01 11:01:39 +0100154 struct lys_module mod = {0}, *modp;
Radek Krejci151a5b72018-10-19 14:21:44 +0200155 const char *str;
156 struct lysc_feature *f, *f1;
157
158 str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n"
159 "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100160 "feature orfeature {if-feature \"f1 or f2\";}\n"
161 "feature andfeature {if-feature \"f1 and f2\";}\n"
Radek Krejci151a5b72018-10-19 14:21:44 +0200162 "feature f6 {if-feature \"not f1\";}\n"
Radek Krejcidde18c52018-10-24 14:43:04 +0200163 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
Radek Krejci87616bb2018-10-31 13:30:52 +0100164 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
165 "feature f9 {if-feature \"not not f1\";}}";
Radek Krejci151a5b72018-10-19 14:21:44 +0200166
Radek Krejci313d9902018-11-08 09:42:58 +0100167 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
168 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100169 assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0));
Radek Krejci151a5b72018-10-19 14:21:44 +0200170 assert_non_null(mod.compiled);
171 assert_non_null(mod.compiled->features);
Radek Krejci87616bb2018-10-31 13:30:52 +0100172 assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features));
Radek Krejci151a5b72018-10-19 14:21:44 +0200173 /* all features are disabled by default */
174 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
175 assert_int_equal(0, lysc_feature_value(f));
176 }
177 /* enable f1 */
178 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200179 f1 = &mod.compiled->features[0];
Radek Krejci151a5b72018-10-19 14:21:44 +0200180 assert_int_equal(1, lysc_feature_value(f1));
181
Radek Krejci87616bb2018-10-31 13:30:52 +0100182 /* enable orfeature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200183 f = &mod.compiled->features[3];
Radek Krejci151a5b72018-10-19 14:21:44 +0200184 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100185 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200186 assert_int_equal(1, lysc_feature_value(f));
187
Radek Krejci87616bb2018-10-31 13:30:52 +0100188 /* enable andfeature - no possible since f2 is disabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200189 f = &mod.compiled->features[4];
Radek Krejci151a5b72018-10-19 14:21:44 +0200190 assert_int_equal(0, lysc_feature_value(f));
Radek Krejci87616bb2018-10-31 13:30:52 +0100191 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
192 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci151a5b72018-10-19 14:21:44 +0200193 assert_int_equal(0, lysc_feature_value(f));
194
195 /* first enable f2, so f5 can be enabled then */
196 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2"));
Radek Krejci87616bb2018-10-31 13:30:52 +0100197 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature"));
Radek Krejci151a5b72018-10-19 14:21:44 +0200198 assert_int_equal(1, lysc_feature_value(f));
199
200 /* f1 is enabled, so f6 cannot be enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200201 f = &mod.compiled->features[5];
Radek Krejci151a5b72018-10-19 14:21:44 +0200202 assert_int_equal(0, lysc_feature_value(f));
203 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6"));
204 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
205 assert_int_equal(0, lysc_feature_value(f));
206
Radek Krejci87616bb2018-10-31 13:30:52 +0100207 /* so disable f1 - andfeature will became also disabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200208 assert_int_equal(1, lysc_feature_value(f1));
Radek Krejci151a5b72018-10-19 14:21:44 +0200209 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
210 assert_int_equal(0, lysc_feature_value(f1));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200211 assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4]));
Radek Krejci87616bb2018-10-31 13:30:52 +0100212 /* while orfeature is stille enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200213 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200214 /* and finally f6 can be enabled */
Radek Krejci151a5b72018-10-19 14:21:44 +0200215 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6"));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200216 assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200217
218 /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200219 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0]));
Radek Krejcidde18c52018-10-24 14:43:04 +0200220 /* long evaluation of f8 to need to reallocate internal stack for operators */
221 assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0]));
Radek Krejci151a5b72018-10-19 14:21:44 +0200222
Radek Krejci87616bb2018-10-31 13:30:52 +0100223 /* double negation of disabled f1 -> disabled */
224 assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0]));
225
Radek Krejci38920282018-11-01 09:24:16 +0100226 /* disable all features */
227 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*"));
228 LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) {
229 assert_int_equal(0, lys_feature_value(&mod, f->name));
230 }
231 /* re-setting already set feature */
232 assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1"));
233 assert_int_equal(0, lys_feature_value(&mod, "f1"));
234
235 /* enabling feature that cannot be enabled due to its if-features */
Radek Krejci1aefdf72018-11-01 11:01:39 +0100236 assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1"));
237 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature"));
238 logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejcica3db002018-11-01 10:31:01 +0100239 assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*"));
240 logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
Radek Krejci1aefdf72018-11-01 11:01:39 +0100241 /* test if not changed */
242 assert_int_equal(1, lys_feature_value(&mod, "f1"));
243 assert_int_equal(0, lys_feature_value(&mod, "f2"));
Radek Krejci38920282018-11-01 09:24:16 +0100244
Radek Krejci1aefdf72018-11-01 11:01:39 +0100245 /* invalid reference */
Radek Krejci38920282018-11-01 09:24:16 +0100246 assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx"));
247 logbuf_assert("Feature \"xxx\" not found in module \"a\".");
248
Radek Krejci151a5b72018-10-19 14:21:44 +0200249 lysc_module_free(mod.compiled, NULL);
250 lysp_module_free(mod.parsed);
Radek Krejci87616bb2018-10-31 13:30:52 +0100251
252 /* some invalid expressions */
Radek Krejci313d9902018-11-08 09:42:58 +0100253 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 +0100254 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100255 logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
256 lysp_module_free(mod.parsed);
257
Radek Krejci313d9902018-11-08 09:42:58 +0100258 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 +0100259 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100260 logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression.");
261 lysp_module_free(mod.parsed);
262
Radek Krejci313d9902018-11-08 09:42:58 +0100263 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100264 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100265 logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression.");
266 lysp_module_free(mod.parsed);
267
Radek Krejci313d9902018-11-08 09:42:58 +0100268 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100269 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100270 logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
271 lysp_module_free(mod.parsed);
272
Radek Krejci313d9902018-11-08 09:42:58 +0100273 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100274 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100275 logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
276 lysp_module_free(mod.parsed);
277
Radek Krejci313d9902018-11-08 09:42:58 +0100278 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100279 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100280 logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\".");
281 lysp_module_free(mod.parsed);
282
Radek Krejci313d9902018-11-08 09:42:58 +0100283 assert_int_equal(LY_SUCCESS, yang_parse(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod.parsed));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100284 assert_int_equal(LY_EVALID, lys_compile(&mod, 0));
Radek Krejci87616bb2018-10-31 13:30:52 +0100285 logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
286 lysp_module_free(mod.parsed);
287
Radek Krejci1aefdf72018-11-01 11:01:39 +0100288 /* import reference */
Radek Krejci313d9902018-11-08 09:42:58 +0100289 assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
Radek Krejci1aefdf72018-11-01 11:01:39 +0100290 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
291 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1"));
Radek Krejci313d9902018-11-08 09:42:58 +0100292 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 +0100293 assert_int_equal(LY_SUCCESS, lys_compile(modp, 0));
294 assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2"));
295 assert_int_equal(0, lys_feature_value(modp, "f1"));
296 assert_int_equal(1, lys_feature_value(modp, "f2"));
297
Radek Krejci313d9902018-11-08 09:42:58 +0100298 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200299}
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200300
Radek Krejci478020e2018-10-30 16:02:14 +0100301static void
302test_identity(void **state)
303{
304 (void) state; /* unused */
305
306 struct ly_ctx *ctx;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100307 struct lys_module *mod1, *mod2;
Radek Krejci478020e2018-10-30 16:02:14 +0100308 const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
Radek Krejci027d5802018-11-14 16:57:28 +0100309 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 +0100310
311 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
312 assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
313 assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100314 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100315
316 assert_non_null(mod1->compiled);
317 assert_non_null(mod1->compiled->identities);
318 assert_non_null(mod2->compiled);
319 assert_non_null(mod2->compiled->identities);
320
321 assert_non_null(mod1->compiled->identities[0].derived);
322 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
323 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
324 assert_non_null(mod2->compiled->identities[0].derived);
325 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
326 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
327 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
328 assert_non_null(mod2->compiled->identities[1].derived);
329 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
330 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
331 assert_non_null(mod2->compiled->identities[2].derived);
332 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
333 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
334
Radek Krejci478020e2018-10-30 16:02:14 +0100335 ly_ctx_destroy(ctx, NULL);
336}
337
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100338static void
339test_node_container(void **state)
340{
341 (void) state; /* unused */
342
343 struct ly_ctx *ctx;
344 struct lys_module *mod;
345 struct lysc_node_container *cont;
346
347 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
348 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
349 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
350 assert_non_null(mod->compiled);
351 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
352 assert_int_equal(LYS_CONTAINER, cont->nodetype);
353 assert_string_equal("c", cont->name);
354 assert_true(cont->flags & LYS_CONFIG_W);
355 assert_true(cont->flags & LYS_STATUS_CURR);
356
Radek Krejci98094b32018-11-02 16:21:47 +0100357 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 +0100358 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100359 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100360 assert_non_null(mod->compiled);
361 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
362 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100363 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100364 assert_non_null((cont = (struct lysc_node_container*)cont->child));
365 assert_int_equal(LYS_CONTAINER, cont->nodetype);
366 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100367 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100368 assert_string_equal("child", cont->name);
369
370 ly_ctx_destroy(ctx, NULL);
371}
372
Radek Krejci0f07bed2018-11-13 14:01:40 +0100373/**
374 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
375 * do not test the expression itself
376 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100377static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100378test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100379{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100380 *state = test_type_range;
381
382 struct ly_ctx *ctx;
383 struct lys_module *mod;
384 struct lysc_type *type;
385
386 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
387
388 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));
389 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
390 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
391 assert_non_null(type);
392 assert_int_equal(LY_TYPE_INT8, type->basetype);
393 assert_non_null(((struct lysc_type_num*)type)->range);
394 assert_non_null(((struct lysc_type_num*)type)->range->parts);
395 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
396 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
397 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
398 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
399 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
400
401 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));
402 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
403 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
404 assert_non_null(type);
405 assert_int_equal(LY_TYPE_INT16, type->basetype);
406 assert_non_null(((struct lysc_type_num*)type)->range);
407 assert_non_null(((struct lysc_type_num*)type)->range->parts);
408 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
409 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
410 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
411 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
412 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
413
414 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));
415 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
416 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
417 assert_non_null(type);
418 assert_int_equal(LY_TYPE_INT32, type->basetype);
419 assert_non_null(((struct lysc_type_num*)type)->range);
420 assert_non_null(((struct lysc_type_num*)type)->range->parts);
421 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
422 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
423 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
424 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
425 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
426
427 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));
428 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
429 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
430 assert_non_null(type);
431 assert_int_equal(LY_TYPE_INT64, type->basetype);
432 assert_non_null(((struct lysc_type_num*)type)->range);
433 assert_non_null(((struct lysc_type_num*)type)->range->parts);
434 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
435 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
436 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
437 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
438 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
439
440 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));
441 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
442 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
443 assert_non_null(type);
444 assert_int_equal(LY_TYPE_UINT8, type->basetype);
445 assert_non_null(((struct lysc_type_num*)type)->range);
446 assert_non_null(((struct lysc_type_num*)type)->range->parts);
447 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
448 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
449 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
450 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
451 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
452
453 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));
454 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
455 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
456 assert_non_null(type);
457 assert_int_equal(LY_TYPE_UINT16, type->basetype);
458 assert_non_null(((struct lysc_type_num*)type)->range);
459 assert_non_null(((struct lysc_type_num*)type)->range->parts);
460 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
461 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
462 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
463 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
464 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
465
466 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));
467 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
468 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
469 assert_non_null(type);
470 assert_int_equal(LY_TYPE_UINT32, type->basetype);
471 assert_non_null(((struct lysc_type_num*)type)->range);
472 assert_non_null(((struct lysc_type_num*)type)->range->parts);
473 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
474 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
475 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
476 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
477 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
478
479 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));
480 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
481 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
482 assert_non_null(type);
483 assert_int_equal(LY_TYPE_UINT64, type->basetype);
484 assert_non_null(((struct lysc_type_num*)type)->range);
485 assert_non_null(((struct lysc_type_num*)type)->range->parts);
486 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
487 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
488 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
489 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
490 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
491
492 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
493 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
494 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
495 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
496 assert_non_null(type);
497 assert_int_equal(3, type->refcount);
498 assert_int_equal(LY_TYPE_UINT8, type->basetype);
499 assert_non_null(((struct lysc_type_num*)type)->range);
500 assert_non_null(((struct lysc_type_num*)type)->range->parts);
501 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
502
503 *state = NULL;
504 ly_ctx_destroy(ctx, NULL);
505}
506
507static void
508test_type_length(void **state)
509{
510 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100511
512 struct ly_ctx *ctx;
513 struct lys_module *mod;
514 struct lysc_type *type;
515
516 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
517
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100518 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 +0100519 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
520 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
521 assert_non_null(type);
522 assert_non_null(((struct lysc_type_bin*)type)->length);
523 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100524 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
525 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100526 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
527 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
528 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
529
530 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
531 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
532 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
533 assert_non_null(type);
534 assert_non_null(((struct lysc_type_bin*)type)->length);
535 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
536 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
537 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
538 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
539
540 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));
541 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
542 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
543 assert_non_null(type);
544 assert_non_null(((struct lysc_type_bin*)type)->length);
545 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
546 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
547 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
548 assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
549
550 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
551 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
552 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
553 assert_non_null(type);
554 assert_non_null(((struct lysc_type_bin*)type)->length);
555 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
556 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
557 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
558 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
559
560 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));
561 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
562 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
563 assert_non_null(type);
564 assert_non_null(((struct lysc_type_bin*)type)->length);
565 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
566 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
567 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
568 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
569
570 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));
571 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
572 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
573 assert_non_null(type);
574 assert_non_null(((struct lysc_type_bin*)type)->length);
575 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
576 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
577 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
578 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
579 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
580 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
581
582 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));
583 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
584 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
585 assert_non_null(type);
586 assert_non_null(((struct lysc_type_bin*)type)->length);
587 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
588 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
589 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
590 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
591 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
592 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
593
Radek Krejcib31c8992018-11-12 16:29:16 +0100594 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
595 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
596 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
597 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
598 assert_non_null(type);
599 assert_non_null(((struct lysc_type_bin*)type)->length);
600 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
601 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
602 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
603 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
604
605 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
606 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
607 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
608 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
609 assert_non_null(type);
610 assert_non_null(((struct lysc_type_bin*)type)->length);
611 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
612 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
613 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
614 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
615
616 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
617 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
618 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
619 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
620 assert_non_null(type);
621 assert_non_null(((struct lysc_type_bin*)type)->length);
622 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
623 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
624 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
625 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
626 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
627 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
628
629 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 +0100630 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100631 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
632 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
633 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100634 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100635 assert_non_null(((struct lysc_type_bin*)type)->length);
636 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
637 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
638 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100639 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +0100640 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
641 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100642 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100643 assert_non_null(((struct lysc_type_bin*)type)->length);
644 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
645 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
646 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
647 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
648
Radek Krejci56aa27c2018-11-13 14:21:59 +0100649 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
650 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +0100651 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
652 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
653 assert_non_null(type);
654 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100655 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +0100656 assert_non_null(((struct lysc_type_str*)type)->length);
657 assert_non_null(((struct lysc_type_str*)type)->length->parts);
658 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
659 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
660 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +0100661
Radek Krejci5969f272018-11-23 10:03:58 +0100662 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
663 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
664 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
665 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
666 assert_non_null(type);
667 assert_int_equal(LY_TYPE_STRING, type->basetype);
668 assert_int_equal(1, type->refcount);
669 assert_non_null(((struct lysc_type_str*)type)->length);
670 assert_non_null(((struct lysc_type_str*)type)->length->parts);
671 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
672 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
673 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
674
Radek Krejci4f28eda2018-11-12 11:46:16 +0100675 /* invalid values */
676 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
677 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
678 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
679 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
680 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
681 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
682 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));
683 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
684 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
685 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));
686 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
687 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
688 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));
689 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
690 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
691 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
692 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
693 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100694 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));
695 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
696 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
697 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
698 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
699 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
700 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
701 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
702 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
703 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
704 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
705 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
706 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
707 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
708 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100709 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));
710 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
711 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100712
713 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
714 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
715 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
716 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
717 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
718 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
719 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
720 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
721 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
722 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
723 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
724 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
725 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
726 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
727 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
728 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
729 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
730 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
731 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
732 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
733 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
734 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
735 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
736 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100737 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
738 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
739 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
740 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +0100741
Radek Krejci495903e2018-11-13 11:30:45 +0100742 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));
743 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
744 logbuf_assert("Invalid type restrictions for binary type.");
745
Radek Krejci4f28eda2018-11-12 11:46:16 +0100746 *state = NULL;
747 ly_ctx_destroy(ctx, NULL);
748}
749
Radek Krejcicda74152018-11-13 15:27:32 +0100750static void
751test_type_pattern(void **state)
752{
753 *state = test_type_pattern;
754
755 struct ly_ctx *ctx;
756 struct lys_module *mod;
757 struct lysc_type *type;
758
759 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
760
Radek Krejci027d5802018-11-14 16:57:28 +0100761 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 +0100762 "pattern .* {error-app-tag errortag;error-message error;}"
763 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
764 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
765 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
766 assert_non_null(type);
767 assert_non_null(((struct lysc_type_str*)type)->patterns);
768 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
769 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
770 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
771 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
772 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
773 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
774 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
775
776 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
777 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
778 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
779 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
780 assert_non_null(type);
781 assert_int_equal(LY_TYPE_STRING, type->basetype);
782 assert_int_equal(1, type->refcount);
783 assert_non_null(((struct lysc_type_str*)type)->patterns);
784 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
785 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
786 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
787
Radek Krejci04bc1e92018-11-14 14:28:13 +0100788 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
789 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
790 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
791 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
792 assert_non_null(type);
793 assert_int_equal(LY_TYPE_STRING, type->basetype);
794 assert_int_equal(1, type->refcount);
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 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
798
Radek Krejcicda74152018-11-13 15:27:32 +0100799 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +0100800 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 +0100801 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
802 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
803 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
804 assert_non_null(type);
805 assert_non_null(((struct lysc_type_str*)type)->patterns);
806 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
807 /* TODO check some data "^Å™$" */
808
809 *state = NULL;
810 ly_ctx_destroy(ctx, NULL);
811}
812
Radek Krejci8b764662018-11-14 14:15:13 +0100813static void
814test_type_enum(void **state)
815{
816 *state = test_type_enum;
817
818 struct ly_ctx *ctx;
819 struct lys_module *mod;
820 struct lysc_type *type;
821
822 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
823
Radek Krejci027d5802018-11-14 16:57:28 +0100824 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 +0100825 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
826 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
827 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
828 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
829 assert_non_null(type);
830 assert_int_equal(LY_TYPE_ENUM, type->basetype);
831 assert_non_null(((struct lysc_type_enum*)type)->enums);
832 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
833 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
834 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
835 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
836 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
837 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
838 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
839 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
840 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
841 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
842 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
843 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
844 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
845 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
846
Radek Krejci027d5802018-11-14 16:57:28 +0100847 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 {"
848 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +0100849 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
850 LYS_IN_YANG));
851 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
852 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
853 assert_non_null(type);
854 assert_int_equal(LY_TYPE_ENUM, type->basetype);
855 assert_non_null(((struct lysc_type_enum*)type)->enums);
856 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
857 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
858 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
859 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
860 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
861
862
863 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +0100864 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
865 "enum one {if-feature f;}}}}", LYS_IN_YANG));
866 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 +0100867 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
868 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
869 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
870 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
871 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
872 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
873 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
874 "enum one; enum one;}}}", LYS_IN_YANG));
875 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
876 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
877 "enum '';}}}", LYS_IN_YANG));
878 logbuf_assert("Enum name must not be zero-length. Line number 1.");
879 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
880 "enum ' x';}}}", LYS_IN_YANG));
881 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
882 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
883 "enum 'x ';}}}", LYS_IN_YANG));
884 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
885 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
886 "enum 'inva\nlid';}}}", LYS_IN_YANG));
887 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
888
889 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
890 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
891 logbuf_assert("Missing enum substatement for enumeration type.");
892
Radek Krejci027d5802018-11-14 16:57:28 +0100893 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 +0100894 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
895 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
896 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
897
Radek Krejci027d5802018-11-14 16:57:28 +0100898 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 +0100899 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
900 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
901 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
902 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;}}}",
903 LYS_IN_YANG));
904 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
905 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
906
907 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;}}}}",
908 LYS_IN_YANG));
909 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
910 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
911
Radek Krejci04bc1e92018-11-14 14:28:13 +0100912 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
913 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
914 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100915 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +0100916
Radek Krejci027d5802018-11-14 16:57:28 +0100917 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
918 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
919 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
920 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
921
Radek Krejci8b764662018-11-14 14:15:13 +0100922 *state = NULL;
923 ly_ctx_destroy(ctx, NULL);
924}
925
926static void
927test_type_bits(void **state)
928{
929 *state = test_type_bits;
930
931 struct ly_ctx *ctx;
932 struct lys_module *mod;
933 struct lysc_type *type;
934
935 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
936
Radek Krejci027d5802018-11-14 16:57:28 +0100937 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 +0100938 "bit automin; bit one {if-feature f; position 1;}"
939 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
940 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
941 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
942 assert_non_null(type);
943 assert_int_equal(LY_TYPE_BITS, type->basetype);
944 assert_non_null(((struct lysc_type_bits*)type)->bits);
945 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
946 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
947 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
948 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
949 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
950 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
951 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
952 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
953 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
954 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
955 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
956 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
957
Radek Krejci027d5802018-11-14 16:57:28 +0100958 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 {"
959 "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 +0100960 LYS_IN_YANG));
961 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
962 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
963 assert_non_null(type);
964 assert_int_equal(LY_TYPE_BITS, type->basetype);
965 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +0100966 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
967 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
968 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
969 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
970 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
971 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
972 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +0100973
974
975 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +0100976 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
977 "bit one {if-feature f;}}}}", LYS_IN_YANG));
978 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 +0100979 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
980 "bit one {position -1;}}}}", LYS_IN_YANG));
981 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
982 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
983 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
984 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
985 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
986 "bit one; bit one;}}}", LYS_IN_YANG));
987 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
988 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
989 "bit '11';}}}", LYS_IN_YANG));
990 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
991 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
992 "bit 'x1$1';}}}", LYS_IN_YANG));
993 logbuf_assert("Invalid identifier character '$'. Line number 1.");
994
995 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
996 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
997 logbuf_assert("Missing bit substatement for bits type.");
998
Radek Krejci027d5802018-11-14 16:57:28 +0100999 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 +01001000 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
1001 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1002 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1003
Radek Krejci027d5802018-11-14 16:57:28 +01001004 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 +01001005 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
1006 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1007 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
1008 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;}}}",
1009 LYS_IN_YANG));
1010 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1011 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1012
1013 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;}}}}",
1014 LYS_IN_YANG));
1015 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1016 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1017
Radek Krejci04bc1e92018-11-14 14:28:13 +01001018 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1019 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1020 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001021 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001022
Radek Krejci027d5802018-11-14 16:57:28 +01001023 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1024 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1025 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1026 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1027
Radek Krejci8b764662018-11-14 14:15:13 +01001028 *state = NULL;
1029 ly_ctx_destroy(ctx, NULL);
1030}
1031
Radek Krejci6cba4292018-11-15 17:33:29 +01001032static void
1033test_type_dec64(void **state)
1034{
1035 *state = test_type_dec64;
1036
1037 struct ly_ctx *ctx;
1038 struct lys_module *mod;
1039 struct lysc_type *type;
1040
1041 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1042
1043 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1044 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
1045 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1046 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1047 assert_non_null(type);
1048 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1049 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1050 assert_non_null(((struct lysc_type_dec*)type)->range);
1051 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1052 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1053 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1054 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1055
1056 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1057 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1058 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1059 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1060 assert_non_null(type);
1061 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1062 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1063 assert_non_null(((struct lysc_type_dec*)type)->range);
1064 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1065 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1066 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1067 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1068 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1069 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1070 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1071 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1072
1073 /* invalid cases */
1074 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1075 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1076 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1077 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1078 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1079 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1080
1081 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
1082 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1083 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1084
Radek Krejci643c8242018-11-15 17:51:11 +01001085 assert_non_null(mod = lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG));
1086 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001087 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001088
Radek Krejci6cba4292018-11-15 17:33:29 +01001089 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
1090 "range '3.142';}}}", LYS_IN_YANG));
1091 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1092 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1093
1094 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
1095 "range '4 | 3.14';}}}", LYS_IN_YANG));
1096 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1097 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1098
Radek Krejci643c8242018-11-15 17:51:11 +01001099 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
1100 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
1101 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1102 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1103
1104 assert_non_null(mod = lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
1105 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1106 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1107 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1108
Radek Krejci6cba4292018-11-15 17:33:29 +01001109 *state = NULL;
1110 ly_ctx_destroy(ctx, NULL);
1111}
1112
Radek Krejci16c0f822018-11-16 10:46:10 +01001113static void
1114test_type_instanceid(void **state)
1115{
1116 *state = test_type_instanceid;
1117
1118 struct ly_ctx *ctx;
1119 struct lys_module *mod;
1120 struct lysc_type *type;
1121
1122 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1123
1124 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1125 "leaf l1 {type instance-identifier {require-instance true;}}"
1126 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
1127 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1128 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1129 assert_non_null(type);
1130 assert_int_equal(LY_TYPE_INST, type->basetype);
1131 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1132
1133 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1134 assert_non_null(type);
1135 assert_int_equal(LY_TYPE_INST, type->basetype);
1136 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1137
1138 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1139 assert_non_null(type);
1140 assert_int_equal(LY_TYPE_INST, type->basetype);
1141 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1142
1143 /* invalid cases */
1144 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1145 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1146
1147 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG));
1148 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1149 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1150
1151 *state = NULL;
1152 ly_ctx_destroy(ctx, NULL);
1153}
1154
Radek Krejci555cb5b2018-11-16 14:54:33 +01001155static void
1156test_type_identityref(void **state)
1157{
1158 *state = test_type_identityref;
1159
1160 struct ly_ctx *ctx;
1161 struct lys_module *mod;
1162 struct lysc_type *type;
1163
1164 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1165
1166 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;identity i; identity j; identity k {base i;}"
1167 "typedef mytype {type identityref {base i;}}"
1168 "leaf l1 {type mytype;} leaf l2 {type identityref {base k; base j;}}}", LYS_IN_YANG));
1169 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1170 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1171 assert_non_null(type);
1172 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1173 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1174 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1175 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1176
1177 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1178 assert_non_null(type);
1179 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1180 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1181 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1182 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1183 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1184
1185 /* invalid cases */
1186 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
1187 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1188 logbuf_assert("Missing base substatement for identityref type.");
1189
1190 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
1191 "leaf l {type mytype;}}", LYS_IN_YANG));
1192 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1193 logbuf_assert("Missing base substatement for identityref type mytype.");
1194
1195 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
1196 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
1197 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1198 logbuf_assert("Invalid base substatement for type not directly derived from identityref built-in type.");
1199
1200 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
1201 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1202 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1203 logbuf_assert("Invalid base substatement for type \"mytype2\" not directly derived from identityref built-in type.");
1204
1205 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
1206 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
1207 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1208 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1209
Radek Krejci322614f2018-11-16 15:05:44 +01001210 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG));
1211 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1212 logbuf_assert("Unable to find base (j) of identityref.");
1213
1214 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG));
1215 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1216 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1217
Radek Krejci555cb5b2018-11-16 14:54:33 +01001218 *state = NULL;
1219 ly_ctx_destroy(ctx, NULL);
1220}
1221
Radek Krejcia3045382018-11-22 14:30:31 +01001222static void
1223test_type_leafref(void **state)
1224{
1225 *state = test_type_leafref;
1226
1227 struct ly_ctx *ctx;
1228 struct lys_module *mod;
1229 struct lysc_type *type;
1230 const char *path, *name, *prefix;
1231 size_t prefix_len, name_len;
1232 int parent_times, has_predicate;
1233
1234 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1235
1236 /* lys_path_token() */
1237 path = "invalid_path";
1238 parent_times = 0;
1239 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1240 path = "..";
1241 parent_times = 0;
1242 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1243 path = "..[";
1244 parent_times = 0;
1245 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1246 path = "../";
1247 parent_times = 0;
1248 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1249 path = "/";
1250 parent_times = 0;
1251 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1252
1253 path = "../../pref:id/xxx[predicate]/invalid!!!";
1254 parent_times = 0;
1255 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1256 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1257 assert_int_equal(4, prefix_len);
1258 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1259 assert_int_equal(2, name_len);
1260 assert_int_equal(0, strncmp("id", name, name_len));
1261 assert_int_equal(2, parent_times);
1262 assert_int_equal(0, has_predicate);
1263 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1264 assert_string_equal("[predicate]/invalid!!!", path);
1265 assert_int_equal(0, prefix_len);
1266 assert_null(prefix);
1267 assert_int_equal(3, name_len);
1268 assert_int_equal(0, strncmp("xxx", name, name_len));
1269 assert_int_equal(1, has_predicate);
1270 path += 11;
1271 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1272 assert_string_equal("!!!", path);
1273 assert_int_equal(0, prefix_len);
1274 assert_null(prefix);
1275 assert_int_equal(7, name_len);
1276 assert_int_equal(0, strncmp("invalid", name, name_len));
1277
1278 path = "/absolute/prefix:path";
1279 parent_times = 0;
1280 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1281 assert_string_equal("/prefix:path", path);
1282 assert_int_equal(0, prefix_len);
1283 assert_null(prefix);
1284 assert_int_equal(8, name_len);
1285 assert_int_equal(0, strncmp("absolute", name, name_len));
1286 assert_int_equal(-1, parent_times);
1287 assert_int_equal(0, has_predicate);
1288 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1289 assert_int_equal(0, *path);
1290 assert_int_equal(6, prefix_len);
1291 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1292 assert_int_equal(4, name_len);
1293 assert_int_equal(0, strncmp("path", name, name_len));
1294 assert_int_equal(0, has_predicate);
1295
1296 /* complete leafref paths */
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001297 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
Radek Krejcia3045382018-11-22 14:30:31 +01001298 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1299 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
1300 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1301 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1302 assert_non_null(type);
1303 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1304 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001305 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejcia3045382018-11-22 14:30:31 +01001306 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1307 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1308 assert_non_null(type);
1309 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1310 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
1311 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1312
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001313 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mytype {type leafref {path /b:target;}}"
1314 "typedef mytype2 {type mytype;} leaf ref {type mytype2;}leaf target {type string;}}", LYS_IN_YANG));
1315 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1316 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1317 assert_non_null(type);
1318 assert_int_equal(3, type->refcount);
1319 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1320 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1321 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1322 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1323
1324 /* prefixes are reversed to check using correct context of the path! */
1325 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix b; import b {prefix c;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001326 "typedef mytype3 {type c:mytype {require-instance false;}}"
1327 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001328 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1329 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1330 assert_non_null(type);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001331 assert_int_equal(2, type->refcount);
1332 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1333 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1334 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1335 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1336 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1337 assert_non_null(type);
1338 assert_int_equal(4, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001339 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1340 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1341 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1342 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1343
Radek Krejcia3045382018-11-22 14:30:31 +01001344 /* TODO target in list with predicates */
1345
1346
1347 /* invalid paths */
1348 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
1349 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
1350 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1351 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
1352 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
1353 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
1354 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1355 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
1356 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
1357 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
1358 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1359 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
1360 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf target1 {type string;}container a {leaf target2 {type uint8;}}"
1361 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
1362 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1363 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
1364 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
1365 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
1366 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1367 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
1368 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
1369 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
1370 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1371 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
1372 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
1373 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
1374 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1375 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci2f4a0292018-11-22 15:41:53 +01001376 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
1377 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
1378 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1379 logbuf_assert("Missing path substatement for leafref type.");
1380 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
1381 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
1382 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1383 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejcia3045382018-11-22 14:30:31 +01001384
1385 *state = NULL;
1386 ly_ctx_destroy(ctx, NULL);
1387}
1388
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001389int main(void)
1390{
1391 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01001392 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
1393 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
1394 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01001395 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
1396 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01001397 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01001398 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
1399 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01001400 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01001401 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01001402 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01001403 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01001404 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001405 };
1406
1407 return cmocka_run_group_tests(tests, NULL, NULL);
1408}