Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Report exception/important matches through engine.matches API #4

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions examples/cpp/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,39 @@ void Assert(bool value, const std::string& message) {
}

void Check(bool expected_result,
bool expected_saved_from_exception,
bool expected_did_match_exception,
bool expected_did_match_important,
std::string expected_redirect,
const std::string& test_description,
Engine& engine, const std::string& url, const std::string& host,
const std::string& tab_host, bool third_party,
const std::string& resource_type) {
bool saved_from_exception;
bool did_match_exception;
bool did_match_important;
std::string redirect;
bool match = engine.matches(url, host, tab_host, third_party,
resource_type, &saved_from_exception, &redirect);
resource_type, &did_match_exception, &did_match_important, &redirect,
false, true);
cout << test_description << "... ";
if (expected_result != match) {
cout << "Failed!" << endl;
cout << "Unexpected result: " << url << " in " << tab_host << endl;
num_failed++;
} else if (saved_from_exception != expected_saved_from_exception) {
} else if (did_match_exception != expected_did_match_exception) {
cout << "Failed!" << endl;
cout << "Unexpected saved from exception value: " << url <<
cout << "Unexpected did match exception value: " << url <<
" in " << tab_host << endl;
} else if (did_match_important != expected_did_match_important) {
cout << "Failed!" << endl;
cout << "Unexpected did match important value: " << url <<
" in " << tab_host << endl;
} else {
cout << "Passed!" << endl;
num_passed++;
}
assert(expected_result == match);
assert(saved_from_exception == expected_saved_from_exception);
assert(did_match_exception == expected_did_match_exception);
assert(did_match_important == expected_did_match_important);
assert(redirect == expected_redirect);
}

