forked from riot-ml/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytepattern_test.ml
1273 lines (1209 loc) · 44.5 KB
/
bytepattern_test.ml
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
open Ppxlib
open Ast_helper
open Bytepattern
let keyword fmt = Spices.(default |> fg (color "#00FF00") |> build) fmt
let error fmt = Spices.(default |> fg (color "#FF0000") |> build) fmt
let loc = Location.none
let id name =
let longident = Location.{ loc; txt = Longident.parse name } in
Exp.ident longident
let int n = Exp.constant (Const.int n)
let ocaml str =
let lexbuf = Lexing.from_string ~with_positions:false str in
Parse.expression lexbuf
let () =
let test str expected =
let actual = Lexer.read ~loc str in
let actual_str = Format.asprintf "%a" Lexer.pp actual in
let expect_str = Format.asprintf "%a" Lexer.pp expected in
if String.equal actual_str expect_str then
Format.printf "lexer test %S %s\r\n%!" str (keyword "OK")
else (
Format.printf "%s\n\nExpected:\n\n%a\n\nbut found:\n\n%a\n\n"
(error "Tokens do not match")
Lexer.pp expected Lexer.pp actual;
assert false)
in
test "" [];
test "all" [ IDENT "all" ];
test "all::4" [ IDENT "all"; COLON_COLON; NUMBER 4 ];
test "all::1024" [ IDENT "all"; COLON_COLON; NUMBER 1024 ];
test "all::utf8" [ IDENT "all"; COLON_COLON; IDENT "utf8" ];
test "2112::utf8" [ NUMBER 2112; COLON_COLON; IDENT "utf8" ];
test {|"rush\r\n"::bytes|} [ STRING "rush\r\n"; COLON_COLON; IDENT "bytes" ];
test {|"rush"::utf8|} [ STRING "rush"; COLON_COLON; IDENT "utf8" ];
test "all::bytes(10)"
[ IDENT "all"; COLON_COLON; IDENT "bytes"; EXPRESSION (int 10) ];
test "all::bytes(10),"
[ IDENT "all"; COLON_COLON; IDENT "bytes"; EXPRESSION (int 10); COMMA ];
test "all::bytes(foo)"
[ IDENT "all"; COLON_COLON; IDENT "bytes"; EXPRESSION (id "foo") ];
test "all::bytes(foo 1234)"
[ IDENT "all"; COLON_COLON; IDENT "bytes"; EXPRESSION (ocaml "foo 1234") ];
test "all::bytes(foo ())"
[ IDENT "all"; COLON_COLON; IDENT "bytes"; EXPRESSION (ocaml "foo ()") ];
test "all::bytes( (foo ()) * 8 / ( 10 - 1 ) )"
[
IDENT "all";
COLON_COLON;
IDENT "bytes";
EXPRESSION (ocaml "((foo ()) * 8) / (10 - 1)");
];
test "all::bytes(10), rest::binary"
[
IDENT "all";
COLON_COLON;
IDENT "bytes";
EXPRESSION (int 10);
COMMA;
IDENT "rest";
COLON_COLON;
IDENT "binary";
];
test
{| fin::1, comp::1, _rsv::2, 1::4 , 0::1, 127::7 , len ::64
,
mask::32
,payload::bytes(
len
)
|}
[
IDENT "fin";
COLON_COLON;
NUMBER 1;
COMMA;
IDENT "comp";
COLON_COLON;
NUMBER 1;
COMMA;
IDENT "_rsv";
COLON_COLON;
NUMBER 2;
COMMA;
NUMBER 1;
COLON_COLON;
NUMBER 4;
COMMA;
NUMBER 0;
COLON_COLON;
NUMBER 1;
COMMA;
NUMBER 127;
COLON_COLON;
NUMBER 7;
COMMA;
IDENT "len";
COLON_COLON;
NUMBER 64;
COMMA;
IDENT "mask";
COLON_COLON;
NUMBER 32;
COMMA;
IDENT "payload";
COLON_COLON;
IDENT "bytes";
EXPRESSION (id "len");
];
()
let () =
let open Parser in
let test str expected =
let actual = Parser.parse ~loc str in
let actual_str = Format.asprintf "%a" Parser.pp actual in
let expect_str = Format.asprintf "%a" Parser.pp expected in
if String.equal actual_str expect_str then
Format.printf "parser test %S %s\r\n%!" str (keyword "OK")
else (
Format.printf "%s\n\nExpected:\n\n%a\n\nbut found:\n\n%a\n\n"
(error "Parse trees do not match")
Parser.pp expected Parser.pp actual;
assert false)
in
test "" [];
test "all" [ Bind { name = "all"; size = Rest } ];
test "all::8" [ Bind { name = "all"; size = Fixed_bits 8 } ];
test "all::1024" [ Bind { name = "all"; size = Fixed_bits 1024 } ];
test "all::utf8" [ Bind { name = "all"; size = Utf8 } ];
test "all::bytes" [ Bind { name = "all"; size = Rest } ];
test "all::bytes(10)" [ Bind { name = "all"; size = Dynamic_bytes (int 10) } ];
test "all::bytes(foo ())"
[ Bind { name = "all"; size = Dynamic_bytes (ocaml "foo ()") } ];
test "all::bytes( (foo ()) * 8 / ( 10 - 1 ) )"
[
Bind
{
name = "all";
size = Dynamic_bytes (ocaml "((foo ()) * 8) / (10 - 1)");
};
];
test "2112::utf8" [ Expect { value = Number 2112; size = Utf8 } ];
test {|"rush"|} [ Expect { value = String "rush"; size = Rest } ];
test {|"rush\r\n"::bytes|}
[ Expect { value = String "rush\r\n"; size = Rest } ];
test {|"rush"::utf8|} [ Expect { value = String "rush"; size = Utf8 } ];
test "len::8, body::bytes(len)"
[
Bind { name = "len"; size = Fixed_bits 8 };
Bind { name = "body"; size = Dynamic_bytes (id "len") };
];
test "one::8, all::bytes"
[
Bind { name = "one"; size = Fixed_bits 8 };
Bind { name = "all"; size = Rest };
];
test
{| fin::1, comp::1, _rsv::2, 1::4, 0::1, 127::7, len::bytes(8), mask::32, payload::bytes(len), rest |}
[
bind "fin" (Fixed_bits 1);
bind "comp" (Fixed_bits 1);
bind "_rsv" (Fixed_bits 2);
expect (Number 1) (Fixed_bits 4);
expect (Number 0) (Fixed_bits 1);
expect (Number 127) (Fixed_bits 7);
bind "len" (Dynamic_bytes (int 8));
bind "mask" (Fixed_bits 32);
bind "payload" (Dynamic_bytes (id "len"));
bind "rest" Rest;
];
()
(**
Lowered representation of the pattern language for Constructing matches
*)
let () =
let open Construction_lower in
let test str expected =
let actual = parse ~loc str |> Construction_lower.lower ~loc in
let actual_str = Format.asprintf "%a" Construction_lower.pp actual in
let expect_str = Format.asprintf "%a" Construction_lower.pp expected in
if String.equal actual_str expect_str then
Format.printf "cstr-low test %S %s\r\n%!" str (keyword "OK")
else (
Format.printf "%s\n\nExpected:\n\n%a\n\nbut found:\n\n%a\n\n"
(error "Construction_lowered trees do not match")
Construction_lower.pp expected Construction_lower.pp actual;
assert false)
in
test "" [ Empty ];
test "all" [ Bypass "all" ];
test "all::8"
[
Create_transient "_trns";
Add_next_fixed_bits { src = "all"; size = 8 };
Commit_transient "_trns";
];
test "all::1024"
[
Create_transient "_trns";
Add_next_fixed_bits { src = "all"; size = 1024 };
Commit_transient "_trns";
];
test "all::utf8"
[
Create_transient "_trns";
Add_next_utf8 { src = "all" };
Commit_transient "_trns";
];
test "all::bytes" [ Bypass "all" ];
test "all::bytes(10)"
[
Create_transient "_trns";
Add_next_dynamic_bytes { src = "all"; expr = int 10 };
Commit_transient "_trns";
];
test "all::bytes(foo ())"
[
Create_transient "_trns";
Add_next_dynamic_bytes { src = "all"; expr = ocaml "foo ()" };
Commit_transient "_trns";
];
test "all::bytes( (foo ()) * 8 / ( 10 - 1 ) )"
[
Create_transient "_trns";
Add_next_dynamic_bytes
{ src = "all"; expr = ocaml "((foo ()) * 8) / (10 - 1)" };
Commit_transient "_trns";
];
test "2112::1"
[
Create_transient "_trns";
Add_int_fixed_bits { value = 2112; size = 1 };
Commit_transient "_trns";
];
test "2112::bits(1234)"
[
Create_transient "_trns";
Add_int_dynamic_bits { value = 2112; expr = int 1234 };
Commit_transient "_trns";
];
test "2112::bytes(1234)"
[
Create_transient "_trns";
Add_int_dynamic_bytes { value = 2112; expr = int 1234 };
Commit_transient "_trns";
];
test {|"rush"::utf8|}
[
Create_transient "_trns";
Add_string_utf8 { value = "rush" };
Commit_transient "_trns";
];
test {|"rush"::bytes|}
[
Create_transient "_trns";
Add_string_bytes { value = "rush" };
Commit_transient "_trns";
];
test {|"rush"::bytes(3)|}
[
Create_transient "_trns";
Add_string_dynamic_bytes { value = "rush"; expr = int 3 };
Commit_transient "_trns";
];
test "len::8, body::bytes(len)"
[
Create_transient "_trns";
Add_next_fixed_bits { src = "len"; size = 8 };
Add_next_dynamic_bytes { src = "body"; expr = id "len" };
Commit_transient "_trns";
];
test "one::8, all::bytes"
[
Create_transient "_trns";
Add_next_fixed_bits { src = "one"; size = 8 };
Add_rest { src = "all" };
Commit_transient "_trns";
];
test
{| fin::1, comp::1, _rsv::2,
1::4, 0::1, 127::7,
len::bytes(8), mask::32,
payload::bytes(len), rest |}
[
Create_transient "_trns";
Add_next_fixed_bits { src = "fin"; size = 1 };
Add_next_fixed_bits { src = "comp"; size = 1 };
Add_next_fixed_bits { src = "_rsv"; size = 2 };
Add_int_fixed_bits { value = 1; size = 4 };
Add_int_fixed_bits { value = 0; size = 1 };
Add_int_fixed_bits { value = 127; size = 7 };
Add_next_dynamic_bytes { src = "len"; expr = int 8 };
Add_next_fixed_bits { src = "mask"; size = 32 };
Add_next_dynamic_bytes { src = "payload"; expr = id "len" };
Add_rest { src = "rest" };
Commit_transient "_trns";
];
()
(**
Bytestring construction tests
*)
let () =
let test str expected =
let lower = Bytepattern.parse ~loc str in
let actual = lower |> Bytepattern.to_transient_builder ~loc in
let actual = Ppxlib.Pprintast.string_of_expression actual in
let expected = Ppxlib.Pprintast.string_of_expression expected in
if not (String.equal actual expected) then (
Format.printf "%s\n\nExpected:\n\n%s\n\nbut found:\n\n%s\n\n"
(error "Construction ASTs do not match")
expected actual;
assert false)
else Format.printf "transl-low test %S %s\r\n%!" str (keyword "OK")
in
(* test: empty pattern is just an empty bytestring *)
test {| |} [%expr Bytestring.empty];
test {| all |} [%expr all];
test {| all::bytes |} [%expr all];
test {| all::8 |}
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_bits _trns ~size:8 all;
Bytestring.Transient.commit _trns];
test "all::1024"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_bits _trns ~size:1024 all;
Bytestring.Transient.commit _trns];
test "all::utf8"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_utf8 _trns all;
Bytestring.Transient.commit _trns];
test "all::bytes(10)"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_string _trns ~size:10 all;
Bytestring.Transient.commit _trns];
test "all::bytes(foo ())"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_string _trns ~size:(foo ()) all;
Bytestring.Transient.commit _trns];
test "all::bytes( (foo ()) * 8 / ( 10 - 1 ) )"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_string _trns ~size:(foo () * 8 / (10 - 1)) all;
Bytestring.Transient.commit _trns];
test "len::8 , body::bytes(len)"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_bits _trns ~size:8 len;
Bytestring.Transient.add_string _trns ~size:len body;
Bytestring.Transient.commit _trns];
test "one::8, all ::bytes"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_bits _trns ~size:8 one;
Bytestring.Transient.add_string _trns all;
Bytestring.Transient.commit _trns];
test "2112::1"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_int _trns ~size:1 2112;
Bytestring.Transient.commit _trns];
test "2112::bits(1234)"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_int _trns ~size:1234 2112;
Bytestring.Transient.commit _trns];
test "2112::bytes(1234)"
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_int _trns ~size:(1234 * 8) 2112;
Bytestring.Transient.commit _trns];
test {|"rush"|}
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_string _trns "rush";
Bytestring.Transient.commit _trns];
test {|"rush"::utf8|}
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_utf8 _trns "rush";
Bytestring.Transient.commit _trns];
test {|"rush"::bytes|}
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_string _trns "rush";
Bytestring.Transient.commit _trns];
test {|"rush"::bytes(3)|}
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_literal_string _trns ~size:3 "rush";
Bytestring.Transient.commit _trns];
test
{| fin::1, comp::1, _rsv::2,
1::4, 0::1, 127::7,
len::bits(8*8), mask::32,
payload::bytes(len), rest |}
[%expr
let _trns = Bytestring.Transient.create () in
Bytestring.Transient.add_bits _trns ~size:1 fin;
Bytestring.Transient.add_bits _trns ~size:1 comp;
Bytestring.Transient.add_bits _trns ~size:2 _rsv;
Bytestring.Transient.add_literal_int _trns ~size:4 1;
Bytestring.Transient.add_literal_int _trns ~size:1 0;
Bytestring.Transient.add_literal_int _trns ~size:7 127;
Bytestring.Transient.add_bits _trns ~size:(8 * 8) len;
Bytestring.Transient.add_bits _trns ~size:32 mask;
Bytestring.Transient.add_string _trns ~size:len payload;
Bytestring.Transient.add_string _trns rest;
Bytestring.Transient.commit _trns];
()
let () =
let open Matching_lower in
let test str expected =
let actual = parse ~loc str |> Matching_lower.lower ~loc in
let actual_str = Format.asprintf "%a" Matching_lower.pp actual in
let expect_str = Format.asprintf "%a" Matching_lower.pp expected in
if String.equal actual_str expect_str then
Format.printf "match-low test %S %s\r\n%!" str (keyword "OK")
else (
Format.printf "%s\n\nExpected:\n\n%a\n\nbut found:\n\n%a\n\n"
(error "Matching_lower trees do not match")
Matching_lower.pp expected Matching_lower.pp actual;
assert false)
in
test "" [ Empty "_data_src" ];
test "all" [ Bypass { src = "_data_src"; name = "all" } ];
test "all::8"
[
Create_iterator "_data_src";
Bind_next_fixed_bits { src = "all"; size = 8; iter = "_data_src" };
Empty "_data_src";
];
test "all::1024"
[
Create_iterator "_data_src";
Bind_next_fixed_bits { src = "all"; size = 1024; iter = "_data_src" };
Empty "_data_src";
];
test "all::utf8"
[
Create_iterator "_data_src";
Bind_next_utf8 { src = "all"; iter = "_data_src" };
Empty "_data_src";
];
test "all::bytes" [ Bypass { src = "_data_src"; name = "all" } ];
test "all::bytes(10)"
[
Create_iterator "_data_src";
Bind_next_dynamic_bytes { src = "all"; expr = int 10; iter = "_data_src" };
Empty "_data_src";
];
test "all::bytes(foo ())"
[
Create_iterator "_data_src";
Bind_next_dynamic_bytes
{ src = "all"; expr = ocaml "foo ()"; iter = "_data_src" };
Empty "_data_src";
];
test "all::bytes( (foo ()) * 8 / ( 10 - 1 ) )"
[
Create_iterator "_data_src";
Bind_next_dynamic_bytes
{
src = "all";
expr = ocaml "((foo ()) * 8) / (10 - 1)";
iter = "_data_src";
};
Empty "_data_src";
];
test "2112::1"
[
Create_iterator "_data_src";
Expect_int_fixed_bits { value = 2112; size = 1; iter = "_data_src" };
Empty "_data_src";
];
test "2112::bits(1234)"
[
Create_iterator "_data_src";
Expect_int_dynamic_bits
{ value = 2112; expr = int 1234; iter = "_data_src" };
Empty "_data_src";
];
test "2112::bytes(1234)"
[
Create_iterator "_data_src";
Expect_int_dynamic_bytes
{ value = 2112; expr = int 1234; iter = "_data_src" };
Empty "_data_src";
];
test {|"rush"::utf8|}
[
Create_iterator "_data_src";
Expect_string_utf8 { value = "rush"; iter = "_data_src" };
Empty "_data_src";
];
test {|"rush"::bytes|}
[
Create_iterator "_data_src";
Expect_string_bytes { value = "rush"; iter = "_data_src" };
Empty "_data_src";
];
test {|"rush"::bytes(3)|}
[
Create_iterator "_data_src";
Expect_string_dynamic_bytes
{ value = "rush"; expr = int 3; iter = "_data_src" };
Empty "_data_src";
];
test "len::8, body::bytes(len)"
[
Create_iterator "_data_src";
Bind_next_fixed_bits { src = "len"; size = 8; iter = "_data_src" };
Bind_next_dynamic_bytes
{ src = "body"; expr = id "len"; iter = "_data_src" };
Empty "_data_src";
];
test "one::8, all::bytes"
[
Create_iterator "_data_src";
Bind_next_fixed_bits { src = "one"; size = 8; iter = "_data_src" };
Bind_rest { src = "all"; iter = "_data_src" };
Empty "_data_src";
];
test
{| fin::1, comp::1, _rsv::2,
1::4, 0::1, 127::7,
len::bytes(8), mask::32,
payload::bytes(len), rest |}
[
Create_iterator "_data_src";
Bind_next_fixed_bits { src = "fin"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits { src = "comp"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits { src = "_rsv"; size = 2; iter = "_data_src" };
Expect_int_fixed_bits { value = 1; size = 4; iter = "_data_src" };
Expect_int_fixed_bits { value = 0; size = 1; iter = "_data_src" };
Expect_int_fixed_bits { value = 127; size = 7; iter = "_data_src" };
Bind_next_dynamic_bytes { src = "len"; expr = int 8; iter = "_data_src" };
Bind_next_fixed_bits { src = "mask"; size = 32; iter = "_data_src" };
Bind_next_dynamic_bytes
{ src = "payload"; expr = id "len"; iter = "_data_src" };
Bind_rest { src = "rest"; iter = "_data_src" };
Empty "_data_src";
];
()
(**
Bytestring matching tests
*)
let () =
let loc = Location.none in
let test str expected =
let lower = Bytepattern.parse ~loc str in
let actual = lower |> Bytepattern.to_pattern_match ~loc ~body:[%expr ()] in
let actual = Ppxlib.Pprintast.string_of_expression actual in
let expected = Ppxlib.Pprintast.string_of_expression expected in
if not (String.equal actual expected) then (
Format.printf "%s\n\nExpected:\n\n%s\n\nbut found:\n\n%s\n\n"
(error "Native matching trees do not match")
expected actual;
assert false)
else Format.printf "match-native test %S %s\r\n%!" str (keyword "OK")
in
(* test: empty pattern is just an empty bytestring *)
test {| |}
[%expr
Bytestring.Iter.expect_empty _data_src;
()];
test {| all |}
[%expr
let all = _data_src in
()];
test {| all::bytes |}
[%expr
let all = _data_src in
()];
test {| all::8 |}
[%expr
let _data_src = Bytestring.to_iter _data_src in
let all = Bytestring.Iter.next_bits ~size:8 _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "all::1024"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let all = Bytestring.Iter.next_bits ~size:1024 _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "all::utf8"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let all = Bytestring.Iter.next_utf8 _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "all::bytes(10)"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let all = Bytestring.Iter.next_bytes ~size:10 _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "all::bytes(foo ())"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let all = Bytestring.Iter.next_bytes ~size:(foo ()) _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "all::bytes( (foo ()) * 8 / ( 10 - 1 ) )"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let all =
Bytestring.Iter.next_bytes ~size:(foo () * 8 / (10 - 1)) _data_src
in
Bytestring.Iter.expect_empty _data_src;
()];
test "len::8 , body::bytes(len)"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let len = Bytestring.Iter.next_bits ~size:8 _data_src in
let body = Bytestring.Iter.next_bytes ~size:len _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "one::8, all ::bytes"
[%expr
let _data_src = Bytestring.to_iter _data_src in
let one = Bytestring.Iter.next_bits ~size:8 _data_src in
let all = Bytestring.Iter.rest _data_src in
Bytestring.Iter.expect_empty _data_src;
()];
test "2112::1"
[%expr
let _data_src = Bytestring.to_iter _data_src in
Bytestring.Iter.expect_literal_int _data_src ~size:1 2112;
Bytestring.Iter.expect_empty _data_src;
()];
test "2112::bits(1234)"
[%expr
let _data_src = Bytestring.to_iter _data_src in
Bytestring.Iter.expect_literal_int _data_src ~size:1234 2112;
Bytestring.Iter.expect_empty _data_src;
()];
test "2112::bytes(1234)"
[%expr
let _data_src = Bytestring.to_iter _data_src in
Bytestring.Iter.expect_literal_int _data_src ~size:(1234 * 8) 2112;
Bytestring.Iter.expect_empty _data_src;
()];
test {|"rush"::utf8|}
[%expr
let _data_src = Bytestring.to_iter _data_src in
Bytestring.Iter.expect_literal_utf8 _data_src "rush";
Bytestring.Iter.expect_empty _data_src;
()];
test {|"rush"::bytes|}
[%expr
let _data_src = Bytestring.to_iter _data_src in
Bytestring.Iter.expect_literal_string _data_src "rush";
Bytestring.Iter.expect_empty _data_src;
()];
test {|"rush"::bytes(3)|}
[%expr
let _data_src = Bytestring.to_iter _data_src in
Bytestring.Iter.expect_literal_string _data_src ~size:3 "rush";
Bytestring.Iter.expect_empty _data_src;
()];
test
{| fin::1, comp::1, _rsv::2,
1::4, 0::1, 127::7,
len::bits(8*8), mask::32,
payload::bytes(foo len), rest |}
[%expr
let _data_src = Bytestring.to_iter _data_src in
let fin = Bytestring.Iter.next_bits ~size:1 _data_src in
let comp = Bytestring.Iter.next_bits ~size:1 _data_src in
let _rsv = Bytestring.Iter.next_bits ~size:2 _data_src in
Bytestring.Iter.expect_literal_int _data_src ~size:4 1;
Bytestring.Iter.expect_literal_int _data_src ~size:1 0;
Bytestring.Iter.expect_literal_int _data_src ~size:7 127;
let len = Bytestring.Iter.next_bits ~size:(8 * 8) _data_src in
let mask = Bytestring.Iter.next_bits ~size:32 _data_src in
let payload = Bytestring.Iter.next_bytes ~size:(foo len) _data_src in
let rest = Bytestring.Iter.rest _data_src in
Bytestring.Iter.expect_empty _data_src;
()]
(**
Bytestring prefix optimizatoin tests
*)
let () =
let loc = Location.none in
let test ?guard n strs expected =
let lowers =
List.mapi
(fun idx str ->
let guard = if idx = 0 then guard else None in
let test_name =
id ("test_" ^ string_of_int n ^ "_body_" ^ string_of_int idx)
in
(Bytepattern.parse ~loc str, guard, [%expr [%e test_name]]))
strs
in
let actual = Bytepattern.to_prefix_match ~loc lowers in
let actual =
Format.asprintf "%a" Bytepattern.Prefix_matching.pp [ actual ]
in
let expect = Format.asprintf "%a" Bytepattern.Prefix_matching.pp expected in
if not (String.equal actual expect) then (
Format.printf "%s\n\nExpected:\n\n%s\n\nbut found:\n\n%s\n\n"
(error "#%d Prefix matching trees do not match" n)
expect actual;
assert false)
else Format.printf "match-prefix test #%d %s\r\n%!" n (keyword "OK")
in
(* test: empty pattern is just an empty bytestring *)
test 0 [ {| |}; {| |} ]
[
Prefix
( [ Empty "_data_src" ],
[
Try_run ([], (None, id "test_0_body_0"));
Try_run ([], (None, id "test_0_body_1"));
] );
];
test 1
[
{| hello::8, byte::2, other::3 |}; {| hello::8,byte::2 |}; {| world::10 |};
]
[
Prefix
( [ Create_iterator "_data_src" ],
[
Prefix
( [
Bind_next_fixed_bits
{ src = "hello"; size = 8; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "byte"; size = 2; iter = "_data_src" };
],
[
Try_run
( [
Bind_next_fixed_bits
{ src = "other"; size = 3; iter = "_data_src" };
Empty "_data_src";
],
(None, id "test_1_body_0") );
Try_run ([ Empty "_data_src" ], (None, id "test_1_body_1"));
] );
Try_run
( [
Bind_next_fixed_bits
{ src = "world"; size = 10; iter = "_data_src" };
Empty "_data_src";
],
(None, id "test_1_body_2") );
] );
];
test 2
[
{| fin::1, comp::1, 0::2, 1::4, 0::1, 127::7,
len::bits(compute_bits ()), _mask::32, _payload::bytes(len), rest, |};
{| fin::1, comp::1, 0::2, 1::4, 0::1, 127::7, |};
{| fin::1, comp::1 |};
{| rest |};
]
[
Prefix
( [],
[
Prefix
( [
Create_iterator "_data_src";
Bind_next_fixed_bits
{ src = "fin"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "comp"; size = 1; iter = "_data_src" };
],
[
Prefix
( [
Expect_int_fixed_bits
{ value = 0; size = 2; iter = "_data_src" };
Expect_int_fixed_bits
{ value = 1; size = 4; iter = "_data_src" };
Expect_int_fixed_bits
{ value = 0; size = 1; iter = "_data_src" };
Expect_int_fixed_bits
{ value = 127; size = 7; iter = "_data_src" };
],
[
Try_run
( [
Bind_next_dynamic_bits
{
src = "len";
expr = ocaml "compute_bits ()";
iter = "_data_src";
};
Bind_next_fixed_bits
{ src = "_mask"; size = 32; iter = "_data_src" };
Bind_next_dynamic_bytes
{
src = "_payload";
expr = ocaml "len";
iter = "_data_src";
};
Bind_rest { src = "rest"; iter = "_data_src" };
Empty "_data_src";
],
(None, id "test_2_body_0") );
Try_run
([ Empty "_data_src" ], (None, id "test_2_body_1"));
] );
Try_run ([ Empty "_data_src" ], (None, id "test_2_body_2"));
] );
Try_run
( [ Bypass { src = "_data_src"; name = "rest" } ],
(None, id "test_2_body_3") );
] );
];
test 3 ~guard:[%expr run_guard ()]
[
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 1 :: 1, 127 :: 7,
length :: 64, mask :: 32, payload :: bytes(length), rest :: bytes |};
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 1 :: 1, 127 :: 7,
length :: 64, mask :: 32, payload :: bytes(length), rest :: bytes |};
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 1 :: 1, 127 :: 7,
length :: 64, mask :: 32, payload :: bytes(length), rest :: bytes |};
{| data :: bytes |};
]
[
Prefix
( [],
[
Prefix
( [
Create_iterator "_data_src";
Bind_next_fixed_bits
{ src = "fin"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "compressed"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "rsv"; size = 2; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "opcode"; size = 4; iter = "_data_src" };
Expect_int_fixed_bits
{ value = 1; size = 1; iter = "_data_src" };
Expect_int_fixed_bits
{ value = 127; size = 7; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "length"; size = 64; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "mask"; size = 32; iter = "_data_src" };
Bind_next_dynamic_bytes
{
src = "payload";
expr = ocaml "length";
iter = "_data_src";
};
Bind_rest { src = "rest"; iter = "_data_src" };
Empty "_data_src";
],
[
Try_run ([], (Some (ocaml "run_guard ()"), id "test_3_body_0"));
Try_run ([], (None, id "test_3_body_1"));
Try_run ([], (None, id "test_3_body_2"));
] );
Try_run
( [ Bypass { src = "_data_src"; name = "data" } ],
(None, id "test_3_body_3") );
] );
];
test 4 ~guard:[%expr run_guard ()]
[
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 1 :: 1, 127 :: 7,
length :: 64, mask :: 32, payload :: bytes(length), rest :: bytes |};
]
[
Try_run
( [
Create_iterator "_data_src";
Bind_next_fixed_bits { src = "fin"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "compressed"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits { src = "rsv"; size = 2; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "opcode"; size = 4; iter = "_data_src" };
Expect_int_fixed_bits { value = 1; size = 1; iter = "_data_src" };
Expect_int_fixed_bits { value = 127; size = 7; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "length"; size = 64; iter = "_data_src" };
Bind_next_fixed_bits { src = "mask"; size = 32; iter = "_data_src" };
Bind_next_dynamic_bytes
{ src = "payload"; expr = ocaml "length"; iter = "_data_src" };
Bind_rest { src = "rest"; iter = "_data_src" };
Empty "_data_src";
],
(Some (ocaml "run_guard ()"), id "test_4_body_0") );
];
test 5
[
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 1 :: 1, 127 :: 7,
length :: 64, mask :: 32, payload :: bytes(length), rest :: bytes |};
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 0 :: 1, 126 :: 7,
length :: 16, mask :: 32, payload :: bytes(length * 8), rest :: bytes |};
{| fin :: 1, compressed :: 1, rsv :: 2, opcode :: 4, 0 :: 1,
length :: 7, mask :: 32, payload :: bytes(length * 8), rest :: bytes |};
{| data :: bytes |};
]
[
Prefix
( [],
[
Prefix
( [
Create_iterator "_data_src";
Bind_next_fixed_bits
{ src = "fin"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "compressed"; size = 1; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "rsv"; size = 2; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "opcode"; size = 4; iter = "_data_src" };
],
[
Try_run
( [
Expect_int_fixed_bits
{ value = 1; size = 1; iter = "_data_src" };
Expect_int_fixed_bits
{ value = 127; size = 7; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "length"; size = 64; iter = "_data_src" };
Bind_next_fixed_bits
{ src = "mask"; size = 32; iter = "_data_src" };
Bind_next_dynamic_bytes
{
src = "payload";
expr = ocaml "length";
iter = "_data_src";
};
Bind_rest { src = "rest"; iter = "_data_src" };
Empty "_data_src";
],
(None, id "test_5_body_0") );