coreutils/tests/by-util/test_od.rs

876 lines
23 KiB
Rust
Raw Normal View History

2021-10-10 08:10:46 +00:00
// spell-checker:ignore abcdefghijklmnopqrstuvwxyz
// * 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.
extern crate unindent;
2020-04-13 18:36:03 +00:00
use self::unindent::*;
use crate::common::util::*;
2016-04-26 03:06:38 +00:00
use std::env;
use std::fs::remove_file;
2020-04-13 18:36:03 +00:00
use std::fs::File;
use std::io::Write;
use std::path::Path;
// octal dump of 'abcdefghijklmnopqrstuvwxyz\n' // spell-checker:disable-line
2021-05-29 12:32:35 +00:00
static ALPHA_OUT: &str = "
0000000 061141 062143 063145 064147 065151 066153 067155 070157
0000020 071161 072163 073165 074167 075171 000012
0000033
";
2016-11-25 19:36:56 +00:00
// XXX We could do a better job of ensuring that we have a fresh temp dir to ourselves,
// not a general one full of other process leftovers.
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
// Test that od can read one file and dump with default format
2016-04-26 03:06:38 +00:00
#[test]
fn test_file() {
2021-04-01 00:16:15 +00:00
// TODO: Can this be replaced by AtPath?
use std::env;
let temp = env::temp_dir();
2016-04-26 03:06:38 +00:00
let tmpdir = Path::new(&temp);
let file = tmpdir.join("test");
2016-04-26 03:06:38 +00:00
{
let mut f = File::create(&file).unwrap();
// spell-checker:disable-next-line
assert!(
2022-01-30 10:09:50 +00:00
f.write_all(b"abcdefghijklmnopqrstuvwxyz\n").is_ok(),
"Test setup failed - could not write file"
);
2016-04-26 03:06:38 +00:00
}
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg(file.as_os_str())
2021-04-01 00:16:15 +00:00
.succeeds()
.no_stderr()
.stdout_is(unindent(ALPHA_OUT));
// Ensure that default format matches `-t o2`, and that `-t` does not absorb file argument
new_ucmd!()
.arg("--endian=little")
.arg("-t")
.arg("o2")
.arg(file.as_os_str())
.succeeds()
.no_stderr()
.stdout_is(unindent(ALPHA_OUT));
2016-04-26 03:06:38 +00:00
let _ = remove_file(file);
}
2016-04-26 03:06:38 +00:00
// Test that od can read 2 files and concatenate the contents
#[test]
fn test_2files() {
let temp = env::temp_dir();
2016-04-26 03:06:38 +00:00
let tmpdir = Path::new(&temp);
let file1 = tmpdir.join("test1");
let file2 = tmpdir.join("test2");
2022-04-02 08:47:37 +00:00
for (n, a) in [(1, "a"), (2, "b")] {
println!("number: {n} letter:{a}");
2020-04-13 18:36:03 +00:00
}
// spell-checker:disable-next-line
2022-04-02 08:47:37 +00:00
for (path, data) in [(&file1, "abcdefghijklmnop"), (&file2, "qrstuvwxyz\n")] {
let mut f = File::create(path).unwrap();
assert!(
2022-01-30 10:09:50 +00:00
f.write_all(data.as_bytes()).is_ok(),
"Test setup failed - could not write file"
);
2016-04-26 03:06:38 +00:00
}
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg(file1.as_os_str())
.arg(file2.as_os_str())
2021-04-01 00:16:15 +00:00
.succeeds()
.no_stderr()
.stdout_is(unindent(ALPHA_OUT));
// TODO: Handle errors?
2016-04-26 03:06:38 +00:00
let _ = remove_file(file1);
let _ = remove_file(file2);
}
2016-11-25 19:36:56 +00:00
// Test that od gives non-0 exit val for filename that doesn't exist.
2016-04-26 03:06:38 +00:00
#[test]
fn test_no_file() {
let temp = env::temp_dir();
2016-04-26 03:06:38 +00:00
let tmpdir = Path::new(&temp);
2021-05-31 03:55:28 +00:00
let file = tmpdir.join("}surely'none'would'thus'a'file'name"); // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!().arg(file.as_os_str()).fails();
}
// Test that od reads from stdin instead of a file
#[test]
fn test_from_stdin() {
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopqrstuvwxyz\n"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.success()
.no_stderr()
.stdout_is(unindent(ALPHA_OUT));
}
// Test that od reads from stdin and also from files
#[test]
fn test_from_mixed() {
let temp = env::temp_dir();
let tmpdir = Path::new(&temp);
let file1 = tmpdir.join("test-1");
let file3 = tmpdir.join("test-3");
// spell-checker:disable-next-line
2020-04-13 18:36:03 +00:00
let (data1, data2, data3) = ("abcdefg", "hijklmnop", "qrstuvwxyz\n");
2022-04-02 08:47:37 +00:00
for (path, data) in [(&file1, data1), (&file3, data3)] {
let mut f = File::create(path).unwrap();
assert!(
2022-01-30 10:09:50 +00:00
f.write_all(data.as_bytes()).is_ok(),
"Test setup failed - could not write file"
);
}
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg(file1.as_os_str())
.arg("-")
.arg(file3.as_os_str())
2021-04-01 00:16:15 +00:00
.run_piped_stdin(data2.as_bytes())
.success()
.no_stderr()
.stdout_is(unindent(ALPHA_OUT));
}
#[test]
fn test_multiple_formats() {
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopqrstuvwxyz\n"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-c")
.arg("-b")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.success()
.no_stderr()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
"
0000000 a b c d e f g h i j k l m n o p
141 142 143 144 145 146 147 150 151 152 153 154 155 156 157 160
0000020 q r s t u v w x y z \\n
161 162 163 164 165 166 167 170 171 172 012
0000033
2021-04-01 00:16:15 +00:00
",
));
}
#[test]
fn test_dec() {
// spell-checker:ignore (words) 0xffu8 xffu
let input = [
2020-04-13 18:36:03 +00:00
0u8, 0u8, 1u8, 0u8, 2u8, 0u8, 3u8, 0u8, 0xffu8, 0x7fu8, 0x00u8, 0x80u8, 0x01u8, 0x80u8,
];
let expected_output = unindent(
"
0000000 0 1 2 3 32767 -32768 -32767
0000016
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-s")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
}
2016-07-24 19:51:21 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_hex16() {
let input: [u8; 9] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff];
// spell-checker:disable
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
0000000 2301 6745 ab89 efcd 00ff
2016-07-24 19:51:21 +00:00
0000011
2020-04-13 18:36:03 +00:00
",
);
// spell-checker:enable
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-x")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
2016-07-24 19:51:21 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_hex32() {
let input: [u8; 9] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff];
let expected_output = unindent(
"
0000000 67452301 efcdab89 000000ff
2016-07-24 19:51:21 +00:00
0000011
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-X")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
2016-07-24 19:51:21 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_f16() {
let input: [u8; 14] = [
0x00, 0x3c, // 0x3C00 1.0
0x00, 0x00, // 0x0000 0.0
0x00, 0x80, // 0x8000 -0.0
0x00, 0x7c, // 0x7C00 Inf
0x00, 0xfc, // 0xFC00 -Inf
0x00, 0xfe, // 0xFE00 NaN
2020-04-13 18:36:03 +00:00
0x00, 0x84,
]; // 0x8400 -6.104e-5
let expected_output = unindent(
"
0000000 1.000 0 -0 inf
0000010 -inf NaN -6.104e-5
0000016
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-tf2")
.arg("-w8")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_f32() {
let input: [u8; 28] = [
0x52, 0x06, 0x9e, 0xbf, // 0xbf9e0652 -1.2345679
0x4e, 0x61, 0x3c, 0x4b, // 0x4b3c614e 12345678
0x0f, 0x9b, 0x94, 0xfe, // 0xfe949b0f -9.876543E37
0x00, 0x00, 0x00, 0x80, // 0x80000000 -0.0
0xff, 0xff, 0xff, 0x7f, // 0x7fffffff NaN
0xc2, 0x16, 0x01, 0x00, // 0x000116c2 1e-40
2020-04-13 18:36:03 +00:00
0x00, 0x00, 0x7f, 0x80,
]; // 0x807f0000 -1.1663108E-38
let expected_output = unindent(
"
0000000 -1.2345679 12345678 -9.8765427e37 -0
0000020 NaN 1e-40 -1.1663108e-38
0000034
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-f")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_f64() {
let input: [u8; 40] = [
2020-04-13 18:36:03 +00:00
0x27, 0x6b, 0x0a, 0x2f, 0x2a, 0xee, 0x45,
0x43, // 0x4345EE2A2F0A6B27 12345678912345678
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0000000000000000 0
2020-04-13 18:36:03 +00:00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, // 0x8010000000000000 -2.2250738585072014e-308
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, // 0x0000000000000001 5e-324 (subnormal)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
]; // 0xc000000000000000 -2
let expected_output = unindent(
"
0000000 12345678912345678 0
0000020 -2.2250738585072014e-308 5e-324
0000040 -2.0000000000000000
0000050
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-F")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
}
#[test]
2016-08-01 14:39:58 +00:00
fn test_multibyte() {
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-c")
.arg("-w12")
2021-05-31 03:55:28 +00:00
.run_piped_stdin("Universität Tübingen \u{1B000}".as_bytes()) // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
.success()
.no_stderr()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
"
2016-08-01 14:39:58 +00:00
0000000 U n i v e r s i t ä ** t
2016-08-07 21:57:53 +00:00
0000014 T ü ** b i n g e n \u{1B000}
0000030 ** ** **
2016-08-01 14:39:58 +00:00
0000033
2021-04-01 00:16:15 +00:00
",
));
}
2016-07-23 19:49:43 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_width() {
let input: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
0000000 000000 000000
0000004 000000 000000
2016-07-23 19:49:43 +00:00
0000010
2020-04-13 18:36:03 +00:00
",
);
2016-07-23 19:49:43 +00:00
2021-04-01 00:16:15 +00:00
new_ucmd!()
.arg("-w4")
.arg("-v")
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
2016-07-23 19:49:43 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_invalid_width() {
let input: [u8; 4] = [0x00, 0x00, 0x00, 0x00];
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
0000000 000000
0000002 000000
2016-07-23 19:49:43 +00:00
0000004
2020-04-13 18:36:03 +00:00
",
);
2016-07-23 19:49:43 +00:00
2021-04-01 00:16:15 +00:00
new_ucmd!()
.arg("-w5")
.arg("-v")
.run_piped_stdin(&input[..])
.success()
.stderr_is_bytes("od: warning: invalid width 5; using 2 instead\n".as_bytes())
.stdout_is(expected_output);
2016-07-23 19:49:43 +00:00
}
2016-08-23 20:55:40 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_zero_width() {
let input: [u8; 4] = [0x00, 0x00, 0x00, 0x00];
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
2016-08-23 20:55:40 +00:00
0000000 000000
0000002 000000
0000004
2020-04-13 18:36:03 +00:00
",
);
2016-08-23 20:55:40 +00:00
2021-04-01 00:16:15 +00:00
new_ucmd!()
.arg("-w0")
.arg("-v")
.run_piped_stdin(&input[..])
.success()
.stderr_is_bytes("od: warning: invalid width 0; using 2 instead\n".as_bytes())
.stdout_is(expected_output);
2016-08-23 20:55:40 +00:00
}
2016-07-23 19:49:43 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_width_without_value() {
let input: [u8; 40] = [0; 40];
2016-07-23 19:49:43 +00:00
let expected_output = unindent("
0000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
0000040 000000 000000 000000 000000
2016-07-23 19:49:43 +00:00
0000050
");
2021-04-01 00:16:15 +00:00
new_ucmd!()
.arg("-w")
.run_piped_stdin(&input[..])
.success()
.no_stderr()
.stdout_is(expected_output);
2016-07-23 19:49:43 +00:00
}
2016-07-25 12:17:45 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_suppress_duplicates() {
let input: [u8; 41] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
let expected_output = unindent(
"
0000000 00000000000
0000 0000
2016-07-25 12:17:45 +00:00
*
2016-08-07 21:57:53 +00:00
0000020 00000000001
0001 0000
0000024 00000000000
0000 0000
*
0000050 00000000000
0000
2016-07-25 12:17:45 +00:00
0000051
2020-04-13 18:36:03 +00:00
",
);
2016-07-25 12:17:45 +00:00
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-w4")
.arg("-O")
.arg("-x")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
2016-07-25 12:17:45 +00:00
}
2016-07-30 18:39:09 +00:00
#[test]
fn test_big_endian() {
2020-04-13 18:36:03 +00:00
let input: [u8; 8] = [0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; // 0xc000000000000000 -2
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
0000000 -2.0000000000000000
-2.0000000 0
c0000000 00000000
c000 0000 0000 0000
0000010
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=big")
.arg("-F")
.arg("-f")
.arg("-X")
.arg("-x")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
#[allow(non_snake_case)]
fn test_alignment_Xxa() {
2020-04-13 18:36:03 +00:00
let input: [u8; 8] = [0x0A, 0x0D, 0x65, 0x66, 0x67, 0x00, 0x9e, 0x9f];
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
0000000 66650d0a 9f9e0067
0d0a 6665 0067 9f9e
2016-08-01 14:39:58 +00:00
nl cr e f g nul rs us
0000010
2020-04-13 18:36:03 +00:00
",
);
// in this case the width of the -a (8-bit) determines the alignment for the other fields
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-X")
.arg("-x")
.arg("-a")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
#[allow(non_snake_case)]
fn test_alignment_Fx() {
2020-04-13 18:36:03 +00:00
let input: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0]; // 0xc000000000000000 -2
2020-04-13 18:36:03 +00:00
let expected_output = unindent(
"
0000000 -2.0000000000000000
0000 0000 0000 c000
0000010
2020-04-13 18:36:03 +00:00
",
);
// in this case the width of the -F (64-bit) determines the alignment for the other field
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("-F")
.arg("-x")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
fn test_max_uint() {
2020-04-13 18:36:03 +00:00
let input = [0xFFu8; 8];
let expected_output = unindent(
"
2016-08-11 11:45:39 +00:00
0000000 1777777777777777777777
37777777777 37777777777
177777 177777 177777 177777
377 377 377 377 377 377 377 377
2016-08-11 11:45:39 +00:00
18446744073709551615
4294967295 4294967295
65535 65535 65535 65535
2016-08-11 11:45:39 +00:00
255 255 255 255 255 255 255 255
2016-07-30 18:39:09 +00:00
0000010
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--format=o8")
2021-05-31 03:55:28 +00:00
.arg("-Oobtu8") // spell-checker:disable-line
2020-04-13 18:36:03 +00:00
.arg("-Dd")
.arg("--format=u1")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
2016-07-30 18:39:09 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_hex_offset() {
let input = [0u8; 0x1F];
let expected_output = unindent(
"
000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
000010 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00001F
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-Ax")
.arg("-X")
.arg("-X")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_dec_offset() {
let input = [0u8; 19];
let expected_output = unindent(
"
0000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
0000016 00000000
00000000
0000019
2020-04-13 18:36:03 +00:00
",
);
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-Ad")
.arg("-X")
.arg("-X")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_no_offset() {
let input = [0u8; 31];
2021-05-29 12:32:35 +00:00
const LINE: &str = " 00000000 00000000 00000000 00000000\n";
let expected_output = [LINE, LINE, LINE, LINE].join("");
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-An")
.arg("-X")
.arg("-X")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_invalid_offset() {
2021-04-01 00:16:15 +00:00
new_ucmd!().arg("-Ab").fails();
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_skip_bytes() {
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopq"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-c")
.arg("--skip-bytes=5")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
"
0000005 f g h i j k l m n o p q
0000021
2021-04-01 00:16:15 +00:00
",
));
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_skip_bytes_error() {
let input = "12345";
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--skip-bytes=10")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.failure();
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_read_bytes() {
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopqrstuvwxyz\n12345678"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--endian=little")
.arg("--read-bytes=27")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(ALPHA_OUT));
}
2016-08-12 15:51:24 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_ascii_dump() {
let input: [u8; 22] = [
2020-04-13 18:36:03 +00:00
0x00, 0x01, 0x0a, 0x0d, 0x10, 0x1f, 0x20, 0x61, 0x62, 0x63, 0x7d, 0x7e, 0x7f, 0x80, 0x90,
0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff,
];
2021-04-01 00:16:15 +00:00
new_ucmd!()
.arg("-tx1zacz") // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
.run_piped_stdin(&input[..])
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
r"
2016-08-12 15:51:24 +00:00
0000000 00 01 0a 0d 10 1f 20 61 62 63 7d 7e 7f 80 90 a0 >...... abc}~....<
nul soh nl cr dle us sp a b c } ~ del nul dle sp
\0 001 \n \r 020 037 a b c } ~ 177 ** ** ** >...... abc}~....<
0000020 b0 c0 d0 e0 f0 ff >......<
0 @ P ` p del
** 300 320 340 360 377 >......<
0000026
2021-04-01 00:16:15 +00:00
",
));
2016-08-12 15:51:24 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_filename_parsing() {
// files "a" and "x" both exists, but are no filenames in the command line below
// "-f" must be treated as a filename, it contains the text: minus lowercase f
// so "-f" should not be interpreted as a formatting option.
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--format")
.arg("a")
.arg("-A")
.arg("x")
.arg("--")
.arg("-f")
2021-04-01 00:16:15 +00:00
.succeeds()
.no_stderr()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
"
000000 m i n u s sp l o w e r c a s e sp
000010 f nl
000012
2021-04-01 00:16:15 +00:00
",
));
}
2016-08-15 22:37:33 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_stdin_offset() {
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopq"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-c")
.arg("+5")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
"
2016-08-15 22:37:33 +00:00
0000005 f g h i j k l m n o p q
0000021
2021-04-01 00:16:15 +00:00
",
));
2016-08-15 22:37:33 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_file_offset() {
2021-04-01 00:16:15 +00:00
new_ucmd!()
.arg("-c")
.arg("--")
.arg("-f")
.arg("10")
.succeeds()
.no_stderr()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
r"
2016-08-15 22:37:33 +00:00
0000010 w e r c a s e f \n
0000022
2021-04-01 00:16:15 +00:00
",
));
2016-08-15 22:37:33 +00:00
}
2016-08-18 20:17:03 +00:00
#[test]
2020-04-13 18:36:03 +00:00
fn test_traditional() {
2016-08-18 20:17:03 +00:00
// note gnu od does not align both lines
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopq"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--traditional")
.arg("-a")
.arg("-c")
.arg("-")
.arg("10")
.arg("0")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
r"
2016-08-18 20:17:03 +00:00
0000010 (0000000) i j k l m n o p q
i j k l m n o p q
0000021 (0000011)
2021-04-01 00:16:15 +00:00
",
));
2016-08-18 20:17:03 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_traditional_with_skip_bytes_override() {
2016-08-18 20:17:03 +00:00
// --skip-bytes is ignored in this case
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnop"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--traditional")
.arg("--skip-bytes=10")
.arg("-c")
.arg("0")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
r"
2016-08-18 20:17:03 +00:00
0000000 a b c d e f g h i j k l m n o p
0000020
2021-04-01 00:16:15 +00:00
",
));
2016-08-18 20:17:03 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_traditional_with_skip_bytes_non_override() {
2016-08-18 20:17:03 +00:00
// no offset specified in the traditional way, so --skip-bytes is used
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnop"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--traditional")
.arg("--skip-bytes=10")
.arg("-c")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
r"
2016-08-18 20:17:03 +00:00
0000012 k l m n o p
0000020
2021-04-01 00:16:15 +00:00
",
));
2016-08-18 20:17:03 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_traditional_error() {
2016-08-18 20:17:03 +00:00
// file "0" exists - don't fail on that, but --traditional only accepts a single input
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("--traditional")
.arg("0")
.arg("0")
.arg("0")
.arg("0")
2021-04-01 00:16:15 +00:00
.fails();
2016-08-18 20:17:03 +00:00
}
#[test]
2020-04-13 18:36:03 +00:00
fn test_traditional_only_label() {
2021-05-31 03:55:28 +00:00
let input = "abcdefghijklmnopqrstuvwxyz"; // spell-checker:disable-line
2021-04-01 00:16:15 +00:00
new_ucmd!()
2020-04-13 18:36:03 +00:00
.arg("-An")
.arg("--traditional")
.arg("-a")
.arg("-c")
.arg("-")
.arg("10")
.arg("0x10")
2021-04-01 00:16:15 +00:00
.run_piped_stdin(input.as_bytes())
.no_stderr()
.success()
.stdout_is(unindent(
2020-04-13 18:36:03 +00:00
r"
2016-08-18 20:17:03 +00:00
(0000020) i j k l m n o p q r s t u v w x
i j k l m n o p q r s t u v w x
(0000040) y z
y z
(0000042)
2021-04-01 00:16:15 +00:00
",
));
2016-08-18 20:17:03 +00:00
}
#[test]
fn test_od_invalid_bytes() {
const INVALID_SIZE: &str = "x";
const INVALID_SUFFIX: &str = "1fb4t";
const BIG_SIZE: &str = "1Y";
// NOTE:
// GNU's od (8.32) with option '--width' does not accept 'Y' as valid suffix.
// According to the man page it should be valid in the same way it is valid for
// '--read-bytes' and '--skip-bytes'.
let options = [
"--read-bytes",
"--skip-bytes",
"--width",
// "--strings", // TODO: consider testing here once '--strings' is implemented
];
for option in &options {
new_ucmd!()
.arg(format!("{option}={INVALID_SIZE}"))
.arg("file")
.fails()
.code_is(1)
.stderr_only(format!("od: invalid {option} argument '{INVALID_SIZE}'\n"));
new_ucmd!()
.arg(format!("{option}={INVALID_SUFFIX}"))
.arg("file")
.fails()
.code_is(1)
.stderr_only(format!(
"od: invalid suffix in {option} argument '{INVALID_SUFFIX}'\n"
));
#[cfg(not(target_pointer_width = "128"))]
new_ucmd!()
.arg(format!("{option}={BIG_SIZE}"))
.arg("file")
.fails()
.code_is(1)
.stderr_only(format!("od: {option} argument '{BIG_SIZE}' too large\n"));
}
}