blob: 0349157f47fa3c0179a7efe1a24a9e0edab792dd [file] [log] [blame]
Radek Iša56ca9e42020-09-08 18:42:00 +02001/**
2 * @file test_yanglib.c
3 * @author: Michal Vasko <mvasko@cesnet.cz>
4 * @brief unit tests for ietf-yang-library 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 */
14#define _UTEST_MAIN_
15#include "utests.h"
16
17#include <string.h>
18
19#include "context.h"
20#include "in.h"
21#include "log.h"
22#include "set.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020023#include "tests_config.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020024#include "tree_data.h"
25#include "tree_schema.h"
26
27const char *schema_a =
28 "module a {\n"
29 " namespace urn:tests:a;\n"
30 " prefix a;\n"
31 " yang-version 1.1;\n"
32 "\n"
33 " include a_sub;\n"
34 "\n"
35 " list l2 {\n"
36 " key \"a\";\n"
37 " leaf a {\n"
38 " type uint16;\n"
39 " }\n"
40 " leaf b {\n"
41 " type uint16;\n"
42 " }\n"
43 " }\n"
44 "}";
45const char *schema_b =
46 "module b {\n"
47 " namespace urn:tests:b;\n"
48 " prefix b;\n"
49 " yang-version 1.1;\n"
50 "\n"
51 " import a {\n"
52 " prefix a;\n"
53 " }\n"
54 "\n"
55 " deviation /a:l2 {\n"
56 " deviate add {\n"
57 " max-elements 40;\n"
58 " }\n"
59 " }\n"
60 "\n"
61 " leaf foo {\n"
62 " type string;\n"
63 " }\n"
64 "}";
65
66static LY_ERR
67test_imp_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, void *user_data,
68 LYS_INFORMAT *format, const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
69{
70 const char *schema_a_sub =
71 "submodule a_sub {\n"
72 " belongs-to a {\n"
73 " prefix a;\n"
74 " }\n"
75 " yang-version 1.1;\n"
76 "\n"
77 " feature feat1;\n"
78 "\n"
79 " list l3 {\n"
80 " key \"a\";\n"
81 " leaf a {\n"
82 " type uint16;\n"
83 " }\n"
84 " leaf b {\n"
85 " type uint16;\n"
86 " }\n"
87 " }\n"
88 "}\n";
89
90 assert_string_equal(mod_name, "a");
91 assert_null(mod_rev);
92 if (!submod_name) {
93 return LY_ENOTFOUND;
94 }
95 assert_string_equal(submod_name, "a_sub");
96 assert_null(sub_rev);
97 assert_null(user_data);
98
99 *format = LYS_IN_YANG;
100 *module_data = schema_a_sub;
101 *free_module_data = NULL;
102 return LY_SUCCESS;
103}
104
105static void
106test_yanglib(void **state)
107{
108 const char *feats[] = {"feat1", NULL};
109 struct lyd_node *tree;
110 struct ly_set *set;
111 LY_ERR ret;
112
113 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, NULL);
114 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
115 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
116
Michal Vasko794ab4b2021-03-31 09:42:19 +0200117 assert_int_equal(LY_SUCCESS, ly_ctx_get_yanglib_data(UTEST_LYCTX, &tree, "<<%u>>", ly_ctx_get_change_count(UTEST_LYCTX)));
118 lyd_free_all(tree);
119 assert_int_equal(LY_SUCCESS, ly_ctx_get_yanglib_data(UTEST_LYCTX, &tree, "%u", -10));
120 lyd_free_all(tree);
121 assert_int_equal(LY_SUCCESS, ly_ctx_get_yanglib_data(UTEST_LYCTX, &tree, ""));
122 lyd_free_all(tree);
123 assert_int_equal(LY_SUCCESS, ly_ctx_get_yanglib_data(UTEST_LYCTX, &tree, "%u", ly_ctx_get_change_count(UTEST_LYCTX)));
Radek Iša56ca9e42020-09-08 18:42:00 +0200124
125 /* make sure there is "a" with a submodule and deviation */
126 ret = lyd_find_xpath(tree, "/ietf-yang-library:yang-library/module-set/module[name='a'][submodule/name='a_sub']"
127 "[feature='feat1'][deviation='b']", &set);
128 assert_int_equal(ret, LY_SUCCESS);
129
130 assert_int_equal(set->count, 1);
131 ly_set_free(set, NULL);
132
133 lyd_free_all(tree);
134}
135
136int
137main(void)
138{
139 const struct CMUnitTest tests[] = {
140 UTEST(test_yanglib),
141 };
142
143 return cmocka_run_group_tests(tests, NULL, NULL);
144}