-
Notifications
You must be signed in to change notification settings - Fork 176
Don't use old method in tests #560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8cb4178
a163215
8967aea
c167d3e
fd26238
7e6fe72
5035c10
9102886
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,6 +139,11 @@ impl Engine { | |
| self.blocker.check(request, &self.resources) | ||
| } | ||
|
|
||
| #[doc(hidden)] | ||
| pub(crate) fn check_network_request_exceptions(&self, request: &Request) -> bool { | ||
| self.blocker.check_exceptions(request) | ||
| } | ||
|
|
||
| pub fn check_network_request_subset( | ||
| &self, | ||
| request: &Request, | ||
|
|
@@ -266,6 +271,11 @@ impl Engine { | |
| self.blocker.set_regex_discard_policy(new_discard_policy); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub fn borrow_regex_manager(&self) -> crate::blocker::RegexManagerRef<'_> { | ||
|
Comment on lines
+274
to
+275
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment describing why this is necessary would be helpful (I can tell from the diffs below, but later it won't be as obvious) |
||
| self.blocker.borrow_regex_manager() | ||
| } | ||
|
|
||
| #[cfg(feature = "debug-info")] | ||
| pub fn discard_regex(&mut self, regex_id: u64) { | ||
| self.blocker.discard_regex(regex_id); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -969,6 +969,18 @@ impl NetworkFilter { | |
| FilterTokens::Other(t) | ||
| } | ||
| } | ||
|
|
||
| #[doc(hidden)] | ||
| pub fn matches_test(&self, request: &request::Request) -> bool { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let filter_set = crate::FilterSet::new_with_rules(vec![self.clone()], vec![], true); | ||
| let engine = crate::Engine::from_filter_set(filter_set, true); | ||
|
|
||
| if self.is_exception() { | ||
| engine.check_network_request_exceptions(request) | ||
| } else { | ||
| engine.check_network_request(request).matched | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl NetworkFilterMaskHelper for NetworkFilter { | ||
|
|
@@ -993,30 +1005,6 @@ pub trait NetworkMatchable { | |
| fn matches_test(&self, request: &request::Request) -> bool; | ||
| } | ||
|
|
||
| impl NetworkMatchable for NetworkFilter { | ||
| fn matches(&self, request: &request::Request, regex_manager: &mut RegexManager) -> bool { | ||
|
Comment on lines
-996
to
-997
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fwiw, this is a breaking API change and will require another bump up to |
||
| use crate::filters::network_matchers::{ | ||
| check_excluded_domains, check_included_domains, check_options, check_pattern, | ||
| }; | ||
| check_options(self.mask, request) | ||
| && check_included_domains(self.opt_domains.as_deref(), request) | ||
| && check_excluded_domains(self.opt_not_domains.as_deref(), request) | ||
| && check_pattern( | ||
| self.mask, | ||
| self.filter.iter(), | ||
| self.hostname.as_deref(), | ||
| (self as *const NetworkFilter) as u64, | ||
| request, | ||
| regex_manager, | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| fn matches_test(&self, request: &request::Request) -> bool { | ||
| self.matches(request, &mut RegexManager::default()) | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Filter parsing | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -350,9 +350,38 @@ mod match_tests { | |
| } | ||
|
|
||
| fn check_options(filter: &NetworkFilter, request: &request::Request) -> bool { | ||
| let mut mapping = HashMap::new(); | ||
| let opt_domains = filter.opt_domains.clone().map(|domains| { | ||
| domains | ||
| .iter() | ||
| .map(|domain| { | ||
| mapping.insert(*domain, *domain as u32); | ||
| *domain as u32 | ||
| }) | ||
| .collect::<Vec<u32>>() | ||
| }); | ||
|
|
||
| let opt_not_domains = filter.opt_not_domains.clone().map(|domains| { | ||
| domains | ||
| .iter() | ||
| .map(|domain| { | ||
| mapping.insert(*domain, *domain as u32); | ||
| *domain as u32 | ||
| }) | ||
| .collect::<Vec<u32>>() | ||
| }); | ||
|
|
||
| super::super::check_options(filter.mask, request) | ||
| && super::super::check_included_domains(filter.opt_domains.as_deref(), request) | ||
| && super::super::check_excluded_domains(filter.opt_not_domains.as_deref(), request) | ||
| && super::super::check_included_domains_mapped( | ||
| opt_domains.as_deref(), | ||
| request, | ||
| &mapping, | ||
| ) | ||
| && super::super::check_excluded_domains_mapped( | ||
| opt_not_domains.as_deref(), | ||
| request, | ||
| &mapping, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -659,22 +688,34 @@ mod match_tests { | |
| } | ||
|
|
||
| #[test] | ||
| #[ignore] // Not going to handle lookaround regexes | ||
| #[cfg(feature = "debug-info")] | ||
| fn check_lookaround_regex_handled() { | ||
| { | ||
| use crate::Engine; | ||
| let filter = r#"/^https?:\/\/([0-9a-z\-]+\.)?(9anime|animeland|animenova|animeplus|animetoon|animewow|gamestorrent|goodanime|gogoanime|igg-games|kimcartoon|memecenter|readcomiconline|toonget|toonova|watchcartoononline)\.[a-z]{2,4}\/(?!([Ee]xternal|[Ii]mages|[Ss]cripts|[Uu]ploads|ac|ajax|assets|combined|content|cov|cover|(img\/bg)|(img\/icon)|inc|jwplayer|player|playlist-cat-rss|static|thumbs|wp-content|wp-includes)\/)(.*)/$image,other,script,~third-party,xmlhttprequest,domain=~animeland.hu"#; | ||
| let network_filter = NetworkFilter::parse(filter, true, Default::default()).unwrap(); | ||
| let url = "https://data.foo.com/9VjjrjU9Or2aqkb8PDiqTBnULPgeI48WmYEHkYer"; | ||
| let source = "http://123movies.com"; | ||
| let engine = Engine::from_rules(vec![filter], Default::default()); | ||
| let url = "https://9anime.to/watch/episode-1"; | ||
| let source = "https://9anime.to"; | ||
| let request = request::Request::new(url, source, "script").unwrap(); | ||
| let mut regex_manager = RegexManager::default(); | ||
| assert!(regex_manager.get_compiled_regex_count() == 0); | ||
| assert_eq!( | ||
| engine | ||
| .get_debug_info() | ||
| .regex_debug_info | ||
| .compiled_regex_count, | ||
| 0 | ||
| ); | ||
| // Regex can't be compiled, so no match. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the test case added here (already in the disabled state): regex crate can't parse such regex, so I changed the test to verify it doesn't match |
||
| assert!( | ||
| network_filter.matches(&request, &mut regex_manager), | ||
| "Expected match for {filter} on {url}" | ||
| !engine.check_network_request(&request).matched, | ||
| "Expected no match for {filter} on {url}" | ||
| ); | ||
| assert_eq!( | ||
| engine | ||
| .get_debug_info() | ||
| .regex_debug_info | ||
| .compiled_regex_count, | ||
| 1 | ||
| ); | ||
| assert!(regex_manager.get_compiled_regex_count() == 1); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to include
#[doc(hidden)]onpub(crate)items