#[path="../../os-shutdown-io/src/lease_io.rs"]mod lease_io; #[path="../../scripts/os-acceptance/features/k2155-shutdown-request.rs"]mod request; use lease_io::{Backend,Drain,Owner,Observation,Resource,IoError,Outcome}; use request::{Request,Error}; const OWNER:Owner=Owner{pid:1,generation:0x21550001}; #[derive(Default)]struct Calls{clock:usize,observe:usize,commands:usize} struct Refusing<'a>{calls:&'a mut Calls} impl Backend for Refusing<'_>{ fn now(&mut self)->u64{self.calls.clock+=1;1} fn observe(&mut self)->Result{self.calls.observe+=1;Err(IoError::Eio)} fn stop_submissions(&mut self,_:u64)->Result<(),IoError>{self.calls.commands+=1;Err(IoError::Eio)} fn mask_irq(&mut self,_:u64)->Result<(),IoError>{unreachable!()} fn stop_threads(&mut self,_:u64)->Result<(),IoError>{unreachable!()} fn drain_dma(&mut self,_:u64)->Result<(),IoError>{unreachable!()} fn reset(&mut self,_:u64)->Result<(),IoError>{unreachable!()} fn release(&mut self)->Result<(),IoError>{unreachable!()} } fn resource(owner:Owner)->Resource{Resource{device:0xa003e00,epoch:42,owner,pages:22}} #[test]fn exact_pid_one_and_generation_required(){ for owner in [Owner{pid:0,generation:1},Owner{pid:2,generation:1},Owner{pid:1,generation:0}]{assert!(matches!(Request::new(owner),Err(Error::InvalidOwner)))} let mut r=Request::new(OWNER).unwrap(); for caller in [Owner{pid:9,..OWNER},Owner{generation:OWNER.generation-1,..OWNER},Owner{pid:0,..OWNER}]{assert_eq!(r.request(caller,[0;3]),Err(Error::Owner));assert!(!r.pending())} assert!(matches!(r.take(),Err(Error::NoRequest)));r.request(OWNER,[0;3]).unwrap();assert!(r.pending()); } #[test]fn arguments_cannot_supply_identity_or_extend_deadline(){ let mut r=Request::new(OWNER).unwrap(); for args in [[1,0,0],[0,OWNER.generation,0],[0,0,u64::MAX],[1,OWNER.generation,u64::MAX]]{assert_eq!(r.request(OWNER,args),Err(Error::Arguments));assert!(!r.pending());} assert_eq!(r.request(Owner{pid:9,..OWNER},[1,OWNER.generation,u64::MAX]),Err(Error::Owner)); r.request(OWNER,[0;3]).unwrap(); } #[test]fn ordinary_exit_or_missing_request_has_no_authorization(){let mut r=Request::new(OWNER).unwrap();assert!(!r.pending());assert!(matches!(r.take(),Err(Error::NoRequest)));assert!(matches!(r.take(),Err(Error::NoRequest)));} #[test]fn accepted_request_is_single_use_before_and_after_take(){let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap();assert_eq!(r.request(OWNER,[0;3]),Err(Error::Repeated));let _token=r.take().unwrap();assert!(!r.pending());assert!(matches!(r.take(),Err(Error::NoRequest)));assert_eq!(r.request(OWNER,[0;3]),Err(Error::Repeated));} #[test]fn token_cannot_authorize_another_backend_owner(){ let mut calls=Calls::default();let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap(); let mut d=Drain::new(resource(Owner{generation:OWNER.generation+1,..OWNER}),Refusing{calls:&mut calls}).unwrap(); assert_eq!(r.take().unwrap().execute(&mut d,100).unwrap_err().error,lease_io::Error::Owner);assert!(!d.status().attempted);drop(d); assert_eq!((calls.clock,calls.observe,calls.commands),(0,0,0)); } #[test]fn request_or_io_failure_never_means_clean_shutdown(){ let mut calls=Calls::default();let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap(); let mut d=Drain::new(resource(OWNER),Refusing{calls:&mut calls}).unwrap();assert_eq!(d.status().outcome,Outcome::Held);assert!(!d.status().attempted); assert_eq!(r.take().unwrap().execute(&mut d,100).unwrap_err().error,lease_io::Error::Io(IoError::Eio)); assert_eq!(d.status().outcome,Outcome::Held);assert!(!d.status().observation_current);assert!(d.status().attempted);drop(d); assert_eq!((calls.clock,calls.observe,calls.commands),(1,1,0));assert!(matches!(r.take(),Err(Error::NoRequest))); } #[test]fn expired_kernel_deadline_does_not_issue_a_command(){ let mut calls=Calls::default();let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap();let mut d=Drain::new(resource(OWNER),Refusing{calls:&mut calls}).unwrap(); assert_eq!(r.take().unwrap().execute(&mut d,1).unwrap_err().error,lease_io::Error::Deadline);drop(d);assert_eq!((calls.clock,calls.observe,calls.commands),(1,0,0)); }