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 let mut file = File::open(&path).unwrap();
54 // Test that seeking to the beginning and reading until EOF gets the text again.
55 file.seek(SeekFrom::Start(0)).unwrap();
56 let mut contents = Vec::new();
57 file.read_to_end(&mut contents).unwrap();
58 assert_eq!(bytes, contents.as_slice());
59 // Test seeking relative to the end of the file.
60 file.seek(SeekFrom::End(-1)).unwrap();
61 let mut contents = Vec::new();
62 file.read_to_end(&mut contents).unwrap();
63 assert_eq!(&bytes[bytes.len() - 1..], contents.as_slice());
64 // Test seeking relative to the current position.
65 file.seek(SeekFrom::Start(5)).unwrap();
66 file.seek(SeekFrom::Current(-3)).unwrap();
67 let mut contents = Vec::new();
68 file.read_to_end(&mut contents).unwrap();
69 assert_eq!(&bytes[2..], contents.as_slice());
71 // Test that metadata of an absolute path is correct.
72 test_metadata(bytes, &path).unwrap();
73 // Test that metadata of a relative path is correct.
74 std::env::set_current_dir(&tmp).unwrap();
75 test_metadata(bytes, &filename).unwrap();
77 // Creating a symbolic link should succeed.
78 std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
79 // Test that the symbolic link has the same contents as the file.
80 let mut symlink_file = File::open(&symlink_path).unwrap();
81 let mut contents = Vec::new();
82 symlink_file.read_to_end(&mut contents).unwrap();
83 assert_eq!(bytes, contents.as_slice());
84 // Test that metadata of a symbolic link is correct.
85 test_metadata(bytes, &symlink_path).unwrap();
86 // Test that the metadata of a symbolic link is correct when not following it.
87 assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
88 // Removing symbolic link should succeed.
89 remove_file(&symlink_path).unwrap();
91 // Removing file should succeed.
92 remove_file(&path).unwrap();
94 // Renaming a file should succeed.
95 let path1 = tmp.join("rename_source.txt");
96 let path2 = tmp.join("rename_destination.txt");
97 // Clean files for robustness.
98 remove_file(&path1).ok();
99 remove_file(&path2).ok();
100 let file = File::create(&path1).unwrap();
102 rename(&path1, &path2).unwrap();
103 assert_eq!(ErrorKind::NotFound, path1.metadata().unwrap_err().kind());
104 assert!(path2.metadata().unwrap().is_file());
105 remove_file(&path2).unwrap();
107 // The two following tests also check that the `__errno_location()` shim is working properly.
108 // Opening a non-existing file should fail with a "not found" error.
109 assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
110 // Removing a non-existing file should fail with a "not found" error.
111 assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());
112 // Reading the metadata of a non-existing file should fail with a "not found" error.
113 assert_eq!(ErrorKind::NotFound, test_metadata(bytes, &path).unwrap_err().kind());