|
| 1 | +use nix::pty::openpty; |
| 2 | +use nix::sys::termios::*; |
| 3 | +use nix::unistd::{read, write, close}; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_openpty() { |
| 7 | + let pty = openpty(None, None).unwrap(); |
| 8 | + assert!(pty.master > 0); // must be valid file descriptors |
| 9 | + assert!(pty.slave > 0); |
| 10 | + |
| 11 | + // writing to one should be readable on the other one |
| 12 | + let string = "foofoofoo\n"; |
| 13 | + let mut buf = [0u8; 16]; |
| 14 | + write(pty.master, string.as_bytes()).unwrap(); |
| 15 | + let len = read(pty.slave, &mut buf).unwrap(); |
| 16 | + |
| 17 | + assert_eq!(len, string.len()); |
| 18 | + assert_eq!(&buf[0..len], string.as_bytes()); |
| 19 | + |
| 20 | + // read the echo as well |
| 21 | + let echoed_string = "foofoofoo\r\n"; |
| 22 | + let len = read(pty.master, &mut buf).unwrap(); |
| 23 | + assert_eq!(len, echoed_string.len()); |
| 24 | + assert_eq!(&buf[0..len], echoed_string.as_bytes()); |
| 25 | + |
| 26 | + let string2 = "barbarbarbar\n"; |
| 27 | + let echoed_string2 = "barbarbarbar\r\n"; |
| 28 | + write(pty.slave, string2.as_bytes()).unwrap(); |
| 29 | + let len = read(pty.master, &mut buf).unwrap(); |
| 30 | + |
| 31 | + assert_eq!(len, echoed_string2.len()); |
| 32 | + assert_eq!(&buf[0..len], echoed_string2.as_bytes()); |
| 33 | + |
| 34 | + close(pty.master).unwrap(); |
| 35 | + close(pty.slave).unwrap(); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn test_openpty_with_termios() { |
| 40 | + // open one pty to get attributes for the second one |
| 41 | + let mut termios = { |
| 42 | + let pty = openpty(None, None).unwrap(); |
| 43 | + assert!(pty.master > 0); // must be valid file descriptors |
| 44 | + assert!(pty.slave > 0); |
| 45 | + let termios = tcgetattr(pty.master).unwrap(); |
| 46 | + close(pty.master).unwrap(); |
| 47 | + close(pty.slave).unwrap(); |
| 48 | + termios |
| 49 | + }; |
| 50 | + termios.c_oflag &= !ONLCR; |
| 51 | + |
| 52 | + let pty = openpty(None, &termios).unwrap(); |
| 53 | + assert!(pty.master > 0); // must be valid file descriptors |
| 54 | + assert!(pty.slave > 0); |
| 55 | + |
| 56 | + // writing to one should be readable on the other one |
| 57 | + let string = "foofoofoo\n"; |
| 58 | + let mut buf = [0u8; 16]; |
| 59 | + write(pty.master, string.as_bytes()).unwrap(); |
| 60 | + let len = read(pty.slave, &mut buf).unwrap(); |
| 61 | + |
| 62 | + assert_eq!(len, string.len()); |
| 63 | + assert_eq!(&buf[0..len], string.as_bytes()); |
| 64 | + |
| 65 | + // read the echo as well |
| 66 | + let echoed_string = "foofoofoo\n"; |
| 67 | + let len = read(pty.master, &mut buf).unwrap(); |
| 68 | + assert_eq!(len, echoed_string.len()); |
| 69 | + assert_eq!(&buf[0..len], echoed_string.as_bytes()); |
| 70 | + |
| 71 | + let string2 = "barbarbarbar\n"; |
| 72 | + let echoed_string2 = "barbarbarbar\n"; |
| 73 | + write(pty.slave, string2.as_bytes()).unwrap(); |
| 74 | + let len = read(pty.master, &mut buf).unwrap(); |
| 75 | + |
| 76 | + assert_eq!(len, echoed_string2.len()); |
| 77 | + assert_eq!(&buf[0..len], echoed_string2.as_bytes()); |
| 78 | + |
| 79 | + close(pty.master).unwrap(); |
| 80 | + close(pty.slave).unwrap(); |
| 81 | +} |
0 commit comments