Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 2 | * @file test_schema.c |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 5 | * @brief unit tests for schema related functions |
| 6 | * |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 7 | * Copyright (c) 2018 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 15 | #define _UTEST_MAIN_ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 16 | #include "utests.h" |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 17 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 20 | #include "compat.h" |
| 21 | #include "context.h" |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 22 | #include "log.h" |
| 23 | #include "parser_schema.h" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 24 | #include "plugins_exts.h" |
Radek Krejci | 8297b79 | 2020-08-16 14:49:05 +0200 | [diff] [blame] | 25 | #include "set.h" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 26 | #include "tree_edit.h" |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 27 | #include "tree_schema.h" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 28 | #include "tree_schema_internal.h" |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 29 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 30 | static LY_ERR |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 31 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 32 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 33 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 34 | { |
| 35 | *module_data = user_data; |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 36 | if ((*module_data)[0] == '<') { |
| 37 | *format = LYS_IN_YIN; |
| 38 | } else { |
| 39 | *format = LYS_IN_YANG; |
| 40 | } |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 41 | *free_module_data = NULL; |
| 42 | return LY_SUCCESS; |
| 43 | } |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 44 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 45 | #define TEST_YANG_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \ |
| 46 | "module "MOD_NAME" { namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}" |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 47 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 48 | #define TEST_YANG_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \ |
| 49 | "module "MOD_NAME" {yang-version 1.1; namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}" |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 50 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 51 | #define TEST_YIN_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \ |
| 52 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\">" \ |
| 53 | "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>" |
| 54 | |
| 55 | #define TEST_YIN_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \ |
| 56 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\"><yang-version value=\"1.1\"/>" \ |
| 57 | "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>" |
| 58 | |
| 59 | #define TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, STR) \ |
| 60 | if (YIN) { \ |
| 61 | if (RFC7950) { \ |
| 62 | STR = TEST_YIN_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \ |
| 63 | } else { \ |
| 64 | STR = TEST_YIN_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \ |
| 65 | } \ |
| 66 | } else { /* YANG */ \ |
| 67 | if (RFC7950) { \ |
| 68 | STR = TEST_YANG_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \ |
| 69 | } else { \ |
| 70 | STR = TEST_YANG_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \ |
| 71 | } \ |
| 72 | } |
| 73 | |
| 74 | #define TEST_SCHEMA_OK(RFC7950, YIN, MOD_NAME, CONTENT, RESULT) \ |
| 75 | { \ |
| 76 | const char *test_str__; \ |
| 77 | TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \ |
| 78 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, &(RESULT))); \ |
| 79 | } |
| 80 | |
| 81 | #define TEST_SCHEMA_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \ |
| 82 | { \ |
| 83 | const char *test_str__; \ |
| 84 | TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \ |
| 85 | assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 86 | CHECK_LOG_CTX(ERRMSG, ERRPATH, 0); \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 89 | #define TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH, ERRLINE) \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 90 | { \ |
| 91 | const char *test_str__; \ |
| 92 | TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \ |
| 93 | assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 94 | CHECK_LOG_CTX("Parsing module \""MOD_NAME"\" failed.", NULL, 0); \ |
| 95 | CHECK_LOG_CTX(ERRMSG, ERRPATH, ERRLINE); \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #define TEST_STMT_DUP(RFC7950, YIN, STMT, MEMBER, VALUE1, VALUE2, LINE) \ |
| 99 | if (YIN) { \ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 100 | TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", "", "Duplicate keyword \""MEMBER"\".", NULL, LINE); \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 101 | } else { \ |
| 102 | TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", STMT"{"MEMBER" "VALUE1";"MEMBER" "VALUE2";}", \ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 103 | "Duplicate keyword \""MEMBER"\".", NULL, LINE); \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | #define TEST_STMT_SUBSTM_ERR(RFC7950, STMT, SUBSTMT, VALUE) ;\ |
| 107 | TEST_SCHEMA_PARSE_ERR(RFC7950, 0, "inv", STMT" test {"SUBSTMT" "VALUE";}", \ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 108 | "Invalid keyword \""SUBSTMT"\" as a child of \""STMT"\".", NULL, 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 109 | |
| 110 | struct module_clb_list { |
| 111 | const char *name; |
| 112 | const char *data; |
| 113 | }; |
| 114 | |
| 115 | static LY_ERR |
| 116 | module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name, |
| 117 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 118 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 119 | { |
| 120 | struct module_clb_list *list = (struct module_clb_list *)user_data; |
| 121 | |
| 122 | for (unsigned int i = 0; list[i].data; i++) { |
| 123 | |
| 124 | if ((submod_name && !strcmp(list[i].name, submod_name)) || |
| 125 | (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) { |
| 126 | *module_data = list[i].data; |
| 127 | *format = LYS_IN_YANG; |
| 128 | *free_module_data = NULL; |
| 129 | return LY_SUCCESS; |
| 130 | } |
| 131 | } |
| 132 | return LY_EINVAL; |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | test_getnext(void **state) |
| 137 | { |
| 138 | struct lys_module *mod; |
| 139 | const struct lysc_node *node = NULL, *four; |
| 140 | const struct lysc_node_container *cont; |
| 141 | const struct lysc_action *rpc; |
| 142 | |
| 143 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {yang-version 1.1; namespace urn:a;prefix a;" |
| 144 | "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}" |
| 145 | " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}" |
| 146 | " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}" |
| 147 | " notification nine {leaf nine-data {type string;}}}" |
| 148 | "leaf b {type string;} leaf-list c {type string;} list d {config false;}" |
| 149 | "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;" |
| 150 | "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}" |
| 151 | "rpc i;" |
| 152 | "notification j {leaf i-data {type string;}}" |
| 153 | "notification k;}", LYS_IN_YANG, &mod)); |
| 154 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 155 | assert_string_equal("a", node->name); |
| 156 | cont = (const struct lysc_node_container *)node; |
| 157 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 158 | assert_string_equal("b", node->name); |
| 159 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 160 | assert_string_equal("c", node->name); |
| 161 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 162 | assert_string_equal("d", node->name); |
| 163 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 164 | assert_string_equal("e", node->name); |
| 165 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 166 | assert_string_equal("f", node->name); |
| 167 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 168 | assert_string_equal("g", node->name); |
| 169 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 170 | assert_string_equal("h", node->name); |
| 171 | rpc = (const struct lysc_action *)node; |
| 172 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 173 | assert_string_equal("i", node->name); |
| 174 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 175 | assert_string_equal("j", node->name); |
| 176 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 177 | assert_string_equal("k", node->name); |
| 178 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 179 | /* Inside container */ |
| 180 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 181 | assert_string_equal("one", node->name); |
| 182 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 183 | assert_string_equal("two", node->name); |
| 184 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 185 | assert_string_equal("three", node->name); |
| 186 | assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 187 | assert_string_equal("four", node->name); |
| 188 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 189 | assert_string_equal("five", node->name); |
| 190 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 191 | assert_string_equal("six", node->name); |
| 192 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 193 | assert_string_equal("seven", node->name); |
| 194 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 195 | assert_string_equal("eight", node->name); |
| 196 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 197 | assert_string_equal("nine", node->name); |
| 198 | assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0)); |
| 199 | /* Inside RPC */ |
| 200 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0)); |
| 201 | assert_string_equal("h-input", node->name); |
| 202 | assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0)); |
| 203 | |
| 204 | /* options */ |
| 205 | assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 206 | assert_string_equal("x", node->name); |
| 207 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 208 | assert_string_equal("seven", node->name); |
| 209 | |
| 210 | assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE)); |
| 211 | assert_string_equal("seven", node->name); |
| 212 | |
| 213 | assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 214 | assert_string_equal("five", node->name); |
| 215 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 216 | assert_string_equal("y", node->name); |
| 217 | assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 218 | assert_string_equal("seven", node->name); |
| 219 | |
| 220 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT)); |
| 221 | assert_string_equal("one", node->name); |
| 222 | |
| 223 | assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 224 | assert_string_equal("h-output", node->name); |
| 225 | assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 226 | |
| 227 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod)); |
| 228 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 229 | assert_string_equal("c", node->name); |
| 230 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 231 | |
| 232 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod)); |
| 233 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 234 | assert_string_equal("d", node->name); |
| 235 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 236 | |
| 237 | 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)); |
| 238 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 239 | assert_string_equal("c", node->name); |
| 240 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT)); |
| 241 | assert_string_equal("a", node->name); |
| 242 | } |
| 243 | |
| 244 | static void |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 245 | test_date(void **UNUSED(state)) |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 246 | { |
| 247 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 248 | CHECK_LOG_LASTMSG("Invalid argument date (lysp_check_date())."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 249 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 250 | CHECK_LOG_LASTMSG("Invalid length 1 of a date."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 251 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 252 | CHECK_LOG_LASTMSG("Invalid value \"nonsencexx\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 253 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 254 | CHECK_LOG_LASTMSG("Invalid value \"123x-11-11\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 255 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 256 | CHECK_LOG_LASTMSG("Invalid value \"2018-13-11\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 257 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 258 | CHECK_LOG_LASTMSG("Invalid value \"2018-11-41\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 259 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 260 | CHECK_LOG_LASTMSG("Invalid value \"2018-02-29\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 261 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 262 | CHECK_LOG_LASTMSG("Invalid value \"2018.02-28\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 263 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date")); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 264 | CHECK_LOG_LASTMSG("Invalid value \"2018-02.28\" of \"date\"."); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 265 | |
| 266 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 267 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 268 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 269 | } |
| 270 | |
| 271 | static void |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 272 | test_revisions(void **UNUSED(state)) |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 273 | { |
| 274 | struct lysp_revision *revs = NULL, *rev; |
| 275 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 276 | /* revisions are stored in wrong order - the newest is the last */ |
| 277 | LY_ARRAY_NEW_RET(NULL, revs, rev, ); |
| 278 | strcpy(rev->date, "2018-01-01"); |
| 279 | LY_ARRAY_NEW_RET(NULL, revs, rev, ); |
| 280 | strcpy(rev->date, "2018-12-31"); |
| 281 | |
| 282 | assert_int_equal(2, LY_ARRAY_COUNT(revs)); |
Michal Vasko | 892355e | 2023-03-14 13:37:48 +0100 | [diff] [blame] | 283 | assert_string_equal("2018-01-01", revs[0].date); |
| 284 | assert_string_equal("2018-12-31", revs[1].date); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 285 | /* the order should be fixed, so the newest revision will be the first in the array */ |
| 286 | lysp_sort_revisions(revs); |
Michal Vasko | 892355e | 2023-03-14 13:37:48 +0100 | [diff] [blame] | 287 | assert_string_equal("2018-12-31", revs[0].date); |
| 288 | assert_string_equal("2018-01-01", revs[1].date); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 289 | |
| 290 | LY_ARRAY_FREE(revs); |
| 291 | } |
| 292 | |
| 293 | static void |
| 294 | test_collision_typedef(void **state) |
| 295 | { |
| 296 | const char *str; |
| 297 | char *submod; |
| 298 | struct module_clb_list list[3] = {0}; |
| 299 | |
| 300 | list[0].name = "asub"; |
| 301 | list[1].name = "bsub"; |
| 302 | |
| 303 | /* collision with a built-in type */ |
| 304 | str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}"; |
| 305 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 306 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 307 | CHECK_LOG_CTX("Duplicate identifier \"binary\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 308 | str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}"; |
| 309 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 310 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 311 | CHECK_LOG_CTX("Duplicate identifier \"bits\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 312 | str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}"; |
| 313 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 314 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 315 | CHECK_LOG_CTX("Duplicate identifier \"boolean\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 316 | str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}"; |
| 317 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 318 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 319 | CHECK_LOG_CTX("Duplicate identifier \"decimal64\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 320 | str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}"; |
| 321 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 322 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 323 | CHECK_LOG_CTX("Duplicate identifier \"empty\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 324 | str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}"; |
| 325 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 326 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 327 | CHECK_LOG_CTX("Duplicate identifier \"enumeration\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 328 | str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}"; |
| 329 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 330 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 331 | CHECK_LOG_CTX("Duplicate identifier \"int8\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 332 | str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}"; |
| 333 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 334 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 335 | CHECK_LOG_CTX("Duplicate identifier \"int16\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 336 | str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}"; |
| 337 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 338 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 339 | CHECK_LOG_CTX("Duplicate identifier \"int32\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 340 | str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}"; |
| 341 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 342 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 343 | CHECK_LOG_CTX("Duplicate identifier \"int64\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 344 | str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}"; |
| 345 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 346 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 347 | CHECK_LOG_CTX("Duplicate identifier \"instance-identifier\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 348 | str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}"; |
| 349 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 350 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 351 | CHECK_LOG_CTX("Duplicate identifier \"identityref\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 352 | str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}"; |
| 353 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 354 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 355 | CHECK_LOG_CTX("Duplicate identifier \"leafref\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 356 | str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}"; |
| 357 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 358 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 359 | CHECK_LOG_CTX("Duplicate identifier \"string\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 360 | str = "module a {namespace urn:a; prefix a; typedef union {type string;}}"; |
| 361 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 362 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 363 | CHECK_LOG_CTX("Duplicate identifier \"union\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 364 | str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}"; |
| 365 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 366 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 367 | CHECK_LOG_CTX("Duplicate identifier \"uint8\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 368 | str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}"; |
| 369 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 370 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 371 | CHECK_LOG_CTX("Duplicate identifier \"uint16\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 372 | str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}"; |
| 373 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 374 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 375 | CHECK_LOG_CTX("Duplicate identifier \"uint32\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 376 | str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}"; |
| 377 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 378 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 379 | CHECK_LOG_CTX("Duplicate identifier \"uint64\" of typedef statement - name collision with a built-in type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 380 | |
| 381 | str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} " |
| 382 | "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}" |
| 383 | "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}" |
| 384 | "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}" |
| 385 | "typedef uint32_ {type string;} typedef uint64_ {type string;}}"; |
| 386 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 387 | |
| 388 | /* collision in node's scope */ |
| 389 | str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}"; |
| 390 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 391 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 392 | CHECK_LOG_CTX("Duplicate identifier \"y\" of typedef statement - name collision with sibling type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 393 | |
| 394 | /* collision with parent node */ |
| 395 | str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}"; |
| 396 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 397 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 398 | CHECK_LOG_CTX("Duplicate identifier \"y\" of typedef statement - name collision with another scoped type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 399 | |
| 400 | /* collision with module's top-level */ |
| 401 | str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}"; |
| 402 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 403 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 404 | CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 405 | |
| 406 | /* collision of submodule's node with module's top-level */ |
| 407 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}"); |
| 408 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
| 409 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 410 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 411 | CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 412 | |
| 413 | /* collision of module's node with submodule's top-level */ |
| 414 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}"); |
| 415 | str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}"; |
| 416 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 417 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 418 | CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 419 | |
| 420 | /* collision of submodule's node with another submodule's top-level */ |
| 421 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}"; |
| 422 | list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}"; |
| 423 | list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {typedef g {type int;}}}"; |
| 424 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 425 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 426 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 427 | CHECK_LOG_CTX("Duplicate identifier \"g\" of typedef statement - scoped type collide with a top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 428 | |
| 429 | /* collision of module's top-levels */ |
| 430 | str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}"; |
| 431 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 432 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 433 | CHECK_LOG_CTX("Duplicate identifier \"test\" of typedef statement - name collision with another top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 434 | |
| 435 | /* collision of submodule's top-levels */ |
| 436 | submod = "submodule asub {belongs-to a {prefix a;} typedef g {type int;} typedef g {type int;}}"; |
| 437 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}"; |
| 438 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod); |
| 439 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 440 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 441 | CHECK_LOG_CTX("Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 442 | |
| 443 | /* collision of module's top-level with submodule's top-levels */ |
| 444 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}"); |
| 445 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
| 446 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 447 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 448 | CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - name collision with another top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 449 | |
| 450 | /* collision of submodule's top-level with another submodule's top-levels */ |
| 451 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}"; |
| 452 | list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}"; |
| 453 | list[1].data = "submodule bsub {belongs-to a {prefix a;} typedef g {type int;}}"; |
| 454 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 455 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 456 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 457 | CHECK_LOG_CTX("Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 458 | |
| 459 | /* error in type-stmt */ |
| 460 | str = "module a {namespace urn:a; prefix a; container c {typedef x {type t{}}"; |
| 461 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 462 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 463 | CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 464 | |
| 465 | /* no collision if the same names are in different scope */ |
| 466 | str = "module a {yang-version 1.1; namespace urn:a; prefix a;" |
| 467 | "container c {typedef g {type int;}} container d {typedef g {type int;}}}"; |
| 468 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
| 469 | } |
| 470 | |
| 471 | static void |
| 472 | test_collision_grouping(void **state) |
| 473 | { |
| 474 | const char *str; |
| 475 | char *submod; |
| 476 | struct module_clb_list list[3] = {0}; |
| 477 | |
| 478 | list[0].name = "asub"; |
| 479 | list[1].name = "bsub"; |
| 480 | |
| 481 | /* collision in node's scope */ |
| 482 | str = "module a {namespace urn:a; prefix a; container c {grouping y; grouping y;}}"; |
| 483 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 484 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 485 | CHECK_LOG_CTX("Duplicate identifier \"y\" of grouping statement - name collision with sibling grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 486 | |
| 487 | /* collision with parent node */ |
| 488 | str = "module a {namespace urn:a; prefix a; container c {container d {grouping y;} grouping y;}}"; |
| 489 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 490 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 491 | CHECK_LOG_CTX("Duplicate identifier \"y\" of grouping statement - name collision with another scoped grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 492 | |
| 493 | /* collision with module's top-level */ |
| 494 | str = "module a {namespace urn:a; prefix a; grouping x; container c {grouping x;}}"; |
| 495 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 496 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 497 | CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 498 | |
| 499 | /* collision of submodule's node with module's top-level */ |
| 500 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {grouping x;}}"); |
| 501 | str = "module a {namespace urn:a; prefix a; include b; grouping x;}"; |
| 502 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 503 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 504 | CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 505 | |
| 506 | /* collision of module's node with submodule's top-level */ |
| 507 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}"); |
| 508 | str = "module a {namespace urn:a; prefix a; include b; container c {grouping x;}}"; |
| 509 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 510 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 511 | CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 512 | |
| 513 | /* collision of submodule's node with another submodule's top-level */ |
| 514 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}"; |
| 515 | list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}"; |
| 516 | list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {grouping g;}}"; |
| 517 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 518 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 519 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 520 | CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 521 | |
| 522 | /* collision of module's top-levels */ |
| 523 | str = "module a {namespace urn:a; prefix a; grouping test; grouping test;}"; |
| 524 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 525 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 526 | CHECK_LOG_CTX("Duplicate identifier \"test\" of grouping statement - name collision with another top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 527 | |
| 528 | /* collision of submodule's top-levels */ |
| 529 | submod = "submodule asub {belongs-to a {prefix a;} grouping g; grouping g;}"; |
| 530 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}"; |
| 531 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod); |
| 532 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 533 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 534 | CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 535 | |
| 536 | /* collision of module's top-level with submodule's top-levels */ |
| 537 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}"); |
| 538 | str = "module a {namespace urn:a; prefix a; include b; grouping x;}"; |
| 539 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 540 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 541 | CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - name collision with another top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 542 | |
| 543 | /* collision of submodule's top-level with another submodule's top-levels */ |
| 544 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}"; |
| 545 | list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}"; |
| 546 | list[1].data = "submodule bsub {belongs-to a {prefix a;} grouping g;}"; |
| 547 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 548 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 549 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 550 | CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 551 | |
| 552 | /* collision in nested groupings, top-level */ |
| 553 | str = "module a {namespace urn:a; prefix a; grouping g {grouping g;}}"; |
| 554 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 555 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 556 | CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 557 | |
| 558 | /* collision in nested groupings, in node */ |
| 559 | str = "module a {namespace urn:a; prefix a; container c {grouping g {grouping g;}}}"; |
| 560 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 561 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 562 | CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - name collision with another scoped grouping.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 563 | |
| 564 | /* no collision if the same names are in different scope */ |
| 565 | str = "module a {yang-version 1.1; namespace urn:a; prefix a;" |
| 566 | "container c {grouping g;} container d {grouping g;}}"; |
| 567 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 568 | CHECK_LOG_CTX("Locally scoped grouping \"g\" not used.", NULL, 0); |
| 569 | CHECK_LOG_CTX("Locally scoped grouping \"g\" not used.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static void |
| 573 | test_collision_identity(void **state) |
| 574 | { |
| 575 | const char *str; |
| 576 | char *submod; |
| 577 | struct module_clb_list list[3] = {0}; |
| 578 | |
| 579 | list[0].name = "asub"; |
| 580 | list[1].name = "bsub"; |
| 581 | |
| 582 | /* collision of module's top-levels */ |
| 583 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; identity g; identity g;}"; |
| 584 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 585 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 586 | CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 587 | |
| 588 | /* collision of submodule's top-levels */ |
| 589 | submod = "submodule asub {belongs-to a {prefix a;} identity g; identity g;}"; |
| 590 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}"; |
| 591 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod); |
| 592 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 593 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 594 | CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 595 | |
| 596 | /* collision of module's top-level with submodule's top-levels */ |
| 597 | submod = "submodule asub {belongs-to a {prefix a;} identity g;}"; |
| 598 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; identity g;}"; |
| 599 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod); |
| 600 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 601 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 602 | CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 603 | |
| 604 | /* collision of submodule's top-level with another submodule's top-levels */ |
| 605 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}"; |
| 606 | list[0].data = "submodule asub {belongs-to a {prefix a;} identity g;}"; |
| 607 | list[1].data = "submodule bsub {belongs-to a {prefix a;} identity g;}"; |
| 608 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 609 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 610 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 611 | CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static void |
| 615 | test_collision_feature(void **state) |
| 616 | { |
| 617 | const char *str; |
| 618 | char *submod; |
| 619 | struct module_clb_list list[3] = {0}; |
| 620 | |
| 621 | list[0].name = "asub"; |
| 622 | list[1].name = "bsub"; |
| 623 | |
| 624 | /* collision of module's top-levels */ |
| 625 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; feature g; feature g;}"; |
| 626 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 627 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 628 | CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 629 | |
| 630 | /* collision of submodule's top-levels */ |
| 631 | submod = "submodule asub {belongs-to a {prefix a;} feature g; feature g;}"; |
| 632 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}"; |
| 633 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod); |
| 634 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 635 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 636 | CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 637 | |
| 638 | /* collision of module's top-level with submodule's top-levels */ |
| 639 | submod = "submodule asub {belongs-to a {prefix a;} feature g;}"; |
| 640 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; feature g;}"; |
| 641 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod); |
| 642 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 643 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 644 | CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 645 | |
| 646 | /* collision of submodule's top-level with another submodule's top-levels */ |
| 647 | str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}"; |
| 648 | list[0].data = "submodule asub {belongs-to a {prefix a;} feature g;}"; |
| 649 | list[1].data = "submodule bsub {belongs-to a {prefix a;} feature g;}"; |
| 650 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 651 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 652 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, 0); |
| 653 | CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static void |
| 657 | test_accessible_tree(void **state) |
| 658 | { |
| 659 | const char *str; |
| 660 | |
| 661 | /* config -> config */ |
| 662 | str = "module a {\n" |
| 663 | " namespace urn:a;\n" |
| 664 | " prefix a;\n" |
| 665 | " container cont {\n" |
| 666 | " leaf l {\n" |
| 667 | " type empty;\n" |
| 668 | " }\n" |
| 669 | " }\n" |
| 670 | " container cont2 {\n" |
| 671 | " leaf l2 {\n" |
| 672 | " must ../../cont/l;\n" |
| 673 | " type leafref {\n" |
| 674 | " path /cont/l;\n" |
| 675 | " }\n" |
| 676 | " }\n" |
| 677 | " }\n" |
| 678 | "}"; |
| 679 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 680 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 681 | |
| 682 | /* config -> state leafref */ |
| 683 | str = "module b {\n" |
| 684 | " namespace urn:b;\n" |
| 685 | " prefix b;\n" |
| 686 | " container cont {\n" |
| 687 | " config false;\n" |
| 688 | " leaf l {\n" |
| 689 | " type empty;\n" |
| 690 | " }\n" |
| 691 | " }\n" |
| 692 | " container cont2 {\n" |
| 693 | " leaf l2 {\n" |
| 694 | " type leafref {\n" |
| 695 | " path /cont/l;\n" |
| 696 | " }\n" |
| 697 | " }\n" |
| 698 | " }\n" |
| 699 | "}"; |
| 700 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
| 701 | CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data" |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 702 | " (as the leafref does), but it does not.", "/b:cont2/l2", 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 703 | |
| 704 | /* config -> state must */ |
| 705 | str = "module b {\n" |
| 706 | " namespace urn:b;\n" |
| 707 | " prefix b;\n" |
| 708 | " container cont {\n" |
| 709 | " config false;\n" |
| 710 | " leaf l {\n" |
| 711 | " type empty;\n" |
| 712 | " }\n" |
| 713 | " }\n" |
| 714 | " container cont2 {\n" |
| 715 | " leaf l2 {\n" |
| 716 | " must ../../cont/l;\n" |
| 717 | " type empty;\n" |
| 718 | " }\n" |
| 719 | " }\n" |
| 720 | "}"; |
| 721 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 722 | CHECK_LOG_CTX("Schema node \"cont\" for parent \"<config-root>\" not found; in expr \"../../cont\" " |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 723 | "with context node \"/b:cont2/l2\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 724 | |
| 725 | /* state -> config */ |
| 726 | str = "module c {\n" |
| 727 | " namespace urn:c;\n" |
| 728 | " prefix c;\n" |
| 729 | " container cont {\n" |
| 730 | " leaf l {\n" |
| 731 | " type empty;\n" |
| 732 | " }\n" |
| 733 | " }\n" |
| 734 | " container cont2 {\n" |
| 735 | " config false;\n" |
| 736 | " leaf l2 {\n" |
| 737 | " must ../../cont/l;\n" |
| 738 | " type leafref {\n" |
| 739 | " path /cont/l;\n" |
| 740 | " }\n" |
| 741 | " }\n" |
| 742 | " }\n" |
| 743 | "}"; |
| 744 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 745 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 746 | |
| 747 | /* notif -> state */ |
| 748 | str = "module d {\n" |
| 749 | " namespace urn:d;\n" |
| 750 | " prefix d;\n" |
| 751 | " container cont {\n" |
| 752 | " config false;\n" |
| 753 | " leaf l {\n" |
| 754 | " type empty;\n" |
| 755 | " }\n" |
| 756 | " }\n" |
| 757 | " notification notif {\n" |
| 758 | " leaf l2 {\n" |
| 759 | " must ../../cont/l;\n" |
| 760 | " type leafref {\n" |
| 761 | " path /cont/l;\n" |
| 762 | " }\n" |
| 763 | " }\n" |
| 764 | " }\n" |
| 765 | "}"; |
| 766 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 767 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 768 | |
| 769 | /* notif -> notif */ |
| 770 | str = "module e {\n" |
| 771 | " namespace urn:e;\n" |
| 772 | " prefix e;\n" |
| 773 | " notification notif {\n" |
| 774 | " leaf l {\n" |
| 775 | " type empty;\n" |
| 776 | " }\n" |
| 777 | " leaf l2 {\n" |
| 778 | " must ../../notif/l;\n" |
| 779 | " type leafref {\n" |
| 780 | " path /notif/l;\n" |
| 781 | " }\n" |
| 782 | " }\n" |
| 783 | " }\n" |
| 784 | "}"; |
| 785 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 786 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 787 | |
| 788 | /* rpc input -> state */ |
| 789 | str = "module f {\n" |
| 790 | " namespace urn:f;\n" |
| 791 | " prefix f;\n" |
| 792 | " container cont {\n" |
| 793 | " config false;\n" |
| 794 | " leaf l {\n" |
| 795 | " type empty;\n" |
| 796 | " }\n" |
| 797 | " }\n" |
| 798 | " rpc rp {\n" |
| 799 | " input {\n" |
| 800 | " leaf l2 {\n" |
| 801 | " must ../../cont/l;\n" |
| 802 | " type leafref {\n" |
| 803 | " path /cont/l;\n" |
| 804 | " }\n" |
| 805 | " }\n" |
| 806 | " }\n" |
| 807 | " }\n" |
| 808 | "}"; |
| 809 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 810 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 811 | |
| 812 | /* rpc input -> rpc input */ |
| 813 | str = "module g {\n" |
| 814 | " namespace urn:g;\n" |
| 815 | " prefix g;\n" |
| 816 | " rpc rp {\n" |
| 817 | " input {\n" |
| 818 | " leaf l {\n" |
| 819 | " type empty;\n" |
| 820 | " }\n" |
| 821 | " leaf l2 {\n" |
| 822 | " must ../l;\n" |
| 823 | " type leafref {\n" |
| 824 | " path /rp/l;\n" |
| 825 | " }\n" |
| 826 | " }\n" |
| 827 | " }\n" |
| 828 | " }\n" |
| 829 | "}"; |
| 830 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 831 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 832 | |
| 833 | /* rpc input -> rpc output leafref */ |
| 834 | str = "module h {\n" |
| 835 | " namespace urn:h;\n" |
| 836 | " prefix h;\n" |
| 837 | " rpc rp {\n" |
| 838 | " input {\n" |
| 839 | " leaf l2 {\n" |
| 840 | " type leafref {\n" |
| 841 | " path /rp/l;\n" |
| 842 | " }\n" |
| 843 | " }\n" |
| 844 | " }\n" |
| 845 | " output {\n" |
| 846 | " leaf l {\n" |
| 847 | " type empty;\n" |
| 848 | " }\n" |
| 849 | " }\n" |
| 850 | " }\n" |
| 851 | "}"; |
| 852 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 853 | CHECK_LOG_CTX("Not found node \"l\" in path.", "/h:rp/input/l2", 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 854 | |
| 855 | /* rpc input -> rpc output must */ |
| 856 | str = "module h {\n" |
| 857 | " namespace urn:h;\n" |
| 858 | " prefix h;\n" |
| 859 | " rpc rp {\n" |
| 860 | " input {\n" |
| 861 | " leaf l2 {\n" |
| 862 | " must ../l;\n" |
| 863 | " type empty;\n" |
| 864 | " }\n" |
| 865 | " }\n" |
| 866 | " output {\n" |
| 867 | " leaf l {\n" |
| 868 | " type empty;\n" |
| 869 | " }\n" |
| 870 | " }\n" |
| 871 | " }\n" |
| 872 | "}"; |
| 873 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 874 | CHECK_LOG_CTX("Schema node \"l\" for parent \"/h:rp\" not found; in expr \"../l\" with context node \"/h:rp/input/l2\".", |
| 875 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 876 | |
| 877 | /* rpc input -> notif leafref */ |
| 878 | str = "module i {\n" |
| 879 | " namespace urn:i;\n" |
| 880 | " prefix i;\n" |
| 881 | " rpc rp {\n" |
| 882 | " input {\n" |
| 883 | " leaf l2 {\n" |
| 884 | " type leafref {\n" |
| 885 | " path ../../notif/l;\n" |
| 886 | " }\n" |
| 887 | " }\n" |
| 888 | " }\n" |
| 889 | " }\n" |
| 890 | " notification notif {\n" |
| 891 | " leaf l {\n" |
| 892 | " type empty;\n" |
| 893 | " }\n" |
| 894 | " }\n" |
| 895 | "}"; |
| 896 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 897 | CHECK_LOG_CTX("Not found node \"notif\" in path.", "/i:rp/input/l2", 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 898 | |
| 899 | /* rpc input -> notif must */ |
| 900 | str = "module i {\n" |
| 901 | " namespace urn:i;\n" |
| 902 | " prefix i;\n" |
| 903 | " rpc rp {\n" |
| 904 | " input {\n" |
| 905 | " leaf l2 {\n" |
| 906 | " must /notif/l;\n" |
| 907 | " type empty;\n" |
| 908 | " }\n" |
| 909 | " }\n" |
| 910 | " }\n" |
| 911 | " notification notif {\n" |
| 912 | " leaf l {\n" |
| 913 | " type empty;\n" |
| 914 | " }\n" |
| 915 | " }\n" |
| 916 | "}"; |
| 917 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 918 | CHECK_LOG_CTX("Schema node \"notif\" for parent \"<root>\" not found; in expr \"/notif\" " |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 919 | "with context node \"/i:rp/input/l2\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 920 | |
| 921 | /* action output -> state */ |
| 922 | str = "module j {\n" |
| 923 | " yang-version 1.1;\n" |
| 924 | " namespace urn:j;\n" |
| 925 | " prefix j;\n" |
| 926 | " container cont {\n" |
| 927 | " list ll {\n" |
| 928 | " key k;\n" |
| 929 | " leaf k {\n" |
| 930 | " type string;\n" |
| 931 | " }\n" |
| 932 | " action act {\n" |
| 933 | " output {\n" |
| 934 | " leaf l2 {\n" |
| 935 | " must /cont/l;\n" |
| 936 | " type leafref {\n" |
| 937 | " path ../../../l;\n" |
| 938 | " }\n" |
| 939 | " }\n" |
| 940 | " }\n" |
| 941 | " }\n" |
| 942 | " }\n" |
| 943 | " leaf l {\n" |
| 944 | " config false;\n" |
| 945 | " type empty;\n" |
| 946 | " }\n" |
| 947 | " }\n" |
| 948 | "}"; |
| 949 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 950 | CHECK_LOG_CTX(NULL, NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 951 | |
| 952 | /* action output -> action input leafref */ |
| 953 | str = "module k {\n" |
| 954 | " yang-version 1.1;\n" |
| 955 | " namespace urn:k;\n" |
| 956 | " prefix k;\n" |
| 957 | " container cont {\n" |
| 958 | " list ll {\n" |
| 959 | " key k;\n" |
| 960 | " leaf k {\n" |
| 961 | " type string;\n" |
| 962 | " }\n" |
| 963 | " action act {\n" |
| 964 | " input {\n" |
| 965 | " leaf l {\n" |
| 966 | " type empty;\n" |
| 967 | " }\n" |
| 968 | " }\n" |
| 969 | " output {\n" |
| 970 | " leaf l2 {\n" |
| 971 | " type leafref {\n" |
| 972 | " path ../l;\n" |
| 973 | " }\n" |
| 974 | " }\n" |
| 975 | " }\n" |
| 976 | " }\n" |
| 977 | " }\n" |
| 978 | " }\n" |
| 979 | "}"; |
| 980 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 981 | CHECK_LOG_CTX("Not found node \"l\" in path.", "/k:cont/ll/act/output/l2", 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 982 | |
| 983 | /* action output -> action input must */ |
| 984 | str = "module k {\n" |
| 985 | " yang-version 1.1;\n" |
| 986 | " namespace urn:k;\n" |
| 987 | " prefix k;\n" |
| 988 | " container cont {\n" |
| 989 | " list ll {\n" |
| 990 | " key k;\n" |
| 991 | " leaf k {\n" |
| 992 | " type string;\n" |
| 993 | " }\n" |
| 994 | " action act {\n" |
| 995 | " input {\n" |
| 996 | " leaf l {\n" |
| 997 | " type empty;\n" |
| 998 | " }\n" |
| 999 | " }\n" |
| 1000 | " output {\n" |
| 1001 | " leaf l2 {\n" |
| 1002 | " must /cont/ll/act/l;\n" |
| 1003 | " type empty;\n" |
| 1004 | " }\n" |
| 1005 | " }\n" |
| 1006 | " }\n" |
| 1007 | " }\n" |
| 1008 | " }\n" |
| 1009 | "}"; |
| 1010 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 1011 | CHECK_LOG_CTX("Schema node \"l\" for parent \"/k:cont/ll/act\" not found; in expr \"/cont/ll/act/l\" " |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1012 | "with context node \"/k:cont/ll/act/output/l2\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | static void |
| 1016 | test_includes(void **state) |
| 1017 | { |
| 1018 | struct lys_module *mod; |
| 1019 | |
| 1020 | { |
| 1021 | /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */ |
| 1022 | struct module_clb_list list[] = { |
| 1023 | {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"}, |
| 1024 | {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"}, |
| 1025 | {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"}, |
| 1026 | {NULL, NULL} |
| 1027 | }; |
| 1028 | |
| 1029 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 1030 | mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL); |
| 1031 | assert_non_null(mod); |
| 1032 | assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes)); |
| 1033 | assert_true(mod->parsed->includes[1].injected); |
| 1034 | } |
| 1035 | |
| 1036 | { |
| 1037 | /* YANG 1.1 - the missing include sub_b_two in main_b is error */ |
| 1038 | struct module_clb_list list[] = { |
| 1039 | {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"}, |
| 1040 | {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"}, |
| 1041 | {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"}, |
| 1042 | {NULL, NULL} |
| 1043 | }; |
| 1044 | |
| 1045 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 1046 | mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL); |
| 1047 | assert_null(mod); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1048 | CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL, 0); |
| 1049 | CHECK_LOG_CTX("Data model \"main_b\" not found in local searchdirs.", NULL, 0); |
| 1050 | CHECK_LOG_CTX("Parsing module \"main_b\" failed.", NULL, 0); |
| 1051 | CHECK_LOG_CTX("Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL, 0); |
| 1052 | CHECK_LOG_CTX("Data model \"sub_b_one\" not found in local searchdirs.", NULL, 0); |
| 1053 | CHECK_LOG_CTX("Parsing submodule \"sub_b_one\" failed.", NULL, 0); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 1054 | CHECK_LOG_CTX("YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes " |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1055 | "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL, 0); |
| 1056 | CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_b_one) are not necessary.", |
| 1057 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | { |
| 1061 | /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */ |
| 1062 | struct module_clb_list list[] = { |
| 1063 | {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"}, |
| 1064 | {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"}, |
| 1065 | {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_one;}"}, |
| 1066 | {NULL, NULL} |
| 1067 | }; |
| 1068 | |
| 1069 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list); |
| 1070 | mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL); |
| 1071 | assert_non_null(mod); |
| 1072 | assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes)); |
| 1073 | assert_false(mod->parsed->includes[1].injected); |
| 1074 | /* result is ok, but log includes the warning */ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1075 | CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_two) are not necessary.", |
| 1076 | NULL, 0); |
| 1077 | CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_one) are not necessary.", |
| 1078 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | static void |
| 1083 | test_key_order(void **state) |
| 1084 | { |
| 1085 | struct lys_module *mod; |
| 1086 | const struct lysc_node *node; |
| 1087 | |
| 1088 | struct module_clb_list list1[] = { |
Michal Vasko | 84bf6f4 | 2023-05-19 11:09:48 +0200 | [diff] [blame] | 1089 | { |
| 1090 | "a", "module a {" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1091 | "yang-version 1.1;" |
| 1092 | "namespace urn:test:a;" |
| 1093 | "prefix a;" |
| 1094 | "list l {" |
| 1095 | " key \"k1 k2\";" |
| 1096 | " leaf k2 {type string;}" |
| 1097 | " leaf k1 {type string;}" |
| 1098 | "}" |
Michal Vasko | 84bf6f4 | 2023-05-19 11:09:48 +0200 | [diff] [blame] | 1099 | "}" |
| 1100 | }, |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1101 | {NULL, NULL} |
| 1102 | }; |
| 1103 | |
| 1104 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list1); |
| 1105 | mod = ly_ctx_load_module(UTEST_LYCTX, "a", NULL, NULL); |
| 1106 | assert_non_null(mod); |
| 1107 | |
| 1108 | node = lysc_node_child(mod->compiled->data); |
| 1109 | assert_string_equal("k1", node->name); |
| 1110 | node = node->next; |
| 1111 | assert_string_equal("k2", node->name); |
| 1112 | |
| 1113 | struct module_clb_list list2[] = { |
Michal Vasko | 84bf6f4 | 2023-05-19 11:09:48 +0200 | [diff] [blame] | 1114 | { |
| 1115 | "b", "module b {" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1116 | "yang-version 1.1;" |
| 1117 | "namespace urn:test:b;" |
| 1118 | "prefix b;" |
| 1119 | "list l {" |
| 1120 | " key \"k1 k2 k3 k4\";" |
| 1121 | " leaf k4 {type string;}" |
| 1122 | " container c {" |
| 1123 | " leaf l1 {type string;}" |
| 1124 | " }" |
| 1125 | " leaf k2 {type string;}" |
| 1126 | " leaf l2 {type string;}" |
| 1127 | " leaf k1 {type string;}" |
| 1128 | " leaf k3 {type string;}" |
| 1129 | "}" |
Michal Vasko | 84bf6f4 | 2023-05-19 11:09:48 +0200 | [diff] [blame] | 1130 | "}" |
| 1131 | }, |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1132 | {NULL, NULL} |
| 1133 | }; |
| 1134 | |
| 1135 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list2); |
| 1136 | mod = ly_ctx_load_module(UTEST_LYCTX, "b", NULL, NULL); |
| 1137 | assert_non_null(mod); |
| 1138 | |
| 1139 | node = lysc_node_child(mod->compiled->data); |
| 1140 | assert_string_equal("k1", node->name); |
| 1141 | node = node->next; |
| 1142 | assert_string_equal("k2", node->name); |
| 1143 | node = node->next; |
| 1144 | assert_string_equal("k3", node->name); |
| 1145 | node = node->next; |
| 1146 | assert_string_equal("k4", node->name); |
| 1147 | } |
| 1148 | |
| 1149 | static void |
| 1150 | test_disabled_enum(void **state) |
| 1151 | { |
| 1152 | const char *str; |
| 1153 | |
| 1154 | /* no enabled enum */ |
| 1155 | str = "module a {" |
| 1156 | "yang-version 1.1;" |
| 1157 | "namespace urn:test:a;" |
| 1158 | "prefix a;" |
| 1159 | "feature f;" |
| 1160 | "leaf l {type enumeration {" |
| 1161 | " enum e1 {if-feature f;}" |
| 1162 | " enum e2 {if-feature f;}" |
| 1163 | "}}" |
| 1164 | "}"; |
| 1165 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1166 | CHECK_LOG_CTX("Node \"l\" without any (or all disabled) valid values.", "/a:l", 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1167 | |
| 1168 | /* disabled default value */ |
| 1169 | str = "module a {" |
| 1170 | "yang-version 1.1;" |
| 1171 | "namespace urn:test:a;" |
| 1172 | "prefix a;" |
| 1173 | "feature f;" |
| 1174 | "leaf l {" |
| 1175 | " type enumeration {" |
| 1176 | " enum e1 {if-feature f;}" |
| 1177 | " enum e2;" |
| 1178 | " }" |
| 1179 | " default e1;" |
| 1180 | "}" |
| 1181 | "}"; |
| 1182 | assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1183 | CHECK_LOG_CTX("Invalid default - value does not fit the type (Invalid enumeration value \"e1\".).", "/a:l", 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void |
| 1187 | test_identity(void **state) |
| 1188 | { |
| 1189 | struct lys_module *mod, *mod_imp; |
| 1190 | |
| 1191 | /* |
| 1192 | * parsing YANG |
| 1193 | */ |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1194 | TEST_STMT_DUP(1, 0, "identity id", "description", "a", "b", 1); |
| 1195 | TEST_STMT_DUP(1, 0, "identity id", "reference", "a", "b", 1); |
| 1196 | TEST_STMT_DUP(1, 0, "identity id", "status", "current", "obsolete", 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1197 | |
| 1198 | /* full content */ |
| 1199 | TEST_SCHEMA_OK(1, 0, "identityone", |
| 1200 | "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}" |
| 1201 | "identity a; identity b; extension ext; feature x; feature y;", mod); |
| 1202 | assert_non_null(mod->parsed->identities); |
| 1203 | assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities)); |
| 1204 | |
| 1205 | /* invalid substatement */ |
| 1206 | TEST_STMT_SUBSTM_ERR(0, "identity", "organization", "XXX"); |
| 1207 | |
| 1208 | /* |
| 1209 | * parsing YIN |
| 1210 | */ |
| 1211 | /* max subelems */ |
| 1212 | TEST_SCHEMA_OK(1, 1, "identityone-yin", "<identity name=\"ident-name\">" |
| 1213 | "<if-feature name=\"iff\"/>" |
| 1214 | "<base name=\"base-name\"/>" |
| 1215 | "<status value=\"deprecated\"/>" |
| 1216 | "<description><text>desc</text></description>" |
| 1217 | "<reference><text>ref</text></reference>" |
Michal Vasko | 3cfa2bb | 2024-05-24 09:01:44 +0200 | [diff] [blame] | 1218 | "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1219 | "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod); |
| 1220 | assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities)); |
| 1221 | assert_string_equal(mod->parsed->identities[0].name, "ident-name"); |
| 1222 | assert_string_equal(mod->parsed->identities[0].bases[0], "base-name"); |
| 1223 | assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff"); |
| 1224 | assert_string_equal(mod->parsed->identities[0].dsc, "desc"); |
| 1225 | assert_string_equal(mod->parsed->identities[0].ref, "ref"); |
| 1226 | assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC); |
Michal Vasko | 3cfa2bb | 2024-05-24 09:01:44 +0200 | [diff] [blame] | 1227 | assert_string_equal(mod->parsed->identities[0].exts[0].name, "myext:ext"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1228 | |
| 1229 | /* min subelems */ |
| 1230 | TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod); |
| 1231 | assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities)); |
| 1232 | assert_string_equal(mod->parsed->identities[0].name, "ident-name"); |
| 1233 | |
| 1234 | /* invalid substatement */ |
| 1235 | TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>", |
| 1236 | "Invalid sub-elemnt \"if-feature\" of \"identity\" element - " |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1237 | "this sub-element is allowed only in modules with version 1.1 or newer.", NULL, 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1238 | |
| 1239 | /* |
| 1240 | * compiling |
| 1241 | */ |
| 1242 | TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp); |
| 1243 | TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}" |
| 1244 | "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}" |
| 1245 | "identity b4 {base b:b1; base b3;}", mod); |
| 1246 | assert_non_null(mod_imp->compiled); |
| 1247 | assert_non_null(mod_imp->identities); |
| 1248 | assert_non_null(mod->identities); |
| 1249 | assert_non_null(mod_imp->identities[0].derived); |
| 1250 | assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived)); |
| 1251 | assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]); |
| 1252 | assert_non_null(mod->identities[0].derived); |
| 1253 | assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived)); |
| 1254 | assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]); |
| 1255 | assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]); |
| 1256 | assert_non_null(mod->identities[1].derived); |
| 1257 | assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived)); |
| 1258 | assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]); |
| 1259 | assert_non_null(mod->identities[2].derived); |
| 1260 | assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived)); |
| 1261 | assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]); |
| 1262 | |
| 1263 | TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod); |
| 1264 | assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived)); |
| 1265 | assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]); |
| 1266 | |
| 1267 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} identity i1;}"); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1268 | TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "/inv:{identity='i1'}"); |
| 1269 | TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i1;}", "Identity \"i1\" is derived from itself.", "/inv:{identity='i1'}"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1270 | TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1271 | "Identity \"i1\" is indirectly derived from itself.", "/inv:{identity='i3'}"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1272 | |
| 1273 | /* base in non-implemented module */ |
| 1274 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, |
| 1275 | "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}"); |
| 1276 | TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod); |
| 1277 | |
| 1278 | /* default value from non-implemented module */ |
| 1279 | TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}", |
| 1280 | "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value" |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1281 | " - identity found in non-implemented module \"base\".).", "/ident2:l"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1282 | |
| 1283 | /* default value in typedef from non-implemented module */ |
| 1284 | TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}" |
| 1285 | "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid" |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1286 | " identityref \"b:i2\" value - identity found in non-implemented module \"base\".).", "/ident2:l"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* |
| 1289 | * printing |
| 1290 | */ |
| 1291 | |
| 1292 | /* |
| 1293 | * cleanup |
| 1294 | */ |
| 1295 | } |
| 1296 | |
| 1297 | static void |
| 1298 | test_feature(void **state) |
| 1299 | { |
| 1300 | struct lys_module *mod; |
| 1301 | const struct lysp_feature *f; |
| 1302 | |
| 1303 | /* |
| 1304 | * parsing YANG |
| 1305 | */ |
| 1306 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1307 | TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", 1); |
| 1308 | TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", 1); |
| 1309 | TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1310 | |
| 1311 | /* full content */ |
| 1312 | TEST_SCHEMA_OK(1, 0, "featureone", |
| 1313 | "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}" |
| 1314 | "extension ext; feature x; feature y;", mod); |
| 1315 | assert_non_null(mod->parsed->features); |
| 1316 | assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1317 | |
| 1318 | /* invalid substatement */ |
| 1319 | TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX"); |
| 1320 | |
| 1321 | /* |
| 1322 | * parsing YIN |
| 1323 | */ |
| 1324 | /* max subelems */ |
| 1325 | TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">" |
| 1326 | "<if-feature name=\"iff\"/>" |
| 1327 | "<status value=\"deprecated\"/>" |
| 1328 | "<description><text>desc</text></description>" |
| 1329 | "<reference><text>ref</text></reference>" |
Michal Vasko | 3cfa2bb | 2024-05-24 09:01:44 +0200 | [diff] [blame] | 1330 | "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1331 | "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod); |
| 1332 | assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1333 | assert_string_equal(mod->parsed->features[0].name, "feature-name"); |
| 1334 | assert_string_equal(mod->parsed->features[0].dsc, "desc"); |
| 1335 | assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC); |
| 1336 | assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff"); |
| 1337 | assert_string_equal(mod->parsed->features[0].ref, "ref"); |
Michal Vasko | 3cfa2bb | 2024-05-24 09:01:44 +0200 | [diff] [blame] | 1338 | assert_string_equal(mod->parsed->features[0].exts[0].name, "myext:ext"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1339 | |
| 1340 | /* min subelems */ |
| 1341 | TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod) |
| 1342 | assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1343 | assert_string_equal(mod->parsed->features[0].name, "feature-name"); |
| 1344 | |
| 1345 | /* invalid substatement */ |
| 1346 | TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1347 | "Unexpected sub-element \"organization\" of \"feature\" element.", NULL, 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * compiling |
| 1351 | */ |
| 1352 | |
| 1353 | TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n" |
| 1354 | "feature orfeature {if-feature \"f1 or f2\";}\n" |
| 1355 | "feature andfeature {if-feature \"f1 and f2\";}\n" |
| 1356 | "feature f6 {if-feature \"not f1\";}\n" |
| 1357 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n" |
| 1358 | "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n" |
| 1359 | "feature f9 {if-feature \"not not f1\";}", mod); |
| 1360 | assert_non_null(mod->parsed->features); |
| 1361 | assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1362 | |
| 1363 | /* all features are disabled by default */ |
| 1364 | LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) { |
| 1365 | assert_false(f->flags & LYS_FENABLED); |
| 1366 | } |
| 1367 | |
| 1368 | /* some invalid expressions */ |
| 1369 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1370 | "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1371 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1372 | "Invalid value \"f and\" of if-feature - unexpected end of expression.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1373 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1374 | "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1375 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1376 | "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1377 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1378 | "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1379 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature ---;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1380 | "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1381 | TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1382 | "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1383 | |
| 1384 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}"); |
| 1385 | TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1386 | "Feature \"f1\" is indirectly referenced from itself.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1387 | TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1388 | "Feature \"f1\" is referenced from itself.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1389 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1390 | "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1391 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1392 | "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1393 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1394 | "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1395 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1396 | "Invalid value \"f1 not \" of if-feature - unexpected end of expression.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1397 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1398 | "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1399 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1400 | "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1401 | |
| 1402 | /* |
| 1403 | * printing |
| 1404 | */ |
| 1405 | |
| 1406 | /* |
| 1407 | * cleanup |
| 1408 | */ |
| 1409 | } |
| 1410 | |
| 1411 | static void |
| 1412 | test_extension_argument(void **state) |
| 1413 | { |
| 1414 | struct lys_module *mod; |
| 1415 | const char *mod_def_yang = "module a {\n" |
| 1416 | " namespace \"urn:a\";\n" |
| 1417 | " prefix a;\n\n" |
| 1418 | " extension e {\n" |
| 1419 | " argument name;\n" |
| 1420 | " }\n\n" |
| 1421 | " a:e \"aaa\";\n" |
| 1422 | "}\n"; |
| 1423 | const char *mod_def_yin = |
| 1424 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1425 | "<module name=\"a\"\n" |
| 1426 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1427 | " xmlns:a=\"urn:a\">\n" |
| 1428 | " <namespace uri=\"urn:a\"/>\n" |
| 1429 | " <prefix value=\"a\"/>\n" |
| 1430 | " <extension name=\"e\">\n" |
| 1431 | " <argument name=\"name\"/>\n" |
| 1432 | " </extension>\n" |
| 1433 | " <a:e name=\"aaa\"/>\n" |
| 1434 | "</module>\n"; |
| 1435 | const char *mod_test_yin, *mod_test_yang; |
| 1436 | char *printed; |
| 1437 | |
| 1438 | mod_test_yang = "module b {\n" |
| 1439 | " namespace \"urn:b\";\n" |
| 1440 | " prefix b;\n\n" |
| 1441 | " import a {\n" |
| 1442 | " prefix a;\n" |
| 1443 | " }\n\n" |
| 1444 | " a:e \"xxx\";\n" |
| 1445 | "}\n"; |
| 1446 | mod_test_yin = |
| 1447 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1448 | "<module name=\"b\"\n" |
| 1449 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1450 | " xmlns:b=\"urn:b\"\n" |
| 1451 | " xmlns:a=\"urn:a\">\n" |
| 1452 | " <namespace uri=\"urn:b\"/>\n" |
| 1453 | " <prefix value=\"b\"/>\n" |
| 1454 | " <import module=\"a\">\n" |
| 1455 | " <prefix value=\"a\"/>\n" |
| 1456 | " </import>\n" |
| 1457 | " <a:e name=\"xxx\"/>\n" |
| 1458 | "</module>\n"; |
| 1459 | |
| 1460 | /* from YANG */ |
| 1461 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang); |
| 1462 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod)); |
| 1463 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1464 | assert_string_equal(printed, mod_test_yang); |
| 1465 | free(printed); |
| 1466 | |
| 1467 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1468 | assert_string_equal(printed, mod_test_yin); |
| 1469 | free(printed); |
| 1470 | |
| 1471 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1472 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1473 | assert_string_equal(printed, mod_def_yang); |
| 1474 | free(printed); |
| 1475 | |
| 1476 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1477 | assert_string_equal(printed, mod_def_yin); |
| 1478 | free(printed); |
| 1479 | |
| 1480 | /* context reset */ |
| 1481 | ly_ctx_destroy(UTEST_LYCTX); |
| 1482 | ly_ctx_new(NULL, 0, &UTEST_LYCTX); |
| 1483 | |
| 1484 | /* from YIN */ |
| 1485 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin); |
| 1486 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod)); |
| 1487 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1488 | assert_string_equal(printed, mod_test_yang); |
| 1489 | free(printed); |
| 1490 | |
| 1491 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1492 | assert_string_equal(printed, mod_test_yin); |
| 1493 | free(printed); |
| 1494 | |
| 1495 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1496 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1497 | assert_string_equal(printed, mod_def_yang); |
| 1498 | free(printed); |
| 1499 | |
| 1500 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1501 | assert_string_equal(printed, mod_def_yin); |
| 1502 | free(printed); |
| 1503 | } |
| 1504 | |
| 1505 | static void |
| 1506 | test_extension_argument_element(void **state) |
| 1507 | { |
| 1508 | struct lys_module *mod; |
| 1509 | const char *mod_def_yang = "module a {\n" |
| 1510 | " namespace \"urn:a\";\n" |
| 1511 | " prefix a;\n\n" |
| 1512 | " extension e {\n" |
| 1513 | " argument name {\n" |
| 1514 | " yin-element true;\n" |
| 1515 | " }\n" |
| 1516 | " }\n\n" |
| 1517 | " a:e \"aaa\";\n" |
| 1518 | "}\n"; |
| 1519 | const char *mod_def_yin = |
| 1520 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1521 | "<module name=\"a\"\n" |
| 1522 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1523 | " xmlns:a=\"urn:a\">\n" |
| 1524 | " <namespace uri=\"urn:a\"/>\n" |
| 1525 | " <prefix value=\"a\"/>\n" |
| 1526 | " <extension name=\"e\">\n" |
| 1527 | " <argument name=\"name\">\n" |
| 1528 | " <yin-element value=\"true\"/>\n" |
| 1529 | " </argument>\n" |
| 1530 | " </extension>\n" |
| 1531 | " <a:e>\n" |
| 1532 | " <a:name>aaa</a:name>\n" |
| 1533 | " </a:e>\n" |
| 1534 | "</module>\n"; |
| 1535 | const char *mod_test_yin, *mod_test_yang; |
| 1536 | char *printed; |
| 1537 | |
| 1538 | mod_test_yang = "module b {\n" |
| 1539 | " namespace \"urn:b\";\n" |
| 1540 | " prefix b;\n\n" |
| 1541 | " import a {\n" |
| 1542 | " prefix a;\n" |
| 1543 | " }\n\n" |
| 1544 | " a:e \"xxx\";\n" |
| 1545 | "}\n"; |
| 1546 | mod_test_yin = |
| 1547 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1548 | "<module name=\"b\"\n" |
| 1549 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1550 | " xmlns:b=\"urn:b\"\n" |
| 1551 | " xmlns:a=\"urn:a\">\n" |
| 1552 | " <namespace uri=\"urn:b\"/>\n" |
| 1553 | " <prefix value=\"b\"/>\n" |
| 1554 | " <import module=\"a\">\n" |
| 1555 | " <prefix value=\"a\"/>\n" |
| 1556 | " </import>\n" |
| 1557 | " <a:e>\n" |
| 1558 | " <a:name>xxx</a:name>\n" |
| 1559 | " </a:e>\n" |
| 1560 | "</module>\n"; |
| 1561 | |
| 1562 | /* from YANG */ |
| 1563 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang); |
| 1564 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod)); |
| 1565 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1566 | assert_string_equal(printed, mod_test_yang); |
| 1567 | free(printed); |
| 1568 | |
| 1569 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1570 | assert_string_equal(printed, mod_test_yin); |
| 1571 | free(printed); |
| 1572 | |
| 1573 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1574 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1575 | assert_string_equal(printed, mod_def_yang); |
| 1576 | free(printed); |
| 1577 | |
| 1578 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1579 | assert_string_equal(printed, mod_def_yin); |
| 1580 | free(printed); |
| 1581 | |
| 1582 | /* context reset */ |
| 1583 | ly_ctx_destroy(UTEST_LYCTX); |
| 1584 | ly_ctx_new(NULL, 0, &UTEST_LYCTX); |
| 1585 | |
| 1586 | /* from YIN */ |
| 1587 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin); |
| 1588 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod)); |
| 1589 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1590 | assert_string_equal(printed, mod_test_yang); |
| 1591 | free(printed); |
| 1592 | |
| 1593 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1594 | assert_string_equal(printed, mod_test_yin); |
| 1595 | free(printed); |
| 1596 | |
| 1597 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1598 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1599 | assert_string_equal(printed, mod_def_yang); |
| 1600 | free(printed); |
| 1601 | |
| 1602 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1603 | assert_string_equal(printed, mod_def_yin); |
| 1604 | free(printed); |
| 1605 | |
| 1606 | /* invalid */ |
| 1607 | mod_test_yang = "module x { namespace \"urn:x\"; prefix x; import a { prefix a; } a:e; }"; |
| 1608 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1609 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
| 1610 | CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1611 | |
| 1612 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1613 | "<module name=\"x\"\n" |
| 1614 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1615 | " xmlns:x=\"urn:x\"\n" |
| 1616 | " xmlns:a=\"urn:a\">\n" |
| 1617 | " <namespace uri=\"urn:x\"/>\n" |
| 1618 | " <prefix value=\"x\"/>\n" |
| 1619 | " <import module=\"a\">\n" |
| 1620 | " <prefix value=\"a\"/>\n" |
| 1621 | " </import>\n\n" |
| 1622 | " <a:e/>\n" |
| 1623 | "</module>\n"; |
| 1624 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1625 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
| 1626 | CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1627 | |
| 1628 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1629 | "<module name=\"x\"\n" |
| 1630 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1631 | " xmlns:x=\"urn:x\"\n" |
| 1632 | " xmlns:a=\"urn:a\">\n" |
| 1633 | " <namespace uri=\"urn:x\"/>\n" |
| 1634 | " <prefix value=\"x\"/>\n" |
| 1635 | " <import module=\"a\">\n" |
| 1636 | " <prefix value=\"a\"/>\n" |
| 1637 | " </import>\n\n" |
| 1638 | " <a:e name=\"xxx\"/>\n" |
| 1639 | "</module>\n"; |
| 1640 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1641 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
| 1642 | CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1643 | |
| 1644 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1645 | "<module name=\"x\"\n" |
| 1646 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1647 | " xmlns:x=\"urn:x\"\n" |
| 1648 | " xmlns:a=\"urn:a\">\n" |
| 1649 | " <namespace uri=\"urn:x\"/>\n" |
| 1650 | " <prefix value=\"x\"/>\n" |
| 1651 | " <import module=\"a\">\n" |
| 1652 | " <prefix value=\"a\"/>\n" |
| 1653 | " </import>\n\n" |
| 1654 | " <a:e>\n" |
| 1655 | " <x:name>xxx</x:name>\n" |
| 1656 | " </a:e>\n" |
| 1657 | "</module>\n"; |
| 1658 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1659 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 1660 | CHECK_LOG_CTX("Extension instance \"a:e\" element and its argument element \"name\" are expected in the same namespace, but they differ.", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1661 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1662 | |
| 1663 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1664 | "<module name=\"x\"\n" |
| 1665 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1666 | " xmlns:x=\"urn:x\"\n" |
| 1667 | " xmlns:a=\"urn:a\">\n" |
| 1668 | " <namespace uri=\"urn:x\"/>\n" |
| 1669 | " <prefix value=\"x\"/>\n" |
| 1670 | " <import module=\"a\">\n" |
| 1671 | " <prefix value=\"a\"/>\n" |
| 1672 | " </import>\n\n" |
| 1673 | " <a:e>\n" |
| 1674 | " <a:value>xxx</a:value>\n" |
| 1675 | " </a:e>\n" |
| 1676 | "</module>\n"; |
| 1677 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL)); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1678 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 1679 | CHECK_LOG_CTX("Extension instance \"a:e\" expects argument element \"name\" as its first XML child, but \"value\" element found.", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1680 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1681 | |
| 1682 | } |
| 1683 | |
| 1684 | static void |
| 1685 | test_extension_compile(void **state) |
| 1686 | { |
| 1687 | struct lys_module *mod; |
| 1688 | struct lysc_ctx cctx = {0}; |
| 1689 | struct lysp_ext_instance ext_p = {0}; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1690 | struct lysp_ext_substmt *substmtp; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1691 | struct lysp_stmt child = {0}; |
| 1692 | struct lysc_ext_instance ext_c = {0}; |
| 1693 | struct lysc_ext_substmt *substmt; |
| 1694 | LY_ERR rc = LY_SUCCESS; |
| 1695 | |
| 1696 | /* current module, whatever */ |
| 1697 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang"); |
| 1698 | assert_true(mod); |
| 1699 | |
| 1700 | /* compile context */ |
| 1701 | cctx.ctx = UTEST_LYCTX; |
| 1702 | cctx.cur_mod = mod; |
| 1703 | cctx.pmod = mod->parsed; |
| 1704 | cctx.path_len = 1; |
| 1705 | cctx.path[0] = '/'; |
| 1706 | |
| 1707 | /* parsed ext instance */ |
| 1708 | lydict_insert(UTEST_LYCTX, "pref:my-ext", 0, &ext_p.name); |
| 1709 | ext_p.format = LY_VALUE_JSON; |
| 1710 | ext_p.parent_stmt = LY_STMT_MODULE; |
| 1711 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1712 | LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_p.substmts, substmtp, rc, cleanup); |
| 1713 | |
| 1714 | substmtp->stmt = LY_STMT_ERROR_MESSAGE; |
Michal Vasko | 53eb6a1 | 2024-06-13 11:38:47 +0200 | [diff] [blame] | 1715 | substmtp->storage = (uintptr_t)(void *)&ext_p.parsed; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1716 | /* fake parse */ |
| 1717 | lydict_insert(UTEST_LYCTX, "my error", 0, (const char **)&ext_p.parsed); |
| 1718 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1719 | /* compiled ext instance */ |
| 1720 | ext_c.parent_stmt = ext_p.parent_stmt; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1721 | LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_c.substmts, substmt, rc, cleanup); |
| 1722 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1723 | substmt->stmt = LY_STMT_ERROR_MESSAGE; |
Michal Vasko | 53eb6a1 | 2024-06-13 11:38:47 +0200 | [diff] [blame] | 1724 | substmt->storage = (uintptr_t)(void *)&ext_c.compiled; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1725 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1726 | /* |
| 1727 | * error-message |
| 1728 | */ |
| 1729 | ext_p.child = &child; |
| 1730 | lydict_insert(UTEST_LYCTX, "error-message", 0, &child.stmt); |
| 1731 | lydict_insert(UTEST_LYCTX, "my error", 0, &child.arg); |
| 1732 | child.format = LY_VALUE_JSON; |
| 1733 | child.kw = LY_STMT_ERROR_MESSAGE; |
| 1734 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1735 | /* compile */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1736 | assert_int_equal(LY_SUCCESS, lyplg_ext_compile_extension_instance(&cctx, &ext_p, &ext_c)); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1737 | |
| 1738 | /* check */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1739 | assert_string_equal(ext_c.compiled, "my error"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1740 | |
| 1741 | cleanup: |
| 1742 | lydict_remove(UTEST_LYCTX, ext_p.name); |
| 1743 | lydict_remove(UTEST_LYCTX, child.stmt); |
| 1744 | lydict_remove(UTEST_LYCTX, child.arg); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1745 | LY_ARRAY_FREE(ext_p.substmts); |
| 1746 | lydict_remove(UTEST_LYCTX, ext_p.parsed); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1747 | LY_ARRAY_FREE(ext_c.substmts); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1748 | lydict_remove(UTEST_LYCTX, ext_c.compiled); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1749 | if (rc) { |
| 1750 | fail(); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | static void |
| 1755 | test_ext_recursive(void **state) |
| 1756 | { |
| 1757 | const char *mod_base_yang, *mod_imp_yang, *mod_base_yin, *mod_imp_yin; |
| 1758 | |
| 1759 | mod_imp_yang = "module b {\n" |
| 1760 | " namespace \"urn:b\";\n" |
| 1761 | " prefix b;\n\n" |
| 1762 | " extension use-in {\n" |
| 1763 | " argument name {\n" |
| 1764 | " b:arg-type {\n" |
| 1765 | " type string;\n" |
| 1766 | " }\n" |
| 1767 | " }\n" |
| 1768 | " b:use-in \"extension\";\n" |
| 1769 | " b:occurence \"*\";\n" |
| 1770 | " }\n" |
| 1771 | "\n" |
| 1772 | " extension substatement {\n" |
| 1773 | " argument name {\n" |
| 1774 | " b:arg-type {\n" |
| 1775 | " type string;\n" |
| 1776 | " }\n" |
| 1777 | " }\n" |
| 1778 | " b:use-in \"extension\";\n" |
| 1779 | " b:occurence \"*\";\n" |
| 1780 | " b:substatement \"b:occurence\";\n" |
| 1781 | " }\n" |
| 1782 | "\n" |
| 1783 | " extension arg-type {\n" |
| 1784 | " b:use-in \"argument\";\n" |
| 1785 | " b:substatement \"type\" {\n" |
| 1786 | " b:occurence \"1\";\n" |
| 1787 | " }\n" |
| 1788 | " b:substatement \"default\";\n" |
| 1789 | " }\n" |
| 1790 | "\n" |
| 1791 | " extension occurence {\n" |
| 1792 | " argument value {\n" |
| 1793 | " b:arg-type {\n" |
| 1794 | " type enumeration {\n" |
| 1795 | " enum \"?\";\n" |
| 1796 | " enum \"*\";\n" |
| 1797 | " enum \"+\";\n" |
| 1798 | " enum \"1\";\n" |
| 1799 | " }\n" |
| 1800 | " }\n" |
| 1801 | " }\n" |
| 1802 | " b:use-in \"extension\";\n" |
| 1803 | " }\n" |
| 1804 | "}\n"; |
| 1805 | |
| 1806 | mod_imp_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1807 | "<module name=\"b\"\n" |
| 1808 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1809 | " xmlns:b=\"urn:b\"\n" |
| 1810 | " xmlns:a=\"urn:a\">\n" |
| 1811 | " <namespace uri=\"urn:b\"/>\n" |
| 1812 | " <prefix value=\"b\"/>\n" |
| 1813 | " <import module=\"a\">\n" |
| 1814 | " <prefix value=\"a\"/>\n" |
| 1815 | " </import>\n\n" |
| 1816 | " <a:e name=\"xxx\"/>\n" |
| 1817 | "</module>\n"; |
| 1818 | |
| 1819 | mod_base_yang = "module a {\n" |
| 1820 | " namespace \"urn:a\";\n" |
| 1821 | " prefix a;\n\n" |
| 1822 | " import b {\n" |
| 1823 | " prefix b;\n" |
| 1824 | " }\n" |
| 1825 | "\n" |
| 1826 | " extension abstract {\n" |
| 1827 | " b:use-in \"identity\";\n" |
| 1828 | " }\n" |
| 1829 | "\n" |
| 1830 | " identity mount-id;\n" |
| 1831 | "\n" |
| 1832 | " identity yang-lib-id {\n" |
| 1833 | " base mount-id;\n" |
| 1834 | " a:abstract;\n" |
| 1835 | " }\n" |
| 1836 | "}\n"; |
| 1837 | |
| 1838 | mod_base_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1839 | "<module name=\"a\"\n" |
| 1840 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1841 | " xmlns:a=\"urn:a\">\n" |
| 1842 | " <namespace uri=\"urn:a\"/>\n" |
| 1843 | " <prefix value=\"a\"/>\n\n" |
| 1844 | " <extension name=\"e\">\n" |
| 1845 | " <argument name=\"name\"/>\n" |
| 1846 | " </extension>\n\n" |
| 1847 | " <a:e name=\"aaa\"/>\n" |
| 1848 | "</module>\n"; |
| 1849 | |
| 1850 | /* from YANG */ |
| 1851 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yang); |
| 1852 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yang, LYS_IN_YANG, NULL)); |
| 1853 | |
| 1854 | /* context reset */ |
| 1855 | ly_ctx_destroy(UTEST_LYCTX); |
| 1856 | ly_ctx_new(NULL, 0, &UTEST_LYCTX); |
| 1857 | |
| 1858 | /* from YIN */ |
| 1859 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yin); |
| 1860 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yin, LYS_IN_YIN, NULL)); |
| 1861 | } |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 1862 | |
Michal Vasko | d775df2 | 2024-05-24 09:25:41 +0200 | [diff] [blame] | 1863 | static void |
| 1864 | test_lysc_path(void **state) |
| 1865 | { |
| 1866 | const struct lysc_node *node; |
| 1867 | char *path; |
| 1868 | |
| 1869 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {yang-version 1.1; namespace urn:b;prefix b;" |
| 1870 | "container a {" |
| 1871 | " list l {" |
| 1872 | " key \"k l m\";" |
| 1873 | " leaf k {type string;}" |
| 1874 | " leaf l {type string;}" |
| 1875 | " leaf m {type string;}" |
Alexandre Snarskii | 669345c | 2024-05-24 15:56:36 +0300 | [diff] [blame] | 1876 | " leaf n {type string;}" |
Michal Vasko | d775df2 | 2024-05-24 09:25:41 +0200 | [diff] [blame] | 1877 | " }" |
| 1878 | "}}", LYS_IN_YANG, NULL)); |
| 1879 | |
| 1880 | node = lys_find_path(UTEST_LYCTX, NULL, "/b:a/l", 0); |
| 1881 | path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0); |
| 1882 | assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']"); |
| 1883 | free(path); |
Alexandre Snarskii | 669345c | 2024-05-24 15:56:36 +0300 | [diff] [blame] | 1884 | node = lys_find_path(UTEST_LYCTX, NULL, "/b:a/l/n", 0); |
| 1885 | path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0); |
| 1886 | assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n"); |
| 1887 | free(path); |
Michal Vasko | d775df2 | 2024-05-24 09:25:41 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1890 | int |
| 1891 | main(void) |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 1892 | { |
| 1893 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1894 | UTEST(test_getnext), |
| 1895 | UTEST(test_date), |
| 1896 | UTEST(test_revisions), |
aPiecek | 4f49a14 | 2021-06-29 15:32:39 +0200 | [diff] [blame] | 1897 | UTEST(test_collision_typedef), |
aPiecek | 0b36561 | 2021-06-30 13:12:58 +0200 | [diff] [blame] | 1898 | UTEST(test_collision_grouping), |
aPiecek | 14d07f0 | 2021-06-30 09:46:50 +0200 | [diff] [blame] | 1899 | UTEST(test_collision_identity), |
| 1900 | UTEST(test_collision_feature), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1901 | UTEST(test_accessible_tree), |
Radek Krejci | 589c547 | 2021-01-20 10:29:06 +0100 | [diff] [blame] | 1902 | UTEST(test_includes), |
Michal Vasko | ac4450e | 2021-11-09 13:53:40 +0100 | [diff] [blame] | 1903 | UTEST(test_key_order), |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 1904 | UTEST(test_disabled_enum), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1905 | UTEST(test_identity), |
| 1906 | UTEST(test_feature), |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 1907 | UTEST(test_extension_argument), |
| 1908 | UTEST(test_extension_argument_element), |
Michal Vasko | 633ae8a | 2022-08-25 09:52:02 +0200 | [diff] [blame] | 1909 | UTEST(test_extension_compile), |
Michal Vasko | 002d403 | 2022-08-03 12:13:32 +0200 | [diff] [blame] | 1910 | UTEST(test_ext_recursive), |
Michal Vasko | d775df2 | 2024-05-24 09:25:41 +0200 | [diff] [blame] | 1911 | UTEST(test_lysc_path), |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 1912 | }; |
| 1913 | |
| 1914 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1915 | } |