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>" |
| 1218 | /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */ |
| 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); |
| 1227 | /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext"); |
| 1228 | assert_non_null(mod->parsed->identities[0].exts[0].compiled); |
| 1229 | assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1); |
| 1230 | assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0); |
| 1231 | assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/ |
| 1232 | |
| 1233 | /* min subelems */ |
| 1234 | TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod); |
| 1235 | assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities)); |
| 1236 | assert_string_equal(mod->parsed->identities[0].name, "ident-name"); |
| 1237 | |
| 1238 | /* invalid substatement */ |
| 1239 | TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>", |
| 1240 | "Invalid sub-elemnt \"if-feature\" of \"identity\" element - " |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1241 | "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] | 1242 | |
| 1243 | /* |
| 1244 | * compiling |
| 1245 | */ |
| 1246 | TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp); |
| 1247 | TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}" |
| 1248 | "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}" |
| 1249 | "identity b4 {base b:b1; base b3;}", mod); |
| 1250 | assert_non_null(mod_imp->compiled); |
| 1251 | assert_non_null(mod_imp->identities); |
| 1252 | assert_non_null(mod->identities); |
| 1253 | assert_non_null(mod_imp->identities[0].derived); |
| 1254 | assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived)); |
| 1255 | assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]); |
| 1256 | assert_non_null(mod->identities[0].derived); |
| 1257 | assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived)); |
| 1258 | assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]); |
| 1259 | assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]); |
| 1260 | assert_non_null(mod->identities[1].derived); |
| 1261 | assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived)); |
| 1262 | assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]); |
| 1263 | assert_non_null(mod->identities[2].derived); |
| 1264 | assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived)); |
| 1265 | assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]); |
| 1266 | |
| 1267 | TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod); |
| 1268 | assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived)); |
| 1269 | assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]); |
| 1270 | |
| 1271 | 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] | 1272 | TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "/inv:{identity='i1'}"); |
| 1273 | 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] | 1274 | 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] | 1275 | "Identity \"i1\" is indirectly derived from itself.", "/inv:{identity='i3'}"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1276 | |
| 1277 | /* base in non-implemented module */ |
| 1278 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, |
| 1279 | "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}"); |
| 1280 | TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod); |
| 1281 | |
| 1282 | /* default value from non-implemented module */ |
| 1283 | TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}", |
| 1284 | "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] | 1285 | " - identity found in non-implemented module \"base\".).", "/ident2:l"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1286 | |
| 1287 | /* default value in typedef from non-implemented module */ |
| 1288 | TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}" |
| 1289 | "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] | 1290 | " 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] | 1291 | |
| 1292 | /* |
| 1293 | * printing |
| 1294 | */ |
| 1295 | |
| 1296 | /* |
| 1297 | * cleanup |
| 1298 | */ |
| 1299 | } |
| 1300 | |
| 1301 | static void |
| 1302 | test_feature(void **state) |
| 1303 | { |
| 1304 | struct lys_module *mod; |
| 1305 | const struct lysp_feature *f; |
| 1306 | |
| 1307 | /* |
| 1308 | * parsing YANG |
| 1309 | */ |
| 1310 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1311 | TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", 1); |
| 1312 | TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", 1); |
| 1313 | TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1314 | |
| 1315 | /* full content */ |
| 1316 | TEST_SCHEMA_OK(1, 0, "featureone", |
| 1317 | "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}" |
| 1318 | "extension ext; feature x; feature y;", mod); |
| 1319 | assert_non_null(mod->parsed->features); |
| 1320 | assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1321 | |
| 1322 | /* invalid substatement */ |
| 1323 | TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX"); |
| 1324 | |
| 1325 | /* |
| 1326 | * parsing YIN |
| 1327 | */ |
| 1328 | /* max subelems */ |
| 1329 | TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">" |
| 1330 | "<if-feature name=\"iff\"/>" |
| 1331 | "<status value=\"deprecated\"/>" |
| 1332 | "<description><text>desc</text></description>" |
| 1333 | "<reference><text>ref</text></reference>" |
| 1334 | /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */ |
| 1335 | "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod); |
| 1336 | assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1337 | assert_string_equal(mod->parsed->features[0].name, "feature-name"); |
| 1338 | assert_string_equal(mod->parsed->features[0].dsc, "desc"); |
| 1339 | assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC); |
| 1340 | assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff"); |
| 1341 | assert_string_equal(mod->parsed->features[0].ref, "ref"); |
| 1342 | /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext"); |
| 1343 | assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0); |
| 1344 | assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/ |
| 1345 | |
| 1346 | /* min subelems */ |
| 1347 | TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod) |
| 1348 | assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1349 | assert_string_equal(mod->parsed->features[0].name, "feature-name"); |
| 1350 | |
| 1351 | /* invalid substatement */ |
| 1352 | 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] | 1353 | "Unexpected sub-element \"organization\" of \"feature\" element.", NULL, 1); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1354 | |
| 1355 | /* |
| 1356 | * compiling |
| 1357 | */ |
| 1358 | |
| 1359 | TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n" |
| 1360 | "feature orfeature {if-feature \"f1 or f2\";}\n" |
| 1361 | "feature andfeature {if-feature \"f1 and f2\";}\n" |
| 1362 | "feature f6 {if-feature \"not f1\";}\n" |
| 1363 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n" |
| 1364 | "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n" |
| 1365 | "feature f9 {if-feature \"not not f1\";}", mod); |
| 1366 | assert_non_null(mod->parsed->features); |
| 1367 | assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features)); |
| 1368 | |
| 1369 | /* all features are disabled by default */ |
| 1370 | LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) { |
| 1371 | assert_false(f->flags & LYS_FENABLED); |
| 1372 | } |
| 1373 | |
| 1374 | /* some invalid expressions */ |
| 1375 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1376 | "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] | 1377 | 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] | 1378 | "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] | 1379 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1380 | "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1381 | 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] | 1382 | "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] | 1383 | 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] | 1384 | "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] | 1385 | 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] | 1386 | "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1387 | 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] | 1388 | "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] | 1389 | |
| 1390 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}"); |
| 1391 | 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] | 1392 | "Feature \"f1\" is indirectly referenced from itself.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1393 | TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1394 | "Feature \"f1\" is referenced from itself.", NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1395 | TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}", |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1396 | "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] | 1397 | 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] | 1398 | "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] | 1399 | 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] | 1400 | "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] | 1401 | 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] | 1402 | "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] | 1403 | 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] | 1404 | "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] | 1405 | 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] | 1406 | "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] | 1407 | |
| 1408 | /* |
| 1409 | * printing |
| 1410 | */ |
| 1411 | |
| 1412 | /* |
| 1413 | * cleanup |
| 1414 | */ |
| 1415 | } |
| 1416 | |
| 1417 | static void |
| 1418 | test_extension_argument(void **state) |
| 1419 | { |
| 1420 | struct lys_module *mod; |
| 1421 | const char *mod_def_yang = "module a {\n" |
| 1422 | " namespace \"urn:a\";\n" |
| 1423 | " prefix a;\n\n" |
| 1424 | " extension e {\n" |
| 1425 | " argument name;\n" |
| 1426 | " }\n\n" |
| 1427 | " a:e \"aaa\";\n" |
| 1428 | "}\n"; |
| 1429 | const char *mod_def_yin = |
| 1430 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1431 | "<module name=\"a\"\n" |
| 1432 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1433 | " xmlns:a=\"urn:a\">\n" |
| 1434 | " <namespace uri=\"urn:a\"/>\n" |
| 1435 | " <prefix value=\"a\"/>\n" |
| 1436 | " <extension name=\"e\">\n" |
| 1437 | " <argument name=\"name\"/>\n" |
| 1438 | " </extension>\n" |
| 1439 | " <a:e name=\"aaa\"/>\n" |
| 1440 | "</module>\n"; |
| 1441 | const char *mod_test_yin, *mod_test_yang; |
| 1442 | char *printed; |
| 1443 | |
| 1444 | mod_test_yang = "module b {\n" |
| 1445 | " namespace \"urn:b\";\n" |
| 1446 | " prefix b;\n\n" |
| 1447 | " import a {\n" |
| 1448 | " prefix a;\n" |
| 1449 | " }\n\n" |
| 1450 | " a:e \"xxx\";\n" |
| 1451 | "}\n"; |
| 1452 | mod_test_yin = |
| 1453 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1454 | "<module name=\"b\"\n" |
| 1455 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1456 | " xmlns:b=\"urn:b\"\n" |
| 1457 | " xmlns:a=\"urn:a\">\n" |
| 1458 | " <namespace uri=\"urn:b\"/>\n" |
| 1459 | " <prefix value=\"b\"/>\n" |
| 1460 | " <import module=\"a\">\n" |
| 1461 | " <prefix value=\"a\"/>\n" |
| 1462 | " </import>\n" |
| 1463 | " <a:e name=\"xxx\"/>\n" |
| 1464 | "</module>\n"; |
| 1465 | |
| 1466 | /* from YANG */ |
| 1467 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang); |
| 1468 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod)); |
| 1469 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1470 | assert_string_equal(printed, mod_test_yang); |
| 1471 | free(printed); |
| 1472 | |
| 1473 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1474 | assert_string_equal(printed, mod_test_yin); |
| 1475 | free(printed); |
| 1476 | |
| 1477 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1478 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1479 | assert_string_equal(printed, mod_def_yang); |
| 1480 | free(printed); |
| 1481 | |
| 1482 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1483 | assert_string_equal(printed, mod_def_yin); |
| 1484 | free(printed); |
| 1485 | |
| 1486 | /* context reset */ |
| 1487 | ly_ctx_destroy(UTEST_LYCTX); |
| 1488 | ly_ctx_new(NULL, 0, &UTEST_LYCTX); |
| 1489 | |
| 1490 | /* from YIN */ |
| 1491 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin); |
| 1492 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod)); |
| 1493 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1494 | assert_string_equal(printed, mod_test_yang); |
| 1495 | free(printed); |
| 1496 | |
| 1497 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1498 | assert_string_equal(printed, mod_test_yin); |
| 1499 | free(printed); |
| 1500 | |
| 1501 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1502 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1503 | assert_string_equal(printed, mod_def_yang); |
| 1504 | free(printed); |
| 1505 | |
| 1506 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1507 | assert_string_equal(printed, mod_def_yin); |
| 1508 | free(printed); |
| 1509 | } |
| 1510 | |
| 1511 | static void |
| 1512 | test_extension_argument_element(void **state) |
| 1513 | { |
| 1514 | struct lys_module *mod; |
| 1515 | const char *mod_def_yang = "module a {\n" |
| 1516 | " namespace \"urn:a\";\n" |
| 1517 | " prefix a;\n\n" |
| 1518 | " extension e {\n" |
| 1519 | " argument name {\n" |
| 1520 | " yin-element true;\n" |
| 1521 | " }\n" |
| 1522 | " }\n\n" |
| 1523 | " a:e \"aaa\";\n" |
| 1524 | "}\n"; |
| 1525 | const char *mod_def_yin = |
| 1526 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1527 | "<module name=\"a\"\n" |
| 1528 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1529 | " xmlns:a=\"urn:a\">\n" |
| 1530 | " <namespace uri=\"urn:a\"/>\n" |
| 1531 | " <prefix value=\"a\"/>\n" |
| 1532 | " <extension name=\"e\">\n" |
| 1533 | " <argument name=\"name\">\n" |
| 1534 | " <yin-element value=\"true\"/>\n" |
| 1535 | " </argument>\n" |
| 1536 | " </extension>\n" |
| 1537 | " <a:e>\n" |
| 1538 | " <a:name>aaa</a:name>\n" |
| 1539 | " </a:e>\n" |
| 1540 | "</module>\n"; |
| 1541 | const char *mod_test_yin, *mod_test_yang; |
| 1542 | char *printed; |
| 1543 | |
| 1544 | mod_test_yang = "module b {\n" |
| 1545 | " namespace \"urn:b\";\n" |
| 1546 | " prefix b;\n\n" |
| 1547 | " import a {\n" |
| 1548 | " prefix a;\n" |
| 1549 | " }\n\n" |
| 1550 | " a:e \"xxx\";\n" |
| 1551 | "}\n"; |
| 1552 | mod_test_yin = |
| 1553 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1554 | "<module name=\"b\"\n" |
| 1555 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1556 | " xmlns:b=\"urn:b\"\n" |
| 1557 | " xmlns:a=\"urn:a\">\n" |
| 1558 | " <namespace uri=\"urn:b\"/>\n" |
| 1559 | " <prefix value=\"b\"/>\n" |
| 1560 | " <import module=\"a\">\n" |
| 1561 | " <prefix value=\"a\"/>\n" |
| 1562 | " </import>\n" |
| 1563 | " <a:e>\n" |
| 1564 | " <a:name>xxx</a:name>\n" |
| 1565 | " </a:e>\n" |
| 1566 | "</module>\n"; |
| 1567 | |
| 1568 | /* from YANG */ |
| 1569 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang); |
| 1570 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod)); |
| 1571 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1572 | assert_string_equal(printed, mod_test_yang); |
| 1573 | free(printed); |
| 1574 | |
| 1575 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1576 | assert_string_equal(printed, mod_test_yin); |
| 1577 | free(printed); |
| 1578 | |
| 1579 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1580 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1581 | assert_string_equal(printed, mod_def_yang); |
| 1582 | free(printed); |
| 1583 | |
| 1584 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1585 | assert_string_equal(printed, mod_def_yin); |
| 1586 | free(printed); |
| 1587 | |
| 1588 | /* context reset */ |
| 1589 | ly_ctx_destroy(UTEST_LYCTX); |
| 1590 | ly_ctx_new(NULL, 0, &UTEST_LYCTX); |
| 1591 | |
| 1592 | /* from YIN */ |
| 1593 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin); |
| 1594 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod)); |
| 1595 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1596 | assert_string_equal(printed, mod_test_yang); |
| 1597 | free(printed); |
| 1598 | |
| 1599 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1600 | assert_string_equal(printed, mod_test_yin); |
| 1601 | free(printed); |
| 1602 | |
| 1603 | assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 1604 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 1605 | assert_string_equal(printed, mod_def_yang); |
| 1606 | free(printed); |
| 1607 | |
| 1608 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 1609 | assert_string_equal(printed, mod_def_yin); |
| 1610 | free(printed); |
| 1611 | |
| 1612 | /* invalid */ |
| 1613 | mod_test_yang = "module x { namespace \"urn:x\"; prefix x; import a { prefix a; } a:e; }"; |
| 1614 | 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] | 1615 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
| 1616 | 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] | 1617 | |
| 1618 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1619 | "<module name=\"x\"\n" |
| 1620 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1621 | " xmlns:x=\"urn:x\"\n" |
| 1622 | " xmlns:a=\"urn:a\">\n" |
| 1623 | " <namespace uri=\"urn:x\"/>\n" |
| 1624 | " <prefix value=\"x\"/>\n" |
| 1625 | " <import module=\"a\">\n" |
| 1626 | " <prefix value=\"a\"/>\n" |
| 1627 | " </import>\n\n" |
| 1628 | " <a:e/>\n" |
| 1629 | "</module>\n"; |
| 1630 | 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] | 1631 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
| 1632 | 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] | 1633 | |
| 1634 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1635 | "<module name=\"x\"\n" |
| 1636 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1637 | " xmlns:x=\"urn:x\"\n" |
| 1638 | " xmlns:a=\"urn:a\">\n" |
| 1639 | " <namespace uri=\"urn:x\"/>\n" |
| 1640 | " <prefix value=\"x\"/>\n" |
| 1641 | " <import module=\"a\">\n" |
| 1642 | " <prefix value=\"a\"/>\n" |
| 1643 | " </import>\n\n" |
| 1644 | " <a:e name=\"xxx\"/>\n" |
| 1645 | "</module>\n"; |
| 1646 | 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] | 1647 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
| 1648 | 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] | 1649 | |
| 1650 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1651 | "<module name=\"x\"\n" |
| 1652 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1653 | " xmlns:x=\"urn:x\"\n" |
| 1654 | " xmlns:a=\"urn:a\">\n" |
| 1655 | " <namespace uri=\"urn:x\"/>\n" |
| 1656 | " <prefix value=\"x\"/>\n" |
| 1657 | " <import module=\"a\">\n" |
| 1658 | " <prefix value=\"a\"/>\n" |
| 1659 | " </import>\n\n" |
| 1660 | " <a:e>\n" |
| 1661 | " <x:name>xxx</x:name>\n" |
| 1662 | " </a:e>\n" |
| 1663 | "</module>\n"; |
| 1664 | 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] | 1665 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 1666 | 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] | 1667 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1668 | |
| 1669 | mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1670 | "<module name=\"x\"\n" |
| 1671 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1672 | " xmlns:x=\"urn:x\"\n" |
| 1673 | " xmlns:a=\"urn:a\">\n" |
| 1674 | " <namespace uri=\"urn:x\"/>\n" |
| 1675 | " <prefix value=\"x\"/>\n" |
| 1676 | " <import module=\"a\">\n" |
| 1677 | " <prefix value=\"a\"/>\n" |
| 1678 | " </import>\n\n" |
| 1679 | " <a:e>\n" |
| 1680 | " <a:value>xxx</a:value>\n" |
| 1681 | " </a:e>\n" |
| 1682 | "</module>\n"; |
| 1683 | 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] | 1684 | CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL, 0); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 1685 | 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] | 1686 | NULL, 0); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1687 | |
| 1688 | } |
| 1689 | |
| 1690 | static void |
| 1691 | test_extension_compile(void **state) |
| 1692 | { |
| 1693 | struct lys_module *mod; |
| 1694 | struct lysc_ctx cctx = {0}; |
| 1695 | struct lysp_ext_instance ext_p = {0}; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1696 | struct lysp_ext_substmt *substmtp; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1697 | struct lysp_stmt child = {0}; |
| 1698 | struct lysc_ext_instance ext_c = {0}; |
| 1699 | struct lysc_ext_substmt *substmt; |
| 1700 | LY_ERR rc = LY_SUCCESS; |
| 1701 | |
| 1702 | /* current module, whatever */ |
| 1703 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang"); |
| 1704 | assert_true(mod); |
| 1705 | |
| 1706 | /* compile context */ |
| 1707 | cctx.ctx = UTEST_LYCTX; |
| 1708 | cctx.cur_mod = mod; |
| 1709 | cctx.pmod = mod->parsed; |
| 1710 | cctx.path_len = 1; |
| 1711 | cctx.path[0] = '/'; |
| 1712 | |
| 1713 | /* parsed ext instance */ |
| 1714 | lydict_insert(UTEST_LYCTX, "pref:my-ext", 0, &ext_p.name); |
| 1715 | ext_p.format = LY_VALUE_JSON; |
| 1716 | ext_p.parent_stmt = LY_STMT_MODULE; |
| 1717 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1718 | LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_p.substmts, substmtp, rc, cleanup); |
| 1719 | |
| 1720 | substmtp->stmt = LY_STMT_ERROR_MESSAGE; |
| 1721 | substmtp->storage = &ext_p.parsed; |
| 1722 | /* fake parse */ |
| 1723 | lydict_insert(UTEST_LYCTX, "my error", 0, (const char **)&ext_p.parsed); |
| 1724 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1725 | /* compiled ext instance */ |
| 1726 | ext_c.parent_stmt = ext_p.parent_stmt; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1727 | LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_c.substmts, substmt, rc, cleanup); |
| 1728 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1729 | substmt->stmt = LY_STMT_ERROR_MESSAGE; |
| 1730 | substmt->storage = &ext_c.compiled; |
| 1731 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1732 | /* |
| 1733 | * error-message |
| 1734 | */ |
| 1735 | ext_p.child = &child; |
| 1736 | lydict_insert(UTEST_LYCTX, "error-message", 0, &child.stmt); |
| 1737 | lydict_insert(UTEST_LYCTX, "my error", 0, &child.arg); |
| 1738 | child.format = LY_VALUE_JSON; |
| 1739 | child.kw = LY_STMT_ERROR_MESSAGE; |
| 1740 | |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1741 | /* compile */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1742 | 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] | 1743 | |
| 1744 | /* check */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1745 | assert_string_equal(ext_c.compiled, "my error"); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1746 | |
| 1747 | cleanup: |
| 1748 | lydict_remove(UTEST_LYCTX, ext_p.name); |
| 1749 | lydict_remove(UTEST_LYCTX, child.stmt); |
| 1750 | lydict_remove(UTEST_LYCTX, child.arg); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1751 | LY_ARRAY_FREE(ext_p.substmts); |
| 1752 | lydict_remove(UTEST_LYCTX, ext_p.parsed); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1753 | LY_ARRAY_FREE(ext_c.substmts); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 1754 | lydict_remove(UTEST_LYCTX, ext_c.compiled); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1755 | if (rc) { |
| 1756 | fail(); |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | static void |
| 1761 | test_ext_recursive(void **state) |
| 1762 | { |
| 1763 | const char *mod_base_yang, *mod_imp_yang, *mod_base_yin, *mod_imp_yin; |
| 1764 | |
| 1765 | mod_imp_yang = "module b {\n" |
| 1766 | " namespace \"urn:b\";\n" |
| 1767 | " prefix b;\n\n" |
| 1768 | " extension use-in {\n" |
| 1769 | " argument name {\n" |
| 1770 | " b:arg-type {\n" |
| 1771 | " type string;\n" |
| 1772 | " }\n" |
| 1773 | " }\n" |
| 1774 | " b:use-in \"extension\";\n" |
| 1775 | " b:occurence \"*\";\n" |
| 1776 | " }\n" |
| 1777 | "\n" |
| 1778 | " extension substatement {\n" |
| 1779 | " argument name {\n" |
| 1780 | " b:arg-type {\n" |
| 1781 | " type string;\n" |
| 1782 | " }\n" |
| 1783 | " }\n" |
| 1784 | " b:use-in \"extension\";\n" |
| 1785 | " b:occurence \"*\";\n" |
| 1786 | " b:substatement \"b:occurence\";\n" |
| 1787 | " }\n" |
| 1788 | "\n" |
| 1789 | " extension arg-type {\n" |
| 1790 | " b:use-in \"argument\";\n" |
| 1791 | " b:substatement \"type\" {\n" |
| 1792 | " b:occurence \"1\";\n" |
| 1793 | " }\n" |
| 1794 | " b:substatement \"default\";\n" |
| 1795 | " }\n" |
| 1796 | "\n" |
| 1797 | " extension occurence {\n" |
| 1798 | " argument value {\n" |
| 1799 | " b:arg-type {\n" |
| 1800 | " type enumeration {\n" |
| 1801 | " enum \"?\";\n" |
| 1802 | " enum \"*\";\n" |
| 1803 | " enum \"+\";\n" |
| 1804 | " enum \"1\";\n" |
| 1805 | " }\n" |
| 1806 | " }\n" |
| 1807 | " }\n" |
| 1808 | " b:use-in \"extension\";\n" |
| 1809 | " }\n" |
| 1810 | "}\n"; |
| 1811 | |
| 1812 | mod_imp_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1813 | "<module name=\"b\"\n" |
| 1814 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1815 | " xmlns:b=\"urn:b\"\n" |
| 1816 | " xmlns:a=\"urn:a\">\n" |
| 1817 | " <namespace uri=\"urn:b\"/>\n" |
| 1818 | " <prefix value=\"b\"/>\n" |
| 1819 | " <import module=\"a\">\n" |
| 1820 | " <prefix value=\"a\"/>\n" |
| 1821 | " </import>\n\n" |
| 1822 | " <a:e name=\"xxx\"/>\n" |
| 1823 | "</module>\n"; |
| 1824 | |
| 1825 | mod_base_yang = "module a {\n" |
| 1826 | " namespace \"urn:a\";\n" |
| 1827 | " prefix a;\n\n" |
| 1828 | " import b {\n" |
| 1829 | " prefix b;\n" |
| 1830 | " }\n" |
| 1831 | "\n" |
| 1832 | " extension abstract {\n" |
| 1833 | " b:use-in \"identity\";\n" |
| 1834 | " }\n" |
| 1835 | "\n" |
| 1836 | " identity mount-id;\n" |
| 1837 | "\n" |
| 1838 | " identity yang-lib-id {\n" |
| 1839 | " base mount-id;\n" |
| 1840 | " a:abstract;\n" |
| 1841 | " }\n" |
| 1842 | "}\n"; |
| 1843 | |
| 1844 | mod_base_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 1845 | "<module name=\"a\"\n" |
| 1846 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 1847 | " xmlns:a=\"urn:a\">\n" |
| 1848 | " <namespace uri=\"urn:a\"/>\n" |
| 1849 | " <prefix value=\"a\"/>\n\n" |
| 1850 | " <extension name=\"e\">\n" |
| 1851 | " <argument name=\"name\"/>\n" |
| 1852 | " </extension>\n\n" |
| 1853 | " <a:e name=\"aaa\"/>\n" |
| 1854 | "</module>\n"; |
| 1855 | |
| 1856 | /* from YANG */ |
| 1857 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yang); |
| 1858 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yang, LYS_IN_YANG, NULL)); |
| 1859 | |
| 1860 | /* context reset */ |
| 1861 | ly_ctx_destroy(UTEST_LYCTX); |
| 1862 | ly_ctx_new(NULL, 0, &UTEST_LYCTX); |
| 1863 | |
| 1864 | /* from YIN */ |
| 1865 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yin); |
| 1866 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yin, LYS_IN_YIN, NULL)); |
| 1867 | } |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 1868 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1869 | int |
| 1870 | main(void) |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 1871 | { |
| 1872 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1873 | UTEST(test_getnext), |
| 1874 | UTEST(test_date), |
| 1875 | UTEST(test_revisions), |
aPiecek | 4f49a14 | 2021-06-29 15:32:39 +0200 | [diff] [blame] | 1876 | UTEST(test_collision_typedef), |
aPiecek | 0b36561 | 2021-06-30 13:12:58 +0200 | [diff] [blame] | 1877 | UTEST(test_collision_grouping), |
aPiecek | 14d07f0 | 2021-06-30 09:46:50 +0200 | [diff] [blame] | 1878 | UTEST(test_collision_identity), |
| 1879 | UTEST(test_collision_feature), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1880 | UTEST(test_accessible_tree), |
Radek Krejci | 589c547 | 2021-01-20 10:29:06 +0100 | [diff] [blame] | 1881 | UTEST(test_includes), |
Michal Vasko | ac4450e | 2021-11-09 13:53:40 +0100 | [diff] [blame] | 1882 | UTEST(test_key_order), |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 1883 | UTEST(test_disabled_enum), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1884 | UTEST(test_identity), |
| 1885 | UTEST(test_feature), |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 1886 | UTEST(test_extension_argument), |
| 1887 | UTEST(test_extension_argument_element), |
Michal Vasko | 633ae8a | 2022-08-25 09:52:02 +0200 | [diff] [blame] | 1888 | UTEST(test_extension_compile), |
Michal Vasko | 002d403 | 2022-08-03 12:13:32 +0200 | [diff] [blame] | 1889 | UTEST(test_ext_recursive), |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 1890 | }; |
| 1891 | |
| 1892 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1893 | } |