#[cfg(not(feature = "crc"))]
use hadris_part::Error;
use hadris_part::gpt::{GptAttributes, GptPartitionEntry, GptPartitionName, Guid};
use hadris_part::{PartitionTable, PartitionTableReadExt};
use std::io::Cursor;
#[test]
fn mbr_end_lba_saturates_on_corrupt_entry() {
let mut sector = [0u8; 512];
sector[446 + 4] = 0x83;
sector[446 + 8..446 + 12].copy_from_slice(&0xFFFFFF00u32.to_le_bytes());
sector[446 + 12..446 + 16].copy_from_slice(&0xFFFFFFFFu32.to_le_bytes());
sector[510] = 0x55;
sector[511] = 0xAA;
let table = PartitionTable::read_from(&mut Cursor::new(sector), 512).expect("read MBR");
let parts = table.partitions();
assert_eq!(parts.len(), 1);
assert_eq!(parts[0].end_lba, u32::MAX as u64);
}
#[test]
fn gpt_size_sectors_saturates_on_full_range_entry() {
let mut entry = GptPartitionEntry {
type_guid: Guid::from_bytes([0xAF; 16]),
unique_guid: Guid::from_bytes([0xDC; 16]),
first_lba: 0u64.into(),
last_lba: u64::MAX.into(),
attributes: GptAttributes::new(0),
name: GptPartitionName::default(),
};
assert!(!entry.is_unused());
assert_eq!(entry.size_sectors(), u64::MAX);
entry.last_lba = (u64::MAX - 1).into();
assert_eq!(entry.size_sectors(), u64::MAX);
}
#[cfg(not(feature = "crc"))]
fn protective_gpt_image(partition_entry_lba: u64, num_entries: u32, alternate_lba: u64) -> Vec<u8> {
let mut image = vec![0u8; 2048];
image[446 + 4] = 0xEE;
image[510] = 0x55;
image[511] = 0xAA;
let h = 512;
image[h..h + 8].copy_from_slice(b"EFI PART");
image[h + 24..h + 32].copy_from_slice(&1u64.to_le_bytes()); image[h + 32..h + 40].copy_from_slice(&alternate_lba.to_le_bytes());
image[h + 72..h + 80].copy_from_slice(&partition_entry_lba.to_le_bytes());
image[h + 80..h + 84].copy_from_slice(&num_entries.to_le_bytes());
image[h + 84..h + 88].copy_from_slice(&128u32.to_le_bytes()); image
}
#[cfg(not(feature = "crc"))]
#[test]
fn gpt_oversized_entry_array_is_rejected() {
let image = protective_gpt_image(2, u32::MAX, 34);
let err = PartitionTable::read_from(&mut Cursor::new(&image), 512).unwrap_err();
assert!(matches!(err, Error::DiskTooSmall { .. }));
}
#[cfg(not(feature = "crc"))]
#[test]
fn gpt_entry_lba_times_block_size_overflow_is_rejected() {
let image = protective_gpt_image(u64::MAX, 1, 34);
let err = PartitionTable::read_from(&mut Cursor::new(&image), 512).unwrap_err();
assert!(matches!(err, Error::DiskTooSmall { .. }));
}
#[cfg(not(feature = "crc"))]
#[test]
fn gpt_backup_header_lba_overflow_is_rejected() {
let image = protective_gpt_image(2, 4, u64::MAX);
let err = PartitionTable::read_from(&mut Cursor::new(&image), 512).unwrap_err();
assert!(matches!(err, Error::BackupHeaderIo { .. }));
}