|
1 | 1 | //@ignore-target-windows: No libc on Windows
|
2 | 2 | //@compile-flags: -Zmiri-disable-isolation
|
| 3 | +#![feature(io_error_more)] |
3 | 4 | #![feature(rustc_private)]
|
4 | 5 |
|
5 | 6 | use std::fs::{remove_file, File};
|
6 | 7 | use std::os::unix::io::AsRawFd;
|
| 8 | +use std::path::PathBuf; |
7 | 9 |
|
8 |
| -fn tmp() -> std::path::PathBuf { |
| 10 | +fn tmp() -> PathBuf { |
9 | 11 | std::env::var("MIRI_TEMP")
|
10 |
| - .map(std::path::PathBuf::from) |
| 12 | + .map(|tmp| { |
| 13 | + // MIRI_TEMP is set outside of our emulated |
| 14 | + // program, so it may have path separators that don't |
| 15 | + // correspond to our target platform. We normalize them here |
| 16 | + // before constructing a `PathBuf` |
| 17 | + return PathBuf::from(tmp.replace("\\", "/")); |
| 18 | + }) |
11 | 19 | .unwrap_or_else(|_| std::env::temp_dir())
|
12 | 20 | }
|
13 | 21 |
|
| 22 | +/// Test allocating variant of `realpath`. |
| 23 | +fn test_posix_realpath_alloc() { |
| 24 | + use std::ffi::OsString; |
| 25 | + use std::ffi::{CStr, CString}; |
| 26 | + use std::os::unix::ffi::OsStrExt; |
| 27 | + use std::os::unix::ffi::OsStringExt; |
| 28 | + |
| 29 | + let buf; |
| 30 | + let path = tmp().join("miri_test_libc_posix_realpath_alloc"); |
| 31 | + let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed"); |
| 32 | + |
| 33 | + // Cleanup before test. |
| 34 | + remove_file(&path).ok(); |
| 35 | + // Create file. |
| 36 | + drop(File::create(&path).unwrap()); |
| 37 | + unsafe { |
| 38 | + let r = libc::realpath(c_path.as_ptr(), std::ptr::null_mut()); |
| 39 | + assert!(!r.is_null()); |
| 40 | + buf = CStr::from_ptr(r).to_bytes().to_vec(); |
| 41 | + libc::free(r as *mut _); |
| 42 | + } |
| 43 | + let canonical = PathBuf::from(OsString::from_vec(buf)); |
| 44 | + assert_eq!(path.file_name(), canonical.file_name()); |
| 45 | + |
| 46 | + // Cleanup after test. |
| 47 | + remove_file(&path).unwrap(); |
| 48 | +} |
| 49 | + |
| 50 | +/// Test non-allocating variant of `realpath`. |
| 51 | +fn test_posix_realpath_noalloc() { |
| 52 | + use std::ffi::{CStr, CString}; |
| 53 | + use std::os::unix::ffi::OsStrExt; |
| 54 | + |
| 55 | + let path = tmp().join("miri_test_libc_posix_realpath_noalloc"); |
| 56 | + let c_path = CString::new(path.as_os_str().as_bytes()).expect("CString::new failed"); |
| 57 | + |
| 58 | + let mut v = vec![0; libc::PATH_MAX as usize]; |
| 59 | + |
| 60 | + // Cleanup before test. |
| 61 | + remove_file(&path).ok(); |
| 62 | + // Create file. |
| 63 | + drop(File::create(&path).unwrap()); |
| 64 | + unsafe { |
| 65 | + let r = libc::realpath(c_path.as_ptr(), v.as_mut_ptr()); |
| 66 | + assert!(!r.is_null()); |
| 67 | + } |
| 68 | + let c = unsafe { CStr::from_ptr(v.as_ptr()) }; |
| 69 | + let canonical = PathBuf::from(c.to_str().expect("CStr to str")); |
| 70 | + |
| 71 | + assert_eq!(path.file_name(), canonical.file_name()); |
| 72 | + |
| 73 | + // Cleanup after test. |
| 74 | + remove_file(&path).unwrap(); |
| 75 | +} |
| 76 | + |
| 77 | +/// Test failure cases for `realpath`. |
| 78 | +fn test_posix_realpath_errors() { |
| 79 | + use std::ffi::CString; |
| 80 | + use std::io::ErrorKind; |
| 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 | + |
14 | 91 | #[cfg(any(target_os = "linux", target_os = "freebsd"))]
|
15 | 92 | fn test_posix_fadvise() {
|
16 | 93 | use std::convert::TryInto;
|
@@ -336,6 +413,10 @@ fn main() {
|
336 | 413 |
|
337 | 414 | test_posix_gettimeofday();
|
338 | 415 |
|
| 416 | + test_posix_realpath_alloc(); |
| 417 | + test_posix_realpath_noalloc(); |
| 418 | + test_posix_realpath_errors(); |
| 419 | + |
339 | 420 | #[cfg(any(target_os = "linux"))]
|
340 | 421 | test_sync_file_range();
|
341 | 422 |
|
|
0 commit comments