summaryrefslogtreecommitdiffstats
path: root/vendor/io-lifetimes/tests
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/io-lifetimes/tests')
-rw-r--r--vendor/io-lifetimes/tests/api.rs146
-rw-r--r--vendor/io-lifetimes/tests/assumptions.rs45
-rw-r--r--vendor/io-lifetimes/tests/ffi.rs80
-rw-r--r--vendor/io-lifetimes/tests/niche-optimizations.rs71
4 files changed, 342 insertions, 0 deletions
diff --git a/vendor/io-lifetimes/tests/api.rs b/vendor/io-lifetimes/tests/api.rs
new file mode 100644
index 000000000..19f65c440
--- /dev/null
+++ b/vendor/io-lifetimes/tests/api.rs
@@ -0,0 +1,146 @@
+#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
+#![cfg(feature = "close")]
+#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
+
+use io_lifetimes::raw::{AsRawFilelike, AsRawSocketlike};
+use io_lifetimes::views::{FilelikeView, SocketlikeView};
+use io_lifetimes::{
+ AsFilelike, AsSocketlike, BorrowedFilelike, FromFilelike, FromSocketlike, IntoFilelike,
+ IntoSocketlike,
+};
+use std::io::{Read, Write};
+
+struct Tester {}
+impl Tester {
+ fn use_file<Filelike: AsFilelike>(filelike: Filelike) {
+ let mut buf = Vec::new();
+
+ let filelike = filelike.as_filelike();
+
+ let view = filelike.as_filelike_view::<std::fs::File>();
+ let _ = (&*view).read(&mut buf).is_ok();
+ let _ = (&*view).write(&buf).is_ok();
+
+ let view = unsafe {
+ FilelikeView::<std::fs::File>::view_raw(
+ filelike
+ .as_filelike_view::<std::fs::File>()
+ .as_raw_filelike(),
+ )
+ };
+ let _ = (&*view).read(&mut buf).is_ok();
+ let _ = (&*view).write(&buf).is_ok();
+
+ let _ = dbg!(filelike);
+ }
+
+ fn use_socket<Socketlike: AsSocketlike>(socketlike: Socketlike) {
+ let mut buf = Vec::new();
+
+ let socketlike = socketlike.as_socketlike();
+ let view = socketlike.as_socketlike_view::<std::net::TcpStream>();
+ let _ = (&*view).read(&mut buf).is_ok();
+ let _ = (&*view).write(&buf).is_ok();
+
+ let view = unsafe {
+ SocketlikeView::<std::net::TcpStream>::view_raw(
+ socketlike
+ .as_socketlike_view::<std::net::TcpStream>()
+ .as_raw_socketlike(),
+ )
+ };
+ let _ = (&*view).read(&mut buf).is_ok();
+ let _ = (&*view).write(&buf).is_ok();
+
+ let _ = dbg!(socketlike);
+ }
+
+ fn from_file<Filelike: IntoFilelike>(filelike: Filelike) {
+ let mut buf = Vec::new();
+
+ let filelike = filelike.into_filelike();
+ let view = filelike.as_filelike_view::<std::fs::File>();
+ let _ = (&*view).read(&mut buf).is_ok();
+ let _ = (&*view).write(&buf).is_ok();
+ drop(view);
+
+ let _ = dbg!(&filelike);
+ let _ = std::fs::File::from_filelike(filelike);
+ }
+
+ fn from_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike) {
+ let mut buf = Vec::new();
+
+ let socketlike = socketlike.into_socketlike();
+ let view = socketlike.as_socketlike_view::<std::net::TcpStream>();
+ let _ = (&*view).read(&mut buf).is_ok();
+ let _ = (&*view).write(&buf).is_ok();
+ drop(view);
+
+ let _ = dbg!(&socketlike);
+ let _ = std::net::TcpStream::from_socketlike(socketlike);
+ }
+
+ fn from_into_file<Filelike: IntoFilelike>(filelike: Filelike) {
+ let _ = std::fs::File::from_into_filelike(filelike);
+ }
+
+ fn from_into_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike) {
+ let _ = std::net::TcpStream::from_into_socketlike(socketlike);
+ }
+}
+
+#[test]
+fn test_api() {
+ let file = std::fs::File::open("Cargo.toml").unwrap();
+ Tester::use_file(&file);
+ Tester::use_file(file.as_filelike());
+ Tester::use_file(&*file.as_filelike_view::<std::fs::File>());
+ Tester::use_file(file.as_filelike_view::<std::fs::File>().as_filelike());
+
+ let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ Tester::use_socket(&socket);
+ Tester::use_socket(socket.as_socketlike());
+ Tester::use_socket(&*socket.as_socketlike_view::<std::net::TcpListener>());
+ Tester::use_socket(
+ socket
+ .as_socketlike_view::<std::net::TcpListener>()
+ .as_socketlike(),
+ );
+
+ Tester::from_file(std::fs::File::open("Cargo.toml").unwrap().into_filelike());
+ Tester::from_file(
+ std::fs::File::open("Cargo.toml")
+ .unwrap()
+ .into_filelike()
+ .into_filelike(),
+ );
+ Tester::from_socket(
+ std::net::TcpListener::bind("127.0.0.1:0")
+ .unwrap()
+ .into_socketlike(),
+ );
+ Tester::from_socket(
+ std::net::TcpListener::bind("127.0.0.1:0")
+ .unwrap()
+ .into_socketlike()
+ .into_socketlike(),
+ );
+
+ Tester::from_into_file(std::fs::File::open("Cargo.toml").unwrap().into_filelike());
+ Tester::from_into_socket(
+ std::net::TcpListener::bind("127.0.0.1:0")
+ .unwrap()
+ .into_socketlike(),
+ );
+}
+
+#[test]
+fn test_as() {
+ let file = std::fs::File::open("Cargo.toml").unwrap();
+ let borrow: BorrowedFilelike = file.as_filelike();
+ let reborrow: BorrowedFilelike = borrow.as_filelike();
+ let ref_reborrow: &BorrowedFilelike = &reborrow;
+ let borrow_ref_reborrow: BorrowedFilelike = ref_reborrow.as_filelike();
+ let _ref_borrow_ref_reborrow: &BorrowedFilelike = &borrow_ref_reborrow;
+}
diff --git a/vendor/io-lifetimes/tests/assumptions.rs b/vendor/io-lifetimes/tests/assumptions.rs
new file mode 100644
index 000000000..33094b653
--- /dev/null
+++ b/vendor/io-lifetimes/tests/assumptions.rs
@@ -0,0 +1,45 @@
+#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
+
+#[cfg(any(not(windows), feature = "close"))]
+use std::mem::size_of;
+
+#[cfg(unix)]
+#[test]
+fn test_assumptions() {
+ assert_eq!(size_of::<std::os::unix::io::RawFd>(), size_of::<i32>());
+ assert_eq!(
+ size_of::<std::os::unix::io::RawFd>(),
+ size_of::<std::os::raw::c_int>()
+ );
+}
+
+#[cfg(target_os = "wasi")]
+#[test]
+fn test_assumptions() {
+ assert_eq!(size_of::<std::os::wasi::io::RawFd>(), size_of::<i32>());
+ assert_eq!(
+ size_of::<std::os::wasi::io::RawFd>(),
+ size_of::<std::os::raw::c_int>()
+ );
+}
+
+#[cfg(all(windows, feature = "close"))]
+#[test]
+fn test_assumptions() {
+ assert_eq!(
+ size_of::<std::os::windows::io::RawSocket>(),
+ size_of::<windows_sys::Win32::Networking::WinSock::SOCKET>()
+ );
+ assert_eq!(
+ size_of::<std::os::windows::io::RawHandle>(),
+ size_of::<windows_sys::Win32::Foundation::HANDLE>()
+ );
+ assert_eq!(
+ windows_sys::Win32::Networking::WinSock::INVALID_SOCKET,
+ usize::MAX
+ );
+ assert_ne!(
+ windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE,
+ std::ptr::null_mut() as std::os::windows::io::RawHandle as _
+ );
+}
diff --git a/vendor/io-lifetimes/tests/ffi.rs b/vendor/io-lifetimes/tests/ffi.rs
new file mode 100644
index 000000000..af03efe05
--- /dev/null
+++ b/vendor/io-lifetimes/tests/ffi.rs
@@ -0,0 +1,80 @@
+#![cfg_attr(not(rustc_attrs), allow(unused_imports))]
+#![cfg(feature = "close")]
+#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
+
+#[cfg(any(unix, windows))]
+use io_lifetimes::example_ffi::*;
+#[cfg(windows)]
+use io_lifetimes::{InvalidHandleError, OwnedHandle};
+#[cfg(windows)]
+use std::{convert::TryInto, os::windows::io::RawHandle, ptr::null_mut};
+#[cfg(windows)]
+use windows_sys::Win32::Storage::FileSystem::{
+ FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, OPEN_EXISTING,
+};
+
+#[cfg(all(rustc_attrs, unix))]
+#[test]
+fn test_file_not_found() {
+ assert!(unsafe {
+ open(
+ "/dev/no/such/file\0".as_ptr() as *const _,
+ O_RDONLY | O_CLOEXEC,
+ )
+ }
+ .is_none());
+}
+
+#[cfg(windows)]
+#[test]
+fn test_file_not_found() {
+ let handle: Result<OwnedHandle, InvalidHandleError> = unsafe {
+ CreateFileW(
+ [
+ 'C' as u16, ':' as _, '/' as _, 'n' as _, 'o' as _, '/' as _, 's' as _, 'u' as _,
+ 'c' as _, 'h' as _, '/' as _, 'f' as _, 'i' as _, 'l' as _, 'e' as _, 0,
+ ]
+ .as_ptr(),
+ FILE_GENERIC_READ,
+ 0,
+ null_mut(),
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ null_mut() as RawHandle as HANDLE,
+ )
+ }
+ .try_into();
+ assert!(handle.is_err());
+ assert_eq!(
+ std::io::Error::last_os_error().kind(),
+ std::io::ErrorKind::NotFound
+ );
+}
+
+#[cfg(all(rustc_attrs, unix))]
+#[test]
+fn test_file_found() {
+ assert!(unsafe { open("Cargo.toml\0".as_ptr() as *const _, O_RDONLY | O_CLOEXEC) }.is_some());
+}
+
+#[cfg(windows)]
+#[test]
+fn test_file_found() {
+ let handle: Result<OwnedHandle, InvalidHandleError> = unsafe {
+ CreateFileW(
+ [
+ 'C' as u16, 'a' as _, 'r' as _, 'g' as _, 'o' as _, '.' as _, 't' as _, 'o' as _,
+ 'm' as _, 'l' as _, 0,
+ ]
+ .as_ptr(),
+ FILE_GENERIC_READ,
+ 0,
+ null_mut(),
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ null_mut() as RawHandle as HANDLE,
+ )
+ }
+ .try_into();
+ assert!(handle.is_ok());
+}
diff --git a/vendor/io-lifetimes/tests/niche-optimizations.rs b/vendor/io-lifetimes/tests/niche-optimizations.rs
new file mode 100644
index 000000000..89316d559
--- /dev/null
+++ b/vendor/io-lifetimes/tests/niche-optimizations.rs
@@ -0,0 +1,71 @@
+#![cfg_attr(not(rustc_attrs), allow(unused_imports))]
+#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
+#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
+
+use std::mem::size_of;
+
+#[cfg(any(unix, target_os = "wasi"))]
+use io_lifetimes::{BorrowedFd, OwnedFd};
+#[cfg(windows)]
+use io_lifetimes::{BorrowedSocket, OwnedSocket};
+
+#[cfg(unix)]
+use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
+#[cfg(target_os = "wasi")]
+use std::os::wasi::io::{FromRawSocket, IntoRawSocket, RawFd};
+#[cfg(windows)]
+use std::os::windows::io::{FromRawSocket, IntoRawSocket, RawSocket};
+
+#[cfg(all(rustc_attrs, any(unix, target_os = "wasi")))]
+#[test]
+fn test_niche_optimizations() {
+ assert_eq!(size_of::<Option<OwnedFd>>(), size_of::<RawFd>());
+ assert_eq!(size_of::<Option<BorrowedFd<'static>>>(), size_of::<RawFd>());
+ unsafe {
+ assert_eq!(OwnedFd::from_raw_fd(RawFd::MIN).into_raw_fd(), RawFd::MIN);
+ assert_eq!(OwnedFd::from_raw_fd(RawFd::MAX).into_raw_fd(), RawFd::MAX);
+ assert_eq!(
+ Some(OwnedFd::from_raw_fd(RawFd::MIN))
+ .unwrap()
+ .into_raw_fd(),
+ RawFd::MIN
+ );
+ assert_eq!(
+ Some(OwnedFd::from_raw_fd(RawFd::MAX))
+ .unwrap()
+ .into_raw_fd(),
+ RawFd::MAX
+ );
+ }
+}
+
+#[cfg(all(rustc_attrs, windows))]
+#[test]
+fn test_niche_optimizations_socket() {
+ assert_eq!(size_of::<Option<OwnedSocket>>(), size_of::<RawSocket>());
+ assert_eq!(
+ size_of::<Option<BorrowedSocket<'static>>>(),
+ size_of::<RawSocket>(),
+ );
+ unsafe {
+ #[cfg(target_pointer_width = "32")]
+ let (min, max) = (i32::MIN as u32, i32::MAX as u32);
+ #[cfg(target_pointer_width = "64")]
+ let (min, max) = (i64::MIN as u64, i64::MAX as u64);
+
+ assert_eq!(OwnedSocket::from_raw_socket(min).into_raw_socket(), min);
+ assert_eq!(OwnedSocket::from_raw_socket(max).into_raw_socket(), max);
+ assert_eq!(
+ Some(OwnedSocket::from_raw_socket(min))
+ .unwrap()
+ .into_raw_socket(),
+ min
+ );
+ assert_eq!(
+ Some(OwnedSocket::from_raw_socket(max))
+ .unwrap()
+ .into_raw_socket(),
+ max
+ );
+ }
+}