use turkos_icmp_echo::echo::{Responder,Error}; use turkos_ethernet_frame::frame::Ethernet; const MAC:[u8;6]=[0x52,0x54,0,0x12,0x34,0x56]; const PEER:[u8;6]=[2,0,0,0,0,1]; fn checksum(b:&[u8])->u16 { let mut owned=b.to_vec();if owned.len()%2!=0{owned.push(0);}let sum:u32=owned.chunks_exact(2).map(|x|u16::from_be_bytes([x[0],x[1]])as u32).sum();let folded=(sum&65535)+(sum>>16);!((folded&65535)+(folded>>16))as u16 } fn ip_checksum(b:&mut[u8]){b[24..26].fill(0);let n=(b[14]&15)as usize*4;let c=checksum(&b[14..14+n]);b[24..26].copy_from_slice(&c.to_be_bytes());} fn wire(data:&[u8])->Vec{let n=42+data.len();let mut b=vec![0xa5;n.max(60)];b[..6].copy_from_slice(&MAC);b[6..12].copy_from_slice(&PEER);b[12..34].copy_from_slice(&[8,0,0x45,0x2c,0,0,1,0x23,0,0,31,1,0,0,10,0,2,99,10,0,2,15]);b[16..18].copy_from_slice(&((n-14)as u16).to_be_bytes());b[34..42].copy_from_slice(&[8,0,0,0,0x12,0x34,0,7]);b[42..n].copy_from_slice(data);let c=checksum(&b[34..n]);b[36..38].copy_from_slice(&c.to_be_bytes());ip_checksum(&mut b);b} fn responder()->Responder{Responder::new([10,0,2,15],[255,255,255,0],MAC,512,2,10,100).unwrap()} #[test] fn os_icmp_echo_positive(){ let eth=Ethernet::new(MAC).unwrap();let mut r=responder();let mut out=[0xa5;600]; for data in [&b"abc"[..],&b"abcd"[..]]{let b=wire(data);let f=eth.parse(&b).unwrap();assert_eq!(r.respond(&f,100,&mut out),Ok(60));assert_eq!(&out[..6],&PEER);assert_eq!(&out[6..12],&MAC);assert_eq!(&out[12..16],&[8,0,0x45,0x2c]);assert_eq!(out[22],64);assert_eq!(&out[26..34],&[10,0,2,15,10,0,2,99]);assert_eq!(&out[34..36],&[0,0]);assert_eq!(&out[38..42],&[0x12,0x34,0,7]);assert_eq!(&out[42..42+data.len()],data);assert_eq!(checksum(&out[14..34]),0);assert_eq!(checksum(&out[34..42+data.len()]),0);assert!(out[42+data.len()..60].iter().all(|v|*v==0));assert!(out[60..].iter().all(|v|*v==0xa5));} assert_eq!(r.snapshot().replies,2);assert_eq!(r.snapshot().tokens,0); let b=wire(b"abc");let f=eth.parse(&b).unwrap();assert_eq!(r.respond(&f,110,&mut out),Ok(60));assert_eq!(r.snapshot().tokens,0);assert_eq!(r.snapshot().anchor,110); assert_eq!(r.respond(&f,u64::MAX,&mut out),Ok(60));assert_eq!(r.snapshot().tokens,1);assert_eq!(r.respond(&f,u64::MAX,&mut out),Ok(60));assert_eq!(r.snapshot().tokens,0); // Maximum permitted body: 8 byte ICMP header plus 504 bytes of data. let b=wire(&[0x73;504]);let f=eth.parse(&b).unwrap();let mut large=responder();assert_eq!(large.respond(&f,100,&mut out),Ok(546));assert_eq!(&out[42..546],&[0x73;504]);assert_eq!(checksum(&out[34..546]),0); } #[test] fn os_icmp_echo_negative(){ let eth=Ethernet::new(MAC).unwrap();let b=wire(b"abc");let f=eth.parse(&b).unwrap();let mut r=responder();let mut out=[0xa5;600]; assert_eq!(r.respond(&f,100,&mut out[..59]),Err(Error::Output));assert_eq!(out,[0xa5;600]);assert_eq!(r.snapshot().tokens,2); for (offset,value,error) in [(0,255,Error::Ethernet),(6,3,Error::Ethernet),(23,17,Error::Protocol),(33,16,Error::Destination),(26,0,Error::Source),(29,255,Error::Source),(29,0,Error::Source),(29,15,Error::Source),(34,0,Error::Kind),(35,1,Error::Kind),(44,0x66,Error::Checksum),(20,0x20,Error::Fragment)] { let mut bad=b.clone();bad[offset]=value;if offset==0{bad[..6].fill(255);}ip_checksum(&mut bad);let frame=eth.parse(&bad).unwrap();assert_eq!(r.respond(&frame,100,&mut out),Err(error));assert_eq!(out,[0xa5;600]);assert_eq!(r.snapshot().tokens,2); } let mut invalid_ip=b.clone();invalid_ip[24]^=1;assert_eq!(r.respond(ð.parse(&invalid_ip).unwrap(),100,&mut out),Err(Error::Ipv4));assert_eq!(out,[0xa5;600]); let mut short=b.clone();short[16..18].copy_from_slice(&27u16.to_be_bytes());ip_checksum(&mut short);assert_eq!(r.respond(ð.parse(&short).unwrap(),100,&mut out),Err(Error::Length)); let big=wire(&[0;505]);assert_eq!(r.respond(ð.parse(&big).unwrap(),100,&mut out),Err(Error::Length)); // IPv4 options are an explicit unsupported case; no reply reflects them. let mut options=b.clone();options.splice(34..34,[1,1,0,0]);options[14]=0x46;options[17]+=4;ip_checksum(&mut options);assert_eq!(r.respond(ð.parse(&options).unwrap(),100,&mut out),Err(Error::Options)); assert_eq!(out,[0xa5;600]);assert_eq!(r.snapshot().tokens,2); assert_eq!(r.respond(&f,100,&mut out),Ok(60));assert_eq!(r.respond(&f,100,&mut out),Ok(60));let delivered=out; for _ in 0..64{assert_eq!(r.respond(&f,100,&mut out),Err(Error::Rate));assert_eq!(out,delivered);} assert_eq!(r.snapshot().limited,64);assert_eq!(r.snapshot().replies,2);assert_eq!(r.snapshot().received,r.snapshot().invalid+66); assert_eq!(r.respond(&f,109,&mut out),Err(Error::Rate));let prior=r.snapshot();assert_eq!(r.respond(&f,108,&mut out),Err(Error::Clock));assert_eq!(r.snapshot(),prior);assert_eq!(out,delivered); assert_eq!(r.respond(&f,110,&mut out),Ok(60));assert_eq!(r.respond(&f,110,&mut out),Err(Error::Rate)); for(max,cap,ticks)in [(7,2,10),(513,2,10),(512,0,10),(512,65,10),(512,2,0)]{assert!(matches!(Responder::new([10,0,2,15],[255,255,255,0],MAC,max,cap,ticks,0),Err(Error::Config)));} for (ip,mask) in [([0,0,0,0],[255,255,255,0]),([10,0,2,255],[255,255,255,0]),([10,0,2,0],[255,255,255,0]),([10,0,2,15],[255,0,255,0]),([10,0,2,15],[0;4]),([224,0,0,1],[255,255,255,0])]{assert!(matches!(Responder::new(ip,mask,MAC,512,2,10,0),Err(Error::Config)));} }