blob: dabf106c6e1df38a1573f4a149da805b532131a8 [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 c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200128 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
129 assert_string_equal("c", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100130 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200131
Michal Vasko3a41dff2020-07-15 14:30:28 +0200132 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 +0200133 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
134 assert_string_equal("d", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100135 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200136
Radek Krejcib93bd412020-11-02 13:23:11 +0100137 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));
138 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
139 assert_string_equal("c", node->name);
140 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
141 assert_string_equal("a", node->name);
142
Radek Krejci3a4889a2020-05-19 17:01:58 +0200143 *state = NULL;
144 ly_ctx_destroy(ctx, NULL);
145}
Radek Krejci18abde42020-06-13 20:04:39 +0200146void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200147test_date(void **state)
148{
149 *state = test_date;
150
151 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
152 logbuf_assert("Invalid argument date (lysp_check_date()).");
153 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
154 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
155 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
156 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
157 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
158 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
159 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
160 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
161 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
162 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
163 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
164 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
165 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
166 logbuf_assert("Invalid value \"2018.02-28\" 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
170 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
171 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
172 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
173
174 *state = NULL;
175}
176
Radek Krejci18abde42020-06-13 20:04:39 +0200177void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200178test_revisions(void **state)
179{
180 (void) state; /* unused */
181
182 struct lysp_revision *revs = NULL, *rev;
183
184 logbuf_clean();
185 /* no error, it just does nothing */
186 lysp_sort_revisions(NULL);
187 logbuf_assert("");
188
189 /* revisions are stored in wrong order - the newest is the last */
190 LY_ARRAY_NEW_RET(NULL, revs, rev,);
191 strcpy(rev->date, "2018-01-01");
192 LY_ARRAY_NEW_RET(NULL, revs, rev,);
193 strcpy(rev->date, "2018-12-31");
194
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200195 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200196 assert_string_equal("2018-01-01", &revs[0]);
197 assert_string_equal("2018-12-31", &revs[1]);
198 /* the order should be fixed, so the newest revision will be the first in the array */
199 lysp_sort_revisions(revs);
200 assert_string_equal("2018-12-31", &revs[0]);
201 assert_string_equal("2018-01-01", &revs[1]);
202
203 LY_ARRAY_FREE(revs);
204}
205
Radek Krejci18abde42020-06-13 20:04:39 +0200206void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200207test_typedef(void **state)
208{
209 *state = test_typedef;
210
211 struct ly_ctx *ctx = NULL;
212 const char *str;
213
214 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
215
216 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100217 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200218 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type. Line number 1.");
219 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100220 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200221 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type. Line number 1.");
222 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100223 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200224 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type. Line number 1.");
225 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100226 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200227 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type. Line number 1.");
228 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100229 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200230 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type. Line number 1.");
231 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100232 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200233 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type. Line number 1.");
234 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100235 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200236 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type. Line number 1.");
237 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100238 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200239 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type. Line number 1.");
240 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100241 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200242 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type. Line number 1.");
243 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100244 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200245 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type. Line number 1.");
246 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100247 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200248 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type. Line number 1.");
249 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100250 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200251 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type. Line number 1.");
252 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100253 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200254 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type. Line number 1.");
255 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100256 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200257 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type. Line number 1.");
258 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100259 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200260 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type. Line number 1.");
261 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100262 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200263 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type. Line number 1.");
264 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100265 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200266 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type. Line number 1.");
267 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100268 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200269 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type. Line number 1.");
270 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100271 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200272 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type. Line number 1.");
273
274 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
275 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
276 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
277 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
278 "typedef uint32_ {type string;} 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_SUCCESS);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200280
281 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100282 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200283 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type. Line number 1.");
284
285 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100286 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200287 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
288
289 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100290 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200291 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type. Line number 1.");
292
293 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100294 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200295 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type. Line number 1.");
296
297 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
298 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100299 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200300 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type. Line number 1.");
301
302 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
303 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100304 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200305 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
306
307 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
308 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100309 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200310 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
311
312 *state = NULL;
313 ly_ctx_destroy(ctx, NULL);
314}
Michal Vasko6b26e742020-07-17 15:02:10 +0200315
316void
317test_accessible_tree(void **state)
318{
319 *state = test_accessible_tree;
320
321 struct ly_ctx *ctx = NULL;
322 const char *str;
323
324 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
325 logbuf_clean();
326
327 /* config -> config */
328 str =
329 "module a {"
330 "namespace urn:a;"
331 "prefix a;"
332 "container cont {"
333 "leaf l {"
334 "type empty;"
335 "}"
336 "}"
337 "container cont2 {"
338 "leaf l2 {"
339 "must ../../cont/l;"
340 "type leafref {"
341 "path /cont/l;"
342 "}"
343 "}"
344 "}"
345 "}";
346 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
347 logbuf_assert("");
348
349 /* config -> state leafref */
350 str =
351 "module b {"
352 "namespace urn:a;"
353 "prefix a;"
354 "container cont {"
355 "config false;"
356 "leaf l {"
357 "type empty;"
358 "}"
359 "}"
360 "container cont2 {"
361 "leaf l2 {"
362 "type leafref {"
363 "path /cont/l;"
364 "}"
365 "}"
366 "}"
367 "}";
368 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
369 logbuf_assert("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
370 " (as the leafref does), but it does not. /b:cont2/l2");
371 logbuf_clean();
372
373 /* config -> state must */
374 str =
375 "module b {"
376 "namespace urn:a;"
377 "prefix a;"
378 "container cont {"
379 "config false;"
380 "leaf l {"
381 "type empty;"
382 "}"
383 "}"
384 "container cont2 {"
385 "leaf l2 {"
386 "must ../../cont/l;"
387 "type empty;"
388 "}"
389 "}"
390 "}";
391 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100392 logbuf_assert("Schema node \"l\" not found (\"../../cont/l\") with context node \"/b:cont2/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200393 logbuf_clean();
394
395 /* state -> config */
396 str =
397 "module c {"
398 "namespace urn:a;"
399 "prefix a;"
400 "container cont {"
401 "leaf l {"
402 "type empty;"
403 "}"
404 "}"
405 "container cont2 {"
406 "config false;"
407 "leaf l2 {"
408 "must ../../cont/l;"
409 "type leafref {"
410 "path /cont/l;"
411 "}"
412 "}"
413 "}"
414 "}";
415 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
416 logbuf_assert("");
417
418 /* notif -> state */
419 str =
420 "module d {"
421 "namespace urn:a;"
422 "prefix a;"
423 "container cont {"
424 "config false;"
425 "leaf l {"
426 "type empty;"
427 "}"
428 "}"
429 "notification notif {"
430 "leaf l2 {"
431 "must ../../cont/l;"
432 "type leafref {"
433 "path /cont/l;"
434 "}"
435 "}"
436 "}"
437 "}";
438 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
439 logbuf_assert("");
440
441 /* notif -> notif */
442 str =
443 "module e {"
444 "namespace urn:a;"
445 "prefix a;"
446 "notification notif {"
447 "leaf l {"
448 "type empty;"
449 "}"
450 "leaf l2 {"
451 "must ../../notif/l;"
452 "type leafref {"
453 "path /notif/l;"
454 "}"
455 "}"
456 "}"
457 "}";
458 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
459 logbuf_assert("");
460
461 /* rpc input -> state */
462 str =
463 "module f {"
464 "namespace urn:a;"
465 "prefix a;"
466 "container cont {"
467 "config false;"
468 "leaf l {"
469 "type empty;"
470 "}"
471 "}"
472 "rpc rp {"
473 "input {"
474 "leaf l2 {"
475 "must ../../cont/l;"
476 "type leafref {"
477 "path /cont/l;"
478 "}"
479 "}"
480 "}"
481 "}"
482 "}";
483 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
484 logbuf_assert("");
485
486 /* rpc input -> rpc input */
487 str =
488 "module g {"
489 "namespace urn:a;"
490 "prefix a;"
491 "rpc rp {"
492 "input {"
493 "leaf l {"
494 "type empty;"
495 "}"
496 "leaf l2 {"
497 "must ../l;"
498 "type leafref {"
499 "path /rp/l;"
500 "}"
501 "}"
502 "}"
503 "}"
504 "}";
505 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
506 logbuf_assert("");
507
508 /* rpc input -> rpc output leafref */
509 str =
510 "module h {"
511 "namespace urn:a;"
512 "prefix a;"
513 "rpc rp {"
514 "input {"
515 "leaf l2 {"
516 "type leafref {"
517 "path /rp/l;"
518 "}"
519 "}"
520 "}"
521 "output {"
522 "leaf l {"
523 "type empty;"
524 "}"
525 "}"
526 "}"
527 "}";
528 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
529 logbuf_assert("Not found node \"l\" in path. /h:rp/l2");
530 logbuf_clean();
531
532 /* rpc input -> rpc output must */
533 str =
534 "module h {"
535 "namespace urn:a;"
536 "prefix a;"
537 "rpc rp {"
538 "input {"
539 "leaf l2 {"
540 "must ../l;"
541 "type empty;"
542 "}"
543 "}"
544 "output {"
545 "leaf l {"
546 "type empty;"
547 "}"
548 "}"
549 "}"
550 "}";
551 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100552 logbuf_assert("Schema node \"l\" not found (\"../l\") with context node \"/h:rp/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200553 logbuf_clean();
554
555 /* rpc input -> notif leafref */
556 str =
557 "module i {"
558 "namespace urn:a;"
559 "prefix a;"
560 "rpc rp {"
561 "input {"
562 "leaf l2 {"
563 "type leafref {"
564 "path ../../notif/l;"
565 "}"
566 "}"
567 "}"
568 "}"
569 "notification notif {"
570 "leaf l {"
571 "type empty;"
572 "}"
573 "}"
574 "}";
575 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
576 logbuf_assert("Not found node \"notif\" in path. /i:rp/l2");
577 logbuf_clean();
578
579 /* rpc input -> notif must */
580 str =
581 "module i {"
582 "namespace urn:a;"
583 "prefix a;"
584 "rpc rp {"
585 "input {"
586 "leaf l2 {"
587 "must /notif/l;"
588 "type empty;"
589 "}"
590 "}"
591 "}"
592 "notification notif {"
593 "leaf l {"
594 "type empty;"
595 "}"
596 "}"
597 "}";
598 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100599 logbuf_assert("Schema node \"l\" not found (\"/notif/l\") with context node \"/i:rp/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200600 logbuf_clean();
601
602 /* action output -> state */
603 str =
604 "module j {"
605 "yang-version 1.1;"
606 "namespace urn:a;"
607 "prefix a;"
608 "container cont {"
609 "list ll {"
610 "key k;"
611 "leaf k {"
612 "type string;"
613 "}"
614 "action act {"
615 "output {"
616 "leaf l2 {"
617 "must /cont/l;"
618 "type leafref {"
619 "path ../../../l;"
620 "}"
621 "}"
622 "}"
623 "}"
624 "}"
625 "leaf l {"
626 "config false;"
627 "type empty;"
628 "}"
629 "}"
630 "}";
631 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
632 logbuf_assert("");
633
634 /* action output -> action input leafref */
635 str =
636 "module k {"
637 "yang-version 1.1;"
638 "namespace urn:a;"
639 "prefix a;"
640 "container cont {"
641 "list ll {"
642 "key k;"
643 "leaf k {"
644 "type string;"
645 "}"
646 "action act {"
647 "input {"
648 "leaf l {"
649 "type empty;"
650 "}"
651 "}"
652 "output {"
653 "leaf l2 {"
654 "type leafref {"
655 "path ../l;"
656 "}"
657 "}"
658 "}"
659 "}"
660 "}"
661 "}"
662 "}";
663 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
664 logbuf_assert("Not found node \"l\" in path. /k:cont/ll/act/l2");
665 logbuf_clean();
666
667 /* action output -> action input must */
668 str =
669 "module k {"
670 "yang-version 1.1;"
671 "namespace urn:a;"
672 "prefix a;"
673 "container cont {"
674 "list ll {"
675 "key k;"
676 "leaf k {"
677 "type string;"
678 "}"
679 "action act {"
680 "input {"
681 "leaf l {"
682 "type empty;"
683 "}"
684 "}"
685 "output {"
686 "leaf l2 {"
687 "must /cont/ll/act/l;"
688 "type empty;"
689 "}"
690 "}"
691 "}"
692 "}"
693 "}"
694 "}";
695 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100696 logbuf_assert("Schema node \"l\" not found (\"/cont/ll/act/l\") with context node \"/k:cont/ll/act/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200697 logbuf_clean();
698
699 *state = NULL;
700 ly_ctx_destroy(ctx, NULL);
701}