Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_new.c |
| 3 | * @author: Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief unit tests for functions for creating data |
| 5 | * |
| 6 | * Copyright (c) 2020 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 15 | #include "utests.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 16 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 17 | #include "libyang.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 18 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 19 | /* common module for the tests */ |
| 20 | const char *schema_a = "module a {\n" |
| 21 | " namespace urn:tests:a;\n" |
| 22 | " prefix a;yang-version 1.1;\n" |
| 23 | " list l1 {\n" |
| 24 | " key \"a b\";\n" |
| 25 | " leaf a {\n" |
| 26 | " type string;\n" |
| 27 | " }\n" |
| 28 | " leaf b {\n" |
| 29 | " type string;\n" |
| 30 | " }\n" |
| 31 | " leaf c {\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 32 | " type string;\n" |
| 33 | " }\n" |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 34 | " }\n" |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 35 | " list l11 {\n" |
| 36 | " key \"a\";\n" |
| 37 | " leaf a {\n" |
| 38 | " type uint32;\n" |
| 39 | " }\n" |
| 40 | " leaf b {\n" |
| 41 | " type uint32;\n" |
| 42 | " }\n" |
| 43 | " }\n" |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 44 | " leaf foo {\n" |
| 45 | " type uint16;\n" |
| 46 | " }\n" |
| 47 | " leaf-list ll {\n" |
| 48 | " type string;\n" |
| 49 | " }\n" |
| 50 | " container c {\n" |
| 51 | " leaf-list x {\n" |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 52 | " type string;\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 53 | " }\n" |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 54 | " }\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 55 | " anydata any {\n" |
| 56 | " config false;\n" |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 57 | " }\n" |
Michal Vasko | 54bf9e2 | 2023-04-27 08:20:30 +0200 | [diff] [blame] | 58 | " anyxml anyx;\n" |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 59 | " leaf-list ll2 {\n" |
| 60 | " config false;\n" |
| 61 | " type string;\n" |
| 62 | " }\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 63 | " list l2 {\n" |
| 64 | " config false;\n" |
| 65 | " container c {\n" |
| 66 | " leaf x {\n" |
| 67 | " type string;\n" |
| 68 | " }\n" |
| 69 | " }\n" |
| 70 | " }\n" |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 71 | " container c2 {\n" |
| 72 | " config false;\n" |
| 73 | " list l3 {\n" |
| 74 | " leaf x {\n" |
| 75 | " type string;\n" |
| 76 | " }\n" |
| 77 | " leaf y {\n" |
| 78 | " type string;\n" |
| 79 | " }\n" |
| 80 | " }\n" |
| 81 | " }\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 82 | " rpc oper {\n" |
| 83 | " input {\n" |
| 84 | " leaf param {\n" |
| 85 | " type string;\n" |
| 86 | " }\n" |
| 87 | " }\n" |
| 88 | " output {\n" |
| 89 | " leaf param {\n" |
| 90 | " type int8;\n" |
| 91 | " }\n" |
| 92 | " }\n" |
| 93 | " }\n" |
| 94 | "}\n"; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 95 | |
| 96 | static void |
| 97 | test_top_level(void **state) |
| 98 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 99 | struct lys_module *mod; |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 100 | struct lyd_node *node, *rpc; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 101 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 102 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 103 | |
| 104 | /* list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 105 | assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 106 | lyd_free_tree(node); |
| 107 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 108 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 109 | CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "/a:l1", 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 110 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 111 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 112 | CHECK_LOG_CTX("Not found node \"key1\" in path.", "/a:l1", 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 113 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 114 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 115 | CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "/a:l1", 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 116 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 117 | assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 118 | CHECK_LOG_CTX("List node \"c\" not found.", NULL, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 119 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 120 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 121 | lyd_free_tree(node); |
| 122 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 123 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 124 | lyd_free_tree(node); |
| 125 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 126 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a:a='a'][a:b='b']", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 127 | lyd_free_tree(node); |
| 128 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 129 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a= 'a']\n[b =\t'b']", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 130 | lyd_free_tree(node); |
| 131 | |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame] | 132 | const char *key_vals[] = {"a", "b"}; |
Michal Vasko | f793156 | 2023-10-17 14:18:03 +0200 | [diff] [blame] | 133 | |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame] | 134 | assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, NULL, 0, &node), LY_SUCCESS); |
| 135 | lyd_free_tree(node); |
| 136 | |
| 137 | uint32_t val_lens[] = {1, 1}; |
Michal Vasko | f793156 | 2023-10-17 14:18:03 +0200 | [diff] [blame] | 138 | |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame^] | 139 | assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_BIN_VALUE, &node), LY_EINVAL); |
| 140 | CHECK_LOG_CTX("Invalid argument !(options & 0x04) (lyd_new_list3()).", NULL, 0); |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame] | 141 | assert_int_equal(lyd_new_list3_bin(NULL, mod, "l1", (const void **)key_vals, val_lens, 0, &node), LY_SUCCESS); |
| 142 | lyd_free_tree(node); |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame^] | 143 | assert_int_equal(lyd_new_list3_bin(NULL, mod, "l1", (const void **)key_vals, val_lens, LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL); |
| 144 | CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_list3()).", NULL, 0); |
| 145 | |
| 146 | assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_CANON_VALUE, &node), LY_SUCCESS); |
| 147 | lyd_free_tree(node); |
| 148 | assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_CANON_VALUE | LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL); |
| 149 | CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_list3()).", NULL, 0); |
| 150 | |
| 151 | assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_BIN_VALUE, &node, "val_a", 5, "val_b", 5), LY_SUCCESS); |
| 152 | lyd_free_tree(node); |
| 153 | assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_BIN_VALUE | LYD_NEW_VAL_STORE_ONLY, &node, "val_a", 5, "val_b", 5), LY_EINVAL); |
| 154 | CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list()).", NULL, 0); |
| 155 | |
| 156 | assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_CANON_VALUE, &node, "val_a", "val_b"), LY_SUCCESS); |
| 157 | lyd_free_tree(node); |
| 158 | assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_CANON_VALUE | LYD_NEW_VAL_STORE_ONLY, &node, "val_a", "val_b"), LY_EINVAL); |
| 159 | CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list()).", NULL, 0); |
Michal Vasko | 2c1e327 | 2023-10-17 14:08:35 +0200 | [diff] [blame] | 160 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 161 | /* leaf */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 162 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 163 | CHECK_LOG_CTX("Invalid type uint16 value \"[a='a'][b='b'][c='c']\".", "/a:foo", 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 164 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 165 | assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 166 | CHECK_LOG_CTX("Term node \"c\" not found.", NULL, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 167 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 168 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 169 | lyd_free_tree(node); |
| 170 | |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame^] | 171 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_BIN_VALUE, &node), LY_EINVAL); |
| 172 | CHECK_LOG_CTX("Invalid argument !(options & 0x04) (lyd_new_term()).", NULL, 0); |
| 173 | assert_int_equal(lyd_new_term_bin(NULL, mod, "foo", "25", 2, LYD_NEW_VAL_BIN_VALUE, &node), LY_SUCCESS); |
| 174 | lyd_free_tree(node); |
| 175 | assert_int_equal(lyd_new_term_bin(NULL, mod, "foo", "25", 2, LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL); |
| 176 | CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_term()).", NULL, 0); |
| 177 | |
| 178 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_CANON_VALUE, &node), LY_SUCCESS); |
| 179 | lyd_free_tree(node); |
| 180 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_CANON_VALUE | LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL); |
| 181 | CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_term()).", NULL, 0); |
| 182 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 183 | /* leaf-list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 184 | assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 185 | lyd_free_tree(node); |
| 186 | |
| 187 | /* container */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 188 | assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 189 | lyd_free_tree(node); |
| 190 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 191 | assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 192 | CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l1\" not found.", NULL, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 193 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 194 | assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 195 | CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l2\" not found.", NULL, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 196 | |
| 197 | /* anydata */ |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame^] | 198 | assert_int_equal(lyd_new_any(NULL, mod, "any", "{\"node\":\"val\"}", 0, LYD_ANYDATA_STRING, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 199 | lyd_free_tree(node); |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame^] | 200 | assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", 0, LYD_ANYDATA_STRING, &node), LY_SUCCESS); |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 201 | lyd_free_tree(node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 202 | |
| 203 | /* key-less list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 204 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 205 | CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "/a:l2", 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 206 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 207 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 208 | lyd_free_tree(node); |
| 209 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 210 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 211 | lyd_free_tree(node); |
| 212 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 213 | assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 214 | lyd_free_tree(node); |
| 215 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 216 | /* RPC */ |
| 217 | assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS); |
| 218 | assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS); |
| 219 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype); |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame^] | 220 | assert_int_equal(lyd_new_term(rpc, mod, "param", "22", LYD_NEW_VAL_OUTPUT, &node), LY_SUCCESS); |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 221 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype); |
| 222 | lyd_free_tree(rpc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 225 | static void |
| 226 | test_opaq(void **state) |
| 227 | { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 228 | struct lyd_node *root, *node; |
| 229 | struct lyd_node_opaq *opq; |
| 230 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 231 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL); |
| 232 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 233 | assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 234 | assert_null(root->schema); |
| 235 | opq = (struct lyd_node_opaq *)root; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 236 | assert_string_equal(opq->name.name, "node1"); |
| 237 | assert_string_equal(opq->name.module_name, "my-module"); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 238 | assert_string_equal(opq->value, ""); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 239 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 240 | assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 241 | assert_null(node->schema); |
| 242 | opq = (struct lyd_node_opaq *)node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 243 | assert_string_equal(opq->name.name, "node2"); |
| 244 | assert_string_equal(opq->name.module_name, "my-module2"); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 245 | assert_string_equal(opq->value, "value"); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 246 | assert_ptr_equal(opq->parent, root); |
| 247 | |
| 248 | lyd_free_tree(root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void |
| 252 | test_path(void **state) |
| 253 | { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 254 | LY_ERR ret; |
| 255 | struct lyd_node *root, *node, *parent; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 256 | struct lys_module *mod; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 257 | char *str; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 258 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 259 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 260 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 261 | /* create 2 nodes */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 262 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, 0, &root, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 263 | assert_int_equal(ret, LY_SUCCESS); |
| 264 | assert_non_null(root); |
| 265 | assert_string_equal(root->schema->name, "c"); |
| 266 | assert_non_null(node); |
| 267 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 268 | assert_string_equal("val", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 269 | |
| 270 | /* append another */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 271 | ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, 0, &parent, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 272 | assert_int_equal(ret, LY_SUCCESS); |
| 273 | assert_ptr_equal(parent, node); |
| 274 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 275 | assert_string_equal("val2", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 276 | |
| 277 | /* and a last one */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 278 | ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, 0, &parent, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 279 | assert_int_equal(ret, LY_SUCCESS); |
| 280 | assert_ptr_equal(parent, node); |
| 281 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 282 | assert_string_equal("val3", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 283 | |
| 284 | lyd_free_tree(root); |
| 285 | |
| 286 | /* try LYD_NEWOPT_OPAQ */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 287 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, 0, NULL, NULL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 288 | assert_int_equal(ret, LY_EINVAL); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 289 | CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "/a:l1", 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 290 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 291 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, LYD_NEW_PATH_OPAQ, NULL, &root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 292 | assert_int_equal(ret, LY_SUCCESS); |
| 293 | assert_non_null(root); |
| 294 | assert_null(root->schema); |
| 295 | |
| 296 | lyd_free_tree(root); |
| 297 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 298 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, 0, NULL, NULL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 299 | assert_int_equal(ret, LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 300 | CHECK_LOG_CTX("Invalid type uint16 empty value.", "/a:foo", 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 301 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 302 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, LYD_NEW_PATH_OPAQ, NULL, &root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 303 | assert_int_equal(ret, LY_SUCCESS); |
| 304 | assert_non_null(root); |
| 305 | assert_null(root->schema); |
| 306 | |
| 307 | lyd_free_tree(root); |
| 308 | |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 309 | ret = lyd_new_path(NULL, UTEST_LYCTX, "/a:l11", NULL, LYD_NEW_PATH_OPAQ, &root); |
| 310 | assert_int_equal(ret, LY_SUCCESS); |
| 311 | assert_non_null(root); |
| 312 | assert_null(root->schema); |
| 313 | |
| 314 | ret = lyd_new_path(root, NULL, "a", NULL, LYD_NEW_PATH_OPAQ, NULL); |
| 315 | assert_int_equal(ret, LY_SUCCESS); |
| 316 | assert_non_null(lyd_child(root)); |
| 317 | assert_null(lyd_child(root)->schema); |
| 318 | |
| 319 | ret = lyd_new_path(root, NULL, "b", NULL, LYD_NEW_PATH_OPAQ, NULL); |
| 320 | assert_int_equal(ret, LY_SUCCESS); |
| 321 | assert_non_null(lyd_child(root)->next); |
| 322 | assert_null(lyd_child(root)->next->schema); |
| 323 | |
| 324 | lyd_free_tree(root); |
| 325 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 326 | /* key-less list */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 327 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c2/l3/x", "val1", 0, 0, 0, &root, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 328 | assert_int_equal(ret, LY_SUCCESS); |
| 329 | assert_non_null(root); |
| 330 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 331 | assert_string_equal("val1", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 332 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 333 | ret = lyd_new_path2(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 334 | assert_int_equal(ret, LY_EEXIST); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 335 | CHECK_LOG_CTX("Path \"/a:c2/l3[1]\" already exists.", "/a:c2/l3[1]", 0); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 336 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 337 | ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 338 | assert_int_equal(ret, LY_SUCCESS); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 339 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 340 | ret = lyd_new_path2(root, NULL, "/a:c2/l3/x", "val3", 0, 0, 0, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 341 | assert_int_equal(ret, LY_SUCCESS); |
| 342 | assert_non_null(node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 343 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 344 | ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "empty", 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 345 | assert_int_equal(ret, LY_SUCCESS); |
| 346 | assert_non_null(node); |
| 347 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 348 | ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "val4", 0, 0, LYD_NEW_PATH_UPDATE, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 349 | assert_int_equal(ret, LY_SUCCESS); |
| 350 | assert_non_null(node); |
| 351 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 352 | ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 353 | assert_int_equal(ret, LY_SUCCESS); |
| 354 | assert_non_null(node); |
| 355 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 356 | ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 357 | assert_int_equal(ret, LY_SUCCESS); |
| 358 | assert_non_null(node); |
| 359 | |
| 360 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 361 | assert_string_equal(str, |
| 362 | "<c2 xmlns=\"urn:tests:a\">\n" |
| 363 | " <l3>\n" |
| 364 | " <x>val1</x>\n" |
| 365 | " </l3>\n" |
| 366 | " <l3>\n" |
| 367 | " <x>val2</x>\n" |
| 368 | " </l3>\n" |
| 369 | " <l3>\n" |
| 370 | " <x>val3</x>\n" |
| 371 | " </l3>\n" |
| 372 | " <l3>\n" |
| 373 | " <x>val4</x>\n" |
| 374 | " </l3>\n" |
| 375 | " <l3>\n" |
| 376 | " <x>val5</x>\n" |
| 377 | " </l3>\n" |
| 378 | " <l3>\n" |
| 379 | " <x>val6</x>\n" |
| 380 | " </l3>\n" |
| 381 | "</c2>\n"); |
| 382 | free(str); |
| 383 | lyd_free_siblings(root); |
| 384 | |
| 385 | /* state leaf-list */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 386 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 387 | assert_int_equal(ret, LY_SUCCESS); |
| 388 | assert_non_null(root); |
| 389 | assert_string_equal(node->schema->name, "ll2"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 390 | assert_string_equal("val_first", lyd_get_value(node)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 391 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 392 | ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 393 | assert_int_equal(ret, LY_EEXIST); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 394 | CHECK_LOG_CTX("Path \"/a:ll2[1]\" already exists.", "/a:ll2[1]", 0); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 395 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 396 | ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 397 | assert_int_equal(ret, LY_SUCCESS); |
| 398 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 399 | ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "val", 0, 0, LYD_NEW_PATH_UPDATE, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 400 | assert_int_equal(ret, LY_SUCCESS); |
| 401 | assert_non_null(node); |
| 402 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 403 | ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 404 | assert_int_equal(ret, LY_SUCCESS); |
| 405 | assert_non_null(node); |
| 406 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 407 | ret = lyd_new_path2(root, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 408 | assert_int_equal(ret, LY_EVALID); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 409 | CHECK_LOG_CTX("Unparsed characters \"[.='val3']\" left at the end of path.", NULL, 0); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 410 | |
| 411 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 412 | assert_string_equal(str, |
| 413 | "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n" |
| 414 | "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n" |
| 415 | "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n"); |
| 416 | free(str); |
| 417 | lyd_free_siblings(root); |
Michal Vasko | ea0f96c | 2022-12-20 08:34:39 +0100 | [diff] [blame] | 418 | |
| 419 | /* anydata */ |
| 420 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", "<elem>val</elem>", 0, LYD_ANYDATA_XML, 0, &root, NULL); |
| 421 | assert_int_equal(ret, LY_SUCCESS); |
| 422 | assert_non_null(root); |
| 423 | |
| 424 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 425 | assert_string_equal(str, |
| 426 | "<any xmlns=\"urn:tests:a\">\n" |
| 427 | " <elem>val</elem>\n" |
| 428 | "</any>\n"); |
| 429 | free(str); |
| 430 | lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 431 | assert_string_equal(str, |
| 432 | "{\n" |
| 433 | " \"a:any\": {\n" |
| 434 | " \"elem\": \"val\"\n" |
| 435 | " }\n" |
| 436 | "}\n"); |
| 437 | free(str); |
| 438 | lyd_free_siblings(root); |
Michal Vasko | 54bf9e2 | 2023-04-27 08:20:30 +0200 | [diff] [blame] | 439 | |
| 440 | /* anyxml */ |
| 441 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", "<a/><b/><c/>", 0, LYD_ANYDATA_XML, 0, &root, NULL); |
| 442 | assert_int_equal(ret, LY_SUCCESS); |
| 443 | assert_non_null(root); |
| 444 | |
| 445 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 446 | assert_string_equal(str, |
| 447 | "<anyx xmlns=\"urn:tests:a\">\n" |
| 448 | " <a/>\n" |
| 449 | " <b/>\n" |
| 450 | " <c/>\n" |
| 451 | "</anyx>\n"); |
| 452 | free(str); |
| 453 | lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 454 | assert_string_equal(str, |
| 455 | "{\n" |
| 456 | " \"a:anyx\": {\n" |
| 457 | " \"a\": [null],\n" |
| 458 | " \"b\": [null],\n" |
| 459 | " \"c\": [null]\n" |
| 460 | " }\n" |
| 461 | "}\n"); |
| 462 | free(str); |
| 463 | lyd_free_siblings(root); |
| 464 | |
| 465 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", "{\"a\":[null],\"b\":[null],\"c\":[null]}", 0, LYD_ANYDATA_JSON, 0, &root, NULL); |
| 466 | assert_int_equal(ret, LY_SUCCESS); |
| 467 | assert_non_null(root); |
| 468 | |
| 469 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 470 | assert_string_equal(str, |
| 471 | "<anyx xmlns=\"urn:tests:a\">\n" |
| 472 | " <a/>\n" |
| 473 | " <b/>\n" |
| 474 | " <c/>\n" |
| 475 | "</anyx>\n"); |
| 476 | free(str); |
| 477 | lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 478 | assert_string_equal(str, |
| 479 | "{\n" |
| 480 | " \"a:anyx\": {\n" |
| 481 | " \"a\": [null],\n" |
| 482 | " \"b\": [null],\n" |
| 483 | " \"c\": [null]\n" |
| 484 | " }\n" |
| 485 | "}\n"); |
| 486 | free(str); |
| 487 | lyd_free_siblings(root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 490 | static void |
| 491 | test_path_ext(void **state) |
| 492 | { |
| 493 | LY_ERR ret; |
| 494 | struct lyd_node *root, *node; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 495 | struct lys_module *mod; |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 496 | const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;" |
| 497 | "import ietf-restconf {revision-date 2017-01-26; prefix rc;}" |
| 498 | "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}"; |
| 499 | |
| 500 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
| 501 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL)); |
| 502 | |
| 503 | UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod); |
| 504 | |
| 505 | /* create x */ |
| 506 | ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root); |
| 507 | assert_int_equal(ret, LY_SUCCESS); |
| 508 | assert_non_null(root); |
| 509 | assert_string_equal(root->schema->name, "c"); |
| 510 | assert_non_null(node = lyd_child(root)); |
| 511 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 512 | assert_string_equal("xxx", lyd_get_value(node)); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 513 | |
| 514 | /* append y */ |
| 515 | ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node); |
| 516 | assert_int_equal(ret, LY_SUCCESS); |
| 517 | assert_string_equal(node->schema->name, "y"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 518 | assert_string_equal("yyy", lyd_get_value(node)); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 519 | |
| 520 | /* append z */ |
| 521 | ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node); |
| 522 | assert_int_equal(ret, LY_SUCCESS); |
| 523 | assert_string_equal(node->schema->name, "z"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 524 | assert_string_equal("zzz", lyd_get_value(node)); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 525 | |
| 526 | lyd_free_tree(root); |
| 527 | } |
| 528 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 529 | int |
| 530 | main(void) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 531 | { |
| 532 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 533 | UTEST(test_top_level), |
| 534 | UTEST(test_opaq), |
| 535 | UTEST(test_path), |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 536 | UTEST(test_path_ext), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 540 | } |