2021-08-24 10:00:07 +00:00
|
|
|
// spell-checker:ignore NOFILE
|
|
|
|
|
2020-05-25 17:05:26 +00:00
|
|
|
use crate::common::util::*;
|
2021-04-21 10:21:31 +00:00
|
|
|
use std::fs::OpenOptions;
|
|
|
|
#[cfg(unix)]
|
2021-04-10 20:19:53 +00:00
|
|
|
use std::io::Read;
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2021-08-24 10:00:07 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
use rlimit::Resource;
|
|
|
|
|
2021-04-03 14:06:29 +00:00
|
|
|
#[test]
|
|
|
|
fn test_output_simple() {
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&["alpha.txt"])
|
|
|
|
.succeeds()
|
2021-05-31 03:55:28 +00:00
|
|
|
.stdout_only("abcde\nfghij\nklmno\npqrst\nuvwxyz\n"); // spell-checker:disable-line
|
2021-04-03 14:06:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 20:19:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_no_options() {
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable-next-line
|
2021-04-10 20:19:53 +00:00
|
|
|
for fixture in &["empty.txt", "alpha.txt", "nonewline.txt"] {
|
|
|
|
// Give fixture through command line file argument
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&[fixture])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture(fixture);
|
|
|
|
// Give fixture through stdin
|
|
|
|
new_ucmd!()
|
|
|
|
.pipe_in_fixture(fixture)
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture(fixture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-19 09:14:04 +00:00
|
|
|
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))]
|
2021-04-10 20:19:53 +00:00
|
|
|
fn test_no_options_big_input() {
|
|
|
|
for &n in &[
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
42,
|
|
|
|
16 * 1024 - 7,
|
|
|
|
16 * 1024 - 1,
|
|
|
|
16 * 1024,
|
|
|
|
16 * 1024 + 1,
|
|
|
|
16 * 1024 + 3,
|
|
|
|
32 * 1024,
|
|
|
|
64 * 1024,
|
|
|
|
80 * 1024,
|
|
|
|
96 * 1024,
|
|
|
|
112 * 1024,
|
|
|
|
128 * 1024,
|
|
|
|
] {
|
|
|
|
let data = vec_of_size(n);
|
|
|
|
let data2 = data.clone();
|
|
|
|
assert_eq!(data.len(), data2.len());
|
|
|
|
new_ucmd!().pipe_in(data).succeeds().stdout_is_bytes(&data2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn test_fifo_symlink() {
|
|
|
|
use std::io::Write;
|
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
s.fixtures.mkdir("dir");
|
|
|
|
s.fixtures.mkfifo("dir/pipe");
|
|
|
|
assert!(s.fixtures.is_fifo("dir/pipe"));
|
|
|
|
|
|
|
|
// Make cat read the pipe through a symlink
|
2021-05-31 03:55:28 +00:00
|
|
|
s.fixtures.symlink_file("dir/pipe", "sympipe"); // spell-checker:disable-line
|
|
|
|
let proc = s.ucmd().args(&["sympipe"]).run_no_wait(); // spell-checker:disable-line
|
2021-04-10 20:19:53 +00:00
|
|
|
|
|
|
|
let data = vec_of_size(128 * 1024);
|
|
|
|
let data2 = data.clone();
|
|
|
|
|
|
|
|
let pipe_path = s.fixtures.plus("dir/pipe");
|
|
|
|
let thread = thread::spawn(move || {
|
|
|
|
let mut pipe = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(false)
|
|
|
|
.open(pipe_path)
|
|
|
|
.unwrap();
|
|
|
|
pipe.write_all(&data).unwrap();
|
|
|
|
});
|
|
|
|
|
|
|
|
let output = proc.wait_with_output().unwrap();
|
|
|
|
assert_eq!(&output.stdout, &data2);
|
|
|
|
thread.join().unwrap();
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:00:07 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn test_closes_file_descriptors() {
|
|
|
|
// Each file creates a pipe, which has two file descriptors.
|
|
|
|
// If they are not closed then five is certainly too many.
|
|
|
|
new_ucmd!()
|
|
|
|
.args(&[
|
|
|
|
"alpha.txt",
|
|
|
|
"alpha.txt",
|
|
|
|
"alpha.txt",
|
|
|
|
"alpha.txt",
|
|
|
|
"alpha.txt",
|
|
|
|
])
|
|
|
|
.with_limit(Resource::NOFILE, 9, 9)
|
|
|
|
.succeeds();
|
|
|
|
}
|
|
|
|
|
2021-04-21 10:21:31 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn test_piped_to_regular_file() {
|
|
|
|
use std::fs::read_to_string;
|
|
|
|
|
|
|
|
for &append in &[true, false] {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
let file_path = s.fixtures.plus("file.txt");
|
|
|
|
|
|
|
|
{
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.create_new(true)
|
|
|
|
.write(true)
|
|
|
|
.append(append)
|
|
|
|
.open(&file_path)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
s.ucmd()
|
|
|
|
.set_stdout(file)
|
|
|
|
.pipe_in_fixture("alpha.txt")
|
|
|
|
.succeeds();
|
|
|
|
}
|
|
|
|
let contents = read_to_string(&file_path).unwrap();
|
2021-05-31 03:55:28 +00:00
|
|
|
assert_eq!(contents, "abcde\nfghij\nklmno\npqrst\nuvwxyz\n"); // spell-checker:disable-line
|
2021-04-21 10:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn test_piped_to_dev_null() {
|
|
|
|
for &append in &[true, false] {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
{
|
|
|
|
let dev_null = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.append(append)
|
|
|
|
.open("/dev/null")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
s.ucmd()
|
|
|
|
.set_stdout(dev_null)
|
|
|
|
.pipe_in_fixture("alpha.txt")
|
|
|
|
.succeeds();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
|
|
|
|
fn test_piped_to_dev_full() {
|
|
|
|
for &append in &[true, false] {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
{
|
|
|
|
let dev_full = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.append(append)
|
|
|
|
.open("/dev/full")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
s.ucmd()
|
|
|
|
.set_stdout(dev_full)
|
|
|
|
.pipe_in_fixture("alpha.txt")
|
|
|
|
.fails()
|
|
|
|
.stderr_contains(&"No space left on device".to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 20:19:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_directory() {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
s.fixtures.mkdir("test_directory");
|
|
|
|
s.ucmd()
|
|
|
|
.args(&["test_directory"])
|
|
|
|
.fails()
|
|
|
|
.stderr_is("cat: test_directory: Is a directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_directory_and_file() {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
s.fixtures.mkdir("test_directory2");
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable-next-line
|
2021-04-10 20:19:53 +00:00
|
|
|
for fixture in &["empty.txt", "alpha.txt", "nonewline.txt"] {
|
|
|
|
s.ucmd()
|
|
|
|
.args(&["test_directory2", fixture])
|
|
|
|
.fails()
|
|
|
|
.stderr_is("cat: test_directory2: Is a directory")
|
|
|
|
.stdout_is_fixture(fixture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-16 17:44:40 +00:00
|
|
|
#[cfg(unix)]
|
2021-04-10 20:19:53 +00:00
|
|
|
fn test_three_directories_and_file_and_stdin() {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
s.fixtures.mkdir("test_directory3");
|
|
|
|
s.fixtures.mkdir("test_directory3/test_directory4");
|
|
|
|
s.fixtures.mkdir("test_directory3/test_directory5");
|
|
|
|
s.ucmd()
|
|
|
|
.args(&[
|
|
|
|
"test_directory3/test_directory4",
|
|
|
|
"alpha.txt",
|
|
|
|
"-",
|
2021-05-30 05:10:54 +00:00
|
|
|
"file_which_does_not_exist.txt",
|
2021-05-31 03:55:28 +00:00
|
|
|
"nonewline.txt", // spell-checker:disable-line
|
2021-04-10 20:19:53 +00:00
|
|
|
"test_directory3/test_directory5",
|
|
|
|
"test_directory3/../test_directory3/test_directory5",
|
|
|
|
"test_directory3",
|
|
|
|
])
|
|
|
|
.pipe_in("stdout bytes")
|
|
|
|
.fails()
|
|
|
|
.stderr_is_fixture("three_directories_and_file_and_stdin.stderr.expected")
|
|
|
|
.stdout_is(
|
2021-05-31 03:55:28 +00:00
|
|
|
"abcde\nfghij\nklmno\npqrst\nuvwxyz\nstdout bytestext without a trailing newline", // spell-checker:disable-line
|
2021-04-10 20:19:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_output_multi_files_print_all_chars() {
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&["alpha.txt", "256.txt", "-A", "-n"])
|
2016-02-17 00:43:25 +00:00
|
|
|
.succeeds()
|
2020-04-13 18:36:03 +00:00
|
|
|
.stdout_only(
|
|
|
|
" 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n \
|
2021-03-18 09:24:30 +00:00
|
|
|
5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n \
|
|
|
|
7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ \
|
|
|
|
!\"#$%&\'()*+,-./0123456789:;\
|
|
|
|
<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^\
|
|
|
|
BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^V\
|
|
|
|
M-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- \
|
|
|
|
M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:\
|
|
|
|
M-;M-<M-=M->M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-U\
|
|
|
|
M-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-\
|
|
|
|
pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?",
|
2020-04-13 18:36:03 +00:00
|
|
|
);
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:enable
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 02:34:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_numbered_lines_no_trailing_newline() {
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable
|
2017-01-23 02:34:47 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.args(&["nonewline.txt", "alpha.txt", "-n"])
|
|
|
|
.succeeds()
|
2020-04-13 18:36:03 +00:00
|
|
|
.stdout_only(
|
|
|
|
" 1\ttext without a trailing newlineabcde\n 2\tfghij\n \
|
2021-03-18 09:24:30 +00:00
|
|
|
3\tklmno\n 4\tpqrst\n 5\tuvwxyz\n",
|
2020-04-13 18:36:03 +00:00
|
|
|
);
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:enable
|
2017-01-23 02:34:47 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 00:43:25 +00:00
|
|
|
#[test]
|
|
|
|
fn test_stdin_show_nonprinting() {
|
2022-01-29 00:03:28 +00:00
|
|
|
for same_param in &["-v", "--show-nonprinting", "--show-non"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-11-25 19:14:46 +00:00
|
|
|
.args(&[same_param])
|
2016-02-17 00:43:25 +00:00
|
|
|
.pipe_in("\t\0\n")
|
|
|
|
.succeeds()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_only("\t^@\n");
|
2016-02-17 00:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stdin_show_tabs() {
|
2022-01-29 00:03:28 +00:00
|
|
|
for same_param in &["-T", "--show-tabs", "--show-ta"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&[same_param])
|
2016-02-17 00:43:25 +00:00
|
|
|
.pipe_in("\t\0\n")
|
|
|
|
.succeeds()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_only("^I\0\n");
|
2016-02-17 00:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stdin_show_ends() {
|
2022-01-29 00:03:28 +00:00
|
|
|
for &same_param in &["-E", "--show-ends", "--show-e"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2020-04-13 18:36:03 +00:00
|
|
|
.args(&[same_param, "-"])
|
2019-02-07 20:54:48 +00:00
|
|
|
.pipe_in("\t\0\n\t")
|
2016-02-17 00:43:25 +00:00
|
|
|
.succeeds()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_only("\t\0$\n\t");
|
2016-02-17 00:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 19:31:05 +00:00
|
|
|
#[test]
|
|
|
|
fn squeeze_all_files() {
|
|
|
|
// empty lines at the end of a file are "squeezed" together with empty lines at the beginning
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
at.write("input1", "a\n\n");
|
|
|
|
at.write("input2", "\n\nb");
|
|
|
|
ucmd.args(&["input1", "input2", "-s"])
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("a\n\nb");
|
|
|
|
}
|
|
|
|
|
2021-08-07 19:05:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_show_ends_crlf() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-E")
|
|
|
|
.pipe_in("a\nb\r\n\rc\n\r\n\r")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("a$\nb^M$\n\rc$\n^M$\n\r");
|
|
|
|
}
|
2016-02-17 00:43:25 +00:00
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
#[test]
|
2016-02-17 07:06:28 +00:00
|
|
|
fn test_stdin_show_all() {
|
2022-01-29 00:03:28 +00:00
|
|
|
for same_param in &["-A", "--show-all", "--show-a"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&[same_param])
|
2016-02-17 00:43:25 +00:00
|
|
|
.pipe_in("\t\0\n")
|
|
|
|
.succeeds()
|
2019-02-07 20:54:48 +00:00
|
|
|
.stdout_only("^I^@$\n");
|
2016-02-17 00:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2016-02-17 00:43:25 +00:00
|
|
|
#[test]
|
|
|
|
fn test_stdin_nonprinting_and_endofline() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&["-e"])
|
2016-02-17 00:43:25 +00:00
|
|
|
.pipe_in("\t\0\n")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("\t^@$\n");
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 07:06:28 +00:00
|
|
|
#[test]
|
2016-02-17 00:43:25 +00:00
|
|
|
fn test_stdin_nonprinting_and_tabs() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&["-t"])
|
2016-02-17 00:43:25 +00:00
|
|
|
.pipe_in("\t\0\n")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only("^I^@\n");
|
|
|
|
}
|
2016-02-17 07:06:28 +00:00
|
|
|
|
2016-02-17 00:43:25 +00:00
|
|
|
#[test]
|
|
|
|
fn test_stdin_squeeze_blank() {
|
2022-01-29 00:03:28 +00:00
|
|
|
for same_param in &["-s", "--squeeze-blank", "--squeeze"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.arg(same_param)
|
2016-07-17 17:39:57 +00:00
|
|
|
.pipe_in("\n\na\n\n\n\n\nb\n\n\n")
|
2016-02-17 00:43:25 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_only("\na\n\nb\n\n");
|
|
|
|
}
|
2016-02-17 07:06:28 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_stdin_number_non_blank() {
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable-next-line
|
2022-01-29 00:03:28 +00:00
|
|
|
for same_param in &["-b", "--number-nonblank", "--number-non"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.arg(same_param)
|
2016-02-17 00:43:25 +00:00
|
|
|
.arg("-")
|
2016-07-17 17:39:57 +00:00
|
|
|
.pipe_in("\na\nb\n\n\nc")
|
2016-02-17 00:43:25 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_only("\n 1\ta\n 2\tb\n\n\n 3\tc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_non_blank_overrides_number() {
|
2021-05-30 05:10:54 +00:00
|
|
|
// spell-checker:disable-next-line
|
2021-05-29 12:32:35 +00:00
|
|
|
for &same_param in &["-b", "--number-nonblank"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&[same_param, "-"])
|
2016-07-17 17:39:57 +00:00
|
|
|
.pipe_in("\na\nb\n\n\nc")
|
2016-02-17 00:43:25 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_only("\n 1\ta\n 2\tb\n\n\n 3\tc");
|
2017-01-08 05:16:32 +00:00
|
|
|
}
|
2016-02-17 00:43:25 +00:00
|
|
|
}
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2016-02-17 00:43:25 +00:00
|
|
|
#[test]
|
|
|
|
fn test_squeeze_blank_before_numbering() {
|
2021-05-29 12:32:35 +00:00
|
|
|
for &same_param in &["-s", "--squeeze-blank"] {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.args(&[same_param, "-n", "-"])
|
2016-02-17 00:43:25 +00:00
|
|
|
.pipe_in("a\n\n\nb")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_only(" 1\ta\n 2\t\n 3\tb");
|
|
|
|
}
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
2017-01-08 05:16:32 +00:00
|
|
|
|
2021-04-10 20:19:53 +00:00
|
|
|
/// This tests reading from Unix character devices
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn test_dev_random() {
|
|
|
|
let mut buf = [0; 2048];
|
2021-05-18 20:10:51 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
2021-05-19 09:06:46 +00:00
|
|
|
const DEV_RANDOM: &str = "/dev/urandom";
|
2021-05-18 20:10:51 +00:00
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
2021-05-19 09:06:46 +00:00
|
|
|
const DEV_RANDOM: &str = "/dev/random";
|
2021-05-18 20:10:51 +00:00
|
|
|
|
2021-05-19 09:06:46 +00:00
|
|
|
let mut proc = new_ucmd!().args(&[DEV_RANDOM]).run_no_wait();
|
2021-04-10 20:19:53 +00:00
|
|
|
let mut proc_stdout = proc.stdout.take().unwrap();
|
|
|
|
proc_stdout.read_exact(&mut buf).unwrap();
|
|
|
|
|
|
|
|
let num_zeroes = buf.iter().fold(0, |mut acc, &n| {
|
|
|
|
if n == 0 {
|
|
|
|
acc += 1;
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
// The probability of more than 512 zero bytes is essentially zero if the
|
|
|
|
// output is truly random.
|
|
|
|
assert!(num_zeroes < 512);
|
|
|
|
proc.kill().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reading from /dev/full should return an infinite amount of zero bytes.
|
|
|
|
/// Wikipedia says there is support on Linux, FreeBSD, and NetBSD.
|
|
|
|
#[test]
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
|
|
|
|
fn test_dev_full() {
|
|
|
|
let mut buf = [0; 2048];
|
|
|
|
let mut proc = new_ucmd!().args(&["/dev/full"]).run_no_wait();
|
|
|
|
let mut proc_stdout = proc.stdout.take().unwrap();
|
|
|
|
let expected = [0; 2048];
|
|
|
|
proc_stdout.read_exact(&mut buf).unwrap();
|
|
|
|
assert_eq!(&buf[..], &expected[..]);
|
|
|
|
proc.kill().unwrap();
|
|
|
|
}
|
|
|
|
|
2017-01-08 05:16:32 +00:00
|
|
|
#[test]
|
2021-04-10 20:19:53 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
|
|
|
|
fn test_dev_full_show_all() {
|
|
|
|
let mut buf = [0; 2048];
|
|
|
|
let mut proc = new_ucmd!().args(&["-A", "/dev/full"]).run_no_wait();
|
|
|
|
let mut proc_stdout = proc.stdout.take().unwrap();
|
|
|
|
proc_stdout.read_exact(&mut buf).unwrap();
|
|
|
|
|
|
|
|
let expected: Vec<u8> = (0..buf.len())
|
|
|
|
.map(|n| if n & 1 == 0 { b'^' } else { b'@' })
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
assert_eq!(&buf[..], &expected[..]);
|
|
|
|
proc.kill().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
2021-05-09 16:42:16 +00:00
|
|
|
#[ignore]
|
2017-01-08 05:16:32 +00:00
|
|
|
fn test_domain_socket() {
|
|
|
|
use std::io::prelude::*;
|
2021-04-24 10:46:06 +00:00
|
|
|
use std::sync::{Arc, Barrier};
|
2020-04-13 18:36:03 +00:00
|
|
|
use std::thread;
|
2021-04-10 20:19:53 +00:00
|
|
|
use unix_socket::UnixListener;
|
2017-01-08 05:16:32 +00:00
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
let dir = tempfile::Builder::new()
|
|
|
|
.prefix("unix_socket")
|
|
|
|
.tempdir()
|
|
|
|
.expect("failed to create dir");
|
2017-01-08 05:16:32 +00:00
|
|
|
let socket_path = dir.path().join("sock");
|
|
|
|
let listener = UnixListener::bind(&socket_path).expect("failed to create socket");
|
|
|
|
|
2021-04-22 06:42:56 +00:00
|
|
|
// use a barrier to ensure we don't run cat before the listener is setup
|
|
|
|
let barrier = Arc::new(Barrier::new(2));
|
|
|
|
let barrier2 = Arc::clone(&barrier);
|
|
|
|
|
2017-01-08 05:16:32 +00:00
|
|
|
let thread = thread::spawn(move || {
|
|
|
|
let mut stream = listener.accept().expect("failed to accept connection").0;
|
2021-04-22 06:42:56 +00:00
|
|
|
barrier2.wait();
|
2020-04-13 18:36:03 +00:00
|
|
|
stream
|
|
|
|
.write_all(b"a\tb")
|
|
|
|
.expect("failed to write test data");
|
2017-01-08 05:16:32 +00:00
|
|
|
});
|
|
|
|
|
2021-04-22 06:42:56 +00:00
|
|
|
let child = new_ucmd!().args(&[socket_path]).run_no_wait();
|
|
|
|
barrier.wait();
|
2021-05-29 12:32:35 +00:00
|
|
|
let stdout = &child.wait_with_output().unwrap().stdout;
|
2021-06-06 19:13:54 +00:00
|
|
|
let output = String::from_utf8_lossy(stdout);
|
2021-04-22 06:42:56 +00:00
|
|
|
assert_eq!("a\tb", output);
|
2017-01-08 05:16:32 +00:00
|
|
|
|
|
|
|
thread.join().unwrap();
|
|
|
|
}
|
2021-08-07 22:48:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_to_self_empty() {
|
|
|
|
// it's ok if the input file is also the output file if it's empty
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
let file_path = s.fixtures.plus("file.txt");
|
|
|
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.create_new(true)
|
|
|
|
.write(true)
|
|
|
|
.append(true)
|
|
|
|
.open(&file_path)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
s.ucmd().set_stdout(file).arg(&file_path).succeeds();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_to_self() {
|
|
|
|
let s = TestScenario::new(util_name!());
|
|
|
|
let file_path = s.fixtures.plus("first_file");
|
|
|
|
s.fixtures.write("second_file", "second_file_content.");
|
|
|
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.create_new(true)
|
|
|
|
.write(true)
|
|
|
|
.append(true)
|
|
|
|
.open(&file_path)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
s.fixtures.append("first_file", "first_file_content.");
|
|
|
|
|
|
|
|
s.ucmd()
|
|
|
|
.set_stdout(file)
|
|
|
|
.arg("first_file")
|
|
|
|
.arg("first_file")
|
|
|
|
.arg("second_file")
|
|
|
|
.fails()
|
|
|
|
.code_is(2)
|
|
|
|
.stderr_only("cat: first_file: input file is output file\ncat: first_file: input file is output file");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
s.fixtures.read("first_file"),
|
|
|
|
"first_file_content.second_file_content."
|
|
|
|
);
|
|
|
|
}
|