blob: 5a48b86c1353c992f753275f555431875f5fb3ae [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 /* 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 Krejci7c960162019-09-18 14:16:12 +0200101 "<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ša56ca9e42020-09-08 18:42:00 +0200112 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, &mod));
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200113 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejci7c960162019-09-18 14:16:12 +0200114 e = &mod->compiled->exts[0];
115 assert_non_null(ant = (struct lyext_metadata*)e->data);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200116 assert_string_equal("meters", ant->units);*/
Radek Krejci7c960162019-09-18 14:16:12 +0200117
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ša56ca9e42020-09-08 18:42:00 +0200125 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 Krejci7c960162019-09-18 14:16:12 +0200127
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ša56ca9e42020-09-08 18:42:00 +0200135 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 Krejci7c960162019-09-18 14:16:12 +0200137
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ša56ca9e42020-09-08 18:42:00 +0200147 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 Krejci7c960162019-09-18 14:16:12 +0200149
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ša56ca9e42020-09-08 18:42:00 +0200159 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 Krejci7c960162019-09-18 14:16:12 +0200161
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ša56ca9e42020-09-08 18:42:00 +0200170 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 Krejci7c960162019-09-18 14:16:12 +0200172
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ša56ca9e42020-09-08 18:42:00 +0200182 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 Krejci7c960162019-09-18 14:16:12 +0200185}
186
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100187int
188main(void)
Radek Krejciad5963b2019-09-06 16:03:05 +0200189{
190 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 UTEST(test_yang),
192 UTEST(test_yin),
Radek Krejciad5963b2019-09-06 16:03:05 +0200193 };
194
195 return cmocka_run_group_tests(tests, NULL, NULL);
196}