| 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 |
// spell-checker:ignore abcdefghijklmnopqrstuvwxyz Anone fdbb littl bfloat |
| 7 |
|
| 8 |
#[cfg(unix)] |
| 9 |
use std::io::Read; |
| 10 |
#[cfg(target_os = "linux")] |
| 11 |
use std::path::Path; |
| 12 |
|
| 13 |
use unindent::unindent; |
| 14 |
use uutests::util::TestScenario; |
| 15 |
use uutests::{at_and_ucmd, new_ucmd, util_name}; |
| 16 |
|
| 17 |
// octal dump of 'abcdefghijklmnopqrstuvwxyz\n' |
| 18 |
static ALPHA_OUT: &str = " |
| 19 |
0000000 061141 062143 063145 064147 065151 066153 067155 070157 |
| 20 |
0000020 071161 072163 073165 074167 075171 000012 |
| 21 |
0000033 |
| 22 |
"; |
| 23 |
|
| 24 |
fn erange_message() -> String { |
| 25 |
let err = std::io::Error::from_raw_os_error(libc::ERANGE); |
| 26 |
let msg = err.to_string(); |
| 27 |
msg.split(" (os error").next().unwrap_or(&msg).to_string() |
| 28 |
} |
| 29 |
|
| 30 |
fn run_skip_across_inputs(files: &[(&str, &str)], skip: u64, expected: &str) { |
| 31 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 32 |
for (name, contents) in files { |
| 33 |
at.write(name, contents); |
| 34 |
} |
| 35 |
|
| 36 |
ucmd.arg("-c").arg("-j").arg(skip.to_string()).arg("-An"); |
| 37 |
|
| 38 |
for (name, _) in files { |
| 39 |
ucmd.arg(name); |
| 40 |
} |
| 41 |
|
| 42 |
ucmd.succeeds().stdout_only(expected); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_invalid_arg() { |
| 47 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 48 |
} |
| 49 |
|
| 50 |
// Test that od can read one file and dump with default format |
| 51 |
#[test] |
| 52 |
fn test_file() { |
| 53 |
let scene = TestScenario::new(util_name!()); |
| 54 |
let at = &scene.fixtures; |
| 55 |
at.write("test", "abcdefghijklmnopqrstuvwxyz\n"); |
| 56 |
|
| 57 |
for arg in ["--endian=little", "--endian=littl", "--endian=l"] { |
| 58 |
scene |
| 59 |
.ucmd() |
| 60 |
.arg(arg) |
| 61 |
.arg("test") |
| 62 |
.succeeds() |
| 63 |
.stdout_only(unindent(ALPHA_OUT)); |
| 64 |
} |
| 65 |
|
| 66 |
// Ensure that default format matches `-t o2`, and that `-t` does not absorb file argument |
| 67 |
scene |
| 68 |
.ucmd() |
| 69 |
.arg("--endian=little") |
| 70 |
.arg("-t") |
| 71 |
.arg("o2") |
| 72 |
.arg("test") |
| 73 |
.succeeds() |
| 74 |
.stdout_only(unindent(ALPHA_OUT)); |
| 75 |
} |
| 76 |
|
| 77 |
// Test that od can read 2 files and concatenate the contents |
| 78 |
#[test] |
| 79 |
fn test_two_files() { |
| 80 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 81 |
at.write("test1", "abcdefghijklmnop"); |
| 82 |
at.write("test2", "qrstuvwxyz\n"); // spell-checker:disable-line |
| 83 |
ucmd.arg("--endian=little") |
| 84 |
.arg("test1") |
| 85 |
.arg("test2") |
| 86 |
.succeeds() |
| 87 |
.stdout_only(unindent(ALPHA_OUT)); |
| 88 |
} |
| 89 |
|
| 90 |
// Test that od gives non-0 exit val for filename that doesn't exist. |
| 91 |
#[test] |
| 92 |
fn test_non_existing_file() { |
| 93 |
new_ucmd!().arg("non_existing_file").fails(); |
| 94 |
} |
| 95 |
|
| 96 |
// Test that od reads from stdin instead of a file |
| 97 |
#[test] |
| 98 |
fn test_from_stdin() { |
| 99 |
let input = "abcdefghijklmnopqrstuvwxyz\n"; |
| 100 |
new_ucmd!() |
| 101 |
.arg("--endian=little") |
| 102 |
.run_piped_stdin(input.as_bytes()) |
| 103 |
.success() |
| 104 |
.stdout_only(unindent(ALPHA_OUT)); |
| 105 |
} |
| 106 |
|
| 107 |
// Test that od reads from stdin and also from files |
| 108 |
#[test] |
| 109 |
fn test_from_mixed() { |
| 110 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 111 |
// spell-checker:disable-next-line |
| 112 |
let (data1, data2, data3) = ("abcdefg", "hijklmnop", "qrstuvwxyz\n"); |
| 113 |
at.write("test-1", data1); |
| 114 |
at.write("test-3", data3); |
| 115 |
|
| 116 |
ucmd.arg("--endian=little") |
| 117 |
.arg("test-1") |
| 118 |
.arg("-") |
| 119 |
.arg("test-3") |
| 120 |
.run_piped_stdin(data2.as_bytes()) |
| 121 |
.success() |
| 122 |
.stdout_only(unindent(ALPHA_OUT)); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn test_multiple_formats() { |
| 127 |
let input = "abcdefghijklmnopqrstuvwxyz\n"; |
| 128 |
new_ucmd!() |
| 129 |
.arg("-c") |
| 130 |
.arg("-b") |
| 131 |
.run_piped_stdin(input.as_bytes()) |
| 132 |
.success() |
| 133 |
.stdout_only(unindent( |
| 134 |
" |
| 135 |
0000000 a b c d e f g h i j k l m n o p |
| 136 |
141 142 143 144 145 146 147 150 151 152 153 154 155 156 157 160 |
| 137 |
0000020 q r s t u v w x y z \\n |
| 138 |
161 162 163 164 165 166 167 170 171 172 012 |
| 139 |
0000033 |
| 140 |
", |
| 141 |
)); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn test_dec() { |
| 146 |
// spell-checker:ignore (words) 0xffu8 xffu |
| 147 |
let input = [ |
| 148 |
0u8, 0u8, 1u8, 0u8, 2u8, 0u8, 3u8, 0u8, 0xffu8, 0x7fu8, 0x00u8, 0x80u8, 0x01u8, 0x80u8, |
| 149 |
]; |
| 150 |
let expected_output = unindent( |
| 151 |
" |
| 152 |
0000000 0 1 2 3 32767 -32768 -32767 |
| 153 |
0000016 |
| 154 |
", |
| 155 |
); |
| 156 |
new_ucmd!() |
| 157 |
.arg("--endian=little") |
| 158 |
.arg("-s") |
| 159 |
.run_piped_stdin(&input[..]) |
| 160 |
.success() |
| 161 |
.stdout_only(expected_output); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn test_hex16() { |
| 166 |
let input: [u8; 9] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]; |
| 167 |
// spell-checker:disable |
| 168 |
let expected_output = unindent( |
| 169 |
" |
| 170 |
0000000 2301 6745 ab89 efcd 00ff |
| 171 |
0000011 |
| 172 |
", |
| 173 |
); |
| 174 |
// spell-checker:enable |
| 175 |
new_ucmd!() |
| 176 |
.arg("--endian=little") |
| 177 |
.arg("-x") |
| 178 |
.run_piped_stdin(&input[..]) |
| 179 |
.success() |
| 180 |
.stdout_only(expected_output); |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn test_hex32() { |
| 185 |
let input: [u8; 9] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]; |
| 186 |
let expected_output = unindent( |
| 187 |
" |
| 188 |
0000000 67452301 efcdab89 000000ff |
| 189 |
0000011 |
| 190 |
", |
| 191 |
); |
| 192 |
new_ucmd!() |
| 193 |
.arg("--endian=little") |
| 194 |
.arg("-X") |
| 195 |
.run_piped_stdin(&input[..]) |
| 196 |
.success() |
| 197 |
.stdout_only(expected_output); |
| 198 |
} |
| 199 |
|
| 200 |
// Regression: 16-bit IEEE half should print with canonical precision (no spurious digits) |
| 201 |
#[test] |
| 202 |
fn test_float16_compact() { |
| 203 |
let input: [u8; 4] = [0x3c, 0x00, 0x3c, 0x00]; // two times 1.0 in big-endian half |
| 204 |
new_ucmd!() |
| 205 |
.arg("--endian=big") |
| 206 |
.arg("-An") |
| 207 |
.arg("-tfH") |
| 208 |
.run_piped_stdin(&input[..]) |
| 209 |
.success() |
| 210 |
.stdout_only(" 1 1\n"); |
| 211 |
} |
| 212 |
|
| 213 |
// Regression: 16-bit bfloat should print with canonical precision (no spurious digits) |
| 214 |
#[test] |
| 215 |
fn test_bfloat16_compact() { |
| 216 |
let input: [u8; 4] = [0x3f, 0x80, 0x3f, 0x80]; // two times 1.0 in big-endian bfloat16 |
| 217 |
new_ucmd!() |
| 218 |
.arg("--endian=big") |
| 219 |
.arg("-An") |
| 220 |
.arg("-tfB") |
| 221 |
.run_piped_stdin(&input[..]) |
| 222 |
.success() |
| 223 |
.stdout_only(" 1 1\n"); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn test_tf_default_is_double() { |
| 228 |
let input: [u8; 8] = [0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x40]; |
| 229 |
let default_output = new_ucmd!() |
| 230 |
.arg("--endian=little") |
| 231 |
.arg("-An") |
| 232 |
.arg("-tf") |
| 233 |
.run_piped_stdin(&input[..]) |
| 234 |
.success() |
| 235 |
.stdout_str() |
| 236 |
.to_string(); |
| 237 |
|
| 238 |
let explicit_double_output = new_ucmd!() |
| 239 |
.arg("--endian=little") |
| 240 |
.arg("-An") |
| 241 |
.arg("-tfD") |
| 242 |
.run_piped_stdin(&input[..]) |
| 243 |
.success() |
| 244 |
.stdout_str() |
| 245 |
.to_string(); |
| 246 |
|
| 247 |
let explicit_float_output = new_ucmd!() |
| 248 |
.arg("--endian=little") |
| 249 |
.arg("-An") |
| 250 |
.arg("-tfF") |
| 251 |
.run_piped_stdin(&input[..]) |
| 252 |
.success() |
| 253 |
.stdout_str() |
| 254 |
.to_string(); |
| 255 |
|
| 256 |
assert_eq!(default_output, explicit_double_output); |
| 257 |
assert_ne!(default_output, explicit_float_output); |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn test_tf_explicit_float_still_uses_4_bytes() { |
| 262 |
let input: [u8; 8] = [0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x40]; |
| 263 |
new_ucmd!() |
| 264 |
.arg("--endian=little") |
| 265 |
.arg("-An") |
| 266 |
.arg("-tfF") |
| 267 |
.run_piped_stdin(&input[..]) |
| 268 |
.success() |
| 269 |
.stdout_only(" 1 2\n"); |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn test_f16() { |
| 274 |
let input: [u8; 14] = [ |
| 275 |
0x00, 0x3c, // 0x3C00 1.0 |
| 276 |
0x00, 0x00, // 0x0000 0.0 |
| 277 |
0x00, 0x80, // 0x8000 -0.0 |
| 278 |
0x00, 0x7c, // 0x7C00 Inf |
| 279 |
0x00, 0xfc, // 0xFC00 -Inf |
| 280 |
0x00, 0xfe, // 0xFE00 NaN |
| 281 |
0x00, 0x84, |
| 282 |
]; // 0x8400 -6.104e-5 |
| 283 |
let expected_output = unindent( |
| 284 |
" |
| 285 |
0000000 1 0 -0 inf |
| 286 |
0000010 -inf NaN -6.1035156e-5 |
| 287 |
0000016 |
| 288 |
", |
| 289 |
); |
| 290 |
new_ucmd!() |
| 291 |
.arg("--endian=little") |
| 292 |
.arg("-tf2") |
| 293 |
.arg("-w8") |
| 294 |
.run_piped_stdin(&input[..]) |
| 295 |
.success() |
| 296 |
.stdout_only(expected_output); |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn test_fh() { |
| 301 |
let input: [u8; 14] = [ |
| 302 |
0x00, 0x3c, // 0x3C00 1.0 |
| 303 |
0x00, 0x00, // 0x0000 0.0 |
| 304 |
0x00, 0x80, // 0x8000 -0.0 |
| 305 |
0x00, 0x7c, // 0x7C00 Inf |
| 306 |
0x00, 0xfc, // 0xFC00 -Inf |
| 307 |
0x00, 0xfe, // 0xFE00 NaN |
| 308 |
0x00, 0x84, |
| 309 |
]; // 0x8400 -6.1035156e-5 |
| 310 |
let expected_output = unindent( |
| 311 |
" |
| 312 |
0000000 1 0 -0 inf |
| 313 |
0000010 -inf NaN -6.1035156e-5 |
| 314 |
0000016 |
| 315 |
", |
| 316 |
); |
| 317 |
new_ucmd!() |
| 318 |
.arg("--endian=little") |
| 319 |
.arg("-tfH") |
| 320 |
.arg("-w8") |
| 321 |
.run_piped_stdin(&input[..]) |
| 322 |
.success() |
| 323 |
.stdout_only(expected_output); |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn test_fb() { |
| 328 |
let input: [u8; 14] = [ |
| 329 |
0x80, 0x3f, // 1.0 |
| 330 |
0x00, 0x00, // 0.0 |
| 331 |
0x00, 0x80, // -0.0 |
| 332 |
0x80, 0x7f, // Inf |
| 333 |
0x80, 0xff, // -Inf |
| 334 |
0xc0, 0x7f, // NaN |
| 335 |
0x80, 0xb8, |
| 336 |
]; // -6.1035156e-5 |
| 337 |
let expected_output = unindent( |
| 338 |
" |
| 339 |
0000000 1 0 -0 inf |
| 340 |
0000010 -inf NaN -6.1035156e-5 |
| 341 |
0000016 |
| 342 |
", |
| 343 |
); |
| 344 |
new_ucmd!() |
| 345 |
.arg("--endian=little") |
| 346 |
.arg("-tfB") |
| 347 |
.arg("-w8") |
| 348 |
.run_piped_stdin(&input[..]) |
| 349 |
.success() |
| 350 |
.stdout_only(expected_output); |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn test_f32() { |
| 355 |
let input: [u8; 28] = [ |
| 356 |
0x52, 0x06, 0x9e, 0xbf, // 0xbf9e0652 -1.2345679 |
| 357 |
0x4e, 0x61, 0x3c, 0x4b, // 0x4b3c614e 12345678 |
| 358 |
0x0f, 0x9b, 0x94, 0xfe, // 0xfe949b0f -9.876543E37 |
| 359 |
0x00, 0x00, 0x00, 0x80, // 0x80000000 -0.0 |
| 360 |
0xff, 0xff, 0xff, 0x7f, // 0x7fffffff NaN |
| 361 |
0xc2, 0x16, 0x01, 0x00, // 0x000116c2 1e-40 |
| 362 |
0x00, 0x00, 0x7f, 0x80, |
| 363 |
]; // 0x807f0000 -1.1663108E-38 |
| 364 |
let expected_output = unindent( |
| 365 |
" |
| 366 |
0000000 -1.2345679 12345678 -9.876543e+37 -0 |
| 367 |
0000020 NaN 1e-40 -1.1663108e-38 |
| 368 |
0000034 |
| 369 |
", |
| 370 |
); |
| 371 |
new_ucmd!() |
| 372 |
.arg("--endian=little") |
| 373 |
.arg("-f") |
| 374 |
.run_piped_stdin(&input[..]) |
| 375 |
.success() |
| 376 |
.stdout_only(expected_output); |
| 377 |
} |
| 378 |
|
| 379 |
#[test] |
| 380 |
fn test_f64() { |
| 381 |
let input: [u8; 40] = [ |
| 382 |
0x27, 0x6b, 0x0a, 0x2f, 0x2a, 0xee, 0x45, |
| 383 |
0x43, // 0x4345EE2A2F0A6B27 12345678912345678 |
| 384 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0000000000000000 0 |
| 385 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, |
| 386 |
0x80, // 0x8010000000000000 -2.2250738585072014e-308 |
| 387 |
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 388 |
0x00, // 0x0000000000000001 5e-324 (subnormal) |
| 389 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, |
| 390 |
]; // 0xc000000000000000 -2 |
| 391 |
let expected_output = unindent( |
| 392 |
" |
| 393 |
0000000 12345678912345678 0 |
| 394 |
0000020 -2.2250738585072014e-308 5e-324 |
| 395 |
0000040 -2 |
| 396 |
0000050 |
| 397 |
", |
| 398 |
); |
| 399 |
new_ucmd!() |
| 400 |
.arg("--endian=little") |
| 401 |
.arg("-F") |
| 402 |
.run_piped_stdin(&input[..]) |
| 403 |
.success() |
| 404 |
.stdout_only(expected_output); |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn test_multibyte() { |
| 409 |
let input = "’‐ˆ‘˜語🙂✅🐶𝛑Universität Tübingen \u{1B000}"; // spell-checker:disable-line |
| 410 |
new_ucmd!() |
| 411 |
.args(&["-t", "c"]) |
| 412 |
.run_piped_stdin(input.as_bytes()) |
| 413 |
.success() |
| 414 |
.stdout_only(unindent( |
| 415 |
r" |
| 416 |
0000000 342 200 231 342 200 220 313 206 342 200 230 313 234 350 252 236 |
| 417 |
0000020 360 237 231 202 342 234 205 360 237 220 266 360 235 233 221 U |
| 418 |
0000040 n i v e r s i t 303 244 t T 303 274 b |
| 419 |
0000060 i n g e n 360 233 200 200 |
| 420 |
0000072 |
| 421 |
", |
| 422 |
)); |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
fn test_width() { |
| 427 |
let input: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 428 |
let expected_output = unindent( |
| 429 |
" |
| 430 |
0000000 000000 000000 |
| 431 |
0000004 000000 000000 |
| 432 |
0000010 |
| 433 |
", |
| 434 |
); |
| 435 |
|
| 436 |
new_ucmd!() |
| 437 |
.arg("-w4") |
| 438 |
.arg("-v") |
| 439 |
.run_piped_stdin(&input[..]) |
| 440 |
.success() |
| 441 |
.stdout_only(expected_output); |
| 442 |
} |
| 443 |
|
| 444 |
#[test] |
| 445 |
fn test_invalid_width() { |
| 446 |
let input: [u8; 4] = [0x00, 0x00, 0x00, 0x00]; |
| 447 |
let expected_output = unindent( |
| 448 |
" |
| 449 |
0000000 000000 |
| 450 |
0000002 000000 |
| 451 |
0000004 |
| 452 |
", |
| 453 |
); |
| 454 |
|
| 455 |
new_ucmd!() |
| 456 |
.arg("-w5") |
| 457 |
.arg("-v") |
| 458 |
.run_piped_stdin(&input[..]) |
| 459 |
.success() |
| 460 |
.stderr_is_bytes("od: warning: invalid width 5; using 2 instead\n".as_bytes()) |
| 461 |
.stdout_is(expected_output); |
| 462 |
} |
| 463 |
|
| 464 |
#[test] |
| 465 |
fn test_zero_width() { |
| 466 |
new_ucmd!() |
| 467 |
.arg("-w0") |
| 468 |
.arg("-An") |
| 469 |
.fails_with_code(1) |
| 470 |
.stderr_only("od: invalid -w argument '0'\n"); |
| 471 |
} |
| 472 |
|
| 473 |
#[test] |
| 474 |
fn test_negative_width_argument() { |
| 475 |
new_ucmd!() |
| 476 |
.arg("-w-1") |
| 477 |
.arg("-An") |
| 478 |
.fails_with_code(1) |
| 479 |
.stderr_only("od: invalid -w argument '-1'\n"); |
| 480 |
} |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn test_non_numeric_width_argument() { |
| 484 |
new_ucmd!() |
| 485 |
.arg("-ww") |
| 486 |
.arg("-An") |
| 487 |
.fails_with_code(1) |
| 488 |
.stderr_only("od: invalid -w argument 'w'\n"); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn test_width_without_value() { |
| 493 |
let input: [u8; 40] = [0; 40]; |
| 494 |
let expected_output = unindent(" |
| 495 |
0000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 |
| 496 |
0000040 000000 000000 000000 000000 |
| 497 |
0000050 |
| 498 |
"); |
| 499 |
|
| 500 |
new_ucmd!() |
| 501 |
.arg("-w") |
| 502 |
.run_piped_stdin(&input[..]) |
| 503 |
.success() |
| 504 |
.stdout_only(expected_output); |
| 505 |
} |
| 506 |
|
| 507 |
#[test] |
| 508 |
fn test_very_wide_ascii_output() { |
| 509 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 510 |
at.write("data-a", "x"); |
| 511 |
ucmd.arg("-a") |
| 512 |
.arg("-w65537") |
| 513 |
.arg("-An") |
| 514 |
.arg("data-a") |
| 515 |
.succeeds() |
| 516 |
.stdout_only(" x\n"); |
| 517 |
} |
| 518 |
|
| 519 |
#[test] |
| 520 |
fn test_very_wide_char_output() { |
| 521 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 522 |
at.write("data-c", "x"); |
| 523 |
ucmd.arg("-c") |
| 524 |
.arg("-w65537") |
| 525 |
.arg("-An") |
| 526 |
.arg("data-c") |
| 527 |
.succeeds() |
| 528 |
.stdout_only(" x\n"); |
| 529 |
} |
| 530 |
|
| 531 |
#[test] |
| 532 |
fn test_very_wide_hex_byte_output() { |
| 533 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 534 |
at.write_bytes("data-x", &[0x42]); |
| 535 |
ucmd.arg("-tx1") |
| 536 |
.arg("-w65537") |
| 537 |
.arg("-An") |
| 538 |
.arg("data-x") |
| 539 |
.succeeds() |
| 540 |
.stdout_only(" 42\n"); |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn test_very_wide_hex_byte_ascii_dump() { |
| 545 |
const WIDTH: usize = 100_000; |
| 546 |
let expected = format!(" 41{} >A<\n", " ".repeat(WIDTH * 3 - 3)); |
| 547 |
|
| 548 |
new_ucmd!() |
| 549 |
.arg("-An") |
| 550 |
.arg(format!("-w{WIDTH}")) |
| 551 |
.arg("-tx1z") |
| 552 |
.pipe_in("A") |
| 553 |
.succeeds() |
| 554 |
.stdout_only(expected); |
| 555 |
} |
| 556 |
|
| 557 |
#[test] |
| 558 |
fn test_suppress_duplicates() { |
| 559 |
let input: [u8; 41] = [ |
| 560 |
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, |
| 561 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 562 |
]; |
| 563 |
let expected_output = unindent( |
| 564 |
" |
| 565 |
0000000 00000000000 |
| 566 |
0000 0000 |
| 567 |
* |
| 568 |
0000020 00000000001 |
| 569 |
0001 0000 |
| 570 |
0000024 00000000000 |
| 571 |
0000 0000 |
| 572 |
* |
| 573 |
0000050 00000000000 |
| 574 |
0000 |
| 575 |
0000051 |
| 576 |
", |
| 577 |
); |
| 578 |
|
| 579 |
new_ucmd!() |
| 580 |
.arg("-w4") |
| 581 |
.arg("-O") |
| 582 |
.arg("-x") |
| 583 |
.run_piped_stdin(&input[..]) |
| 584 |
.success() |
| 585 |
.stdout_only(expected_output); |
| 586 |
} |
| 587 |
|
| 588 |
#[test] |
| 589 |
fn test_big_endian() { |
| 590 |
let input: [u8; 8] = [0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; // 0xc000000000000000 -2 |
| 591 |
|
| 592 |
let expected_output = unindent( |
| 593 |
" |
| 594 |
0000000 -2 |
| 595 |
-2 0 |
| 596 |
c0000000 00000000 |
| 597 |
c000 0000 0000 0000 |
| 598 |
0000010 |
| 599 |
", |
| 600 |
); |
| 601 |
|
| 602 |
new_ucmd!() |
| 603 |
.arg("--endian=big") |
| 604 |
.arg("-F") |
| 605 |
.arg("-f") |
| 606 |
.arg("-X") |
| 607 |
.arg("-x") |
| 608 |
.run_piped_stdin(&input[..]) |
| 609 |
.success() |
| 610 |
.stdout_only(&expected_output); |
| 611 |
new_ucmd!() |
| 612 |
.arg("--endian=b") |
| 613 |
.arg("-F") |
| 614 |
.arg("-f") |
| 615 |
.arg("-X") |
| 616 |
.arg("-x") |
| 617 |
.run_piped_stdin(&input[..]) |
| 618 |
.success() |
| 619 |
.stdout_only(expected_output); |
| 620 |
} |
| 621 |
|
| 622 |
#[test] |
| 623 |
#[allow(non_snake_case)] |
| 624 |
fn test_alignment_Xxa() { |
| 625 |
let input: [u8; 8] = [0x0A, 0x0D, 0x65, 0x66, 0x67, 0x00, 0x9e, 0x9f]; |
| 626 |
|
| 627 |
let expected_output = unindent( |
| 628 |
" |
| 629 |
0000000 66650d0a 9f9e0067 |
| 630 |
0d0a 6665 0067 9f9e |
| 631 |
nl cr e f g nul rs us |
| 632 |
0000010 |
| 633 |
", |
| 634 |
); |
| 635 |
|
| 636 |
// in this case the width of the -a (8-bit) determines the alignment for the other fields |
| 637 |
new_ucmd!() |
| 638 |
.arg("--endian=little") |
| 639 |
.arg("-X") |
| 640 |
.arg("-x") |
| 641 |
.arg("-a") |
| 642 |
.run_piped_stdin(&input[..]) |
| 643 |
.success() |
| 644 |
.stdout_only(expected_output); |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
#[allow(non_snake_case)] |
| 649 |
fn test_alignment_Fx() { |
| 650 |
let input: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0]; // 0xc000000000000000 -2 |
| 651 |
|
| 652 |
let expected_output = unindent( |
| 653 |
" |
| 654 |
0000000 -2 |
| 655 |
0000 0000 0000 c000 |
| 656 |
0000010 |
| 657 |
", |
| 658 |
); |
| 659 |
|
| 660 |
// in this case the width of the -F (64-bit) determines the alignment for the other field |
| 661 |
new_ucmd!() |
| 662 |
.arg("--endian=little") |
| 663 |
.arg("-F") |
| 664 |
.arg("-x") |
| 665 |
.run_piped_stdin(&input[..]) |
| 666 |
.success() |
| 667 |
.stdout_only(expected_output); |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
fn test_max_uint() { |
| 672 |
let input = [0xFFu8; 8]; |
| 673 |
let expected_output = unindent( |
| 674 |
" |
| 675 |
0000000 1777777777777777777777 |
| 676 |
37777777777 37777777777 |
| 677 |
177777 177777 177777 177777 |
| 678 |
377 377 377 377 377 377 377 377 |
| 679 |
18446744073709551615 |
| 680 |
4294967295 4294967295 |
| 681 |
65535 65535 65535 65535 |
| 682 |
255 255 255 255 255 255 255 255 |
| 683 |
0000010 |
| 684 |
", |
| 685 |
); |
| 686 |
|
| 687 |
new_ucmd!() |
| 688 |
.arg("--format=o8") |
| 689 |
.arg("-Oobtu8") // spell-checker:disable-line |
| 690 |
.arg("-Dd") |
| 691 |
.arg("--format=u1") |
| 692 |
.run_piped_stdin(&input[..]) |
| 693 |
.success() |
| 694 |
.stdout_only(expected_output); |
| 695 |
} |
| 696 |
|
| 697 |
#[test] |
| 698 |
fn test_hex_offset() { |
| 699 |
let input = [0u8; 0x1F]; |
| 700 |
let expected_output = unindent( |
| 701 |
" |
| 702 |
000000 00000000 00000000 00000000 00000000 |
| 703 |
00000000 00000000 00000000 00000000 |
| 704 |
000010 00000000 00000000 00000000 00000000 |
| 705 |
00000000 00000000 00000000 00000000 |
| 706 |
00001f |
| 707 |
", |
| 708 |
); |
| 709 |
|
| 710 |
new_ucmd!() |
| 711 |
.arg("-Ax") |
| 712 |
.arg("-X") |
| 713 |
.arg("-X") |
| 714 |
.run_piped_stdin(&input[..]) |
| 715 |
.success() |
| 716 |
.stdout_only(expected_output); |
| 717 |
} |
| 718 |
|
| 719 |
#[test] |
| 720 |
fn test_dec_offset() { |
| 721 |
let input = [0u8; 19]; |
| 722 |
let expected_output = unindent( |
| 723 |
" |
| 724 |
0000000 00000000 00000000 00000000 00000000 |
| 725 |
00000000 00000000 00000000 00000000 |
| 726 |
0000016 00000000 |
| 727 |
00000000 |
| 728 |
0000019 |
| 729 |
", |
| 730 |
); |
| 731 |
|
| 732 |
new_ucmd!() |
| 733 |
.arg("-Ad") |
| 734 |
.arg("-X") |
| 735 |
.arg("-X") |
| 736 |
.run_piped_stdin(&input[..]) |
| 737 |
.success() |
| 738 |
.stdout_only(expected_output); |
| 739 |
} |
| 740 |
|
| 741 |
#[test] |
| 742 |
fn test_no_offset() { |
| 743 |
const LINE: &str = " 00000000 00000000 00000000 00000000\n"; |
| 744 |
let input = [0u8; 31]; |
| 745 |
let expected_output = [LINE, LINE, LINE, LINE].join(""); |
| 746 |
|
| 747 |
new_ucmd!() |
| 748 |
.arg("-An") |
| 749 |
.arg("-X") |
| 750 |
.arg("-X") |
| 751 |
.run_piped_stdin(&input[..]) |
| 752 |
.success() |
| 753 |
.stdout_only(expected_output); |
| 754 |
} |
| 755 |
|
| 756 |
#[test] |
| 757 |
fn test_invalid_offset() { |
| 758 |
new_ucmd!().arg("-Ab").fails(); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn test_invalid_traditional_offsets_are_filenames() { |
| 763 |
let cases = [("++0", "++0"), ("+-0", "+-0"), ("+ 0", "'+ 0'")]; |
| 764 |
|
| 765 |
for (input, display) in cases { |
| 766 |
new_ucmd!() |
| 767 |
.arg(input) |
| 768 |
.fails_with_code(1) |
| 769 |
.stderr_only(format!("od: {display}: No such file or directory\n")); |
| 770 |
} |
| 771 |
|
| 772 |
new_ucmd!() |
| 773 |
.arg("--") |
| 774 |
.arg("-0") |
| 775 |
.fails_with_code(1) |
| 776 |
.stderr_only("od: -0: No such file or directory\n"); |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn test_traditional_offset_overflow_diagnosed() { |
| 781 |
// The ERANGE message differs across platforms (e.g. "Result too large" |
| 782 |
// on glibc vs "Result not representable" on wasi-libc), so when testing |
| 783 |
// via a WASM runner we just check that stderr mentions the input value. |
| 784 |
let wasm = std::env::var("UUTESTS_WASM_RUNNER").is_ok(); |
| 785 |
let erange = erange_message(); |
| 786 |
let long_octal = "7".repeat(255); |
| 787 |
let long_decimal = format!("{}.", "9".repeat(254)); |
| 788 |
let long_hex = format!("0x{}", "f".repeat(253)); |
| 789 |
|
| 790 |
let cmd = new_ucmd!() |
| 791 |
.arg("-") |
| 792 |
.arg(&long_octal) |
| 793 |
.pipe_in(Vec::<u8>::new()) |
| 794 |
.fails_with_code(1); |
| 795 |
if wasm { |
| 796 |
cmd.stderr_contains(format!("od: {long_octal}:")); |
| 797 |
} else { |
| 798 |
cmd.stderr_only(format!("od: {long_octal}: {erange}\n")); |
| 799 |
} |
| 800 |
|
| 801 |
let cmd = new_ucmd!() |
| 802 |
.arg("-") |
| 803 |
.arg(&long_decimal) |
| 804 |
.pipe_in(Vec::<u8>::new()) |
| 805 |
.fails_with_code(1); |
| 806 |
if wasm { |
| 807 |
cmd.stderr_contains(format!("od: {long_decimal}:")); |
| 808 |
} else { |
| 809 |
cmd.stderr_only(format!("od: {long_decimal}: {erange}\n")); |
| 810 |
} |
| 811 |
|
| 812 |
let cmd = new_ucmd!() |
| 813 |
.arg("-") |
| 814 |
.arg(&long_hex) |
| 815 |
.pipe_in(Vec::<u8>::new()) |
| 816 |
.fails_with_code(1); |
| 817 |
if wasm { |
| 818 |
cmd.stderr_contains(format!("od: {long_hex}:")); |
| 819 |
} else { |
| 820 |
cmd.stderr_only(format!("od: {long_hex}: {erange}\n")); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
#[test] |
| 825 |
fn test_empty_offset() { |
| 826 |
new_ucmd!() |
| 827 |
.arg("-A") |
| 828 |
.arg("") |
| 829 |
.fails() |
| 830 |
.stderr_only("od: Radix cannot be empty, and must be one of [o, d, x, n]\n"); |
| 831 |
} |
| 832 |
|
| 833 |
#[test] |
| 834 |
fn test_offset_compatibility() { |
| 835 |
let input = [0u8; 4]; |
| 836 |
let expected_output = " 000000 000000\n"; |
| 837 |
|
| 838 |
new_ucmd!() |
| 839 |
.arg("-Anone") |
| 840 |
.pipe_in(input) |
| 841 |
.succeeds() |
| 842 |
.stdout_only(expected_output); |
| 843 |
} |
| 844 |
|
| 845 |
#[test] |
| 846 |
fn test_skip_bytes() { |
| 847 |
let input = "abcdefghijklmnopq"; |
| 848 |
new_ucmd!() |
| 849 |
.arg("-c") |
| 850 |
.arg("--skip-bytes=5") |
| 851 |
.run_piped_stdin(input.as_bytes()) |
| 852 |
.success() |
| 853 |
.stdout_only(unindent( |
| 854 |
" |
| 855 |
0000005 f g h i j k l m n o p q |
| 856 |
0000021 |
| 857 |
", |
| 858 |
)); |
| 859 |
} |
| 860 |
|
| 861 |
#[test] |
| 862 |
fn test_skip_bytes_hex() { |
| 863 |
let input = "abcdefghijklmnopq"; |
| 864 |
new_ucmd!() |
| 865 |
.arg("-c") |
| 866 |
.arg("--skip-bytes=0xB") |
| 867 |
.run_piped_stdin(input.as_bytes()) |
| 868 |
.success() |
| 869 |
.stdout_only(unindent( |
| 870 |
" |
| 871 |
0000013 l m n o p q |
| 872 |
0000021 |
| 873 |
", |
| 874 |
)); |
| 875 |
new_ucmd!() |
| 876 |
.arg("-c") |
| 877 |
.arg("--skip-bytes=0xE") |
| 878 |
.run_piped_stdin(input.as_bytes()) |
| 879 |
.success() |
| 880 |
.stdout_only(unindent( |
| 881 |
" |
| 882 |
0000016 o p q |
| 883 |
0000021 |
| 884 |
", |
| 885 |
)); |
| 886 |
} |
| 887 |
|
| 888 |
#[test] |
| 889 |
fn test_skip_bytes_consumes_single_input() { |
| 890 |
run_skip_across_inputs(&[("g", "a")], 1, ""); |
| 891 |
} |
| 892 |
|
| 893 |
#[test] |
| 894 |
fn test_skip_bytes_consumes_two_inputs() { |
| 895 |
run_skip_across_inputs(&[("g", "a"), ("h", "b")], 2, ""); |
| 896 |
} |
| 897 |
|
| 898 |
#[test] |
| 899 |
fn test_skip_bytes_consumes_three_inputs() { |
| 900 |
run_skip_across_inputs(&[("g", "a"), ("h", "b"), ("i", "c")], 3, ""); |
| 901 |
} |
| 902 |
|
| 903 |
#[test] |
| 904 |
fn test_skip_bytes_prints_after_consuming_multiple_inputs() { |
| 905 |
run_skip_across_inputs( |
| 906 |
&[("g", "a"), ("h", "b"), ("i", "c"), ("j", "d")], |
| 907 |
3, |
| 908 |
" d\n", |
| 909 |
); |
| 910 |
} |
| 911 |
|
| 912 |
#[cfg(target_os = "linux")] |
| 913 |
#[test] |
| 914 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/proc) not visible")] |
| 915 |
fn test_skip_bytes_proc_file_without_seeking() { |
| 916 |
let proc_path = Path::new("/proc/version"); |
| 917 |
if !proc_path.exists() { |
| 918 |
return; |
| 919 |
} |
| 920 |
|
| 921 |
let Ok(contents) = std::fs::read(proc_path) else { |
| 922 |
return; |
| 923 |
}; |
| 924 |
|
| 925 |
if contents.is_empty() { |
| 926 |
return; |
| 927 |
} |
| 928 |
|
| 929 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 930 |
at.write("after", "e"); |
| 931 |
|
| 932 |
ucmd.arg("-An") |
| 933 |
.arg("-c") |
| 934 |
.arg("-j") |
| 935 |
.arg(contents.len().to_string()) |
| 936 |
.arg(proc_path) |
| 937 |
.arg("after") |
| 938 |
.succeeds() |
| 939 |
.stdout_only(" e\n"); |
| 940 |
} |
| 941 |
|
| 942 |
#[test] |
| 943 |
fn test_skip_bytes_error() { |
| 944 |
let input = "12345"; |
| 945 |
new_ucmd!() |
| 946 |
.arg("--skip-bytes=10") |
| 947 |
.run_piped_stdin(input.as_bytes()) |
| 948 |
.failure(); |
| 949 |
} |
| 950 |
|
| 951 |
// The skip is measured over all the inputs joined together, and the message |
| 952 |
// says so, matching GNU od. |
| 953 |
#[test] |
| 954 |
fn test_skip_bytes_past_end_message() { |
| 955 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 956 |
at.write("a", "abc"); |
| 957 |
at.write("b", "de"); |
| 958 |
ucmd.arg("-j6") |
| 959 |
.arg("a") |
| 960 |
.arg("b") |
| 961 |
.fails_with_code(1) |
| 962 |
.no_stdout() |
| 963 |
.stderr_only("od: cannot skip past end of combined input\n"); |
| 964 |
} |
| 965 |
|
| 966 |
// A seekable special file such as /dev/null can be skipped past its (empty) |
| 967 |
// end without error, matching GNU od. |
| 968 |
#[cfg(unix)] |
| 969 |
#[test] |
| 970 |
#[cfg_attr( |
| 971 |
wasi_runner, |
| 972 |
ignore = "WASI sandbox: /dev/null is not a seekable device" |
| 973 |
)] |
| 974 |
fn test_skip_bytes_past_end_of_seekable_device() { |
| 975 |
new_ucmd!() |
| 976 |
.arg("-j1") |
| 977 |
.arg("/dev/null") |
| 978 |
.succeeds() |
| 979 |
.stdout_only("0000001\n"); |
| 980 |
} |
| 981 |
|
| 982 |
// Skipping past the end of a regular file is an error and must not print a |
| 983 |
// trailing offset line, matching GNU od. |
| 984 |
#[test] |
| 985 |
fn test_skip_bytes_past_end_no_offset() { |
| 986 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 987 |
at.write("f", "hello"); |
| 988 |
ucmd.arg("-j10").arg("f").fails().no_stdout(); |
| 989 |
} |
| 990 |
|
| 991 |
#[test] |
| 992 |
#[cfg_attr( |
| 993 |
wasi_runner, |
| 994 |
ignore = "WASI: stdin file position not preserved through wasmtime" |
| 995 |
)] |
| 996 |
fn test_read_bytes() { |
| 997 |
let scene = TestScenario::new(util_name!()); |
| 998 |
let fixtures = &scene.fixtures; |
| 999 |
|
| 1000 |
let input = "abcdefghijklmnopqrstuvwxyz\n12345678"; |
| 1001 |
|
| 1002 |
fixtures.write("f1", input); |
| 1003 |
let file = fixtures.open("f1"); |
| 1004 |
#[cfg(unix)] |
| 1005 |
let mut file_shadow = file.try_clone().unwrap(); |
| 1006 |
|
| 1007 |
scene |
| 1008 |
.ucmd() |
| 1009 |
.arg("--endian=little") |
| 1010 |
.arg("--read-bytes=27") |
| 1011 |
.set_stdin(file) |
| 1012 |
.succeeds() |
| 1013 |
.stdout_only(unindent(ALPHA_OUT)); |
| 1014 |
|
| 1015 |
// On unix platforms, confirm that only 27 bytes and strictly no more were read from stdin. |
| 1016 |
// Leaving stdin in the correct state is required for GNU compatibility. |
| 1017 |
#[cfg(unix)] |
| 1018 |
{ |
| 1019 |
// skip(27) to skip the 27 bytes that should have been consumed with the |
| 1020 |
// --read-bytes flag. |
| 1021 |
let expected_bytes_remaining_in_stdin: Vec<_> = input.bytes().skip(27).collect(); |
| 1022 |
let mut bytes_remaining_in_stdin = vec![]; |
| 1023 |
assert_eq!( |
| 1024 |
file_shadow |
| 1025 |
.read_to_end(&mut bytes_remaining_in_stdin) |
| 1026 |
.unwrap(), |
| 1027 |
expected_bytes_remaining_in_stdin.len() |
| 1028 |
); |
| 1029 |
assert_eq!(expected_bytes_remaining_in_stdin, bytes_remaining_in_stdin); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
#[test] |
| 1034 |
fn test_ascii_dump() { |
| 1035 |
let input: [u8; 22] = [ |
| 1036 |
0x00, 0x01, 0x0a, 0x0d, 0x10, 0x1f, 0x20, 0x61, 0x62, 0x63, 0x7d, 0x7e, 0x7f, 0x80, 0x90, |
| 1037 |
0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff, |
| 1038 |
]; |
| 1039 |
new_ucmd!() |
| 1040 |
.arg("-tx1zacz") // spell-checker:disable-line |
| 1041 |
.run_piped_stdin(&input[..]) |
| 1042 |
.success() |
| 1043 |
.stdout_only(unindent( |
| 1044 |
r" |
| 1045 |
0000000 00 01 0a 0d 10 1f 20 61 62 63 7d 7e 7f 80 90 a0 >...... abc}~....< |
| 1046 |
nul soh nl cr dle us sp a b c } ~ del nul dle sp |
| 1047 |
\0 001 \n \r 020 037 a b c } ~ 177 200 220 240 >...... abc}~....< |
| 1048 |
0000020 b0 c0 d0 e0 f0 ff >......< |
| 1049 |
0 @ P ` p del |
| 1050 |
260 300 320 340 360 377 >......< |
| 1051 |
0000026 |
| 1052 |
", |
| 1053 |
)); |
| 1054 |
} |
| 1055 |
|
| 1056 |
#[test] |
| 1057 |
fn test_filename_parsing() { |
| 1058 |
// files "a" and "x" both exists, but are no filenames in the command line below |
| 1059 |
// "-f" must be treated as a filename, it contains the text: minus lowercase f |
| 1060 |
// so "-f" should not be interpreted as a formatting option. |
| 1061 |
new_ucmd!() |
| 1062 |
.arg("--format") |
| 1063 |
.arg("a") |
| 1064 |
.arg("-A") |
| 1065 |
.arg("x") |
| 1066 |
.arg("--") |
| 1067 |
.arg("-f") |
| 1068 |
.succeeds() |
| 1069 |
.stdout_only(unindent( |
| 1070 |
" |
| 1071 |
000000 m i n u s sp l o w e r c a s e sp |
| 1072 |
000010 f nl |
| 1073 |
000012 |
| 1074 |
", |
| 1075 |
)); |
| 1076 |
} |
| 1077 |
|
| 1078 |
#[test] |
| 1079 |
fn test_stdin_offset() { |
| 1080 |
let input = "abcdefghijklmnopq"; |
| 1081 |
new_ucmd!() |
| 1082 |
.arg("-c") |
| 1083 |
.arg("+5") |
| 1084 |
.run_piped_stdin(input.as_bytes()) |
| 1085 |
.success() |
| 1086 |
.stdout_only(unindent( |
| 1087 |
" |
| 1088 |
0000005 f g h i j k l m n o p q |
| 1089 |
0000021 |
| 1090 |
", |
| 1091 |
)); |
| 1092 |
} |
| 1093 |
|
| 1094 |
#[test] |
| 1095 |
fn test_traditional_decimal_dot_offset() { |
| 1096 |
new_ucmd!() |
| 1097 |
.arg("+1.") |
| 1098 |
.pipe_in("a") |
| 1099 |
.succeeds() |
| 1100 |
.stdout_only("0000001\n"); |
| 1101 |
} |
| 1102 |
|
| 1103 |
#[test] |
| 1104 |
fn test_traditional_dot_block_offset() { |
| 1105 |
new_ucmd!() |
| 1106 |
.arg("+1.b") |
| 1107 |
.pipe_in(vec![b'a'; 512]) |
| 1108 |
.succeeds() |
| 1109 |
.stdout_only("0001000\n"); |
| 1110 |
} |
| 1111 |
|
| 1112 |
#[test] |
| 1113 |
fn test_file_offset() { |
| 1114 |
new_ucmd!() |
| 1115 |
.arg("-c") |
| 1116 |
.arg("--") |
| 1117 |
.arg("-f") |
| 1118 |
.arg("10") |
| 1119 |
.succeeds() |
| 1120 |
.stdout_only(unindent( |
| 1121 |
r" |
| 1122 |
0000010 w e r c a s e f \n |
| 1123 |
0000022 |
| 1124 |
", |
| 1125 |
)); |
| 1126 |
} |
| 1127 |
|
| 1128 |
#[test] |
| 1129 |
fn test_traditional() { |
| 1130 |
// note gnu od does not align both lines |
| 1131 |
let input = "abcdefghijklmnopq"; |
| 1132 |
new_ucmd!() |
| 1133 |
.arg("--traditional") |
| 1134 |
.arg("-a") |
| 1135 |
.arg("-c") |
| 1136 |
.arg("-") |
| 1137 |
.arg("10") |
| 1138 |
.arg("0") |
| 1139 |
.run_piped_stdin(input.as_bytes()) |
| 1140 |
.success() |
| 1141 |
.stdout_only(unindent( |
| 1142 |
r" |
| 1143 |
0000010 (0000000) i j k l m n o p q |
| 1144 |
i j k l m n o p q |
| 1145 |
0000021 (0000011) |
| 1146 |
", |
| 1147 |
)); |
| 1148 |
} |
| 1149 |
|
| 1150 |
#[test] |
| 1151 |
fn test_traditional_with_skip_bytes_override() { |
| 1152 |
// --skip-bytes is ignored in this case |
| 1153 |
let input = "abcdefghijklmnop"; |
| 1154 |
new_ucmd!() |
| 1155 |
.arg("--traditional") |
| 1156 |
.arg("--skip-bytes=10") |
| 1157 |
.arg("-c") |
| 1158 |
.arg("0") |
| 1159 |
.run_piped_stdin(input.as_bytes()) |
| 1160 |
.success() |
| 1161 |
.stdout_only(unindent( |
| 1162 |
r" |
| 1163 |
0000000 a b c d e f g h i j k l m n o p |
| 1164 |
0000020 |
| 1165 |
", |
| 1166 |
)); |
| 1167 |
} |
| 1168 |
|
| 1169 |
#[test] |
| 1170 |
fn test_traditional_with_skip_bytes_non_override() { |
| 1171 |
// no offset specified in the traditional way, so --skip-bytes is used |
| 1172 |
let input = "abcdefghijklmnop"; |
| 1173 |
new_ucmd!() |
| 1174 |
.arg("--traditional") |
| 1175 |
.arg("--skip-bytes=10") |
| 1176 |
.arg("-c") |
| 1177 |
.run_piped_stdin(input.as_bytes()) |
| 1178 |
.success() |
| 1179 |
.stdout_only(unindent( |
| 1180 |
r" |
| 1181 |
0000012 k l m n o p |
| 1182 |
0000020 |
| 1183 |
", |
| 1184 |
)); |
| 1185 |
} |
| 1186 |
|
| 1187 |
#[test] |
| 1188 |
fn test_traditional_error() { |
| 1189 |
// file "0" exists - don't fail on that, but --traditional only accepts a single input |
| 1190 |
new_ucmd!() |
| 1191 |
.arg("--traditional") |
| 1192 |
.arg("0") |
| 1193 |
.arg("0") |
| 1194 |
.arg("0") |
| 1195 |
.arg("0") |
| 1196 |
.fails(); |
| 1197 |
} |
| 1198 |
|
| 1199 |
#[test] |
| 1200 |
fn test_traditional_only_label() { |
| 1201 |
let input = "abcdefghijklmnopqrstuvwxyz"; |
| 1202 |
new_ucmd!() |
| 1203 |
.arg("-An") |
| 1204 |
.arg("--traditional") |
| 1205 |
.arg("-a") |
| 1206 |
.arg("-c") |
| 1207 |
.arg("-") |
| 1208 |
.arg("10") |
| 1209 |
.arg("0x10") |
| 1210 |
.run_piped_stdin(input.as_bytes()) |
| 1211 |
.success() |
| 1212 |
.stdout_only(unindent( |
| 1213 |
r" |
| 1214 |
(0000020) i j k l m n o p q r s t u v w x |
| 1215 |
i j k l m n o p q r s t u v w x |
| 1216 |
(0000040) y z |
| 1217 |
y z |
| 1218 |
(0000042) |
| 1219 |
", |
| 1220 |
)); |
| 1221 |
} |
| 1222 |
|
| 1223 |
#[test] |
| 1224 |
fn test_od_invalid_bytes() { |
| 1225 |
const INVALID_SIZE: &str = "x"; |
| 1226 |
const INVALID_SUFFIX: &str = "1fb4t"; |
| 1227 |
const BIG_SIZE: &str = "1Y"; |
| 1228 |
|
| 1229 |
// NOTE: |
| 1230 |
// GNU's od (8.32) with option '--width' does not accept 'Y' as valid suffix. |
| 1231 |
// According to the man page it should be valid in the same way it is valid for |
| 1232 |
// '--read-bytes' and '--skip-bytes'. |
| 1233 |
|
| 1234 |
let options = [ |
| 1235 |
"--read-bytes", |
| 1236 |
"--skip-bytes", |
| 1237 |
"--width", |
| 1238 |
// "--strings", // TODO: consider testing here once '--strings' is implemented |
| 1239 |
]; |
| 1240 |
for option in &options { |
| 1241 |
new_ucmd!() |
| 1242 |
.arg(format!("{option}={INVALID_SIZE}")) |
| 1243 |
.arg("file") |
| 1244 |
.fails_with_code(1) |
| 1245 |
.stderr_only(format!("od: invalid {option} argument '{INVALID_SIZE}'\n")); |
| 1246 |
|
| 1247 |
new_ucmd!() |
| 1248 |
.arg(format!("{option}={INVALID_SUFFIX}")) |
| 1249 |
.arg("file") |
| 1250 |
.fails_with_code(1) |
| 1251 |
.stderr_only(format!( |
| 1252 |
"od: invalid suffix in {option} argument '{INVALID_SUFFIX}'\n" |
| 1253 |
)); |
| 1254 |
|
| 1255 |
new_ucmd!() |
| 1256 |
.arg(format!("{option}={BIG_SIZE}")) |
| 1257 |
.arg("file") |
| 1258 |
.fails_with_code(1) |
| 1259 |
.stderr_only(format!("od: {option} argument '{BIG_SIZE}' too large\n")); |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
#[test] |
| 1264 |
fn test_od_options_after_filename() { |
| 1265 |
let file = "test"; |
| 1266 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1267 |
let input: [u8; 4] = [0x68, 0x1c, 0xbb, 0xfd]; |
| 1268 |
at.write_bytes(file, &input); |
| 1269 |
|
| 1270 |
ucmd.arg(file) |
| 1271 |
.arg("-v") |
| 1272 |
.arg("-An") |
| 1273 |
.arg("-t") |
| 1274 |
.arg("x2") |
| 1275 |
.succeeds() |
| 1276 |
.stdout_only(" 1c68 fdbb\n"); |
| 1277 |
} |
| 1278 |
|
| 1279 |
#[test] |
| 1280 |
fn test_od_strings_option() { |
| 1281 |
// Test -S option: output strings of at least N graphic chars |
| 1282 |
|
| 1283 |
// Test -S0: output all null-terminated strings regardless of length |
| 1284 |
new_ucmd!() |
| 1285 |
.arg("-S0") |
| 1286 |
.pipe_in(b"hello\x00world\x00") |
| 1287 |
.succeeds() |
| 1288 |
.stdout_is("0000000 hello\n0000006 world\n"); |
| 1289 |
|
| 1290 |
// Test -S0 with single character strings |
| 1291 |
new_ucmd!() |
| 1292 |
.arg("-S0") |
| 1293 |
.pipe_in(b"a\x00b\x00cd\x00") |
| 1294 |
.succeeds() |
| 1295 |
.stdout_is("0000000 a\n0000002 b\n0000004 cd\n"); |
| 1296 |
|
| 1297 |
// Test with null-terminated strings |
| 1298 |
new_ucmd!() |
| 1299 |
.arg("-S3") |
| 1300 |
.pipe_in(b"\x01hello\x00world\x00ab\x00") |
| 1301 |
.succeeds() |
| 1302 |
.stdout_is("0000001 hello\n0000007 world\n"); |
| 1303 |
|
| 1304 |
// Test with -S10 to show only strings with minimum length |
| 1305 |
new_ucmd!() |
| 1306 |
.arg("-S10") |
| 1307 |
.pipe_in(b"\x01 \x00 \x00") |
| 1308 |
.succeeds() |
| 1309 |
.stdout_is("0000001 \n0000014 \n"); |
| 1310 |
|
| 1311 |
// Test with unterminated string at EOF (should not output) |
| 1312 |
new_ucmd!() |
| 1313 |
.arg("-S10") |
| 1314 |
.pipe_in(b" ") |
| 1315 |
.succeeds() |
| 1316 |
.no_output(); |
| 1317 |
|
| 1318 |
// Test with -N limit and pending string (should output even without null) |
| 1319 |
let expected = "0000000 \n"; |
| 1320 |
new_ucmd!() |
| 1321 |
.arg("-N2") |
| 1322 |
.arg("-S1") |
| 1323 |
.pipe_in(" ") |
| 1324 |
.succeeds() |
| 1325 |
.stdout_is(expected); |
| 1326 |
|
| 1327 |
// Test with -N limit and string too short |
| 1328 |
new_ucmd!() |
| 1329 |
.arg("-N11") |
| 1330 |
.arg("-S11") |
| 1331 |
.pipe_in(b" \x00") |
| 1332 |
.succeeds() |
| 1333 |
.no_output(); |
| 1334 |
|
| 1335 |
// Test with no address prefix (-An) |
| 1336 |
new_ucmd!() |
| 1337 |
.arg("-S3") |
| 1338 |
.arg("-An") |
| 1339 |
.pipe_in(b"hello\x00world\x00") |
| 1340 |
.succeeds() |
| 1341 |
.stdout_is("hello\nworld\n"); |
| 1342 |
} |
| 1343 |
|
| 1344 |
#[test] |
| 1345 |
fn test_od_eintr_handling() { |
| 1346 |
// Test that od properly handles EINTR (ErrorKind::Interrupted) during read operations |
| 1347 |
// This verifies the signal interruption retry logic in PartialReader implementation |
| 1348 |
// This verifies the signal interruption retry logic in PartialReader implementation |
| 1349 |
|
| 1350 |
let file = "test_eintr"; |
| 1351 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1352 |
|
| 1353 |
// Create test data |
| 1354 |
let test_data = [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0a]; // "Hello\n" |
| 1355 |
at.write_bytes(file, &test_data); |
| 1356 |
|
| 1357 |
// Test that od can handle interrupted reads during file processing |
| 1358 |
// The EINTR handling in PartialReader should retry and complete successfully |
| 1359 |
ucmd.arg(file) |
| 1360 |
.arg("-t") |
| 1361 |
.arg("c") |
| 1362 |
.succeeds() |
| 1363 |
.no_stderr() |
| 1364 |
.stdout_contains("H"); // Should contain 'H' from "Hello" |
| 1365 |
|
| 1366 |
// Test with skip and offset options to exercise different code paths |
| 1367 |
// Create a new command instance to avoid "already run this UCommand" error |
| 1368 |
let (at, mut ucmd2) = at_and_ucmd!(); |
| 1369 |
at.write_bytes(file, &test_data); |
| 1370 |
|
| 1371 |
ucmd2 |
| 1372 |
.arg(file) |
| 1373 |
.arg("-j") |
| 1374 |
.arg("1") |
| 1375 |
.arg("-N") |
| 1376 |
.arg("3") |
| 1377 |
.arg("-t") |
| 1378 |
.arg("c") |
| 1379 |
.succeeds() |
| 1380 |
.no_stderr() |
| 1381 |
.stdout_contains("e"); // Should contain 'e' from "ello" |
| 1382 |
} |
| 1383 |
|
| 1384 |
// Regression test: od should handle write errors to /dev/full without aborting. |
| 1385 |
#[test] |
| 1386 |
#[cfg(target_os = "linux")] |
| 1387 |
fn test_write_error_dev_full() { |
| 1388 |
use std::fs::File; |
| 1389 |
|
| 1390 |
let dev_full = File::create("/dev/full").expect("Failed to open /dev/full"); |
| 1391 |
|
| 1392 |
new_ucmd!() |
| 1393 |
.arg("-An") |
| 1394 |
.pipe_in("abcd") |
| 1395 |
.set_stdout(dev_full) |
| 1396 |
.fails() |
| 1397 |
.code_is(1) |
| 1398 |
.stderr_contains("No space left on device"); |
| 1399 |
} |
| 1400 |
|
| 1401 |
#[test] |
| 1402 |
fn test_hex_lowercase() { |
| 1403 |
let input = [0u8; 10]; |
| 1404 |
// Test verifies that the output hex byte offset is in lowercase |
| 1405 |
new_ucmd!() |
| 1406 |
.arg("-Ax") |
| 1407 |
.run_piped_stdin(input) |
| 1408 |
.success() |
| 1409 |
.no_stderr() |
| 1410 |
.stdout_only(unindent( |
| 1411 |
r" |
| 1412 |
000000 000000 000000 000000 000000 000000 |
| 1413 |
00000a |
| 1414 |
", |
| 1415 |
)); |
| 1416 |
} |
| 1417 |
|
| 1418 |
#[test] |
| 1419 |
#[cfg(not(windows))] |
| 1420 |
#[cfg_attr(wasi_runner, ignore)] |
| 1421 |
fn test_is_a_directory() { |
| 1422 |
let scene = TestScenario::new(util_name!()); |
| 1423 |
let fixtures = &scene.fixtures; |
| 1424 |
|
| 1425 |
fixtures.mkdir("a"); |
| 1426 |
|
| 1427 |
scene |
| 1428 |
.ucmd() |
| 1429 |
.args(&["a"]) |
| 1430 |
.fails_with_code(1) |
| 1431 |
.stderr_is("od: a: Is a directory\n"); |
| 1432 |
} |
| 1433 |
|
| 1434 |
// Regression: offset must be 4, not 3, and -N must be respected |
| 1435 |
#[test] |
| 1436 |
fn test_od_strings_with_n_flag() { |
| 1437 |
let input = b"foo\0bar\0"; |
| 1438 |
|
| 1439 |
new_ucmd!() |
| 1440 |
.args(&["--strings", "-N7"]) |
| 1441 |
.run_piped_stdin(&input[..]) |
| 1442 |
.success() |
| 1443 |
.stdout_only("0000000 foo\n0000004 bar\n"); |
| 1444 |
|
| 1445 |
new_ucmd!() |
| 1446 |
.args(&["--strings", "-N8"]) |
| 1447 |
.run_piped_stdin(&input[..]) |
| 1448 |
.success() |
| 1449 |
.stdout_only("0000000 foo\n0000004 bar\n"); |
| 1450 |
} |
| 1451 |
|
| 1452 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 1453 |
mod diagnostics { |
| 1454 |
use super::*; |
| 1455 |
|
| 1456 |
#[cfg(unix)] |
| 1457 |
#[test] |
| 1458 |
fn test_snippet_points_at_the_unknown_unit_of_read_bytes() { |
| 1459 |
let result = new_ucmd!() |
| 1460 |
.terminal_sim_stderr() |
| 1461 |
.args(&["-N", "3zz", "/dev/null"]) |
| 1462 |
.fails_with_code(1); |
| 1463 |
|
| 1464 |
// The number parsed; only the unit did not. The headline keeps the |
| 1465 |
// option spelled the way it was typed. |
| 1466 |
assert_eq!( |
| 1467 |
result.stderr_as_displayed(), |
| 1468 |
"\ |
| 1469 |
od: invalid suffix in -N argument '3zz' |
| 1470 |
╭─[ od:1:8 ] |
| 1471 |
│ |
| 1472 |
1 │ od -N 3zz /dev/null |
| 1473 |
│ ─┬ |
| 1474 |
│ ╰── not a known unit |
| 1475 |
│ |
| 1476 |
│ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000 |
| 1477 |
───╯" |
| 1478 |
); |
| 1479 |
} |
| 1480 |
|
| 1481 |
#[cfg(unix)] |
| 1482 |
#[test] |
| 1483 |
fn test_snippet_points_inside_a_width_value() { |
| 1484 |
let result = new_ucmd!() |
| 1485 |
.terminal_sim_stderr() |
| 1486 |
.args(&["--width=4qq", "/dev/null"]) |
| 1487 |
.fails_with_code(1); |
| 1488 |
let stderr = result.stderr_as_displayed(); |
| 1489 |
|
| 1490 |
assert!(stderr.contains("od:1:13"), "{stderr}"); |
| 1491 |
assert!(stderr.contains("not a known unit"), "{stderr}"); |
| 1492 |
} |
| 1493 |
|
| 1494 |
#[cfg(unix)] |
| 1495 |
#[test] |
| 1496 |
fn test_snippet_underlines_a_hexadecimal_offset_that_does_not_parse() { |
| 1497 |
let result = new_ucmd!() |
| 1498 |
.terminal_sim_stderr() |
| 1499 |
.args(&["-j", "0x1zz", "/dev/null"]) |
| 1500 |
.fails_with_code(1); |
| 1501 |
let stderr = result.stderr_as_displayed(); |
| 1502 |
|
| 1503 |
// Nothing usable was read, so the whole value is underlined. |
| 1504 |
assert!(stderr.contains("od:1:7"), "{stderr}"); |
| 1505 |
assert!(!stderr.contains("not a known unit"), "{stderr}"); |
| 1506 |
} |
| 1507 |
|
| 1508 |
#[test] |
| 1509 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 1510 |
new_ucmd!() |
| 1511 |
.args(&["-N", "3zz", "/dev/null"]) |
| 1512 |
.fails_with_code(1) |
| 1513 |
.stderr_is("od: invalid suffix in -N argument '3zz'\n"); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
#[test] |
| 1518 |
fn test_hyphen_leading_byte_count_is_reported_as_invalid() { |
| 1519 |
// GNU hands the argument after the option to that option even when it |
| 1520 |
// starts with a hyphen, so it is reported as an invalid argument rather |
| 1521 |
// than as an unknown option. |
| 1522 |
for opt in ["-N", "-j"] { |
| 1523 |
new_ucmd!() |
| 1524 |
.args(&[opt, "-1"]) |
| 1525 |
.pipe_in("") |
| 1526 |
.fails() |
| 1527 |
.stderr_contains(format!("invalid {opt} argument '-1'")); |
| 1528 |
} |
| 1529 |
} |