Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * @file test_tree_data.c |
| 3 | * @author: Mislav Novakovic <mislav.novakovic@sartura.hr> |
| 4 | * @brief unit tests for functions from tree_data.h header |
| 5 | * |
| 6 | * Copyright (C) 2016 Deutsche Telekom AG. |
| 7 | * |
| 8 | * Author: Mislav Novakovic <mislav.novakovic@sartura.hr> |
| 9 | * |
Luka Perkov | 0148458 | 2016-09-08 13:57:14 +0200 | [diff] [blame] | 10 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 11 | * You may not use this file except in compliance with the License. |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 12 | * You may obtain a copy of the License at |
| 13 | * |
Luka Perkov | 0148458 | 2016-09-08 13:57:14 +0200 | [diff] [blame] | 14 | * https://opensource.org/licenses/BSD-3-Clause |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <unistd.h> |
| 28 | #include <string.h> |
| 29 | |
Jan Kundrát | fd7a5c7 | 2017-10-26 17:45:06 +0200 | [diff] [blame] | 30 | #include "tests/config.h" |
| 31 | #include "libyang.h" |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 32 | #include "../../src/tree_data.h" |
| 33 | #include "../../src/tree_schema.h" |
| 34 | |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 35 | #define TMP_TEMPLATE "/tmp/libyang-XXXXXX" |
| 36 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 37 | struct ly_ctx *ctx = NULL; |
| 38 | struct lyd_node *root = NULL; |
| 39 | |
| 40 | const char *a_data_xml = "\ |
| 41 | <x xmlns=\"urn:a\">\n\ |
| 42 | <bubba>test</bubba>\n\ |
| 43 | </x>\n"; |
| 44 | |
| 45 | const char *lys_module_a = \ |
| 46 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 47 | <module name=\"a\" \ |
| 48 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" \ |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 49 | xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"\ |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 50 | xmlns:a=\"urn:a\"> \ |
| 51 | <namespace uri=\"urn:a\"/> \ |
| 52 | <prefix value=\"a_mod\"/> \ |
| 53 | <include module=\"asub\"/> \ |
| 54 | <include module=\"atop\"/> \ |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 55 | <import module=\"ietf-yang-metadata\"> \ |
| 56 | <prefix value=\"md\"/> \ |
| 57 | </import> \ |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 58 | <feature name=\"foo\"/> \ |
| 59 | <grouping name=\"gg\"> \ |
| 60 | <leaf name=\"bar-gggg\"> \ |
| 61 | <type name=\"string\"/> \ |
| 62 | </leaf> \ |
| 63 | </grouping> \ |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 64 | <md:annotation name=\"test\"> \ |
| 65 | <type name=\"string\"/> \ |
| 66 | </md:annotation> \ |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 67 | <container name=\"x\"> \ |
| 68 | <leaf name=\"bar-leaf\"> \ |
| 69 | <if-feature name=\"bar\"/> \ |
| 70 | <type name=\"string\"/> \ |
| 71 | </leaf> \ |
| 72 | <uses name=\"gg\"> \ |
| 73 | <if-feature name=\"bar\"/> \ |
| 74 | </uses> \ |
| 75 | <leaf name=\"baz\"> \ |
| 76 | <if-feature name=\"foo\"/> \ |
| 77 | <type name=\"string\"/> \ |
| 78 | </leaf> \ |
| 79 | <leaf name=\"bubba\"> \ |
| 80 | <type name=\"string\"/> \ |
| 81 | </leaf> \ |
| 82 | <leaf name=\"number32\"> \ |
| 83 | <type name=\"int32\"/> \ |
| 84 | </leaf> \ |
| 85 | <leaf name=\"number64\"> \ |
| 86 | <type name=\"int64\"/> \ |
| 87 | </leaf> \ |
Michal Vasko | 3c0eb75 | 2018-02-08 16:09:19 +0100 | [diff] [blame^] | 88 | <leaf name=\"def-leaf\"> \ |
| 89 | <type name=\"string\"/> \ |
| 90 | <default value=\"def\"/> \ |
| 91 | </leaf> \ |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 92 | </container> \ |
Radek Krejci | 434780f | 2016-09-16 15:28:27 +0200 | [diff] [blame] | 93 | <leaf name=\"y\"><type name=\"string\"/></leaf> \ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 94 | <anyxml name=\"any\"/> \ |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 95 | <augment target-node=\"/x\"> \ |
| 96 | <if-feature name=\"bar\"/> \ |
| 97 | <container name=\"bar-y\"/> \ |
| 98 | </augment> \ |
| 99 | <rpc name=\"bar-rpc\"> \ |
| 100 | <if-feature name=\"bar\"/> \ |
| 101 | </rpc> \ |
| 102 | <rpc name=\"foo-rpc\"> \ |
| 103 | <if-feature name=\"foo\"/> \ |
| 104 | </rpc> \ |
| 105 | <rpc name=\"rpc1\"> \ |
| 106 | <input> \ |
| 107 | <leaf name=\"input-leaf1\"> \ |
| 108 | <type name=\"string\"/> \ |
| 109 | </leaf> \ |
| 110 | <container name=\"x\"> \ |
| 111 | <leaf name=\"input-leaf2\"> \ |
| 112 | <type name=\"string\"/> \ |
| 113 | </leaf> \ |
| 114 | </container> \ |
| 115 | </input> \ |
| 116 | <output> \ |
| 117 | <leaf name=\"output-leaf1\"> \ |
| 118 | <type name=\"string\"/> \ |
| 119 | </leaf> \ |
| 120 | <leaf name=\"output-leaf2\"> \ |
| 121 | <type name=\"string\"/> \ |
| 122 | </leaf> \ |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 123 | <container name=\"rpc-container\"> \ |
| 124 | <leaf name=\"output-leaf3\"> \ |
| 125 | <type name=\"string\"/> \ |
| 126 | </leaf> \ |
| 127 | </container> \ |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 128 | </output> \ |
| 129 | </rpc> \ |
| 130 | <list name=\"l\"> \ |
| 131 | <key value=\"key1 key2\"/> \ |
| 132 | <leaf name=\"key1\"> \ |
| 133 | <type name=\"uint8\"/> \ |
| 134 | </leaf> \ |
| 135 | <leaf name=\"key2\"> \ |
| 136 | <type name=\"uint8\"/> \ |
| 137 | </leaf> \ |
| 138 | <leaf name=\"value\"> \ |
| 139 | <type name=\"string\"/> \ |
| 140 | </leaf> \ |
| 141 | </list> \ |
| 142 | </module> \ |
| 143 | "; |
| 144 | |
| 145 | const char *result_xml = "<x xmlns=\"urn:a\"><bubba>test</bubba></x>"; |
| 146 | |
| 147 | const char *result_xml_format ="\ |
| 148 | <x xmlns=\"urn:a\">\n\ |
| 149 | <bubba>test</bubba>\n\ |
| 150 | </x>\n\ |
| 151 | "; |
| 152 | |
| 153 | const char *result_json = "\ |
| 154 | {\n\ |
| 155 | \"a:x\": {\n\ |
| 156 | \"bubba\": \"test\"\n\ |
| 157 | }\n\ |
| 158 | }\n\ |
| 159 | "; |
| 160 | |
| 161 | int |
| 162 | generic_init(char *config_file, const char *module, char *yang_folder) |
| 163 | { |
| 164 | LYS_INFORMAT yang_format; |
| 165 | LYD_FORMAT in_format; |
| 166 | char *config = NULL; |
| 167 | struct stat sb_config; |
| 168 | int fd = -1; |
| 169 | |
Radek Krejci | be90a90 | 2016-06-14 16:11:51 +0200 | [diff] [blame] | 170 | if (!yang_folder) { |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 171 | goto error; |
| 172 | } |
| 173 | |
| 174 | yang_format = LYS_IN_YIN; |
| 175 | in_format = LYD_XML; |
| 176 | |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 177 | ctx = ly_ctx_new(yang_folder, 0); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 178 | if (!ctx) { |
| 179 | goto error; |
| 180 | } |
| 181 | |
Radek Krejci | be90a90 | 2016-06-14 16:11:51 +0200 | [diff] [blame] | 182 | if (module && !lys_parse_mem(ctx, module, yang_format)) { |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 183 | goto error; |
| 184 | } |
| 185 | |
Radek Krejci | be90a90 | 2016-06-14 16:11:51 +0200 | [diff] [blame] | 186 | if (config_file) { |
| 187 | fd = open(config_file, O_RDONLY); |
| 188 | if (fd == -1 || fstat(fd, &sb_config) == -1 || !S_ISREG(sb_config.st_mode)) { |
| 189 | goto error; |
| 190 | } |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 191 | |
Radek Krejci | be90a90 | 2016-06-14 16:11:51 +0200 | [diff] [blame] | 192 | config = mmap(NULL, sb_config.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 193 | close(fd); |
| 194 | fd = -1; |
| 195 | |
| 196 | root = lyd_parse_mem(ctx, config, in_format, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 197 | if (!root) { |
| 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | /* cleanup */ |
| 202 | munmap(config, sb_config.st_size); |
| 203 | } else { |
| 204 | root = NULL; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | |
| 209 | error: |
| 210 | if (config) { |
| 211 | munmap(config, sb_config.st_size); |
| 212 | } |
| 213 | if (fd != -1) { |
| 214 | close(fd); |
| 215 | } |
| 216 | |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | static int |
Michal Vasko | 20399fb | 2017-01-09 11:11:07 +0100 | [diff] [blame] | 221 | setup_f2(void **state) |
| 222 | { |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 223 | *state = ly_ctx_new(NULL, 0); |
Michal Vasko | 20399fb | 2017-01-09 11:11:07 +0100 | [diff] [blame] | 224 | if (!*state) { |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int |
| 232 | teardown_f2(void **state) |
| 233 | { |
| 234 | ly_ctx_destroy((struct ly_ctx *)(*state), NULL); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 239 | setup_f(void **state) |
| 240 | { |
| 241 | (void) state; /* unused */ |
| 242 | char *config_file = TESTS_DIR"/api/files/a.xml"; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 243 | char *yang_folder = TESTS_DIR"/api/files"; |
| 244 | int rc; |
| 245 | |
Radek Krejci | be90a90 | 2016-06-14 16:11:51 +0200 | [diff] [blame] | 246 | rc = generic_init(config_file, lys_module_a, yang_folder); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 247 | |
| 248 | if (rc) { |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 256 | teardown_f(void **state) |
| 257 | { |
| 258 | (void) state; /* unused */ |
| 259 | if (root) |
Radek Krejci | 5f001dd | 2016-09-07 16:26:40 +0200 | [diff] [blame] | 260 | lyd_free_withsiblings(root); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 261 | if (ctx) |
| 262 | ly_ctx_destroy(ctx, NULL); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void |
| 268 | test_lyd_parse_mem(void **state) |
| 269 | { |
| 270 | (void) state; /* unused */ |
| 271 | char *yang_folder = TESTS_DIR"/api/files"; |
| 272 | LYD_FORMAT in_format = LYD_XML; |
| 273 | LYS_INFORMAT yang_format = LYS_IN_YIN; |
| 274 | struct ly_ctx *ctx = NULL; |
| 275 | struct lyd_node *root = NULL; |
| 276 | |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 277 | ctx = ly_ctx_new(yang_folder, 0); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 278 | if (!ctx) { |
| 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | if (!lys_parse_mem(ctx, lys_module_a, yang_format)) { |
| 283 | goto error; |
| 284 | } |
| 285 | |
Radek Krejci | 8f55ad9 | 2016-04-12 13:55:56 +0200 | [diff] [blame] | 286 | root = lyd_parse_mem(ctx, a_data_xml, in_format, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 287 | if (!root) { |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | assert_string_equal("x", root->schema->name); |
| 292 | |
| 293 | if (root) |
| 294 | lyd_free(root); |
| 295 | if (ctx) |
| 296 | ly_ctx_destroy(ctx, NULL); |
| 297 | |
| 298 | return; |
| 299 | error: |
| 300 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 301 | if (ctx) |
| 302 | ly_ctx_destroy(ctx, NULL); |
| 303 | |
| 304 | fail(); |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | test_lyd_parse_fd(void **state) |
| 309 | { |
| 310 | (void) state; /* unused */ |
| 311 | char *yang_folder = TESTS_DIR"/api/files"; |
| 312 | char *config_file = TESTS_DIR"/api/files/a.xml"; |
| 313 | LYD_FORMAT in_format = LYD_XML; |
| 314 | LYS_INFORMAT yang_format = LYS_IN_YIN; |
| 315 | struct ly_ctx *ctx = NULL; |
| 316 | struct lyd_node *root = NULL; |
| 317 | struct stat sb; |
| 318 | int fd = -1; |
| 319 | |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 320 | ctx = ly_ctx_new(yang_folder, 0); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 321 | if (!ctx) { |
| 322 | goto error; |
| 323 | } |
| 324 | |
| 325 | if (!lys_parse_mem(ctx, lys_module_a, yang_format)) { |
| 326 | goto error; |
| 327 | } |
| 328 | |
| 329 | fd = open(config_file, O_RDONLY); |
| 330 | if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
| 331 | goto error; |
| 332 | } |
| 333 | |
Radek Krejci | 8f55ad9 | 2016-04-12 13:55:56 +0200 | [diff] [blame] | 334 | root = lyd_parse_fd(ctx, fd, in_format, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 335 | if (!root) { |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | assert_string_equal("x", root->schema->name); |
| 340 | |
| 341 | if (root) |
| 342 | lyd_free(root); |
| 343 | if (ctx) |
| 344 | ly_ctx_destroy(ctx, NULL); |
| 345 | if (fd > 0) |
| 346 | close(fd); |
| 347 | |
| 348 | return; |
| 349 | error: |
| 350 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 351 | if (ctx) |
| 352 | ly_ctx_destroy(ctx, NULL); |
| 353 | if (fd > 0) |
| 354 | close(fd); |
| 355 | fail(); |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | test_lyd_parse_path(void **state) |
| 360 | { |
| 361 | (void) state; /* unused */ |
| 362 | char *yang_folder = TESTS_DIR"/api/files"; |
| 363 | char *config_file = TESTS_DIR"/api/files/a.xml"; |
| 364 | LYD_FORMAT in_format = LYD_XML; |
| 365 | LYS_INFORMAT yang_format = LYS_IN_YIN; |
| 366 | struct ly_ctx *ctx = NULL; |
| 367 | struct lyd_node *root = NULL; |
| 368 | |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 369 | ctx = ly_ctx_new(yang_folder, 0); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 370 | if (!ctx) { |
| 371 | goto error; |
| 372 | } |
| 373 | |
| 374 | if (!lys_parse_mem(ctx, lys_module_a, yang_format)) { |
| 375 | goto error; |
| 376 | } |
| 377 | |
Radek Krejci | 8f55ad9 | 2016-04-12 13:55:56 +0200 | [diff] [blame] | 378 | root = lyd_parse_path(ctx, config_file, in_format, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 379 | if (!root) { |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | assert_string_equal("x", root->schema->name); |
| 384 | |
| 385 | if (root) |
| 386 | lyd_free(root); |
| 387 | if (ctx) |
| 388 | ly_ctx_destroy(ctx, NULL); |
| 389 | |
| 390 | return; |
| 391 | error: |
| 392 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 393 | if (ctx) |
| 394 | ly_ctx_destroy(ctx, NULL); |
| 395 | fail(); |
| 396 | } |
| 397 | |
| 398 | static void |
| 399 | test_lyd_parse_xml(void **state) |
| 400 | { |
| 401 | (void) state; /* unused */ |
| 402 | char *yang_folder = TESTS_DIR"/api/files"; |
| 403 | struct lyd_node *node = NULL; |
| 404 | const struct lys_module *module = NULL; |
| 405 | struct lyxml_elem *root_xml = NULL; |
| 406 | LYS_INFORMAT yang_format = LYS_IN_YIN; |
| 407 | struct ly_ctx *ctx = NULL; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 408 | |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 409 | ctx = ly_ctx_new(yang_folder, 0); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 410 | if (!ctx) { |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | module = lys_parse_mem(ctx, lys_module_a, yang_format); |
| 415 | if (!module) { |
| 416 | goto error; |
| 417 | } |
| 418 | |
| 419 | root_xml = lyxml_parse_mem(ctx, a_data_xml, 1); |
| 420 | |
Radek Krejci | 8f55ad9 | 2016-04-12 13:55:56 +0200 | [diff] [blame] | 421 | node = lyd_parse_xml(ctx, &root_xml, LYD_OPT_NOSIBLINGS | LYD_OPT_DATA); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 422 | if (!node) { |
| 423 | goto error; |
| 424 | } |
| 425 | |
| 426 | assert_string_equal("x", node->schema->name); |
| 427 | |
| 428 | if (node) |
| 429 | lyd_free(node); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 430 | if (root_xml) |
| 431 | lyxml_free(ctx, root_xml); |
| 432 | if (ctx) |
| 433 | ly_ctx_destroy(ctx, NULL); |
| 434 | |
| 435 | return; |
| 436 | error: |
| 437 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 438 | if (root_xml) |
| 439 | lyxml_free(ctx, root_xml); |
| 440 | if (ctx) |
| 441 | ly_ctx_destroy(ctx, NULL); |
| 442 | fail(); |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | test_lyd_new(void **state) |
| 447 | { |
| 448 | (void) state; /* unused */ |
| 449 | struct lyd_node *new = NULL; |
| 450 | |
| 451 | new = lyd_new(root, root->child->schema->module, "bar-y"); |
| 452 | if (!new) { |
| 453 | fail(); |
| 454 | } |
| 455 | |
| 456 | assert_string_equal("bar-y", new->schema->name); |
| 457 | } |
| 458 | |
| 459 | static void |
| 460 | test_lyd_new_leaf(void **state) |
| 461 | { |
| 462 | (void) state; /* unused */ |
| 463 | struct lyd_node *new = NULL; |
| 464 | struct lyd_node_leaf_list *result; |
| 465 | |
| 466 | new = lyd_new_leaf(root, root->schema->module, "number32", "100"); |
| 467 | if (!new) { |
| 468 | fail(); |
| 469 | } |
| 470 | |
| 471 | result = (struct lyd_node_leaf_list *) new; |
| 472 | assert_string_equal("100", result->value_str); |
| 473 | } |
| 474 | |
| 475 | static void |
| 476 | test_lyd_change_leaf(void **state) |
| 477 | { |
| 478 | (void) state; /* unused */ |
| 479 | struct lyd_node_leaf_list *result = NULL; |
| 480 | int rc; |
| 481 | |
| 482 | result = (struct lyd_node_leaf_list *) root->child; |
| 483 | assert_string_equal("test", result->value_str); |
| 484 | |
| 485 | rc = lyd_change_leaf(result, "new_test"); |
| 486 | if (rc) { |
| 487 | fail(); |
| 488 | } |
| 489 | |
| 490 | assert_string_equal("new_test", result->value_str); |
| 491 | |
| 492 | } |
| 493 | |
| 494 | static void |
| 495 | test_lyd_output_new_leaf(void **state) |
| 496 | { |
| 497 | (void) state; /* unused */ |
| 498 | struct lyd_node *node = NULL; |
| 499 | struct lyd_node_leaf_list *result = NULL; |
| 500 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 501 | node = lyd_new_output(NULL, root->schema->module, "rpc1"); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 502 | if (!node) { |
| 503 | fail(); |
| 504 | } |
| 505 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 506 | result = (struct lyd_node_leaf_list *)lyd_new_output_leaf(node, NULL, "output-leaf1", "test"); |
| 507 | if (!result) { |
| 508 | fail(); |
| 509 | } |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 510 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 511 | assert_string_equal("test", result->value_str); |
| 512 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 513 | lyd_free(node); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 517 | test_lyd_new_path(void **state) |
| 518 | { |
| 519 | (void) state; /* unused */ |
| 520 | struct lyd_node *node, *root; |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 521 | char *str; |
| 522 | struct lyxml_elem *xml; |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 523 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 524 | root = lyd_new_path(NULL, ctx, "/a:x/bar-gggg", "a", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 525 | assert_non_null(root); |
| 526 | assert_string_equal(root->schema->name, "x"); |
| 527 | assert_string_equal(root->child->schema->name, "bar-gggg"); |
| 528 | |
Michal Vasko | 3c0eb75 | 2018-02-08 16:09:19 +0100 | [diff] [blame^] | 529 | /* create a default node first, then implicitly rewrite it, then fail to rewrite it again */ |
| 530 | node = lyd_new_path(root, NULL, "def-leaf", "def", 0, LYD_PATH_OPT_DFLT); |
| 531 | assert_non_null(node); |
| 532 | assert_string_equal(node->schema->name, "def-leaf"); |
| 533 | assert_int_equal(node->dflt, 1); |
| 534 | |
| 535 | node = lyd_new_path(root, NULL, "def-leaf", "def", 0, 0); |
| 536 | assert_non_null(node); |
| 537 | assert_int_equal(node->dflt, 0); |
| 538 | |
| 539 | node = lyd_new_path(root, NULL, "def-leaf", "def", 0, 0); |
| 540 | assert_null(node); |
| 541 | assert_int_equal(ly_errno, LY_EVALID); |
| 542 | ly_errno = 0; |
| 543 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 544 | node = lyd_new_path(root, NULL, "bubba", "b", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 545 | assert_non_null(node); |
| 546 | assert_string_equal(node->schema->name, "bubba"); |
| 547 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 548 | node = lyd_new_path(root, NULL, "/a:x/number32", "3", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 549 | assert_non_null(node); |
| 550 | assert_string_equal(node->schema->name, "number32"); |
| 551 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 552 | node = lyd_new_path(root, NULL, "a:number64", "64", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 553 | assert_non_null(node); |
| 554 | assert_string_equal(node->schema->name, "number64"); |
| 555 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 556 | node = lyd_new_path(root, NULL, "/a:l[key1='111'][key2='222']", NULL, 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 557 | assert_non_null(node); |
| 558 | assert_string_equal(node->schema->name, "l"); |
| 559 | assert_ptr_not_equal(root->prev, root); |
| 560 | |
Radek Krejci | 4d0d935 | 2016-03-31 12:45:42 +0200 | [diff] [blame] | 561 | lyd_free_withsiblings(root); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 562 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 563 | root = lyd_new_path(NULL, ctx, "/a:l[key1='1'][key2='2']", NULL, 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 564 | assert_non_null(root); |
| 565 | assert_string_equal(root->schema->name, "l"); |
| 566 | assert_string_equal(root->child->schema->name, "key1"); |
| 567 | assert_string_equal(root->child->next->schema->name, "key2"); |
| 568 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 569 | node = lyd_new_path(root, NULL, "/a:l[key1='11'][key2='22']/value", "val", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 570 | assert_non_null(node); |
| 571 | assert_ptr_not_equal(root->prev, root); |
| 572 | assert_string_equal(node->schema->name, "l"); |
| 573 | assert_string_equal(node->child->schema->name, "key1"); |
| 574 | assert_string_equal(node->child->next->schema->name, "key2"); |
| 575 | assert_string_equal(node->child->next->next->schema->name, "value"); |
| 576 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 577 | node = lyd_new_path(root, NULL, "/a:l[key1='1'][key2='2']/value", "val2", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 578 | assert_non_null(node); |
| 579 | assert_string_equal(node->schema->name, "value"); |
| 580 | |
Radek Krejci | 4d0d935 | 2016-03-31 12:45:42 +0200 | [diff] [blame] | 581 | lyd_free_withsiblings(root); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 582 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 583 | root = lyd_new_path(NULL, ctx, "/a:any", "test <&>\"", LYD_ANYDATA_CONSTSTRING, 0); |
| 584 | assert_non_null(root); |
| 585 | str = NULL; |
| 586 | lyd_print_mem(&str, root, LYD_XML, 0); |
| 587 | assert_non_null(root); |
| 588 | assert_string_equal(str, "<any xmlns=\"urn:a\">test <&>"</any>"); |
| 589 | free(str); |
| 590 | lyd_free(root); |
| 591 | |
| 592 | xml = lyxml_parse_mem(ctx, "<test><</test>", 0); |
| 593 | assert_non_null(xml); |
| 594 | root = lyd_new_path(NULL, ctx, "/a:any", xml, LYD_ANYDATA_XML, 0); |
| 595 | xml = NULL; |
| 596 | assert_non_null(root); |
| 597 | lyd_print_mem(&str, root, LYD_XML, 0); |
| 598 | assert_non_null(root); |
| 599 | assert_string_equal(str, "<any xmlns=\"urn:a\"><test><</test></any>"); |
| 600 | free(str); |
| 601 | |
| 602 | node = root; |
| 603 | root = lyd_new_path(NULL, ctx, "/a:any", node, LYD_ANYDATA_DATATREE, 0); |
| 604 | assert_non_null(root); |
| 605 | str = NULL; |
| 606 | lyd_print_mem(&str, root, LYD_XML, 0); |
| 607 | assert_non_null(root); |
| 608 | assert_string_equal(str, "<any xmlns=\"urn:a\"><any xmlns=\"urn:a\"><test><</test></any></any>"); |
| 609 | free(str); |
| 610 | lyd_free(root); |
| 611 | |
| 612 | root = lyd_new_path(NULL, ctx, "/a:rpc1/x/input-leaf2", "dudu", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 613 | assert_non_null(root); |
| 614 | assert_string_equal(root->schema->name, "rpc1"); |
| 615 | assert_string_equal(root->child->schema->name, "x"); |
| 616 | assert_string_equal(root->child->child->schema->name, "input-leaf2"); |
| 617 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 618 | node = lyd_new_path(root, NULL, "/a:rpc1/input-leaf1", "bubu", 0, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 619 | assert_non_null(node); |
| 620 | assert_string_equal(node->schema->name, "input-leaf1"); |
| 621 | assert_string_equal(root->child->schema->name, "input-leaf1"); |
| 622 | assert_string_equal(root->child->next->schema->name, "x"); |
| 623 | |
| 624 | lyd_free(root); |
| 625 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 626 | root = lyd_new_path(NULL, ctx, "/a:rpc1/rpc-container", NULL, 0, LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 627 | assert_non_null(root); |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 628 | assert_string_equal(root->schema->name, "rpc1"); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 629 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 630 | node = lyd_new_path(root->child, NULL, "output-leaf3", "cc", 0, LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 631 | assert_non_null(node); |
| 632 | assert_string_equal(node->schema->name, "output-leaf3"); |
| 633 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 634 | node = lyd_new_path(root, NULL, "/a:rpc1/output-leaf1", "aa", 0, LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 635 | assert_non_null(node); |
| 636 | assert_string_equal(node->schema->name, "output-leaf1"); |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 637 | assert_ptr_equal(node, root->child); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 638 | |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 639 | node = lyd_new_path(root, NULL, "/a:rpc1/output-leaf2", "bb", 0, LYD_PATH_OPT_OUTPUT); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 640 | assert_non_null(node); |
| 641 | assert_string_equal(node->schema->name, "output-leaf2"); |
| 642 | assert_string_equal(node->prev->schema->name, "output-leaf1"); |
| 643 | assert_string_equal(node->next->schema->name, "rpc-container"); |
| 644 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 645 | lyd_free(root); |
Michal Vasko | 58c2aab | 2017-01-05 10:02:05 +0100 | [diff] [blame] | 646 | |
| 647 | root = lyd_new_path(NULL, ctx, "/a:l", NULL, 0, 0); |
| 648 | assert_non_null(root); |
| 649 | assert_string_equal(root->schema->name, "l"); |
| 650 | |
| 651 | node = lyd_new_path(root, NULL, "/a:l[key1='11'][key2='22']/value", "val", 0, 0); |
| 652 | assert_non_null(node); |
| 653 | assert_ptr_not_equal(root->prev, root); |
| 654 | assert_string_equal(node->schema->name, "l"); |
| 655 | assert_string_equal(node->child->schema->name, "key1"); |
| 656 | |
| 657 | node = lyd_new_path(root, NULL, "/a:l[1]/key1", "1", 0, 0); |
| 658 | assert_non_null(node); |
| 659 | assert_string_equal(node->schema->name, "key1"); |
| 660 | |
| 661 | node = lyd_new_path(root, NULL, "/a:l[1]/key2", "2", 0, 0); |
| 662 | assert_non_null(node); |
| 663 | assert_string_equal(node->schema->name, "key2"); |
| 664 | |
| 665 | node = lyd_new_path(root, NULL, "/a:l[1]/value", "vall", 0, 0); |
| 666 | assert_non_null(node); |
| 667 | assert_string_equal(node->schema->name, "value"); |
| 668 | |
| 669 | node = lyd_new_path(root, NULL, "/a:l[2]/value", "vall", 0, 0); |
| 670 | assert_null(node); |
| 671 | |
| 672 | node = lyd_new_path(root, NULL, "/a:l[3]/value", "vall", 0, 0); |
| 673 | assert_non_null(node); |
| 674 | assert_string_equal(node->schema->name, "l"); |
| 675 | |
| 676 | node = lyd_new_path(root, NULL, "/a:l[0]", NULL, 0, 0); |
| 677 | assert_null(node); |
| 678 | |
| 679 | lyd_free_withsiblings(root); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static void |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 683 | test_lyd_dup(void **state) |
| 684 | { |
| 685 | (void) state; /* unused */ |
| 686 | struct lyd_node *copy = NULL; |
| 687 | |
| 688 | copy = lyd_dup(root->child, 0); |
| 689 | if (!copy) { |
| 690 | fail(); |
| 691 | } |
| 692 | |
| 693 | assert_string_equal("bubba", copy->schema->name); |
| 694 | |
Michal Vasko | bf70464 | 2016-06-06 16:24:07 +0200 | [diff] [blame] | 695 | lyd_free(copy); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static void |
| 699 | test_lyd_insert(void **state) |
| 700 | { |
| 701 | (void) state; /* unused */ |
| 702 | struct lyd_node *new = NULL; |
| 703 | struct lyd_node *node = root; |
| 704 | struct lyd_node_leaf_list *result; |
| 705 | int rc; |
| 706 | |
| 707 | result = (struct lyd_node_leaf_list *) root->child; |
| 708 | assert_string_equal("test", result->value_str); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 709 | |
| 710 | new = lyd_new_leaf(root, node->schema->module, "number32", "100"); |
| 711 | if (!new) { |
| 712 | fail(); |
| 713 | } |
| 714 | |
| 715 | rc = lyd_insert(node, new); |
| 716 | if (rc) { |
| 717 | fail(); |
| 718 | } |
| 719 | |
Radek Krejci | 5f001dd | 2016-09-07 16:26:40 +0200 | [diff] [blame] | 720 | result = (struct lyd_node_leaf_list *) root->child->prev; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 721 | assert_string_equal("100", result->value_str); |
Radek Krejci | 76b4d7d | 2016-09-09 10:18:31 +0200 | [diff] [blame] | 722 | |
| 723 | /* test inserting an empty container that is already present */ |
| 724 | node = NULL; |
| 725 | assert_int_equal(lyd_validate(&node, LYD_OPT_CONFIG, ctx), 0); |
| 726 | assert_ptr_not_equal(node, NULL); |
| 727 | assert_string_equal(node->schema->name, "top"); |
| 728 | assert_ptr_not_equal(node->child, NULL); |
| 729 | assert_string_equal(node->child->schema->name, "bar-sub2"); |
| 730 | assert_int_equal(node->child->dflt, 1); |
| 731 | |
| 732 | new = lyd_new_path(NULL, ctx, "/a:top/bar-sub2", NULL, 0, 0); |
| 733 | assert_ptr_not_equal(new, NULL); |
| 734 | assert_string_equal(new->schema->name, "top"); |
| 735 | assert_int_equal(lyd_insert(node, new->child), 0); |
| 736 | assert_int_not_equal(node->child->dflt, 1); |
| 737 | |
| 738 | lyd_free_withsiblings(node); |
| 739 | lyd_free_withsiblings(new); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static void |
Radek Krejci | 434780f | 2016-09-16 15:28:27 +0200 | [diff] [blame] | 743 | test_lyd_insert_sibling(void **state) |
| 744 | { |
| 745 | (void) state; /* unused */ |
| 746 | struct lyd_node *new = NULL, *last; |
| 747 | |
| 748 | new = lyd_new_leaf(NULL, root->schema->module, "y", "test"); |
| 749 | assert_ptr_not_equal(new, NULL); |
| 750 | last = root->prev; |
| 751 | |
| 752 | assert_int_equal(lyd_insert_sibling(&root, new), 0); |
| 753 | assert_ptr_not_equal(last, root->prev); |
| 754 | assert_string_equal(root->prev->schema->name, "y"); |
| 755 | assert_string_equal(((struct lyd_node_leaf_list *)root->prev)->value_str, "test"); |
| 756 | } |
| 757 | |
| 758 | static void |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 759 | test_lyd_insert_before(void **state) |
| 760 | { |
| 761 | (void) state; /* unused */ |
| 762 | struct lyd_node *new = NULL; |
| 763 | struct lyd_node_leaf_list *result; |
| 764 | int rc; |
| 765 | |
| 766 | result = (struct lyd_node_leaf_list *) root->child; |
| 767 | assert_string_equal("test", result->value_str); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 768 | |
| 769 | new = lyd_new_leaf(root, root->child->schema->module, "number32", "1"); |
| 770 | if (!new) { |
| 771 | fail(); |
| 772 | } |
| 773 | |
| 774 | rc = lyd_insert(root, new); |
| 775 | if (rc) { |
| 776 | fail(); |
| 777 | } |
| 778 | |
| 779 | new = lyd_new_leaf(root, root->child->schema->module, "number64", "1000"); |
| 780 | if (!new) { |
| 781 | fail(); |
| 782 | } |
| 783 | |
| 784 | rc = lyd_insert_before(root->child->next, new); |
| 785 | if (rc) { |
| 786 | fail(); |
| 787 | } |
| 788 | |
Radek Krejci | 5f001dd | 2016-09-07 16:26:40 +0200 | [diff] [blame] | 789 | result = (struct lyd_node_leaf_list *) root->child->prev; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 790 | assert_string_equal("1", result->value_str); |
| 791 | } |
| 792 | |
| 793 | static void |
| 794 | test_lyd_insert_after(void **state) |
| 795 | { |
| 796 | (void) state; /* unused */ |
| 797 | struct lyd_node *new = NULL; |
| 798 | struct lyd_node_leaf_list *result; |
| 799 | int rc; |
| 800 | |
| 801 | result = (struct lyd_node_leaf_list *) root->child; |
| 802 | assert_string_equal("test", result->value_str); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 803 | |
| 804 | new = lyd_new_leaf(root, root->child->schema->module, "number32", "1"); |
| 805 | if (!new) { |
| 806 | fail(); |
| 807 | } |
| 808 | |
| 809 | rc = lyd_insert(root, new); |
| 810 | if (rc) { |
| 811 | fail(); |
| 812 | } |
| 813 | |
| 814 | new = lyd_new_leaf(root, root->child->schema->module, "number64", "1000"); |
| 815 | if (!new) { |
| 816 | fail(); |
| 817 | } |
| 818 | |
| 819 | rc = lyd_insert_after(root->child->next, new); |
| 820 | if (rc) { |
| 821 | fail(); |
| 822 | } |
| 823 | |
| 824 | result = (struct lyd_node_leaf_list *) root->child->next->next; |
| 825 | assert_string_equal("1000", result->value_str); |
| 826 | } |
| 827 | |
| 828 | static void |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 829 | test_lyd_schema_sort(void **state) |
| 830 | { |
| 831 | (void) state; /* unused */ |
| 832 | const struct lys_module *module; |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 833 | struct lyd_node *root, *node, *node2; |
| 834 | |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 835 | module = ly_ctx_get_module(ctx, "a", NULL, 0); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 836 | assert_non_null(module); |
| 837 | |
| 838 | root = lyd_new(NULL, module, "l"); |
| 839 | assert_non_null(root); |
| 840 | node = lyd_new_leaf(root, NULL, "key1", "1"); |
| 841 | assert_non_null(node); |
| 842 | node = lyd_new_leaf(root, NULL, "key2", "2"); |
| 843 | assert_non_null(node); |
| 844 | |
| 845 | node = lyd_new(NULL, module, "x"); |
| 846 | assert_non_null(node); |
| 847 | assert_int_equal(lyd_insert_after(root, node), 0); |
| 848 | |
| 849 | node2 = lyd_new_leaf(node, NULL, "bubba", "a"); |
| 850 | assert_non_null(node2); |
| 851 | node2 = lyd_new_leaf(node, NULL, "bar-gggg", "b"); |
| 852 | assert_non_null(node2); |
| 853 | node2 = lyd_new_leaf(node, NULL, "number64", "64"); |
| 854 | assert_non_null(node2); |
| 855 | node2 = lyd_new_leaf(node, NULL, "number32", "32"); |
| 856 | assert_non_null(node2); |
| 857 | |
| 858 | assert_int_equal(lyd_schema_sort(root, 1), 0); |
| 859 | |
| 860 | root = node; |
| 861 | assert_string_equal(root->schema->name, "x"); |
| 862 | assert_string_equal(root->next->schema->name, "l"); |
| 863 | |
| 864 | assert_string_equal(root->child->schema->name, "bar-gggg"); |
| 865 | assert_string_equal(root->child->next->schema->name, "bubba"); |
| 866 | assert_string_equal(root->child->next->next->schema->name, "number32"); |
| 867 | assert_string_equal(root->child->next->next->next->schema->name, "number64"); |
| 868 | |
| 869 | lyd_free_withsiblings(root); |
| 870 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 871 | root = lyd_new_output(NULL, module, "rpc1"); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 872 | assert_non_null(root); |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 873 | node = lyd_new_output(root, NULL, "rpc-container"); |
| 874 | assert_non_null(node); |
| 875 | node2 = lyd_new_output_leaf(node, NULL, "output-leaf3", "ff"); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 876 | assert_non_null(node2); |
| 877 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 878 | node = lyd_new_output_leaf(root, NULL, "output-leaf1", "gg"); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 879 | assert_non_null(node); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 880 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 881 | node = lyd_new_output_leaf(root, NULL, "output-leaf2", "hh"); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 882 | assert_non_null(node); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 883 | |
| 884 | assert_int_equal(lyd_schema_sort(root, 1), 0); |
| 885 | |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 886 | node = root->child; |
| 887 | assert_string_equal(node->schema->name, "output-leaf1"); |
| 888 | assert_string_equal(node->next->schema->name, "output-leaf2"); |
| 889 | assert_string_equal(node->next->next->schema->name, "rpc-container"); |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 890 | |
| 891 | lyd_free_withsiblings(root); |
| 892 | } |
| 893 | |
| 894 | static void |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 895 | test_lyd_find_path(void **state) |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 896 | { |
| 897 | (void) state; /* unused */ |
| 898 | struct ly_set *set = NULL; |
| 899 | struct lyd_node_leaf_list *result; |
| 900 | |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 901 | set = lyd_find_path(root->child, "/a:x/bubba"); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 902 | |
| 903 | struct lyd_node *node = *set->set.d; |
| 904 | result = (struct lyd_node_leaf_list *) node; |
| 905 | assert_string_equal("test", result->value_str); |
| 906 | |
| 907 | ly_set_free(set); |
| 908 | } |
| 909 | |
| 910 | static void |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 911 | test_lyd_find_instance(void **state) |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 912 | { |
| 913 | (void) state; /* unused */ |
| 914 | struct ly_set *set = NULL; |
| 915 | struct lyd_node_leaf_list *result; |
| 916 | |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 917 | set = lyd_find_instance(root->child, root->child->schema); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 918 | if (!set) { |
| 919 | fail(); |
| 920 | } |
| 921 | |
| 922 | struct lyd_node *node = *set->set.d; |
| 923 | result = (struct lyd_node_leaf_list *) node; |
| 924 | assert_string_equal("test", result->value_str); |
| 925 | |
| 926 | ly_set_free(set); |
| 927 | } |
| 928 | |
| 929 | static void |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 930 | test_lyd_validate(void **state) |
| 931 | { |
| 932 | (void) state; /* unused */ |
| 933 | struct lyd_node *new = NULL; |
| 934 | struct lyd_node *node = root; |
| 935 | int rc; |
| 936 | |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 937 | rc = lyd_validate(&root, LYD_OPT_CONFIG, NULL); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 938 | if (rc) { |
| 939 | fail(); |
| 940 | } |
| 941 | |
Michal Vasko | 3c0eb75 | 2018-02-08 16:09:19 +0100 | [diff] [blame^] | 942 | if (root->child->next->next) { |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 943 | fail(); |
| 944 | } |
| 945 | |
| 946 | new = lyd_new_leaf(root, node->schema->module, "number32", "1"); |
| 947 | if (!new) { |
| 948 | fail(); |
| 949 | } |
| 950 | |
| 951 | rc = lyd_insert(root, new); |
| 952 | if (rc) { |
| 953 | fail(); |
| 954 | } |
| 955 | |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 956 | rc = lyd_validate(&root, LYD_OPT_CONFIG, NULL); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 957 | if (rc) { |
| 958 | fail(); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static void |
| 963 | test_lyd_unlink(void **state) |
| 964 | { |
| 965 | (void) state; /* unused */ |
| 966 | struct lyd_node *new = NULL; |
| 967 | struct lyd_node *node = root->child; |
| 968 | struct lyd_node_leaf_list *result; |
| 969 | int rc; |
| 970 | |
| 971 | new = lyd_new_leaf(root, node->schema->module, "number32", "1"); |
| 972 | if (!new) { |
| 973 | fail(); |
| 974 | } |
| 975 | |
| 976 | rc = lyd_insert(root, new); |
| 977 | if (rc) { |
| 978 | fail(); |
| 979 | } |
| 980 | |
Radek Krejci | 5f001dd | 2016-09-07 16:26:40 +0200 | [diff] [blame] | 981 | result = (struct lyd_node_leaf_list *) node->prev; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 982 | assert_string_equal("1", result->value_str); |
| 983 | |
Radek Krejci | 5f001dd | 2016-09-07 16:26:40 +0200 | [diff] [blame] | 984 | rc = lyd_unlink(node->prev); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 985 | if (rc) { |
| 986 | fail(); |
| 987 | } |
| 988 | |
Radek Krejci | 5f001dd | 2016-09-07 16:26:40 +0200 | [diff] [blame] | 989 | assert_string_not_equal("1", ((struct lyd_node_leaf_list *)node->prev)->value_str); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 990 | |
Michal Vasko | bf70464 | 2016-06-06 16:24:07 +0200 | [diff] [blame] | 991 | lyd_free(new); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static void |
| 995 | test_lyd_free(void **state) |
| 996 | { |
| 997 | (void) state; /* unused */ |
| 998 | struct lyd_node *copy = NULL; |
| 999 | |
| 1000 | copy = lyd_dup(root->child, 0); |
| 1001 | if (!copy) { |
| 1002 | fail(); |
| 1003 | } |
| 1004 | |
| 1005 | lyd_free(copy); |
| 1006 | } |
| 1007 | |
| 1008 | static void |
| 1009 | test_lyd_free_withsiblings(void **state) |
| 1010 | { |
| 1011 | (void) state; /* unused */ |
| 1012 | struct lyd_node *copy = NULL; |
| 1013 | struct lyd_node *new = NULL; |
| 1014 | struct lyd_node *node = root->child; |
| 1015 | int rc; |
| 1016 | |
| 1017 | copy = lyd_dup(root->child, 0); |
| 1018 | if (!copy) { |
| 1019 | fail(); |
| 1020 | } |
| 1021 | |
| 1022 | new = lyd_new_leaf(root, node->schema->module, "number32", "1"); |
| 1023 | if (!new) { |
| 1024 | fail(); |
| 1025 | } |
| 1026 | |
| 1027 | rc = lyd_insert(copy, new); |
| 1028 | if (!rc) { |
| 1029 | fail(); |
| 1030 | } |
| 1031 | |
| 1032 | lyd_free_withsiblings(copy); |
| 1033 | } |
| 1034 | |
| 1035 | static void |
| 1036 | test_lyd_insert_attr(void **state) |
| 1037 | { |
| 1038 | (void) state; /* unused */ |
| 1039 | struct lyd_attr *attr = NULL; |
| 1040 | struct lyd_node *node = root->child; |
| 1041 | |
Radek Krejci | 1e6bc0c | 2016-03-23 14:15:17 +0100 | [diff] [blame] | 1042 | attr = lyd_insert_attr(node, NULL, "test", "test"); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1043 | if (!attr) { |
| 1044 | fail(); |
| 1045 | } |
| 1046 | |
| 1047 | assert_string_equal("test", node->attr->name); |
| 1048 | } |
| 1049 | |
| 1050 | static void |
| 1051 | test_lyd_free_attr(void **state) |
| 1052 | { |
| 1053 | (void) state; /* unused */ |
| 1054 | struct lyd_attr *attr = NULL; |
| 1055 | struct lyd_node *node = root->child; |
| 1056 | |
Radek Krejci | 1e6bc0c | 2016-03-23 14:15:17 +0100 | [diff] [blame] | 1057 | attr = lyd_insert_attr(node, NULL, "test", "test"); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1058 | |
| 1059 | if (!node->attr) { |
| 1060 | fail(); |
| 1061 | } |
| 1062 | |
| 1063 | lyd_free_attr(ctx, node, attr, 0); |
| 1064 | |
| 1065 | if (node->attr) { |
| 1066 | fail(); |
| 1067 | } |
| 1068 | |
| 1069 | } |
| 1070 | |
| 1071 | static void |
| 1072 | test_lyd_print_mem_xml(void **state) |
| 1073 | { |
| 1074 | (void) state; /* unused */ |
| 1075 | char *result = NULL; |
| 1076 | int rc; |
| 1077 | |
| 1078 | rc = lyd_print_mem(&result, root, LYD_XML, 0); |
| 1079 | if (rc) { |
| 1080 | fail(); |
| 1081 | } |
| 1082 | |
| 1083 | assert_string_equal(result_xml, result); |
| 1084 | free(result); |
| 1085 | } |
| 1086 | |
| 1087 | static void |
| 1088 | test_lyd_print_mem_xml_format(void **state) |
| 1089 | { |
| 1090 | (void) state; /* unused */ |
| 1091 | char *result = NULL; |
| 1092 | int rc; |
| 1093 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1094 | rc = lyd_print_mem(&result, root, LYD_XML, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1095 | if (rc) { |
| 1096 | fail(); |
| 1097 | } |
| 1098 | |
| 1099 | assert_string_equal(result_xml_format, result); |
| 1100 | free(result); |
| 1101 | } |
| 1102 | |
| 1103 | static void |
| 1104 | test_lyd_print_mem_json(void **state) |
| 1105 | { |
| 1106 | (void) state; /* unused */ |
| 1107 | char *result = NULL; |
| 1108 | int rc; |
| 1109 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1110 | rc = lyd_print_mem(&result, root, LYD_JSON, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1111 | if (rc) { |
| 1112 | fail(); |
| 1113 | } |
| 1114 | |
| 1115 | assert_string_equal(result_json, result); |
| 1116 | free(result); |
| 1117 | } |
| 1118 | |
| 1119 | static void |
| 1120 | test_lyd_print_fd_xml(void **state) |
| 1121 | { |
| 1122 | (void) state; /* unused */ |
| 1123 | char *result = NULL; |
| 1124 | struct stat sb; |
Radek Krejci | ca73f50 | 2016-09-30 14:48:54 +0200 | [diff] [blame] | 1125 | char file_name[20]; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1126 | int rc; |
| 1127 | int fd; |
| 1128 | |
| 1129 | memset(file_name, 0, sizeof(file_name)); |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 1130 | strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE)); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1131 | |
| 1132 | fd = mkstemp(file_name); |
| 1133 | if (fd < 1) { |
| 1134 | goto error; |
| 1135 | } |
| 1136 | |
| 1137 | rc = lyd_print_fd(fd, root, LYD_XML, 0); |
| 1138 | if (rc) { |
| 1139 | goto error; |
| 1140 | } |
| 1141 | |
| 1142 | if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
| 1143 | goto error; |
| 1144 | } |
| 1145 | |
| 1146 | result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1147 | |
| 1148 | assert_string_equal(result_xml, result); |
| 1149 | |
| 1150 | close(fd); |
| 1151 | unlink(file_name); |
| 1152 | |
| 1153 | return; |
| 1154 | error: |
| 1155 | if (fd > 0) { |
| 1156 | close(fd); |
| 1157 | unlink(file_name); |
| 1158 | } |
| 1159 | fail(); |
| 1160 | } |
| 1161 | |
| 1162 | static void |
| 1163 | test_lyd_print_fd_xml_format(void **state) |
| 1164 | { |
| 1165 | (void) state; /* unused */ |
| 1166 | char *result = NULL; |
| 1167 | struct stat sb; |
Radek Krejci | ca73f50 | 2016-09-30 14:48:54 +0200 | [diff] [blame] | 1168 | char file_name[20]; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1169 | int rc; |
| 1170 | int fd; |
| 1171 | |
| 1172 | memset(file_name, 0, sizeof(file_name)); |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 1173 | strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE)); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1174 | |
| 1175 | fd = mkstemp(file_name); |
| 1176 | if (fd < 1) { |
| 1177 | goto error; |
| 1178 | } |
| 1179 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1180 | rc = lyd_print_fd(fd, root, LYD_XML, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1181 | if (rc) { |
| 1182 | goto error; |
| 1183 | } |
| 1184 | |
| 1185 | if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
| 1186 | goto error; |
| 1187 | } |
| 1188 | |
| 1189 | result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1190 | |
| 1191 | assert_string_equal(result_xml_format, result); |
| 1192 | |
| 1193 | close(fd); |
| 1194 | unlink(file_name); |
| 1195 | |
| 1196 | return; |
| 1197 | error: |
| 1198 | if (fd > 0) { |
| 1199 | close(fd); |
| 1200 | unlink(file_name); |
| 1201 | } |
| 1202 | fail(); |
| 1203 | } |
| 1204 | |
| 1205 | static void |
| 1206 | test_lyd_print_fd_json(void **state) |
| 1207 | { |
| 1208 | (void) state; /* unused */ |
| 1209 | char *result = NULL; |
| 1210 | struct stat sb; |
Radek Krejci | ca73f50 | 2016-09-30 14:48:54 +0200 | [diff] [blame] | 1211 | char file_name[20]; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1212 | int rc; |
| 1213 | int fd; |
| 1214 | |
| 1215 | memset(file_name, 0, sizeof(file_name)); |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 1216 | strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE)); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1217 | |
| 1218 | fd = mkstemp(file_name); |
| 1219 | if (fd < 1) { |
| 1220 | goto error; |
| 1221 | } |
| 1222 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1223 | rc = lyd_print_fd(fd, root, LYD_JSON, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1224 | if (rc) { |
| 1225 | goto error; |
| 1226 | } |
| 1227 | |
| 1228 | if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
| 1229 | goto error; |
| 1230 | } |
| 1231 | |
| 1232 | result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1233 | |
| 1234 | assert_string_equal(result_json, result); |
| 1235 | |
| 1236 | close(fd); |
| 1237 | unlink(file_name); |
| 1238 | |
| 1239 | return; |
| 1240 | error: |
| 1241 | if (fd > 0) { |
| 1242 | close(fd); |
| 1243 | unlink(file_name); |
| 1244 | } |
| 1245 | fail(); |
| 1246 | } |
| 1247 | |
| 1248 | static void |
| 1249 | test_lyd_print_file_xml(void **state) |
| 1250 | { |
| 1251 | (void) state; /* unused */ |
| 1252 | struct stat sb; |
Radek Krejci | ca73f50 | 2016-09-30 14:48:54 +0200 | [diff] [blame] | 1253 | char file_name[20]; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1254 | char *result; |
| 1255 | FILE *f = NULL; |
| 1256 | int rc; |
| 1257 | int fd; |
| 1258 | |
| 1259 | memset(file_name, 0, sizeof(file_name)); |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 1260 | strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE)); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1261 | |
| 1262 | fd = mkstemp(file_name); |
| 1263 | if (fd < 1) { |
| 1264 | goto error; |
| 1265 | } |
| 1266 | close(fd); |
| 1267 | |
| 1268 | f = (fopen(file_name,"r+")); |
| 1269 | if (f == NULL) { |
| 1270 | goto error; |
| 1271 | } |
| 1272 | |
| 1273 | rc = lyd_print_file(f, root, LYD_XML, 0); |
| 1274 | if (rc) { |
| 1275 | goto error; |
| 1276 | } |
| 1277 | |
| 1278 | fclose(f); |
| 1279 | |
| 1280 | fd = open(file_name, O_RDONLY); |
Radek Krejci | 849ba33 | 2016-03-24 10:23:16 +0100 | [diff] [blame] | 1281 | if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1282 | goto error; |
| 1283 | } |
| 1284 | |
| 1285 | result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1286 | |
| 1287 | assert_string_equal(result_xml, result); |
| 1288 | |
| 1289 | close(fd); |
| 1290 | unlink(file_name); |
| 1291 | |
| 1292 | return; |
| 1293 | error: |
| 1294 | if (f) |
| 1295 | fclose(f); |
| 1296 | if (fd > 0) { |
| 1297 | unlink(file_name); |
| 1298 | close(fd); |
| 1299 | } |
| 1300 | fail(); |
| 1301 | } |
| 1302 | |
| 1303 | static void |
| 1304 | test_lyd_print_file_xml_format(void **state) |
| 1305 | { |
| 1306 | (void) state; /* unused */ |
| 1307 | struct stat sb; |
Radek Krejci | ca73f50 | 2016-09-30 14:48:54 +0200 | [diff] [blame] | 1308 | char file_name[20]; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1309 | char *result; |
| 1310 | FILE *f = NULL; |
| 1311 | int rc; |
| 1312 | int fd; |
| 1313 | |
| 1314 | memset(file_name, 0, sizeof(file_name)); |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 1315 | strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE)); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1316 | |
| 1317 | fd = mkstemp(file_name); |
| 1318 | if (fd < 1) { |
| 1319 | goto error; |
| 1320 | } |
| 1321 | close(fd); |
| 1322 | |
| 1323 | f = (fopen(file_name,"r+")); |
| 1324 | if (f == NULL) { |
| 1325 | goto error; |
| 1326 | } |
| 1327 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1328 | rc = lyd_print_file(f, root, LYD_XML, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1329 | if (rc) { |
| 1330 | goto error; |
| 1331 | } |
| 1332 | |
| 1333 | fclose(f); |
| 1334 | |
| 1335 | fd = open(file_name, O_RDONLY); |
Radek Krejci | 849ba33 | 2016-03-24 10:23:16 +0100 | [diff] [blame] | 1336 | if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1337 | goto error; |
| 1338 | } |
| 1339 | |
| 1340 | result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1341 | |
| 1342 | assert_string_equal(result_xml_format, result); |
| 1343 | |
| 1344 | close(fd); |
| 1345 | unlink(file_name); |
| 1346 | |
| 1347 | return; |
| 1348 | error: |
| 1349 | if (f) |
| 1350 | fclose(f); |
| 1351 | if (fd > 0) { |
| 1352 | unlink(file_name); |
| 1353 | close(fd); |
| 1354 | } |
| 1355 | fail(); |
| 1356 | } |
| 1357 | |
| 1358 | static void |
| 1359 | test_lyd_print_file_json(void **state) |
| 1360 | { |
| 1361 | (void) state; /* unused */ |
| 1362 | struct stat sb; |
Radek Krejci | ca73f50 | 2016-09-30 14:48:54 +0200 | [diff] [blame] | 1363 | char file_name[20]; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1364 | char *result; |
| 1365 | FILE *f = NULL; |
| 1366 | int rc; |
Radek Krejci | 849ba33 | 2016-03-24 10:23:16 +0100 | [diff] [blame] | 1367 | int fd = -1; |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1368 | |
| 1369 | memset(file_name, 0, sizeof(file_name)); |
Denis Volpato Martins | e741628 | 2016-10-05 00:57:32 -0300 | [diff] [blame] | 1370 | strncpy(file_name, TMP_TEMPLATE, strlen(TMP_TEMPLATE)); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1371 | |
| 1372 | fd = mkstemp(file_name); |
| 1373 | if (fd < 1) { |
| 1374 | goto error; |
| 1375 | } |
| 1376 | close(fd); |
| 1377 | |
| 1378 | f = (fopen(file_name,"r+")); |
| 1379 | if (f == NULL) { |
| 1380 | goto error; |
| 1381 | } |
| 1382 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1383 | rc = lyd_print_file(f, root, LYD_JSON, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1384 | if (rc) { |
| 1385 | goto error; |
| 1386 | } |
| 1387 | |
| 1388 | fclose(f); |
| 1389 | |
| 1390 | fd = open(file_name, O_RDONLY); |
Radek Krejci | 849ba33 | 2016-03-24 10:23:16 +0100 | [diff] [blame] | 1391 | if (fd == -1 || fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode)) { |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1392 | goto error; |
| 1393 | } |
| 1394 | |
| 1395 | result = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1396 | |
| 1397 | assert_string_equal(result_json, result); |
| 1398 | |
| 1399 | close(fd); |
| 1400 | unlink(file_name); |
| 1401 | |
| 1402 | return; |
| 1403 | error: |
| 1404 | if (f) |
| 1405 | fclose(f); |
| 1406 | if (fd > 0) { |
| 1407 | unlink(file_name); |
| 1408 | close(fd); |
| 1409 | } |
| 1410 | fail(); |
| 1411 | } |
| 1412 | |
| 1413 | struct buff { |
| 1414 | int len; |
| 1415 | const char *cmp; |
| 1416 | }; |
| 1417 | |
| 1418 | ssize_t custom_lyd_print_clb(void *arg, const void *buf, size_t count) { |
| 1419 | int rc; |
| 1420 | int len; |
| 1421 | struct buff *pos = arg; |
| 1422 | |
| 1423 | len = pos->len + count; |
| 1424 | |
| 1425 | const char *chunk = &pos->cmp[pos->len]; |
| 1426 | |
| 1427 | rc = strncmp(chunk, buf, count); |
| 1428 | if (rc) { |
| 1429 | fail(); |
| 1430 | } |
| 1431 | |
| 1432 | pos->len = len; |
| 1433 | return count; |
| 1434 | } |
| 1435 | |
| 1436 | static void |
| 1437 | test_lyd_print_clb_xml(void **state) |
| 1438 | { |
| 1439 | (void) state; /* unused */ |
| 1440 | int rc; |
| 1441 | |
| 1442 | struct buff *buf = calloc(1, sizeof(struct buff)); |
| 1443 | if (!buf) { |
| 1444 | fail(); |
| 1445 | } |
| 1446 | |
| 1447 | buf->len = 0; |
| 1448 | buf->cmp = result_xml; |
| 1449 | void *arg = buf; |
| 1450 | |
| 1451 | rc = lyd_print_clb(custom_lyd_print_clb, arg, root, LYD_XML, 0); |
| 1452 | if (rc) { |
| 1453 | fail(); |
| 1454 | free(buf); |
| 1455 | } |
| 1456 | |
| 1457 | free(buf); |
| 1458 | } |
| 1459 | |
| 1460 | static void |
| 1461 | test_lyd_print_clb_xml_format(void **state) |
| 1462 | { |
| 1463 | (void) state; /* unused */ |
| 1464 | int rc; |
| 1465 | |
| 1466 | struct buff *buf = calloc(1, sizeof(struct buff)); |
| 1467 | if (!buf) { |
| 1468 | fail(); |
| 1469 | } |
| 1470 | |
| 1471 | buf->len = 0; |
| 1472 | buf->cmp = result_xml_format; |
| 1473 | void *arg = buf; |
| 1474 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1475 | rc = lyd_print_clb(custom_lyd_print_clb, arg, root, LYD_XML, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1476 | if (rc) { |
| 1477 | fail(); |
| 1478 | free(buf); |
| 1479 | } |
| 1480 | |
| 1481 | free(buf); |
| 1482 | } |
| 1483 | |
| 1484 | static void |
| 1485 | test_lyd_print_clb_json(void **state) |
| 1486 | { |
| 1487 | (void) state; /* unused */ |
| 1488 | int rc; |
| 1489 | |
| 1490 | struct buff *buf = calloc(1, sizeof(struct buff)); |
| 1491 | if (!buf) { |
| 1492 | fail(); |
| 1493 | } |
| 1494 | |
| 1495 | buf->len = 0; |
| 1496 | buf->cmp = result_json; |
| 1497 | void *arg = buf; |
| 1498 | |
Michal Vasko | 95068c4 | 2016-03-24 14:58:11 +0100 | [diff] [blame] | 1499 | rc = lyd_print_clb(custom_lyd_print_clb, arg, root, LYD_JSON, LYP_FORMAT); |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1500 | if (rc) { |
| 1501 | fail(); |
| 1502 | free(buf); |
| 1503 | } |
| 1504 | |
| 1505 | free(buf); |
| 1506 | } |
| 1507 | |
Radek Krejci | 6d53828 | 2016-05-05 14:24:12 +0200 | [diff] [blame] | 1508 | static void |
| 1509 | test_lyd_path(void **state) |
| 1510 | { |
| 1511 | (void) state; /* unused */ |
| 1512 | char *str; |
| 1513 | |
| 1514 | str = lyd_path(root); |
| 1515 | assert_ptr_not_equal(str, NULL); |
| 1516 | assert_string_equal(str, "/a:x"); |
| 1517 | free(str); |
| 1518 | |
| 1519 | str = lyd_path(root->child); |
| 1520 | assert_ptr_not_equal(str, NULL); |
| 1521 | assert_string_equal(str, "/a:x/bubba"); |
| 1522 | free(str); |
| 1523 | } |
| 1524 | |
Michal Vasko | 20399fb | 2017-01-09 11:11:07 +0100 | [diff] [blame] | 1525 | static void |
| 1526 | test_lyd_leaf_type(void **state) |
| 1527 | { |
| 1528 | struct ly_ctx *ctx = (struct ly_ctx *)*state; |
| 1529 | const char *yang = "module x {" |
| 1530 | " namespace urn:x;" |
| 1531 | " prefix x;" |
| 1532 | " yang-version 1.1;" |
| 1533 | " container x {" |
| 1534 | " leaf str { type string; }" |
| 1535 | " leaf e { type enumeration { enum ftp; enum http; } }" |
| 1536 | " leaf u { type union {" |
| 1537 | " type leafref { path \"../e\"; }" |
| 1538 | " type leafref { path \"../str\";}" |
| 1539 | " } }" |
| 1540 | "} }"; |
| 1541 | const char *xml1 = "<x xmlns=\"urn:x\"><str>http</str><e>ftp</e><u>http</u></x>"; |
| 1542 | const char *xml2 = "<x xmlns=\"urn:x\"><str>http</str><e>ftp</e><u>ftp</u></x>"; |
| 1543 | struct lyd_node *data; |
| 1544 | |
| 1545 | assert_ptr_not_equal(lys_parse_mem(ctx, yang, LYS_IN_YANG), 0); |
| 1546 | |
| 1547 | data = lyd_parse_mem(ctx, xml1, LYD_XML, LYD_OPT_CONFIG); |
| 1548 | assert_ptr_not_equal(data, NULL); |
| 1549 | assert_int_equal(lyd_leaf_type((struct lyd_node_leaf_list *)data->child->prev)->base, LY_TYPE_STRING); |
| 1550 | lyd_free_withsiblings(data); |
| 1551 | |
| 1552 | data = lyd_parse_mem(ctx, xml2, LYD_XML, LYD_OPT_CONFIG); |
| 1553 | assert_ptr_not_equal(data, NULL); |
| 1554 | assert_int_equal(lyd_leaf_type((struct lyd_node_leaf_list *)data->child->prev)->base, LY_TYPE_ENUM); |
| 1555 | lyd_free_withsiblings(data); |
| 1556 | |
| 1557 | data = lyd_parse_mem(ctx, xml2, LYD_XML, LYD_OPT_CONFIG); |
| 1558 | assert_ptr_not_equal(data, NULL); |
| 1559 | lyd_change_leaf((struct lyd_node_leaf_list *)data->child->prev, "ssh"); |
| 1560 | assert_int_equal(lyd_leaf_type((struct lyd_node_leaf_list *)data->child->prev), NULL); |
| 1561 | lyd_free_withsiblings(data); |
| 1562 | } |
| 1563 | |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1564 | int main(void) |
| 1565 | { |
| 1566 | const struct CMUnitTest tests[] = { |
| 1567 | cmocka_unit_test(test_lyd_parse_mem), |
| 1568 | cmocka_unit_test(test_lyd_parse_fd), |
| 1569 | cmocka_unit_test(test_lyd_parse_path), |
| 1570 | cmocka_unit_test(test_lyd_parse_xml), |
| 1571 | cmocka_unit_test_setup_teardown(test_lyd_new, setup_f, teardown_f), |
| 1572 | cmocka_unit_test_setup_teardown(test_lyd_new_leaf, setup_f, teardown_f), |
| 1573 | cmocka_unit_test_setup_teardown(test_lyd_change_leaf, setup_f, teardown_f), |
| 1574 | cmocka_unit_test_setup_teardown(test_lyd_output_new_leaf, setup_f, teardown_f), |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 1575 | cmocka_unit_test_setup_teardown(test_lyd_new_path, setup_f, teardown_f), |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1576 | cmocka_unit_test_setup_teardown(test_lyd_dup, setup_f, teardown_f), |
| 1577 | cmocka_unit_test_setup_teardown(test_lyd_insert, setup_f, teardown_f), |
Radek Krejci | 434780f | 2016-09-16 15:28:27 +0200 | [diff] [blame] | 1578 | cmocka_unit_test_setup_teardown(test_lyd_insert_sibling, setup_f, teardown_f), |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1579 | cmocka_unit_test_setup_teardown(test_lyd_insert_before, setup_f, teardown_f), |
| 1580 | cmocka_unit_test_setup_teardown(test_lyd_insert_after, setup_f, teardown_f), |
Michal Vasko | 8681b66 | 2016-03-24 13:26:36 +0100 | [diff] [blame] | 1581 | cmocka_unit_test_setup_teardown(test_lyd_schema_sort, setup_f, teardown_f), |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1582 | cmocka_unit_test_setup_teardown(test_lyd_find_path, setup_f, teardown_f), |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 1583 | cmocka_unit_test_setup_teardown(test_lyd_find_instance, setup_f, teardown_f), |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1584 | cmocka_unit_test_setup_teardown(test_lyd_validate, setup_f, teardown_f), |
| 1585 | cmocka_unit_test_setup_teardown(test_lyd_unlink, setup_f, teardown_f), |
| 1586 | cmocka_unit_test_setup_teardown(test_lyd_free, setup_f, teardown_f), |
| 1587 | cmocka_unit_test_setup_teardown(test_lyd_free_withsiblings, setup_f, teardown_f), |
| 1588 | cmocka_unit_test_setup_teardown(test_lyd_insert_attr, setup_f, teardown_f), |
| 1589 | cmocka_unit_test_setup_teardown(test_lyd_free_attr, setup_f, teardown_f), |
| 1590 | cmocka_unit_test_setup_teardown(test_lyd_print_mem_xml, setup_f, teardown_f), |
| 1591 | cmocka_unit_test_setup_teardown(test_lyd_print_mem_xml_format, setup_f, teardown_f), |
| 1592 | cmocka_unit_test_setup_teardown(test_lyd_print_mem_json, setup_f, teardown_f), |
| 1593 | cmocka_unit_test_setup_teardown(test_lyd_print_fd_xml, setup_f, teardown_f), |
| 1594 | cmocka_unit_test_setup_teardown(test_lyd_print_fd_xml_format, setup_f, teardown_f), |
| 1595 | cmocka_unit_test_setup_teardown(test_lyd_print_fd_json, setup_f, teardown_f), |
| 1596 | cmocka_unit_test_setup_teardown(test_lyd_print_file_xml, setup_f, teardown_f), |
| 1597 | cmocka_unit_test_setup_teardown(test_lyd_print_file_xml_format, setup_f, teardown_f), |
| 1598 | cmocka_unit_test_setup_teardown(test_lyd_print_file_json, setup_f, teardown_f), |
| 1599 | cmocka_unit_test_setup_teardown(test_lyd_print_clb_xml, setup_f, teardown_f), |
| 1600 | cmocka_unit_test_setup_teardown(test_lyd_print_clb_xml_format, setup_f, teardown_f), |
| 1601 | cmocka_unit_test_setup_teardown(test_lyd_print_clb_json, setup_f, teardown_f), |
Radek Krejci | 6d53828 | 2016-05-05 14:24:12 +0200 | [diff] [blame] | 1602 | cmocka_unit_test_setup_teardown(test_lyd_path, setup_f, teardown_f), |
Michal Vasko | 20399fb | 2017-01-09 11:11:07 +0100 | [diff] [blame] | 1603 | cmocka_unit_test_setup_teardown(test_lyd_leaf_type, setup_f2, teardown_f2), |
Mislav Novakovic | 1f160ab | 2016-03-23 12:46:48 +0100 | [diff] [blame] | 1604 | }; |
| 1605 | |
| 1606 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1607 | } |