Skip to content

Commit

Permalink
Add tests for non-utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Feb 20, 2023
1 parent 3acd75b commit 5cfc48e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 92 deletions.
81 changes: 61 additions & 20 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ use std::os::unix::fs::PermissionsExt;
use std::os::windows::fs::symlink_file;
#[cfg(not(windows))]
use std::path::Path;
#[cfg(target_os = "linux")]
use std::path::PathBuf;

#[cfg(any(target_os = "linux", target_os = "android"))]
use filetime::FileTime;
#[cfg(any(target_os = "linux", target_os = "android"))]
use rlimit::Resource;
use std::ffi::OsString;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::fs as std_fs;
use std::thread::sleep;
Expand Down Expand Up @@ -91,7 +94,7 @@ fn test_cp_existing_target() {
assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n");

// No backup should have been created
assert!(!at.file_exists(&format!("{TEST_EXISTING_FILE}~")));
assert!(!at.file_exists(format!("{TEST_EXISTING_FILE}~")));
}

#[test]
Expand Down Expand Up @@ -636,7 +639,7 @@ fn test_cp_backup_none() {
.no_stderr();

assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n");
assert!(!at.file_exists(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")));
assert!(!at.file_exists(format!("{TEST_HOW_ARE_YOU_SOURCE}~")));
}

#[test]
Expand All @@ -650,7 +653,7 @@ fn test_cp_backup_off() {
.no_stderr();

assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n");
assert!(!at.file_exists(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")));
assert!(!at.file_exists(format!("{TEST_HOW_ARE_YOU_SOURCE}~")));
}

#[test]
Expand Down Expand Up @@ -700,7 +703,7 @@ fn test_cp_deref() {
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK);
// unlike -P/--no-deref, we expect a file, not a link
assert!(at.file_exists(
&path_to_new_symlink
path_to_new_symlink
.clone()
.into_os_string()
.into_string()
Expand Down Expand Up @@ -1062,7 +1065,7 @@ fn test_cp_deref_folder_to_folder() {
.join(TEST_COPY_TO_FOLDER_NEW)
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK);
assert!(at.file_exists(
&path_to_new_symlink
path_to_new_symlink
.clone()
.into_os_string()
.into_string()
Expand Down Expand Up @@ -1225,8 +1228,8 @@ fn test_cp_archive_recursive() {
let file_2 = at.subdir.join(TEST_COPY_TO_FOLDER).join("2");
let file_2_link = at.subdir.join(TEST_COPY_TO_FOLDER).join("2.link");

at.touch(&file_1.to_string_lossy());
at.touch(&file_2.to_string_lossy());
at.touch(file_1);
at.touch(file_2);

at.symlink_file("1", &file_1_link.to_string_lossy());
at.symlink_file("2", &file_2_link.to_string_lossy());
Expand All @@ -1252,18 +1255,8 @@ fn test_cp_archive_recursive() {
.run();

println!("ls dest {}", result.stdout_str());
assert!(at.file_exists(
&at.subdir
.join(TEST_COPY_TO_FOLDER_NEW)
.join("1")
.to_string_lossy()
));
assert!(at.file_exists(
&at.subdir
.join(TEST_COPY_TO_FOLDER_NEW)
.join("2")
.to_string_lossy()
));
assert!(at.file_exists(at.subdir.join(TEST_COPY_TO_FOLDER_NEW).join("1")));
assert!(at.file_exists(at.subdir.join(TEST_COPY_TO_FOLDER_NEW).join("2")));

assert!(at.is_symlink(
&at.subdir
Expand Down Expand Up @@ -1672,7 +1665,7 @@ fn test_cp_reflink_always_override() {
let dst_path: &str = &vec![MOUNTPOINT, USERDIR, "dst"].concat();

scene.fixtures.mkdir(ROOTDIR);
scene.fixtures.mkdir(&vec![ROOTDIR, USERDIR].concat());
scene.fixtures.mkdir(vec![ROOTDIR, USERDIR].concat());

// Setup:
// Because neither `mkfs.btrfs` not btrfs `mount` options allow us to have a mountpoint owned
Expand Down Expand Up @@ -2532,3 +2525,51 @@ fn test_src_base_dot() {
.no_stdout();
assert!(!at.dir_exists("y/x"));
}

#[cfg(target_os = "linux")]
fn non_utf8_name(suffix: &str) -> OsString {
use std::os::unix::ffi::OsStringExt;
let mut name = OsString::from_vec(vec![0xff, 0xff]);
name.push(suffix);
name
}

#[cfg(target_os = "linux")]
#[test]
fn test_non_utf8_src() {
let (at, mut ucmd) = at_and_ucmd!();
let src = non_utf8_name("src");
std::fs::File::create(at.plus(&src)).unwrap();
ucmd.args(&[src, "dest".into()])
.succeeds()
.no_stderr()
.no_stdout();
assert!(at.file_exists("dest"));
}

#[cfg(target_os = "linux")]
#[test]
fn test_non_utf8_dest() {
let (at, mut ucmd) = at_and_ucmd!();
let dest = non_utf8_name("dest");
ucmd.args(&[TEST_HELLO_WORLD_SOURCE.as_ref(), &*dest])
.succeeds()
.no_stderr()
.no_stdout();
assert!(at.file_exists(dest));
}

#[cfg(target_os = "linux")]
#[test]
fn test_non_utf8_target() {
let (at, mut ucmd) = at_and_ucmd!();
let dest = non_utf8_name("dest");
at.mkdir(&dest);
ucmd.args(&["-t".as_ref(), &*dest, TEST_HELLO_WORLD_SOURCE.as_ref()])
.succeeds()
.no_stderr()
.no_stdout();
let mut copied_file = PathBuf::from(dest);
copied_file.push(TEST_HELLO_WORLD_SOURCE);
assert!(at.file_exists(copied_file));
}
2 changes: 1 addition & 1 deletion tests/by-util/test_ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ fn test_symlink_implicit_target_dir() {
let file = &path.to_string_lossy();

at.mkdir(dir);
at.touch(file);
at.touch(&path);

ucmd.args(&["-s", file]).succeeds().no_stderr();

Expand Down
72 changes: 36 additions & 36 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,10 @@ fn test_ls_a() {
fn test_ls_width() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-width-1"));
at.touch(&at.plus_as_string("test-width-2"));
at.touch(&at.plus_as_string("test-width-3"));
at.touch(&at.plus_as_string("test-width-4"));
at.touch(at.plus_as_string("test-width-1"));
at.touch(at.plus_as_string("test-width-2"));
at.touch(at.plus_as_string("test-width-3"));
at.touch(at.plus_as_string("test-width-4"));

for option in [
"-w 100",
Expand Down Expand Up @@ -692,10 +692,10 @@ fn test_ls_width() {
fn test_ls_columns() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-columns-1"));
at.touch(&at.plus_as_string("test-columns-2"));
at.touch(&at.plus_as_string("test-columns-3"));
at.touch(&at.plus_as_string("test-columns-4"));
at.touch(at.plus_as_string("test-columns-1"));
at.touch(at.plus_as_string("test-columns-2"));
at.touch(at.plus_as_string("test-columns-3"));
at.touch(at.plus_as_string("test-columns-4"));

// Columns is the default
let result = scene.ucmd().succeeds();
Expand Down Expand Up @@ -753,10 +753,10 @@ fn test_ls_columns() {
fn test_ls_across() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-across-1"));
at.touch(&at.plus_as_string("test-across-2"));
at.touch(&at.plus_as_string("test-across-3"));
at.touch(&at.plus_as_string("test-across-4"));
at.touch(at.plus_as_string("test-across-1"));
at.touch(at.plus_as_string("test-across-2"));
at.touch(at.plus_as_string("test-across-3"));
at.touch(at.plus_as_string("test-across-4"));

for option in ACROSS_ARGS {
let result = scene.ucmd().arg(option).succeeds();
Expand All @@ -781,10 +781,10 @@ fn test_ls_across() {
fn test_ls_commas() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-commas-1"));
at.touch(&at.plus_as_string("test-commas-2"));
at.touch(&at.plus_as_string("test-commas-3"));
at.touch(&at.plus_as_string("test-commas-4"));
at.touch(at.plus_as_string("test-commas-1"));
at.touch(at.plus_as_string("test-commas-2"));
at.touch(at.plus_as_string("test-commas-3"));
at.touch(at.plus_as_string("test-commas-4"));

for option in COMMA_ARGS {
let result = scene.ucmd().arg(option).succeeds();
Expand Down Expand Up @@ -814,8 +814,8 @@ fn test_ls_zero() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.mkdir("0-test-zero");
at.touch(&at.plus_as_string("2-test-zero"));
at.touch(&at.plus_as_string("3-test-zero"));
at.touch(at.plus_as_string("2-test-zero"));
at.touch(at.plus_as_string("3-test-zero"));

let ignored_opts = [
"--quoting-style=c",
Expand Down Expand Up @@ -870,7 +870,7 @@ fn test_ls_zero() {

#[cfg(unix)]
{
at.touch(&at.plus_as_string("1\ntest-zero"));
at.touch(at.plus_as_string("1\ntest-zero"));

let ignored_opts = [
"--quoting-style=c",
Expand Down Expand Up @@ -933,9 +933,9 @@ fn test_ls_zero() {
fn test_ls_commas_trailing() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-commas-trailing-2"));
at.touch(at.plus_as_string("test-commas-trailing-2"));

at.touch(&at.plus_as_string("test-commas-trailing-1"));
at.touch(at.plus_as_string("test-commas-trailing-1"));
at.append(
"test-commas-trailing-1",
&(0..2000)
Expand All @@ -957,7 +957,7 @@ fn test_ls_commas_trailing() {
fn test_ls_long() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-long"));
at.touch(at.plus_as_string("test-long"));

for arg in LONG_ARGS {
let result = scene.ucmd().arg(arg).arg("test-long").succeeds();
Expand All @@ -975,7 +975,7 @@ fn test_ls_long_format() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.mkdir(&at.plus_as_string("test-long-dir"));
at.touch(&at.plus_as_string("test-long-dir/test-long-file"));
at.touch(at.plus_as_string("test-long-dir/test-long-file"));
at.mkdir(&at.plus_as_string("test-long-dir/test-long-dir"));

for arg in LONG_ARGS {
Expand Down Expand Up @@ -1231,9 +1231,9 @@ fn test_ls_long_symlink_color() {
fn test_ls_long_total_size() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-long"));
at.touch(at.plus_as_string("test-long"));
at.append("test-long", "1");
at.touch(&at.plus_as_string("test-long2"));
at.touch(at.plus_as_string("test-long2"));
at.append("test-long2", "2");

let expected_prints: HashMap<_, _> = if cfg!(unix) {
Expand Down Expand Up @@ -1275,7 +1275,7 @@ fn test_ls_long_total_size() {
fn test_ls_long_formats() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-long-formats"));
at.touch(at.plus_as_string("test-long-formats"));

// Zero or one "." for indicating a file with security context

Expand Down Expand Up @@ -1422,8 +1422,8 @@ fn test_ls_long_formats() {
fn test_ls_oneline() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch(&at.plus_as_string("test-oneline-1"));
at.touch(&at.plus_as_string("test-oneline-2"));
at.touch(at.plus_as_string("test-oneline-1"));
at.touch(at.plus_as_string("test-oneline-2"));

// Bit of a weird situation: in the tests oneline and columns have the same output,
// except on Windows.
Expand All @@ -1443,7 +1443,7 @@ fn test_ls_deref() {
let path_regexp = r"(.*)test-long.link -> (.*)test-long(.*)";
let re = Regex::new(path_regexp).unwrap();

at.touch(&at.plus_as_string("test-long"));
at.touch(at.plus_as_string("test-long"));
at.symlink_file("test-long", "test-long.link");
assert!(at.is_symlink("test-long.link"));

Expand Down Expand Up @@ -1808,8 +1808,8 @@ fn test_ls_files_dirs() {
at.mkdir("a/b");
at.mkdir("a/b/c");
at.mkdir("z");
at.touch(&at.plus_as_string("a/a"));
at.touch(&at.plus_as_string("a/b/b"));
at.touch(at.plus_as_string("a/a"));
at.touch(at.plus_as_string("a/b/b"));

scene.ucmd().arg("a").succeeds();
scene.ucmd().arg("a/a").succeeds();
Expand Down Expand Up @@ -1840,8 +1840,8 @@ fn test_ls_recursive() {
at.mkdir("a/b");
at.mkdir("a/b/c");
at.mkdir("z");
at.touch(&at.plus_as_string("a/a"));
at.touch(&at.plus_as_string("a/b/b"));
at.touch(at.plus_as_string("a/a"));
at.touch(at.plus_as_string("a/b/b"));

scene.ucmd().arg("a").succeeds();
scene.ucmd().arg("a/a").succeeds();
Expand Down Expand Up @@ -1880,7 +1880,7 @@ fn test_ls_color() {
.join("nested_file")
.to_string_lossy()
.to_string();
at.touch(&nested_file);
at.touch(nested_file);
at.touch("test-color");

let a_with_colors = "\x1b[1;34ma\x1b[0m";
Expand Down Expand Up @@ -1985,7 +1985,7 @@ fn test_ls_indicator_style() {
at.mkdir("directory");
assert!(at.dir_exists("directory"));

at.touch(&at.plus_as_string("link-src"));
at.touch(at.plus_as_string("link-src"));
at.symlink_file("link-src", "link-dest.link");
assert!(at.is_symlink("link-dest.link"));

Expand Down Expand Up @@ -2077,7 +2077,7 @@ fn test_ls_indicator_style() {
at.mkdir("directory");
assert!(at.dir_exists("directory"));

at.touch(&at.plus_as_string("link-src"));
at.touch(at.plus_as_string("link-src"));
at.symlink_file("link-src", "link-dest.link");
assert!(at.is_symlink("link-dest.link"));

Expand Down
Loading

0 comments on commit 5cfc48e

Please sign in to comment.