blob: ab4b3b29f96f2767de148d31b3f0374be3380c35 [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"
20#include "tree_schema.h"
21#include "tree_schema_internal.h"
Radek Krejci18abde42020-06-13 20:04:39 +020022
23void
Radek Krejci3a4889a2020-05-19 17:01:58 +020024test_getnext(void **state)
25{
Michal Vasko3a41dff2020-07-15 14:30:28 +020026 const struct lys_module *mod;
Radek Krejci3a4889a2020-05-19 17:01:58 +020027 const struct lysc_node *node = NULL, *four;
28 const struct lysc_node_container *cont;
29 const struct lysc_action *rpc;
30
Radek Iša56ca9e42020-09-08 18:42:00 +020031 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 +010032 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
33 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
34 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
35 " notification nine {leaf nine-data {type string;}}}"
36 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
37 "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
38 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
39 "rpc i;"
40 "notification j {leaf i-data {type string;}}"
41 "notification k;}", LYS_IN_YANG, &mod));
Radek Krejci3a4889a2020-05-19 17:01:58 +020042 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
43 assert_string_equal("a", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010044 cont = (const struct lysc_node_container *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020045 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
46 assert_string_equal("b", node->name);
47 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
48 assert_string_equal("c", node->name);
49 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
50 assert_string_equal("d", node->name);
51 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
52 assert_string_equal("e", node->name);
53 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
54 assert_string_equal("f", node->name);
55 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
56 assert_string_equal("g", node->name);
57 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
58 assert_string_equal("h", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010059 rpc = (const struct lysc_action *)node;
Radek Krejci3a4889a2020-05-19 17:01:58 +020060 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
61 assert_string_equal("i", node->name);
62 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
63 assert_string_equal("j", node->name);
64 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
65 assert_string_equal("k", node->name);
66 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
67 /* Inside container */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010068 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020069 assert_string_equal("one", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010070 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020071 assert_string_equal("two", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010072 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020073 assert_string_equal("three", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010074 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020075 assert_string_equal("four", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010076 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020077 assert_string_equal("five", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010078 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020079 assert_string_equal("six", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010080 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020081 assert_string_equal("seven", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010082 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020083 assert_string_equal("eight", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010084 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020085 assert_string_equal("nine", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010086 assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020087 /* Inside RPC */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010088 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020089 assert_string_equal("h-input", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010090 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +020091
92 /* options */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010093 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +020094 assert_string_equal("x", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010095 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +020096 assert_string_equal("seven", node->name);
97
Radek Krejcib4ac5a92020-11-23 17:54:33 +010098 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
Radek Krejci3a4889a2020-05-19 17:01:58 +020099 assert_string_equal("seven", node->name);
100
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100101 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200102 assert_string_equal("five", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100103 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200104 assert_string_equal("y", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100105 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200106 assert_string_equal("seven", node->name);
107
108 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
109 assert_string_equal("one", node->name);
110
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100111 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200112 assert_string_equal("h-output", node->name);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100113 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200114
Radek Iša56ca9e42020-09-08 18:42:00 +0200115 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 +0200116 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
117 assert_string_equal("c", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100118 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200119
Radek Iša56ca9e42020-09-08 18:42:00 +0200120 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 +0200121 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
122 assert_string_equal("d", node->name);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100123 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200124
Radek Iša56ca9e42020-09-08 18:42:00 +0200125 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 +0100126 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
127 assert_string_equal("c", node->name);
128 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
129 assert_string_equal("a", node->name);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200130}
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100131
Radek Krejci18abde42020-06-13 20:04:39 +0200132void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200133test_date(void **state)
134{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200135 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200136 CHECK_LOG("Invalid argument date (lysp_check_date()).", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200137 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 CHECK_LOG("Invalid argument date_len (lysp_check_date()).", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200139 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200140 CHECK_LOG("Invalid value \"nonsencexx\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200141 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 CHECK_LOG("Invalid value \"123x-11-11\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200143 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200144 CHECK_LOG("Invalid value \"2018-13-11\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200145 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 CHECK_LOG("Invalid value \"2018-11-41\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200147 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200148 CHECK_LOG("Invalid value \"2018-02-29\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200149 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200150 CHECK_LOG("Invalid value \"2018.02-28\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200151 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 CHECK_LOG("Invalid value \"2018-02.28\" of \"date\".", NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200153
154 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
155 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
156 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200157}
158
Radek Krejci18abde42020-06-13 20:04:39 +0200159void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200160test_revisions(void **state)
161{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200162 struct lysp_revision *revs = NULL, *rev;
163
Radek Krejci3a4889a2020-05-19 17:01:58 +0200164 /* no error, it just does nothing */
165 lysp_sort_revisions(NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200166 CHECK_LOG(NULL, NULL);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200167
168 /* revisions are stored in wrong order - the newest is the last */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100169 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200170 strcpy(rev->date, "2018-01-01");
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100171 LY_ARRAY_NEW_RET(NULL, revs, rev, );
Radek Krejci3a4889a2020-05-19 17:01:58 +0200172 strcpy(rev->date, "2018-12-31");
173
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200174 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Radek Krejci3a4889a2020-05-19 17:01:58 +0200175 assert_string_equal("2018-01-01", &revs[0]);
176 assert_string_equal("2018-12-31", &revs[1]);
177 /* the order should be fixed, so the newest revision will be the first in the array */
178 lysp_sort_revisions(revs);
179 assert_string_equal("2018-12-31", &revs[0]);
180 assert_string_equal("2018-01-01", &revs[1]);
181
182 LY_ARRAY_FREE(revs);
183}
184
Radek Krejci18abde42020-06-13 20:04:39 +0200185void
Radek Krejci3a4889a2020-05-19 17:01:58 +0200186test_typedef(void **state)
187{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200188 const char *str;
189
Radek Krejci3a4889a2020-05-19 17:01:58 +0200190 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
192 CHECK_LOG("Invalid name \"binary\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200193 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200194 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
195 CHECK_LOG("Invalid name \"bits\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200196 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200197 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
198 CHECK_LOG("Invalid name \"boolean\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200199 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
201 CHECK_LOG("Invalid name \"decimal64\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200202 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200203 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
204 CHECK_LOG("Invalid name \"empty\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200205 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200206 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
207 CHECK_LOG("Invalid name \"enumeration\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200208 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200209 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
210 CHECK_LOG("Invalid name \"int8\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200211 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200212 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
213 CHECK_LOG("Invalid name \"int16\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200214 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200215 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
216 CHECK_LOG("Invalid name \"int32\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200217 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200218 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
219 CHECK_LOG("Invalid name \"int64\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200220 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200221 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
222 CHECK_LOG("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200223 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200224 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
225 CHECK_LOG("Invalid name \"identityref\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200226 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200227 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
228 CHECK_LOG("Invalid name \"leafref\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200229 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
231 CHECK_LOG("Invalid name \"string\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200232 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200233 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
234 CHECK_LOG("Invalid name \"union\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200235 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200236 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
237 CHECK_LOG("Invalid name \"uint8\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200238 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
240 CHECK_LOG("Invalid name \"uint16\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200241 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
243 CHECK_LOG("Invalid name \"uint32\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200244 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
246 CHECK_LOG("Invalid name \"uint64\" of typedef - name collision with a built-in type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200247
248 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 +0100249 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
250 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
251 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
252 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200254
255 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
257 CHECK_LOG("Invalid name \"test\" of typedef - name collision with another top-level type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200258
259 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 +0200260 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
261 CHECK_LOG("Invalid name \"x\" of typedef - scoped type collide with a top-level type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200262
263 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 +0200264 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
265 CHECK_LOG("Invalid name \"y\" of typedef - name collision with another scoped type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200266
267 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 +0200268 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
269 CHECK_LOG("Invalid name \"y\" of typedef - name collision with sibling type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200270
Radek Iša56ca9e42020-09-08 18:42:00 +0200271 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 +0200272 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200273 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
274 CHECK_LOG("Invalid name \"x\" of typedef - name collision with another top-level type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200275
Radek Iša56ca9e42020-09-08 18:42:00 +0200276 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 +0200277 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200278 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
279 CHECK_LOG("Invalid name \"x\" of typedef - scoped type collide with a top-level type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200280
Radek Iša56ca9e42020-09-08 18:42:00 +0200281 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 +0200282 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200283 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EEXIST);
284 CHECK_LOG("Invalid name \"x\" of typedef - scoped type collide with a top-level type.", "Line number 1.");
Radek Krejci3a4889a2020-05-19 17:01:58 +0200285}
Michal Vasko6b26e742020-07-17 15:02:10 +0200286
287void
288test_accessible_tree(void **state)
289{
Michal Vasko6b26e742020-07-17 15:02:10 +0200290 const char *str;
291
Michal Vasko6b26e742020-07-17 15:02:10 +0200292 /* config -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200293 str = "module a {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100294 " namespace urn:a;\n"
295 " prefix a;\n"
296 " container cont {\n"
297 " leaf l {\n"
298 " type empty;\n"
299 " }\n"
300 " }\n"
301 " container cont2 {\n"
302 " leaf l2 {\n"
303 " must ../../cont/l;\n"
304 " type leafref {\n"
305 " path /cont/l;\n"
306 " }\n"
307 " }\n"
308 " }\n"
309 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200310 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
311 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200312
313 /* config -> state leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200314 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100315 " namespace urn:a;\n"
316 " prefix a;\n"
317 " container cont {\n"
318 " config false;\n"
319 " leaf l {\n"
320 " type empty;\n"
321 " }\n"
322 " }\n"
323 " container cont2 {\n"
324 " leaf l2 {\n"
325 " type leafref {\n"
326 " path /cont/l;\n"
327 " }\n"
328 " }\n"
329 " }\n"
330 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200331 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
332 CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
333 " (as the leafref does), but it does not.", "/b:cont2/l2");
Michal Vasko6b26e742020-07-17 15:02:10 +0200334
335 /* config -> state must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200336 str = "module b {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100337 " namespace urn:a;\n"
338 " prefix a;\n"
339 " container cont {\n"
340 " config false;\n"
341 " leaf l {\n"
342 " type empty;\n"
343 " }\n"
344 " }\n"
345 " container cont2 {\n"
346 " leaf l2 {\n"
347 " must ../../cont/l;\n"
348 " type empty;\n"
349 " }\n"
350 " }\n"
351 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200352 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
353 CHECK_LOG_CTX("Schema node \"l\" not found (\"../../cont/l\") with context node \"/b:cont2/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200354
355 /* state -> config */
Radek Iša56ca9e42020-09-08 18:42:00 +0200356 str = "module c {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100357 " namespace urn:a;\n"
358 " prefix a;\n"
359 " container cont {\n"
360 " leaf l {\n"
361 " type empty;\n"
362 " }\n"
363 " }\n"
364 " container cont2 {\n"
365 " config false;\n"
366 " leaf l2 {\n"
367 " must ../../cont/l;\n"
368 " type leafref {\n"
369 " path /cont/l;\n"
370 " }\n"
371 " }\n"
372 " }\n"
373 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200374 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
375 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200376
377 /* notif -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200378 str = "module d {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100379 " namespace urn:a;\n"
380 " prefix a;\n"
381 " container cont {\n"
382 " config false;\n"
383 " leaf l {\n"
384 " type empty;\n"
385 " }\n"
386 " }\n"
387 " notification notif {\n"
388 " leaf l2 {\n"
389 " must ../../cont/l;\n"
390 " type leafref {\n"
391 " path /cont/l;\n"
392 " }\n"
393 " }\n"
394 " }\n"
395 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200396 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
397 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200398
399 /* notif -> notif */
Radek Iša56ca9e42020-09-08 18:42:00 +0200400 str = "module e {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100401 " namespace urn:a;\n"
402 " prefix a;\n"
403 " notification notif {\n"
404 " leaf l {\n"
405 " type empty;\n"
406 " }\n"
407 " leaf l2 {\n"
408 " must ../../notif/l;\n"
409 " type leafref {\n"
410 " path /notif/l;\n"
411 " }\n"
412 " }\n"
413 " }\n"
414 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200415 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
416 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200417
418 /* rpc input -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200419 str = "module f {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100420 " namespace urn:a;\n"
421 " prefix a;\n"
422 " container cont {\n"
423 " config false;\n"
424 " leaf l {\n"
425 " type empty;\n"
426 " }\n"
427 " }\n"
428 " rpc rp {\n"
429 " input {\n"
430 " leaf l2 {\n"
431 " must ../../cont/l;\n"
432 " type leafref {\n"
433 " path /cont/l;\n"
434 " }\n"
435 " }\n"
436 " }\n"
437 " }\n"
438 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
440 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200441
442 /* rpc input -> rpc input */
Radek Iša56ca9e42020-09-08 18:42:00 +0200443 str = "module g {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100444 " namespace urn:a;\n"
445 " prefix a;\n"
446 " rpc rp {\n"
447 " input {\n"
448 " leaf l {\n"
449 " type empty;\n"
450 " }\n"
451 " leaf l2 {\n"
452 " must ../l;\n"
453 " type leafref {\n"
454 " path /rp/l;\n"
455 " }\n"
456 " }\n"
457 " }\n"
458 " }\n"
459 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200460 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
461 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200462
463 /* rpc input -> rpc output leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100465 " namespace urn:a;\n"
466 " prefix a;\n"
467 " rpc rp {\n"
468 " input {\n"
469 " leaf l2 {\n"
470 " type leafref {\n"
471 " path /rp/l;\n"
472 " }\n"
473 " }\n"
474 " }\n"
475 " output {\n"
476 " leaf l {\n"
477 " type empty;\n"
478 " }\n"
479 " }\n"
480 " }\n"
481 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
483 CHECK_LOG_CTX("Not found node \"l\" in path.", "/h:rp/l2");
Michal Vasko6b26e742020-07-17 15:02:10 +0200484
485 /* rpc input -> rpc output must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200486 str = "module h {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100487 " namespace urn:a;\n"
488 " prefix a;\n"
489 " rpc rp {\n"
490 " input {\n"
491 " leaf l2 {\n"
492 " must ../l;\n"
493 " type empty;\n"
494 " }\n"
495 " }\n"
496 " output {\n"
497 " leaf l {\n"
498 " type empty;\n"
499 " }\n"
500 " }\n"
501 " }\n"
502 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200503 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
504 CHECK_LOG_CTX("Schema node \"l\" not found (\"../l\") with context node \"/h:rp/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200505
506 /* rpc input -> notif leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200507 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100508 " namespace urn:a;\n"
509 " prefix a;\n"
510 " rpc rp {\n"
511 " input {\n"
512 " leaf l2 {\n"
513 " type leafref {\n"
514 " path ../../notif/l;\n"
515 " }\n"
516 " }\n"
517 " }\n"
518 " }\n"
519 " notification notif {\n"
520 " leaf l {\n"
521 " type empty;\n"
522 " }\n"
523 " }\n"
524 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200525 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
526 CHECK_LOG_CTX("Not found node \"notif\" in path.", "/i:rp/l2");
Michal Vasko6b26e742020-07-17 15:02:10 +0200527
528 /* rpc input -> notif must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200529 str = "module i {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100530 " namespace urn:a;\n"
531 " prefix a;\n"
532 " rpc rp {\n"
533 " input {\n"
534 " leaf l2 {\n"
535 " must /notif/l;\n"
536 " type empty;\n"
537 " }\n"
538 " }\n"
539 " }\n"
540 " notification notif {\n"
541 " leaf l {\n"
542 " type empty;\n"
543 " }\n"
544 " }\n"
545 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200546 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
547 CHECK_LOG_CTX("Schema node \"l\" not found (\"/notif/l\") with context node \"/i:rp/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200548
549 /* action output -> state */
Radek Iša56ca9e42020-09-08 18:42:00 +0200550 str = "module j {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100551 " yang-version 1.1;\n"
552 " namespace urn:a;\n"
553 " prefix a;\n"
554 " container cont {\n"
555 " list ll {\n"
556 " key k;\n"
557 " leaf k {\n"
558 " type string;\n"
559 " }\n"
560 " action act {\n"
561 " output {\n"
562 " leaf l2 {\n"
563 " must /cont/l;\n"
564 " type leafref {\n"
565 " path ../../../l;\n"
566 " }\n"
567 " }\n"
568 " }\n"
569 " }\n"
570 " }\n"
571 " leaf l {\n"
572 " config false;\n"
573 " type empty;\n"
574 " }\n"
575 " }\n"
576 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200577 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
578 CHECK_LOG_CTX(NULL, NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200579
580 /* action output -> action input leafref */
Radek Iša56ca9e42020-09-08 18:42:00 +0200581 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100582 " yang-version 1.1;\n"
583 " namespace urn:a;\n"
584 " prefix a;\n"
585 " container cont {\n"
586 " list ll {\n"
587 " key k;\n"
588 " leaf k {\n"
589 " type string;\n"
590 " }\n"
591 " action act {\n"
592 " input {\n"
593 " leaf l {\n"
594 " type empty;\n"
595 " }\n"
596 " }\n"
597 " output {\n"
598 " leaf l2 {\n"
599 " type leafref {\n"
600 " path ../l;\n"
601 " }\n"
602 " }\n"
603 " }\n"
604 " }\n"
605 " }\n"
606 " }\n"
607 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200608 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
609 CHECK_LOG_CTX("Not found node \"l\" in path.", "/k:cont/ll/act/l2");
Michal Vasko6b26e742020-07-17 15:02:10 +0200610
611 /* action output -> action input must */
Radek Iša56ca9e42020-09-08 18:42:00 +0200612 str = "module k {\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100613 " yang-version 1.1;\n"
614 " namespace urn:a;\n"
615 " prefix a;\n"
616 " container cont {\n"
617 " list ll {\n"
618 " key k;\n"
619 " leaf k {\n"
620 " type string;\n"
621 " }\n"
622 " action act {\n"
623 " input {\n"
624 " leaf l {\n"
625 " type empty;\n"
626 " }\n"
627 " }\n"
628 " output {\n"
629 " leaf l2 {\n"
630 " must /cont/ll/act/l;\n"
631 " type empty;\n"
632 " }\n"
633 " }\n"
634 " }\n"
635 " }\n"
636 " }\n"
637 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200638 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
639 CHECK_LOG_CTX("Schema node \"l\" not found (\"/cont/ll/act/l\") with context node \"/k:cont/ll/act/l2\".", NULL);
Michal Vasko6b26e742020-07-17 15:02:10 +0200640}