]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/rpc: add partial data tests
authorVictor Julien <victor@inliniac.net>
Thu, 6 Feb 2020 10:43:17 +0000 (10:43 +0000)
committerVictor Julien <victor@inliniac.net>
Mon, 2 Mar 2020 16:16:42 +0000 (17:16 +0100)
rust/src/nfs/rpc_records.rs

index a218eb717ce4e05f314895b8f999e465af449acf..2341be109af1436096cc703ea9e6c3f8a2269781 100644 (file)
@@ -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<RpcReplyPacket>,
            }
    ))
 );
+
+#[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); }
+        }
+    }
+}