blob: 24417c423057b228d54d4cf627f2025cdf1221d7 [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 */
14
Radek Krejci3a4889a2020-05-19 17:01:58 +020015#include <string.h>
16
Radek Krejci18abde42020-06-13 20:04:39 +020017#include "context.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010018#include "log.h"
Radek Krejci18abde42020-06-13 20:04:39 +020019#include "test_schema.h"
20#include "tree_schema.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "utests.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020022
Radek Krejci18abde42020-06-13 20:04:39 +020023#include "test_schema.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020024
Radek Krejci18abde42020-06-13 20:04:39 +020025void
Radek Krejci3a4889a2020-05-19 17:01:58 +020026test_identity(void **state)
27{
28 *state = test_identity;
29
30 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +020031 const struct lys_module *mod, *mod_imp;
Radek Krejci3a4889a2020-05-19 17:01:58 +020032
33 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
34
35 /*
36 * parsing YANG
37 */
38 TEST_STMT_DUP(ctx, 1, 0, "identity id", "description", "a", "b", "1");
39 TEST_STMT_DUP(ctx, 1, 0, "identity id", "reference", "a", "b", "1");
40 TEST_STMT_DUP(ctx, 1, 0, "identity id", "status", "current", "obsolete", "1");
41
42 /* full content */
43 TEST_SCHEMA_OK(ctx, 1, 0, "identityone",
Radek Krejcib4ac5a92020-11-23 17:54:33 +010044 "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}"
45 "identity a; identity b; extension ext; feature x; feature y;", mod);
Radek Krejci3a4889a2020-05-19 17:01:58 +020046 assert_non_null(mod->parsed->identities);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020047 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities));
Radek Krejci3a4889a2020-05-19 17:01:58 +020048
49 /* invalid substatement */
50 TEST_STMT_SUBSTM_ERR(ctx, 0, "identity", "organization", "XXX");
51
52 /*
53 * parsing YIN
54 */
55 /* max subelems */
56 TEST_SCHEMA_OK(ctx, 1, 1, "identityone-yin", "<identity name=\"ident-name\">"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010057 "<if-feature name=\"iff\"/>"
58 "<base name=\"base-name\"/>"
59 "<status value=\"deprecated\"/>"
60 "<description><text>desc</text></description>"
61 "<reference><text>ref</text></reference>"
62 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */
63 "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020064 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities));
Radek Krejci3a4889a2020-05-19 17:01:58 +020065 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
66 assert_string_equal(mod->parsed->identities[0].bases[0], "base-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +020067 assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff");
Radek Krejci3a4889a2020-05-19 17:01:58 +020068 assert_string_equal(mod->parsed->identities[0].dsc, "desc");
69 assert_string_equal(mod->parsed->identities[0].ref, "ref");
70 assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC);
Michal Vasko7f45cf22020-10-01 12:49:44 +020071 /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext");
Radek Krejci3a4889a2020-05-19 17:01:58 +020072 assert_non_null(mod->parsed->identities[0].exts[0].compiled);
73 assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1);
74 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +020075 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
Radek Krejci3a4889a2020-05-19 17:01:58 +020076
77 /* min subelems */
78 TEST_SCHEMA_OK(ctx, 1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020079 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities));
Radek Krejci3a4889a2020-05-19 17:01:58 +020080 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
81
82 /* invalid substatement */
83 TEST_SCHEMA_ERR(ctx, 0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>",
Radek Krejcib4ac5a92020-11-23 17:54:33 +010084 "Invalid sub-elemnt \"if-feature\" of \"identity\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +020085
86 /*
87 * compiling
88 */
89 TEST_SCHEMA_OK(ctx, 0, 0, "a", "identity a1;", mod_imp);
90 TEST_SCHEMA_OK(ctx, 1, 0, "b", "import a {prefix a;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010091 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
92 "identity b4 {base b:b1; base b3;}", mod);
Radek Krejci3a4889a2020-05-19 17:01:58 +020093 assert_non_null(mod_imp->compiled);
Radek Krejci80d281e2020-09-14 17:42:54 +020094 assert_non_null(mod_imp->identities);
95 assert_non_null(mod->identities);
96 assert_non_null(mod_imp->identities[0].derived);
97 assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived));
98 assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]);
99 assert_non_null(mod->identities[0].derived);
100 assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived));
101 assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]);
102 assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]);
103 assert_non_null(mod->identities[1].derived);
104 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
105 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]);
106 assert_non_null(mod->identities[2].derived);
107 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived));
108 assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200109
110 TEST_SCHEMA_OK(ctx, 1, 0, "c", "identity c2 {base c1;} identity c1;", mod);
Radek Krejci80d281e2020-09-14 17:42:54 +0200111 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
112 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200113
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100114 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "identity i1;identity i1;", "Duplicate identifier \"i1\" of identity statement. Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200115
116 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} identity i1;}");
117 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "include inv_sub;identity i1;",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100118 "Duplicate identifier \"i1\" of identity statement. Line number 1.");
119 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\". /inv:{identity='i1'}");
120 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "identity i1 {base i1;}", "Identity \"i1\" is derived from itself. /inv:{identity='i1'}");
121 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}",
122 "Identity \"i1\" is indirectly derived from itself. /inv:{identity='i3'}");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200123
Michal Vasko33ff9422020-07-03 09:50:39 +0200124 /* base in non-implemented module */
125 ly_ctx_set_module_imp_clb(ctx, test_imp_clb,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100126 "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}");
Michal Vasko33ff9422020-07-03 09:50:39 +0200127 TEST_SCHEMA_OK(ctx, 0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod);
128
129 /* default value from non-implemented module */
130 TEST_SCHEMA_ERR(ctx, 0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100131 "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value"
132 " - identity found in non-implemented module \"base\".). /ident2:l");
Michal Vasko33ff9422020-07-03 09:50:39 +0200133
134 /* default value in typedef from non-implemented module */
135 TEST_SCHEMA_ERR(ctx, 0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100136 "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid"
137 " identityref \"b:i2\" value - identity found in non-implemented module \"base\".). /ident2:l");
Michal Vasko33ff9422020-07-03 09:50:39 +0200138
Radek Krejci3a4889a2020-05-19 17:01:58 +0200139 /*
140 * printing
141 */
142
143 /*
144 * cleanup
145 */
146
147 *state = NULL;
148 ly_ctx_destroy(ctx, NULL);
149}
150
Radek Krejci18abde42020-06-13 20:04:39 +0200151void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200152test_feature(void **state)
153{
154 *state = test_feature;
155
156 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200157 const struct lys_module *mod;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100158 const struct lysp_feature *f;
Radek Krejci3a4889a2020-05-19 17:01:58 +0200159
160 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
161
162 /*
163 * parsing YANG
164 */
165
166 TEST_STMT_DUP(ctx, 1, 0, "feature f", "description", "a", "b", "1");
167 TEST_STMT_DUP(ctx, 1, 0, "feature f", "reference", "a", "b", "1");
168 TEST_STMT_DUP(ctx, 1, 0, "feature f", "status", "current", "obsolete", "1");
169
170 /* full content */
171 TEST_SCHEMA_OK(ctx, 1, 0, "featureone",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100172 "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}"
173 "extension ext; feature x; feature y;", mod);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200174 assert_non_null(mod->parsed->features);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200175 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200176
177 /* invalid substatement */
178 TEST_STMT_SUBSTM_ERR(ctx, 0, "feature", "organization", "XXX");
179
180 /*
181 * parsing YIN
182 */
183 /* max subelems */
184 TEST_SCHEMA_OK(ctx, 0, 1, "featureone-yin", "<feature name=\"feature-name\">"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100185 "<if-feature name=\"iff\"/>"
186 "<status value=\"deprecated\"/>"
187 "<description><text>desc</text></description>"
188 "<reference><text>ref</text></reference>"
189 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */
190 "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200191 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200192 assert_string_equal(mod->parsed->features[0].name, "feature-name");
193 assert_string_equal(mod->parsed->features[0].dsc, "desc");
194 assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200195 assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200196 assert_string_equal(mod->parsed->features[0].ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200197 /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200198 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200199 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
Radek Krejci3a4889a2020-05-19 17:01:58 +0200200
201 /* min subelems */
202 TEST_SCHEMA_OK(ctx, 0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod)
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200203 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200204 assert_string_equal(mod->parsed->features[0].name, "feature-name");
205
206 /* invalid substatement */
207 TEST_SCHEMA_ERR(ctx, 0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100208 "Unexpected sub-element \"organization\" of \"feature\" element. Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200209
210 /*
211 * compiling
212 */
213
214 TEST_SCHEMA_OK(ctx, 1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100215 "feature orfeature {if-feature \"f1 or f2\";}\n"
216 "feature andfeature {if-feature \"f1 and f2\";}\n"
217 "feature f6 {if-feature \"not f1\";}\n"
218 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
219 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
220 "feature f9 {if-feature \"not not f1\";}", mod);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100221 assert_non_null(mod->parsed->features);
222 assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200223
224 /* all features are disabled by default */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100225 LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) {
226 assert_false(f->flags & LYS_FENABLED);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200227 }
Radek Krejci3a4889a2020-05-19 17:01:58 +0200228
229 /* some invalid expressions */
230 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f{if-feature f1;}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100231 "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200232 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100233 "Invalid value \"f and\" of if-feature - unexpected end of expression.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200234 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f{if-feature 'or';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100235 "Invalid value \"or\" of if-feature - unexpected end of expression.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200236 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100237 "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200238 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100239 "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200240 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f2{if-feature ---;}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100241 "Invalid value \"---\" of if-feature - unable to find feature \"---\".");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200242 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100243 "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200244 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "feature f1; feature f1;",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100245 "Duplicate identifier \"f1\" of feature statement. Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200246
247 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}");
248 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "include inv_sub;feature f1;",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100249 "Duplicate identifier \"f1\" of feature statement. Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200250 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100251 "Feature \"f1\" is indirectly referenced from itself.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200252 TEST_SCHEMA_ERR(ctx, 0, 0, "inv", "feature f1 {if-feature f1;}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100253 "Feature \"f1\" is referenced from itself.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200254 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f {if-feature ();}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100255 "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200256 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100257 "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200258 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100259 "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200260 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100261 "Invalid value \"f1 not \" of if-feature - unexpected end of expression.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200262 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100263 "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200264 TEST_SCHEMA_ERR(ctx, 1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}",
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100265 "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200266
267 /*
268 * printing
269 */
270
271 /*
272 * cleanup
273 */
274
275 *state = NULL;
276 ly_ctx_destroy(ctx, NULL);
277}