use turkos_os_guest_power::capability::*; const PID1:Identity=Identity{pid:1,generation:7}; const SHELL:Identity=Identity{pid:9,generation:2}; fn authority()->Authority{Authority::new(PID1,ALL).unwrap()} #[test]fn os_guest_power_capability_positive(){let mut a=authority();let t=a.request(PID1,Op::Off).unwrap();assert_eq!((t.op(),t.sequence()),(Op::Off,1)); assert_eq!(a.status().pending,1);assert_eq!(a.commit(t),Ok(Op::Off)); assert_eq!(a.status(),Status{holder:PID1,grant:1,ops:ALL,pending:0,issued:1,committed:1,cancelled:0,denied:0,revoked:false}); let t=a.request(PID1,Op::Reset).unwrap();a.cancel(t).unwrap();assert_eq!(a.status().cancelled,1)} #[test]fn os_guest_power_capability_negative(){let mut a=authority();for caller in[SHELL,Identity{pid:1,generation:6},Identity{pid:1,generation:8}]{for op in[Op::Off,Op::Reset]{let before=a.status();assert_eq!(a.request(caller,op),Err(Error::Denied));assert_eq!(a.status(),Status{denied:before.denied+1,..before});}} assert_eq!(a.status().denied,6);assert_eq!(a.status().pending,0);assert_eq!(a.status().issued,0);let t=a.request(PID1,Op::Off).unwrap();assert_eq!(t.sequence(),1)} #[test]fn os_guest_power_capability_one_pending_ticket(){let mut a=authority();let t=a.request(PID1,Op::Off).unwrap();assert_eq!(a.request(PID1,Op::Reset),Err(Error::Pending));assert_eq!(a.status().issued,1);a.commit(t).unwrap();a.request(PID1,Op::Reset).unwrap();} #[test]fn os_guest_power_capability_ticket_is_single_use(){let mut a=authority();let t=a.request(PID1,Op::Off).unwrap();let other=Authority::new(PID1,ALL).unwrap().request(PID1,Op::Off).unwrap();a.commit(t).unwrap();let before=a.status();let(e,_)=a.commit(other).unwrap_err();assert_eq!(e,Error::Stale);assert_eq!(a.status(),before)} #[test]fn os_guest_power_capability_ops_are_bounded(){let mut a=Authority::new(PID1,Op::Reset as u64).unwrap();assert_eq!(a.request(PID1,Op::Off),Err(Error::Unsupported));assert_eq!(a.status().issued,0);a.request(PID1,Op::Reset).unwrap();} #[test]fn os_guest_power_capability_transfer_stales_tickets(){let mut a=authority();let t=a.request(PID1,Op::Off).unwrap();assert_eq!(a.transfer(SHELL,SHELL),Err(Error::Denied));let next=Identity{pid:1,generation:8};a.transfer(PID1,next).unwrap(); let(e,_)=a.commit(t).unwrap_err();assert_eq!(e,Error::Stale);assert_eq!(a.request(PID1,Op::Off),Err(Error::Denied));let t=a.request(next,Op::Off).unwrap();assert_eq!(a.commit(t),Ok(Op::Off));assert_eq!(a.status().grant,2)} #[test]fn os_guest_power_capability_revocation_is_final(){let mut a=authority();let t=a.request(PID1,Op::Reset).unwrap();assert_eq!(a.revoke(SHELL),Err(Error::Denied));a.revoke(PID1).unwrap();let(e,_)=a.cancel(t).unwrap_err();assert_eq!(e,Error::Stale);assert_eq!(a.request(PID1,Op::Off),Err(Error::Revoked));assert_eq!(a.transfer(PID1,SHELL),Err(Error::Revoked));assert!(a.status().revoked)} #[test]fn os_guest_power_capability_invalid_configuration(){for(h,ops)in[(Identity{pid:0,generation:1},ALL),(Identity{pid:1,generation:0},ALL),(PID1,0),(PID1,4)]{assert_eq!(Authority::new(h,ops).unwrap_err(),Error::Configuration)}let mut a=authority();assert_eq!(a.transfer(PID1,Identity{pid:0,generation:1}),Err(Error::Configuration));assert_eq!(a.status().holder,PID1)} #[test]fn os_guest_power_capability_cancelled_ticket_cannot_commit(){let mut a=authority();let t=a.request(PID1,Op::Off).unwrap();let copy=Authority::new(PID1,ALL).unwrap().request(PID1,Op::Off).unwrap();a.cancel(t).unwrap();assert_eq!(a.commit(copy).unwrap_err().0,Error::Stale);assert_eq!(a.status().committed,0)} #[test]fn os_guest_power_capability_foreign_holder_ticket_is_stale(){let mut a=authority();a.request(PID1,Op::Off).unwrap();let foreign=Authority::new(SHELL,ALL).unwrap().request(SHELL,Op::Off).unwrap();let before=a.status();assert_eq!(a.commit(foreign).unwrap_err().0,Error::Stale);assert_eq!(a.status(),before)} #[test]fn os_guest_power_capability_other_grant_ticket_is_stale(){let mut a=authority();a.request(PID1,Op::Off).unwrap();let mut b=authority();b.transfer(PID1,PID1).unwrap();let other=b.request(PID1,Op::Off).unwrap();let before=a.status();assert_eq!(a.cancel(other).unwrap_err().0,Error::Stale);assert_eq!(a.status(),before)} #[test]fn os_guest_power_capability_old_sequence_ticket_is_stale(){let mut a=authority();let t=a.request(PID1,Op::Off).unwrap();a.cancel(t).unwrap();a.request(PID1,Op::Reset).unwrap();let old=authority().request(PID1,Op::Off).unwrap();let before=a.status();assert_eq!(a.commit(old).unwrap_err().0,Error::Stale);assert_eq!(a.status(),before)}