Expand All @@ -106,42 +114,42 @@ void TestBasics() {
"-advertisement/script.\n"
"@@good-advertisement\n"
);
Check(true, false, "", "Basic match", engine, "http://example.com/-advertisement-icon.",
Check(true, false, false, "", "Basic match", engine, "http://example.com/-advertisement-icon.",
"example.com", "example.com", false , "image");
Check(false, false, "", "Basic not match", engine, "https://brianbondy.com",
Check(false, false, false, "", "Basic not match", engine, "https://brianbondy.com",
"brianbondy.com", "example.com", true, "image");
Check(false, true, "", "Basic saved from exception", engine, "http://example.com/good-advertisement-icon.",
Check(false, true, false, "", "Basic saved from exception", engine, "http://example.com/good-advertisement-icon.",
"example.com", "example.com", false, "image");
}

void TestDeserialization() {
Engine engine("");
engine.deserialize(reinterpret_cast<const char*>(ad_banner_dat_buffer),
sizeof(ad_banner_dat_buffer)/sizeof(ad_banner_dat_buffer[0]));
Check(true, false, "", "Basic match after deserialization", engine, "http://example.com/ad-banner.gif",
Check(true, false, false, "", "Basic match after deserialization", engine, "http://example.com/ad-banner.gif",
"example.com", "example.com", false , "image");

Engine engine2("");
engine2.deserialize(reinterpret_cast<const char*>(ad_banner_with_tag_abc_dat_buffer),
sizeof(ad_banner_with_tag_abc_dat_buffer)/sizeof(ad_banner_with_tag_abc_dat_buffer[0]));
Check(false, false, "", "Basic match after deserialization for a buffer with tags and no tag match", engine2, "http://example.com/ad-banner.gif",
Check(false, false, false, "", "Basic match after deserialization for a buffer with tags and no tag match", engine2, "http://example.com/ad-banner.gif",
"example.com", "example.com", false , "image");
engine2.addTag("abc");
Check(true, false, "", "Basic match after deserialization for a buffer with tags and a tag match", engine2, "http://example.com/ad-banner.gif",
Check(true, false, false, "", "Basic match after deserialization for a buffer with tags and a tag match", engine2, "http://example.com/ad-banner.gif",
"example.com", "example.com", false , "image");

// Deserialize after adding tag still works
Engine engine3("");
engine3.addTag("abc");
engine3.deserialize(reinterpret_cast<const char*>(ad_banner_with_tag_abc_dat_buffer),
sizeof(ad_banner_with_tag_abc_dat_buffer)/sizeof(ad_banner_with_tag_abc_dat_buffer[0]));
Check(true, false, "", "Basic match after deserialization with resources with a tag on the engine before", engine3, "http://example.com/ad-banner.gif",
Check(true, false, false, "", "Basic match after deserialization with resources with a tag on the engine before", engine3, "http://example.com/ad-banner.gif",
"example.com", "example.com", false , "image");

Engine engine4("");
engine4.deserialize(reinterpret_cast<const char*>(ad_banner_with_resources_abc_dat_buffer),
sizeof(ad_banner_with_resources_abc_dat_buffer)/sizeof(ad_banner_with_resources_abc_dat_buffer[0]));
Check(true, false, "data:text/plain;base64,", "Basic match after deserialization with resources", engine4, "http://example.com/ad-banner.gif",
Check(true, false, false, "data:text/plain;base64,", "Basic match after deserialization with resources", engine4, "http://example.com/ad-banner.gif",
"example.com", "example.com", false , "image");
}

Expand All @@ -150,22 +158,22 @@ void TestTags() {
"-advertisement-management$tag=abc\n"
"-advertisement.$tag=abc\n"
"-advertisement/script.$tag=abc\n");
Check(false, false, "", "Without needed tags", engine,
Check(false, false, false, "", "Without needed tags", engine,
"http://example.com/-advertisement-icon.", "example.com", "example.com",
false, "image");
engine.addTag("abc");
Assert(engine.tagExists("abc"), "abc tag should exist");
Assert(!engine.tagExists("abcd"), "abcd should not exist");
Check(true, false, "", "With needed tags",
Check(true, false, false, "", "With needed tags",
engine, "http://example.com/-advertisement-icon.", "example.com",
"example.com", false, "image");
// Adding a second tag doesn't clear the first.
engine.addTag("hello");
Check(true, false, "", "With extra unneeded tags",
Check(true, false, false, "", "With extra unneeded tags",
engine, "http://example.com/-advertisement-icon.", "example.com",
"example.com", false, "image");
engine.removeTag("abc");
Check(false, false, "", "With removed tags",
Check(false, false, false, "", "With removed tags",
engine, "http://example.com/-advertisement-icon.", "example.com",
"example.com", false, "image");
}
Expand All @@ -176,15 +184,15 @@ void TestRedirects() {
"\"aliases\": [],"
"\"kind\": {\"mime\": \"image/gif\"},"
"\"content\":\"R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\"}]");
Check(true, false, "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "Testing redirects match", engine,
Check(true, false, false, "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==", "Testing redirects match", engine,
"http://example.com/-advertisement-icon.", "example.com", "example.com",
false, "image");
}

void TestRedirect() {
Engine engine("-advertisement-$redirect=test\n");
engine.addResource("test", "application/javascript", "YWxlcnQoMSk=");
Check(true, false, "data:application/javascript;base64,YWxlcnQoMSk=", "Testing single redirect match", engine,
Check(true, false, false, "data:application/javascript;base64,YWxlcnQoMSk=", "Testing single redirect match", engine,
"http://example.com/-advertisement-icon.", "example.com", "example.com",
false, "image");
}
Expand All @@ -202,14 +210,37 @@ void TestRedirect() {

void TestThirdParty() {
Engine engine("-advertisement-icon$third-party");
Check(true, false, "", "Without needed tags", engine,
Check(true, false, false, "", "Without needed tags", engine,
"http://example.com/-advertisement-icon", "example.com", "brianbondy.com",
true, "image");
Check(false, false, "", "Without needed tags", engine,
Check(false, false, false, "", "Without needed tags", engine,
"http://example.com/-advertisement-icon", "example.com", "example.com",
false, "image");
}

void TestImportant() {
Engine engine("-advertisement-icon$important\n"
"@@-advertisement-icon-good\n");
Check(true, false, true, "", "Exactly matching important rule", engine,
"http://example.com/-advertisement-icon", "example.com", "example.com",
false, "image");
Check(true, false, true, "", "Matching exception rule and important rule", engine,
"http://example.com/-advertisement-icon-good", "example.com", "example.com",
false, "image");
}

void TestException() {
Engine engine("*banner.png\n");
Check(true, false, false, "", "Without exception", engine,
"http://example.com/ad_banner.png", "example.com", "example.com",
false, "image");

Engine engine2("@@*ad_banner.png\n");
Check(false, true, false, "", "With exception", engine2,
"http://example.com/ad_banner.png", "example.com", "example.com",
false, "image");
}

void TestClassId() {
Engine engine(
"###element\n"
Expand Down Expand Up @@ -377,6 +408,8 @@ int main() {
TestRedirects();
TestRedirect();
TestThirdParty();
TestImportant();
TestException();
TestClassId();
TestUrlCosmetics();
TestSubdomainUrlCosmetics();
Expand Down
29 changes: 16 additions & 13 deletions src/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,60 @@ bool set_domain_resolver(C_DomainResolverCallback resolver);
/**
* Create a new `Engine`.
*/
C_Engine *engine_create(const char *rules);
struct C_Engine *engine_create(const char *rules);

/**
* Checks if a `url` matches for the specified `Engine` within the context.
*/
bool engine_match(C_Engine *engine,
bool engine_match(struct C_Engine *engine,
const char *url,
const char *host,
const char *tab_host,
bool third_party,
const char *resource_type,
bool *saved_from_exception,
char **redirect);
bool *did_match_exception,
bool *did_match_important,
char **redirect,
bool previously_matched_rule,
bool force_check_exceptions);

/**
* Adds a tag to the engine for consideration
*/
void engine_add_tag(C_Engine *engine, const char *tag);
void engine_add_tag(struct C_Engine *engine, const char *tag);

/**
* Checks if a tag exists in the engine
*/
bool engine_tag_exists(C_Engine *engine, const char *tag);
bool engine_tag_exists(struct C_Engine *engine, const char *tag);

/**
* Adds a resource to the engine by name
*/
bool engine_add_resource(C_Engine *engine,
bool engine_add_resource(struct C_Engine *engine,
const char *key,
const char *content_type,
const char *data);

/**
* Adds a list of `Resource`s from JSON format
*/
void engine_add_resources(C_Engine *engine, const char *resources);
void engine_add_resources(struct C_Engine *engine, const char *resources);

/**
* Removes a tag to the engine for consideration
*/
void engine_remove_tag(C_Engine *engine, const char *tag);
void engine_remove_tag(struct C_Engine *engine, const char *tag);

/**
* Deserializes a previously serialized data file list.
*/
bool engine_deserialize(C_Engine *engine, const char *data, size_t data_size);
bool engine_deserialize(struct C_Engine *engine, const char *data, size_t data_size);

/**
* Destroy a `Engine` once you are done with it.
*/
void engine_destroy(C_Engine *engine);
void engine_destroy(struct C_Engine *engine);

/**
* Destroy a `*c_char` once you are done with it.
Expand All @@ -90,14 +93,14 @@ void c_char_buffer_destroy(char *s);
/**
* Returns a set of cosmetic filtering resources specific to the given url, in JSON format
*/
char *engine_url_cosmetic_resources(C_Engine *engine, const char *url);
char *engine_url_cosmetic_resources(struct C_Engine *engine, const char *url);

/**
* Returns a stylesheet containing all generic cosmetic rules that begin with any of the provided class and id selectors
*
* The leading '.' or '#' character should not be provided
*/
char *engine_hidden_class_id_selectors(C_Engine *engine,
char *engine_hidden_class_id_selectors(struct C_Engine *engine,
const char *const *classes,
size_t classes_size,
const char *const *ids,
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,29 @@ pub unsafe extern "C" fn engine_match(
tab_host: *const c_char,
third_party: bool,
resource_type: *const c_char,
saved_from_exception: *mut bool,
did_match_exception: *mut bool,
did_match_important: *mut bool,
redirect: *mut *mut c_char,
previously_matched_rule: bool,
force_check_exceptions: bool,
) -> bool {
let url = CStr::from_ptr(url).to_str().unwrap();
let host = CStr::from_ptr(host).to_str().unwrap();
let tab_host = CStr::from_ptr(tab_host).to_str().unwrap();
let resource_type = CStr::from_ptr(resource_type).to_str().unwrap();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
let blocker_result = engine.check_network_urls_with_hostnames(
let blocker_result = engine.check_network_urls_with_hostnames_subset(
url,
host,
tab_host,
resource_type,
Some(third_party),
previously_matched_rule,
force_check_exceptions,
);
*saved_from_exception = blocker_result.filter != None && blocker_result.exception != None;
*did_match_exception = blocker_result.exception.is_some();
*did_match_important = blocker_result.important;
*redirect = match blocker_result.redirect {
Some(x) => match CString::new(x) {
Ok(y) => y.into_raw(),
Expand Down
9 changes: 5 additions & 4 deletions src/wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ Engine::Engine(const std::string& rules) : raw(engine_create(rules.c_str())) {

bool Engine::matches(const std::string& url, const std::string& host,
const std::string& tab_host, bool is_third_party,
const std::string& resource_type, bool* saved_from_exception,
std::string* redirect) {
const std::string& resource_type, bool* did_match_exception,
bool* did_match_important, std::string* redirect, bool
previously_matched_rule, bool previously_matched_exception) {
char* redirect_char_ptr = nullptr;
bool result = engine_match(raw, url.c_str(), host.c_str(),tab_host.c_str(),
is_third_party, resource_type.c_str(), saved_from_exception,
&redirect_char_ptr);
is_third_party, resource_type.c_str(), did_match_exception, did_match_important,
&redirect_char_ptr, previously_matched_rule, previously_matched_exception);
if (redirect_char_ptr) {
if (redirect) {
*redirect = redirect_char_ptr;
Expand Down
5 changes: 3 additions & 2 deletions src/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class ADBLOCK_EXPORT Engine {
Engine(const std::string& rules);
bool matches(const std::string& url, const std::string& host,
const std::string& tab_host, bool is_third_party,
const std::string& resource_type, bool* saved_from_exception,
std::string *redirect);
const std::string& resource_type, bool* did_match_exception,
bool* did_match_important, std::string *redirect,
bool previously_matched_rule, bool previously_matched_exception);
bool deserialize(const char* data, size_t data_size);
void addTag(const std::string& tag);
void addResource(const std::string& key,
Expand Down