Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 1 | /** |
| 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 Westphal | 41a9106 | 2018-05-19 22:23:58 -0300 | [diff] [blame] | 18 | #include <stdarg.h> |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 19 | #include <cmocka.h> |
| 20 | |
Jan Kundrát | fd7a5c7 | 2017-10-26 17:45:06 +0200 | [diff] [blame] | 21 | #include "tests/config.h" |
| 22 | #include "libyang.h" |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 23 | |
| 24 | struct state { |
| 25 | struct ly_ctx *ctx; |
| 26 | struct lyd_node *dt; |
Radek Krejci | 73ed048 | 2016-03-17 17:01:14 +0100 | [diff] [blame] | 27 | struct lyd_node *aux; |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 28 | char *xml; |
| 29 | }; |
| 30 | static int |
| 31 | setup_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 Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 44 | st->ctx = ly_ctx_new(NULL, 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 45 | 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 | |
| 66 | error: |
Radek Krejci | d186567 | 2016-09-09 13:11:19 +0200 | [diff] [blame] | 67 | lyd_free_withsiblings(st->dt); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 68 | ly_ctx_destroy(st->ctx, NULL); |
| 69 | free(st); |
| 70 | (*state) = NULL; |
| 71 | |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | static int |
| 76 | teardown_f(void **state) |
| 77 | { |
| 78 | struct state *st = (*state); |
| 79 | |
Radek Krejci | d186567 | 2016-09-09 13:11:19 +0200 | [diff] [blame] | 80 | lyd_free_withsiblings(st->dt); |
| 81 | lyd_free_withsiblings(st->aux); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 82 | ly_ctx_destroy(st->ctx, NULL); |
| 83 | free(st->xml); |
| 84 | free(st); |
| 85 | (*state) = NULL; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | test_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 Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 99 | assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 100 | |
| 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 Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 106 | assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 107 | |
| 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 | |
| 113 | static void |
| 114 | test_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 Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 123 | assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 124 | |
| 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 Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 130 | assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 131 | |
| 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 | |
| 137 | static void |
| 138 | test_insert_fail(void **state) |
| 139 | { |
| 140 | struct state *st = (*state); |
| 141 | struct lyd_node *node; |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 142 | |
| 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 Krejci | 73ed048 | 2016-03-17 17:01:14 +0100 | [diff] [blame] | 147 | 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 Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 150 | assert_ptr_not_equal(node, NULL); |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 151 | assert_int_equal(lyd_validate(&(st->aux), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 152 | |
| 153 | assert_int_not_equal(lyd_insert_after(st->dt->child, node), 0); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 154 | assert_string_equal(ly_errmsg(st->ctx), "Insert request refers node (/autodel:c/a) that is going to be auto-deleted."); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void |
| 158 | test_insert_correct(void **state) |
| 159 | { |
| 160 | struct state *st = (*state); |
| 161 | struct lyd_node *node, *node2; |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 162 | |
| 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 Krejci | 73ed048 | 2016-03-17 17:01:14 +0100 | [diff] [blame] | 169 | 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 Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 172 | assert_ptr_not_equal(node, NULL); |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 173 | assert_int_equal(lyd_validate(&(st->aux), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 174 | |
| 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 Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 178 | assert_int_equal(lyd_validate(&(st->dt), LYD_OPT_CONFIG, NULL), 0); |
Radek Krejci | 6f60e46 | 2016-03-15 14:05:23 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | int 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 | } |