blob: 523bcdcc68a9be1025f2b7780244a5c6007da9bb [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;
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 "}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200124 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YANG, &mod));
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;}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200135 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200136 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;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200142 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200143 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;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200149 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200150 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;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200156 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200157 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;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200163 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200164 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;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200170 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejciad5963b2019-09-06 16:03:05 +0200171 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
Michal Vasko3a41dff2020-07-15 14:30:28 +0200183 const struct lys_module *mod;
Radek Krejci7c960162019-09-18 14:16:12 +0200184 struct lysc_ext_instance *e;
185 struct lyext_metadata *ant;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200186 const char *data;
Radek Krejci7c960162019-09-18 14:16:12 +0200187
Michal Vasko7f45cf22020-10-01 12:49:44 +0200188 /* TODO yin-extension-prefix-compilation-bug
189 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 +0200190 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n"
191 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
192 "<feature name=\"f\"/>\n"
193 "<md:annotation name=\"x\">\n"
194 " <description><text>test</text></description>\n"
195 " <reference><text>test</text></reference>\n"
196 " <if-feature name=\"f\"/>\n"
197 " <status value=\"current\"/>\n"
198 " <type name=\"uint8\"/>\n"
199 " <units name=\"meters\"/>\n"
200 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200201 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YIN, &mod));
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200202 assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
Radek Krejci7c960162019-09-18 14:16:12 +0200203 e = &mod->compiled->exts[0];
204 assert_non_null(ant = (struct lyext_metadata*)e->data);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200205 assert_string_equal("meters", ant->units);*/
Radek Krejci7c960162019-09-18 14:16:12 +0200206
207 /* invalid */
208 /* missing mandatory type substatement */
209 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"
210 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
211 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
212 "<md:annotation name=\"aa\"/>\n"
213 "</module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200214 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200215 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa");
216
217 /* not allowed substatement */
218 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"
219 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
220 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
221 "<md:annotation name=\"aa\">\n"
222 " <default value=\"x\"/>\n"
223 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200224 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200225 logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa");
226
227 /* invalid cardinality of units substatement */
228 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"
229 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
230 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
231 "<md:annotation name=\"aa\">\n"
232 " <type name=\"string\"/>\n"
233 " <units name=\"x\"/>\n"
234 " <units name=\"y\"/>\n"
235 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200236 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200237 logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa");
238
239 /* invalid cardinality of status substatement */
240 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"
241 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
242 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
243 "<md:annotation name=\"aa\">\n"
244 " <type name=\"string\"/>\n"
245 " <status value=\"current\"/>\n"
246 " <status value=\"obsolete\"/>\n"
247 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200248 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200249 logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa");
250
251 /* invalid cardinality of status substatement */
252 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"
253 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
254 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
255 "<md:annotation name=\"aa\">\n"
256 " <type name=\"string\"/>\n"
257 " <type name=\"uint8\"/>\n"
258 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200259 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200260 logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa");
261
262 /* duplication of the same annotation */
263 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"
264 "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
265 "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
266 "<md:annotation name=\"aa\">\n"
267 " <type name=\"string\"/>\n"
268 "</md:annotation><md:annotation name=\"aa\">\n"
269 " <type name=\"uint8\"/>\n"
270 "</md:annotation></module>";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200271 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL));
Radek Krejci7c960162019-09-18 14:16:12 +0200272 logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": "
273 "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa");
274 s->func = NULL;
275}
276
Radek Krejciad5963b2019-09-06 16:03:05 +0200277int main(void)
278{
279 const struct CMUnitTest tests[] = {
280 cmocka_unit_test_setup_teardown(test_yang, setup, teardown),
Radek Krejci7c960162019-09-18 14:16:12 +0200281 cmocka_unit_test_setup_teardown(test_yin, setup, teardown),
Radek Krejciad5963b2019-09-06 16:03:05 +0200282 };
283
284 return cmocka_run_group_tests(tests, NULL, NULL);
285}