blob: a43d6fc0e3ac3bd05e817400b9729c1e795a5db9 [file] [log] [blame]
Radek Krejci3a4889a2020-05-19 17:01:58 +02001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from common.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
15#include <stdarg.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
21#include <string.h>
22
Radek Krejci18abde42020-06-13 20:04:39 +020023#include "context.h"
24#include "log.h"
25#include "tree_schema.h"
26#include "tree_schema_internal.h"
27
28#include "test_schema.h"
29
30void
Radek Krejci3a4889a2020-05-19 17:01:58 +020031test_getnext(void **state)
32{
33 *state = test_getnext;
34
35 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +020036 const struct lys_module *mod;
Radek Krejci3a4889a2020-05-19 17:01:58 +020037 const struct lysc_node *node = NULL, *four;
38 const struct lysc_node_container *cont;
39 const struct lysc_action *rpc;
40
41 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
42
Michal Vasko3a41dff2020-07-15 14:30:28 +020043 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
Radek Krejci3a4889a2020-05-19 17:01:58 +020044 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
45 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
46 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
47 " notification nine {leaf nine-data {type string;}}}"
48 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
Radek Krejcib93bd412020-11-02 13:23:11 +010049 "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
Radek Krejci3a4889a2020-05-19 17:01:58 +020050 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
51 "rpc i;"
52 "notification j {leaf i-data {type string;}}"
Michal Vasko3a41dff2020-07-15 14:30:28 +020053 "notification k;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +020054 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
55 assert_string_equal("a", node->name);
56 cont = (const struct lysc_node_container*)node;
57 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
58 assert_string_equal("b", node->name);
59 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
60 assert_string_equal("c", node->name);
61 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
62 assert_string_equal("d", node->name);
63 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
64 assert_string_equal("e", node->name);
65 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
66 assert_string_equal("f", node->name);
67 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
68 assert_string_equal("g", node->name);
69 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
70 assert_string_equal("h", node->name);
71 rpc = (const struct lysc_action*)node;
72 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
73 assert_string_equal("i", node->name);
74 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
75 assert_string_equal("j", node->name);
76 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
77 assert_string_equal("k", node->name);
78 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
79 /* Inside container */
80 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
81 assert_string_equal("one", node->name);
82 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
83 assert_string_equal("two", node->name);
84 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
85 assert_string_equal("three", node->name);
86 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
87 assert_string_equal("four", node->name);
88 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
89 assert_string_equal("five", node->name);
90 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
91 assert_string_equal("six", node->name);
92 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
93 assert_string_equal("seven", node->name);
94 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
95 assert_string_equal("eight", node->name);
96 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
97 assert_string_equal("nine", node->name);
98 assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
99 /* Inside RPC */
100 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
101 assert_string_equal("h-input", node->name);
102 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
103
104 /* options */
105 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
106 assert_string_equal("x", node->name);
107 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
108 assert_string_equal("seven", node->name);
109
110 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
111 assert_string_equal("seven", node->name);
112
113 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
114 assert_string_equal("five", node->name);
115 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
116 assert_string_equal("y", node->name);
117 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
118 assert_string_equal("seven", node->name);
119
120 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
121 assert_string_equal("one", node->name);
122
123 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
124 assert_string_equal("h-output", node->name);
125 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
126
Michal Vasko3a41dff2020-07-15 14:30:28 +0200127 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;"
Radek Krejci3a4889a2020-05-19 17:01:58 +0200128 "leaf a {type string; if-feature f;}"
Michal Vasko3a41dff2020-07-15 14:30:28 +0200129 "leaf b {type string;}}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200130 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
131 assert_string_equal("b", node->name);
132 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
133 assert_string_equal("a", node->name);
134
Michal Vasko3a41dff2020-07-15 14:30:28 +0200135 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200136 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
137 assert_string_equal("c", node->name);
138 assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
139
Michal Vasko3a41dff2020-07-15 14:30:28 +0200140 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200141 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
142 assert_string_equal("d", node->name);
143 assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
144
Radek Krejcib93bd412020-11-02 13:23:11 +0100145 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; container c {container cc;} leaf a {type string;}}", LYS_IN_YANG, &mod));
146 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
147 assert_string_equal("c", node->name);
148 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
149 assert_string_equal("a", node->name);
150
Radek Krejci3a4889a2020-05-19 17:01:58 +0200151 *state = NULL;
152 ly_ctx_destroy(ctx, NULL);
153}
Radek Krejci18abde42020-06-13 20:04:39 +0200154void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200155test_date(void **state)
156{
157 *state = test_date;
158
159 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
160 logbuf_assert("Invalid argument date (lysp_check_date()).");
161 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
162 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
163 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
164 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
165 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
166 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
167 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
168 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
169 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
170 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
171 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
172 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
173 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
174 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
175 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
176 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
177
178 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
179 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
180 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
181
182 *state = NULL;
183}
184
Radek Krejci18abde42020-06-13 20:04:39 +0200185void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200186test_revisions(void **state)
187{
188 (void) state; /* unused */
189
190 struct lysp_revision *revs = NULL, *rev;
191
192 logbuf_clean();
193 /* no error, it just does nothing */
194 lysp_sort_revisions(NULL);
195 logbuf_assert("");
196
197 /* revisions are stored in wrong order - the newest is the last */
198 LY_ARRAY_NEW_RET(NULL, revs, rev,);
199 strcpy(rev->date, "2018-01-01");
200 LY_ARRAY_NEW_RET(NULL, revs, rev,);
201 strcpy(rev->date, "2018-12-31");
202
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200203 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200204 assert_string_equal("2018-01-01", &revs[0]);
205 assert_string_equal("2018-12-31", &revs[1]);
206 /* the order should be fixed, so the newest revision will be the first in the array */
207 lysp_sort_revisions(revs);
208 assert_string_equal("2018-12-31", &revs[0]);
209 assert_string_equal("2018-01-01", &revs[1]);
210
211 LY_ARRAY_FREE(revs);
212}
213
Radek Krejci18abde42020-06-13 20:04:39 +0200214void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200215test_typedef(void **state)
216{
217 *state = test_typedef;
218
219 struct ly_ctx *ctx = NULL;
220 const char *str;
221
222 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
223
224 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200225 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200226 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type. Line number 1.");
227 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200228 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200229 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type. Line number 1.");
230 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200231 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200232 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type. Line number 1.");
233 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200234 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200235 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type. Line number 1.");
236 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200237 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200238 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type. Line number 1.");
239 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200240 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200241 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type. Line number 1.");
242 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200243 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200244 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type. Line number 1.");
245 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200246 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200247 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type. Line number 1.");
248 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200249 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200250 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type. Line number 1.");
251 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200252 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200253 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type. Line number 1.");
254 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200255 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200256 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type. Line number 1.");
257 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200258 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200259 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type. Line number 1.");
260 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200261 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200262 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type. Line number 1.");
263 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200264 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200265 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type. Line number 1.");
266 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200267 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200268 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type. Line number 1.");
269 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200270 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200271 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type. Line number 1.");
272 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200273 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200274 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type. Line number 1.");
275 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200276 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200277 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type. Line number 1.");
278 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200279 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200280 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type. Line number 1.");
281
282 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
283 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
284 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
285 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
286 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200287 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200288
289 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200290 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200291 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type. Line number 1.");
292
293 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200294 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200295 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
296
297 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200298 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200299 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type. Line number 1.");
300
301 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200302 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200303 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type. Line number 1.");
304
305 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
306 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200307 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200308 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type. Line number 1.");
309
310 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
311 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200312 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200313 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
314
315 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
316 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200317 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200318 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
319
320 *state = NULL;
321 ly_ctx_destroy(ctx, NULL);
322}
Michal Vasko6b26e742020-07-17 15:02:10 +0200323
324void
325test_accessible_tree(void **state)
326{
327 *state = test_accessible_tree;
328
329 struct ly_ctx *ctx = NULL;
330 const char *str;
331
332 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
333 logbuf_clean();
334
335 /* config -> config */
336 str =
337 "module a {"
338 "namespace urn:a;"
339 "prefix a;"
340 "container cont {"
341 "leaf l {"
342 "type empty;"
343 "}"
344 "}"
345 "container cont2 {"
346 "leaf l2 {"
347 "must ../../cont/l;"
348 "type leafref {"
349 "path /cont/l;"
350 "}"
351 "}"
352 "}"
353 "}";
354 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
355 logbuf_assert("");
356
357 /* config -> state leafref */
358 str =
359 "module b {"
360 "namespace urn:a;"
361 "prefix a;"
362 "container cont {"
363 "config false;"
364 "leaf l {"
365 "type empty;"
366 "}"
367 "}"
368 "container cont2 {"
369 "leaf l2 {"
370 "type leafref {"
371 "path /cont/l;"
372 "}"
373 "}"
374 "}"
375 "}";
376 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
377 logbuf_assert("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
378 " (as the leafref does), but it does not. /b:cont2/l2");
379 logbuf_clean();
380
381 /* config -> state must */
382 str =
383 "module b {"
384 "namespace urn:a;"
385 "prefix a;"
386 "container cont {"
387 "config false;"
388 "leaf l {"
389 "type empty;"
390 "}"
391 "}"
392 "container cont2 {"
393 "leaf l2 {"
394 "must ../../cont/l;"
395 "type empty;"
396 "}"
397 "}"
398 "}";
399 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
400 logbuf_assert("Schema node \"l\" not found (../../cont/) with context node \"/b:cont2/l2\".");
401 logbuf_clean();
402
403 /* state -> config */
404 str =
405 "module c {"
406 "namespace urn:a;"
407 "prefix a;"
408 "container cont {"
409 "leaf l {"
410 "type empty;"
411 "}"
412 "}"
413 "container cont2 {"
414 "config false;"
415 "leaf l2 {"
416 "must ../../cont/l;"
417 "type leafref {"
418 "path /cont/l;"
419 "}"
420 "}"
421 "}"
422 "}";
423 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
424 logbuf_assert("");
425
426 /* notif -> state */
427 str =
428 "module d {"
429 "namespace urn:a;"
430 "prefix a;"
431 "container cont {"
432 "config false;"
433 "leaf l {"
434 "type empty;"
435 "}"
436 "}"
437 "notification notif {"
438 "leaf l2 {"
439 "must ../../cont/l;"
440 "type leafref {"
441 "path /cont/l;"
442 "}"
443 "}"
444 "}"
445 "}";
446 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
447 logbuf_assert("");
448
449 /* notif -> notif */
450 str =
451 "module e {"
452 "namespace urn:a;"
453 "prefix a;"
454 "notification notif {"
455 "leaf l {"
456 "type empty;"
457 "}"
458 "leaf l2 {"
459 "must ../../notif/l;"
460 "type leafref {"
461 "path /notif/l;"
462 "}"
463 "}"
464 "}"
465 "}";
466 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
467 logbuf_assert("");
468
469 /* rpc input -> state */
470 str =
471 "module f {"
472 "namespace urn:a;"
473 "prefix a;"
474 "container cont {"
475 "config false;"
476 "leaf l {"
477 "type empty;"
478 "}"
479 "}"
480 "rpc rp {"
481 "input {"
482 "leaf l2 {"
483 "must ../../cont/l;"
484 "type leafref {"
485 "path /cont/l;"
486 "}"
487 "}"
488 "}"
489 "}"
490 "}";
491 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
492 logbuf_assert("");
493
494 /* rpc input -> rpc input */
495 str =
496 "module g {"
497 "namespace urn:a;"
498 "prefix a;"
499 "rpc rp {"
500 "input {"
501 "leaf l {"
502 "type empty;"
503 "}"
504 "leaf l2 {"
505 "must ../l;"
506 "type leafref {"
507 "path /rp/l;"
508 "}"
509 "}"
510 "}"
511 "}"
512 "}";
513 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
514 logbuf_assert("");
515
516 /* rpc input -> rpc output leafref */
517 str =
518 "module h {"
519 "namespace urn:a;"
520 "prefix a;"
521 "rpc rp {"
522 "input {"
523 "leaf l2 {"
524 "type leafref {"
525 "path /rp/l;"
526 "}"
527 "}"
528 "}"
529 "output {"
530 "leaf l {"
531 "type empty;"
532 "}"
533 "}"
534 "}"
535 "}";
536 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
537 logbuf_assert("Not found node \"l\" in path. /h:rp/l2");
538 logbuf_clean();
539
540 /* rpc input -> rpc output must */
541 str =
542 "module h {"
543 "namespace urn:a;"
544 "prefix a;"
545 "rpc rp {"
546 "input {"
547 "leaf l2 {"
548 "must ../l;"
549 "type empty;"
550 "}"
551 "}"
552 "output {"
553 "leaf l {"
554 "type empty;"
555 "}"
556 "}"
557 "}"
558 "}";
559 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
560 logbuf_assert("Schema node \"l\" not found (../) with context node \"/h:rp/l2\".");
561 logbuf_clean();
562
563 /* rpc input -> notif leafref */
564 str =
565 "module i {"
566 "namespace urn:a;"
567 "prefix a;"
568 "rpc rp {"
569 "input {"
570 "leaf l2 {"
571 "type leafref {"
572 "path ../../notif/l;"
573 "}"
574 "}"
575 "}"
576 "}"
577 "notification notif {"
578 "leaf l {"
579 "type empty;"
580 "}"
581 "}"
582 "}";
583 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
584 logbuf_assert("Not found node \"notif\" in path. /i:rp/l2");
585 logbuf_clean();
586
587 /* rpc input -> notif must */
588 str =
589 "module i {"
590 "namespace urn:a;"
591 "prefix a;"
592 "rpc rp {"
593 "input {"
594 "leaf l2 {"
595 "must /notif/l;"
596 "type empty;"
597 "}"
598 "}"
599 "}"
600 "notification notif {"
601 "leaf l {"
602 "type empty;"
603 "}"
604 "}"
605 "}";
606 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
607 logbuf_assert("Schema node \"l\" not found (/notif/) with context node \"/i:rp/l2\".");
608 logbuf_clean();
609
610 /* action output -> state */
611 str =
612 "module j {"
613 "yang-version 1.1;"
614 "namespace urn:a;"
615 "prefix a;"
616 "container cont {"
617 "list ll {"
618 "key k;"
619 "leaf k {"
620 "type string;"
621 "}"
622 "action act {"
623 "output {"
624 "leaf l2 {"
625 "must /cont/l;"
626 "type leafref {"
627 "path ../../../l;"
628 "}"
629 "}"
630 "}"
631 "}"
632 "}"
633 "leaf l {"
634 "config false;"
635 "type empty;"
636 "}"
637 "}"
638 "}";
639 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
640 logbuf_assert("");
641
642 /* action output -> action input leafref */
643 str =
644 "module k {"
645 "yang-version 1.1;"
646 "namespace urn:a;"
647 "prefix a;"
648 "container cont {"
649 "list ll {"
650 "key k;"
651 "leaf k {"
652 "type string;"
653 "}"
654 "action act {"
655 "input {"
656 "leaf l {"
657 "type empty;"
658 "}"
659 "}"
660 "output {"
661 "leaf l2 {"
662 "type leafref {"
663 "path ../l;"
664 "}"
665 "}"
666 "}"
667 "}"
668 "}"
669 "}"
670 "}";
671 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
672 logbuf_assert("Not found node \"l\" in path. /k:cont/ll/act/l2");
673 logbuf_clean();
674
675 /* action output -> action input must */
676 str =
677 "module k {"
678 "yang-version 1.1;"
679 "namespace urn:a;"
680 "prefix a;"
681 "container cont {"
682 "list ll {"
683 "key k;"
684 "leaf k {"
685 "type string;"
686 "}"
687 "action act {"
688 "input {"
689 "leaf l {"
690 "type empty;"
691 "}"
692 "}"
693 "output {"
694 "leaf l2 {"
695 "must /cont/ll/act/l;"
696 "type empty;"
697 "}"
698 "}"
699 "}"
700 "}"
701 "}"
702 "}";
703 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
704 logbuf_assert("Schema node \"l\" not found (/cont/ll/act/) with context node \"/k:cont/ll/act/l2\".");
705 logbuf_clean();
706
707 *state = NULL;
708 ly_ctx_destroy(ctx, NULL);
709}