File: pipe_input.rs

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (68 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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.

use std::ffi::OsString;
use std::io::{self, Read};
use uufuzz::{compare_result, generate_and_run_uumain, run_gnu_cmd};

// Mock cat implementation for demonstration
fn mock_cat_main(args: std::vec::IntoIter<OsString>) -> i32 {
    let _args: Vec<OsString> = args.collect();

    // Read from stdin and write to stdout
    let mut input = String::new();
    match io::stdin().read_to_string(&mut input) {
        Ok(_) => {
            print!("{}", input);
            0
        }
        Err(_) => {
            eprintln!("Error reading from stdin");
            1
        }
    }
}

fn main() {
    println!("=== Pipe Input uufuzz Example ===");

    let args = vec![OsString::from("cat")];
    let pipe_input = "Hello from pipe!\nThis is line 2.\nAnd line 3.";

    println!("Running mock cat implementation with pipe input...");
    let rust_result = generate_and_run_uumain(&args, mock_cat_main, Some(pipe_input));

    println!("Running GNU cat with pipe input...");
    match run_gnu_cmd("cat", &args[1..], false, Some(pipe_input)) {
        Ok(gnu_result) => {
            println!("Comparing results...");
            compare_result(
                "cat",
                "",
                Some(pipe_input),
                &rust_result,
                &gnu_result,
                false,
            );
        }
        Err(error_result) => {
            println!("Failed to run GNU cat: {}", error_result.stderr);
            println!("This is expected if GNU coreutils is not installed");

            // Show what our implementation produced
            println!("\nOur implementation result:");
            println!("Stdout: '{}'", rust_result.stdout);
            println!("Stderr: '{}'", rust_result.stderr);
            println!("Exit code: {}", rust_result.exit_code);

            // Verify our mock implementation works
            if rust_result.stdout.trim() == pipe_input.trim() {
                println!("✓ Our mock cat implementation correctly echoed the pipe input");
            } else {
                println!("✗ Our mock cat implementation failed to echo the pipe input correctly");
            }
        }
    }
}