blob: 662fd9b49beb2babb81f4eaa93ab86d42c5cf1fb [file] [log] [blame]
Radek Krejci5902be92021-03-25 21:25:14 +01001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_plugins.c
Radek Krejci5902be92021-03-25 21:25:14 +01003 * @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));
Radek Krejci5902be92021-03-25 21:25:14 +010039}
40
41static void
42test_add_simple(void **state)
43{
44 const struct lys_module *mod;
45 struct lysc_node_leaf *leaf;
46 struct lyplg_ext *plugin_e;
47 struct lyplg_type *plugin_t;
48
49 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
50
51 UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);
52
53 leaf = (struct lysc_node_leaf *)mod->compiled->data;
54 assert_int_equal(LYS_LEAF, leaf->nodetype);
55
56 assert_non_null(plugin_t = lyplg_find(LYPLG_TYPE, "libyang-plugins-simple", NULL, "note"));
57 assert_string_equal("libyang 2 - simple test, version 1", plugin_t->id);
58 assert_ptr_equal(leaf->type->plugin, plugin_t);
59
60 assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts));
61 assert_non_null(plugin_e = lyplg_find(LYPLG_EXTENSION, "libyang-plugins-simple", NULL, "hint"));
62 assert_string_equal("libyang 2 - simple test, version 1", plugin_e->id);
63 assert_ptr_equal(leaf->exts[0].def->plugin, plugin_e);
64
65 /* the second loading of the same plugin - still success */
66 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
67}
68
Radek Krejci4f2e3e52021-03-30 14:20:28 +020069static void
70test_validation(void **state)
71{
72 const struct lys_module *mod;
73 struct lyd_node *tree;
74 const char *data;
75 const char *schema = "module libyang-plugins-validate {"
76 " namespace urn:libyang:tests:plugins:validate;"
77 " prefix v;"
78 " extension extra-validation;"
79 " typedef note { type string { v:extra-validation;}}"
80 " leaf test1 {"
81 " type v:note;"
82 " }"
83 " leaf test2 {"
84 " type string;"
85 " v:extra-validation;"
86 " }"
87 " leaf test3 {"
88 " type string {v:extra-validation;}"
89 " }"
90 " leaf test4 {"
91 " type string;"
92 " }"
93 "}";
94
95 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_validate" LYPLG_SUFFIX));
96
97 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
98
99 /* test1 - extra-validation done based on typedef's extension */
100 data = "<test1 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test1>";
101 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
102 CHECK_LOG_CTX("Extension plugin \"libyang 2 - validation test, version 1\": extra validation callback invoked on test1", NULL);
103 lyd_free_all(tree);
104
105 /* test2 - extra-validation done based on node's extension */
106 data = "<test2 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test2>";
107 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
108 CHECK_LOG_CTX("Extension plugin \"libyang 2 - validation test, version 1\": extra validation callback invoked on test2", NULL);
109 lyd_free_all(tree);
110
111 /* test3 - extra-validation done based on node type's extension */
112 data = "<test3 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test3>";
113 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
114 CHECK_LOG_CTX("Extension plugin \"libyang 2 - validation test, version 1\": extra validation callback invoked on test3", NULL);
115 lyd_free_all(tree);
116
117 /* test4 - extra-validation not done */
118 data = "<test4 xmlns=\"urn:libyang:tests:plugins:validate\">xxx</test4>";
119 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
120 CHECK_LOG_CTX(NULL, NULL);
121 lyd_free_all(tree);
122}
123
Radek Krejci4a4d1e02021-04-09 14:04:40 +0200124static void
125test_not_implemented(void **state)
126{
127 const struct lys_module *mod;
128 struct lyd_node *tree;
129 const char *schema = "module libyang-plugins-unknown {"
130 " namespace urn:libyang:tests:plugins:unknown;"
131 " prefix u;"
132 " extension myext;"
133 " typedef mytype { type string;}"
134 " leaf test {"
135 " u:myext;"
136 " type mytype;"
137 " }"
138 "}";
139 const char *data = "<test xmlns=\"urn:libyang:tests:plugins:unknown\">xxx</test>";
140 char *printed = NULL;
141
142 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
143
144 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0));
145 free(printed);
146
147 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
148 CHECK_LOG_CTX(NULL, NULL);
149
150 lyd_free_all(tree);
151}
152
Radek Krejci5902be92021-03-25 21:25:14 +0100153int
154main(void)
155{
156 const struct CMUnitTest tests[] = {
157 UTEST(test_add_invalid),
158 UTEST(test_add_simple),
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200159 UTEST(test_validation),
Radek Krejci4a4d1e02021-04-09 14:04:40 +0200160 UTEST(test_not_implemented),
Radek Krejci5902be92021-03-25 21:25:14 +0100161 };
162
163 return cmocka_run_group_tests(tests, NULL, NULL);
164}