blob: 8cd09747048322b26a7c1a67d593b84179c894a0 [file] [log] [blame]
Radek Krejci3a4889a2020-05-19 17:01:58 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_schema_common.c
Radek Krejci3a4889a2020-05-19 17:01:58 +02003 * @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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#include "test_schema.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020015
Radek Krejci3a4889a2020-05-19 17:01:58 +020016#include <string.h>
17
Radek Krejci18abde42020-06-13 20:04:39 +020018#include "context.h"
19#include "log.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010020#include "tree_edit.h"
Radek Krejci18abde42020-06-13 20:04:39 +020021#include "tree_schema.h"
22#include "tree_schema_internal.h"
Radek Krejci18abde42020-06-13 20:04:39 +020023
aPiecek4f49a142021-06-29 15:32:39 +020024struct module_clb_list {
25 const char *name;
26 const char *data;
27};
28
29static LY_ERR
30module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name,
31 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
32 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
33{
34 struct module_clb_list *list = (struct module_clb_list *)user_data;
35
36 for (unsigned int i = 0; list[i].data; i++) {
37
38 if ((submod_name && !strcmp(list[i].name, submod_name)) ||
39 (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) {
40 *module_data = list[i].data;
41 *format = LYS_IN_YANG;
42 *free_module_data = NULL;
43 return LY_SUCCESS;
44 }
45 }
46 return LY_EINVAL;
47}
48
Radek Krejci18abde42020-06-13 20:04:39 +020049void
Radek Krejci3a4889a2020-05-19 17:01:58 +020050test_getnext(void **state)
51{
Michal Vasko3a41dff2020-07-15 14:30:28 +020052 const struct lys_module *mod;
Radek Krejci3a4889a2020-05-19 17:01:58 +020053 const struct lysc_node *node = NULL, *four;
54 const struct lysc_node_container *cont;
55 const struct lysc_action *rpc;
56
Radek Iša56ca9e42020-09-08 18:42:00 +020057 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {yang-version 1.1; namespace urn:a;prefix a;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010058 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
59 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
60 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
61 " notification nine {leaf nine-data {type string;}}}"
62 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
63 "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
64 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
65 "rpc i;"
66 "notification j {leaf i-data {type string;}}"
67 "notification k;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +020068 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
69 assert_string_equal("a", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010070 cont = (const struct lysc_node_container *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020071 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
72 assert_string_equal("b", node->name);
73 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
74 assert_string_equal("c", node->name);
75 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
76 assert_string_equal("d", node->name);
77 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
78 assert_string_equal("e", node->name);
79 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
80 assert_string_equal("f", node->name);
81 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
82 assert_string_equal("g", node->name);
83 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
84 assert_string_equal("h", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010085 rpc = (const struct lysc_action *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020086 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
87 assert_string_equal("i", node->name);
88 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
89 assert_string_equal("j", node->name);
90 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
91 assert_string_equal("k", node->name);
92 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
93 /* Inside container */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010094 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020095 assert_string_equal("one", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010096 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020097 assert_string_equal("two", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010098 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020099 assert_string_equal("three", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100100 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200101 assert_string_equal("four", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100102 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200103 assert_string_equal("five", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100104 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200105 assert_string_equal("six", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100106 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200107 assert_string_equal("seven", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100108 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200109 assert_string_equal("eight", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100110 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200111 assert_string_equal("nine", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100112 assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200113 /* Inside RPC */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100114 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200115 assert_string_equal("h-input", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100116 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200117
118 /* options */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100119 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200120 assert_string_equal("x", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100121 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200122 assert_string_equal("seven", node->name);
123
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100124 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200125 assert_string_equal("seven", node->name);
126
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100127 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200128 assert_string_equal("five", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100129 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200130 assert_string_equal("y", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100131 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200132 assert_string_equal("seven", node->name);
133
134 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
135 assert_string_equal("one", node->name);
136
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100137 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200138 assert_string_equal("h-output", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100139 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200140
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200142 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
143 assert_string_equal("c", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100144 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200145
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200147 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
148 assert_string_equal("d", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100149 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200150
Radek Iša56ca9e42020-09-08 18:42:00 +0200151 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module e {namespace urn:e;prefix e; container c {container cc;} leaf a {type string;}}", LYS_IN_YANG, &mod));
Radek Krejcib93bd412020-11-02 13:23:11 +0100152 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
153 assert_string_equal("c", node->name);
154 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
155 assert_string_equal("a", node->name);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200156}
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100157
Radek Krejci18abde42020-06-13 20:04:39 +0200158void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200159test_date(void **state)
160{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200161 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200162 CHECK_LOG("Invalid argument date (lysp_check_date()).", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200163 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200164 CHECK_LOG("Invalid argument date_len (lysp_check_date()).", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200165 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200166 CHECK_LOG("Invalid value \"nonsencexx\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200167 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200168 CHECK_LOG("Invalid value \"123x-11-11\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200169 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200170 CHECK_LOG("Invalid value \"2018-13-11\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200171 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200172 CHECK_LOG("Invalid value \"2018-11-41\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200173 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200174 CHECK_LOG("Invalid value \"2018-02-29\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200175 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 CHECK_LOG("Invalid value \"2018.02-28\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200177 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 CHECK_LOG("Invalid value \"2018-02.28\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200179
180 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
181 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
182 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200183}
184
Radek Krejci18abde42020-06-13 20:04:39 +0200185void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200186test_revisions(void **state)
187{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200188 struct lysp_revision *revs = NULL, *rev;
189
Radek Krejci3a4889a2020-05-19 17:01:58 +0200190 /* no error, it just does nothing */
191 lysp_sort_revisions(NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 CHECK_LOG(NULL, NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200193
194 /* revisions are stored in wrong order - the newest is the last */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100195 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200196 strcpy(rev->date, "2018-01-01");
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100197 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200198 strcpy(rev->date, "2018-12-31");
199
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200200 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200201 assert_string_equal("2018-01-01", &revs[0]);
202 assert_string_equal("2018-12-31", &revs[1]);
203 /* the order should be fixed, so the newest revision will be the first in the array */
204 lysp_sort_revisions(revs);
205 assert_string_equal("2018-12-31", &revs[0]);
206 assert_string_equal("2018-01-01", &revs[1]);
207
208 LY_ARRAY_FREE(revs);
209}
210
Radek Krejci18abde42020-06-13 20:04:39 +0200211void
aPiecek4f49a142021-06-29 15:32:39 +0200212test_collision_typedef(void **state)
Radek Krejci3a4889a2020-05-19 17:01:58 +0200213{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200214 const char *str;
aPiecek28afeef2021-06-30 07:57:18 +0200215 char *submod;
216 struct module_clb_list list[3] = {0};
217
218 list[0].name = "asub";
219 list[1].name = "bsub";
Radek Krejci3a4889a2020-05-19 17:01:58 +0200220
aPiecek4f49a142021-06-29 15:32:39 +0200221 /* collision with a built-in type */
Radek Krejci3a4889a2020-05-19 17:01:58 +0200222 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200223 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
224 CHECK_LOG("Duplicate identifier \"binary\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200225 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200226 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
227 CHECK_LOG("Duplicate identifier \"bits\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200228 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200229 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
230 CHECK_LOG("Duplicate identifier \"boolean\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200231 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200232 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
233 CHECK_LOG("Duplicate identifier \"decimal64\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200234 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200235 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
236 CHECK_LOG("Duplicate identifier \"empty\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200237 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200238 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
239 CHECK_LOG("Duplicate identifier \"enumeration\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200240 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200241 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
242 CHECK_LOG("Duplicate identifier \"int8\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200243 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200244 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
245 CHECK_LOG("Duplicate identifier \"int16\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200246 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200247 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
248 CHECK_LOG("Duplicate identifier \"int32\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200249 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200250 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
251 CHECK_LOG("Duplicate identifier \"int64\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200252 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200253 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
254 CHECK_LOG("Duplicate identifier \"instance-identifier\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200255 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200256 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
257 CHECK_LOG("Duplicate identifier \"identityref\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200258 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200259 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
260 CHECK_LOG("Duplicate identifier \"leafref\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200261 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200262 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
263 CHECK_LOG("Duplicate identifier \"string\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200264 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200265 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
266 CHECK_LOG("Duplicate identifier \"union\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200267 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200268 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
269 CHECK_LOG("Duplicate identifier \"uint8\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200270 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200271 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
272 CHECK_LOG("Duplicate identifier \"uint16\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200273 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200274 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
275 CHECK_LOG("Duplicate identifier \"uint32\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200276 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200277 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
278 CHECK_LOG("Duplicate identifier \"uint64\" of typedef statement - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200279
280 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 +0100281 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
282 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
283 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
284 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200285 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200286
aPiecek4f49a142021-06-29 15:32:39 +0200287 /* collision in node's scope */
Radek Krejci3a4889a2020-05-19 17:01:58 +0200288 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
aPiecekc7594b72021-06-29 09:00:03 +0200289 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
290 CHECK_LOG("Duplicate identifier \"y\" of typedef statement - name collision with sibling type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200291
aPiecek4f49a142021-06-29 15:32:39 +0200292 /* collision with parent node */
293 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
aPiecek8d4e75d2021-06-24 14:47:06 +0200294 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
aPiecek4f49a142021-06-29 15:32:39 +0200295 CHECK_LOG("Duplicate identifier \"y\" of typedef statement - name collision with another scoped type.", NULL);
aPiecek8d4e75d2021-06-24 14:47:06 +0200296
aPiecek4f49a142021-06-29 15:32:39 +0200297 /* collision with module's top-level */
298 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
aPiecekc7594b72021-06-29 09:00:03 +0200299 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
aPiecek4f49a142021-06-29 15:32:39 +0200300 CHECK_LOG("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200301
aPiecek4f49a142021-06-29 15:32:39 +0200302 /* collision of submodule's node with module's top-level */
Radek Iša56ca9e42020-09-08 18:42:00 +0200303 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200304 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
aPiecekc7594b72021-06-29 09:00:03 +0200305 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
306 CHECK_LOG("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200307
aPiecek4f49a142021-06-29 15:32:39 +0200308 /* collision of module's node with submodule's top-level */
Radek Iša56ca9e42020-09-08 18:42:00 +0200309 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200310 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
aPiecekc7594b72021-06-29 09:00:03 +0200311 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
312 CHECK_LOG("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
aPiecek4f49a142021-06-29 15:32:39 +0200313
314 /* collision of submodule's node with another submodule's top-level */
aPiecek28afeef2021-06-30 07:57:18 +0200315 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
316 list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
317 list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {typedef g {type int;}}}";
318 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
319 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
320 CHECK_LOG("Duplicate identifier \"g\" of typedef statement - scoped type collide with a top-level type.", NULL);
aPiecek4f49a142021-06-29 15:32:39 +0200321
322 /* collision of module's top-levels */
323 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
324 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
325 CHECK_LOG("Duplicate identifier \"test\" of typedef statement - name collision with another top-level type.", NULL);
326
327 /* collision of submodule's top-levels */
aPiecek28afeef2021-06-30 07:57:18 +0200328 submod = "submodule asub {belongs-to a {prefix a;} typedef g {type int;} typedef g {type int;}}";
329 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
330 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
331 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
332 CHECK_LOG("Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
aPiecek4f49a142021-06-29 15:32:39 +0200333
334 /* collision of module's top-level with submodule's top-levels */
335 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
336 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
337 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
338 CHECK_LOG("Duplicate identifier \"x\" of typedef statement - name collision with another top-level type.", NULL);
339
340 /* collision of submodule's top-level with another submodule's top-levels */
aPiecek28afeef2021-06-30 07:57:18 +0200341 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
342 list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
343 list[1].data = "submodule bsub {belongs-to a {prefix a;} typedef g {type int;}}";
344 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
345 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
346 CHECK_LOG("Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
aPiecek4f49a142021-06-29 15:32:39 +0200347
348 /* error in type-stmt */
349 str = "module a {namespace urn:a; prefix a; container c {typedef x {type t{}}";
350 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
351 CHECK_STRING(_UC->err_msg, "Unexpected end-of-input.");
352 UTEST_LOG_CLEAN;
353
354 /* no collision if the same names are in different scope */
aPiecek28afeef2021-06-30 07:57:18 +0200355 str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
356 "container c {typedef g {type int;}} container d {typedef g {type int;}}}";
357 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200358}
Michal Vasko6b26e742020-07-17 15:02:10 +0200359
360void
aPiecek14d07f02021-06-30 09:46:50 +0200361test_collision_identity(void **state)
362{
363 const char *str;
364 char *submod;
365
366 /* collision of module's top-levels */
367 str = "module a {yang-version 1.1; namespace urn:a; prefix a; identity g; identity g;}";
368 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
369 CHECK_LOG("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
370
371 /* collision of module's top-level with submodule's top-levels */
372 submod = "submodule asub {belongs-to a {prefix a;} identity g;}";
373 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; identity g;}";
374 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
375 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
376 CHECK_LOG("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
377}
378
379void
380test_collision_feature(void **state)
381{
382 const char *str;
383 char *submod;
384
385 /* collision of module's top-levels */
386 str = "module a {yang-version 1.1; namespace urn:a; prefix a; feature g; feature g;}";
387 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
388 CHECK_LOG("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
389
390 /* collision of module's top-level with submodule's top-levels */
391 submod = "submodule asub {belongs-to a {prefix a;} feature g;}";
392 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; feature g;}";
393 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
394 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
395 CHECK_LOG("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
396}
397
398void
Michal Vasko6b26e742020-07-17 15:02:10 +0200399test_accessible_tree(void **state)
400{
Michal Vasko6b26e742020-07-17 15:02:10 +0200401 const char *str;
402
Michal Vasko6b26e742020-07-17 15:02:10 +0200403 /* config -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200404 str = "module a {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100405 " namespace urn:a;\n"
406 " prefix a;\n"
407 " container cont {\n"
408 " leaf l {\n"
409 " type empty;\n"
410 " }\n"
411 " }\n"
412 " container cont2 {\n"
413 " leaf l2 {\n"
414 " must ../../cont/l;\n"
415 " type leafref {\n"
416 " path /cont/l;\n"
417 " }\n"
418 " }\n"
419 " }\n"
420 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200421 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
422 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200423
424 /* config -> state leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200425 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100426 " namespace urn:a;\n"
427 " prefix a;\n"
428 " container cont {\n"
429 " config false;\n"
430 " leaf l {\n"
431 " type empty;\n"
432 " }\n"
433 " }\n"
434 " container cont2 {\n"
435 " leaf l2 {\n"
436 " type leafref {\n"
437 " path /cont/l;\n"
438 " }\n"
439 " }\n"
440 " }\n"
441 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200442 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
443 CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100444 " (as the leafref does), but it does not.", "Schema location /b:cont2/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200445
446 /* config -> state must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200447 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100448 " namespace urn:a;\n"
449 " prefix a;\n"
450 " container cont {\n"
451 " config false;\n"
452 " leaf l {\n"
453 " type empty;\n"
454 " }\n"
455 " }\n"
456 " container cont2 {\n"
457 " leaf l2 {\n"
458 " must ../../cont/l;\n"
459 " type empty;\n"
460 " }\n"
461 " }\n"
462 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200463 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
464 CHECK_LOG_CTX("Schema node \"l\" not found (\"../../cont/l\") with context node \"/b:cont2/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200465
466 /* state -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200467 str = "module c {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100468 " namespace urn:a;\n"
469 " prefix a;\n"
470 " container cont {\n"
471 " leaf l {\n"
472 " type empty;\n"
473 " }\n"
474 " }\n"
475 " container cont2 {\n"
476 " config false;\n"
477 " leaf l2 {\n"
478 " must ../../cont/l;\n"
479 " type leafref {\n"
480 " path /cont/l;\n"
481 " }\n"
482 " }\n"
483 " }\n"
484 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200485 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
486 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200487
488 /* notif -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 str = "module d {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100490 " namespace urn:a;\n"
491 " prefix a;\n"
492 " container cont {\n"
493 " config false;\n"
494 " leaf l {\n"
495 " type empty;\n"
496 " }\n"
497 " }\n"
498 " notification notif {\n"
499 " leaf l2 {\n"
500 " must ../../cont/l;\n"
501 " type leafref {\n"
502 " path /cont/l;\n"
503 " }\n"
504 " }\n"
505 " }\n"
506 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200507 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
508 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200509
510 /* notif -> notif */
Radek Iša56ca9e42020-09-08 18:42:00 +0200511 str = "module e {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100512 " namespace urn:a;\n"
513 " prefix a;\n"
514 " notification notif {\n"
515 " leaf l {\n"
516 " type empty;\n"
517 " }\n"
518 " leaf l2 {\n"
519 " must ../../notif/l;\n"
520 " type leafref {\n"
521 " path /notif/l;\n"
522 " }\n"
523 " }\n"
524 " }\n"
525 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200526 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
527 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200528
529 /* rpc input -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200530 str = "module f {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100531 " namespace urn:a;\n"
532 " prefix a;\n"
533 " container cont {\n"
534 " config false;\n"
535 " leaf l {\n"
536 " type empty;\n"
537 " }\n"
538 " }\n"
539 " rpc rp {\n"
540 " input {\n"
541 " leaf l2 {\n"
542 " must ../../cont/l;\n"
543 " type leafref {\n"
544 " path /cont/l;\n"
545 " }\n"
546 " }\n"
547 " }\n"
548 " }\n"
549 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200550 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
551 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200552
553 /* rpc input -> rpc input */
Radek Iša56ca9e42020-09-08 18:42:00 +0200554 str = "module g {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100555 " namespace urn:a;\n"
556 " prefix a;\n"
557 " rpc rp {\n"
558 " input {\n"
559 " leaf l {\n"
560 " type empty;\n"
561 " }\n"
562 " leaf l2 {\n"
563 " must ../l;\n"
564 " type leafref {\n"
565 " path /rp/l;\n"
566 " }\n"
567 " }\n"
568 " }\n"
569 " }\n"
570 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200571 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
572 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200573
574 /* rpc input -> rpc output leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200575 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100576 " namespace urn:a;\n"
577 " prefix a;\n"
578 " rpc rp {\n"
579 " input {\n"
580 " leaf l2 {\n"
581 " type leafref {\n"
582 " path /rp/l;\n"
583 " }\n"
584 " }\n"
585 " }\n"
586 " output {\n"
587 " leaf l {\n"
588 " type empty;\n"
589 " }\n"
590 " }\n"
591 " }\n"
592 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200593 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100594 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location /h:rp/input/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200595
596 /* rpc input -> rpc output must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200597 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100598 " namespace urn:a;\n"
599 " prefix a;\n"
600 " rpc rp {\n"
601 " input {\n"
602 " leaf l2 {\n"
603 " must ../l;\n"
604 " type empty;\n"
605 " }\n"
606 " }\n"
607 " output {\n"
608 " leaf l {\n"
609 " type empty;\n"
610 " }\n"
611 " }\n"
612 " }\n"
613 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200614 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100615 CHECK_LOG_CTX("Schema node \"l\" not found (\"../l\") with context node \"/h:rp/input/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200616
617 /* rpc input -> notif leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200618 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100619 " namespace urn:a;\n"
620 " prefix a;\n"
621 " rpc rp {\n"
622 " input {\n"
623 " leaf l2 {\n"
624 " type leafref {\n"
625 " path ../../notif/l;\n"
626 " }\n"
627 " }\n"
628 " }\n"
629 " }\n"
630 " notification notif {\n"
631 " leaf l {\n"
632 " type empty;\n"
633 " }\n"
634 " }\n"
635 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200636 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100637 CHECK_LOG_CTX("Not found node \"notif\" in path.", "Schema location /i:rp/input/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200638
639 /* rpc input -> notif must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200640 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100641 " namespace urn:a;\n"
642 " prefix a;\n"
643 " rpc rp {\n"
644 " input {\n"
645 " leaf l2 {\n"
646 " must /notif/l;\n"
647 " type empty;\n"
648 " }\n"
649 " }\n"
650 " }\n"
651 " notification notif {\n"
652 " leaf l {\n"
653 " type empty;\n"
654 " }\n"
655 " }\n"
656 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200657 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100658 CHECK_LOG_CTX("Schema node \"l\" not found (\"/notif/l\") with context node \"/i:rp/input/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200659
660 /* action output -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200661 str = "module j {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100662 " yang-version 1.1;\n"
663 " namespace urn:a;\n"
664 " prefix a;\n"
665 " container cont {\n"
666 " list ll {\n"
667 " key k;\n"
668 " leaf k {\n"
669 " type string;\n"
670 " }\n"
671 " action act {\n"
672 " output {\n"
673 " leaf l2 {\n"
674 " must /cont/l;\n"
675 " type leafref {\n"
676 " path ../../../l;\n"
677 " }\n"
678 " }\n"
679 " }\n"
680 " }\n"
681 " }\n"
682 " leaf l {\n"
683 " config false;\n"
684 " type empty;\n"
685 " }\n"
686 " }\n"
687 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200688 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
689 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200690
691 /* action output -> action input leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200692 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100693 " yang-version 1.1;\n"
694 " namespace urn:a;\n"
695 " prefix a;\n"
696 " container cont {\n"
697 " list ll {\n"
698 " key k;\n"
699 " leaf k {\n"
700 " type string;\n"
701 " }\n"
702 " action act {\n"
703 " input {\n"
704 " leaf l {\n"
705 " type empty;\n"
706 " }\n"
707 " }\n"
708 " output {\n"
709 " leaf l2 {\n"
710 " type leafref {\n"
711 " path ../l;\n"
712 " }\n"
713 " }\n"
714 " }\n"
715 " }\n"
716 " }\n"
717 " }\n"
718 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200719 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100720 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location /k:cont/ll/act/output/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200721
722 /* action output -> action input must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200723 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100724 " yang-version 1.1;\n"
725 " namespace urn:a;\n"
726 " prefix a;\n"
727 " container cont {\n"
728 " list ll {\n"
729 " key k;\n"
730 " leaf k {\n"
731 " type string;\n"
732 " }\n"
733 " action act {\n"
734 " input {\n"
735 " leaf l {\n"
736 " type empty;\n"
737 " }\n"
738 " }\n"
739 " output {\n"
740 " leaf l2 {\n"
741 " must /cont/ll/act/l;\n"
742 " type empty;\n"
743 " }\n"
744 " }\n"
745 " }\n"
746 " }\n"
747 " }\n"
748 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200749 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100750 CHECK_LOG_CTX("Schema node \"l\" not found (\"/cont/ll/act/l\") with context node \"/k:cont/ll/act/output/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200751}
Radek Krejci589c5472021-01-20 10:29:06 +0100752
Radek Krejci589c5472021-01-20 10:29:06 +0100753void
754test_includes(void **state)
755{
756 const struct lys_module *mod;
757
758 {
759 /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */
760 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100761 {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"},
762 {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"},
763 {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"},
764 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100765 };
766 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
767 mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL);
768 assert_non_null(mod);
769 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
770 assert_true(mod->parsed->includes[1].injected);
771 }
772
773 {
774 /* YANG 1.1 - the missing include sub_b_two in main_b is error */
775 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100776 {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"},
777 {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"},
778 {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"},
779 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100780 };
781 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
782 mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL);
783 assert_null(mod);
784 CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL,
785 "Data model \"main_b\" not found in local searchdirs.", NULL,
786 "Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL,
787 "Data model \"sub_b_one\" not found in local searchdirs.", NULL,
788 "YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes "
789 "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL);
790 }
791
792 {
793 /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */
794 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100795 {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"},
796 {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"},
797 {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } }"},
798 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100799 };
800 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
801 mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL);
802 assert_non_null(mod);
803 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
804 assert_false(mod->parsed->includes[1].injected);
805 /* result is ok, but log includes the warning */
806 CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_one) are not necessary.", NULL);
807 }
808}