From: Victor Julien Date: Thu, 6 Feb 2020 10:43:17 +0000 (+0000) Subject: rust/rpc: add partial data tests X-Git-Tag: suricata-6.0.0-beta1~696 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=576e92983e636ef3d49ec09bfcf436531cc4e9fe;p=thirdparty%2Fsuricata.git rust/rpc: add partial data tests --- diff --git a/rust/src/nfs/rpc_records.rs b/rust/src/nfs/rpc_records.rs index a218eb717c..2341be109a 100644 --- a/rust/src/nfs/rpc_records.rs +++ b/rust/src/nfs/rpc_records.rs @@ -158,7 +158,7 @@ pub struct RpcReplyPacket<'a> { } // top of request packet, just to get to procedure -#[derive(Debug)] +#[derive(Debug,PartialEq)] pub struct RpcRequestPacketPartial { pub hdr: RpcPacketHeader, @@ -365,3 +365,56 @@ named!(pub parse_rpc_udp_reply, } )) ); + +#[cfg(test)] +mod tests { + use crate::nfs::rpc_records::*; + use nom::Err::Incomplete; + use nom::Needed::Size; + + #[test] + fn test_partial_input_too_short() { + let buf: &[u8] = &[ + 0x80, 0x00, 0x00, 0x9c, // flags + 0x8e, 0x28, 0x02, 0x7e // xid + ]; + + let r = parse_rpc_request_partial(buf); + match r { + Err(Incomplete(s)) => { assert_eq!(s, Size(4)); }, + _ => { panic!("failed {:?}",r); } + } + } + #[test] + fn test_partial_input_ok() { + let buf: &[u8] = &[ + 0x80, 0x00, 0x00, 0x9c, // flags + 0x8e, 0x28, 0x02, 0x7e, // xid + 0x00, 0x00, 0x00, 0x01, // msgtype + 0x00, 0x00, 0x00, 0x02, // rpcver + 0x00, 0x00, 0x00, 0x03, // program + 0x00, 0x00, 0x00, 0x04, // progver + 0x00, 0x00, 0x00, 0x05, // procedure + ]; + let expected = RpcRequestPacketPartial { + hdr: RpcPacketHeader { + frag_is_last: true, + frag_len: 156, + xid: 2384986750, + msgtype: 1 + }, + rpcver: 2, + program: 3, + progver: 4, + procedure: 5 + }; + let r = parse_rpc_request_partial(buf); + match r { + Ok((rem, hdr)) => { + assert_eq!(rem.len(), 0); + assert_eq!(hdr, expected); + }, + _ => { panic!("failed {:?}",r); } + } + } +}