blob: e206d224c754893bf8dd1fe80db769e6df3d572f [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 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
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 */
14
Radek Krejciad5963b2019-09-06 16:03:05 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "libyang.h"
24#include "plugins_exts_metadata.h"
Radek Krejciad5963b2019-09-06 16:03:05 +020025
26#define BUFSIZE 1024
27char logbuf[BUFSIZE] = {0};
28int store = -1; /* negative for infinite logging, positive for limited logging */
29
30struct state_s {
31 void *func;
32 struct ly_ctx *ctx;
33};
34
35/* set to 0 to printing error messages to stderr instead of checking them in code */
36#define ENABLE_LOGGER_CHECKING 1
37
38#if ENABLE_LOGGER_CHECKING
39static void
40logger(LY_LOG_LEVEL level, const char *msg, const char *path)
41{
42 (void) level; /* unused */
43 if (store) {
44 if (path && path[0]) {
45 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
46 } else {
47 strncpy(logbuf, msg, BUFSIZE - 1);
48 }
49 if (store > 0) {
50 --store;
51 }
52 }
53}
54#endif
55
56static int
57setup(void **state)
58{
59 struct state_s *s;
60
61 s = calloc(1, sizeof *s);
62 assert_non_null(s);
63
64#if ENABLE_LOGGER_CHECKING
65 ly_set_log_clb(logger, 1);
66#endif
67
68 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &s->ctx));
69 *state = s;
70
71 return 0;
72}
73
74static int
75teardown(void **state)
76{
77 struct state_s *s = (struct state_s*)(*state);
78
79#if ENABLE_LOGGER_CHECKING
80 if (s->func) {
81 fprintf(stderr, "%s\n", logbuf);
82 }
83#endif
84
85 ly_ctx_destroy(s->ctx, NULL);
86 free(s);
87
88 return 0;
89}
90
91void
92logbuf_clean(void)
93{
94 logbuf[0] = '\0';
95}
96
97#if ENABLE_LOGGER_CHECKING
98# define logbuf_assert(str) assert_string_equal(logbuf, str)
99#else
100# define logbuf_assert(str)
101#endif
102
103static void
104test_yang(void **state)
105{
106 struct state_s *s = (struct state_s*)(*state);
107 s->func = test_yang;
108
109 struct lys_module *mod;
110 struct lysc_ext_instance *e;
111 struct lyext_metadata *ant;
112
113 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:metadata:a; prefix a;"
114 "import ietf-yang-metadata {prefix md;}"
115 "feature f;"
116 "md:annotation x {"
117 " description \"test\";"
118 " if-feature f;"
119 " reference \"test\";"
120 " status \"current\";"
121 " type uint8;"
122 " units meters;"
123 "}}";
124 assert_non_null(mod = lys_parse_mem(s->ctx, data, LYS_IN_YANG));
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200125 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejciad5963b2019-09-06 16:03:05 +0200126 e = &mod->compiled->exts[0];
127 assert_non_null(ant = (struct lyext_metadata*)e->data);
128 assert_string_equal("meters", ant->units);
129
130 /* invalid */
131 /* missing mandatory type substatement */
132 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
133 "import ietf-yang-metadata {prefix md;}"
134 "md:annotation aa;}";
135 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YANG));
136 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa");
137
138 /* not allowed substatement */
139 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
140 "import ietf-yang-metadata {prefix md;}"
141 "md:annotation aa {default x;}}";
142 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YANG));
143 logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa");
144
145 /* invalid cardinality of units substatement */
146 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
147 "import ietf-yang-metadata {prefix md;}"
148 "md:annotation aa {type string; units x; units y;}}";
149 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YANG));
150 logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa");
151
152 /* invalid cardinality of status substatement */
153 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
154 "import ietf-yang-metadata {prefix md;}"
155 "md:annotation aa {type string; status current; status obsolete;}}";
156 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YANG));
157 logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa");
158
159 /* invalid cardinality of status substatement */
160 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
161 "import ietf-yang-metadata {prefix md;}"
162 "md:annotation aa {type string; type uint8;}}";
163 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YANG));
164 logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa");
165
166 /* duplication of the same annotation */
167 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
168 "import ietf-yang-metadata {prefix md;}"
169 "md:annotation aa {type string;} md:annotation aa {type uint8;}}";
170 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YANG));
171 logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": "
172 "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa");
173
174 s->func = NULL;
175}
176
Radek Krejci7c960162019-09-18 14:16:12 +0200177static void
178test_yin(void **state)
179{
180 struct state_s *s = (struct state_s*)(*state);
181 s->func = test_yin;
182
183 struct lys_module *mod;
184 struct lysc_ext_instance *e;
185 struct lyext_metadata *ant;
186
187 const char *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"
188 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n"
189 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
190 "<feature name=\"f\"/>\n"
191 "<md:annotation name=\"x\">\n"
192 " <description><text>test</text></description>\n"
193 " <reference><text>test</text></reference>\n"
194 " <if-feature name=\"f\"/>\n"
195 " <status value=\"current\"/>\n"
196 " <type name=\"uint8\"/>\n"
197 " <units name=\"meters\"/>\n"
198 "</md:annotation></module>";
199 assert_non_null(mod = lys_parse_mem(s->ctx, data, LYS_IN_YIN));
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200200 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejci7c960162019-09-18 14:16:12 +0200201 e = &mod->compiled->exts[0];
202 assert_non_null(ant = (struct lyext_metadata*)e->data);
203 assert_string_equal("meters", ant->units);
204
205 /* invalid */
206 /* missing mandatory type substatement */
207 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"
208 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
209 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
210 "<md:annotation name=\"aa\"/>\n"
211 "</module>";
212 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YIN));
213 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa");
214
215 /* not allowed substatement */
216 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"
217 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
218 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
219 "<md:annotation name=\"aa\">\n"
220 " <default value=\"x\"/>\n"
221 "</md:annotation></module>";
222 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YIN));
223 logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa");
224
225 /* invalid cardinality of units substatement */
226 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"
227 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
228 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
229 "<md:annotation name=\"aa\">\n"
230 " <type name=\"string\"/>\n"
231 " <units name=\"x\"/>\n"
232 " <units name=\"y\"/>\n"
233 "</md:annotation></module>";
234 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YIN));
235 logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa");
236
237 /* invalid cardinality of status substatement */
238 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"
239 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
240 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
241 "<md:annotation name=\"aa\">\n"
242 " <type name=\"string\"/>\n"
243 " <status value=\"current\"/>\n"
244 " <status value=\"obsolete\"/>\n"
245 "</md:annotation></module>";
246 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YIN));
247 logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa");
248
249 /* invalid cardinality of status substatement */
250 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"
251 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
252 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
253 "<md:annotation name=\"aa\">\n"
254 " <type name=\"string\"/>\n"
255 " <type name=\"uint8\"/>\n"
256 "</md:annotation></module>";
257 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YIN));
258 logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa");
259
260 /* duplication of the same annotation */
261 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"
262 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
263 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
264 "<md:annotation name=\"aa\">\n"
265 " <type name=\"string\"/>\n"
266 "</md:annotation><md:annotation name=\"aa\">\n"
267 " <type name=\"uint8\"/>\n"
268 "</md:annotation></module>";
269 assert_null(lys_parse_mem(s->ctx, data, LYS_IN_YIN));
270 logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": "
271 "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa");
272 s->func = NULL;
273}
274
Radek Krejciad5963b2019-09-06 16:03:05 +0200275int main(void)
276{
277 const struct CMUnitTest tests[] = {
278 cmocka_unit_test_setup_teardown(test_yang, setup, teardown),
Radek Krejci7c960162019-09-18 14:16:12 +0200279 cmocka_unit_test_setup_teardown(test_yin, setup, teardown),
Radek Krejciad5963b2019-09-06 16:03:05 +0200280 };
281
282 return cmocka_run_group_tests(tests, NULL, NULL);
283}