//! Property tests for [`super`]. use super::*; use proptest::prelude::*; const SCOPE: &str = "22222222-2222-2222-2222-222222222222"; fn policy() -> UrlPolicy { UrlPolicy::new( "https://u.makenot.work/a/p", [ "makenot.work".to_string(), "u.makenot.work".to_string(), "cdn.makenot.work".to_string(), ], ) .unwrap() } proptest! { // Arbitrary input never panics, and the output is always valid CSS // (it re-parses cleanly). #[test] fn never_panics_output_reparses(input in "\\PC{0,400}") { let (out, _rej) = sanitize_css(&input, SCOPE, &policy()); prop_assert!(StyleSheet::parse(&out, parser_options()).is_ok(), "invalid output: {out}"); } // A randomly-built external url() is always neutralized. #[test] fn external_url_always_stripped(host in "[a-z]{3,10}", tld in "(com|net|io|xyz)", path in "[a-z0-9]{1,10}") { let domain = format!("{host}.{tld}"); let css = format!(".x {{ background: url(https://{domain}/{path}) }}"); let out = sanitize_css(&css, SCOPE, &policy()).0; let leaked = out.contains(&domain); prop_assert!(!leaked, "leaked host: {}", out); } // Every non-empty sanitized sheet confines its style rules to the canvas // and ends with the reduced-motion guard. #[test] fn always_scoped_and_guarded(sel in "[a-z][a-z0-9]{0,8}", prop in "(color|background-color|margin)") { let css = format!("{sel} {{ {prop}: inherit }}"); let out = sanitize_css(&css, SCOPE, &policy()).0; let scope_tag = format!("uc-{SCOPE}"); let has_scope = out.contains(&scope_tag); let has_guard = out.contains("prefers-reduced-motion"); prop_assert!(has_scope, "missing scope: {}", out); prop_assert!(has_guard, "missing guard: {}", out); } }