blob: ccf50050ba09ab694611965e7e5d332e50683e99 [file] [log] [blame]
Radek Krejci6f60e462016-03-15 14:05:23 +01001/**
2 * @file test_autodel.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Cmocka tests for auto-delete of choice's case when a node from another case comes.
5 *
6 * Copyright (c) 2016 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <setjmp.h>
Renato Westphal41a91062018-05-19 22:23:58 -030018#include <stdarg.h>
Radek Krejci6f60e462016-03-15 14:05:23 +010019#include <cmocka.h>
20
Jan Kundrátfd7a5c72017-10-26 17:45:06 +020021#include "tests/config.h"
22#include "libyang.h"
Radek Krejci6f60e462016-03-15 14:05:23 +010023
24struct state {
25 struct ly_ctx *ctx;
26 struct lyd_node *dt;
Radek Krejci73ed0482016-03-17 17:01:14 +010027 struct lyd_node *aux;
Radek Krejci6f60e462016-03-15 14:05:23 +010028 char *xml;
29};
30static int
31setup_f(void **state)
32{
33 struct state *st;
34 const struct lys_module *mod;
35 const char *schemafile = TESTS_DIR"/data/files/autodel.yin";
36
37 (*state) = st = calloc(1, sizeof *st);
38 if (!st) {
39 fprintf(stderr, "Memory allocation error");
40 return -1;
41 }
42
43 /* libyang context */
Radek Krejcidd3263a2017-07-15 11:50:09 +020044 st->ctx = ly_ctx_new(NULL, 0);
Radek Krejci6f60e462016-03-15 14:05:23 +010045 if (!st->ctx) {
46 fprintf(stderr, "Failed to create context.\n");
47 goto error;
48 }
49
50 /* schema */
51 mod = lys_parse_path(st->ctx, schemafile, LYS_IN_YIN);
52 if (!mod) {
53 fprintf(stderr, "Failed to load data model \"%s\".\n", schemafile);
54 goto error;
55 }
56
57 /* root */
58 st->dt = lyd_new(NULL, mod, "c");
59 if (!st->dt) {
60 fprintf(stderr, "Failed to create data root.\n");
61 goto error;
62 }
63
64 return 0;
65
66error:
Radek Krejcid1865672016-09-09 13:11:19 +020067 lyd_free_withsiblings(st->dt);
Radek Krejci6f60e462016-03-15 14:05:23 +010068 ly_ctx_destroy(st->ctx, NULL);
69 free(st);
70 (*state) = NULL;
71
72 return -1;
73}
74
75static int
76teardown_f(void **state)
77{
78 struct state *st = (*state);
79
Radek Krejcid1865672016-09-09 13:11:19 +020080 lyd_free_withsiblings(st->dt);
81 lyd_free_withsiblings(st->aux);
Radek Krejci6f60e462016-03-15 14:05:23 +010082 ly_ctx_destroy(st->ctx, NULL);
83 free(st->xml);
84 free(st);
85 (*state) = NULL;
86
87 return 0;
88}
89
90static void
91test_atob(void **state)
92{
93 struct state *st = (*state);
94 struct lyd_node *node;
95
96 node = lyd_new_leaf(st->dt, NULL, "a", "x");
97 assert_ptr_not_equal(node, NULL);
98 assert_ptr_equal(st->dt->child, node);
Michal Vaskocdb90172016-09-13 09:34:36 +020099 assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100100
101 lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0);
102 assert_string_equal(st->xml, "<c xmlns=\"urn:autodel\"><a>x</a></c>");
103
104 node = lyd_new(st->dt, NULL, "b1");
105 assert_ptr_not_equal(node, NULL);
Michal Vaskocdb90172016-09-13 09:34:36 +0200106 assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100107
108 free(st->xml);
109 lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0);
110 assert_string_equal(st->xml, "<c xmlns=\"urn:autodel\"><b1/></c>");
111}
112
113static void
114test_btoa(void **state)
115{
116 struct state *st = (*state);
117 struct lyd_node *node;
118
119 node = lyd_new(st->dt, NULL, "b1");
120 assert_ptr_not_equal(node, NULL);
121 node = lyd_new_leaf(st->dt, NULL, "b2", "z");
122 assert_ptr_not_equal(node, NULL);
Michal Vaskocdb90172016-09-13 09:34:36 +0200123 assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100124
125 lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0);
126 assert_string_equal(st->xml, "<c xmlns=\"urn:autodel\"><b1/><b2>z</b2></c>");
127
128 node = lyd_new_leaf(st->dt, NULL, "a", "x");
129 assert_ptr_not_equal(node, NULL);
Michal Vaskocdb90172016-09-13 09:34:36 +0200130 assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100131
132 free(st->xml);
133 lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0);
134 assert_string_equal(st->xml, "<c xmlns=\"urn:autodel\"><a>x</a></c>");
135}
136
137static void
138test_insert_fail(void **state)
139{
140 struct state *st = (*state);
141 struct lyd_node *node;
Radek Krejci6f60e462016-03-15 14:05:23 +0100142
143 node = lyd_new_leaf(st->dt, NULL, "a", "x");
144 assert_ptr_not_equal(node, NULL);
145 assert_ptr_equal(st->dt->child, node);
146
Radek Krejci73ed0482016-03-17 17:01:14 +0100147 st->aux = lyd_new(NULL, st->dt->schema->module, "c");
148 assert_ptr_not_equal(st->aux, NULL);
149 node = lyd_new(st->aux, NULL, "b1");
Radek Krejci6f60e462016-03-15 14:05:23 +0100150 assert_ptr_not_equal(node, NULL);
Michal Vaskocdb90172016-09-13 09:34:36 +0200151 assert_int_equal(lyd_validate(&(st->aux), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100152
153 assert_int_not_equal(lyd_insert_after(st->dt->child, node), 0);
Michal Vasko53b7da02018-02-13 15:28:42 +0100154 assert_string_equal(ly_errmsg(st->ctx), "Insert request refers node (/autodel:c/a) that is going to be auto-deleted.");
Radek Krejci6f60e462016-03-15 14:05:23 +0100155}
156
157static void
158test_insert_correct(void **state)
159{
160 struct state *st = (*state);
161 struct lyd_node *node, *node2;
Radek Krejci6f60e462016-03-15 14:05:23 +0100162
163 node = lyd_new_leaf(st->dt, NULL, "a", "x");
164 assert_ptr_not_equal(node, NULL);
165 assert_ptr_equal(st->dt->child, node);
166 node2 = lyd_new_leaf(st->dt, NULL, "x", "node");
167 assert_ptr_not_equal(node, NULL);
168
Radek Krejci73ed0482016-03-17 17:01:14 +0100169 st->aux = lyd_new(NULL, st->dt->schema->module, "c");
170 assert_ptr_not_equal(st->aux, NULL);
171 node = lyd_new(st->aux, NULL, "b1");
Radek Krejci6f60e462016-03-15 14:05:23 +0100172 assert_ptr_not_equal(node, NULL);
Michal Vaskocdb90172016-09-13 09:34:36 +0200173 assert_int_equal(lyd_validate(&(st->aux), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100174
175 assert_int_equal(lyd_insert_after(node2, node), 0);
176 lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0);
177 assert_string_equal(st->xml, "<c xmlns=\"urn:autodel\"><x>node</x><b1/></c>");
Michal Vaskocdb90172016-09-13 09:34:36 +0200178 assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0);
Radek Krejci6f60e462016-03-15 14:05:23 +0100179}
180
181
182int main(void)
183{
184 const struct CMUnitTest tests[] = {
185 cmocka_unit_test_setup_teardown(test_atob, setup_f, teardown_f),
186 cmocka_unit_test_setup_teardown(test_btoa, setup_f, teardown_f),
187 cmocka_unit_test_setup_teardown(test_insert_fail, setup_f, teardown_f),
188 cmocka_unit_test_setup_teardown(test_insert_correct, setup_f, teardown_f), };
189
190 return cmocka_run_group_tests(tests, NULL, NULL);
191}