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 | /* TODO yin-extension-prefix-compilation-bug |
| 100 | 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] | 101 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n" |
| 102 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 103 | "<feature name=\"f\"/>\n" |
| 104 | "<md:annotation name=\"x\">\n" |
| 105 | " <description><text>test</text></description>\n" |
| 106 | " <reference><text>test</text></reference>\n" |
| 107 | " <if-feature name=\"f\"/>\n" |
| 108 | " <status value=\"current\"/>\n" |
| 109 | " <type name=\"uint8\"/>\n" |
| 110 | " <units name=\"meters\"/>\n" |
| 111 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 112 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, &mod)); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 113 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 114 | e = &mod->compiled->exts[0]; |
| 115 | assert_non_null(ant = (struct lyext_metadata*)e->data); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 116 | assert_string_equal("meters", ant->units);*/ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 117 | |
| 118 | /* invalid */ |
| 119 | /* missing mandatory type substatement */ |
| 120 | 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" |
| 121 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 122 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 123 | "<md:annotation name=\"aa\"/>\n" |
| 124 | "</module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 125 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 126 | 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] | 127 | |
| 128 | /* not allowed substatement */ |
| 129 | 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" |
| 130 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 131 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 132 | "<md:annotation name=\"aa\">\n" |
| 133 | " <default value=\"x\"/>\n" |
| 134 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 135 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 136 | 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] | 137 | |
| 138 | /* invalid cardinality of units substatement */ |
| 139 | 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" |
| 140 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 141 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 142 | "<md:annotation name=\"aa\">\n" |
| 143 | " <type name=\"string\"/>\n" |
| 144 | " <units name=\"x\"/>\n" |
| 145 | " <units name=\"y\"/>\n" |
| 146 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 147 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 148 | CHECK_LOG_CTX("Duplicate keyword \"units\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 149 | |
| 150 | /* invalid cardinality of status substatement */ |
| 151 | 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" |
| 152 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 153 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 154 | "<md:annotation name=\"aa\">\n" |
| 155 | " <type name=\"string\"/>\n" |
| 156 | " <status value=\"current\"/>\n" |
| 157 | " <status value=\"obsolete\"/>\n" |
| 158 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 159 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 160 | CHECK_LOG_CTX("Duplicate keyword \"status\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 161 | |
| 162 | /* invalid cardinality of status substatement */ |
| 163 | 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" |
| 164 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 165 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 166 | "<md:annotation name=\"aa\">\n" |
| 167 | " <type name=\"string\"/>\n" |
| 168 | " <type name=\"uint8\"/>\n" |
| 169 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 171 | CHECK_LOG_CTX("Duplicate keyword \"type\".", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 172 | |
| 173 | /* duplication of the same annotation */ |
| 174 | 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" |
| 175 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 176 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 177 | "<md:annotation name=\"aa\">\n" |
| 178 | " <type name=\"string\"/>\n" |
| 179 | "</md:annotation><md:annotation name=\"aa\">\n" |
| 180 | " <type name=\"uint8\"/>\n" |
| 181 | "</md:annotation></module>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 182 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL)); |
| 183 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 184 | "Extension md:annotation is instantiated multiple times.)", "/aa:{extension='md:annotation'}/aa"); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 187 | int |
| 188 | main(void) |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 189 | { |
| 190 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 191 | UTEST(test_yang), |
| 192 | UTEST(test_yin), |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 196 | } |