Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
feat: logs
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Jul 20, 2024
1 parent 3f98a90 commit ae9202a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
41 changes: 21 additions & 20 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! TODO doc
use crate::util::{TestError, TestResult};
use crate::{test_assert, test_assert_eq, util};
use crate::{log, test_assert, test_assert_eq, util};
use std::fs;
use std::fs::OpenOptions;
use std::io;
Expand All @@ -13,25 +13,25 @@ use std::os::fd::AsRawFd;
use std::path::Path;

pub fn basic() -> TestResult {
log!("File creation");
const PATH: &str = "test";
// Test creating file
let mut file = OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.open(PATH)?;

// Test writing
log!("File write");
let len = file.write(b"hello world!")?;
test_assert_eq!(len, 12);

// Test seeking
log!("File seek");
let off = file.seek(SeekFrom::Start(0))?;
test_assert_eq!(off, 0);
let off = file.seek(SeekFrom::End(0))?;
test_assert_eq!(off, 12);

// Test reading
log!("File read");
let mut buf: [u8; 16] = [0; 16];
let len = file.read(&mut buf)?;
test_assert_eq!(len, 0);
Expand All @@ -42,13 +42,13 @@ pub fn basic() -> TestResult {
test_assert_eq!(len, 12);
test_assert_eq!(&buf, b"hello world!\0\0\0\0");

// Test overwriting
log!("File overwriting");
let off = file.seek(SeekFrom::Start(6))?;
test_assert_eq!(off, 6);
let len = file.write(b"abcdefghij")?;
test_assert_eq!(len, 10);

// chmod
log!("File chmod");
for mode in 0..=0o7777 {
util::fchmod(file.as_raw_fd(), mode)?;
let stat = util::fstat(file.as_raw_fd())?;
Expand All @@ -57,13 +57,13 @@ pub fn basic() -> TestResult {

// TODO change access/modification times

// Test removing the file
log!("File remove");
test_assert!(Path::new(PATH).exists());
fs::remove_file(PATH)?;
test_assert!(!Path::new(PATH).exists());
test_assert!(matches!(fs::remove_file(PATH), Err(e) if e.kind() == io::ErrorKind::NotFound));

// Test file remove defer (file is still open)
log!("File remove defer");
let off = file.seek(SeekFrom::End(0))?;
test_assert_eq!(off, 16);
let off = file.seek(SeekFrom::Start(0))?;
Expand All @@ -77,11 +77,11 @@ pub fn basic() -> TestResult {
}

pub fn directories() -> TestResult {
// Create directory at path that do not exist (invalid)
log!("Create directory at non-existent location (invalid)");
let res = fs::create_dir("/abc/def");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::NotFound));

// Create directories
log!("Create directories");
fs::create_dir_all("/abc/def/ghi")?;
let stat = util::stat("/")?;
test_assert_eq!(stat.st_nlink, 3);
Expand All @@ -94,7 +94,7 @@ pub fn directories() -> TestResult {
let stat = util::stat("/abc/def/ghi")?;
test_assert_eq!(stat.st_mode & 0o7777, 0o755);

// Permissions check
log!("Permissions check");
// No permission
util::chmod("/abc", 0o000)?;
util::stat("/abc")?;
Expand All @@ -117,10 +117,11 @@ pub fn directories() -> TestResult {
let res = fs::create_dir("/abc/no_perm");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::PermissionDenied));

// Entries iteration
log!("Create entries");
for i in 0..1000 {
fs::create_dir(format!("/abc/{i}"))?;
}
log!("List entries");
let mut entries = fs::read_dir("/abc")?
.map(|ent| {
let ent = ent?;
Expand All @@ -134,39 +135,39 @@ pub fn directories() -> TestResult {
.collect::<Result<Vec<u32>, TestError>>()?;
entries.sort_unstable();

// Remove non-empty directory (invalid)
log!("Remove non-empty directory (invalid)");
let res = fs::remove_dir("/abc");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::DirectoryNotEmpty));

// Cleanup
log!("Cleanup");
fs::remove_dir_all("/abc")?;

Ok(())
}

pub fn hardlinks() -> TestResult {
// Link to directory (invalid)
log!("Create link to directory (invalid)");
fs::create_dir("/test_dir")?;
let res = fs::hard_link("/test_dir", "/bad_link");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::PermissionDenied));
// Check the link has not been created
let res = fs::remove_dir("/bad_link");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::NotFound));
// Cleanup
log!("Cleanup");
fs::remove_dir("/test_dir")?;

// Link to file
log!("Create link to file");
fs::hard_link("/maestro-test", "/good_link")?;
let inode0 = util::stat("/maestro-test")?.st_ino;
let inode1 = util::stat("/good_link")?.st_ino;
test_assert_eq!(inode0, inode1);
// Remove and check
log!("Remove link to file");
fs::remove_file("/good_link")?;
util::stat("/maestro-test")?;
let res = util::stat("/good_link");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::NotFound));

// Link to file that don't exist (invalid)
log!("Create link to file that don't exist (invalid)");
let res = fs::hard_link("/not_found", "/link");
test_assert!(matches!(res, Err(e) if e.kind() == io::ErrorKind::NotFound));

Expand Down
4 changes: 3 additions & 1 deletion src/procfs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! procfs filesystem testing.
use crate::test_assert_eq;
use crate::util;
use crate::util::{TestError, TestResult};
use crate::{log, test_assert_eq};
use std::collections::HashMap;
use std::env::current_dir;
use std::ffi::CString;
Expand All @@ -11,7 +11,9 @@ use std::ptr::null;
use std::{env, fs};

pub fn mount() -> TestResult {
log!("Create directory");
fs::create_dir_all("/proc")?;
log!("Mount");
let src = CString::new("procfs")?;
let target = CString::new("/proc")?;
let fstype = CString::new("procfs")?;
Expand Down
8 changes: 8 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ macro_rules! test_assert_eq {
}};
}

/// Prints a log.
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {{
println!("[LOG] {}", format_args!($($arg)*));
}};
}

pub fn chmod<P: AsRef<Path>>(path: P, mode: mode_t) -> io::Result<()> {
let path = CString::new(path.as_ref().as_os_str().as_bytes())?;
let res = unsafe { libc::chmod(path.as_ptr(), mode) };
Expand Down

0 comments on commit ae9202a

Please sign in to comment.