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