Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust-toolchain: [nightly]
rust-toolchain: [nightly-2025-05-20]
targets: [x86_64-unknown-linux-gnu, x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none-softfloat]
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions axfs_devfs/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl VfsNodeOps for DirNode {
}

fn create(&self, path: &str, ty: VfsNodeType) -> VfsResult {
log::debug!("create {:?} at devfs: {}", ty, path);
log::debug!("create {ty:?} at devfs: {path}");
let (name, rest) = split_path(path);
if let Some(rest) = rest {
match name {
Expand All @@ -109,7 +109,7 @@ impl VfsNodeOps for DirNode {
}

fn remove(&self, path: &str) -> VfsResult {
log::debug!("remove at devfs: {}", path);
log::debug!("remove at devfs: {path}");
let (name, rest) = split_path(path);
if let Some(rest) = rest {
match name {
Expand Down
135 changes: 135 additions & 0 deletions axfs_devfs/tests/test_axfs_devfs.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to improve upon existing tests to minimize code changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, trying it

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// tests/test_axfs_dexfs.rs

// 1. Import the crate itself.

// 2. Import other dependencies required for testing.
use axfs_vfs::{VfsError, VfsNodeType, VfsOps};
use std::sync::Arc;

// Test Scene 1: Basic filesystem construction and operations on device nodes in the root directory.
#[test]
fn test_root_device_operations() {
// === Setup Phase ===
let devfs = axfs_devfs::DeviceFileSystem::new();
devfs.add("null", Arc::new(axfs_devfs::NullDev));
devfs.add("zero", Arc::new(axfs_devfs::ZeroDev));

let root = devfs.root_dir();

// === Test Phase ===

// Verify root directory attributes.
assert!(root.get_attr().unwrap().is_dir());
assert_eq!(root.get_attr().unwrap().file_type(), VfsNodeType::Dir);

// Verify lookup of a non-existent node.
// Use clone() to move a copy, not root itself.
assert_eq!(
root.clone().lookup("non_existent_file").err(),
Some(VfsError::NotFound)
);

// --- Test /null device ---
// Clone again.
let null_dev = root.clone().lookup("null").expect("Failed to lookup /null");
assert_eq!(
null_dev.get_attr().unwrap().file_type(),
VfsNodeType::CharDevice
);

let mut buffer = [42u8; 16];
assert_eq!(null_dev.read_at(0, &mut buffer).unwrap(), 0);
assert_eq!(buffer, [42u8; 16]);
assert_eq!(null_dev.write_at(0, &[1, 2, 3]).unwrap(), 3);

// --- Test /zero device ---
// This is the last use of root, so we can move it directly without cloning.
let zero_dev = root.lookup("zero").expect("Failed to lookup /zero");
assert_eq!(
zero_dev.get_attr().unwrap().file_type(),
VfsNodeType::CharDevice
);
assert_eq!(zero_dev.read_at(0, &mut buffer).unwrap(), buffer.len());
assert_eq!(buffer, [0u8; 16]);
}

// Test Scene 2: Subdirectory creation, nested lookups, and path traversal.
#[test]
fn test_subdirectory_and_path_traversal() {
// === Setup Phase ===
let devfs = axfs_devfs::DeviceFileSystem::new();
devfs.add("null", Arc::new(axfs_devfs::NullDev));
let dir_sub = devfs.mkdir("sub");
dir_sub.add("zero", Arc::new(axfs_devfs::ZeroDev));
let dir_sub_nested = dir_sub.mkdir("nested");
dir_sub_nested.add("another_null", Arc::new(axfs_devfs::NullDev));

let root = devfs.root_dir();

// === Test Phase ===

// --- Basic path lookup ---
let sub_dir = root.clone().lookup("sub").unwrap();
assert!(sub_dir.get_attr().unwrap().is_dir());

// Clone sub_dir to use it for lookup.
let zero_in_sub = sub_dir.clone().lookup("zero").unwrap();
assert_eq!(
zero_in_sub.get_attr().unwrap().file_type(),
VfsNodeType::CharDevice
);

// --- Test complex and redundant paths ---
let same_zero_in_sub = root.clone().lookup("sub/./zero").unwrap();
assert!(Arc::ptr_eq(&zero_in_sub, &same_zero_in_sub));

// --- Test '..' parent directory lookup ---
let sub_from_nested = root.clone().lookup("sub/nested/..").unwrap();
assert!(Arc::ptr_eq(&sub_dir, &sub_from_nested));

// --- Test deep lookup from the root directory ---
let deep_null = root.clone().lookup("sub/nested/another_null").unwrap();
assert_eq!(
deep_null.get_attr().unwrap().file_type(),
VfsNodeType::CharDevice
);

// --- Verify parent node relationship ---
let nested_dir = root.clone().lookup("sub/nested").unwrap();
let parent_of_nested = nested_dir
.parent()
.expect("nested dir should have a parent");
assert!(Arc::ptr_eq(&parent_of_nested, &sub_dir));
}

// Test Scene 3: Error handling.
#[test]
fn test_error_conditions() {
let devfs = axfs_devfs::DeviceFileSystem::new();
devfs.add("null", Arc::new(axfs_devfs::NullDev));
let root = devfs.root_dir();

// Perform directory operations on a file node.
assert_eq!(
root.clone().lookup("null/some_file").err(),
Some(VfsError::NotADirectory)
);

// Perform file operations on a directory node.
assert_eq!(
root.read_at(0, &mut [0; 1]).err(),
Some(VfsError::IsADirectory)
);
assert_eq!(
root.write_at(0, &[0; 1]).err(),
Some(VfsError::IsADirectory)
);

// devfs does not support runtime creation/deletion; verify it returns a permission error.
assert_eq!(
root.clone().create("new_file", VfsNodeType::File).err(),
Some(VfsError::PermissionDenied)
);
// This is the last use of root, so we can move it directly without cloning.
assert_eq!(root.remove("null").err(), Some(VfsError::PermissionDenied));
}
6 changes: 3 additions & 3 deletions axfs_ramfs/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl DirNode {
/// Creates a new node with the given name and type in this directory.
pub fn create_node(&self, name: &str, ty: VfsNodeType) -> VfsResult {
if self.exist(name) {
log::error!("AlreadyExists {}", name);
log::error!("AlreadyExists {name}");
return Err(VfsError::AlreadyExists);
}
let node: VfsNodeRef = match ty {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl VfsNodeOps for DirNode {
}

fn create(&self, path: &str, ty: VfsNodeType) -> VfsResult {
log::debug!("create {:?} at ramfs: {}", ty, path);
log::debug!("create {ty:?} at ramfs: {path}");
let (name, rest) = split_path(path);
if let Some(rest) = rest {
match name {
Expand All @@ -142,7 +142,7 @@ impl VfsNodeOps for DirNode {
}

fn remove(&self, path: &str) -> VfsResult {
log::debug!("remove at ramfs: {}", path);
log::debug!("remove at ramfs: {path}");
let (name, rest) = split_path(path);
if let Some(rest) = rest {
match name {
Expand Down
194 changes: 194 additions & 0 deletions axfs_ramfs/tests/test_axfs_ramfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// tests/test_axfs_ramfs.rs

use axfs_ramfs::*;
use axfs_vfs::{VfsError, VfsNodeType, VfsOps};
use std::sync::Arc;

/// Helper function: Creates a standard filesystem structure for multiple tests.
fn setup_test_fs() -> RamFileSystem {
let ramfs = RamFileSystem::new();
let root = ramfs.root_dir();

// .clone() before calling methods that consume ownership.
root.clone().create("file.txt", VfsNodeType::File).unwrap();
root.clone().create("dir1", VfsNodeType::Dir).unwrap();
let dir1 = root.clone().lookup("dir1").unwrap();
dir1.clone().create("dir2", VfsNodeType::Dir).unwrap();
let dir2 = dir1.clone().lookup("dir2").unwrap();
dir2.create("nested_file.txt", VfsNodeType::File).unwrap();
ramfs
}

// =================================================================
// Test Case 1: Edge cases for path resolution and lookup.
// =================================================================
#[test]
fn test_path_manipulation_and_lookup() {
println!("\n--- Test: Path Resolution and Lookup ---");
let ramfs = setup_test_fs();
let root = ramfs.root_dir();

// Clone before each call to .lookup() or other methods that consume self.
let dir1 = root.clone().lookup("dir1").unwrap();
let dir2 = root.clone().lookup("dir1/dir2").unwrap();

// 1. Test with redundant slashes.
println!("Testing: Redundant slashes");
let found_dir2 = root.clone().lookup("///dir1//dir2/").unwrap();
assert!(
Arc::ptr_eq(&dir2, &found_dir2),
"Path resolution with extra slashes should be correct"
);

// 2. Test with current directory indicator '.'.
println!("Testing: Current directory indicator '.'");
let found_dir2_with_dots = root.clone().lookup("./dir1/./dir2").unwrap();
assert!(
Arc::ptr_eq(&dir2, &found_dir2_with_dots),
"Path resolution with '.' should be correct"
);

// 3. Test with parent directory indicator '..'.
println!("Testing: Parent directory indicator '..'");
let found_dir1 = dir2.clone().lookup("..").unwrap();
assert!(
Arc::ptr_eq(&dir1, &found_dir1),
"'..' should correctly return the parent directory"
);

// 4. Test complex '..' combination paths.
let found_root = dir2.clone().lookup("../..").unwrap();
assert!(
Arc::ptr_eq(&root, &found_root),
"'../..' should return the root directory"
);

// 5. Test lookup from a file node (should fail).
println!("Testing: Lookup on a file node");
let file = root.clone().lookup("file.txt").unwrap();
assert_eq!(
file.lookup("any").err(), // 'file' is moved here, but it's okay as it's not used afterward.
Some(VfsError::NotADirectory),
"Performing lookup on a file should return NotADirectory"
);

println!("--- Path resolution tests passed ---");
}

// =================================================================
// Test Case 2: Edge cases for file I/O.
// =================================================================
#[test]
fn test_io_edge_cases() {
println!("\n--- Test: File I/O Edge Cases ---");
let ramfs = RamFileSystem::new();
let root = ramfs.root_dir();
root.clone().create("data.dat", VfsNodeType::File).unwrap();
let file = root.clone().lookup("data.dat").unwrap();

let mut buf = [0u8; 32];

// 1. Write past the end of the file.
println!("Testing: Writing past the end of the file");
file.clone().write_at(10, b"world").unwrap();
assert_eq!(file.get_attr().unwrap().size(), 15);

let bytes_read = file.clone().read_at(0, &mut buf).unwrap();
assert_eq!(bytes_read, 15);
assert_eq!(&buf[..bytes_read], b"\0\0\0\0\0\0\0\0\0\0world");

// 2. Read past the end of the file.
println!("Testing: Reading past the end of the file");
let mut small_buf = [0u8; 8];
let bytes_read = file.clone().read_at(10, &mut small_buf).unwrap();
assert_eq!(bytes_read, 5);
assert_eq!(&small_buf[..bytes_read], b"world");

// 3. Truncate file to make it larger.
println!("Testing: Truncating file to make it larger");
file.clone().truncate(20).unwrap();
assert_eq!(file.get_attr().unwrap().size(), 20);
let bytes_read = file.clone().read_at(0, &mut buf).unwrap();
assert_eq!(bytes_read, 20);
assert_eq!(&buf[..15], b"\0\0\0\0\0\0\0\0\0\0world");
assert_eq!(&buf[15..20], &[0, 0, 0, 0, 0]);

// 4. Truncate file to zero.
println!("Testing: Truncating file to zero");
file.clone().truncate(0).unwrap();
assert_eq!(file.get_attr().unwrap().size(), 0);
let bytes_read = file.read_at(0, &mut buf).unwrap(); // Last use, no clone needed.
assert_eq!(
bytes_read, 0,
"Reading from an empty file should return 0 bytes"
);

println!("--- File I/O edge case tests passed ---");
}

// =================================================================
// Test Case 3: Verification of various error conditions.
// =================================================================
#[test]
fn test_error_conditions() {
println!("\n--- Test: Error Conditions ---");
let ramfs = setup_test_fs();
let root = ramfs.root_dir();
let dir1 = root.clone().lookup("dir1").unwrap();
let file = root.clone().lookup("file.txt").unwrap();

// 1. AlreadyExists error.
println!("Testing: AlreadyExists error");
assert_eq!(
root.clone().create("file.txt", VfsNodeType::File).err(),
Some(VfsError::AlreadyExists)
);
assert_eq!(
root.clone().create("dir1", VfsNodeType::Dir).err(),
Some(VfsError::AlreadyExists)
);

// 2. DirectoryNotEmpty error.
println!("Testing: DirectoryNotEmpty error");
assert_eq!(
root.clone().remove("dir1").err(),
Some(VfsError::DirectoryNotEmpty)
);

// 3. NotADirectory error.
println!("Testing: NotADirectory error");
assert_eq!(
file.clone().create("anything", VfsNodeType::File).err(),
Some(VfsError::NotADirectory)
);

// 4. IsADirectory error.
println!("Testing: IsADirectory error");
let mut buf = [0u8; 1];
assert_eq!(
dir1.clone().read_at(0, &mut buf).err(),
Some(VfsError::IsADirectory)
);
assert_eq!(
dir1.write_at(0, &buf).err(), // Last use of dir1, no clone needed.
Some(VfsError::IsADirectory)
);

// 5. InvalidInput error.
println!("Testing: InvalidInput error");
assert_eq!(root.clone().remove(".").err(), Some(VfsError::InvalidInput));
assert_eq!(
root.clone().remove("..").err(),
Some(VfsError::InvalidInput)
);

// 6. NotFound error.
println!("Testing: NotFound error");
assert_eq!(
root.clone().lookup("no-such-file").err(),
Some(VfsError::NotFound)
);
assert_eq!(root.remove("no-such-file").err(), Some(VfsError::NotFound)); // Last use of root.

println!("--- Error condition tests passed ---");
}
2 changes: 1 addition & 1 deletion axfs_vfs/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct VfsNodeAttr {

bitflags::bitflags! {
/// Node (file/directory) permission mode.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VfsNodePerm: u16 {
/// Owner has read permission.
const OWNER_READ = 0o400;
Expand Down
Loading