Lines of
tests/utils.rs
from check-in dd307557e6
that are changed by the sequence of edits moving toward
check-in 6e7d1e877b:
1: use smtp2tg::utils::{
2: validate,
3: RE_CLOSING,
4: RE_DOMAIN,
5: };
6: use stacked_errors::Result;
7:
8: #[test]
dd307557e6 2026-07-31 9: fn test_validate_valid_html() -> Result<()> {
dd307557e6 2026-07-31 10: let html = "<p>Some <b>valid</b> HTML</p>";
dd307557e6 2026-07-31 11: let escaped = validate(html)?;
dd307557e6 2026-07-31 12: assert_eq!(escaped, "<p>Some <b>valid</b> HTML</p>");
dd307557e6 2026-07-31 13: Ok(())
dd307557e6 2026-07-31 14: }
dd307557e6 2026-07-31 15:
dd307557e6 2026-07-31 16: #[test]
dd307557e6 2026-07-31 17: fn test_validate_closing_tag() -> Result<()> {
dd307557e6 2026-07-31 18: let html = "<p>Some <b>valid</b> HTML</p></code><a href='http://somewere.com'>Link injection!</a>";
dd307557e6 2026-07-31 19: assert!(validate(html).is_err());
dd307557e6 2026-07-31 20: assert!(validate("</pre>").is_err());
dd307557e6 2026-07-31 21: assert!(validate("</code>").is_err());
dd307557e6 2026-07-31 22: assert!(validate("</pre>\n").is_err());
dd307557e6 2026-07-31 23: assert!(validate("</code>\t").is_err());
dd307557e6 2026-07-31 24: Ok(())
dd307557e6 2026-07-31 25: }
dd307557e6 2026-07-31 26:
dd307557e6 2026-07-31 27: #[test]
dd307557e6 2026-07-31 28: fn test_validate_empty_string() -> Result<()> {
dd307557e6 2026-07-31 29: assert_eq!(validate("").unwrap(), "");
dd307557e6 2026-07-31 30: Ok(())
dd307557e6 2026-07-31 31: }
dd307557e6 2026-07-31 32:
dd307557e6 2026-07-31 33: #[test]
dd307557e6 2026-07-31 34: fn test_validate_whitespace() -> Result<()> {
dd307557e6 2026-07-31 35: assert_eq!(validate(" \t\n").unwrap(), " \t\n"); // no escaping for whitespace
36: Ok(())
37: }
38:
39: #[test]
dd307557e6 2026-07-31 40: fn test_regex_closing_tag_matches() {
dd307557e6 2026-07-31 41: assert!(RE_CLOSING.is_match("</pre>"));
dd307557e6 2026-07-31 42: assert!(RE_CLOSING.is_match("</code>\t>"));
dd307557e6 2026-07-31 43: assert!(!RE_CLOSING.is_match("</div>")); // Not a pre/code tag
44: }
45:
46: #[test]
dd307557e6 2026-07-31 47: fn test_regex_domain_matches() {
dd307557e6 2026-07-31 48: assert!(RE_DOMAIN.is_match("example.com"));
dd307557e6 2026-07-31 49: assert!(RE_DOMAIN.is_match("sub.example.co.uk"));
dd307557e6 2026-07-31 50: assert!(!RE_DOMAIN.is_match("invalid@domain.com"));
51: }