|
@@ -119,8 +119,9 @@ mod test {
|
|
|
ffi::{OsStr, OsString},
|
|
|
fs::{
|
|
|
create_dir, hard_link, metadata, read, read_dir, remove_dir, remove_file, rename,
|
|
|
- set_permissions, write, Permissions, ReadDir,
|
|
|
+ set_permissions, write, OpenOptions, Permissions, ReadDir,
|
|
|
},
|
|
|
+ io::{Read, Seek, SeekFrom, Write},
|
|
|
os::unix::fs::PermissionsExt,
|
|
|
thread::JoinHandle,
|
|
|
time::Duration,
|
|
@@ -498,4 +499,69 @@ mod test {
|
|
|
let actual = read(&dst_path).unwrap();
|
|
|
assert_eq!(EXPECTED, actual)
|
|
|
}
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn write_read_with_file_struct() {
|
|
|
+ const FILE_NAME: &str = "big.dat";
|
|
|
+ const LEN: usize = btlib::SECTOR_SZ_DEFAULT + 1;
|
|
|
+ fn fill(buf: &mut Vec<u8>, value: u8) {
|
|
|
+ buf.clear();
|
|
|
+ buf.extend(std::iter::repeat(value).take(buf.capacity()));
|
|
|
+ }
|
|
|
+
|
|
|
+ let case = TestCase::new();
|
|
|
+ let file_path = case.mnt_dir().join(FILE_NAME);
|
|
|
+ let mut buf = vec![1u8; LEN];
|
|
|
+ let mut file = OpenOptions::new()
|
|
|
+ .create(true)
|
|
|
+ .read(true)
|
|
|
+ .write(true)
|
|
|
+ .open(&file_path)
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ file.write_all(&buf).unwrap();
|
|
|
+ fill(&mut buf, 2);
|
|
|
+ file.write_all(&buf).unwrap();
|
|
|
+ file.rewind().unwrap();
|
|
|
+ let mut actual = vec![0u8; LEN];
|
|
|
+ file.read_exact(&mut actual).unwrap();
|
|
|
+
|
|
|
+ fill(&mut buf, 1);
|
|
|
+ assert_eq!(buf, actual);
|
|
|
+ }
|
|
|
+
|
|
|
+ //#[test]
|
|
|
+ #[allow(dead_code)]
|
|
|
+ /// KMC: This test is currently not working, and I've not been able to figure out why, not
|
|
|
+ /// reproduce it at a lower layer of the stack.
|
|
|
+ fn read_more_than_whats_buffered() {
|
|
|
+ const FILE_NAME: &str = "big.dat";
|
|
|
+ const SECT_SZ: usize = btlib::SECTOR_SZ_DEFAULT;
|
|
|
+ const DIVISOR: usize = 8;
|
|
|
+ const READ_SZ: usize = SECT_SZ / DIVISOR;
|
|
|
+
|
|
|
+ let case = TestCase::new();
|
|
|
+ let file_path = case.mnt_dir().join(FILE_NAME);
|
|
|
+ let mut file = OpenOptions::new()
|
|
|
+ .create(true)
|
|
|
+ .read(true)
|
|
|
+ .write(true)
|
|
|
+ .open(&file_path)
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ let mut buf = vec![1u8; 2 * SECT_SZ];
|
|
|
+ file.write_all(&buf).unwrap();
|
|
|
+ file.flush().unwrap();
|
|
|
+ let mut file = OpenOptions::new()
|
|
|
+ .read(true)
|
|
|
+ .write(true)
|
|
|
+ .open(&file_path)
|
|
|
+ .unwrap();
|
|
|
+ file.seek(SeekFrom::Start(SECT_SZ as u64)).unwrap();
|
|
|
+ let mut actual = vec![0u8; READ_SZ];
|
|
|
+ file.read_exact(&mut actual).unwrap();
|
|
|
+
|
|
|
+ buf.truncate(READ_SZ);
|
|
|
+ assert!(buf == actual);
|
|
|
+ }
|
|
|
}
|