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