tftp: Add test cases

This commit adds test cases for the TFTP parser.
pull/5720/head
Jeff Lucovsky 5 years ago committed by Victor Julien
parent e900b6d265
commit 8ce2078a38

@ -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[..]));
}
}

Loading…
Cancel
Save