Skip to content

Commit e481eef

Browse files
Fix file info sizes and add tests (#365)
1 parent fd2a331 commit e481eef

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

src/proto/media/file/info.rs

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<Header> FromUefi for NamedFileProtocolInfo<Header> {
124124
}
125125

126126
/// Errors that can occur when creating a `FileProtocolInfo`
127+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
127128
pub enum FileInfoCreationError {
128129
/// The provided buffer was too small to hold the `FileInfo`. You need at
129130
/// least the indicated buffer size (in bytes). Please remember that using
@@ -198,7 +199,7 @@ impl FileInfo {
198199
attribute,
199200
};
200201
let info = Self::new_impl(storage, header, file_name)?;
201-
info.header.size = mem::size_of_val(&info) as u64;
202+
info.header.size = mem::size_of_val(info) as u64;
202203
Ok(info)
203204
}
204205

@@ -287,7 +288,7 @@ impl FileSystemInfo {
287288
block_size,
288289
};
289290
let info = Self::new_impl(storage, header, volume_label)?;
290-
info.header.size = mem::size_of_val(&info) as u64;
291+
info.header.size = mem::size_of_val(info) as u64;
291292
Ok(info)
292293
}
293294

@@ -355,3 +356,91 @@ impl FileSystemVolumeLabel {
355356
}
356357

357358
impl FileProtocolInfo for FileSystemVolumeLabel {}
359+
360+
#[cfg(test)]
361+
mod tests {
362+
use super::*;
363+
use crate::alloc_api::string::ToString;
364+
use crate::alloc_api::vec;
365+
use crate::table::runtime::{Daylight, Time};
366+
367+
#[test]
368+
fn test_file_info() {
369+
let mut storage = vec![0; 128];
370+
371+
let file_size = 123;
372+
let physical_size = 456;
373+
let create_time = Time::new(1970, 1, 1, 0, 0, 0, 0, 0, Daylight::IN_DAYLIGHT);
374+
let last_access_time = Time::new(1971, 1, 1, 0, 0, 0, 0, 0, Daylight::IN_DAYLIGHT);
375+
let modification_time = Time::new(1972, 1, 1, 0, 0, 0, 0, 0, Daylight::IN_DAYLIGHT);
376+
let attribute = FileAttribute::READ_ONLY;
377+
let name = "test_name";
378+
let info = FileInfo::new(
379+
&mut storage,
380+
file_size,
381+
physical_size,
382+
create_time,
383+
last_access_time,
384+
modification_time,
385+
attribute,
386+
name,
387+
)
388+
.unwrap();
389+
390+
// Header size: 80 bytes
391+
// + Name size (including trailing null): 20 bytes
392+
// = 100
393+
// Round size up to match FileInfo alignment of 8: 104
394+
assert_eq!(info.header.size, 104);
395+
396+
assert_eq!(info.file_size(), file_size);
397+
assert_eq!(info.physical_size(), physical_size);
398+
assert_eq!(info.create_time(), &create_time);
399+
assert_eq!(info.last_access_time(), &last_access_time);
400+
assert_eq!(info.modification_time(), &modification_time);
401+
assert_eq!(info.attribute(), attribute);
402+
assert_eq!(info.file_name().to_string(), name);
403+
}
404+
405+
#[test]
406+
fn test_file_system_info() {
407+
let mut storage = vec![0; 128];
408+
409+
let read_only = false;
410+
let volume_size = 123;
411+
let free_space = 456;
412+
let block_size = 789;
413+
let name = "test_name";
414+
let info = FileSystemInfo::new(
415+
&mut storage,
416+
read_only,
417+
volume_size,
418+
free_space,
419+
block_size,
420+
name,
421+
)
422+
.unwrap();
423+
424+
// Header size: 40 bytes
425+
// + Name size (including trailing null): 20 bytes
426+
// = 60
427+
// Round size up to match FileInfo alignment of 8: 64
428+
assert_eq!(info.header.size, 64);
429+
430+
assert_eq!(info.read_only(), read_only);
431+
assert_eq!(info.volume_size(), volume_size);
432+
assert_eq!(info.free_space(), free_space);
433+
assert_eq!(info.block_size(), block_size);
434+
assert_eq!(info.volume_label().to_string(), name);
435+
}
436+
437+
#[test]
438+
fn test_file_system_volume_label() {
439+
let mut storage = vec![0; 128];
440+
441+
let name = "test_name";
442+
let info = FileSystemVolumeLabel::new(&mut storage, &name).unwrap();
443+
444+
assert_eq!(info.volume_label().to_string(), name);
445+
}
446+
}

src/table/runtime.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,22 @@ impl fmt::Display for Time {
421421
}
422422
}
423423

424+
impl PartialEq for Time {
425+
fn eq(&self, other: &Time) -> bool {
426+
self.year == other.year
427+
&& self.month == other.month
428+
&& self.day == other.day
429+
&& self.hour == other.hour
430+
&& self.minute == other.minute
431+
&& self.second == other.second
432+
&& self.nanosecond == other.nanosecond
433+
&& self.time_zone == other.time_zone
434+
&& self.daylight == other.daylight
435+
}
436+
}
437+
438+
impl Eq for Time {}
439+
424440
/// Real time clock capabilities
425441
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
426442
#[repr(C)]

0 commit comments

Comments
 (0)