blob: fa125d8a6ef659c6bf757016a5bc2265fd16d209 [file] [log] [blame]
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001/*
2 * @file test_tree_data.c
3 * @author: Mislav Novakovic <mislav.novakovic@sartura.hr>
4 * @brief unit tests for functions from tree_data.h header
5 *
6 * Copyright (C) 2016 Deutsche Telekom AG.
7 *
8 * Author: Mislav Novakovic <mislav.novakovic@sartura.hr>
9 *
Luka Perkov01484582016-09-08 13:57:14 +020010 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010012 * You may obtain a copy of the License at
13 *
Luka Perkov01484582016-09-08 13:57:14 +020014 * https://opensource.org/licenses/BSD-3-Clause
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010015 */
16
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <sys/mman.h>
27#include <unistd.h>
28#include <string.h>
29
Jan Kundrátfd7a5c72017-10-26 17:45:06 +020030#include "tests/config.h"
31#include "libyang.h"
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010032#include "../../src/tree_data.h"
33#include "../../src/tree_schema.h"
34
Denis Volpato Martinse7416282016-10-05 00:57:32 -030035#define TMP_TEMPLATE "/tmp/libyang-XXXXXX"
36
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010037struct ly_ctx *ctx = NULL;
38struct lyd_node *root = NULL;
39
40const char *a_data_xml = "\
41<x xmlns=\"urn:a\">\n\
42 <bubba>test</bubba>\n\
43 </x>\n";
44
45const char *lys_module_a = \
46"<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
47<module name=\"a\" \
48 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" \
Radek Krejci532e5e92017-02-22 12:59:24 +010049 xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"\
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010050 xmlns:a=\"urn:a\"> \
51 <namespace uri=\"urn:a\"/> \
52 <prefix value=\"a_mod\"/> \
53 <include module=\"asub\"/> \
54 <include module=\"atop\"/> \
Radek Krejci532e5e92017-02-22 12:59:24 +010055 <import module=\"ietf-yang-metadata\"> \
56 <prefix value=\"md\"/> \
57 </import> \
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010058 <feature name=\"foo\"/> \
59 <grouping name=\"gg\"> \
60 <leaf name=\"bar-gggg\"> \
61 <type name=\"string\"/> \
62 </leaf> \
63 </grouping> \
Radek Krejci532e5e92017-02-22 12:59:24 +010064 <md:annotation name=\"test\"> \
65 <type name=\"string\"/> \
66 </md:annotation> \
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010067 <container name=\"x\"> \
68 <leaf name=\"bar-leaf\"> \
69 <if-feature name=\"bar\"/> \
70 <type name=\"string\"/> \
71 </leaf> \
72 <uses name=\"gg\"> \
73 <if-feature name=\"bar\"/> \
74 </uses> \
75 <leaf name=\"baz\"> \
76 <if-feature name=\"foo\"/> \
77 <type name=\"string\"/> \
78 </leaf> \
79 <leaf name=\"bubba\"> \
80 <type name=\"string\"/> \
81 </leaf> \
82 <leaf name=\"number32\"> \
83 <type name=\"int32\"/> \
84 </leaf> \
85 <leaf name=\"number64\"> \
86 <type name=\"int64\"/> \
87 </leaf> \
Michal Vasko3c0eb752018-02-08 16:09:19 +010088 <leaf name=\"def-leaf\"> \
89 <type name=\"string\"/> \
90 <default value=\"def\"/> \
91 </leaf> \
Michal Vasko8681b662016-03-24 13:26:36 +010092 </container> \
Radek Krejci434780f2016-09-16 15:28:27 +020093 <leaf name=\"y\"><type name=\"string\"/></leaf> \
Radek Krejci45826012016-08-24 15:07:57 +020094 <anyxml name=\"any\"/> \
Mislav Novakovic1f160ab2016-03-23 12:46:48 +010095 <augment target-node=\"/x\"> \
96 <if-feature name=\"bar\"/> \
97 <container name=\"bar-y\"/> \
98 </augment> \
99 <rpc name=\"bar-rpc\"> \
100 <if-feature name=\"bar\"/> \
101 </rpc> \
102 <rpc name=\"foo-rpc\"> \
103 <if-feature name=\"foo\"/> \
104 </rpc> \
105 <rpc name=\"rpc1\"> \
106 <input> \
107 <leaf name=\"input-leaf1\"> \
108 <type name=\"string\"/> \
109 </leaf> \
110 <container name=\"x\"> \
111 <leaf name=\"input-leaf2\"> \
112 <type name=\"string\"/> \
113 </leaf> \
114 </container> \
115 </input> \
116 <output> \
117 <leaf name=\"output-leaf1\"> \
118 <type name=\"string\"/> \
119 </leaf> \
120 <leaf name=\"output-leaf2\"> \
121 <type name=\"string\"/> \
122 </leaf> \
Michal Vasko8681b662016-03-24 13:26:36 +0100123 <container name=\"rpc-container\"> \
124 <leaf name=\"output-leaf3\"> \
125 <type name=\"string\"/> \
126 </leaf> \
127 </container> \
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100128 </output> \
129 </rpc> \
130 <list name=\"l\"> \
131 <key value=\"key1 key2\"/> \
132 <leaf name=\"key1\"> \
133 <type name=\"uint8\"/> \
134 </leaf> \
135 <leaf name=\"key2\"> \
136 <type name=\"uint8\"/> \
137 </leaf> \
138 <leaf name=\"value\"> \
139 <type name=\"string\"/> \
140 </leaf> \
141 </list> \
142</module> \
143";
144
145const char *result_xml = "<x xmlns=\"urn:a\"><bubba>test</bubba></x>";
146
147const char *result_xml_format ="\
148<x xmlns=\"urn:a\">\n\
149 <bubba>test</bubba>\n\
150</x>\n\
151";
152
153const char *result_json = "\
154{\n\
155 \"a:x\": {\n\
156 \"bubba\": \"test\"\n\
157 }\n\
158}\n\
159";
160
161int
162generic_init(char *config_file, const char *module, char *yang_folder)
163{
164 LYS_INFORMAT yang_format;
165 LYD_FORMAT in_format;
166 char *config = NULL;
167 struct stat sb_config;
168 int fd = -1;
169
Radek Krejcibe90a902016-06-14 16:11:51 +0200170 if (!yang_folder) {
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100171 goto error;
172 }
173
174 yang_format = LYS_IN_YIN;
175 in_format = LYD_XML;
176
Radek Krejcidd3263a2017-07-15 11:50:09 +0200177 ctx = ly_ctx_new(yang_folder, 0);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100178 if (!ctx) {
179 goto error;
180 }
181
Radek Krejcibe90a902016-06-14 16:11:51 +0200182 if (module && !lys_parse_mem(ctx, module, yang_format)) {
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100183 goto error;
184 }
185
Radek Krejcibe90a902016-06-14 16:11:51 +0200186 if (config_file) {
187 fd = open(config_file, O_RDONLY);
188 if (fd == -1 || fstat(fd, &sb_config) == -1 || !S_ISREG(sb_config.st_mode)) {
189 goto error;
190 }
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100191
Radek Krejcibe90a902016-06-14 16:11:51 +0200192 config = mmap(NULL, sb_config.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
193 close(fd);
194 fd = -1;
195
196 root = lyd_parse_mem(ctx, config, in_format, LYD_OPT_CONFIG | LYD_OPT_STRICT);
197 if (!root) {
198 goto error;
199 }
200
201 /* cleanup */
202 munmap(config, sb_config.st_size);
203 } else {
204 root = NULL;
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100205 }
206
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100207 return 0;
208
209error:
210 if (config) {
211 munmap(config, sb_config.st_size);
212 }
213 if (fd != -1) {
214 close(fd);
215 }
216
217 return -1;
218}
219
220static int
Michal Vasko20399fb2017-01-09 11:11:07 +0100221setup_f2(void **state)
222{
Radek Krejcidd3263a2017-07-15 11:50:09 +0200223 *state = ly_ctx_new(NULL, 0);
Michal Vasko20399fb2017-01-09 11:11:07 +0100224 if (!*state) {
225 return -1;
226 }
227
228 return 0;
229}
230
231static int
232teardown_f2(void **state)
233{
234 ly_ctx_destroy((struct ly_ctx *)(*state), NULL);
235 return 0;
236}
237
238static int
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100239setup_f(void **state)
240{
241 (void) state; /* unused */
242 char *config_file = TESTS_DIR"/api/files/a.xml";
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100243 char *yang_folder = TESTS_DIR"/api/files";
244 int rc;
245
Radek Krejcibe90a902016-06-14 16:11:51 +0200246 rc = generic_init(config_file, lys_module_a, yang_folder);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100247
248 if (rc) {
249 return -1;
250 }
251
252 return 0;
253}
254
255static int
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100256teardown_f(void **state)
257{
258 (void) state; /* unused */
259 if (root)
Radek Krejci5f001dd2016-09-07 16:26:40 +0200260 lyd_free_withsiblings(root);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100261 if (ctx)
262 ly_ctx_destroy(ctx, NULL);
263
264 return 0;
265}
266
267static void
268test_lyd_parse_mem(void **state)
269{
270 (void) state; /* unused */
271 char *yang_folder = TESTS_DIR"/api/files";
272 LYD_FORMAT in_format = LYD_XML;
273 LYS_INFORMAT yang_format = LYS_IN_YIN;
274 struct ly_ctx *ctx = NULL;
275 struct lyd_node *root = NULL;
276
Radek Krejcidd3263a2017-07-15 11:50:09 +0200277 ctx = ly_ctx_new(yang_folder, 0);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100278 if (!ctx) {
279 goto error;
280 }
281
282 if (!lys_parse_mem(ctx, lys_module_a, yang_format)) {
283 goto error;
284 }
285
Radek Krejci8f55ad92016-04-12 13:55:56 +0200286 root = lyd_parse_mem(ctx, a_data_xml, in_format, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100287 if (!root) {
288 goto error;
289 }
290
291 assert_string_equal("x", root->schema->name);
292
293 if (root)
294 lyd_free(root);
295 if (ctx)
296 ly_ctx_destroy(ctx, NULL);
297
298 return;
299error:
300
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100301 if (ctx)
302 ly_ctx_destroy(ctx, NULL);
303
304 fail();
305}
306
307static void
308test_lyd_parse_fd(void **state)
309{
310 (void) state; /* unused */
311 char *yang_folder = TESTS_DIR"/api/files";
312 char *config_file = TESTS_DIR"/api/files/a.xml";
313 LYD_FORMAT in_format = LYD_XML;
314 LYS_INFORMAT yang_format = LYS_IN_YIN;
315 struct ly_ctx *ctx = NULL;
316 struct lyd_node *root = NULL;
317 struct stat sb;
318 int fd = -1;
319
Radek Krejcidd3263a2017-07-15 11:50:09 +0200320 ctx = ly_ctx_new(yang_folder, 0);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100321 if (!ctx) {
322 goto error;
323 }
324
325 if (!lys_parse_mem(ctx, lys_module_a, yang_format)) {
326 goto error;
327 }
328
329 fd = open(config_file, O_RDONLY);
330 if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
331 goto error;
332 }
333
Radek Krejci8f55ad92016-04-12 13:55:56 +0200334 root = lyd_parse_fd(ctx, fd, in_format, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100335 if (!root) {
336 goto error;
337 }
338
339 assert_string_equal("x", root->schema->name);
340
341 if (root)
342 lyd_free(root);
343 if (ctx)
344 ly_ctx_destroy(ctx, NULL);
345 if (fd > 0)
346 close(fd);
347
348 return;
349error:
350
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100351 if (ctx)
352 ly_ctx_destroy(ctx, NULL);
353 if (fd > 0)
354 close(fd);
355 fail();
356}
357
358static void
359test_lyd_parse_path(void **state)
360{
361 (void) state; /* unused */
362 char *yang_folder = TESTS_DIR"/api/files";
363 char *config_file = TESTS_DIR"/api/files/a.xml";
364 LYD_FORMAT in_format = LYD_XML;
365 LYS_INFORMAT yang_format = LYS_IN_YIN;
366 struct ly_ctx *ctx = NULL;
367 struct lyd_node *root = NULL;
368
Radek Krejcidd3263a2017-07-15 11:50:09 +0200369 ctx = ly_ctx_new(yang_folder, 0);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100370 if (!ctx) {
371 goto error;
372 }
373
374 if (!lys_parse_mem(ctx, lys_module_a, yang_format)) {
375 goto error;
376 }
377
Radek Krejci8f55ad92016-04-12 13:55:56 +0200378 root = lyd_parse_path(ctx, config_file, in_format, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100379 if (!root) {
380 goto error;
381 }
382
383 assert_string_equal("x", root->schema->name);
384
385 if (root)
386 lyd_free(root);
387 if (ctx)
388 ly_ctx_destroy(ctx, NULL);
389
390 return;
391error:
392
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100393 if (ctx)
394 ly_ctx_destroy(ctx, NULL);
395 fail();
396}
397
398static void
399test_lyd_parse_xml(void **state)
400{
401 (void) state; /* unused */
402 char *yang_folder = TESTS_DIR"/api/files";
403 struct lyd_node *node = NULL;
404 const struct lys_module *module = NULL;
405 struct lyxml_elem *root_xml = NULL;
406 LYS_INFORMAT yang_format = LYS_IN_YIN;
407 struct ly_ctx *ctx = NULL;
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100408
Radek Krejcidd3263a2017-07-15 11:50:09 +0200409 ctx = ly_ctx_new(yang_folder, 0);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100410 if (!ctx) {
411 goto error;
412 }
413
414 module = lys_parse_mem(ctx, lys_module_a, yang_format);
415 if (!module) {
416 goto error;
417 }
418
419 root_xml = lyxml_parse_mem(ctx, a_data_xml, 1);
420
Radek Krejci8f55ad92016-04-12 13:55:56 +0200421 node = lyd_parse_xml(ctx, &root_xml, LYD_OPT_NOSIBLINGS | LYD_OPT_DATA);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100422 if (!node) {
423 goto error;
424 }
425
426 assert_string_equal("x", node->schema->name);
427
428 if (node)
429 lyd_free(node);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100430 if (root_xml)
431 lyxml_free(ctx, root_xml);
432 if (ctx)
433 ly_ctx_destroy(ctx, NULL);
434
435 return;
436error:
437
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100438 if (root_xml)
439 lyxml_free(ctx, root_xml);
440 if (ctx)
441 ly_ctx_destroy(ctx, NULL);
442 fail();
443}
444
445static void
446test_lyd_new(void **state)
447{
448 (void) state; /* unused */
449 struct lyd_node *new = NULL;
450
451 new = lyd_new(root, root->child->schema->module, "bar-y");
452 if (!new) {
453 fail();
454 }
455
456 assert_string_equal("bar-y", new->schema->name);
457}
458
459static void
460test_lyd_new_leaf(void **state)
461{
462 (void) state; /* unused */
463 struct lyd_node *new = NULL;
464 struct lyd_node_leaf_list *result;
465
466 new = lyd_new_leaf(root, root->schema->module, "number32", "100");
467 if (!new) {
468 fail();
469 }
470
471 result = (struct lyd_node_leaf_list *) new;
472 assert_string_equal("100", result->value_str);
473}
474
475static void
476test_lyd_change_leaf(void **state)
477{
478 (void) state; /* unused */
479 struct lyd_node_leaf_list *result = NULL;
480 int rc;
481
482 result = (struct lyd_node_leaf_list *) root->child;
483 assert_string_equal("test", result->value_str);
484
485 rc = lyd_change_leaf(result, "new_test");
486 if (rc) {
487 fail();
488 }
489
490 assert_string_equal("new_test", result->value_str);
491
492}
493
494static void
495test_lyd_output_new_leaf(void **state)
496{
497 (void) state; /* unused */
498 struct lyd_node *node = NULL;
499 struct lyd_node_leaf_list *result = NULL;
500
Michal Vasko98a5a742016-05-11 11:02:56 +0200501 node = lyd_new_output(NULL, root->schema->module, "rpc1");
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100502 if (!node) {
503 fail();
504 }
505
Michal Vasko98a5a742016-05-11 11:02:56 +0200506 result = (struct lyd_node_leaf_list *)lyd_new_output_leaf(node, NULL, "output-leaf1", "test");
507 if (!result) {
508 fail();
509 }
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100510
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100511 assert_string_equal("test", result->value_str);
512
Michal Vasko98a5a742016-05-11 11:02:56 +0200513 lyd_free(node);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100514}
515
516static void
Michal Vasko8681b662016-03-24 13:26:36 +0100517test_lyd_new_path(void **state)
518{
519 (void) state; /* unused */
520 struct lyd_node *node, *root;
Radek Krejci45826012016-08-24 15:07:57 +0200521 char *str;
522 struct lyxml_elem *xml;
Michal Vasko8681b662016-03-24 13:26:36 +0100523
Radek Krejci45826012016-08-24 15:07:57 +0200524 root = lyd_new_path(NULL, ctx, "/a:x/bar-gggg", "a", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100525 assert_non_null(root);
526 assert_string_equal(root->schema->name, "x");
527 assert_string_equal(root->child->schema->name, "bar-gggg");
528
Michal Vasko3c0eb752018-02-08 16:09:19 +0100529 /* create a default node first, then implicitly rewrite it, then fail to rewrite it again */
530 node = lyd_new_path(root, NULL, "def-leaf", "def", 0, LYD_PATH_OPT_DFLT);
531 assert_non_null(node);
532 assert_string_equal(node->schema->name, "def-leaf");
533 assert_int_equal(node->dflt, 1);
534
535 node = lyd_new_path(root, NULL, "def-leaf", "def", 0, 0);
536 assert_non_null(node);
537 assert_int_equal(node->dflt, 0);
538
539 node = lyd_new_path(root, NULL, "def-leaf", "def", 0, 0);
540 assert_null(node);
541 assert_int_equal(ly_errno, LY_EVALID);
542 ly_errno = 0;
543
Radek Krejci45826012016-08-24 15:07:57 +0200544 node = lyd_new_path(root, NULL, "bubba", "b", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100545 assert_non_null(node);
546 assert_string_equal(node->schema->name, "bubba");
547
Radek Krejci45826012016-08-24 15:07:57 +0200548 node = lyd_new_path(root, NULL, "/a:x/number32", "3", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100549 assert_non_null(node);
550 assert_string_equal(node->schema->name, "number32");
551
Radek Krejci45826012016-08-24 15:07:57 +0200552 node = lyd_new_path(root, NULL, "a:number64", "64", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100553 assert_non_null(node);
554 assert_string_equal(node->schema->name, "number64");
555
Radek Krejci45826012016-08-24 15:07:57 +0200556 node = lyd_new_path(root, NULL, "/a:l[key1='111'][key2='222']", NULL, 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100557 assert_non_null(node);
558 assert_string_equal(node->schema->name, "l");
559 assert_ptr_not_equal(root->prev, root);
560
Radek Krejci4d0d9352016-03-31 12:45:42 +0200561 lyd_free_withsiblings(root);
Michal Vasko8681b662016-03-24 13:26:36 +0100562
Radek Krejci45826012016-08-24 15:07:57 +0200563 root = lyd_new_path(NULL, ctx, "/a:l[key1='1'][key2='2']", NULL, 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100564 assert_non_null(root);
565 assert_string_equal(root->schema->name, "l");
566 assert_string_equal(root->child->schema->name, "key1");
567 assert_string_equal(root->child->next->schema->name, "key2");
568
Radek Krejci45826012016-08-24 15:07:57 +0200569 node = lyd_new_path(root, NULL, "/a:l[key1='11'][key2='22']/value", "val", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100570 assert_non_null(node);
571 assert_ptr_not_equal(root->prev, root);
572 assert_string_equal(node->schema->name, "l");
573 assert_string_equal(node->child->schema->name, "key1");
574 assert_string_equal(node->child->next->schema->name, "key2");
575 assert_string_equal(node->child->next->next->schema->name, "value");
576
Radek Krejci45826012016-08-24 15:07:57 +0200577 node = lyd_new_path(root, NULL, "/a:l[key1='1'][key2='2']/value", "val2", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100578 assert_non_null(node);
579 assert_string_equal(node->schema->name, "value");
580
Radek Krejci4d0d9352016-03-31 12:45:42 +0200581 lyd_free_withsiblings(root);
Michal Vasko8681b662016-03-24 13:26:36 +0100582
Radek Krejci45826012016-08-24 15:07:57 +0200583 root = lyd_new_path(NULL, ctx, "/a:any", "test <&>\"", LYD_ANYDATA_CONSTSTRING, 0);
584 assert_non_null(root);
585 str = NULL;
586 lyd_print_mem(&str, root, LYD_XML, 0);
587 assert_non_null(root);
588 assert_string_equal(str, "<any xmlns=\"urn:a\">test &lt;&amp;&gt;&quot;</any>");
589 free(str);
590 lyd_free(root);
591
592 xml = lyxml_parse_mem(ctx, "<test>&lt;</test>", 0);
593 assert_non_null(xml);
594 root = lyd_new_path(NULL, ctx, "/a:any", xml, LYD_ANYDATA_XML, 0);
595 xml = NULL;
596 assert_non_null(root);
597 lyd_print_mem(&str, root, LYD_XML, 0);
598 assert_non_null(root);
599 assert_string_equal(str, "<any xmlns=\"urn:a\"><test>&lt;</test></any>");
600 free(str);
601
602 node = root;
603 root = lyd_new_path(NULL, ctx, "/a:any", node, LYD_ANYDATA_DATATREE, 0);
604 assert_non_null(root);
605 str = NULL;
606 lyd_print_mem(&str, root, LYD_XML, 0);
607 assert_non_null(root);
608 assert_string_equal(str, "<any xmlns=\"urn:a\"><any xmlns=\"urn:a\"><test>&lt;</test></any></any>");
609 free(str);
610 lyd_free(root);
611
612 root = lyd_new_path(NULL, ctx, "/a:rpc1/x/input-leaf2", "dudu", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100613 assert_non_null(root);
614 assert_string_equal(root->schema->name, "rpc1");
615 assert_string_equal(root->child->schema->name, "x");
616 assert_string_equal(root->child->child->schema->name, "input-leaf2");
617
Radek Krejci45826012016-08-24 15:07:57 +0200618 node = lyd_new_path(root, NULL, "/a:rpc1/input-leaf1", "bubu", 0, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100619 assert_non_null(node);
620 assert_string_equal(node->schema->name, "input-leaf1");
621 assert_string_equal(root->child->schema->name, "input-leaf1");
622 assert_string_equal(root->child->next->schema->name, "x");
623
624 lyd_free(root);
625
Radek Krejci45826012016-08-24 15:07:57 +0200626 root = lyd_new_path(NULL, ctx, "/a:rpc1/rpc-container", NULL, 0, LYD_PATH_OPT_OUTPUT);
Michal Vasko8681b662016-03-24 13:26:36 +0100627 assert_non_null(root);
Michal Vasko98a5a742016-05-11 11:02:56 +0200628 assert_string_equal(root->schema->name, "rpc1");
Michal Vasko8681b662016-03-24 13:26:36 +0100629
Radek Krejci45826012016-08-24 15:07:57 +0200630 node = lyd_new_path(root->child, NULL, "output-leaf3", "cc", 0, LYD_PATH_OPT_OUTPUT);
Michal Vasko8681b662016-03-24 13:26:36 +0100631 assert_non_null(node);
632 assert_string_equal(node->schema->name, "output-leaf3");
633
Radek Krejci45826012016-08-24 15:07:57 +0200634 node = lyd_new_path(root, NULL, "/a:rpc1/output-leaf1", "aa", 0, LYD_PATH_OPT_OUTPUT);
Michal Vasko8681b662016-03-24 13:26:36 +0100635 assert_non_null(node);
636 assert_string_equal(node->schema->name, "output-leaf1");
Michal Vasko98a5a742016-05-11 11:02:56 +0200637 assert_ptr_equal(node, root->child);
Michal Vasko8681b662016-03-24 13:26:36 +0100638
Radek Krejci45826012016-08-24 15:07:57 +0200639 node = lyd_new_path(root, NULL, "/a:rpc1/output-leaf2", "bb", 0, LYD_PATH_OPT_OUTPUT);
Michal Vasko8681b662016-03-24 13:26:36 +0100640 assert_non_null(node);
641 assert_string_equal(node->schema->name, "output-leaf2");
642 assert_string_equal(node->prev->schema->name, "output-leaf1");
643 assert_string_equal(node->next->schema->name, "rpc-container");
644
Michal Vasko98a5a742016-05-11 11:02:56 +0200645 lyd_free(root);
Michal Vasko58c2aab2017-01-05 10:02:05 +0100646
647 root = lyd_new_path(NULL, ctx, "/a:l", NULL, 0, 0);
648 assert_non_null(root);
649 assert_string_equal(root->schema->name, "l");
650
651 node = lyd_new_path(root, NULL, "/a:l[key1='11'][key2='22']/value", "val", 0, 0);
652 assert_non_null(node);
653 assert_ptr_not_equal(root->prev, root);
654 assert_string_equal(node->schema->name, "l");
655 assert_string_equal(node->child->schema->name, "key1");
656
657 node = lyd_new_path(root, NULL, "/a:l[1]/key1", "1", 0, 0);
658 assert_non_null(node);
659 assert_string_equal(node->schema->name, "key1");
660
661 node = lyd_new_path(root, NULL, "/a:l[1]/key2", "2", 0, 0);
662 assert_non_null(node);
663 assert_string_equal(node->schema->name, "key2");
664
665 node = lyd_new_path(root, NULL, "/a:l[1]/value", "vall", 0, 0);
666 assert_non_null(node);
667 assert_string_equal(node->schema->name, "value");
668
669 node = lyd_new_path(root, NULL, "/a:l[2]/value", "vall", 0, 0);
670 assert_null(node);
671
672 node = lyd_new_path(root, NULL, "/a:l[3]/value", "vall", 0, 0);
673 assert_non_null(node);
674 assert_string_equal(node->schema->name, "l");
675
676 node = lyd_new_path(root, NULL, "/a:l[0]", NULL, 0, 0);
677 assert_null(node);
678
679 lyd_free_withsiblings(root);
Michal Vasko8681b662016-03-24 13:26:36 +0100680}
681
682static void
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100683test_lyd_dup(void **state)
684{
685 (void) state; /* unused */
686 struct lyd_node *copy = NULL;
687
688 copy = lyd_dup(root->child, 0);
689 if (!copy) {
690 fail();
691 }
692
693 assert_string_equal("bubba", copy->schema->name);
694
Michal Vaskobf704642016-06-06 16:24:07 +0200695 lyd_free(copy);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100696}
697
698static void
699test_lyd_insert(void **state)
700{
701 (void) state; /* unused */
702 struct lyd_node *new = NULL;
703 struct lyd_node *node = root;
704 struct lyd_node_leaf_list *result;
705 int rc;
706
707 result = (struct lyd_node_leaf_list *) root->child;
708 assert_string_equal("test", result->value_str);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100709
710 new = lyd_new_leaf(root, node->schema->module, "number32", "100");
711 if (!new) {
712 fail();
713 }
714
715 rc = lyd_insert(node, new);
716 if (rc) {
717 fail();
718 }
719
Radek Krejci5f001dd2016-09-07 16:26:40 +0200720 result = (struct lyd_node_leaf_list *) root->child->prev;
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100721 assert_string_equal("100", result->value_str);
Radek Krejci76b4d7d2016-09-09 10:18:31 +0200722
723 /* test inserting an empty container that is already present */
724 node = NULL;
725 assert_int_equal(lyd_validate(&node, LYD_OPT_CONFIG, ctx), 0);
726 assert_ptr_not_equal(node, NULL);
727 assert_string_equal(node->schema->name, "top");
728 assert_ptr_not_equal(node->child, NULL);
729 assert_string_equal(node->child->schema->name, "bar-sub2");
730 assert_int_equal(node->child->dflt, 1);
731
732 new = lyd_new_path(NULL, ctx, "/a:top/bar-sub2", NULL, 0, 0);
733 assert_ptr_not_equal(new, NULL);
734 assert_string_equal(new->schema->name, "top");
735 assert_int_equal(lyd_insert(node, new->child), 0);
736 assert_int_not_equal(node->child->dflt, 1);
737
738 lyd_free_withsiblings(node);
739 lyd_free_withsiblings(new);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100740}
741
742static void
Radek Krejci434780f2016-09-16 15:28:27 +0200743test_lyd_insert_sibling(void **state)
744{
745 (void) state; /* unused */
746 struct lyd_node *new = NULL, *last;
747
748 new = lyd_new_leaf(NULL, root->schema->module, "y", "test");
749 assert_ptr_not_equal(new, NULL);
750 last = root->prev;
751
752 assert_int_equal(lyd_insert_sibling(&root, new), 0);
753 assert_ptr_not_equal(last, root->prev);
754 assert_string_equal(root->prev->schema->name, "y");
755 assert_string_equal(((struct lyd_node_leaf_list *)root->prev)->value_str, "test");
756}
757
758static void
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100759test_lyd_insert_before(void **state)
760{
761 (void) state; /* unused */
762 struct lyd_node *new = NULL;
763 struct lyd_node_leaf_list *result;
764 int rc;
765
766 result = (struct lyd_node_leaf_list *) root->child;
767 assert_string_equal("test", result->value_str);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100768
769 new = lyd_new_leaf(root, root->child->schema->module, "number32", "1");
770 if (!new) {
771 fail();
772 }
773
774 rc = lyd_insert(root, new);
775 if (rc) {
776 fail();
777 }
778
779 new = lyd_new_leaf(root, root->child->schema->module, "number64", "1000");
780 if (!new) {
781 fail();
782 }
783
784 rc = lyd_insert_before(root->child->next, new);
785 if (rc) {
786 fail();
787 }
788
Radek Krejci5f001dd2016-09-07 16:26:40 +0200789 result = (struct lyd_node_leaf_list *) root->child->prev;
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100790 assert_string_equal("1", result->value_str);
791}
792
793static void
794test_lyd_insert_after(void **state)
795{
796 (void) state; /* unused */
797 struct lyd_node *new = NULL;
798 struct lyd_node_leaf_list *result;
799 int rc;
800
801 result = (struct lyd_node_leaf_list *) root->child;
802 assert_string_equal("test", result->value_str);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100803
804 new = lyd_new_leaf(root, root->child->schema->module, "number32", "1");
805 if (!new) {
806 fail();
807 }
808
809 rc = lyd_insert(root, new);
810 if (rc) {
811 fail();
812 }
813
814 new = lyd_new_leaf(root, root->child->schema->module, "number64", "1000");
815 if (!new) {
816 fail();
817 }
818
819 rc = lyd_insert_after(root->child->next, new);
820 if (rc) {
821 fail();
822 }
823
824 result = (struct lyd_node_leaf_list *) root->child->next->next;
825 assert_string_equal("1000", result->value_str);
826}
827
828static void
Michal Vasko8681b662016-03-24 13:26:36 +0100829test_lyd_schema_sort(void **state)
830{
831 (void) state; /* unused */
832 const struct lys_module *module;
Michal Vasko8681b662016-03-24 13:26:36 +0100833 struct lyd_node *root, *node, *node2;
834
Radek Krejcidfb00d62017-09-06 09:39:35 +0200835 module = ly_ctx_get_module(ctx, "a", NULL, 0);
Michal Vasko8681b662016-03-24 13:26:36 +0100836 assert_non_null(module);
837
838 root = lyd_new(NULL, module, "l");
839 assert_non_null(root);
840 node = lyd_new_leaf(root, NULL, "key1", "1");
841 assert_non_null(node);
842 node = lyd_new_leaf(root, NULL, "key2", "2");
843 assert_non_null(node);
844
845 node = lyd_new(NULL, module, "x");
846 assert_non_null(node);
847 assert_int_equal(lyd_insert_after(root, node), 0);
848
849 node2 = lyd_new_leaf(node, NULL, "bubba", "a");
850 assert_non_null(node2);
851 node2 = lyd_new_leaf(node, NULL, "bar-gggg", "b");
852 assert_non_null(node2);
853 node2 = lyd_new_leaf(node, NULL, "number64", "64");
854 assert_non_null(node2);
855 node2 = lyd_new_leaf(node, NULL, "number32", "32");
856 assert_non_null(node2);
857
858 assert_int_equal(lyd_schema_sort(root, 1), 0);
859
860 root = node;
861 assert_string_equal(root->schema->name, "x");
862 assert_string_equal(root->next->schema->name, "l");
863
864 assert_string_equal(root->child->schema->name, "bar-gggg");
865 assert_string_equal(root->child->next->schema->name, "bubba");
866 assert_string_equal(root->child->next->next->schema->name, "number32");
867 assert_string_equal(root->child->next->next->next->schema->name, "number64");
868
869 lyd_free_withsiblings(root);
870
Michal Vasko98a5a742016-05-11 11:02:56 +0200871 root = lyd_new_output(NULL, module, "rpc1");
Michal Vasko8681b662016-03-24 13:26:36 +0100872 assert_non_null(root);
Michal Vasko98a5a742016-05-11 11:02:56 +0200873 node = lyd_new_output(root, NULL, "rpc-container");
874 assert_non_null(node);
875 node2 = lyd_new_output_leaf(node, NULL, "output-leaf3", "ff");
Michal Vasko8681b662016-03-24 13:26:36 +0100876 assert_non_null(node2);
877
Michal Vasko98a5a742016-05-11 11:02:56 +0200878 node = lyd_new_output_leaf(root, NULL, "output-leaf1", "gg");
Michal Vasko8681b662016-03-24 13:26:36 +0100879 assert_non_null(node);
Michal Vasko8681b662016-03-24 13:26:36 +0100880
Michal Vasko98a5a742016-05-11 11:02:56 +0200881 node = lyd_new_output_leaf(root, NULL, "output-leaf2", "hh");
Michal Vasko8681b662016-03-24 13:26:36 +0100882 assert_non_null(node);
Michal Vasko8681b662016-03-24 13:26:36 +0100883
884 assert_int_equal(lyd_schema_sort(root, 1), 0);
885
Michal Vasko98a5a742016-05-11 11:02:56 +0200886 node = root->child;
887 assert_string_equal(node->schema->name, "output-leaf1");
888 assert_string_equal(node->next->schema->name, "output-leaf2");
889 assert_string_equal(node->next->next->schema->name, "rpc-container");
Michal Vasko8681b662016-03-24 13:26:36 +0100890
891 lyd_free_withsiblings(root);
892}
893
894static void
Michal Vasko50576712017-07-28 12:28:33 +0200895test_lyd_find_path(void **state)
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100896{
897 (void) state; /* unused */
898 struct ly_set *set = NULL;
899 struct lyd_node_leaf_list *result;
900
Michal Vasko50576712017-07-28 12:28:33 +0200901 set = lyd_find_path(root->child, "/a:x/bubba");
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100902
903 struct lyd_node *node = *set->set.d;
904 result = (struct lyd_node_leaf_list *) node;
905 assert_string_equal("test", result->value_str);
906
907 ly_set_free(set);
908}
909
910static void
Michal Vaskof06fb5b2016-09-08 10:05:56 +0200911test_lyd_find_instance(void **state)
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100912{
913 (void) state; /* unused */
914 struct ly_set *set = NULL;
915 struct lyd_node_leaf_list *result;
916
Michal Vaskof06fb5b2016-09-08 10:05:56 +0200917 set = lyd_find_instance(root->child, root->child->schema);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100918 if (!set) {
919 fail();
920 }
921
922 struct lyd_node *node = *set->set.d;
923 result = (struct lyd_node_leaf_list *) node;
924 assert_string_equal("test", result->value_str);
925
926 ly_set_free(set);
927}
928
929static void
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100930test_lyd_validate(void **state)
931{
932 (void) state; /* unused */
933 struct lyd_node *new = NULL;
934 struct lyd_node *node = root;
935 int rc;
936
Michal Vaskocdb90172016-09-13 09:34:36 +0200937 rc = lyd_validate(&root, LYD_OPT_CONFIG, NULL);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100938 if (rc) {
939 fail();
940 }
941
Michal Vasko3c0eb752018-02-08 16:09:19 +0100942 if (root->child->next->next) {
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100943 fail();
944 }
945
946 new = lyd_new_leaf(root, node->schema->module, "number32", "1");
947 if (!new) {
948 fail();
949 }
950
951 rc = lyd_insert(root, new);
952 if (rc) {
953 fail();
954 }
955
Michal Vaskocdb90172016-09-13 09:34:36 +0200956 rc = lyd_validate(&root, LYD_OPT_CONFIG, NULL);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100957 if (rc) {
958 fail();
959 }
960}
961
962static void
963test_lyd_unlink(void **state)
964{
965 (void) state; /* unused */
966 struct lyd_node *new = NULL;
967 struct lyd_node *node = root->child;
968 struct lyd_node_leaf_list *result;
969 int rc;
970
971 new = lyd_new_leaf(root, node->schema->module, "number32", "1");
972 if (!new) {
973 fail();
974 }
975
976 rc = lyd_insert(root, new);
977 if (rc) {
978 fail();
979 }
980
Radek Krejci5f001dd2016-09-07 16:26:40 +0200981 result = (struct lyd_node_leaf_list *) node->prev;
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100982 assert_string_equal("1", result->value_str);
983
Radek Krejci5f001dd2016-09-07 16:26:40 +0200984 rc = lyd_unlink(node->prev);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100985 if (rc) {
986 fail();
987 }
988
Radek Krejci5f001dd2016-09-07 16:26:40 +0200989 assert_string_not_equal("1", ((struct lyd_node_leaf_list *)node->prev)->value_str);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100990
Michal Vaskobf704642016-06-06 16:24:07 +0200991 lyd_free(new);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +0100992}
993
994static void
995test_lyd_free(void **state)
996{
997 (void) state; /* unused */
998 struct lyd_node *copy = NULL;
999
1000 copy = lyd_dup(root->child, 0);
1001 if (!copy) {
1002 fail();
1003 }
1004
1005 lyd_free(copy);
1006}
1007
1008static void
1009test_lyd_free_withsiblings(void **state)
1010{
1011 (void) state; /* unused */
1012 struct lyd_node *copy = NULL;
1013 struct lyd_node *new = NULL;
1014 struct lyd_node *node = root->child;
1015 int rc;
1016
1017 copy = lyd_dup(root->child, 0);
1018 if (!copy) {
1019 fail();
1020 }
1021
1022 new = lyd_new_leaf(root, node->schema->module, "number32", "1");
1023 if (!new) {
1024 fail();
1025 }
1026
1027 rc = lyd_insert(copy, new);
1028 if (!rc) {
1029 fail();
1030 }
1031
1032 lyd_free_withsiblings(copy);
1033}
1034
1035static void
1036test_lyd_insert_attr(void **state)
1037{
1038 (void) state; /* unused */
1039 struct lyd_attr *attr = NULL;
1040 struct lyd_node *node = root->child;
1041
Radek Krejci1e6bc0c2016-03-23 14:15:17 +01001042 attr = lyd_insert_attr(node, NULL, "test", "test");
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001043 if (!attr) {
1044 fail();
1045 }
1046
1047 assert_string_equal("test", node->attr->name);
1048}
1049
1050static void
1051test_lyd_free_attr(void **state)
1052{
1053 (void) state; /* unused */
1054 struct lyd_attr *attr = NULL;
1055 struct lyd_node *node = root->child;
1056
Radek Krejci1e6bc0c2016-03-23 14:15:17 +01001057 attr = lyd_insert_attr(node, NULL, "test", "test");
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001058
1059 if (!node->attr) {
1060 fail();
1061 }
1062
1063 lyd_free_attr(ctx, node, attr, 0);
1064
1065 if (node->attr) {
1066 fail();
1067 }
1068
1069}
1070
1071static void
1072test_lyd_print_mem_xml(void **state)
1073{
1074 (void) state; /* unused */
1075 char *result = NULL;
1076 int rc;
1077
1078 rc = lyd_print_mem(&result, root, LYD_XML, 0);
1079 if (rc) {
1080 fail();
1081 }
1082
1083 assert_string_equal(result_xml, result);
1084 free(result);
1085}
1086
1087static void
1088test_lyd_print_mem_xml_format(void **state)
1089{
1090 (void) state; /* unused */
1091 char *result = NULL;
1092 int rc;
1093
Michal Vasko95068c42016-03-24 14:58:11 +01001094 rc = lyd_print_mem(&result, root, LYD_XML, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001095 if (rc) {
1096 fail();
1097 }
1098
1099 assert_string_equal(result_xml_format, result);
1100 free(result);
1101}
1102
1103static void
1104test_lyd_print_mem_json(void **state)
1105{
1106 (void) state; /* unused */
1107 char *result = NULL;
1108 int rc;
1109
Michal Vasko95068c42016-03-24 14:58:11 +01001110 rc = lyd_print_mem(&result, root, LYD_JSON, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001111 if (rc) {
1112 fail();
1113 }
1114
1115 assert_string_equal(result_json, result);
1116 free(result);
1117}
1118
1119static void
1120test_lyd_print_fd_xml(void **state)
1121{
1122 (void) state; /* unused */
1123 char *result = NULL;
1124 struct stat sb;
Radek Krejcica73f502016-09-30 14:48:54 +02001125 char file_name[20];
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001126 int rc;
1127 int fd;
1128
1129 memset(file_name, 0, sizeof(file_name));
Denis Volpato Martinse7416282016-10-05 00:57:32 -03001130 strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE));
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001131
1132 fd = mkstemp(file_name);
1133 if (fd < 1) {
1134 goto error;
1135 }
1136
1137 rc = lyd_print_fd(fd, root, LYD_XML, 0);
1138 if (rc) {
1139 goto error;
1140 }
1141
1142 if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1143 goto error;
1144 }
1145
1146 result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1147
1148 assert_string_equal(result_xml, result);
1149
1150 close(fd);
1151 unlink(file_name);
1152
1153 return;
1154error:
1155 if (fd > 0) {
1156 close(fd);
1157 unlink(file_name);
1158 }
1159 fail();
1160}
1161
1162static void
1163test_lyd_print_fd_xml_format(void **state)
1164{
1165 (void) state; /* unused */
1166 char *result = NULL;
1167 struct stat sb;
Radek Krejcica73f502016-09-30 14:48:54 +02001168 char file_name[20];
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001169 int rc;
1170 int fd;
1171
1172 memset(file_name, 0, sizeof(file_name));
Denis Volpato Martinse7416282016-10-05 00:57:32 -03001173 strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE));
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001174
1175 fd = mkstemp(file_name);
1176 if (fd < 1) {
1177 goto error;
1178 }
1179
Michal Vasko95068c42016-03-24 14:58:11 +01001180 rc = lyd_print_fd(fd, root, LYD_XML, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001181 if (rc) {
1182 goto error;
1183 }
1184
1185 if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1186 goto error;
1187 }
1188
1189 result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1190
1191 assert_string_equal(result_xml_format, result);
1192
1193 close(fd);
1194 unlink(file_name);
1195
1196 return;
1197error:
1198 if (fd > 0) {
1199 close(fd);
1200 unlink(file_name);
1201 }
1202 fail();
1203}
1204
1205static void
1206test_lyd_print_fd_json(void **state)
1207{
1208 (void) state; /* unused */
1209 char *result = NULL;
1210 struct stat sb;
Radek Krejcica73f502016-09-30 14:48:54 +02001211 char file_name[20];
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001212 int rc;
1213 int fd;
1214
1215 memset(file_name, 0, sizeof(file_name));
Denis Volpato Martinse7416282016-10-05 00:57:32 -03001216 strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE));
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001217
1218 fd = mkstemp(file_name);
1219 if (fd < 1) {
1220 goto error;
1221 }
1222
Michal Vasko95068c42016-03-24 14:58:11 +01001223 rc = lyd_print_fd(fd, root, LYD_JSON, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001224 if (rc) {
1225 goto error;
1226 }
1227
1228 if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
1229 goto error;
1230 }
1231
1232 result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1233
1234 assert_string_equal(result_json, result);
1235
1236 close(fd);
1237 unlink(file_name);
1238
1239 return;
1240error:
1241 if (fd > 0) {
1242 close(fd);
1243 unlink(file_name);
1244 }
1245 fail();
1246}
1247
1248static void
1249test_lyd_print_file_xml(void **state)
1250{
1251 (void) state; /* unused */
1252 struct stat sb;
Radek Krejcica73f502016-09-30 14:48:54 +02001253 char file_name[20];
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001254 char *result;
1255 FILE *f = NULL;
1256 int rc;
1257 int fd;
1258
1259 memset(file_name, 0, sizeof(file_name));
Denis Volpato Martinse7416282016-10-05 00:57:32 -03001260 strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE));
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001261
1262 fd = mkstemp(file_name);
1263 if (fd < 1) {
1264 goto error;
1265 }
1266 close(fd);
1267
1268 f = (fopen(file_name,"r+"));
1269 if (f == NULL) {
1270 goto error;
1271 }
1272
1273 rc = lyd_print_file(f, root, LYD_XML, 0);
1274 if (rc) {
1275 goto error;
1276 }
1277
1278 fclose(f);
1279
1280 fd = open(file_name, O_RDONLY);
Radek Krejci849ba332016-03-24 10:23:16 +01001281 if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001282 goto error;
1283 }
1284
1285 result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1286
1287 assert_string_equal(result_xml, result);
1288
1289 close(fd);
1290 unlink(file_name);
1291
1292 return;
1293error:
1294 if (f)
1295 fclose(f);
1296 if (fd > 0) {
1297 unlink(file_name);
1298 close(fd);
1299 }
1300 fail();
1301}
1302
1303static void
1304test_lyd_print_file_xml_format(void **state)
1305{
1306 (void) state; /* unused */
1307 struct stat sb;
Radek Krejcica73f502016-09-30 14:48:54 +02001308 char file_name[20];
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001309 char *result;
1310 FILE *f = NULL;
1311 int rc;
1312 int fd;
1313
1314 memset(file_name, 0, sizeof(file_name));
Denis Volpato Martinse7416282016-10-05 00:57:32 -03001315 strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE));
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001316
1317 fd = mkstemp(file_name);
1318 if (fd < 1) {
1319 goto error;
1320 }
1321 close(fd);
1322
1323 f = (fopen(file_name,"r+"));
1324 if (f == NULL) {
1325 goto error;
1326 }
1327
Michal Vasko95068c42016-03-24 14:58:11 +01001328 rc = lyd_print_file(f, root, LYD_XML, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001329 if (rc) {
1330 goto error;
1331 }
1332
1333 fclose(f);
1334
1335 fd = open(file_name, O_RDONLY);
Radek Krejci849ba332016-03-24 10:23:16 +01001336 if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001337 goto error;
1338 }
1339
1340 result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1341
1342 assert_string_equal(result_xml_format, result);
1343
1344 close(fd);
1345 unlink(file_name);
1346
1347 return;
1348error:
1349 if (f)
1350 fclose(f);
1351 if (fd > 0) {
1352 unlink(file_name);
1353 close(fd);
1354 }
1355 fail();
1356}
1357
1358static void
1359test_lyd_print_file_json(void **state)
1360{
1361 (void) state; /* unused */
1362 struct stat sb;
Radek Krejcica73f502016-09-30 14:48:54 +02001363 char file_name[20];
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001364 char *result;
1365 FILE *f = NULL;
1366 int rc;
Radek Krejci849ba332016-03-24 10:23:16 +01001367 int fd = -1;
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001368
1369 memset(file_name, 0, sizeof(file_name));
Denis Volpato Martinse7416282016-10-05 00:57:32 -03001370 strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE));
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001371
1372 fd = mkstemp(file_name);
1373 if (fd < 1) {
1374 goto error;
1375 }
1376 close(fd);
1377
1378 f = (fopen(file_name,"r+"));
1379 if (f == NULL) {
1380 goto error;
1381 }
1382
Michal Vasko95068c42016-03-24 14:58:11 +01001383 rc = lyd_print_file(f, root, LYD_JSON, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001384 if (rc) {
1385 goto error;
1386 }
1387
1388 fclose(f);
1389
1390 fd = open(file_name, O_RDONLY);
Radek Krejci849ba332016-03-24 10:23:16 +01001391 if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) {
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001392 goto error;
1393 }
1394
1395 result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1396
1397 assert_string_equal(result_json, result);
1398
1399 close(fd);
1400 unlink(file_name);
1401
1402 return;
1403error:
1404 if (f)
1405 fclose(f);
1406 if (fd > 0) {
1407 unlink(file_name);
1408 close(fd);
1409 }
1410 fail();
1411}
1412
1413struct buff {
1414 int len;
1415 const char *cmp;
1416};
1417
1418ssize_t custom_lyd_print_clb(void *arg, const void *buf, size_t count) {
1419 int rc;
1420 int len;
1421 struct buff *pos = arg;
1422
1423 len = pos->len + count;
1424
1425 const char *chunk = &pos->cmp[pos->len];
1426
1427 rc = strncmp(chunk, buf, count);
1428 if (rc) {
1429 fail();
1430 }
1431
1432 pos->len = len;
1433 return count;
1434}
1435
1436static void
1437test_lyd_print_clb_xml(void **state)
1438{
1439 (void) state; /* unused */
1440 int rc;
1441
1442 struct buff *buf = calloc(1, sizeof(struct buff));
1443 if (!buf) {
1444 fail();
1445 }
1446
1447 buf->len = 0;
1448 buf->cmp = result_xml;
1449 void *arg = buf;
1450
1451 rc = lyd_print_clb(custom_lyd_print_clb, arg, root, LYD_XML, 0);
1452 if (rc) {
1453 fail();
1454 free(buf);
1455 }
1456
1457 free(buf);
1458}
1459
1460static void
1461test_lyd_print_clb_xml_format(void **state)
1462{
1463 (void) state; /* unused */
1464 int rc;
1465
1466 struct buff *buf = calloc(1, sizeof(struct buff));
1467 if (!buf) {
1468 fail();
1469 }
1470
1471 buf->len = 0;
1472 buf->cmp = result_xml_format;
1473 void *arg = buf;
1474
Michal Vasko95068c42016-03-24 14:58:11 +01001475 rc = lyd_print_clb(custom_lyd_print_clb, arg, root, LYD_XML, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001476 if (rc) {
1477 fail();
1478 free(buf);
1479 }
1480
1481 free(buf);
1482}
1483
1484static void
1485test_lyd_print_clb_json(void **state)
1486{
1487 (void) state; /* unused */
1488 int rc;
1489
1490 struct buff *buf = calloc(1, sizeof(struct buff));
1491 if (!buf) {
1492 fail();
1493 }
1494
1495 buf->len = 0;
1496 buf->cmp = result_json;
1497 void *arg = buf;
1498
Michal Vasko95068c42016-03-24 14:58:11 +01001499 rc = lyd_print_clb(custom_lyd_print_clb, arg, root, LYD_JSON, LYP_FORMAT);
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001500 if (rc) {
1501 fail();
1502 free(buf);
1503 }
1504
1505 free(buf);
1506}
1507
Radek Krejci6d538282016-05-05 14:24:12 +02001508static void
1509test_lyd_path(void **state)
1510{
1511 (void) state; /* unused */
1512 char *str;
1513
1514 str = lyd_path(root);
1515 assert_ptr_not_equal(str, NULL);
1516 assert_string_equal(str, "/a:x");
1517 free(str);
1518
1519 str = lyd_path(root->child);
1520 assert_ptr_not_equal(str, NULL);
1521 assert_string_equal(str, "/a:x/bubba");
1522 free(str);
1523}
1524
Michal Vasko20399fb2017-01-09 11:11:07 +01001525static void
1526test_lyd_leaf_type(void **state)
1527{
1528 struct ly_ctx *ctx = (struct ly_ctx *)*state;
1529 const char *yang = "module x {"
1530" namespace urn:x;"
1531" prefix x;"
1532" yang-version 1.1;"
1533" container x {"
1534" leaf str { type string; }"
1535" leaf e { type enumeration { enum ftp; enum http; } }"
1536" leaf u { type union {"
1537" type leafref { path \"../e\"; }"
1538" type leafref { path \"../str\";}"
1539" } }"
1540"} }";
1541 const char *xml1 = "<x xmlns=\"urn:x\"><str>http</str><e>ftp</e><u>http</u></x>";
1542 const char *xml2 = "<x xmlns=\"urn:x\"><str>http</str><e>ftp</e><u>ftp</u></x>";
1543 struct lyd_node *data;
1544
1545 assert_ptr_not_equal(lys_parse_mem(ctx, yang, LYS_IN_YANG), 0);
1546
1547 data = lyd_parse_mem(ctx, xml1, LYD_XML, LYD_OPT_CONFIG);
1548 assert_ptr_not_equal(data, NULL);
1549 assert_int_equal(lyd_leaf_type((struct lyd_node_leaf_list *)data->child->prev)->base, LY_TYPE_STRING);
1550 lyd_free_withsiblings(data);
1551
1552 data = lyd_parse_mem(ctx, xml2, LYD_XML, LYD_OPT_CONFIG);
1553 assert_ptr_not_equal(data, NULL);
1554 assert_int_equal(lyd_leaf_type((struct lyd_node_leaf_list *)data->child->prev)->base, LY_TYPE_ENUM);
1555 lyd_free_withsiblings(data);
1556
1557 data = lyd_parse_mem(ctx, xml2, LYD_XML, LYD_OPT_CONFIG);
1558 assert_ptr_not_equal(data, NULL);
1559 lyd_change_leaf((struct lyd_node_leaf_list *)data->child->prev, "ssh");
1560 assert_int_equal(lyd_leaf_type((struct lyd_node_leaf_list *)data->child->prev), NULL);
1561 lyd_free_withsiblings(data);
1562}
1563
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001564int main(void)
1565{
1566 const struct CMUnitTest tests[] = {
1567 cmocka_unit_test(test_lyd_parse_mem),
1568 cmocka_unit_test(test_lyd_parse_fd),
1569 cmocka_unit_test(test_lyd_parse_path),
1570 cmocka_unit_test(test_lyd_parse_xml),
1571 cmocka_unit_test_setup_teardown(test_lyd_new, setup_f, teardown_f),
1572 cmocka_unit_test_setup_teardown(test_lyd_new_leaf, setup_f, teardown_f),
1573 cmocka_unit_test_setup_teardown(test_lyd_change_leaf, setup_f, teardown_f),
1574 cmocka_unit_test_setup_teardown(test_lyd_output_new_leaf, setup_f, teardown_f),
Michal Vasko8681b662016-03-24 13:26:36 +01001575 cmocka_unit_test_setup_teardown(test_lyd_new_path, setup_f, teardown_f),
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001576 cmocka_unit_test_setup_teardown(test_lyd_dup, setup_f, teardown_f),
1577 cmocka_unit_test_setup_teardown(test_lyd_insert, setup_f, teardown_f),
Radek Krejci434780f2016-09-16 15:28:27 +02001578 cmocka_unit_test_setup_teardown(test_lyd_insert_sibling, setup_f, teardown_f),
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001579 cmocka_unit_test_setup_teardown(test_lyd_insert_before, setup_f, teardown_f),
1580 cmocka_unit_test_setup_teardown(test_lyd_insert_after, setup_f, teardown_f),
Michal Vasko8681b662016-03-24 13:26:36 +01001581 cmocka_unit_test_setup_teardown(test_lyd_schema_sort, setup_f, teardown_f),
Michal Vasko50576712017-07-28 12:28:33 +02001582 cmocka_unit_test_setup_teardown(test_lyd_find_path, setup_f, teardown_f),
Michal Vaskof06fb5b2016-09-08 10:05:56 +02001583 cmocka_unit_test_setup_teardown(test_lyd_find_instance, setup_f, teardown_f),
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001584 cmocka_unit_test_setup_teardown(test_lyd_validate, setup_f, teardown_f),
1585 cmocka_unit_test_setup_teardown(test_lyd_unlink, setup_f, teardown_f),
1586 cmocka_unit_test_setup_teardown(test_lyd_free, setup_f, teardown_f),
1587 cmocka_unit_test_setup_teardown(test_lyd_free_withsiblings, setup_f, teardown_f),
1588 cmocka_unit_test_setup_teardown(test_lyd_insert_attr, setup_f, teardown_f),
1589 cmocka_unit_test_setup_teardown(test_lyd_free_attr, setup_f, teardown_f),
1590 cmocka_unit_test_setup_teardown(test_lyd_print_mem_xml, setup_f, teardown_f),
1591 cmocka_unit_test_setup_teardown(test_lyd_print_mem_xml_format, setup_f, teardown_f),
1592 cmocka_unit_test_setup_teardown(test_lyd_print_mem_json, setup_f, teardown_f),
1593 cmocka_unit_test_setup_teardown(test_lyd_print_fd_xml, setup_f, teardown_f),
1594 cmocka_unit_test_setup_teardown(test_lyd_print_fd_xml_format, setup_f, teardown_f),
1595 cmocka_unit_test_setup_teardown(test_lyd_print_fd_json, setup_f, teardown_f),
1596 cmocka_unit_test_setup_teardown(test_lyd_print_file_xml, setup_f, teardown_f),
1597 cmocka_unit_test_setup_teardown(test_lyd_print_file_xml_format, setup_f, teardown_f),
1598 cmocka_unit_test_setup_teardown(test_lyd_print_file_json, setup_f, teardown_f),
1599 cmocka_unit_test_setup_teardown(test_lyd_print_clb_xml, setup_f, teardown_f),
1600 cmocka_unit_test_setup_teardown(test_lyd_print_clb_xml_format, setup_f, teardown_f),
1601 cmocka_unit_test_setup_teardown(test_lyd_print_clb_json, setup_f, teardown_f),
Radek Krejci6d538282016-05-05 14:24:12 +02001602 cmocka_unit_test_setup_teardown(test_lyd_path, setup_f, teardown_f),
Michal Vasko20399fb2017-01-09 11:11:07 +01001603 cmocka_unit_test_setup_teardown(test_lyd_leaf_type, setup_f2, teardown_f2),
Mislav Novakovic1f160ab2016-03-23 12:46:48 +01001604 };
1605
1606 return cmocka_run_group_tests(tests, NULL, NULL);
1607}