Fix/Add test for tor
This commit is contained in:
@@ -1,251 +0,0 @@
|
||||
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
* 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 "brave/browser/net/brave_tor_network_delegate_helper.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/files/scoped_temp_dir.h"
|
||||
#include "brave/browser/net/url_context.h"
|
||||
#include "brave/browser/profiles/brave_profile_manager.h"
|
||||
#include "brave/browser/profiles/tor_unittest_profile_manager.h"
|
||||
#include "brave/browser/renderer_host/brave_navigation_ui_data.h"
|
||||
#include "brave/browser/tor/mock_tor_profile_service_factory.h"
|
||||
#include "brave/browser/tor/tor_proxy_config_service.h"
|
||||
#include "brave/common/tor/tor_common.h"
|
||||
#include "brave/common/tor/tor_test_constants.h"
|
||||
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
|
||||
#include "chrome/test/base/scoped_testing_local_state.h"
|
||||
#include "chrome/test/base/testing_browser_process.h"
|
||||
#include "content/public/browser/resource_request_info.h"
|
||||
#include "content/public/test/mock_resource_context.h"
|
||||
#include "content/public/test/test_utils.h"
|
||||
#include "net/log/net_log_with_source.h"
|
||||
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
|
||||
#include "net/url_request/url_request_test_util.h"
|
||||
#include "url/gurl.h"
|
||||
#include "url/url_constants.h"
|
||||
|
||||
namespace {
|
||||
|
||||
int kRenderProcessId = 1;
|
||||
int kRenderFrameId = 2;
|
||||
|
||||
} // namespace
|
||||
|
||||
class BraveTorNetworkDelegateHelperTest: public testing::Test {
|
||||
public:
|
||||
BraveTorNetworkDelegateHelperTest()
|
||||
: local_state_(TestingBrowserProcess::GetGlobal()),
|
||||
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
|
||||
context_(new net::TestURLRequestContext(true)) {}
|
||||
|
||||
~BraveTorNetworkDelegateHelperTest() override {}
|
||||
|
||||
void SetUp() override {
|
||||
// Create a new temporary directory, and store the path
|
||||
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
||||
TestingBrowserProcess::GetGlobal()->SetProfileManager(
|
||||
new TorUnittestProfileManager(temp_dir_.GetPath()));
|
||||
context_->Init();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
|
||||
content::RunAllTasksUntilIdle();
|
||||
}
|
||||
|
||||
net::TestURLRequestContext* context() { return context_.get(); }
|
||||
|
||||
content::MockResourceContext* resource_context() {
|
||||
return resource_context_.get();
|
||||
}
|
||||
|
||||
protected:
|
||||
// The path to temporary directory used to contain the test operations.
|
||||
base::ScopedTempDir temp_dir_;
|
||||
ScopedTestingLocalState local_state_;
|
||||
|
||||
private:
|
||||
content::TestBrowserThreadBundle thread_bundle_;
|
||||
std::unique_ptr<net::TestURLRequestContext> context_;
|
||||
std::unique_ptr<content::MockResourceContext> resource_context_;
|
||||
DISALLOW_COPY_AND_ASSIGN(BraveTorNetworkDelegateHelperTest);
|
||||
};
|
||||
|
||||
TEST_F(BraveTorNetworkDelegateHelperTest, NotTorProfile) {
|
||||
net::TestDelegate test_delegate;
|
||||
GURL url("https://check.torproject.org/");
|
||||
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;
|
||||
|
||||
std::unique_ptr<BraveNavigationUIData> navigation_ui_data =
|
||||
std::make_unique<BraveNavigationUIData>();
|
||||
content::ResourceRequestInfo::AllocateForTesting(
|
||||
request.get(), content::ResourceType::kMainFrame, resource_context(),
|
||||
kRenderProcessId, /*render_view_id=*/-1, kRenderFrameId,
|
||||
/*is_main_frame=*/true, content::ResourceInterceptPolicy::kAllowNone,
|
||||
/*is_async=*/true, content::PREVIEWS_OFF, std::move(navigation_ui_data));
|
||||
int ret =
|
||||
brave::OnBeforeURLRequest_TorWork(callback,
|
||||
before_url_context);
|
||||
EXPECT_TRUE(before_url_context->new_url_spec.empty());
|
||||
auto* proxy_service = request->context()->proxy_resolution_service();
|
||||
net::ProxyInfo info;
|
||||
std::unique_ptr<net::ProxyResolutionService::Request> proxy_request;
|
||||
proxy_service->ResolveProxy(url, std::string(), &info, base::DoNothing(),
|
||||
&proxy_request, net::NetLogWithSource());
|
||||
EXPECT_EQ(info.ToPacString(), "DIRECT");
|
||||
ASSERT_TRUE(proxy_service->config());
|
||||
EXPECT_TRUE(proxy_service->config()->value().proxy_rules().empty());
|
||||
EXPECT_EQ(ret, net::OK);
|
||||
}
|
||||
|
||||
TEST_F(BraveTorNetworkDelegateHelperTest, TorProfile) {
|
||||
const GURL urls[] = {
|
||||
GURL("https://check.torproject.org"),
|
||||
GURL("https://1.1.1.1"),
|
||||
GURL("https://127.0.0.1"),
|
||||
GURL("https://[::1]"),
|
||||
GURL("https://localhost"),
|
||||
};
|
||||
|
||||
ProfileManager* profile_manager = g_browser_process->profile_manager();
|
||||
base::FilePath tor_path = BraveProfileManager::GetTorProfilePath();
|
||||
|
||||
Profile* profile = profile_manager->GetProfile(tor_path);
|
||||
ASSERT_TRUE(profile);
|
||||
|
||||
for (auto& url : urls) {
|
||||
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>
|
||||
before_url_context(new brave::BraveRequestInfo());
|
||||
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
|
||||
before_url_context);
|
||||
brave::ResponseCallback callback;
|
||||
|
||||
std::unique_ptr<BraveNavigationUIData> navigation_ui_data =
|
||||
std::make_unique<BraveNavigationUIData>();
|
||||
BraveNavigationUIData* navigation_ui_data_ptr = navigation_ui_data.get();
|
||||
content::ResourceRequestInfo::AllocateForTesting(
|
||||
request.get(), content::ResourceType::kMainFrame, resource_context(),
|
||||
kRenderProcessId, /*render_view_id=*/-1, kRenderFrameId,
|
||||
/*is_main_frame=*/true, content::ResourceInterceptPolicy::kAllowNone,
|
||||
/*is_async=*/true, content::PREVIEWS_OFF,
|
||||
std::move(navigation_ui_data));
|
||||
|
||||
MockTorProfileServiceFactory::SetTorNavigationUIData(
|
||||
profile,
|
||||
navigation_ui_data_ptr);
|
||||
int ret = brave::OnBeforeURLRequest_TorWork(callback, before_url_context);
|
||||
EXPECT_TRUE(before_url_context->new_url_spec.empty());
|
||||
auto* proxy_service = request->context()->proxy_resolution_service();
|
||||
net::ProxyInfo info;
|
||||
std::unique_ptr<net::ProxyResolutionService::Request> proxy_request;
|
||||
proxy_service->ResolveProxy(url, std::string(), &info, base::DoNothing(),
|
||||
&proxy_request, net::NetLogWithSource());
|
||||
EXPECT_EQ(info.ToPacString(), tor::kTestTorPacString);
|
||||
ASSERT_TRUE(proxy_service->config());
|
||||
ASSERT_FALSE(proxy_service->config()->value().proxy_rules().empty());
|
||||
net::ProxyConfig::ProxyRules rules;
|
||||
rules.ParseFromString(tor::kTestTorProxy);
|
||||
rules.bypass_rules.AddRulesToSubtractImplicit();
|
||||
EXPECT_TRUE(proxy_service->config()->value().proxy_rules().Equals(rules));
|
||||
EXPECT_EQ(ret, net::OK);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(BraveTorNetworkDelegateHelperTest, TorProfileBlockFile) {
|
||||
ProfileManager* profile_manager = g_browser_process->profile_manager();
|
||||
base::FilePath tor_path = BraveProfileManager::GetTorProfilePath();
|
||||
|
||||
Profile* profile = profile_manager->GetProfile(tor_path);
|
||||
ASSERT_TRUE(profile);
|
||||
|
||||
net::TestDelegate test_delegate;
|
||||
GURL url("file://test");
|
||||
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;
|
||||
|
||||
std::unique_ptr<BraveNavigationUIData> navigation_ui_data =
|
||||
std::make_unique<BraveNavigationUIData>();
|
||||
BraveNavigationUIData* navigation_ui_data_ptr = navigation_ui_data.get();
|
||||
content::ResourceRequestInfo::AllocateForTesting(
|
||||
request.get(), content::ResourceType::kMainFrame, resource_context(),
|
||||
kRenderProcessId, /*render_view_id=*/-1, kRenderFrameId,
|
||||
/*is_main_frame=*/true, content::ResourceInterceptPolicy::kAllowNone,
|
||||
/*is_async=*/true, content::PREVIEWS_OFF, std::move(navigation_ui_data));
|
||||
|
||||
MockTorProfileServiceFactory::SetTorNavigationUIData(profile,
|
||||
navigation_ui_data_ptr);
|
||||
int ret =
|
||||
brave::OnBeforeURLRequest_TorWork(callback,
|
||||
before_url_context);
|
||||
EXPECT_TRUE(before_url_context->new_url_spec.empty());
|
||||
EXPECT_EQ(ret, net::ERR_DISALLOWED_URL_SCHEME);
|
||||
}
|
||||
|
||||
TEST_F(BraveTorNetworkDelegateHelperTest, TorProfileBlockIfHosed) {
|
||||
ProfileManager* profile_manager = g_browser_process->profile_manager();
|
||||
base::FilePath tor_path = BraveProfileManager::GetTorProfilePath();
|
||||
|
||||
Profile* profile = profile_manager->GetProfile(tor_path);
|
||||
ASSERT_TRUE(profile);
|
||||
|
||||
net::TestDelegate test_delegate;
|
||||
GURL url("https://check.torproject.org/");
|
||||
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;
|
||||
|
||||
std::unique_ptr<BraveNavigationUIData> navigation_ui_data =
|
||||
std::make_unique<BraveNavigationUIData>();
|
||||
BraveNavigationUIData* navigation_ui_data_ptr = navigation_ui_data.get();
|
||||
content::ResourceRequestInfo::AllocateForTesting(
|
||||
request.get(), content::ResourceType::kMainFrame, resource_context(),
|
||||
kRenderProcessId, /*render_view_id=*/-1, kRenderFrameId,
|
||||
/*is_main_frame=*/true, content::ResourceInterceptPolicy::kAllowNone,
|
||||
/*is_async=*/true, content::PREVIEWS_OFF, std::move(navigation_ui_data));
|
||||
|
||||
MockTorProfileServiceFactory::SetTorNavigationUIData(profile,
|
||||
navigation_ui_data_ptr);
|
||||
|
||||
// `Relaunch' tor with broken config.
|
||||
{
|
||||
auto* tor_profile_service = navigation_ui_data_ptr->GetTorProfileService();
|
||||
base::FilePath path(tor::kTestBrokenTorPath);
|
||||
std::string proxy(tor::kTestTorProxy);
|
||||
tor_profile_service->ReLaunchTor(tor::TorConfig(path, proxy));
|
||||
}
|
||||
|
||||
int ret =
|
||||
brave::OnBeforeURLRequest_TorWork(callback,
|
||||
before_url_context);
|
||||
EXPECT_TRUE(before_url_context->new_url_spec.empty());
|
||||
EXPECT_NE(ret, net::OK);
|
||||
}
|
||||
@@ -7,13 +7,8 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "brave/browser/tor/tor_proxy_config_service.h"
|
||||
#include "base/callback.h"
|
||||
#include "brave/common/tor/tor_test_constants.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
using tor::TorProxyConfigService;
|
||||
|
||||
namespace tor {
|
||||
|
||||
@@ -32,8 +27,8 @@ void MockTorProfileServiceImpl::ReLaunchTor(const TorConfig& config) {
|
||||
}
|
||||
|
||||
|
||||
void MockTorProfileServiceImpl::SetNewTorCircuit(const GURL& request_url,
|
||||
const base::Closure& callback) {}
|
||||
void MockTorProfileServiceImpl::SetNewTorCircuit(
|
||||
const GURL& request_url, NewTorCircuitCallback callback) {}
|
||||
|
||||
const TorConfig& MockTorProfileServiceImpl::GetTorConfig() {
|
||||
return config_;
|
||||
@@ -41,22 +36,4 @@ const TorConfig& MockTorProfileServiceImpl::GetTorConfig() {
|
||||
|
||||
int64_t MockTorProfileServiceImpl::GetTorPid() { return -1; }
|
||||
|
||||
int MockTorProfileServiceImpl::SetProxy(
|
||||
net::ProxyResolutionService* service, const GURL& request_url,
|
||||
bool new_circuit) {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||
DCHECK(request_url.SchemeIsHTTPOrHTTPS());
|
||||
std::string isolation_key = CircuitIsolationKey(request_url);
|
||||
if (config_.empty()) {
|
||||
// No tor config => we absolutely cannot talk to the network.
|
||||
// This might mean that there was a problem trying to initialize
|
||||
// Tor.
|
||||
LOG(ERROR) << "Tor not configured -- blocking connection";
|
||||
return net::ERR_SOCKS_CONNECTION_FAILED;
|
||||
}
|
||||
TorProxyConfigService::TorSetProxy(service, config_.proxy_string(),
|
||||
isolation_key, nullptr, new_circuit);
|
||||
return net::OK;
|
||||
}
|
||||
|
||||
} // namespace tor
|
||||
|
||||
@@ -19,14 +19,11 @@ class MockTorProfileServiceImpl : public TorProfileService {
|
||||
// TorProfileService:
|
||||
void LaunchTor(const TorConfig&) override;
|
||||
void ReLaunchTor(const TorConfig&) override;
|
||||
void SetNewTorCircuit(const GURL& request_url, const base::Closure&) override;
|
||||
void SetNewTorCircuit(const GURL& request_url,
|
||||
NewTorCircuitCallback) override;
|
||||
const TorConfig& GetTorConfig() override;
|
||||
int64_t GetTorPid() override;
|
||||
|
||||
int SetProxy(net::ProxyResolutionService*,
|
||||
const GURL& request_url,
|
||||
bool new_circuit) override;
|
||||
|
||||
private:
|
||||
TorConfig config_;
|
||||
DISALLOW_COPY_AND_ASSIGN(MockTorProfileServiceImpl);
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
* 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 "brave/browser/tor/tor_profile_service.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
class TorProfileServiceTest : public testing::Test {
|
||||
public:
|
||||
TorProfileServiceTest() {}
|
||||
~TorProfileServiceTest() override {}
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(TorProfileServiceTest);
|
||||
};
|
||||
|
||||
TEST_F(TorProfileServiceTest, CircuitIsolationKey) {
|
||||
const struct {
|
||||
GURL url;
|
||||
std::string key;
|
||||
} cases[] = {
|
||||
{ GURL("https://1.1.1.1/"), "1.1.1.1", },
|
||||
{ GURL("https://1.1.1.1:53/"), "1.1.1.1", },
|
||||
{ GURL("https://127.0.0.1/"), "127.0.0.1", },
|
||||
{ GURL("https://127.0.0.53/"), "127.0.0.53", },
|
||||
{ GURL("https://8.8.8.8/"), "8.8.8.8", },
|
||||
{ GURL("https://8.8.8.8:80/"), "8.8.8.8", },
|
||||
{ GURL("https://[::1]/"), "[::1]", },
|
||||
{ GURL("https://check.torproject.org/"), "torproject.org", },
|
||||
{ GURL("https://check.torproject.org/x"), "torproject.org", },
|
||||
{ GURL("https://check.torproject.org/x?y"), "torproject.org", },
|
||||
{ GURL("https://check.torproject.org/x?y#z"), "torproject.org", },
|
||||
{ GURL("https://localhost/"), "localhost", },
|
||||
{ GURL("https://localhost:8888/"), "localhost", },
|
||||
{ GURL("https://user:pass@localhost:8888/"), "localhost", },
|
||||
{ GURL("https://www.bbc.co.uk/"), "bbc.co.uk", },
|
||||
};
|
||||
|
||||
for (auto& c : cases) {
|
||||
const GURL& url = c.url;
|
||||
const std::string& expected_key = c.key;
|
||||
std::string actual_key = tor::TorProfileService::CircuitIsolationKey(url);
|
||||
|
||||
EXPECT_EQ(expected_key, actual_key);
|
||||
}
|
||||
}
|
||||
@@ -51,8 +51,6 @@ class NET_EXPORT ProxyConfigServiceTor : public net::ProxyConfigService {
|
||||
|
||||
void SetUsername(const std::string &username, TorProxyMap* map);
|
||||
|
||||
net::ProxyConfig* config() { return &config_; }
|
||||
|
||||
static std::string CircuitIsolationKey(const GURL& request_url);
|
||||
|
||||
// ProxyConfigService methods:
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
* 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 "brave/net/proxy_resolution/proxy_config_service_tor.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "net/proxy_resolution/proxy_config_with_annotation.h"
|
||||
#include "net/test/test_with_scoped_task_environment.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace net {
|
||||
|
||||
class ProxyConfigServiceTorTest : public TestWithScopedTaskEnvironment {
|
||||
public:
|
||||
ProxyConfigServiceTorTest() {}
|
||||
~ProxyConfigServiceTorTest() override {}
|
||||
|
||||
ProxyConfigServiceTor::TorProxyMap* GetTorProxyMap() {
|
||||
return &tor_proxy_map_;
|
||||
}
|
||||
private:
|
||||
ProxyConfigServiceTor::TorProxyMap tor_proxy_map_;
|
||||
DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceTorTest);
|
||||
};
|
||||
|
||||
TEST_F(ProxyConfigServiceTorTest, CircuitIsolationKey) {
|
||||
const struct {
|
||||
GURL url;
|
||||
std::string key;
|
||||
} cases[] = {
|
||||
{ GURL("https://1.1.1.1/"), "1.1.1.1", },
|
||||
{ GURL("https://1.1.1.1:53/"), "1.1.1.1", },
|
||||
{ GURL("https://127.0.0.1/"), "127.0.0.1", },
|
||||
{ GURL("https://127.0.0.53/"), "127.0.0.53", },
|
||||
{ GURL("https://8.8.8.8/"), "8.8.8.8", },
|
||||
{ GURL("https://8.8.8.8:80/"), "8.8.8.8", },
|
||||
{ GURL("https://[::1]/"), "[::1]", },
|
||||
{ GURL("https://check.torproject.org/"), "torproject.org", },
|
||||
{ GURL("https://check.torproject.org/x"), "torproject.org", },
|
||||
{ GURL("https://check.torproject.org/x?y"), "torproject.org", },
|
||||
{ GURL("https://check.torproject.org/x?y#z"), "torproject.org", },
|
||||
{ GURL("https://localhost/"), "localhost", },
|
||||
{ GURL("https://localhost:8888/"), "localhost", },
|
||||
{ GURL("https://user:pass@localhost:8888/"), "localhost", },
|
||||
{ GURL("https://www.bbc.co.uk/"), "bbc.co.uk", },
|
||||
};
|
||||
|
||||
for (auto& c : cases) {
|
||||
const GURL& url = c.url;
|
||||
const std::string& expected_key = c.key;
|
||||
std::string actual_key = ProxyConfigServiceTor::CircuitIsolationKey(url);
|
||||
|
||||
EXPECT_EQ(expected_key, actual_key);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ProxyConfigServiceTorTest, SetUsername) {
|
||||
std::string proxy_uri("socks5://127.0.0.1:5566");
|
||||
GURL site_url("https://check.torproject.org/");
|
||||
std::string isolation_key =
|
||||
ProxyConfigServiceTor::CircuitIsolationKey(site_url);
|
||||
ProxyConfigServiceTor proxy_config_service(proxy_uri);
|
||||
proxy_config_service.SetUsername(isolation_key, GetTorProxyMap());
|
||||
|
||||
ProxyConfigWithAnnotation config;
|
||||
proxy_config_service.GetLatestProxyConfig(&config);
|
||||
ASSERT_FALSE(config.value().proxy_rules().single_proxies.IsEmpty());
|
||||
ProxyServer server = config.value().proxy_rules().single_proxies.Get();
|
||||
HostPortPair host_port = server.host_port_pair();
|
||||
EXPECT_TRUE(server.scheme() == ProxyServer::SCHEME_SOCKS5);
|
||||
EXPECT_EQ(host_port.host(), "127.0.0.1");
|
||||
EXPECT_EQ(host_port.port(), 5566);
|
||||
EXPECT_EQ(host_port.username(), isolation_key);
|
||||
|
||||
// persistent circuit isolation until timeout
|
||||
std::string password = host_port.password();
|
||||
EXPECT_FALSE(host_port.password().empty());
|
||||
proxy_config_service.SetUsername(isolation_key, GetTorProxyMap());
|
||||
proxy_config_service.GetLatestProxyConfig(&config);
|
||||
ASSERT_FALSE(config.value().proxy_rules().single_proxies.IsEmpty());
|
||||
server = config.value().proxy_rules().single_proxies.Get();
|
||||
host_port = server.host_port_pair();
|
||||
EXPECT_EQ(host_port.password(), password);
|
||||
|
||||
// New identity
|
||||
GetTorProxyMap()->Erase(isolation_key);
|
||||
proxy_config_service.SetUsername(isolation_key, GetTorProxyMap());
|
||||
proxy_config_service.GetLatestProxyConfig(&config);
|
||||
ASSERT_FALSE(config.value().proxy_rules().single_proxies.IsEmpty());
|
||||
server = config.value().proxy_rules().single_proxies.Get();
|
||||
host_port = server.host_port_pair();
|
||||
EXPECT_NE(host_port.password(), password);
|
||||
}
|
||||
|
||||
} // namespace net
|
||||
@@ -0,0 +1,106 @@
|
||||
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
* 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 "net/proxy_resolution/proxy_resolution_service.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "brave/net/proxy_resolution/proxy_config_service_tor.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "net/base/test_completion_callback.h"
|
||||
#include "net/log/test_net_log.h"
|
||||
#include "net/proxy_resolution/mock_proxy_resolver.h"
|
||||
#include "net/test/gtest_util.h"
|
||||
#include "net/test/test_with_scoped_task_environment.h"
|
||||
|
||||
using net::test::IsOk;
|
||||
|
||||
namespace net {
|
||||
|
||||
class ProxyResolutionServiceTest : public TestWithScopedTaskEnvironment {
|
||||
public:
|
||||
ProxyResolutionServiceTest() {}
|
||||
~ProxyResolutionServiceTest() override {}
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(ProxyResolutionServiceTest);
|
||||
};
|
||||
|
||||
TEST_F(ProxyResolutionServiceTest, TorProxy) {
|
||||
MockAsyncProxyResolverFactory* factory =
|
||||
new MockAsyncProxyResolverFactory(false);
|
||||
std::string proxy_uri("socks5://127.0.0.1:5566");
|
||||
ProxyResolutionService service(
|
||||
std::make_unique<ProxyConfigServiceTor>(proxy_uri),
|
||||
base::WrapUnique(factory), nullptr);
|
||||
|
||||
GURL site_url("https://check.torproject.org/");
|
||||
std::string isolation_key =
|
||||
ProxyConfigServiceTor::CircuitIsolationKey(site_url);
|
||||
|
||||
ProxyInfo info;
|
||||
TestCompletionCallback callback;
|
||||
BoundTestNetLog log;
|
||||
std::unique_ptr<ProxyResolutionService::Request> request;
|
||||
int rv = service.ResolveProxy(site_url, std::string(), &info,
|
||||
callback.callback(),
|
||||
&request, log.bound());
|
||||
EXPECT_THAT(rv, IsOk());
|
||||
EXPECT_TRUE(factory->pending_requests().empty());
|
||||
|
||||
EXPECT_TRUE(info.is_socks());
|
||||
ASSERT_FALSE(info.proxy_list().IsEmpty());
|
||||
|
||||
ProxyServer server = info.proxy_list().Get();
|
||||
HostPortPair host_port = server.host_port_pair();
|
||||
EXPECT_TRUE(server.scheme() == ProxyServer::SCHEME_SOCKS5);
|
||||
EXPECT_EQ(host_port.host(), "127.0.0.1");
|
||||
EXPECT_EQ(host_port.port(), 5566);
|
||||
EXPECT_EQ(host_port.username(), isolation_key);
|
||||
EXPECT_FALSE(host_port.password().empty());
|
||||
}
|
||||
|
||||
TEST_F(ProxyResolutionServiceTest, NewTorCircuit) {
|
||||
MockAsyncProxyResolverFactory* factory =
|
||||
new MockAsyncProxyResolverFactory(false);
|
||||
std::string proxy_uri("socks5://127.0.0.1:5566");
|
||||
ProxyResolutionService service(
|
||||
std::make_unique<ProxyConfigServiceTor>(proxy_uri),
|
||||
base::WrapUnique(factory), nullptr);
|
||||
|
||||
GURL site_url("https://check.torproject.org/");
|
||||
std::string isolation_key =
|
||||
ProxyConfigServiceTor::CircuitIsolationKey(site_url);
|
||||
|
||||
ProxyInfo info;
|
||||
TestCompletionCallback callback;
|
||||
BoundTestNetLog log;
|
||||
std::unique_ptr<ProxyResolutionService::Request> request;
|
||||
int rv = service.ResolveProxy(site_url, std::string(), &info,
|
||||
callback.callback(),
|
||||
&request, log.bound());
|
||||
|
||||
ProxyServer server = info.proxy_list().Get();
|
||||
HostPortPair host_port = server.host_port_pair();
|
||||
EXPECT_FALSE(host_port.password().empty());
|
||||
std::string password = host_port.password();
|
||||
|
||||
GURL site_url_ref("https://check.torproject.org/#NewTorCircuit");
|
||||
rv = service.ResolveProxy(site_url_ref, std::string(), &info,
|
||||
callback.callback(),
|
||||
&request, log.bound());
|
||||
|
||||
server = info.proxy_list().Get();
|
||||
host_port = server.host_port_pair();
|
||||
EXPECT_NE(host_port.password(), password);
|
||||
EXPECT_TRUE(server.scheme() == ProxyServer::SCHEME_SOCKS5);
|
||||
EXPECT_EQ(host_port.host(), "127.0.0.1");
|
||||
EXPECT_EQ(host_port.port(), 5566);
|
||||
EXPECT_EQ(host_port.username(), isolation_key);
|
||||
}
|
||||
|
||||
} // namespace net
|
||||
+2
-2
@@ -58,7 +58,6 @@ test("brave_unit_tests") {
|
||||
"//brave/browser/tor/mock_tor_profile_service_impl.h",
|
||||
"//brave/browser/tor/mock_tor_profile_service_factory.cc",
|
||||
"//brave/browser/tor/mock_tor_profile_service_factory.h",
|
||||
"//brave/browser/tor/tor_profile_service_unittest.cc",
|
||||
"//brave/browser/metrics/metrics_reporting_util_unittest_linux.cc",
|
||||
"//brave/browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc",
|
||||
"//brave/browser/net/brave_common_static_redirect_network_delegate_helper_unittest.cc",
|
||||
@@ -67,7 +66,6 @@ test("brave_unit_tests") {
|
||||
"//brave/browser/net/brave_referrals_network_delegate_helper_unittest.cc",
|
||||
"//brave/browser/net/brave_site_hacks_network_delegate_helper_unittest.cc",
|
||||
"//brave/browser/net/brave_static_redirect_network_delegate_helper_unittest.cc",
|
||||
"//brave/browser/net/brave_tor_network_delegate_helper_unittest.cc",
|
||||
"//brave/browser/profiles/tor_unittest_profile_manager.cc",
|
||||
"//brave/browser/profiles/tor_unittest_profile_manager.h",
|
||||
"//brave/browser/profiles/brave_profile_manager_unittest.cc",
|
||||
@@ -105,6 +103,8 @@ test("brave_unit_tests") {
|
||||
"//brave/components/invalidation/push_client_channel_unittest.cc",
|
||||
"//brave/components/omnibox/browser/topsites_provider_unittest.cc",
|
||||
"//brave/components/rappor/log_uploader_unittest.cc",
|
||||
"//brave/net/proxy_resolution/proxy_config_service_tor_unittest.cc",
|
||||
"//brave/net/proxy_resolution/proxy_resolution_service_unittest.cc",
|
||||
"//brave/third_party/libaddressinput/chromium/chrome_metadata_source_unittest.cc",
|
||||
"//brave/vendor/brave_base/random_unittest.cc",
|
||||
"//chrome/common/importer/mock_importer_bridge.cc",
|
||||
|
||||
Reference in New Issue
Block a user