rust/tests/run-pass/fs.rs

199 lines
7.0 KiB
Rust
Raw Normal View History

2019-09-24 17:28:00 -05:00
// ignore-windows: File handling is not implemented yet
// compile-flags: -Zmiri-disable-isolation
2020-01-26 16:45:05 -06:00
use std::fs::{File, remove_file, rename};
2020-01-26 12:36:36 -06:00
use std::io::{Read, Write, ErrorKind, Result, Seek, SeekFrom};
2019-11-30 14:09:52 -06:00
use std::path::{PathBuf, Path};
2020-02-17 22:36:33 -06:00
fn main() {
test_file();
test_file_clone();
test_seek();
test_metadata();
test_symlink();
test_errors();
test_rename();
2019-11-30 14:09:52 -06:00
}
2020-02-17 22:36:33 -06:00
fn test_file() {
2019-11-30 14:09:52 -06:00
let tmp = std::env::temp_dir();
2020-02-17 22:36:33 -06:00
let filename = PathBuf::from("miri_test_fs_file.txt");
2019-11-30 14:09:52 -06:00
let path = tmp.join(&filename);
let bytes = b"Hello, World!\n";
2020-01-07 10:09:07 -06:00
// Clean the paths for robustness.
2020-01-10 11:01:05 -06:00
remove_file(&path).ok();
2019-11-30 14:09:52 -06:00
2019-09-30 11:46:07 -05:00
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
2019-10-11 16:02:04 -05:00
let mut file = File::create(&path).unwrap();
// Writing 0 bytes should not change the file contents.
file.write(&mut []).unwrap();
assert_eq!(file.metadata().unwrap().len(), 0);
file.write(bytes).unwrap();
assert_eq!(file.metadata().unwrap().len(), bytes.len() as u64);
2019-09-30 11:46:07 -05:00
// Test opening, reading and closing a file.
2019-10-11 16:02:04 -05:00
let mut file = File::open(&path).unwrap();
let mut contents = Vec::new();
// Reading 0 bytes should not move the file pointer.
file.read(&mut []).unwrap();
// Reading until EOF should get the whole text.
file.read_to_end(&mut contents).unwrap();
assert_eq!(bytes, contents.as_slice());
2020-01-30 18:43:34 -06:00
2020-02-17 22:36:33 -06:00
// Removing file should succeed.
remove_file(&path).unwrap();
}
fn test_file_clone() {
let tmp = std::env::temp_dir();
let filename = PathBuf::from("miri_test_fs_file_clone.txt");
let path = tmp.join(&filename);
let bytes = b"Hello, World!\n";
// Clean the paths for robustness.
remove_file(&path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
2020-01-30 18:43:34 -06:00
// Cloning a file should be successful.
let file = File::open(&path).unwrap();
let mut cloned = file.try_clone().unwrap();
// Reading from a cloned file should get the same text.
let mut contents = Vec::new();
cloned.read_to_end(&mut contents).unwrap();
assert_eq!(bytes, contents.as_slice());
2019-11-30 14:09:52 -06:00
2020-02-17 22:36:33 -06:00
// Removing file should succeed.
remove_file(&path).unwrap();
}
fn test_seek() {
let tmp = std::env::temp_dir();
let filename = PathBuf::from("miri_test_fs_seek.txt");
let path = tmp.join(&filename);
let bytes = b"Hello, World!\n";
// Clean the paths for robustness.
remove_file(&path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
2020-02-09 14:43:45 -06:00
let mut file = File::open(&path).unwrap();
2020-02-17 22:36:33 -06:00
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
2020-01-26 12:36:36 -06:00
// Test that seeking to the beginning and reading until EOF gets the text again.
file.seek(SeekFrom::Start(0)).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
assert_eq!(bytes, contents.as_slice());
// Test seeking relative to the end of the file.
file.seek(SeekFrom::End(-1)).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
assert_eq!(&bytes[bytes.len() - 1..], contents.as_slice());
// Test seeking relative to the current position.
file.seek(SeekFrom::Start(5)).unwrap();
file.seek(SeekFrom::Current(-3)).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
assert_eq!(&bytes[2..], contents.as_slice());
2020-01-26 12:36:36 -06:00
2020-02-17 22:36:33 -06:00
// Removing file should succeed.
remove_file(&path).unwrap();
}
fn check_metadata(bytes: &[u8], path: &Path) -> Result<()> {
// Test that the file metadata is correct.
let metadata = path.metadata()?;
// `path` should point to a file.
assert!(metadata.is_file());
// The size of the file must be equal to the number of written bytes.
assert_eq!(bytes.len() as u64, metadata.len());
Ok(())
}
fn test_metadata() {
let tmp = std::env::temp_dir();
let filename = PathBuf::from("miri_test_fs_metadata.txt");
let path = tmp.join(&filename);
let bytes = b"Hello, World!\n";
// Clean the paths for robustness.
remove_file(&path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
2019-11-30 14:09:52 -06:00
// Test that metadata of an absolute path is correct.
2020-02-17 22:36:33 -06:00
check_metadata(bytes, &path).unwrap();
2019-11-30 14:09:52 -06:00
// Test that metadata of a relative path is correct.
2020-01-06 15:30:17 -06:00
std::env::set_current_dir(&tmp).unwrap();
2020-02-17 22:36:33 -06:00
check_metadata(bytes, &filename).unwrap();
// Removing file should succeed.
remove_file(&path).unwrap();
}
fn test_symlink() {
let tmp = std::env::temp_dir();
let filename = PathBuf::from("miri_test_fs_link_target.txt");
let path = tmp.join(&filename);
let symlink_path = tmp.join("miri_test_fs_symlink.txt");
let bytes = b"Hello, World!\n";
// Clean the paths for robustness.
remove_file(&path).ok();
remove_file(&symlink_path).ok();
let mut file = File::create(&path).unwrap();
file.write(bytes).unwrap();
2019-11-30 14:09:52 -06:00
2020-01-07 10:09:07 -06:00
// Creating a symbolic link should succeed.
2020-01-06 15:30:17 -06:00
std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
// Test that the symbolic link has the same contents as the file.
let mut symlink_file = File::open(&symlink_path).unwrap();
let mut contents = Vec::new();
symlink_file.read_to_end(&mut contents).unwrap();
assert_eq!(bytes, contents.as_slice());
// Test that metadata of a symbolic link is correct.
2020-02-17 22:36:33 -06:00
check_metadata(bytes, &symlink_path).unwrap();
2020-01-06 15:30:17 -06:00
// Test that the metadata of a symbolic link is correct when not following it.
assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink());
// Removing symbolic link should succeed.
remove_file(&symlink_path).unwrap();
2019-11-30 14:09:52 -06:00
// Removing file should succeed.
2019-10-11 16:02:04 -05:00
remove_file(&path).unwrap();
2020-02-17 22:36:33 -06:00
}
fn test_errors() {
let tmp = std::env::temp_dir();
let filename = PathBuf::from("miri_test_fs_errors.txt");
let path = tmp.join(&filename);
let bytes = b"Hello, World!\n";
// Clean the paths for robustness.
remove_file(&path).ok();
// The following tests also check that the `__errno_location()` shim is working properly.
// Opening a non-existing file should fail with a "not found" error.
assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
// Removing a non-existing file should fail with a "not found" error.
assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());
// Reading the metadata of a non-existing file should fail with a "not found" error.
assert_eq!(ErrorKind::NotFound, check_metadata(bytes, &path).unwrap_err().kind());
}
2020-02-17 22:36:33 -06:00
fn test_rename() {
let tmp = std::env::temp_dir();
2020-01-26 16:45:05 -06:00
// Renaming a file should succeed.
2020-02-17 22:36:33 -06:00
let path1 = tmp.join("miri_test_fs_rename_source.txt");
let path2 = tmp.join("miri_test_fs_rename_destination.txt");
2020-01-26 16:45:05 -06:00
// Clean files for robustness.
remove_file(&path1).ok();
remove_file(&path2).ok();
let file = File::create(&path1).unwrap();
drop(file);
rename(&path1, &path2).unwrap();
assert_eq!(ErrorKind::NotFound, path1.metadata().unwrap_err().kind());
2020-01-26 16:45:05 -06:00
assert!(path2.metadata().unwrap().is_file());
remove_file(&path2).unwrap();
2019-09-24 17:28:00 -05:00
}