forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
1441 lines (1282 loc) · 66.8 KB
/
string.c
File metadata and controls
1441 lines (1282 loc) · 66.8 KB
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
/**
* @file string.c
* @author Radek Iša <isa@cesnet.cz>
* @brief test for string 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
*/
#define _UTEST_MAIN_
#include "../utests.h"
/* GLOBAL INCLUDE HEADERS */
#include <ctype.h>
/* LOCAL INCLUDE HEADERS */
#include "libyang.h"
#include "path.h"
#include "plugins_internal.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_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
{ \
struct lyd_node *tree_1; \
struct lyd_node *tree_2; \
char *xml_out, *data; \
data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LY_SUCCESS, tree_1); \
assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_SIBLINGS), 0); \
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2)); \
assert_non_null(tree_2); \
CHECK_LYD(tree_1, tree_2); \
free(xml_out); \
lyd_free_all(tree_1); \
lyd_free_all(tree_2); \
}
#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;
struct lysc_range *range;
/* TEST BASE STRING */
schema = MODULE_CREATE_YANG("base", "leaf port {type string;}");
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_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_STRING, 0, 0);
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, "string", 0, 0, 1, 0, 0, 0);
/* TEST MODULE T0 */
schema = MODULE_CREATE_YANG("T0", "leaf port {type string"
"{length \"10 .. max\";}"
"}");
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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 1, NULL);
assert_int_equal(range->parts[0].min_u64, 10);
assert_true(range->parts[0].max_u64 == 18446744073709551615ull);
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, 0x10, 0, 1, "string", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "10 .. max", NULL, NULL, NULL, 0, NULL);
/* TEST MODULE T1 */
schema = MODULE_CREATE_YANG("T1", "leaf port {type string"
"{length \"min .. 20 | 50\";}"
"}");
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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 0);
assert_int_equal(range->parts[0].max_u64, 20);
assert_int_equal(range->parts[1].min_u64, 50);
assert_int_equal(range->parts[1].max_u64, 50);
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, 0x10, 0, 1, "string", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "min .. 20 | 50", NULL, NULL, NULL, 0, NULL);
/* TEST MODULE T2 */
schema = MODULE_CREATE_YANG("T2", "leaf port {type string"
"{length \"10 .. 20 | 50 .. 100 | 255\";}"
"}");
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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 3, NULL);
assert_int_equal(range->parts[0].min_u64, 10);
assert_int_equal(range->parts[0].max_u64, 20);
assert_int_equal(range->parts[1].min_u64, 50);
assert_int_equal(range->parts[1].max_u64, 100);
assert_int_equal(range->parts[2].min_u64, 255);
assert_int_equal(range->parts[2].max_u64, 255);
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, 0x10, 0, 1, "string", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "10 .. 20 | 50 .. 100 | 255", NULL, NULL, NULL, 0, NULL);
/* SUBTYPE MODULE T2 */
schema = MODULE_CREATE_YANG("TS0",
"typedef my_type {"
" type string {length \"10 .. 20 | 50 .. 100 | 255\";}"
"}"
"leaf port {type my_type {length \"min .. 15 | max\";}}");
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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 10);
assert_int_equal(range->parts[0].max_u64, 15);
assert_int_equal(range->parts[1].min_u64, 255);
assert_int_equal(range->parts[1].max_u64, 255);
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, 0x10, 0, 1, "my_type", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "min .. 15 | max", NULL, NULL, NULL, 0, NULL);
/* ERROR TESTS NEGATIVE VALUE */
schema = MODULE_CREATE_YANG("ERR0", "leaf port {type string {length \"-1 .. 20\";}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EDENIED);
CHECK_LOG_CTX("Invalid length restriction - value \"-1\" does not fit the type limitations.", "/ERR0:port", 0);
schema = MODULE_CREATE_YANG("ERR1", "leaf port {type string {length \"100 .. 18446744073709551616\";}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
CHECK_LOG_CTX("Invalid length restriction - invalid value \"18446744073709551616\".", "/ERR1:port", 0);
schema = MODULE_CREATE_YANG("ERR2", "leaf port {type string {length \"10 .. 20 | 20 .. 30\";}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EEXIST);
CHECK_LOG_CTX("Invalid length restriction - values are not in ascending order (20).", "/ERR2:port", 0);
schema = MODULE_CREATE_YANG("ERR3",
"typedef my_type {"
" type string;"
"}"
"leaf port {type my_type {length \"-1 .. 15\";}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EDENIED);
CHECK_LOG_CTX("Invalid length restriction - value \"-1\" does not fit the type limitations.", "/ERR3:port", 0);
/*
* PATTERN
*/
schema = MODULE_CREATE_YANG("TPATTERN_0", "leaf port {type string"
"{pattern '[a-zA-Z_][a-zA-Z0-9\\-_.]*';}"
"}");
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, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 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_][a-zA-Z0-9\\-_.]*", NULL, NULL, NULL, 0, NULL);
schema = MODULE_CREATE_YANG("TPATTERN_1", "leaf port {type string{"
" pattern '[a-zA-Z_][a-zA-Z0-9\\-_.]*' ;"
" pattern 'abc.*' {modifier invert-match;}"
"}}");
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, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "abc.*", 0, 0x1, 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, 2, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[a-zA-Z_][a-zA-Z0-9\\-_.]*", NULL, NULL, NULL, 0, NULL);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[1]), "\x15" "abc.*", NULL, NULL, NULL, 0, NULL);
schema = MODULE_CREATE_YANG("TPATTERN_2",
"typedef my_type {"
" type string{"
" pattern '[a-zA-Z_][a-zA-Z0-9\\-_.]*' ;"
" pattern 'abc.*' {modifier invert-match;}"
"}}"
"leaf port {type my_type {pattern 'bcd.*';}}");
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, 3);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "abc.*", 0, 0x1, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[2];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "bcd.*", 0, 0x0, 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" "bcd.*", NULL, NULL, NULL, 0, NULL);
/*
* TEST pattern error
*/
schema = MODULE_CREATE_YANG("TPATTERN_ERR_0", "leaf port {type string {"
"pattern '[a-zA-Z_[a-zA-Z0-9\\-_.*';"
"}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
CHECK_LOG_CTX("Regular expression \"[a-zA-Z_[a-zA-Z0-9\\-_.*\" is not valid (\"\": missing terminating ] for character class).",
"/TPATTERN_ERR_0:port", 0);
schema = MODULE_CREATE_YANG("TDEFAULT_0",
"typedef my_type {"
" type string{"
" pattern \"[a-zA-Z_][a-zA-Z0-9\\\\-_.]*\";"
" length \"2 .. 5 | 10\";"
" }"
" default \"a1i-j\";"
"}"
"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, "a1i-j");
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 1, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 2);
assert_int_equal(range->parts[0].max_u64, 5);
assert_int_equal(range->parts[1].min_u64, 10);
assert_int_equal(range->parts[1].max_u64, 10);
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);
/* TEST pattern backslash
* The '[' character is escaped, thus character group is broken.
*/
schema = MODULE_CREATE_YANG("TPATTERN_BC_ERR_1", "leaf port {type string {"
"pattern '\\[a]b';" /* pattern '\[a]b'; */
"}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
CHECK_LOG_CTX("Regular expression \"\\[a]b\" is not valid (\"]b\": character group doesn't begin with '[').",
"/TPATTERN_BC_ERR_1:port", 0);
schema = MODULE_CREATE_YANG("TPATTERN_BC_ERR_2", "leaf port {type string {"
"pattern \"\\\\[a]b\";" /* pattern "\\[a]b"; */
"}}");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
CHECK_LOG_CTX("Regular expression \"\\[a]b\" is not valid (\"]b\": character group doesn't begin with '[').",
"/TPATTERN_BC_ERR_2:port", 0);
/* PATTERN AND LENGTH */
schema = MODULE_CREATE_YANG("TPL_0",
"typedef my_type {"
" type string{"
" length \"2 .. 10\";"
" }"
"}"
"leaf port {type my_type{ pattern \"[a-zA-Z_][a-zA-Z0-9\\\\-_.]*\";}}");
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, 1, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 1, NULL);
assert_int_equal(range->parts[0].min_u64, 2);
assert_int_equal(range->parts[0].max_u64, 10);
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-zA-Z_][a-zA-Z0-9\\-_.]*",
NULL, NULL, NULL, 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;
struct lysc_range *range;
/* TEST BASE STRING */
schema = MODULE_CREATE_YIN("base", "<leaf name=\"port\"> <type name=\"string\"/> </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_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_STRING, 0, 0);
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, "string", 0, 0, 1, 0, 0, 0);
/* TEST MODULE T0 */
schema = MODULE_CREATE_YIN("T0", "<leaf name=\"port\"> <type name=\"string\">"
"<length value=\"10 .. max\"/>"
"</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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 1, NULL);
assert_int_equal(range->parts[0].min_u64, 10);
assert_true(range->parts[0].max_u64 == 18446744073709551615ull);
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, 0x10, 0, 1, "string", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "10 .. max", NULL, NULL, NULL, 0, NULL);
/* TEST MODULE T1 */
schema = MODULE_CREATE_YIN("T1", "<leaf name=\"port\"> <type name=\"string\">"
" <length value=\"min .. 20 | 50\"/>"
"</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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 0);
assert_int_equal(range->parts[0].max_u64, 20);
assert_int_equal(range->parts[1].min_u64, 50);
assert_int_equal(range->parts[1].max_u64, 50);
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, 0x10, 0, 1, "string", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "min .. 20 | 50", NULL, NULL, NULL, 0, NULL);
/* TEST MODULE T2 */
schema = MODULE_CREATE_YIN("T2", "<leaf name=\"port\"> <type name=\"string\">"
"<length value=\"10 .. 20 | 50 .. 100 | 255\"/>"
"</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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 3, NULL);
assert_int_equal(range->parts[0].min_u64, 10);
assert_int_equal(range->parts[0].max_u64, 20);
assert_int_equal(range->parts[1].min_u64, 50);
assert_int_equal(range->parts[1].max_u64, 100);
assert_int_equal(range->parts[2].min_u64, 255);
assert_int_equal(range->parts[2].max_u64, 255);
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, 0x10, 0, 1, "string", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "10 .. 20 | 50 .. 100 | 255", NULL, NULL, NULL, 0, NULL);
/* SUBTYPE MODULE T2 */
schema = MODULE_CREATE_YIN("TS0",
"<typedef name=\"my_type\">"
" <type name=\"string\"> <length value=\"10 .. 20 | 50 .. 100 | 255\"/> </type>"
"</typedef>"
"<leaf name=\"port\"> <type name=\"my_type\">"
" <length value=\"min .. 15 | max\"/>"
"</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, 1, 0);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 10);
assert_int_equal(range->parts[0].max_u64, 15);
assert_int_equal(range->parts[1].min_u64, 255);
assert_int_equal(range->parts[1].max_u64, 255);
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, 0x10, 0, 1, "my_type", 0, 0, 1, 0, 0, 0);
CHECK_LYSP_RESTR(lysp_leaf->type.length, "min .. 15 | max", NULL, NULL, NULL, 0, NULL);
/* ERROR TESTS NEGATIVE VALUE */
schema = MODULE_CREATE_YIN("ERR0", "<leaf name=\"port\"> <type name=\"string\">"
"<length value =\"-1 .. 20\"/> </type></leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EDENIED);
CHECK_LOG_CTX("Invalid length restriction - value \"-1\" does not fit the type limitations.", "/ERR0:port", 0);
schema = MODULE_CREATE_YIN("ERR1", "<leaf name=\"port\"> <type name=\"string\">"
"<length value=\"100 .. 18446744073709551616\"/>"
"</type> </leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
CHECK_LOG_CTX("Invalid length restriction - invalid value \"18446744073709551616\".", "/ERR1:port", 0);
schema = MODULE_CREATE_YIN("ERR2", "<leaf name=\"port\">"
"<type name=\"string\"> <length value=\"10 .. 20 | 20 .. 30\"/>"
"</type> </leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EEXIST);
CHECK_LOG_CTX("Invalid length restriction - values are not in ascending order (20).", "/ERR2:port", 0);
schema = MODULE_CREATE_YIN("ERR3",
"<typedef name=\"my_type\"> <type name=\"string\"/> </typedef>"
"<leaf name=\"port\"> <type name=\"my_type\"> <length value=\"-1 .. 15\"/>"
"</type> </leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EDENIED);
CHECK_LOG_CTX("Invalid length restriction - value \"-1\" does not fit the type limitations.", "/ERR3:port", 0);
/*
* PATTERN
*/
schema = MODULE_CREATE_YIN("TPATTERN_0", "<leaf name=\"port\"> <type name=\"string\">"
"<pattern value=\"[a-zA-Z_][a-zA-Z0-9\\-_.]*\"/>"
"</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, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 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_][a-zA-Z0-9\\-_.]*", NULL, NULL, NULL, 0, NULL);
schema = MODULE_CREATE_YIN("TPATTERN_1", "<leaf name=\"port\"> <type name=\"string\">"
" <pattern value=\"[a-zA-Z_][a-zA-Z0-9\\-_.]*\"/>"
" <pattern value=\"abc.*\"> <modifier value=\"invert-match\"/> </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, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "abc.*", 0, 0x1, 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, 2, 1, 0, 0, 0);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[0]), "\x6" "[a-zA-Z_][a-zA-Z0-9\\-_.]*", NULL, NULL, NULL, 0, NULL);
CHECK_LYSP_RESTR(&(lysp_leaf->type.patterns[1]), "\x15" "abc.*", NULL, NULL, NULL, 0, NULL);
schema = MODULE_CREATE_YIN("TPATTERN_2",
"<typedef name=\"my_type\">"
" <type name=\"string\">"
" <pattern value=\"[a-zA-Z_][a-zA-Z0-9\\-_.]*\"/>"
" <pattern value=\"abc.*\"> <modifier value=\"invert-match\"/> </pattern>"
"</type> </typedef>"
"<leaf name=\"port\"><type name=\"my_type\"> <pattern value=\"bcd.*\"/> </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, 3);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[1];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "abc.*", 0, 0x1, NULL);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[2];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "bcd.*", 0, 0x0, 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" "bcd.*", NULL, NULL, NULL, 0, NULL);
/*
* TEST pattern error
* */
schema = MODULE_CREATE_YIN("TPATTERN_ERR_0",
"<leaf name=\"port\"> <type name=\"string\">"
" <pattern value=\"[a-zA-Z_][a-zA-Z0-9\\-_.*\"/>"
"</type> </leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
CHECK_LOG_CTX("Regular expression \"[a-zA-Z_][a-zA-Z0-9\\-_.*\" is not valid (\"\": missing terminating ] for character class).",
"/TPATTERN_ERR_0:port", 0);
/*
* DEFAUT VALUE
*/
schema = MODULE_CREATE_YIN("TDEFAULT_0",
"<typedef name=\"my_type\">"
" <type name=\"string\">"
" <pattern value=\"[a-zA-Z_][a-zA-Z0-9\\-_.]*\"/>"
" <length value=\"2 .. 5 | 10\"/>"
" </type>"
" <default value=\"a1i-j\"/>"
"</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, "a1i-j");
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 1, 1);
pattern = ((struct lysc_type_str *)lysc_leaf->type)->patterns[0];
CHECK_LYSC_PATTERN(pattern, NULL, NULL, NULL, "[a-zA-Z_][a-zA-Z0-9\\-_.]*", 0, 0, NULL);
range = ((struct lysc_type_str *)lysc_leaf->type)->length;
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 2);
assert_int_equal(range->parts[0].max_u64, 5);
assert_int_equal(range->parts[1].min_u64, 10);
assert_int_equal(range->parts[1].max_u64, 10);
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);
schema = MODULE_CREATE_YIN("TDEFAULT_1",
"<typedef name=\"my_type\">"
" <type name=\"string\">"
" </type>"
" <default value=\"a1i-j<\"/>"
"</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, "a1i-j<");
CHECK_LYSC_TYPE_STR((struct lysc_type_str *)lysc_leaf->type, 0, 0, 0);
CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 2, NULL);
assert_int_equal(range->parts[0].min_u64, 2);
assert_int_equal(range->parts[0].max_u64, 5);
assert_int_equal(range->parts[1].min_u64, 10);
assert_int_equal(range->parts[1].max_u64, 10);
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);
schema = MODULE_CREATE_YIN("TDEFAULT_2",
"<typedef name=\"my_type\">"
" <type name=\"string\">"
" <length value=\"2\"/>"
" </type>"
" <default value=\"a1i-j<\"/>"
"</typedef>"
"<leaf name=\"port\"> <type name=\"my_type\"/> </leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
CHECK_LOG_CTX("Invalid default - value does not fit the type (Unsatisfied length - string \"a1i-j<\" length is not allowed.).",
"/TDEFAULT_2:port", 0);
schema = MODULE_CREATE_YIN("TDEFAULT_3",
"<typedef name=\"my_type\">"
" <default value=\"a1i-j<\"/>"
" <type name=\"string\">"
"</type> </typedef>"
"<leaf name=\"port\"><type name=\"my_type\"> <pattern value=\"bcd.*\"/> </type></leaf>");
UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
CHECK_LOG_CTX("Invalid default - value does not fit the type (Unsatisfied pattern - \"a1i-j<\" does not match \"bcd.*\".).",
"/TDEFAULT_3:port", 0);
}
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 {"
"length \"min .. 20 | 50\";"
"pattern \"p.*\\\\\\\\\";"
"pattern 'p4.*' {modifier invert-match;}"
"}default \"p\\\"<\\\\\";}");
schema_yin = MODULE_CREATE_YIN("PRINT0",
" <leaf name=\"port\">\n"
" <type name=\"string\">\n"
" <length value=\"min .. 20 | 50\"/>\n"
" <pattern value=\"p.*\\\\\"/>\n"
" <pattern value=\"p4.*\">\n"
" <modifier value=\"invert-match\"/>\n"
" </pattern>\n"
" </type>\n"
" <default value=\"p"<\\\"/>\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"
" length \"min .. 20 | 50\";\n"
" pattern \"p.*\\\\\\\\\";\n"
" pattern \"p4.*\" {\n"
" modifier invert-match;\n"
" }\n"
" }\n"
" default \"p\\\"<\\\\\";\n"
" }\n");
schema_yin = MODULE_CREATE_YIN("PRINT1",
" <leaf name=\"port\">\n"
" <type name=\"string\">\n"
" <length value=\"min .. 20 | 50\"/>\n"
" <pattern value=\"p.*\\\\\"/>\n"
" <pattern value=\"p4.*\">\n"
" <modifier value=\"invert-match\"/>\n"
" </pattern>\n"
" </type>\n"
" <default value=\"p"<\\\"/>\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;
struct lyd_node *tree;
const char *data;
struct lysc_node_container *lysc_root;
struct lyd_node_inner *lyd_root;
/* NO RESTRICTION TESTS */
schema = MODULE_CREATE_YANG("T0", "leaf port {type string;}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
/* space on start and new line */
TEST_SUCCESS_XML("T0", " 50 \n\t 64", STRING, " 50 \n\t 64");
/* nuber as string */
TEST_SUCCESS_XML("T0", "50", STRING, "50");
TEST_SUCCESS_XML("T0", "+250", STRING, "+250");
/* references */
TEST_SUCCESS_XML("T0", """, STRING, "\"");
TEST_SUCCESS_XML("T0", "|&|", STRING, "|&|");
TEST_SUCCESS_XML("T0", "'", STRING, "'");
TEST_SUCCESS_XML("T0", "<", STRING, "<");
TEST_SUCCESS_XML("T0", ">", STRING, ">");
TEST_SUCCESS_XML("T0", "ЯЯ", STRING, "ЯЯ");
/* special characters */
TEST_SUCCESS_XML("T0", "\"", STRING, "\"");
TEST_SUCCESS_XML("T0", "'", STRING, "'");
TEST_SUCCESS_XML("T0", ">", STRING, ">");
TEST_SUCCESS_XML("T0", "", STRING, "");
TEST_SUCCESS_XML("T0", "&<lt;", STRING, "&<lt;");
/* CDATA IS NOT SUPPORTED
* TEST_SUCCESS_XML("T2", "<![CDATA[<greeting>Hello, world! & Wecome</greeting>]]>", STRING,
* "<greeting>Hello, world! & Wecome</greeting>");
* COMMENT IN MIDDLE OF TEXT IS NOT SUPPORTED
* TEST_SUCCESS_XML("T2", "this isn't <!--' this is comment '-->comment",
* STRING, "this isn't comment");
*/
/* error */
TEST_ERROR_XML("T0", "< df");
CHECK_LOG_CTX("Child element \"df\" inside a terminal node \"port\" found.", "/T0:port", 1);
TEST_ERROR_XML("T0", "&text;");
CHECK_LOG_CTX("Entity reference \"&text;</po\" not supported, only predefined references allowed.", NULL, 1);
TEST_ERROR_XML("T0", "\"\"");
CHECK_LOG_CTX("Invalid character reference \"\"</port\" (0x00000008).", NULL, 1);
/* TEST INVERTED PATTERN ADN LENGTH */
schema = MODULE_CREATE_YANG("T1", "leaf port {type string {"
" length \"5 .. 10 | 20\";"
" pattern '[a-zA-Z_][a-zA-Z0-9\\-_.<]*' ;"
" pattern 'p4.*' {modifier invert-match;"
" error-app-tag \"pattern-violation\"; error-message \"invalid pattern of value\";}"
"}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
/* inverted value */
TEST_SUCCESS_XML("T1", "abcde", STRING, "abcde");
TEST_SUCCESS_XML("T1", "abcde<", STRING, "abcde<");
TEST_ERROR_XML("T1", "p4abc");
CHECK_LOG_CTX_APPTAG("invalid pattern of value", "/T1:port", 1, "pattern-violation");
/* size 20 */
TEST_SUCCESS_XML("T1", "ahojahojahojahojahoj", STRING, "ahojahojahojahojahoj");
TEST_SUCCESS_XML("T1", "abc-de", STRING, "abc-de");
/* ERROR LENGTH */
TEST_ERROR_XML("T1", "p4a<");
CHECK_LOG_CTX("Unsatisfied length - string \"p4a<\" length is not allowed.", "/T1:port", 1);
/* TEST DEFAULT VALUE */
schema = MODULE_CREATE_YANG("T2",
"container cont {\n"
" leaf port {type string {length \"0 .. 50 | 105\";} default \"test\";}"
"}");
/* using default value */
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
data = "<cont xmlns=\"urn:tests:" "T2" "\">" "</cont>";
CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
lysc_root = (void *)tree->schema;
CHECK_LYSC_NODE(lysc_root->child, NULL, 0, 0x205, 1, "port", 0, LYS_LEAF, 1, 0, 0, 0);
lyd_root = ((struct lyd_node_inner *) tree);
CHECK_LYD_NODE_TERM((struct lyd_node_term *) lyd_root->child, 1, 0, 0, 1, 1,
STRING, "test");\
lyd_free_all(tree);
/* rewriting default value */
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
data = "<cont xmlns=\"urn:tests:T2\">" "<port> 52 </port>" "</cont>";
CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
lysc_root = (void *)tree->schema;
CHECK_LYSC_NODE(lysc_root->child, NULL, 0, 0x205, 1, "port", 0, LYS_LEAF, 1, 0, 0, 0);
lyd_root = ((struct lyd_node_inner *) tree);
CHECK_LYD_NODE_TERM((struct lyd_node_term *) lyd_root->child, 0, 0, 0, 1, 1,
STRING, " 52 ");
lyd_free_all(tree);
/* WHIT STRING TEST */
schema = MODULE_CREATE_YANG("T_WHITE", "leaf port {type string;}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_WHITE", " \n\t", STRING, " \n\t");
TEST_SUCCESS_XML("T_WHITE", " \n<\t", STRING, " \n<\t");
/* UTF-8 length and pattern*/
schema = MODULE_CREATE_YANG("T_UTF8", "leaf port {type string {"
" length \"5 .. 10\";"
" pattern '[€]{5,7}' ;"
"}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UTF8", "€€€€€", STRING, "€€€€€");
TEST_ERROR_XML("T_UTF8", "€€€");
CHECK_LOG_CTX("Unsatisfied length - string \"€€€\" length is not allowed.", "/T_UTF8:port", 1);
TEST_ERROR_XML("T_UTF8", "€€€€€€€€");
CHECK_LOG_CTX("Unsatisfied pattern - \"€€€€€€€€\" does not match \"[€]{5,7}\".", "/T_UTF8:port", 1);
/* ANCHOR TEST ^$ is implicit */
schema = MODULE_CREATE_YANG("T_ANCHOR", "leaf port {type string {"
" pattern 'a.*b' ;"
"}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_ERROR_XML("T_ANCHOR", "abc");
CHECK_LOG_CTX("Unsatisfied pattern - \"abc\" does not match \"a.*b\".", "/T_ANCHOR:port", 1);
TEST_ERROR_XML("T_ANCHOR", "cab");
CHECK_LOG_CTX("Unsatisfied pattern - \"cab\" does not match \"a.*b\".", "/T_ANCHOR:port", 1);
/* Unicode block test 1 - Basic Latin */
schema = MODULE_CREATE_YANG("T_UB_1", "leaf port {type string { pattern '\\p{IsBasicLatin}+';} } ");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_1", "B4s1cLatin!", STRING, "B4s1cLatin!");
/* Unicode block test 2 - Basic Latin within brackets */
schema = MODULE_CREATE_YANG("T_UB_2", "leaf port {type string { pattern '[\\p{IsBasicLatin}]+';} } ");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_2", "B4s1cLatin!", STRING, "B4s1cLatin!");
/* Unicode block test 3 - Latin-1 Supplement */
schema = MODULE_CREATE_YANG("T_UB_3", "leaf port {type string { pattern '[\\p{IsLatin-1Supplement}]+';} } ");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_3", "ÁÉÍÓÖÜ", STRING, "ÁÉÍÓÖÜ");
/* Unicode block test 4 - Latin-1 Supplement */
schema = MODULE_CREATE_YANG("T_UB_4", "leaf port {type string { pattern '[\\p{IsLatin-1Supplement}]+';} } ");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_4", "ÁÉÍÓÖÜ", STRING, "ÁÉÍÓÖÜ");
/* Unicode block test 5 - Latin Extended-A */
schema = MODULE_CREATE_YANG("T_UB_5", "leaf port {type string { pattern '[\\p{IsLatinExtended-A}]+';} } ");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_5", "ŐŰőű", STRING, "ŐŰőű");
/* Unicode block test 6 - Basic Latin, Latin-1 Supplement, and Latin Extended-A */
schema = MODULE_CREATE_YANG("T_UB_6", "leaf port {type string {"
" pattern '[\\p{IsBasicLatin}\\p{IsLatin-1Supplement}\\p{IsLatinExtended-A}]+';"
"}} ");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_6", "Árvíztűrő tükörfúrógép!", STRING, "Árvíztűrő tükörfúrógép!");
/* Unicode block test 7 - Unknown Unicode block */
schema = MODULE_CREATE_YANG("T_UB_7", "leaf port {type string { pattern '\\p{IsUnknownUnicodeBlock}+';} } ");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
CHECK_LOG_CTX("Regular expression \"\\p{IsUnknownUnicodeBlock}+\" "
"is not valid (\"UnknownUnicodeBlock}+\": unknown block name).", "/T_UB_7:port", 0);
/* Unicode block test 8 - Unknown Unicode block with Basic Latin */
schema = MODULE_CREATE_YANG("T_UB_8", "leaf port {type string { "
" pattern '[\\p{IsBasicLatin}\\p{IsUnknownUnicodeBlock}]+';"
"}} ");
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
CHECK_LOG_CTX("Regular expression \"[\\p{IsBasicLatin}\\p{IsUnknownUnicodeBlock}]+\" "
"is not valid (\"UnknownUnicodeBlock}]+\": unknown block name).", "/T_UB_8:port", 0);
schema = MODULE_CREATE_YANG("T_UB_9", "leaf port {type string { pattern "
"'[\\p{IsSpecials}]+';}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_9", "�", STRING, "\xef\xbf\xba\xef\xbf\xbd");
schema = MODULE_CREATE_YANG("T_UB_10", "leaf port {type string { pattern "
"'[\\p{IsSpecials}]+';}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_ERROR_XML("T_UB_10", "￟�");
CHECK_LOG_CTX("Unsatisfied pattern - \"\xef\xbf\x9f\xef\xbf\xbd\" does not match \"[\\p{IsSpecials}]+\".", "/T_UB_10:port", 1);
schema = MODULE_CREATE_YANG("T_UB_11", "leaf port {type string { pattern "
"'[\\p{IsSpecials}]+';}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_XML("T_UB_11", "�", STRING, "\xef\xbb\xbf\xef\xbf\xbd");
}
static void
test_data_json(void **state)
{
const char *schema;
struct lyd_node *tree;
const char *data;
struct lysc_node_container *lysc_root;
struct lyd_node_inner *lyd_root;
/* NO RESTRICTION TESTS */
schema = MODULE_CREATE_YANG("T0", "leaf port {type string;}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_JSON("T0", "this is text", STRING, "this is text");
/* space on start and new line */
TEST_SUCCESS_JSON("T0", " 50 \\n\\t 64", STRING, " 50 \n\t 64");
/* nuber as string */
TEST_SUCCESS_JSON("T0", "50", STRING, "50");
TEST_SUCCESS_JSON("T0", "+250", STRING, "+250");
/* references */
TEST_SUCCESS_JSON("T0", "\\\"", STRING, "\"");
TEST_SUCCESS_JSON("T0", "|&|", STRING, "|&|");
TEST_SUCCESS_JSON("T0", "<", STRING, "<");
TEST_SUCCESS_JSON("T0", ">", STRING, ">");
TEST_SUCCESS_JSON("T0", "\\u042F", STRING, "Я");
TEST_SUCCESS_JSON("T0", "\\u042FFF", STRING, "ЯFF");
/* special characters */
TEST_SUCCESS_JSON("T0", "'", STRING, "'");
TEST_SUCCESS_JSON("T0", ">", STRING, ">");
TEST_SUCCESS_JSON("T0", "", STRING, "");
TEST_SUCCESS_JSON("T0", "\\\" \\\\ \\r \\/ \\n \\t \\u20ac", STRING, "\" \\ \r / \n \t €");
/* ERROR invalid json string */
TEST_ERROR_JSON("T0", "\n");
CHECK_LOG_CTX("Invalid character in JSON string \"\n\" (0x0000000a).", NULL, 1);
/* backspace and form feed are valid JSON escape sequences, but the control characters they represents are not allowed values for YANG string type */
TEST_ERROR_JSON("T0", "\\b");
CHECK_LOG_CTX("Invalid character reference \"\\b\" (0x00000008).", NULL, 1);
TEST_ERROR_JSON("T0", "\\f");
CHECK_LOG_CTX("Invalid character reference \"\\f\" (0x0000000c).", NULL, 1);
TEST_ERROR_JSON("T0", "\"");
CHECK_LOG_CTX("Invalid character sequence \"\"}\", expected a JSON object-end or next item.", NULL, 1);
TEST_ERROR_JSON("T0", "aabb \\x");
CHECK_LOG_CTX("Invalid character escape sequence \\x.", NULL, 1);
/* TEST INVERTED PATTERN ADN LENGTH */
schema = MODULE_CREATE_YANG("T1", "leaf port {type string {"
" length \"5 .. 10 | 20\";"
" pattern '[a-zA-Z_][a-zA-Z0-9\\-_.<]*\\n[a-zA-Z0-9\\-_.<]*' ;"
" pattern 'p4.*\\n' {modifier invert-match;}"
"}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
/* inverted value */
TEST_SUCCESS_JSON("T1", "a\\nbcde", STRING, "a\nbcde");
TEST_ERROR_JSON("T1", "p4abc\\n");
CHECK_LOG_CTX("Unsatisfied pattern - \"p4abc\n\" does not match inverted \"p4.*\\n\".", "/T1:port", 1);
/* size 20 */
TEST_SUCCESS_JSON("T1", "ahojahojaho\\njahojaho", STRING, "ahojahojaho\njahojaho");
TEST_SUCCESS_JSON("T1", "abc\\n-de", STRING, "abc\n-de");
/* ERROR LENGTH */
TEST_ERROR_JSON("T1", "p4a\u042F");
CHECK_LOG_CTX("Unsatisfied length - string \"p4aЯ\" length is not allowed.", "/T1:port", 1);
/* TEST DEFAULT VALUE */
schema = MODULE_CREATE_YANG("T_DEFAULT2",
"container cont {\n"
" leaf port {type string {length \"0 .. 50 | 105\";} default \"test\";}"
"}");
/* using default value */
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
data = "{\"T_DEFAULT2:cont\":{}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
lysc_root = (void *)tree->schema;
CHECK_LYSC_NODE(lysc_root->child, NULL, 0, 0x205, 1, "port", 0, LYS_LEAF, 1, 0, 0, 0);
lyd_root = ((struct lyd_node_inner *) tree);
CHECK_LYD_NODE_TERM((struct lyd_node_term *) lyd_root->child, 1, 0, 0, 1, 1,
STRING, "test");\
lyd_free_all(tree);
/* rewriting default value */
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
data = "{\"T_DEFAULT2:cont\":{\":port\": \" 52 \"}}";\
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
lysc_root = (void *)tree->schema;
CHECK_LYSC_NODE(lysc_root->child, NULL, 0, 0x205, 1, "port", 0, LYS_LEAF, 1, 0, 0, 0);
lyd_root = ((struct lyd_node_inner *) tree);
CHECK_LYD_NODE_TERM((struct lyd_node_term *) lyd_root->child, 0, 0, 0, 1, 1,
STRING, " 52 ");
lyd_free_all(tree);
/* UTF-8 length and pattern*/
schema = MODULE_CREATE_YANG("T_UTF8", "leaf port {type string {"
" length \"5 .. 10\";"
" pattern '[€]{5,7}' ;"
"}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_SUCCESS_JSON("T_UTF8", "€€€€€", STRING, "€€€€€");
TEST_ERROR_JSON("T_UTF8", "€€€");
CHECK_LOG_CTX("Unsatisfied length - string \"€€€\" length is not allowed.", "/T_UTF8:port", 1);
TEST_ERROR_JSON("T_UTF8", "€€€€€€€€");
CHECK_LOG_CTX("Unsatisfied pattern - \"€€€€€€€€\" does not match \"[€]{5,7}\".", "/T_UTF8:port", 1);
/* ANCHOR TEST ^$ is implicit */
schema = MODULE_CREATE_YANG("T_ANCHOR", "leaf port {type string {"
" pattern 'a.*b' ;"
"}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
TEST_ERROR_JSON("T_ANCHOR", "abc");
CHECK_LOG_CTX("Unsatisfied pattern - \"abc\" does not match \"a.*b\".", "/T_ANCHOR:port", 1);
TEST_ERROR_JSON("T_ANCHOR", "cb");
CHECK_LOG_CTX("Unsatisfied pattern - \"cb\" does not match \"a.*b\".", "/T_ANCHOR:port", 1);
}
static void
test_data_lyb(void **state)