From 014d4802f111a5fb0a8512edabf4f619dce8cd40 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Fri, 5 Jul 2019 16:37:53 -0700 Subject: [PATCH] Add test for ProxyConfigMonitor --- .../brave_profile_manager_unittest.cc | 21 +++++++++++ .../profiles/tor_unittest_profile_manager.cc | 6 +++- browser/tor/tor_launcher_factory.cc | 1 + .../browser/net/proxy_config_monitor.cc | 5 +++ .../chrome/browser/net/proxy_config_monitor.h | 15 ++++++++ chromium_src/net/base/host_port_pair.cc | 3 +- .../browser/webtorrent_util.h | 3 +- .../proxy_config_service_tor.cc | 2 +- .../proxy_config_service_tor_unittest.cc | 35 +++++++++++++------ ...e-browser-net-proxy_config_monitor.h.patch | 13 +++++++ 10 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 chromium_src/chrome/browser/net/proxy_config_monitor.h create mode 100644 patches/chrome-browser-net-proxy_config_monitor.h.patch diff --git a/browser/profiles/brave_profile_manager_unittest.cc b/browser/profiles/brave_profile_manager_unittest.cc index 4ea113eb9b81..9d1fc7bebd22 100644 --- a/browser/profiles/brave_profile_manager_unittest.cc +++ b/browser/profiles/brave_profile_manager_unittest.cc @@ -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 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)); +} diff --git a/browser/profiles/tor_unittest_profile_manager.cc b/browser/profiles/tor_unittest_profile_manager.cc index 066c7f60d1cf..8a50297f410b 100644 --- a/browser/profiles/tor_unittest_profile_manager.cc +++ b/browser/profiles/tor_unittest_profile_manager.cc @@ -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 +#include + #include "base/files/file_util.h" #include "base/threading/thread_task_runner_handle.h" #include "brave/browser/profiles/brave_profile_manager.h" diff --git a/browser/tor/tor_launcher_factory.cc b/browser/tor/tor_launcher_factory.cc index 63d16d520108..70840a9dfe1d 100644 --- a/browser/tor/tor_launcher_factory.cc +++ b/browser/tor/tor_launcher_factory.cc @@ -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; } diff --git a/chromium_src/chrome/browser/net/proxy_config_monitor.cc b/chromium_src/chrome/browser/net/proxy_config_monitor.cc index 213677fee065..cb61266083ba 100644 --- a/chromium_src/chrome/browser/net/proxy_config_monitor.cc +++ b/chromium_src/chrome/browser/net/proxy_config_monitor.cc @@ -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 + #include "brave/browser/tor/buildflags.h" #if BUILDFLAG(ENABLE_TOR) @@ -33,3 +35,6 @@ std::unique_ptr 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" diff --git a/chromium_src/chrome/browser/net/proxy_config_monitor.h b/chromium_src/chrome/browser/net/proxy_config_monitor.h new file mode 100644 index 000000000000..f6bf22a02e2e --- /dev/null +++ b/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_ diff --git a/chromium_src/net/base/host_port_pair.cc b/chromium_src/net/base/host_port_pair.cc index 509886ddcbe6..9899a7870812 100644 --- a/chromium_src/net/base/host_port_pair.cc +++ b/chromium_src/net/base/host_port_pair.cc @@ -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 += ':'; diff --git a/components/brave_webtorrent/browser/webtorrent_util.h b/components/brave_webtorrent/browser/webtorrent_util.h index f9abae45bd5c..14728f839df6 100644 --- a/components/brave_webtorrent/browser/webtorrent_util.h +++ b/components/brave_webtorrent/browser/webtorrent_util.h @@ -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/. */ diff --git a/net/proxy_resolution/proxy_config_service_tor.cc b/net/proxy_resolution/proxy_config_service_tor.cc index 8fc0dc63d1db..8b2be4366116 100644 --- a/net/proxy_resolution/proxy_config_service_tor.cc +++ b/net/proxy_resolution/proxy_config_service_tor.cc @@ -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(), diff --git a/net/proxy_resolution/proxy_config_service_tor_unittest.cc b/net/proxy_resolution/proxy_config_service_tor_unittest.cc index cd88832bb9a5..894eec1c603e 100644 --- a/net/proxy_resolution/proxy_config_service_tor_unittest.cc +++ b/net/proxy_resolution/proxy_config_service_tor_unittest.cc @@ -6,6 +6,7 @@ #include "brave/net/proxy_resolution/proxy_config_service_tor.h" #include +#include #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 diff --git a/patches/chrome-browser-net-proxy_config_monitor.h.patch b/patches/chrome-browser-net-proxy_config_monitor.h.patch new file mode 100644 index 000000000000..0ad66ce790fb --- /dev/null +++ b/patches/chrome-browser-net-proxy_config_monitor.h.patch @@ -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(