#[cfg(test)]mod cpu_tests{ use super::*; const IP:[u8;4]=[10,0,2,15];const P:Principal=Principal{table:21,slot:0,generation:2}; fn hardware(cpu:u64,mask:u64,el:u64){crate::set_context((mask,cpu,el));} #[test]fn all_cpus_bind_close_and_reject_foreign_namespace(){ let mut previous=None;let mut namespace=0; for cpu in 0..4{hardware(cpu,0x3c0,4);let mut t=unsafe{Bindings::<1>::new_on_cpu(IP,cpu)}.unwrap();let s=t.snapshot();assert!(s.namespace>namespace);namespace=s.namespace; let handle=t.bind(Credentials{principal:P,privileged_ports:false},IP,8000).unwrap();let before=t.snapshot(); if let Some(old)=previous{assert_eq!(t.lookup(P,old),Err(Error::Foreign));assert_eq!(t.close(P,old),Err(Error::Foreign));assert_eq!(t.snapshot(),before);} assert_eq!(t.lookup(P,handle).unwrap().port,8000);let stale=Principal{generation:1,..P};assert_eq!(t.close_principal(stale),Ok(0));assert_eq!(t.snapshot(),before);t.close(P,handle).unwrap();assert!(t.snapshot().entries.iter().all(|e|e.live==0));assert_eq!(t.lookup(P,handle),Err(Error::Stale));previous=Some(handle); } } #[test]fn wrong_cpu_or_execution_does_not_consume_namespace(){ let mut cases=std::vec::Vec::new();for expected in 0..4{for actual in 0..4{if expected!=actual{cases.push((expected,actual,0x3c0,4));}}} cases.extend([(4,4,0x3c0,4),(255,255,0x3c0,4),(0,0x100,0x3c0,4),(0,1<<32,0x3c0,4),(0,0,0,4),(1,1,0x340,4),(2,2,0x3c1,4),(0,0,0x3c0,0),(1,1,0x3c0,8)]); for(expected,actual,mask,el)in cases{hardware(actual,mask,el);let before=unsafe{NEXT};assert_eq!(unsafe{Bindings::<1>::new_on_cpu(IP,expected)}.err(),Some(Error::Configuration));assert_eq!(unsafe{NEXT},before);} } #[test]fn legacy_constructor_stays_on_cpu_zero(){ for cpu in 1..4{hardware(cpu,0x3c0,4);let before=unsafe{NEXT};assert_eq!(Bindings::<1>::new(IP).err(),Some(Error::Configuration));assert_eq!(unsafe{NEXT},before);} hardware(0,0x3c0,4);assert!(Bindings::<1>::new(IP).unwrap().snapshot().namespace>0); } #[test]fn exhausted_namespace_never_wraps(){ hardware(3,0x3c0,4);let old=unsafe{NEXT};unsafe{NEXT=u64::MAX};assert_eq!(unsafe{Bindings::<1>::new_on_cpu(IP,3)}.err(),Some(Error::Exhausted));assert_eq!(unsafe{NEXT},u64::MAX);unsafe{NEXT=old}; } }