|
| 1 | +use libc; |
| 2 | + |
| 3 | +use nix::pty::openpty; |
| 4 | +use nix::sys::termios::*; |
| 5 | +use nix::unistd::{read, write, close}; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn test_openpty() { |
| 9 | + // TODO: figure out the right termios settings to pass in here |
| 10 | + println!("foo"); |
| 11 | + let mut termios = tcgetattr(libc::STDIN_FILENO).unwrap(); |
| 12 | + println!("bar"); |
| 13 | + termios.c_oflag &= !ONLCR; |
| 14 | + |
| 15 | + let pty = openpty(None, &termios).unwrap(); |
| 16 | + assert!(pty.master > 0); // must be valid file descriptors |
| 17 | + assert!(pty.slave > 0); |
| 18 | + |
| 19 | + // writing to one should be readable on the other one |
| 20 | + let string = "foofoofoo\n"; |
| 21 | + let mut buf = [0u8; 16]; |
| 22 | + write(pty.master, string.as_bytes()).unwrap(); |
| 23 | + let len = read(pty.slave, &mut buf).unwrap(); |
| 24 | + |
| 25 | + assert_eq!(len, string.len()); |
| 26 | + assert_eq!(&buf[0..len], string.as_bytes()); |
| 27 | + |
| 28 | + // read the echo as well |
| 29 | + let echoed_string = "foofoofoo\n"; |
| 30 | + let len = read(pty.master, &mut buf).unwrap(); |
| 31 | + assert_eq!(len, echoed_string.len()); |
| 32 | + assert_eq!(&buf[0..len], echoed_string.as_bytes()); |
| 33 | + |
| 34 | + let string2 = "barbarbarbar\n"; |
| 35 | + let echoed_string2 = "barbarbarbar\n"; |
| 36 | + write(pty.slave, string2.as_bytes()).unwrap(); |
| 37 | + let len = read(pty.master, &mut buf).unwrap(); |
| 38 | + |
| 39 | + assert_eq!(len, echoed_string2.len()); |
| 40 | + assert_eq!(&buf[0..len], echoed_string2.as_bytes()); |
| 41 | + |
| 42 | + close(pty.master).unwrap(); |
| 43 | + close(pty.slave).unwrap(); |
| 44 | +} |
0 commit comments