blob: d6070aa0a549d97b85d990adfedaa3f949072737 [file] [log] [blame]
Radek Krejci3a4889a2020-05-19 17:01:58 +02001/*
2 * @file test_schema_stmts.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for YANG (YIN) statements in (sub)modules
5 *
6 * Copyright (c) 2018-2020 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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#include "test_schema.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020015
Radek Krejci3a4889a2020-05-19 17:01:58 +020016#include <string.h>
17
Radek Krejci18abde42020-06-13 20:04:39 +020018#include "context.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010019#include "log.h"
Radek Krejci18abde42020-06-13 20:04:39 +020020#include "tree_schema.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020021
Radek Krejci18abde42020-06-13 20:04:39 +020022void
Radek Krejci3a4889a2020-05-19 17:01:58 +020023test_identity(void **state)
24{
Michal Vasko4de7d072021-07-09 09:13:18 +020025 struct lys_module *mod, *mod_imp;
Radek Krejci3a4889a2020-05-19 17:01:58 +020026
Radek Krejci3a4889a2020-05-19 17:01:58 +020027 /*
28 * parsing YANG
29 */
Radek Iša56ca9e42020-09-08 18:42:00 +020030 TEST_STMT_DUP(1, 0, "identity id", "description", "a", "b", "1");
31 TEST_STMT_DUP(1, 0, "identity id", "reference", "a", "b", "1");
32 TEST_STMT_DUP(1, 0, "identity id", "status", "current", "obsolete", "1");
Radek Krejci3a4889a2020-05-19 17:01:58 +020033
34 /* full content */
Radek Iša56ca9e42020-09-08 18:42:00 +020035 TEST_SCHEMA_OK(1, 0, "identityone",
Radek Krejcib4ac5a92020-11-23 17:54:33 +010036 "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}"
37 "identity a; identity b; extension ext; feature x; feature y;", mod);
Radek Krejci3a4889a2020-05-19 17:01:58 +020038 assert_non_null(mod->parsed->identities);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020039 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities));
Radek Krejci3a4889a2020-05-19 17:01:58 +020040
41 /* invalid substatement */
Radek Iša56ca9e42020-09-08 18:42:00 +020042 TEST_STMT_SUBSTM_ERR(0, "identity", "organization", "XXX");
Radek Krejci3a4889a2020-05-19 17:01:58 +020043
44 /*
45 * parsing YIN
46 */
47 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +020048 TEST_SCHEMA_OK(1, 1, "identityone-yin", "<identity name=\"ident-name\">"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010049 "<if-feature name=\"iff\"/>"
50 "<base name=\"base-name\"/>"
51 "<status value=\"deprecated\"/>"
52 "<description><text>desc</text></description>"
53 "<reference><text>ref</text></reference>"
54 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */
55 "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020056 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities));
Radek Krejci3a4889a2020-05-19 17:01:58 +020057 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
58 assert_string_equal(mod->parsed->identities[0].bases[0], "base-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +020059 assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff");
Radek Krejci3a4889a2020-05-19 17:01:58 +020060 assert_string_equal(mod->parsed->identities[0].dsc, "desc");
61 assert_string_equal(mod->parsed->identities[0].ref, "ref");
62 assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC);
Michal Vasko7f45cf22020-10-01 12:49:44 +020063 /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext");
Radek Krejci3a4889a2020-05-19 17:01:58 +020064 assert_non_null(mod->parsed->identities[0].exts[0].compiled);
65 assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1);
66 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +020067 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
Radek Krejci3a4889a2020-05-19 17:01:58 +020068
69 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +020070 TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020071 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities));
Radek Krejci3a4889a2020-05-19 17:01:58 +020072 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
73
74 /* invalid substatement */
Radek Krejci8297b792020-08-16 14:49:05 +020075 TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>",
Radek Iša56ca9e42020-09-08 18:42:00 +020076 "Invalid sub-elemnt \"if-feature\" of \"identity\" element - "
77 "this sub-element is allowed only in modules with version 1.1 or newer.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +020078
79 /*
80 * compiling
81 */
Radek Iša56ca9e42020-09-08 18:42:00 +020082 TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp);
83 TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010084 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
85 "identity b4 {base b:b1; base b3;}", mod);
Radek Krejci3a4889a2020-05-19 17:01:58 +020086 assert_non_null(mod_imp->compiled);
Radek Krejci80d281e2020-09-14 17:42:54 +020087 assert_non_null(mod_imp->identities);
88 assert_non_null(mod->identities);
89 assert_non_null(mod_imp->identities[0].derived);
90 assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived));
91 assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]);
92 assert_non_null(mod->identities[0].derived);
93 assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived));
94 assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]);
95 assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]);
96 assert_non_null(mod->identities[1].derived);
97 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
98 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]);
99 assert_non_null(mod->identities[2].derived);
100 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived));
101 assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200102
Radek Iša56ca9e42020-09-08 18:42:00 +0200103 TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod);
Radek Krejci80d281e2020-09-14 17:42:54 +0200104 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
105 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200106
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} identity i1;}");
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "/inv:{identity='i1'}");
109 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i1;}", "Identity \"i1\" is derived from itself.", "/inv:{identity='i1'}");
110 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}",
111 "Identity \"i1\" is indirectly derived from itself.", "/inv:{identity='i3'}");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200112
Michal Vasko33ff9422020-07-03 09:50:39 +0200113 /* base in non-implemented module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200114 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100115 "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}");
Radek Iša56ca9e42020-09-08 18:42:00 +0200116 TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod);
Michal Vasko33ff9422020-07-03 09:50:39 +0200117
118 /* default value from non-implemented module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200119 TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100120 "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100121 " - identity found in non-implemented module \"base\".).", "Schema location /ident2:l.");
Michal Vasko33ff9422020-07-03 09:50:39 +0200122
123 /* default value in typedef from non-implemented module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200124 TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100125 "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100126 " identityref \"b:i2\" value - identity found in non-implemented module \"base\".).", "Schema location /ident2:l.");
Michal Vasko33ff9422020-07-03 09:50:39 +0200127
Radek Krejci3a4889a2020-05-19 17:01:58 +0200128 /*
129 * printing
130 */
131
132 /*
133 * cleanup
134 */
Radek Krejci3a4889a2020-05-19 17:01:58 +0200135}
136
Radek Krejci18abde42020-06-13 20:04:39 +0200137void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200138test_feature(void **state)
139{
Michal Vasko4de7d072021-07-09 09:13:18 +0200140 struct lys_module *mod;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100141 const struct lysp_feature *f;
Radek Krejci3a4889a2020-05-19 17:01:58 +0200142
Radek Krejci3a4889a2020-05-19 17:01:58 +0200143 /*
144 * parsing YANG
145 */
146
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", "1");
148 TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", "1");
149 TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", "1");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200150
151 /* full content */
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 TEST_SCHEMA_OK(1, 0, "featureone",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100153 "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}"
154 "extension ext; feature x; feature y;", mod);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200155 assert_non_null(mod->parsed->features);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200156 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200157
158 /* invalid substatement */
Radek Iša56ca9e42020-09-08 18:42:00 +0200159 TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200160
161 /*
162 * parsing YIN
163 */
164 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +0200165 TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100166 "<if-feature name=\"iff\"/>"
167 "<status value=\"deprecated\"/>"
168 "<description><text>desc</text></description>"
169 "<reference><text>ref</text></reference>"
170 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */
171 "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200172 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200173 assert_string_equal(mod->parsed->features[0].name, "feature-name");
174 assert_string_equal(mod->parsed->features[0].dsc, "desc");
175 assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200176 assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200177 assert_string_equal(mod->parsed->features[0].ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200178 /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200179 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200180 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
Radek Krejci3a4889a2020-05-19 17:01:58 +0200181
182 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod)
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200184 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200185 assert_string_equal(mod->parsed->features[0].name, "feature-name");
186
187 /* invalid substatement */
Radek Krejci8297b792020-08-16 14:49:05 +0200188 TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>",
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 "Unexpected sub-element \"organization\" of \"feature\" element.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200190
191 /*
192 * compiling
193 */
194
Radek Iša56ca9e42020-09-08 18:42:00 +0200195 TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100196 "feature orfeature {if-feature \"f1 or f2\";}\n"
197 "feature andfeature {if-feature \"f1 and f2\";}\n"
198 "feature f6 {if-feature \"not f1\";}\n"
199 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
200 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
201 "feature f9 {if-feature \"not not f1\";}", mod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100202 assert_non_null(mod->parsed->features);
203 assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200204
205 /* all features are disabled by default */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100206 LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) {
207 assert_false(f->flags & LYS_FENABLED);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200208 }
Radek Krejci3a4889a2020-05-19 17:01:58 +0200209
210 /* some invalid expressions */
Radek Krejci8297b792020-08-16 14:49:05 +0200211 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200212 "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200213 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 "Invalid value \"f and\" of if-feature - unexpected end of expression.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200215 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200216 "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200217 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200218 "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200219 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200221 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature ---;}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200222 "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200223 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200224 "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200225
Radek Iša56ca9e42020-09-08 18:42:00 +0200226 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}");
Radek Krejci8297b792020-08-16 14:49:05 +0200227 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200228 "Feature \"f1\" is indirectly referenced from itself.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200229 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 "Feature \"f1\" is referenced from itself.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200231 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200232 "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200233 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200235 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200236 "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200237 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200238 "Invalid value \"f1 not \" of if-feature - unexpected end of expression.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200239 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.", NULL);
Radek Krejci8297b792020-08-16 14:49:05 +0200241 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}",
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200243
244 /*
245 * printing
246 */
247
248 /*
249 * cleanup
250 */
Radek Krejci3a4889a2020-05-19 17:01:58 +0200251}