blob: 75df59749648f4675755174c3ed8338d2f7b95e4 [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
Radek Krejci6bb080b2018-11-28 17:23:41 +0100412 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf-list ll {type string;}}", LYS_IN_YANG));
Radek Krejci0e5d8382018-11-28 16:37:53 +0100413 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
Radek Krejci6bb080b2018-11-28 17:23:41 +0100419 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;typedef mytype {type int8;default 10;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100420 "leaf-list ll1 {type mytype;default 1; default 1; config false;}"
Radek Krejcia6d57732018-11-29 13:40:37 +0100421 "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG));
Radek Krejci6bb080b2018-11-28 17:23:41 +0100422 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
423 assert_non_null(mod->compiled);
424 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
425 assert_non_null(ll->dflts);
426 assert_int_equal(3, ll->type->refcount);
427 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
428 assert_string_equal("1", ll->dflts[0]);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100429 assert_string_equal("1", ll->dflts[1]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100430 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100431 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next));
432 assert_non_null(ll->dflts);
433 assert_int_equal(3, ll->type->refcount);
434 assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts));
435 assert_string_equal("10", ll->dflts[0]);
Radek Krejcia6d57732018-11-29 13:40:37 +0100436 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags);
437
438 /* ordered-by is ignored for state data, RPC/action output parameters and notification content
439 * TODO test also, RPC output parameters and notification content */
440 assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;"
441 "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG));
442 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
443 /* but warning is present: */
444 logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ().");
445 assert_non_null(mod->compiled);
446 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
447 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags);
Radek Krejci6bb080b2018-11-28 17:23:41 +0100448
449 /* invalid */
450 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG));
451 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
452 logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
453
454 assert_non_null(mod = lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG));
455 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
456 logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x).");
457
458 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;"
459 "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG));
460 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
461 assert_non_null(mod->compiled);
462 assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data));
463 assert_non_null(ll->dflts);
464 assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts));
465 assert_non_null(mod = lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;"
466 "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG));
467 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
468 logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\".");
469
Radek Krejci0e5d8382018-11-28 16:37:53 +0100470 *state = NULL;
471 ly_ctx_destroy(ctx, NULL);
472}
473
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100474static void
475test_node_list(void **state)
476{
477 *state = test_node_list;
478
479 struct ly_ctx *ctx;
480 struct lys_module *mod;
481 struct lysc_node_list *list;
482
483 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
484
485 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;"
486 "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}"
487 "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG));
488 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
489 list = (struct lysc_node_list*)mod->compiled->data;
490 assert_non_null(list);
491 assert_non_null(list->keys);
492 assert_int_equal(2, LY_ARRAY_SIZE(list->keys));
493 assert_string_equal("x", list->keys[0]->name);
494 assert_string_equal("y", list->keys[1]->name);
495 assert_non_null(list->child);
496 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags);
497 assert_true(list->child->flags & LYS_KEY);
498 assert_true(list->child->next->flags & LYS_KEY);
499 list = (struct lysc_node_list*)mod->compiled->data->next;
500 assert_non_null(list);
501 assert_null(list->keys);
502 assert_non_null(list->child);
503 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags);
504 assert_false(list->child->flags & LYS_KEY);
505
506 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;"
507 "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";"
Radek Krejci665421c2018-12-05 08:03:39 +0100508 "leaf a {type string; default x;} leaf d {type string;config false;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100509 "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG));
510 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
511 list = (struct lysc_node_list*)mod->compiled->data;
512 assert_non_null(list);
513 assert_string_equal("l", list->name);
514 assert_non_null(list->keys);
515 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
516 assert_string_equal("a", list->keys[0]->name);
517 assert_true(list->keys[0]->flags & LYS_KEY);
Radek Krejci665421c2018-12-05 08:03:39 +0100518 assert_null(list->keys[0]->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100519 assert_non_null(list->uniques);
520 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques));
521 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0]));
522 assert_string_equal("a", list->uniques[0][0]->name);
523 assert_true(list->uniques[0][0]->flags & LYS_UNIQUE);
524 assert_string_equal("b", list->uniques[0][1]->name);
525 assert_true(list->uniques[0][1]->flags & LYS_UNIQUE);
526 assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1]));
527 assert_string_equal("e", list->uniques[1][0]->name);
528 assert_true(list->uniques[1][0]->flags & LYS_UNIQUE);
529 assert_string_equal("d", list->uniques[1][1]->name);
530 assert_true(list->uniques[1][1]->flags & LYS_UNIQUE);
531
Radek Krejci665421c2018-12-05 08:03:39 +0100532 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;"
533 "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG));
534 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
535 list = (struct lysc_node_list*)mod->compiled->data;
536 assert_non_null(list);
537 assert_string_equal("l", list->name);
538 assert_non_null(list->keys);
539 assert_int_equal(1, LY_ARRAY_SIZE(list->keys));
540 assert_string_equal("a", list->keys[0]->name);
541 assert_true(list->keys[0]->flags & LYS_KEY);
542 assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype);
543
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100544 /* invalid */
545 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG));
546 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
547 logbuf_assert("Missing key in list representing configuration data.");
548
549 assert_non_null(mod = lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;"
550 "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG));
551 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
552 logbuf_assert("List's key \"x\" must not have any \"when\" statement.");
553
554 assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;"
555 "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG));
556 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
557 logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement.");
558
559 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;"
560 "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG));
561 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
562 logbuf_assert("Key of the configuration list must not be status leaf.");
563
564 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;"
565 "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG));
566 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
567 logbuf_assert("Configuration node cannot be child of any state data node.");
568
569 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;"
570 "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG));
571 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
572 logbuf_assert("The list's key \"x\" not found.");
573
574 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;"
575 "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG));
576 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
577 logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf.");
578
579 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
580 "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG));
581 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
582 logbuf_assert("Unique statement \"x y\" refers to leafs with different config type.");
583
Radek Krejci665421c2018-12-05 08:03:39 +0100584 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;"
585 "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG));
586 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
587 logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\".");
588
589 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;"
590 "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
591 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
592 logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found.");
593
594 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
595 "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG));
596 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
597 logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator.");
598
599 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
600 "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG));
601 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
602 logbuf_assert("Duplicated key identifier \"x\".");
603
604 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;"
605 "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG));
606 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
607 logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
608
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100609 *state = NULL;
610 ly_ctx_destroy(ctx, NULL);
611}
612
Radek Krejci0f07bed2018-11-13 14:01:40 +0100613/**
614 * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types,
615 * do not test the expression itself
616 */
Radek Krejci4f28eda2018-11-12 11:46:16 +0100617static void
Radek Krejci0f07bed2018-11-13 14:01:40 +0100618test_type_range(void **state)
Radek Krejci4f28eda2018-11-12 11:46:16 +0100619{
Radek Krejci0f07bed2018-11-13 14:01:40 +0100620 *state = test_type_range;
621
622 struct ly_ctx *ctx;
623 struct lys_module *mod;
624 struct lysc_type *type;
625
626 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
627
628 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));
629 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
630 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
631 assert_non_null(type);
632 assert_int_equal(LY_TYPE_INT8, type->basetype);
633 assert_non_null(((struct lysc_type_num*)type)->range);
634 assert_non_null(((struct lysc_type_num*)type)->range->parts);
635 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
636 assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64);
637 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
638 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64);
639 assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64);
640
641 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));
642 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
643 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
644 assert_non_null(type);
645 assert_int_equal(LY_TYPE_INT16, type->basetype);
646 assert_non_null(((struct lysc_type_num*)type)->range);
647 assert_non_null(((struct lysc_type_num*)type)->range->parts);
648 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
649 assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64);
650 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
651 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64);
652 assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64);
653
654 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));
655 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
656 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
657 assert_non_null(type);
658 assert_int_equal(LY_TYPE_INT32, type->basetype);
659 assert_non_null(((struct lysc_type_num*)type)->range);
660 assert_non_null(((struct lysc_type_num*)type)->range->parts);
661 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
662 assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64);
663 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
664 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64);
665 assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64);
666
667 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));
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_int_equal(LY_TYPE_INT64, type->basetype);
672 assert_non_null(((struct lysc_type_num*)type)->range);
673 assert_non_null(((struct lysc_type_num*)type)->range->parts);
674 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
675 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64);
676 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64);
677 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64);
678 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64);
679
680 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));
681 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
682 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
683 assert_non_null(type);
684 assert_int_equal(LY_TYPE_UINT8, type->basetype);
685 assert_non_null(((struct lysc_type_num*)type)->range);
686 assert_non_null(((struct lysc_type_num*)type)->range->parts);
687 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
688 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
689 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
690 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
691 assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
692
693 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));
694 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
695 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
696 assert_non_null(type);
697 assert_int_equal(LY_TYPE_UINT16, type->basetype);
698 assert_non_null(((struct lysc_type_num*)type)->range);
699 assert_non_null(((struct lysc_type_num*)type)->range->parts);
700 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
701 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
702 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
703 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64);
704 assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64);
705
706 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));
707 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
708 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
709 assert_non_null(type);
710 assert_int_equal(LY_TYPE_UINT32, type->basetype);
711 assert_non_null(((struct lysc_type_num*)type)->range);
712 assert_non_null(((struct lysc_type_num*)type)->range->parts);
713 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
714 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
715 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
716 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
717 assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
718
719 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));
720 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
721 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
722 assert_non_null(type);
723 assert_int_equal(LY_TYPE_UINT64, type->basetype);
724 assert_non_null(((struct lysc_type_num*)type)->range);
725 assert_non_null(((struct lysc_type_num*)type)->range->parts);
726 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
727 assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64);
728 assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64);
729 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64);
730 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64);
731
732 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}"
733 "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG));
734 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
735 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
736 assert_non_null(type);
737 assert_int_equal(3, type->refcount);
738 assert_int_equal(LY_TYPE_UINT8, type->basetype);
739 assert_non_null(((struct lysc_type_num*)type)->range);
740 assert_non_null(((struct lysc_type_num*)type)->range->parts);
741 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts));
742
743 *state = NULL;
744 ly_ctx_destroy(ctx, NULL);
745}
746
747static void
748test_type_length(void **state)
749{
750 *state = test_type_length;
Radek Krejci4f28eda2018-11-12 11:46:16 +0100751
752 struct ly_ctx *ctx;
753 struct lys_module *mod;
754 struct lysc_type *type;
755
756 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
757
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100758 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 +0100759 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
760 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
761 assert_non_null(type);
762 assert_non_null(((struct lysc_type_bin*)type)->length);
763 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
Radek Krejcic5ddcc02018-11-12 13:28:18 +0100764 assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag);
765 assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100766 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
767 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
768 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
769
770 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG));
771 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
772 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
773 assert_non_null(type);
774 assert_non_null(((struct lysc_type_bin*)type)->length);
775 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
776 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
Radek Krejci0fe20752018-11-27 11:33:24 +0100777 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
778 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100779
780 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));
781 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
782 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
783 assert_non_null(type);
784 assert_non_null(((struct lysc_type_bin*)type)->length);
785 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
786 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
787 assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci0fe20752018-11-27 11:33:24 +0100788 assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejci4f28eda2018-11-12 11:46:16 +0100789
790 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG));
791 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
792 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
793 assert_non_null(type);
794 assert_non_null(((struct lysc_type_bin*)type)->length);
795 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
796 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
797 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
798 assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
799
800 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));
801 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
802 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
803 assert_non_null(type);
804 assert_non_null(((struct lysc_type_bin*)type)->length);
805 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
806 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
807 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
808 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
809
810 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));
811 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
812 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
813 assert_non_null(type);
814 assert_non_null(((struct lysc_type_bin*)type)->length);
815 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
816 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
817 assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
818 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
819 assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
820 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
821
822 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));
823 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
824 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
825 assert_non_null(type);
826 assert_non_null(((struct lysc_type_bin*)type)->length);
827 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
828 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
829 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
830 assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
831 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
832 assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
833
Radek Krejcib31c8992018-11-12 16:29:16 +0100834 assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}"
835 "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG));
836 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
837 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
838 assert_non_null(type);
839 assert_non_null(((struct lysc_type_bin*)type)->length);
840 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
841 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
842 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
843 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
844
845 assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}"
846 "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG));
847 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
848 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
849 assert_non_null(type);
850 assert_non_null(((struct lysc_type_bin*)type)->length);
851 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
852 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
853 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
854 assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
855
856 assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}"
857 "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG));
858 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
859 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
860 assert_non_null(type);
861 assert_non_null(((struct lysc_type_bin*)type)->length);
862 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
863 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
864 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
865 assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
866 assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64);
867 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64);
868
869 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 +0100870 "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG));
Radek Krejcib31c8992018-11-12 16:29:16 +0100871 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
872 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
873 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100874 assert_int_equal(1, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100875 assert_non_null(((struct lysc_type_bin*)type)->length);
876 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
877 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
878 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100879 assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
Radek Krejcib31c8992018-11-12 16:29:16 +0100880 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
881 assert_non_null(type);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100882 assert_int_equal(2, type->refcount);
Radek Krejcib31c8992018-11-12 16:29:16 +0100883 assert_non_null(((struct lysc_type_bin*)type)->length);
884 assert_non_null(((struct lysc_type_bin*)type)->length->parts);
885 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts));
886 assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64);
887 assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64);
888
Radek Krejci56aa27c2018-11-13 14:21:59 +0100889 assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}"
890 "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
Radek Krejci9a191e62018-11-13 13:19:56 +0100891 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
892 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
893 assert_non_null(type);
894 assert_int_equal(LY_TYPE_STRING, type->basetype);
Radek Krejci56aa27c2018-11-13 14:21:59 +0100895 assert_int_equal(1, type->refcount);
Radek Krejcicda74152018-11-13 15:27:32 +0100896 assert_non_null(((struct lysc_type_str*)type)->length);
897 assert_non_null(((struct lysc_type_str*)type)->length->parts);
898 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
899 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
900 assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
Radek Krejci9a191e62018-11-13 13:19:56 +0100901
Radek Krejci5969f272018-11-23 10:03:58 +0100902 assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}"
903 "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG));
904 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
905 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
906 assert_non_null(type);
907 assert_int_equal(LY_TYPE_STRING, type->basetype);
908 assert_int_equal(1, type->refcount);
909 assert_non_null(((struct lysc_type_str*)type)->length);
910 assert_non_null(((struct lysc_type_str*)type)->length->parts);
911 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts));
912 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64);
913 assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64);
914
Radek Krejci4f28eda2018-11-12 11:46:16 +0100915 /* invalid values */
916 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG));
917 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
918 logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations.");
919 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG));
920 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
921 logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\".");
922 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));
923 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
924 logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10).");
925 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));
926 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
927 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
928 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));
929 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
930 logbuf_assert("Invalid length restriction - values are not in ascending order (10).");
931 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG));
932 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
933 logbuf_assert("Invalid length restriction - unexpected data (x).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100934 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));
935 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
936 logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | ).");
937 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG));
938 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
939 logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50).");
940 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG));
941 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
942 logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..).");
943 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG));
944 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
945 logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound.");
946 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG));
947 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
948 logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |).");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100949 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));
950 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
951 logbuf_assert("Invalid length restriction - values are not in ascending order (15).");
Radek Krejcib31c8992018-11-12 16:29:16 +0100952
953 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}"
954 "leaf l {type mytype {length 11;}}}", LYS_IN_YANG));
955 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
956 logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting.");
957 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}"
958 "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG));
959 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
960 logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting.");
961 assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}"
962 "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG));
963 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
964 logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting.");
965 assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}"
966 "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG));
967 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
968 logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting.");
969 assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}"
970 "leaf l {type mytype {length 15;}}}", LYS_IN_YANG));
971 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
972 logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting.");
973 assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}"
974 "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG));
975 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
976 logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting.");
Radek Krejci6489bbe2018-11-12 17:05:19 +0100977 assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}"
978 "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG));
979 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
980 logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting.");
Radek Krejcib31c8992018-11-12 16:29:16 +0100981
Radek Krejci495903e2018-11-13 11:30:45 +0100982 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));
983 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
984 logbuf_assert("Invalid type restrictions for binary type.");
985
Radek Krejci4f28eda2018-11-12 11:46:16 +0100986 *state = NULL;
987 ly_ctx_destroy(ctx, NULL);
988}
989
Radek Krejcicda74152018-11-13 15:27:32 +0100990static void
991test_type_pattern(void **state)
992{
993 *state = test_type_pattern;
994
995 struct ly_ctx *ctx;
996 struct lys_module *mod;
997 struct lysc_type *type;
998
999 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1000
Radek Krejci027d5802018-11-14 16:57:28 +01001001 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 +01001002 "pattern .* {error-app-tag errortag;error-message error;}"
1003 "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG));
1004 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1005 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1006 assert_non_null(type);
1007 assert_non_null(((struct lysc_type_str*)type)->patterns);
1008 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1009 assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag);
1010 assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg);
1011 assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted);
1012 assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag);
1013 assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg);
1014 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted);
1015
1016 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}"
1017 "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG));
1018 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1019 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1020 assert_non_null(type);
1021 assert_int_equal(LY_TYPE_STRING, type->basetype);
1022 assert_int_equal(1, type->refcount);
1023 assert_non_null(((struct lysc_type_str*)type)->patterns);
1024 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1025 assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1026 assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount);
1027
Radek Krejci04bc1e92018-11-14 14:28:13 +01001028 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}"
1029 "leaf l {type mytype {length 10;}}}", LYS_IN_YANG));
1030 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1031 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1032 assert_non_null(type);
1033 assert_int_equal(LY_TYPE_STRING, type->basetype);
1034 assert_int_equal(1, type->refcount);
1035 assert_non_null(((struct lysc_type_str*)type)->patterns);
1036 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1037 assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount);
1038
Radek Krejcicda74152018-11-13 15:27:32 +01001039 /* test substitutions */
Radek Krejci04bc1e92018-11-14 14:28:13 +01001040 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 +01001041 "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG));
1042 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1043 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1044 assert_non_null(type);
1045 assert_non_null(((struct lysc_type_str*)type)->patterns);
1046 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns));
1047 /* TODO check some data "^Å™$" */
1048
1049 *state = NULL;
1050 ly_ctx_destroy(ctx, NULL);
1051}
1052
Radek Krejci8b764662018-11-14 14:15:13 +01001053static void
1054test_type_enum(void **state)
1055{
1056 *state = test_type_enum;
1057
1058 struct ly_ctx *ctx;
1059 struct lys_module *mod;
1060 struct lysc_type *type;
1061
1062 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1063
Radek Krejci027d5802018-11-14 16:57:28 +01001064 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 +01001065 "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}"
1066 "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG));
1067 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1068 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1069 assert_non_null(type);
1070 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1071 assert_non_null(((struct lysc_type_enum*)type)->enums);
1072 assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1073 assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures);
1074 assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name);
1075 assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value);
1076 assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name);
1077 assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value);
1078 assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name);
1079 assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value);
1080 assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name);
1081 assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value);
1082 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name);
1083 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value);
1084 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name);
1085 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value);
1086
Radek Krejci027d5802018-11-14 16:57:28 +01001087 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 {"
1088 "enum 11; enum min {value -2147483648;}enum x$&;"
Radek Krejci8b764662018-11-14 14:15:13 +01001089 "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}",
1090 LYS_IN_YANG));
1091 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1092 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1093 assert_non_null(type);
1094 assert_int_equal(LY_TYPE_ENUM, type->basetype);
1095 assert_non_null(((struct lysc_type_enum*)type)->enums);
1096 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums));
1097 assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name);
1098 assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value);
1099 assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name);
1100 assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value);
1101
1102
1103 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001104 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {"
1105 "enum one {if-feature f;}}}}", LYS_IN_YANG));
1106 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 +01001107 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1108 "enum one {value -2147483649;}}}}", LYS_IN_YANG));
1109 logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1.");
1110 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1111 "enum one {value 2147483648;}}}}", LYS_IN_YANG));
1112 logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1.");
1113 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1114 "enum one; enum one;}}}", LYS_IN_YANG));
1115 logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1.");
1116 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1117 "enum '';}}}", LYS_IN_YANG));
1118 logbuf_assert("Enum name must not be zero-length. Line number 1.");
1119 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1120 "enum ' x';}}}", LYS_IN_YANG));
1121 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1.");
1122 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1123 "enum 'x ';}}}", LYS_IN_YANG));
1124 logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1.");
1125 assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {"
1126 "enum 'inva\nlid';}}}", LYS_IN_YANG));
1127 logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5).");
1128
1129 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG));
1130 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1131 logbuf_assert("Missing enum substatement for enumeration type.");
1132
Radek Krejci027d5802018-11-14 16:57:28 +01001133 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 +01001134 "leaf l {type mytype {enum two;}}}", LYS_IN_YANG));
1135 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1136 logbuf_assert("Invalid enumeration - derived type adds new item \"two\".");
1137
Radek Krejci027d5802018-11-14 16:57:28 +01001138 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 +01001139 "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG));
1140 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1141 logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type.");
1142 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;}}}",
1143 LYS_IN_YANG));
1144 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1145 logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647.");
1146
1147 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;}}}}",
1148 LYS_IN_YANG));
1149 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1150 logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\".");
1151
Radek Krejci04bc1e92018-11-14 14:28:13 +01001152 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}"
1153 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1154 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001155 logbuf_assert("Missing enum substatement for enumeration type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001156
Radek Krejci027d5802018-11-14 16:57:28 +01001157 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}"
1158 "leaf l {type mytype {enum one;}}}", LYS_IN_YANG));
1159 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1160 logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules.");
1161
Radek Krejci8b764662018-11-14 14:15:13 +01001162 *state = NULL;
1163 ly_ctx_destroy(ctx, NULL);
1164}
1165
1166static void
1167test_type_bits(void **state)
1168{
1169 *state = test_type_bits;
1170
1171 struct ly_ctx *ctx;
1172 struct lys_module *mod;
1173 struct lysc_type *type;
1174
1175 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1176
Radek Krejci027d5802018-11-14 16:57:28 +01001177 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 +01001178 "bit automin; bit one {if-feature f; position 1;}"
1179 "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG));
1180 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1181 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1182 assert_non_null(type);
1183 assert_int_equal(LY_TYPE_BITS, type->basetype);
1184 assert_non_null(((struct lysc_type_bits*)type)->bits);
1185 assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1186 assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures);
1187 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1188 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1189 assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name);
1190 assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position);
1191 assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name);
1192 assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position);
1193 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name);
1194 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position);
1195 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name);
1196 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position);
1197
Radek Krejci027d5802018-11-14 16:57:28 +01001198 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 {"
1199 "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 +01001200 LYS_IN_YANG));
1201 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1202 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1203 assert_non_null(type);
1204 assert_int_equal(LY_TYPE_BITS, type->basetype);
1205 assert_non_null(((struct lysc_type_bits*)type)->bits);
Radek Krejci027d5802018-11-14 16:57:28 +01001206 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits));
1207 assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name);
1208 assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position);
1209 assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name);
1210 assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position);
1211 assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name);
1212 assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position);
Radek Krejci8b764662018-11-14 14:15:13 +01001213
1214
1215 /* invalid cases */
Radek Krejci027d5802018-11-14 16:57:28 +01001216 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {"
1217 "bit one {if-feature f;}}}}", LYS_IN_YANG));
1218 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 +01001219 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1220 "bit one {position -1;}}}}", LYS_IN_YANG));
1221 logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1.");
1222 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1223 "bit one {value 4294967296;}}}}", LYS_IN_YANG));
1224 logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1.");
1225 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1226 "bit one; bit one;}}}", LYS_IN_YANG));
1227 logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1.");
1228 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1229 "bit '11';}}}", LYS_IN_YANG));
1230 logbuf_assert("Invalid identifier first character '1'. Line number 1.");
1231 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {"
1232 "bit 'x1$1';}}}", LYS_IN_YANG));
1233 logbuf_assert("Invalid identifier character '$'. Line number 1.");
1234
1235 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG));
1236 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1237 logbuf_assert("Missing bit substatement for bits type.");
1238
Radek Krejci027d5802018-11-14 16:57:28 +01001239 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 +01001240 "leaf l {type mytype {bit two;}}}", LYS_IN_YANG));
1241 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1242 logbuf_assert("Invalid bits - derived type adds new item \"two\".");
1243
Radek Krejci027d5802018-11-14 16:57:28 +01001244 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 +01001245 "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG));
1246 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1247 logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type.");
1248 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;}}}",
1249 LYS_IN_YANG));
1250 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1251 logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295.");
1252
1253 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;}}}}",
1254 LYS_IN_YANG));
1255 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1256 logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\".");
1257
Radek Krejci04bc1e92018-11-14 14:28:13 +01001258 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}"
1259 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1260 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001261 logbuf_assert("Missing bit substatement for bits type mytype.");
Radek Krejci04bc1e92018-11-14 14:28:13 +01001262
Radek Krejci027d5802018-11-14 16:57:28 +01001263 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}"
1264 "leaf l {type mytype {bit one;}}}", LYS_IN_YANG));
1265 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1266 logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules.");
1267
Radek Krejci8b764662018-11-14 14:15:13 +01001268 *state = NULL;
1269 ly_ctx_destroy(ctx, NULL);
1270}
1271
Radek Krejci6cba4292018-11-15 17:33:29 +01001272static void
1273test_type_dec64(void **state)
1274{
1275 *state = test_type_dec64;
1276
1277 struct ly_ctx *ctx;
1278 struct lys_module *mod;
1279 struct lysc_type *type;
1280
1281 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1282
1283 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {"
1284 "fraction-digits 2;range min..max;}}}", LYS_IN_YANG));
1285 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1286 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1287 assert_non_null(type);
1288 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1289 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1290 assert_non_null(((struct lysc_type_dec*)type)->range);
1291 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1292 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1293 assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1294 assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1295
1296 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {"
1297 "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG));
1298 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1299 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1300 assert_non_null(type);
1301 assert_int_equal(LY_TYPE_DEC64, type->basetype);
1302 assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits);
1303 assert_non_null(((struct lysc_type_dec*)type)->range);
1304 assert_non_null(((struct lysc_type_dec*)type)->range->parts);
1305 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts));
1306 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64);
1307 assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64);
1308 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64);
1309 assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64);
1310 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64);
1311 assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64);
1312
1313 /* invalid cases */
1314 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG));
1315 logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1.");
1316 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG));
1317 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1318 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG));
1319 logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1.");
1320
1321 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG));
1322 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1323 logbuf_assert("Missing fraction-digits substatement for decimal64 type.");
1324
Radek Krejci643c8242018-11-15 17:51:11 +01001325 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));
1326 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001327 logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype.");
Radek Krejci643c8242018-11-15 17:51:11 +01001328
Radek Krejci6cba4292018-11-15 17:33:29 +01001329 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;"
1330 "range '3.142';}}}", LYS_IN_YANG));
1331 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1332 logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits.");
1333
1334 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;"
1335 "range '4 | 3.14';}}}", LYS_IN_YANG));
1336 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1337 logbuf_assert("Invalid range restriction - values are not in ascending order (3.14).");
1338
Radek Krejci643c8242018-11-15 17:51:11 +01001339 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}"
1340 "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG));
1341 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1342 logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1343
1344 assert_non_null(mod = lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}"
1345 "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1346 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1347 logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type.");
1348
Radek Krejci6cba4292018-11-15 17:33:29 +01001349 *state = NULL;
1350 ly_ctx_destroy(ctx, NULL);
1351}
1352
Radek Krejci16c0f822018-11-16 10:46:10 +01001353static void
1354test_type_instanceid(void **state)
1355{
1356 *state = test_type_instanceid;
1357
1358 struct ly_ctx *ctx;
1359 struct lys_module *mod;
1360 struct lysc_type *type;
1361
1362 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1363
1364 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}"
1365 "leaf l1 {type instance-identifier {require-instance true;}}"
1366 "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG));
1367 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1368 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1369 assert_non_null(type);
1370 assert_int_equal(LY_TYPE_INST, type->basetype);
1371 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1372
1373 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1374 assert_non_null(type);
1375 assert_int_equal(LY_TYPE_INST, type->basetype);
1376 assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance);
1377
1378 type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type;
1379 assert_non_null(type);
1380 assert_int_equal(LY_TYPE_INST, type->basetype);
1381 assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance);
1382
1383 /* invalid cases */
1384 assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG));
1385 logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1.");
1386
1387 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));
1388 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1389 logbuf_assert("Invalid type restrictions for instance-identifier type.");
1390
1391 *state = NULL;
1392 ly_ctx_destroy(ctx, NULL);
1393}
1394
Radek Krejci555cb5b2018-11-16 14:54:33 +01001395static void
1396test_type_identityref(void **state)
1397{
1398 *state = test_type_identityref;
1399
1400 struct ly_ctx *ctx;
1401 struct lys_module *mod;
1402 struct lysc_type *type;
1403
1404 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1405
1406 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;}"
1407 "typedef mytype {type identityref {base i;}}"
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001408 "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001409 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1410 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1411 assert_non_null(type);
1412 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1413 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1414 assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1415 assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name);
1416
1417 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1418 assert_non_null(type);
1419 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1420 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1421 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1422 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1423 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1424
Radek Krejcib83ea3e2018-11-23 14:29:13 +01001425 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}"
1426 "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG));
1427 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1428 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1429 assert_non_null(type);
1430 assert_int_equal(LY_TYPE_IDENT, type->basetype);
1431 assert_non_null(((struct lysc_type_identityref*)type)->bases);
1432 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases));
1433 assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name);
1434 assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name);
1435
Radek Krejci555cb5b2018-11-16 14:54:33 +01001436 /* invalid cases */
1437 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG));
1438 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1439 logbuf_assert("Missing base substatement for identityref type.");
1440
1441 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}"
1442 "leaf l {type mytype;}}", LYS_IN_YANG));
1443 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1444 logbuf_assert("Missing base substatement for identityref type mytype.");
1445
1446 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}"
1447 "leaf l {type mytype {base i;}}}", LYS_IN_YANG));
1448 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001449 logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001450
1451 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}"
1452 "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1453 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
Radek Krejcicdfecd92018-11-26 11:27:32 +01001454 logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01001455
1456 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;"
1457 "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG));
1458 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1459 logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules.");
1460
Radek Krejci322614f2018-11-16 15:05:44 +01001461 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));
1462 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1463 logbuf_assert("Unable to find base (j) of identityref.");
1464
1465 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));
1466 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1467 logbuf_assert("Invalid prefix used for base (x:j) of identityref.");
1468
Radek Krejci555cb5b2018-11-16 14:54:33 +01001469 *state = NULL;
1470 ly_ctx_destroy(ctx, NULL);
1471}
1472
Radek Krejcia3045382018-11-22 14:30:31 +01001473static void
1474test_type_leafref(void **state)
1475{
1476 *state = test_type_leafref;
1477
1478 struct ly_ctx *ctx;
1479 struct lys_module *mod;
1480 struct lysc_type *type;
1481 const char *path, *name, *prefix;
1482 size_t prefix_len, name_len;
1483 int parent_times, has_predicate;
1484
1485 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1486
1487 /* lys_path_token() */
1488 path = "invalid_path";
1489 parent_times = 0;
1490 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1491 path = "..";
1492 parent_times = 0;
1493 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1494 path = "..[";
1495 parent_times = 0;
1496 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1497 path = "../";
1498 parent_times = 0;
1499 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1500 path = "/";
1501 parent_times = 0;
1502 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1503
1504 path = "../../pref:id/xxx[predicate]/invalid!!!";
1505 parent_times = 0;
1506 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1507 assert_string_equal("/xxx[predicate]/invalid!!!", path);
1508 assert_int_equal(4, prefix_len);
1509 assert_int_equal(0, strncmp("pref", prefix, prefix_len));
1510 assert_int_equal(2, name_len);
1511 assert_int_equal(0, strncmp("id", name, name_len));
1512 assert_int_equal(2, parent_times);
1513 assert_int_equal(0, has_predicate);
1514 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1515 assert_string_equal("[predicate]/invalid!!!", path);
1516 assert_int_equal(0, prefix_len);
1517 assert_null(prefix);
1518 assert_int_equal(3, name_len);
1519 assert_int_equal(0, strncmp("xxx", name, name_len));
1520 assert_int_equal(1, has_predicate);
1521 path += 11;
1522 assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1523 assert_string_equal("!!!", path);
1524 assert_int_equal(0, prefix_len);
1525 assert_null(prefix);
1526 assert_int_equal(7, name_len);
1527 assert_int_equal(0, strncmp("invalid", name, name_len));
1528
1529 path = "/absolute/prefix:path";
1530 parent_times = 0;
1531 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1532 assert_string_equal("/prefix:path", path);
1533 assert_int_equal(0, prefix_len);
1534 assert_null(prefix);
1535 assert_int_equal(8, name_len);
1536 assert_int_equal(0, strncmp("absolute", name, name_len));
1537 assert_int_equal(-1, parent_times);
1538 assert_int_equal(0, has_predicate);
1539 assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate));
1540 assert_int_equal(0, *path);
1541 assert_int_equal(6, prefix_len);
1542 assert_int_equal(0, strncmp("prefix", prefix, prefix_len));
1543 assert_int_equal(4, name_len);
1544 assert_int_equal(0, strncmp("path", name, name_len));
1545 assert_int_equal(0, has_predicate);
1546
1547 /* complete leafref paths */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001548 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;"
Radek Krejcia3045382018-11-22 14:30:31 +01001549 "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}"
1550 "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG));
1551 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1552 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1553 assert_non_null(type);
1554 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1555 assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001556 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001557 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1558 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001559 assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance);
1560 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1561 assert_non_null(type);
1562 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1563 assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001564 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1565 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1566 assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001567 assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance);
1568
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001569 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 +01001570 "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001571 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001572 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1573 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1574 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001575 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001576 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1577 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1578 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001579 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1580 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001581 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1582
1583 /* prefixes are reversed to check using correct context of the path! */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001584 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix b; import b {prefix c;}"
Radek Krejci2f4a0292018-11-22 15:41:53 +01001585 "typedef mytype3 {type c:mytype {require-instance false;}}"
1586 "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG));
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001587 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1588 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1589 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001590 assert_int_equal(1, type->refcount);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001591 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1592 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1593 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001594 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1595 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejci2f4a0292018-11-22 15:41:53 +01001596 assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance);
1597 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1598 assert_non_null(type);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001599 assert_int_equal(1, type->refcount);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001600 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1601 assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path);
1602 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1603 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1604
Radek Krejci4ce68932018-11-28 12:53:36 +01001605 /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */
1606 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}"
1607 "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG));
1608 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1609 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1610 assert_non_null(type);
1611 assert_int_equal(1, type->refcount);
1612 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1613 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1614 assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1615 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1616 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype);
1617 assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance);
1618
Radek Krejci58d171e2018-11-23 13:50:55 +01001619 /* conditional leafrefs */
Radek Krejci4ce68932018-11-28 12:53:36 +01001620 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 +01001621 "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}"
1622 "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG));
1623 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1624 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1625 assert_non_null(type);
1626 assert_int_equal(1, type->refcount);
1627 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1628 assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path);
1629 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1630 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1631 assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype);
1632
Radek Krejcia3045382018-11-22 14:30:31 +01001633 /* TODO target in list with predicates */
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001634 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;"
1635 "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}"
1636 "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}"
1637 "leaf address {type leafref{ path \"../../interface[name = current()/../ifname]/address/ip\";}}}}",
1638 LYS_IN_YANG));
1639 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1640 type = ((struct lysc_node_leaf*)(*lysc_node_children(mod->compiled->data->prev))->prev)->type;
1641 assert_non_null(type);
1642 assert_int_equal(1, type->refcount);
1643 assert_int_equal(LY_TYPE_LEAFREF, type->basetype);
1644 assert_string_equal("../../interface[name = current()/../ifname]/address/ip", ((struct lysc_type_leafref* )type)->path);
1645 assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context);
1646 assert_non_null(((struct lysc_type_leafref*)type)->realtype);
1647 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype);
Radek Krejcia3045382018-11-22 14:30:31 +01001648
Radek Krejcia3045382018-11-22 14:30:31 +01001649 /* invalid paths */
1650 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}"
1651 "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG));
1652 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1653 logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\".");
1654 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}"
1655 "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG));
1656 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1657 logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path.");
1658 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}"
1659 "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG));
1660 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1661 logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\".");
1662 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;}}"
1663 "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG));
1664 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1665 logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container.");
1666 assert_non_null(mod = lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}"
1667 "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG));
1668 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1669 logbuf_assert("Invalid leafref path at character 3 (/a!invalid).");
1670 assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}"
1671 "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG));
1672 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1673 logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list.");
1674 assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}"
1675 "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG));
1676 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1677 logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\".");
Radek Krejci2f4a0292018-11-22 15:41:53 +01001678 assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;"
1679 "leaf ref1 {type leafref;}}", LYS_IN_YANG));
1680 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1681 logbuf_assert("Missing path substatement for leafref type.");
1682 assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}"
1683 "leaf ref1 {type mytype;}}", LYS_IN_YANG));
1684 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1685 logbuf_assert("Missing path substatement for leafref type mytype.");
Radek Krejci58d171e2018-11-23 13:50:55 +01001686 assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;"
1687 "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG));
1688 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1689 logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features "
1690 "applicable to the leafref itself.");
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001691 assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;"
1692 "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG));
1693 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1694 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 +01001695
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001696 assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;"
1697 "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG));
1698 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1699 logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1700 assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}"
1701 "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG));
1702 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1703 logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules.");
1704
Radek Krejci412ddfa2018-11-23 11:44:11 +01001705 /* circular chain */
1706 assert_non_null(mod = lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;"
1707 "leaf ref1 {type leafref {path /ref2;}}"
1708 "leaf ref2 {type leafref {path /ref3;}}"
Radek Krejci0c3f1ad2018-11-23 14:19:38 +01001709 "leaf ref3 {type leafref {path /ref4;}}"
1710 "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG));
Radek Krejci412ddfa2018-11-23 11:44:11 +01001711 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1712 logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected.");
1713
Radek Krejcia3045382018-11-22 14:30:31 +01001714 *state = NULL;
1715 ly_ctx_destroy(ctx, NULL);
1716}
1717
Radek Krejci43699232018-11-23 14:59:46 +01001718static void
1719test_type_empty(void **state)
1720{
1721 *state = test_type_empty;
1722
1723 struct ly_ctx *ctx;
1724 struct lys_module *mod;
1725
1726 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1727
1728 /* invalid */
1729 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
1730 "leaf l {type empty; default x;}}", LYS_IN_YANG));
1731 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1732 logbuf_assert("Leaf of type \"empty\" must not have a default value (x).");
1733
1734 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}"
1735 "leaf l {type mytype;}}", LYS_IN_YANG));
1736 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1737 logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x).");
1738
1739 *state = NULL;
1740 ly_ctx_destroy(ctx, NULL);
1741}
1742
Radek Krejcicdfecd92018-11-26 11:27:32 +01001743
1744static void
1745test_type_union(void **state)
1746{
1747 *state = test_type_union;
1748
1749 struct ly_ctx *ctx;
1750 struct lys_module *mod;
1751 struct lysc_type *type;
1752
1753 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1754
1755 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}"
1756 "typedef mytype {type union {type int8; type mybasetype;}}"
1757 "leaf l {type mytype;}}", LYS_IN_YANG));
1758 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1759 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1760 assert_non_null(type);
1761 assert_int_equal(2, type->refcount);
1762 assert_int_equal(LY_TYPE_UNION, type->basetype);
1763 assert_non_null(((struct lysc_type_union*)type)->types);
1764 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1765 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype);
1766 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1767
1768 assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}"
1769 "typedef mytype {type union {type int8; type mybasetype;}}"
1770 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG));
1771 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1772 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1773 assert_non_null(type);
1774 assert_int_equal(1, type->refcount);
1775 assert_int_equal(LY_TYPE_UNION, type->basetype);
1776 assert_non_null(((struct lysc_type_union*)type)->types);
1777 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1778 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1779 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype);
1780 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1781
1782 assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}"
1783 "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}"
Radek Krejci6be5ff12018-11-26 13:18:15 +01001784 "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}"
1785 "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}",
Radek Krejcicdfecd92018-11-26 11:27:32 +01001786 LYS_IN_YANG));
1787 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1788 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1789 assert_non_null(type);
1790 assert_int_equal(1, type->refcount);
1791 assert_int_equal(LY_TYPE_UNION, type->basetype);
1792 assert_non_null(((struct lysc_type_union*)type)->types);
1793 assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1794 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1795 assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype);
1796 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype);
1797 assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype);
1798 assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype);
1799
Radek Krejci6be5ff12018-11-26 13:18:15 +01001800 /* invalid unions */
1801 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}"
1802 "leaf l {type mytype;}}", LYS_IN_YANG));
1803 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1804 logbuf_assert("Missing type substatement for union type mytype.");
1805
1806 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG));
1807 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1808 logbuf_assert("Missing type substatement for union type.");
1809
1810 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}"
1811 "leaf l {type mytype {type string;}}}", LYS_IN_YANG));
1812 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1813 logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type.");
1814
1815 assert_non_null(mod = lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}"
1816 "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG));
1817 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1818 logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type.");
1819
Radek Krejcicdfecd92018-11-26 11:27:32 +01001820 *state = NULL;
1821 ly_ctx_destroy(ctx, NULL);
1822}
1823
1824static void
1825test_type_dflt(void **state)
1826{
1827 *state = test_type_union;
1828
1829 struct ly_ctx *ctx;
1830 struct lys_module *mod;
1831 struct lysc_type *type;
1832
1833 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1834
1835 /* default is not inherited from union's types */
1836 assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}"
1837 "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG));
1838 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1839 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1840 assert_non_null(type);
1841 assert_int_equal(1, type->refcount);
1842 assert_int_equal(LY_TYPE_UNION, type->basetype);
1843 assert_non_null(((struct lysc_type_union*)type)->types);
1844 assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types));
1845 assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype);
1846 assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype);
1847 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1848 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units);
1849
1850 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}"
1851 "leaf l {type mybasetype;}}", LYS_IN_YANG));
1852 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1853 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1854 assert_non_null(type);
1855 assert_int_equal(2, type->refcount);
1856 assert_int_equal(LY_TYPE_STRING, type->basetype);
1857 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1858 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1859
1860 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}"
1861 "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG));
1862 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1863 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1864 assert_non_null(type);
1865 assert_int_equal(2, type->refcount);
1866 assert_int_equal(LY_TYPE_STRING, type->basetype);
1867 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1868 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1869
Radek Krejci6be5ff12018-11-26 13:18:15 +01001870 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}"
1871 "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}"
1872 "leaf l2 {type mytype;}}", LYS_IN_YANG));
1873 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1874 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1875 assert_non_null(type);
1876 assert_int_equal(4, type->refcount);
1877 assert_int_equal(LY_TYPE_STRING, type->basetype);
1878 assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1879 assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1880 type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type;
1881 assert_non_null(type);
1882 assert_int_equal(4, type->refcount);
1883 assert_int_equal(LY_TYPE_STRING, type->basetype);
1884 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt);
1885 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units);
1886
1887 assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}"
1888 "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG));
1889 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1890 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1891 assert_non_null(type);
1892 assert_int_equal(3, type->refcount);
1893 assert_int_equal(LY_TYPE_STRING, type->basetype);
1894 assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1895 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1896
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001897 /* mandatory leaf does not takes default value from type */
1898 assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}"
1899 "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG));
1900 assert_int_equal(LY_SUCCESS, lys_compile(mod, 0));
1901 type = ((struct lysc_node_leaf*)mod->compiled->data)->type;
1902 assert_non_null(type);
1903 assert_int_equal(LY_TYPE_STRING, type->basetype);
1904 assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt);
1905 assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units);
1906
Radek Krejcicdfecd92018-11-26 11:27:32 +01001907 *state = NULL;
1908 ly_ctx_destroy(ctx, NULL);
1909}
1910
Radek Krejci665421c2018-12-05 08:03:39 +01001911static void
1912test_status(void **state)
1913{
1914 *state = test_status;
1915
1916 struct ly_ctx *ctx;
1917 struct lys_module *mod;
1918
1919 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
1920
1921 assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;"
1922 "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG));
1923 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1924 logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status.");
1925
1926 assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;"
1927 "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG));
1928 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1929 logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status.");
1930
1931 assert_non_null(mod = lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;"
1932 "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG));
1933 assert_int_equal(LY_EVALID, lys_compile(mod, 0));
1934 logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1935
1936 *state = NULL;
1937 ly_ctx_destroy(ctx, NULL);
1938}
1939
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001940int main(void)
1941{
1942 const struct CMUnitTest tests[] = {
Radek Krejci4f28eda2018-11-12 11:46:16 +01001943 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
1944 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
1945 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci0f07bed2018-11-13 14:01:40 +01001946 cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown),
1947 cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown),
Radek Krejcicda74152018-11-13 15:27:32 +01001948 cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown),
Radek Krejci8b764662018-11-14 14:15:13 +01001949 cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown),
1950 cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown),
Radek Krejci6cba4292018-11-15 17:33:29 +01001951 cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown),
Radek Krejci16c0f822018-11-16 10:46:10 +01001952 cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown),
Radek Krejci555cb5b2018-11-16 14:54:33 +01001953 cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +01001954 cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown),
Radek Krejci43699232018-11-23 14:59:46 +01001955 cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown),
Radek Krejcicdfecd92018-11-26 11:27:32 +01001956 cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown),
1957 cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown),
Radek Krejci665421c2018-12-05 08:03:39 +01001958 cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown),
Radek Krejci4f28eda2018-11-12 11:46:16 +01001959 cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01001960 cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001961 cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown),
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001962 };
1963
1964 return cmocka_run_group_tests(tests, NULL, NULL);
1965}