1- use std:: os:: unix:: prelude :: * ;
1+ use std:: os:: unix:: io :: { AsFd , AsRawFd } ;
22use tempfile:: tempfile;
33
44use nix:: errno:: Errno ;
55use nix:: fcntl;
66use nix:: pty:: openpty;
77use nix:: sys:: termios:: { self , tcgetattr, LocalFlags , OutputFlags } ;
8- use nix:: unistd:: { close , read, write} ;
8+ use nix:: unistd:: { read, write} ;
99
10- /// Helper function analogous to `std::io::Write::write_all`, but for `RawFd `s
11- fn write_all ( f : RawFd , buf : & [ u8 ] ) {
10+ /// Helper function analogous to `std::io::Write::write_all`, but for `Fd `s
11+ fn write_all < Fd : AsFd > ( f : & Fd , buf : & [ u8 ] ) {
1212 let mut len = 0 ;
1313 while len < buf. len ( ) {
14- len += write ( f, & buf[ len..] ) . unwrap ( ) ;
14+ len += write ( f. as_fd ( ) . as_raw_fd ( ) , & buf[ len..] ) . unwrap ( ) ;
1515 }
1616}
1717
@@ -22,25 +22,14 @@ fn test_tcgetattr_pty() {
2222 let _m = crate :: PTSNAME_MTX . lock ( ) ;
2323
2424 let pty = openpty ( None , None ) . expect ( "openpty failed" ) ;
25- termios:: tcgetattr ( pty. slave ) . unwrap ( ) ;
26- close ( pty. master ) . expect ( "closing the master failed" ) ;
27- close ( pty. slave ) . expect ( "closing the slave failed" ) ;
25+ termios:: tcgetattr ( & pty. slave ) . unwrap ( ) ;
2826}
2927
3028// Test tcgetattr on something that isn't a terminal
3129#[ test]
3230fn test_tcgetattr_enotty ( ) {
3331 let file = tempfile ( ) . unwrap ( ) ;
34- assert_eq ! (
35- termios:: tcgetattr( file. as_raw_fd( ) ) . err( ) ,
36- Some ( Errno :: ENOTTY )
37- ) ;
38- }
39-
40- // Test tcgetattr on an invalid file descriptor
41- #[ test]
42- fn test_tcgetattr_ebadf ( ) {
43- assert_eq ! ( termios:: tcgetattr( -1 ) . err( ) , Some ( Errno :: EBADF ) ) ;
32+ assert_eq ! ( termios:: tcgetattr( & file) . err( ) , Some ( Errno :: ENOTTY ) ) ;
4433}
4534
4635// Test modifying output flags
@@ -52,12 +41,7 @@ fn test_output_flags() {
5241 // Open one pty to get attributes for the second one
5342 let mut termios = {
5443 let pty = openpty ( None , None ) . expect ( "openpty failed" ) ;
55- assert ! ( pty. master > 0 ) ;
56- assert ! ( pty. slave > 0 ) ;
57- let termios = tcgetattr ( pty. slave ) . expect ( "tcgetattr failed" ) ;
58- close ( pty. master ) . unwrap ( ) ;
59- close ( pty. slave ) . unwrap ( ) ;
60- termios
44+ tcgetattr ( & pty. slave ) . expect ( "tcgetattr failed" )
6145 } ;
6246
6347 // Make sure postprocessing '\r' isn't specified by default or this test is useless.
@@ -73,19 +57,15 @@ fn test_output_flags() {
7357
7458 // Open a pty
7559 let pty = openpty ( None , & termios) . unwrap ( ) ;
76- assert ! ( pty. master > 0 ) ;
77- assert ! ( pty. slave > 0 ) ;
7860
7961 // Write into the master
8062 let string = "foofoofoo\r " ;
81- write_all ( pty. master , string. as_bytes ( ) ) ;
63+ write_all ( & pty. master , string. as_bytes ( ) ) ;
8264
8365 // Read from the slave verifying that the output has been properly transformed
8466 let mut buf = [ 0u8 ; 10 ] ;
85- crate :: read_exact ( pty. slave , & mut buf) ;
67+ crate :: read_exact ( & pty. slave , & mut buf) ;
8668 let transformed_string = "foofoofoo\n " ;
87- close ( pty. master ) . unwrap ( ) ;
88- close ( pty. slave ) . unwrap ( ) ;
8969 assert_eq ! ( & buf, transformed_string. as_bytes( ) ) ;
9070}
9171
@@ -98,12 +78,7 @@ fn test_local_flags() {
9878 // Open one pty to get attributes for the second one
9979 let mut termios = {
10080 let pty = openpty ( None , None ) . unwrap ( ) ;
101- assert ! ( pty. master > 0 ) ;
102- assert ! ( pty. slave > 0 ) ;
103- let termios = tcgetattr ( pty. slave ) . unwrap ( ) ;
104- close ( pty. master ) . unwrap ( ) ;
105- close ( pty. slave ) . unwrap ( ) ;
106- termios
81+ tcgetattr ( & pty. slave ) . unwrap ( )
10782 } ;
10883
10984 // Make sure echo is specified by default or this test is useless.
@@ -114,23 +89,19 @@ fn test_local_flags() {
11489
11590 // Open a new pty with our modified termios settings
11691 let pty = openpty ( None , & termios) . unwrap ( ) ;
117- assert ! ( pty. master > 0 ) ;
118- assert ! ( pty. slave > 0 ) ;
11992
12093 // Set the master is in nonblocking mode or reading will never return.
121- let flags = fcntl:: fcntl ( pty. master , fcntl:: F_GETFL ) . unwrap ( ) ;
94+ let flags = fcntl:: fcntl ( pty. master . as_raw_fd ( ) , fcntl:: F_GETFL ) . unwrap ( ) ;
12295 let new_flags =
12396 fcntl:: OFlag :: from_bits_truncate ( flags) | fcntl:: OFlag :: O_NONBLOCK ;
124- fcntl:: fcntl ( pty. master , fcntl:: F_SETFL ( new_flags) ) . unwrap ( ) ;
97+ fcntl:: fcntl ( pty. master . as_raw_fd ( ) , fcntl:: F_SETFL ( new_flags) ) . unwrap ( ) ;
12598
12699 // Write into the master
127100 let string = "foofoofoo\r " ;
128- write_all ( pty. master , string. as_bytes ( ) ) ;
101+ write_all ( & pty. master , string. as_bytes ( ) ) ;
129102
130103 // Try to read from the master, which should not have anything as echoing was disabled.
131104 let mut buf = [ 0u8 ; 10 ] ;
132- let read = read ( pty. master , & mut buf) . unwrap_err ( ) ;
133- close ( pty. master ) . unwrap ( ) ;
134- close ( pty. slave ) . unwrap ( ) ;
105+ let read = read ( pty. master . as_raw_fd ( ) , & mut buf) . unwrap_err ( ) ;
135106 assert_eq ! ( read, Errno :: EAGAIN ) ;
136107}
0 commit comments