diff --git a/components/brave_shields/browser/ad_block_service_browsertest.cc b/components/brave_shields/browser/ad_block_service_browsertest.cc index 25d3604f3602..13f03c9ba784 100644 --- a/components/brave_shields/browser/ad_block_service_browsertest.cc +++ b/components/brave_shields/browser/ad_block_service_browsertest.cc @@ -1,3 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + #include "base/path_service.h" #include "base/test/thread_test_helper.h" #include "brave/common/brave_paths.h" diff --git a/components/brave_shields/browser/https_everywhere_service.cc b/components/brave_shields/browser/https_everywhere_service.cc index c10c9dc76ab7..362f5a9b2216 100644 --- a/components/brave_shields/browser/https_everywhere_service.cc +++ b/components/brave_shields/browser/https_everywhere_service.cc @@ -79,8 +79,10 @@ namespace { namespace brave_shields { +GURL HTTPSEverywhereService::g_https_everywhere_url(DAT_FILE_URL); + HTTPSEverywhereService::HTTPSEverywhereService() : - BaseBraveShieldsService(DAT_FILE, GURL(DAT_FILE_URL)) { + BaseBraveShieldsService(DAT_FILE, g_https_everywhere_url) { } HTTPSEverywhereService::~HTTPSEverywhereService() { @@ -330,11 +332,15 @@ std::string HTTPSEverywhereService::CorrecttoRuleToRE2Engine( return correctedto; } +// static +void HTTPSEverywhereService::SetHttpsEveryWhereURLForTest(const GURL& url) { + g_https_everywhere_url = url; +} + /////////////////////////////////////////////////////////////////////////////// // The brave shields factory. Using the Brave Shields as a singleton // is the job of the browser process. -// TODO(bbondy): consider making this a singleton. std::unique_ptr HTTPSEverywhereServiceFactory() { return base::MakeUnique(); } diff --git a/components/brave_shields/browser/https_everywhere_service.h b/components/brave_shields/browser/https_everywhere_service.h index 3e534c488b0f..d8537ea18eb4 100644 --- a/components/brave_shields/browser/https_everywhere_service.h +++ b/components/brave_shields/browser/https_everywhere_service.h @@ -20,6 +20,8 @@ namespace leveldb { class DB; } +class HTTPSEverywhereServiceTest; + namespace brave_shields { struct HTTPSE_REDIRECTS_COUNT_ST { @@ -54,6 +56,10 @@ class HTTPSEverywhereService : public BaseBraveShieldsService { std::string CorrecttoRuleToRE2Engine(const std::string& to); private: + friend class ::HTTPSEverywhereServiceTest; + static GURL g_https_everywhere_url; + static void SetHttpsEveryWhereURLForTest(const GURL& url); + std::mutex httpse_get_urls_redirects_count_mutex_; std::vector httpse_urls_redirects_count_; HTTPSERecentlyUsedCache recently_used_cache_; diff --git a/components/brave_shields/browser/https_everywhere_service_browsertest.cc b/components/brave_shields/browser/https_everywhere_service_browsertest.cc new file mode 100644 index 000000000000..5020e15c2353 --- /dev/null +++ b/components/brave_shields/browser/https_everywhere_service_browsertest.cc @@ -0,0 +1,77 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "base/path_service.h" +#include "base/test/thread_test_helper.h" +#include "brave/browser/brave_browser_process_impl.h" +#include "brave/common/brave_paths.h" +#include "brave/components/brave_shields/browser/https_everywhere_service.h" +#include "chrome/browser/net/url_request_mock_util.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "content/public/test/browser_test_utils.h" +#include "net/dns/mock_host_resolver.h" + +class HTTPSEverywhereServiceTest : public InProcessBrowserTest { +public: + HTTPSEverywhereServiceTest() {} + + void SetUp() override { + InitEmbeddedTestServer(); + InitService(); + InProcessBrowserTest::SetUp(); + } + + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + content::BrowserThread::PostTask( + content::BrowserThread::IO, FROM_HERE, + base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); + host_resolver()->AddRule("*", "127.0.0.1"); + } + + void PreRunTestOnMainThread() override { + InProcessBrowserTest::PreRunTestOnMainThread(); + WaitForHTTPSEverywhereServiceThread(); + ASSERT_TRUE( + g_brave_browser_process->https_everywhere_service()->IsInitialized()); + } + + void InitEmbeddedTestServer() { + brave::RegisterPathProvider(); + base::FilePath test_data_dir; + PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); + embedded_test_server()->ServeFilesFromDirectory(test_data_dir); + ASSERT_TRUE(embedded_test_server()->Start()); + } + + void InitService() { + brave_shields::HTTPSEverywhereService::SetHttpsEveryWhereURLForTest( + embedded_test_server()->GetURL("https-everywhere-data/5.2/httpse.leveldb.zip")); + } + + void WaitForHTTPSEverywhereServiceThread() { + scoped_refptr io_helper( + new base::ThreadTestHelper( + g_brave_browser_process->https_everywhere_service()->GetTaskRunner())); + ASSERT_TRUE(io_helper->Run()); + } +}; + +// Load a URL which has an HTTPSE rule and verify we rewrote it. +IN_PROC_BROWSER_TEST_F(HTTPSEverywhereServiceTest, RedirectsKnownSite) { + GURL url = embedded_test_server()->GetURL("http://www.digg.com/"); + ui_test_utils::NavigateToURL(browser(), url); + content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents(); + EXPECT_STREQ("https://www.digg.com/", contents->GetLastCommittedURL().spec().c_str()); +} + +// Load a URL which has no HTTPSE rule and verify we did not rewrite it. +IN_PROC_BROWSER_TEST_F(HTTPSEverywhereServiceTest, NoRedirectsNotKnownSite) { + GURL url = embedded_test_server()->GetURL("http://www.brianbondy.com/"); + ui_test_utils::NavigateToURL(browser(), url); + content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents(); + EXPECT_STREQ("http://www.brianbondy.com/", contents->GetLastCommittedURL().spec().c_str()); +} diff --git a/test/BUILD.gn b/test/BUILD.gn index b4fc429841ba..dd09c4ae3ee4 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -66,6 +66,7 @@ group("brave_browser_tests_deps") { test("brave_browser_tests") { sources = [ "//brave/components/brave_shields/browser/ad_block_service_browsertest.cc", + "//brave/components/brave_shields/browser/https_everywhere_service_browsertest.cc", ] defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] deps = [