summaryrefslogtreecommitdiffstats
path: root/tests/utests/extensions/test_metadata.c
blob: f1edd2946746c668ddc03f9136ed0a751e132022 (plain)
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
/**
 * @file test_metadata.c
 * @author Radek Krejci <rkrejci@cesnet.cz>
 * @brief unit tests for Metadata extension (annotation) support
 *
 * Copyright (c) 2019 - 2022 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 "libyang.h"
#include "plugins_exts.h"
#include "plugins_exts/metadata.h"

static void
test_yang(void **state)
{
    struct lys_module *mod;
    struct lysc_ext_instance *e;
    const char *units;

    const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:metadata:a; prefix a;"
            "import ietf-yang-metadata {prefix md;}"
            "feature f;"
            "md:annotation x {"
            "  description \"test\";"
            "  if-feature f;"
            "  reference \"test\";"
            "  status \"current\";"
            "  type uint8;"
            "  units meters;"
            "}}";
    const char *feats[] = {"f", NULL};

    UTEST_ADD_MODULE(data, LYS_IN_YANG, feats, &mod);
    assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
    e = &mod->compiled->exts[0];
    assert_non_null(e->compiled);
    assert_non_null(e->substmts);
    lyplg_ext_get_storage(e, LY_STMT_UNITS, sizeof units, (const void **)&units);
    assert_string_equal("meters", units);

    /* invalid */
    /* missing mandatory type substatement */
    data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
            "import ietf-yang-metadata {prefix md;}"
            "md:annotation aa;}";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
    CHECK_LOG_CTX("Ext plugin \"ly2 metadata v1\": Missing mandatory keyword \"type\" as a child of \"md:annotation aa\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* not allowed substatement */
    data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
            "import ietf-yang-metadata {prefix md;}"
            "md:annotation aa {default x;}}";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
    CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance.",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* invalid cardinality of units substatement */
    data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
            "import ietf-yang-metadata {prefix md;}"
            "md:annotation aa {type string; units x; units y;}}";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
    CHECK_LOG_CTX("Duplicate keyword \"units\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* invalid cardinality of status substatement */
    data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
            "import ietf-yang-metadata {prefix md;}"
            "md:annotation aa {type string; status current; status obsolete;}}";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
    CHECK_LOG_CTX("Duplicate keyword \"status\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* invalid cardinality of status substatement */
    data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
            "import ietf-yang-metadata {prefix md;}"
            "md:annotation aa {type string; type uint8;}}";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
    CHECK_LOG_CTX("Duplicate keyword \"type\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* duplication of the same annotation */
    data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;"
            "import ietf-yang-metadata {prefix md;}"
            "md:annotation aa {type string;} md:annotation aa {type uint8;}}";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
    CHECK_LOG_CTX("Ext plugin \"ly2 metadata v1\": Extension md:annotation is instantiated multiple times.",
            "Path \"/aa:{extension='md:annotation'}/aa\".");
}

static void
test_yin(void **state)
{
    struct lys_module *mod;
    struct lysc_ext_instance *e;
    const char *data, *units;

    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"a\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<feature name=\"f\"/>\n"
            "<md:annotation name=\"x\">\n"
            "  <description><text>test</text></description>\n"
            "  <reference><text>test</text></reference>\n"
            "  <if-feature name=\"f\"/>\n"
            "  <status value=\"current\"/>\n"
            "  <type name=\"uint8\"/>\n"
            "  <units name=\"meters\"/>\n"
            "</md:annotation></module>";
    const char *feats[] = {"f", NULL};

    UTEST_ADD_MODULE(data, LYS_IN_YIN, feats, &mod);
    assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts));
    e = &mod->compiled->exts[0];
    assert_non_null(e->compiled);
    assert_non_null(e->substmts);
    lyplg_ext_get_storage(e, LY_STMT_UNITS, sizeof units, (const void **)&units);
    assert_string_equal("meters", units);

    /* invalid */
    /* missing mandatory type substatement */
    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<md:annotation name=\"aa\"/>\n"
            "</module>";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
    CHECK_LOG_CTX("Ext plugin \"ly2 metadata v1\": Missing mandatory keyword \"type\" as a child of \"md:annotation aa\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* not allowed substatement */
    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<md:annotation name=\"aa\">\n"
            "  <default value=\"x\"/>\n"
            "</md:annotation></module>";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
    CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance.",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* invalid cardinality of units substatement */
    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<md:annotation name=\"aa\">\n"
            "  <type name=\"string\"/>\n"
            "  <units name=\"x\"/>\n"
            "  <units name=\"y\"/>\n"
            "</md:annotation></module>";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
    CHECK_LOG_CTX("Duplicate keyword \"units\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* invalid cardinality of status substatement */
    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<md:annotation name=\"aa\">\n"
            "  <type name=\"string\"/>\n"
            "  <status value=\"current\"/>\n"
            "  <status value=\"obsolete\"/>\n"
            "</md:annotation></module>";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
    CHECK_LOG_CTX("Duplicate keyword \"status\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* invalid cardinality of status substatement */
    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<md:annotation name=\"aa\">\n"
            "  <type name=\"string\"/>\n"
            "  <type name=\"uint8\"/>\n"
            "</md:annotation></module>";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
    CHECK_LOG_CTX("Duplicate keyword \"type\".",
            "Path \"/aa:{extension='md:annotation'}/aa\".");

    /* duplication of the same annotation */
    data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n"
            "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n"
            "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n"
            "<md:annotation name=\"aa\">\n"
            "  <type name=\"string\"/>\n"
            "</md:annotation><md:annotation name=\"aa\">\n"
            "  <type name=\"uint8\"/>\n"
            "</md:annotation></module>";
    assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YIN, NULL));
    CHECK_LOG_CTX("Ext plugin \"ly2 metadata v1\": Extension md:annotation is instantiated multiple times.",
            "Path \"/aa:{extension='md:annotation'}/aa\".");
}

int
main(void)
{
    const struct CMUnitTest tests[] = {
        UTEST(test_yang),
        UTEST(test_yin),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}