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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/**
* @file test_pattern.c
* @author Radek Iša <isa@cesnet.cz>
* @brief test for int8 values
*
* Copyright (c) 2021 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
*/
/* INCLUDE UTEST HEADER */
#define _UTEST_MAIN_
#include "../utests.h"
/* GLOBAL INCLUDE HEADERS */
#include <ctype.h>
/* LOCAL INCLUDE HEADERS */
#include "libyang.h"
#include "path.h"
#define MODULE_CREATE_YIN(MOD_NAME, NODES) \
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
"<module name=\"" MOD_NAME "\"\n" \
" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" \
" xmlns:pref=\"urn:tests:" MOD_NAME "\">\n" \
" <yang-version value=\"1.1\"/>\n" \
" <namespace uri=\"urn:tests:" MOD_NAME "\"/>\n" \
" <prefix value=\"pref\"/>\n" \
NODES \
"</module>\n"
#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
"module " MOD_NAME " {\n" \
" yang-version 1.1;\n" \
" namespace \"urn:tests:" MOD_NAME "\";\n" \
" prefix pref;\n" \
NODES \
"}\n"
#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
{ \
struct lyd_node *tree; \
const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, __VA_ARGS__); \
lyd_free_all(tree); \
}
#define TEST_SUCCESS_JSON(MOD_NAME, DATA, TYPE, ...) \
{ \
struct lyd_node *tree; \
const char *data = "{\"" MOD_NAME ":port\":" DATA "}"; \
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, __VA_ARGS__); \
lyd_free_all(tree); \
}
#define TEST_ERROR_XML(MOD_NAME, DATA) \
{\
struct lyd_node *tree; \
const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
assert_null(tree); \
}
#define TEST_ERROR_JSON(MOD_NAME, DATA) \
{ \
struct lyd_node *tree; \
const char *data = "{\"" MOD_NAME ":port\":" DATA "}"; \
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
assert_null(tree); \
}
static void
test_schema_yang(void **state)
{
const char *schema;
struct lys_module *mod;
struct lysc_node_leaf *lysc_leaf;
struct lysp_node_leaf *lysp_leaf;
struct lysc_pattern *pattern;
schema = MODULE_CREATE_YANG("T0", "leaf port {type string {"
"pattern \"[A-Za-z]*\"{"
"description \"pattern description\";"
"error-app-tag \"pattern err-apt-tag\";"
"error-message \"pattern error message\";}}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag",
"pattern error message", "[A-Za-z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "string", 0, 1, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Za-z]*", "pattern description",
"pattern err-apt-tag", "pattern error message", 0, NULL);
/* heredity */
schema = MODULE_CREATE_YANG("T1", "typedef my_type {type string {"
"pattern \"[A-Za-z]*\"{"
"description \"pattern description\";"
"error-app-tag \"pattern err-apt-tag\";"
"error-message \"pattern error message\";}}}"
"leaf port {type my_type;}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag",
"pattern error message", "[A-Za-z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x0, 0, 0, "my_type", 0, 0, 1, 0, 0, 0);
/* heredity new pattern */
schema = MODULE_CREATE_YANG("T2", "typedef my_type {type string {"
"pattern \"[A-Za-z]*\"{"
"description \"pattern description\";"
"error-app-tag \"pattern err-apt-tag\";"
"error-message \"pattern error message\";}}}"
"leaf port {type my_type{pattern \"[A-Z]*\";}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag",
"pattern error message", "[A-Za-z]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[A-Z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", NULL, NULL, NULL, 0, NULL);
/* heredity new pattern */
schema = MODULE_CREATE_YANG("T3", "typedef my_type {type string {"
"pattern \"[A-Za-z]*\"{"
" description \"pattern 0 description\";"
" error-app-tag \"pattern 0 err-apt-tag\";"
" error-message \"pattern 0 error message\";}}}"
"leaf port {type my_type{pattern \"[A-Z]*\"{"
" description \"pattern 1 description\";"
" error-app-tag \"pattern 1 err-apt-tag\";"
" error-message \"pattern 1 error message\";"
"}}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern 0 description", "pattern 0 err-apt-tag",
"pattern 0 error message", "[A-Za-z]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, "pattern 1 description", "pattern 1 err-apt-tag",
"pattern 1 error message", "[A-Z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", "pattern 1 description",
"pattern 1 err-apt-tag", "pattern 1 error message", 0, NULL);
}
static void
test_schema_yin(void **state)
{
const char *schema;
struct lys_module *mod;
struct lysc_node_leaf *lysc_leaf;
struct lysp_node_leaf *lysp_leaf;
struct lysc_pattern *pattern;
schema = MODULE_CREATE_YIN("T0", "<leaf name=\"port\"> <type name=\"string\">"
"<pattern value=\"[A-Za-z]*\">"
" <description><text>pattern description</text></description>"
" <error-app-tag value=\"pattern err-apt-tag\"/>"
" <error-message> <value>pattern error message</value></error-message>"
"</pattern></type></leaf>");
UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag",
"pattern error message", "[A-Za-z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "string", 0, 1, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Za-z]*", "pattern description",
"pattern err-apt-tag", "pattern error message", 0, NULL);
/* heredity */
schema = MODULE_CREATE_YIN("T1", "<typedef name=\"my_type\"> <type name=\"string\">"
"<pattern value=\"[A-Za-z]*\">"
" <description><text>pattern description</text></description>"
" <error-app-tag value=\"pattern err-apt-tag\"/>"
" <error-message><value>pattern error message</value></error-message>"
"</pattern></type></typedef>"
"<leaf name=\"port\"><type name=\"my_type\"/></leaf>");
UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag",
"pattern error message", "[A-Za-z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x0, 0, 0, "my_type", 0, 0, 1, 0, 0, 0);
/* heredity new pattern */
schema = MODULE_CREATE_YIN("T2", "<typedef name=\"my_type\"> <type name=\"string\">"
"<pattern value=\"[A-Za-z]*\">"
" <description><text>pattern description</text></description>"
" <error-app-tag value=\"pattern err-apt-tag\"/>"
" <error-message><value>pattern error message</value></error-message>"
"</pattern></type></typedef>"
"<leaf name=\"port\"> <type name=\"my_type\"><pattern value=\"[A-Z]*\"/>"
"</type></leaf>");
UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern description", "pattern err-apt-tag",
"pattern error message", "[A-Za-z]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[A-Z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", NULL, NULL, NULL, 0, NULL);
/* heredity new pattern */
schema = MODULE_CREATE_YIN("T3", "<typedef name=\"my_type\"> <type name=\"string\">"
"<pattern value=\"[A-Za-z]*\">"
" <description> <text>pattern 0 description</text></description>"
" <error-app-tag value=\"pattern 0 err-apt-tag\"/>"
" <error-message> <value>pattern 0 error message</value></error-message>"
"</pattern></type></typedef>"
"<leaf name=\"port\"> <type name=\"my_type\">"
"<pattern value=\"[A-Z]*\">"
" <description><text>pattern 1 description</text></description>"
" <error-app-tag value=\"pattern 1 err-apt-tag\"/>"
" <error-message><value>pattern 1 error message</value></error-message>"
"</pattern></type></leaf>");
UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
assert_non_null(mod);
lysc_leaf = (void *) mod->compiled->data;
CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 2);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, "pattern 0 description", "pattern 0 err-apt-tag",
"pattern 0 error message", "[A-Za-z]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, "pattern 1 description", "pattern 1 err-apt-tag",
"pattern 1 error message", "[A-Z]*", 0, 0, NULL);
lysp_leaf = (void *) mod->parsed->data;
CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x40, 0, 0, "my_type", 0, 1, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[A-Z]*", "pattern 1 description",
"pattern 1 err-apt-tag", "pattern 1 error message", 0, NULL);
}
static void
test_schema_print(void **state)
{
const char *schema_yang, *schema_yin;
char *printed;
struct lys_module *mod;
/* test print yang to yin */
schema_yang = MODULE_CREATE_YANG("PRINT0", "leaf port {type string {"
"pattern \"[A-Z]*\"{"
"description \"desc < \";"
"error-app-tag \"err-apt-tag <\";"
"error-message \"error message <\";}}}");
schema_yin = MODULE_CREATE_YIN("PRINT0",
" <leaf name=\"port\">\n"
" <type name=\"string\">\n"
" <pattern value=\"[A-Z]*\">\n"
" <error-message>\n"
" <value>error message <</value>\n"
" </error-message>\n"
" <error-app-tag value=\"err-apt-tag <\"/>\n"
" <description>\n"
" <text>desc < </text>\n"
" </description>\n"
" </pattern>\n"
" </type>\n"
" </leaf>\n");
UTEST_ADD_MODULE(schema_yang, LYS_IN_YANG, NULL, &mod);
assert_non_null(mod);
assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
assert_string_equal(printed, schema_yin);
free(printed);
/* test print yin to yang */
schema_yang = MODULE_CREATE_YANG("PRINT1",
"\n"
" leaf port {\n"
" type string {\n"
" pattern \"[A-Z]*\" {\n"
" error-message\n"
" \"error message <\";\n"
" error-app-tag \"err-apt-tag <\";\n"
" description\n"
" \"desc < \";\n"
" }\n"
" }\n"
" }\n");
schema_yin = MODULE_CREATE_YIN("PRINT1",
" <leaf name=\"port\">\n"
" <type name=\"string\">\n"
" <pattern value=\"[A-Z]*\">\n"
" <error-message>\n"
" <value>error message <</value>\n"
" </error-message>\n"
" <error-app-tag value=\"err-apt-tag <\"/>\n"
" <description>\n"
" <text>desc < </text>\n"
" </description>\n"
" </pattern>\n"
" </type>\n"
" </leaf>\n");
UTEST_ADD_MODULE(schema_yin, LYS_IN_YIN, NULL, &mod);
assert_non_null(mod);
assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
assert_string_equal(printed, schema_yang);
free(printed);
}
static void
test_data_xml(void **state)
{
const char *schema;
/* xml test */
schema = MODULE_CREATE_YANG("TPATTERN_0", "typedef my_type {type string {"
"pattern \"[A-Za-z]*\"{"
" description \"pattern 0 description\";"
" error-app-tag \"pattern 0 err-apt-tag\";"
" error-message \"pattern 0 error message\";}}}"
"leaf port {type my_type{pattern \"[A-Z]*\"{"
" description \"pattern 1 description\";"
" error-app-tag \"pattern 1 err-apt-tag\";"
" error-message \"pattern 1 error message\";"
"}}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
/* test success */
TEST_SUCCESS_XML("TPATTERN_0", "AHOJ", STRING, "AHOJ");
/* test print error */
TEST_ERROR_XML("TPATTERN_0", "T128");
CHECK_LOG_CTX("pattern 0 error message",
"Schema location \"/TPATTERN_0:port\", line number 1.");
TEST_ERROR_XML("TPATTERN_0", "ahoj");
CHECK_LOG_CTX("pattern 1 error message",
"Schema location \"/TPATTERN_0:port\", line number 1.");
}
int
main(void)
{
const struct CMUnitTest tests[] = {
UTEST(test_schema_yang),
UTEST(test_schema_yin),
UTEST(test_schema_print),
UTEST(test_data_xml),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|