#[path = "../../../os-partitions/src/lib.rs"] mod partitions; use partitions::{Error, MountState, Partition, parse}; fn mbr() -> [u8; 512] { let mut bytes = [0u8; 512]; bytes[510..512].copy_from_slice(&[0x55, 0xaa]); bytes } fn entry(bytes: &mut [u8; 512], slot: usize, flag: u8, type_id: u8, start: u32, count: u32) { let offset = 446 + slot * 16; bytes[offset] = flag; bytes[offset + 4] = type_id; bytes[offset + 8..offset + 12].copy_from_slice(&start.to_le_bytes()); bytes[offset + 12..offset + 16].copy_from_slice(&count.to_le_bytes()); } #[test] fn os_partitions_mbr_positive() { let mut bytes = mbr(); entry(&mut bytes, 0, 0x80, 0x83, 1, 100); entry(&mut bytes, 2, 0, 0x7f, 101, 1947); let table = parse(&bytes, 2048).unwrap(); assert_eq!(table.count, 2); assert_eq!(table.disk_sectors, 2048); assert_eq!(table.entry(0), Some(Partition {slot: 0, type_id: 0x83, bootable: true, start_lba: 1, sectors: 100, end_lba: 101})); assert_eq!(table.entry(1), None); assert_eq!(table.entry(2), Some(Partition {slot: 2, type_id: 0x7f, bootable: false, start_lba: 101, sectors: 1947, end_lba: 2048})); assert_eq!(table.entry(3), None); let mut mounts = MountState::new(); assert_eq!(mounts.mount(&bytes, 2048), Ok(table)); assert_eq!(mounts.table, Some(table)); assert_eq!(mounts.generation, 1); } #[test] fn os_partitions_mbr_negative() { let mut bytes = mbr(); entry(&mut bytes, 0, 0, 0x83, 1, 100); entry(&mut bytes, 1, 0, 0x83, 100, 50); let original = bytes; assert_eq!(parse(&bytes, 2048), Err(Error::Overlap)); assert_eq!(bytes, original); let mut mounts = MountState::new(); let before = mounts; assert_eq!(mounts.mount(&bytes, 2048), Err(Error::Overlap)); assert_eq!(mounts, before); entry(&mut bytes, 1, 0, 0x83, 2047, 2); assert_eq!(parse(&bytes, 2048), Err(Error::OutsideCapacity)); entry(&mut bytes, 1, 0, 0xee, 101, 50); assert_eq!(parse(&bytes, 2048), Err(Error::UnsupportedLayout)); entry(&mut bytes, 1, 0, 0x83, 101, 50); bytes[510] = 0; assert_eq!(parse(&bytes, 2048), Err(Error::BadSignature)); assert_eq!(parse(&mbr(), 0), Err(Error::EmptyDevice)); assert_eq!(mounts, before); }