Skip to content
Open
Changes from all 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
63 changes: 18 additions & 45 deletions litebox_shim_linux/src/syscalls/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use litebox::fs::{FileSystem as _, Mode, OFlags};
use litebox::platform::RawConstPointer as _;
use litebox_common_linux::{AtFlags, EfdFlags, FcntlArg, FileDescriptorFlags, errno::Errno};
use litebox_platform_multiplex::{Platform, set_platform};
use zerocopy::FromBytes as _;

use crate::MutPtr;

Expand Down Expand Up @@ -179,22 +180,8 @@ fn test_getdent64() {
let mut found_entries = alloc::vec::Vec::new();

while offset < bytes_read {
assert!(
unsafe { buffer.as_ptr().add(offset) }.addr()
& (core::mem::align_of::<litebox_common_linux::LinuxDirent64>() - 1)
== 0,
"Pointer at offset {} is not aligned for LinuxDirent64 (requires {}-byte alignment)",
offset,
core::mem::align_of::<litebox_common_linux::LinuxDirent64>()
);
let dirent = unsafe {
core::ptr::read_unaligned(
buffer
.as_ptr()
.add(offset)
.cast::<litebox_common_linux::LinuxDirent64>(),
)
};
let (dirent, _) =
litebox_common_linux::LinuxDirent64::read_from_prefix(&buffer[offset..]).unwrap();

// Validate the entry length
assert!(dirent.len > 0, "Directory entry length must be positive");
Expand All @@ -203,14 +190,11 @@ fn test_getdent64() {
"Entry should not exceed buffer"
);

let name_ptr = unsafe {
buffer
.as_ptr()
.add(offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name))
let name_bytes = {
let start = offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name);
let end = offset + dirent.len as usize;
&buffer[start..end]
};
let name_len = dirent.len as usize
- core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name);
let name_bytes = unsafe { core::slice::from_raw_parts(name_ptr, name_len) };

// Find the null terminator
let null_pos = name_bytes
Expand Down Expand Up @@ -281,13 +265,8 @@ fn test_getdent64() {
assert!(bytes <= small_buffer.len(), "Should not exceed buffer size");
// If bytes > 0, verify the structure is valid
if bytes > 0 {
let dirent = unsafe {
core::ptr::read_unaligned(
small_buffer
.as_ptr()
.cast::<litebox_common_linux::LinuxDirent64>(),
)
};
let (dirent, _) =
litebox_common_linux::LinuxDirent64::read_from_prefix(&small_buffer[..bytes]).unwrap();
assert!(
dirent.len as usize <= bytes,
"First entry length should fit in returned bytes"
Expand Down Expand Up @@ -358,29 +337,23 @@ fn test_getdent64() {
// Parse entries from this chunk
let mut offset = 0;
while offset < bytes_read {
let dirent = unsafe {
core::ptr::read_unaligned(
chunk_buffer
.as_ptr()
.add(offset)
.cast::<litebox_common_linux::LinuxDirent64>(),
)
};
let (dirent, _) = litebox_common_linux::LinuxDirent64::read_from_prefix(
&chunk_buffer[offset..bytes_read],
)
.unwrap();

assert!(dirent.len > 0, "Entry length must be positive");
assert!(
offset + dirent.len as usize <= bytes_read,
"Entry should fit in chunk"
);

let name_ptr = unsafe {
chunk_buffer.as_ptr().add(
offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name),
)
let name_bytes = {
let start =
offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name);
let end = offset + dirent.len as usize;
&chunk_buffer[start..end]
};
let name_len = dirent.len as usize
- core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name);
let name_bytes = unsafe { core::slice::from_raw_parts(name_ptr, name_len) };

let null_pos = name_bytes
.iter()
Expand Down
Loading