]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
tftp: Add test cases
authorJeff Lucovsky <jeff@lucovsky.org>
Fri, 8 Jan 2021 13:00:35 +0000 (08:00 -0500)
committerVictor Julien <victor@inliniac.net>
Tue, 12 Jan 2021 17:29:13 +0000 (18:29 +0100)
This commit adds test cases for the TFTP parser.

rust/src/tftp/tftp.rs

index 38dc0b06ac2003c78fb636bfdea39ea2f8b85046..361c8e15cbc81e57b4918b5aa400838812172d8a 100644 (file)
@@ -179,3 +179,92 @@ pub extern "C" fn rs_tftp_get_tx_data(
     let tx = cast_pointer!(tx, TFTPTransaction);
     return &mut tx.tx_data;
 }
+
+#[cfg(test)]
+mod test {
+    use super::*;
+    static READ_REQUEST: [u8; 20] = [
+            0x00, 0x01, 0x72, 0x66, 0x63, 0x31, 0x33, 0x35, 0x30, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x6f, 0x63, 0x74, 0x65, 0x74, 0x00,
+    ];
+    /* filename not terminated */
+    static READ_REQUEST_INVALID_1: [u8; 20] = [
+            0x00, 0x01, 0x72, 0x66, 0x63, 0x31, 0x33, 0x35, 0x30, 0x2e, 0x74, 0x78, 0x74, 0x6e, 0x6f, 0x63, 0x74, 0x65, 0x74, 0x00,
+    ];
+    /* garbage */
+    static READ_REQUEST_INVALID_2: [u8; 3] = [
+            0xff, 0xff, 0xff,
+    ];
+    static WRITE_REQUEST: [u8; 20] = [
+            0x00, 0x02, 0x72, 0x66, 0x63, 0x31, 0x33, 0x35, 0x30, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x6f, 0x63, 0x74, 0x65, 0x74, 0x00,
+    ];
+    /* filename not terminated */
+    static INVALID_OPCODE: [u8; 20] = [
+            0x00, 0x06, 0x72, 0x66, 0x63, 0x31, 0x33, 0x35, 0x30, 0x2e, 0x74, 0x78, 0x74, 0x6e, 0x6f, 0x63, 0x74, 0x65, 0x74, 0x00,
+    ];
+    static INVALID_MODE: [u8; 20] = [
+            0x00, 0x01, 0x72, 0x66, 0x63, 0x31, 0x33, 0x35, 0x30, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x63, 0x63, 0x63, 0x63, 0x63, 0x00,
+    ];
+
+    #[test]
+    pub fn test_parse_tftp_read_request_1() {
+        let tx = TFTPTransaction {
+            opcode: READREQUEST,
+            filename: String::from("rfc1350.txt"),
+            mode: String::from("octet"),
+            id: 0,
+            tx_data: AppLayerTxData::new(),
+        };
+
+        match parse_tftp_request(&READ_REQUEST[..]) {
+            Some(txp) => {
+                assert_eq!(tx, txp);
+            }
+            None => {
+                assert!(true);
+            }
+        }
+    }
+
+    #[test]
+    pub fn test_parse_tftp_write_request_1() {
+        let tx = TFTPTransaction {
+            opcode: WRITEREQUEST,
+            filename: String::from("rfc1350.txt"),
+            mode: String::from("octet"),
+            id: 0,
+            tx_data: AppLayerTxData::new(),
+        };
+
+        match parse_tftp_request(&WRITE_REQUEST[..]) {
+            Some(txp) => {
+                assert_eq!(tx, txp);
+            }
+            None => {
+                assert!(true, "fadfasd");
+            }
+        }
+    }
+
+    // Invalid request: filename not terminated
+    #[test]
+    pub fn test_parse_tftp_read_request_2() {
+        assert_eq!(None, parse_tftp_request(&READ_REQUEST_INVALID_1[..]));
+    }
+
+    // Invalid request: garbage input
+    #[test]
+    pub fn test_parse_tftp_read_request_3() {
+        assert_eq!(None, parse_tftp_request(&READ_REQUEST_INVALID_2[..]));
+    }
+
+    #[test]
+    pub fn test_parse_tftp_invalid_opcode_1() {
+        assert_eq!(None, parse_tftp_request(&INVALID_OPCODE[..]));
+    }
+
+    #[test]
+    pub fn test_parse_tftp_invalid_mode() {
+
+        assert_eq!(None, parse_tftp_request(&INVALID_MODE[..]));
+    }
+}