1 // ignore-windows: File handling is not implemented yet
2 // compile-flags: -Zmiri-disable-isolation
4 use std::fs::{File, create_dir, remove_dir, remove_dir_all, remove_file, rename};
5 use std::io::{Read, Write, ErrorKind, Result, Seek, SeekFrom};
6 use std::path::{PathBuf, Path};
19 /// Prepare: compute filename and make sure the file does not exist.
20 fn prepare(filename: &str) -> PathBuf {
21 let tmp = std::env::temp_dir();
22 let path = tmp.join(filename);
23 // Clean the paths for robustness.
24 remove_file(&path).ok();
28 /// Prepare directory: compute directory name and make sure it does not exist.
29 fn prepare_dir(dirname: &str) -> PathBuf {
30 let tmp = std::env::temp_dir();
31 let path = tmp.join(&dirname);
32 // Clean the directory for robustness.
33 remove_dir_all(&path).ok();
37 /// Prepare like above, and also write some initial content to the file.
38 fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
39 let path = prepare(filename);
40 let mut file = File::create(&path).unwrap();
41 file.write(content).unwrap();
46 let bytes = b"Hello, World!\n";
47 let path = prepare("miri_test_fs_file.txt");
49 // Test creating, writing and closing a file (closing is tested when `file` is dropped).
50 let mut file = File::create(&path).unwrap();
51 // Writing 0 bytes should not change the file contents.
52 file.write(&mut []).unwrap();
53 assert_eq!(file.metadata().unwrap().len(), 0);
55 file.write(bytes).unwrap();
56 assert_eq!(file.metadata().unwrap().len(), bytes.len() as u64);
57 // Test opening, reading and closing a file.
58 let mut file = File::open(&path).unwrap();
59 let mut contents = Vec::new();
60 // Reading 0 bytes should not move the file pointer.
61 file.read(&mut []).unwrap();
62 // Reading until EOF should get the whole text.
63 file.read_to_end(&mut contents).unwrap();
64 assert_eq!(bytes, contents.as_slice());
66 // Removing file should succeed.
67 remove_file(&path).unwrap();
70 fn test_file_clone() {
71 let bytes = b"Hello, World!\n";
72 let path = prepare_with_content("miri_test_fs_file_clone.txt", bytes);
74 // Cloning a file should be successful.
75 let file = File::open(&path).unwrap();
76 let mut cloned = file.try_clone().unwrap();
77 // Reading from a cloned file should get the same text.
78 let mut contents = Vec::new();
79 cloned.read_to_end(&mut contents).unwrap();
80 assert_eq!(bytes, contents.as_slice());
82 // Removing file should succeed.
83 remove_file(&path).unwrap();
87 let bytes = b"Hello, entire World!\n";
88 let path = prepare_with_content("miri_test_fs_seek.txt", bytes);
90 let mut file = File::open(&path).unwrap();
91 let mut contents = Vec::new();
92 file.read_to_end(&mut contents).unwrap();
93 assert_eq!(bytes, contents.as_slice());
94 // Test that seeking to the beginning and reading until EOF gets the text again.
95 file.seek(SeekFrom::Start(0)).unwrap();
96 let mut contents = Vec::new();
97 file.read_to_end(&mut contents).unwrap();
98 assert_eq!(bytes, contents.as_slice());
99 // Test seeking relative to the end of the file.
100 file.seek(SeekFrom::End(-1)).unwrap();
101 let mut contents = Vec::new();
102 file.read_to_end(&mut contents).unwrap();
103 assert_eq!(&bytes[bytes.len() - 1..], contents.as_slice());
104 // Test seeking relative to the current position.
105 file.seek(SeekFrom::Start(5)).unwrap();
106 file.seek(SeekFrom::Current(-3)).unwrap();
107 let mut contents = Vec::new();
108 file.read_to_end(&mut contents).unwrap();
109 assert_eq!(&bytes[2..], contents.as_slice());
111 // Removing file should succeed.
112 remove_file(&path).unwrap();
115 fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
116 // Test that the file metadata is correct.
117 let metadata = path.metadata()?;
118 // `path` should point to a file.
119 assert!(metadata.is_file());
120 // The size of the file must be equal to the number of written bytes.
121 assert_eq!(bytes.len() as u64, metadata.len());
126 let bytes = b"Hello, meta-World!\n";
127 let path = prepare_with_content("miri_test_fs_metadata.txt", bytes);
129 // Test that metadata of an absolute path is correct.
130 check_metadata(bytes, &path).unwrap();
131 // Test that metadata of a relative path is correct.
132 std::env::set_current_dir(path.parent().unwrap()).unwrap();
133 check_metadata(bytes, Path::new(path.file_name().unwrap())).unwrap();
135 // Removing file should succeed.
136 remove_file(&path).unwrap();
140 let bytes = b"Hello, World!\n";
141 let path = prepare_with_content("miri_test_fs_link_target.txt", bytes);
142 let symlink_path = prepare("miri_test_fs_symlink.txt");
144 // Creating a symbolic link should succeed.
145 std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
146 // Test that the symbolic link has the same contents as the file.
147 let mut symlink_file = File::open(&symlink_path).unwrap();
148 let mut contents = Vec::new();
149 symlink_file.read_to_end(&mut contents).unwrap();
150 assert_eq!(bytes, contents.as_slice());
151 // Test that metadata of a symbolic link is correct.
152 check_metadata(bytes, &symlink_path).unwrap();
153 // Test that the metadata of a symbolic link is correct when not following it.
154 assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
155 // Removing symbolic link should succeed.
156 remove_file(&symlink_path).unwrap();
158 // Removing file should succeed.
159 remove_file(&path).unwrap();
163 let bytes = b"Hello, World!\n";
164 let path = prepare("miri_test_fs_errors.txt");
166 // The following tests also check that the `__errno_location()` shim is working properly.
167 // Opening a non-existing file should fail with a "not found" error.
168 assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
169 // Removing a non-existing file should fail with a "not found" error.
170 assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());
171 // Reading the metadata of a non-existing file should fail with a "not found" error.
172 assert_eq!(ErrorKind::NotFound, check_metadata(bytes, &path).unwrap_err().kind());
176 // Renaming a file should succeed.
177 let path1 = prepare("miri_test_fs_rename_source.txt");
178 let path2 = prepare("miri_test_fs_rename_destination.txt");
180 let file = File::create(&path1).unwrap();
183 // Renaming should succeed
184 rename(&path1, &path2).unwrap();
185 // Check that the old file path isn't present
186 assert_eq!(ErrorKind::NotFound, path1.metadata().unwrap_err().kind());
187 // Check that the file has moved successfully
188 assert!(path2.metadata().unwrap().is_file());
190 // Renaming a nonexistent file should fail
191 assert_eq!(ErrorKind::NotFound, rename(&path1, &path2).unwrap_err().kind());
193 remove_file(&path2).unwrap();
196 fn test_directory() {
197 let dir_path = prepare_dir("miri_test_fs_dir");
198 // Creating a directory should succeed.
199 create_dir(&dir_path).unwrap();
200 // Test that the metadata of a directory is correct.
201 assert!(dir_path.metadata().unwrap().is_dir());
202 // Deleting the directory should succeed.
203 remove_dir(&dir_path).unwrap();
204 // Reading the metadata of a non-existent file should fail with a "not found" error.
205 assert_eq!(ErrorKind::NotFound, check_metadata(&[], &dir_path).unwrap_err().kind());