2023-03-20 19:55:45 +00:00
|
|
|
// This file is part of the uutils coreutils package.
|
|
|
|
//
|
|
|
|
// For the full copyright and license information, please view the LICENSE
|
|
|
|
// file that was distributed with this source code.
|
|
|
|
|
2023-12-21 15:18:00 +00:00
|
|
|
use libc::STDIN_FILENO;
|
2023-11-08 22:06:11 +00:00
|
|
|
use libc::{close, dup, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};
|
2023-11-12 09:25:55 +00:00
|
|
|
use rand::prelude::SliceRandom;
|
|
|
|
use rand::Rng;
|
2023-09-28 06:42:30 +00:00
|
|
|
use std::ffi::OsString;
|
2023-12-21 15:18:00 +00:00
|
|
|
use std::io::{Seek, SeekFrom, Write};
|
|
|
|
use std::os::fd::{AsRawFd, RawFd};
|
|
|
|
use std::process::{Command, Stdio};
|
2023-03-20 19:55:45 +00:00
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::sync::{atomic::AtomicBool, Once};
|
2023-12-31 15:28:21 +00:00
|
|
|
use std::{io, thread};
|
2023-03-20 19:55:45 +00:00
|
|
|
|
2023-11-10 15:01:38 +00:00
|
|
|
/// Represents the result of running a command, including its standard output,
|
|
|
|
/// standard error, and exit code.
|
|
|
|
pub struct CommandResult {
|
|
|
|
/// The standard output (stdout) of the command as a string.
|
|
|
|
pub stdout: String,
|
|
|
|
|
|
|
|
/// The standard error (stderr) of the command as a string.
|
|
|
|
pub stderr: String,
|
|
|
|
|
|
|
|
/// The exit code of the command.
|
|
|
|
pub exit_code: i32,
|
|
|
|
}
|
|
|
|
|
2023-03-20 19:55:45 +00:00
|
|
|
static CHECK_GNU: Once = Once::new();
|
|
|
|
static IS_GNU: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
pub fn is_gnu_cmd(cmd_path: &str) -> Result<(), std::io::Error> {
|
|
|
|
CHECK_GNU.call_once(|| {
|
|
|
|
let version_output = Command::new(cmd_path).arg("--version").output().unwrap();
|
|
|
|
|
|
|
|
println!("version_output {:#?}", version_output);
|
|
|
|
|
|
|
|
let version_str = String::from_utf8_lossy(&version_output.stdout).to_string();
|
|
|
|
if version_str.contains("GNU coreutils") {
|
|
|
|
IS_GNU.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if IS_GNU.load(Ordering::Relaxed) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
panic!("Not the GNU implementation");
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 06:42:30 +00:00
|
|
|
|
2023-12-21 15:18:00 +00:00
|
|
|
pub fn generate_and_run_uumain<F>(
|
|
|
|
args: &[OsString],
|
|
|
|
uumain_function: F,
|
|
|
|
pipe_input: Option<&str>,
|
|
|
|
) -> CommandResult
|
2023-09-28 06:42:30 +00:00
|
|
|
where
|
2023-12-31 15:28:21 +00:00
|
|
|
F: FnOnce(std::vec::IntoIter<OsString>) -> i32 + Send + 'static,
|
2023-09-28 06:42:30 +00:00
|
|
|
{
|
2023-11-08 22:06:11 +00:00
|
|
|
// Duplicate the stdout and stderr file descriptors
|
2023-09-28 06:42:30 +00:00
|
|
|
let original_stdout_fd = unsafe { dup(STDOUT_FILENO) };
|
2023-11-08 22:06:11 +00:00
|
|
|
let original_stderr_fd = unsafe { dup(STDERR_FILENO) };
|
|
|
|
if original_stdout_fd == -1 || original_stderr_fd == -1 {
|
2023-11-10 15:01:38 +00:00
|
|
|
return CommandResult {
|
2023-12-21 15:18:00 +00:00
|
|
|
stdout: "".to_string(),
|
|
|
|
stderr: "Failed to duplicate STDOUT_FILENO or STDERR_FILENO".to_string(),
|
2023-11-10 15:01:38 +00:00
|
|
|
exit_code: -1,
|
|
|
|
};
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
2023-12-31 15:28:21 +00:00
|
|
|
|
2023-11-08 11:46:24 +00:00
|
|
|
println!("Running test {:?}", &args[0..]);
|
2023-11-08 22:06:11 +00:00
|
|
|
let mut pipe_stdout_fds = [-1; 2];
|
|
|
|
let mut pipe_stderr_fds = [-1; 2];
|
|
|
|
|
|
|
|
// Create pipes for stdout and stderr
|
|
|
|
if unsafe { pipe(pipe_stdout_fds.as_mut_ptr()) } == -1
|
|
|
|
|| unsafe { pipe(pipe_stderr_fds.as_mut_ptr()) } == -1
|
|
|
|
{
|
2023-11-10 15:01:38 +00:00
|
|
|
return CommandResult {
|
2023-12-21 15:18:00 +00:00
|
|
|
stdout: "".to_string(),
|
|
|
|
stderr: "Failed to create pipes".to_string(),
|
2023-11-10 15:01:38 +00:00
|
|
|
exit_code: -1,
|
|
|
|
};
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
// Redirect stdout and stderr to their respective pipes
|
|
|
|
if unsafe { dup2(pipe_stdout_fds[1], STDOUT_FILENO) } == -1
|
|
|
|
|| unsafe { dup2(pipe_stderr_fds[1], STDERR_FILENO) } == -1
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
close(pipe_stdout_fds[0]);
|
|
|
|
close(pipe_stdout_fds[1]);
|
|
|
|
close(pipe_stderr_fds[0]);
|
|
|
|
close(pipe_stderr_fds[1]);
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
2023-11-10 15:01:38 +00:00
|
|
|
return CommandResult {
|
2023-12-21 15:18:00 +00:00
|
|
|
stdout: "".to_string(),
|
|
|
|
stderr: "Failed to redirect STDOUT_FILENO or STDERR_FILENO".to_string(),
|
2023-11-10 15:01:38 +00:00
|
|
|
exit_code: -1,
|
|
|
|
};
|
2023-09-28 06:42:30 +00:00
|
|
|
}
|
2023-11-08 11:46:24 +00:00
|
|
|
|
2023-12-21 15:18:00 +00:00
|
|
|
let original_stdin_fd = if let Some(input_str) = pipe_input {
|
|
|
|
// we have pipe input
|
|
|
|
let mut input_file = tempfile::tempfile().unwrap();
|
|
|
|
write!(input_file, "{}", input_str).unwrap();
|
|
|
|
input_file.seek(SeekFrom::Start(0)).unwrap();
|
|
|
|
|
|
|
|
// Redirect stdin to read from the in-memory file
|
|
|
|
let original_stdin_fd = unsafe { dup(STDIN_FILENO) };
|
|
|
|
if original_stdin_fd == -1 || unsafe { dup2(input_file.as_raw_fd(), STDIN_FILENO) } == -1 {
|
|
|
|
return CommandResult {
|
|
|
|
stdout: "".to_string(),
|
|
|
|
stderr: "Failed to set up stdin redirection".to_string(),
|
|
|
|
exit_code: -1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(original_stdin_fd)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-12-31 15:28:21 +00:00
|
|
|
let (uumain_exit_status, captured_stdout, captured_stderr) = thread::scope(|s| {
|
|
|
|
let out = s.spawn(|| read_from_fd(pipe_stdout_fds[0]));
|
|
|
|
let err = s.spawn(|| read_from_fd(pipe_stderr_fds[0]));
|
|
|
|
let status = uumain_function(args.to_owned().into_iter());
|
2024-01-04 18:03:30 +00:00
|
|
|
// Reset the exit code global variable in case we run another test after this one
|
|
|
|
// See https://github.com/uutils/coreutils/issues/5777
|
|
|
|
uucore::error::set_exit_code(0);
|
2023-12-31 15:28:21 +00:00
|
|
|
io::stdout().flush().unwrap();
|
|
|
|
io::stderr().flush().unwrap();
|
|
|
|
unsafe {
|
|
|
|
close(pipe_stdout_fds[1]);
|
|
|
|
close(pipe_stderr_fds[1]);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
}
|
|
|
|
(status, out.join().unwrap(), err.join().unwrap())
|
|
|
|
});
|
2023-11-08 22:06:11 +00:00
|
|
|
|
|
|
|
// Restore the original stdout and stderr
|
|
|
|
if unsafe { dup2(original_stdout_fd, STDOUT_FILENO) } == -1
|
|
|
|
|| unsafe { dup2(original_stderr_fd, STDERR_FILENO) } == -1
|
|
|
|
{
|
2023-11-10 15:01:38 +00:00
|
|
|
return CommandResult {
|
2023-12-21 15:18:00 +00:00
|
|
|
stdout: "".to_string(),
|
|
|
|
stderr: "Failed to restore the original STDOUT_FILENO or STDERR_FILENO".to_string(),
|
2023-11-10 15:01:38 +00:00
|
|
|
exit_code: -1,
|
|
|
|
};
|
2023-11-08 22:06:11 +00:00
|
|
|
}
|
2024-01-05 10:04:16 +00:00
|
|
|
unsafe {
|
|
|
|
close(original_stdout_fd);
|
|
|
|
close(original_stderr_fd);
|
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
|
2023-12-21 15:18:00 +00:00
|
|
|
// Restore the original stdin if it was modified
|
|
|
|
if let Some(fd) = original_stdin_fd {
|
|
|
|
if unsafe { dup2(fd, STDIN_FILENO) } == -1 {
|
|
|
|
return CommandResult {
|
|
|
|
stdout: "".to_string(),
|
|
|
|
stderr: "Failed to restore the original STDIN".to_string(),
|
|
|
|
exit_code: -1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
unsafe { close(fd) };
|
|
|
|
}
|
|
|
|
|
2023-11-10 15:01:38 +00:00
|
|
|
CommandResult {
|
|
|
|
stdout: captured_stdout,
|
2023-12-31 15:28:21 +00:00
|
|
|
stderr: captured_stderr
|
|
|
|
.split_once(':')
|
|
|
|
.map(|x| x.1)
|
|
|
|
.unwrap_or("")
|
|
|
|
.trim()
|
|
|
|
.to_string(),
|
2023-11-10 15:01:38 +00:00
|
|
|
exit_code: uumain_exit_status,
|
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
}
|
2023-09-28 06:42:30 +00:00
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
fn read_from_fd(fd: RawFd) -> String {
|
2023-09-28 06:42:30 +00:00
|
|
|
let mut captured_output = Vec::new();
|
|
|
|
let mut read_buffer = [0; 1024];
|
|
|
|
loop {
|
|
|
|
let bytes_read = unsafe {
|
|
|
|
libc::read(
|
2023-11-08 22:06:11 +00:00
|
|
|
fd,
|
2023-09-28 06:42:30 +00:00
|
|
|
read_buffer.as_mut_ptr() as *mut libc::c_void,
|
|
|
|
read_buffer.len(),
|
|
|
|
)
|
|
|
|
};
|
2023-11-08 11:46:24 +00:00
|
|
|
|
|
|
|
if bytes_read == -1 {
|
|
|
|
eprintln!("Failed to read from the pipe");
|
|
|
|
break;
|
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
if bytes_read == 0 {
|
2023-09-28 06:42:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
captured_output.extend_from_slice(&read_buffer[..bytes_read as usize]);
|
|
|
|
}
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
unsafe { libc::close(fd) };
|
2023-09-28 06:42:30 +00:00
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
String::from_utf8_lossy(&captured_output).into_owned()
|
2023-09-28 06:42:30 +00:00
|
|
|
}
|
2023-09-28 19:48:34 +00:00
|
|
|
|
|
|
|
pub fn run_gnu_cmd(
|
|
|
|
cmd_path: &str,
|
|
|
|
args: &[OsString],
|
|
|
|
check_gnu: bool,
|
2023-12-21 15:18:00 +00:00
|
|
|
pipe_input: Option<&str>,
|
2023-11-10 15:01:38 +00:00
|
|
|
) -> Result<CommandResult, CommandResult> {
|
2023-09-28 19:48:34 +00:00
|
|
|
if check_gnu {
|
2023-11-08 22:06:11 +00:00
|
|
|
match is_gnu_cmd(cmd_path) {
|
|
|
|
Ok(_) => {} // if the check passes, do nothing
|
|
|
|
Err(e) => {
|
|
|
|
// Convert the io::Error into the function's error type
|
2023-11-10 15:01:38 +00:00
|
|
|
return Err(CommandResult {
|
|
|
|
stdout: String::new(),
|
|
|
|
stderr: e.to_string(),
|
|
|
|
exit_code: -1,
|
|
|
|
});
|
2023-11-08 22:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-28 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = Command::new(cmd_path);
|
|
|
|
for arg in args {
|
|
|
|
command.arg(arg);
|
|
|
|
}
|
|
|
|
|
2023-12-21 15:18:00 +00:00
|
|
|
let output = if let Some(input_str) = pipe_input {
|
|
|
|
// We have an pipe input
|
|
|
|
command.stdin(Stdio::piped()).stdout(Stdio::piped());
|
|
|
|
|
|
|
|
let mut child = command.spawn().expect("Failed to execute command");
|
|
|
|
let child_stdin = child.stdin.as_mut().unwrap();
|
|
|
|
child_stdin
|
|
|
|
.write_all(input_str.as_bytes())
|
|
|
|
.expect("Failed to write to stdin");
|
|
|
|
|
|
|
|
match child.wait_with_output() {
|
|
|
|
Ok(output) => output,
|
|
|
|
Err(e) => {
|
|
|
|
return Err(CommandResult {
|
|
|
|
stdout: String::new(),
|
|
|
|
stderr: e.to_string(),
|
|
|
|
exit_code: -1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just run with args
|
|
|
|
match command.output() {
|
|
|
|
Ok(output) => output,
|
|
|
|
Err(e) => {
|
|
|
|
return Err(CommandResult {
|
|
|
|
stdout: String::new(),
|
|
|
|
stderr: e.to_string(),
|
|
|
|
exit_code: -1,
|
|
|
|
});
|
|
|
|
}
|
2023-11-10 15:01:38 +00:00
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
};
|
2023-09-28 19:48:34 +00:00
|
|
|
let exit_code = output.status.code().unwrap_or(-1);
|
2023-11-08 22:06:11 +00:00
|
|
|
// Here we get stdout and stderr as Strings
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stderr = stderr
|
2023-11-08 23:04:14 +00:00
|
|
|
.split_once(':')
|
|
|
|
.map(|x| x.1)
|
2023-11-08 22:06:11 +00:00
|
|
|
.unwrap_or("")
|
|
|
|
.trim()
|
|
|
|
.to_string();
|
|
|
|
|
2023-09-28 19:48:34 +00:00
|
|
|
if output.status.success() || !check_gnu {
|
2023-11-10 15:01:38 +00:00
|
|
|
Ok(CommandResult {
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
exit_code,
|
|
|
|
})
|
2023-09-28 19:48:34 +00:00
|
|
|
} else {
|
2023-11-10 15:01:38 +00:00
|
|
|
Err(CommandResult {
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
exit_code,
|
|
|
|
})
|
2023-09-28 19:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-08 23:04:14 +00:00
|
|
|
|
2023-12-17 16:57:21 +00:00
|
|
|
/// Compare results from two different implementations of a command.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `test_type` - The command.
|
|
|
|
/// * `input` - The input provided to the command.
|
|
|
|
/// * `rust_result` - The result of running the command with the Rust implementation.
|
|
|
|
/// * `gnu_result` - The result of running the command with the GNU implementation.
|
|
|
|
/// * `fail_on_stderr_diff` - Whether to fail the test if there is a difference in stderr output.
|
2023-11-08 23:04:14 +00:00
|
|
|
pub fn compare_result(
|
|
|
|
test_type: &str,
|
|
|
|
input: &str,
|
2023-12-21 15:18:00 +00:00
|
|
|
pipe_input: Option<&str>,
|
2023-12-17 16:57:21 +00:00
|
|
|
rust_result: &CommandResult,
|
|
|
|
gnu_result: &CommandResult,
|
2023-11-08 23:04:14 +00:00
|
|
|
fail_on_stderr_diff: bool,
|
|
|
|
) {
|
|
|
|
println!("Test Type: {}", test_type);
|
|
|
|
println!("Input: {}", input);
|
2023-12-21 15:18:00 +00:00
|
|
|
if let Some(pipe) = pipe_input {
|
|
|
|
println!("Pipe: {}", pipe);
|
|
|
|
}
|
2023-11-08 23:04:14 +00:00
|
|
|
|
|
|
|
let mut discrepancies = Vec::new();
|
|
|
|
let mut should_panic = false;
|
|
|
|
|
2023-12-17 16:57:21 +00:00
|
|
|
if rust_result.stdout.trim() != gnu_result.stdout.trim() {
|
2023-11-08 23:04:14 +00:00
|
|
|
discrepancies.push("stdout differs");
|
2023-12-17 16:57:21 +00:00
|
|
|
println!("Rust stdout: {}", rust_result.stdout);
|
|
|
|
println!("GNU stdout: {}", gnu_result.stdout);
|
2023-11-08 23:04:14 +00:00
|
|
|
should_panic = true;
|
|
|
|
}
|
2023-12-17 16:57:21 +00:00
|
|
|
if rust_result.stderr.trim() != gnu_result.stderr.trim() {
|
2023-11-08 23:04:14 +00:00
|
|
|
discrepancies.push("stderr differs");
|
2023-12-17 16:57:21 +00:00
|
|
|
println!("Rust stderr: {}", rust_result.stderr);
|
|
|
|
println!("GNU stderr: {}", gnu_result.stderr);
|
2023-11-08 23:04:14 +00:00
|
|
|
if fail_on_stderr_diff {
|
|
|
|
should_panic = true;
|
|
|
|
}
|
|
|
|
}
|
2023-12-17 16:57:21 +00:00
|
|
|
if rust_result.exit_code != gnu_result.exit_code {
|
2023-11-08 23:04:14 +00:00
|
|
|
discrepancies.push("exit code differs");
|
2023-12-17 16:57:21 +00:00
|
|
|
println!("Rust exit code: {}", rust_result.exit_code);
|
|
|
|
println!("GNU exit code: {}", gnu_result.exit_code);
|
2023-11-08 23:04:14 +00:00
|
|
|
should_panic = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if discrepancies.is_empty() {
|
|
|
|
println!("All outputs and exit codes matched.");
|
|
|
|
} else {
|
|
|
|
println!("Discrepancy detected: {}", discrepancies.join(", "));
|
|
|
|
if should_panic {
|
|
|
|
panic!("Test failed for {}: {}", test_type, input);
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"Test completed with discrepancies for {}: {}",
|
|
|
|
test_type, input
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 09:25:55 +00:00
|
|
|
|
|
|
|
pub fn generate_random_string(max_length: usize) -> String {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
.chars()
|
|
|
|
.collect();
|
|
|
|
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
|
|
|
|
let mut result = String::new();
|
|
|
|
|
|
|
|
for _ in 0..rng.gen_range(1..=max_length) {
|
|
|
|
if rng.gen_bool(0.9) {
|
|
|
|
let ch = valid_utf8.choose(&mut rng).unwrap();
|
|
|
|
result.push(*ch);
|
|
|
|
} else {
|
|
|
|
let ch = invalid_utf8.choose(&mut rng).unwrap();
|
|
|
|
if let Some(c) = char::from_u32(*ch as u32) {
|
|
|
|
result.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|