From 8ce2078a38a7e08ddd9afd47f2cb9858fbb8eb0b Mon Sep 17 00:00:00 2001 From: Jeff Lucovsky Date: Fri, 8 Jan 2021 08:00:35 -0500 Subject: [PATCH] tftp: Add test cases This commit adds test cases for the TFTP parser. --- rust/src/tftp/tftp.rs | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/rust/src/tftp/tftp.rs b/rust/src/tftp/tftp.rs index 38dc0b06ac..361c8e15cb 100644 --- a/rust/src/tftp/tftp.rs +++ b/rust/src/tftp/tftp.rs @@ -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[..])); + } +} -- 2.47.2