use turkos_threat_pipeline::{pipeline::*,manifest::{Root,Error as ManifestError},rotation::Error as KeyError,rollback::Error as SequenceError}; const ELF:&[u8]=include_bytes!("fixtures/threat-pipeline-rust-v1.elf");const DAMAGED:&[u8]=include_bytes!("fixtures/threat-pipeline-damaged-v1.bin");const CORPUS:&[u8]=include_bytes!("fixtures/threat-pipeline-corpus-v1.bin");const KEYS:&[u8]=include_bytes!("fixtures/threat-pipeline-keys-v1.bin"); fn root(index:usize)->Root{Root{key_id:[1965,1975][index],public_key:KEYS[index*32..(index+1)*32].try_into().unwrap()}} fn pipeline(issuer:u64)->Pipeline{unsafe{Pipeline::new(issuer,issuer+1,issuer+2,root(0),6)}.unwrap()} fn prepare(p:&mut Pipeline,issuer:u64,case:usize)->Result,Error>{unsafe{p.prepare_from_kernel(issuer,&CORPUS[case*192..(case+1)*192],ELF)}} #[test]fn os_threat_pipeline_positive(){let mut p=pipeline(1);let pending=prepare(&mut p,101,0).unwrap();assert_eq!(p.sequence().floor,6);unsafe{p.install_from_kernel(root(1))}.unwrap();unsafe{p.revoke_from_kernel(1965)}.unwrap();assert_eq!(p.commit(pending).err(),Some(Error::Stale));assert_eq!(p.sequence().floor,6);assert_eq!(p.sequence().pending_sequence,0);assert_eq!(p.snapshot().aborted,1);let pending=prepare(&mut p,102,2).unwrap();let image=p.commit(pending).unwrap();assert_eq!(image.bytes(),ELF);assert_eq!(image.key_id(),1975);assert_eq!(image.sequence(),8);assert_eq!(p.sequence().floor,8);assert_eq!(p.snapshot().committed,1);assert_eq!(p.registry().version,3);p.close_from_kernel().unwrap();assert_eq!(p.sequence().phase,2);assert_eq!(p.registry().phase,2);} #[test]fn os_threat_pipeline_negative(){let mut p=pipeline(11);let initial=p.snapshot();assert_eq!(prepare(&mut p,111,2).err(),Some(Error::Key(KeyError::UnknownKey)));assert_eq!(unsafe{p.prepare_from_kernel(112,&CORPUS[..192],DAMAGED)}.err(),Some(Error::Key(KeyError::Manifest(ManifestError::Digest))));assert_eq!(prepare(&mut p,113,4).err(),Some(Error::Key(KeyError::Manifest(ManifestError::Signature))));assert_eq!(prepare(&mut p,114,1).err(),Some(Error::Sequence(SequenceError::Rollback)));assert_eq!(p.snapshot(),initial);assert_eq!(p.sequence().floor,6);let pending=prepare(&mut p,115,0).unwrap();assert_eq!(prepare(&mut p,116,0).err(),Some(Error::Busy));assert_eq!(p.close_from_kernel(),Err(Error::Busy));let mut other=pipeline(21);let foreign=prepare(&mut other,117,0).unwrap();let saved=p.snapshot();assert_eq!(p.commit(foreign).err(),Some(Error::Transaction));assert_eq!(p.snapshot(),saved);p.abort(pending).unwrap();assert_eq!(p.sequence().floor,6);let pending=prepare(&mut p,118,0).unwrap();p.commit(pending).unwrap();assert_eq!(prepare(&mut p,119,0).err(),Some(Error::Sequence(SequenceError::Rollback)));unsafe{p.install_from_kernel(root(1))}.unwrap();unsafe{p.revoke_from_kernel(1965)}.unwrap();assert_eq!(prepare(&mut p,120,0).err(),Some(Error::Key(KeyError::Revoked)));let next=prepare(&mut p,121,2).unwrap();p.commit(next).unwrap();for case in[2,3]{assert_eq!(prepare(&mut p,122+case as u64,case).err(),Some(Error::Sequence(SequenceError::Rollback)));}p.close_from_kernel().unwrap();assert_eq!(prepare(&mut p,130,2).err(),Some(Error::Closed));assert_eq!(unsafe{p.install_from_kernel(root(1))},Err(Error::Closed));assert_eq!(unsafe{p.revoke_from_kernel(1975)},Err(Error::Closed));assert_eq!(p.close_from_kernel(),Err(Error::Closed));}