#![cfg(any(target_os = "linux", target_os = "freebsd"))]
mod common;
use std::fs::File;
use std::fs::OpenOptions;
use std::fs::{self};
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::thread;
use std::time::Duration;
use common::OfsTestContext;
use test_context::test_context;
static TEST_TEXT: &str = include_str!("../Cargo.toml");
#[test_context(OfsTestContext)]
#[test]
fn test_file(ctx: &mut OfsTestContext) {
let path = ctx.mount_point.path().join("test_file.txt");
let mut file = File::create(&path).unwrap();
file.write_all(TEST_TEXT.as_bytes()).unwrap();
drop(file);
let mut file = File::open(&path).unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
assert_eq!(buf, TEST_TEXT);
drop(file);
fs::remove_file(path).unwrap();
}
#[test_context(OfsTestContext)]
#[test]
fn test_file_append(ctx: &mut OfsTestContext) {
if !ctx.capability.write_can_append {
thread::sleep(Duration::from_secs(1));
return;
}
let path = ctx.mount_point.path().join("test_file_append.txt");
let mut file = File::create(&path).unwrap();
file.write_all(TEST_TEXT.as_bytes()).unwrap();
drop(file);
let mut file = File::options().append(true).open(&path).unwrap();
file.write_all(b"test").unwrap();
drop(file);
let mut file = File::open(&path).unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
assert_eq!(buf, TEST_TEXT.to_owned() + "test");
drop(file);
fs::remove_file(path).unwrap();
}
#[test_context(OfsTestContext)]
#[test]
fn test_file_seek(ctx: &mut OfsTestContext) {
let path = ctx.mount_point.path().join("test_file_seek.txt");
let mut file = File::create(&path).unwrap();
file.write_all(TEST_TEXT.as_bytes()).unwrap();
drop(file);
let mut file = File::open(&path).unwrap();
file.seek(SeekFrom::Start(TEST_TEXT.len() as u64 / 2))
.unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
assert_eq!(buf, TEST_TEXT[TEST_TEXT.len() / 2..]);
drop(file);
fs::remove_file(path).unwrap();
}
#[test_context(OfsTestContext)]
#[test]
fn test_file_truncate(ctx: &mut OfsTestContext) {
let path = ctx.mount_point.path().join("test_file_truncate.txt");
let mut file = File::create(&path).unwrap();
file.write_all(TEST_TEXT.as_bytes()).unwrap();
drop(file);
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open(&path)
.unwrap();
file.write_all(&TEST_TEXT.as_bytes()[..TEST_TEXT.len() / 2])
.unwrap();
drop(file);
assert_eq!(
fs::read_to_string(&path).unwrap(),
TEST_TEXT[..TEST_TEXT.len() / 2]
);
fs::remove_file(path).unwrap();
}