]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/fs.rs
Add shims for mkdir and rmdir
[rust.git] / tests / run-pass / fs.rs
1 // ignore-windows: File handling is not implemented yet
2 // compile-flags: -Zmiri-disable-isolation
3
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};
7
8 fn main() {
9     test_file();
10     test_file_clone();
11     test_seek();
12     test_metadata();
13     test_symlink();
14     test_errors();
15     test_rename();
16     test_directory();
17 }
18
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();
25     path
26 }
27
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();
34     path
35 }
36
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();
42     path
43 }
44
45 fn test_file() {
46     let bytes = b"Hello, World!\n";
47     let path = prepare("miri_test_fs_file.txt");
48
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);
54
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());
65
66     // Removing file should succeed.
67     remove_file(&path).unwrap();
68 }
69
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);
73
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());
81
82     // Removing file should succeed.
83     remove_file(&path).unwrap();
84 }
85
86 fn test_seek() {
87     let bytes = b"Hello, entire World!\n";
88     let path = prepare_with_content("miri_test_fs_seek.txt", bytes);
89
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());
110
111     // Removing file should succeed.
112     remove_file(&path).unwrap();
113 }
114
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());
122     Ok(())
123 }
124
125 fn test_metadata() {
126     let bytes = b"Hello, meta-World!\n";
127     let path = prepare_with_content("miri_test_fs_metadata.txt", bytes);
128
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();
134
135     // Removing file should succeed.
136     remove_file(&path).unwrap();
137 }
138
139 fn test_symlink() {
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");
143
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();
157
158     // Removing file should succeed.
159     remove_file(&path).unwrap();
160 }
161
162 fn test_errors() {
163     let bytes = b"Hello, World!\n";
164     let path = prepare("miri_test_fs_errors.txt");
165
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());
173 }
174
175 fn test_rename() {
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");
179
180     let file = File::create(&path1).unwrap();
181     drop(file);
182
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());
189
190     // Renaming a nonexistent file should fail
191     assert_eq!(ErrorKind::NotFound, rename(&path1, &path2).unwrap_err().kind());
192
193     remove_file(&path2).unwrap();
194 }
195
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());
206 }