#[path="../../scripts/os-acceptance/features/k2160-request.rs"]mod request; use request::*; const OWNER:Owner=Owner{pid:1,generation:0x21600001}; #[test]fn identity_is_kernel_assigned(){ for owner in [Owner{pid:0,..OWNER},Owner{pid:9,..OWNER},Owner{generation:0,..OWNER}]{assert!(matches!(Request::new(owner),Err(Error::InvalidOwner)));} let mut r=Request::new(OWNER).unwrap(); for id in [Owner{pid:9,..OWNER},Owner{generation:OWNER.generation-1,..OWNER}]{assert_eq!(r.request(id,[0;3]),Err(Error::Owner));assert!(!r.pending());} } #[test]fn arguments_cannot_forge_identity(){ let mut r=Request::new(OWNER).unwrap(); for args in [[1,0,0],[0,OWNER.generation,0],[0,0,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)); } #[test]fn ordinary_exit_and_missing_request_grant_nothing(){ 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(){ let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap();assert!(r.pending()); 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)); let mut calls=0;assert_eq!(token.execute(OWNER,||{calls+=1;47}),Ok(47));assert_eq!(calls,1); } #[test]fn wrong_executing_owner_never_runs_action(){ for owner in [Owner{pid:9,..OWNER},Owner{generation:OWNER.generation+1,..OWNER}]{ let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap();let mut calls=0; assert_eq!(r.take().unwrap().execute(owner,||{calls+=1;}),Err(Error::Owner));assert_eq!(calls,0); } } #[test]fn failed_action_does_not_reissue_permission(){ let mut r=Request::new(OWNER).unwrap();r.request(OWNER,[0;3]).unwrap(); assert_eq!(r.take().unwrap().execute(OWNER,||Err::<(),_>("io")),Ok(Err("io"))); assert!(!r.pending());assert!(matches!(r.take(),Err(Error::NoRequest)));assert_eq!(r.request(OWNER,[0;3]),Err(Error::Repeated)); }