ADDED tests/utils.rs Index: tests/utils.rs ================================================================== --- /dev/null +++ tests/utils.rs @@ -0,0 +1,51 @@ +use smtp2tg::utils::{ + validate, + RE_CLOSING, + RE_DOMAIN, +}; +use stacked_errors::Result; + +#[test] +fn test_validate_valid_html() -> Result<()> { + let html = "

Some valid HTML

"; + let escaped = validate(html)?; + assert_eq!(escaped, "<p>Some <b>valid</b> HTML</p>"); + Ok(()) +} + +#[test] +fn test_validate_closing_tag() -> Result<()> { + let html = "

Some valid HTML

Link injection!"; + assert!(validate(html).is_err()); + assert!(validate("").is_err()); + assert!(validate("").is_err()); + assert!(validate("\n").is_err()); + assert!(validate("\t").is_err()); + Ok(()) +} + +#[test] +fn test_validate_empty_string() -> Result<()> { + assert_eq!(validate("").unwrap(), ""); + Ok(()) +} + +#[test] +fn test_validate_whitespace() -> Result<()> { + assert_eq!(validate(" \t\n").unwrap(), " \t\n"); // no escaping for whitespace + Ok(()) +} + +#[test] +fn test_regex_closing_tag_matches() { + assert!(RE_CLOSING.is_match("")); + assert!(RE_CLOSING.is_match("\t>")); + assert!(!RE_CLOSING.is_match("")); // Not a pre/code tag +} + +#[test] +fn test_regex_domain_matches() { + assert!(RE_DOMAIN.is_match("example.com")); + assert!(RE_DOMAIN.is_match("sub.example.co.uk")); + assert!(!RE_DOMAIN.is_match("invalid@domain.com")); +}