summaryrefslogtreecommitdiffstats
path: root/library/std/src/fs/tests.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /library/std/src/fs/tests.rs
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/src/fs/tests.rs')
-rw-r--r--library/std/src/fs/tests.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs
index d74f0f00e..547a7b705 100644
--- a/library/std/src/fs/tests.rs
+++ b/library/std/src/fs/tests.rs
@@ -1707,3 +1707,89 @@ fn test_file_times() {
assert_eq!(metadata.created().unwrap(), created);
}
}
+
+#[test]
+#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos"))]
+fn test_file_times_pre_epoch_with_nanos() {
+ #[cfg(target_os = "ios")]
+ use crate::os::ios::fs::FileTimesExt;
+ #[cfg(target_os = "macos")]
+ use crate::os::macos::fs::FileTimesExt;
+ #[cfg(target_os = "tvos")]
+ use crate::os::tvos::fs::FileTimesExt;
+ #[cfg(target_os = "watchos")]
+ use crate::os::watchos::fs::FileTimesExt;
+
+ let tmp = tmpdir();
+ let file = File::create(tmp.join("foo")).unwrap();
+
+ for (accessed, modified, created) in [
+ // The first round is to set filetimes to something we know works, but this time
+ // it's validated with nanoseconds as well which probe the numeric boundary.
+ (
+ SystemTime::UNIX_EPOCH + Duration::new(12345, 1),
+ SystemTime::UNIX_EPOCH + Duration::new(54321, 100_000_000),
+ SystemTime::UNIX_EPOCH + Duration::new(32123, 999_999_999),
+ ),
+ // The second rounds uses pre-epoch dates along with nanoseconds that probe
+ // the numeric boundary.
+ (
+ SystemTime::UNIX_EPOCH - Duration::new(1, 1),
+ SystemTime::UNIX_EPOCH - Duration::new(60, 100_000_000),
+ SystemTime::UNIX_EPOCH - Duration::new(3600, 999_999_999),
+ ),
+ ] {
+ let mut times = FileTimes::new();
+ times = times.set_accessed(accessed).set_modified(modified).set_created(created);
+ file.set_times(times).unwrap();
+
+ let metadata = file.metadata().unwrap();
+ assert_eq!(metadata.accessed().unwrap(), accessed);
+ assert_eq!(metadata.modified().unwrap(), modified);
+ assert_eq!(metadata.created().unwrap(), created);
+ }
+}
+
+#[test]
+#[cfg(windows)]
+fn windows_unix_socket_exists() {
+ use crate::sys::{c, net};
+ use crate::{mem, ptr};
+
+ let tmp = tmpdir();
+ let socket_path = tmp.join("socket");
+
+ // std doesn't currently support Unix sockets on Windows so manually create one here.
+ net::init();
+ unsafe {
+ let socket = c::WSASocketW(
+ c::AF_UNIX as i32,
+ c::SOCK_STREAM,
+ 0,
+ ptr::null_mut(),
+ 0,
+ c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
+ );
+ // AF_UNIX is not supported on earlier versions of Windows,
+ // so skip this test if it's unsupported and we're not in CI.
+ if socket == c::INVALID_SOCKET {
+ let error = c::WSAGetLastError();
+ if env::var_os("CI").is_none() && error == c::WSAEAFNOSUPPORT {
+ return;
+ } else {
+ panic!("Creating AF_UNIX socket failed (OS error {error})");
+ }
+ }
+ let mut addr = c::SOCKADDR_UN { sun_family: c::AF_UNIX, sun_path: mem::zeroed() };
+ let bytes = socket_path.as_os_str().as_encoded_bytes();
+ addr.sun_path[..bytes.len()].copy_from_slice(bytes);
+ let len = mem::size_of_val(&addr) as i32;
+ let result = c::bind(socket, ptr::addr_of!(addr).cast::<c::SOCKADDR>(), len);
+ c::closesocket(socket);
+ assert_eq!(result, 0);
+ }
+ // Make sure all ways of testing a file exist work for a Unix socket.
+ assert_eq!(socket_path.exists(), true);
+ assert_eq!(socket_path.try_exists().unwrap(), true);
+ assert_eq!(socket_path.metadata().is_ok(), true);
+}