Add test for ProxyConfigMonitor

This commit is contained in:
Anthony Tseng
2019-07-05 16:39:36 -07:00
parent 2e0e6a5684
commit 014d4802f1
10 changed files with 88 additions and 16 deletions
@@ -12,6 +12,7 @@
#include "base/files/scoped_temp_dir.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/profiles/tor_unittest_profile_manager.h"
#include "brave/browser/tor/tor_launcher_factory.h"
#include "brave/common/tor/pref_names.h"
#include "brave/common/tor/tor_constants.h"
#include "brave/components/brave_webtorrent/browser/webtorrent_util.h"
@@ -19,6 +20,7 @@
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/net/proxy_config_monitor.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
@@ -32,6 +34,8 @@
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "net/proxy_resolution/proxy_config_with_annotation.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "ui/base/l10n/l10n_util.h"
namespace {
@@ -202,3 +206,20 @@ TEST_F(BraveProfileManagerTest, NoWebtorrentInTorProfile) {
EXPECT_FALSE(webtorrent::IsWebtorrentEnabled(profile));
}
TEST_F(BraveProfileManagerTest, ProxyConfigMonitorInTorProfile) {
ScopedTorLaunchPreventerForTest prevent_tor_process;
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath tor_path = BraveProfileManager::GetTorProfilePath();
Profile* profile = profile_manager->GetProfile(tor_path);
ASSERT_TRUE(profile);
std::unique_ptr<ProxyConfigMonitor> monitor(new ProxyConfigMonitor(profile));
auto* proxy_config_service = monitor->GetProxyConfigServiceForTesting();
net::ProxyConfigWithAnnotation config;
proxy_config_service->GetLatestProxyConfig(&config);
const std::string& proxy_uri =
config.value().proxy_rules().single_proxies.Get().ToURI();
EXPECT_EQ(proxy_uri, g_browser_process->local_state()
->GetString(tor::prefs::kTorProxyString));
}
@@ -1,9 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/profiles/tor_unittest_profile_manager.h"
#include <memory>
#include <utility>
#include "base/files/file_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "brave/browser/profiles/brave_profile_manager.h"
+1
View File
@@ -25,6 +25,7 @@ TorLauncherFactory::TorLauncherFactory()
: is_starting_(false),
tor_pid_(-1) {
if (g_prevent_tor_launch_for_tests) {
tor_pid_ = 1234;
VLOG(1) << "Skipping the tor process launch in tests.";
return;
}
@@ -3,6 +3,8 @@
* 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 <memory>
#include "brave/browser/tor/buildflags.h"
#if BUILDFLAG(ENABLE_TOR)
@@ -33,3 +35,6 @@ std::unique_ptr<net::ProxyConfigService> CreateProxyConfigServiceTor(
#endif
#include "../../../../../chrome/browser/net/proxy_config_monitor.cc" // NOLINT
// Required by lint
#include "brave/chromium_src/chrome/browser/net/proxy_config_monitor.h"
@@ -0,0 +1,15 @@
/* 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/. */
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_NET_PROXY_CONFIG_MONITOR_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_NET_PROXY_CONFIG_MONITOR_H_
#define BRAVE_PROXY_CONFIG_MONITOR_H \
net::ProxyConfigService* GetProxyConfigServiceForTesting() \
{ return proxy_config_service_.get(); }
#include "../../../../../chrome/browser/net/proxy_config_monitor.h" // NOLINT
#undef BRAVE_PROXY_CONFIG_MONITOR_H
#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_NET_PROXY_CONFIG_MONITOR_H_
+1 -2
View File
@@ -42,8 +42,7 @@ HostPortPair FromStringWithAuthentication(const std::string& str) {
std::string MaybeAddUsernameAndPassword(const HostPortPair* host_port_pair,
const std::string& str) {
std::string ret;
if (!host_port_pair->username().empty() ||
!host_port_pair->password().empty()) {
if (!host_port_pair->username().empty()) {
ret += host_port_pair->username();
if (!host_port_pair->password().empty()) {
ret += ':';
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/. */
@@ -170,7 +170,7 @@ void ProxyConfigServiceTor::SetProxyAuthorization(
if (!username.empty()) {
auto* map = GetTorProxyMap(service);
if (!host_port_pair.username().empty()) {
if (host_port_pair.username() == username) {
// password is a int64_t -> std::to_string in milliseconds
int64_t time = strtoll(host_port_pair.password().c_str(), nullptr, 10);
map->MaybeExpire(host_port_pair.username(),
@@ -6,6 +6,7 @@
#include "brave/net/proxy_resolution/proxy_config_service_tor.h"
#include <string>
#include <memory>
#include "base/macros.h"
#include "base/threading/thread_task_runner_handle.h"
@@ -104,13 +105,10 @@ TEST_F(ProxyConfigServiceTorTest, CircuitIsolationKey) {
}
}
// TODO(bridiver) - test that it uses the proxy config service tor
// new ProxyConfigMonitor(tor_profile);
TEST_F(ProxyConfigServiceTorTest, SetNewTorCircuit) {
std::string proxy_uri("socks5://127.0.0.1:5566");
GURL site_url("https://check.torproject.org/");
std::string isolation_key =
const std::string proxy_uri("socks5://127.0.0.1:5566");
const GURL site_url("https://check.torproject.org/");
const std::string isolation_key =
ProxyConfigServiceTor::CircuitIsolationKey(site_url);
ProxyConfigServiceTor proxy_config_service(proxy_uri);
@@ -127,10 +125,13 @@ TEST_F(ProxyConfigServiceTorTest, SetNewTorCircuit) {
}
TEST_F(ProxyConfigServiceTorTest, SetProxyAuthorization) {
std::string proxy_uri("socks5://127.0.0.1:5566");
GURL site_url("https://check.torproject.org/");
std::string isolation_key =
const std::string proxy_uri("socks5://127.0.0.1:5566");
const GURL site_url("https://check.torproject.org/");
const GURL site_url2("https://brave.com/");
const std::string isolation_key =
ProxyConfigServiceTor::CircuitIsolationKey(site_url);
const std::string isolation_key2 =
ProxyConfigServiceTor::CircuitIsolationKey(site_url2);
auto config_service = ProxyResolutionService::CreateSystemProxyConfigService(
base::ThreadTaskRunnerHandle::Get());
@@ -167,9 +168,9 @@ TEST_F(ProxyConfigServiceTorTest, SetProxyAuthorization) {
EXPECT_EQ(host_port_pair.host(), "127.0.0.1");
EXPECT_EQ(host_port_pair.port(), 5566);
// TODO Test persistent circuit isolation until timeout.
// TODO(darkdh): Test persistent circuit isolation until timeout.
// Test new identity.
// Test new tor circuit.
proxy_config_service.SetNewTorCircuit(site_url);
proxy_config_service.GetLatestProxyConfig(&config);
ProxyInfo info3;
@@ -195,6 +196,18 @@ TEST_F(ProxyConfigServiceTorTest, SetProxyAuthorization) {
EXPECT_TRUE(info4.proxy_server().scheme() == ProxyServer::SCHEME_SOCKS5);
EXPECT_EQ(host_port_pair.host(), "127.0.0.1");
EXPECT_EQ(host_port_pair.port(), 5566);
// SetNewTorCircuit should not affect other urls
proxy_config_service.GetLatestProxyConfig(&config);
ProxyInfo info5;
ProxyConfigServiceTor::SetProxyAuthorization(
config, site_url2, service, &info5);
host_port_pair = info5.proxy_server().host_port_pair();
EXPECT_EQ(host_port_pair.username(), isolation_key2);
EXPECT_NE(host_port_pair.password(), password);
EXPECT_TRUE(info5.proxy_server().scheme() == ProxyServer::SCHEME_SOCKS5);
EXPECT_EQ(host_port_pair.host(), "127.0.0.1");
EXPECT_EQ(host_port_pair.port(), 5566);
}
} // namespace net
@@ -0,0 +1,13 @@
diff --git a/chrome/browser/net/proxy_config_monitor.h b/chrome/browser/net/proxy_config_monitor.h
index 0f20947c7819c3be2086a69f5997412652a99915..29f3b0cd58a012ce5656972be8ea3fe8c76fef43 100644
--- a/chrome/browser/net/proxy_config_monitor.h
+++ b/chrome/browser/net/proxy_config_monitor.h
@@ -63,7 +63,7 @@ class ProxyConfigMonitor : public net::ProxyConfigService::Observer,
// they're received, to allow tests to wait until all pending proxy
// configuration changes have been applied.
void FlushForTesting();
-
+ BRAVE_PROXY_CONFIG_MONITOR_H
private:
// net::ProxyConfigService::Observer implementation:
void OnProxyConfigChanged(