1
1
use std:: os:: unix:: prelude:: * ;
2
+ use tempfile:: tempfile;
2
3
3
- use nix:: { Error , fcntl, unistd } ;
4
+ use nix:: { Error , fcntl} ;
4
5
use nix:: errno:: Errno ;
5
6
use nix:: pty:: openpty;
6
7
use nix:: sys:: termios:: { self , ECHO , OPOST , OCRNL , Termios , tcgetattr} ;
@@ -14,22 +15,27 @@ fn write_all(f: RawFd, buf: &[u8]) {
14
15
}
15
16
}
16
17
18
+ // Test tcgetattr on a terminal
17
19
#[ test]
18
- fn test_tcgetattr ( ) {
19
- for fd in 0 ..5 {
20
- let termios = termios:: tcgetattr ( fd) ;
21
- match unistd:: isatty ( fd) {
22
- // If `fd` is a TTY, tcgetattr must succeed.
23
- Ok ( true ) => assert ! ( termios. is_ok( ) ) ,
24
- // If it's an invalid file descriptor, tcgetattr should also return
25
- // the same error
26
- Err ( Error :: Sys ( Errno :: EBADF ) ) => {
27
- assert_eq ! ( termios. err( ) , Some ( Error :: Sys ( Errno :: EBADF ) ) ) ;
28
- } ,
29
- // Otherwise it should return any error
30
- _ => assert ! ( termios. is_err( ) )
31
- }
32
- }
20
+ fn test_tcgetattr_pty ( ) {
21
+ let pty = openpty ( None , None ) . unwrap ( ) ;
22
+ assert ! ( termios:: tcgetattr( pty. master) . is_ok( ) ) ;
23
+ close ( pty. master ) . unwrap ( ) ;
24
+ close ( pty. slave ) . unwrap ( ) ;
25
+ }
26
+ // Test tcgetattr on something that isn't a terminal
27
+ #[ test]
28
+ fn test_tcgetattr_enotty ( ) {
29
+ let file = tempfile ( ) . unwrap ( ) ;
30
+ assert_eq ! ( termios:: tcgetattr( file. as_raw_fd( ) ) . err( ) ,
31
+ Some ( Error :: Sys ( Errno :: ENOTTY ) ) ) ;
32
+ }
33
+
34
+ // Test tcgetattr on an invalid file descriptor
35
+ #[ test]
36
+ fn test_tcgetattr_ebadf ( ) {
37
+ assert_eq ! ( termios:: tcgetattr( -1 ) . err( ) ,
38
+ Some ( Error :: Sys ( Errno :: EBADF ) ) ) ;
33
39
}
34
40
35
41
// Test modifying output flags
@@ -41,6 +47,8 @@ fn test_output_flags() {
41
47
assert ! ( pty. master > 0 ) ;
42
48
assert ! ( pty. slave > 0 ) ;
43
49
let termios = tcgetattr ( pty. master ) . unwrap ( ) ;
50
+ close ( pty. master ) . unwrap ( ) ;
51
+ close ( pty. slave ) . unwrap ( ) ;
44
52
termios
45
53
} ;
46
54
@@ -64,6 +72,8 @@ fn test_output_flags() {
64
72
let mut buf = [ 0u8 ; 10 ] ;
65
73
:: read_exact ( pty. slave , & mut buf) ;
66
74
let transformed_string = "foofoofoo\n " ;
75
+ close ( pty. master ) . unwrap ( ) ;
76
+ close ( pty. slave ) . unwrap ( ) ;
67
77
assert_eq ! ( & buf, transformed_string. as_bytes( ) ) ;
68
78
}
69
79
@@ -76,6 +86,8 @@ fn test_local_flags() {
76
86
assert ! ( pty. master > 0 ) ;
77
87
assert ! ( pty. slave > 0 ) ;
78
88
let termios = tcgetattr ( pty. master ) . unwrap ( ) ;
89
+ close ( pty. master ) . unwrap ( ) ;
90
+ close ( pty. slave ) . unwrap ( ) ;
79
91
termios
80
92
} ;
81
93
@@ -102,6 +114,8 @@ fn test_local_flags() {
102
114
// Try to read from the master, which should not have anything as echoing was disabled.
103
115
let mut buf = [ 0u8 ; 10 ] ;
104
116
let read = read ( pty. master , & mut buf) . unwrap_err ( ) ;
117
+ close ( pty. master ) . unwrap ( ) ;
118
+ close ( pty. slave ) . unwrap ( ) ;
105
119
assert_eq ! ( read, Error :: Sys ( Errno :: EAGAIN ) ) ;
106
120
}
107
121
0 commit comments