summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKelvin Ly <kelvin.ly1618@gmail.com>2016-11-02 09:26:29 -0400
committerKelvin Ly <kelvin.ly1618@gmail.com>2017-06-12 08:34:16 -0400
commit45b7b1bd1aee2335ed38e0f1d677e7e0edd2c4e7 (patch)
treea1a7f86475c65e7a104c0ec610ca79f52c4de848 /test
parentf37b0d3a39ef0dde1e0e0eb79b6a891c4b534d6d (diff)
downloadnix-45b7b1bd1aee2335ed38e0f1d677e7e0edd2c4e7.zip
Add openpty()
Diffstat (limited to 'test')
-rw-r--r--test/test.rs12
-rw-r--r--test/test_pty.rs82
2 files changed, 87 insertions, 7 deletions
diff --git a/test/test.rs b/test/test.rs
index 1f87171d..e2f5a024 100644
--- a/test/test.rs
+++ b/test/test.rs
@@ -11,20 +11,18 @@ extern crate nix_test as nixtest;
mod sys;
mod test_fcntl;
+#[cfg(any(target_os = "linux"))]
+mod test_mq;
mod test_net;
mod test_nix_path;
+#[cfg(any(target_os = "linux", target_os = "macos"))]
+mod test_poll;
+mod test_pty;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod test_sendfile;
mod test_stat;
mod test_unistd;
-#[cfg(any(target_os = "linux"))]
-mod test_mq;
-
-#[cfg(any(target_os = "linux", target_os = "macos"))]
-mod test_poll;
-mod test_pty;
-
use nixtest::assert_size_of;
#[test]
diff --git a/test/test_pty.rs b/test/test_pty.rs
index 61780421..8b6ed2b8 100644
--- a/test/test_pty.rs
+++ b/test/test_pty.rs
@@ -1,8 +1,11 @@
use std::path::Path;
use std::os::unix::prelude::*;
+
use nix::fcntl::{O_RDWR, open};
use nix::pty::*;
use nix::sys::stat;
+use nix::sys::termios::*;
+use nix::unistd::{read, write, close};
/// Test equivalence of `ptsname` and `ptsname_r`
#[test]
@@ -90,3 +93,82 @@ fn test_open_ptty_pair() {
let slave_fd = open(Path::new(&slave_name), O_RDWR, stat::Mode::empty()).unwrap();
assert!(slave_fd > 0);
}
+
+#[test]
+fn test_openpty() {
+ let pty = openpty(None, None).unwrap();
+ assert!(pty.master > 0);
+ assert!(pty.slave > 0);
+
+ // Writing to one should be readable on the other one
+ let string = "foofoofoo\n";
+ let mut buf = [0u8; 16];
+ write(pty.master, string.as_bytes()).unwrap();
+ let len = read(pty.slave, &mut buf).unwrap();
+
+ assert_eq!(len, string.len());
+ assert_eq!(&buf[0..len], string.as_bytes());
+
+ // Read the echo as well
+ let echoed_string = "foofoofoo\r\n";
+ let len = read(pty.master, &mut buf).unwrap();
+ assert_eq!(len, echoed_string.len());
+ assert_eq!(&buf[0..len], echoed_string.as_bytes());
+
+ let string2 = "barbarbarbar\n";
+ let echoed_string2 = "barbarbarbar\r\n";
+ write(pty.slave, string2.as_bytes()).unwrap();
+ let len = read(pty.master, &mut buf).unwrap();
+
+ assert_eq!(len, echoed_string2.len());
+ assert_eq!(&buf[0..len], echoed_string2.as_bytes());
+
+ close(pty.master).unwrap();
+ close(pty.slave).unwrap();
+}
+
+#[test]
+fn test_openpty_with_termios() {
+ // Open one pty to get attributes for the second one
+ let mut termios = {
+ let pty = openpty(None, None).unwrap();
+ assert!(pty.master > 0);
+ assert!(pty.slave > 0);
+ let termios = tcgetattr(pty.master).unwrap();
+ close(pty.master).unwrap();
+ close(pty.slave).unwrap();
+ termios
+ };
+ termios.c_oflag &= !ONLCR;
+
+ let pty = openpty(None, &termios).unwrap();
+ // Must be valid file descriptors
+ assert!(pty.master > 0);
+ assert!(pty.slave > 0);
+
+ // Writing to one should be readable on the other one
+ let string = "foofoofoo\n";
+ let mut buf = [0u8; 16];
+ write(pty.master, string.as_bytes()).unwrap();
+ let len = read(pty.slave, &mut buf).unwrap();
+
+ assert_eq!(len, string.len());
+ assert_eq!(&buf[0..len], string.as_bytes());
+
+ // read the echo as well
+ let echoed_string = "foofoofoo\n";
+ let len = read(pty.master, &mut buf).unwrap();
+ assert_eq!(len, echoed_string.len());
+ assert_eq!(&buf[0..len], echoed_string.as_bytes());
+
+ let string2 = "barbarbarbar\n";
+ let echoed_string2 = "barbarbarbar\n";
+ write(pty.slave, string2.as_bytes()).unwrap();
+ let len = read(pty.master, &mut buf).unwrap();
+
+ assert_eq!(len, echoed_string2.len());
+ assert_eq!(&buf[0..len], echoed_string2.as_bytes());
+
+ close(pty.master).unwrap();
+ close(pty.slave).unwrap();
+}