blob: 6c76a2f18ca15b16af9e2ad8c948c4123f5ff63d [file] [log] [blame]
Radek Krejci1a4a1c82016-09-14 16:07:25 +02001/**
Radek Krejci9e68d872016-09-15 14:50:48 +02002 * \file test_conformance.c
Radek Krejci1a4a1c82016-09-14 16:07:25 +02003 * \author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejci9e68d872016-09-15 14:50:48 +02004 * \brief libyang tests - setting up modules as imported or implemented
Radek Krejci1a4a1c82016-09-14 16:07:25 +02005 *
6 * Copyright (c) 2015 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
15#include <errno.h>
16#include <fcntl.h>
17#include <pthread.h>
18#include <setjmp.h>
19#include <stdarg.h>
20#include <stddef.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
26#include <cmocka.h>
27
28#include "../../src/libyang.h"
29#include "../config.h"
30
31#define SCHEMA_FOLDER_YIN TESTS_DIR"/schema/yin/conformance"
32#define SCHEMA_FOLDER_YANG TESTS_DIR"/schema/yang/conformance"
33
34static int
35setup_ctx(void **state)
36{
37 *state = ly_ctx_new(NULL);
38 if (!*state) {
39 return -1;
40 }
41 return 0;
42}
43
44static int
45teardown_ctx(void **state)
46{
47 ly_ctx_destroy(*state, NULL);
48 return 0;
49}
50
51static void
52test_implemented1_yin(void **state)
53{
54 struct ly_ctx *ctx = *state;
55 const struct lys_module *a, *b, *b2, *c, *c2;
56
57 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN);
58
59 /* loads a.yin (impl), b@2015-01-01.yin (impl by augment) and c@2015-03-03.yin (imp) */
60 a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN);
61 assert_ptr_not_equal(a, NULL);
62 assert_int_equal(a->implemented, 1);
63
64 b = ly_ctx_get_module(ctx, "b", NULL);
65 assert_ptr_not_equal(b, NULL);
66 assert_int_equal(b->implemented, 1);
67
68 c = ly_ctx_get_module(ctx, "c", NULL);
69 assert_ptr_not_equal(c, NULL);
70 assert_int_equal(c->implemented, 0);
71
72 /* another b cannot be loaded, since it is already implemented */
73 b2 = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/b@2015-04-04.yin", LYS_IN_YIN);
74 assert_ptr_equal(b2, NULL);
75 assert_int_equal(ly_errno, LY_EINVAL);
76 assert_string_equal(ly_errmsg(), "Module \"b\" parsing failed.");
77
78 /* older c can be loaded and it will be marked as implemented */
79 c2 = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/c@2015-01-01.yin", LYS_IN_YIN);
80 assert_ptr_not_equal(c2, NULL);
81 assert_int_equal(c2->implemented, 1);
82 assert_ptr_not_equal(c, c2);
83}
84
85static void
Pavol Vican5c186c22016-09-27 10:46:14 +020086test_implemented1_yang(void **state)
87{
88 struct ly_ctx *ctx = *state;
89 const struct lys_module *a, *b, *b2, *c, *c2;
90
91 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG);
92
93 /* loads a.yang (impl), b@2015-01-01.yang (impl by augment) and c@2015-03-03.yang (imp) */
94 a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG);
95 assert_ptr_not_equal(a, NULL);
96 assert_int_equal(a->implemented, 1);
97
98 b = ly_ctx_get_module(ctx, "b", NULL);
99 assert_ptr_not_equal(b, NULL);
100 assert_int_equal(b->implemented, 1);
101
102 c = ly_ctx_get_module(ctx, "c", NULL);
103 assert_ptr_not_equal(c, NULL);
104 assert_int_equal(c->implemented, 0);
105
106 /* another b cannot be loaded, since it is already implemented */
107 b2 = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/b@2015-04-04.yang", LYS_IN_YANG);
108 assert_ptr_equal(b2, NULL);
109 assert_int_equal(ly_errno, LY_EINVAL);
110 assert_string_equal(ly_errmsg(), "Module \"b\" parsing failed.");
111
112 /* older c can be loaded and it will be marked as implemented */
113 c2 = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/c@2015-01-01.yang", LYS_IN_YANG);
114 assert_ptr_not_equal(c2, NULL);
115 assert_int_equal(c2->implemented, 1);
116 assert_ptr_not_equal(c, c2);
117}
118
119static void
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200120test_implemented2_yin(void **state)
121{
122 struct ly_ctx *ctx = *state;
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200123 const struct lys_module *a, *b2;
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200124
125 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN);
126
127 /* load the newest b first, it is implemented */
128 b2 = ly_ctx_load_module(ctx, "b", "2015-04-04");
129 assert_ptr_not_equal(b2, NULL);
130 assert_int_equal(b2->implemented, 1);
131
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200132 /* loads a.yin (impl), b@2015-04-04 is augmented by a, but cannot be implemented */
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200133 a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN);
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200134 assert_ptr_equal(a, NULL);
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200135}
136
137static void
Pavol Vican5c186c22016-09-27 10:46:14 +0200138test_implemented2_yang(void **state)
139{
140 struct ly_ctx *ctx = *state;
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200141 const struct lys_module *a, *b2;
Pavol Vican5c186c22016-09-27 10:46:14 +0200142
143 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG);
144
145 /* load the newest b first, it is implemented */
146 b2 = ly_ctx_load_module(ctx, "b", "2015-04-04");
147 assert_ptr_not_equal(b2, NULL);
148 assert_int_equal(b2->implemented, 1);
149
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200150 /* loads a.yin (impl), b@2015-04-04 is augmented by a, but cannot be implemented */
Pavol Vican5c186c22016-09-27 10:46:14 +0200151 a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG);
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200152 assert_ptr_equal(a, NULL);
Pavol Vican5c186c22016-09-27 10:46:14 +0200153}
154
155static void
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200156test_implemented_info_yin(void **state)
157{
158 struct ly_ctx *ctx = *state;
159 struct lyd_node *info;
160 const struct lys_module *a;
161 char *data;
162 const char *template = "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
163" <module>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100164" <name>ietf-yang-metadata</name>\n"
165" <revision>2016-08-05</revision>\n"
166" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
167" <conformance-type>import</conformance-type>\n"
168" </module>\n"
169" <module>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200170" <name>yang</name>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100171" <revision>2017-02-20</revision>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200172" <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
173" <conformance-type>implement</conformance-type>\n"
174" </module>\n"
175" <module>\n"
176" <name>ietf-inet-types</name>\n"
177" <revision>2013-07-15</revision>\n"
178" <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
179" <conformance-type>import</conformance-type>\n"
180" </module>\n"
181" <module>\n"
182" <name>ietf-yang-types</name>\n"
183" <revision>2013-07-15</revision>\n"
184" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
185" <conformance-type>import</conformance-type>\n"
186" </module>\n"
187" <module>\n"
188" <name>ietf-yang-library</name>\n"
Radek Krejcid9723912016-09-16 17:06:06 +0200189" <revision>2016-06-21</revision>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200190" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
191" <conformance-type>implement</conformance-type>\n"
192" </module>\n"
193" <module>\n"
194" <name>b</name>\n"
195" <revision>2015-01-01</revision>\n"
196" <schema>file://"SCHEMA_FOLDER_YIN"/b@2015-01-01.yin</schema>\n"
197" <namespace>urn:example:b</namespace>\n"
198" <conformance-type>implement</conformance-type>\n"
199" </module>\n"
200" <module>\n"
201" <name>c</name>\n"
202" <revision>2015-03-03</revision>\n"
203" <schema>file://"SCHEMA_FOLDER_YIN"/c@2015-03-03.yin</schema>\n"
204" <namespace>urn:example:c</namespace>\n"
205" <conformance-type>import</conformance-type>\n"
206" </module>\n"
207" <module>\n"
208" <name>a</name>\n"
209" <revision>2015-01-01</revision>\n"
210" <schema>file://"SCHEMA_FOLDER_YIN"/a.yin</schema>\n"
211" <namespace>urn:example:a</namespace>\n"
212" <feature>foo</feature>\n"
213" <conformance-type>implement</conformance-type>\n"
214" </module>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100215" <module-set-id>9</module-set-id>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200216"</modules-state>\n";
217
218 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN);
219
220 /* loads a.yin (impl), b@2015-01-01.yin (impl by augment) and c@2015-03-03.yin (imp) */
221 assert_ptr_not_equal((a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN)), NULL);
222 assert_int_equal(lys_features_enable(a, "foo"), 0);
223
224 /* get yang-library data */
225 info = ly_ctx_info(ctx);
226 assert_ptr_not_equal(info, NULL);
227
228 lyd_print_mem(&data, info, LYD_XML, LYP_FORMAT);
229 lyd_free_withsiblings(info);
230 assert_string_equal(data, template);
231 free(data);
232}
233
Pavol Vican5c186c22016-09-27 10:46:14 +0200234static void
235test_implemented_info_yang(void **state)
236{
237 struct ly_ctx *ctx = *state;
238 struct lyd_node *info;
239 const struct lys_module *a;
240 char *data;
241 const char *template = "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
242" <module>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100243" <name>ietf-yang-metadata</name>\n"
244" <revision>2016-08-05</revision>\n"
245" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
246" <conformance-type>import</conformance-type>\n"
247" </module>\n"
248" <module>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200249" <name>yang</name>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100250" <revision>2017-02-20</revision>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200251" <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
252" <conformance-type>implement</conformance-type>\n"
253" </module>\n"
254" <module>\n"
255" <name>ietf-inet-types</name>\n"
256" <revision>2013-07-15</revision>\n"
257" <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
258" <conformance-type>import</conformance-type>\n"
259" </module>\n"
260" <module>\n"
261" <name>ietf-yang-types</name>\n"
262" <revision>2013-07-15</revision>\n"
263" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
264" <conformance-type>import</conformance-type>\n"
265" </module>\n"
266" <module>\n"
267" <name>ietf-yang-library</name>\n"
268" <revision>2016-06-21</revision>\n"
269" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
270" <conformance-type>implement</conformance-type>\n"
271" </module>\n"
272" <module>\n"
273" <name>b</name>\n"
274" <revision>2015-01-01</revision>\n"
275" <schema>file://"SCHEMA_FOLDER_YANG"/b@2015-01-01.yang</schema>\n"
276" <namespace>urn:example:b</namespace>\n"
277" <conformance-type>implement</conformance-type>\n"
278" </module>\n"
279" <module>\n"
280" <name>c</name>\n"
281" <revision>2015-03-03</revision>\n"
282" <schema>file://"SCHEMA_FOLDER_YANG"/c@2015-03-03.yang</schema>\n"
283" <namespace>urn:example:c</namespace>\n"
284" <conformance-type>import</conformance-type>\n"
285" </module>\n"
286" <module>\n"
287" <name>a</name>\n"
288" <revision>2015-01-01</revision>\n"
289" <schema>file://"SCHEMA_FOLDER_YANG"/a.yang</schema>\n"
290" <namespace>urn:example:a</namespace>\n"
291" <feature>foo</feature>\n"
292" <conformance-type>implement</conformance-type>\n"
293" </module>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100294" <module-set-id>9</module-set-id>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200295"</modules-state>\n";
296
297 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG);
298
299 /* loads a.yang (impl), b@2015-01-01.yang (impl by augment) and c@2015-03-03.yang (imp) */
300 assert_ptr_not_equal((a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG)), NULL);
301 assert_int_equal(lys_features_enable(a, "foo"), 0);
302
303 /* get yang-library data */
304 info = ly_ctx_info(ctx);
305 assert_ptr_not_equal(info, NULL);
306
307 lyd_print_mem(&data, info, LYD_XML, LYP_FORMAT);
308 lyd_free_withsiblings(info);
309 assert_string_equal(data, template);
310 free(data);
311}
312
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200313int
314main(void)
315{
316 const struct CMUnitTest cmut[] = {
317 cmocka_unit_test_setup_teardown(test_implemented1_yin, setup_ctx, teardown_ctx),
Pavol Vican5c186c22016-09-27 10:46:14 +0200318 cmocka_unit_test_setup_teardown(test_implemented1_yang, setup_ctx, teardown_ctx),
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200319 cmocka_unit_test_setup_teardown(test_implemented2_yin, setup_ctx, teardown_ctx),
Pavol Vican5c186c22016-09-27 10:46:14 +0200320 cmocka_unit_test_setup_teardown(test_implemented2_yang, setup_ctx, teardown_ctx),
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200321 cmocka_unit_test_setup_teardown(test_implemented_info_yin, setup_ctx, teardown_ctx),
Pavol Vican5c186c22016-09-27 10:46:14 +0200322 cmocka_unit_test_setup_teardown(test_implemented_info_yang, setup_ctx, teardown_ctx),
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200323 };
324
325 return cmocka_run_group_tests(cmut, NULL, NULL);
326}