Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / web / src / main / rust / tests / shields.rs

shields.rs134 lines · 5.1 KiBRaw
1//! Host-side integration tests for the `web_shields` Brave Shields engine.
2//!
3//! Seam note: `src/lib.rs` is a thin JNI wrapper around Brave's `adblock`
4//! `Engine` — every `native*` entry point only moves JSON strings across the
5//! boundary, and the two pure helpers (`json_string_array`, `apply_resources`)
6//! are private to the crate. These tests therefore pin the `Engine`
7//! filter-syntax semantics the wrappers forward as JSON (network blocking,
8//! exception precedence, `$important`, `$removeparam` rewrites, cosmetic
9//! rules, class/id mapping, cache serialize round-trips and the
10//! invalid-resources fallback) without crossing JNI. If a test here fails,
11//! the Kotlin-visible behaviour behind `ShieldsNative` changed too.
12
13use adblock::request::Request;
14use adblock::resources::Resource;
15use adblock::Engine;
16use std::collections::HashSet;
17
18fn engine_with(filters: &str) -> Engine {
19 Engine::new_with_list_text(filters.to_string())
20}
21
22/// Mirrors `nativeCheck`: build the request exactly as the JNI layer does and
23/// return `(blocked, important, has_exception)`.
24fn check(url: &str, source: &str, kind: &str, engine: &Engine) -> (bool, bool, bool) {
25 let url = url.to_string();
26 let source = source.to_string();
27 let kind = kind.to_string();
28 let request = Request::new(&url, &source, &kind, "get").expect("test urls must parse");
29 let result = engine.check_network_request(&request);
30 (result.should_block(), result.important, result.exception.is_some())
31}
32
33#[test]
34fn blocks_request_matching_a_domain_rule() {
35 let engine = engine_with("||ads.example.com^");
36 let (blocked, _, _) =
37 check("https://ads.example.com/banner.js", "https://news.example/", "script", &engine);
38 assert!(blocked);
39}
40
41#[test]
42fn allows_unrelated_request() {
43 let engine = engine_with("||ads.example.com^");
44 let (blocked, _, _) =
45 check("https://cdn.example.com/app.js", "https://news.example/", "script", &engine);
46 assert!(!blocked);
47}
48
49#[test]
50fn empty_filter_list_blocks_nothing() {
51 let engine = engine_with("");
52 let (blocked, _, _) =
53 check("https://ads.example.com/x.js", "https://news.example/", "script", &engine);
54 assert!(!blocked);
55}
56
57#[test]
58fn exception_rule_lifts_the_block() {
59 let engine = engine_with("||ads.example.com^\n@@||ads.example.com/allowed^");
60 let (blocked, _, has_exception) = check(
61 "https://ads.example.com/allowed/banner.js",
62 "https://news.example/",
63 "script",
64 &engine,
65 );
66 assert!(!blocked);
67 assert!(has_exception);
68}
69
70#[test]
71fn important_flag_is_reported() {
72 let engine = engine_with("||ads.example.com^$important");
73 let (_, important, _) =
74 check("https://ads.example.com/x.js", "https://news.example/", "script", &engine);
75 assert!(important);
76}
77
78#[test]
79fn removeparam_rewrites_tracking_urls_without_blocking() {
80 let engine = engine_with("||example.com^$removeparam=utm_source");
81 let url = "https://example.com/p?utm_source=google&id=7".to_string();
82 let source = "https://news.example/".to_string();
83 // A typeless $removeparam rule applies to document/subdocument/XHR
84 // requests only (adblock's NetworkFilterMask default for IS_REMOVEPARAM);
85 // a script request is legitimately not rewritten by it.
86 let kind = "document".to_string();
87 let request = Request::new(&url, &source, &kind, "get").expect("test urls must parse");
88 let result = engine.check_network_request(&request);
89 assert!(!result.should_block());
90 let rewritten = result.rewritten_url.expect("removeparam must rewrite the url");
91 assert!(rewritten.contains("id=7"), "kept params must survive: {rewritten}");
92 assert!(!rewritten.contains("utm_source"), "tracking param must be gone: {rewritten}");
93}
94
95#[test]
96fn cosmetic_filter_yields_hide_selector() {
97 let engine = engine_with("example.com##.adbox");
98 let res = engine.url_cosmetic_resources("https://example.com/article");
99 assert!(res.hide_selectors.contains(".adbox"));
100}
101
102#[test]
103fn generic_cosmetic_rule_matches_an_observed_class() {
104 let engine = engine_with("##.promo");
105 let selectors = engine.hidden_class_id_selectors(
106 vec!["promo".to_string()],
107 Vec::<String>::new(),
108 &HashSet::new(),
109 );
110 assert!(selectors.iter().any(|s| s.contains("promo")));
111}
112
113#[test]
114fn serialize_deserialize_roundtrip_preserves_blocking() {
115 let engine = engine_with("||ads.example.com^");
116 let bytes = engine.serialize();
117 let mut restored = Engine::default();
118 restored.deserialize(&bytes).expect("cache must deserialize");
119 let (blocked, _, _) =
120 check("https://ads.example.com/x.js", "https://news.example/", "script", &restored);
121 assert!(blocked);
122}
123
124#[test]
125fn invalid_resources_json_leaves_the_engine_working() {
126 let bad: Result<Vec<Resource>, _> = serde_json::from_str("not json");
127 assert!(bad.is_err());
128 let empty: Vec<Resource> = serde_json::from_str("[]").expect("empty resources must parse");
129 let mut engine = engine_with("||ads.example.com^");
130 engine.use_resources(empty);
131 let (blocked, _, _) =
132 check("https://ads.example.com/x.js", "https://news.example/", "script", &engine);
133 assert!(blocked);
134}