@@ -155,7 +155,7 @@ fn get_argument_values(arguments: &ArgumentsBag) -> Result<Arguments> {
155
155
} )
156
156
}
157
157
158
- fn parse_json ( reader : & mut dyn Read ) -> Result < JsonFile > {
158
+ fn parse_json ( reader : impl Read ) -> Result < JsonFile > {
159
159
serde_json:: from_reader ( reader) . map_err ( Error :: Json )
160
160
}
161
161
@@ -231,9 +231,8 @@ mod tests {
231
231
use crate :: backend:: SeccompCmpOp :: { Le , * } ;
232
232
use crate :: backend:: { SeccompAction , SeccompCondition as Cond , TargetArch , TargetArchError } ;
233
233
234
- // test helper for generating correct JSON input data
235
- fn get_correct_json_input ( ) -> String {
236
- r#"
234
+ // Correct JSON input data
235
+ static CORRECT_JSON_INPUT : & str = r#"
237
236
{
238
237
"thread_1": {
239
238
"default_action": {
@@ -328,9 +327,7 @@ mod tests {
328
327
]
329
328
}
330
329
}
331
- "#
332
- . to_string ( )
333
- }
330
+ "# ;
334
331
335
332
#[ test]
336
333
fn test_error_messages ( ) {
@@ -533,70 +530,51 @@ mod tests {
533
530
. is_err( ) ) ;
534
531
}
535
532
536
- #[ allow( clippy:: useless_asref) ]
537
533
#[ test]
538
534
fn test_parse_json ( ) {
539
535
// test with malformed JSON
540
536
{
541
537
// empty file
542
- let mut json_input = "" . to_string ( ) ;
543
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
544
-
545
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
538
+ assert ! ( parse_json( std:: io:: empty( ) ) . is_err( ) ) ;
546
539
547
540
// not json
548
- let mut json_input = "hjkln" . to_string ( ) ;
549
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
550
-
551
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
541
+ let json_input = "hjkln" ;
542
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
552
543
553
544
// top-level array
554
- let mut json_input = "[]" . to_string ( ) ;
555
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
556
-
557
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
545
+ let json_input = "[]" ;
546
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
558
547
559
548
// thread key must be a string
560
- let mut json_input = "{1}" . to_string ( ) ;
561
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
562
-
563
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
549
+ let json_input = "{1}" ;
550
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
564
551
565
552
// empty Filter object
566
- let mut json_input = r#"{"a": {}}"# . to_string ( ) ;
567
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
568
-
569
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
553
+ let json_input = r#"{"a": {}}"# ;
554
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
570
555
571
556
// missing 'filter' field
572
- let mut json_input =
573
- r#"{"a": {"filter_action": "allow", "default_action":"log"}}"# . to_string ( ) ;
574
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
575
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
557
+ let json_input = r#"{"a": {"filter_action": "allow", "default_action":"log"}}"# ;
558
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
576
559
577
560
// wrong key 'filters'
578
- let mut json_input =
579
- r#"{"a": {"filter_action": "allow", "default_action":"log", "filters": []}}"#
580
- . to_string ( ) ;
581
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
582
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
561
+ let json_input =
562
+ r#"{"a": {"filter_action": "allow", "default_action":"log", "filters": []}}"# ;
563
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
583
564
584
565
// wrong action 'logs'
585
- let mut json_input =
586
- r#"{"a": {"filter_action": "allow", "default_action":"logs", "filter": []}}"#
587
- . to_string ( ) ;
588
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
589
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
566
+ let json_input =
567
+ r#"{"a": {"filter_action": "allow", "default_action":"logs", "filter": []}}"# ;
568
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
590
569
591
570
// action that expects a value
592
- let mut json_input =
593
- r#"{"a": {"filter_action": "allow", "default_action":"errno", "filter": []}}"#
594
- . to_string ( ) ;
595
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
596
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
571
+ let json_input =
572
+ r#"{"a": {"filter_action": "allow", "default_action":"errno", "filter": []}}"# ;
573
+
574
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
597
575
598
576
// overflowing u64 value
599
- let mut json_input = r#"
577
+ let json_input = r#"
600
578
{
601
579
"thread_2": {
602
580
"default_action": "trap",
@@ -616,13 +594,11 @@ mod tests {
616
594
]
617
595
}
618
596
}
619
- "#
620
- . to_string ( ) ;
621
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
622
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
597
+ "# ;
598
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
623
599
624
600
// negative integer value
625
- let mut json_input = r#"
601
+ let json_input = r#"
626
602
{
627
603
"thread_2": {
628
604
"default_action": "trap",
@@ -642,13 +618,11 @@ mod tests {
642
618
]
643
619
}
644
620
}
645
- "#
646
- . to_string ( ) ;
647
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
648
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
621
+ "# ;
622
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
649
623
650
624
// float value
651
- let mut json_input = r#"
625
+ let json_input = r#"
652
626
{
653
627
"thread_2": {
654
628
"default_action": "trap",
@@ -668,13 +642,11 @@ mod tests {
668
642
]
669
643
}
670
644
}
671
- "#
672
- . to_string ( ) ;
673
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
674
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
645
+ "# ;
646
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
675
647
676
648
// duplicate filter keys
677
- let mut json_input = r#"
649
+ let json_input = r#"
678
650
{
679
651
"thread_1": {
680
652
"default_action": "trap",
@@ -687,32 +659,22 @@ mod tests {
687
659
"filter": []
688
660
}
689
661
}
690
- "#
691
- . to_string ( ) ;
692
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
693
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_err( ) ) ;
662
+ "# ;
663
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_err( ) ) ;
694
664
}
695
665
696
666
// test with correctly formed JSON
697
667
{
698
668
// empty JSON file
699
- let mut json_input = "{}" . to_string ( ) ;
700
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
701
-
702
- assert_eq ! ( parse_json( & mut json_input. as_ref( ) ) . unwrap( ) . 0 . len( ) , 0 ) ;
669
+ let json_input = "{}" ;
670
+ assert_eq ! ( parse_json( json_input. as_bytes( ) ) . unwrap( ) . 0 . len( ) , 0 ) ;
703
671
704
672
// empty Filter
705
- let mut json_input =
706
- r#"{"a": {"filter_action": "allow", "default_action":"log", "filter": []}}"#
707
- . to_string ( ) ;
708
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
709
- assert ! ( parse_json( & mut json_input. as_ref( ) ) . is_ok( ) ) ;
673
+ let json_input =
674
+ r#"{"a": {"filter_action": "allow", "default_action":"log", "filter": []}}"# ;
675
+ assert ! ( parse_json( json_input. as_bytes( ) ) . is_ok( ) ) ;
710
676
711
677
// correctly formed JSON filter
712
- let mut json_input = get_correct_json_input ( ) ;
713
- // safe because we know the string is UTF-8
714
- let json_input = unsafe { json_input. as_bytes_mut ( ) } ;
715
-
716
678
let mut filters = HashMap :: new ( ) ;
717
679
filters. insert (
718
680
"thread_1" . to_string ( ) ,
@@ -765,7 +727,7 @@ mod tests {
765
727
let mut v1: Vec < _ > = filters. into_iter ( ) . collect ( ) ;
766
728
v1. sort_by ( |x, y| x. 0 . cmp ( & y. 0 ) ) ;
767
729
768
- let mut v2: Vec < _ > = parse_json ( & mut json_input . as_ref ( ) )
730
+ let mut v2: Vec < _ > = parse_json ( CORRECT_JSON_INPUT . as_bytes ( ) )
769
731
. unwrap ( )
770
732
. 0
771
733
. into_iter ( )
@@ -800,10 +762,10 @@ mod tests {
800
762
let in_file = TempFile :: new ( ) . unwrap ( ) ;
801
763
let out_file = TempFile :: new ( ) . unwrap ( ) ;
802
764
803
- let mut json_input = get_correct_json_input ( ) ;
804
- // safe because we know the string is UTF-8
805
- let json_input = unsafe { json_input . as_bytes_mut ( ) } ;
806
- in_file . as_file ( ) . write_all ( json_input ) . unwrap ( ) ;
765
+ in_file
766
+ . as_file ( )
767
+ . write_all ( CORRECT_JSON_INPUT . as_bytes ( ) )
768
+ . unwrap ( ) ;
807
769
808
770
let arguments = Arguments {
809
771
input_file : in_file. as_path ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
0 commit comments