1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

fuzz: create a function run_gnu_cmd to deduplicate the code

This commit is contained in:
Sylvestre Ledru 2023-09-28 21:48:34 +02:00
parent aa0437c28a
commit e6f9e358d4
3 changed files with 34 additions and 44 deletions

View file

@ -5,6 +5,7 @@
use libc::{dup, dup2, STDOUT_FILENO}; use libc::{dup, dup2, STDOUT_FILENO};
use std::ffi::OsString; use std::ffi::OsString;
use std::io;
use std::process::Command; use std::process::Command;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::sync::{atomic::AtomicBool, Once}; use std::sync::{atomic::AtomicBool, Once};
@ -75,3 +76,32 @@ where
(my_output, uumain_exit_status) (my_output, uumain_exit_status)
} }
pub fn run_gnu_cmd(
cmd_path: &str,
args: &[OsString],
check_gnu: bool,
) -> Result<(String, i32), io::Error> {
if check_gnu {
is_gnu_cmd(cmd_path)?; // Check if it's a GNU implementation
}
let mut command = Command::new(cmd_path);
for arg in args {
command.arg(arg);
}
let output = command.output()?;
let exit_code = output.status.code().unwrap_or(-1);
if output.status.success() || !check_gnu {
Ok((
String::from_utf8_lossy(&output.stdout).to_string(),
exit_code,
))
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("GNU command execution failed with exit code {}", exit_code),
))
}
}

View file

@ -12,35 +12,11 @@ use rand::seq::SliceRandom;
use rand::Rng; use rand::Rng;
use std::ffi::OsString; use std::ffi::OsString;
use std::process::Command;
mod fuzz_common; mod fuzz_common;
use crate::fuzz_common::generate_and_run_uumain; use crate::fuzz_common::{generate_and_run_uumain, run_gnu_cmd};
use crate::fuzz_common::is_gnu_cmd;
static CMD_PATH: &str = "expr"; static CMD_PATH: &str = "expr";
fn run_gnu_expr(args: &[OsString]) -> Result<(String, i32), std::io::Error> {
is_gnu_cmd(CMD_PATH)?; // Check if it's a GNU implementation
let mut command = Command::new(CMD_PATH);
for arg in args {
command.arg(arg);
}
let output = command.output()?;
let exit_code = output.status.code().unwrap_or(-1);
if output.status.success() {
Ok((
String::from_utf8_lossy(&output.stdout).to_string(),
exit_code,
))
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("GNU expr execution failed with exit code {}", exit_code),
))
}
}
fn generate_random_string(max_length: usize) -> String { fn generate_random_string(max_length: usize) -> String {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@ -111,7 +87,7 @@ fuzz_target!(|_data: &[u8]| {
let (rust_output, uumain_exit_code) = generate_and_run_uumain(&mut args, uumain); let (rust_output, uumain_exit_code) = generate_and_run_uumain(&mut args, uumain);
// Run GNU expr with the provided arguments and compare the output // Run GNU expr with the provided arguments and compare the output
match run_gnu_expr(&args[1..]) { match run_gnu_cmd(CMD_PATH, &args[1..], true) {
Ok((gnu_output, gnu_exit_code)) => { Ok((gnu_output, gnu_exit_code)) => {
let gnu_output = gnu_output.trim().to_owned(); let gnu_output = gnu_output.trim().to_owned();
if uumain_exit_code != gnu_exit_code { if uumain_exit_code != gnu_exit_code {

View file

@ -12,10 +12,8 @@ use rand::seq::SliceRandom;
use rand::Rng; use rand::Rng;
use std::ffi::OsString; use std::ffi::OsString;
use std::process::Command;
mod fuzz_common; mod fuzz_common;
use crate::fuzz_common::generate_and_run_uumain; use crate::fuzz_common::{generate_and_run_uumain, run_gnu_cmd};
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]
enum ArgType { enum ArgType {
@ -30,20 +28,6 @@ enum ArgType {
static CMD_PATH: &str = "test"; static CMD_PATH: &str = "test";
fn run_gnu_test(args: &[OsString]) -> Result<(String, i32), std::io::Error> {
let mut command = Command::new(CMD_PATH);
for arg in args {
command.arg(arg);
}
let output = command.output()?;
let exit_status = output.status.code().unwrap_or(-1); // Capture the exit status code
Ok((
String::from_utf8_lossy(&output.stdout).to_string(),
exit_status,
))
}
fn generate_random_string(max_length: usize) -> String { fn generate_random_string(max_length: usize) -> String {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@ -223,7 +207,7 @@ fuzz_target!(|_data: &[u8]| {
let (rust_output, uumain_exit_status) = generate_and_run_uumain(&mut args, uumain); let (rust_output, uumain_exit_status) = generate_and_run_uumain(&mut args, uumain);
// Run GNU test with the provided arguments and compare the output // Run GNU test with the provided arguments and compare the output
match run_gnu_test(&args[1..]) { match run_gnu_cmd(CMD_PATH, &args[1..], false) {
Ok((gnu_output, gnu_exit_status)) => { Ok((gnu_output, gnu_exit_status)) => {
let gnu_output = gnu_output.trim().to_owned(); let gnu_output = gnu_output.trim().to_owned();
println!("gnu_exit_status {}", gnu_exit_status); println!("gnu_exit_status {}", gnu_exit_status);