From 4d4b8b564f08b920faa22b97ea310e4c22c998f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 16 Jul 2023 10:38:35 +0200 Subject: [PATCH] Add tests to input parsing --- src/input.rs | 109 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 3 deletions(-) diff --git a/src/input.rs b/src/input.rs index 9de31f6..df4b77f 100644 --- a/src/input.rs +++ b/src/input.rs @@ -29,13 +29,18 @@ pub fn parse_input(input: &str) -> Result { let mut params_it = input.split(crate::PARAM_SEPARATOR); let stream = next_param!(params_it)?; ensure!(stream == "filter", "{stream}: invalid stream"); - let _version = next_param!(params_it)?; - let _timestamp = next_param!(params_it)?; - let _subsystem = next_param!(params_it)?; + let version = next_param!(params_it)?; + ensure!(!version.is_empty(), "empty version"); + let timestamp = next_param!(params_it)?; + ensure!(!timestamp.is_empty(), "empty timestamp"); + let subsystem = next_param!(params_it)?; + ensure!(subsystem == "smtp-in", "{subsystem}: invalid subsystem"); let filter = next_param!(params_it)?; ensure!(filter == "rcpt-to", "{filter}: invalid filter"); let session_id = next_param!(params_it)?.to_string(); + ensure!(!session_id.is_empty(), "empty session id"); let token = next_param!(params_it)?.to_string(); + ensure!(!token.is_empty(), "empty token"); let address = next_param!(params_it)?.trim_end(); let address = CodedAddress::from_str(address)?; Ok(Input { @@ -44,3 +49,101 @@ pub fn parse_input(input: &str) -> Result { address, }) } + +#[cfg(test)] +mod tests { + use super::{parse_input, Input}; + use crate::address::CodedAddress; + + #[test] + fn test_valid_input() { + let input = "filter|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + let res = parse_input(input); + assert!(res.is_ok()); + let inp = res.unwrap(); + assert_eq!(inp.session_id, "7641df9771b4ed00"); + assert_eq!(inp.token, "1ef1c203cc576e5d"); + } + + #[test] + fn test_empty_input() { + assert!(parse_input("").is_err()); + } + + #[test] + fn test_empty_stream() { + let input = "|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_invalid_stream() { + let input = "invalid|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_version() { + let input = "filter||1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_timestamp() { + let input = + "filter|0.5||smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_subsystem() { + let input = "filter|0.5|1576146008.006099||rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_invalid_subsystem() { + let input = "filter|0.5|1576146008.006099|invalid|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_phase() { + let input = "filter|0.5|1576146008.006099|smtp-in||7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_invalid_phase() { + let input = "filter|0.5|1576146008.006099|smtp-in|invalid|7641df9771b4ed00|1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_id() { + let input = + "filter|0.5|1576146008.006099|smtp-in|rcpt-to||1ef1c203cc576e5d|derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_token() { + let input = + "filter|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00||derp@example.com"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_empty_data() { + let input = + "filter|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|"; + assert!(parse_input(input).is_err()); + } + + #[test] + fn test_missing_data() { + let input = + "filter|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d"; + assert!(parse_input(input).is_err()); + } +}