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;}}" |
| 37 | " grouping my-grp2 {leaf gl-obs {type uint16;}}" |
| 38 | " container n1 {leaf l {config false; type uint32;}}" |
| 39 | " list n2 {leaf l {type leafref {path /n1/l;}}}" |
| 40 | " uses my-grp;" |
| 41 | " uses my-grp2 {status obsolete;}" |
| 42 | "}}"; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame^] | 43 | |
| 44 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod)); |
| 45 | assert_non_null(e = mod->compiled->exts); |
| 46 | assert_int_equal(LY_ARRAY_COUNT(mod->compiled->exts), 1); |
| 47 | |
| 48 | /* valid augment data */ |
| 49 | data = "module b {yang-version 1.1; namespace urn:tests:extensions:structure:b; prefix b;" |
| 50 | "import ietf-yang-structure-ext {prefix sx;}" |
| 51 | "import a {prefix a;}" |
| 52 | "sx:augment-structure \"/a:struct/a:n1\" {" |
| 53 | " status obsolete;" |
| 54 | " reference none;" |
| 55 | " leaf aug-leaf {type string;}" |
| 56 | "}}"; |
| 57 | |
| 58 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod)); |
| 59 | assert_non_null(e = mod->compiled->exts); |
| 60 | assert_int_equal(LY_ARRAY_COUNT(mod->compiled->exts), 1); |
| 61 | |
| 62 | /* yang compiled print */ |
| 63 | info = "module a {\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 64 | " namespace \"urn:tests:extensions:structure:a\";\n" |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame^] | 65 | " prefix a;\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 66 | "\n" |
| 67 | " ietf-yang-structure-ext:structure \"struct\" {\n" |
| 68 | " must \"/n2/l\";\n" |
| 69 | " status deprecated;\n" |
| 70 | " description\n" |
| 71 | " \"desc\";\n" |
| 72 | " reference\n" |
| 73 | " \"no-ref\";\n" |
| 74 | " container n1 {\n" |
| 75 | " status deprecated;\n" |
| 76 | " leaf l {\n" |
| 77 | " type uint32;\n" |
| 78 | " status deprecated;\n" |
| 79 | " }\n" |
| 80 | " }\n" |
| 81 | " list n2 {\n" |
| 82 | " min-elements 0;\n" |
| 83 | " max-elements 4294967295;\n" |
| 84 | " ordered-by user;\n" |
| 85 | " status deprecated;\n" |
| 86 | " leaf l {\n" |
| 87 | " type leafref {\n" |
| 88 | " path \"/n1/l\";\n" |
| 89 | " require-instance true;\n" |
| 90 | " type uint32;\n" |
| 91 | " }\n" |
| 92 | " status deprecated;\n" |
| 93 | " }\n" |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame^] | 94 | " leaf aug-leaf {\n" |
| 95 | " type string;\n" |
| 96 | " status obsolete;\n" |
| 97 | " }\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 98 | " }\n" |
| 99 | " leaf gl {\n" |
| 100 | " type string;\n" |
| 101 | " status deprecated;\n" |
| 102 | " }\n" |
| 103 | " leaf gl-obs {\n" |
| 104 | " type uint16;\n" |
| 105 | " status obsolete;\n" |
| 106 | " }\n" |
| 107 | " }\n" |
| 108 | "}\n"; |
| 109 | |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame^] | 110 | assert_non_null(mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a")); |
| 111 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0)); |
| 112 | assert_string_equal(printed, info); |
| 113 | free(printed); |
| 114 | |
| 115 | info = "module b {\n" |
| 116 | " namespace \"urn:tests:extensions:structure:b\";\n" |
| 117 | " prefix b;\n" |
| 118 | "\n" |
| 119 | " ietf-yang-structure-ext:augment-structure \"/a:struct/a:n1\" {\n" |
| 120 | " status obsolete;\n" |
| 121 | " reference\n" |
| 122 | " \"none\";\n" |
| 123 | " }\n" |
| 124 | "}\n"; |
| 125 | |
| 126 | assert_non_null(mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b")); |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 127 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0)); |
| 128 | assert_string_equal(printed, info); |
| 129 | free(printed); |
| 130 | |
| 131 | /* no substatements */ |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame^] | 132 | 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] | 133 | "import ietf-yang-structure-ext {prefix sx;}" |
| 134 | "sx:structure struct;}"; |
Michal Vasko | 0b50f6b | 2022-10-05 15:07:55 +0200 | [diff] [blame^] | 135 | info = "module c {\n" |
| 136 | " namespace \"urn:tests:extensions:structure:c\";\n" |
| 137 | " prefix c;\n" |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 138 | "\n" |
| 139 | " ietf-yang-structure-ext:structure \"struct\";\n" |
| 140 | "}\n"; |
| 141 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod)); |
| 142 | assert_non_null(e = mod->compiled->exts); |
| 143 | assert_int_equal(LY_ARRAY_COUNT(mod->compiled->exts), 1); |
| 144 | assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0)); |
| 145 | assert_string_equal(printed, info); |
| 146 | free(printed); |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | test_schema_invalid(void **state) |
| 151 | { |
| 152 | const char *data; |
| 153 | |
| 154 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 155 | "import ietf-yang-structure-ext {prefix sx;}" |
| 156 | "sx:structure struct {import yang;}}"; |
| 157 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 158 | CHECK_LOG_CTX("Invalid keyword \"import\" as a child of \"sx:structure struct\" extension instance.", |
| 159 | "/a:{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 | "container b { sx:structure struct { container x { leaf x {type string;}}}}}"; |
| 164 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 165 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - structure, version 1\": " |
| 166 | "Extension sx:structure must not be used as a non top-level statement in \"container\" statement.", |
| 167 | "/a:b/{extension='sx:structure'}/struct"); |
| 168 | |
| 169 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 170 | "import ietf-yang-structure-ext {prefix sx;}" |
| 171 | "sx:structure { container x { leaf x {type string;}}}}"; |
| 172 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 173 | CHECK_LOG_CTX("Extension instance \"sx:structure\" misses argument element \"name\".", |
| 174 | "/a:{extension='sx:structure'}"); |
| 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 | "sx:structure struct { container y { leaf y {type string;}}}}"; |
| 180 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 181 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - structure, version 1\": " |
| 182 | "Extension sx:structure is instantiated multiple times.", |
| 183 | "/a:{extension='sx:structure'}/struct"); |
| 184 | |
| 185 | data = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 186 | "import ietf-yang-structure-ext {prefix sx;}" |
| 187 | "sx:structure struct { container x { leaf x {type string;}}}" |
| 188 | "choice struct { container y { leaf y {type string;}}}}"; |
| 189 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 190 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - structure, version 1\": " |
| 191 | "Extension sx:structure collides with a choice with the same identifier.", |
| 192 | "/a:{extension='sx:structure'}/struct"); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | test_parse(void **state) |
| 197 | { |
| 198 | struct lys_module *mod; |
| 199 | struct lysc_ext_instance *e; |
| 200 | struct lyd_node *tree = NULL; |
| 201 | const char *schema = "module a {yang-version 1.1; namespace urn:tests:extensions:structure:a; prefix self;" |
| 202 | "import ietf-yang-structure-ext {prefix sx;}" |
| 203 | "sx:structure struct { container x { leaf x { type string;}}}}"; |
| 204 | const char *xml = "<x xmlns=\"urn:tests:extensions:structure:a\"><x>test</x></x>"; |
| 205 | const char *json = "{\"a:x\":{\"x\":\"test\"}}"; |
| 206 | |
| 207 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, &mod)); |
| 208 | assert_non_null(e = mod->compiled->exts); |
| 209 | |
| 210 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(xml, &UTEST_IN)); |
| 211 | assert_int_equal(LY_SUCCESS, lyd_parse_ext_data(e, NULL, UTEST_IN, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 212 | CHECK_LYD_STRING_PARAM(tree, xml, LYD_XML, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS); |
| 213 | lyd_free_all(tree); |
| 214 | |
| 215 | ly_in_memory(UTEST_IN, json); |
| 216 | assert_int_equal(LY_SUCCESS, lyd_parse_ext_data(e, NULL, UTEST_IN, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 217 | CHECK_LYD_STRING_PARAM(tree, json, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS); |
| 218 | |
| 219 | lyd_free_all(tree); |
| 220 | } |
| 221 | |
| 222 | int |
| 223 | main(void) |
| 224 | { |
| 225 | const struct CMUnitTest tests[] = { |
| 226 | UTEST(test_schema), |
| 227 | UTEST(test_schema_invalid), |
| 228 | UTEST(test_parse), |
| 229 | }; |
| 230 | |
| 231 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 232 | } |