|
1 | 1 | // ignore-windows: No libc on Windows
|
2 | 2 | // compile-flags: -Zmiri-disable-isolation
|
3 | 3 |
|
| 4 | +#![feature(io_error_more)] |
4 | 5 | #![feature(rustc_private)]
|
5 | 6 |
|
6 | 7 | extern crate libc;
|
7 | 8 |
|
8 |
| -#[cfg(any(target_os = "linux", target_os = "freebsd"))] |
9 | 9 | fn tmp() -> std::path::PathBuf {
|
10 | 10 | std::env::var("MIRI_TEMP")
|
11 | 11 | .map(std::path::PathBuf::from)
|
12 | 12 | .unwrap_or_else(|_| std::env::temp_dir())
|
13 | 13 | }
|
14 | 14 |
|
| 15 | +/// Test allocating variant of `realpath`. |
| 16 | +fn test_posix_realpath_alloc() { |
| 17 | + use std::ffi::OsString; |
| 18 | + use std::ffi::{CStr, CString}; |
| 19 | + use std::fs::{remove_file, File}; |
| 20 | + use std::os::unix::ffi::OsStrExt; |
| 21 | + use std::os::unix::ffi::OsStringExt; |
| 22 | + use std::path::PathBuf; |
| 23 | + |
| 24 | + let buf; |
| 25 | + let path = tmp().join("miri_test_libc_posix_realpath_alloc"); |
| 26 | + let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed"); |
| 27 | + |
| 28 | + // Cleanup before test. |
| 29 | + remove_file(&path).ok(); |
| 30 | + // Create file. |
| 31 | + drop(File::create(&path).unwrap()); |
| 32 | + unsafe { |
| 33 | + let r = libc::realpath(c_path.as_ptr(), std::ptr::null_mut()); |
| 34 | + assert!(!r.is_null()); |
| 35 | + buf = CStr::from_ptr(r).to_bytes().to_vec(); |
| 36 | + libc::free(r as *mut _); |
| 37 | + } |
| 38 | + let canonical = PathBuf::from(OsString::from_vec(buf)); |
| 39 | + // Cleanup after test. |
| 40 | + remove_file(&path).unwrap(); |
| 41 | + |
| 42 | + assert_eq!(path.file_name(), canonical.file_name()); |
| 43 | +} |
| 44 | + |
| 45 | +/// Test non-allocating variant of `realpath`. |
| 46 | +fn test_posix_realpath_noalloc() { |
| 47 | + use std::ffi::{CStr, CString}; |
| 48 | + use std::fs::{remove_file, File}; |
| 49 | + use std::os::unix::ffi::OsStrExt; |
| 50 | + use std::path::PathBuf; |
| 51 | + |
| 52 | + let path = tmp().join("miri_test_libc_posix_realpath_noalloc"); |
| 53 | + let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed"); |
| 54 | + |
| 55 | + let mut v = vec![0; libc::PATH_MAX as usize]; |
| 56 | + |
| 57 | + // Cleanup before test. |
| 58 | + remove_file(&path).ok(); |
| 59 | + // Create file. |
| 60 | + drop(File::create(&path).unwrap()); |
| 61 | + unsafe { |
| 62 | + let r = libc::realpath(c_path.as_ptr(), v.as_mut_ptr()); |
| 63 | + assert!(!r.is_null()); |
| 64 | + } |
| 65 | + let c = unsafe { CStr::from_ptr(v.as_ptr()) }; |
| 66 | + let canonical = PathBuf::from(c.to_str().expect("CStr to str")); |
| 67 | + // Cleanup after test. |
| 68 | + remove_file(&path).unwrap(); |
| 69 | + |
| 70 | + assert_eq!(path.file_name(), canonical.file_name()); |
| 71 | +} |
| 72 | + |
| 73 | +/// Test failure cases for `realpath`. |
| 74 | +fn test_posix_realpath_errors() { |
| 75 | + use std::convert::TryInto; |
| 76 | + use std::ffi::CString; |
| 77 | + use std::fs::{create_dir_all, remove_dir, remove_file}; |
| 78 | + use std::io::ErrorKind; |
| 79 | + use std::os::unix::ffi::OsStrExt; |
| 80 | + use std::os::unix::fs::symlink; |
| 81 | + |
| 82 | + // Test non-existent path returns an error. |
| 83 | + let c_path = CString::new("./nothing_to_see_here").expect("CString::new failed"); |
| 84 | + let r = unsafe { libc::realpath(c_path.as_ptr(), std::ptr::null_mut()) }; |
| 85 | + assert!(r.is_null()); |
| 86 | + let e = std::io::Error::last_os_error(); |
| 87 | + assert_eq!(e.raw_os_error(), Some(libc::ENOENT)); |
| 88 | + assert_eq!(e.kind(), ErrorKind::NotFound); |
| 89 | + |
| 90 | + // Test that a long path returns an error. |
| 91 | + // |
| 92 | + // Linux first checks if the path to exists and macos does not. |
| 93 | + // Using an existing path ensures all platforms return `ENAMETOOLONG` given a long path. |
| 94 | + // |
| 95 | + // Rather than creating a bunch of directories, we create two directories containing symlinks. |
| 96 | + // Sadly we can't avoid creating directories via a path like "./././././" or"/../../" as linux |
| 97 | + // appears to collapse "." and ".." before checking path length. |
| 98 | + let path = tmp(); |
| 99 | + |
| 100 | + // The directories we will put symlinks in. |
| 101 | + let x = path.join("x/"); |
| 102 | + let y = path.join("y/"); |
| 103 | + |
| 104 | + // The symlinks in each directory pointing to each other. |
| 105 | + let yx_sym = y.join("x"); |
| 106 | + let xy_sym = x.join("y"); |
| 107 | + |
| 108 | + // Cleanup before test. |
| 109 | + remove_file(&yx_sym).ok(); |
| 110 | + remove_file(&xy_sym).ok(); |
| 111 | + remove_dir(&x).ok(); |
| 112 | + remove_dir(&y).ok(); |
| 113 | + |
| 114 | + // Create directories. |
| 115 | + create_dir_all(&x).expect("dir x"); |
| 116 | + create_dir_all(&y).expect("dir y"); |
| 117 | + |
| 118 | + // Create symlinks between directories. |
| 119 | + symlink(&x, &yx_sym).expect("symlink x"); |
| 120 | + symlink(&y, &xy_sym).expect("symlink y "); |
| 121 | + |
| 122 | + // This pass exists due to the symlinks created above but is too long. |
| 123 | + let too_long = path.join("x/y/".repeat(libc::PATH_MAX.try_into().unwrap())); |
| 124 | + |
| 125 | + let c_path = CString::new(too_long.into_os_string().as_bytes()).expect("CString::new failed"); |
| 126 | + let r = unsafe { libc::realpath(c_path.as_ptr(), std::ptr::null_mut()) }; |
| 127 | + let e = std::io::Error::last_os_error(); |
| 128 | + |
| 129 | + // Cleanup after test. |
| 130 | + remove_file(&yx_sym).ok(); |
| 131 | + remove_file(&xy_sym).ok(); |
| 132 | + remove_dir(&x).ok(); |
| 133 | + remove_dir(&y).ok(); |
| 134 | + |
| 135 | + assert!(r.is_null()); |
| 136 | + assert_eq!(e.raw_os_error(), Some(libc::ENAMETOOLONG)); |
| 137 | + assert_eq!(e.kind(), ErrorKind::InvalidFilename); |
| 138 | +} |
| 139 | + |
15 | 140 | #[cfg(any(target_os = "linux", target_os = "freebsd"))]
|
16 | 141 | fn test_posix_fadvise() {
|
17 | 142 | use std::convert::TryInto;
|
@@ -317,6 +442,10 @@ fn main() {
|
317 | 442 |
|
318 | 443 | test_posix_gettimeofday();
|
319 | 444 |
|
| 445 | + test_posix_realpath_alloc(); |
| 446 | + test_posix_realpath_noalloc(); |
| 447 | + test_posix_realpath_errors(); |
| 448 | + |
320 | 449 | #[cfg(any(target_os = "linux", target_os = "freebsd"))]
|
321 | 450 | test_sync_file_range();
|
322 | 451 |
|
|
0 commit comments