@@ -23,7 +23,8 @@ pub mod fs_tests {
2323 ut_lind_fs_fcntl_valid_args ( ) ;
2424 ut_lind_fs_fcntl_invalid_args ( ) ;
2525 ut_lind_fs_fcntl_dup ( ) ;
26- ut_lind_fs_ioctl ( ) ;
26+ ut_lind_fs_ioctl_valid_args ( ) ;
27+ ut_lind_fs_ioctl_invalid_args ( ) ;
2728 ut_lind_fs_fdflags ( ) ;
2829 ut_lind_fs_file_link_unlink ( ) ;
2930 ut_lind_fs_file_lseek_past_end ( ) ;
@@ -525,48 +526,75 @@ pub mod fs_tests {
525526 }
526527
527528
528- pub fn ut_lind_fs_ioctl ( ) {
529+ pub fn ut_lind_fs_ioctl_valid_args ( ) {
529530 lindrustinit ( 0 ) ;
530531 let cage = interface:: cagetable_getref ( 1 ) ;
531532
533+ //setting up two integer values (a zero value to test clearing nonblocking I/O behavior
534+ //and a non-zero value to test setting nonblocking I/O behavior)
532535 let mut arg0: i32 = 0 ;
533536 let mut arg1: i32 = 1 ;
534537
538+ //ioctl requires a pointer to an integer to be passed with FIONBIO command
535539 let union0: IoctlPtrUnion = IoctlPtrUnion { int_ptr : & mut arg0 } ;
536540 let union1: IoctlPtrUnion = IoctlPtrUnion { int_ptr : & mut arg1 } ;
537541
538542 let sockfd = cage. socket_syscall ( AF_INET , SOCK_STREAM , 0 ) ;
539- let filefd = cage. open_syscall ( "/ioctl_file" , O_CREAT | O_EXCL , S_IRWXA ) ;
540-
541- //try to use FIONBIO for a non-socket
542- assert_eq ! (
543- cage. ioctl_syscall( filefd, FIONBIO , union0) ,
544- -( Errno :: ENOTTY as i32 )
545- ) ;
546543
547- //clear the O_NONBLOCK flag
544+ //calling ioctl with FIONBIO command and a pointer to a zero-valued integer
545+ //to clear the socket's nonblocking I/O, and checking if the flag was correctly set
548546 assert_eq ! ( cage. ioctl_syscall( sockfd, FIONBIO , union0) , 0 ) ;
549-
550- //checking to see if the flag was updated
551547 assert_eq ! ( cage. fcntl_syscall( sockfd, F_GETFL , 0 ) & O_NONBLOCK , 0 ) ;
552548
553- //set the O_NONBLOCK flag
549+ //calling ioctl with FIONBIO command and a pointer to a non-zero-valued integer
550+ //to set the socket's nonblocking I/O, and checking if the flag was correctly set
554551 assert_eq ! ( cage. ioctl_syscall( sockfd, FIONBIO , union1) , 0 ) ;
552+ assert_eq ! ( cage. fcntl_syscall( sockfd, F_GETFL , 0 ) & O_NONBLOCK , O_NONBLOCK ) ;
555553
556- //checking to see if the flag was updated
557- assert_eq ! (
558- cage. fcntl_syscall( sockfd, F_GETFL , 0 ) & O_NONBLOCK ,
559- O_NONBLOCK
560- ) ;
554+ assert_eq ! ( cage. close_syscall( sockfd) , 0 ) ;
561555
562- //clear the O_NONBLOCK flag
563- assert_eq ! ( cage. ioctl_syscall( sockfd, FIONBIO , union0) , 0 ) ;
556+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
557+ lindrustfinalize ( ) ;
558+ }
564559
565- //checking to see if the flag was updated
566- assert_eq ! ( cage. fcntl_syscall( sockfd, F_GETFL , 0 ) & O_NONBLOCK , 0 ) ;
560+ pub fn ut_lind_fs_ioctl_invalid_args ( ) {
561+ lindrustinit ( 0 ) ;
562+ let cage = interface:: cagetable_getref ( 1 ) ;
563+
564+ //setting up two integer values (a zero value to test clearing nonblocking I/O behavior on
565+ //non-socket type and a non-zero value to test setting nonblocking I/O behavior
566+ //on non-socket type)
567+ let mut arg0: i32 = 0 ;
568+ let mut arg1: i32 = 1 ;
569+
570+ //ioctl requires a pointer to an integer to be passed with FIONBIO command
571+ let union0: IoctlPtrUnion = IoctlPtrUnion { int_ptr : & mut arg0 } ;
572+ let union1: IoctlPtrUnion = IoctlPtrUnion { int_ptr : & mut arg1 } ;
567573
574+ let sockfd = cage. socket_syscall ( AF_INET , SOCK_STREAM , 0 ) ;
575+ let filefd = cage. open_syscall ( "/ioctl_file" , O_CREAT | O_EXCL , S_IRWXA ) ;
576+
577+ //trying to use FIONBIO command on a non-socket type (the file type in this case)
578+ //for any 'ptrunion' value should throw a 'Not a typewriter' error
579+ assert_eq ! ( cage. ioctl_syscall( filefd, FIONBIO , union0) , -( Errno :: ENOTTY as i32 ) ) ;
580+ assert_eq ! ( cage. ioctl_syscall( filefd, FIONBIO , union1) , -( Errno :: ENOTTY as i32 ) ) ;
568581 assert_eq ! ( cage. close_syscall( filefd) , 0 ) ;
582+
583+ //calling 'ioctl' with a control function that is not implemented yet should
584+ //return an 'Invalid argument' error
585+ //21600 is an arbitrary integer that does not correspond to any implemented
586+ //control functions for ioctl syscall
587+ assert_eq ! ( cage. ioctl_syscall( sockfd, 21600 , union0) , -( Errno :: EINVAL as i32 ) ) ;
588+
589+ //calling ioctl with FIONBIO command and a null pointer
590+ //should return a 'Bad address' error
591+ let null_ptr: * mut i32 = std:: ptr:: null_mut ( ) ;
592+ let union_null: IoctlPtrUnion = IoctlPtrUnion { int_ptr : null_ptr } ;
593+ assert_eq ! ( cage. ioctl_syscall( sockfd, FIONBIO , union_null) , -( Errno :: EFAULT as i32 ) ) ;
594+
595+ //calling ioctl on a closed file descriptor should throw a 'Bad file number' error
569596 assert_eq ! ( cage. close_syscall( sockfd) , 0 ) ;
597+ assert_eq ! ( cage. fcntl_syscall( sockfd, F_GETFL , 0 ) , -( Errno :: EBADF as i32 ) ) ;
570598
571599 assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
572600 lindrustfinalize ( ) ;
0 commit comments