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