diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index e6e8a6ba2ab..3a1e0e4094b 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -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; @@ -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] @@ -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] @@ -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] @@ -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() @@ -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() @@ -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()); @@ -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 @@ -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 @@ -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)); +} diff --git a/tests/by-util/test_ln.rs b/tests/by-util/test_ln.rs index bc51fc10747..1a4c9095b5c 100644 --- a/tests/by-util/test_ln.rs +++ b/tests/by-util/test_ln.rs @@ -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(); diff --git a/tests/common/util.rs b/tests/common/util.rs index ed4ecf8e9ce..b9600e5b686 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -746,14 +746,14 @@ impl AtPath { self.subdir.to_str().unwrap().to_owned() } - pub fn plus(&self, name: &str) -> PathBuf { + pub fn plus>(&self, name: P) -> PathBuf { let mut pathbuf = self.subdir.clone(); pathbuf.push(name); pathbuf } - pub fn plus_as_string(&self, name: &str) -> String { - String::from(self.plus(name).to_str().unwrap()) + pub fn plus_as_string>(&self, name: P) -> String { + self.plus(name).display().to_string() } fn minus(&self, name: &str) -> PathBuf { @@ -876,7 +876,8 @@ impl AtPath { fs::remove_dir(self.plus(dir)).unwrap(); } - pub fn mkdir(&self, dir: &str) { + pub fn mkdir>(&self, dir: P) { + let dir = dir.as_ref(); log_info("mkdir", self.plus_as_string(dir)); fs::create_dir(self.plus(dir)).unwrap(); } @@ -893,7 +894,8 @@ impl AtPath { } } - pub fn touch(&self, file: &str) { + pub fn touch>(&self, file: P) { + let file = file.as_ref(); log_info("touch", self.plus_as_string(file)); File::create(self.plus(file)).unwrap(); } @@ -1016,7 +1018,7 @@ impl AtPath { } } - pub fn file_exists(&self, path: &str) -> bool { + pub fn file_exists>(&self, path: P) -> bool { match fs::metadata(self.plus(path)) { Ok(m) => m.is_file(), Err(_) => false,