blob: e5d20f4d338bad6d2e46eaaf9f9c0d97a06b189e [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
24void
Radek Krejci3a4889a2020-05-19 17:01:58 +020025test_getnext(void **state)
26{
Michal Vasko3a41dff2020-07-15 14:30:28 +020027 const struct lys_module *mod;
Radek Krejci3a4889a2020-05-19 17:01:58 +020028 const struct lysc_node *node = NULL, *four;
29 const struct lysc_node_container *cont;
30 const struct lysc_action *rpc;
31
Radek Iša56ca9e42020-09-08 18:42:00 +020032 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 +010033 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
34 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
35 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
36 " notification nine {leaf nine-data {type string;}}}"
37 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
38 "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
39 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
40 "rpc i;"
41 "notification j {leaf i-data {type string;}}"
42 "notification k;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +020043 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
44 assert_string_equal("a", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010045 cont = (const struct lysc_node_container *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020046 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
47 assert_string_equal("b", node->name);
48 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
49 assert_string_equal("c", node->name);
50 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
51 assert_string_equal("d", node->name);
52 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
53 assert_string_equal("e", node->name);
54 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
55 assert_string_equal("f", node->name);
56 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
57 assert_string_equal("g", node->name);
58 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
59 assert_string_equal("h", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010060 rpc = (const struct lysc_action *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020061 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
62 assert_string_equal("i", node->name);
63 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
64 assert_string_equal("j", node->name);
65 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
66 assert_string_equal("k", node->name);
67 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
68 /* Inside container */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010069 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020070 assert_string_equal("one", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010071 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020072 assert_string_equal("two", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010073 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020074 assert_string_equal("three", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010075 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020076 assert_string_equal("four", 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("five", 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("six", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010081 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020082 assert_string_equal("seven", 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("eight", 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("nine", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010087 assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020088 /* Inside RPC */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010089 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020090 assert_string_equal("h-input", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010091 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020092
93 /* options */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010094 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +020095 assert_string_equal("x", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010096 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +020097 assert_string_equal("seven", node->name);
98
Radek Krejcib4ac5a92020-11-23 17:54:33 +010099 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200100 assert_string_equal("seven", node->name);
101
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100102 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
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, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200105 assert_string_equal("y", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100106 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200107 assert_string_equal("seven", node->name);
108
109 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
110 assert_string_equal("one", node->name);
111
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100112 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200113 assert_string_equal("h-output", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100114 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200115
Radek Iša56ca9e42020-09-08 18:42:00 +0200116 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 +0200117 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
118 assert_string_equal("c", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100119 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200120
Radek Iša56ca9e42020-09-08 18:42:00 +0200121 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 +0200122 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
123 assert_string_equal("d", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100124 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200125
Radek Iša56ca9e42020-09-08 18:42:00 +0200126 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 +0100127 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
128 assert_string_equal("c", node->name);
129 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
130 assert_string_equal("a", node->name);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200131}
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100132
Radek Krejci18abde42020-06-13 20:04:39 +0200133void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200134test_date(void **state)
135{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200136 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200137 CHECK_LOG("Invalid argument date (lysp_check_date()).", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200138 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 CHECK_LOG("Invalid argument date_len (lysp_check_date()).", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200140 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 CHECK_LOG("Invalid value \"nonsencexx\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200142 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 CHECK_LOG("Invalid value \"123x-11-11\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200144 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200145 CHECK_LOG("Invalid value \"2018-13-11\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200146 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 CHECK_LOG("Invalid value \"2018-11-41\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200148 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200149 CHECK_LOG("Invalid value \"2018-02-29\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200150 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200151 CHECK_LOG("Invalid value \"2018.02-28\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200152 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200153 CHECK_LOG("Invalid value \"2018-02.28\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200154
155 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
156 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
157 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200158}
159
Radek Krejci18abde42020-06-13 20:04:39 +0200160void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200161test_revisions(void **state)
162{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200163 struct lysp_revision *revs = NULL, *rev;
164
Radek Krejci3a4889a2020-05-19 17:01:58 +0200165 /* no error, it just does nothing */
166 lysp_sort_revisions(NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200167 CHECK_LOG(NULL, NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200168
169 /* revisions are stored in wrong order - the newest is the last */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100170 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200171 strcpy(rev->date, "2018-01-01");
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100172 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200173 strcpy(rev->date, "2018-12-31");
174
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200175 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200176 assert_string_equal("2018-01-01", &revs[0]);
177 assert_string_equal("2018-12-31", &revs[1]);
178 /* the order should be fixed, so the newest revision will be the first in the array */
179 lysp_sort_revisions(revs);
180 assert_string_equal("2018-12-31", &revs[0]);
181 assert_string_equal("2018-01-01", &revs[1]);
182
183 LY_ARRAY_FREE(revs);
184}
185
Radek Krejci18abde42020-06-13 20:04:39 +0200186void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200187test_typedef(void **state)
188{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200189 const char *str;
190
Radek Krejci3a4889a2020-05-19 17:01:58 +0200191 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100193 CHECK_LOG("Invalid name \"binary\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200194 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200195 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100196 CHECK_LOG("Invalid name \"bits\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200197 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200198 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100199 CHECK_LOG("Invalid name \"boolean\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200200 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100202 CHECK_LOG("Invalid name \"decimal64\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200203 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200204 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100205 CHECK_LOG("Invalid name \"empty\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200206 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100208 CHECK_LOG("Invalid name \"enumeration\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200209 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100211 CHECK_LOG("Invalid name \"int8\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200212 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200213 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100214 CHECK_LOG("Invalid name \"int16\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200215 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200216 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100217 CHECK_LOG("Invalid name \"int32\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200218 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200219 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100220 CHECK_LOG("Invalid name \"int64\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200221 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200222 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100223 CHECK_LOG("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200224 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200225 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100226 CHECK_LOG("Invalid name \"identityref\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200227 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200228 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100229 CHECK_LOG("Invalid name \"leafref\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200230 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200231 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100232 CHECK_LOG("Invalid name \"string\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200233 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100235 CHECK_LOG("Invalid name \"union\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200236 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200237 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100238 CHECK_LOG("Invalid name \"uint8\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200239 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100241 CHECK_LOG("Invalid name \"uint16\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200242 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200243 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100244 CHECK_LOG("Invalid name \"uint32\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200245 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200246 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100247 CHECK_LOG("Invalid name \"uint64\" of typedef - name collision with a built-in type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200248
249 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 +0100250 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
251 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
252 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
253 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200255
256 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100258 CHECK_LOG("Invalid name \"test\" of typedef - name collision with another top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200259
260 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200261 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100262 CHECK_LOG("Invalid name \"x\" of typedef - scoped type collide with a top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200263
264 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100266 CHECK_LOG("Invalid name \"y\" of typedef - name collision with another scoped type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200267
268 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200269 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100270 CHECK_LOG("Invalid name \"y\" of typedef - name collision with sibling type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200271
aPiecek8d4e75d2021-06-24 14:47:06 +0200272 str = "module a {namespace urn:a; prefix a; container c {typedef x {type t{}}";
273 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
274 CHECK_STRING(_UC->err_msg, "Unexpected end-of-input.");
275 UTEST_LOG_CLEAN;
276
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200278 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200279 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100280 CHECK_LOG("Invalid name \"x\" of typedef - name collision with another top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200281
Radek Iša56ca9e42020-09-08 18:42:00 +0200282 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 +0200283 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200284 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100285 CHECK_LOG("Invalid name \"x\" of typedef - scoped type collide with a top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200286
Radek Iša56ca9e42020-09-08 18:42:00 +0200287 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 +0200288 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200289 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100290 CHECK_LOG("Invalid name \"x\" of typedef - scoped type collide with a top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200291}
Michal Vasko6b26e742020-07-17 15:02:10 +0200292
293void
294test_accessible_tree(void **state)
295{
Michal Vasko6b26e742020-07-17 15:02:10 +0200296 const char *str;
297
Michal Vasko6b26e742020-07-17 15:02:10 +0200298 /* config -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200299 str = "module a {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100300 " namespace urn:a;\n"
301 " prefix a;\n"
302 " container cont {\n"
303 " leaf l {\n"
304 " type empty;\n"
305 " }\n"
306 " }\n"
307 " container cont2 {\n"
308 " leaf l2 {\n"
309 " must ../../cont/l;\n"
310 " type leafref {\n"
311 " path /cont/l;\n"
312 " }\n"
313 " }\n"
314 " }\n"
315 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200316 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
317 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200318
319 /* config -> state leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200320 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100321 " namespace urn:a;\n"
322 " prefix a;\n"
323 " container cont {\n"
324 " config false;\n"
325 " leaf l {\n"
326 " type empty;\n"
327 " }\n"
328 " }\n"
329 " container cont2 {\n"
330 " leaf l2 {\n"
331 " type leafref {\n"
332 " path /cont/l;\n"
333 " }\n"
334 " }\n"
335 " }\n"
336 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200337 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
338 CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100339 " (as the leafref does), but it does not.", "Schema location /b:cont2/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200340
341 /* config -> state must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200342 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100343 " namespace urn:a;\n"
344 " prefix a;\n"
345 " container cont {\n"
346 " config false;\n"
347 " leaf l {\n"
348 " type empty;\n"
349 " }\n"
350 " }\n"
351 " container cont2 {\n"
352 " leaf l2 {\n"
353 " must ../../cont/l;\n"
354 " type empty;\n"
355 " }\n"
356 " }\n"
357 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200358 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
359 CHECK_LOG_CTX("Schema node \"l\" not found (\"../../cont/l\") with context node \"/b:cont2/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200360
361 /* state -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200362 str = "module c {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100363 " namespace urn:a;\n"
364 " prefix a;\n"
365 " container cont {\n"
366 " leaf l {\n"
367 " type empty;\n"
368 " }\n"
369 " }\n"
370 " container cont2 {\n"
371 " config false;\n"
372 " leaf l2 {\n"
373 " must ../../cont/l;\n"
374 " type leafref {\n"
375 " path /cont/l;\n"
376 " }\n"
377 " }\n"
378 " }\n"
379 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200380 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
381 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200382
383 /* notif -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200384 str = "module d {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100385 " namespace urn:a;\n"
386 " prefix a;\n"
387 " container cont {\n"
388 " config false;\n"
389 " leaf l {\n"
390 " type empty;\n"
391 " }\n"
392 " }\n"
393 " notification notif {\n"
394 " leaf l2 {\n"
395 " must ../../cont/l;\n"
396 " type leafref {\n"
397 " path /cont/l;\n"
398 " }\n"
399 " }\n"
400 " }\n"
401 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200402 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
403 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200404
405 /* notif -> notif */
Radek Iša56ca9e42020-09-08 18:42:00 +0200406 str = "module e {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100407 " namespace urn:a;\n"
408 " prefix a;\n"
409 " notification notif {\n"
410 " leaf l {\n"
411 " type empty;\n"
412 " }\n"
413 " leaf l2 {\n"
414 " must ../../notif/l;\n"
415 " type leafref {\n"
416 " path /notif/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 /* rpc input -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200425 str = "module f {\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 " rpc rp {\n"
435 " input {\n"
436 " leaf l2 {\n"
437 " must ../../cont/l;\n"
438 " type leafref {\n"
439 " path /cont/l;\n"
440 " }\n"
441 " }\n"
442 " }\n"
443 " }\n"
444 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200445 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
446 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200447
448 /* rpc input -> rpc input */
Radek Iša56ca9e42020-09-08 18:42:00 +0200449 str = "module g {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100450 " namespace urn:a;\n"
451 " prefix a;\n"
452 " rpc rp {\n"
453 " input {\n"
454 " leaf l {\n"
455 " type empty;\n"
456 " }\n"
457 " leaf l2 {\n"
458 " must ../l;\n"
459 " type leafref {\n"
460 " path /rp/l;\n"
461 " }\n"
462 " }\n"
463 " }\n"
464 " }\n"
465 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200466 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
467 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200468
469 /* rpc input -> rpc output leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200470 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100471 " namespace urn:a;\n"
472 " prefix a;\n"
473 " rpc rp {\n"
474 " input {\n"
475 " leaf l2 {\n"
476 " type leafref {\n"
477 " path /rp/l;\n"
478 " }\n"
479 " }\n"
480 " }\n"
481 " output {\n"
482 " leaf l {\n"
483 " type empty;\n"
484 " }\n"
485 " }\n"
486 " }\n"
487 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200488 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100489 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location /h:rp/input/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200490
491 /* rpc input -> rpc output must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200492 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100493 " namespace urn:a;\n"
494 " prefix a;\n"
495 " rpc rp {\n"
496 " input {\n"
497 " leaf l2 {\n"
498 " must ../l;\n"
499 " type empty;\n"
500 " }\n"
501 " }\n"
502 " output {\n"
503 " leaf l {\n"
504 " type empty;\n"
505 " }\n"
506 " }\n"
507 " }\n"
508 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200509 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100510 CHECK_LOG_CTX("Schema node \"l\" not found (\"../l\") with context node \"/h:rp/input/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200511
512 /* rpc input -> notif leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200513 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100514 " namespace urn:a;\n"
515 " prefix a;\n"
516 " rpc rp {\n"
517 " input {\n"
518 " leaf l2 {\n"
519 " type leafref {\n"
520 " path ../../notif/l;\n"
521 " }\n"
522 " }\n"
523 " }\n"
524 " }\n"
525 " notification notif {\n"
526 " leaf l {\n"
527 " type empty;\n"
528 " }\n"
529 " }\n"
530 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200531 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100532 CHECK_LOG_CTX("Not found node \"notif\" in path.", "Schema location /i:rp/input/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200533
534 /* rpc input -> notif must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200535 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100536 " namespace urn:a;\n"
537 " prefix a;\n"
538 " rpc rp {\n"
539 " input {\n"
540 " leaf l2 {\n"
541 " must /notif/l;\n"
542 " type empty;\n"
543 " }\n"
544 " }\n"
545 " }\n"
546 " notification notif {\n"
547 " leaf l {\n"
548 " type empty;\n"
549 " }\n"
550 " }\n"
551 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200552 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100553 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 +0200554
555 /* action output -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200556 str = "module j {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100557 " yang-version 1.1;\n"
558 " namespace urn:a;\n"
559 " prefix a;\n"
560 " container cont {\n"
561 " list ll {\n"
562 " key k;\n"
563 " leaf k {\n"
564 " type string;\n"
565 " }\n"
566 " action act {\n"
567 " output {\n"
568 " leaf l2 {\n"
569 " must /cont/l;\n"
570 " type leafref {\n"
571 " path ../../../l;\n"
572 " }\n"
573 " }\n"
574 " }\n"
575 " }\n"
576 " }\n"
577 " leaf l {\n"
578 " config false;\n"
579 " type empty;\n"
580 " }\n"
581 " }\n"
582 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200583 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
584 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200585
586 /* action output -> action input leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200587 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100588 " yang-version 1.1;\n"
589 " namespace urn:a;\n"
590 " prefix a;\n"
591 " container cont {\n"
592 " list ll {\n"
593 " key k;\n"
594 " leaf k {\n"
595 " type string;\n"
596 " }\n"
597 " action act {\n"
598 " input {\n"
599 " leaf l {\n"
600 " type empty;\n"
601 " }\n"
602 " }\n"
603 " output {\n"
604 " leaf l2 {\n"
605 " type leafref {\n"
606 " path ../l;\n"
607 " }\n"
608 " }\n"
609 " }\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_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100615 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location /k:cont/ll/act/output/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200616
617 /* action output -> action input must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200618 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100619 " yang-version 1.1;\n"
620 " namespace urn:a;\n"
621 " prefix a;\n"
622 " container cont {\n"
623 " list ll {\n"
624 " key k;\n"
625 " leaf k {\n"
626 " type string;\n"
627 " }\n"
628 " action act {\n"
629 " input {\n"
630 " leaf l {\n"
631 " type empty;\n"
632 " }\n"
633 " }\n"
634 " output {\n"
635 " leaf l2 {\n"
636 " must /cont/ll/act/l;\n"
637 " type empty;\n"
638 " }\n"
639 " }\n"
640 " }\n"
641 " }\n"
642 " }\n"
643 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200644 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100645 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 +0200646}
Radek Krejci589c5472021-01-20 10:29:06 +0100647
648struct module_clb_list {
649 const char *name;
650 const char *data;
651};
652
653static LY_ERR
654module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name,
655 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
656 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
657{
658 struct module_clb_list *list = (struct module_clb_list *)user_data;
659
660 for (unsigned int i = 0; list[i].data; i++) {
661
662 if ((submod_name && !strcmp(list[i].name, submod_name)) ||
663 (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) {
664 *module_data = list[i].data;
665 *format = LYS_IN_YANG;
666 *free_module_data = NULL;
667 return LY_SUCCESS;
668 }
669 }
670 return LY_EINVAL;
671}
672
673void
674test_includes(void **state)
675{
676 const struct lys_module *mod;
677
678 {
679 /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */
680 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100681 {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"},
682 {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"},
683 {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"},
684 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100685 };
686 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
687 mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL);
688 assert_non_null(mod);
689 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
690 assert_true(mod->parsed->includes[1].injected);
691 }
692
693 {
694 /* YANG 1.1 - the missing include sub_b_two in main_b is error */
695 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100696 {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"},
697 {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"},
698 {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"},
699 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100700 };
701 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
702 mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL);
703 assert_null(mod);
704 CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL,
705 "Data model \"main_b\" not found in local searchdirs.", NULL,
706 "Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL,
707 "Data model \"sub_b_one\" not found in local searchdirs.", NULL,
708 "YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes "
709 "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL);
710 }
711
712 {
713 /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */
714 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100715 {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"},
716 {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"},
717 {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } }"},
718 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100719 };
720 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
721 mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL);
722 assert_non_null(mod);
723 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
724 assert_false(mod->parsed->includes[1].injected);
725 /* result is ok, but log includes the warning */
726 CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_one) are not necessary.", NULL);
727 }
728}