| 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 bsdutils toybox |
| 7 |
#[cfg(target_os = "linux")] |
| 8 |
use std::os::unix::ffi::OsStringExt; |
| 9 |
use uutests::at_and_ucmd; |
| 10 |
use uutests::new_ucmd; |
| 11 |
|
| 12 |
struct TestData<'b> { |
| 13 |
name: &'b str, |
| 14 |
args: &'b [&'b str], |
| 15 |
ins: &'b [&'b str], |
| 16 |
out: &'b str, |
| 17 |
} |
| 18 |
|
| 19 |
const EXAMPLE_DATA: &[TestData] = &[ |
| 20 |
// Ensure that paste properly handles files lacking a final newline. |
| 21 |
TestData { |
| 22 |
name: "no-nl-1", |
| 23 |
args: &[], |
| 24 |
ins: &["a", "b"], |
| 25 |
out: "a\tb\n", |
| 26 |
}, |
| 27 |
TestData { |
| 28 |
name: "no-nl-2", |
| 29 |
args: &[], |
| 30 |
ins: &["a\n", "b"], |
| 31 |
out: "a\tb\n", |
| 32 |
}, |
| 33 |
TestData { |
| 34 |
name: "no-nl-3", |
| 35 |
args: &[], |
| 36 |
ins: &["a", "b\n"], |
| 37 |
out: "a\tb\n", |
| 38 |
}, |
| 39 |
TestData { |
| 40 |
name: "no-nl-4", |
| 41 |
args: &[], |
| 42 |
ins: &["a\n", "b\n"], |
| 43 |
out: "a\tb\n", |
| 44 |
}, |
| 45 |
TestData { |
| 46 |
name: "zno-nl-1", |
| 47 |
args: &["-z"], |
| 48 |
ins: &["a", "b"], |
| 49 |
out: "a\tb\0", |
| 50 |
}, |
| 51 |
TestData { |
| 52 |
name: "zno-nl-2", |
| 53 |
args: &["-z"], |
| 54 |
ins: &["a\0", "b"], |
| 55 |
out: "a\tb\0", |
| 56 |
}, |
| 57 |
TestData { |
| 58 |
name: "zno-nl-3", |
| 59 |
args: &["-z"], |
| 60 |
ins: &["a", "b\0"], |
| 61 |
out: "a\tb\0", |
| 62 |
}, |
| 63 |
TestData { |
| 64 |
name: "zno-nl-4", |
| 65 |
args: &["-z"], |
| 66 |
ins: &["a\0", "b\0"], |
| 67 |
out: "a\tb\0", |
| 68 |
}, |
| 69 |
// Same as above, but with a two lines in each input file and the |
| 70 |
// addition of the -d option to make SPACE be the output |
| 71 |
// delimiter. |
| 72 |
TestData { |
| 73 |
name: "no-nla-1", |
| 74 |
args: &["-d", " "], |
| 75 |
ins: &["1\na", "2\nb"], |
| 76 |
out: "1 2\na b\n", |
| 77 |
}, |
| 78 |
TestData { |
| 79 |
name: "no-nla-2", |
| 80 |
args: &["-d", " "], |
| 81 |
ins: &["1\na\n", "2\nb"], |
| 82 |
out: "1 2\na b\n", |
| 83 |
}, |
| 84 |
TestData { |
| 85 |
name: "no-nla-3", |
| 86 |
args: &["-d", " "], |
| 87 |
ins: &["1\na", "2\nb\n"], |
| 88 |
out: "1 2\na b\n", |
| 89 |
}, |
| 90 |
TestData { |
| 91 |
name: "no-nla-4", |
| 92 |
args: &["-d", " "], |
| 93 |
ins: &["1\na\n", "2\nb\n"], |
| 94 |
out: "1 2\na b\n", |
| 95 |
}, |
| 96 |
TestData { |
| 97 |
name: "zno-nla1", |
| 98 |
args: &["-zd", " "], |
| 99 |
ins: &["1\0a", "2\0b"], |
| 100 |
out: "1 2\0a b\0", |
| 101 |
}, |
| 102 |
TestData { |
| 103 |
name: "zno-nla2", |
| 104 |
args: &["-zd", " "], |
| 105 |
ins: &["1\0a\0", "2\0b"], |
| 106 |
out: "1 2\0a b\0", |
| 107 |
}, |
| 108 |
TestData { |
| 109 |
name: "zno-nla3", |
| 110 |
args: &["-zd", " "], |
| 111 |
ins: &["1\0a", "2\0b\0"], |
| 112 |
out: "1 2\0a b\0", |
| 113 |
}, |
| 114 |
TestData { |
| 115 |
name: "zno-nla4", |
| 116 |
args: &["-zd", " "], |
| 117 |
ins: &["1\0a\0", "2\0b\0"], |
| 118 |
out: "1 2\0a b\0", |
| 119 |
}, |
| 120 |
TestData { |
| 121 |
name: "multibyte-delim", |
| 122 |
args: &["-d", "π£"], |
| 123 |
ins: &["1\na\n", "2\nb\n"], |
| 124 |
out: "1π£2\naπ£b\n", |
| 125 |
}, |
| 126 |
TestData { |
| 127 |
name: "multibyte-delim-serial", |
| 128 |
args: &["-d", "π£", "-s"], |
| 129 |
ins: &["1\na\n", "2\nb\n"], |
| 130 |
out: "1π£a\n2π£b\n", |
| 131 |
}, |
| 132 |
TestData { |
| 133 |
name: "trailing whitespace", |
| 134 |
args: &["-d", "|"], |
| 135 |
ins: &["1 \na \n", "2\t\nb\t\n"], |
| 136 |
out: "1 |2\t\na |b\t\n", |
| 137 |
}, |
| 138 |
TestData { |
| 139 |
name: "utf8-2byte-delim", |
| 140 |
args: &["-d", "\u{00A2}"], |
| 141 |
ins: &["1\n2\n", "a\nb\n"], |
| 142 |
out: "1\u{00A2}a\n2\u{00A2}b\n", |
| 143 |
}, |
| 144 |
TestData { |
| 145 |
name: "utf8-3byte-delim", |
| 146 |
args: &["-d", "\u{20AC}"], |
| 147 |
ins: &["1\n2\n", "a\nb\n"], |
| 148 |
out: "1\u{20AC}a\n2\u{20AC}b\n", |
| 149 |
}, |
| 150 |
TestData { |
| 151 |
name: "utf8-4byte-delim", |
| 152 |
args: &["-d", "\u{1F600}", "-s"], |
| 153 |
ins: &["1\n2\n3\n"], |
| 154 |
out: "1\u{1F600}2\u{1F600}3\n", |
| 155 |
}, |
| 156 |
TestData { |
| 157 |
name: "utf8-multi-delim-cycle", |
| 158 |
args: &["-d", "\u{00A2}\u{20AC}"], |
| 159 |
ins: &["a\nb\nc\n", "1\n2\n3\n", "x\ny\nz\n"], |
| 160 |
out: "a\u{00A2}1\u{20AC}x\nb\u{00A2}2\u{20AC}y\nc\u{00A2}3\u{20AC}z\n", |
| 161 |
}, |
| 162 |
]; |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn test_invalid_arg() { |
| 166 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn test_delimiter_hyphen_leading_as_separate_arg() { |
| 171 |
// A hyphen-leading delimiter value passed as its own argument (not |
| 172 |
// attached with `-d-x`/`=`) must not be mistaken for a new, |
| 173 |
// unrecognized flag. |
| 174 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 175 |
at.write("f1", "a\nb\n"); |
| 176 |
at.write("f2", "1\n2\n"); |
| 177 |
ucmd.args(&["-d", "-x", "f1", "f2"]) |
| 178 |
.succeeds() |
| 179 |
.stdout_is("a-1\nb-2\n"); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn test_combine_pairs_of_lines() { |
| 184 |
for s in ["-s", "--serial"] { |
| 185 |
for d in ["-d", "--delimiters"] { |
| 186 |
new_ucmd!() |
| 187 |
.args(&[s, d, "\t\n", "html_colors.txt"]) |
| 188 |
.succeeds() |
| 189 |
.stdout_is_fixture("html_colors.expected"); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn test_multi_stdin() { |
| 196 |
for d in ["-d", "--delimiters"] { |
| 197 |
new_ucmd!() |
| 198 |
.args(&[d, "\t\n", "-", "-"]) |
| 199 |
.pipe_in_fixture("html_colors.txt") |
| 200 |
.succeeds() |
| 201 |
.stdout_is_fixture("html_colors.expected"); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
// TODO: make this test work on Windows |
| 207 |
#[cfg(not(windows))] |
| 208 |
fn test_delimiter_list_ending_with_escaped_backslash() { |
| 209 |
for d in ["-d", "--delimiters"] { |
| 210 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 211 |
let mut ins = vec![]; |
| 212 |
for (i, one_in) in ["a\n", "b\n"].iter().enumerate() { |
| 213 |
let file = format!("in{i}"); |
| 214 |
at.write(&file, one_in); |
| 215 |
ins.push(file); |
| 216 |
} |
| 217 |
ucmd.args(&[d, r"\\"]) |
| 218 |
.args(&ins) |
| 219 |
.succeeds() |
| 220 |
.stdout_is("a\\b\n"); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn test_delimiter_list_ending_with_unescaped_backslash() { |
| 226 |
for d in ["-d", "--delimiters"] { |
| 227 |
new_ucmd!() |
| 228 |
.args(&[d, r"\"]) |
| 229 |
.fails() |
| 230 |
.stderr_contains(r"delimiter list ends with an unescaped backslash: \"); |
| 231 |
|
| 232 |
new_ucmd!() |
| 233 |
.args(&[d, r"\\\"]) |
| 234 |
.fails() |
| 235 |
.stderr_contains(r"delimiter list ends with an unescaped backslash: \\\"); |
| 236 |
|
| 237 |
new_ucmd!() |
| 238 |
.args(&[d, r"_\"]) |
| 239 |
.fails() |
| 240 |
.stderr_contains(r"delimiter list ends with an unescaped backslash: _\"); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn test_delimiter_list_empty() { |
| 246 |
for option_style in ["-d", "--delimiters"] { |
| 247 |
new_ucmd!() |
| 248 |
.args(&[option_style, "", "-s"]) |
| 249 |
.pipe_in( |
| 250 |
"\ |
| 251 |
A ALPHA 1 _ |
| 252 |
B BRAVO 2 _ |
| 253 |
C CHARLIE 3 _ |
| 254 |
", |
| 255 |
) |
| 256 |
.succeeds() |
| 257 |
.stdout_only( |
| 258 |
"\ |
| 259 |
A ALPHA 1 _B BRAVO 2 _C CHARLIE 3 _ |
| 260 |
", |
| 261 |
); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
// Was panicking (usize subtraction that would have resulted in a negative number) |
| 266 |
// Not observable in release builds, since integer overflow checking is not enabled |
| 267 |
#[test] |
| 268 |
fn test_delimiter_truncation() { |
| 269 |
for option_style in ["-d", "--delimiters"] { |
| 270 |
new_ucmd!() |
| 271 |
.args(&[option_style, "!@#", "-s", "-", "-", "-"]) |
| 272 |
.pipe_in( |
| 273 |
"\ |
| 274 |
FIRST |
| 275 |
SECOND |
| 276 |
THIRD |
| 277 |
FOURTH |
| 278 |
ABCDEFG |
| 279 |
", |
| 280 |
) |
| 281 |
.succeeds() |
| 282 |
.stdout_only( |
| 283 |
"\ |
| 284 |
FIRST!SECOND@THIRD#FOURTH!ABCDEFG |
| 285 |
|
| 286 |
|
| 287 |
", |
| 288 |
); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
// With `-s`, each input file is written as its own output line, so the |
| 293 |
// delimiter list must restart from its first element for every file instead |
| 294 |
// of carrying the cycle over from the previous one. |
| 295 |
#[test] |
| 296 |
fn test_serial_delimiter_list_resets_per_file() { |
| 297 |
for option_style in ["-d", "--delimiters"] { |
| 298 |
for serial in ["-s", "--serial"] { |
| 299 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 300 |
at.write("f1", "a\nb\n"); |
| 301 |
at.write("f2", "c\nd\ne\n"); |
| 302 |
at.write("f3", "f\ng\n"); |
| 303 |
|
| 304 |
ucmd.args(&[option_style, ":|", serial, "f1", "f2", "f3"]) |
| 305 |
.succeeds() |
| 306 |
.stdout_only("a:b\nc:d|e\nf:g\n"); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
#[cfg(unix)] |
| 313 |
fn test_non_utf8_input() { |
| 314 |
// 0xC0 is not valid UTF-8 |
| 315 |
const INPUT: &[u8] = b"Non-UTF-8 test: \xC0\x00\xC0.\n"; |
| 316 |
|
| 317 |
new_ucmd!() |
| 318 |
.pipe_in(INPUT) |
| 319 |
.succeeds() |
| 320 |
.stdout_only_bytes(INPUT); |
| 321 |
} |
| 322 |
|
| 323 |
#[test] |
| 324 |
fn test_three_trailing_backslashes_delimiter() { |
| 325 |
const ONE_BACKSLASH_STR: &str = r"\"; |
| 326 |
|
| 327 |
let three_backslashes_string = ONE_BACKSLASH_STR.repeat(3); |
| 328 |
|
| 329 |
for option_style in ["-d", "--delimiters"] { |
| 330 |
new_ucmd!() |
| 331 |
.args(&[option_style, &three_backslashes_string]) |
| 332 |
.fails() |
| 333 |
.no_stdout() |
| 334 |
.stderr_str_check(|st| { |
| 335 |
st.ends_with(&format!( |
| 336 |
": delimiter list ends with an unescaped backslash: {three_backslashes_string}\n" |
| 337 |
)) |
| 338 |
}); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// "If any other characters follow the <backslash>, the results are unspecified." |
| 343 |
// https://pubs.opengroup.org/onlinepubs/9799919799/utilities/paste.html |
| 344 |
// However, other implementations remove the backslash |
| 345 |
#[test] |
| 346 |
fn test_posix_unspecified_delimiter() { |
| 347 |
for option_style in ["-d", "--delimiters"] { |
| 348 |
new_ucmd!() |
| 349 |
.args(&[option_style, r"\z", "-s"]) |
| 350 |
.pipe_in( |
| 351 |
"\ |
| 352 |
1 |
| 353 |
2 |
| 354 |
3 |
| 355 |
4 |
| 356 |
", |
| 357 |
) |
| 358 |
.succeeds() |
| 359 |
.stdout_only( |
| 360 |
"\ |
| 361 |
1z2z3z4 |
| 362 |
", |
| 363 |
); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// "Empty string (not a null character)" |
| 368 |
// https://pubs.opengroup.org/onlinepubs/9799919799/utilities/paste.html |
| 369 |
#[test] |
| 370 |
fn test_backslash_zero_delimiter() { |
| 371 |
for option_style in ["-d", "--delimiters"] { |
| 372 |
new_ucmd!() |
| 373 |
.args(&[option_style, r"\0z\0", "-s"]) |
| 374 |
.pipe_in( |
| 375 |
"\ |
| 376 |
1 |
| 377 |
2 |
| 378 |
3 |
| 379 |
4 |
| 380 |
5 |
| 381 |
6 |
| 382 |
", |
| 383 |
) |
| 384 |
.succeeds() |
| 385 |
.stdout_only( |
| 386 |
"\ |
| 387 |
12z345z6 |
| 388 |
", |
| 389 |
); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
#[test] |
| 394 |
fn test_paste_delimiter_escape_sequences() { |
| 395 |
let cases: &[(&str, u8)] = &[(r"\b", 0x08), (r"\f", 0x0C), (r"\r", 0x0D), (r"\v", 0x0B)]; |
| 396 |
for &(esc, byte) in cases { |
| 397 |
let expected = [b'a', byte, b'b', byte, b'c', b'\n']; |
| 398 |
new_ucmd!() |
| 399 |
.args(&["-s", "-d", esc]) |
| 400 |
.pipe_in("a\nb\nc\n") |
| 401 |
.succeeds() |
| 402 |
.stdout_only_bytes(expected); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
// As of 2024-10-09, only bsdutils (https://github.com/dcantrell/bsdutils, derived from FreeBSD) and toybox handle |
| 407 |
// multibyte delimiter characters in the way a user would likely expect. BusyBox and GNU Core Utilities do not. |
| 408 |
#[test] |
| 409 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 410 |
fn test_multi_byte_delimiter() { |
| 411 |
for option_style in ["-d", "--delimiters"] { |
| 412 |
new_ucmd!() |
| 413 |
// A multibyte delimiter is stepped per character in a UTF-8 locale. |
| 414 |
.env("LC_ALL", "C.UTF-8") |
| 415 |
.args(&[option_style, "!Γ@", "-s"]) |
| 416 |
.pipe_in( |
| 417 |
"\ |
| 418 |
1 |
| 419 |
2 |
| 420 |
3 |
| 421 |
4 |
| 422 |
5 |
| 423 |
6 |
| 424 |
", |
| 425 |
) |
| 426 |
.succeeds() |
| 427 |
.stdout_only( |
| 428 |
"\ |
| 429 |
1!2Γ3@4!5Γ6 |
| 430 |
", |
| 431 |
); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 437 |
fn test_data() { |
| 438 |
for example in EXAMPLE_DATA { |
| 439 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 440 |
let mut ins = vec![]; |
| 441 |
for (i, one_in) in example.ins.iter().enumerate() { |
| 442 |
let file = format!("in{i}"); |
| 443 |
at.write(&file, one_in); |
| 444 |
ins.push(file); |
| 445 |
} |
| 446 |
println!("{}", example.name); |
| 447 |
// Some examples use multibyte delimiters, which need a UTF-8 locale. |
| 448 |
ucmd.env("LC_ALL", "C.UTF-8") |
| 449 |
.args(example.args) |
| 450 |
.args(&ins) |
| 451 |
.succeeds() |
| 452 |
.stdout_is(example.out); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
#[cfg(target_os = "linux")] |
| 458 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 459 |
fn test_non_utf8_delimiter() { |
| 460 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 461 |
at.write("f1", "1\n2\n"); |
| 462 |
at.write("f2", "a\nb\n"); |
| 463 |
let delim = std::ffi::OsString::from_vec(vec![0xA2, 0xE3]); |
| 464 |
ucmd.env("LC_ALL", "zh_CN.gb18030") |
| 465 |
.arg("-d") |
| 466 |
.arg(&delim) |
| 467 |
.args(&["f1", "f2"]) |
| 468 |
.succeeds() |
| 469 |
.stdout_only_bytes(b"1\xA2\xE3a\n2\xA2\xE3b\n"); |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
#[cfg(target_os = "linux")] |
| 474 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 475 |
fn test_paste_non_utf8_paths() { |
| 476 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 477 |
|
| 478 |
let filename1 = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]); |
| 479 |
let filename2 = std::ffi::OsString::from_vec(vec![0xF0, 0x90]); |
| 480 |
|
| 481 |
std::fs::write(at.plus(&filename1), b"line1\nline2\n").unwrap(); |
| 482 |
std::fs::write(at.plus(&filename2), b"col1\ncol2\n").unwrap(); |
| 483 |
|
| 484 |
ucmd.arg(&filename1) |
| 485 |
.arg(&filename2) |
| 486 |
.succeeds() |
| 487 |
.stdout_is("line1\tcol1\nline2\tcol2\n"); |
| 488 |
} |
| 489 |
|
| 490 |
#[cfg(target_os = "linux")] |
| 491 |
fn make_broken_pipe() -> std::io::PipeWriter { |
| 492 |
let (read, write) = std::io::pipe().expect("Failed to create pipe"); |
| 493 |
// Drop the read end so writes fail with EPIPE. |
| 494 |
drop(read); |
| 495 |
// Return the write end of the pipe |
| 496 |
write |
| 497 |
} |
| 498 |
|
| 499 |
#[test] |
| 500 |
#[cfg(target_os = "linux")] |
| 501 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/dev) not visible")] |
| 502 |
fn test_dev_zero_write_error_dev_full() { |
| 503 |
use std::fs::File; |
| 504 |
|
| 505 |
let dev_full = |
| 506 |
File::create("/dev/full").expect("Failed to open /dev/full - test must run on Linux"); |
| 507 |
|
| 508 |
new_ucmd!() |
| 509 |
.arg("/dev/zero") |
| 510 |
.set_stdout(dev_full) |
| 511 |
.fails() |
| 512 |
.code_is(1) |
| 513 |
.stderr_contains("No space left on device"); |
| 514 |
} |
| 515 |
|
| 516 |
#[test] |
| 517 |
#[cfg(target_os = "linux")] |
| 518 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/dev) not visible")] |
| 519 |
fn test_dev_zero_closed_pipe() { |
| 520 |
new_ucmd!() |
| 521 |
.arg("/dev/zero") |
| 522 |
.set_stdout(make_broken_pipe()) |
| 523 |
.run() |
| 524 |
.fails_silently(); |
| 525 |
} |
| 526 |
|
| 527 |
#[test] |
| 528 |
fn test_repeated_delimiter_takes_the_last() { |
| 529 |
// GNU lets a later -d override an earlier one rather than erroring. |
| 530 |
new_ucmd!() |
| 531 |
.args(&["-d", ",", "-d", ":", "-s", "-"]) |
| 532 |
.pipe_in("a\nb\n") |
| 533 |
.succeeds() |
| 534 |
.stdout_is("a:b\n"); |
| 535 |
} |