Skip to content

Commit

Permalink
Tests for ipfs policy
Browse files Browse the repository at this point in the history
  • Loading branch information
yrliou committed Oct 30, 2020
1 parent 3292a97 commit 830a2df
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 28 deletions.
176 changes: 176 additions & 0 deletions browser/ipfs/ipfs_policy_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/* Copyright (c) 2020 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 "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "brave/browser/ipfs/content_browser_client_helper.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/browser/ipfs/ipfs_tab_helper.h"
#include "brave/components/ipfs/features.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_navigation_throttle.h"
#include "brave/components/ipfs/pref_names.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/navigation_handle_observer.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"

#include "components/infobars/core/infobar.h"

namespace policy {

template <bool enable>
class IpfsPolicyTest : public InProcessBrowserTest {
public:
IpfsPolicyTest() {
feature_list_.InitAndEnableFeature(ipfs::features::kIpfsFeature);
}

void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
ASSERT_TRUE(embedded_test_server()->Start());
ipfs_url_ = GURL(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"wiki/Vincent_van_Gogh.html");

auto* prefs = user_prefs::UserPrefs::Get(browser_context());
prefs->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_LOCAL));
}

void SetUpInProcessBrowserTestFixture() override {
EXPECT_CALL(provider_, IsInitializationComplete(testing::_))
.WillRepeatedly(testing::Return(true));
BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
PolicyMap policies;
policies.Set(key::kIPFSEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM,
base::Value(enable), nullptr);
provider_.UpdateChromePolicy(policies);
}

content::WebContents* web_contents() const {
return browser()->tab_strip_model()->GetActiveWebContents();
}

content::BrowserContext* browser_context() {
return web_contents()->GetBrowserContext();
}

size_t infobar_count() const {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
return infobar_service->infobar_count();
}

const GURL& ipfs_url() { return ipfs_url_; }

private:
MockConfigurationPolicyProvider provider_;
base::test::ScopedFeatureList feature_list_;
GURL ipfs_url_;
};

using IpfsEnabledPolicyTest = IpfsPolicyTest<true>;
using IpfsDisabledPolicyTest = IpfsPolicyTest<false>;

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, IsIpfsDisabledByPolicy) {
EXPECT_FALSE(ipfs::IpfsServiceFactory::IsIpfsDisabledByPolicy());
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, IsIpfsDisabledByPolicy) {
EXPECT_TRUE(ipfs::IpfsServiceFactory::IsIpfsDisabledByPolicy());
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, GetService) {
EXPECT_NE(ipfs::IpfsServiceFactory::GetForContext(browser_context()),
nullptr);
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, GetService) {
EXPECT_EQ(ipfs::IpfsServiceFactory::GetForContext(browser_context()),
nullptr);
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, IPFSPageAccess) {
content::NavigationHandleObserver observer(web_contents(),
GURL("chrome://ipfs"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("chrome://ipfs")));
EXPECT_TRUE(observer.has_committed());
EXPECT_FALSE(observer.is_error());
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, IPFSPageAccess) {
content::NavigationHandleObserver observer(web_contents(),
GURL("chrome://ipfs"));
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("chrome://ipfs")));
EXPECT_TRUE(observer.has_committed());
EXPECT_TRUE(observer.is_error());
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, NavigationThrottle) {
content::MockNavigationHandle test_handle(web_contents());
auto throttle = ipfs::IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs::IpfsServiceFactory::GetForContext(browser_context()),
"en-US");
EXPECT_TRUE(throttle != nullptr);
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, NavigationThrottle) {
content::MockNavigationHandle test_handle(web_contents());
auto throttle = ipfs::IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs::IpfsServiceFactory::GetForContext(browser_context()),
"en-US");
EXPECT_TRUE(throttle == nullptr);
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, TabHelper) {
EXPECT_TRUE(ipfs::IPFSTabHelper::MaybeCreateForWebContents(web_contents()));
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, TabHelper) {
EXPECT_FALSE(ipfs::IPFSTabHelper::MaybeCreateForWebContents(web_contents()));
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, HandleIPFSURLRewrite) {
GURL url(ipfs_url());
EXPECT_TRUE(ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite(
&url, browser_context()));
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, HandleIPFSURLRewrite) {
GURL url(ipfs_url());
EXPECT_FALSE(ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite(
&url, browser_context()));
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, ShouldNavigateIPFSURI) {
GURL new_url;
EXPECT_TRUE(ipfs::ContentBrowserClientHelper::ShouldNavigateIPFSURI(
ipfs_url(), &new_url, browser_context()));
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, ShouldNavigateIPFSURI) {
GURL new_url;
EXPECT_FALSE(ipfs::ContentBrowserClientHelper::ShouldNavigateIPFSURI(
ipfs_url(), &new_url, browser_context()));
}

} // namespace policy
5 changes: 3 additions & 2 deletions browser/ipfs/ipfs_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ IPFSTabHelper::IPFSTabHelper(content::WebContents* web_contents)
}

// static
void IPFSTabHelper::MaybeCreateForWebContents(
bool IPFSTabHelper::MaybeCreateForWebContents(
content::WebContents* web_contents) {
if (!ipfs::IpfsServiceFactory::GetForContext(
web_contents->GetBrowserContext())) {
return;
return false;
}

CreateForWebContents(web_contents);
return true;
}

void IPFSTabHelper::UpdateActiveState(content::NavigationHandle* handle) {
Expand Down
2 changes: 1 addition & 1 deletion browser/ipfs/ipfs_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class IPFSTabHelper : public content::WebContentsObserver,

bool IsActiveForMainFrame() const { return active_; }

static void MaybeCreateForWebContents(content::WebContents* web_contents);
static bool MaybeCreateForWebContents(content::WebContents* web_contents);

private:
friend class content::WebContentsUserData<IPFSTabHelper>;
Expand Down
45 changes: 20 additions & 25 deletions components/ipfs/ipfs_navigation_throttle_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class IpfsNavigationThrottleUnitTest : public testing::Test {
temp_dir_.GetPath().AppendASCII(TestingProfile::kTestUserProfileDir));
web_contents_ =
content::WebContentsTester::CreateTestWebContents(profile_, nullptr);
ipfs_service_ = IpfsServiceFactory::GetForContext(profile_);
locale_ = "en-US";
}

Expand All @@ -86,7 +85,9 @@ class IpfsNavigationThrottleUnitTest : public testing::Test {

content::WebContents* web_contents() { return web_contents_.get(); }

IpfsService* ipfs_service() { return ipfs_service_; }
IpfsService* ipfs_service(content::BrowserContext* context) {
return IpfsServiceFactory::GetForContext(context);
}

base::ScopedTempDir* temp_dir() { return &temp_dir_; }

Expand All @@ -107,7 +108,6 @@ class IpfsNavigationThrottleUnitTest : public testing::Test {
ScopedTestingLocalState local_state_;
content::RenderViewHostTestEnabler test_render_host_factories_;
std::unique_ptr<content::WebContents> web_contents_;
IpfsService* ipfs_service_;
Profile* profile_;
base::test::ScopedFeatureList feature_list_;
std::string locale_;
Expand All @@ -123,21 +123,21 @@ TEST_F(IpfsNavigationThrottleUnitTest, DeferUntilIpfsProcessLaunched) {
"/ip4/101.101.101.101/tcp/4001/p2p/"
"QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"};

ipfs_service()->SetSkipGetConnectedPeersCallbackForTest(true);
ipfs_service(profile())->SetSkipGetConnectedPeersCallbackForTest(true);

content::MockNavigationHandle test_handle(web_contents());
test_handle.set_url(GetIPFSURL());
auto* service = ipfs_service(profile());
auto throttle = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs_service(), brave::IsRegularProfile(profile()),
locale());
&test_handle, service, locale());
ASSERT_TRUE(throttle != nullptr);
bool was_navigation_resumed = false;
throttle->set_resume_callback_for_testing(
base::BindLambdaForTesting([&]() { was_navigation_resumed = true; }));
EXPECT_EQ(NavigationThrottle::DEFER, throttle->WillStartRequest().action())
<< GetIPFSURL();
ipfs_service()->SetIpfsLaunchedForTest(true);
ipfs_service()->RunLaunchDaemonCallbackForTest(true);
service->SetIpfsLaunchedForTest(true);
service->RunLaunchDaemonCallbackForTest(true);
EXPECT_TRUE(was_navigation_resumed);

was_navigation_resumed = false;
Expand All @@ -146,14 +146,14 @@ TEST_F(IpfsNavigationThrottleUnitTest, DeferUntilIpfsProcessLaunched) {
throttle->OnGetConnectedPeers(true, peers);
EXPECT_TRUE(was_navigation_resumed);

ipfs_service()->SetIpfsLaunchedForTest(false);
service->SetIpfsLaunchedForTest(false);

was_navigation_resumed = false;
test_handle.set_url(GetIPNSURL());
EXPECT_EQ(NavigationThrottle::DEFER, throttle->WillStartRequest().action())
<< GetIPNSURL();
ipfs_service()->SetIpfsLaunchedForTest(true);
ipfs_service()->RunLaunchDaemonCallbackForTest(true);
service->SetIpfsLaunchedForTest(true);
service->RunLaunchDaemonCallbackForTest(true);
EXPECT_TRUE(was_navigation_resumed);

was_navigation_resumed = false;
Expand All @@ -171,8 +171,7 @@ TEST_F(IpfsNavigationThrottleUnitTest, ProceedForGatewayNodeMode) {
content::MockNavigationHandle test_handle(web_contents());
test_handle.set_url(GetIPFSURL());
auto throttle = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs_service(), brave::IsRegularProfile(profile()),
locale());
&test_handle, ipfs_service(profile()), locale());
ASSERT_TRUE(throttle != nullptr);
EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action())
<< GetIPFSURL();
Expand All @@ -191,8 +190,7 @@ TEST_F(IpfsNavigationThrottleUnitTest, ProceedForAskNodeMode) {
content::MockNavigationHandle test_handle(web_contents());
test_handle.set_url(GetIPFSURL());
auto throttle = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs_service(), brave::IsRegularProfile(profile()),
locale());
&test_handle, ipfs_service(profile()), locale());
ASSERT_TRUE(throttle != nullptr);
EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action())
<< GetIPFSURL();
Expand All @@ -207,17 +205,16 @@ TEST_F(IpfsNavigationThrottleUnitTest, ProceedForAskNodeMode) {
TEST_F(IpfsNavigationThrottleUnitTest, Instantiation) {
content::MockNavigationHandle test_handle(web_contents());
auto throttle = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs_service(), brave::IsRegularProfile(profile()),
locale());
&test_handle, ipfs_service(profile()), 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 = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&otr_test_handle, ipfs_service(),
brave::IsRegularProfile(profile()->GetPrimaryOTRProfile()), locale());
&otr_test_handle, ipfs_service(profile()->GetPrimaryOTRProfile()),
locale());
EXPECT_EQ(throttle_in_otr, nullptr);

// Disable in guest sessions.
Expand All @@ -226,8 +223,7 @@ TEST_F(IpfsNavigationThrottleUnitTest, Instantiation) {
guest_profile.get(), nullptr);
content::MockNavigationHandle guest_test_handle(guest_web_contents.get());
auto throttle_in_guest = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&guest_test_handle, ipfs_service(),
brave::IsRegularProfile(guest_profile.get()), locale());
&guest_test_handle, ipfs_service(guest_profile.get()), locale());
EXPECT_EQ(throttle_in_guest, nullptr);
}

Expand All @@ -246,16 +242,15 @@ TEST_F(IpfsNavigationThrottleUnitTest, NotInstantiatedInTor) {
tor_reg_profile, nullptr);
content::MockNavigationHandle tor_reg_test_handle(tor_reg_web_contents.get());
auto throttle_in_tor_reg = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&tor_reg_test_handle, ipfs_service(),
brave::IsRegularProfile(tor_reg_profile), locale());
&tor_reg_test_handle, ipfs_service(tor_reg_profile), locale());
EXPECT_EQ(throttle_in_tor_reg, nullptr);

auto tor_otr_web_contents = content::WebContentsTester::CreateTestWebContents(
tor_reg_profile->GetPrimaryOTRProfile(), nullptr);
content::MockNavigationHandle tor_otr_test_handle(tor_otr_web_contents.get());
auto throttle_in_tor_otr = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&tor_otr_test_handle, ipfs_service(),
brave::IsRegularProfile(tor_reg_profile), locale());
&tor_otr_test_handle,
ipfs_service(tor_reg_profile->GetPrimaryOTRProfile()), locale());
EXPECT_EQ(throttle_in_tor_otr, nullptr);
}
#endif
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ if (!is_android) {
if (ipfs_enabled) {
sources += [
"//brave/browser/extensions/api/ipfs_apitest.cc",
"//brave/browser/ipfs/ipfs_policy_browsertest.cc",
"//brave/browser/ipfs/ipfs_tab_helper_browsertest.cc",
]
deps += [
Expand Down

0 comments on commit 830a2df

Please sign in to comment.