Skip to content
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

Cleanup brave shields tests and get rid of dead code. #3645

Merged
merged 5 commits into from
Oct 20, 2019
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
187 changes: 61 additions & 126 deletions browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,176 +11,111 @@

#include "brave/browser/net/url_context.h"
#include "brave/common/network_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

using brave::GetPolyfillForAdBlock;
using brave::ResponseCallback;

namespace {

class BraveAdBlockTPNetworkDelegateHelperTest: public testing::Test {
public:
BraveAdBlockTPNetworkDelegateHelperTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
context_(new net::TestURLRequestContext(true)) {
}
~BraveAdBlockTPNetworkDelegateHelperTest() override {}
void SetUp() override {
context_->Init();
}
net::TestURLRequestContext* context() { return context_.get(); }

private:
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<net::TestURLRequestContext> context_;
};


TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, NoChangeURL) {
net::TestDelegate test_delegate;
GURL url("https://bradhatesprimes.brave.com/composite_numbers_ftw");
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(url, net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_TRUE(brave_request_info->new_url_spec.empty());
EXPECT_EQ(ret, net::OK);
TEST(BraveAdBlockTPNetworkDelegateHelperTest, NoChangeURL) {
const GURL url("https://bradhatesprimes.brave.com/composite_numbers_ftw");
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
int rc =
OnBeforeURLRequest_AdBlockTPPreWork(ResponseCallback(), request_info);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(rc, net::OK);
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, EmptyRequestURL) {
net::TestDelegate test_delegate;
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(GURL(), net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_TRUE(brave_request_info->new_url_spec.empty());
EXPECT_EQ(ret, net::OK);
TEST(BraveAdBlockTPNetworkDelegateHelperTest, EmptyRequestURL) {
auto request_info = std::make_shared<brave::BraveRequestInfo>(GURL());
int rc =
OnBeforeURLRequest_AdBlockTPPreWork(ResponseCallback(), request_info);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(rc, net::OK);
}


TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, RedirectsToStubs) {
std::vector<GURL> urls({
GURL(kGoogleTagManagerPattern),
GURL(kGoogleTagServicesPattern)
});
std::for_each(urls.begin(), urls.end(),
[this](GURL url){
net::TestDelegate test_delegate;
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(url, net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_EQ(ret, net::OK);
EXPECT_TRUE(GURL(brave_request_info->new_url_spec).SchemeIs("data"));
});
TEST(BraveAdBlockTPNetworkDelegateHelperTest, RedirectsToStubs) {
const std::vector<const GURL> urls(
{GURL(kGoogleTagManagerPattern), GURL(kGoogleTagServicesPattern)});
for (const auto& url : urls) {
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
int rc =
OnBeforeURLRequest_AdBlockTPPreWork(ResponseCallback(), request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_TRUE(GURL(request_info->new_url_spec).SchemeIs("data"));
}
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
std::vector<GURL> urls({
TEST(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
const std::vector<const GURL> urls({
GURL("https://pdfjs.robwu.nl/ping"),
});
std::for_each(urls.begin(), urls.end(), [this](GURL url){
net::TestDelegate test_delegate;
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(url, net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_STREQ(brave_request_info->new_url_spec.c_str(), kEmptyDataURI);
EXPECT_EQ(ret, net::OK);
});
for (const auto& url : urls) {
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
int rc =
OnBeforeURLRequest_AdBlockTPPreWork(ResponseCallback(), request_info);
EXPECT_EQ(request_info->new_url_spec, kEmptyDataURI);
EXPECT_EQ(rc, net::OK);
}
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, GetPolyfill) {
GURL tab_origin("https://test.com");
GURL google_analytics_url(kGoogleAnalyticsPattern);
GURL tag_manager_url(kGoogleTagManagerPattern);
GURL tag_services_url(kGoogleTagServicesPattern);
GURL normal_url("https://a.com");
TEST(BraveAdBlockTPNetworkDelegateHelperTest, GetPolyfill) {
using brave::GetPolyfillForAdBlock;

const GURL tab_origin("https://test.com");
const GURL google_analytics_url(kGoogleAnalyticsPattern);
const GURL tag_manager_url(kGoogleTagManagerPattern);
const GURL tag_services_url(kGoogleTagServicesPattern);
const GURL normal_url("https://a.com");

std::string out_url_spec;
// Shields up, block ads, google analytics should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin,
google_analytics_url, &out_url_spec));
google_analytics_url, &out_url_spec));
// Shields up, block ads, tag manager should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin, tag_manager_url,
&out_url_spec));
&out_url_spec));
// Shields up, block ads, tag services should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin, tag_services_url,
&out_url_spec));
&out_url_spec));
// Shields up, block ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, false, tab_origin, normal_url,
&out_url_spec));
&out_url_spec));

