Debian Codegraph @

debian/rust-vendor/nix/test/test_pty.rs

raw
1 use std::fs::File;
2 use std::io::{stdout, Read, Write};
3 use std::os::unix::prelude::*;
4 use std::path::Path;
5
6 use libc::_exit;
7 use nix::fcntl::{open, OFlag};
8 use nix::pty::*;
9 use nix::sys::stat;
10 use nix::sys::termios::*;
11 use nix::sys::wait::WaitStatus;
12 use nix::unistd::{pause, write};
13
14 /// Test equivalence of `ptsname` and `ptsname_r`
15 #[test]
16 #[cfg(linux_android)]
17 fn test_ptsname_equivalence() {
18 let _m = crate::PTSNAME_MTX.lock();
19
20 // Open a new PTY master
21 let master_fd = posix_openpt(OFlag::O_RDWR).unwrap();
22 assert!(master_fd.as_raw_fd() > 0);
23 assert!(master_fd.as_fd().as_raw_fd() == master_fd.as_raw_fd());
24
25 // Get the name of the slave
26 let slave_name = unsafe { ptsname(&master_fd) }.unwrap();
27 let slave_name_r = ptsname_r(&master_fd).unwrap();
28 assert_eq!(slave_name, slave_name_r);
29 }
30
31 /// Test data copying of `ptsname`
32 // TODO need to run in a subprocess, since ptsname is non-reentrant
33 #[test]
34 #[cfg(linux_android)]
35 fn test_ptsname_copy() {
36 let _m = crate::PTSNAME_MTX.lock();
37
38 // Open a new PTTY master
39 let master_fd = posix_openpt(OFlag::O_RDWR).unwrap();
40
41 // Get the name of the slave
42 let slave_name1 = unsafe { ptsname(&master_fd) }.unwrap();
43 let slave_name2 = unsafe { ptsname(&master_fd) }.unwrap();
44 assert_eq!(slave_name1, slave_name2);
45 // Also make sure that the string was actually copied and they point to different parts of
46 // memory.
47 assert_ne!(slave_name1.as_ptr(), slave_name2.as_ptr());
48 }
49
50 /// Test data copying of `ptsname_r`
51 #[test]
52 #[cfg(linux_android)]
53 fn test_ptsname_r_copy() {
54 // Open a new PTTY master
55 let master_fd = posix_openpt(OFlag::O_RDWR).unwrap();
56
57 // Get the name of the slave
58 let slave_name1 = ptsname_r(&master_fd).unwrap();
59 let slave_name2 = ptsname_r(&master_fd).unwrap();
60 assert_eq!(slave_name1, slave_name2);
61 assert_ne!(slave_name1.as_ptr(), slave_name2.as_ptr());
62 }
63
64 /// Test that `ptsname` returns different names for different devices
65 #[test]
66 #[cfg(linux_android)]
67 fn test_ptsname_unique() {
68 let _m = crate::PTSNAME_MTX.lock();
69
70 // Open a new PTTY master
71 let master1_fd = posix_openpt(OFlag::O_RDWR).unwrap();
72
73 // Open a second PTTY master
74 let master2_fd = posix_openpt(OFlag::O_RDWR).unwrap();
75
76 // Get the name of the slave
77 let slave_name1 = unsafe { ptsname(&master1_fd) }.unwrap();
78 let slave_name2 = unsafe { ptsname(&master2_fd) }.unwrap();
79 assert_ne!(slave_name1, slave_name2);
80 }
81
82 /// Common setup for testing PTTY pairs
83 fn open_ptty_pair() -> (PtyMaster, File) {
84 let _m = crate::PTSNAME_MTX.lock();
85
86 // Open a new PTTY master
87 let master = posix_openpt(OFlag::O_RDWR).expect("posix_openpt failed");
88
89 // Allow a slave to be generated for it
90 grantpt(&master).expect("grantpt failed");
91 unlockpt(&master).expect("unlockpt failed");
92
93 // Get the name of the slave
94 let slave_name = unsafe { ptsname(&master) }.expect("ptsname failed");
95
96 // Open the slave device
97 let slave_fd =
98 open(Path::new(&slave_name), OFlag::O_RDWR, stat::Mode::empty())
99 .unwrap();
100
101 #[cfg(solarish)]
102 // TODO: rewrite using ioctl!
103 #[allow(clippy::comparison_chain)]
104 {
105 use libc::{ioctl, I_FIND, I_PUSH};
106
107 // On illumos systems, as per pts(7D), one must push STREAMS modules
108 // after opening a device path returned from ptsname().
109 let ptem = b"ptem\0";
110 let ldterm = b"ldterm\0";
111 let r = unsafe { ioctl(slave_fd.as_raw_fd(), I_FIND, ldterm.as_ptr()) };
112 if r < 0 {
113 panic!("I_FIND failure");
114 } else if r == 0 {
115 if unsafe { ioctl(slave_fd.as_raw_fd(), I_PUSH, ptem.as_ptr()) } < 0
116 {
117 panic!("I_PUSH ptem failure");
118 }
119 if unsafe { ioctl(slave_fd.as_raw_fd(), I_PUSH, ldterm.as_ptr()) }
120 < 0
121 {
122 panic!("I_PUSH ldterm failure");
123 }
124 }
125 }
126
127 let slave = File::from(slave_fd);
128
129 (master, slave)
130 }
131
132 /// Test opening a master/slave PTTY pair
133 ///
134 /// This uses a common `open_ptty_pair` because much of these functions aren't useful by
135 /// themselves. So for this test we perform the basic act of getting a file handle for a
136 /// master/slave PTTY pair.
137 #[test]
138 fn test_open_ptty_pair() {
139 let (_, _) = open_ptty_pair();
140 }
141
142 /// Put the terminal in raw mode.
143 fn make_raw<Fd: AsFd>(fd: Fd) {
144 let mut termios = tcgetattr(&fd).unwrap();
145 cfmakeraw(&mut termios);
146 tcsetattr(&fd, SetArg::TCSANOW, &termios).unwrap();
147 }
148
149 /// Test `io::Read` on the PTTY master
150 #[test]
151 #[cfg(not(target_os = "solaris"))]
152 fn test_read_ptty_pair() {
153 let (mut master, mut slave) = open_ptty_pair();
154 make_raw(&slave);
155
156 let mut buf = [0u8; 5];
157 slave.write_all(b"hello").unwrap();
158 master.read_exact(&mut buf).unwrap();
159 assert_eq!(&buf, b"hello");
160
161 let mut master = &master;
162 slave.write_all(b"hello").unwrap();
163 master.read_exact(&mut buf).unwrap();
164 assert_eq!(&buf, b"hello");
165 }
166
167 /// Test `io::Write` on the PTTY master
168 #[test]
169 fn test_write_ptty_pair() {
170 let (mut master, mut slave) = open_ptty_pair();
171 make_raw(&slave);
172
173 let mut buf = [0u8; 5];
174 master.write_all(b"adios").unwrap();
175 slave.read_exact(&mut buf).unwrap();
176 assert_eq!(&buf, b"adios");
177
178 let mut master = &master;
179 master.write_all(b"adios").unwrap();
180 slave.read_exact(&mut buf).unwrap();
181 assert_eq!(&buf, b"adios");
182 }
183
184 #[test]
185 fn test_openpty() {
186 // openpty uses ptname(3) internally
187 let _m = crate::PTSNAME_MTX.lock();
188
189 let pty = openpty(None, None).unwrap();
190
191 // Writing to one should be readable on the other one
192 let string = "foofoofoo\n";
193 let mut buf = [0u8; 10];
194 write(&pty.master, string.as_bytes()).unwrap();
195 crate::read_exact(&pty.slave, &mut buf);
196
197 assert_eq!(&buf, string.as_bytes());
198
199 // Read the echo as well
200 let echoed_string = "foofoofoo\r\n";
201 let mut buf = [0u8; 11];
202 crate::read_exact(&pty.master, &mut buf);
203 assert_eq!(&buf, echoed_string.as_bytes());
204
205 let string2 = "barbarbarbar\n";
206 let echoed_string2 = "barbarbarbar\r\n";
207 let mut buf = [0u8; 14];
208 write(&pty.slave, string2.as_bytes()).unwrap();
209 crate::read_exact(&pty.master, &mut buf);
210
211 assert_eq!(&buf, echoed_string2.as_bytes());
212 }
213
214 #[test]
215 fn test_openpty_with_termios() {
216 // openpty uses ptname(3) internally
217 let _m = crate::PTSNAME_MTX.lock();
218
219 // Open one pty to get attributes for the second one
220 let mut termios = {
221 let pty = openpty(None, None).unwrap();
222 tcgetattr(&pty.slave).unwrap()
223 };
224 // Make sure newlines are not transformed so the data is preserved when sent.
225 termios.output_flags.remove(OutputFlags::ONLCR);
226
227 let pty = openpty(None, &termios).unwrap();
228 // Must be valid file descriptors
229
230 // Writing to one should be readable on the other one
231 let string = "foofoofoo\n";
232 let mut buf = [0u8; 10];
233 write(&pty.master, string.as_bytes()).unwrap();
234 crate::read_exact(&pty.slave, &mut buf);
235
236 assert_eq!(&buf, string.as_bytes());
237
238 // read the echo as well
239 let echoed_string = "foofoofoo\n";
240 crate::read_exact(&pty.master, &mut buf);
241 assert_eq!(&buf, echoed_string.as_bytes());
242
243 let string2 = "barbarbarbar\n";
244 let echoed_string2 = "barbarbarbar\n";
245 let mut buf = [0u8; 13];
246 write(&pty.slave, string2.as_bytes()).unwrap();
247 crate::read_exact(&pty.master, &mut buf);
248
249 assert_eq!(&buf, echoed_string2.as_bytes());
250 }
251
252 #[test]
253 fn test_forkpty() {
254 use nix::sys::signal::*;
255 use nix::sys::wait::wait;
256 // forkpty calls openpty which uses ptname(3) internally.
257 let _m0 = crate::PTSNAME_MTX.lock();
258 // forkpty spawns a child process
259 let _m1 = crate::FORK_MTX.lock();
260
261 let string = "naninani\n";
262 let echoed_string = "naninani\r\n";
263 let res = unsafe { forkpty(None, None).unwrap() };
264 match res {
265 ForkptyResult::Child => {
266 write(stdout(), string.as_bytes()).unwrap();
267 pause(); // we need the child to stay alive until the parent calls read
268 unsafe {
269 _exit(0);
270 }
271 }
272 ForkptyResult::Parent { child, master } => {
273 let mut buf = [0u8; 10];
274 assert!(child.as_raw() > 0);
275 crate::read_exact(&master, &mut buf);
276 kill(child, SIGTERM).unwrap();
277 let status = wait().unwrap(); // keep other tests using generic wait from getting our child
278 assert_eq!(status, WaitStatus::Signaled(child, SIGTERM, false));
279 assert_eq!(&buf, echoed_string.as_bytes());
280 }
281 }
282 }