blob: 3ab199ba54820fadc21dbf1f02ebd1ba95b9e0f0 [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
Jan Kundrátfd7a5c72017-10-26 17:45:06 +020028#include "libyang.h"
Peter Feige8deb9a02018-06-19 05:29:42 +000029#include <context.h>
Jan Kundrátfd7a5c72017-10-26 17:45:06 +020030#include "tests/config.h"
Radek Krejci1a4a1c82016-09-14 16:07:25 +020031
32#define SCHEMA_FOLDER_YIN TESTS_DIR"/schema/yin/conformance"
33#define SCHEMA_FOLDER_YANG TESTS_DIR"/schema/yang/conformance"
34
35static int
36setup_ctx(void **state)
37{
Radek Krejcidd3263a2017-07-15 11:50:09 +020038 *state = ly_ctx_new(NULL, 0);
Radek Krejci1a4a1c82016-09-14 16:07:25 +020039 if (!*state) {
40 return -1;
41 }
42 return 0;
43}
44
45static int
46teardown_ctx(void **state)
47{
48 ly_ctx_destroy(*state, NULL);
49 return 0;
50}
51
52static void
53test_implemented1_yin(void **state)
54{
55 struct ly_ctx *ctx = *state;
56 const struct lys_module *a, *b, *b2, *c, *c2;
57
58 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN);
59
60 /* loads a.yin (impl), b@2015-01-01.yin (impl by augment) and c@2015-03-03.yin (imp) */
61 a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN);
62 assert_ptr_not_equal(a, NULL);
63 assert_int_equal(a->implemented, 1);
64
Radek Krejcidfb00d62017-09-06 09:39:35 +020065 b = ly_ctx_get_module(ctx, "b", NULL, 1);
Radek Krejci1a4a1c82016-09-14 16:07:25 +020066 assert_ptr_not_equal(b, NULL);
67 assert_int_equal(b->implemented, 1);
68
Radek Krejcidfb00d62017-09-06 09:39:35 +020069 c = ly_ctx_get_module(ctx, "c", NULL, 0);
Radek Krejci1a4a1c82016-09-14 16:07:25 +020070 assert_ptr_not_equal(c, NULL);
71 assert_int_equal(c->implemented, 0);
72
73 /* another b cannot be loaded, since it is already implemented */
74 b2 = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/b@2015-04-04.yin", LYS_IN_YIN);
75 assert_ptr_equal(b2, NULL);
76 assert_int_equal(ly_errno, LY_EINVAL);
Michal Vasko53b7da02018-02-13 15:28:42 +010077 assert_string_equal(ly_errmsg(ctx), "Module \"b\" parsing failed.");
Radek Krejci1a4a1c82016-09-14 16:07:25 +020078
79 /* older c can be loaded and it will be marked as implemented */
80 c2 = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/c@2015-01-01.yin", LYS_IN_YIN);
81 assert_ptr_not_equal(c2, NULL);
82 assert_int_equal(c2->implemented, 1);
83 assert_ptr_not_equal(c, c2);
84}
85
86static void
Pavol Vican5c186c22016-09-27 10:46:14 +020087test_implemented1_yang(void **state)
88{
89 struct ly_ctx *ctx = *state;
90 const struct lys_module *a, *b, *b2, *c, *c2;
91
92 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG);
93
94 /* loads a.yang (impl), b@2015-01-01.yang (impl by augment) and c@2015-03-03.yang (imp) */
95 a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG);
96 assert_ptr_not_equal(a, NULL);
97 assert_int_equal(a->implemented, 1);
98
Radek Krejcidfb00d62017-09-06 09:39:35 +020099 b = ly_ctx_get_module(ctx, "b", NULL, 1);
Pavol Vican5c186c22016-09-27 10:46:14 +0200100 assert_ptr_not_equal(b, NULL);
101 assert_int_equal(b->implemented, 1);
102
Radek Krejcidfb00d62017-09-06 09:39:35 +0200103 c = ly_ctx_get_module(ctx, "c", NULL, 0);
Pavol Vican5c186c22016-09-27 10:46:14 +0200104 assert_ptr_not_equal(c, NULL);
105 assert_int_equal(c->implemented, 0);
106
107 /* another b cannot be loaded, since it is already implemented */
108 b2 = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/b@2015-04-04.yang", LYS_IN_YANG);
109 assert_ptr_equal(b2, NULL);
110 assert_int_equal(ly_errno, LY_EINVAL);
Michal Vasko53b7da02018-02-13 15:28:42 +0100111 assert_string_equal(ly_errmsg(ctx), "Module \"b\" parsing failed.");
Pavol Vican5c186c22016-09-27 10:46:14 +0200112
113 /* older c can be loaded and it will be marked as implemented */
114 c2 = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/c@2015-01-01.yang", LYS_IN_YANG);
115 assert_ptr_not_equal(c2, NULL);
116 assert_int_equal(c2->implemented, 1);
117 assert_ptr_not_equal(c, c2);
118}
119
120static void
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200121test_implemented2_yin(void **state)
122{
123 struct ly_ctx *ctx = *state;
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200124 const struct lys_module *a, *b2;
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200125
126 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN);
127
128 /* load the newest b first, it is implemented */
129 b2 = ly_ctx_load_module(ctx, "b", "2015-04-04");
130 assert_ptr_not_equal(b2, NULL);
131 assert_int_equal(b2->implemented, 1);
132
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200133 /* loads a.yin (impl), b@2015-04-04 is augmented by a, but cannot be implemented */
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200134 a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN);
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200135 assert_ptr_equal(a, NULL);
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200136}
137
138static void
Pavol Vican5c186c22016-09-27 10:46:14 +0200139test_implemented2_yang(void **state)
140{
141 struct ly_ctx *ctx = *state;
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200142 const struct lys_module *a, *b2;
Pavol Vican5c186c22016-09-27 10:46:14 +0200143
144 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG);
145
146 /* load the newest b first, it is implemented */
147 b2 = ly_ctx_load_module(ctx, "b", "2015-04-04");
148 assert_ptr_not_equal(b2, NULL);
149 assert_int_equal(b2->implemented, 1);
150
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200151 /* loads a.yin (impl), b@2015-04-04 is augmented by a, but cannot be implemented */
Pavol Vican5c186c22016-09-27 10:46:14 +0200152 a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG);
Michal Vaskoa51f3ad2017-05-23 10:54:37 +0200153 assert_ptr_equal(a, NULL);
Pavol Vican5c186c22016-09-27 10:46:14 +0200154}
155
156static void
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200157test_implemented_info_yin(void **state)
158{
159 struct ly_ctx *ctx = *state;
160 struct lyd_node *info;
161 const struct lys_module *a;
162 char *data;
Radek Krejcidfb00d62017-09-06 09:39:35 +0200163 const char *template = "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
Michal Vasko78901162018-01-26 12:36:46 +0100164" <module-set>\n"
165" <name>complete</name>\n"
166" <checksum>10</checksum>\n"
167" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200168" <name>ietf-yang-metadata</name>\n"
169" <revision>2016-08-05</revision>\n"
170" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100171" </import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200172" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200173" <name>yang</name>\n"
174" <revision>2017-02-20</revision>\n"
175" <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200176" </module>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100177" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200178" <name>ietf-inet-types</name>\n"
179" <revision>2013-07-15</revision>\n"
180" <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100181" </import-only-module>\n"
182" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200183" <name>ietf-yang-types</name>\n"
184" <revision>2013-07-15</revision>\n"
185" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100186" </import-only-module>\n"
187" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200188" <name>ietf-datastores</name>\n"
189" <revision>2017-08-17</revision>\n"
190" <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100191" </import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200192" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200193" <name>ietf-yang-library</name>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100194" <revision>2018-01-17</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200195" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200196" </module>\n"
197" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200198" <name>b</name>\n"
199" <revision>2015-01-01</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200200" <namespace>urn:example:b</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100201" <location>file://"SCHEMA_FOLDER_YIN"/b@2015-01-01.yin</location>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200202" </module>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100203" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200204" <name>c</name>\n"
205" <revision>2015-03-03</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200206" <namespace>urn:example:c</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100207" <location>file://"SCHEMA_FOLDER_YIN"/c@2015-03-03.yin</location>\n"
208" </import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200209" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200210" <name>a</name>\n"
211" <revision>2015-01-01</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200212" <namespace>urn:example:a</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100213" <location>file://"SCHEMA_FOLDER_YIN"/a.yin</location>\n"
214" <feature>\n"
215" <name>foo</name>\n"
216" </feature>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200217" </module>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100218" </module-set>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200219" <checksum>10</checksum>\n"
220"</yang-library>\n"
221"<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200222" <module>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100223" <name>ietf-yang-metadata</name>\n"
224" <revision>2016-08-05</revision>\n"
225" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
226" <conformance-type>import</conformance-type>\n"
227" </module>\n"
228" <module>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200229" <name>yang</name>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100230" <revision>2017-02-20</revision>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200231" <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
232" <conformance-type>implement</conformance-type>\n"
233" </module>\n"
234" <module>\n"
235" <name>ietf-inet-types</name>\n"
236" <revision>2013-07-15</revision>\n"
237" <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
238" <conformance-type>import</conformance-type>\n"
239" </module>\n"
240" <module>\n"
241" <name>ietf-yang-types</name>\n"
242" <revision>2013-07-15</revision>\n"
243" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
244" <conformance-type>import</conformance-type>\n"
245" </module>\n"
246" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200247" <name>ietf-datastores</name>\n"
248" <revision>2017-08-17</revision>\n"
249" <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
Radek Krejcib39dfce2017-09-12 14:28:00 +0200250" <conformance-type>import</conformance-type>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200251" </module>\n"
252" <module>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200253" <name>ietf-yang-library</name>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100254" <revision>2018-01-17</revision>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200255" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
256" <conformance-type>implement</conformance-type>\n"
257" </module>\n"
258" <module>\n"
259" <name>b</name>\n"
260" <revision>2015-01-01</revision>\n"
261" <schema>file://"SCHEMA_FOLDER_YIN"/b@2015-01-01.yin</schema>\n"
262" <namespace>urn:example:b</namespace>\n"
263" <conformance-type>implement</conformance-type>\n"
264" </module>\n"
265" <module>\n"
266" <name>c</name>\n"
267" <revision>2015-03-03</revision>\n"
268" <schema>file://"SCHEMA_FOLDER_YIN"/c@2015-03-03.yin</schema>\n"
269" <namespace>urn:example:c</namespace>\n"
270" <conformance-type>import</conformance-type>\n"
271" </module>\n"
272" <module>\n"
273" <name>a</name>\n"
274" <revision>2015-01-01</revision>\n"
275" <schema>file://"SCHEMA_FOLDER_YIN"/a.yin</schema>\n"
276" <namespace>urn:example:a</namespace>\n"
277" <feature>foo</feature>\n"
278" <conformance-type>implement</conformance-type>\n"
279" </module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200280" <module-set-id>10</module-set-id>\n"
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200281"</modules-state>\n";
282
283 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN);
284
285 /* loads a.yin (impl), b@2015-01-01.yin (impl by augment) and c@2015-03-03.yin (imp) */
286 assert_ptr_not_equal((a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN)), NULL);
287 assert_int_equal(lys_features_enable(a, "foo"), 0);
288
289 /* get yang-library data */
290 info = ly_ctx_info(ctx);
291 assert_ptr_not_equal(info, NULL);
292
Radek Krejcidfb00d62017-09-06 09:39:35 +0200293 lyd_print_mem(&data, info, LYD_XML, LYP_FORMAT | LYP_WITHSIBLINGS);
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200294 lyd_free_withsiblings(info);
295 assert_string_equal(data, template);
296 free(data);
297}
298
Pavol Vican5c186c22016-09-27 10:46:14 +0200299static void
300test_implemented_info_yang(void **state)
301{
302 struct ly_ctx *ctx = *state;
303 struct lyd_node *info;
304 const struct lys_module *a;
305 char *data;
Radek Krejcidfb00d62017-09-06 09:39:35 +0200306 const char *template = "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
Michal Vasko78901162018-01-26 12:36:46 +0100307" <module-set>\n"
308" <name>complete</name>\n"
309" <checksum>10</checksum>\n"
310" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200311" <name>ietf-yang-metadata</name>\n"
312" <revision>2016-08-05</revision>\n"
313" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100314" </import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200315" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200316" <name>yang</name>\n"
317" <revision>2017-02-20</revision>\n"
318" <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200319" </module>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100320" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200321" <name>ietf-inet-types</name>\n"
322" <revision>2013-07-15</revision>\n"
323" <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100324" </import-only-module>\n"
325" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200326" <name>ietf-yang-types</name>\n"
327" <revision>2013-07-15</revision>\n"
328" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100329" </import-only-module>\n"
330" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200331" <name>ietf-datastores</name>\n"
332" <revision>2017-08-17</revision>\n"
333" <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100334" </import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200335" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200336" <name>ietf-yang-library</name>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100337" <revision>2018-01-17</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200338" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200339" </module>\n"
340" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200341" <name>b</name>\n"
342" <revision>2015-01-01</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200343" <namespace>urn:example:b</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100344" <location>file://"SCHEMA_FOLDER_YANG"/b@2015-01-01.yang</location>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200345" </module>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100346" <import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200347" <name>c</name>\n"
348" <revision>2015-03-03</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200349" <namespace>urn:example:c</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100350" <location>file://"SCHEMA_FOLDER_YANG"/c@2015-03-03.yang</location>\n"
351" </import-only-module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200352" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200353" <name>a</name>\n"
354" <revision>2015-01-01</revision>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200355" <namespace>urn:example:a</namespace>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100356" <location>file://"SCHEMA_FOLDER_YANG"/a.yang</location>\n"
357" <feature>\n"
358" <name>foo</name>\n"
359" </feature>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200360" </module>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100361" </module-set>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200362" <checksum>10</checksum>\n"
363"</yang-library>\n"
364"<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200365" <module>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100366" <name>ietf-yang-metadata</name>\n"
367" <revision>2016-08-05</revision>\n"
368" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
369" <conformance-type>import</conformance-type>\n"
370" </module>\n"
371" <module>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200372" <name>yang</name>\n"
Radek Krejci532e5e92017-02-22 12:59:24 +0100373" <revision>2017-02-20</revision>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200374" <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
375" <conformance-type>implement</conformance-type>\n"
376" </module>\n"
377" <module>\n"
378" <name>ietf-inet-types</name>\n"
379" <revision>2013-07-15</revision>\n"
380" <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
381" <conformance-type>import</conformance-type>\n"
382" </module>\n"
383" <module>\n"
384" <name>ietf-yang-types</name>\n"
385" <revision>2013-07-15</revision>\n"
386" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
387" <conformance-type>import</conformance-type>\n"
388" </module>\n"
389" <module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200390" <name>ietf-datastores</name>\n"
391" <revision>2017-08-17</revision>\n"
392" <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
Radek Krejcib39dfce2017-09-12 14:28:00 +0200393" <conformance-type>import</conformance-type>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200394" </module>\n"
395" <module>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200396" <name>ietf-yang-library</name>\n"
Michal Vasko78901162018-01-26 12:36:46 +0100397" <revision>2018-01-17</revision>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200398" <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
399" <conformance-type>implement</conformance-type>\n"
400" </module>\n"
401" <module>\n"
402" <name>b</name>\n"
403" <revision>2015-01-01</revision>\n"
404" <schema>file://"SCHEMA_FOLDER_YANG"/b@2015-01-01.yang</schema>\n"
405" <namespace>urn:example:b</namespace>\n"
406" <conformance-type>implement</conformance-type>\n"
407" </module>\n"
408" <module>\n"
409" <name>c</name>\n"
410" <revision>2015-03-03</revision>\n"
411" <schema>file://"SCHEMA_FOLDER_YANG"/c@2015-03-03.yang</schema>\n"
412" <namespace>urn:example:c</namespace>\n"
413" <conformance-type>import</conformance-type>\n"
414" </module>\n"
415" <module>\n"
416" <name>a</name>\n"
417" <revision>2015-01-01</revision>\n"
418" <schema>file://"SCHEMA_FOLDER_YANG"/a.yang</schema>\n"
419" <namespace>urn:example:a</namespace>\n"
420" <feature>foo</feature>\n"
421" <conformance-type>implement</conformance-type>\n"
422" </module>\n"
Radek Krejcidfb00d62017-09-06 09:39:35 +0200423" <module-set-id>10</module-set-id>\n"
Pavol Vican5c186c22016-09-27 10:46:14 +0200424"</modules-state>\n";
425
426 ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG);
427
428 /* loads a.yang (impl), b@2015-01-01.yang (impl by augment) and c@2015-03-03.yang (imp) */
429 assert_ptr_not_equal((a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG)), NULL);
430 assert_int_equal(lys_features_enable(a, "foo"), 0);
431
432 /* get yang-library data */
433 info = ly_ctx_info(ctx);
434 assert_ptr_not_equal(info, NULL);
435
Radek Krejcidfb00d62017-09-06 09:39:35 +0200436 lyd_print_mem(&data, info, LYD_XML, LYP_FORMAT | LYP_WITHSIBLINGS);
Pavol Vican5c186c22016-09-27 10:46:14 +0200437 lyd_free_withsiblings(info);
438 assert_string_equal(data, template);
439 free(data);
440}
441
Radek Krejci43c49832018-03-21 09:55:41 +0100442static void
443test_revision_date_yin(void **state)
444{
445 struct ly_ctx *ctx = *state;
446 const char *yin1 = "<module name=\"x\""
447 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
448 "xmlns:x=\"urn:cesnet:x\">"
449 "<namespace uri=\"urn:cesnet:x\"/>"
450 "<prefix value=\"x\"/>"
451 "<revision date=\"2018-02-29\"/>"
452 "</module>";
453 const char *yin2 = "<module name=\"x\""
454 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
455 "xmlns:x=\"urn:cesnet:x\">"
456 "<namespace uri=\"urn:cesnet:x\"/>"
457 "<prefix value=\"x\"/>"
458 "<revision date=\"18-02-28\"/>"
459 "</module>";
460 const char *yin3 = "<module name=\"x\""
461 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
462 "xmlns:x=\"urn:cesnet:x\">"
463 "<namespace uri=\"urn:cesnet:x\"/>"
464 "<prefix value=\"x\"/>"
465 "<revision date=\"today\"/>"
466 "</module>";
467 const char *yin4 = "<module name=\"x\""
468 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
469 "xmlns:x=\"urn:cesnet:x\">"
470 "<namespace uri=\"urn:cesnet:x\"/>"
471 "<prefix value=\"x\"/>"
472 "<revision date=\"2018-02-28\"/>"
473 "</module>";
474 const char *yin5 = "<module name=\"y\""
475 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
476 "xmlns:y=\"urn:cesnet:y\">"
477 "<namespace uri=\"urn:cesnet:y\"/>"
478 "<prefix value=\"y\"/>"
479 "<revision date=\"2016-02-29\"/>"
480 "</module>";
481 const char *yin6 = "<module name=\"z\""
482 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
483 "xmlns:z=\"urn:cesnet:z\">"
484 "<namespace uri=\"urn:cesnet:z\"/>"
485 "<prefix value=\"z\"/>"
486 "<revision date=\"2000-02-29\"/>"
487 "</module>";
488
489 /* invalid dates */
490 assert_ptr_equal(lys_parse_mem(ctx, yin1, LYS_IN_YIN), NULL);
491 assert_int_equal(ly_vecode(ctx), LYVE_INDATE);
492
493 assert_ptr_equal(lys_parse_mem(ctx, yin2, LYS_IN_YIN), NULL);
494 assert_int_equal(ly_vecode(ctx), LYVE_INDATE);
495
496 assert_ptr_equal(lys_parse_mem(ctx, yin3, LYS_IN_YIN), NULL);
497 assert_int_equal(ly_vecode(ctx), LYVE_INDATE);
498
499 /* valid dates */
500 assert_ptr_not_equal(lys_parse_mem(ctx, yin4, LYS_IN_YIN), NULL);
501 assert_ptr_not_equal(lys_parse_mem(ctx, yin5, LYS_IN_YIN), NULL);
502 assert_ptr_not_equal(lys_parse_mem(ctx, yin6, LYS_IN_YIN), NULL);
503}
504
505static void
506test_revision_date_yang(void **state)
507{
508 struct ly_ctx *ctx = *state;
509 const char *yang1 = "module x {"
510 "namespace urn:cesnet:x;"
511 "prefix x;"
512 "revision \"2018-02-29\";}";
513 const char *yang2 = "module x {"
514 "namespace urn:cesnet:x;"
515 "prefix x;"
516 "revision \"18-02-28\";}";
517 const char *yang3 = "module x {"
518 "namespace urn:cesnet:x;"
519 "prefix x;"
520 "revision \"today\";}";
521 const char *yang4 = "module x {"
522 "namespace urn:cesnet:x;"
523 "prefix x;"
524 "revision \"2018-02-28\";}";
525 const char *yang5 = "module y {"
526 "namespace urn:cesnet:y;"
527 "prefix y;"
528 "revision \"2016-02-29\";}";
529 const char *yang6 = "module z {"
530 "namespace urn:cesnet:z;"
531 "prefix z;"
532 "revision \"2000-02-29\";}";
533
534 /* invalid dates */
535 assert_ptr_equal(lys_parse_mem(ctx, yang1, LYS_IN_YANG), NULL);
536 assert_int_equal(ly_vecode(ctx), LYVE_INDATE);
537
538 assert_ptr_equal(lys_parse_mem(ctx, yang2, LYS_IN_YANG), NULL);
539 assert_int_equal(ly_vecode(ctx), LYVE_INDATE);
540
541 assert_ptr_equal(lys_parse_mem(ctx, yang3, LYS_IN_YANG), NULL);
542 assert_int_equal(ly_vecode(ctx), LYVE_INDATE);
543
544 /* valid dates */
545 assert_ptr_not_equal(lys_parse_mem(ctx, yang4, LYS_IN_YANG), NULL);
546 assert_ptr_not_equal(lys_parse_mem(ctx, yang5, LYS_IN_YANG), NULL);
547 assert_ptr_not_equal(lys_parse_mem(ctx, yang6, LYS_IN_YANG), NULL);
548}
549
Peter Feige8deb9a02018-06-19 05:29:42 +0000550
551const struct lys_module *
552_my_data_clb(struct ly_ctx *ctx, const char *name, const char *ns, int options, void *user_data)
553{
554 char filepath[256] = {0};
555 const struct lys_module *ly_module = NULL;
556
557 fprintf(stderr, "%s:%i %s() name:%s ns:%s \n", __FILE__, __LINE__, __FUNCTION__, name, ns);
558
559 snprintf(filepath, sizeof(filepath), "%s/%s.yang", SCHEMA_FOLDER_YANG, name);
560 ly_module = lys_parse_path(ctx, filepath, LYS_IN_YANG);
561 if ( !ly_module ) {
562 fprintf(stderr, "%s:%i %s() lys_parse_path(%s) failed (%d %p)\n", __FILE__, __LINE__, __FUNCTION__, filepath, options, user_data);
563 }
564
565 return ly_module;
566}
567
568static void
569test_issue_already_implemented(void **state) {
570 struct ly_ctx *ctx = *state;
571 const struct lys_module *a = NULL;
572 char* search_paths[] = {SCHEMA_FOLDER_YANG, NULL};
573
574 ctx->models.search_paths = search_paths;
575 ctx->data_clb = _my_data_clb;
576
577 a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG "/ident-aug-must-issue-apst.yang", LYS_IN_YANG);
578
579 ctx->data_clb = NULL;
580 ctx->models.search_paths = NULL;
581
582 assert_ptr_not_equal(a, NULL);
583
584}
585
586
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200587int
588main(void)
589{
590 const struct CMUnitTest cmut[] = {
591 cmocka_unit_test_setup_teardown(test_implemented1_yin, setup_ctx, teardown_ctx),
Pavol Vican5c186c22016-09-27 10:46:14 +0200592 cmocka_unit_test_setup_teardown(test_implemented1_yang, setup_ctx, teardown_ctx),
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200593 cmocka_unit_test_setup_teardown(test_implemented2_yin, setup_ctx, teardown_ctx),
Pavol Vican5c186c22016-09-27 10:46:14 +0200594 cmocka_unit_test_setup_teardown(test_implemented2_yang, setup_ctx, teardown_ctx),
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200595 cmocka_unit_test_setup_teardown(test_implemented_info_yin, setup_ctx, teardown_ctx),
Pavol Vican5c186c22016-09-27 10:46:14 +0200596 cmocka_unit_test_setup_teardown(test_implemented_info_yang, setup_ctx, teardown_ctx),
Radek Krejci43c49832018-03-21 09:55:41 +0100597 cmocka_unit_test_setup_teardown(test_revision_date_yin, setup_ctx, teardown_ctx),
598 cmocka_unit_test_setup_teardown(test_revision_date_yang, setup_ctx, teardown_ctx),
Peter Feige8deb9a02018-06-19 05:29:42 +0000599 cmocka_unit_test_setup_teardown(test_issue_already_implemented, setup_ctx, teardown_ctx)
Radek Krejci1a4a1c82016-09-14 16:07:25 +0200600 };
601
602 return cmocka_run_group_tests(cmut, NULL, NULL);
603}