Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_pattern.c |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 3 | * @author Radek Iša <isa@cesnet.cz> |
| 4 | * @brief test for int8 values |
| 5 | * |
| 6 | * Copyright (c) 2021 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | /* INCLUDE UTEST HEADER */ |
| 16 | #define _UTEST_MAIN_ |
| 17 | #include "../utests.h" |
| 18 | |
| 19 | /* GLOBAL INCLUDE HEADERS */ |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | /* LOCAL INCLUDE HEADERS */ |
| 23 | #include "libyang.h" |
| 24 | #include "path.h" |
| 25 | |
| 26 | #define MODULE_CREATE_YIN(MOD_NAME, NODES) \ |
| 27 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \ |
| 28 | "<module name=\"" MOD_NAME "\"\n" \ |
| 29 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" \ |
| 30 | " xmlns:pref=\"urn:tests:" MOD_NAME "\">\n" \ |
| 31 | " <yang-version value=\"1.1\"/>\n" \ |
| 32 | " <namespace uri=\"urn:tests:" MOD_NAME "\"/>\n" \ |
| 33 | " <prefix value=\"pref\"/>\n" \ |
| 34 | NODES \ |
| 35 | "</module>\n" |
| 36 | |
| 37 | #define MODULE_CREATE_YANG(MOD_NAME, NODES) \ |
| 38 | "module " MOD_NAME " {\n" \ |
| 39 | " yang-version 1.1;\n" \ |
| 40 | " namespace \"urn:tests:" MOD_NAME "\";\n" \ |
| 41 | " prefix pref;\n" \ |
| 42 | NODES \ |
| 43 | "}\n" |
| 44 | |
| 45 | #define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \ |
| 46 | { \ |
| 47 | struct lyd_node *tree; \ |
| 48 | const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \ |
| 49 | CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \ |
| 50 | CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \ |
Michal Vasko | 151ae6c | 2021-09-23 08:23:51 +0200 | [diff] [blame] | 51 | CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, __VA_ARGS__); \ |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 52 | lyd_free_all(tree); \ |
| 53 | } |
| 54 | |
| 55 | #define TEST_SUCCESS_JSON(MOD_NAME, DATA, TYPE, ...) \ |
| 56 | { \ |
| 57 | struct lyd_node *tree; \ |
| 58 | const char *data = "{\"" MOD_NAME ":port\":" DATA "}"; \ |
| 59 | CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \ |
| 60 | CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \ |
Michal Vasko | 151ae6c | 2021-09-23 08:23:51 +0200 | [diff] [blame] | 61 | CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, __VA_ARGS__); \ |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 62 | lyd_free_all(tree); \ |
| 63 | } |
| 64 | |
| 65 | #define TEST_ERROR_XML(MOD_NAME, DATA) \ |
| 66 | {\ |
| 67 | struct lyd_node *tree; \ |
| 68 | const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \ |
| 69 | CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \ |
| 70 | assert_null(tree); \ |
| 71 | } |
| 72 | |
| 73 | #define TEST_ERROR_JSON(MOD_NAME, DATA) \ |
| 74 | { \ |
| 75 | struct lyd_node *tree; \ |
| 76 | const char *data = "{\"" MOD_NAME ":port\":" DATA "}"; \ |
| 77 | CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \ |
| 78 | assert_null(tree); \ |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | test_schema_yang(void **state) |
| 83 | { |
| 84 | const char *schema; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 85 | struct lys_module *mod; |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 86 | struct lysc_node_leaf *lysc_leaf; |
| 87 | struct lysp_node_leaf *lysp_leaf; |
| 88 | struct lysc_pattern *pattern; |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 89 | |
| 90 | schema = MODULE_CREATE_YANG("T0", "leaf port {type string {" |
| 91 | "pattern \"[A-Za-z]*\"{" |
| 92 | "description \"pattern description\";" |
| 93 | "error-app-tag \"pattern err-apt-tag\";" |
| 94 | "error-message \"pattern error message\";}}}"); |
| 95 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod); |
| 96 | assert_non_null(mod); |
| 97 | lysc_leaf = (void *) mod->compiled->data; |
| 98 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 99 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1); |
| 100 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 101 | CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag", |
| 102 | "pattern error message", "[A-Za-z]*", 0, 0, NULL); |
| 103 | lysp_leaf = (void *) mod->parsed->data; |
| 104 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 105 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "string", 0, 1, 1, 0, 0, 0); |
| 106 | CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Za-z]*", "pattern description", |
| 107 | "pattern err-apt-tag", "pattern error message", 0, NULL); |
| 108 | |
| 109 | /* heredity */ |
| 110 | schema = MODULE_CREATE_YANG("T1", "typedef my_type {type string {" |
| 111 | "pattern \"[A-Za-z]*\"{" |
| 112 | "description \"pattern description\";" |
| 113 | "error-app-tag \"pattern err-apt-tag\";" |
| 114 | "error-message \"pattern error message\";}}}" |
| 115 | "leaf port {type my_type;}"); |
| 116 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod); |
| 117 | assert_non_null(mod); |
| 118 | lysc_leaf = (void *) mod->compiled->data; |
| 119 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 120 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1); |
| 121 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 122 | CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag", |
| 123 | "pattern error message", "[A-Za-z]*", 0, 0, NULL); |
| 124 | lysp_leaf = (void *) mod->parsed->data; |
| 125 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 126 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x0, 0, 0, "my_type", 0, 0, 1, 0, 0, 0); |
| 127 | |
| 128 | /* heredity new pattern */ |
| 129 | schema = MODULE_CREATE_YANG("T2", "typedef my_type {type string {" |
| 130 | "pattern \"[A-Za-z]*\"{" |
| 131 | "description \"pattern description\";" |
| 132 | "error-app-tag \"pattern err-apt-tag\";" |
| 133 | "error-message \"pattern error message\";}}}" |
| 134 | "leaf port {type my_type{pattern \"[A-Z]*\";}}"); |
| 135 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod); |
| 136 | assert_non_null(mod); |
| 137 | lysc_leaf = (void *) mod->compiled->data; |
| 138 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 139 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2); |
| 140 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 141 | CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag", |
| 142 | "pattern error message", "[A-Za-z]*", 0, 0, NULL); |
| 143 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1]; |
| 144 | CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[A-Z]*", 0, 0, NULL); |
| 145 | lysp_leaf = (void *) mod->parsed->data; |
| 146 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 147 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0); |
| 148 | CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", NULL, NULL, NULL, 0, NULL); |
| 149 | |
| 150 | /* heredity new pattern */ |
| 151 | schema = MODULE_CREATE_YANG("T3", "typedef my_type {type string {" |
| 152 | "pattern \"[A-Za-z]*\"{" |
| 153 | " description \"pattern 0 description\";" |
| 154 | " error-app-tag \"pattern 0 err-apt-tag\";" |
| 155 | " error-message \"pattern 0 error message\";}}}" |
| 156 | "leaf port {type my_type{pattern \"[A-Z]*\"{" |
| 157 | " description \"pattern 1 description\";" |
| 158 | " error-app-tag \"pattern 1 err-apt-tag\";" |
| 159 | " error-message \"pattern 1 error message\";" |
| 160 | "}}}"); |
| 161 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod); |
| 162 | assert_non_null(mod); |
| 163 | lysc_leaf = (void *) mod->compiled->data; |
| 164 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 165 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2); |
| 166 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 167 | CHECK_LYSC_PATTERN(pattern, "pattern 0 description", "pattern 0 err-apt-tag", |
| 168 | "pattern 0 error message", "[A-Za-z]*", 0, 0, NULL); |
| 169 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1]; |
| 170 | CHECK_LYSC_PATTERN(pattern, "pattern 1 description", "pattern 1 err-apt-tag", |
| 171 | "pattern 1 error message", "[A-Z]*", 0, 0, NULL); |
| 172 | lysp_leaf = (void *) mod->parsed->data; |
| 173 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 174 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0); |
| 175 | CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", "pattern 1 description", |
| 176 | "pattern 1 err-apt-tag", "pattern 1 error message", 0, NULL); |
| 177 | } |
| 178 | |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 179 | static void |
| 180 | test_schema_yin(void **state) |
| 181 | { |
| 182 | const char *schema; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 183 | struct lys_module *mod; |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 184 | struct lysc_node_leaf *lysc_leaf; |
| 185 | struct lysp_node_leaf *lysp_leaf; |
| 186 | struct lysc_pattern *pattern; |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 187 | |
| 188 | schema = MODULE_CREATE_YIN("T0", "<leaf name=\"port\"> <type name=\"string\">" |
| 189 | "<pattern value=\"[A-Za-z]*\">" |
| 190 | " <description><text>pattern description</text></description>" |
| 191 | " <error-app-tag value=\"pattern err-apt-tag\"/>" |
| 192 | " <error-message> <value>pattern error message</value></error-message>" |
| 193 | "</pattern></type></leaf>"); |
| 194 | UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod); |
| 195 | assert_non_null(mod); |
| 196 | lysc_leaf = (void *) mod->compiled->data; |
| 197 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 198 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1); |
| 199 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 200 | CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag", |
| 201 | "pattern error message", "[A-Za-z]*", 0, 0, NULL); |
| 202 | lysp_leaf = (void *) mod->parsed->data; |
| 203 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 204 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "string", 0, 1, 1, 0, 0, 0); |
| 205 | CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Za-z]*", "pattern description", |
| 206 | "pattern err-apt-tag", "pattern error message", 0, NULL); |
| 207 | |
| 208 | /* heredity */ |
| 209 | schema = MODULE_CREATE_YIN("T1", "<typedef name=\"my_type\"> <type name=\"string\">" |
| 210 | "<pattern value=\"[A-Za-z]*\">" |
| 211 | " <description><text>pattern description</text></description>" |
| 212 | " <error-app-tag value=\"pattern err-apt-tag\"/>" |
| 213 | " <error-message><value>pattern error message</value></error-message>" |
| 214 | "</pattern></type></typedef>" |
| 215 | "<leaf name=\"port\"><type name=\"my_type\"/></leaf>"); |
| 216 | UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod); |
| 217 | assert_non_null(mod); |
| 218 | lysc_leaf = (void *) mod->compiled->data; |
| 219 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 220 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1); |
| 221 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 222 | CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag", |
| 223 | "pattern error message", "[A-Za-z]*", 0, 0, NULL); |
| 224 | lysp_leaf = (void *) mod->parsed->data; |
| 225 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 226 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x0, 0, 0, "my_type", 0, 0, 1, 0, 0, 0); |
| 227 | |
| 228 | /* heredity new pattern */ |
| 229 | schema = MODULE_CREATE_YIN("T2", "<typedef name=\"my_type\"> <type name=\"string\">" |
| 230 | "<pattern value=\"[A-Za-z]*\">" |
| 231 | " <description><text>pattern description</text></description>" |
| 232 | " <error-app-tag value=\"pattern err-apt-tag\"/>" |
| 233 | " <error-message><value>pattern error message</value></error-message>" |
| 234 | "</pattern></type></typedef>" |
| 235 | "<leaf name=\"port\"> <type name=\"my_type\"><pattern value=\"[A-Z]*\"/>" |
| 236 | "</type></leaf>"); |
| 237 | UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod); |
| 238 | assert_non_null(mod); |
| 239 | lysc_leaf = (void *) mod->compiled->data; |
| 240 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 241 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2); |
| 242 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 243 | CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag", |
| 244 | "pattern error message", "[A-Za-z]*", 0, 0, NULL); |
| 245 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1]; |
| 246 | CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[A-Z]*", 0, 0, NULL); |
| 247 | lysp_leaf = (void *) mod->parsed->data; |
| 248 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 249 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0); |
| 250 | CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", NULL, NULL, NULL, 0, NULL); |
| 251 | |
| 252 | /* heredity new pattern */ |
| 253 | schema = MODULE_CREATE_YIN("T3", "<typedef name=\"my_type\"> <type name=\"string\">" |
| 254 | "<pattern value=\"[A-Za-z]*\">" |
| 255 | " <description> <text>pattern 0 description</text></description>" |
| 256 | " <error-app-tag value=\"pattern 0 err-apt-tag\"/>" |
| 257 | " <error-message> <value>pattern 0 error message</value></error-message>" |
| 258 | "</pattern></type></typedef>" |
| 259 | "<leaf name=\"port\"> <type name=\"my_type\">" |
| 260 | "<pattern value=\"[A-Z]*\">" |
| 261 | " <description><text>pattern 1 description</text></description>" |
| 262 | " <error-app-tag value=\"pattern 1 err-apt-tag\"/>" |
| 263 | " <error-message><value>pattern 1 error message</value></error-message>" |
| 264 | "</pattern></type></leaf>"); |
| 265 | |
| 266 | UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod); |
| 267 | assert_non_null(mod); |
| 268 | lysc_leaf = (void *) mod->compiled->data; |
| 269 | CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL); |
| 270 | CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2); |
| 271 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0]; |
| 272 | CHECK_LYSC_PATTERN(pattern, "pattern 0 description", "pattern 0 err-apt-tag", |
| 273 | "pattern 0 error message", "[A-Za-z]*", 0, 0, NULL); |
| 274 | pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1]; |
| 275 | CHECK_LYSC_PATTERN(pattern, "pattern 1 description", "pattern 1 err-apt-tag", |
| 276 | "pattern 1 error message", "[A-Z]*", 0, 0, NULL); |
| 277 | lysp_leaf = (void *) mod->parsed->data; |
| 278 | CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL); |
| 279 | CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0); |
| 280 | CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", "pattern 1 description", |
| 281 | "pattern 1 err-apt-tag", "pattern 1 error message", 0, NULL); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | test_schema_print(void **state) |
| 286 | { |
| 287 | const char *schema_yang, *schema_yin; |
| 288 | char *printed; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 289 | struct lys_module *mod; |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 290 | |
| 291 | /* test print yang to yin */ |
| 292 | schema_yang = MODULE_CREATE_YANG("PRINT0", "leaf port {type string {" |
| 293 | "pattern \"[A-Z]*\"{" |
| 294 | "description \"desc < \";" |
| 295 | "error-app-tag \"err-apt-tag <\";" |
| 296 | "error-message \"error message <\";}}}"); |
| 297 | |
| 298 | schema_yin = MODULE_CREATE_YIN("PRINT0", |
| 299 | " <leaf name=\"port\">\n" |
| 300 | " <type name=\"string\">\n" |
| 301 | " <pattern value=\"[A-Z]*\">\n" |
| 302 | " <error-message>\n" |
| 303 | " <value>error message <</value>\n" |
| 304 | " </error-message>\n" |
| 305 | " <error-app-tag value=\"err-apt-tag <\"/>\n" |
| 306 | " <description>\n" |
| 307 | " <text>desc < </text>\n" |
| 308 | " </description>\n" |
| 309 | " </pattern>\n" |
| 310 | " </type>\n" |
| 311 | " </leaf>\n"); |
| 312 | |
| 313 | UTEST_ADD_MODULE(schema_yang, LYS_IN_YANG, NULL, &mod); |
| 314 | assert_non_null(mod); |
| 315 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0)); |
| 316 | assert_string_equal(printed, schema_yin); |
| 317 | free(printed); |
| 318 | |
| 319 | /* test print yin to yang */ |
| 320 | schema_yang = MODULE_CREATE_YANG("PRINT1", |
Radek Krejci | aa14a0c | 2020-09-04 11:16:47 +0200 | [diff] [blame] | 321 | "\n" |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 322 | " leaf port {\n" |
| 323 | " type string {\n" |
| 324 | " pattern \"[A-Z]*\" {\n" |
| 325 | " error-message\n" |
| 326 | " \"error message <\";\n" |
| 327 | " error-app-tag \"err-apt-tag <\";\n" |
| 328 | " description\n" |
| 329 | " \"desc < \";\n" |
| 330 | " }\n" |
| 331 | " }\n" |
| 332 | " }\n"); |
| 333 | |
| 334 | schema_yin = MODULE_CREATE_YIN("PRINT1", |
| 335 | " <leaf name=\"port\">\n" |
| 336 | " <type name=\"string\">\n" |
| 337 | " <pattern value=\"[A-Z]*\">\n" |
| 338 | " <error-message>\n" |
| 339 | " <value>error message <</value>\n" |
| 340 | " </error-message>\n" |
| 341 | " <error-app-tag value=\"err-apt-tag <\"/>\n" |
| 342 | " <description>\n" |
| 343 | " <text>desc < </text>\n" |
| 344 | " </description>\n" |
| 345 | " </pattern>\n" |
| 346 | " </type>\n" |
| 347 | " </leaf>\n"); |
| 348 | |
| 349 | UTEST_ADD_MODULE(schema_yin, LYS_IN_YIN, NULL, &mod); |
| 350 | assert_non_null(mod); |
| 351 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0)); |
| 352 | assert_string_equal(printed, schema_yang); |
| 353 | free(printed); |
| 354 | } |
| 355 | |
| 356 | static void |
| 357 | test_data_xml(void **state) |
| 358 | { |
| 359 | const char *schema; |
| 360 | |
| 361 | /* xml test */ |
| 362 | schema = MODULE_CREATE_YANG("TPATTERN_0", "typedef my_type {type string {" |
| 363 | "pattern \"[A-Za-z]*\"{" |
| 364 | " description \"pattern 0 description\";" |
| 365 | " error-app-tag \"pattern 0 err-apt-tag\";" |
| 366 | " error-message \"pattern 0 error message\";}}}" |
| 367 | "leaf port {type my_type{pattern \"[A-Z]*\"{" |
| 368 | " description \"pattern 1 description\";" |
| 369 | " error-app-tag \"pattern 1 err-apt-tag\";" |
| 370 | " error-message \"pattern 1 error message\";" |
| 371 | "}}}"); |
| 372 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL); |
| 373 | |
| 374 | /* test success */ |
| 375 | TEST_SUCCESS_XML("TPATTERN_0", "AHOJ", STRING, "AHOJ"); |
| 376 | /* test print error */ |
| 377 | TEST_ERROR_XML("TPATTERN_0", "T128"); |
| 378 | CHECK_LOG_CTX("pattern 0 error message", |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 379 | "Schema location \"/TPATTERN_0:port\", line number 1."); |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 380 | TEST_ERROR_XML("TPATTERN_0", "ahoj"); |
| 381 | CHECK_LOG_CTX("pattern 1 error message", |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 382 | "Schema location \"/TPATTERN_0:port\", line number 1."); |
Radek Iša | 59bf327 | 2021-03-10 11:22:52 +0100 | [diff] [blame] | 383 | |
| 384 | } |
| 385 | |
| 386 | int |
| 387 | main(void) |
| 388 | { |
| 389 | const struct CMUnitTest tests[] = { |
| 390 | UTEST(test_schema_yang), |
| 391 | UTEST(test_schema_yin), |
| 392 | UTEST(test_schema_print), |
| 393 | UTEST(test_data_xml), |
| 394 | }; |
| 395 | |
| 396 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 397 | } |