@@ -336,14 +336,14 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
336
336
#[ cfg( test) ]
337
337
mod tests {
338
338
#![ allow( clippy:: undocumented_unsafe_blocks) ]
339
- #![ allow( clippy:: cast_possible_truncation) ]
340
339
341
340
use std:: os:: unix:: net:: UnixStream ;
342
341
343
342
use utils:: tempfile:: TempFile ;
344
343
use vhost:: { VhostUserMemoryRegionInfo , VringConfigData } ;
345
344
346
345
use super :: * ;
346
+ use crate :: utilities:: test_utils:: create_tmp_socket;
347
347
use crate :: vstate:: memory:: { FileOffset , GuestAddress , GuestMemoryExtension } ;
348
348
349
349
#[ test]
@@ -395,40 +395,7 @@ mod tests {
395
395
}
396
396
}
397
397
398
- let tmp_dir = utils:: tempdir:: TempDir :: new ( ) . unwrap ( ) ;
399
- let tmp_dir_path_str = tmp_dir. as_path ( ) . to_str ( ) . unwrap ( ) ;
400
- let tmp_socket_path = format ! ( "{tmp_dir_path_str}/tmp_socket" ) ;
401
-
402
- unsafe {
403
- let socketfd = libc:: socket ( libc:: AF_UNIX , libc:: SOCK_STREAM , 0 ) ;
404
- if socketfd < 0 {
405
- panic ! ( "Cannot create socket" ) ;
406
- }
407
- let mut socket_addr = libc:: sockaddr_un {
408
- sun_family : libc:: AF_UNIX as u16 ,
409
- sun_path : [ 0 ; 108 ] ,
410
- } ;
411
-
412
- std:: ptr:: copy :: < i8 > (
413
- tmp_socket_path. as_ptr ( ) . cast ( ) ,
414
- socket_addr. sun_path . as_mut_ptr ( ) ,
415
- tmp_socket_path. as_bytes ( ) . len ( ) ,
416
- ) ;
417
-
418
- let bind = libc:: bind (
419
- socketfd,
420
- ( & socket_addr as * const libc:: sockaddr_un ) . cast ( ) ,
421
- std:: mem:: size_of :: < libc:: sockaddr_un > ( ) as u32 ,
422
- ) ;
423
- if bind < 0 {
424
- panic ! ( "Cannot bind socket" ) ;
425
- }
426
-
427
- let listen = libc:: listen ( socketfd, 1 ) ;
428
- if listen < 0 {
429
- panic ! ( "Cannot listen on socket" ) ;
430
- }
431
- }
398
+ let ( _tmp_dir, tmp_socket_path) = create_tmp_socket ( ) ;
432
399
433
400
let vhost_block_config = VhostUserBlockConfig {
434
401
drive_id : "test_drive" . to_string ( ) ,
@@ -526,40 +493,7 @@ mod tests {
526
493
}
527
494
}
528
495
529
- let tmp_dir = utils:: tempdir:: TempDir :: new ( ) . unwrap ( ) ;
530
- let tmp_dir_path_str = tmp_dir. as_path ( ) . to_str ( ) . unwrap ( ) ;
531
- let tmp_socket_path = format ! ( "{tmp_dir_path_str}/tmp_socket" ) ;
532
-
533
- unsafe {
534
- let socketfd = libc:: socket ( libc:: AF_UNIX , libc:: SOCK_STREAM , 0 ) ;
535
- if socketfd < 0 {
536
- panic ! ( "Cannot create socket" ) ;
537
- }
538
- let mut socket_addr = libc:: sockaddr_un {
539
- sun_family : libc:: AF_UNIX as u16 ,
540
- sun_path : [ 0 ; 108 ] ,
541
- } ;
542
-
543
- std:: ptr:: copy :: < i8 > (
544
- tmp_socket_path. as_ptr ( ) . cast ( ) ,
545
- socket_addr. sun_path . as_mut_ptr ( ) ,
546
- tmp_socket_path. as_bytes ( ) . len ( ) ,
547
- ) ;
548
-
549
- let bind = libc:: bind (
550
- socketfd,
551
- ( & socket_addr as * const libc:: sockaddr_un ) . cast ( ) ,
552
- std:: mem:: size_of :: < libc:: sockaddr_un > ( ) as u32 ,
553
- ) ;
554
- if bind < 0 {
555
- panic ! ( "Cannot bind socket" ) ;
556
- }
557
-
558
- let listen = libc:: listen ( socketfd, 1 ) ;
559
- if listen < 0 {
560
- panic ! ( "Cannot listen on socket" ) ;
561
- }
562
- }
496
+ let ( _tmp_dir, tmp_socket_path) = create_tmp_socket ( ) ;
563
497
564
498
let vhost_block_config = VhostUserBlockConfig {
565
499
drive_id : "test_drive" . to_string ( ) ,
@@ -711,41 +645,7 @@ mod tests {
711
645
}
712
646
713
647
// Block creation
714
- let tmp_dir = utils:: tempdir:: TempDir :: new ( ) . unwrap ( ) ;
715
- let tmp_dir_path_str = tmp_dir. as_path ( ) . to_str ( ) . unwrap ( ) ;
716
- let tmp_socket_path = format ! ( "{tmp_dir_path_str}/tmp_socket" ) ;
717
-
718
- unsafe {
719
- let socketfd = libc:: socket ( libc:: AF_UNIX , libc:: SOCK_STREAM , 0 ) ;
720
- if socketfd < 0 {
721
- panic ! ( "Cannot create socket" ) ;
722
- }
723
- let mut socket_addr = libc:: sockaddr_un {
724
- sun_family : libc:: AF_UNIX as u16 ,
725
- sun_path : [ 0 ; 108 ] ,
726
- } ;
727
-
728
- std:: ptr:: copy :: < i8 > (
729
- tmp_socket_path. as_ptr ( ) . cast ( ) ,
730
- socket_addr. sun_path . as_mut_ptr ( ) ,
731
- tmp_socket_path. as_bytes ( ) . len ( ) ,
732
- ) ;
733
-
734
- let bind = libc:: bind (
735
- socketfd,
736
- ( & socket_addr as * const libc:: sockaddr_un ) . cast ( ) ,
737
- std:: mem:: size_of :: < libc:: sockaddr_un > ( ) as u32 ,
738
- ) ;
739
- if bind < 0 {
740
- panic ! ( "Cannot bind socket" ) ;
741
- }
742
-
743
- let listen = libc:: listen ( socketfd, 1 ) ;
744
- if listen < 0 {
745
- panic ! ( "Cannot listen on socket" ) ;
746
- }
747
- }
748
-
648
+ let ( _tmp_dir, tmp_socket_path) = create_tmp_socket ( ) ;
749
649
let vhost_block_config = VhostUserBlockConfig {
750
650
drive_id : "test_drive" . to_string ( ) ,
751
651
partuuid : None ,
0 commit comments