test cleanup

This commit is contained in:
Ralf Jung 2024-08-17 10:53:33 +02:00
parent 5402be8f99
commit 5d59bdefb4
2 changed files with 30 additions and 55 deletions

View File

@ -10,35 +10,31 @@ fn main() {
fn test_pipe() { fn test_pipe() {
let mut fds = [-1, -1]; let mut fds = [-1, -1];
let mut res = unsafe { libc::pipe(fds.as_mut_ptr()) }; let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
assert_eq!(res, 0); assert_eq!(res, 0);
// Read size == data available in buffer. // Read size == data available in buffer.
let data = "12345".as_bytes().as_ptr(); let data = "12345".as_bytes().as_ptr();
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
assert_eq!(res, 5); assert_eq!(res, 5);
let mut buf3: [u8; 5] = [0; 5]; let mut buf3: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[0], buf3.as_mut_ptr().cast(), buf3.len() as libc::size_t) };
libc::read(fds[0], buf3.as_mut_ptr().cast(), buf3.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 5); assert_eq!(res, 5);
assert_eq!(buf3, "12345".as_bytes()); assert_eq!(buf3, "12345".as_bytes());
// Read size > data available in buffer. // Read size > data available in buffer.
let data = "123".as_bytes().as_ptr(); let data = "123".as_bytes().as_ptr();
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 3).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 3) };
assert_eq!(res, 3); assert_eq!(res, 3);
let mut buf4: [u8; 5] = [0; 5]; let mut buf4: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[0], buf4.as_mut_ptr().cast(), buf4.len() as libc::size_t) };
libc::read(fds[0], buf4.as_mut_ptr().cast(), buf4.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 3); assert_eq!(res, 3);
assert_eq!(&buf4[0..3], "123".as_bytes()); assert_eq!(&buf4[0..3], "123".as_bytes());
} }
fn test_pipe_threaded() { fn test_pipe_threaded() {
let mut fds = [-1, -1]; let mut fds = [-1, -1];
let mut res = unsafe { libc::pipe(fds.as_mut_ptr()) }; let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
assert_eq!(res, 0); assert_eq!(res, 0);
let thread1 = thread::spawn(move || { let thread1 = thread::spawn(move || {
@ -54,7 +50,7 @@ fn test_pipe_threaded() {
// FIXME: we should yield here once blocking is implemented. // FIXME: we should yield here once blocking is implemented.
//thread::yield_now(); //thread::yield_now();
let data = "abcde".as_bytes().as_ptr(); let data = "abcde".as_bytes().as_ptr();
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
assert_eq!(res, 5); assert_eq!(res, 5);
thread1.join().unwrap(); thread1.join().unwrap();
@ -63,16 +59,13 @@ fn test_pipe_threaded() {
// FIXME: we should yield here once blocking is implemented. // FIXME: we should yield here once blocking is implemented.
//thread::yield_now(); //thread::yield_now();
let data = "12345".as_bytes().as_ptr(); let data = "12345".as_bytes().as_ptr();
let res: i64 = let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() };
assert_eq!(res, 5); assert_eq!(res, 5);
}); });
// FIXME: we should not yield here once blocking is implemented. // FIXME: we should not yield here once blocking is implemented.
thread::yield_now(); thread::yield_now();
let mut buf: [u8; 5] = [0; 5]; let mut buf: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 5); assert_eq!(res, 5);
assert_eq!(buf, "12345".as_bytes()); assert_eq!(buf, "12345".as_bytes());
thread2.join().unwrap(); thread2.join().unwrap();
@ -81,7 +74,7 @@ fn test_pipe_threaded() {
fn test_race() { fn test_race() {
static mut VAL: u8 = 0; static mut VAL: u8 = 0;
let mut fds = [-1, -1]; let mut fds = [-1, -1];
let mut res = unsafe { libc::pipe(fds.as_mut_ptr()) }; let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
assert_eq!(res, 0); assert_eq!(res, 0);
let thread1 = thread::spawn(move || { let thread1 = thread::spawn(move || {
let mut buf: [u8; 1] = [0; 1]; let mut buf: [u8; 1] = [0; 1];
@ -99,7 +92,7 @@ fn test_race() {
}); });
unsafe { VAL = 1 }; unsafe { VAL = 1 };
let data = "a".as_bytes().as_ptr(); let data = "a".as_bytes().as_ptr();
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 1).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 1) };
assert_eq!(res, 1); assert_eq!(res, 1);
thread::yield_now(); thread::yield_now();
thread1.join().unwrap(); thread1.join().unwrap();

View File

@ -10,80 +10,66 @@ fn main() {
fn test_socketpair() { fn test_socketpair() {
let mut fds = [-1, -1]; let mut fds = [-1, -1];
let mut res = let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
assert_eq!(res, 0); assert_eq!(res, 0);
// Read size == data available in buffer. // Read size == data available in buffer.
let data = "abcde".as_bytes().as_ptr(); let data = "abcde".as_bytes().as_ptr();
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() }; let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
assert_eq!(res, 5); assert_eq!(res, 5);
let mut buf: [u8; 5] = [0; 5]; let mut buf: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 5); assert_eq!(res, 5);
assert_eq!(buf, "abcde".as_bytes()); assert_eq!(buf, "abcde".as_bytes());
// Read size > data available in buffer. // Read size > data available in buffer.
let data = "abc".as_bytes().as_ptr(); let data = "abc".as_bytes().as_ptr();
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 3).try_into().unwrap() }; let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 3) };
assert_eq!(res, 3); assert_eq!(res, 3);
let mut buf2: [u8; 5] = [0; 5]; let mut buf2: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[1], buf2.as_mut_ptr().cast(), buf2.len() as libc::size_t) };
libc::read(fds[1], buf2.as_mut_ptr().cast(), buf2.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 3); assert_eq!(res, 3);
assert_eq!(&buf2[0..3], "abc".as_bytes()); assert_eq!(&buf2[0..3], "abc".as_bytes());
// Test read and write from another direction. // Test read and write from another direction.
// Read size == data available in buffer. // Read size == data available in buffer.
let data = "12345".as_bytes().as_ptr(); let data = "12345".as_bytes().as_ptr();
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
assert_eq!(res, 5); assert_eq!(res, 5);
let mut buf3: [u8; 5] = [0; 5]; let mut buf3: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[0], buf3.as_mut_ptr().cast(), buf3.len() as libc::size_t) };
libc::read(fds[0], buf3.as_mut_ptr().cast(), buf3.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 5); assert_eq!(res, 5);
assert_eq!(buf3, "12345".as_bytes()); assert_eq!(buf3, "12345".as_bytes());
// Read size > data available in buffer. // Read size > data available in buffer.
let data = "123".as_bytes().as_ptr(); let data = "123".as_bytes().as_ptr();
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 3).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 3) };
assert_eq!(res, 3); assert_eq!(res, 3);
let mut buf4: [u8; 5] = [0; 5]; let mut buf4: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[0], buf4.as_mut_ptr().cast(), buf4.len() as libc::size_t) };
libc::read(fds[0], buf4.as_mut_ptr().cast(), buf4.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 3); assert_eq!(res, 3);
assert_eq!(&buf4[0..3], "123".as_bytes()); assert_eq!(&buf4[0..3], "123".as_bytes());
// Test when happens when we close one end, with some data in the buffer. // Test when happens when we close one end, with some data in the buffer.
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 3).try_into().unwrap() }; let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 3) };
assert_eq!(res, 3); assert_eq!(res, 3);
unsafe { libc::close(fds[0]) }; unsafe { libc::close(fds[0]) };
// Reading the other end should return that data, then EOF. // Reading the other end should return that data, then EOF.
let mut buf: [u8; 5] = [0; 5]; let mut buf: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 3); assert_eq!(res, 3);
assert_eq!(&buf[0..3], "123".as_bytes()); assert_eq!(&buf[0..3], "123".as_bytes());
res = unsafe { let res = unsafe { libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 0); // 0-sized read: EOF. assert_eq!(res, 0); // 0-sized read: EOF.
// Writing the other end should emit EPIPE. // Writing the other end should emit EPIPE.
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 1).try_into().unwrap() }; let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 1) };
assert_eq!(res, -1); assert_eq!(res, -1);
assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::EPIPE)); assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::EPIPE));
} }
fn test_socketpair_threaded() { fn test_socketpair_threaded() {
let mut fds = [-1, -1]; let mut fds = [-1, -1];
let mut res = let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
assert_eq!(res, 0); assert_eq!(res, 0);
let thread1 = thread::spawn(move || { let thread1 = thread::spawn(move || {
@ -99,7 +85,7 @@ fn test_socketpair_threaded() {
// FIXME: we should yield here once blocking is implemented. // FIXME: we should yield here once blocking is implemented.
//thread::yield_now(); //thread::yield_now();
let data = "abcde".as_bytes().as_ptr(); let data = "abcde".as_bytes().as_ptr();
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5).try_into().unwrap() }; let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 5) };
assert_eq!(res, 5); assert_eq!(res, 5);
thread1.join().unwrap(); thread1.join().unwrap();
@ -108,16 +94,13 @@ fn test_socketpair_threaded() {
// FIXME: we should yield here once blocking is implemented. // FIXME: we should yield here once blocking is implemented.
//thread::yield_now(); //thread::yield_now();
let data = "12345".as_bytes().as_ptr(); let data = "12345".as_bytes().as_ptr();
let res: i64 = let res = unsafe { libc::write(fds[1], data as *const libc::c_void, 5) };
unsafe { libc::write(fds[1], data as *const libc::c_void, 5).try_into().unwrap() };
assert_eq!(res, 5); assert_eq!(res, 5);
}); });
// FIXME: we should not yield here once blocking is implemented. // FIXME: we should not yield here once blocking is implemented.
thread::yield_now(); thread::yield_now();
let mut buf: [u8; 5] = [0; 5]; let mut buf: [u8; 5] = [0; 5];
res = unsafe { let res = unsafe { libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t) };
libc::read(fds[0], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
};
assert_eq!(res, 5); assert_eq!(res, 5);
assert_eq!(buf, "12345".as_bytes()); assert_eq!(buf, "12345".as_bytes());
thread2.join().unwrap(); thread2.join().unwrap();
@ -126,8 +109,7 @@ fn test_socketpair_threaded() {
fn test_race() { fn test_race() {
static mut VAL: u8 = 0; static mut VAL: u8 = 0;
let mut fds = [-1, -1]; let mut fds = [-1, -1];
let mut res = let res = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
assert_eq!(res, 0); assert_eq!(res, 0);
let thread1 = thread::spawn(move || { let thread1 = thread::spawn(move || {
let mut buf: [u8; 1] = [0; 1]; let mut buf: [u8; 1] = [0; 1];
@ -145,7 +127,7 @@ fn test_race() {
}); });
unsafe { VAL = 1 }; unsafe { VAL = 1 };
let data = "a".as_bytes().as_ptr(); let data = "a".as_bytes().as_ptr();
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 1).try_into().unwrap() }; let res = unsafe { libc::write(fds[0], data as *const libc::c_void, 1) };
assert_eq!(res, 1); assert_eq!(res, 1);
thread::yield_now(); thread::yield_now();
thread1.join().unwrap(); thread1.join().unwrap();