blob: f4dfa8b7d3ade17adcb2c8dc0ccefd65647a84b3 [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{
Radek Krejci7f2a5362018-11-28 13:05:37 +0100304 *state = test_identity;
Radek Krejci478020e2018-10-30 16:02:14 +0100305
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 Krejci7f2a5362018-11-28 13:05:37 +0100314 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0));
Radek Krejcif8f882a2018-10-31 14:51:15 +0100315 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
Radek Krejci478020e2018-10-30 16:02:14 +0100316
317 assert_non_null(mod1->compiled);
318 assert_non_null(mod1->compiled->identities);
319 assert_non_null(mod2->compiled);
320 assert_non_null(mod2->compiled->identities);
321
322 assert_non_null(mod1->compiled->identities[0].derived);
323 assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
324 assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
325 assert_non_null(mod2->compiled->identities[0].derived);
326 assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
327 assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
328 assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
329 assert_non_null(mod2->compiled->identities[1].derived);
330 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
331 assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
332 assert_non_null(mod2->compiled->identities[2].derived);
333 assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
334 assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
335
Radek Krejci7f2a5362018-11-28 13:05:37 +0100336 *state = NULL;
Radek Krejci478020e2018-10-30 16:02:14 +0100337 ly_ctx_destroy(ctx, NULL);
338}
339
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100340static void
341test_node_container(void **state)
342{
343 (void) state; /* unused */
344
345 struct ly_ctx *ctx;
346 struct lys_module *mod;
347 struct lysc_node_container *cont;
348
349 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
350 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG));
351 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
352 assert_non_null(mod->compiled);
353 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
354 assert_int_equal(LYS_CONTAINER, cont->nodetype);
355 assert_string_equal("c", cont->name);
356 assert_true(cont->flags & LYS_CONFIG_W);
357 assert_true(cont->flags & LYS_STATUS_CURR);
358
Radek Krejci98094b32018-11-02 16:21:47 +0100359 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 +0100360 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
Radek Krejci98094b32018-11-02 16:21:47 +0100361 logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting.");
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100362 assert_non_null(mod->compiled);
363 assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data));
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_non_null((cont = (struct lysc_node_container*)cont->child));
367 assert_int_equal(LYS_CONTAINER, cont->nodetype);
368 assert_true(cont->flags & LYS_CONFIG_R);
Radek Krejci98094b32018-11-02 16:21:47 +0100369 assert_true(cont->flags & LYS_STATUS_DEPRC);
Radek Krejcibd8d9ba2018-11-02 16:06:26 +0100370 assert_string_equal("child", cont->name);
371
372 ly_ctx_destroy(ctx, NULL);
373}
374
Radek Krejci0e5d8382018-11-28 16:37:53 +0100375static void
376test_node_leaflist(void **state)
377{
378 *state = test_node_leaflist;
379
380 struct ly_ctx *ctx;
381 struct lys_module *mod;
382 struct lysc_type *type;
383 struct lysc_node_leaflist *ll;
384
385 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
386
387 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
388 "typedef mytype {type union {type leafref {path ../target;} type string;}}"
389 "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
390 "leaf-list ll2 {type leafref {path ../target;}}"
391 "leaf target {type int8;}}",
392 LYS_IN_YANG));
393 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
394 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
395 assert_non_null(type);
396 assert_int_equal(1, type->refcount);
397 assert_int_equal(LY_TYPE_UNION, type->basetype);
398 assert_non_null(((struct lysc_type_union*)type)->types);
399 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
400 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
401 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
402 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
403 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
404 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
405 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
406 assert_non_null(type);
407 assert_int_equal(1, type->refcount);
408 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
409 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
410 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
411
412 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;feature f; leaf-list ll {type string;}}", LYS_IN_YANG));
413 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
414 assert_non_null(mod->compiled);
415 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
416 assert_int_equal(0, ll->min);
417 assert_int_equal((uint32_t)-1, ll->max);
418
419 *state = NULL;
420 ly_ctx_destroy(ctx, NULL);
421}
422
Radek Krejci0f07bed2018-11-13 14:01:40 +0100423/**
424 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
425 * do not test the expression itself
426 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100427static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100428test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100429{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100430 *state = test_type_range;
431
432 struct ly_ctx *ctx;
433 struct lys_module *mod;
434 struct lysc_type *type;
435
436 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
437
438 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));
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_INT8, 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(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
447 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
448 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
449 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
450
451 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));
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_INT16, 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(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
460 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
461 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
462 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
463
464 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));
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_INT32, 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(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
473 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
474 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
475 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
476
477 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));
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_INT64, 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(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
486 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
487 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
488 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
489
490 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));
491 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
492 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
493 assert_non_null(type);
494 assert_int_equal(LY_TYPE_UINT8, type->basetype);
495 assert_non_null(((struct lysc_type_num*)type)->range);
496 assert_non_null(((struct lysc_type_num*)type)->range->parts);
497 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
498 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
499 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
500 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
501 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
502
503 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));
504 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
505 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
506 assert_non_null(type);
507 assert_int_equal(LY_TYPE_UINT16, type->basetype);
508 assert_non_null(((struct lysc_type_num*)type)->range);
509 assert_non_null(((struct lysc_type_num*)type)->range->parts);
510 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
511 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
512 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
513 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
514 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
515
516 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));
517 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_int_equal(LY_TYPE_UINT32, type->basetype);
521 assert_non_null(((struct lysc_type_num*)type)->range);
522 assert_non_null(((struct lysc_type_num*)type)->range->parts);
523 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
524 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
525 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
526 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
527 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
528
529 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));
530 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
531 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
532 assert_non_null(type);
533 assert_int_equal(LY_TYPE_UINT64, type->basetype);
534 assert_non_null(((struct lysc_type_num*)type)->range);
535 assert_non_null(((struct lysc_type_num*)type)->range->parts);
536 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
537 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
538 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
539 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
540 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
541
542 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
543 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
544 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
545 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
546 assert_non_null(type);
547 assert_int_equal(3, type->refcount);
548 assert_int_equal(LY_TYPE_UINT8, type->basetype);
549 assert_non_null(((struct lysc_type_num*)type)->range);
550 assert_non_null(((struct lysc_type_num*)type)->range->parts);
551 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
552
553 *state = NULL;
554 ly_ctx_destroy(ctx, NULL);
555}
556
557static void
558test_type_length(void **state)
559{
560 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100561
562 struct ly_ctx *ctx;
563 struct lys_module *mod;
564 struct lysc_type *type;
565
566 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
567
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100568 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 +0100569 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);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100574 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
575 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100576 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
577 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
578 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
579
580 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", 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(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100587 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
588 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100589
590 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));
591 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
592 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
593 assert_non_null(type);
594 assert_non_null(((struct lysc_type_bin*)type)->length);
595 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
596 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
597 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100598 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100599
600 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
601 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
602 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
603 assert_non_null(type);
604 assert_non_null(((struct lysc_type_bin*)type)->length);
605 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
606 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
607 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
608 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
609
610 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));
611 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
612 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
613 assert_non_null(type);
614 assert_non_null(((struct lysc_type_bin*)type)->length);
615 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
616 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
617 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
618 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
619
620 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));
621 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
622 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
623 assert_non_null(type);
624 assert_non_null(((struct lysc_type_bin*)type)->length);
625 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
626 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
627 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
628 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
629 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
630 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
631
632 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));
633 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
634 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
635 assert_non_null(type);
636 assert_non_null(((struct lysc_type_bin*)type)->length);
637 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
638 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
639 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
640 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
641 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
642 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
643
Radek Krejcib31c8992018-11-12 16:29:16 +0100644 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
645 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
646 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
647 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
648 assert_non_null(type);
649 assert_non_null(((struct lysc_type_bin*)type)->length);
650 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
651 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
652 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
653 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
654
655 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
656 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
657 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
658 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
659 assert_non_null(type);
660 assert_non_null(((struct lysc_type_bin*)type)->length);
661 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
662 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
663 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
664 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
665
666 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
667 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
668 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
669 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
670 assert_non_null(type);
671 assert_non_null(((struct lysc_type_bin*)type)->length);
672 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
673 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
674 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
675 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
676 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
677 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
678
679 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 +0100680 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100681 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
682 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
683 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100684 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100685 assert_non_null(((struct lysc_type_bin*)type)->length);
686 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
687 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
688 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100689 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +0100690 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
691 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100692 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100693 assert_non_null(((struct lysc_type_bin*)type)->length);
694 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
695 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
696 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
697 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
698
Radek Krejci56aa27c2018-11-13 14:21:59 +0100699 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
700 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +0100701 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
702 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
703 assert_non_null(type);
704 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100705 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +0100706 assert_non_null(((struct lysc_type_str*)type)->length);
707 assert_non_null(((struct lysc_type_str*)type)->length->parts);
708 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
709 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
710 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +0100711
Radek Krejci5969f272018-11-23 10:03:58 +0100712 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
713 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
714 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
715 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
716 assert_non_null(type);
717 assert_int_equal(LY_TYPE_STRING, type->basetype);
718 assert_int_equal(1, type->refcount);
719 assert_non_null(((struct lysc_type_str*)type)->length);
720 assert_non_null(((struct lysc_type_str*)type)->length->parts);
721 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
722 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
723 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
724
Radek Krejci4f28eda2018-11-12 11:46:16 +0100725 /* invalid values */
726 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
727 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
728 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
729 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
730 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
731 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
732 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));
733 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
734 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
735 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));
736 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
737 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
738 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));
739 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
740 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
741 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
742 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
743 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100744 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));
745 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
746 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
747 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
748 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
749 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
750 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
751 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
752 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
753 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
754 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
755 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
756 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
757 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
758 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100759 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));
760 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
761 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100762
763 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
764 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
765 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
766 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
767 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
768 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
769 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
770 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
771 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
772 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
773 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
774 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
775 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
776 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
777 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
778 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
779 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
780 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
781 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
782 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
783 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
784 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
785 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
786 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100787 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
788 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
789 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
790 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +0100791
Radek Krejci495903e2018-11-13 11:30:45 +0100792 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));
793 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
794 logbuf_assert("Invalid type restrictions for binary type.");
795
Radek Krejci4f28eda2018-11-12 11:46:16 +0100796 *state = NULL;
797 ly_ctx_destroy(ctx, NULL);
798}
799
Radek Krejcicda74152018-11-13 15:27:32 +0100800static void
801test_type_pattern(void **state)
802{
803 *state = test_type_pattern;
804
805 struct ly_ctx *ctx;
806 struct lys_module *mod;
807 struct lysc_type *type;
808
809 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
810
Radek Krejci027d5802018-11-14 16:57:28 +0100811 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 +0100812 "pattern .* {error-app-tag errortag;error-message error;}"
813 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
814 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
815 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
816 assert_non_null(type);
817 assert_non_null(((struct lysc_type_str*)type)->patterns);
818 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
819 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
820 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
821 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
822 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
823 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
824 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
825
826 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
827 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
828 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
829 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
830 assert_non_null(type);
831 assert_int_equal(LY_TYPE_STRING, type->basetype);
832 assert_int_equal(1, type->refcount);
833 assert_non_null(((struct lysc_type_str*)type)->patterns);
834 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
835 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
836 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
837
Radek Krejci04bc1e92018-11-14 14:28:13 +0100838 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
839 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
840 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
841 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
842 assert_non_null(type);
843 assert_int_equal(LY_TYPE_STRING, type->basetype);
844 assert_int_equal(1, type->refcount);
845 assert_non_null(((struct lysc_type_str*)type)->patterns);
846 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
847 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
848
Radek Krejcicda74152018-11-13 15:27:32 +0100849 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +0100850 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 +0100851 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
852 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
853 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
854 assert_non_null(type);
855 assert_non_null(((struct lysc_type_str*)type)->patterns);
856 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
857 /* TODO check some data "^Å™$" */
858
859 *state = NULL;
860 ly_ctx_destroy(ctx, NULL);
861}
862
Radek Krejci8b764662018-11-14 14:15:13 +0100863static void
864test_type_enum(void **state)
865{
866 *state = test_type_enum;
867
868 struct ly_ctx *ctx;
869 struct lys_module *mod;
870 struct lysc_type *type;
871
872 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
873
Radek Krejci027d5802018-11-14 16:57:28 +0100874 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 +0100875 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
876 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
877 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
878 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
879 assert_non_null(type);
880 assert_int_equal(LY_TYPE_ENUM, type->basetype);
881 assert_non_null(((struct lysc_type_enum*)type)->enums);
882 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
883 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
884 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
885 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
886 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
887 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
888 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
889 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
890 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
891 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
892 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
893 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
894 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
895 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
896
Radek Krejci027d5802018-11-14 16:57:28 +0100897 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 {"
898 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +0100899 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
900 LYS_IN_YANG));
901 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
902 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
903 assert_non_null(type);
904 assert_int_equal(LY_TYPE_ENUM, type->basetype);
905 assert_non_null(((struct lysc_type_enum*)type)->enums);
906 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
907 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
908 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
909 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
910 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
911
912
913 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +0100914 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
915 "enum one {if-feature f;}}}}", LYS_IN_YANG));
916 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 +0100917 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
918 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
919 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
920 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
921 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
922 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
923 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
924 "enum one; enum one;}}}", LYS_IN_YANG));
925 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
926 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
927 "enum '';}}}", LYS_IN_YANG));
928 logbuf_assert("Enum name must not be zero-length. Line number 1.");
929 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
930 "enum ' x';}}}", LYS_IN_YANG));
931 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
932 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
933 "enum 'x ';}}}", LYS_IN_YANG));
934 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
935 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
936 "enum 'inva\nlid';}}}", LYS_IN_YANG));
937 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
938
939 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
940 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
941 logbuf_assert("Missing enum substatement for enumeration type.");
942
Radek Krejci027d5802018-11-14 16:57:28 +0100943 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 +0100944 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
945 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
946 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
947
Radek Krejci027d5802018-11-14 16:57:28 +0100948 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 +0100949 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
950 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
951 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
952 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;}}}",
953 LYS_IN_YANG));
954 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
955 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
956
957 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;}}}}",
958 LYS_IN_YANG));
959 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
960 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
961
Radek Krejci04bc1e92018-11-14 14:28:13 +0100962 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
963 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
964 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100965 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +0100966
Radek Krejci027d5802018-11-14 16:57:28 +0100967 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
968 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
969 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
970 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
971
Radek Krejci8b764662018-11-14 14:15:13 +0100972 *state = NULL;
973 ly_ctx_destroy(ctx, NULL);
974}
975
976static void
977test_type_bits(void **state)
978{
979 *state = test_type_bits;
980
981 struct ly_ctx *ctx;
982 struct lys_module *mod;
983 struct lysc_type *type;
984
985 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
986
Radek Krejci027d5802018-11-14 16:57:28 +0100987 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 +0100988 "bit automin; bit one {if-feature f; position 1;}"
989 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
990 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
991 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
992 assert_non_null(type);
993 assert_int_equal(LY_TYPE_BITS, type->basetype);
994 assert_non_null(((struct lysc_type_bits*)type)->bits);
995 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
996 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
997 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
998 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
999 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1000 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1001 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1002 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1003 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1004 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1005 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1006 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1007
Radek Krejci027d5802018-11-14 16:57:28 +01001008 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 {"
1009 "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 +01001010 LYS_IN_YANG));
1011 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1012 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1013 assert_non_null(type);
1014 assert_int_equal(LY_TYPE_BITS, type->basetype);
1015 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001016 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1017 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1018 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1019 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1020 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1021 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1022 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001023
1024
1025 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001026 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1027 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1028 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 +01001029 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1030 "bit one {position -1;}}}}", LYS_IN_YANG));
1031 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1032 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1033 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1034 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1035 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1036 "bit one; bit one;}}}", LYS_IN_YANG));
1037 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1038 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1039 "bit '11';}}}", LYS_IN_YANG));
1040 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1041 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1042 "bit 'x1$1';}}}", LYS_IN_YANG));
1043 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1044
1045 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
1046 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1047 logbuf_assert("Missing bit substatement for bits type.");
1048
Radek Krejci027d5802018-11-14 16:57:28 +01001049 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 +01001050 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
1051 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1052 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1053
Radek Krejci027d5802018-11-14 16:57:28 +01001054 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 +01001055 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
1056 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1057 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
1058 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;}}}",
1059 LYS_IN_YANG));
1060 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1061 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1062
1063 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;}}}}",
1064 LYS_IN_YANG));
1065 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1066 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1067
Radek Krejci04bc1e92018-11-14 14:28:13 +01001068 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1069 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1070 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001071 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001072
Radek Krejci027d5802018-11-14 16:57:28 +01001073 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1074 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1075 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1076 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1077
Radek Krejci8b764662018-11-14 14:15:13 +01001078 *state = NULL;
1079 ly_ctx_destroy(ctx, NULL);
1080}
1081
Radek Krejci6cba4292018-11-15 17:33:29 +01001082static void
1083test_type_dec64(void **state)
1084{
1085 *state = test_type_dec64;
1086
1087 struct ly_ctx *ctx;
1088 struct lys_module *mod;
1089 struct lysc_type *type;
1090
1091 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1092
1093 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1094 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
1095 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1096 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1097 assert_non_null(type);
1098 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1099 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1100 assert_non_null(((struct lysc_type_dec*)type)->range);
1101 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1102 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1103 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1104 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1105
1106 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1107 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1108 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1109 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1110 assert_non_null(type);
1111 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1112 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1113 assert_non_null(((struct lysc_type_dec*)type)->range);
1114 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1115 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1116 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1117 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1118 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1119 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1120 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1121 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1122
1123 /* invalid cases */
1124 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1125 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1126 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1127 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1128 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1129 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1130
1131 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
1132 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1133 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1134
Radek Krejci643c8242018-11-15 17:51:11 +01001135 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));
1136 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001137 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001138
Radek Krejci6cba4292018-11-15 17:33:29 +01001139 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
1140 "range '3.142';}}}", LYS_IN_YANG));
1141 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1142 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1143
1144 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
1145 "range '4 | 3.14';}}}", LYS_IN_YANG));
1146 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1147 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1148
Radek Krejci643c8242018-11-15 17:51:11 +01001149 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
1150 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
1151 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1152 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1153
1154 assert_non_null(mod = lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
1155 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1156 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1157 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1158
Radek Krejci6cba4292018-11-15 17:33:29 +01001159 *state = NULL;
1160 ly_ctx_destroy(ctx, NULL);
1161}
1162
Radek Krejci16c0f822018-11-16 10:46:10 +01001163static void
1164test_type_instanceid(void **state)
1165{
1166 *state = test_type_instanceid;
1167
1168 struct ly_ctx *ctx;
1169 struct lys_module *mod;
1170 struct lysc_type *type;
1171
1172 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1173
1174 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1175 "leaf l1 {type instance-identifier {require-instance true;}}"
1176 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
1177 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1178 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1179 assert_non_null(type);
1180 assert_int_equal(LY_TYPE_INST, type->basetype);
1181 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1182
1183 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1184 assert_non_null(type);
1185 assert_int_equal(LY_TYPE_INST, type->basetype);
1186 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1187
1188 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1189 assert_non_null(type);
1190 assert_int_equal(LY_TYPE_INST, type->basetype);
1191 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1192
1193 /* invalid cases */
1194 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1195 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1196
1197 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));
1198 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1199 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1200
1201 *state = NULL;
1202 ly_ctx_destroy(ctx, NULL);
1203}
1204
Radek Krejci555cb5b2018-11-16 14:54:33 +01001205static void
1206test_type_identityref(void **state)
1207{
1208 *state = test_type_identityref;
1209
1210 struct ly_ctx *ctx;
1211 struct lys_module *mod;
1212 struct lysc_type *type;
1213
1214 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1215
1216 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;}"
1217 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001218 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001219 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1220 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1221 assert_non_null(type);
1222 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1223 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1224 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1225 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1226
1227 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1228 assert_non_null(type);
1229 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1230 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1231 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1232 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1233 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1234
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001235 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1236 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
1237 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1238 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1239 assert_non_null(type);
1240 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1241 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1242 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1243 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1244 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1245
Radek Krejci555cb5b2018-11-16 14:54:33 +01001246 /* invalid cases */
1247 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
1248 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1249 logbuf_assert("Missing base substatement for identityref type.");
1250
1251 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
1252 "leaf l {type mytype;}}", LYS_IN_YANG));
1253 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1254 logbuf_assert("Missing base substatement for identityref type mytype.");
1255
1256 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
1257 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
1258 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001259 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001260
1261 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
1262 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1263 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001264 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001265
1266 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
1267 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
1268 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1269 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1270
Radek Krejci322614f2018-11-16 15:05:44 +01001271 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));
1272 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1273 logbuf_assert("Unable to find base (j) of identityref.");
1274
1275 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));
1276 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1277 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1278
Radek Krejci555cb5b2018-11-16 14:54:33 +01001279 *state = NULL;
1280 ly_ctx_destroy(ctx, NULL);
1281}
1282
Radek Krejcia3045382018-11-22 14:30:31 +01001283static void
1284test_type_leafref(void **state)
1285{
1286 *state = test_type_leafref;
1287
1288 struct ly_ctx *ctx;
1289 struct lys_module *mod;
1290 struct lysc_type *type;
1291 const char *path, *name, *prefix;
1292 size_t prefix_len, name_len;
1293 int parent_times, has_predicate;
1294
1295 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1296
1297 /* lys_path_token() */
1298 path = "invalid_path";
1299 parent_times = 0;
1300 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1301 path = "..";
1302 parent_times = 0;
1303 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1304 path = "..[";
1305 parent_times = 0;
1306 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1307 path = "../";
1308 parent_times = 0;
1309 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1310 path = "/";
1311 parent_times = 0;
1312 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1313
1314 path = "../../pref:id/xxx[predicate]/invalid!!!";
1315 parent_times = 0;
1316 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1317 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1318 assert_int_equal(4, prefix_len);
1319 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1320 assert_int_equal(2, name_len);
1321 assert_int_equal(0, strncmp("id", name, name_len));
1322 assert_int_equal(2, parent_times);
1323 assert_int_equal(0, has_predicate);
1324 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1325 assert_string_equal("[predicate]/invalid!!!", path);
1326 assert_int_equal(0, prefix_len);
1327 assert_null(prefix);
1328 assert_int_equal(3, name_len);
1329 assert_int_equal(0, strncmp("xxx", name, name_len));
1330 assert_int_equal(1, has_predicate);
1331 path += 11;
1332 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1333 assert_string_equal("!!!", path);
1334 assert_int_equal(0, prefix_len);
1335 assert_null(prefix);
1336 assert_int_equal(7, name_len);
1337 assert_int_equal(0, strncmp("invalid", name, name_len));
1338
1339 path = "/absolute/prefix:path";
1340 parent_times = 0;
1341 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1342 assert_string_equal("/prefix:path", path);
1343 assert_int_equal(0, prefix_len);
1344 assert_null(prefix);
1345 assert_int_equal(8, name_len);
1346 assert_int_equal(0, strncmp("absolute", name, name_len));
1347 assert_int_equal(-1, parent_times);
1348 assert_int_equal(0, has_predicate);
1349 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1350 assert_int_equal(0, *path);
1351 assert_int_equal(6, prefix_len);
1352 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1353 assert_int_equal(4, name_len);
1354 assert_int_equal(0, strncmp("path", name, name_len));
1355 assert_int_equal(0, has_predicate);
1356
1357 /* complete leafref paths */
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001358 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;"
Radek Krejcia3045382018-11-22 14:30:31 +01001359 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1360 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
1361 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1362 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1363 assert_non_null(type);
1364 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1365 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001366 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001367 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1368 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001369 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1370 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1371 assert_non_null(type);
1372 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1373 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001374 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1375 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1376 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001377 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1378
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001379 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mytype {type leafref {path /b:target;}}"
Radek Krejci4ce68932018-11-28 12:53:36 +01001380 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001381 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001382 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1383 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1384 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001385 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001386 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1387 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1388 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001389 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1390 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001391 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1392
1393 /* prefixes are reversed to check using correct context of the path! */
1394 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 +01001395 "typedef mytype3 {type c:mytype {require-instance false;}}"
1396 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001397 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1398 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1399 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001400 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001401 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1402 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1403 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001404 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1405 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001406 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1407 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1408 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001409 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001410 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1411 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1412 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1413 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1414
Radek Krejci4ce68932018-11-28 12:53:36 +01001415 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1416 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1417 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
1418 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1419 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1420 assert_non_null(type);
1421 assert_int_equal(1, type->refcount);
1422 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1423 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1424 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1425 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1426 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1427 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1428
Radek Krejci58d171e2018-11-23 13:50:55 +01001429 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001430 assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;feature f1; feature f2;"
Radek Krejci58d171e2018-11-23 13:50:55 +01001431 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1432 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
1433 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1434 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1435 assert_non_null(type);
1436 assert_int_equal(1, type->refcount);
1437 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1438 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1439 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1440 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1441 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1442
Radek Krejcia3045382018-11-22 14:30:31 +01001443 /* TODO target in list with predicates */
1444
Radek Krejcia3045382018-11-22 14:30:31 +01001445 /* invalid paths */
1446 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
1447 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
1448 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1449 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
1450 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
1451 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
1452 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1453 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
1454 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
1455 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
1456 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1457 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
1458 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;}}"
1459 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
1460 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1461 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
1462 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
1463 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
1464 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1465 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
1466 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
1467 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
1468 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1469 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
1470 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
1471 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
1472 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1473 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci2f4a0292018-11-22 15:41:53 +01001474 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
1475 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
1476 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1477 logbuf_assert("Missing path substatement for leafref type.");
1478 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
1479 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
1480 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1481 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001482 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
1483 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
1484 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1485 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1486 "applicable to the leafref itself.");
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001487 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
1488 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
1489 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1490 logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001491
Radek Krejci412ddfa2018-11-23 11:44:11 +01001492 /* circular chain */
1493 assert_non_null(mod = lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
1494 "leaf ref1 {type leafref {path /ref2;}}"
1495 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001496 "leaf ref3 {type leafref {path /ref4;}}"
1497 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001498 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1499 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1500
Radek Krejcia3045382018-11-22 14:30:31 +01001501 *state = NULL;
1502 ly_ctx_destroy(ctx, NULL);
1503}
1504
Radek Krejci43699232018-11-23 14:59:46 +01001505static void
1506test_type_empty(void **state)
1507{
1508 *state = test_type_empty;
1509
1510 struct ly_ctx *ctx;
1511 struct lys_module *mod;
1512
1513 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1514
1515 /* invalid */
1516 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
1517 "leaf l {type empty; default x;}}", LYS_IN_YANG));
1518 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1519 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1520
1521 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
1522 "leaf l {type mytype;}}", LYS_IN_YANG));
1523 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1524 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1525
1526 *state = NULL;
1527 ly_ctx_destroy(ctx, NULL);
1528}
1529
Radek Krejcicdfecd92018-11-26 11:27:32 +01001530
1531static void
1532test_type_union(void **state)
1533{
1534 *state = test_type_union;
1535
1536 struct ly_ctx *ctx;
1537 struct lys_module *mod;
1538 struct lysc_type *type;
1539
1540 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1541
1542 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
1543 "typedef mytype {type union {type int8; type mybasetype;}}"
1544 "leaf l {type mytype;}}", LYS_IN_YANG));
1545 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1546 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1547 assert_non_null(type);
1548 assert_int_equal(2, type->refcount);
1549 assert_int_equal(LY_TYPE_UNION, type->basetype);
1550 assert_non_null(((struct lysc_type_union*)type)->types);
1551 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1552 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
1553 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1554
1555 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
1556 "typedef mytype {type union {type int8; type mybasetype;}}"
1557 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
1558 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1559 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1560 assert_non_null(type);
1561 assert_int_equal(1, type->refcount);
1562 assert_int_equal(LY_TYPE_UNION, type->basetype);
1563 assert_non_null(((struct lysc_type_union*)type)->types);
1564 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1565 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1566 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
1567 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1568
1569 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
1570 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001571 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
1572 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01001573 LYS_IN_YANG));
1574 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1575 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1576 assert_non_null(type);
1577 assert_int_equal(1, type->refcount);
1578 assert_int_equal(LY_TYPE_UNION, type->basetype);
1579 assert_non_null(((struct lysc_type_union*)type)->types);
1580 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1581 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1582 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
1583 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1584 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
1585 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
1586
Radek Krejci6be5ff12018-11-26 13:18:15 +01001587 /* invalid unions */
1588 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
1589 "leaf l {type mytype;}}", LYS_IN_YANG));
1590 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1591 logbuf_assert("Missing type substatement for union type mytype.");
1592
1593 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
1594 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1595 logbuf_assert("Missing type substatement for union type.");
1596
1597 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
1598 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
1599 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1600 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
1601
1602 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
1603 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1604 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1605 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
1606
Radek Krejcicdfecd92018-11-26 11:27:32 +01001607 *state = NULL;
1608 ly_ctx_destroy(ctx, NULL);
1609}
1610
1611static void
1612test_type_dflt(void **state)
1613{
1614 *state = test_type_union;
1615
1616 struct ly_ctx *ctx;
1617 struct lys_module *mod;
1618 struct lysc_type *type;
1619
1620 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1621
1622 /* default is not inherited from union's types */
1623 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
1624 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
1625 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1626 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1627 assert_non_null(type);
1628 assert_int_equal(1, type->refcount);
1629 assert_int_equal(LY_TYPE_UNION, type->basetype);
1630 assert_non_null(((struct lysc_type_union*)type)->types);
1631 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1632 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1633 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1634 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1635 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
1636
1637 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
1638 "leaf l {type mybasetype;}}", LYS_IN_YANG));
1639 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1640 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1641 assert_non_null(type);
1642 assert_int_equal(2, type->refcount);
1643 assert_int_equal(LY_TYPE_STRING, type->basetype);
1644 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1645 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1646
1647 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
1648 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
1649 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1650 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1651 assert_non_null(type);
1652 assert_int_equal(2, type->refcount);
1653 assert_int_equal(LY_TYPE_STRING, type->basetype);
1654 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1655 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1656
Radek Krejci6be5ff12018-11-26 13:18:15 +01001657 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
1658 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
1659 "leaf l2 {type mytype;}}", LYS_IN_YANG));
1660 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1661 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1662 assert_non_null(type);
1663 assert_int_equal(4, type->refcount);
1664 assert_int_equal(LY_TYPE_STRING, type->basetype);
1665 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1666 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1667 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1668 assert_non_null(type);
1669 assert_int_equal(4, type->refcount);
1670 assert_int_equal(LY_TYPE_STRING, type->basetype);
1671 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
1672 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
1673
1674 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
1675 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
1676 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1677 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1678 assert_non_null(type);
1679 assert_int_equal(3, type->refcount);
1680 assert_int_equal(LY_TYPE_STRING, type->basetype);
1681 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1682 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1683
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001684 /* mandatory leaf does not takes default value from type */
1685 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
1686 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
1687 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1688 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1689 assert_non_null(type);
1690 assert_int_equal(LY_TYPE_STRING, type->basetype);
1691 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1692 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1693
Radek Krejcicdfecd92018-11-26 11:27:32 +01001694 *state = NULL;
1695 ly_ctx_destroy(ctx, NULL);
1696}
1697
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001698int main(void)
1699{
1700 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01001701 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
1702 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
1703 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01001704 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
1705 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01001706 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01001707 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
1708 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01001709 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01001710 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01001711 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01001712 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01001713 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01001714 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
1715 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01001716 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01001717 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001718 };
1719
1720 return cmocka_run_group_tests(tests, NULL, NULL);
1721}