| 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 |
// |
| 7 |
#[cfg(unix)] |
| 8 |
use uutests::at_and_ucmd; |
| 9 |
use uutests::new_ucmd; |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn test_version() { |
| 13 |
new_ucmd!() |
| 14 |
.arg("--version") |
| 15 |
.succeeds() |
| 16 |
.no_stderr() |
| 17 |
.stdout_is(format!("base32 {}\n", uucore::crate_version!())); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn test_encode() { |
| 22 |
let input = "Hello, World!"; |
| 23 |
new_ucmd!() |
| 24 |
.pipe_in(input) |
| 25 |
.succeeds() |
| 26 |
.stdout_only("JBSWY3DPFQQFO33SNRSCC===\n"); // spell-checker:disable-line |
| 27 |
|
| 28 |
// Using '-' as our file |
| 29 |
new_ucmd!() |
| 30 |
.arg("-") |
| 31 |
.pipe_in(input) |
| 32 |
.succeeds() |
| 33 |
.stdout_only("JBSWY3DPFQQFO33SNRSCC===\n"); // spell-checker:disable-line |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
fn test_encode_repeat_flags_later_wrap_10() { |
| 38 |
let input = "Hello, World!\n"; |
| 39 |
new_ucmd!() |
| 40 |
.args(&["-ii", "-w17", "-w10"]) |
| 41 |
.pipe_in(input) |
| 42 |
.succeeds() |
| 43 |
.stdout_only("JBSWY3DPFQ\nQFO33SNRSC\nCCQ=\n"); // spell-checker:disable-line |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn test_encode_repeat_flags_later_wrap_17() { |
| 48 |
let input = "Hello, World!\n"; |
| 49 |
new_ucmd!() |
| 50 |
.args(&["-ii", "-w10", "-w17"]) |
| 51 |
.pipe_in(input) |
| 52 |
.succeeds() |
| 53 |
.stdout_only("JBSWY3DPFQQFO33SN\nRSCCCQ=\n"); // spell-checker:disable-line |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn test_base32_encode_file() { |
| 58 |
new_ucmd!() |
| 59 |
.arg("input-simple.txt") |
| 60 |
.succeeds() |
| 61 |
.stdout_only("JBSWY3DPFQQFO33SNRSCCCQ=\n"); // spell-checker:disable-line |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn test_decode() { |
| 66 |
for decode_param in ["-d", "--decode", "--dec", "-D"] { |
| 67 |
let input = "JBSWY3DPFQQFO33SNRSCC===\n"; // spell-checker:disable-line |
| 68 |
new_ucmd!() |
| 69 |
.arg(decode_param) |
| 70 |
.pipe_in(input) |
| 71 |
.succeeds() |
| 72 |
.stdout_only("Hello, World!"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_decode_repeat_flags() { |
| 78 |
let input = "JBSWY3DPFQQFO33SNRSCC===\n"; // spell-checker:disable-line |
| 79 |
new_ucmd!() |
| 80 |
.args(&["-didiw80", "--wrap=17", "--wrap", "8"]) // spell-checker:disable-line |
| 81 |
.pipe_in(input) |
| 82 |
.succeeds() |
| 83 |
.stdout_only("Hello, World!"); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn test_garbage() { |
| 88 |
let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line |
| 89 |
new_ucmd!() |
| 90 |
.arg("-d") |
| 91 |
.pipe_in(input) |
| 92 |
.fails() |
| 93 |
.stderr_only("base32: error: invalid input\n"); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn test_ignore_garbage() { |
| 98 |
for ignore_garbage_param in ["-i", "--ignore-garbage", "--ig"] { |
| 99 |
let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n"; // spell-checker:disable-line |
| 100 |
new_ucmd!() |
| 101 |
.arg("-d") |
| 102 |
.arg(ignore_garbage_param) |
| 103 |
.pipe_in(input) |
| 104 |
.succeeds() |
| 105 |
.stdout_only("Hello, World!"); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_wrap() { |
| 111 |
for wrap_param in ["-w", "--wrap", "--wr"] { |
| 112 |
let input = "The quick brown fox jumps over the lazy dog."; |
| 113 |
new_ucmd!() |
| 114 |
.arg(wrap_param) |
| 115 |
.arg("20") |
| 116 |
.pipe_in(input) |
| 117 |
.succeeds() |
| 118 |
.stdout_only( |
| 119 |
"KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n", // spell-checker:disable-line |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_wrap_no_arg() { |
| 126 |
for wrap_param in ["-w", "--wrap"] { |
| 127 |
new_ucmd!() |
| 128 |
.arg(wrap_param) |
| 129 |
.fails() |
| 130 |
.stderr_contains("error: a value is required for '--wrap <COLS>' but none was supplied") |
| 131 |
.stderr_contains("For more information, try '--help'.") |
| 132 |
.no_stdout(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn test_wrap_bad_arg() { |
| 138 |
for wrap_param in ["-w", "--wrap"] { |
| 139 |
new_ucmd!() |
| 140 |
.arg(wrap_param) |
| 141 |
.arg("b") |
| 142 |
.fails() |
| 143 |
.stderr_only("base32: invalid wrap size: 'b'\n"); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn test_base32_extra_operand() { |
| 149 |
// Expect a failure when multiple files are specified. |
| 150 |
new_ucmd!() |
| 151 |
.arg("a.txt") |
| 152 |
.arg("b.txt") |
| 153 |
.fails() |
| 154 |
.usage_error("extra operand 'b.txt'"); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn test_base32_file_not_found() { |
| 159 |
new_ucmd!() |
| 160 |
.arg("a.txt") |
| 161 |
.fails() |
| 162 |
.stderr_only("base32: a.txt: No such file or directory\n"); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn test_encode_large_input_is_buffered() { |
| 167 |
let input = "A".repeat(6000); |
| 168 |
new_ucmd!() |
| 169 |
.pipe_in(input) |
| 170 |
.succeeds() |
| 171 |
.stdout_contains("BIFAUCQK"); // spell-checker:disable-line |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
#[cfg(unix)] |
| 176 |
fn test_base32_file_with_trailing_slash() { |
| 177 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 178 |
at.write("a", "b"); |
| 179 |
|
| 180 |
ucmd.arg("a/") |
| 181 |
.fails() |
| 182 |
.stderr_only("base32: a/: Not a directory\n"); |
| 183 |
} |