Port test_wwrite_to_fd

This commit is contained in:
Johannes Altmanninger 2023-12-09 21:02:14 +01:00
parent d5ccbb6e9c
commit fe19cbded0
3 changed files with 47 additions and 48 deletions

View file

@ -5,6 +5,7 @@ pub mod fileid;
pub mod gettext;
pub mod printf;
#[cfg(test)]
#[allow(unused_imports)] // Easy way to suppress warnings while we have two testing modes.
mod tests;
pub mod wcstod;
pub mod wcstoi;

View file

@ -1,3 +1,10 @@
use crate::ffi_tests::add_test;
use libc::{c_void, O_CREAT, O_RDWR, O_TRUNC, SEEK_SET};
use rand::random;
use std::ffi::CString;
use crate::fallback::fish_mkstemp_cloexec;
use super::*;
#[test]
@ -49,3 +56,42 @@ fn test_wdirname_wbasename() {
assert_eq!(wdirname(&longpath), longpath_dir);
assert_eq!(wbasename(&longpath), "overlong"L);
}
add_test!("test_wwrite_to_fd", || {
let (fd, filename) =
fish_mkstemp_cloexec(CString::new("/tmp/fish_test_wwrite.XXXXXX").unwrap());
{
let mut tmpfd = AutoCloseFd::new(fd);
assert!(tmpfd.is_valid());
tmpfd.close();
}
let sizes = [0, 1, 2, 3, 5, 13, 23, 64, 128, 255, 4096, 4096 * 2];
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(),
(&mut contents[0]) as *mut u8 as *mut c_void,
narrow.len(),
)
};
assert!(usize::try_from(read_amt).unwrap() == narrow.len());
assert_eq!(&contents, &narrow);
}
unsafe { libc::remove(filename.as_ptr()) };
});

View file

@ -1077,53 +1077,6 @@ static void test_input() {
}
}
// todo!("port this")
static void test_wwrite_to_fd() {
say(L"Testing wwrite_to_fd");
char t[] = "/tmp/fish_test_wwrite.XXXXXX";
autoclose_fd_t tmpfd{mkstemp(t)};
if (!tmpfd.valid()) {
err(L"Unable to create temporary file");
return;
}
tmpfd.close();
size_t sizes[] = {0, 1, 2, 3, 5, 13, 23, 64, 128, 255, 4096, 4096 * 2};
for (size_t size : sizes) {
autoclose_fd_t fd{open(t, O_RDWR | O_TRUNC | O_CREAT, 0666)};
if (!fd.valid()) {
wperror(L"open");
err(L"Unable to open temporary file");
return;
}
wcstring input{};
for (size_t i = 0; i < size; i++) {
input.push_back(wchar_t(random()));
}
ssize_t amt = wwrite_to_fd(input, fd.fd());
if (amt < 0) {
wperror(L"write");
err(L"Unable to write to temporary file");
return;
}
std::string narrow = wcs2string(input);
size_t expected_size = narrow.size();
do_test(static_cast<size_t>(amt) == expected_size);
if (lseek(fd.fd(), 0, SEEK_SET) < 0) {
wperror(L"seek");
err(L"Unable to seek temporary file");
return;
}
std::string contents(expected_size, '\0');
ssize_t read_amt = read(fd.fd(), &contents[0], expected_size);
do_test(read_amt >= 0 && static_cast<size_t>(read_amt) == expected_size);
}
(void)remove(t);
}
/// Helper for test_timezone_env_vars().
long return_timezone_hour(time_t tstamp, const wchar_t *timezone) {
env_stack_t vars{parser_principal_parser()->deref().vars_boxed()};
@ -1380,7 +1333,6 @@ struct test_comparator_t {
static const test_t s_tests[]{
{TEST_GROUP("utility_functions"), test_utility_functions},
{TEST_GROUP("dir_iter"), test_dir_iter},
{TEST_GROUP("wwrite_to_fd"), test_wwrite_to_fd},
{TEST_GROUP("env"), test_env_snapshot},
{TEST_GROUP("str_to_num"), test_str_to_num},
{TEST_GROUP("enum"), test_enum_set},