Skip to content

Commit

Permalink
Merge pull request #8135 from brave/no_opt_in_otr
Browse files Browse the repository at this point in the history
Do not show opt-in page for decentralized DNS support in OTR contexts
  • Loading branch information
yrliou committed Mar 18, 2021
1 parent 433c702 commit 698702a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
7 changes: 7 additions & 0 deletions browser/decentralized_dns/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# You can obtain one at http://mozilla.org/MPL/2.0/. */

import("//brave/build/config.gni")
import("//brave/components/tor/buildflags/buildflags.gni")
import("//testing/test.gni")

source_set("browser_tests") {
Expand All @@ -29,6 +30,7 @@ source_set("browser_tests") {
source_set("unit_tests") {
testonly = true
sources = [
"//brave/browser/decentralized_dns/test/decentralized_dns_navigation_throttle_unittest.cc",
"//brave/browser/decentralized_dns/test/utils_unittest.cc",
"//brave/net/dns/dns_transaction_unittest.cc",
]
Expand All @@ -37,11 +39,16 @@ source_set("unit_tests") {
"//base",
"//base/test:test_support",
"//brave/components/decentralized_dns",
"//brave/components/tor/buildflags",
"//chrome/test:test_support",
"//components/prefs",
"//net",
"//net:test_support",
"//testing/gmock",
"//testing/gtest",
]

if (enable_tor) {
deps += [ "//brave/browser/tor" ]
}
} # source_set("unit_tests")
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* Copyright (c) 2021 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/components/decentralized_dns/decentralized_dns_navigation_throttle.h"

#include "base/test/scoped_feature_list.h"
#include "brave/components/decentralized_dns/features.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/tor/tor_profile_manager.h"
#endif

constexpr char kTestProfileName[] = "TestProfile";

namespace decentralized_dns {

class DecentralizedDnsNavigationThrottleTest : public testing::Test {
public:
DecentralizedDnsNavigationThrottleTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()) {}
~DecentralizedDnsNavigationThrottleTest() override = default;

void SetUp() override {
feature_list_.InitAndEnableFeature(features::kDecentralizedDns);

ASSERT_TRUE(profile_manager_.SetUp());
profile_ = profile_manager_.CreateTestingProfile(kTestProfileName);
local_state_ = profile_manager_.local_state();
web_contents_ =
content::WebContentsTester::CreateTestWebContents(profile_, nullptr);
locale_ = "en-US";
}

void TearDown() override { web_contents_.reset(); }

PrefService* local_state() { return local_state_->Get(); }
content::WebContents* web_contents() { return web_contents_.get(); }

// Helper that creates simple test guest profile.
Profile* CreateGuestProfile() {
return profile_manager_.CreateGuestProfile()->GetPrimaryOTRProfile();
}

TestingProfile* profile() { return profile_; }

const std::string& locale() { return locale_; }

private:
content::BrowserTaskEnvironment task_environment_;
content::RenderViewHostTestEnabler test_render_host_factories_;
ScopedTestingLocalState* local_state_;
TestingProfileManager profile_manager_;
TestingProfile* profile_;
base::test::ScopedFeatureList feature_list_;
std::unique_ptr<content::WebContents> web_contents_;
std::string locale_;
};

TEST_F(DecentralizedDnsNavigationThrottleTest, Instantiation) {
content::MockNavigationHandle test_handle(web_contents());
auto throttle = DecentralizedDnsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, local_state(), locale());
EXPECT_TRUE(throttle != nullptr);

// Disable in OTR profile.
auto otr_web_contents = content::WebContentsTester::CreateTestWebContents(
profile()->GetPrimaryOTRProfile(), nullptr);
content::MockNavigationHandle otr_test_handle(otr_web_contents.get());
auto throttle_in_otr =
DecentralizedDnsNavigationThrottle::MaybeCreateThrottleFor(
&otr_test_handle, local_state(), locale());
EXPECT_EQ(throttle_in_otr, nullptr);

// Disable in guest profiles.
auto* guest_profile = CreateGuestProfile();
auto guest_web_contents =
content::WebContentsTester::CreateTestWebContents(guest_profile, nullptr);
content::MockNavigationHandle guest_test_handle(guest_web_contents.get());
auto throttle_in_guest =
DecentralizedDnsNavigationThrottle::MaybeCreateThrottleFor(
&guest_test_handle, local_state(), locale());
EXPECT_EQ(throttle_in_guest, nullptr);
}

#if BUILDFLAG(ENABLE_TOR)
TEST_F(DecentralizedDnsNavigationThrottleTest, NotInstantiatedInTor) {
Profile* tor_profile =
TorProfileManager::GetInstance().GetTorProfile(profile());
ASSERT_TRUE(tor_profile->IsTor());
ASSERT_TRUE(tor_profile->IsOffTheRecord());

auto tor_web_contents =
content::WebContentsTester::CreateTestWebContents(tor_profile, nullptr);
content::MockNavigationHandle tor_test_handle(tor_web_contents.get());
auto throttle_in_tor =
DecentralizedDnsNavigationThrottle::MaybeCreateThrottleFor(
&tor_test_handle, local_state(), locale());
EXPECT_EQ(throttle_in_tor, nullptr);
}
#endif

} // namespace decentralized_dns
4 changes: 4 additions & 0 deletions browser/profiles/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ source_set("profiles") {
if (enable_tor) {
deps += [ "//brave/browser/tor" ]
}

if (decentralized_dns_enabled) {
deps += [ "//brave/browser/decentralized_dns" ]
}
}
1 change: 1 addition & 0 deletions browser/tor/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ source_set("tor") {
visibility = [
":*",
"//brave/browser:browser_process",
"//brave/browser/decentralized_dns/test:unit_tests",
"//brave/browser/profiles",
"//brave/browser/ui",
"//brave/components/ipfs/test:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ DecentralizedDnsNavigationThrottle::MaybeCreateThrottleFor(
content::NavigationHandle* navigation_handle,
PrefService* local_state,
const std::string& locale) {
if (!IsDecentralizedDnsEnabled())
content::BrowserContext* context =
navigation_handle->GetWebContents()->GetBrowserContext();
if (!IsDecentralizedDnsEnabled() || context->IsOffTheRecord())
return nullptr;

return std::make_unique<DecentralizedDnsNavigationThrottle>(
Expand Down

0 comments on commit 698702a

Please sign in to comment.