Debian Codegraph @

fuzz/fuzz_targets/fuzz_split.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore parens
7
8 #![no_main]
9 use libfuzzer_sys::fuzz_target;
10 use uu_split::uumain;
11
12 use rand::RngExt;
13 use std::ffi::OsString;
14
15 use uufuzz::{
16 CommandResult, compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd,
17 };
18 static CMD_PATH: &str = "split";
19
20 fn generate_split_args() -> String {
21 let mut rng = rand::rng();
22 let mut args = Vec::new();
23
24 match rng.random_range(0..=9) {
25 0 => {
26 args.push(String::from("-a")); // Suffix length
27 args.push(rng.random_range(1..=8).to_string());
28 }
29 1 => {
30 args.push(String::from("--additional-suffix"));
31 args.push(generate_random_string(5)); // Random suffix
32 }
33 2 => {
34 args.push(String::from("-b")); // Bytes per output file
35 args.push(rng.random_range(1..=1024).to_string() + "K");
36 }
37 3 => {
38 args.push(String::from("-C")); // Line bytes
39 args.push(rng.random_range(1..=1024).to_string());
40 }
41 4 => args.push(String::from("-d")), // Use numeric suffixes
42 5 => args.push(String::from("-x")), // Use hex suffixes
43 6 => {
44 args.push(String::from("-l")); // Number of lines per output file
45 args.push(rng.random_range(1..=1000).to_string());
46 }
47 7 => {
48 args.push(String::from("--filter"));
49 args.push(String::from("cat > /dev/null")); // Example filter command
50 }
51 8 => {
52 args.push(String::from("-t")); // Separator
53 args.push(String::from("\n")); // Newline as separator
54 }
55 9 => args.push(String::from("--verbose")), // Verbose
56 _ => (),
57 }
58
59 args.join(" ")
60 }
61
62 /// Function to generate a random string of lines
63 fn generate_random_lines(count: usize) -> String {
64 let mut rng = rand::rng();
65 let mut lines = Vec::new();
66
67 for _ in 0..count {
68 lines.push(generate_random_string(rng.random_range(1..=20)));
69 }
70
71 lines.join("\n")
72 }
73
74 fuzz_target!(|_data: &[u8]| {
75 let split_args = generate_split_args();
76 let mut args = vec![OsString::from("split")];
77 args.extend(split_args.split_whitespace().map(OsString::from));
78
79 let input_lines = generate_random_lines(10);
80
81 let rust_result = generate_and_run_uumain(&args, uumain, Some(&input_lines));
82
83 let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false, Some(&input_lines)) {
84 Ok(result) => result,
85 Err(error_result) => {
86 eprintln!("Failed to run GNU command:");
87 eprintln!("Stderr: {}", error_result.stderr);
88 eprintln!("Exit Code: {}", error_result.exit_code);
89 CommandResult {
90 stdout: String::new(),
91 stderr: error_result.stderr,
92 exit_code: error_result.exit_code,
93 }
94 }
95 };
96
97 compare_result(
98 "split",
99 &format!("{:?}", &args[1..]),
100 None,
101 &rust_result,
102 &gnu_result,
103 false, // Set to true if you want to fail on stderr diff
104 );
105 });