Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test using TTY #33

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions bzip2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(non_upper_case_globals)]

use std::ffi::{c_char, CStr, CString, OsStr};
use std::io::IsTerminal;
use std::mem::zeroed;
use std::path::{Path, PathBuf};
use std::ptr;
Expand All @@ -14,8 +15,8 @@ use libbzip2_rs_sys::{

use libc::{
_exit, close, exit, fclose, fdopen, ferror, fflush, fgetc, fileno, fopen, fprintf, fread,
fwrite, isatty, open, perror, remove, rewind, signal, stat, strcat, strcmp, strlen, strncpy,
ungetc, utimbuf, write, FILE,
fwrite, open, perror, remove, rewind, signal, stat, strcat, strcmp, strlen, strncpy, ungetc,
utimbuf, write, FILE,
};
extern "C" {
static mut stdin: *mut FILE;
Expand Down Expand Up @@ -1219,7 +1220,7 @@ unsafe fn compress(name: *mut c_char) {
SourceMode::I2O => {
inStr = stdin;
outStr = stdout;
if isatty(fileno(stdout)) != 0 {
if std::io::stdout().is_terminal() {
fprintf(
stderr,
b"%s: I won't write compressed data to a terminal.\n\0" as *const u8
Expand All @@ -1242,7 +1243,7 @@ unsafe fn compress(name: *mut c_char) {
b"rb\0" as *const u8 as *const libc::c_char,
);
outStr = stdout;
if isatty(fileno(stdout)) != 0 {
if std::io::stdout().is_terminal() {
fprintf(
stderr,
b"%s: I won't write compressed data to a terminal.\n\0" as *const u8
Expand Down Expand Up @@ -1494,7 +1495,7 @@ unsafe fn uncompress(name: Option<String>) {
SourceMode::I2O => {
inStr = stdin;
outStr = stdout;
if isatty(fileno(stdin)) != 0 {
if std::io::stdin().is_terminal() {
eprint!(
concat!(
"{program_name}: I won't read compressed data from a terminal.\n",
Expand Down Expand Up @@ -1688,7 +1689,7 @@ unsafe fn testf(name: Option<String>) {
}
match srcMode {
SourceMode::I2O => {
if isatty(fileno(stdin)) != 0 {
if std::io::stdin().is_terminal() {
eprintln!(
"{}: I won't read compressed data from a terminal.",
get_program_name().display(),
Expand Down
147 changes: 147 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,41 @@ macro_rules! expect_success {
};
}

#[cfg(unix)]
unsafe fn setup_custom_tty() -> (std::process::Stdio, std::process::Stdio) {
use std::os::fd::FromRawFd;

// Open a new PTY master device
let master_fd = libc::posix_openpt(libc::O_RDWR | libc::O_NOCTTY);
if master_fd == -1 {
panic!("{}", std::io::Error::last_os_error());
}

// Grant access to the slave PTY
if unsafe { libc::grantpt(master_fd) } < 0 {
panic!("{}", std::io::Error::last_os_error());
}

// Unlock the slave PTY
if unsafe { libc::unlockpt(master_fd) } < 0 {
panic!("{}", std::io::Error::last_os_error());
}

// Get the path to the slave PTY
let slave_path_ptr = unsafe { libc::ptsname(master_fd) };
if slave_path_ptr.is_null() {
panic!("{}", std::io::Error::last_os_error());
}

// Open the slave PTY
let slave_fd = unsafe { libc::open(slave_path_ptr, libc::O_RDWR) };
if slave_fd < 0 {
panic!("{}", std::io::Error::last_os_error());
}

unsafe { (Stdio::from_raw_fd(master_fd), Stdio::from_raw_fd(slave_fd)) }
}

fn command() -> Command {
match env::var("RUNNER") {
Ok(runner) if !runner.is_empty() => {
Expand Down Expand Up @@ -867,6 +902,22 @@ mod decompress_command {
),),
);
}

#[test]
#[cfg(unix)]
fn stdin_is_terminal() {
let mut cmd = command();

let (_master, tty) = unsafe { setup_custom_tty() };

expect_failure!(
cmd.arg("-d").arg("-c").stdin(tty),
concat!(
"bzip2: I won't read compressed data from a terminal.\n",
"bzip2: For help, type: `bzip2 --help'.\n",
),
);
}
}

mod test_command {
Expand Down Expand Up @@ -1041,6 +1092,22 @@ mod test_command {
),
);
}

#[test]
#[cfg(unix)]
fn stdin_is_terminal() {
let mut cmd = command();

let (_master, tty) = unsafe { setup_custom_tty() };

expect_failure!(
cmd.arg("-t").stdin(tty),
concat!(
"bzip2: I won't read compressed data from a terminal.\n",
"bzip2: For help, type: `bzip2 --help'.\n",
),
);
}
}

mod compress_command {
Expand Down Expand Up @@ -1481,4 +1548,84 @@ mod compress_command {
),
);
}

#[test]
fn stdout_is_terminal_i2o() {
let mut cmd = command();

let (_master, tty) = unsafe { setup_custom_tty() };

expect_failure!(
cmd.arg("-z").arg("-c").stdin(Stdio::piped()).stdout(tty),
concat!(
"bzip2: I won't write compressed data to a terminal.\n",
"bzip2: For help, type: `bzip2 --help'.\n",
),
);
}

#[test]
#[cfg(unix)]
fn stdout_is_terminal_f2o() {
let mut cmd = command();

let tmpdir = tempfile::tempdir().unwrap();

let sample1_ref = tmpdir.path().join("sample1.ref");
std::fs::copy("tests/input/quick/sample1.ref", &sample1_ref).unwrap();

let (_master, tty) = unsafe { setup_custom_tty() };

expect_failure!(
cmd.arg("-z")
.arg("-c")
.arg("-k")
.arg(&sample1_ref)
.stdout(tty),
concat!(
"bzip2: I won't write compressed data to a terminal.\n",
"bzip2: For help, type: `bzip2 --help'.\n",
),
);

// the `-k` flag should keep the input file
assert!(sample1_ref.exists());
}

#[test]
#[cfg(unix)]
fn io_error() {
let mut cmd = command();

let tmpdir = tempfile::tempdir().unwrap();

let sample1_ref = tmpdir.path().join("sample1.ref");
std::fs::copy("tests/input/quick/sample1.ref", &sample1_ref).unwrap();

let (master, tty) = unsafe { setup_custom_tty() };

// dropping here triggers an IO error down the line
drop(master);

expect_failure!(
cmd.arg("-z")
.arg("-c")
.arg("-k")
.arg(&sample1_ref)
.stdout(tty),
format!(
concat!(
"\n",
"bzip2: I/O or other error, bailing out. Possible reason follows.\n",
"bzip2: Input/output error\n",
"\tInput file = {in_file}, output file = (stdout)\n",
""
),
in_file = sample1_ref.display(),
)
);

// the `-k` flag should keep the input file
assert!(sample1_ref.exists());
}
}
Loading