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 76ed204
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 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
14 changes: 8 additions & 6 deletions tests/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,14 @@ impl AtPath {
self.subdir.to_str().unwrap().to_owned()
}

pub fn plus(&self, name: &str) -> PathBuf {
pub fn plus<P: AsRef<Path>>(&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<P: AsRef<Path>>(&self, name: P) -> String {
self.plus(name).display().to_string()
}

fn minus(&self, name: &str) -> PathBuf {
Expand Down Expand Up @@ -876,7 +876,8 @@ impl AtPath {
fs::remove_dir(self.plus(dir)).unwrap();
}

pub fn mkdir(&self, dir: &str) {
pub fn mkdir<P: AsRef<Path>>(&self, dir: P) {
let dir = dir.as_ref();
log_info("mkdir", self.plus_as_string(dir));
fs::create_dir(self.plus(dir)).unwrap();
}
Expand All @@ -893,7 +894,8 @@ impl AtPath {
}
}

pub fn touch(&self, file: &str) {
pub fn touch<P: AsRef<Path>>(&self, file: P) {
let file = file.as_ref();
log_info("touch", self.plus_as_string(file));
File::create(self.plus(file)).unwrap();
}
Expand Down Expand Up @@ -1016,7 +1018,7 @@ impl AtPath {
}
}

pub fn file_exists(&self, path: &str) -> bool {
pub fn file_exists<P: AsRef<Path>>(&self, path: P) -> bool {
match fs::metadata(self.plus(path)) {
Ok(m) => m.is_file(),
Err(_) => false,
Expand Down

0 comments on commit 76ed204

Please sign in to comment.