blob: ebb835ce4b5344c38bc33eea3f798e2031a6b443 [file] [log] [blame]
Radek Krejci5902be92021-03-25 21:25:14 +01001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from set.c
5 *
6 * Copyright (c) 2018 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 <stdlib.h>
18#include <string.h>
19
20#include "config.h"
21#include "plugins.h"
22#include "plugins_internal.h"
23
24const char *simple = "module libyang-plugins-simple {"
25 " namespace urn:libyang:tests:plugins:simple;"
26 " prefix s;"
27 " typedef note { type string; }"
28 " extension hint { argument value; }"
29 " leaf test {"
30 " type s:note {length 255;}"
31 " s:hint \"some hint here\";"
32 " }"
33 "}";
34
35static void
36test_add_invalid(void **state)
37{
38 assert_int_equal(LY_ESYS, lyplg_add(TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX));
39
40#ifdef __APPLE__
41 CHECK_LOG("Loading \""TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX "\" as a plugin failed "
42 "(dlopen("TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX ", 2): image not found).", NULL);
43#else
44 CHECK_LOG("Loading \""TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX "\" as a plugin failed "
45 "("TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX ": cannot open shared object file: "
46 "No such file or directory).", NULL);
47#endif
48
49 assert_int_equal(LY_EINVAL, lyplg_add(TESTS_BIN "/plugins/plugin_invalid" LYPLG_SUFFIX));
50#ifndef __APPLE__
51 /* OS X prints address of the symbol being searched and cmocka doesn't support wildcards in string checking assert */
52 CHECK_LOG("Processing user type plugin \""TESTS_BIN "/plugins/plugin_invalid"LYPLG_SUFFIX "\" failed, "
53 "missing type plugins information ("TESTS_BIN "/plugins/plugin_invalid"LYPLG_SUFFIX ": "
54 "undefined symbol: plugins_types__).", NULL);
55#endif
56}
57
58static void
59test_add_simple(void **state)
60{
61 const struct lys_module *mod;
62 struct lysc_node_leaf *leaf;
63 struct lyplg_ext *plugin_e;
64 struct lyplg_type *plugin_t;
65
66 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
67
68 UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);
69
70 leaf = (struct lysc_node_leaf *)mod->compiled->data;
71 assert_int_equal(LYS_LEAF, leaf->nodetype);
72
73 assert_non_null(plugin_t = lyplg_find(LYPLG_TYPE, "libyang-plugins-simple", NULL, "note"));
74 assert_string_equal("libyang 2 - simple test, version 1", plugin_t->id);
75 assert_ptr_equal(leaf->type->plugin, plugin_t);
76
77 assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts));
78 assert_non_null(plugin_e = lyplg_find(LYPLG_EXTENSION, "libyang-plugins-simple", NULL, "hint"));
79 assert_string_equal("libyang 2 - simple test, version 1", plugin_e->id);
80 assert_ptr_equal(leaf->exts[0].def->plugin, plugin_e);
81
82 /* the second loading of the same plugin - still success */
83 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
84}
85
Radek Krejci4f2e3e52021-03-30 14:20:28 +020086static void
87test_validation(void **state)
88{
89 const struct lys_module *mod;
90 struct lyd_node *tree;
91 const char *data;
92 const char *schema = "module libyang-plugins-validate {"
93 " namespace urn:libyang:tests:plugins:validate;"
94 " prefix v;"
95 " extension extra-validation;"
96 " typedef note { type string { v:extra-validation;}}"
97 " leaf test1 {"
98 " type v:note;"
99 " }"
100 " leaf test2 {"
101 " type string;"
102 " v:extra-validation;"
103 " }"
104 " leaf test3 {"
105 " type string {v:extra-validation;}"
106 " }"
107 " leaf test4 {"
108 " type string;"
109 " }"
110 "}";
111
112 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_validate" LYPLG_SUFFIX));
113
114 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
115
116 /* test1 - extra-validation done based on typedef's extension */
117 data = "<test1 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test1>";
118 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
119 CHECK_LOG_CTX("Extension plugin \"libyang 2 - validation test, version 1\": extra validation callback invoked on test1", NULL);
120 lyd_free_all(tree);
121
122 /* test2 - extra-validation done based on node's extension */
123 data = "<test2 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test2>";
124 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
125 CHECK_LOG_CTX("Extension plugin \"libyang 2 - validation test, version 1\": extra validation callback invoked on test2", NULL);
126 lyd_free_all(tree);
127
128 /* test3 - extra-validation done based on node type's extension */
129 data = "<test3 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test3>";
130 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
131 CHECK_LOG_CTX("Extension plugin \"libyang 2 - validation test, version 1\": extra validation callback invoked on test3", NULL);
132 lyd_free_all(tree);
133
134 /* test4 - extra-validation not done */
135 data = "<test4 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test4>";
136 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
137 CHECK_LOG_CTX(NULL, NULL);
138 lyd_free_all(tree);
139}
140
Radek Krejci4a4d1e02021-04-09 14:04:40 +0200141static void
142test_not_implemented(void **state)
143{
144 const struct lys_module *mod;
145 struct lyd_node *tree;
146 const char *schema = "module libyang-plugins-unknown {"
147 " namespace urn:libyang:tests:plugins:unknown;"
148 " prefix u;"
149 " extension myext;"
150 " typedef mytype { type string;}"
151 " leaf test {"
152 " u:myext;"
153 " type mytype;"
154 " }"
155 "}";
156 const char *data = "<test xmlns=\"urn:libyang:tests:plugins:unknown\">xxx</test>";
157 char *printed = NULL;
158
159 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
160
161 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0));
162 free(printed);
163
164 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
165 CHECK_LOG_CTX(NULL, NULL);
166
167 lyd_free_all(tree);
168}
169
Radek Krejci5902be92021-03-25 21:25:14 +0100170int
171main(void)
172{
173 const struct CMUnitTest tests[] = {
174 UTEST(test_add_invalid),
175 UTEST(test_add_simple),
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200176 UTEST(test_validation),
Radek Krejci4a4d1e02021-04-09 14:04:40 +0200177 UTEST(test_not_implemented),
Radek Krejci5902be92021-03-25 21:25:14 +0100178 };
179
180 return cmocka_run_group_tests(tests, NULL, NULL);
181}