fuzz: Simplify the compare_result function

This commit is contained in:
Sylvestre Ledru 2023-12-17 17:57:21 +01:00
parent be9b675ec1
commit b2e26f2aec
6 changed files with 29 additions and 45 deletions

View file

@ -222,15 +222,19 @@ pub fn run_gnu_cmd(
}
}
/// 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.
pub fn compare_result(
test_type: &str,
input: &str,
rust_stdout: &str,
gnu_stdout: &str,
rust_stderr: &str,
gnu_stderr: &str,
rust_exit_code: i32,
gnu_exit_code: i32,
rust_result: &CommandResult,
gnu_result: &CommandResult,
fail_on_stderr_diff: bool,
) {
println!("Test Type: {}", test_type);
@ -239,24 +243,24 @@ pub fn compare_result(
let mut discrepancies = Vec::new();
let mut should_panic = false;
if rust_stdout.trim() != gnu_stdout.trim() {
if rust_result.stdout.trim() != gnu_result.stdout.trim() {
discrepancies.push("stdout differs");
println!("Rust stdout: {}", rust_stdout);
println!("GNU stdout: {}", gnu_stdout);
println!("Rust stdout: {}", rust_result.stdout);
println!("GNU stdout: {}", gnu_result.stdout);
should_panic = true;
}
if rust_stderr.trim() != gnu_stderr.trim() {
if rust_result.stderr.trim() != gnu_result.stderr.trim() {
discrepancies.push("stderr differs");
println!("Rust stderr: {}", rust_stderr);
println!("GNU stderr: {}", gnu_stderr);
println!("Rust stderr: {}", rust_result.stderr);
println!("GNU stderr: {}", gnu_result.stderr);
if fail_on_stderr_diff {
should_panic = true;
}
}
if rust_exit_code != gnu_exit_code {
if rust_result.exit_code != gnu_result.exit_code {
discrepancies.push("exit code differs");
println!("Rust exit code: {}", rust_exit_code);
println!("GNU exit code: {}", gnu_exit_code);
println!("Rust exit code: {}", rust_result.exit_code);
println!("GNU exit code: {}", gnu_result.exit_code);
should_panic = true;
}

View file

@ -78,12 +78,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result(
"echo",
&format!("{:?}", &args[1..]),
&rust_result.stdout,
&gnu_result.stdout,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
&rust_result,
&gnu_result,
true,
);
});

View file

@ -86,12 +86,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result(
"expr",
&format!("{:?}", &args[1..]),
&rust_result.stdout,
&gnu_result.stdout,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
&rust_result,
&gnu_result,
false, // Set to true if you want to fail on stderr diff
);
});

View file

@ -99,12 +99,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result(
"printf",
&format!("{:?}", &args[1..]),
&rust_result.stdout,
&gnu_result.stdout,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
&rust_result,
&gnu_result,
false, // Set to true if you want to fail on stderr diff
);
});

View file

@ -67,12 +67,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result(
"seq",
&format!("{:?}", &args[1..]),
&rust_result.stdout,
&gnu_result.stdout,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
&rust_result,
&gnu_result,
false, // Set to true if you want to fail on stderr diff
);
});

View file

@ -203,12 +203,8 @@ fuzz_target!(|_data: &[u8]| {
compare_result(
"test",
&format!("{:?}", &args[1..]),
&rust_result.stdout,
&gnu_result.stdout,
&rust_result.stderr,
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
&rust_result,
&gnu_result,
false, // Set to true if you want to fail on stderr diff
);
});