| 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 uutests::new_ucmd; |
| 7 |
use uutests::util::TestScenario; |
| 8 |
use uutests::util_name; |
| 9 |
// spell-checker:ignore checkfile, testf, ntestf |
| 10 |
macro_rules! get_hash( |
| 11 |
($str:expr) => ( |
| 12 |
$str.split(' ').collect::<Vec<&str>>()[0] |
| 13 |
); |
| 14 |
); |
| 15 |
|
| 16 |
macro_rules! test_digest { |
| 17 |
($id:ident) => { |
| 18 |
mod $id { |
| 19 |
use uutests::util::*; |
| 20 |
use uutests::util_name; |
| 21 |
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected"); |
| 22 |
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile"); |
| 23 |
static INPUT_FILE: &'static str = "input.txt"; |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_single_file() { |
| 27 |
let ts = TestScenario::new(util_name!()); |
| 28 |
assert_eq!( |
| 29 |
ts.fixtures.read(EXPECTED_FILE), |
| 30 |
get_hash!( |
| 31 |
ts.ucmd() |
| 32 |
.arg(INPUT_FILE) |
| 33 |
.succeeds() |
| 34 |
.no_stderr() |
| 35 |
.stdout_str() |
| 36 |
) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_stdin() { |
| 42 |
let ts = TestScenario::new(util_name!()); |
| 43 |
assert_eq!( |
| 44 |
ts.fixtures.read(EXPECTED_FILE), |
| 45 |
get_hash!( |
| 46 |
ts.ucmd() |
| 47 |
.pipe_in_fixture(INPUT_FILE) |
| 48 |
.succeeds() |
| 49 |
.no_stderr() |
| 50 |
.stdout_str() |
| 51 |
) |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn test_stdin_with_dash_directory() { |
| 57 |
let ts = TestScenario::new(util_name!()); |
| 58 |
ts.fixtures.mkdir("-"); |
| 59 |
assert_eq!( |
| 60 |
ts.fixtures.read(EXPECTED_FILE), |
| 61 |
get_hash!( |
| 62 |
ts.ucmd() |
| 63 |
.pipe_in_fixture(INPUT_FILE) |
| 64 |
.succeeds() |
| 65 |
.no_stderr() |
| 66 |
.stdout_str() |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_check() { |
| 73 |
let ts = TestScenario::new(util_name!()); |
| 74 |
println!("File content='{}'", ts.fixtures.read(INPUT_FILE)); |
| 75 |
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE)); |
| 76 |
|
| 77 |
ts.ucmd() |
| 78 |
.args(&["--check", CHECK_FILE]) |
| 79 |
.succeeds() |
| 80 |
.no_stderr() |
| 81 |
.stdout_is("input.txt: OK\n"); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn test_zero() { |
| 86 |
let ts = TestScenario::new(util_name!()); |
| 87 |
assert_eq!( |
| 88 |
ts.fixtures.read(EXPECTED_FILE), |
| 89 |
get_hash!( |
| 90 |
ts.ucmd() |
| 91 |
.arg("--zero") |
| 92 |
.arg(INPUT_FILE) |
| 93 |
.succeeds() |
| 94 |
.no_stderr() |
| 95 |
.stdout_str() |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn test_missing_file() { |
| 102 |
let ts = TestScenario::new(util_name!()); |
| 103 |
let at = &ts.fixtures; |
| 104 |
|
| 105 |
at.write("a", "file1\n"); |
| 106 |
at.write("c", "file3\n"); |
| 107 |
|
| 108 |
ts.ucmd() |
| 109 |
.args(&["a", "b", "c"]) |
| 110 |
.fails() |
| 111 |
.stdout_contains("a\n") |
| 112 |
.stdout_contains("c\n") |
| 113 |
.stderr_contains("b: No such file or directory"); |
| 114 |
} |
| 115 |
} |
| 116 |
}; |
| 117 |
} |
| 118 |
|
| 119 |
test_digest! {sha1} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_check_sha1() { |
| 123 |
// To make sure that #3815 doesn't happen again |
| 124 |
let scene = TestScenario::new(util_name!()); |
| 125 |
let at = &scene.fixtures; |
| 126 |
|
| 127 |
at.write("testf", "foobar\n"); |
| 128 |
at.write( |
| 129 |
"testf.sha1", |
| 130 |
"988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n", |
| 131 |
); |
| 132 |
scene |
| 133 |
.ccmd("sha1sum") |
| 134 |
.arg("-c") |
| 135 |
.arg(at.subdir.join("testf.sha1")) |
| 136 |
.succeeds() |
| 137 |
.stdout_only("testf: OK\n"); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn test_check_file_not_found_warning() { |
| 142 |
let scene = TestScenario::new(util_name!()); |
| 143 |
let at = &scene.fixtures; |
| 144 |
|
| 145 |
at.write("testf", "foobar\n"); |
| 146 |
at.write( |
| 147 |
"testf.sha1", |
| 148 |
"988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n", |
| 149 |
); |
| 150 |
at.remove("testf"); |
| 151 |
scene |
| 152 |
.ccmd("sha1sum") |
| 153 |
.arg("-c") |
| 154 |
.arg(at.subdir.join("testf.sha1")) |
| 155 |
.fails() |
| 156 |
.stdout_is("testf: FAILED open or read\n") |
| 157 |
.stderr_is("sha1sum: testf: No such file or directory\nsha1sum: WARNING: 1 listed file could not be read\n"); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn test_invalid_arg() { |
| 162 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn test_conflicting_arg() { |
| 167 |
new_ucmd!().arg("--tag").arg("--check").fails_with_code(1); |
| 168 |
new_ucmd!().arg("--tag").arg("--text").fails_with_code(1); |
| 169 |
} |