|
| 1 | +/** |
| 2 | + * @file test_yangtypes_xpath.c |
| 3 | + * @author Michal Vasko <[email protected]> |
| 4 | + * @brief Cmocka tests for resolving ietf-yang-types type xpath1.0. |
| 5 | + * |
| 6 | + * Copyright (c) 2017 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 | + */ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <setjmp.h> |
| 18 | +#include <cmocka.h> |
| 19 | + |
| 20 | +#include "tests/config.h" |
| 21 | +#include "libyang.h" |
| 22 | + |
| 23 | +struct state { |
| 24 | + struct ly_ctx *ctx; |
| 25 | + struct lyd_node *dt; |
| 26 | + char *xml; |
| 27 | +}; |
| 28 | + |
| 29 | +static int |
| 30 | +setup_f(void **state) |
| 31 | +{ |
| 32 | + struct state *st; |
| 33 | + |
| 34 | + (*state) = st = calloc(1, sizeof *st); |
| 35 | + if (!st) { |
| 36 | + fprintf(stderr, "Memory allocation error"); |
| 37 | + return -1; |
| 38 | + } |
| 39 | + |
| 40 | + /* libyang context */ |
| 41 | + st->ctx = ly_ctx_new(NULL, 0); |
| 42 | + if (!st->ctx) { |
| 43 | + fprintf(stderr, "Failed to create context.\n"); |
| 44 | + goto error; |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | + |
| 49 | +error: |
| 50 | + ly_ctx_destroy(st->ctx, NULL); |
| 51 | + free(st); |
| 52 | + (*state) = NULL; |
| 53 | + |
| 54 | + return -1; |
| 55 | +} |
| 56 | + |
| 57 | +static int |
| 58 | +teardown_f(void **state) |
| 59 | +{ |
| 60 | + struct state *st = (*state); |
| 61 | + |
| 62 | + lyd_free_withsiblings(st->dt); |
| 63 | + ly_ctx_destroy(st->ctx, NULL); |
| 64 | + free(st->xml); |
| 65 | + free(st); |
| 66 | + (*state) = NULL; |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +static void |
| 72 | +test_acm_yangtypes_xpath(void **state) |
| 73 | +{ |
| 74 | + struct state *st = (struct state *)*state; |
| 75 | + |
| 76 | + /* schema */ |
| 77 | + assert_ptr_not_equal(lys_parse_path(st->ctx, TESTS_DIR"/schema/yang/ietf/ietf-netconf-acm.yang", LYS_IN_YANG), NULL); |
| 78 | + assert_ptr_not_equal(lys_parse_path(st->ctx, TESTS_DIR"/data/files/all.yang", LYS_IN_YANG), NULL); |
| 79 | + |
| 80 | + /* data */ |
| 81 | + st->dt = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/nacm.xml", LYD_XML, LYD_OPT_CONFIG /*DATA_NO_YANGLIB*/); |
| 82 | + assert_ptr_not_equal(st->dt, NULL); |
| 83 | + |
| 84 | + assert_string_equal(((struct lyd_node_leaf_list *)st->dt->child->child->next->child->next)->value_str, "/all:cont1/leaf3"); |
| 85 | + |
| 86 | + lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0); |
| 87 | + assert_string_equal(st->xml, "<nacm xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-acm\"><rule-list><name>test-list</name><rule><name>test-rule</name><path xmlns:all_mod=\"urn:all\">/all_mod:cont1/all_mod:leaf3</path><action>deny</action></rule></rule-list></nacm>"); |
| 88 | +} |
| 89 | + |
| 90 | +int main(void) |
| 91 | +{ |
| 92 | + const struct CMUnitTest tests[] = { |
| 93 | + cmocka_unit_test_setup_teardown(test_acm_yangtypes_xpath, setup_f, teardown_f), |
| 94 | + }; |
| 95 | + |
| 96 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 97 | +} |
0 commit comments