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" |
| 18 | #include "plugins_exts_metadata.h" |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 19 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 20 | static void |
| 21 | test_yang(void **state) |
| 22 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 23 | const struct lys_module *mod; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 24 | struct lysc_ext_instance *e; |
| 25 | struct lyext_metadata *ant; |
| 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 | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 43 | assert_non_null(ant = (struct lyext_metadata *)e->data); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 44 | assert_string_equal("meters", ant->units); |
| 45 | |
| 46 | /* invalid */ |
| 47 | /* missing mandatory type substatement */ |
| 48 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 49 | "import ietf-yang-metadata {prefix md;}" |
| 50 | "md:annotation aa;}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 51 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 52 | 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] | 53 | |
| 54 | /* not allowed substatement */ |
| 55 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 56 | "import ietf-yang-metadata {prefix md;}" |
| 57 | "md:annotation aa {default x;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 58 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 59 | 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] | 60 | |
| 61 | /* invalid cardinality of units substatement */ |
| 62 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 63 | "import ietf-yang-metadata {prefix md;}" |
| 64 | "md:annotation aa {type string; units x; units y;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 65 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 66 | CHECK_LOG_CTX("Duplicate keyword \"units\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 67 | |
| 68 | /* invalid cardinality of status substatement */ |
| 69 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 70 | "import ietf-yang-metadata {prefix md;}" |
| 71 | "md:annotation aa {type string; status current; status obsolete;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 72 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 73 | CHECK_LOG_CTX("Duplicate keyword \"status\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 74 | |
| 75 | /* invalid cardinality of status substatement */ |
| 76 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 77 | "import ietf-yang-metadata {prefix md;}" |
| 78 | "md:annotation aa {type string; type uint8;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 79 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 80 | CHECK_LOG_CTX("Duplicate keyword \"type\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 81 | |
| 82 | /* duplication of the same annotation */ |
| 83 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 84 | "import ietf-yang-metadata {prefix md;}" |
| 85 | "md:annotation aa {type string;} md:annotation aa {type uint8;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 86 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
| 87 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 88 | "Extension md:annotation is instantiated multiple times.)", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 91 | static void |
| 92 | test_yin(void **state) |
| 93 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 94 | const struct lys_module *mod; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 95 | struct lysc_ext_instance *e; |
| 96 | struct lyext_metadata *ant; |
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]; |
| 116 | assert_non_null(ant = (struct lyext_metadata*)e->data); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 117 | assert_string_equal("meters", ant->units); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 118 | |
| 119 | /* invalid */ |
| 120 | /* missing mandatory type substatement */ |
| 121 | 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" |
| 122 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 123 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 124 | "<md:annotation name=\"aa\"/>\n" |
| 125 | "</module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 126 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 127 | CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"md:annotation\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 128 | |
| 129 | /* not allowed substatement */ |
| 130 | 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" |
| 131 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 132 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 133 | "<md:annotation name=\"aa\">\n" |
| 134 | " <default value=\"x\"/>\n" |
| 135 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 136 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 137 | CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"md:annotation\" extension instance.", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 138 | |
| 139 | /* invalid cardinality of units substatement */ |
| 140 | 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" |
| 141 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 142 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 143 | "<md:annotation name=\"aa\">\n" |
| 144 | " <type name=\"string\"/>\n" |
| 145 | " <units name=\"x\"/>\n" |
| 146 | " <units name=\"y\"/>\n" |
| 147 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 148 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 149 | CHECK_LOG_CTX("Duplicate keyword \"units\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 150 | |
| 151 | /* invalid cardinality of status substatement */ |
| 152 | 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" |
| 153 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 154 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 155 | "<md:annotation name=\"aa\">\n" |
| 156 | " <type name=\"string\"/>\n" |
| 157 | " <status value=\"current\"/>\n" |
| 158 | " <status value=\"obsolete\"/>\n" |
| 159 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 160 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 161 | CHECK_LOG_CTX("Duplicate keyword \"status\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 162 | |
| 163 | /* invalid cardinality of status substatement */ |
| 164 | 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" |
| 165 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 166 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 167 | "<md:annotation name=\"aa\">\n" |
| 168 | " <type name=\"string\"/>\n" |
| 169 | " <type name=\"uint8\"/>\n" |
| 170 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 171 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 172 | CHECK_LOG_CTX("Duplicate keyword \"type\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 173 | |
| 174 | /* duplication of the same annotation */ |
| 175 | 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" |
| 176 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 177 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 178 | "<md:annotation name=\"aa\">\n" |
| 179 | " <type name=\"string\"/>\n" |
| 180 | "</md:annotation><md:annotation name=\"aa\">\n" |
| 181 | " <type name=\"uint8\"/>\n" |
| 182 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 183 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 184 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 185 | "Extension md:annotation is instantiated multiple times.)", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 188 | int |
| 189 | main(void) |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 190 | { |
| 191 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 192 | UTEST(test_yang), |
| 193 | UTEST(test_yin), |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 197 | } |