@@ -97,19 +97,17 @@ where
97
97
98
98
// prepare vt preferences
99
99
for pref in & vt. parameters {
100
- if let Some ( ( prefid, class, name, _value) ) =
101
- nvt. preferences . iter ( ) . find_map ( |p| {
102
- if let Some ( i) = p. id {
103
- if i as u16 == pref. id {
104
- Some ( p. into ( ) )
105
- } else {
106
- None
107
- }
100
+ if let Some ( ( prefid, class, name, _) ) = nvt. preferences . iter ( ) . find_map ( |p| {
101
+ if let Some ( i) = p. id {
102
+ if i as u16 == pref. id {
103
+ Some ( p. into ( ) )
108
104
} else {
109
105
None
110
106
}
111
- } )
112
- {
107
+ } else {
108
+ None
109
+ }
110
+ } ) {
113
111
let value_aux: String = if class == * "checkbox" {
114
112
bool_to_str ( & pref. value )
115
113
} else {
@@ -293,30 +291,31 @@ where
293
291
async fn prepare_reverse_lookup_opt_for_openvas ( & mut self ) -> RedisStorageResult < ( ) > {
294
292
let mut lookup_opts: Vec < String > = vec ! [ ] ;
295
293
296
- if let Some ( reverse_lookup_only) = self . scan_config . target . reverse_lookup_only {
297
- if reverse_lookup_only {
298
- lookup_opts. push ( "reverse_lookup_only|||yes" . to_string ( ) ) ;
299
- } else {
300
- lookup_opts. push ( "reverse_lookup_only|||no" . to_string ( ) ) ;
301
- }
302
- }
303
-
304
- if let Some ( reverse_lookup_unify) = self . scan_config . target . reverse_lookup_unify {
305
- if reverse_lookup_unify {
306
- lookup_opts. push ( "reverse_lookup_unify|||yes" . to_string ( ) ) ;
307
- } else {
308
- lookup_opts. push ( "reverse_lookup_unify|||no " . to_string ( ) ) ;
309
- }
294
+ if self
295
+ . scan_config
296
+ . target
297
+ . reverse_lookup_only
298
+ . is_some_and ( |x| x)
299
+ {
300
+ lookup_opts. push ( "reverse_lookup_only|||yes" . to_string ( ) ) ;
301
+ } else {
302
+ lookup_opts. push ( "reverse_lookup_only|||no" . to_string ( ) ) ;
310
303
}
311
304
312
- if !lookup_opts. is_empty ( ) {
313
- self . redis_connector . push_kb_item (
314
- format ! ( "internal/{}/scanprefs" , self . scan_config. scan_id. clone( ) ) . as_str ( ) ,
315
- lookup_opts,
316
- ) ?
305
+ if self
306
+ . scan_config
307
+ . target
308
+ . reverse_lookup_unify
309
+ . is_some_and ( |x| x)
310
+ {
311
+ lookup_opts. push ( "reverse_lookup_unify|||yes" . to_string ( ) ) ;
312
+ } else {
313
+ lookup_opts. push ( "reverse_lookup_unify|||no" . to_string ( ) ) ;
317
314
}
318
-
319
- Ok ( ( ) )
315
+ self . redis_connector . push_kb_item (
316
+ format ! ( "internal/{}/scanprefs" , self . scan_config. scan_id. clone( ) ) . as_str ( ) ,
317
+ lookup_opts,
318
+ )
320
319
}
321
320
322
321
async fn prepare_target_for_openvas ( & mut self ) -> RedisStorageResult < ( ) > {
@@ -549,6 +548,7 @@ mod tests {
549
548
scan_id : "123-456" . to_string ( ) ,
550
549
..Default :: default ( )
551
550
} ;
551
+ scan. target . hosts = vec ! [ "127.0.0.1" . to_string( ) , "10.0.0.1" . to_string( ) ] ;
552
552
scan. target . alive_test_methods = vec ! [ AliveTestMethods :: Icmp , AliveTestMethods :: TcpSyn ] ;
553
553
scan. target . credentials = vec ! [ Credential {
554
554
service: Service :: SSH ,
@@ -573,48 +573,148 @@ mod tests {
573
573
} ,
574
574
] ,
575
575
} ] ;
576
+ scan. scan_preferences = vec ! [
577
+ crate :: models:: ScanPreference {
578
+ id: "testParam1" . to_string( ) ,
579
+ value: "1" . to_string( ) ,
580
+ } ,
581
+ crate :: models:: ScanPreference {
582
+ id: "testParam2" . to_string( ) ,
583
+ value: "abc" . to_string( ) ,
584
+ } ,
585
+ ] ;
586
+ scan. vts = vec ! [ crate :: models:: VT {
587
+ oid: "123" . to_string( ) ,
588
+ parameters: vec![
589
+ crate :: models:: Parameter {
590
+ id: 1 ,
591
+ value: "yes" . to_string( ) ,
592
+ } ,
593
+ crate :: models:: Parameter {
594
+ id: 2 ,
595
+ value: "abc" . to_string( ) ,
596
+ } ,
597
+ ] ,
598
+ } ] ;
576
599
577
600
let mut rc = FakeRedis {
578
601
data : HashMap :: new ( ) ,
579
602
} ;
580
603
581
604
let mut prefh = PreferenceHandler :: new ( scan, & mut rc) ;
582
605
assert_eq ! ( prefh. redis_connector. kb_id( ) . unwrap( ) , 3 ) ;
606
+ // Prepare and test Scan ID
583
607
assert ! ( prefh. prepare_scan_id_for_openvas( ) . await . is_ok( ) ) ;
584
608
assert ! ( prefh
585
609
. redis_connector
586
610
. item_exists( "internal/scanid" , "123-456" ) ) ;
587
611
assert ! ( prefh. redis_connector. item_exists( "internal/123-456" , "new" ) ) ;
588
612
589
- assert ! ( prefh. prepare_main_kbindex_for_openvas( ) . await . is_ok( ) ) ;
613
+ // Prepare and test Target
614
+ assert ! ( prefh. prepare_target_for_openvas( ) . await . is_ok( ) ) ;
590
615
assert ! ( prefh
591
616
. redis_connector
592
- . item_exists( "internal/123-456/scanprefs" , "ov_maindbid |||3 " ) ) ;
617
+ . item_exists( "internal/123-456/scanprefs" , "TARGET |||127.0.0.1,10.0.0.1 " ) ) ;
593
618
594
- assert ! ( prefh. prepare_boreas_alive_test( ) . await . is_ok( ) ) ;
619
+ // Prepare and test Ports
620
+ assert ! ( prefh. prepare_ports_for_openvas( ) . await . is_ok( ) ) ;
621
+ assert ! ( prefh. redis_connector. item_exists(
622
+ "internal/123-456/scanprefs" ,
623
+ "port_range|||T:22,23,24,25,80,"
624
+ ) ) ;
625
+
626
+ // Prepare and test Credentials
627
+ assert ! ( prefh. prepare_credentials_for_openvas( ) . await . is_ok( ) ) ;
628
+ assert ! ( prefh. redis_connector. item_exists(
629
+ "internal/123-456/scanprefs" ,
630
+ "1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass"
631
+ ) ) ;
632
+ assert ! ( prefh. redis_connector. item_exists(
633
+ "internal/123-456/scanprefs" ,
634
+ "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||user"
635
+ ) ) ;
636
+
637
+ // Prepare and test Plugins
638
+ assert ! ( prefh. prepare_plugins_for_openvas( ) . await . is_ok( ) ) ;
595
639
assert ! ( prefh
596
640
. redis_connector
597
- . item_exists( "internal/123-456/scanprefs" , "ALIVE_TEST |||18 " ) ) ;
641
+ . item_exists( "internal/123-456/scanprefs" , "plugin_set |||123 " ) ) ;
598
642
643
+ // Prepare and test Main KB Index
644
+ assert ! ( prefh. prepare_main_kbindex_for_openvas( ) . await . is_ok( ) ) ;
645
+ assert ! ( prefh
646
+ . redis_connector
647
+ . item_exists( "internal/123-456/scanprefs" , "ov_maindbid|||3" ) ) ;
648
+
649
+ // Prepare and test Host Options
599
650
assert ! ( prefh. prepare_host_options_for_openvas( ) . await . is_ok( ) ) ;
600
651
assert ! ( prefh
601
652
. redis_connector
602
653
. item_exists( "internal/123-456/scanprefs" , "exclude_hosts|||127.0.0.1" ) ) ;
603
654
604
- assert ! ( prefh. prepare_credentials_for_openvas( ) . await . is_ok( ) ) ;
655
+ // Prepare and test Scan Params
656
+ assert ! ( prefh. prepare_scan_params_for_openvas( ) . await . is_ok( ) ) ;
657
+ assert ! ( prefh
658
+ . redis_connector
659
+ . item_exists( "internal/123-456/scanprefs" , "testParam1|||1" ) ) ;
660
+ assert ! ( prefh
661
+ . redis_connector
662
+ . item_exists( "internal/123-456/scanprefs" , "testParam2|||abc" ) ) ;
663
+
664
+ // Prepare and test Reverse Lookup Options
665
+ assert ! ( prefh. prepare_reverse_lookup_opt_for_openvas( ) . await . is_ok( ) ) ;
666
+ assert ! ( prefh
667
+ . redis_connector
668
+ . item_exists( "internal/123-456/scanprefs" , "reverse_lookup_only|||no" ) ) ;
669
+ assert ! ( prefh
670
+ . redis_connector
671
+ . item_exists( "internal/123-456/scanprefs" , "reverse_lookup_unify|||no" ) ) ;
672
+
673
+ // Prepare Alive Test Options
674
+ // To test this options we have to call prepare_nvt_preferences first
675
+ assert ! ( prefh. prepare_alive_test_option_for_openvas( ) . await . is_ok( ) ) ;
676
+
677
+ // Prepare NVT Preferences
678
+ assert ! ( prefh. prepare_nvt_preferences( ) . await . is_ok( ) ) ;
679
+
680
+ // Test Alive Test Options
605
681
assert ! ( prefh. redis_connector. item_exists(
606
682
"internal/123-456/scanprefs" ,
607
- "1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!): |||pass "
683
+ "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping |||yes "
608
684
) ) ;
609
685
assert ! ( prefh. redis_connector. item_exists(
610
686
"internal/123-456/scanprefs" ,
611
- "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name: |||user "
687
+ "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping |||no "
612
688
) ) ;
613
-
614
- assert ! ( prefh. prepare_ports_for_openvas( ) . await . is_ok( ) ) ;
615
689
assert ! ( prefh. redis_connector. item_exists(
616
690
"internal/123-456/scanprefs" ,
617
- "port_range|||T:22,23,24,25,80,"
691
+ "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||yes"
692
+ ) ) ;
693
+ assert ! ( prefh. redis_connector. item_exists(
694
+ "internal/123-456/scanprefs" ,
695
+ "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes"
696
+ ) ) ;
697
+ assert ! ( prefh. redis_connector. item_exists(
698
+ "internal/123-456/scanprefs" ,
699
+ "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no"
700
+ ) ) ;
701
+ assert ! ( prefh. redis_connector. item_exists(
702
+ "internal/123-456/scanprefs" ,
703
+ "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unreachable Hosts as dead (not scanning)|||yes"
618
704
) ) ;
705
+
706
+ // Test NVT Preferences
707
+ assert ! ( prefh
708
+ . redis_connector
709
+ . item_exists( "internal/123-456/scanprefs" , "123:1:checkbox:test1|||yes" ) ) ;
710
+ assert ! ( prefh
711
+ . redis_connector
712
+ . item_exists( "internal/123-456/scanprefs" , "123:2:entry:test2|||abc" ) ) ;
713
+
714
+ // Prepare Boreas Alive Test
715
+ assert ! ( prefh. prepare_boreas_alive_test( ) . await . is_ok( ) ) ;
716
+ assert ! ( prefh
717
+ . redis_connector
718
+ . item_exists( "internal/123-456/scanprefs" , "ALIVE_TEST|||18" ) ) ;
619
719
}
620
720
}
0 commit comments