blob: d663c9a159bac2ec9eb7a7795800a89b0b459e1b [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 */
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
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 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 +0200273 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200274 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100275 CHECK_LOG("Invalid name \"x\" of typedef - name collision with another top-level type.", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200276
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;} container c {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 - scoped type collide with a 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;} typedef x {type int8;}}");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200283 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
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}
Michal Vasko6b26e742020-07-17 15:02:10 +0200287
288void
289test_accessible_tree(void **state)
290{
Michal Vasko6b26e742020-07-17 15:02:10 +0200291 const char *str;
292
Michal Vasko6b26e742020-07-17 15:02:10 +0200293 /* config -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200294 str = "module a {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100295 " namespace urn:a;\n"
296 " prefix a;\n"
297 " container cont {\n"
298 " leaf l {\n"
299 " type empty;\n"
300 " }\n"
301 " }\n"
302 " container cont2 {\n"
303 " leaf l2 {\n"
304 " must ../../cont/l;\n"
305 " type leafref {\n"
306 " path /cont/l;\n"
307 " }\n"
308 " }\n"
309 " }\n"
310 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200311 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
312 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200313
314 /* config -> state leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100316 " namespace urn:a;\n"
317 " prefix a;\n"
318 " container cont {\n"
319 " config false;\n"
320 " leaf l {\n"
321 " type empty;\n"
322 " }\n"
323 " }\n"
324 " container cont2 {\n"
325 " leaf l2 {\n"
326 " type leafref {\n"
327 " path /cont/l;\n"
328 " }\n"
329 " }\n"
330 " }\n"
331 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200332 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
333 CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100334 " (as the leafref does), but it does not.", "Schema location /b:cont2/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200335
336 /* config -> state must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200337 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100338 " namespace urn:a;\n"
339 " prefix a;\n"
340 " container cont {\n"
341 " config false;\n"
342 " leaf l {\n"
343 " type empty;\n"
344 " }\n"
345 " }\n"
346 " container cont2 {\n"
347 " leaf l2 {\n"
348 " must ../../cont/l;\n"
349 " type empty;\n"
350 " }\n"
351 " }\n"
352 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200353 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
354 CHECK_LOG_CTX("Schema node \"l\" not found (\"../../cont/l\") with context node \"/b:cont2/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200355
356 /* state -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200357 str = "module c {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100358 " namespace urn:a;\n"
359 " prefix a;\n"
360 " container cont {\n"
361 " leaf l {\n"
362 " type empty;\n"
363 " }\n"
364 " }\n"
365 " container cont2 {\n"
366 " config false;\n"
367 " leaf l2 {\n"
368 " must ../../cont/l;\n"
369 " type leafref {\n"
370 " path /cont/l;\n"
371 " }\n"
372 " }\n"
373 " }\n"
374 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200375 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
376 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200377
378 /* notif -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200379 str = "module d {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100380 " namespace urn:a;\n"
381 " prefix a;\n"
382 " container cont {\n"
383 " config false;\n"
384 " leaf l {\n"
385 " type empty;\n"
386 " }\n"
387 " }\n"
388 " notification notif {\n"
389 " leaf l2 {\n"
390 " must ../../cont/l;\n"
391 " type leafref {\n"
392 " path /cont/l;\n"
393 " }\n"
394 " }\n"
395 " }\n"
396 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200397 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
398 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200399
400 /* notif -> notif */
Radek Iša56ca9e42020-09-08 18:42:00 +0200401 str = "module e {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100402 " namespace urn:a;\n"
403 " prefix a;\n"
404 " notification notif {\n"
405 " leaf l {\n"
406 " type empty;\n"
407 " }\n"
408 " leaf l2 {\n"
409 " must ../../notif/l;\n"
410 " type leafref {\n"
411 " path /notif/l;\n"
412 " }\n"
413 " }\n"
414 " }\n"
415 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200416 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
417 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200418
419 /* rpc input -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200420 str = "module f {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100421 " namespace urn:a;\n"
422 " prefix a;\n"
423 " container cont {\n"
424 " config false;\n"
425 " leaf l {\n"
426 " type empty;\n"
427 " }\n"
428 " }\n"
429 " rpc rp {\n"
430 " input {\n"
431 " leaf l2 {\n"
432 " must ../../cont/l;\n"
433 " type leafref {\n"
434 " path /cont/l;\n"
435 " }\n"
436 " }\n"
437 " }\n"
438 " }\n"
439 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200440 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
441 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200442
443 /* rpc input -> rpc input */
Radek Iša56ca9e42020-09-08 18:42:00 +0200444 str = "module g {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100445 " namespace urn:a;\n"
446 " prefix a;\n"
447 " rpc rp {\n"
448 " input {\n"
449 " leaf l {\n"
450 " type empty;\n"
451 " }\n"
452 " leaf l2 {\n"
453 " must ../l;\n"
454 " type leafref {\n"
455 " path /rp/l;\n"
456 " }\n"
457 " }\n"
458 " }\n"
459 " }\n"
460 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200461 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
462 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200463
464 /* rpc input -> rpc output leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200465 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100466 " namespace urn:a;\n"
467 " prefix a;\n"
468 " rpc rp {\n"
469 " input {\n"
470 " leaf l2 {\n"
471 " type leafref {\n"
472 " path /rp/l;\n"
473 " }\n"
474 " }\n"
475 " }\n"
476 " output {\n"
477 " leaf l {\n"
478 " type empty;\n"
479 " }\n"
480 " }\n"
481 " }\n"
482 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200483 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100484 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location /h:rp/input/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200485
486 /* rpc input -> rpc output must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200487 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100488 " namespace urn:a;\n"
489 " prefix a;\n"
490 " rpc rp {\n"
491 " input {\n"
492 " leaf l2 {\n"
493 " must ../l;\n"
494 " type empty;\n"
495 " }\n"
496 " }\n"
497 " output {\n"
498 " leaf l {\n"
499 " type empty;\n"
500 " }\n"
501 " }\n"
502 " }\n"
503 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200504 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100505 CHECK_LOG_CTX("Schema node \"l\" not found (\"../l\") with context node \"/h:rp/input/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200506
507 /* rpc input -> notif leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200508 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100509 " namespace urn:a;\n"
510 " prefix a;\n"
511 " rpc rp {\n"
512 " input {\n"
513 " leaf l2 {\n"
514 " type leafref {\n"
515 " path ../../notif/l;\n"
516 " }\n"
517 " }\n"
518 " }\n"
519 " }\n"
520 " notification notif {\n"
521 " leaf l {\n"
522 " type empty;\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_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100527 CHECK_LOG_CTX("Not found node \"notif\" in path.", "Schema location /i:rp/input/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200528
529 /* rpc input -> notif must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200530 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100531 " namespace urn:a;\n"
532 " prefix a;\n"
533 " rpc rp {\n"
534 " input {\n"
535 " leaf l2 {\n"
536 " must /notif/l;\n"
537 " type empty;\n"
538 " }\n"
539 " }\n"
540 " }\n"
541 " notification notif {\n"
542 " leaf l {\n"
543 " type empty;\n"
544 " }\n"
545 " }\n"
546 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200547 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100548 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 +0200549
550 /* action output -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200551 str = "module j {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100552 " yang-version 1.1;\n"
553 " namespace urn:a;\n"
554 " prefix a;\n"
555 " container cont {\n"
556 " list ll {\n"
557 " key k;\n"
558 " leaf k {\n"
559 " type string;\n"
560 " }\n"
561 " action act {\n"
562 " output {\n"
563 " leaf l2 {\n"
564 " must /cont/l;\n"
565 " type leafref {\n"
566 " path ../../../l;\n"
567 " }\n"
568 " }\n"
569 " }\n"
570 " }\n"
571 " }\n"
572 " leaf l {\n"
573 " config false;\n"
574 " type empty;\n"
575 " }\n"
576 " }\n"
577 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200578 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
579 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200580
581 /* action output -> action input leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200582 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100583 " yang-version 1.1;\n"
584 " namespace urn:a;\n"
585 " prefix a;\n"
586 " container cont {\n"
587 " list ll {\n"
588 " key k;\n"
589 " leaf k {\n"
590 " type string;\n"
591 " }\n"
592 " action act {\n"
593 " input {\n"
594 " leaf l {\n"
595 " type empty;\n"
596 " }\n"
597 " }\n"
598 " output {\n"
599 " leaf l2 {\n"
600 " type leafref {\n"
601 " path ../l;\n"
602 " }\n"
603 " }\n"
604 " }\n"
605 " }\n"
606 " }\n"
607 " }\n"
608 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200609 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100610 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location /k:cont/ll/act/output/l2.");
Michal Vasko6b26e742020-07-17 15:02:10 +0200611
612 /* action output -> action input must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200613 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100614 " yang-version 1.1;\n"
615 " namespace urn:a;\n"
616 " prefix a;\n"
617 " container cont {\n"
618 " list ll {\n"
619 " key k;\n"
620 " leaf k {\n"
621 " type string;\n"
622 " }\n"
623 " action act {\n"
624 " input {\n"
625 " leaf l {\n"
626 " type empty;\n"
627 " }\n"
628 " }\n"
629 " output {\n"
630 " leaf l2 {\n"
631 " must /cont/ll/act/l;\n"
632 " type empty;\n"
633 " }\n"
634 " }\n"
635 " }\n"
636 " }\n"
637 " }\n"
638 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200639 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100640 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 +0200641}
Radek Krejci589c5472021-01-20 10:29:06 +0100642
643struct module_clb_list {
644 const char *name;
645 const char *data;
646};
647
648static LY_ERR
649module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name,
650 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
651 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
652{
653 struct module_clb_list *list = (struct module_clb_list *)user_data;
654
655 for (unsigned int i = 0; list[i].data; i++) {
656
657 if ((submod_name && !strcmp(list[i].name, submod_name)) ||
658 (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) {
659 *module_data = list[i].data;
660 *format = LYS_IN_YANG;
661 *free_module_data = NULL;
662 return LY_SUCCESS;
663 }
664 }
665 return LY_EINVAL;
666}
667
668void
669test_includes(void **state)
670{
671 const struct lys_module *mod;
672
673 {
674 /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */
675 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100676 {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"},
677 {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"},
678 {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"},
679 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100680 };
681 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
682 mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL);
683 assert_non_null(mod);
684 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
685 assert_true(mod->parsed->includes[1].injected);
686 }
687
688 {
689 /* YANG 1.1 - the missing include sub_b_two in main_b is error */
690 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100691 {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"},
692 {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"},
693 {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"},
694 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100695 };
696 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
697 mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL);
698 assert_null(mod);
699 CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL,
700 "Data model \"main_b\" not found in local searchdirs.", NULL,
701 "Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL,
702 "Data model \"sub_b_one\" not found in local searchdirs.", NULL,
703 "YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes "
704 "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL);
705 }
706
707 {
708 /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */
709 struct module_clb_list list[] = {
Radek Krejcidf549132021-01-21 10:32:32 +0100710 {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"},
711 {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"},
712 {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } }"},
713 {NULL, NULL}
Radek Krejci589c5472021-01-20 10:29:06 +0100714 };
715 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
716 mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL);
717 assert_non_null(mod);
718 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
719 assert_false(mod->parsed->includes[1].injected);
720 /* result is ok, but log includes the warning */
721 CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_one) are not necessary.", NULL);
722 }
723}