2024-01-04 03:57:28 +08:00
|
|
|
use crate::tests::prelude::*;
|
2023-12-10 04:02:14 +08:00
|
|
|
use libc::{c_void, O_CREAT, O_RDWR, O_TRUNC, SEEK_SET};
|
|
|
|
use rand::random;
|
2024-01-04 04:12:13 +08:00
|
|
|
use std::{ffi::CString, ptr};
|
2023-12-10 04:02:14 +08:00
|
|
|
|
|
|
|
use crate::fallback::fish_mkstemp_cloexec;
|
|
|
|
|
2023-06-28 01:05:55 +08:00
|
|
|
use super::*;
|
|
|
|
|
2023-07-02 04:37:44 +08:00
|
|
|
#[test]
|
|
|
|
fn test_wdirname_wbasename() {
|
|
|
|
// path, dir, base
|
|
|
|
struct Test(&'static wstr, &'static wstr, &'static wstr);
|
|
|
|
const testcases: &[Test] = &[
|
2024-01-13 02:10:56 +08:00
|
|
|
Test(L!(""), L!("."), L!(".")),
|
|
|
|
Test(L!("foo//"), L!("."), L!("foo")),
|
|
|
|
Test(L!("foo//////"), L!("."), L!("foo")),
|
|
|
|
Test(L!("/////foo"), L!("/"), L!("foo")),
|
|
|
|
Test(L!("//foo/////bar"), L!("//foo"), L!("bar")),
|
|
|
|
Test(L!("foo/////bar"), L!("foo"), L!("bar")),
|
2023-07-02 04:37:44 +08:00
|
|
|
// Examples given in XPG4.2.
|
2024-01-13 02:10:56 +08:00
|
|
|
Test(L!("/usr/lib"), L!("/usr"), L!("lib")),
|
|
|
|
Test(L!("usr"), L!("."), L!("usr")),
|
|
|
|
Test(L!("/"), L!("/"), L!("/")),
|
|
|
|
Test(L!("."), L!("."), L!(".")),
|
|
|
|
Test(L!(".."), L!("."), L!("..")),
|
2023-07-02 04:37:44 +08:00
|
|
|
];
|
2023-06-28 01:05:55 +08:00
|
|
|
|
2023-07-02 04:37:44 +08:00
|
|
|
for tc in testcases {
|
|
|
|
let Test(path, tc_dir, tc_base) = *tc;
|
2023-07-02 16:10:29 +08:00
|
|
|
let dir = wdirname(path);
|
2023-07-02 04:37:44 +08:00
|
|
|
assert_eq!(
|
|
|
|
dir, tc_dir,
|
|
|
|
"\npath: {:?}, dir: {:?}, tc.dir: {:?}",
|
|
|
|
path, dir, tc_dir
|
|
|
|
);
|
2023-06-28 01:05:55 +08:00
|
|
|
|
2023-07-02 16:10:29 +08:00
|
|
|
let base = wbasename(path);
|
2023-07-02 04:37:44 +08:00
|
|
|
assert_eq!(
|
|
|
|
base, tc_base,
|
|
|
|
"\npath: {:?}, base: {:?}, tc.base: {:?}",
|
|
|
|
path, base, tc_base
|
|
|
|
);
|
|
|
|
}
|
2023-06-28 01:05:55 +08:00
|
|
|
|
2023-07-02 04:37:44 +08:00
|
|
|
// Ensure strings which greatly exceed PATH_MAX still work (#7837).
|
|
|
|
const PATH_MAX: usize = libc::PATH_MAX as usize;
|
|
|
|
let mut longpath = WString::new();
|
|
|
|
longpath.reserve(PATH_MAX * 2 + 10);
|
|
|
|
while longpath.char_count() <= PATH_MAX * 2 {
|
|
|
|
longpath.push_str("/overlong");
|
|
|
|
}
|
|
|
|
let last_slash = longpath.chars().rposition(|c| c == '/').unwrap();
|
|
|
|
let longpath_dir = &longpath[..last_slash];
|
|
|
|
assert_eq!(wdirname(&longpath), longpath_dir);
|
2024-01-13 02:10:56 +08:00
|
|
|
assert_eq!(wbasename(&longpath), L!("overlong"));
|
2023-06-28 01:05:55 +08:00
|
|
|
}
|
2023-12-10 04:02:14 +08:00
|
|
|
|
2024-01-04 03:57:28 +08:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn test_wwrite_to_fd() {
|
2024-03-24 18:55:03 +08:00
|
|
|
let _cleanup = test_init();
|
2024-03-06 04:29:31 +08:00
|
|
|
let (_fd, filename) =
|
|
|
|
fish_mkstemp_cloexec(CString::new("/tmp/fish_test_wwrite.XXXXXX").unwrap()).unwrap();
|
2024-01-02 04:29:05 +08:00
|
|
|
let sizes = [1, 2, 3, 5, 13, 23, 64, 128, 255, 4096, 4096 * 2];
|
2023-12-10 04:02:14 +08:00
|
|
|
for &size in &sizes {
|
|
|
|
let fd = AutoCloseFd::new(unsafe {
|
|
|
|
libc::open(filename.as_ptr(), O_RDWR | O_TRUNC | O_CREAT, 0o666)
|
|
|
|
});
|
|
|
|
assert!(fd.is_valid());
|
|
|
|
let mut input = WString::new();
|
|
|
|
for _i in 0..size {
|
|
|
|
input.push(random());
|
|
|
|
}
|
|
|
|
|
|
|
|
let amt = wwrite_to_fd(&input, fd.fd()).unwrap();
|
|
|
|
let narrow = wcs2string(&input);
|
|
|
|
assert_eq!(amt, narrow.len());
|
|
|
|
|
|
|
|
assert!(unsafe { libc::lseek(fd.fd(), 0, SEEK_SET) } >= 0);
|
|
|
|
|
|
|
|
let mut contents = vec![0u8; narrow.len()];
|
|
|
|
let read_amt = unsafe {
|
|
|
|
libc::read(
|
|
|
|
fd.fd(),
|
2024-01-04 04:12:13 +08:00
|
|
|
if size == 0 {
|
|
|
|
ptr::null_mut()
|
|
|
|
} else {
|
|
|
|
(&mut contents[0]) as *mut u8 as *mut c_void
|
|
|
|
},
|
2023-12-10 04:02:14 +08:00
|
|
|
narrow.len(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
assert!(usize::try_from(read_amt).unwrap() == narrow.len());
|
|
|
|
assert_eq!(&contents, &narrow);
|
|
|
|
}
|
|
|
|
unsafe { libc::remove(filename.as_ptr()) };
|
2024-01-04 03:57:28 +08:00
|
|
|
}
|