File: test_plugins.c

package info (click to toggle)
libyang 4.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 10,788 kB
  • sloc: ansic: 127,487; xml: 671; sh: 442; tcl: 318; makefile: 19
file content (236 lines) | stat: -rw-r--r-- 6,925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * @file test_plugins.c
 * @author: Radek Krejci <rkrejci@cesnet.cz>
 * @brief unit tests for functions from set.c
 *
 * Copyright (c) 2018 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */
#define _UTEST_MAIN_
#include "utests.h"

#include <stdlib.h>
#include <string.h>

#include "ly_config.h"
#include "plugins.h"
#include "plugins_internal.h"

const char *simple = "module libyang-plugins-simple {"
        "  namespace urn:libyang:tests:plugins:simple;"
        "  prefix s;"
        "  typedef note { type string; }"
        "  extension hint { argument value; }"
        "  extension rt;"
        "  leaf test {"
        "    type s:note {length 255;}"
        "    s:hint \"some hint here\";"
        "  }"
        "  leaf enum1 {"
        "    type enumeration {"
        "      enum val1 {s:rt;}"
        "      enum val2 {s:rt;}"
        "    }"
        "  }"
        "  leaf u1 {"
        "    type union {"
        "      type string {s:rt;}"
        "      type int8 {s:rt;}"
        "    }"
        "  }"
        "  grouping grp1 {"
        "    list l1 {key v; leaf v {type string;} leaf k {type string;}}"
        "    list l2 {key v; leaf v {type string;} leaf k {type string;}}"
        "    typedef t1 {"
        "      type string {s:rt;}"
        "      s:rt;"
        "    }"
        "    typedef t2 {"
        "      type string {s:rt;}"
        "      s:rt;"
        "    }"
        "  }"
        "  uses grp1 {"
        "    refine l1 {"
        "      s:rt;"
        "    }"
        "    refine l2 {"
        "      s:rt;"
        "    }"
        "  }"
        "}";

static void
test_add_invalid(void **state)
{
    (void)state;
    assert_int_equal(LY_ESYS, lyplg_add(TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX));
}

static void
test_add_simple(void **state)
{
    struct lys_module *mod;
    struct lysc_node_leaf *leaf;
    struct lyplg_ext *plugin_e;
    struct lyplg_type *plugin_t;

    assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));

    UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);

    leaf = (struct lysc_node_leaf *)mod->compiled->data;
    assert_int_equal(LYS_LEAF, leaf->nodetype);

    assert_non_null(plugin_t = lysc_get_type_plugin(lyplg_type_plugin_find(NULL, "libyang-plugins-simple", NULL, "note")));
    assert_string_equal("ly2 simple test v1", plugin_t->id);
    assert_ptr_equal(leaf->type->plugin_ref, plugin_t);

    assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts));
    assert_non_null(plugin_e = lysc_get_ext_plugin(lyplg_ext_plugin_find(NULL, "libyang-plugins-simple", NULL, "hint")));
    assert_string_equal("ly2 simple test v1", plugin_e->id);
    assert_ptr_equal(leaf->exts[0].def->plugin_ref, plugin_e);

    /* the second loading of the same plugin - still success */
    assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
}

static void
test_not_implemented(void **state)
{
    struct lys_module *mod;
    struct lyd_node *tree;
    const char *schema = "module libyang-plugins-unknown {"
            "  namespace urn:libyang:tests:plugins:unknown;"
            "  prefix u;"
            "  extension myext;"
            "  typedef mytype { type string;}"
            "  leaf test {"
            "    u:myext;"
            "    type mytype;"
            "  }"
            "}";
    const char *data = "<test xmlns=\"urn:libyang:tests:plugins:unknown\">xxx</test>";
    char *printed = NULL;

    UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);

    assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0));
    free(printed);

    assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
    CHECK_LOG_CTX(NULL, NULL, 0);

    lyd_free_all(tree);
}

static LY_ERR
parse_clb(struct lysp_ctx *UNUSED(pctx), struct lysp_ext_instance *ext)
{
    struct lysp_node_leaf *leaf;

    leaf = (struct lysp_node_leaf *)ext->parent;
    leaf->flags |= LYS_STATUS_DEPRC;
    return LY_SUCCESS;
}

static LY_ERR
parse_clb2(struct lysp_ctx *UNUSED(pctx), struct lysp_ext_instance *ext)
{
    struct lysp_refine *refine;
    struct lysp_tpdf *tpdf;
    struct lysp_type_enum *en;
    struct lysp_type *type;
    LY_ARRAY_COUNT_TYPE count = 0;

    if (ext->parent_stmt == LY_STMT_REFINE) {
        refine = (struct lysp_refine *)ext->parent;
        count = LY_ARRAY_COUNT(refine->exts);
    } else if (ext->parent_stmt == LY_STMT_TYPEDEF) {
        tpdf = (struct lysp_tpdf *)ext->parent;
        count = LY_ARRAY_COUNT(tpdf->exts);
    } else if (ext->parent_stmt == LY_STMT_ENUM) {
        en = (struct lysp_type_enum *)ext->parent;
        count = LY_ARRAY_COUNT(en->exts);
    } else if (ext->parent_stmt == LY_STMT_TYPE) {
        type = (struct lysp_type *)ext->parent;
        count = LY_ARRAY_COUNT(type->exts);
    } else {
        return LY_SUCCESS;
    }

    if (count != 1) {
        return LY_EINVAL;
    }
    return LY_SUCCESS;
}

struct lyplg_ext_record memory_recs[] = {
    {
        .module = "libyang-plugins-simple",
        .revision = NULL,
        .name = "hint",

        .plugin.id = "memory-plugin-v1",
        .plugin.parse = parse_clb,
        .plugin.compile = NULL,
        .plugin.printer_info = NULL,
        .plugin.printer_ctree = NULL,
        .plugin.printer_ptree = NULL,
        .plugin.node_xpath = NULL,
        .plugin.snode = NULL,
        .plugin.validate = NULL,
        .plugin.pfree = NULL,
        .plugin.cfree = NULL
    },
    {
        .module = "libyang-plugins-simple",
        .revision = NULL,
        .name = "rt",

        .plugin.id = "memory-plugin-v1",
        .plugin.parse = parse_clb2,
        .plugin.compile = NULL,
        .plugin.printer_info = NULL,
        .plugin.printer_ctree = NULL,
        .plugin.printer_ptree = NULL,
        .plugin.node_xpath = NULL,
        .plugin.snode = NULL,
        .plugin.validate = NULL,
        .plugin.pfree = NULL,
        .plugin.cfree = NULL
    },
    {0} /* terminating zeroed item */
};

static void
test_simple_from_memory(void **state)
{
    struct lys_module *mod;
    struct lysc_node_leaf *leaf;

    lyplg_add_extension_plugin(UTEST_LYCTX, LYPLG_EXT_API_VERSION, memory_recs);
    UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);

    leaf = (struct lysc_node_leaf *)mod->compiled->data;
    assert_int_equal(LYS_LEAF, leaf->nodetype);
    assert_true(leaf->flags & LYS_STATUS_DEPRC);
}

int
main(void)
{
    const struct CMUnitTest tests[] = {
        UTEST(test_add_invalid),
        UTEST(test_add_simple),
        UTEST(test_not_implemented),
        UTEST(test_simple_from_memory),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}