2019-09-24 17:28:00 -05:00
|
|
|
// ignore-windows: File handling is not implemented yet
|
|
|
|
// compile-flags: -Zmiri-disable-isolation
|
|
|
|
|
2019-10-03 09:33:36 -05:00
|
|
|
use std::fs::{File, remove_file};
|
2019-10-13 14:48:07 -05:00
|
|
|
use std::io::{Read, Write, ErrorKind};
|
2019-09-24 17:28:00 -05:00
|
|
|
|
|
|
|
fn main() {
|
2019-10-11 16:02:04 -05:00
|
|
|
let path = std::env::temp_dir().join("miri_test_fs.txt");
|
2019-10-02 08:43:23 -05:00
|
|
|
let bytes = b"Hello, World!\n";
|
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();
|
2019-10-02 08:50:32 -05:00
|
|
|
// Writing 0 bytes should not change the file contents.
|
|
|
|
file.write(&mut []).unwrap();
|
|
|
|
|
2019-10-02 08:43:23 -05:00
|
|
|
file.write(bytes).unwrap();
|
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();
|
2019-10-02 08:43:23 -05:00
|
|
|
let mut contents = Vec::new();
|
2019-10-02 08:50:32 -05:00
|
|
|
// Reading 0 bytes should not move the file pointer.
|
|
|
|
file.read(&mut []).unwrap();
|
2019-10-02 08:43:23 -05:00
|
|
|
// Reading until EOF should get the whole text.
|
|
|
|
file.read_to_end(&mut contents).unwrap();
|
|
|
|
assert_eq!(bytes, contents.as_slice());
|
2019-10-03 09:33:36 -05:00
|
|
|
// Removing file should succeed
|
2019-10-11 16:02:04 -05:00
|
|
|
remove_file(&path).unwrap();
|
2019-10-13 14:48:07 -05:00
|
|
|
|
|
|
|
// The two 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());
|
2019-09-24 17:28:00 -05:00
|
|
|
}
|