Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ScopedTorLaunchPreventerForTest to prevent tor process launch #827

Merged
merged 1 commit into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions browser/search_engine_provider_controller_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/profiles/brave_profile_manager.h"
#include "brave/browser/search_engine_provider_util.h"
#include "brave/browser/tor/tor_launcher_factory.h"
#include "brave/browser/ui/browser_commands.h"
#include "brave/components/search_engines/brave_prepopulated_engines.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_service_observer.h"
#include "content/public/test/test_utils.h"

using SearchEngineProviderControllerTest = InProcessBrowserTest;

class TorSearchEngineProviderControllerTest : public InProcessBrowserTest {
public:
void SetUp() override {
disable_io_checks();
InProcessBrowserTest::SetUp();
}
};
bool IsRegionForQwant(Profile* profile) {
return TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(
profile->GetPrefs())->prepopulate_id ==
TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_QWANT;
}

TemplateURLData CreateTestSearchEngine() {
TemplateURLData result;
Expand Down Expand Up @@ -85,20 +88,23 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderControllerTest,
brave::ToggleUseAlternativeSearchEngineProvider(private_window_2->profile());
}

// This test crashes with below. I don't know how to deal with now.
// [FATAL:brave_content_browser_client.cc(217)] Check failed: !path.empty().
// TODO(simonhong): Enable this later.
IN_PROC_BROWSER_TEST_F(TorSearchEngineProviderControllerTest,
DISABLED_CheckTorProfileSearchProviderTest) {
base::FilePath tor_path = BraveProfileManager::GetTorProfilePath();
ProfileManager* profile_manager = g_browser_process->profile_manager();
Profile* tor_profile = profile_manager->GetProfile(tor_path);
// Checks the default search engine of the tor profile.
IN_PROC_BROWSER_TEST_F(SearchEngineProviderControllerTest,
CheckDefaultTorProfileSearchProviderTest) {
ScopedTorLaunchPreventerForTest prevent_tor_process;

brave::NewOffTheRecordWindowTor(browser());
content::RunAllTasksUntilIdle();

Profile* tor_profile = BrowserList::GetInstance()->GetLastActive()->profile();
EXPECT_TRUE(tor_profile->IsTorProfile());

auto* service = TemplateURLServiceFactory::GetForProfile(tor_profile);

std::string default_provider = IsRegionForQwant(tor_profile) ? "Qwant"
: "DuckDuckGo";

//Check tor profile's search provider is set to ddg.
EXPECT_EQ(service->GetDefaultSearchProvider()->data().short_name(),
base::ASCIIToUTF16("DuckDuckGo"));

content::RunAllTasksUntilIdle();
base::ASCIIToUTF16(default_provider));
}
27 changes: 27 additions & 0 deletions browser/tor/tor_launcher_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@

using content::BrowserThread;

namespace {
bool g_prevent_tor_launch_for_tests = false;
}

// static
TorLauncherFactory* TorLauncherFactory::GetInstance() {
return base::Singleton<TorLauncherFactory>::get();
}

TorLauncherFactory::TorLauncherFactory()
: tor_pid_(-1) {
if (g_prevent_tor_launch_for_tests) {
VLOG(1) << "Skipping the tor process launch in tests.";
return;
}

content::ServiceManagerConnection::GetForProcess()->GetConnector()
->BindInterface(tor::mojom::kTorLauncherServiceName,
&tor_launcher_);
Expand All @@ -42,6 +51,11 @@ bool TorLauncherFactory::SetConfig(const tor::TorConfig& config) {

void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (g_prevent_tor_launch_for_tests) {
VLOG(1) << "Skipping the tor process launch in tests.";
return;
}

if (tor_pid_ >= 0) {
LOG(WARNING) << "tor process(" << tor_pid_ << ") is running";
return;
Expand All @@ -57,6 +71,11 @@ void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) {

void TorLauncherFactory::ReLaunchTorProcess(const tor::TorConfig& config) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DLOG_IF(ERROR, g_prevent_tor_launch_for_tests)
<< "The tor process launch was suppressed for testing. Feel free to "
"replace this logging with a condition if you want to prevent "
"relaunch as well";

if (tor_pid_ < 0) {
LOG(WARNING) << "No currently running tor process. \
Did you call LaunchTorProcess?";
Expand Down Expand Up @@ -103,3 +122,11 @@ void TorLauncherFactory::OnTorLaunched(bool result, int64_t pid) {
for (auto& observer : observers_)
observer.NotifyTorLaunched(result, pid);
}

ScopedTorLaunchPreventerForTest::ScopedTorLaunchPreventerForTest() {
g_prevent_tor_launch_for_tests = true;
}

ScopedTorLaunchPreventerForTest::~ScopedTorLaunchPreventerForTest() {
g_prevent_tor_launch_for_tests = false;
}
7 changes: 7 additions & 0 deletions browser/tor/tor_launcher_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ class TorLauncherFactory {
DISALLOW_COPY_AND_ASSIGN(TorLauncherFactory);
};

// Use this in tests to avoid the actual launch of the Tor process.
class ScopedTorLaunchPreventerForTest {
public:
ScopedTorLaunchPreventerForTest();
~ScopedTorLaunchPreventerForTest();
};

#endif // BRAVE_BROWSER_TOR_TOR_LAUNCHER_FACTORY_H_