#[path = "../../../os-shared-memory/src/lifetime.rs"] mod lifetime; use lifetime::{Error, Lifetime}; #[test] fn os_shared_memory_lifetime_positive() { let mut object = Lifetime::new(1u64, [0x1000, 0x2000]).unwrap(); assert_eq!(object.map(1, 0), Ok(0x1000)); assert_eq!(object.map(1, 1), Ok(0x2000)); object.write(1, 0, 0xa5).unwrap(); object.write(1, 1, 0x5a).unwrap(); object.grant(1, 2).unwrap(); assert_eq!(object.map(2, 0), Ok(0x1000)); assert_eq!(object.map(2, 1), Ok(0x2000)); object.close_handle(1).unwrap(); object.close_handle(2).unwrap(); assert_eq!(object.read(1, 0), Ok(0xa5)); assert_eq!(object.read(2, 1), Ok(0x5a)); assert!(object.snapshot().backing_live); assert_eq!(object.snapshot().free_count, 0); object.unmap(1, 0).unwrap(); object.unmap(1, 1).unwrap(); object.unmap(2, 0).unwrap(); assert!(object.snapshot().backing_live); object.unmap(2, 1).unwrap(); assert!(!object.snapshot().backing_live); assert_eq!(object.snapshot().free_count, 1); assert_eq!(object.free_backing(1), Err(Error::Stale)); assert_eq!(object.unmap(2, 1), Err(Error::Stale)); assert_eq!(object.snapshot().free_count, 1); } #[test] fn os_shared_memory_lifetime_negative() { let mut object = Lifetime::new(1u64, [0x1000, 0x2000]).unwrap(); object.map(1, 0).unwrap(); object.write(1, 0, 0x71).unwrap(); object.close_handle(1).unwrap(); let before = object.snapshot(); assert_eq!(object.free_backing(1), Err(Error::Busy)); assert_eq!(object.snapshot(), before); assert_eq!(object.read(1, 0), Ok(0x71)); object.unmap(1, 0).unwrap(); assert_eq!(object.snapshot().free_count, 1); } #[test] fn lifetime_authority_and_boundaries_are_atomic() { assert!(matches!(Lifetime::new(1u64, [0x1000, 0x1000]), Err(Error::Invalid))); let mut object = Lifetime::new(1u64, [0x1000, 0x2000]).unwrap(); let before = object.snapshot(); assert_eq!(object.map(3, 0), Err(Error::Access)); assert_eq!(object.map(1, 2), Err(Error::Invalid)); assert_eq!(object.free_backing(3), Err(Error::Access)); assert_eq!(object.grant(3, 2), Err(Error::Access)); assert_eq!(object.snapshot(), before); }