Skip to content

Commit

Permalink
Add ParseOptions to control parsing of redirect-url
Browse files Browse the repository at this point in the history
Test tweaks
  • Loading branch information
ShivanKaul committed Sep 15, 2021
1 parent 900ce93 commit b2c2039
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 187 deletions.
66 changes: 47 additions & 19 deletions src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ mod tests {
let filters = vec!["||foo.com"];
let network_filters: Vec<_> = filters
.into_iter()
.map(|f| NetworkFilter::parse(&f, true))
.map(|f| NetworkFilter::parse(&f, true, Default::default()))
.filter_map(Result::ok)
.collect();
let filter_list = NetworkFilterList::new(network_filters, false);
Expand All @@ -939,7 +939,7 @@ mod tests {
let filters = vec!["||foo.com", "||bar.com/foo"];
let network_filters: Vec<_> = filters
.into_iter()
.map(|f| NetworkFilter::parse(&f, true))
.map(|f| NetworkFilter::parse(&f, true, Default::default()))
.filter_map(Result::ok)
.collect();
let filter_list = NetworkFilterList::new(network_filters, false);
Expand All @@ -957,7 +957,7 @@ mod tests {
let filters = vec!["||foo.com", "||foo.com/bar", "||www"];
let network_filters: Vec<_> = filters
.into_iter()
.map(|f| NetworkFilter::parse(&f, true))
.map(|f| NetworkFilter::parse(&f, true, Default::default()))
.filter_map(Result::ok)
.collect();
let filter_list = NetworkFilterList::new(network_filters, false);
Expand All @@ -976,7 +976,7 @@ mod tests {
let filters = vec!["||foo.com", "||foo.com$domain=bar.com"];
let network_filters: Vec<_> = filters
.into_iter()
.map(|f| NetworkFilter::parse(&f, true))
.map(|f| NetworkFilter::parse(&f, true, Default::default()))
.filter_map(Result::ok)
.collect();
let filter_list = NetworkFilterList::new(network_filters, false);
Expand All @@ -999,7 +999,7 @@ mod tests {
let filters = vec!["foo*$domain=bar.com|baz.com"];
let network_filters: Vec<_> = filters
.into_iter()
.map(|f| NetworkFilter::parse(&f, true))
.map(|f| NetworkFilter::parse(&f, true, Default::default()))
.filter_map(Result::ok)
.collect();
let filter_list = NetworkFilterList::new(network_filters, false);
Expand Down Expand Up @@ -1036,7 +1036,7 @@ mod tests {
fn test_requests_filters(filters: &Vec<&str>, requests: &Vec<(Request, bool)>) {
let network_filters: Vec<_> = filters
.into_iter()
.map(|f| NetworkFilter::parse(&f, true))
.map(|f| NetworkFilter::parse(&f, true, Default::default()))
.filter_map(Result::ok)
.collect();
let filter_list = NetworkFilterList::new(network_filters, false);
Expand Down Expand Up @@ -1201,7 +1201,7 @@ mod tests {
mod blocker_tests {

use super::*;
use crate::lists::{parse_filters, FilterFormat};
use crate::lists::{parse_filters, FilterFormat, parse_filters_with_opts, ParseOptions};
use crate::request::Request;
use std::collections::HashSet;
use std::iter::FromIterator;
Expand Down Expand Up @@ -1233,7 +1233,9 @@ mod blocker_tests {

let request = Request::from_urls("https://foo.com", "https://foo.com", "script").unwrap();

let (network_filters, _) = parse_filters(&filters, true, FilterFormat::Standard);
let opts = ParseOptions { parse_redirect_urls: true, ..Default::default() };

let (network_filters, _) = parse_filters_with_opts(&filters, true, opts);

let blocker_options: BlockerOptions = BlockerOptions {
enable_optimizations: false,
Expand All @@ -1248,6 +1250,28 @@ mod blocker_tests {
assert_eq!(matched_rule.error, None);
}

#[test]
fn redirect_url_not_recognized_without_parse_opt() {
let filters = vec![
String::from("||foo.com$important,redirect-url=http://xyz.com"),
];

let request = Request::from_urls("https://foo.com", "https://foo.com", "script").unwrap();

let (network_filters, _) = parse_filters(&filters, true, FilterFormat::Standard);

let blocker_options: BlockerOptions = BlockerOptions {
enable_optimizations: false,
};

let blocker = Blocker::new(network_filters, &blocker_options);

let matched_rule = blocker.check(&request);
assert_eq!(matched_rule.matched, false);
assert_eq!(matched_rule.redirect, None);
assert_eq!(matched_rule.error, None);
}

#[test]
fn redirect_url_malformed() {
let filters = vec![
Expand All @@ -1256,7 +1280,9 @@ mod blocker_tests {

let request = Request::from_urls("https://foo.com", "https://foo.com", "script").unwrap();

let (network_filters, _) = parse_filters(&filters, true, FilterFormat::Standard);
let opts = ParseOptions { parse_redirect_urls: true, ..Default::default() };

let (network_filters, _) = parse_filters_with_opts(&filters, true, opts);

let blocker_options: BlockerOptions = BlockerOptions {
enable_optimizations: false,
Expand All @@ -1280,7 +1306,9 @@ mod blocker_tests {

let request = Request::from_urls("https://imdb-video.media-imdb.com/kBOeI88k1o23eNAi", "https://www.imdb.com/video/13", "media").unwrap();

let (network_filters, _) = parse_filters(&filters, true, FilterFormat::Standard);
let opts = ParseOptions { parse_redirect_urls: true, ..Default::default() };

let (network_filters, _) = parse_filters_with_opts(&filters, true, opts);

let blocker_options: BlockerOptions = BlockerOptions {
enable_optimizations: false,
Expand Down Expand Up @@ -1596,7 +1624,7 @@ mod blocker_tests {

let mut blocker = Blocker::new(Vec::new(), &blocker_options);

let filter = NetworkFilter::parse("adv$badfilter", true).unwrap();
let filter = NetworkFilter::parse("adv$badfilter", true, Default::default()).unwrap();
let added = blocker.add_filter(filter);
assert!(added.is_err());
assert_eq!(added.err().unwrap(), BlockerError::BadFilterAddUnsupported);
Expand All @@ -1613,7 +1641,7 @@ mod blocker_tests {

let mut blocker = Blocker::new(Vec::new(), &blocker_options);

let filter = NetworkFilter::parse("adv", true).unwrap();
let filter = NetworkFilter::parse("adv", true, Default::default()).unwrap();
blocker.add_filter(filter.clone()).unwrap();
assert!(blocker.filter_exists(&filter), "Expected filter to be inserted");
let added = blocker.add_filter(filter);
Expand All @@ -1628,7 +1656,7 @@ mod blocker_tests {

let mut blocker = Blocker::new(Vec::new(), &blocker_options);

let filter = NetworkFilter::parse("adv", true).unwrap();
let filter = NetworkFilter::parse("adv", true, Default::default()).unwrap();
blocker.add_filter(filter.clone()).unwrap();
let added = blocker.add_filter(filter);
assert!(added.is_ok());
Expand All @@ -1645,10 +1673,10 @@ mod blocker_tests {
let mut blocker = Blocker::new(Vec::new(), &blocker_options);
blocker.enable_tags(&["brian"]);

blocker.add_filter(NetworkFilter::parse("adv$tag=stuff", true).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("somelongpath/test$tag=stuff", true).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("||brianbondy.com/$tag=brian", true).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("||brave.com$tag=brian", true).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("adv$tag=stuff", true, Default::default()).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("somelongpath/test$tag=stuff", true, Default::default()).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("||brianbondy.com/$tag=brian", true, Default::default()).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("||brave.com$tag=brian", true, Default::default()).unwrap()).unwrap();

let url_results = vec![
(Request::from_url("http://example.com/advert.html").unwrap(), false),
Expand All @@ -1675,7 +1703,7 @@ mod blocker_tests {

let mut blocker = Blocker::new(Vec::new(), &blocker_options);

blocker.add_filter(NetworkFilter::parse("@@*ad_banner.png", true).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("@@*ad_banner.png", true, Default::default()).unwrap()).unwrap();

let request = Request::from_url("http://example.com/ad_banner.png").unwrap();

Expand All @@ -1692,7 +1720,7 @@ mod blocker_tests {

let mut blocker = Blocker::new(Vec::new(), &blocker_options);

blocker.add_filter(NetworkFilter::parse("@@||example.com$generichide", true).unwrap()).unwrap();
blocker.add_filter(NetworkFilter::parse("@@||example.com$generichide", true, Default::default()).unwrap()).unwrap();

assert!(blocker.check_generic_hide(&Request::from_url("https://example.com").unwrap()));
}
Expand Down
20 changes: 16 additions & 4 deletions src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::blocker::{Blocker, BlockerError, BlockerOptions, BlockerResult};
use crate::cosmetic_filter_cache::{CosmeticFilterCache, UrlSpecificResources};
use crate::lists::{FilterFormat, FilterSet};
use crate::lists::{FilterFormat, FilterSet, ParseOptions};
use crate::request::Request;
use crate::resources::{Resource, RedirectResource};

Expand Down Expand Up @@ -44,12 +44,24 @@ impl Engine {

/// Loads rules, enabling optimizations and including debug information.
pub fn from_rules_debug(rules: &[String], format: FilterFormat) -> Self {
Self::from_rules_parametrised(&rules, format, true, true)
let opts = ParseOptions { format, ..Default::default() };
Self::from_rules_debug_with_opts(&rules, opts)
}

/// Loads rules, enabling optimizations and including debug information.
/// Include opts
pub fn from_rules_debug_with_opts(rules: &[String], opts: ParseOptions) -> Self {
Self::from_rules_parametrised_with_opts(&rules, opts, true, true)
}

pub fn from_rules_parametrised(filter_rules: &[String], format: FilterFormat, debug: bool, optimize: bool) -> Self {
let opts = ParseOptions { format, ..Default::default() };
Self::from_rules_parametrised_with_opts(filter_rules, opts, debug, optimize)
}

pub fn from_rules_parametrised_with_opts(filter_rules: &[String], opts: ParseOptions, debug: bool, optimize: bool) -> Self {
let mut filter_set = FilterSet::new(debug);
filter_set.add_filters(filter_rules, format);
filter_set.add_filters_with_opts(filter_rules, opts);
Self::from_filter_set(filter_set, optimize)
}

Expand Down Expand Up @@ -184,7 +196,7 @@ impl Engine {
/// Note that only network filters are currently supported by this method.
pub fn filter_exists(&self, filter: &str) -> bool {
use crate::filters::network::NetworkFilter;
let filter_parsed = NetworkFilter::parse(filter, false);
let filter_parsed = NetworkFilter::parse(filter, false, Default::default());
match filter_parsed.map(|f| self.blocker.filter_exists(&f)) {
Ok(exists) => exists,
Err(_e) => {
Expand Down
Loading

0 comments on commit b2c2039

Please sign in to comment.