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 | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 58 | " leaf-list ll2 {\n" |
| 59 | " config false;\n" |
| 60 | " type string;\n" |
| 61 | " }\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 62 | " list l2 {\n" |
| 63 | " config false;\n" |
| 64 | " container c {\n" |
| 65 | " leaf x {\n" |
| 66 | " type string;\n" |
| 67 | " }\n" |
| 68 | " }\n" |
| 69 | " }\n" |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 70 | " container c2 {\n" |
| 71 | " config false;\n" |
| 72 | " list l3 {\n" |
| 73 | " leaf x {\n" |
| 74 | " type string;\n" |
| 75 | " }\n" |
| 76 | " leaf y {\n" |
| 77 | " type string;\n" |
| 78 | " }\n" |
| 79 | " }\n" |
| 80 | " }\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 81 | " rpc oper {\n" |
| 82 | " input {\n" |
| 83 | " leaf param {\n" |
| 84 | " type string;\n" |
| 85 | " }\n" |
| 86 | " }\n" |
| 87 | " output {\n" |
| 88 | " leaf param {\n" |
| 89 | " type int8;\n" |
| 90 | " }\n" |
| 91 | " }\n" |
| 92 | " }\n" |
| 93 | "}\n"; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 94 | |
| 95 | static void |
| 96 | test_top_level(void **state) |
| 97 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 98 | struct lys_module *mod; |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 99 | struct lyd_node *node, *rpc; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 100 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 101 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 102 | |
| 103 | /* list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 104 | 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] | 105 | lyd_free_tree(node); |
| 106 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 107 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 108 | CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location \"/a:l1\"."); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 109 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 110 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 111 | CHECK_LOG_CTX("Not found node \"key1\" in path.", "Schema location \"/a:l1\"."); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 112 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 113 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 114 | CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "Schema location \"/a:l1\"."); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 115 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 116 | assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", 0, &node), LY_ENOTFOUND); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 117 | CHECK_LOG_CTX("List node \"c\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 118 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 119 | 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] | 120 | lyd_free_tree(node); |
| 121 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 122 | 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] | 123 | lyd_free_tree(node); |
| 124 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 125 | 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] | 126 | lyd_free_tree(node); |
| 127 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 128 | 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] | 129 | lyd_free_tree(node); |
| 130 | |
| 131 | /* leaf */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 132 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 133 | CHECK_LOG_CTX("Invalid type uint16 value \"[a='a'][b='b'][c='c']\".", "Schema location \"/a:foo\"."); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 134 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 135 | assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 136 | CHECK_LOG_CTX("Term node \"c\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 137 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 138 | 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] | 139 | lyd_free_tree(node); |
| 140 | |
| 141 | /* leaf-list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 142 | 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] | 143 | lyd_free_tree(node); |
| 144 | |
| 145 | /* container */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 146 | 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] | 147 | lyd_free_tree(node); |
| 148 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 149 | assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 150 | CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l1\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 151 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 152 | assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 153 | CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l2\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 154 | |
| 155 | /* anydata */ |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 156 | assert_int_equal(lyd_new_any(NULL, mod, "any", "{\"node\":\"val\"}", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 157 | lyd_free_tree(node); |
Michal Vasko | ce2e07c | 2022-12-01 11:08:52 +0100 | [diff] [blame] | 158 | assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS); |
| 159 | lyd_free_tree(node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 160 | |
| 161 | /* key-less list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 162 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 163 | CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location \"/a:l2\"."); |
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_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 166 | lyd_free_tree(node); |
| 167 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 168 | 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] | 169 | lyd_free_tree(node); |
| 170 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 171 | 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] | 172 | lyd_free_tree(node); |
| 173 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 174 | /* RPC */ |
| 175 | assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS); |
| 176 | assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS); |
| 177 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype); |
| 178 | assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS); |
| 179 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype); |
| 180 | lyd_free_tree(rpc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 183 | static void |
| 184 | test_opaq(void **state) |
| 185 | { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 186 | struct lyd_node *root, *node; |
| 187 | struct lyd_node_opaq *opq; |
| 188 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 189 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL); |
| 190 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 191 | 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] | 192 | assert_null(root->schema); |
| 193 | opq = (struct lyd_node_opaq *)root; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 194 | assert_string_equal(opq->name.name, "node1"); |
| 195 | assert_string_equal(opq->name.module_name, "my-module"); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 196 | assert_string_equal(opq->value, ""); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 197 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 198 | 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] | 199 | assert_null(node->schema); |
| 200 | opq = (struct lyd_node_opaq *)node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 201 | assert_string_equal(opq->name.name, "node2"); |
| 202 | assert_string_equal(opq->name.module_name, "my-module2"); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 203 | assert_string_equal(opq->value, "value"); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 204 | assert_ptr_equal(opq->parent, root); |
| 205 | |
| 206 | lyd_free_tree(root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void |
| 210 | test_path(void **state) |
| 211 | { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 212 | LY_ERR ret; |
| 213 | struct lyd_node *root, *node, *parent; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 214 | struct lys_module *mod; |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 215 | char *str; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 216 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 217 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 218 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 219 | /* create 2 nodes */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 220 | 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] | 221 | assert_int_equal(ret, LY_SUCCESS); |
| 222 | assert_non_null(root); |
| 223 | assert_string_equal(root->schema->name, "c"); |
| 224 | assert_non_null(node); |
| 225 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 226 | assert_string_equal("val", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 227 | |
| 228 | /* append another */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 229 | 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] | 230 | assert_int_equal(ret, LY_SUCCESS); |
| 231 | assert_ptr_equal(parent, node); |
| 232 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 233 | assert_string_equal("val2", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 234 | |
| 235 | /* and a last one */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 236 | 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] | 237 | assert_int_equal(ret, LY_SUCCESS); |
| 238 | assert_ptr_equal(parent, node); |
| 239 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 240 | assert_string_equal("val3", lyd_get_value(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 241 | |
| 242 | lyd_free_tree(root); |
| 243 | |
| 244 | /* try LYD_NEWOPT_OPAQ */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 245 | 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] | 246 | assert_int_equal(ret, LY_EINVAL); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 247 | CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location \"/a:l1\"."); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 248 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 249 | 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] | 250 | assert_int_equal(ret, LY_SUCCESS); |
| 251 | assert_non_null(root); |
| 252 | assert_null(root->schema); |
| 253 | |
| 254 | lyd_free_tree(root); |
| 255 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 256 | 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] | 257 | assert_int_equal(ret, LY_EVALID); |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 258 | CHECK_LOG_CTX("Invalid type uint16 empty value.", "Schema location \"/a:foo\"."); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 259 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 260 | 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] | 261 | assert_int_equal(ret, LY_SUCCESS); |
| 262 | assert_non_null(root); |
| 263 | assert_null(root->schema); |
| 264 | |
| 265 | lyd_free_tree(root); |
| 266 | |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 267 | ret = lyd_new_path(NULL, UTEST_LYCTX, "/a:l11", NULL, LYD_NEW_PATH_OPAQ, &root); |
| 268 | assert_int_equal(ret, LY_SUCCESS); |
| 269 | assert_non_null(root); |
| 270 | assert_null(root->schema); |
| 271 | |
| 272 | ret = lyd_new_path(root, NULL, "a", NULL, LYD_NEW_PATH_OPAQ, NULL); |
| 273 | assert_int_equal(ret, LY_SUCCESS); |
| 274 | assert_non_null(lyd_child(root)); |
| 275 | assert_null(lyd_child(root)->schema); |
| 276 | |
| 277 | ret = lyd_new_path(root, NULL, "b", NULL, LYD_NEW_PATH_OPAQ, NULL); |
| 278 | assert_int_equal(ret, LY_SUCCESS); |
| 279 | assert_non_null(lyd_child(root)->next); |
| 280 | assert_null(lyd_child(root)->next->schema); |
| 281 | |
| 282 | lyd_free_tree(root); |
| 283 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 284 | /* key-less list */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 285 | 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] | 286 | assert_int_equal(ret, LY_SUCCESS); |
| 287 | assert_non_null(root); |
| 288 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 289 | assert_string_equal("val1", lyd_get_value(node)); |
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(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 292 | assert_int_equal(ret, LY_EEXIST); |
| 293 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 294 | 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] | 295 | assert_int_equal(ret, LY_SUCCESS); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 296 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 297 | 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] | 298 | assert_int_equal(ret, LY_SUCCESS); |
| 299 | assert_non_null(node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 300 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 301 | 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] | 302 | assert_int_equal(ret, LY_SUCCESS); |
| 303 | assert_non_null(node); |
| 304 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 305 | 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] | 306 | assert_int_equal(ret, LY_SUCCESS); |
| 307 | assert_non_null(node); |
| 308 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 309 | 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] | 310 | assert_int_equal(ret, LY_SUCCESS); |
| 311 | assert_non_null(node); |
| 312 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 313 | 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] | 314 | assert_int_equal(ret, LY_SUCCESS); |
| 315 | assert_non_null(node); |
| 316 | |
| 317 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 318 | assert_string_equal(str, |
| 319 | "<c2 xmlns=\"urn:tests:a\">\n" |
| 320 | " <l3>\n" |
| 321 | " <x>val1</x>\n" |
| 322 | " </l3>\n" |
| 323 | " <l3>\n" |
| 324 | " <x>val2</x>\n" |
| 325 | " </l3>\n" |
| 326 | " <l3>\n" |
| 327 | " <x>val3</x>\n" |
| 328 | " </l3>\n" |
| 329 | " <l3>\n" |
| 330 | " <x>val4</x>\n" |
| 331 | " </l3>\n" |
| 332 | " <l3>\n" |
| 333 | " <x>val5</x>\n" |
| 334 | " </l3>\n" |
| 335 | " <l3>\n" |
| 336 | " <x>val6</x>\n" |
| 337 | " </l3>\n" |
| 338 | "</c2>\n"); |
| 339 | free(str); |
| 340 | lyd_free_siblings(root); |
| 341 | |
| 342 | /* state leaf-list */ |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 343 | 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] | 344 | assert_int_equal(ret, LY_SUCCESS); |
| 345 | assert_non_null(root); |
| 346 | assert_string_equal(node->schema->name, "ll2"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 347 | assert_string_equal("val_first", lyd_get_value(node)); |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 348 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 349 | 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] | 350 | assert_int_equal(ret, LY_EEXIST); |
| 351 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 352 | 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] | 353 | assert_int_equal(ret, LY_SUCCESS); |
| 354 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 355 | 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] | 356 | assert_int_equal(ret, LY_SUCCESS); |
| 357 | assert_non_null(node); |
| 358 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 359 | 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] | 360 | assert_int_equal(ret, LY_SUCCESS); |
| 361 | assert_non_null(node); |
| 362 | |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 363 | 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] | 364 | assert_int_equal(ret, LY_EVALID); |
| 365 | |
| 366 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 367 | assert_string_equal(str, |
| 368 | "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n" |
| 369 | "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n" |
| 370 | "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n"); |
| 371 | free(str); |
| 372 | lyd_free_siblings(root); |
Michal Vasko | ea0f96c | 2022-12-20 08:34:39 +0100 | [diff] [blame^] | 373 | |
| 374 | /* anydata */ |
| 375 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", "<elem>val</elem>", 0, LYD_ANYDATA_XML, 0, &root, NULL); |
| 376 | assert_int_equal(ret, LY_SUCCESS); |
| 377 | assert_non_null(root); |
| 378 | |
| 379 | lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 380 | assert_string_equal(str, |
| 381 | "<any xmlns=\"urn:tests:a\">\n" |
| 382 | " <elem>val</elem>\n" |
| 383 | "</any>\n"); |
| 384 | free(str); |
| 385 | lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 386 | assert_string_equal(str, |
| 387 | "{\n" |
| 388 | " \"a:any\": {\n" |
| 389 | " \"elem\": \"val\"\n" |
| 390 | " }\n" |
| 391 | "}\n"); |
| 392 | free(str); |
| 393 | lyd_free_siblings(root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 396 | static void |
| 397 | test_path_ext(void **state) |
| 398 | { |
| 399 | LY_ERR ret; |
| 400 | struct lyd_node *root, *node; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 401 | struct lys_module *mod; |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 402 | const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;" |
| 403 | "import ietf-restconf {revision-date 2017-01-26; prefix rc;}" |
| 404 | "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}"; |
| 405 | |
| 406 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
| 407 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL)); |
| 408 | |
| 409 | UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod); |
| 410 | |
| 411 | /* create x */ |
| 412 | ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root); |
| 413 | assert_int_equal(ret, LY_SUCCESS); |
| 414 | assert_non_null(root); |
| 415 | assert_string_equal(root->schema->name, "c"); |
| 416 | assert_non_null(node = lyd_child(root)); |
| 417 | assert_string_equal(node->schema->name, "x"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 418 | assert_string_equal("xxx", lyd_get_value(node)); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 419 | |
| 420 | /* append y */ |
| 421 | ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node); |
| 422 | assert_int_equal(ret, LY_SUCCESS); |
| 423 | assert_string_equal(node->schema->name, "y"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 424 | assert_string_equal("yyy", lyd_get_value(node)); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 425 | |
| 426 | /* append z */ |
| 427 | ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node); |
| 428 | assert_int_equal(ret, LY_SUCCESS); |
| 429 | assert_string_equal(node->schema->name, "z"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 430 | assert_string_equal("zzz", lyd_get_value(node)); |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 431 | |
| 432 | lyd_free_tree(root); |
| 433 | } |
| 434 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 435 | int |
| 436 | main(void) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 437 | { |
| 438 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 439 | UTEST(test_top_level), |
| 440 | UTEST(test_opaq), |
| 441 | UTEST(test_path), |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 442 | UTEST(test_path_ext), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 446 | } |