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" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 43 | " type string;\n" |
| 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" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 49 | " list l2 {\n" |
| 50 | " config false;\n" |
| 51 | " container c {\n" |
| 52 | " leaf x {\n" |
| 53 | " type string;\n" |
| 54 | " }\n" |
| 55 | " }\n" |
| 56 | " }\n" |
| 57 | " rpc oper {\n" |
| 58 | " input {\n" |
| 59 | " leaf param {\n" |
| 60 | " type string;\n" |
| 61 | " }\n" |
| 62 | " }\n" |
| 63 | " output {\n" |
| 64 | " leaf param {\n" |
| 65 | " type int8;\n" |
| 66 | " }\n" |
| 67 | " }\n" |
| 68 | " }\n" |
| 69 | "}\n"; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 70 | |
| 71 | static void |
| 72 | test_top_level(void **state) |
| 73 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 74 | const struct lys_module *mod; |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 75 | struct lyd_node *node, *rpc; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 76 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 77 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 78 | |
| 79 | /* list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 80 | 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] | 81 | lyd_free_tree(node); |
| 82 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 83 | 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] | 84 | CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location /a:l1."); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 85 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 86 | 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] | 87 | 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] | 88 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 89 | 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] | 90 | 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] | 91 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 92 | 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] | 93 | CHECK_LOG_CTX("List node \"c\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 94 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 95 | 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] | 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", "[a=''][b='']", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 99 | lyd_free_tree(node); |
| 100 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 101 | 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] | 102 | lyd_free_tree(node); |
| 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']\n[b =\t'b']", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 105 | lyd_free_tree(node); |
| 106 | |
| 107 | /* leaf */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 108 | 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] | 109 | 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] | 110 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 111 | 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] | 112 | CHECK_LOG_CTX("Term node \"c\" not found.", NULL); |
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_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 115 | lyd_free_tree(node); |
| 116 | |
| 117 | /* leaf-list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 118 | 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] | 119 | lyd_free_tree(node); |
| 120 | |
| 121 | /* container */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 122 | 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] | 123 | lyd_free_tree(node); |
| 124 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 125 | 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] | 126 | CHECK_LOG_CTX("Inner node (not a list) \"l1\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 127 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 128 | 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] | 129 | CHECK_LOG_CTX("Inner node (not a list) \"l2\" not found.", NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 130 | |
| 131 | /* anydata */ |
Michal Vasko | 2a4ab2b | 2021-03-04 15:52:40 +0100 | [diff] [blame] | 132 | 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] | 133 | lyd_free_tree(node); |
| 134 | |
| 135 | /* key-less list */ |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 136 | 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] | 137 | 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] | 138 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 139 | 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] | 140 | lyd_free_tree(node); |
| 141 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 142 | 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] | 143 | lyd_free_tree(node); |
| 144 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 145 | 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] | 146 | lyd_free_tree(node); |
| 147 | |
Radek Krejci | 41ac994 | 2020-11-02 14:47:56 +0100 | [diff] [blame] | 148 | /* RPC */ |
| 149 | assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS); |
| 150 | assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS); |
| 151 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype); |
| 152 | assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS); |
| 153 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype); |
| 154 | lyd_free_tree(rpc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 157 | static void |
| 158 | test_opaq(void **state) |
| 159 | { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 160 | struct lyd_node *root, *node; |
| 161 | struct lyd_node_opaq *opq; |
| 162 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 163 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL); |
| 164 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 165 | 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] | 166 | assert_null(root->schema); |
| 167 | opq = (struct lyd_node_opaq *)root; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 168 | assert_string_equal(opq->name.name, "node1"); |
| 169 | assert_string_equal(opq->name.module_name, "my-module"); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 170 | assert_string_equal(opq->value, ""); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 171 | |
Michal Vasko | 0ab974d | 2021-02-24 13:18:26 +0100 | [diff] [blame] | 172 | 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] | 173 | assert_null(node->schema); |
| 174 | opq = (struct lyd_node_opaq *)node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 175 | assert_string_equal(opq->name.name, "node2"); |
| 176 | assert_string_equal(opq->name.module_name, "my-module2"); |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 177 | assert_string_equal(opq->value, "value"); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 178 | assert_ptr_equal(opq->parent, root); |
| 179 | |
| 180 | lyd_free_tree(root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void |
| 184 | test_path(void **state) |
| 185 | { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 186 | LY_ERR ret; |
| 187 | struct lyd_node *root, *node, *parent; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 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 | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 191 | /* create 2 nodes */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 192 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, &root, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 193 | assert_int_equal(ret, LY_SUCCESS); |
| 194 | assert_non_null(root); |
| 195 | assert_string_equal(root->schema->name, "c"); |
| 196 | assert_non_null(node); |
| 197 | assert_string_equal(node->schema->name, "x"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 198 | assert_string_equal("val", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 199 | |
| 200 | /* append another */ |
| 201 | ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, &parent, &node); |
| 202 | assert_int_equal(ret, LY_SUCCESS); |
| 203 | assert_ptr_equal(parent, node); |
| 204 | assert_string_equal(node->schema->name, "x"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 205 | assert_string_equal("val2", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 206 | |
| 207 | /* and a last one */ |
| 208 | ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, &parent, &node); |
| 209 | assert_int_equal(ret, LY_SUCCESS); |
| 210 | assert_ptr_equal(parent, node); |
| 211 | assert_string_equal(node->schema->name, "x"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 212 | assert_string_equal("val3", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 213 | |
| 214 | lyd_free_tree(root); |
| 215 | |
| 216 | /* try LYD_NEWOPT_OPAQ */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 217 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, NULL, NULL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 218 | assert_int_equal(ret, LY_EINVAL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 219 | 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] | 220 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 221 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 222 | assert_int_equal(ret, LY_SUCCESS); |
| 223 | assert_non_null(root); |
| 224 | assert_null(root->schema); |
| 225 | |
| 226 | lyd_free_tree(root); |
| 227 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 228 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, NULL, NULL); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 229 | assert_int_equal(ret, LY_EVALID); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 230 | CHECK_LOG_CTX("Invalid empty uint16 value.", "Schema location /a:foo."); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 231 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 232 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 233 | assert_int_equal(ret, LY_SUCCESS); |
| 234 | assert_non_null(root); |
| 235 | assert_null(root->schema); |
| 236 | |
| 237 | lyd_free_tree(root); |
| 238 | |
| 239 | /* try LYD_NEWOPT_UPDATE */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 240 | ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l2[1]/c/x", "val", 0, 0, &root, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 241 | assert_int_equal(ret, LY_SUCCESS); |
| 242 | assert_non_null(root); |
| 243 | assert_string_equal(node->schema->name, "x"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 244 | assert_string_equal("val", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 245 | |
| 246 | ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, 0, NULL, &node); |
| 247 | assert_int_equal(ret, LY_EEXIST); |
| 248 | |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 249 | ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, LYD_NEW_PATH_UPDATE, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 250 | assert_int_equal(ret, LY_SUCCESS); |
| 251 | assert_null(node); |
| 252 | |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 253 | ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val2", 0, LYD_NEW_PATH_UPDATE, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 254 | assert_int_equal(ret, LY_SUCCESS); |
| 255 | assert_non_null(node); |
| 256 | assert_string_equal(node->schema->name, "x"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 257 | assert_string_equal("val2", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 258 | |
| 259 | lyd_free_tree(root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 262 | static void |
| 263 | test_path_ext(void **state) |
| 264 | { |
| 265 | LY_ERR ret; |
| 266 | struct lyd_node *root, *node; |
| 267 | const struct lys_module *mod; |
| 268 | const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;" |
| 269 | "import ietf-restconf {revision-date 2017-01-26; prefix rc;}" |
| 270 | "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}"; |
| 271 | |
| 272 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
| 273 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL)); |
| 274 | |
| 275 | UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod); |
| 276 | |
| 277 | /* create x */ |
| 278 | ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root); |
| 279 | assert_int_equal(ret, LY_SUCCESS); |
| 280 | assert_non_null(root); |
| 281 | assert_string_equal(root->schema->name, "c"); |
| 282 | assert_non_null(node = lyd_child(root)); |
| 283 | assert_string_equal(node->schema->name, "x"); |
| 284 | assert_string_equal("xxx", LYD_CANON_VALUE(node)); |
| 285 | |
| 286 | /* append y */ |
| 287 | ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node); |
| 288 | assert_int_equal(ret, LY_SUCCESS); |
| 289 | assert_string_equal(node->schema->name, "y"); |
| 290 | assert_string_equal("yyy", LYD_CANON_VALUE(node)); |
| 291 | |
| 292 | /* append z */ |
| 293 | ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node); |
| 294 | assert_int_equal(ret, LY_SUCCESS); |
| 295 | assert_string_equal(node->schema->name, "z"); |
| 296 | assert_string_equal("zzz", LYD_CANON_VALUE(node)); |
| 297 | |
| 298 | lyd_free_tree(root); |
| 299 | } |
| 300 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 301 | int |
| 302 | main(void) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 303 | { |
| 304 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 305 | UTEST(test_top_level), |
| 306 | UTEST(test_opaq), |
| 307 | UTEST(test_path), |
Radek Krejci | 95ccd1b | 2021-03-12 14:57:22 +0100 | [diff] [blame] | 308 | UTEST(test_path_ext), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 312 | } |