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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
|
/**
* @file test_yang.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief unit tests for YANG module parser and printer
*
* Copyright (c) 2018 - 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 <stdio.h>
#include <string.h>
#include "common.h"
#include "in_internal.h"
#include "parser_internal.h"
#include "schema_compile.h"
#include "tree_edit.h"
#include "tree_schema.h"
#include "tree_schema_free.h"
/* originally static functions from parser_yang.c and parser_yin.c */
LY_ERR buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
LY_ERR buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p,
size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix);
LY_ERR get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
LY_ERR get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg,
uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
LY_ERR skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment);
LY_ERR parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions);
LY_ERR parse_any(struct lysp_yang_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments);
LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates);
LY_ERR parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations);
LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
LY_ERR parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
LY_ERR parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod);
LY_ERR parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs);
LY_ERR parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod);
LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
LY_ERR parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p);
LY_ERR parse_type_enum_value_pos(struct lysp_yang_ctx *ctx, enum ly_stmt val_kw, struct lysp_type_enum *enm);
struct lysp_yang_ctx *YCTX;
struct lysf_ctx fctx;
struct ly_in in = {0};
#define YCTX_INIT \
in.line = 1; \
YCTX->in = ∈ \
LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in)
static int
setup(void **state)
{
struct lysp_module *pmod;
UTEST_SETUP;
/* allocate parser context */
YCTX = calloc(1, sizeof(*YCTX));
YCTX->main_ctx = (struct lysp_ctx *)YCTX;
YCTX->format = LYS_IN_YANG;
ly_set_new(&YCTX->parsed_mods);
/* allocate new parsed module */
pmod = calloc(1, sizeof *pmod);
ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
/* allocate new module */
pmod->mod = calloc(1, sizeof *pmod->mod);
pmod->mod->ctx = UTEST_LYCTX;
pmod->mod->parsed = pmod;
/* initilize and use the global easily available and customizable input handler */
in.line = 1;
YCTX->in = ∈
LOG_LOCSET(NULL, NULL, NULL, &in);
fctx.ctx = PARSER_CTX(YCTX);
fctx.mod = pmod->mod;
return 0;
}
static int
teardown(void **state)
{
lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
LOG_LOCBACK(0, 0, 0, 1);
ly_set_free(YCTX->parsed_mods, NULL);
ly_set_erase(&YCTX->ext_inst, NULL);
free(YCTX);
YCTX = NULL;
lysf_ctx_erase(&fctx);
UTEST_TEARDOWN;
return 0;
}
#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
CLEANUP
static void
test_helpers(void **state)
{
char *buf, *p;
size_t len, size;
uint8_t prefix = 0;
/* storing into buffer */
in.current = "abcd";
buf = NULL;
size = len = 0;
assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
assert_int_not_equal(0, size);
assert_int_equal(2, len);
assert_string_equal("cd", in.current);
assert_false(strncmp("ab", buf, 2));
free(buf);
buf = NULL;
/* invalid first characters */
len = 0;
in.current = "2invalid";
assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
in.current = ".invalid";
assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
in.current = "-invalid";
assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
/* invalid following characters */
len = 3; /* number of characters read before the str content */
in.current = "!";
assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
in.current = ":";
assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
/* valid colon for prefixed identifiers */
len = size = 0;
p = NULL;
prefix = 0;
in.current = "x:id";
assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
assert_int_equal(1, len);
assert_null(buf);
assert_string_equal(":id", in.current);
assert_int_equal('x', p[len - 1]);
assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
assert_int_equal(2, len);
assert_string_equal("id", in.current);
assert_int_equal(':', p[len - 1]);
free(buf);
prefix = 0;
/* checking identifiers */
assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, NULL));
CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, '#', 1, NULL));
CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, 'a', 1, &prefix));
assert_int_equal(0, prefix);
assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
assert_int_equal(1, prefix);
assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
assert_int_equal(1, prefix);
assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, 'b', 0, &prefix));
assert_int_equal(2, prefix);
/* second colon is invalid */
assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
}
#define TEST_GET_ARGUMENT_SUCCESS(INPUT_TEXT, CTX, ARG_TYPE, EXPECT_WORD, EXPECT_LEN, EXPECT_CURRENT, EXPECT_LINE)\
{\
const char * text = INPUT_TEXT;\
in.line = 1;\
in.current = text;\
assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
assert_string_equal(word, EXPECT_WORD);\
assert_int_equal(len, EXPECT_LEN);\
assert_string_equal(EXPECT_CURRENT, in.current);\
assert_int_equal(EXPECT_LINE, in.line);\
}
static void
test_comments(void **state)
{
char *word, *buf;
size_t len;
TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;",
YCTX, Y_STR_ARG, "argument;", 8, ";", 2);
assert_null(buf);
TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";",
YCTX, Y_STR_ARG, "argument", 8, ";", 4);
assert_ptr_equal(buf, word);
free(word);
in.line = 1;
in.current = " this is one line comment on last line";
assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
assert_true(in.current[0] == '\0');
in.line = 1;
in.current = " this is a not terminated comment x";
assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1.");
assert_true(in.current[0] == '\0');
}
static void
test_arg(void **state)
{
char *word, *buf;
size_t len;
/* missing argument */
in.current = ";";
assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
assert_null(word);
in.current = "{";
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
/* invalid escape sequence */
in.current = "\"\\s\"";
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1);
/* invalid character after the argument */
in.current = "hello\"";
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
in.current = "hello}";
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
/* invalid identifier-ref-arg-str */
in.current = "pre:pre:value";
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
in.current = "\"\";"; /* empty identifier is not allowed */
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
in.current = "\"\";"; /* empty reference identifier is not allowed */
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
/* slash is not an invalid character */
TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
assert_null(buf);
/* different quoting */
TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1);
TEST_GET_ARGUMENT_SUCCESS("hello/*comment*/\n", YCTX, Y_STR_ARG, "hello/*comment*/\n", 5, "\n", 1);
TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1);
free(buf);
YCTX->indent = 14;
/* - space and tabs before newline are stripped out
* - space and tabs after newline (indentation) are stripped out
*/
TEST_GET_ARGUMENT_SUCCESS("\"hello \t\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n world!", 14, "", 2);
free(buf);
/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
YCTX->indent = 14;
TEST_GET_ARGUMENT_SUCCESS("\"hello \\t\n\t\\t world!\"", YCTX, Y_STR_ARG, "hello \t\n\t world!", 16, "", 2);
assert_ptr_equal(word, buf);
free(buf);
/* Do not handle whitespaces after backslash-escaped newline as indentation */
YCTX->indent = 14;
TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n\t\t world!", 15, "", 1);
assert_ptr_equal(word, buf);
free(buf);
YCTX->indent = 14;
TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2);
assert_ptr_equal(word, buf);
free(buf);
TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1);
TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
assert_ptr_equal(word, buf);
free(buf);
in.line = 1;
in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2.");
TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
free(buf);
TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2);
free(buf);
/* missing argument */
in.line = 1;
in.current = ";";
assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1.");
}
#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
in.current = INPUT_TEXT;\
assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
assert_int_equal(ACTION, kw);\
assert_int_equal(strlen(EXPECT_WORD), len);\
assert_true(0 == strncmp(EXPECT_WORD, word, len))
static void
test_stmts(void **state)
{
const char *p;
enum ly_stmt kw;
char *word;
size_t len;
in.current = "\n// comment\n\tinput\t{";
assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
assert_int_equal(LY_STMT_INPUT, kw);
assert_int_equal(5, len);
assert_string_equal("input\t{", word);
assert_string_equal("\t{", in.current);
in.current = "\t /* comment */\t output\n\t{";
assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
assert_int_equal(LY_STMT_OUTPUT, kw);
assert_int_equal(6, len);
assert_string_equal("output\n\t{", word);
assert_string_equal("\n\t{", in.current);
assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
assert_int_equal(1, len);
assert_string_equal("{", word);
assert_string_equal("", in.current);
in.current = "/input { "; /* invalid slash */
assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
in.current = "path;"; /* missing sep after the keyword */
assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
assert_string_equal("config false;", in.current);
TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
assert_string_equal(" config false;", in.current);
TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
assert_string_equal("", in.current);
/* geenric extension */
in.current = p = "nacm:default-deny-write;";
assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
assert_int_equal(23, len);
assert_ptr_equal(p, word);
}
static void
test_minmax(void **state)
{
uint16_t flags = 0;
uint32_t value = 0;
struct lysp_ext_instance *ext = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
in.current = " 1invalid; ...";
assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
flags = value = 0;
in.current = " -1; ...";
assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
/* implementation limit */
flags = value = 0;
in.current = " 4294967296; ...";
assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
flags = value = 0;
in.current = " 1 {config true;} ...";
assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
in.current = " 1invalid; ...";
assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
flags = value = 0;
in.current = " -1; ...";
assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
/* implementation limit */
flags = value = 0;
in.current = " 4294967296; ...";
assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
flags = value = 0;
in.current = " 1 {config true;} ...";
assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
}
static void
test_valid_module(void **state)
{
struct lys_module *mod;
char *printed;
const char *links_yang =
"module links {\n"
" yang-version 1.1;\n"
" namespace \"urn:module2\";\n"
" prefix mod2;\n"
"\n"
" identity just-another-identity;\n"
"\n"
" leaf one-leaf {\n"
" type string;\n"
" }\n"
"\n"
" list list-for-augment {\n"
" key keyleaf;\n"
"\n"
" leaf keyleaf {\n"
" type string;\n"
" }\n"
"\n"
" leaf just-leaf {\n"
" type int32;\n"
" }\n"
" }\n"
"\n"
" leaf rleaf {\n"
" type string;\n"
" }\n"
"\n"
" leaf-list llist {\n"
" type string;\n"
" min-elements 0;\n"
" max-elements 100;\n"
" ordered-by user;\n"
" }\n"
"\n"
" grouping rgroup {\n"
" leaf rg1 {\n"
" type string;\n"
" }\n"
"\n"
" leaf rg2 {\n"
" type string;\n"
" }\n"
" }\n"
"}\n";
const char *statements_yang =
"module statements {\n"
" yang-version 1.1;\n"
" namespace \"urn:module\";\n"
" prefix mod;\n"
"\n"
" import links {\n"
" prefix mod2;\n"
" }\n"
"\n"
" extension ext;\n"
"\n"
" identity random-identity {\n"
" base mod2:just-another-identity;\n"
" base another-identity;\n"
" }\n"
"\n"
" identity another-identity {\n"
" base mod2:just-another-identity;\n"
" }\n"
"\n"
" typedef percent {\n"
" type uint8 {\n"
" range \"0 .. 100\";\n"
" }\n"
" units \"percent\";\n"
" }\n"
"\n"
" list list1 {\n"
" key \"a\";\n"
" leaf a {\n"
" type string;\n"
" }\n"
" leaf x {\n"
" type string;\n"
" }\n"
" leaf y {\n"
" type string;\n"
" }\n"
" }\n"
" container ice-cream-shop {\n"
" container employees {\n"
" when \"/list1/x\";\n"
" list employee {\n"
" key \"id\";\n"
" unique \"name\";\n"
" config true;\n"
" min-elements 0 {\n"
" mod:ext;\n"
" }\n"
" max-elements unbounded;\n"
" leaf id {\n"
" type uint64;\n"
" mandatory true;\n"
" }\n"
" leaf name {\n"
" type string;\n"
" }\n"
" leaf age {\n"
" type uint32;\n"
" }\n"
" }\n"
" }\n"
" }\n"
" container random {\n"
" grouping group {\n"
" leaf g1 {\n"
" type percent;\n"
" mandatory false;\n"
" }\n"
" leaf g2 {\n"
" type string;\n"
" }\n"
" }\n"
" choice switch {\n"
" case a {\n"
" leaf aleaf {\n"
" type string;\n"
" default \"aaa\";\n"
" }\n"
" }\n"
" case c {\n"
" leaf cleaf {\n"
" type string;\n"
" }\n"
" }\n"
" }\n"
" anyxml xml-data;\n"
" anydata any-data;\n"
" leaf-list leaflist {\n"
" type string;\n"
" min-elements 0;\n"
" max-elements 20;\n"
" }\n"
" uses group;\n"
" uses mod2:rgroup;\n"
" leaf lref {\n"
" type leafref {\n"
" path \"/mod2:one-leaf\";\n"
" }\n"
" }\n"
" leaf iref {\n"
" type identityref {\n"
" base mod2:just-another-identity;\n"
" }\n"
" }\n"
" }\n"
"\n"
" augment \"/random\" {\n"
" leaf aug-leaf {\n"
" type string;\n"
" }\n"
" }\n"
"\n"
" notification notif;\n"
"\n"
" deviation \"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\" {\n"
" deviate not-supported {\n"
" mod:ext;\n"
" }\n"
" }\n"
" deviation \"/mod:list1\" {\n"
" deviate add {\n"
" mod:ext;\n"
" must \"1\";\n"
" must \"2\";\n"
" unique \"x\";\n"
" unique \"y\";\n"
" config true;\n"
" min-elements 1;\n"
" max-elements 2;\n"
" }\n"
" }\n"
" deviation \"/mod:ice-cream-shop/mod:employees/mod:employee\" {\n"
" deviate delete {\n"
" unique \"name\";\n"
" }\n"
" }\n"
" deviation \"/mod:random/mod:leaflist\" {\n"
" deviate replace {\n"
" type uint32;\n"
" min-elements 10;\n"
" max-elements 15;\n"
" }\n"
" }\n"
"}\n";
UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, &mod);
lys_print_mem(&printed, mod, LYS_OUT_YANG, 0);
assert_string_equal(printed, statements_yang);
free(printed);
}
static struct lysp_module *
mod_renew(struct lysp_yang_ctx *ctx)
{
struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
struct lysp_module *pmod;
lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
pmod = calloc(1, sizeof *pmod);
ctx->parsed_mods->objs[0] = pmod;
pmod->mod = calloc(1, sizeof *pmod->mod);
pmod->mod->parsed = pmod;
pmod->mod->ctx = ly_ctx;
ctx->in->line = 1;
fctx.mod = pmod->mod;
return pmod;
}
static struct lysp_submodule *
submod_renew(struct lysp_yang_ctx *ctx)
{
struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
struct lysp_submodule *submod;
lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
submod = calloc(1, sizeof *submod);
ctx->parsed_mods->objs[0] = submod;
submod->mod = calloc(1, sizeof *submod->mod);
lydict_insert(ly_ctx, "name", 0, &submod->mod->name);
submod->mod->parsed = (struct lysp_module *)submod;
submod->mod->ctx = ly_ctx;
fctx.mod = submod->mod;
return submod;
}
static LY_ERR
test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
{
*module_data = user_data;
*format = LYS_IN_YANG;
*free_module_data = NULL;
return LY_SUCCESS;
}
static void
test_module(void **state)
{
struct lysp_module *mod = NULL;
struct lysp_submodule *submod = NULL;
struct lys_module *m;
struct lysp_yang_ctx *ctx_p;
mod = mod_renew(YCTX);
/* missing mandatory substatements */
in.current = " name {}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
assert_string_equal("name", mod->mod->name);
CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
mod = mod_renew(YCTX);
in.current = " name {namespace urn:name;}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
assert_string_equal("urn:name", mod->mod->ns);
CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
mod = mod_renew(YCTX);
in.current = " name {namespace urn:name;prefix \"n\";}";
assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
assert_string_equal("n", mod->mod->prefix);
mod = mod_renew(YCTX);
#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
#define TEST_NODE(NODETYPE, INPUT, NAME) \
in.current = SCHEMA_BEGINNING INPUT; \
assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
assert_non_null(mod->data); \
assert_int_equal(NODETYPE, mod->data->nodetype); \
assert_string_equal(NAME, mod->data->name); \
mod = mod_renew(YCTX);
#define TEST_GENERIC(INPUT, TARGET, TEST) \
in.current = SCHEMA_BEGINNING INPUT; \
assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
assert_non_null(TARGET); \
TEST; \
mod = mod_renew(YCTX);
#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
parse_module, mod, LINE, mod = mod_renew(YCTX))
/* duplicated namespace, prefix */
TEST_DUP("namespace", "y", "z", "1");
TEST_DUP("prefix", "y", "z", "1");
TEST_DUP("contact", "a", "b", "1");
TEST_DUP("description", "a", "b", "1");
TEST_DUP("organization", "a", "b", "1");
TEST_DUP("reference", "a", "b", "1");
/* not allowed in module (submodule-specific) */
in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
mod = mod_renew(YCTX);
/* anydata */
TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
/* anyxml */
TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
/* augment */
TEST_GENERIC("augment /somepath;}", mod->augments,
assert_string_equal("/somepath", mod->augments[0].nodeid));
/* choice */
TEST_NODE(LYS_CHOICE, "choice test;}", "test");
/* contact 0..1 */
TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
assert_string_equal("firstname surname", mod->mod->contact));
/* container */
TEST_NODE(LYS_CONTAINER, "container test;}", "test");
/* description 0..1 */
TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
assert_string_equal("some description", mod->mod->dsc));
/* deviation */
TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
assert_string_equal("/somepath", mod->deviations[0].nodeid));
/* extension */
TEST_GENERIC("extension test;}", mod->extensions,
assert_string_equal("test", mod->extensions[0].name));
/* feature */
TEST_GENERIC("feature test;}", mod->features,
assert_string_equal("test", mod->features[0].name));
/* grouping */
TEST_GENERIC("grouping grp;}", mod->groupings,
assert_string_equal("grp", mod->groupings[0].name));
/* identity */
TEST_GENERIC("identity test;}", mod->identities,
assert_string_equal("test", mod->identities[0].name));
/* import */
ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
assert_string_equal("zzz", mod->imports[0].name));
/* import - prefix collision */
in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
mod = mod_renew(YCTX);
in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
mod = mod_renew(YCTX);
LOG_LOCBACK(0, 0, 0, 1);
in.current = "module name10 {yang-version 1.1;namespace urn:name10;prefix \"n10\";import zzz {prefix y;}import zzz {prefix z;}}";
assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
/* include */
ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
in.current = "module" SCHEMA_BEGINNING "include xxx;}";
assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
in.current = "module" SCHEMA_BEGINNING "include xxx;}";
assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
TEST_GENERIC("include xxx;}", mod->includes,
assert_string_equal("xxx", mod->includes[0].name));
/* leaf */
TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
/* leaf-list */
TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
/* list */
TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
/* notification */
TEST_GENERIC("notification test;}", mod->notifs,
assert_string_equal("test", mod->notifs[0].name));
/* organization 0..1 */
TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
assert_string_equal("CESNET a.l.e.", mod->mod->org));
/* reference 0..1 */
TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
assert_string_equal("RFC7950", mod->mod->ref));
/* revision */
TEST_GENERIC("revision 2018-10-12;}", mod->revs,
assert_string_equal("2018-10-12", mod->revs[0].date));
/* rpc */
TEST_GENERIC("rpc test;}", mod->rpcs,
assert_string_equal("test", mod->rpcs[0].name));
/* typedef */
TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
assert_string_equal("test", mod->typedefs[0].name));
/* uses */
TEST_NODE(LYS_USES, "uses test;}", "test");
/* yang-version */
in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
mod = mod_renew(YCTX);
in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
mod = mod_renew(YCTX);
in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
assert_int_equal(1, mod->version);
mod = mod_renew(YCTX);
in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
assert_int_equal(2, mod->version);
mod = mod_renew(YCTX);
in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
m = calloc(1, sizeof *m);
m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
lysp_yang_ctx_free(ctx_p);
lys_module_free(&fctx, m, 0);
in.current = "prefix " SCHEMA_BEGINNING "}";
m = calloc(1, sizeof *m);
m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
lysp_yang_ctx_free(ctx_p);
lys_module_free(&fctx, m, 0);
in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
m = calloc(1, sizeof *m);
m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
lysp_yang_ctx_free(ctx_p);
lys_module_free(&fctx, m, 0);
/* extensions */
TEST_GENERIC("prefix:test;}", mod->exts,
assert_string_equal("prefix:test", mod->exts[0].name);
assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
mod = mod_renew(YCTX);
/* invalid substatement */
in.current = SCHEMA_BEGINNING "must false;}";
assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
/* submodule */
submod = submod_renew(YCTX);
/* missing mandatory substatements */
in.current = " subname {}";
assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
assert_string_equal("subname", submod->name);
submod = submod_renew(YCTX);
in.current = " subname {belongs-to name {prefix x;}}";
assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
assert_string_equal("name", submod->mod->name);
submod = submod_renew(YCTX);
#undef SCHEMA_BEGINNING
#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
/* duplicated namespace, prefix */
in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
submod = submod_renew(YCTX);
/* not allowed in submodule (module-specific) */
in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
submod = submod_renew(YCTX);
in.current = SCHEMA_BEGINNING "prefix m;}}";
assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
submod = submod_renew(YCTX);
in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_ctx *)YCTX, YCTX->in, &submod));
CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
lysp_yang_ctx_free(ctx_p);
in.current = "prefix " SCHEMA_BEGINNING "}";
assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_ctx *)YCTX, YCTX->in, &submod));
CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
lysp_yang_ctx_free(ctx_p);
submod = submod_renew(YCTX);
#undef TEST_GENERIC
#undef TEST_NODE
#undef TEST_DUP
#undef SCHEMA_BEGINNING
}
static void
test_deviation(void **state)
{
struct lysp_deviation *d = NULL;
/* invalid cardinality */
TEST_DUP_GENERIC(" test {deviate not-supported;", "description", "a", "b", parse_deviation, &d, "1", );
TEST_DUP_GENERIC(" test {deviate not-supported;", "reference", "a", "b", parse_deviation, &d, "1", );
/* missing mandatory substatement */
in.current = " test {description text;}";
assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
/* invalid substatement */
in.current = " test {deviate not-supported; status obsolete;}";
assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
}
static void
test_deviate(void **state)
{
struct lysp_deviate *d = NULL;
/* invalid cardinality */
TEST_DUP_GENERIC("add {", "config", "true", "false", parse_deviate, &d, "1", );
TEST_DUP_GENERIC("add {", "mandatory", "true", "false", parse_deviate, &d, "1", );
TEST_DUP_GENERIC("add {", "max-elements", "1", "2", parse_deviate, &d, "1", );
TEST_DUP_GENERIC("add {", "min-elements", "1", "2", parse_deviate, &d, "1", );
TEST_DUP_GENERIC("add {", "units", "kilometers", "miles", parse_deviate, &d, "1", );
/* invalid substatements */
#define TEST_NOT_SUP(DEV, STMT, VALUE) \
in.current = " "DEV" {"STMT" "VALUE";}..."; \
assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");
TEST_NOT_SUP("not-supported", "units", "meters");
TEST_NOT_SUP("not-supported", "must", "1");
TEST_NOT_SUP("not-supported", "unique", "x");
TEST_NOT_SUP("not-supported", "default", "a");
TEST_NOT_SUP("not-supported", "config", "true");
TEST_NOT_SUP("not-supported", "mandatory", "true");
TEST_NOT_SUP("not-supported", "min-elements", "1");
TEST_NOT_SUP("not-supported", "max-elements", "2");
TEST_NOT_SUP("not-supported", "type", "string");
TEST_NOT_SUP("add", "type", "string");
TEST_NOT_SUP("delete", "config", "true");
TEST_NOT_SUP("delete", "mandatory", "true");
TEST_NOT_SUP("delete", "min-elements", "1");
TEST_NOT_SUP("delete", "max-elements", "2");
TEST_NOT_SUP("delete", "type", "string");
TEST_NOT_SUP("replace", "must", "1");
TEST_NOT_SUP("replace", "unique", "a");
in.current = " nonsence; ...";
assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
assert_null(d);
#undef TEST_NOT_SUP
}
static void
test_container(void **state)
{
struct lysp_node_container *c = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
YCTX->main_ctx = (struct lysp_ctx *)YCTX;
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)c); c = NULL;
TEST_DUP("config", "true", "false");
TEST_DUP("description", "text1", "text2");
TEST_DUP("presence", "true", "false");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content */
in.current = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l {type string;}"
"leaf-list ll {type string;} list li;must 'expr';notification not; presence true; reference test;status current;typedef t {type int8;}uses g;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
CHECK_LYSP_NODE(c, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "cont", 0, LYS_CONTAINER, 0, "test", 1);
assert_non_null(c->actions);
assert_non_null(c->child);
assert_non_null(c->groupings);
assert_non_null(c->musts);
assert_non_null(c->notifs);
assert_string_equal("true", c->presence);
assert_non_null(c->typedefs);
ly_set_erase(&YCTX->tpdfs_nodes, NULL);
ly_set_erase(&YCTX->grps_nodes, NULL);
lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
/* invalid */
in.current = " cont {augment /root;} ...";
assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
in.current = " cont {nonsence true;} ...";
assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
in.current = " cont {action x;} ...";
assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
"the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
}
static void
test_leaf(void **state)
{
struct lysp_node_leaf *l = NULL;
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
TEST_DUP("config", "true", "false");
TEST_DUP("default", "x", "y");
TEST_DUP("description", "text1", "text2");
TEST_DUP("mandatory", "true", "false");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("type", "int8", "uint8");
TEST_DUP("units", "text1", "text2");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content - without mandatory which is mutual exclusive with default */
in.current = "l {config false;default \"xxx\";description test;if-feature f;"
"must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "l", 0, LYS_LEAF, 0, "test", 1);
assert_string_equal("xxx", l->dflt.str);
assert_string_equal("yyy", l->units);
assert_string_equal("string", l->type.name);
assert_non_null(l->musts);
lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
/* full content - now with mandatory */
in.current = "l {mandatory true; type string;} ...";
assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
assert_string_equal("string", l->type.name);
lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
/* invalid */
in.current = " l {description \"missing type\";} ...";
assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
in.current = "l { type iid { path qpud wrong {";
assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
}
static void
test_leaflist(void **state)
{
struct lysp_node_leaflist *ll = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)ll); ll = NULL;
TEST_DUP("config", "true", "false");
TEST_DUP("description", "text1", "text2");
TEST_DUP("max-elements", "10", "20");
TEST_DUP("min-elements", "10", "20");
TEST_DUP("ordered-by", "user", "system");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("type", "int8", "uint8");
TEST_DUP("units", "text1", "text2");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content - without min-elements which is mutual exclusive with default */
in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
"max-elements 10;must 'expr';ordered-by user;reference test;"
"status current;type string; units zzz;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
assert_non_null(ll->dflts);
assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
assert_string_equal("xxx", ll->dflts[0].str);
assert_string_equal("yyy", ll->dflts[1].str);
assert_string_equal("zzz", ll->units);
assert_int_equal(10, ll->max);
assert_int_equal(0, ll->min);
assert_string_equal("string", ll->type.name);
assert_non_null(ll->musts);
assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
/* full content - now with min-elements */
in.current = "ll {min-elements 10; type string;} ...";
assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
assert_string_equal("string", ll->type.name);
assert_int_equal(0, ll->max);
assert_int_equal(10, ll->min);
assert_int_equal(LYS_SET_MIN, ll->flags);
lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
/* invalid */
in.current = " ll {description \"missing type\";} ...";
assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
in.current = " ll {default xx; type string;} ...";
assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
}
static void
test_list(void **state)
{
struct lysp_node_list *l = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
YCTX->main_ctx = (struct lysp_ctx *)YCTX;
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
TEST_DUP("config", "true", "false");
TEST_DUP("description", "text1", "text2");
TEST_DUP("key", "one", "two");
TEST_DUP("max-elements", "10", "20");
TEST_DUP("min-elements", "10", "20");
TEST_DUP("ordered-by", "user", "system");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content */
in.current = "l {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; key l; leaf l {type string;}"
"leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
"status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
0, LYS_LIST, 0, "test", 1);
assert_string_equal("l", l->key);
assert_non_null(l->uniques);
assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
assert_string_equal("xxx", l->uniques[0].str);
assert_string_equal("yyy", l->uniques[1].str);
assert_int_equal(10, l->max);
assert_int_equal(1, l->min);
assert_non_null(l->musts);
ly_set_erase(&YCTX->tpdfs_nodes, NULL);
ly_set_erase(&YCTX->grps_nodes, NULL);
lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
/* invalid content */
PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
in.current = "l {action x;} ...";
assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
}
static void
test_choice(void **state)
{
struct lysp_node_choice *ch = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)ch); ch = NULL;
TEST_DUP("config", "true", "false");
TEST_DUP("default", "a", "b");
TEST_DUP("description", "text1", "text2");
TEST_DUP("mandatory", "true", "false");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content - without default due to a collision with mandatory */
in.current = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;description test;if-feature f;leaf l {type string;}"
"leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
/* full content - the default missing from the previous node */
in.current = "ch {default c;case c;} ...";
assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
assert_string_equal("c", ch->dflt.str);
lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
}
static void
test_case(void **state)
{
struct lysp_node_case *cs = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)cs); cs = NULL;
TEST_DUP("description", "text1", "text2");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content */
in.current = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
"leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
/* invalid content */
in.current = "cs {config true} ...";
assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
}
static void
test_any(void **state, enum ly_stmt kw)
{
struct lysp_node_anydata *any = NULL;
if (kw == LY_STMT_ANYDATA) {
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
} else {
PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
}
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)any); any = NULL;
TEST_DUP("config", "true", "false");
TEST_DUP("description", "text1", "text2");
TEST_DUP("mandatory", "true", "false");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content */
in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
// CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
CHECK_LYSP_NODE(any, "test", 1, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "any", 0, node_type, 0, "test", 1);
assert_non_null(any->musts);
lysp_node_free(&fctx, (struct lysp_node *)any); any = NULL;
}
static void
test_anydata(void **state)
{
test_any(state, LY_STMT_ANYDATA);
}
static void
test_anyxml(void **state)
{
test_any(state, LY_STMT_ANYXML);
}
static void
test_grouping(void **state)
{
struct lysp_node_grp *grp = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
YCTX->main_ctx = (struct lysp_ctx *)YCTX;
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, &grp->node); grp = NULL;
TEST_DUP("description", "text1", "text2");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
#undef TEST_DUP
/* full content */
in.current = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
"leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
assert_non_null(grp);
assert_int_equal(LYS_GROUPING, grp->nodetype);
assert_string_equal("grp", grp->name);
assert_string_equal("test", grp->dsc);
assert_non_null(grp->exts);
assert_string_equal("test", grp->ref);
assert_null(grp->parent);
assert_int_equal(LYS_STATUS_CURR, grp->flags);
ly_set_erase(&YCTX->tpdfs_nodes, NULL);
ly_set_erase(&YCTX->grps_nodes, NULL);
lysp_node_free(&fctx, &grp->node);
grp = NULL;
/* invalid content */
in.current = "grp {config true} ...";
assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
lysp_node_free(&fctx, &grp->node);
grp = NULL;
in.current = "grp {must 'expr'} ...";
assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
lysp_node_free(&fctx, &grp->node);
grp = NULL;
}
static void
test_action(void **state)
{
struct lysp_node_action *rpcs = NULL;
struct lysp_node_container *c = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
YCTX->main_ctx = (struct lysp_ctx *)YCTX;
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)rpcs); rpcs = NULL;
TEST_DUP("description", "text1", "text2");
TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
#undef TEST_DUP
/* full content */
in.current = "top;";
assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
in.current = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
"input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
" list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
"output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
" list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
assert_non_null(rpcs);
assert_int_equal(LYS_ACTION, rpcs->nodetype);
assert_string_equal("func", rpcs->name);
assert_string_equal("test", rpcs->dsc);
assert_non_null(rpcs->exts);
assert_non_null(rpcs->iffeatures);
assert_string_equal("test", rpcs->ref);
assert_non_null(rpcs->groupings);
assert_non_null(rpcs->typedefs);
assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
/* input */
assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
assert_non_null(rpcs->input.groupings);
assert_non_null(rpcs->input.exts);
assert_non_null(rpcs->input.musts);
assert_non_null(rpcs->input.typedefs);
assert_non_null(rpcs->input.child);
/* output */
assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
assert_non_null(rpcs->output.groupings);
assert_non_null(rpcs->output.exts);
assert_non_null(rpcs->output.musts);
assert_non_null(rpcs->output.typedefs);
assert_non_null(rpcs->output.child);
ly_set_erase(&YCTX->tpdfs_nodes, NULL);
ly_set_erase(&YCTX->grps_nodes, NULL);
lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
/* invalid content */
in.current = "func {config true} ...";
assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
lysp_node_free(&fctx, (struct lysp_node *)c);
}
static void
test_notification(void **state)
{
struct lysp_node_notif *notifs = NULL;
struct lysp_node_container *c = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
YCTX->main_ctx = (struct lysp_ctx *)YCTX;
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, ¬ifs)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)notifs); notifs = NULL;
TEST_DUP("description", "text1", "text2");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
#undef TEST_DUP
/* full content */
in.current = "top;";
assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
in.current = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
"leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, ¬ifs));
assert_non_null(notifs);
assert_int_equal(LYS_NOTIF, notifs->nodetype);
assert_string_equal("ntf", notifs->name);
assert_string_equal("test", notifs->dsc);
assert_non_null(notifs->exts);
assert_non_null(notifs->iffeatures);
assert_string_equal("test", notifs->ref);
assert_non_null(notifs->groupings);
assert_non_null(notifs->typedefs);
assert_non_null(notifs->musts);
assert_non_null(notifs->child);
assert_int_equal(LYS_STATUS_CURR, notifs->flags);
ly_set_erase(&YCTX->tpdfs_nodes, NULL);
ly_set_erase(&YCTX->grps_nodes, NULL);
lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
/* invalid content */
in.current = "ntf {config true} ...";
assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, ¬ifs));
CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
lysp_node_free(&fctx, (struct lysp_node *)c);
}
static void
test_uses(void **state)
{
struct lysp_node_uses *u = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node*)u); u = NULL;
TEST_DUP("description", "text1", "text2");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content */
in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
assert_non_null(u->augments);
assert_non_null(u->refines);
lysp_node_free(&fctx, (struct lysp_node *)u); u = NULL;
}
static void
test_augment(void **state)
{
struct lysp_node_augment *a = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
/* invalid cardinality */
#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
TEST_DUP("description", "text1", "text2");
TEST_DUP("reference", "1", "2");
TEST_DUP("status", "current", "obsolete");
TEST_DUP("when", "true", "false");
#undef TEST_DUP
/* full content */
in.current = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
"leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
assert_non_null(a);
assert_int_equal(LYS_AUGMENT, a->nodetype);
assert_string_equal("/target/nodeid", a->nodeid);
assert_string_equal("test", a->dsc);
assert_non_null(a->exts);
assert_non_null(a->iffeatures);
assert_string_equal("test", a->ref);
assert_non_null(a->when);
assert_null(a->parent);
assert_int_equal(LYS_STATUS_CURR, a->flags);
lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
}
static void
test_when(void **state)
{
struct lysp_when *w = NULL;
PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
in.current = "l { description text1;description text2;} ...";
assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
assert_null(w);
CHECK_LOG_CTX("Duplicate keyword \"description\".", "Line number 1.");
in.current = "l { reference 1;reference 2;} ...";
assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
assert_null(w);
CHECK_LOG_CTX("Duplicate keyword \"reference\".", "Line number 1.");
}
static void
test_value(void **state)
{
struct lysp_type_enum enm;
in.current = "-0;";
memset(&enm, 0, sizeof enm);
assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &enm), LY_SUCCESS);
in.current = "-0;";
memset(&enm, 0, sizeof enm);
assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &enm), LY_EVALID);
CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
}
int
main(void)
{
const struct CMUnitTest tests[] = {
UTEST(test_helpers, setup, teardown),
UTEST(test_comments, setup, teardown),
UTEST(test_arg, setup, teardown),
UTEST(test_stmts, setup, teardown),
UTEST(test_minmax, setup, teardown),
UTEST(test_valid_module, setup, teardown),
UTEST(test_module, setup, teardown),
UTEST(test_deviation, setup, teardown),
UTEST(test_deviate, setup, teardown),
UTEST(test_container, setup, teardown),
UTEST(test_leaf, setup, teardown),
UTEST(test_leaflist, setup, teardown),
UTEST(test_list, setup, teardown),
UTEST(test_choice, setup, teardown),
UTEST(test_case, setup, teardown),
UTEST(test_anydata, setup, teardown),
UTEST(test_anyxml, setup, teardown),
UTEST(test_action, setup, teardown),
UTEST(test_notification, setup, teardown),
UTEST(test_grouping, setup, teardown),
UTEST(test_uses, setup, teardown),
UTEST(test_augment, setup, teardown),
UTEST(test_when, setup, teardown),
UTEST(test_value, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|