Files

91 lines
2.5 KiB
Rust
Raw Permalink Normal View History

// 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-11-29 09:55:55 +01:00
#![no_main]
2023-11-29 09:55:55 +01:00
use libfuzzer_sys::fuzz_target;
2023-11-29 14:11:43 +01:00
use uu_echo::uumain;
2023-11-29 09:55:55 +01:00
2026-03-13 10:47:43 +01:00
use rand::RngExt;
2025-04-08 17:30:43 +02:00
use rand::prelude::IndexedRandom;
2023-11-29 09:55:55 +01:00
use std::ffi::OsString;
use uufuzz::CommandResult;
use uufuzz::{compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd};
2023-11-29 09:55:55 +01:00
2023-11-29 14:11:43 +01:00
static CMD_PATH: &str = "echo";
2023-11-29 09:55:55 +01:00
fn generate_echo() -> String {
2025-01-30 10:57:17 +01:00
let mut rng = rand::rng();
2023-11-29 09:55:55 +01:00
let mut echo_str = String::new();
// Randomly decide whether to include options
2025-01-30 10:57:17 +01:00
let include_n = rng.random_bool(0.1); // 10% chance
let include_e = rng.random_bool(0.1); // 10% chance
2024-04-01 01:01:39 +02:00
#[allow(non_snake_case)]
2025-01-30 10:57:17 +01:00
let include_E = rng.random_bool(0.1); // 10% chance
2023-11-29 09:55:55 +01:00
if include_n {
echo_str.push_str("-n ");
}
if include_e {
echo_str.push_str("-e ");
}
if include_E {
echo_str.push_str("-E ");
}
// Add a random string
2025-01-30 10:57:17 +01:00
echo_str.push_str(&generate_random_string(rng.random_range(1..=10)));
2023-11-29 09:55:55 +01:00
// Include escape sequences if -e is enabled
if include_e {
// Add a 10% chance of including an escape sequence
2025-01-30 10:57:17 +01:00
if rng.random_bool(0.1) {
2023-11-29 14:11:43 +01:00
echo_str.push_str(&generate_escape_sequence(&mut rng));
2023-11-29 09:55:55 +01:00
}
}
echo_str
}
2026-03-13 10:47:43 +01:00
fn generate_escape_sequence(rng: &mut impl RngExt) -> String {
2023-11-29 09:55:55 +01:00
let escape_sequences = [
2023-11-29 14:11:43 +01:00
"\\\\", "\\a", "\\b", "\\c", "\\e", "\\f", "\\n", "\\r", "\\t", "\\v", "\\0NNN", "\\xHH",
2023-11-29 09:55:55 +01:00
];
2023-12-04 21:20:47 +01:00
// \0NNN and \xHH need more work
2023-11-29 09:55:55 +01:00
escape_sequences.choose(rng).unwrap().to_string()
}
fuzz_target!(|_data: &[u8]| {
2023-11-29 14:11:43 +01:00
let echo_input = generate_echo();
let mut args = vec![OsString::from("echo")];
2023-11-29 09:55:55 +01:00
args.extend(echo_input.split_whitespace().map(OsString::from));
let rust_result = generate_and_run_uumain(&args, uumain, None);
2023-11-29 09:55:55 +01:00
let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false, None) {
2023-11-29 09:55:55 +01:00
Ok(result) => result,
Err(error_result) => {
eprintln!("Failed to run GNU command:");
eprintln!("Stderr: {}", error_result.stderr);
eprintln!("Exit Code: {}", error_result.exit_code);
CommandResult {
stdout: String::new(),
stderr: error_result.stderr,
exit_code: error_result.exit_code,
}
}
};
compare_result(
"echo",
&format!("{:?}", &args[1..]),
None,
2023-12-17 17:57:21 +01:00
&rust_result,
&gnu_result,
2023-11-29 14:11:43 +01:00
true,
2023-11-29 09:55:55 +01:00
);
});