1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/policy/dlp/dlp_content_tab_helper.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/chromeos/policy/dlp/test/mock_dlp_content_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_activity_simulator.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/test_browser_window.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/navigation_simulator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
using testing::_;
using testing::Return;
namespace {
const DlpContentRestrictionSet kEmptyRestrictionSet;
const DlpContentRestrictionSet kNonEmptyRestrictionSet(
DlpContentRestriction::kScreenshot,
DlpRulesManager::Level::kBlock);
} // namespace
class DlpContentTabHelperTest : public ChromeRenderViewHostTestHarness {
protected:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
scoped_dlp_content_observer_ =
std::make_unique<ScopedDlpContentObserverForTesting>(
&mock_dlp_content_observer_);
// Initialize browser.
const Browser::CreateParams params(profile(), /*user_gesture=*/true);
browser_ = CreateBrowserWithTestWindowForParams(params);
tab_strip_model_ = browser_->tab_strip_model();
}
void TearDown() override {
tab_strip_model_->CloseAllTabs();
browser_.reset();
scoped_dlp_content_observer_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
DlpContentTabHelper::ScopedIgnoreDlpRulesManager ignore_dlp_rules_manager_{
DlpContentTabHelper::IgnoreDlpRulesManagerForTesting()};
MockDlpContentObserver mock_dlp_content_observer_;
std::unique_ptr<ScopedDlpContentObserverForTesting>
scoped_dlp_content_observer_;
TabActivitySimulator tab_activity_simulator_;
raw_ptr<TabStripModel, DanglingUntriaged> tab_strip_model_;
std::unique_ptr<Browser> browser_;
};
TEST_F(DlpContentTabHelperTest, NotCreatedForIncognito) {
const Browser::CreateParams params(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true),
/*user_gesture=*/true);
auto browser = CreateBrowserWithTestWindowForParams(params);
content::WebContents* web_contents =
tab_activity_simulator_.AddWebContentsAndNavigate(
browser->tab_strip_model(), GURL("https://example.com"));
EXPECT_EQ(nullptr, DlpContentTabHelper::FromWebContents(web_contents));
// Close tabs before |browser| is destructed.
browser->tab_strip_model()->CloseAllTabs();
}
TEST_F(DlpContentTabHelperTest, NotConfidential) {
GURL kUrl = GURL("https://example.com");
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
GURL(), kEmptyRestrictionSet);
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kUrl, kEmptyRestrictionSet);
EXPECT_CALL(mock_dlp_content_observer_, OnConfidentialityChanged(_, _))
.Times(0);
EXPECT_CALL(mock_dlp_content_observer_, OnVisibilityChanged(_)).Times(0);
content::WebContents* web_contents =
tab_activity_simulator_.AddWebContentsAndNavigate(tab_strip_model_, kUrl);
EXPECT_NE(nullptr, DlpContentTabHelper::FromWebContents(web_contents));
EXPECT_CALL(mock_dlp_content_observer_, OnWebContentsDestroyed(_)).Times(1);
}
TEST_F(DlpContentTabHelperTest, Confidential) {
GURL kUrl = GURL("https://example.com");
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
GURL(), kEmptyRestrictionSet);
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kUrl, kNonEmptyRestrictionSet);
EXPECT_CALL(mock_dlp_content_observer_,
OnConfidentialityChanged(_, kNonEmptyRestrictionSet))
.Times(1);
EXPECT_CALL(mock_dlp_content_observer_, OnVisibilityChanged(_)).Times(0);
content::WebContents* web_contents =
tab_activity_simulator_.AddWebContentsAndNavigate(tab_strip_model_, kUrl);
EXPECT_NE(nullptr, DlpContentTabHelper::FromWebContents(web_contents));
EXPECT_CALL(mock_dlp_content_observer_,
OnConfidentialityChanged(_, kEmptyRestrictionSet))
.Times(1);
EXPECT_CALL(mock_dlp_content_observer_, OnWebContentsDestroyed(_)).Times(1);
}
TEST_F(DlpContentTabHelperTest, VisibilityChanged) {
GURL kUrl1 = GURL("https://example1.com");
GURL kUrl2 = GURL("https://example2.com");
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
GURL(), kEmptyRestrictionSet);
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kUrl1, kNonEmptyRestrictionSet);
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kUrl2, kEmptyRestrictionSet);
EXPECT_CALL(mock_dlp_content_observer_,
OnConfidentialityChanged(_, kNonEmptyRestrictionSet))
.Times(1);
EXPECT_CALL(mock_dlp_content_observer_, OnVisibilityChanged(_)).Times(0);
content::WebContents* web_contents1 =
tab_activity_simulator_.AddWebContentsAndNavigate(tab_strip_model_,
kUrl1);
content::WebContents* web_contents2 =
tab_activity_simulator_.AddWebContentsAndNavigate(tab_strip_model_,
kUrl2);
EXPECT_NE(nullptr, DlpContentTabHelper::FromWebContents(web_contents1));
EXPECT_NE(nullptr, DlpContentTabHelper::FromWebContents(web_contents2));
EXPECT_CALL(mock_dlp_content_observer_, OnVisibilityChanged(_)).Times(1);
tab_activity_simulator_.SwitchToTabAt(tab_strip_model_, 1);
EXPECT_CALL(mock_dlp_content_observer_, OnVisibilityChanged(_)).Times(1);
tab_activity_simulator_.SwitchToTabAt(tab_strip_model_, 0);
EXPECT_CALL(mock_dlp_content_observer_,
OnConfidentialityChanged(_, kEmptyRestrictionSet))
.Times(1);
EXPECT_CALL(mock_dlp_content_observer_, OnWebContentsDestroyed(_)).Times(2);
}
TEST_F(DlpContentTabHelperTest, SubFrameNavigation) {
GURL kNonConfidentialUrl = GURL("https://example.com");
GURL kConfidentialUrl = GURL("https://google.com");
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
GURL(), kEmptyRestrictionSet);
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kNonConfidentialUrl, kEmptyRestrictionSet);
DlpContentRestrictionSet::SetRestrictionsForURLForTesting(
kConfidentialUrl, kNonEmptyRestrictionSet);
EXPECT_CALL(mock_dlp_content_observer_, OnConfidentialityChanged(_, _))
.Times(0);
EXPECT_CALL(mock_dlp_content_observer_, OnVisibilityChanged(_)).Times(0);
// Create WebContents.
content::WebContents* web_contents =
tab_activity_simulator_.AddWebContentsAndNavigate(tab_strip_model_,
kNonConfidentialUrl);
EXPECT_NE(nullptr, DlpContentTabHelper::FromWebContents(web_contents));
// Add subframe and navigate to confidential URL.
EXPECT_CALL(mock_dlp_content_observer_,
OnConfidentialityChanged(_, kNonEmptyRestrictionSet))
.Times(1);
content::RenderFrameHost* subframe =
content::NavigationSimulator::NavigateAndCommitFromDocument(
kConfidentialUrl, content::RenderFrameHostTester::For(
web_contents->GetPrimaryMainFrame())
->AppendChild("child"));
// Navigate away from confidential URL.
EXPECT_CALL(mock_dlp_content_observer_,
OnConfidentialityChanged(_, kEmptyRestrictionSet))
.Times(1);
content::NavigationSimulator::NavigateAndCommitFromDocument(
kNonConfidentialUrl, subframe);
EXPECT_CALL(mock_dlp_content_observer_, OnWebContentsDestroyed(_)).Times(1);
}
} // namespace policy
|