// Compiled by the standalone source-bound session harness, which imports the // same flush/quiesce/flush_io/session bytes as the kernel candidate. #[path="../src/flush.rs"] mod flush; #[path="../src/quiesce.rs"] mod quiesce; #[path="../src/flush_io.rs"] mod flush_io; #[path="../src/flush_session.rs"] mod flush_session; use crate::{flush_session::*,flush_io as io,quiesce::QuiesceGate}; const OWNER:Identity=Identity{pid:1,generation:0x21500001}; struct Disk {calls:usize,failure:Option<(usize,io::IoError)>} impl Disk {fn event(&mut self)->Result<(),io::IoError>{self.calls+=1;match self.failure{Some((at,e))if at==self.calls=>Err(e),_=>Ok(())}}} impl io::Storage for Disk { fn capacity_sectors(&self)->u64{2048} fn write_sector(&mut self,_:u64,_:&io::Block)->Result<(),io::IoError>{self.event()} fn flush(&mut self)->Result<(),io::IoError>{self.event()} } static DATA:io::Block=[0xd1;512];static META:io::Block=[0xa2;512];static JOURNAL:io::Block=[0xb3;512]; static D:[io::Write;1]=[io::Write{lba:112,data:&DATA}]; static M:[io::Write;1]=[io::Write{lba:113,data:&META}]; static J:[io::Write;1]=[io::Write{lba:120,data:&JOURNAL}]; fn plan()->io::Plan<'static>{io::Plan{data:&D,metadata:&M,journal:&J,journal_sequence:9}} fn gate()->QuiesceGate{let mut g=QuiesceGate::new();g.begin_quiesce().unwrap();g} #[test] fn authorized_actual_flush_is_required_before_clean_claim(){ let mut s=Session::new(OWNER).unwrap();let mut d=Disk{calls:0,failure:None}; assert_eq!(s.claim_clean(OWNER,false),REFUSED|CLEAN_REFUSED);assert!(!s.status().has_receipt); assert_eq!(s.flush(OWNER,&mut d,&gate(),&plan()),0);assert_eq!(d.calls,6); assert_eq!(s.claim_clean(OWNER,true),REFUSED|QUIT_NOT_FLUSH); assert_eq!(s.claim_clean(OWNER,false),7);assert!(s.status().has_receipt); assert_eq!(s.flush(OWNER,&mut d,&gate(),&plan()),REFUSED|ALREADY_ATTEMPTED);assert_eq!(d.calls,6); assert_eq!((s.status().early_claim,s.status().quit_claim,s.status().clean_claim,s.status().repeated),(1,1,1,1)); } #[test] fn other_pid_and_stale_generation_cannot_submit_or_claim(){ for caller in [Identity{pid:9,..OWNER},Identity{generation:OWNER.generation-1,..OWNER},Identity{pid:0,generation:0}]{ let mut s=Session::new(OWNER).unwrap();let mut d=Disk{calls:0,failure:None}; assert_eq!(s.flush(caller,&mut d,&gate(),&plan()),REFUSED|EPERM);assert_eq!(d.calls,0); assert_eq!(s.claim_clean(caller,false),REFUSED|EPERM);assert!(!s.status().attempted); assert_eq!(s.flush(OWNER,&mut d,&gate(),&plan()),0); assert_eq!(s.claim_clean(caller,false),REFUSED|EPERM);assert_eq!(s.status().unauthorized,3); assert_eq!(s.claim_clean(OWNER,false),7); } assert!(Session::new(Identity{pid:0,..OWNER}).is_none());assert!(Session::new(Identity{generation:0,..OWNER}).is_none()); } #[test] fn every_device_failure_is_terminal_without_clean_receipt(){ for error in [io::IoError::Eio,io::IoError::Timeout]{for at in 1..=6{ let mut s=Session::new(OWNER).unwrap();let mut d=Disk{calls:0,failure:Some((at,error))}; let errno=if error==io::IoError::Eio{5}else{110}; assert_eq!(s.flush(OWNER,&mut d,&gate(),&plan()),REFUSED|errno);assert_eq!(d.calls,at); assert!(!s.status().has_receipt);assert!(!s.status().progress.unwrap().clean); assert_eq!(s.claim_clean(OWNER,false),REFUSED|CLEAN_REFUSED); assert_eq!(s.claim_clean(OWNER,true),REFUSED|QUIT_NOT_FLUSH); assert_eq!(s.flush(OWNER,&mut d,&gate(),&plan()),REFUSED|ALREADY_ATTEMPTED);assert_eq!(d.calls,at); assert_eq!(s.status().errno,errno); }} } #[test] fn unquiesced_and_invalid_plan_never_reach_device(){ for invalid in [false,true]{let mut s=Session::new(OWNER).unwrap();let mut d=Disk{calls:0,failure:None}; let g=if invalid{gate()}else{QuiesceGate::new()};let p=if invalid{io::Plan{journal_sequence:0,..plan()}}else{plan()}; assert_eq!(s.flush(OWNER,&mut d,&g,&p),REFUSED|if invalid{22}else{108});assert_eq!(d.calls,0); assert_eq!(s.claim_clean(OWNER,false),REFUSED|CLEAN_REFUSED); } }