blob: fec21fc4104ed2598764d041a149995094958673 [file] [log] [blame]
Radek Krejciad5963b2019-09-06 16:03:05 +02001/*
2 * @file test_metadata.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for Metadata extension (annotation) support
5 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2019-2020 CESNET, z.s.p.o.
Radek Krejciad5963b2019-09-06 16:03:05 +02007 *
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ša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
Radek Krejcib4ac5a92020-11-23 17:54:33 +010015#include "utests.h"
Radek Krejciad5963b2019-09-06 16:03:05 +020016
Radek Krejci70593c12020-06-13 20:48:09 +020017#include "libyang.h"
18#include "plugins_exts_metadata.h"
Radek Krejciad5963b2019-09-06 16:03:05 +020019
Radek Krejciad5963b2019-09-06 16:03:05 +020020static void
21test_yang(void **state)
22{
Michal Vasko3a41dff2020-07-15 14:30:28 +020023 const struct lys_module *mod;
Radek Krejciad5963b2019-09-06 16:03:05 +020024 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 Vasko7b1ad1a2020-11-02 15:41:27 +010038 const char *feats[] = {"f", NULL};
Radek Krejcib4ac5a92020-11-23 17:54:33 +010039
Radek Iša56ca9e42020-09-08 18:42:00 +020040 UTEST_ADD_MODULE(data, LYS_IN_YANG, feats, &mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020041 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejciad5963b2019-09-06 16:03:05 +020042 e = &mod->compiled->exts[0];
Radek Krejcib4ac5a92020-11-23 17:54:33 +010043 assert_non_null(ant = (struct lyext_metadata *)e->data);
Radek Krejciad5963b2019-09-06 16:03:05 +020044 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ša56ca9e42020-09-08 18:42:00 +020051 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 Krejciad5963b2019-09-06 16:03:05 +020053
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ša56ca9e42020-09-08 18:42:00 +020058 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 Krejciad5963b2019-09-06 16:03:05 +020060
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ša56ca9e42020-09-08 18:42:00 +020065 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 Krejciad5963b2019-09-06 16:03:05 +020067
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ša56ca9e42020-09-08 18:42:00 +020072 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 Krejciad5963b2019-09-06 16:03:05 +020074
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ša56ca9e42020-09-08 18:42:00 +020079 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 Krejciad5963b2019-09-06 16:03:05 +020081
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ša56ca9e42020-09-08 18:42:00 +020086 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 Krejciad5963b2019-09-06 16:03:05 +020089}
90
Radek Krejci7c960162019-09-18 14:16:12 +020091static void
92test_yin(void **state)
93{
Michal Vasko3a41dff2020-07-15 14:30:28 +020094 const struct lys_module *mod;
Radek Krejci7c960162019-09-18 14:16:12 +020095 struct lysc_ext_instance *e;
96 struct lyext_metadata *ant;
Michal Vasko7f45cf22020-10-01 12:49:44 +020097 const char *data;
Radek Krejci7c960162019-09-18 14:16:12 +020098
Michal Vasko7f45cf22020-10-01 12:49:44 +020099 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 Krejci7c960162019-09-18 14:16:12 +0200100 "<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 Vaskofc2cd072021-02-24 13:17:17 +0100111 const char *feats[] = {"f", NULL};
112
113 UTEST_ADD_MODULE(data, LYS_IN_YIN, feats, &mod);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200114 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejci7c960162019-09-18 14:16:12 +0200115 e = &mod->compiled->exts[0];
116 assert_non_null(ant = (struct lyext_metadata*)e->data);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100117 assert_string_equal("meters", ant->units);
Radek Krejci7c960162019-09-18 14:16:12 +0200118
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ša56ca9e42020-09-08 18:42:00 +0200126 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
Michal Vaskofc2cd072021-02-24 13:17:17 +0100127 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"md:annotation\".", "/aa:{extension='md:annotation'}/aa");
Radek Krejci7c960162019-09-18 14:16:12 +0200128
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ša56ca9e42020-09-08 18:42:00 +0200136 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
Michal Vaskofc2cd072021-02-24 13:17:17 +0100137 CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"md:annotation\" extension instance.", "/aa:{extension='md:annotation'}/aa");
Radek Krejci7c960162019-09-18 14:16:12 +0200138
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ša56ca9e42020-09-08 18:42:00 +0200148 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 Krejci7c960162019-09-18 14:16:12 +0200150
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ša56ca9e42020-09-08 18:42:00 +0200160 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 Krejci7c960162019-09-18 14:16:12 +0200162
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ša56ca9e42020-09-08 18:42:00 +0200171 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 Krejci7c960162019-09-18 14:16:12 +0200173
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ša56ca9e42020-09-08 18:42:00 +0200183 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 Krejci7c960162019-09-18 14:16:12 +0200186}
187
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100188int
189main(void)
Radek Krejciad5963b2019-09-06 16:03:05 +0200190{
191 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 UTEST(test_yang),
193 UTEST(test_yin),
Radek Krejciad5963b2019-09-06 16:03:05 +0200194 };
195
196 return cmocka_run_group_tests(tests, NULL, NULL);
197}