blob: 1623905fe92f5a662f803a9c27b9329b8128b7e7 [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;
36 struct lys_module *mod;
37 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
43 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
44 "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;}"
49 "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
50 "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;}}"
53 "notification k;}", LYS_IN_YANG));
54 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
127 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;"
128 "leaf a {type string; if-feature f;}"
129 "leaf b {type string;}}", LYS_IN_YANG));
130 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
135 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG));
136 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
140 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG));
141 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
145 *state = NULL;
146 ly_ctx_destroy(ctx, NULL);
147}
Radek Krejci18abde42020-06-13 20:04:39 +0200148void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200149test_date(void **state)
150{
151 *state = test_date;
152
153 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
154 logbuf_assert("Invalid argument date (lysp_check_date()).");
155 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
156 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
157 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
158 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
159 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
160 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
161 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
162 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
163 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
164 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
165 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
166 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
167 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
168 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
169 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
170 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
171
172 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
173 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
174 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
175
176 *state = NULL;
177}
178
Radek Krejci18abde42020-06-13 20:04:39 +0200179void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200180test_revisions(void **state)
181{
182 (void) state; /* unused */
183
184 struct lysp_revision *revs = NULL, *rev;
185
186 logbuf_clean();
187 /* no error, it just does nothing */
188 lysp_sort_revisions(NULL);
189 logbuf_assert("");
190
191 /* revisions are stored in wrong order - the newest is the last */
192 LY_ARRAY_NEW_RET(NULL, revs, rev,);
193 strcpy(rev->date, "2018-01-01");
194 LY_ARRAY_NEW_RET(NULL, revs, rev,);
195 strcpy(rev->date, "2018-12-31");
196
197 assert_int_equal(2, LY_ARRAY_SIZE(revs));
198 assert_string_equal("2018-01-01", &revs[0]);
199 assert_string_equal("2018-12-31", &revs[1]);
200 /* the order should be fixed, so the newest revision will be the first in the array */
201 lysp_sort_revisions(revs);
202 assert_string_equal("2018-12-31", &revs[0]);
203 assert_string_equal("2018-01-01", &revs[1]);
204
205 LY_ARRAY_FREE(revs);
206}
207
Radek Krejci18abde42020-06-13 20:04:39 +0200208void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200209test_typedef(void **state)
210{
211 *state = test_typedef;
212
213 struct ly_ctx *ctx = NULL;
214 const char *str;
215
216 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
217
218 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
219 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
220 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type. Line number 1.");
221 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
222 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
223 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type. Line number 1.");
224 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
225 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
226 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type. Line number 1.");
227 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
228 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
229 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type. Line number 1.");
230 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
231 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
232 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type. Line number 1.");
233 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
234 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
235 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type. Line number 1.");
236 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
237 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
238 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type. Line number 1.");
239 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
240 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
241 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type. Line number 1.");
242 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
243 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
244 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type. Line number 1.");
245 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
246 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
247 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type. Line number 1.");
248 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
249 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
250 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type. Line number 1.");
251 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
252 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
253 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type. Line number 1.");
254 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
255 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
256 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type. Line number 1.");
257 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
258 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
259 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type. Line number 1.");
260 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
261 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
262 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type. Line number 1.");
263 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
264 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
265 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type. Line number 1.");
266 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
267 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
268 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type. Line number 1.");
269 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
270 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
271 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type. Line number 1.");
272 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
273 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
274 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type. Line number 1.");
275
276 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
277 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
278 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
279 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
280 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
281 assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
282
283 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
284 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
285 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type. Line number 1.");
286
287 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
288 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
289 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
290
291 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
292 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
293 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type. Line number 1.");
294
295 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
296 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
297 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type. Line number 1.");
298
299 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
300 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
301 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
302 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type. Line number 1.");
303
304 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
305 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
306 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
307 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
308
309 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
310 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
311 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
312 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
313
314 *state = NULL;
315 ly_ctx_destroy(ctx, NULL);
316}