| 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 |
use std::ffi::OsString; |
| 7 |
use std::io::{self, Read}; |
| 8 |
use uufuzz::{compare_result, generate_and_run_uumain, run_gnu_cmd}; |
| 9 |
|
| 10 |
// Mock cat implementation for demonstration |
| 11 |
fn mock_cat_main(args: std::vec::IntoIter<OsString>) -> i32 { |
| 12 |
let _args: Vec<OsString> = args.collect(); |
| 13 |
|
| 14 |
// Read from stdin and write to stdout |
| 15 |
let mut input = String::new(); |
| 16 |
match io::stdin().read_to_string(&mut input) { |
| 17 |
Ok(_) => { |
| 18 |
print!("{}", input); |
| 19 |
0 |
| 20 |
} |
| 21 |
Err(_) => { |
| 22 |
eprintln!("Error reading from stdin"); |
| 23 |
1 |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
fn main() { |
| 29 |
println!("=== Pipe Input uufuzz Example ==="); |
| 30 |
|
| 31 |
let args = vec![OsString::from("cat")]; |
| 32 |
let pipe_input = "Hello from pipe!\nThis is line 2.\nAnd line 3."; |
| 33 |
|
| 34 |
println!("Running mock cat implementation with pipe input..."); |
| 35 |
let rust_result = generate_and_run_uumain(&args, mock_cat_main, Some(pipe_input)); |
| 36 |
|
| 37 |
println!("Running GNU cat with pipe input..."); |
| 38 |
match run_gnu_cmd("cat", &args[1..], false, Some(pipe_input)) { |
| 39 |
Ok(gnu_result) => { |
| 40 |
println!("Comparing results..."); |
| 41 |
compare_result( |
| 42 |
"cat", |
| 43 |
"", |
| 44 |
Some(pipe_input), |
| 45 |
&rust_result, |
| 46 |
&gnu_result, |
| 47 |
false, |
| 48 |
); |
| 49 |
} |
| 50 |
Err(error_result) => { |
| 51 |
println!("Failed to run GNU cat: {}", error_result.stderr); |
| 52 |
println!("This is expected if GNU coreutils is not installed"); |
| 53 |
|
| 54 |
// Show what our implementation produced |
| 55 |
println!("\nOur implementation result:"); |
| 56 |
println!("Stdout: '{}'", rust_result.stdout); |
| 57 |
println!("Stderr: '{}'", rust_result.stderr); |
| 58 |
println!("Exit code: {}", rust_result.exit_code); |
| 59 |
|
| 60 |
// Verify our mock implementation works |
| 61 |
if rust_result.stdout.trim() == pipe_input.trim() { |
| 62 |
println!("✓ Our mock cat implementation correctly echoed the pipe input"); |
| 63 |
} else { |
| 64 |
println!("✗ Our mock cat implementation failed to echo the pipe input correctly"); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |