blob: 4f1ac74807add1d4492759d2c23ac7c439a3a250 [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
Michal Vasko3a41dff2020-07-15 14:30:28 +0200109 const struct lys_module *mod;
Radek Krejciad5963b2019-09-06 16:03:05 +0200110 struct lysc_ext_instance *e;
111 struct lyext_metadata *ant;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100112 struct ly_in *in;
Radek Krejciad5963b2019-09-06 16:03:05 +0200113
114 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:metadata:a; prefix a;"
115 "import ietf-yang-metadata {prefix md;}"
116 "feature f;"
117 "md:annotation x {"
118 " description \"test\";"
119 " if-feature f;"
120 " reference \"test\";"
121 " status \"current\";"
122 " type uint8;"
123 " units meters;"
124 "}}";
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100125 const char *feats[] = {"f", NULL};
126 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
127 assert_int_equal(LY_SUCCESS, lys_parse(s->ctx, in, LYS_IN_YANG, feats, &mod));
128 ly_in_free(in, 0);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200129 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejciad5963b2019-09-06 16:03:05 +0200130 e = &mod->compiled->exts[0];
131 assert_non_null(ant = (struct lyext_metadata*)e->data);
132 assert_string_equal("meters", ant->units);
133
134 /* invalid */
135 /* missing mandatory type substatement */
136 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
137 "import ietf-yang-metadata {prefix md;}"
138 "md:annotation aa;}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200139 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200140 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa");
141
142 /* not allowed substatement */
143 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
144 "import ietf-yang-metadata {prefix md;}"
145 "md:annotation aa {default x;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200146 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200147 logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa");
148
149 /* invalid cardinality of units substatement */
150 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
151 "import ietf-yang-metadata {prefix md;}"
152 "md:annotation aa {type string; units x; units y;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200153 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200154 logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa");
155
156 /* invalid cardinality of status substatement */
157 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
158 "import ietf-yang-metadata {prefix md;}"
159 "md:annotation aa {type string; status current; status obsolete;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200160 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200161 logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa");
162
163 /* invalid cardinality of status substatement */
164 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
165 "import ietf-yang-metadata {prefix md;}"
166 "md:annotation aa {type string; type uint8;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200167 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200168 logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa");
169
170 /* duplication of the same annotation */
171 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
172 "import ietf-yang-metadata {prefix md;}"
173 "md:annotation aa {type string;} md:annotation aa {type uint8;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200174 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200175 logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": "
176 "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa");
177
178 s->func = NULL;
179}
180
Radek Krejci7c960162019-09-18 14:16:12 +0200181static void
182test_yin(void **state)
183{
184 struct state_s *s = (struct state_s*)(*state);
185 s->func = test_yin;
186
Michal Vasko3a41dff2020-07-15 14:30:28 +0200187 const struct lys_module *mod;
Radek Krejci7c960162019-09-18 14:16:12 +0200188 struct lysc_ext_instance *e;
189 struct lyext_metadata *ant;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200190 const char *data;
Radek Krejci7c960162019-09-18 14:16:12 +0200191
Michal Vasko7f45cf22020-10-01 12:49:44 +0200192 /* TODO yin-extension-prefix-compilation-bug
193 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 +0200194 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n"
195 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
196 "<feature name=\"f\"/>\n"
197 "<md:annotation name=\"x\">\n"
198 " <description><text>test</text></description>\n"
199 " <reference><text>test</text></reference>\n"
200 " <if-feature name=\"f\"/>\n"
201 " <status value=\"current\"/>\n"
202 " <type name=\"uint8\"/>\n"
203 " <units name=\"meters\"/>\n"
204 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200205 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YIN, &mod));
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200206 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejci7c960162019-09-18 14:16:12 +0200207 e = &mod->compiled->exts[0];
208 assert_non_null(ant = (struct lyext_metadata*)e->data);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200209 assert_string_equal("meters", ant->units);*/
Radek Krejci7c960162019-09-18 14:16:12 +0200210
211 /* invalid */
212 /* missing mandatory type substatement */
213 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"
214 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
215 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
216 "<md:annotation name=\"aa\"/>\n"
217 "</module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200218 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200219 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa");
220
221 /* not allowed substatement */
222 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"
223 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
224 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
225 "<md:annotation name=\"aa\">\n"
226 " <default value=\"x\"/>\n"
227 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200228 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200229 logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa");
230
231 /* invalid cardinality of units substatement */
232 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"
233 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
234 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
235 "<md:annotation name=\"aa\">\n"
236 " <type name=\"string\"/>\n"
237 " <units name=\"x\"/>\n"
238 " <units name=\"y\"/>\n"
239 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200240 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200241 logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa");
242
243 /* invalid cardinality of status substatement */
244 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"
245 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
246 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
247 "<md:annotation name=\"aa\">\n"
248 " <type name=\"string\"/>\n"
249 " <status value=\"current\"/>\n"
250 " <status value=\"obsolete\"/>\n"
251 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200252 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200253 logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa");
254
255 /* invalid cardinality of status substatement */
256 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"
257 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
258 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
259 "<md:annotation name=\"aa\">\n"
260 " <type name=\"string\"/>\n"
261 " <type name=\"uint8\"/>\n"
262 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200263 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200264 logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa");
265
266 /* duplication of the same annotation */
267 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"
268 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
269 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
270 "<md:annotation name=\"aa\">\n"
271 " <type name=\"string\"/>\n"
272 "</md:annotation><md:annotation name=\"aa\">\n"
273 " <type name=\"uint8\"/>\n"
274 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200275 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200276 logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": "
277 "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa");
278 s->func = NULL;
279}
280
Radek Krejciad5963b2019-09-06 16:03:05 +0200281int main(void)
282{
283 const struct CMUnitTest tests[] = {
284 cmocka_unit_test_setup_teardown(test_yang, setup, teardown),
Radek Krejci7c960162019-09-18 14:16:12 +0200285 cmocka_unit_test_setup_teardown(test_yin, setup, teardown),
Radek Krejciad5963b2019-09-06 16:03:05 +0200286 };
287
288 return cmocka_run_group_tests(tests, NULL, NULL);
289}