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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/**
* @file test_common.c
* @author: Radek Krejci <rkrejci@cesnet.cz>
* @brief unit tests for functions from common.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 "ly_common.h"
static void
test_utf8(void **UNUSED(state))
{
char buf[5] = {0};
const char *str = buf;
unsigned int c;
size_t len;
/* test invalid UTF-8 characters in lyxml_getutf8
* - https://en.wikipedia.org/wiki/UTF-8 */
buf[0] = (char)0x04;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[0] = (char)0x80;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[0] = (char)0xc0;
buf[1] = (char)0x00;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[1] = (char)0x80;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[0] = (char)0xe0;
buf[1] = (char)0x00;
buf[2] = (char)0x80;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[1] = (char)0x80;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[0] = (char)0xf0;
buf[1] = (char)0x00;
buf[2] = (char)0x80;
buf[3] = (char)0x80;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
buf[1] = (char)0x80;
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
}
static void
test_parse_int(void **UNUSED(state))
{
const char *str;
int64_t i = 500;
str = "10";
assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
assert_int_equal(i, 10);
/* leading zeros are allowed, trailing whitespaces are allowed */
str = "000\n\t ";
assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
assert_int_equal(i, 0);
/* negative value */
str = "-10";
assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
assert_int_equal(i, -10);
/* non-NULL terminated string */
str = "+5sometext";
assert_int_equal(LY_SUCCESS, ly_parse_int(str, 2, -10, 10, 10, &i));
assert_int_equal(i, 5);
/* out of bounds value */
str = "11";
assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
str = "-11";
assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
/* NaN */
str = "zero";
assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
/* mixing number with text */
str = "10zero";
assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
str = "10 zero";
assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i));
}
static void
test_parse_uint(void **UNUSED(state))
{
const char *str;
uint64_t u = 500;
str = "10";
assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u));
assert_int_equal(u, 10);
/* leading zeros are allowed, trailing whitespaces are allowed */
str = "000\n\t ";
assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u));
assert_int_equal(u, 0);
/* non-NULL terminated string */
str = "+5sometext";
assert_int_equal(LY_SUCCESS, ly_parse_uint(str, 2, 10, 10, &u));
assert_int_equal(u, 5);
/* out of bounds value */
str = "11";
assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), 10, 10, &u));
str = "-1";
assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), (uint64_t)-1, 10, &u));
/* NaN */
str = "zero";
assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u));
/* mixing number with text */
str = "10zero";
assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u));
str = "10 zero";
assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u));
}
static void
test_parse_nodeid(void **UNUSED(state))
{
const char *str;
const char *prefix, *name;
size_t prefix_len, name_len;
str = "123";
assert_int_equal(LY_EINVAL, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
str = "a12_-.!";
assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
assert_null(prefix);
assert_int_equal(0, prefix_len);
assert_non_null(name);
assert_int_equal(6, name_len);
assert_int_equal(0, strncmp("a12_-.", name, name_len));
assert_string_equal("!", str);
str = "a12_-.:_b2 xxx";
assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
assert_non_null(prefix);
assert_int_equal(6, prefix_len);
assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len));
assert_non_null(name);
assert_int_equal(3, name_len);
assert_int_equal(0, strncmp("_b2", name, name_len));
assert_string_equal(" xxx", str);
}
static void
test_parse_instance_predicate(void **UNUSED(state))
{
const char *str, *errmsg;
const char *prefix, *id, *value;
size_t prefix_len, id_len, value_len;
str = "[ex:name='fred']";
assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(str, "");
assert_string_equal(prefix, "ex:name='fred']");
assert_int_equal(prefix_len, 2);
assert_string_equal(id, "name='fred']");
assert_int_equal(id_len, 4);
assert_string_equal(value, "fred']");
assert_int_equal(value_len, 4);
str = "[ex:ip = \"[192.0.2.1]\"][ex:port='80']";
assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(str, "[ex:port='80']");
assert_string_equal(prefix, "ex:ip = \"[192.0.2.1]\"][ex:port='80']");
assert_int_equal(prefix_len, 2);
assert_string_equal(id, "ip = \"[192.0.2.1]\"][ex:port='80']");
assert_int_equal(id_len, 2);
assert_string_equal(value, "[192.0.2.1]\"][ex:port='80']");
assert_int_equal(value_len, 11);
str = "[. = 'blowfish-cbc']";
assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(str, "");
assert_null(prefix);
assert_int_equal(prefix_len, 0);
assert_string_equal(id, ". = 'blowfish-cbc']");
assert_int_equal(id_len, 1);
assert_string_equal(value, "blowfish-cbc']");
assert_int_equal(value_len, 12);
str = "[ 3 ]";
assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(str, "");
assert_null(prefix);
assert_int_equal(prefix_len, 0);
assert_null(id);
assert_int_equal(id_len, 0);
assert_string_equal(value, "3 ]");
assert_int_equal(value_len, 1);
/* invalid predicates */
/* position must be positive integer */
str = "[0]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "The position predicate cannot be zero.");
str = "[-1]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Invalid instance predicate format (negative position or invalid node-identifier).");
/* invalid node-identifier */
str = "[$node='value']";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Invalid node-identifier.");
str = "[.node='value']";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Unexpected character instead of '=' in leaf-list-predicate.");
str = "[13node='value']";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Predicate (pos) is not terminated by \']\' character.");
str = "[ex:node]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Unexpected character instead of '=' in key-predicate.");
str = "[ex:node= value]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "String value is not quoted.");
str = "[ex:node='value\"]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Value is not terminated quoted-string.");
str = "[ex:node='value ]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Value is not terminated quoted-string.");
str = "[ex:node=\"value\"[3]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Predicate (key-predicate) is not terminated by \']\' character.");
str = "[.=\"value\"[3]";
assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Predicate (leaf-list-predicate) is not terminated by \']\' character.");
/* the limit of the string is too short, it ends one character earlier */
str = "[ex:node='value']";
assert_int_equal(LY_EINVAL, ly_parse_instance_predicate(&str, strlen(str) - 1, LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg));
assert_string_equal(errmsg, "Predicate is incomplete.");
}
static void
test_value_prefix_next(void **UNUSED(state))
{
const char *next;
ly_bool is_prefix;
uint32_t bytes;
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(NULL, NULL, &bytes, &is_prefix, &next));
assert_int_equal(0, bytes);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next("", NULL, &bytes, &is_prefix, &next));
assert_int_equal(0, bytes);
/* prefix */
next = "pref:";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(1, is_prefix);
/* no-prefix */
next = "node";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
/* no-prefix */
next = "::::";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
/* no-prefix */
next = "//a/:";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(5, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
/* no-prefix */
next = "//a//";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(5, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
/* prefix, prefix */
next = "pref1:pref2:";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(5, bytes);
assert_string_equal(next, "pref2:");
assert_int_equal(1, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(5, bytes);
assert_null(next);
assert_int_equal(1, is_prefix);
/* prefix, no-prefix */
next = "pref:node";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_string_equal(next, "node");
assert_int_equal(1, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
/* no-prefix, prefix */
next = "/pref:";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(1, bytes);
assert_string_equal(next, "pref:");
assert_int_equal(0, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(1, is_prefix);
/* no-prefix, prefix */
next = "//pref:";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(2, bytes);
assert_string_equal(next, "pref:");
assert_int_equal(0, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(1, is_prefix);
/* no-prefix, prefix, no-prefix */
next = "/pref:node";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(1, bytes);
assert_string_equal(next, "pref:node");
assert_int_equal(0, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_string_equal(next, "node");
assert_int_equal(1, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
/* prefix, no-prefix, prefix */
next = "pref:node pref:";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_string_equal(next, "node pref:");
assert_int_equal(1, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(5, bytes);
assert_string_equal(next, "pref:");
assert_int_equal(0, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(1, is_prefix);
/* prefix, no-prefix, prefix, no-prefix */
next = "pref:node /pref:node";
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_string_equal(next, "node /pref:node");
assert_int_equal(1, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(6, bytes);
assert_string_equal(next, "pref:node");
assert_int_equal(0, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_string_equal(next, "node");
assert_int_equal(1, is_prefix);
assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next));
assert_int_equal(4, bytes);
assert_null(next);
assert_int_equal(0, is_prefix);
}
int
main(void)
{
const struct CMUnitTest tests[] = {
UTEST(test_utf8),
UTEST(test_parse_int),
UTEST(test_parse_uint),
UTEST(test_parse_nodeid),
UTEST(test_parse_instance_predicate),
UTEST(test_value_prefix_next),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|