@@ -327,14 +327,14 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
327
327
#[ cfg( test) ]
328
328
mod tests {
329
329
#![ allow( clippy:: undocumented_unsafe_blocks) ]
330
- #![ allow( clippy:: cast_possible_truncation) ]
331
330
332
331
use std:: os:: unix:: net:: UnixStream ;
333
332
334
333
use utils:: tempfile:: TempFile ;
335
334
use vhost:: { VhostUserMemoryRegionInfo , VringConfigData } ;
336
335
337
336
use super :: * ;
337
+ use crate :: utilities:: test_utils:: create_tmp_socket;
338
338
use crate :: vstate:: memory:: { FileOffset , GuestAddress , GuestMemoryExtension } ;
339
339
340
340
#[ test]
@@ -434,40 +434,7 @@ mod tests {
434
434
}
435
435
}
436
436
437
- let tmp_dir = utils:: tempdir:: TempDir :: new ( ) . unwrap ( ) ;
438
- let tmp_dir_path_str = tmp_dir. as_path ( ) . to_str ( ) . unwrap ( ) ;
439
- let tmp_socket_path = format ! ( "{tmp_dir_path_str}/tmp_socket" ) ;
440
-
441
- unsafe {
442
- let socketfd = libc:: socket ( libc:: AF_UNIX , libc:: SOCK_STREAM , 0 ) ;
443
- if socketfd < 0 {
444
- panic ! ( "Cannot create socket" ) ;
445
- }
446
- let mut socket_addr = libc:: sockaddr_un {
447
- sun_family : libc:: AF_UNIX as u16 ,
448
- sun_path : [ 0 ; 108 ] ,
449
- } ;
450
-
451
- std:: ptr:: copy :: < i8 > (
452
- tmp_socket_path. as_ptr ( ) . cast ( ) ,
453
- socket_addr. sun_path . as_mut_ptr ( ) ,
454
- tmp_socket_path. as_bytes ( ) . len ( ) ,
455
- ) ;
456
-
457
- let bind = libc:: bind (
458
- socketfd,
459
- ( & socket_addr as * const libc:: sockaddr_un ) . cast ( ) ,
460
- std:: mem:: size_of :: < libc:: sockaddr_un > ( ) as u32 ,
461
- ) ;
462
- if bind < 0 {
463
- panic ! ( "Cannot bind socket" ) ;
464
- }
465
-
466
- let listen = libc:: listen ( socketfd, 1 ) ;
467
- if listen < 0 {
468
- panic ! ( "Cannot listen on socket" ) ;
469
- }
470
- }
437
+ let ( _tmp_dir, tmp_socket_path) = create_tmp_socket ( ) ;
471
438
472
439
let vhost_block_config = VhostUserBlockConfig {
473
440
drive_id : "test_drive" . to_string ( ) ,
@@ -567,40 +534,7 @@ mod tests {
567
534
}
568
535
}
569
536
570
- let tmp_dir = utils:: tempdir:: TempDir :: new ( ) . unwrap ( ) ;
571
- let tmp_dir_path_str = tmp_dir. as_path ( ) . to_str ( ) . unwrap ( ) ;
572
- let tmp_socket_path = format ! ( "{tmp_dir_path_str}/tmp_socket" ) ;
573
-
574
- unsafe {
575
- let socketfd = libc:: socket ( libc:: AF_UNIX , libc:: SOCK_STREAM , 0 ) ;
576
- if socketfd < 0 {
577
- panic ! ( "Cannot create socket" ) ;
578
- }
579
- let mut socket_addr = libc:: sockaddr_un {
580
- sun_family : libc:: AF_UNIX as u16 ,
581
- sun_path : [ 0 ; 108 ] ,
582
- } ;
583
-
584
- std:: ptr:: copy :: < i8 > (
585
- tmp_socket_path. as_ptr ( ) . cast ( ) ,
586
- socket_addr. sun_path . as_mut_ptr ( ) ,
587
- tmp_socket_path. as_bytes ( ) . len ( ) ,
588
- ) ;
589
-
590
- let bind = libc:: bind (
591
- socketfd,
592
- ( & socket_addr as * const libc:: sockaddr_un ) . cast ( ) ,
593
- std:: mem:: size_of :: < libc:: sockaddr_un > ( ) as u32 ,
594
- ) ;
595
- if bind < 0 {
596
- panic ! ( "Cannot bind socket" ) ;
597
- }
598
-
599
- let listen = libc:: listen ( socketfd, 1 ) ;
600
- if listen < 0 {
601
- panic ! ( "Cannot listen on socket" ) ;
602
- }
603
- }
537
+ let ( _tmp_dir, tmp_socket_path) = create_tmp_socket ( ) ;
604
538
605
539
let vhost_block_config = VhostUserBlockConfig {
606
540
drive_id : "test_drive" . to_string ( ) ,
@@ -778,41 +712,7 @@ mod tests {
778
712
}
779
713
780
714
// Block creation
781
- let tmp_dir = utils:: tempdir:: TempDir :: new ( ) . unwrap ( ) ;
782
- let tmp_dir_path_str = tmp_dir. as_path ( ) . to_str ( ) . unwrap ( ) ;
783
- let tmp_socket_path = format ! ( "{tmp_dir_path_str}/tmp_socket" ) ;
784
-
785
- unsafe {
786
- let socketfd = libc:: socket ( libc:: AF_UNIX , libc:: SOCK_STREAM , 0 ) ;
787
- if socketfd < 0 {
788
- panic ! ( "Cannot create socket" ) ;
789
- }
790
- let mut socket_addr = libc:: sockaddr_un {
791
- sun_family : libc:: AF_UNIX as u16 ,
792
- sun_path : [ 0 ; 108 ] ,
793
- } ;
794
-
795
- std:: ptr:: copy :: < i8 > (
796
- tmp_socket_path. as_ptr ( ) . cast ( ) ,
797
- socket_addr. sun_path . as_mut_ptr ( ) ,
798
- tmp_socket_path. as_bytes ( ) . len ( ) ,
799
- ) ;
800
-
801
- let bind = libc:: bind (
802
- socketfd,
803
- ( & socket_addr as * const libc:: sockaddr_un ) . cast ( ) ,
804
- std:: mem:: size_of :: < libc:: sockaddr_un > ( ) as u32 ,
805
- ) ;
806
- if bind < 0 {
807
- panic ! ( "Cannot bind socket" ) ;
808
- }
809
-
810
- let listen = libc:: listen ( socketfd, 1 ) ;
811
- if listen < 0 {
812
- panic ! ( "Cannot listen on socket" ) ;
813
- }
814
- }
815
-
715
+ let ( _tmp_dir, tmp_socket_path) = create_tmp_socket ( ) ;
816
716
let vhost_block_config = VhostUserBlockConfig {
817
717
drive_id : "test_drive" . to_string ( ) ,
818
718
partuuid : None ,
0 commit comments