blob: f27e16821dc5a6f48251fb392967dbd8ad006133 [file] [log] [blame]
Michal Vaskoddd76592022-01-17 13:34:48 +01001/**
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01002 * @file test_schema_mount.c
Michal Vaskoddd76592022-01-17 13:34:48 +01003 * @author Tadeas Vintrlik <xvintr04@stud.fit.vutbr.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01005 * @brief unit tests for Schema Mount extension support
6 *
Michal Vaskoddd76592022-01-17 13:34:48 +01007 * Copyright (c) 2021 - 2022 CESNET, z.s.p.o.
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Michal Vaskoddd76592022-01-17 13:34:48 +010015
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010016#define _UTEST_MAIN_
17#include "utests.h"
18
19#include "libyang.h"
20
Michal Vaskoddd76592022-01-17 13:34:48 +010021void **glob_state;
22
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010023static int
24setup(void **state)
25{
Michal Vaskoddd76592022-01-17 13:34:48 +010026 const char *schema =
27 "module sm {yang-version 1.1;namespace \"urn:sm\";prefix \"sm\";"
28 "import ietf-yang-schema-mount {prefix yangmnt;}"
29 "import ietf-interfaces {prefix if;}"
30 "container root {yangmnt:mount-point \"root\";}"
31 "container root2 {yangmnt:mount-point \"root\";}"
32 "container root3 {"
33 " list ls { key name; leaf name {type string;}"
Michal Vasko61ad1ff2022-02-10 15:48:39 +010034 " yangmnt:mount-point \"mnt-root\";"
Michal Vaskoddd76592022-01-17 13:34:48 +010035 " }"
36 "}"
Michal Vasko9e12ffe2022-09-21 15:11:30 +020037 "container root4 {config false; yangmnt:mount-point \"root\";}"
Michal Vaskoddd76592022-01-17 13:34:48 +010038 "leaf target{type string;}"
39 "augment /if:interfaces/if:interface {"
40 " leaf sm-name {type leafref {path \"/sm:target\";}}"
41 "}"
42 "}";
43
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010044 UTEST_SETUP;
Michal Vaskoddd76592022-01-17 13:34:48 +010045 glob_state = state;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010046
47 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Michal Vaskoddd76592022-01-17 13:34:48 +010048 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, NULL));
49 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "iana-if-type", NULL, NULL));
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010050
51 return 0;
52}
53
54static void
Michal Vaskoddd76592022-01-17 13:34:48 +010055test_schema(void **state)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010056{
57 struct lys_module *mod;
Michal Vaskoddd76592022-01-17 13:34:48 +010058 const char *schema;
59 char *str;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010060
Michal Vaskoddd76592022-01-17 13:34:48 +010061 /* invalid */
62 schema =
63 "module sm {\n"
64 " namespace \"urn:sm\";\n"
65 " prefix sm;\n"
66 "\n"
67 " import ietf-yang-schema-mount {\n"
68 " prefix yangmnt;\n"
69 " }\n"
70 "\n"
71 " container root {\n"
72 " yangmnt:mount-point \"root\";\n"
73 " }\n"
74 "}\n";
75 assert_int_equal(LY_EINVAL, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, NULL));
Michal Vasko193dacd2022-10-13 08:43:05 +020076 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vaskoddd76592022-01-17 13:34:48 +010077 "Extension \"yangmnt:mount-point\" instance not allowed in YANG version 1 module.",
Michal Vasko7a266772024-01-23 11:02:38 +010078 "/sm:root/{extension='yangmnt:mount-point'}/root", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +010079
80 schema =
81 "module sm {\n"
82 " yang-version 1.1;\n"
83 " namespace \"urn:sm\";\n"
84 " prefix sm;\n"
85 "\n"
86 " import ietf-yang-schema-mount {\n"
87 " prefix yangmnt;\n"
88 " }\n"
89 "\n"
90 " yangmnt:mount-point \"root\";\n"
91 "}\n";
92 assert_int_equal(LY_EINVAL, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, NULL));
Michal Vasko193dacd2022-10-13 08:43:05 +020093 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vaskoddd76592022-01-17 13:34:48 +010094 "Extension \"yangmnt:mount-point\" instance allowed only in container or list statement.",
Michal Vasko7a266772024-01-23 11:02:38 +010095 "/sm:{extension='yangmnt:mount-point'}/root", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +010096
97 schema =
98 "module sm {\n"
99 " yang-version 1.1;\n"
100 " namespace \"urn:sm\";\n"
101 " prefix sm;\n"
102 "\n"
103 " import ietf-yang-schema-mount {\n"
104 " prefix yangmnt;\n"
105 " }\n"
106 "\n"
107 " container root {\n"
108 " leaf l {\n"
109 " type empty;\n"
110 " yangmnt:mount-point \"root\";\n"
111 " }\n"
112 " }\n"
113 "}\n";
114 assert_int_equal(LY_EINVAL, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, NULL));
Michal Vasko193dacd2022-10-13 08:43:05 +0200115 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vaskoddd76592022-01-17 13:34:48 +0100116 "Extension \"yangmnt:mount-point\" instance allowed only in container or list statement.",
Michal Vasko7a266772024-01-23 11:02:38 +0100117 "/sm:root/l/{extension='yangmnt:mount-point'}/root", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100118
119 schema =
120 "module sm {\n"
121 " yang-version 1.1;\n"
122 " namespace \"urn:sm\";\n"
123 " prefix sm;\n"
124 "\n"
125 " import ietf-yang-schema-mount {\n"
126 " prefix yangmnt;\n"
127 " }\n"
128 "\n"
129 " list l {\n"
130 " key \"k\";\n"
131 " leaf k {\n"
132 " type string;\n"
133 " }\n"
134 " yangmnt:mount-point \"root\";\n"
135 " yangmnt:mount-point \"root2\";\n"
136 " }\n"
137 "}\n";
138 assert_int_equal(LY_EINVAL, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, NULL));
Michal Vasko193dacd2022-10-13 08:43:05 +0200139 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vaskoddd76592022-01-17 13:34:48 +0100140 "Multiple extension \"yangmnt:mount-point\" instances.",
Michal Vasko7a266772024-01-23 11:02:38 +0100141 "/sm:l/{extension='yangmnt:mount-point'}/root", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100142
143 /* valid */
144 schema =
145 "module sm {\n"
146 " yang-version 1.1;\n"
147 " namespace \"urn:sm\";\n"
148 " prefix sm;\n"
149 "\n"
150 " import ietf-yang-schema-mount {\n"
151 " prefix yangmnt;\n"
152 " }\n"
153 "\n"
154 " container root {\n"
155 " yangmnt:mount-point \"root\";\n"
156 " }\n"
157 "}\n";
158 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, schema, LYS_IN_YANG, &mod));
Michal Vasko193dacd2022-10-13 08:43:05 +0200159 lys_print_mem(&str, mod, LYS_OUT_YANG, 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100160 assert_string_equal(str, schema);
161 free(str);
162}
163
164static LY_ERR
165test_ext_data_clb(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, ly_bool *ext_data_free)
166{
167 void **state = glob_state;
168 struct lyd_node *data = NULL;
169
170 (void)ext;
171
172 if (user_data) {
173 CHECK_PARSE_LYD_PARAM(user_data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
174 }
175
176 *ext_data = data;
177 *ext_data_free = 1;
178 return LY_SUCCESS;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100179}
180
181static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100182test_parse_invalid(void **state)
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100183{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100184 const char *xml, *json;
Michal Vaskoddd76592022-01-17 13:34:48 +0100185 struct lyd_node *data;
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +0100186
Michal Vaskoddd76592022-01-17 13:34:48 +0100187 /* no callback set */
188 xml =
189 "<root xmlns=\"urn:sm\">"
190 " <unknown xmlns=\"unknown\">"
191 " <interface>"
192 " <name>bu</name>"
193 " <type xmlns:ii=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ii:ethernetCsmacd</type>"
194 " </interface>"
195 " </unknown>"
196 "</root>";
197 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EINVAL, data);
Michal Vasko193dacd2022-10-13 08:43:05 +0200198 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": Failed to get extension data, no callback set.",
Michal Vasko7a266772024-01-23 11:02:38 +0100199 NULL, 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100200
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100201 json =
202 "{"
203 " \"sm:root\": {"
204 " \"unknown:unknown\": {"
205 " \"interface\": ["
206 " {"
207 " \"name\": \"bu\","
208 " \"type\": \"iana-if-type:ethernetCsmacd\""
209 " }"
210 " ]"
211 " }"
212 " }"
213 "}";
214 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EINVAL, data);
Michal Vasko193dacd2022-10-13 08:43:05 +0200215 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": Failed to get extension data, no callback set.",
Michal Vasko7a266772024-01-23 11:02:38 +0100216 NULL, 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100217
Michal Vaskoddd76592022-01-17 13:34:48 +0100218 /* unknown data */
219 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb, NULL);
220 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
221 assert_string_equal(LYD_NAME(data), "root");
222 assert_null(lyd_child(data));
223 assert_non_null(data->next);
224 assert_true(data->next->flags & LYD_DEFAULT);
225 lyd_free_siblings(data);
226
227 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100228 CHECK_LOG_CTX("No module with namespace \"unknown\" in the context.", "/sm:root", 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100229
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100230 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
231 assert_string_equal(LYD_NAME(data), "root");
232 assert_null(lyd_child(data));
233 assert_non_null(data->next);
234 assert_true(data->next->flags & LYD_DEFAULT);
235 lyd_free_siblings(data);
236
237 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100238 CHECK_LOG_CTX("No module named \"unknown\" in the context.", "/sm:root", 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100239
Michal Vaskoddd76592022-01-17 13:34:48 +0100240 /* missing required callback data */
241 xml =
242 "<root xmlns=\"urn:sm\">"
243 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">"
244 " <interface>"
245 " <name>bu</name>"
246 " </interface>"
247 " </interfaces>"
248 "</root>";
249 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100250 CHECK_LOG_CTX("Node \"interfaces\" not found as a child of \"root\" node.", "/sm:root", 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100251
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100252 json =
253 "{"
254 " \"sm:root\": {"
255 " \"ietf-interfaces:interfaces\": {"
256 " \"interface\": ["
257 " {"
258 " \"name\": \"bu\""
259 " }"
260 " ]"
261 " }"
262 " }"
263 "}";
264 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100265 CHECK_LOG_CTX("Node \"interfaces\" not found as a child of \"root\" node.", "/sm:root", 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100266
Michal Vaskoddd76592022-01-17 13:34:48 +0100267 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
268 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
269 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
270 " <module-set>"
271 " <name>test-set</name>"
272 " <module>"
273 " <name>ietf-yang-library</name>"
274 " <revision>2019-01-04</revision>"
275 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
276 " </module>"
277 " </module-set>"
278 " <schema>"
279 " <name>test-schema</name>"
280 " <module-set>test-set</module-set>"
281 " </schema>"
282 " <datastore>"
283 " <name>ds:running</name>"
284 " <schema>test-schema</schema>"
285 " </datastore>"
286 " <datastore>"
287 " <name>ds:operational</name>"
288 " <schema>test-schema</schema>"
289 " </datastore>"
290 " <content-id>1</content-id>"
291 "</yang-library>"
292 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
293 " <module-set-id>1</module-set-id>"
294 "</modules-state>");
295 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100296 CHECK_LOG_CTX("Node \"interfaces\" not found as a child of \"root\" node.", "/sm:root", 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100298 CHECK_LOG_CTX("Node \"interfaces\" not found as a child of \"root\" node.", "/sm:root", 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100299
300 /* missing module in yang-library data */
301 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
302 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
303 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
304 " <module-set>"
305 " <name>test-set</name>"
306 " <module>"
307 " <name>ietf-yang-library</name>"
308 " <revision>2019-01-04</revision>"
309 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
310 " </module>"
311 " </module-set>"
312 " <schema>"
313 " <name>test-schema</name>"
314 " <module-set>test-set</module-set>"
315 " </schema>"
316 " <datastore>"
317 " <name>ds:running</name>"
318 " <schema>test-schema</schema>"
319 " </datastore>"
320 " <datastore>"
321 " <name>ds:operational</name>"
322 " <schema>test-schema</schema>"
323 " </datastore>"
324 " <content-id>1</content-id>"
325 "</yang-library>"
326 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
327 " <module-set-id>1</module-set-id>"
328 "</modules-state>"
329 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
330 " <mount-point>"
331 " <module>sm</module>"
332 " <label>root</label>"
333 " <inline/>"
334 " </mount-point>"
335 "</schema-mounts>");
336 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100337 CHECK_LOG_CTX("Node \"interfaces\" not found as a child of \"root\" node.", "/sm:root", 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100338 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100339 CHECK_LOG_CTX("Node \"interfaces\" not found as a child of \"root\" node.", "/sm:root", 1);
Michal Vaskoddd76592022-01-17 13:34:48 +0100340
341 /* callback data correct, invalid YANG data */
342 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
343 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
344 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
345 " <module-set>"
346 " <name>test-set</name>"
347 " <module>"
348 " <name>ietf-datastores</name>"
349 " <revision>2018-02-14</revision>"
350 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
351 " </module>"
352 " <module>"
353 " <name>ietf-yang-library</name>"
354 " <revision>2019-01-04</revision>"
355 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
356 " </module>"
357 " <module>"
358 " <name>ietf-yang-schema-mount</name>"
359 " <revision>2019-01-14</revision>"
360 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
361 " </module>"
362 " <module>"
363 " <name>ietf-interfaces</name>"
364 " <revision>2014-05-08</revision>"
365 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
366 " </module>"
367 " <module>"
368 " <name>iana-if-type</name>"
369 " <revision>2014-05-08</revision>"
370 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
371 " </module>"
372 " <import-only-module>"
373 " <name>ietf-yang-types</name>"
374 " <revision>2013-07-15</revision>"
375 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
376 " </import-only-module>"
377 " </module-set>"
378 " <schema>"
379 " <name>test-schema</name>"
380 " <module-set>test-set</module-set>"
381 " </schema>"
382 " <datastore>"
383 " <name>ds:running</name>"
384 " <schema>test-schema</schema>"
385 " </datastore>"
386 " <datastore>"
387 " <name>ds:operational</name>"
388 " <schema>test-schema</schema>"
389 " </datastore>"
390 " <content-id>1</content-id>"
391 "</yang-library>"
392 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
393 " <module-set-id>1</module-set-id>"
394 "</modules-state>"
395 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
396 " <mount-point>"
397 " <module>sm</module>"
398 " <label>root</label>"
399 " <inline/>"
400 " </mount-point>"
401 "</schema-mounts>");
402 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100403 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": Mandatory node \"type\" instance does not exist.",
404 "/ietf-interfaces:interfaces/interface[name='bu']", 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100405 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko7a266772024-01-23 11:02:38 +0100406 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": Mandatory node \"type\" instance does not exist.",
407 "/ietf-interfaces:interfaces/interface[name='bu']", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100408
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100409 /* same validation fail in separate validation */
Michal Vaskoddd76592022-01-17 13:34:48 +0100410 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, LY_SUCCESS, data);
411 assert_int_equal(LY_EVALID, lyd_validate_all(&data, NULL, LYD_VALIDATE_PRESENT, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +0100412 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": Mandatory node \"type\" instance does not exist.",
413 "/ietf-interfaces:interfaces/interface[name='bu']", 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100414 lyd_free_siblings(data);
415
416 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT | LYD_PARSE_ONLY, 0, LY_SUCCESS, data);
417 assert_int_equal(LY_EVALID, lyd_validate_all(&data, NULL, LYD_VALIDATE_PRESENT, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +0100418 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": Mandatory node \"type\" instance does not exist.",
419 "/ietf-interfaces:interfaces/interface[name='bu']", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100420 lyd_free_siblings(data);
421
422 /* success */
423 xml =
424 "<root xmlns=\"urn:sm\">\n"
425 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
426 " <interface>\n"
427 " <name>bu</name>\n"
428 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
429 " </interface>\n"
430 " </interfaces>\n"
431 "</root>\n";
432 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
433 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
434 lyd_free_siblings(data);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100435
436 json =
437 "{\n"
438 " \"sm:root\": {\n"
439 " \"ietf-interfaces:interfaces\": {\n"
440 " \"interface\": [\n"
441 " {\n"
442 " \"name\": \"bu\",\n"
443 " \"type\": \"iana-if-type:ethernetCsmacd\"\n"
444 " }\n"
445 " ]\n"
446 " }\n"
447 " }\n"
448 "}\n";
449 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
450 CHECK_LYD_STRING_PARAM(data, json, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
451 lyd_free_siblings(data);
Michal Vaskoddd76592022-01-17 13:34:48 +0100452}
453
454static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100455test_parse_inline(void **state)
Michal Vaskoddd76592022-01-17 13:34:48 +0100456{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100457 const char *xml, *json;
Michal Vasko9883a3e2022-03-31 12:16:00 +0200458 char *lyb;
Michal Vaskoddd76592022-01-17 13:34:48 +0100459 struct lyd_node *data;
Michal Vasko27653162022-11-21 13:58:44 +0100460 const struct ly_ctx *ext_ctx;
Michal Vaskoddd76592022-01-17 13:34:48 +0100461
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100462 /* valid */
Michal Vaskoddd76592022-01-17 13:34:48 +0100463 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
464 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
465 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
466 " <module-set>"
467 " <name>test-set</name>"
468 " <module>"
469 " <name>ietf-datastores</name>"
470 " <revision>2018-02-14</revision>"
471 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
472 " </module>"
473 " <module>"
474 " <name>ietf-yang-library</name>"
475 " <revision>2019-01-04</revision>"
476 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
477 " </module>"
478 " <module>"
479 " <name>ietf-yang-schema-mount</name>"
480 " <revision>2019-01-14</revision>"
481 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
482 " </module>"
483 " <module>"
484 " <name>ietf-interfaces</name>"
485 " <revision>2014-05-08</revision>"
486 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
487 " </module>"
488 " <module>"
489 " <name>iana-if-type</name>"
490 " <revision>2014-05-08</revision>"
491 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
492 " </module>"
493 " <import-only-module>"
494 " <name>ietf-yang-types</name>"
495 " <revision>2013-07-15</revision>"
496 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
497 " </import-only-module>"
498 " </module-set>"
499 " <schema>"
500 " <name>test-schema</name>"
501 " <module-set>test-set</module-set>"
502 " </schema>"
503 " <datastore>"
504 " <name>ds:running</name>"
505 " <schema>test-schema</schema>"
506 " </datastore>"
507 " <datastore>"
508 " <name>ds:operational</name>"
509 " <schema>test-schema</schema>"
510 " </datastore>"
511 " <content-id>1</content-id>"
512 "</yang-library>"
513 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
514 " <module-set-id>1</module-set-id>"
515 "</modules-state>"
516 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
517 " <mount-point>"
518 " <module>sm</module>"
519 " <label>root</label>"
520 " <inline/>"
521 " </mount-point>"
522 "</schema-mounts>");
523 xml =
524 "<root xmlns=\"urn:sm\">\n"
525 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
526 " <interface>\n"
527 " <name>bu</name>\n"
528 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
529 " </interface>\n"
530 " </interfaces>\n"
531 " <interfaces-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
532 " <interface>\n"
533 " <name>bu</name>\n"
534 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
535 " <oper-status>not-present</oper-status>\n"
536 " <statistics>\n"
537 " <discontinuity-time>2022-01-01T10:00:00-00:00</discontinuity-time>\n"
538 " </statistics>\n"
539 " </interface>\n"
540 " </interfaces-state>\n"
541 "</root>\n";
542 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
543 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vasko27653162022-11-21 13:58:44 +0100544 ext_ctx = LYD_CTX(lyd_child(data));
Michal Vaskoddd76592022-01-17 13:34:48 +0100545 lyd_free_siblings(data);
546
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100547 json =
548 "{\n"
549 " \"sm:root\": {\n"
550 " \"ietf-interfaces:interfaces\": {\n"
551 " \"interface\": [\n"
552 " {\n"
553 " \"name\": \"bu\",\n"
554 " \"type\": \"iana-if-type:ethernetCsmacd\"\n"
555 " }\n"
556 " ]\n"
557 " },\n"
558 " \"ietf-interfaces:interfaces-state\": {\n"
559 " \"interface\": [\n"
560 " {\n"
561 " \"name\": \"bu\",\n"
562 " \"type\": \"iana-if-type:ethernetCsmacd\",\n"
563 " \"oper-status\": \"not-present\",\n"
564 " \"statistics\": {\n"
565 " \"discontinuity-time\": \"2022-01-01T10:00:00-00:00\"\n"
566 " }\n"
567 " }\n"
568 " ]\n"
569 " }\n"
570 " }\n"
571 "}\n";
572 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
573 CHECK_LYD_STRING_PARAM(data, json, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
Michal Vasko27653162022-11-21 13:58:44 +0100574 assert_ptr_equal(ext_ctx, LYD_CTX(lyd_child(data)));
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100575 lyd_free_siblings(data);
576
Michal Vasko27653162022-11-21 13:58:44 +0100577 /* different yang-lib data with the same content-id */
Michal Vaskoddd76592022-01-17 13:34:48 +0100578 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
579 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
580 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
581 " <module-set>"
582 " <name>test-set</name>"
583 " <module>"
584 " <name>ietf-datastores</name>"
585 " <revision>2018-02-14</revision>"
586 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
587 " </module>"
588 " <module>"
589 " <name>ietf-yang-library</name>"
590 " <revision>2019-01-04</revision>"
591 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
592 " </module>"
593 " <module>"
594 " <name>ietf-yang-schema-mount</name>"
595 " <revision>2019-01-14</revision>"
596 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
597 " </module>"
598 " <module>"
599 " <name>ietf-interfaces</name>"
600 " <revision>2014-05-08</revision>"
601 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
602 " </module>"
603 " <module>"
604 " <name>ietf-ip</name>"
605 " <revision>2014-06-16</revision>"
606 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
607 " </module>"
608 " <module>"
609 " <name>iana-if-type</name>"
610 " <revision>2014-05-08</revision>"
611 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
612 " </module>"
613 " <import-only-module>"
614 " <name>ietf-yang-types</name>"
615 " <revision>2013-07-15</revision>"
616 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
617 " </import-only-module>"
618 " </module-set>"
619 " <schema>"
620 " <name>test-schema</name>"
621 " <module-set>test-set</module-set>"
622 " </schema>"
623 " <datastore>"
624 " <name>ds:running</name>"
625 " <schema>test-schema</schema>"
626 " </datastore>"
627 " <datastore>"
628 " <name>ds:operational</name>"
629 " <schema>test-schema</schema>"
630 " </datastore>"
631 " <content-id>1</content-id>"
632 "</yang-library>"
633 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
634 " <module-set-id>1</module-set-id>"
635 "</modules-state>"
636 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
637 " <mount-point>"
638 " <module>sm</module>"
639 " <label>root</label>"
640 " <inline/>"
641 " </mount-point>"
642 "</schema-mounts>");
643 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
644 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vasko27653162022-11-21 13:58:44 +0100645 assert_ptr_not_equal(ext_ctx, LYD_CTX(lyd_child(data)));
646 ext_ctx = LYD_CTX(lyd_child(data));
Michal Vaskoddd76592022-01-17 13:34:48 +0100647 lyd_free_siblings(data);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100648
649 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
650 CHECK_LYD_STRING_PARAM(data, json, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
Michal Vasko27653162022-11-21 13:58:44 +0100651 assert_ptr_equal(ext_ctx, LYD_CTX(lyd_child(data)));
Michal Vasko9883a3e2022-03-31 12:16:00 +0200652
653 assert_int_equal(LY_SUCCESS, lyd_print_mem(&lyb, data, LYD_LYB, 0));
654 lyd_free_siblings(data);
655
656 CHECK_PARSE_LYD_PARAM(lyb, LYD_LYB, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
Michal Vasko27653162022-11-21 13:58:44 +0100657 assert_ptr_equal(ext_ctx, LYD_CTX(lyd_child(data)));
Michal Vasko9883a3e2022-03-31 12:16:00 +0200658 free(lyb);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100659 lyd_free_siblings(data);
Michal Vaskoddd76592022-01-17 13:34:48 +0100660}
661
662static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100663test_parse_shared(void **state)
Michal Vaskoddd76592022-01-17 13:34:48 +0100664{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100665 const char *xml, *json;
Michal Vasko9883a3e2022-03-31 12:16:00 +0200666 char *lyb;
Michal Vaskoddd76592022-01-17 13:34:48 +0100667 struct lyd_node *data;
668
669 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
670 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
671 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
672 " <module-set>"
673 " <name>test-set</name>"
674 " <module>"
675 " <name>ietf-datastores</name>"
676 " <revision>2018-02-14</revision>"
677 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
678 " </module>"
679 " <module>"
680 " <name>ietf-yang-library</name>"
681 " <revision>2019-01-04</revision>"
682 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
683 " </module>"
684 " <module>"
685 " <name>ietf-yang-schema-mount</name>"
686 " <revision>2019-01-14</revision>"
687 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
688 " </module>"
689 " <module>"
690 " <name>ietf-interfaces</name>"
691 " <revision>2014-05-08</revision>"
692 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
693 " </module>"
694 " <module>"
695 " <name>iana-if-type</name>"
696 " <revision>2014-05-08</revision>"
697 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
698 " </module>"
699 " <import-only-module>"
700 " <name>ietf-yang-types</name>"
701 " <revision>2013-07-15</revision>"
702 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
703 " </import-only-module>"
704 " </module-set>"
705 " <schema>"
706 " <name>test-schema</name>"
707 " <module-set>test-set</module-set>"
708 " </schema>"
709 " <datastore>"
710 " <name>ds:running</name>"
711 " <schema>test-schema</schema>"
712 " </datastore>"
713 " <datastore>"
714 " <name>ds:operational</name>"
715 " <schema>test-schema</schema>"
716 " </datastore>"
717 " <content-id>1</content-id>"
718 "</yang-library>"
719 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
720 " <module-set-id>1</module-set-id>"
721 "</modules-state>"
722 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
723 " <mount-point>"
724 " <module>sm</module>"
725 " <label>root</label>"
726 " <shared-schema/>"
727 " </mount-point>"
728 "</schema-mounts>");
729 xml =
730 "<root xmlns=\"urn:sm\">\n"
731 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
732 " <interface>\n"
733 " <name>bu</name>\n"
734 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
735 " </interface>\n"
736 " </interfaces>\n"
737 " <interfaces-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
738 " <interface>\n"
739 " <name>bu</name>\n"
740 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
741 " <oper-status>not-present</oper-status>\n"
742 " <statistics>\n"
743 " <discontinuity-time>2022-01-01T10:00:00-00:00</discontinuity-time>\n"
744 " </statistics>\n"
745 " </interface>\n"
746 " </interfaces-state>\n"
747 "</root>\n";
748 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
749 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
750 lyd_free_siblings(data);
751
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100752 json =
753 "{\n"
754 " \"sm:root\": {\n"
755 " \"ietf-interfaces:interfaces\": {\n"
756 " \"interface\": [\n"
757 " {\n"
758 " \"name\": \"bu\",\n"
759 " \"type\": \"iana-if-type:ethernetCsmacd\"\n"
760 " }\n"
761 " ]\n"
762 " },\n"
763 " \"ietf-interfaces:interfaces-state\": {\n"
764 " \"interface\": [\n"
765 " {\n"
766 " \"name\": \"bu\",\n"
767 " \"type\": \"iana-if-type:ethernetCsmacd\",\n"
768 " \"oper-status\": \"not-present\",\n"
769 " \"statistics\": {\n"
770 " \"discontinuity-time\": \"2022-01-01T10:00:00-00:00\"\n"
771 " }\n"
772 " }\n"
773 " ]\n"
774 " }\n"
775 " }\n"
776 "}\n";
777 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
778 CHECK_LYD_STRING_PARAM(data, json, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
779 lyd_free_siblings(data);
780
Michal Vaskoddd76592022-01-17 13:34:48 +0100781 /* different yang-lib data */
782 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
783 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
784 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
785 " <module-set>"
786 " <name>test-set</name>"
787 " <module>"
788 " <name>ietf-datastores</name>"
789 " <revision>2018-02-14</revision>"
790 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
791 " </module>"
792 " <module>"
793 " <name>ietf-yang-library</name>"
794 " <revision>2019-01-04</revision>"
795 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
796 " </module>"
797 " <module>"
798 " <name>ietf-yang-schema-mount</name>"
799 " <revision>2019-01-14</revision>"
800 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
801 " </module>"
802 " <module>"
803 " <name>ietf-interfaces</name>"
804 " <revision>2014-05-08</revision>"
805 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
806 " </module>"
807 " <module>"
808 " <name>ietf-ip</name>"
809 " <revision>2014-06-16</revision>"
810 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
811 " </module>"
812 " <module>"
813 " <name>iana-if-type</name>"
814 " <revision>2014-05-08</revision>"
815 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
816 " </module>"
817 " <import-only-module>"
818 " <name>ietf-yang-types</name>"
819 " <revision>2013-07-15</revision>"
820 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
821 " </import-only-module>"
822 " </module-set>"
823 " <schema>"
824 " <name>test-schema</name>"
825 " <module-set>test-set</module-set>"
826 " </schema>"
827 " <datastore>"
828 " <name>ds:running</name>"
829 " <schema>test-schema</schema>"
830 " </datastore>"
831 " <datastore>"
832 " <name>ds:operational</name>"
833 " <schema>test-schema</schema>"
834 " </datastore>"
835 " <content-id>2</content-id>"
836 "</yang-library>"
837 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
838 " <module-set-id>1</module-set-id>"
839 "</modules-state>"
840 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
841 " <mount-point>"
842 " <module>sm</module>"
843 " <label>root</label>"
844 " <shared-schema/>"
845 " </mount-point>"
846 "</schema-mounts>");
847 xml =
848 "<root2 xmlns=\"urn:sm\">\n"
849 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
850 " <interface>\n"
851 " <name>bu</name>\n"
852 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
853 " </interface>\n"
854 " </interfaces>\n"
855 " <interfaces-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
856 " <interface>\n"
857 " <name>bu</name>\n"
858 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
859 " <oper-status>not-present</oper-status>\n"
860 " <statistics>\n"
861 " <discontinuity-time>2022-01-01T10:00:00-00:00</discontinuity-time>\n"
862 " </statistics>\n"
863 " </interface>\n"
864 " </interfaces-state>\n"
865 "</root2>\n";
866 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko193dacd2022-10-13 08:43:05 +0200867 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vaskoddd76592022-01-17 13:34:48 +0100868 "Shared-schema yang-library content-id \"2\" differs from \"1\" used previously.",
Michal Vasko7a266772024-01-23 11:02:38 +0100869 "/ietf-yang-library:yang-library/content-id", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +0100870
871 /* data for 2 mount points */
872 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
873 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
874 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
875 " <module-set>"
876 " <name>test-set</name>"
877 " <module>"
878 " <name>ietf-datastores</name>"
879 " <revision>2018-02-14</revision>"
880 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
881 " </module>"
882 " <module>"
883 " <name>ietf-yang-library</name>"
884 " <revision>2019-01-04</revision>"
885 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
886 " </module>"
887 " <module>"
888 " <name>ietf-yang-schema-mount</name>"
889 " <revision>2019-01-14</revision>"
890 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
891 " </module>"
892 " <module>"
893 " <name>ietf-interfaces</name>"
894 " <revision>2014-05-08</revision>"
895 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
896 " </module>"
897 " <module>"
898 " <name>iana-if-type</name>"
899 " <revision>2014-05-08</revision>"
900 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
901 " </module>"
902 " <import-only-module>"
903 " <name>ietf-yang-types</name>"
904 " <revision>2013-07-15</revision>"
905 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
906 " </import-only-module>"
907 " </module-set>"
908 " <schema>"
909 " <name>test-schema</name>"
910 " <module-set>test-set</module-set>"
911 " </schema>"
912 " <datastore>"
913 " <name>ds:running</name>"
914 " <schema>test-schema</schema>"
915 " </datastore>"
916 " <datastore>"
917 " <name>ds:operational</name>"
918 " <schema>test-schema</schema>"
919 " </datastore>"
920 " <content-id>1</content-id>"
921 "</yang-library>"
922 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
923 " <module-set-id>1</module-set-id>"
924 "</modules-state>"
925 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
926 " <mount-point>"
927 " <module>sm</module>"
928 " <label>root</label>"
929 " <shared-schema/>"
930 " </mount-point>"
931 "</schema-mounts>");
932 xml =
933 "<root xmlns=\"urn:sm\">\n"
934 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
935 " <interface>\n"
936 " <name>bu</name>\n"
937 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
938 " </interface>\n"
939 " </interfaces>\n"
940 " <interfaces-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
941 " <interface>\n"
942 " <name>bu</name>\n"
943 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
944 " <oper-status>not-present</oper-status>\n"
945 " <statistics>\n"
946 " <discontinuity-time>2022-01-01T10:00:00-00:00</discontinuity-time>\n"
947 " </statistics>\n"
948 " </interface>\n"
949 " </interfaces-state>\n"
950 "</root>\n"
951 "<root2 xmlns=\"urn:sm\">\n"
952 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
953 " <interface>\n"
954 " <name>fu</name>\n"
955 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:fddi</type>\n"
956 " </interface>\n"
957 " </interfaces>\n"
958 " <interfaces-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
959 " <interface>\n"
960 " <name>fu</name>\n"
961 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:fddi</type>\n"
962 " <oper-status>down</oper-status>\n"
963 " <statistics>\n"
964 " <discontinuity-time>2020-01-01T10:00:00-00:00</discontinuity-time>\n"
965 " </statistics>\n"
966 " </interface>\n"
967 " </interfaces-state>\n"
968 "</root2>\n";
969 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
970 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
971 lyd_free_siblings(data);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100972
973 json =
974 "{\n"
975 " \"sm:root\": {\n"
976 " \"ietf-interfaces:interfaces\": {\n"
977 " \"interface\": [\n"
978 " {\n"
979 " \"name\": \"bu\",\n"
980 " \"type\": \"iana-if-type:ethernetCsmacd\"\n"
981 " }\n"
982 " ]\n"
983 " },\n"
984 " \"ietf-interfaces:interfaces-state\": {\n"
985 " \"interface\": [\n"
986 " {\n"
987 " \"name\": \"bu\",\n"
988 " \"type\": \"iana-if-type:ethernetCsmacd\",\n"
989 " \"oper-status\": \"not-present\",\n"
990 " \"statistics\": {\n"
991 " \"discontinuity-time\": \"2022-01-01T10:00:00-00:00\"\n"
992 " }\n"
993 " }\n"
994 " ]\n"
995 " }\n"
996 " },\n"
997 " \"sm:root2\": {\n"
998 " \"ietf-interfaces:interfaces\": {\n"
999 " \"interface\": [\n"
1000 " {\n"
1001 " \"name\": \"fu\",\n"
1002 " \"type\": \"iana-if-type:fddi\"\n"
1003 " }\n"
1004 " ]\n"
1005 " },\n"
1006 " \"ietf-interfaces:interfaces-state\": {\n"
1007 " \"interface\": [\n"
1008 " {\n"
1009 " \"name\": \"fu\",\n"
1010 " \"type\": \"iana-if-type:fddi\",\n"
1011 " \"oper-status\": \"down\",\n"
1012 " \"statistics\": {\n"
1013 " \"discontinuity-time\": \"2020-01-01T10:00:00-00:00\"\n"
1014 " }\n"
1015 " }\n"
1016 " ]\n"
1017 " }\n"
1018 " }\n"
1019 "}\n";
1020 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1021 CHECK_LYD_STRING_PARAM(data, json, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001022
1023 assert_int_equal(LY_SUCCESS, lyd_print_mem(&lyb, data, LYD_LYB, LYD_PRINT_WITHSIBLINGS));
1024 lyd_free_siblings(data);
1025
1026 CHECK_PARSE_LYD_PARAM(lyb, LYD_LYB, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1027 free(lyb);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001028 lyd_free_siblings(data);
Michal Vaskoddd76592022-01-17 13:34:48 +01001029}
1030
1031static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001032test_parse_shared_parent_ref(void **state)
Michal Vaskoddd76592022-01-17 13:34:48 +01001033{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001034 const char *xml, *json;
Michal Vaskoddd76592022-01-17 13:34:48 +01001035 struct lyd_node *data;
1036
1037 /* wrong leafref value */
1038 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
1039 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
1040 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
1041 " <module-set>"
1042 " <name>test-set</name>"
1043 " <module>"
1044 " <name>ietf-datastores</name>"
1045 " <revision>2018-02-14</revision>"
1046 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
1047 " </module>"
1048 " <module>"
1049 " <name>ietf-yang-library</name>"
1050 " <revision>2019-01-04</revision>"
1051 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
1052 " </module>"
1053 " <module>"
1054 " <name>ietf-yang-schema-mount</name>"
1055 " <revision>2019-01-14</revision>"
1056 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
1057 " </module>"
1058 " <module>"
1059 " <name>sm</name>"
1060 " <namespace>urn:sm</namespace>"
1061 " </module>"
1062 " <module>"
1063 " <name>ietf-interfaces</name>"
1064 " <revision>2014-05-08</revision>"
1065 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
1066 " </module>"
1067 " <module>"
1068 " <name>iana-if-type</name>"
1069 " <revision>2014-05-08</revision>"
1070 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
1071 " </module>"
1072 " <import-only-module>"
1073 " <name>ietf-yang-types</name>"
1074 " <revision>2013-07-15</revision>"
1075 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
1076 " </import-only-module>"
1077 " </module-set>"
1078 " <schema>"
1079 " <name>test-schema</name>"
1080 " <module-set>test-set</module-set>"
1081 " </schema>"
1082 " <datastore>"
1083 " <name>ds:running</name>"
1084 " <schema>test-schema</schema>"
1085 " </datastore>"
1086 " <datastore>"
1087 " <name>ds:operational</name>"
1088 " <schema>test-schema</schema>"
1089 " </datastore>"
1090 " <content-id>1</content-id>"
1091 "</yang-library>"
1092 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
1093 " <module-set-id>1</module-set-id>"
1094 "</modules-state>"
1095 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
1096 " <namespace>"
1097 " <prefix>smp</prefix>"
1098 " <uri>urn:sm</uri>"
1099 " </namespace>"
1100 " <mount-point>"
1101 " <module>sm</module>"
1102 " <label>mnt-root</label>"
1103 " <shared-schema>"
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001104 " <parent-reference>/smp:target[. = current()/smp:name]</parent-reference>"
Michal Vaskoddd76592022-01-17 13:34:48 +01001105 " </shared-schema>"
1106 " </mount-point>"
1107 "</schema-mounts>");
1108 xml =
1109 "<root3 xmlns=\"urn:sm\">\n"
1110 " <ls>\n"
1111 " <name>target-value</name>\n"
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001112 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
1113 " <interface>\n"
1114 " <name>bu</name>\n"
1115 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
1116 " <sm-name xmlns=\"urn:sm\">target-value</sm-name>\n"
1117 " </interface>\n"
1118 " </interfaces>\n"
Michal Vaskoddd76592022-01-17 13:34:48 +01001119 " </ls>\n"
1120 "</root3>\n"
1121 "<target xmlns=\"urn:sm\">wrong-target-value</target>\n";
1122 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko193dacd2022-10-13 08:43:05 +02001123 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vasko5edfefd2022-08-05 10:07:11 +02001124 "Invalid leafref value \"target-value\" - no target instance \"/sm:target\" with the same value.",
Michal Vasko7a266772024-01-23 11:02:38 +01001125 "/ietf-interfaces:interfaces/interface[name='bu']/sm:sm-name", 0);
Michal Vaskoddd76592022-01-17 13:34:48 +01001126
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001127 json =
1128 "{\n"
1129 " \"sm:root3\": {\n"
1130 " \"ls\": ["
1131 " {\n"
1132 " \"name\": \"target-value\",\n"
1133 " \"ietf-interfaces:interfaces\": {\n"
1134 " \"interface\": [\n"
1135 " {\n"
1136 " \"name\": \"bu\",\n"
1137 " \"type\": \"iana-if-type:ethernetCsmacd\",\n"
1138 " \"sm:sm-name\": \"target-value\"\n"
1139 " }\n"
1140 " ]\n"
1141 " }\n"
1142 " }\n"
1143 " ]\n"
1144 " },\n"
1145 " \"sm:target\": \"wrong-target-value\"\n"
1146 "}\n";
1147 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_EVALID, data);
Michal Vasko193dacd2022-10-13 08:43:05 +02001148 CHECK_LOG_CTX("Ext plugin \"ly2 schema mount v1\": "
Michal Vasko5edfefd2022-08-05 10:07:11 +02001149 "Invalid leafref value \"target-value\" - no target instance \"/sm:target\" with the same value.",
Michal Vasko7a266772024-01-23 11:02:38 +01001150 "/ietf-interfaces:interfaces/interface[name='bu']/sm:sm-name", 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001151
Michal Vaskoddd76592022-01-17 13:34:48 +01001152 /* success */
1153 xml =
1154 "<root3 xmlns=\"urn:sm\">\n"
1155 " <ls>\n"
1156 " <name>target-value</name>\n"
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001157 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
1158 " <interface>\n"
1159 " <name>bu</name>\n"
1160 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
1161 " <sm-name xmlns=\"urn:sm\">target-value</sm-name>\n"
1162 " </interface>\n"
1163 " </interfaces>\n"
Michal Vaskoddd76592022-01-17 13:34:48 +01001164 " </ls>\n"
1165 "</root3>\n"
1166 "<target xmlns=\"urn:sm\">target-value</target>\n";
1167 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1168 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
1169 lyd_free_siblings(data);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001170
1171 json =
1172 "{\n"
1173 " \"sm:root3\": {\n"
1174 " \"ls\": [\n"
1175 " {\n"
1176 " \"name\": \"target-value\",\n"
1177 " \"ietf-interfaces:interfaces\": {\n"
1178 " \"interface\": [\n"
1179 " {\n"
1180 " \"name\": \"bu\",\n"
1181 " \"type\": \"iana-if-type:ethernetCsmacd\",\n"
1182 " \"sm:sm-name\": \"target-value\"\n"
1183 " }\n"
1184 " ]\n"
1185 " }\n"
1186 " }\n"
1187 " ]\n"
1188 " },\n"
1189 " \"sm:target\": \"target-value\"\n"
1190 "}\n";
1191 CHECK_PARSE_LYD_PARAM(json, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1192 CHECK_LYD_STRING_PARAM(data, json, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
1193 lyd_free_siblings(data);
Michal Vaskoddd76592022-01-17 13:34:48 +01001194}
1195
1196static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001197test_parse_config(void **state)
Michal Vaskoddd76592022-01-17 13:34:48 +01001198{
1199 const char *xml;
Michal Vasko24edeb92022-07-20 08:08:46 +02001200 char *lyb;
Michal Vaskoddd76592022-01-17 13:34:48 +01001201 struct lyd_node *data;
1202 const struct lyd_node *node;
1203
1204 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
1205 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
1206 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
1207 " <module-set>"
1208 " <name>test-set</name>"
1209 " <module>"
1210 " <name>ietf-datastores</name>"
1211 " <revision>2018-02-14</revision>"
1212 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
1213 " </module>"
1214 " <module>"
1215 " <name>ietf-yang-library</name>"
1216 " <revision>2019-01-04</revision>"
1217 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
1218 " </module>"
1219 " <module>"
1220 " <name>ietf-yang-schema-mount</name>"
1221 " <revision>2019-01-14</revision>"
1222 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
1223 " </module>"
1224 " <module>"
1225 " <name>ietf-interfaces</name>"
1226 " <revision>2014-05-08</revision>"
1227 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
1228 " </module>"
1229 " <module>"
1230 " <name>iana-if-type</name>"
1231 " <revision>2014-05-08</revision>"
1232 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
1233 " </module>"
1234 " <import-only-module>"
1235 " <name>ietf-yang-types</name>"
1236 " <revision>2013-07-15</revision>"
1237 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
1238 " </import-only-module>"
1239 " </module-set>"
1240 " <schema>"
1241 " <name>test-schema</name>"
1242 " <module-set>test-set</module-set>"
1243 " </schema>"
1244 " <datastore>"
1245 " <name>ds:running</name>"
1246 " <schema>test-schema</schema>"
1247 " </datastore>"
1248 " <datastore>"
1249 " <name>ds:operational</name>"
1250 " <schema>test-schema</schema>"
1251 " </datastore>"
1252 " <content-id>1</content-id>"
1253 "</yang-library>"
1254 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
1255 " <module-set-id>1</module-set-id>"
1256 "</modules-state>"
1257 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
1258 " <mount-point>"
1259 " <module>sm</module>"
1260 " <label>root</label>"
1261 " <config>false</config>"
1262 " <inline/>"
1263 " </mount-point>"
1264 "</schema-mounts>");
1265 xml =
1266 "<root xmlns=\"urn:sm\">\n"
1267 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
1268 " <interface>\n"
1269 " <name>bu</name>\n"
1270 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
1271 " <enabled>true</enabled>\n"
1272 " </interface>\n"
1273 " </interfaces>\n"
1274 "</root>\n";
1275 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1276 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
1277
1278 node = lyd_child(data);
1279 assert_string_equal(LYD_NAME(node), "interfaces");
1280 assert_true(node->schema->flags & LYS_CONFIG_R);
1281 node = lyd_child(node);
1282 assert_string_equal(LYD_NAME(node), "interface");
1283 assert_true(node->schema->flags & LYS_CONFIG_R);
1284 node = lyd_child(node);
1285 assert_string_equal(LYD_NAME(node), "name");
1286 assert_true(node->schema->flags & LYS_CONFIG_R);
1287 node = node->next;
1288 assert_string_equal(LYD_NAME(node), "type");
1289 assert_true(node->schema->flags & LYS_CONFIG_R);
1290
Michal Vasko24edeb92022-07-20 08:08:46 +02001291 lyd_print_mem(&lyb, data, LYD_LYB, 0);
1292 lyd_free_siblings(data);
1293 CHECK_PARSE_LYD_PARAM(lyb, LYD_LYB, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1294 free(lyb);
1295
1296 node = lyd_child(data);
1297 assert_string_equal(LYD_NAME(node), "interfaces");
1298 assert_true(node->schema->flags & LYS_CONFIG_R);
1299 node = lyd_child(node);
1300 assert_string_equal(LYD_NAME(node), "interface");
1301 assert_true(node->schema->flags & LYS_CONFIG_R);
1302 node = lyd_child(node);
1303 assert_string_equal(LYD_NAME(node), "name");
1304 assert_true(node->schema->flags & LYS_CONFIG_R);
1305 node = node->next;
1306 assert_string_equal(LYD_NAME(node), "type");
1307 assert_true(node->schema->flags & LYS_CONFIG_R);
1308
Michal Vaskoddd76592022-01-17 13:34:48 +01001309 lyd_free_siblings(data);
Michal Vasko9e12ffe2022-09-21 15:11:30 +02001310
1311 /* the same effect but use a config false mount point instead of the separate metadata node */
1312 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
1313 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
1314 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
1315 " <module-set>"
1316 " <name>test-set</name>"
1317 " <module>"
1318 " <name>ietf-datastores</name>"
1319 " <revision>2018-02-14</revision>"
1320 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
1321 " </module>"
1322 " <module>"
1323 " <name>ietf-yang-library</name>"
1324 " <revision>2019-01-04</revision>"
1325 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
1326 " </module>"
1327 " <module>"
1328 " <name>ietf-yang-schema-mount</name>"
1329 " <revision>2019-01-14</revision>"
1330 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
1331 " </module>"
1332 " <module>"
1333 " <name>ietf-interfaces</name>"
1334 " <revision>2014-05-08</revision>"
1335 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
1336 " </module>"
1337 " <module>"
1338 " <name>iana-if-type</name>"
1339 " <revision>2014-05-08</revision>"
1340 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
1341 " </module>"
1342 " <import-only-module>"
1343 " <name>ietf-yang-types</name>"
1344 " <revision>2013-07-15</revision>"
1345 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
1346 " </import-only-module>"
1347 " </module-set>"
1348 " <schema>"
1349 " <name>test-schema</name>"
1350 " <module-set>test-set</module-set>"
1351 " </schema>"
1352 " <datastore>"
1353 " <name>ds:running</name>"
1354 " <schema>test-schema</schema>"
1355 " </datastore>"
1356 " <datastore>"
1357 " <name>ds:operational</name>"
1358 " <schema>test-schema</schema>"
1359 " </datastore>"
1360 " <content-id>1</content-id>"
1361 "</yang-library>"
1362 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
1363 " <module-set-id>1</module-set-id>"
1364 "</modules-state>"
1365 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
1366 " <mount-point>"
1367 " <module>sm</module>"
1368 " <label>root</label>"
1369 " <inline/>"
1370 " </mount-point>"
1371 "</schema-mounts>");
1372 xml =
1373 "<root4 xmlns=\"urn:sm\">\n"
1374 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
1375 " <interface>\n"
1376 " <name>bu</name>\n"
1377 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
1378 " <enabled>true</enabled>\n"
1379 " </interface>\n"
1380 " </interfaces>\n"
1381 "</root4>\n";
1382 CHECK_PARSE_LYD_PARAM(xml, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, data);
1383 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
1384
1385 node = lyd_child(data->next->next->next);
1386 assert_string_equal(LYD_NAME(node), "interfaces");
1387 assert_true(node->schema->flags & LYS_CONFIG_R);
1388 node = lyd_child(node);
1389 assert_string_equal(LYD_NAME(node), "interface");
1390 assert_true(node->schema->flags & LYS_CONFIG_R);
1391 node = lyd_child(node);
1392 assert_string_equal(LYD_NAME(node), "name");
1393 assert_true(node->schema->flags & LYS_CONFIG_R);
1394 node = node->next;
1395 assert_string_equal(LYD_NAME(node), "type");
1396 assert_true(node->schema->flags & LYS_CONFIG_R);
1397
1398 lyd_free_siblings(data);
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001399}
1400
Michal Vasko8cc3f662022-03-29 11:25:51 +02001401static void
1402test_new(void **state)
1403{
1404 const char *xml;
1405 const struct lys_module *mod;
1406 struct lyd_node *data, *node;
1407
1408 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
1409 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
1410 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
1411 " <module-set>"
1412 " <name>test-set</name>"
1413 " <module>"
1414 " <name>ietf-datastores</name>"
1415 " <revision>2018-02-14</revision>"
1416 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
1417 " </module>"
1418 " <module>"
1419 " <name>ietf-yang-library</name>"
1420 " <revision>2019-01-04</revision>"
1421 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
1422 " </module>"
1423 " <module>"
1424 " <name>ietf-yang-schema-mount</name>"
1425 " <revision>2019-01-14</revision>"
1426 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
1427 " </module>"
1428 " <module>"
1429 " <name>ietf-interfaces</name>"
1430 " <revision>2014-05-08</revision>"
1431 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
1432 " </module>"
1433 " <module>"
1434 " <name>iana-if-type</name>"
1435 " <revision>2014-05-08</revision>"
1436 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
1437 " </module>"
Michal Vasko35b29622022-07-22 14:12:56 +02001438 " <module>"
1439 " <name>ietf-ip</name>"
1440 " <revision>2014-06-16</revision>"
1441 " <namespace>urn:ietf:params:xml:ns:yang:ietf-ip</namespace>"
1442 " </module>"
Michal Vasko8cc3f662022-03-29 11:25:51 +02001443 " <import-only-module>"
1444 " <name>ietf-yang-types</name>"
1445 " <revision>2013-07-15</revision>"
1446 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
1447 " </import-only-module>"
1448 " </module-set>"
1449 " <schema>"
1450 " <name>test-schema</name>"
1451 " <module-set>test-set</module-set>"
1452 " </schema>"
1453 " <datastore>"
1454 " <name>ds:running</name>"
1455 " <schema>test-schema</schema>"
1456 " </datastore>"
1457 " <datastore>"
1458 " <name>ds:operational</name>"
1459 " <schema>test-schema</schema>"
1460 " </datastore>"
1461 " <content-id>1</content-id>"
1462 "</yang-library>"
1463 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
1464 " <module-set-id>1</module-set-id>"
1465 "</modules-state>"
1466 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
1467 " <mount-point>"
1468 " <module>sm</module>"
1469 " <label>root</label>"
1470 " <shared-schema/>"
1471 " </mount-point>"
1472 "</schema-mounts>");
1473 xml =
1474 "<root xmlns=\"urn:sm\">\n"
1475 " <interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
1476 " <interface>\n"
1477 " <name>bu</name>\n"
1478 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
Michal Vasko35b29622022-07-22 14:12:56 +02001479 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
1480 " <enabled>false</enabled>\n"
1481 " </ipv4>\n"
Michal Vasko8cc3f662022-03-29 11:25:51 +02001482 " </interface>\n"
1483 " </interfaces>\n"
1484 " <interfaces-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
1485 " <interface>\n"
1486 " <name>bu</name>\n"
1487 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
1488 " <oper-status>not-present</oper-status>\n"
1489 " <statistics>\n"
1490 " <discontinuity-time>2022-01-01T10:00:00-00:00</discontinuity-time>\n"
1491 " </statistics>\n"
1492 " </interface>\n"
1493 " </interfaces-state>\n"
1494 "</root>\n";
1495
1496 /* create the data manually with simple new functions */
1497 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "sm");
1498 assert_non_null(mod);
1499 assert_int_equal(LY_SUCCESS, lyd_new_inner(NULL, mod, "root", 0, &data));
1500
1501 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-interfaces");
1502 assert_non_null(mod);
1503 assert_int_equal(LY_SUCCESS, lyd_new_inner(data, mod, "interfaces", 0, &node));
1504 assert_int_equal(LY_SUCCESS, lyd_new_list(node, NULL, "interface", 0, &node, "bu"));
1505 assert_int_equal(LY_SUCCESS, lyd_new_term(node, NULL, "type", "iana-if-type:ethernetCsmacd", 0, NULL));
Michal Vasko35b29622022-07-22 14:12:56 +02001506 mod = ly_ctx_get_module_implemented(LYD_CTX(node), "ietf-ip");
1507 assert_non_null(mod);
1508 assert_int_equal(LY_SUCCESS, lyd_new_inner(node, mod, "ipv4", 0, &node));
1509 assert_int_equal(LY_SUCCESS, lyd_new_term(node, NULL, "enabled", "false", 0, NULL));
Michal Vasko8cc3f662022-03-29 11:25:51 +02001510
Michal Vasko35b29622022-07-22 14:12:56 +02001511 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-interfaces");
1512 assert_non_null(mod);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001513 assert_int_equal(LY_SUCCESS, lyd_new_inner(data, mod, "interfaces-state", 0, &node));
1514 assert_int_equal(LY_SUCCESS, lyd_new_list(node, NULL, "interface", 0, &node, "bu"));
1515 assert_int_equal(LY_SUCCESS, lyd_new_term(node, NULL, "type", "iana-if-type:ethernetCsmacd", 0, NULL));
1516 assert_int_equal(LY_SUCCESS, lyd_new_term(node, NULL, "oper-status", "not-present", 0, NULL));
1517 assert_int_equal(LY_SUCCESS, lyd_new_inner(node, NULL, "statistics", 0, &node));
1518 assert_int_equal(LY_SUCCESS, lyd_new_term(node, NULL, "discontinuity-time", "2022-01-01T10:00:00-00:00", 0, NULL));
1519
1520 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
1521 lyd_free_siblings(data);
1522
1523 /* create the data using lyd_new_path */
1524 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX,
1525 "/sm:root/ietf-interfaces:interfaces/interface[name='bu']/type", "iana-if-type:ethernetCsmacd", 0, &data));
1526 assert_int_equal(LY_SUCCESS, lyd_new_path(data, NULL,
Michal Vasko35b29622022-07-22 14:12:56 +02001527 "/sm:root/ietf-interfaces:interfaces/interface[name='bu']/ietf-ip:ipv4/enabled", "false", 0, NULL));
1528 assert_int_equal(LY_SUCCESS, lyd_new_path(data, NULL,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001529 "/sm:root/ietf-interfaces:interfaces-state/interface[name='bu']/type", "iana-if-type:ethernetCsmacd", 0, NULL));
1530 assert_int_equal(LY_SUCCESS, lyd_new_path(data, NULL,
1531 "/sm:root/ietf-interfaces:interfaces-state/interface[name='bu']/oper-status", "not-present", 0, NULL));
1532 assert_int_equal(LY_SUCCESS, lyd_new_path(data, NULL,
1533 "/sm:root/ietf-interfaces:interfaces-state/interface[name='bu']/statistics/discontinuity-time",
1534 "2022-01-01T10:00:00-00:00", 0, NULL));
1535
1536 CHECK_LYD_STRING_PARAM(data, xml, LYD_XML, LYD_PRINT_WITHSIBLINGS);
1537 lyd_free_siblings(data);
1538}
1539
Michal Vaskoe482bb92023-02-01 11:41:41 +01001540static void
1541test_lys_getnext(void **state)
1542{
1543 const struct lysc_node *parent, *node;
1544 struct ly_ctx *sm_ctx;
1545
1546 ly_ctx_set_ext_data_clb(UTEST_LYCTX, test_ext_data_clb,
1547 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\" "
1548 " xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">"
1549 " <module-set>"
1550 " <name>test-set</name>"
1551 " <module>"
1552 " <name>ietf-datastores</name>"
1553 " <revision>2018-02-14</revision>"
1554 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>"
1555 " </module>"
1556 " <module>"
1557 " <name>ietf-yang-library</name>"
1558 " <revision>2019-01-04</revision>"
1559 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
1560 " </module>"
1561 " <module>"
1562 " <name>ietf-yang-schema-mount</name>"
1563 " <revision>2019-01-14</revision>"
1564 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount</namespace>"
1565 " </module>"
1566 " <module>"
1567 " <name>ietf-interfaces</name>"
1568 " <revision>2014-05-08</revision>"
1569 " <namespace>urn:ietf:params:xml:ns:yang:ietf-interfaces</namespace>"
1570 " </module>"
1571 " <module>"
1572 " <name>iana-if-type</name>"
1573 " <revision>2014-05-08</revision>"
1574 " <namespace>urn:ietf:params:xml:ns:yang:iana-if-type</namespace>"
1575 " </module>"
1576 " <module>"
1577 " <name>ietf-ip</name>"
1578 " <revision>2014-06-16</revision>"
1579 " <namespace>urn:ietf:params:xml:ns:yang:ietf-ip</namespace>"
1580 " </module>"
1581 " <import-only-module>"
1582 " <name>ietf-yang-types</name>"
1583 " <revision>2013-07-15</revision>"
1584 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>"
1585 " </import-only-module>"
1586 " </module-set>"
1587 " <schema>"
1588 " <name>test-schema</name>"
1589 " <module-set>test-set</module-set>"
1590 " </schema>"
1591 " <datastore>"
1592 " <name>ds:running</name>"
1593 " <schema>test-schema</schema>"
1594 " </datastore>"
1595 " <datastore>"
1596 " <name>ds:operational</name>"
1597 " <schema>test-schema</schema>"
1598 " </datastore>"
1599 " <content-id>1</content-id>"
1600 "</yang-library>"
1601 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">"
1602 " <module-set-id>1</module-set-id>"
1603 "</modules-state>"
1604 "<schema-mounts xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-schema-mount\">"
1605 " <mount-point>"
1606 " <module>sm</module>"
1607 " <label>root</label>"
1608 " <shared-schema/>"
1609 " </mount-point>"
1610 "</schema-mounts>");
1611
1612 parent = lys_find_path(UTEST_LYCTX, NULL, "/sm:root", 0);
1613 assert_non_null(parent);
1614
1615 node = lys_getnext(NULL, parent, NULL, LYS_GETNEXT_WITHSCHEMAMOUNT);
1616 assert_non_null(node);
1617 assert_string_equal(node->name, "schema-mounts");
1618 sm_ctx = node->module->ctx;
1619
1620 node = lys_getnext(node, parent, NULL, LYS_GETNEXT_WITHSCHEMAMOUNT);
1621 assert_non_null(node);
1622 assert_string_equal(node->name, "yang-library");
1623
1624 node = lys_getnext(node, parent, NULL, LYS_GETNEXT_WITHSCHEMAMOUNT);
1625 assert_non_null(node);
1626 assert_string_equal(node->name, "modules-state");
1627
1628 node = lys_getnext(node, parent, NULL, LYS_GETNEXT_WITHSCHEMAMOUNT);
1629 assert_non_null(node);
1630 assert_string_equal(node->name, "interfaces");
1631
1632 node = lys_getnext(node, parent, NULL, LYS_GETNEXT_WITHSCHEMAMOUNT);
1633 assert_non_null(node);
1634 assert_string_equal(node->name, "interfaces-state");
1635
1636 node = lys_getnext(node, parent, NULL, LYS_GETNEXT_WITHSCHEMAMOUNT);
1637 assert_null(node);
1638
1639 ly_ctx_destroy(sm_ctx);
1640}
1641
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001642int
1643main(void)
1644{
1645 const struct CMUnitTest tests[] = {
Michal Vaskoddd76592022-01-17 13:34:48 +01001646 UTEST(test_schema),
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001647 UTEST(test_parse_invalid, setup),
1648 UTEST(test_parse_inline, setup),
1649 UTEST(test_parse_shared, setup),
1650 UTEST(test_parse_shared_parent_ref, setup),
1651 UTEST(test_parse_config, setup),
Michal Vasko8cc3f662022-03-29 11:25:51 +02001652 UTEST(test_new, setup),
Michal Vaskoe482bb92023-02-01 11:41:41 +01001653 UTEST(test_lys_getnext, setup),
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +01001654 };
1655
1656 return cmocka_run_group_tests(tests, NULL, NULL);
1657}