#[path = "../../../os-device-registry/src/lib.rs"] mod device_registry; use device_registry::{Device, DeviceKey, DeviceKind, Error, Registry, Transport}; const BLOCK: DeviceKey = DeviceKey { transport: Transport::Mmio, location: 0x0a00_0000 }; const NETWORK: DeviceKey = DeviceKey { transport: Transport::Pci, location: 0x100 }; #[test] fn os_device_registry_identity_positive() { let mut registry = Registry::<2>::new(); let block = registry.attach(BLOCK, DeviceKind::Block).unwrap(); let network = registry.attach(NETWORK, DeviceKind::Network).unwrap(); assert_eq!(registry.live_count(), 2); assert_eq!(registry.get(block), Ok(Device { key: BLOCK, kind: DeviceKind::Block })); assert_eq!(registry.get(network), Ok(Device { key: NETWORK, kind: DeviceKind::Network })); assert_eq!(registry.detach(block), Ok(Device { key: BLOCK, kind: DeviceKind::Block })); assert_eq!(registry.get(block), Err(Error::StaleHandle)); let new_block = registry.attach(BLOCK, DeviceKind::Block).unwrap(); assert_eq!(new_block.slot(), block.slot()); assert_eq!(new_block.generation(), block.generation() + 1); assert_eq!(registry.live_count(), 2); assert_eq!(registry.get(new_block).unwrap().key, BLOCK); assert_eq!(registry.get(network).unwrap().key, NETWORK); } #[test] fn os_device_registry_identity_negative() { let mut registry = Registry::<2>::new(); let block = registry.attach(BLOCK, DeviceKind::Block).unwrap(); let network = registry.attach(NETWORK, DeviceKind::Network).unwrap(); let before_duplicate = registry.snapshot(); assert_eq!(registry.attach(BLOCK, DeviceKind::Network), Err(Error::AlreadyAttached)); assert_eq!(registry.snapshot(), before_duplicate); assert_eq!(registry.detach(block).unwrap().key, BLOCK); let replacement = registry.attach(BLOCK, DeviceKind::Block).unwrap(); let before_stale = registry.snapshot(); assert_eq!(registry.get(block), Err(Error::StaleHandle)); assert_eq!(registry.detach(block), Err(Error::StaleHandle)); assert_eq!(registry.snapshot(), before_stale); assert_eq!(registry.get(replacement).unwrap().key, BLOCK); assert_eq!(registry.get(network).unwrap().key, NETWORK); let before_full = registry.snapshot(); assert_eq!(registry.attach(DeviceKey { transport: Transport::Mmio, location: 0x0a00_0200 }, DeviceKind::Block), Err(Error::Full)); assert_eq!(registry.snapshot(), before_full); let mut exhausted = Registry::<1>::with_generation_seeds([u64::MAX]); let before_exhausted = exhausted.snapshot(); assert_eq!(exhausted.attach(BLOCK, DeviceKind::Block), Err(Error::GenerationExhausted)); assert_eq!(exhausted.snapshot(), before_exhausted); let mut empty = Registry::<0>::new(); let before_empty = empty.snapshot(); assert_eq!(empty.attach(BLOCK, DeviceKind::Block), Err(Error::Full)); assert_eq!(empty.snapshot(), before_empty); }