FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kernelscript/tests/test_config.ml at main · multikernel/kernelscript · GitHub
multikernel
/
kernelscript
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
508
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kernelscript
/
tests
/
test_config.ml
Copy path
More file actions
More file actions
Latest commit
History
History
History
803 lines (676 loc) · 24.8 KB
Breadcrumbs
kernelscript
/
tests
/
test_config.ml
Copy path
File metadata and controls
803 lines (676 loc) · 24.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
open
Alcotest
open
Kernelscript.Ast
open
Kernelscript.Symbol_table
open
Kernelscript.Type_checker
open
Kernelscript.Ebpf_c_codegen
open
Kernelscript.Userspace_codegen
open
Kernelscript.Ir_generator
(*
* Helper functions
*)
let
dummy_pos
=
{ line
=
1
; column
=
1
; filename
=
"
test
"
}
let
parse_string
s
=
let
lexbuf
=
Lexing.
from_string s
in
Kernelscript.Parser.
program
Kernelscript.Lexer.
token lexbuf
(*
* Test 1: Name Conflicts
*)
(*
* Test config vs config name conflict
*)
let
test_config_vs_config_name_conflict
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
}
config network {
timeout
:
u32
=
5000
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_
=
build_symbol_table ast
in
fail
"
Expected name conflict between configs
"
with
Symbol_error
(
msg
,
_
) ->
check
bool
"
config vs config conflict detected
"
true
(
String.
contains msg
'a'
)
(*
* Test config vs map name conflict
*)
let
test_config_vs_map_name_conflict
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
}
var
network
:
hash
<
u32,
u64
>
(
1024
)
@
xdp
fn
test
(
ctx
:
*
xdp_md) ->
xdp_action
{
return
2
}
fn
main
() ->
i32
{
return
0
}
|}
in
try
let
ast
=
parse_string program_text
in
let
_
=
build_symbol_table ast
in
fail
"
Expected name conflict between config and map
"
with
Symbol_error
(
msg
,
_
) ->
check
bool
"
config vs map conflict detected
"
true
(
String.
contains msg
'a'
)
(*
* Test config vs function name conflict
*)
let
test_config_vs_function_name_conflict
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
}
fn network
()
-> u32 {
return
42
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_
=
build_symbol_table ast
in
fail
"
Expected name conflict between config and function
"
with
Symbol_error
(
msg
,
_
) ->
check
bool
"
config vs function conflict detected
"
true
(
String.
contains msg
'a'
)
(*
* Test config with no conflicts
*)
let
test_config_no_conflicts
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
timeout
:
u32
=
5000
,
}
config security {
enable_logging
:
bool
=
true
,
threat_level
:
u32
=
3
,
}
var
packet_counts
:
hash
<
u32,
u64
>
(
1024
)
@
xdp
fn
test
(
ctx
:
*
xdp_md) ->
xdp_action
{
return
2
}
fn
main
() ->
i32
{
return
0
}
|}
in
try
let
ast
=
parse_string program_text
in
let
symbol_table
=
build_symbol_table ast
in
(*
Verify both configs are in symbol table
*)
(
match
lookup_symbol symbol_table
"
network
"
with
|
Some
{
kind
=
Config
_
; _ }
->
()
|
_
-> fail
"
network config not found in symbol table
"
);
(
match
lookup_symbol symbol_table
"
security
"
with
|
Some
{
kind
=
Config
_
; _ }
->
()
|
_
-> fail
"
security config not found in symbol table
"
)
with
|
e
-> fail (
"
Unexpected error:
"
^
Printexc.
to_string e)
(*
* Test 2: Valid Field Access
*)
(*
* Test valid config field access with correct types
*)
let
test_valid_config_field_access
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
timeout
:
u32
=
5000
,
enable_logging
:
bool
=
true
,
rate_limit
:
u64
=
1000
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var
size
:
u32
=
network
.max_size
var
timeout
:
u32
=
network
.timeout
var
logging
:
bool
=
network
.enable_logging
var
limit
:
u64
=
network
.rate_limit
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_symbol_table
=
build_symbol_table ast
in
(*
Also test type checking
*)
let
(enhanced_ast, _)
=
type_check_and_annotate_ast ast
in
check
bool
"
type check produces declarations
"
true
(
List.
length enhanced_ast
>
0
)
with
|
e
-> fail (
"
Unexpected error in valid field access:
"
^
Printexc.
to_string e)
(*
* Test config field access in expressions
*)
let
test_config_field_access_in_expressions
()
=
let
program_text
=
{
|
config limits {
max_packet_size
:
u32
=
1500
,
min_packet_size
:
u32
=
64
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var
packet_size
:
u32
=
800
if
(
packet_size
>
limits
.max_packet_size ||
packet_size
<
limits
.min_packet_size) {
return
1
//
DROP
}
return
2
//
PASS
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_symbol_table
=
build_symbol_table ast
in
let
(enhanced_ast, _)
=
type_check_and_annotate_ast ast
in
check
bool
"
type check produces declarations
"
true
(
List.
length enhanced_ast
>
0
)
with
|
e
-> fail (
"
Unexpected error in expression field access:
"
^
Printexc.
to_string e)
(*
* Test 3: Invalid Field Access
*)
(*
* Test invalid config field access (non-existent field)
*)
let
test_invalid_config_field_access
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
timeout
:
u32
=
5000
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var bad_field
=
network.nonexistent_field
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_
=
build_symbol_table ast
in
fail
"
Expected error for non-existent config field
"
with
Symbol_error
(
msg
,
_
) ->
check
bool
"
non-existent field error detected
"
true
(
String.
contains msg
'f'
)
(*
* Test invalid config access (non-existent config)
*)
let
test_invalid_config_access
()
=
let
program_text
=
{
|
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var bad_config
=
nonexistent_config.some_field
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_
=
build_symbol_table ast
in
fail
"
Expected error for non-existent config
"
with
Symbol_error
(
msg
,
_
) ->
check
bool
"
non-existent config error detected
"
true
(
String.
contains msg
'U'
)
(*
* Test accessing map as config
*)
let
test_accessing_map_as_config
()
=
let
program_text
=
{
|
var
packet_counts
:
hash
<
u32,
u64
>
(
1024
)
@
xdp
fn
test
(
ctx
:
*
xdp_md) ->
xdp_action
{
var bad_access
=
packet_counts.some_field
return
2
}
fn
main
() ->
i32
{
return
0
}
|}
in
try
let
ast
=
parse_string program_text
in
let
_
=
build_symbol_table ast
in
let
_
=
type_check_and_annotate_ast ast
in
fail
"
Expected error for accessing map as config
"
with
|
Symbol_error
(
msg
,
_
) ->
check
bool
"
map accessed as config error detected
"
true
(
String.
contains msg
'n'
)
|
Type_error
(
msg
,
_
) ->
check
bool
"
map accessed as config type error detected
"
true
(
String.
contains msg
'i'
)
(*
* Test config declared inside function (invalid)
*)
let
test_config_inside_function
()
=
let
program_text
=
{
|
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
config local_config {
value
:
u32
=
42
,
}
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
_ast
=
parse_string program_text
in
fail
"
Expected error for config declared inside function
"
with
|
_
->
()
(*
* Test config declared inside program block (invalid)
*)
let
test_config_inside_program
()
=
let
program_text
=
{
|
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
config program_config {
size
:
u32
=
1024
,
}
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
_ast
=
parse_string program_text
in
fail
"
Expected error for config declared inside program
"
with
|
_
->
()
(*
* Test 4: eBPF C Code Generation
*)
(*
* Helper function to compile to eBPF C
*)
let
compile_to_ebpf_c
ast
=
let
symbol_table
=
build_symbol_table ast
in
let
(enhanced_ast, _)
=
type_check_and_annotate_ast ast
in
let
ir_result
=
generate_ir enhanced_ast symbol_table
"
test
"
in
let
_config_declarations
=
List.
filter_map (
fun
decl
->
match
decl
with
|
ConfigDecl
config
->
Some
config
|
_
->
None
) ast
in
compile_to_c (
List.
hd (
Kernelscript.Ir.
get_programs ir_result))
(*
* Test config struct generation
*)
let
test_config_struct_generation
()
=
let
program_text
=
{
|
config network {
max_size
:
u32
=
1500
,
timeout
:
u32
=
5000
,
enable_logging
:
bool
=
true
,
rate_limit
:
u64
=
1000
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var size
=
network.max_size
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
c_code
=
compile_to_ebpf_c ast
in
(*
Check for config struct definition
*)
check
bool
"
config struct generated
"
true
(
String.
length c_code
>
0
);
check
bool
"
network_config struct found
"
true
(
String.
contains c_code
'n'
);
check
bool
"
max_size field found
"
true
(
String.
contains c_code
'm'
);
check
bool
"
timeout field found
"
true
(
String.
contains c_code
't'
)
with
|
e
-> fail (
"
Error in config struct generation:
"
^
Printexc.
to_string e)
(*
* Test config BPF map generation
*)
let
test_config_bpf_map_generation
()
=
let
program_text
=
{
|
config settings {
buffer_size
:
u32
=
4096
,
max_entries
:
u32
=
1000
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var size
=
settings.buffer_size
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
c_code
=
compile_to_ebpf_c ast
in
(*
Check for BPF map definition
*)
check
bool
"
config BPF map generated
"
true
(
String.
length c_code
>
0
);
check
bool
"
settings_config_map found
"
true
(
String.
contains c_code
's'
)
with
|
e
-> fail (
"
Error in config BPF map generation:
"
^
Printexc.
to_string e)
(*
* Test 5: Userspace Code Generation
*)
(*
* Helper function to compile to userspace C
*)
let
compile_to_userspace_c
ast
=
let
temp_dir
=
Filename.
temp_file
"
test_config_userspace
"
"
"
in
Unix.
unlink temp_dir;
Unix.
mkdir temp_dir
0o755
;
try
let
_config_declarations
=
List.
filter_map (
fun
decl
->
match
decl
with
|
ConfigDecl
config
->
Some
config
|
_
->
None
) ast
in
(*
Convert AST to IR for the new IR-based codegen
*)
let
ir_multi_prog
=
Kernelscript.Ir.
make_ir_multi_program
"
test
"
dummy_pos
in
let
_output_file
=
generate_userspace_code_from_ir ir_multi_prog
~output_dir:
temp_dir
"
test
"
in
let
generated_file
=
Filename.
concat temp_dir
"
test.c
"
in
if
Sys.
file_exists generated_file
then
(
let
ic
=
open_in generated_file
in
let
content
=
really_input_string ic (in_channel_length ic)
in
close_in ic;
(*
Cleanup
*)
Unix.
unlink generated_file;
Unix.
rmdir temp_dir;
content
)
else
(
Unix.
rmdir temp_dir;
"
"
)
with
|
_exn
->
(*
Cleanup on error
*)
(
try
Unix.
rmdir temp_dir
with
_
->
()
);
"
"
(*
* Test config initialization generation
*)
let
test_config_initialization_generation
()
=
let
program_text
=
{
|
config database {
host
:
u32
=
192168001001
,
port
:
u32
=
5432
,
max_connections
:
u32
=
100
,
timeout_seconds
:
u32
=
30
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var host
=
database.host
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
userspace_code
=
compile_to_userspace_c ast
in
(*
Check for config initialization
*)
check
bool
"
config initialization generated
"
true
(
String.
length userspace_code
>
0
);
check
bool
"
database config found
"
true
(
String.
contains userspace_code
'd'
);
check
bool
"
host field found
"
true
(
String.
contains userspace_code
'h'
);
check
bool
"
port field found
"
true
(
String.
contains userspace_code
'p'
)
with
|
e
-> fail (
"
Error in config initialization generation:
"
^
Printexc.
to_string e)
(*
* Test 6: Integration Tests
*)
(*
* Test end-to-end config compilation
*)
let
test_end_to_end_config_compilation
()
=
let
program_text
=
{
|
config application {
version
:
u32
=
100
,
debug_mode
:
bool
=
false
,
max_memory
:
u64
=
1048576
,
}
var
stats
:
hash
<
u32,
u64
>
(
1024
)
@
xdp
fn
packet_filter
(
ctx
:
*
xdp_md) ->
xdp_action
{
var version
=
application.version
var debug
=
application.debug_mode
var memory_limit
=
application.max_memory
stats[
1
]
=
version
if
(debug) {
return
2
//
PASS
in
debug mode
}
if
(version
>
90
) {
return
2
}
else
{
return
1
}
}
fn
main
() ->
i32
{
return
0
}
|}
in
try
let
ast
=
parse_string program_text
in
let
_symbol_table
=
build_symbol_table ast
in
let
(_enhanced_ast, typed_programs)
=
type_check_and_annotate_ast ast
in
let
ebpf_code
=
compile_to_ebpf_c ast
in
let
userspace_code
=
compile_to_userspace_c ast
in
(*
Verify complete compilation pipeline
*)
check
int
"
one typed program generated
"
1
(
List.
length typed_programs);
check
bool
"
eBPF code generated
"
true
(
String.
length ebpf_code
>
0
);
check
bool
"
userspace code generated
"
true
(
String.
length userspace_code
>
0
);
(*
Verify config in both generated codes
*)
check
bool
"
config in eBPF code
"
true
(
String.
contains ebpf_code
'a'
);
check
bool
"
config in userspace code
"
true
(
String.
contains userspace_code
'a'
)
with
|
e
-> fail (
"
Error in end-to-end compilation:
"
^
Printexc.
to_string e)
(*
* Test config with different BPF types
*)
let
test_config_with_different_types
()
=
let
program_text
=
{
|
config types_test {
flag_u8
:
u32
=
255
,
flag_u16
:
u32
=
65535
,
flag_u32
:
u32
=
4294967295
,
flag_u64
:
u64
=
1000000000000
,
flag_bool
:
bool
=
true
,
}
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
var u32_val
=
types_test.flag_u32
var u64_val
=
types_test.flag_u64
var bool_val
=
types_test.flag_bool
return
2
}
fn main
()
-> i32 {
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
_symbol_table
=
build_symbol_table ast
in
let
(_enhanced_ast, _)
=
type_check_and_annotate_ast ast
in
(*
Test code generation with different types
*)
let
ebpf_code
=
compile_to_ebpf_c ast
in
let
userspace_code
=
compile_to_userspace_c ast
in
check
bool
"
different types in eBPF code
"
true
(
String.
length ebpf_code
>
0
);
check
bool
"
different types in userspace code
"
true
(
String.
length userspace_code
>
0
)
with
|
e
-> fail (
"
Error with different config types:
"
^
Printexc.
to_string e)
(*
* Test config AST to IR conversion bug fix
*)
let
test_config_ast_to_ir_conversion
()
=
let
program_text
=
{
|
config network {
max_packet_size
:
u32
=
1500
,
enable_logging
:
bool
=
true
,
blocked_ports
:
u16
[
4
]
=
[
22
,
23
,
135
,
445
],
}
@
xdp fn packet_filter(ctx:
*xdp_md
) -> xdp_action {
if
(network.max_packet_size
>
1000
) {
if
(network.enable_logging) {
return
1
}
}
return
2
}
fn main
()
-> i32 {
var prog
=
load(packet_filter)
return
0
}
|
}
in
try
let
ast
=
parse_string program_text
in
let
symbol_table
=
build_symbol_table ast
in
let
(annotated_ast, _typed_programs)
=
type_check_and_annotate_ast ast
in
(*
Step 1: Verify config declarations are in AST
*)
let
config_declarations
=
List.
filter_map (
fun
decl
->
match
decl
with
|
ConfigDecl
config
->
Some
config
|
_
->
None
) ast
in
check
int
"
config declarations found in AST
"
1
(
List.
length config_declarations);
let
network_config
=
List.
hd config_declarations
in
check
string
"
config name in AST
"
"
network
"
network_config.config_name;
check
int
"
config fields in AST
"
3
(
List.
length network_config.config_fields);
(*
Step 2: Generate IR and verify config declarations are converted to IR format
*)
let
ir
=
generate_ir annotated_ast symbol_table
"
test_config_ast_ir
"
in
check
int
"
IR multi-program has config declarations
"
1
(
List.
length (
Kernelscript.Ir.
get_global_configs ir));
let
ir_config
=
List.
hd (
Kernelscript.Ir.
get_global_configs ir)
in
check
string
"
config name in IR
"
"
network
"
ir_config.config_name;
check
int
"
config fields in IR
"
3
(
List.
length ir_config.config_fields);
(*
Step 3: Verify specific field conversions from AST to IR
*)
let
field_names
=
List.
map (
fun
(
field
: Kernelscript.Ir.ir_config_field
) -> field.field_name) ir_config.config_fields
in
check
bool
"
max_packet_size field in IR
"
true
(
List.
mem
"
max_packet_size
"
field_names);
check
bool
"
enable_logging field in IR
"
true
(
List.
mem
"
enable_logging
"
field_names);
check
bool
"
blocked_ports field in IR
"
true
(
List.
mem
"
blocked_ports
"
field_names);
(*
Step 4: Generate eBPF C code and verify struct and map definitions are present
*)
let
(ebpf_code, _)
=
compile_multi_to_c ir
in
(*
Test that struct network_config is defined
*)
check
bool
"
struct network_config defined
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
struct network_config
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
(*
Test that network_config_map is defined
*)
check
bool
"
network_config_map defined
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
network_config_map
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
(*
Test that config fields are in the struct definition
*)
check
bool
"
max_packet_size field in struct
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
max_packet_size
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
check
bool
"
enable_logging field in struct
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
enable_logging
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
check
bool
"
blocked_ports field in struct
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
blocked_ports
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
(*
Test that BPF_MAP_TYPE_ARRAY is used for config map
*)
check
bool
"
config map uses BPF_MAP_TYPE_ARRAY
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
BPF_MAP_TYPE_ARRAY
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
(*
Test that get_network_config helper function is generated
*)
check
bool
"
get_network_config helper function
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
get_network_config
"
) ebpf_code
0
);
true
with
Not_found
->
false
);
with
|
e
-> fail (
"
Config AST to IR conversion test failed:
"
^
Printexc.
to_string e)
(*
* Test config map initialization with default values (bug fix test)
*)
let
test_config_map_default_value_initialization
()
=
let
program_text
=
{
|
config network {
max_packet_size
:
u32
=
1500
,
enable_logging
:
bool
=
true
,
blocked_ports
:
u16
[
4
]
=
[
22
,
23
,
135
,
445
],
timeout
:
u32
=
5000
,
}
@
xdp fn packet_filter(ctx:
*xdp_md
) -> xdp_action {
if
(network.max_packet_size
>
1000
) {
return
2
}
return
1
}
fn main
()
-> i32 {
network.enable_logging
=
true
return
0
}
|
}
in
let
temp_dir
=
Filename.
temp_file
"
test_config_init
"
"
"
in
Unix.
unlink temp_dir;
Unix.
mkdir temp_dir
0o755
;
try
let
ast
=
parse_string program_text
in
let
symbol_table
=
build_symbol_table ast
in
let
(annotated_ast, _typed_programs)
=
type_check_and_annotate_ast ast
in
let
ir
=
generate_ir annotated_ast symbol_table
"
test_config_init
"
in
(*
Extract config declarations
*)
let
config_declarations
=
List.
filter_map (
fun
decl
->
match
decl
with
|
ConfigDecl
config
->
Some
config
|
_
->
None
) ast
in
(*
Generate userspace code with config declarations
*)
let
_output_file
=
generate_userspace_code_from_ir
~config_declarations
ir
~output_dir:
temp_dir
"
test_config_init
"
in
let
generated_file
=
Filename.
concat temp_dir
"
test_config_init.c
"
in
if
Sys.
file_exists generated_file
then
(
let
ic
=
open_in generated_file
in
let
content
=
really_input_string ic (in_channel_length ic)
in
close_in ic;
(*
Cleanup
*)
Unix.
unlink generated_file;
Unix.
rmdir temp_dir;
(*
Test 1: Config map is loaded
*)
check
bool
"
network_config_map_fd is loaded
"
true
(
String.
contains content
'n'
&&
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
network_config_map_fd.*find_map_fd_by_name
"
) content
0
);
true
with
Not_found
->
false
));
(*
Test 2: Default value initialization exists
*)
check
bool
"
config initialization comment exists
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
Initialize.*config map with default values
"
) content
0
);
true
with
Not_found
->
false
);
(*
Test 3: Struct with default values is created
*)
check
bool
"
init_config struct created
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
struct network_config init_config
"
) content
0
);
true
with
Not_found
->
false
);
(*
Test 4: Specific default values are set correctly
*)
check
bool
"
max_packet_size initialized to 1500
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.max_packet_size = 1500
"
) content
0
);
true
with
Not_found
->
false
);
check
bool
"
enable_logging initialized to true
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.enable_logging = true
"
) content
0
);
true
with
Not_found
->
false
);
check
bool
"
timeout initialized to 5000
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.timeout = 5000
"
) content
0
);
true
with
Not_found
->
false
);
(*
Test 5: Array initialization is correct
*)
check
bool
"
blocked_ports[0] = 22
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.blocked_ports
\\
[0
\\
] = 22
"
) content
0
);
true
with
Not_found
->
false
);
check
bool
"
blocked_ports[1] = 23
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.blocked_ports
\\
[1
\\
] = 23
"
) content
0
);
true
with
Not_found
->
false
);
check
bool
"
blocked_ports[2] = 135
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.blocked_ports
\\
[2
\\
] = 135
"
) content
0
);
true
with
Not_found
->
false
);
check
bool
"
blocked_ports[3] = 445
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
init_config
\\
.blocked_ports
\\
[3
\\
] = 445
"
) content
0
);
true
with
Not_found
->
false
);
(*
Test 6: Map update call exists
*)
check
bool
"
bpf_map_update_elem called for initialization
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
bpf_map_update_elem.*network_config_map_fd.*init_config
"
) content
0
);
true
with
Not_found
->
false
);
(*
Test 7: Error handling for initialization failure
*)
check
bool
"
initialization error handling
"
true
(
try
ignore (
Str.
search_forward (
Str.
regexp
"
Failed to initialize.*config map with default values
"
) content
0
);
true
with
Not_found
->
false
);
)
else
(
Unix.
rmdir temp_dir;
fail
"
Generated C file does not exist
"
)
with
|
exn
->
(*
Cleanup on error
*)
(
try
Unix.
rmdir temp_dir
with
_
->
()
);
fail (
"
Config map initialization test failed:
"
^
Printexc.
to_string
exn
)
(*
* All config tests
*)
let
config_tests
=
[
(*
Name Conflict Tests
*)
"
config_vs_config_name_conflict
"
,
`Quick
, test_config_vs_config_name_conflict;
"
config_vs_map_name_conflict
"
,
`Quick
, test_config_vs_map_name_conflict;
"
config_vs_function_name_conflict
"
,
`Quick
, test_config_vs_function_name_conflict;
"
config_no_conflicts
"
,
`Quick
, test_config_no_conflicts;
(*
Valid Field Access Tests
*)
"
valid_config_field_access
"
,
`Quick
, test_valid_config_field_access;
"
config_field_access_in_expressions
"
,
`Quick
, test_config_field_access_in_expressions;
(*
Invalid Field Access Tests
*)
"
invalid_config_field_access
"
,
`Quick
, test_invalid_config_field_access;
"
invalid_config_access
"
,
`Quick
, test_invalid_config_access;
"
accessing_map_as_config
"
,
`Quick
, test_accessing_map_as_config;
(*
Invalid Local Config Tests
*)
"
config_inside_function
"
,
`Quick
, test_config_inside_function;
"
config_inside_program
"
,
`Quick
, test_config_inside_program;
(*
eBPF C Code Generation Tests
*)
"
config_struct_generation
"
,
`Quick
, test_config_struct_generation;
"
config_bpf_map_generation
"
,
`Quick
, test_config_bpf_map_generation;
(*
Bug Fix Tests
*)
"
config_ast_to_ir_conversion
"
,
`Quick
, test_config_ast_to_ir_conversion;
(*
Userspace Code Generation Tests
*)
"
config_initialization_generation
"
,
`Quick
, test_config_initialization_generation;
"
config_map_default_value_initialization
"
,
`Quick
, test_config_map_default_value_initialization;
(*
Integration Tests
*)
"
end_to_end_config_compilation
"
,
`Quick
, test_end_to_end_config_compilation;
"
config_with_different_types
"
,
`Quick
, test_config_with_different_types;
]
let
()
=
run
"
KernelScript Config Tests
"
[
"
config
"
, config_tests;
]
Back
|
FazBrowse Home
|
New Git URL