Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_metadata.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for Metadata extension (annotation) support |
| 5 | * |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 6 | * Copyright (c) 2019-2020 CESNET, z.s.p.o. |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 15 | #include "utests.h" |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 17 | #include "libyang.h" |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 18 | #include "plugins_exts.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 19 | #include "plugins_exts_metadata.h" |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 20 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 21 | static void |
| 22 | test_yang(void **state) |
| 23 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 24 | const struct lys_module *mod; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 25 | struct lysc_ext_instance *e; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 26 | |
| 27 | const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:metadata:a; prefix a;" |
| 28 | "import ietf-yang-metadata {prefix md;}" |
| 29 | "feature f;" |
| 30 | "md:annotation x {" |
| 31 | " description \"test\";" |
| 32 | " if-feature f;" |
| 33 | " reference \"test\";" |
| 34 | " status \"current\";" |
| 35 | " type uint8;" |
| 36 | " units meters;" |
| 37 | "}}"; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 38 | const char *feats[] = {"f", NULL}; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 39 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 40 | UTEST_ADD_MODULE(data, LYS_IN_YANG, feats, &mod); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 41 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 42 | e = &mod->compiled->exts[0]; |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 43 | assert_non_null(e->data); |
| 44 | assert_non_null(e->substmts); |
| 45 | assert_string_equal("meters", *(const char **)e->substmts[ANNOTATION_SUBSTMT_UNITS].storage); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 46 | |
| 47 | /* invalid */ |
| 48 | /* missing mandatory type substatement */ |
| 49 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 50 | "import ietf-yang-metadata {prefix md;}" |
| 51 | "md:annotation aa;}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 52 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 53 | CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 54 | |
| 55 | /* not allowed substatement */ |
| 56 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 57 | "import ietf-yang-metadata {prefix md;}" |
| 58 | "md:annotation aa {default x;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 59 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 60 | CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance.", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 61 | |
| 62 | /* invalid cardinality of units substatement */ |
| 63 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 64 | "import ietf-yang-metadata {prefix md;}" |
| 65 | "md:annotation aa {type string; units x; units y;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 66 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 67 | CHECK_LOG_CTX("Duplicate keyword \"units\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 68 | |
| 69 | /* invalid cardinality of status substatement */ |
| 70 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 71 | "import ietf-yang-metadata {prefix md;}" |
| 72 | "md:annotation aa {type string; status current; status obsolete;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 73 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 74 | CHECK_LOG_CTX("Duplicate keyword \"status\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 75 | |
| 76 | /* invalid cardinality of status substatement */ |
| 77 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 78 | "import ietf-yang-metadata {prefix md;}" |
| 79 | "md:annotation aa {type string; type uint8;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 80 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 81 | CHECK_LOG_CTX("Duplicate keyword \"type\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 82 | |
| 83 | /* duplication of the same annotation */ |
| 84 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 85 | "import ietf-yang-metadata {prefix md;}" |
| 86 | "md:annotation aa {type string;} md:annotation aa {type uint8;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 87 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 88 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - metadata, version 1\": " |
Radek Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame] | 89 | "Extension md:annotation is instantiated multiple times.", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 92 | static void |
| 93 | test_yin(void **state) |
| 94 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 95 | const struct lys_module *mod; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 96 | struct lysc_ext_instance *e; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 97 | const char *data; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 98 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 99 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"a\">\n" |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 100 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n" |
| 101 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 102 | "<feature name=\"f\"/>\n" |
| 103 | "<md:annotation name=\"x\">\n" |
| 104 | " <description><text>test</text></description>\n" |
| 105 | " <reference><text>test</text></reference>\n" |
| 106 | " <if-feature name=\"f\"/>\n" |
| 107 | " <status value=\"current\"/>\n" |
| 108 | " <type name=\"uint8\"/>\n" |
| 109 | " <units name=\"meters\"/>\n" |
| 110 | "</md:annotation></module>"; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 111 | const char *feats[] = {"f", NULL}; |
| 112 | |
| 113 | UTEST_ADD_MODULE(data, LYS_IN_YIN, feats, &mod); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 114 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 115 | e = &mod->compiled->exts[0]; |
Radek Krejci | 1b2eef8 | 2021-02-17 11:17:27 +0100 | [diff] [blame] | 116 | assert_non_null(e->data); |
| 117 | assert_non_null(e->substmts); |
| 118 | assert_string_equal("meters", *(const char **)e->substmts[ANNOTATION_SUBSTMT_UNITS].storage); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 119 | |
| 120 | /* invalid */ |
| 121 | /* missing mandatory type substatement */ |
| 122 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 123 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 124 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 125 | "<md:annotation name=\"aa\"/>\n" |
| 126 | "</module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 127 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 128 | CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 129 | |
| 130 | /* not allowed substatement */ |
| 131 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 132 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 133 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 134 | "<md:annotation name=\"aa\">\n" |
| 135 | " <default value=\"x\"/>\n" |
| 136 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 137 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 85ac831 | 2021-03-03 20:21:33 +0100 | [diff] [blame] | 138 | CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance.", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 139 | |
| 140 | /* invalid cardinality of units substatement */ |
| 141 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 142 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 143 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 144 | "<md:annotation name=\"aa\">\n" |
| 145 | " <type name=\"string\"/>\n" |
| 146 | " <units name=\"x\"/>\n" |
| 147 | " <units name=\"y\"/>\n" |
| 148 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 149 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 150 | CHECK_LOG_CTX("Duplicate keyword \"units\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 151 | |
| 152 | /* invalid cardinality of status substatement */ |
| 153 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 154 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 155 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 156 | "<md:annotation name=\"aa\">\n" |
| 157 | " <type name=\"string\"/>\n" |
| 158 | " <status value=\"current\"/>\n" |
| 159 | " <status value=\"obsolete\"/>\n" |
| 160 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 161 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 162 | CHECK_LOG_CTX("Duplicate keyword \"status\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 163 | |
| 164 | /* invalid cardinality of status substatement */ |
| 165 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 166 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 167 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 168 | "<md:annotation name=\"aa\">\n" |
| 169 | " <type name=\"string\"/>\n" |
| 170 | " <type name=\"uint8\"/>\n" |
| 171 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 172 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 173 | CHECK_LOG_CTX("Duplicate keyword \"type\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 174 | |
| 175 | /* duplication of the same annotation */ |
| 176 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 177 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 178 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 179 | "<md:annotation name=\"aa\">\n" |
| 180 | " <type name=\"string\"/>\n" |
| 181 | "</md:annotation><md:annotation name=\"aa\">\n" |
| 182 | " <type name=\"uint8\"/>\n" |
| 183 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 184 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 185 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - metadata, version 1\": " |
Radek Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame] | 186 | "Extension md:annotation is instantiated multiple times.", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 189 | int |
| 190 | main(void) |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 191 | { |
| 192 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 193 | UTEST(test_yang), |
| 194 | UTEST(test_yin), |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 198 | } |