Skip to content

Commit

Permalink
fix: test case ut_lind_fs_getdents
Browse files Browse the repository at this point in the history
  • Loading branch information
lind committed Oct 24, 2024
1 parent 039978e commit a695528
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/tests/fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2512,26 +2512,38 @@ pub mod fs_tests {
let bufsize = 50;
let mut vec = vec![0u8; bufsize as usize];
let baseptr: *mut u8 = &mut vec[0];

// Remove the directory if it exists
let _ = cage.rmdir_syscall("/getdents");
assert_eq!(cage.mkdir_syscall("/getdents", S_IRWXA), 0);
let fd = cage.open_syscall("/getdents", O_RDWR, S_IRWXA);
let fd = cage.open_syscall("/getdents", O_RDONLY | O_DIRECTORY, S_IRWXA);
assert!(fd > 0, "Failed to open directory");
// Check the return value of getdents_syscall
assert_eq!(cage.getdents_syscall(fd, baseptr, bufsize as u32), 48);
let result = cage.getdents_syscall(fd, baseptr, bufsize as u32);
assert!(result >= 0, "getdents_syscall failed with error: {}", result);

unsafe {
let first_dirent = baseptr as *mut interface::ClippedDirent;
assert!((*first_dirent).d_off == 24);
let reclen_matched: bool = ((*first_dirent).d_reclen == 24);
// Copy packed fields into local variables to avoid alignment issues
let d_off_value = (*first_dirent).d_off;
let d_reclen_value = (*first_dirent).d_reclen;
// Print and assert d_off and d_reclen
assert!(d_off_value > 0, "Expected d_off > 0, but got {}", d_off_value);
let reclen_matched: bool = (d_reclen_value == 24);
assert_eq!(reclen_matched, true);

// Handle the directory name, avoiding packed fields
let nameoffset = baseptr.wrapping_offset(interface::CLIPPED_DIRENT_SIZE as isize);
let returnedname = RustCStr::from_ptr(nameoffset as *const _);
let name_matched: bool = (returnedname
== RustCStr::from_bytes_with_nul(b".\0").unwrap())
| (returnedname == RustCStr::from_bytes_with_nul(b"..\0").unwrap());
|| (returnedname == RustCStr::from_bytes_with_nul(b"..\0").unwrap());
assert_eq!(name_matched, true);

// Access second directory entry and copy packed fields into locals
let second_dirent = baseptr.wrapping_offset(24) as *mut interface::ClippedDirent;
assert!((*second_dirent).d_off >= 48);
let second_d_off_value = (*second_dirent).d_off;
assert!(second_d_off_value >= 48, "Expected d_off to be >= 48, but got {}", second_d_off_value);
}

assert_eq!(cage.close_syscall(fd), 0);
Expand All @@ -2540,6 +2552,7 @@ pub mod fs_tests {
assert_eq!(cage.exit_syscall(libc::EXIT_SUCCESS), libc::EXIT_SUCCESS);
lindrustfinalize();
}

#[test]
fn ut_lind_fs_getdents_invalid_fd() {
let _thelock = setup::lock_and_init();
Expand All @@ -2549,6 +2562,8 @@ pub mod fs_tests {
let mut vec = vec![0u8; bufsize as usize];
let baseptr: *mut u8 = &mut vec[0];

// Remove the directory if it exists
let _ = cage.rmdir_syscall("/getdents");
// Create a directory
assert_eq!(cage.mkdir_syscall("/getdents", S_IRWXA), 0);

Expand Down Expand Up @@ -2623,6 +2638,8 @@ pub mod fs_tests {
let mut vec = vec![0u8; bufsize as usize];
let baseptr: *mut u8 = &mut vec[0];

// Remove the directory if it exists
let _ = cage.rmdir_syscall("/getdents");
// Create a directory
assert_eq!(cage.mkdir_syscall("/getdents", S_IRWXA), 0);

Expand Down Expand Up @@ -2668,6 +2685,7 @@ pub mod fs_tests {

// Clean up: Close the file descriptor and finalize the test environment
assert_eq!(cage.close_syscall(fd), 0);
assert_eq!(cage.unlink_syscall(filepath), 0);
assert_eq!(cage.exit_syscall(libc::EXIT_SUCCESS), libc::EXIT_SUCCESS);
lindrustfinalize();
}
Expand Down

0 comments on commit a695528

Please sign in to comment.