1 // ignore-windows: File handling is not implemented yet
2 // compile-flags: -Zmiri-disable-isolation
4 use std::fs::{File, remove_file, rename};
5 use std::io::{Read, Write, ErrorKind, Result, Seek, SeekFrom};
6 use std::path::{PathBuf, Path};
8 fn test_metadata(bytes: &[u8], path: &Path) -> Result<()> {
9 // Test that the file metadata is correct.
10 let metadata = path.metadata()?;
11 // `path` should point to a file.
12 assert!(metadata.is_file());
13 // The size of the file must be equal to the number of written bytes.
14 assert_eq!(bytes.len() as u64, metadata.len());
19 let tmp = std::env::temp_dir();
20 let filename = PathBuf::from("miri_test_fs.txt");
21 let path = tmp.join(&filename);
22 let symlink_path = tmp.join("miri_test_fs_symlink.txt");
23 let bytes = b"Hello, World!\n";
24 // Clean the paths for robustness.
25 remove_file(&path).ok();
26 remove_file(&symlink_path).ok();
28 // Test creating, writing and closing a file (closing is tested when `file` is dropped).
29 let mut file = File::create(&path).unwrap();
30 // Writing 0 bytes should not change the file contents.
31 file.write(&mut []).unwrap();
32 assert_eq!(file.metadata().unwrap().len(), 0);
34 file.write(bytes).unwrap();
35 assert_eq!(file.metadata().unwrap().len(), bytes.len() as u64);
36 // Test opening, reading and closing a file.
37 let mut file = File::open(&path).unwrap();
38 let mut contents = Vec::new();
39 // Reading 0 bytes should not move the file pointer.
40 file.read(&mut []).unwrap();
41 // Reading until EOF should get the whole text.
42 file.read_to_end(&mut contents).unwrap();
43 assert_eq!(bytes, contents.as_slice());
45 // Cloning a file should be successful.
46 let file = File::open(&path).unwrap();
47 let mut cloned = file.try_clone().unwrap();
48 // Reading from a cloned file should get the same text.
49 let mut contents = Vec::new();
50 cloned.read_to_end(&mut contents).unwrap();
51 assert_eq!(bytes, contents.as_slice());
53 // Test that seeking to the beginning and reading until EOF gets the text again.
54 file.seek(SeekFrom::Start(0)).unwrap();
55 let mut contents = Vec::new();
56 file.read_to_end(&mut contents).unwrap();
57 assert_eq!(bytes, contents.as_slice());
58 // Test seeking relative to the end of the file.
59 file.seek(SeekFrom::End(-1)).unwrap();
60 let mut contents = Vec::new();
61 file.read_to_end(&mut contents).unwrap();
62 assert_eq!(&bytes[bytes.len() - 1..], contents.as_slice());
63 // Test seeking relative to the current position.
64 file.seek(SeekFrom::Start(5)).unwrap();
65 file.seek(SeekFrom::Current(-3)).unwrap();
66 let mut contents = Vec::new();
67 file.read_to_end(&mut contents).unwrap();
68 assert_eq!(&bytes[2..], contents.as_slice());
70 // Test that metadata of an absolute path is correct.
71 test_metadata(bytes, &path).unwrap();
72 // Test that metadata of a relative path is correct.
73 std::env::set_current_dir(&tmp).unwrap();
74 test_metadata(bytes, &filename).unwrap();
76 // Creating a symbolic link should succeed.
77 std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
78 // Test that the symbolic link has the same contents as the file.
79 let mut symlink_file = File::open(&symlink_path).unwrap();
80 let mut contents = Vec::new();
81 symlink_file.read_to_end(&mut contents).unwrap();
82 assert_eq!(bytes, contents.as_slice());
83 // Test that metadata of a symbolic link is correct.
84 test_metadata(bytes, &symlink_path).unwrap();
85 // Test that the metadata of a symbolic link is correct when not following it.
86 assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
87 // Removing symbolic link should succeed.
88 remove_file(&symlink_path).unwrap();
90 // Removing file should succeed.
91 remove_file(&path).unwrap();
93 // Renaming a file should succeed.
94 let path1 = tmp.join("rename_source.txt");
95 let path2 = tmp.join("rename_destination.txt");
96 // Clean files for robustness.
97 remove_file(&path1).ok();
98 remove_file(&path2).ok();
99 let file = File::create(&path1).unwrap();
101 rename(&path1, &path2).unwrap();
102 assert_eq!(ErrorKind::NotFound, path1.metadata().unwrap_err().kind());
103 assert!(path2.metadata().unwrap().is_file());
104 remove_file(&path2).unwrap();
106 // The two following tests also check that the `__errno_location()` shim is working properly.
107 // Opening a non-existing file should fail with a "not found" error.
108 assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
109 // Removing a non-existing file should fail with a "not found" error.
110 assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());
111 // Reading the metadata of a non-existing file should fail with a "not found" error.
112 assert_eq!(ErrorKind::NotFound, test_metadata(bytes, &path).unwrap_err().kind());