// Shields up, allow ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin,
google_analytics_url, &out_url_spec));
google_analytics_url, &out_url_spec));
// Shields up, allow ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, tag_manager_url,
&out_url_spec));
&out_url_spec));
// Shields up, allow ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, tag_services_url,
&out_url_spec));
&out_url_spec));
// Shields up, allow ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, normal_url,
&out_url_spec));
ASSERT_FALSE(
GetPolyfillForAdBlock(true, true, tab_origin, normal_url, &out_url_spec));

// Shields down, allow ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin,
google_analytics_url, &out_url_spec));
google_analytics_url, &out_url_spec));
// Shields down, allow ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, tag_manager_url,
&out_url_spec));
&out_url_spec));
// Shields down, allow ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, tag_services_url,
&out_url_spec));
&out_url_spec));
// Shields down, allow ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, normal_url,
&out_url_spec));
&out_url_spec));

// Shields down, block ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin,
google_analytics_url, &out_url_spec));
google_analytics_url, &out_url_spec));
// Shields down, block ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, tag_manager_url,
&out_url_spec));
&out_url_spec));
// Shields down, block ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin,
tag_services_url, &out_url_spec));
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, tag_services_url,
&out_url_spec));
// Shields down, block ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, normal_url,
&out_url_spec));
&out_url_spec));
}

} // namespace
50 changes: 10 additions & 40 deletions browser/net/brave_block_safebrowsing_urls_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,25 @@
#include <vector>

#include "brave/browser/net/url_context.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_constants.h"

namespace {

const char kInvalidUrl[] = "https://no-thanks.invalid";
} // namespace

class BraveBlockReportingUrlsHelperTest : public testing::Test {
public:
BraveBlockReportingUrlsHelperTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
context_(new net::TestURLRequestContext(true)) {}
~BraveBlockReportingUrlsHelperTest() override {}
void SetUp() override { context_->Init(); }
net::TestURLRequestContext* context() { return context_.get(); }
void CheckUrl(const std::string& test_url,
const char* expected_url,
int expected_error);

private:
content::TestBrowserThreadBundle thread_bundle_;
std::unique_ptr<net::TestURLRequestContext> context_;
};

void BraveBlockReportingUrlsHelperTest::CheckUrl(const std::string& test_url,
const char* expected_url,
int expected_error) {
net::TestDelegate test_delegate;
GURL url(test_url);
std::unique_ptr<net::URLRequest> request = context()->CreateRequest(
url, net::IDLE, &test_delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo> before_url_context(
new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
before_url_context);
brave::ResponseCallback callback;
void CheckUrl(const std::string& test_url,
const char* expected_url,
int expected_error) {
GURL new_url;
int ret =
brave::OnBeforeURLRequest_BlockSafeBrowsingReportingURLs(url, &new_url);
int rc = brave::OnBeforeURLRequest_BlockSafeBrowsingReportingURLs(
GURL(test_url), &new_url);
EXPECT_EQ(new_url, GURL(expected_url));
EXPECT_EQ(ret, expected_error);
EXPECT_EQ(rc, expected_error);
}

TEST_F(BraveBlockReportingUrlsHelperTest, PreserveNormalUrls) {
TEST(BraveBlockReportingUrlsHelperTest, PreserveNormalUrls) {
const std::vector<const std::string> normalUrls({
"https://brave.com/",
"https://safebrowsing.google.com/safebrowsing",
Expand All @@ -68,7 +40,7 @@ TEST_F(BraveBlockReportingUrlsHelperTest, PreserveNormalUrls) {
}
}

TEST_F(BraveBlockReportingUrlsHelperTest, CancelReportingUrl) {
TEST(BraveBlockReportingUrlsHelperTest, CancelReportingUrl) {
const std::vector<const std::string> reportingUrls({
"https://sb-ssl.google.com/safebrowsing/clientreport/download",
"https://sb-ssl.google.com/safebrowsing/clientreport/chrome-reset",
Expand All @@ -87,5 +59,3 @@ TEST_F(BraveBlockReportingUrlsHelperTest, CancelReportingUrl) {
CheckUrl(url, kInvalidUrl, net::ERR_ABORTED);
}
}

} // namespace
Loading