Skip to content

Commit

Permalink
Add HTTPS Everywhere service browser test
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Mar 28, 2018
1 parent fd1f430 commit 6acd8d3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 8 additions & 2 deletions components/brave_shields/browser/https_everywhere_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<HTTPSEverywhereService> HTTPSEverywhereServiceFactory() {
return base::MakeUnique<HTTPSEverywhereService>();
}
Expand Down
6 changes: 6 additions & 0 deletions components/brave_shields/browser/https_everywhere_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace leveldb {
class DB;
}

class HTTPSEverywhereServiceTest;

namespace brave_shields {

struct HTTPSE_REDIRECTS_COUNT_ST {
Expand Down Expand Up @@ -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_REDIRECTS_COUNT_ST> httpse_urls_redirects_count_;
HTTPSERecentlyUsedCache<std::string> recently_used_cache_;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<base::ThreadTestHelper> 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());
}
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 6acd8d3

Please sign in to comment.