blob: 8e3bc5da9d1a1bf5aa35e105c786e5760ad03a6e [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
Radek Krejci3a4889a2020-05-19 17:01:58 +020015#include <string.h>
16
Radek Krejci18abde42020-06-13 20:04:39 +020017#include "context.h"
18#include "log.h"
19#include "tree_schema.h"
20#include "tree_schema_internal.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "utests.h"
Radek Krejci18abde42020-06-13 20:04:39 +020022
23#include "test_schema.h"
24
25void
Radek Krejci3a4889a2020-05-19 17:01:58 +020026test_getnext(void **state)
27{
28 *state = test_getnext;
29
30 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +020031 const struct lys_module *mod;
Radek Krejci3a4889a2020-05-19 17:01:58 +020032 const struct lysc_node *node = NULL, *four;
33 const struct lysc_node_container *cont;
34 const struct lysc_action *rpc;
35
36 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
37
Michal Vasko3a41dff2020-07-15 14:30:28 +020038 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010039 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
40 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
41 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
42 " notification nine {leaf nine-data {type string;}}}"
43 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
44 "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
45 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
46 "rpc i;"
47 "notification j {leaf i-data {type string;}}"
48 "notification k;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +020049 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
50 assert_string_equal("a", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010051 cont = (const struct lysc_node_container *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020052 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
53 assert_string_equal("b", node->name);
54 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
55 assert_string_equal("c", node->name);
56 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
57 assert_string_equal("d", node->name);
58 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
59 assert_string_equal("e", node->name);
60 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
61 assert_string_equal("f", node->name);
62 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
63 assert_string_equal("g", node->name);
64 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
65 assert_string_equal("h", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010066 rpc = (const struct lysc_action *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020067 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
68 assert_string_equal("i", node->name);
69 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
70 assert_string_equal("j", node->name);
71 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
72 assert_string_equal("k", node->name);
73 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
74 /* Inside container */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010075 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020076 assert_string_equal("one", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010077 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020078 assert_string_equal("two", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010079 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020080 assert_string_equal("three", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010081 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020082 assert_string_equal("four", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010083 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020084 assert_string_equal("five", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010085 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020086 assert_string_equal("six", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010087 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020088 assert_string_equal("seven", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010089 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020090 assert_string_equal("eight", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010091 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020092 assert_string_equal("nine", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010093 assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020094 /* Inside RPC */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010095 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020096 assert_string_equal("h-input", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010097 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020098
99 /* options */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100100 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200101 assert_string_equal("x", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100102 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200103 assert_string_equal("seven", node->name);
104
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100105 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200106 assert_string_equal("seven", node->name);
107
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100108 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200109 assert_string_equal("five", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100110 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200111 assert_string_equal("y", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100112 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200113 assert_string_equal("seven", node->name);
114
115 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
116 assert_string_equal("one", node->name);
117
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100118 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200119 assert_string_equal("h-output", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100120 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200121
Michal Vasko3a41dff2020-07-15 14:30:28 +0200122 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 +0200123 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
124 assert_string_equal("c", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100125 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200126
Michal Vasko3a41dff2020-07-15 14:30:28 +0200127 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 +0200128 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
129 assert_string_equal("d", 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
Radek Krejcib93bd412020-11-02 13:23:11 +0100132 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));
133 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
134 assert_string_equal("c", node->name);
135 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
136 assert_string_equal("a", node->name);
137
Radek Krejci3a4889a2020-05-19 17:01:58 +0200138 *state = NULL;
139 ly_ctx_destroy(ctx, NULL);
140}
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100141
Radek Krejci18abde42020-06-13 20:04:39 +0200142void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200143test_date(void **state)
144{
145 *state = test_date;
146
147 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
148 logbuf_assert("Invalid argument date (lysp_check_date()).");
149 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
150 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
151 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
152 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
153 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
154 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
155 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
156 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
157 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
158 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
159 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
160 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
161 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
162 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
163 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
164 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
165
166 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
167 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
168 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
169
170 *state = NULL;
171}
172
Radek Krejci18abde42020-06-13 20:04:39 +0200173void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200174test_revisions(void **state)
175{
176 (void) state; /* unused */
177
178 struct lysp_revision *revs = NULL, *rev;
179
180 logbuf_clean();
181 /* no error, it just does nothing */
182 lysp_sort_revisions(NULL);
183 logbuf_assert("");
184
185 /* revisions are stored in wrong order - the newest is the last */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100186 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200187 strcpy(rev->date, "2018-01-01");
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100188 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200189 strcpy(rev->date, "2018-12-31");
190
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200191 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200192 assert_string_equal("2018-01-01", &revs[0]);
193 assert_string_equal("2018-12-31", &revs[1]);
194 /* the order should be fixed, so the newest revision will be the first in the array */
195 lysp_sort_revisions(revs);
196 assert_string_equal("2018-12-31", &revs[0]);
197 assert_string_equal("2018-01-01", &revs[1]);
198
199 LY_ARRAY_FREE(revs);
200}
201
Radek Krejci18abde42020-06-13 20:04:39 +0200202void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200203test_typedef(void **state)
204{
205 *state = test_typedef;
206
207 struct ly_ctx *ctx = NULL;
208 const char *str;
209
210 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
211
212 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100213 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200214 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type. Line number 1.");
215 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100216 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200217 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type. Line number 1.");
218 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100219 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200220 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type. Line number 1.");
221 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100222 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200223 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type. Line number 1.");
224 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100225 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200226 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type. Line number 1.");
227 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100228 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200229 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type. Line number 1.");
230 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100231 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200232 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type. Line number 1.");
233 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100234 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200235 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type. Line number 1.");
236 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100237 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200238 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type. Line number 1.");
239 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100240 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200241 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type. Line number 1.");
242 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100243 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200244 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type. Line number 1.");
245 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100246 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200247 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type. Line number 1.");
248 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100249 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200250 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type. Line number 1.");
251 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100252 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200253 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type. Line number 1.");
254 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100255 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200256 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type. Line number 1.");
257 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100258 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200259 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type. Line number 1.");
260 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100261 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200262 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type. Line number 1.");
263 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100264 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200265 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type. Line number 1.");
266 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100267 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200268 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type. Line number 1.");
269
270 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100271 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
272 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
273 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
274 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200275 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200276
277 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100278 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200279 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type. Line number 1.");
280
281 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 +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 \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
284
285 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 +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 \"y\" of typedef - name collision with another scoped type. Line number 1.");
288
289 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 +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 sibling type. Line number 1.");
292
293 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
294 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100295 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200296 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type. Line number 1.");
297
298 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
299 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100300 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200301 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
302
303 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
304 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
Michal Vasko405cc9e2020-12-01 12:01:27 +0100305 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200306 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1.");
307
308 *state = NULL;
309 ly_ctx_destroy(ctx, NULL);
310}
Michal Vasko6b26e742020-07-17 15:02:10 +0200311
312void
313test_accessible_tree(void **state)
314{
315 *state = test_accessible_tree;
316
317 struct ly_ctx *ctx = NULL;
318 const char *str;
319
320 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
321 logbuf_clean();
322
323 /* config -> config */
324 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100325 "module a {\n"
326 " namespace urn:a;\n"
327 " prefix a;\n"
328 " container cont {\n"
329 " leaf l {\n"
330 " type empty;\n"
331 " }\n"
332 " }\n"
333 " container cont2 {\n"
334 " leaf l2 {\n"
335 " must ../../cont/l;\n"
336 " type leafref {\n"
337 " path /cont/l;\n"
338 " }\n"
339 " }\n"
340 " }\n"
341 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200342 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
343 logbuf_assert("");
344
345 /* config -> state leafref */
346 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100347 "module b {\n"
348 " namespace urn:a;\n"
349 " prefix a;\n"
350 " container cont {\n"
351 " config false;\n"
352 " leaf l {\n"
353 " type empty;\n"
354 " }\n"
355 " }\n"
356 " container cont2 {\n"
357 " leaf l2 {\n"
358 " type leafref {\n"
359 " path /cont/l;\n"
360 " }\n"
361 " }\n"
362 " }\n"
363 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200364 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
365 logbuf_assert("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100366 " (as the leafref does), but it does not. /b:cont2/l2");
Michal Vasko6b26e742020-07-17 15:02:10 +0200367 logbuf_clean();
368
369 /* config -> state must */
370 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100371 "module b {\n"
372 " namespace urn:a;\n"
373 " prefix a;\n"
374 " container cont {\n"
375 " config false;\n"
376 " leaf l {\n"
377 " type empty;\n"
378 " }\n"
379 " }\n"
380 " container cont2 {\n"
381 " leaf l2 {\n"
382 " must ../../cont/l;\n"
383 " type empty;\n"
384 " }\n"
385 " }\n"
386 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200387 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100388 logbuf_assert("Schema node \"l\" not found (\"../../cont/l\") with context node \"/b:cont2/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200389 logbuf_clean();
390
391 /* state -> config */
392 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100393 "module c {\n"
394 " namespace urn:a;\n"
395 " prefix a;\n"
396 " container cont {\n"
397 " leaf l {\n"
398 " type empty;\n"
399 " }\n"
400 " }\n"
401 " container cont2 {\n"
402 " config false;\n"
403 " leaf l2 {\n"
404 " must ../../cont/l;\n"
405 " type leafref {\n"
406 " path /cont/l;\n"
407 " }\n"
408 " }\n"
409 " }\n"
410 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200411 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
412 logbuf_assert("");
413
414 /* notif -> state */
415 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100416 "module d {\n"
417 " namespace urn:a;\n"
418 " prefix a;\n"
419 " container cont {\n"
420 " config false;\n"
421 " leaf l {\n"
422 " type empty;\n"
423 " }\n"
424 " }\n"
425 " notification notif {\n"
426 " leaf l2 {\n"
427 " must ../../cont/l;\n"
428 " type leafref {\n"
429 " path /cont/l;\n"
430 " }\n"
431 " }\n"
432 " }\n"
433 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200434 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
435 logbuf_assert("");
436
437 /* notif -> notif */
438 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100439 "module e {\n"
440 " namespace urn:a;\n"
441 " prefix a;\n"
442 " notification notif {\n"
443 " leaf l {\n"
444 " type empty;\n"
445 " }\n"
446 " leaf l2 {\n"
447 " must ../../notif/l;\n"
448 " type leafref {\n"
449 " path /notif/l;\n"
450 " }\n"
451 " }\n"
452 " }\n"
453 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200454 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
455 logbuf_assert("");
456
457 /* rpc input -> state */
458 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100459 "module f {\n"
460 " namespace urn:a;\n"
461 " prefix a;\n"
462 " container cont {\n"
463 " config false;\n"
464 " leaf l {\n"
465 " type empty;\n"
466 " }\n"
467 " }\n"
468 " rpc rp {\n"
469 " input {\n"
470 " leaf l2 {\n"
471 " must ../../cont/l;\n"
472 " type leafref {\n"
473 " path /cont/l;\n"
474 " }\n"
475 " }\n"
476 " }\n"
477 " }\n"
478 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200479 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
480 logbuf_assert("");
481
482 /* rpc input -> rpc input */
483 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100484 "module g {\n"
485 " namespace urn:a;\n"
486 " prefix a;\n"
487 " rpc rp {\n"
488 " input {\n"
489 " leaf l {\n"
490 " type empty;\n"
491 " }\n"
492 " leaf l2 {\n"
493 " must ../l;\n"
494 " type leafref {\n"
495 " path /rp/l;\n"
496 " }\n"
497 " }\n"
498 " }\n"
499 " }\n"
500 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200501 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
502 logbuf_assert("");
503
504 /* rpc input -> rpc output leafref */
505 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100506 "module h {\n"
507 " namespace urn:a;\n"
508 " prefix a;\n"
509 " rpc rp {\n"
510 " input {\n"
511 " leaf l2 {\n"
512 " type leafref {\n"
513 " path /rp/l;\n"
514 " }\n"
515 " }\n"
516 " }\n"
517 " output {\n"
518 " leaf l {\n"
519 " type empty;\n"
520 " }\n"
521 " }\n"
522 " }\n"
523 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200524 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
525 logbuf_assert("Not found node \"l\" in path. /h:rp/l2");
526 logbuf_clean();
527
528 /* rpc input -> rpc output must */
529 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100530 "module h {\n"
531 " namespace urn:a;\n"
532 " prefix a;\n"
533 " rpc rp {\n"
534 " input {\n"
535 " leaf l2 {\n"
536 " must ../l;\n"
537 " type empty;\n"
538 " }\n"
539 " }\n"
540 " output {\n"
541 " leaf l {\n"
542 " type empty;\n"
543 " }\n"
544 " }\n"
545 " }\n"
546 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200547 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100548 logbuf_assert("Schema node \"l\" not found (\"../l\") with context node \"/h:rp/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200549 logbuf_clean();
550
551 /* rpc input -> notif leafref */
552 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100553 "module i {\n"
554 " namespace urn:a;\n"
555 " prefix a;\n"
556 " rpc rp {\n"
557 " input {\n"
558 " leaf l2 {\n"
559 " type leafref {\n"
560 " path ../../notif/l;\n"
561 " }\n"
562 " }\n"
563 " }\n"
564 " }\n"
565 " notification notif {\n"
566 " leaf l {\n"
567 " type empty;\n"
568 " }\n"
569 " }\n"
570 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200571 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
572 logbuf_assert("Not found node \"notif\" in path. /i:rp/l2");
573 logbuf_clean();
574
575 /* rpc input -> notif must */
576 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100577 "module i {\n"
578 " namespace urn:a;\n"
579 " prefix a;\n"
580 " rpc rp {\n"
581 " input {\n"
582 " leaf l2 {\n"
583 " must /notif/l;\n"
584 " type empty;\n"
585 " }\n"
586 " }\n"
587 " }\n"
588 " notification notif {\n"
589 " leaf l {\n"
590 " type empty;\n"
591 " }\n"
592 " }\n"
593 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200594 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100595 logbuf_assert("Schema node \"l\" not found (\"/notif/l\") with context node \"/i:rp/l2\".");
Michal Vasko6b26e742020-07-17 15:02:10 +0200596 logbuf_clean();
597
598 /* action output -> state */
599 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100600 "module j {\n"
601 " yang-version 1.1;\n"
602 " namespace urn:a;\n"
603 " prefix a;\n"
604 " container cont {\n"
605 " list ll {\n"
606 " key k;\n"
607 " leaf k {\n"
608 " type string;\n"
609 " }\n"
610 " action act {\n"
611 " output {\n"
612 " leaf l2 {\n"
613 " must /cont/l;\n"
614 " type leafref {\n"
615 " path ../../../l;\n"
616 " }\n"
617 " }\n"
618 " }\n"
619 " }\n"
620 " }\n"
621 " leaf l {\n"
622 " config false;\n"
623 " type empty;\n"
624 " }\n"
625 " }\n"
626 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200627 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
628 logbuf_assert("");
629
630 /* action output -> action input leafref */
631 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100632 "module k {\n"
633 " yang-version 1.1;\n"
634 " namespace urn:a;\n"
635 " prefix a;\n"
636 " container cont {\n"
637 " list ll {\n"
638 " key k;\n"
639 " leaf k {\n"
640 " type string;\n"
641 " }\n"
642 " action act {\n"
643 " input {\n"
644 " leaf l {\n"
645 " type empty;\n"
646 " }\n"
647 " }\n"
648 " output {\n"
649 " leaf l2 {\n"
650 " type leafref {\n"
651 " path ../l;\n"
652 " }\n"
653 " }\n"
654 " }\n"
655 " }\n"
656 " }\n"
657 " }\n"
658 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200659 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID);
660 logbuf_assert("Not found node \"l\" in path. /k:cont/ll/act/l2");
661 logbuf_clean();
662
663 /* action output -> action input must */
664 str =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100665 "module k {\n"
666 " yang-version 1.1;\n"
667 " namespace urn:a;\n"
668 " prefix a;\n"
669 " container cont {\n"
670 " list ll {\n"
671 " key k;\n"
672 " leaf k {\n"
673 " type string;\n"
674 " }\n"
675 " action act {\n"
676 " input {\n"
677 " leaf l {\n"
678 " type empty;\n"
679 " }\n"
680 " }\n"
681 " output {\n"
682 " leaf l2 {\n"
683 " must /cont/ll/act/l;\n"
684 " type empty;\n"
685 " }\n"
686 " }\n"
687 " }\n"
688 " }\n"
689 " }\n"
690 "}";
Michal Vasko6b26e742020-07-17 15:02:10 +0200691 assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko90f12dc2020-12-03 14:20:42 +0100692 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 +0200693 logbuf_clean();
694
695 *state = NULL;
696 ly_ctx_destroy(ctx, NULL);
697}