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

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

shields.rs131 lines · 4.9 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 let kind = "script".to_string();
84 let request = Request::new(&url, &source, &kind, "get").expect("test urls must parse");
85 let result = engine.check_network_request(&request);
86 assert!(!result.should_block());
87 let rewritten = result.rewritten_url.expect("removeparam must rewrite the url");
88 assert!(rewritten.contains("id=7"), "kept params must survive: {rewritten}");
89 assert!(!rewritten.contains("utm_source"), "tracking param must be gone: {rewritten}");
90}
91
92#[test]
93fn cosmetic_filter_yields_hide_selector() {
94 let engine = engine_with("example.com##.adbox");
95 let res = engine.url_cosmetic_resources("https://example.com/article");
96 assert!(res.hide_selectors.contains(".adbox"));
97}
98
99#[test]
100fn generic_cosmetic_rule_matches_an_observed_class() {
101 let engine = engine_with("##.promo");
102 let selectors = engine.hidden_class_id_selectors(
103 vec!["promo".to_string()],
104 Vec::<String>::new(),
105 &HashSet::new(),
106 );
107 assert!(selectors.iter().any(|s| s.contains("promo")));
108}
109
110#[test]
111fn serialize_deserialize_roundtrip_preserves_blocking() {
112 let engine = engine_with("||ads.example.com^");
113 let bytes = engine.serialize();
114 let mut restored = Engine::default();
115 restored.deserialize(&bytes).expect("cache must deserialize");
116 let (blocked, _, _) =
117 check("https://ads.example.com/x.js", "https://news.example/", "script", &restored);
118 assert!(blocked);
119}
120
121#[test]
122fn invalid_resources_json_leaves_the_engine_working() {
123 let bad: Result<Vec<Resource>, _> = serde_json::from_str("not json");
124 assert!(bad.is_err());
125 let empty: Vec<Resource> = serde_json::from_str("[]").expect("empty resources must parse");
126 let mut engine = engine_with("||ads.example.com^");
127 engine.use_resources(empty);
128 let (blocked, _, _) =
129 check("https://ads.example.com/x.js", "https://news.example/", "script", &engine);
130 assert!(blocked);
131}