Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_structure.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief unit tests for structure extensions support |
| 5 | * |
| 6 | * Copyright (c) 2022 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 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
| 16 | |
| 17 | #include "libyang.h" |
| 18 | |
| 19 | static void |
| 20 | test_schema(void **state) |
| 21 | { |
| 22 | struct lys_module *mod; |
| 23 | struct lysc_ext_instance *e; |
| 24 | char *printed = NULL; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 25 | const char *data, *info; |
| 26 | |
| 27 | /* valid data */ |
| 28 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix a;" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 29 | "import ietf-yang-structure-ext {prefix sx;}" |
| 30 | "sx:structure struct {" |
| 31 | " must \"/n2/l\";" |
| 32 | " status deprecated;" |
| 33 | " description desc;" |
| 34 | " reference no-ref;" |
| 35 | " typedef my-type {type string;}" |
| 36 | " grouping my-grp {leaf gl {type my-type;}}" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 37 | " container n1 {leaf l {config false; type uint32;}}" |
| 38 | " list n2 {leaf l {type leafref {path /n1/l;}}}" |
| 39 | " uses my-grp;" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 40 | "}}"; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 41 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 42 | UTEST_ADD_MODULE(data, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 43 | assert_non_null(e = mod->compiled->exts); |
| 44 | assert_int_equal(LY_ARRAY_COUNT(mod->compiled->exts), 1); |
| 45 | |
| 46 | /* valid augment data */ |
| 47 | data = "module b {yang-version 1.1; namespace urn:tests:extensions:structure:b; prefix b;" |
| 48 | "import ietf-yang-structure-ext {prefix sx;}" |
| 49 | "import a {prefix a;}" |
| 50 | "sx:augment-structure \"/a:struct/a:n1\" {" |
| 51 | " status obsolete;" |
| 52 | " reference none;" |
| 53 | " leaf aug-leaf {type string;}" |
| 54 | "}}"; |
| 55 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 56 | UTEST_ADD_MODULE(data, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 57 | assert_non_null(e = mod->compiled->exts); |
| 58 | assert_int_equal(LY_ARRAY_COUNT(mod->compiled->exts), 1); |
| 59 | |
| 60 | /* yang compiled print */ |
| 61 | info = "module a {\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 62 | " namespace \"urn:tests:extensions:structure:a\";\n" |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 63 | " prefix a;\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 64 | "\n" |
| 65 | " ietf-yang-structure-ext:structure \"struct\" {\n" |
| 66 | " must \"/n2/l\";\n" |
| 67 | " status deprecated;\n" |
| 68 | " description\n" |
| 69 | " \"desc\";\n" |
| 70 | " reference\n" |
| 71 | " \"no-ref\";\n" |
| 72 | " container n1 {\n" |
| 73 | " status deprecated;\n" |
| 74 | " leaf l {\n" |
| 75 | " type uint32;\n" |
| 76 | " status deprecated;\n" |
| 77 | " }\n" |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 78 | " leaf aug-leaf {\n" |
| 79 | " type string;\n" |
| 80 | " status obsolete;\n" |
| 81 | " }\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 82 | " }\n" |
| 83 | " list n2 {\n" |
| 84 | " min-elements 0;\n" |
| 85 | " max-elements 4294967295;\n" |
| 86 | " ordered-by user;\n" |
| 87 | " status deprecated;\n" |
| 88 | " leaf l {\n" |
| 89 | " type leafref {\n" |
| 90 | " path \"/n1/l\";\n" |
| 91 | " require-instance true;\n" |
| 92 | " type uint32;\n" |
| 93 | " }\n" |
| 94 | " status deprecated;\n" |
| 95 | " }\n" |
| 96 | " }\n" |
| 97 | " leaf gl {\n" |
| 98 | " type string;\n" |
| 99 | " status deprecated;\n" |
| 100 | " }\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 101 | " }\n" |
| 102 | "}\n"; |
| 103 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 104 | assert_non_null(mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a")); |
| 105 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0)); |
| 106 | assert_string_equal(printed, info); |
| 107 | free(printed); |
| 108 | |
| 109 | info = "module b {\n" |
| 110 | " namespace \"urn:tests:extensions:structure:b\";\n" |
| 111 | " prefix b;\n" |
| 112 | "\n" |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 113 | " ietf-yang-structure-ext:augment-structure \"/a:struct/a:n1\";\n" |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 114 | "}\n"; |
| 115 | |
| 116 | assert_non_null(mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b")); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 117 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0)); |
| 118 | assert_string_equal(printed, info); |
| 119 | free(printed); |
| 120 | |
| 121 | /* no substatements */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 122 | data = "module c {yang-version 1.1; namespace urn:tests:extensions:structure:c; prefix c;" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 123 | "import ietf-yang-structure-ext {prefix sx;}" |
| 124 | "sx:structure struct;}"; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame] | 125 | info = "module c {\n" |
| 126 | " namespace \"urn:tests:extensions:structure:c\";\n" |
| 127 | " prefix c;\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 128 | "\n" |
| 129 | " ietf-yang-structure-ext:structure \"struct\";\n" |
| 130 | "}\n"; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 131 | |
| 132 | UTEST_ADD_MODULE(data, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 133 | assert_non_null(e = mod->compiled->exts); |
| 134 | assert_int_equal(LY_ARRAY_COUNT(mod->compiled->exts), 1); |
| 135 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0)); |
| 136 | assert_string_equal(printed, info); |
| 137 | free(printed); |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | test_schema_invalid(void **state) |
| 142 | { |
| 143 | const char *data; |
| 144 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 145 | /* structure */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 146 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 147 | "import ietf-yang-structure-ext {prefix sx;}" |
| 148 | "sx:structure struct {import yang;}}"; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 149 | UTEST_INVALID_MODULE(data, LYS_IN_YANG, NULL, LY_EVALID); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 150 | CHECK_LOG_CTX("Invalid keyword \"import\" as a child of \"sx:structure struct\" extension instance.", |
| 151 | "/a:{extension='sx:structure'}/struct"); |
| 152 | |
| 153 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 154 | "import ietf-yang-structure-ext {prefix sx;}" |
| 155 | "container b { sx:structure struct { container x { leaf x {type string;}}}}}"; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 156 | UTEST_INVALID_MODULE(data, LYS_IN_YANG, NULL, LY_EVALID); |
| 157 | CHECK_LOG_CTX("Ext plugin \"ly2 structure v1\": " |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 158 | "Extension sx:structure must not be used as a non top-level statement in \"container\" statement.", |
| 159 | "/a:b/{extension='sx:structure'}/struct"); |
| 160 | |
| 161 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 162 | "import ietf-yang-structure-ext {prefix sx;}" |
| 163 | "sx:structure { container x { leaf x {type string;}}}}"; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 164 | UTEST_INVALID_MODULE(data, LYS_IN_YANG, NULL, LY_EVALID); |
| 165 | CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL, |
| 166 | "Extension instance \"sx:structure\" missing argument element \"name\".", NULL); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 167 | |
| 168 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 169 | "import ietf-yang-structure-ext {prefix sx;}" |
| 170 | "sx:structure struct { container x { leaf x {type string;}}}" |
| 171 | "sx:structure struct { container y { leaf y {type string;}}}}"; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 172 | UTEST_INVALID_MODULE(data, LYS_IN_YANG, NULL, LY_EVALID); |
| 173 | CHECK_LOG_CTX("Ext plugin \"ly2 structure v1\": Extension sx:structure is instantiated multiple times.", |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 174 | "/a:{extension='sx:structure'}/struct"); |
| 175 | |
| 176 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 177 | "import ietf-yang-structure-ext {prefix sx;}" |
| 178 | "sx:structure struct { container x { leaf x {type string;}}}" |
| 179 | "choice struct { container y { leaf y {type string;}}}}"; |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 180 | UTEST_INVALID_MODULE(data, LYS_IN_YANG, NULL, LY_EVALID); |
| 181 | CHECK_LOG_CTX("Ext plugin \"ly2 structure v1\": Extension sx:structure collides with a choice with the same identifier.", |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 182 | "/a:{extension='sx:structure'}/struct"); |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 183 | |
| 184 | /* augment-structure */ |
| 185 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix a;" |
| 186 | "import ietf-yang-structure-ext {prefix sx;}" |
| 187 | "sx:structure struct {" |
| 188 | " container n1 {leaf l {config false; type uint32;}}" |
| 189 | " list n2 {leaf l {type string;}}" |
| 190 | "}" |
| 191 | "container n1 {leaf l2 {type uint8;}}}"; |
| 192 | UTEST_ADD_MODULE(data, LYS_IN_YANG, NULL, NULL); |
| 193 | |
| 194 | data = "module b {yang-version 1.1; namespace urn:tests:extensions:structure:b; prefix b;" |
| 195 | "import ietf-yang-structure-ext {prefix sx;}" |
| 196 | "import a {prefix a;}" |
| 197 | "sx:augment-structure \"/a:n1\" {" |
| 198 | " leaf aug-leaf {type string;}" |
| 199 | "}}"; |
| 200 | UTEST_INVALID_MODULE(data, LYS_IN_YANG, NULL, LY_ENOTFOUND); |
| 201 | CHECK_LOG_CTX("Augment extension target node \"/a:n1\" from module \"b\" was not found.", |
| 202 | "/b:{extension='sx:augment-structure'}/{augment='/a:n1'}"); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void |
| 206 | test_parse(void **state) |
| 207 | { |
| 208 | struct lys_module *mod; |
| 209 | struct lysc_ext_instance *e; |
| 210 | struct lyd_node *tree = NULL; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 211 | const char *yang; |
| 212 | const char *xml = "<x xmlns=\"urn:tests:extensions:structure:a\">" |
| 213 | "<x>test</x>" |
| 214 | "<x2 xmlns=\"urn:tests:extensions:structure:b\">25</x2>" |
| 215 | "</x>"; |
| 216 | const char *json = "{\"a:x\":{\"x\":\"test\",\"b:x2\":25}}"; |
| 217 | |
| 218 | yang = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix a;" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 219 | "import ietf-yang-structure-ext {prefix sx;}" |
| 220 | "sx:structure struct { container x { leaf x { type string;}}}}"; |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 221 | UTEST_ADD_MODULE(yang, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 222 | |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 223 | yang = "module b {yang-version 1.1; namespace urn:tests:extensions:structure:b; prefix b;" |
| 224 | "import ietf-yang-structure-ext {prefix sx;}" |
| 225 | "import a {prefix a;}" |
| 226 | "sx:augment-structure \"/a:struct/a:x\" {" |
| 227 | " leaf x2 {type uint32;}" |
| 228 | "}}"; |
| 229 | UTEST_ADD_MODULE(yang, LYS_IN_YANG, NULL, NULL); |
| 230 | |
| 231 | /* get extension after recompilation */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 232 | assert_non_null(e = mod->compiled->exts); |
| 233 | |
| 234 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(xml, &UTEST_IN)); |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 235 | assert_int_equal(LY_SUCCESS, lyd_parse_ext_data(e, NULL, UTEST_IN, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 236 | CHECK_LYD_STRING_PARAM(tree, xml, LYD_XML, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS); |
| 237 | lyd_free_all(tree); |
| 238 | |
| 239 | ly_in_memory(UTEST_IN, json); |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 240 | assert_int_equal(LY_SUCCESS, lyd_parse_ext_data(e, NULL, UTEST_IN, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 241 | CHECK_LYD_STRING_PARAM(tree, json, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 242 | lyd_free_all(tree); |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | main(void) |
| 247 | { |
| 248 | const struct CMUnitTest tests[] = { |
| 249 | UTEST(test_schema), |
| 250 | UTEST(test_schema_invalid), |
| 251 | UTEST(test_parse), |
| 252 | }; |
| 253 | |
| 254 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 255 | } |