Commit 8a905049 authored by Dominique Fauteux-Chapleau's avatar Dominique Fauteux-Chapleau Committed by Commit Bot

Reuse upload browsertests for corresponding Enterprise Connectors

This reuses tests by having the policy setters of
DeepScanningBrowserTestBase set either legacy or Connector policies, and
then making every upload browsertest parametrized with a bool so both
policy sets get tested.

The DeepScanningDialogViews test also change a bit, the ScanType test
parameter is removed since this dialog doesn't depend on the type of
scanning being done. This avoids having a very large number of
combinations for that test that don't cover different code paths.

Bug: 1076083
Change-Id: I419b21de0095add8b5b214993be010d022941202
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2210782Reviewed-by: default avatarRoger Tawa <rogerta@chromium.org>
Commit-Queue: Dominique Fauteux-Chapleau <domfc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770745}
parent 2613db76
......@@ -27,7 +27,7 @@ AnalysisServiceSettings::AnalysisServiceSettings(
matcher_ = std::make_unique<url_matcher::URLMatcher>();
url_matcher::URLMatcherConditionSet::ID id(0);
const base::Value* enable = settings_value.FindListKey(kKeyEnable);
if (enable && enable->is_list()) {
if (enable && enable->is_list() && !enable->GetList().empty()) {
for (const base::Value& value : enable->GetList())
AddUrlPatternSettings(value, true, &id);
} else {
......
......@@ -5,13 +5,16 @@
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_browsertest_base.h"
#include "base/bind_helpers.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/connectors/connectors_manager.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_dialog_views.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/deep_scanning_test_utils.h"
#include "chrome/browser/safe_browsing/cloud_content_scanning/fake_deep_scanning_dialog_delegate.h"
#include "chrome/browser/safe_browsing/dm_token_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/safe_browsing/core/features.h"
namespace safe_browsing {
......@@ -63,10 +66,17 @@ class UnresponsiveDeepScanningDialogDelegate
} // namespace
DeepScanningBrowserTestBase::DeepScanningBrowserTestBase() {
DeepScanningBrowserTestBase::DeepScanningBrowserTestBase(
bool use_legacy_policies)
: use_legacy_policies_(use_legacy_policies) {
// Enable every deep scanning features.
scoped_feature_list_.InitWithFeatures(
{kContentComplianceEnabled, kMalwareScanEnabled}, {});
if (use_legacy_policies_) {
scoped_feature_list_.InitWithFeatures(
{kContentComplianceEnabled, kMalwareScanEnabled}, {});
} else {
scoped_feature_list_.InitWithFeatures(
{enterprise_connectors::kEnterpriseConnectorsEnabled}, {});
}
// Change the time values of the upload UI to smaller ones to make tests
// showing it run faster.
......@@ -78,7 +88,12 @@ DeepScanningBrowserTestBase::DeepScanningBrowserTestBase() {
DeepScanningBrowserTestBase::~DeepScanningBrowserTestBase() = default;
void DeepScanningBrowserTestBase::SetUpOnMainThread() {
enterprise_connectors::ConnectorsManager::GetInstance()->SetUpForTesting();
}
void DeepScanningBrowserTestBase::TearDownOnMainThread() {
enterprise_connectors::ConnectorsManager::GetInstance()->TearDownForTesting();
DeepScanningDialogDelegate::ResetFactoryForTesting();
SetDlpPolicy(CheckContentComplianceValues::CHECK_NONE);
......@@ -90,42 +105,68 @@ void DeepScanningBrowserTestBase::TearDownOnMainThread() {
BlockUnsupportedFiletypesValues::BLOCK_UNSUPPORTED_FILETYPES_NONE);
SetBlockLargeFileTransferPolicy(BlockLargeFileTransferValues::BLOCK_NONE);
SetUnsafeEventsReportingPolicy(false);
ClearUrlsToCheckComplianceOfDownloads();
ClearUrlsToCheckForMalwareOfUploads();
}
void DeepScanningBrowserTestBase::SetDlpPolicy(
CheckContentComplianceValues state) {
g_browser_process->local_state()->SetInteger(prefs::kCheckContentCompliance,
state);
if (use_legacy_policies_) {
g_browser_process->local_state()->SetInteger(prefs::kCheckContentCompliance,
state);
} else {
SetDlpPolicyForConnectors(state);
}
}
void DeepScanningBrowserTestBase::SetMalwarePolicy(
SendFilesForMalwareCheckValues state) {
browser()->profile()->GetPrefs()->SetInteger(
prefs::kSafeBrowsingSendFilesForMalwareCheck, state);
if (use_legacy_policies_) {
browser()->profile()->GetPrefs()->SetInteger(
prefs::kSafeBrowsingSendFilesForMalwareCheck, state);
} else {
SetMalwarePolicyForConnectors(state);
}
}
void DeepScanningBrowserTestBase::SetWaitPolicy(
DelayDeliveryUntilVerdictValues state) {
g_browser_process->local_state()->SetInteger(
prefs::kDelayDeliveryUntilVerdict, state);
if (use_legacy_policies_) {
g_browser_process->local_state()->SetInteger(
prefs::kDelayDeliveryUntilVerdict, state);
} else {
SetDelayDeliveryUntilVerdictPolicyForConnectors(state);
}
}
void DeepScanningBrowserTestBase::SetAllowPasswordProtectedFilesPolicy(
AllowPasswordProtectedFilesValues state) {
g_browser_process->local_state()->SetInteger(
prefs::kAllowPasswordProtectedFiles, state);
if (use_legacy_policies_) {
g_browser_process->local_state()->SetInteger(
prefs::kAllowPasswordProtectedFiles, state);
} else {
SetAllowPasswordProtectedFilesPolicyForConnectors(state);
}
}
void DeepScanningBrowserTestBase::SetBlockUnsupportedFileTypesPolicy(
BlockUnsupportedFiletypesValues state) {
g_browser_process->local_state()->SetInteger(
prefs::kBlockUnsupportedFiletypes, state);
if (use_legacy_policies_) {
g_browser_process->local_state()->SetInteger(
prefs::kBlockUnsupportedFiletypes, state);
} else {
SetBlockUnsupportedFileTypesPolicyForConnectors(state);
}
}
void DeepScanningBrowserTestBase::SetBlockLargeFileTransferPolicy(
BlockLargeFileTransferValues state) {
g_browser_process->local_state()->SetInteger(prefs::kBlockLargeFileTransfer,
state);
if (use_legacy_policies_) {
g_browser_process->local_state()->SetInteger(prefs::kBlockLargeFileTransfer,
state);
} else {
SetBlockLargeFileTransferPolicyForConnectors(state);
}
}
void DeepScanningBrowserTestBase::SetUnsafeEventsReportingPolicy(bool report) {
......@@ -135,9 +176,44 @@ void DeepScanningBrowserTestBase::SetUnsafeEventsReportingPolicy(bool report) {
void DeepScanningBrowserTestBase::AddUrlToCheckComplianceOfDownloads(
const std::string& url) {
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckComplianceOfDownloadedContent)
->Append(url);
if (use_legacy_policies_) {
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckComplianceOfDownloadedContent)
->Append(url);
} else {
AddUrlsToCheckComplianceOfDownloadsForConnectors({url});
}
}
void DeepScanningBrowserTestBase::AddUrlToCheckForMalwareOfUploads(
const std::string& url) {
if (use_legacy_policies_) {
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckForMalwareOfUploadedContent)
->Append(url);
} else {
AddUrlsToCheckForMalwareOfUploadsForConnectors({url});
}
}
void DeepScanningBrowserTestBase::ClearUrlsToCheckComplianceOfDownloads() {
if (use_legacy_policies_) {
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckComplianceOfDownloadedContent)
->Clear();
} else {
ClearUrlsToCheckComplianceOfDownloadsForConnectors();
}
}
void DeepScanningBrowserTestBase::ClearUrlsToCheckForMalwareOfUploads() {
if (use_legacy_policies_) {
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckForMalwareOfUploadedContent)
->Clear();
} else {
ClearUrlsToCheckForMalwareOfUploadsForConnectors();
}
}
void DeepScanningBrowserTestBase::SetUpDelegate() {
......
......@@ -15,9 +15,10 @@ namespace safe_browsing {
// used by browser tests should be added to this class.
class DeepScanningBrowserTestBase : public InProcessBrowserTest {
public:
DeepScanningBrowserTestBase();
explicit DeepScanningBrowserTestBase(bool use_legacy_policies = true);
~DeepScanningBrowserTestBase() override;
void SetUpOnMainThread() override;
void TearDownOnMainThread() override;
// Setters for deep scanning policies.
......@@ -31,6 +32,9 @@ class DeepScanningBrowserTestBase : public InProcessBrowserTest {
void SetBlockLargeFileTransferPolicy(BlockLargeFileTransferValues state);
void SetUnsafeEventsReportingPolicy(bool report);
void AddUrlToCheckComplianceOfDownloads(const std::string& url);
void AddUrlToCheckForMalwareOfUploads(const std::string& url);
void ClearUrlsToCheckComplianceOfDownloads();
void ClearUrlsToCheckForMalwareOfUploads();
// Sets up a FakeDeepScanningDialogDelegate to use this class's StatusCallback
// and EncryptionStatusCallback. Also sets up a test DM token.
......@@ -64,6 +68,7 @@ class DeepScanningBrowserTestBase : public InProcessBrowserTest {
DeepScanningClientResponse status_callback_response_;
base::ScopedTempDir temp_dir_;
std::vector<base::FilePath> created_file_paths_;
bool use_legacy_policies_;
};
} // namespace safe_browsing
......
......@@ -176,7 +176,8 @@ class DeepScanningDialogDelegateBrowserTest
: public DeepScanningBrowserTestBase,
public DeepScanningDialogViews::TestObserver {
public:
DeepScanningDialogDelegateBrowserTest() {
explicit DeepScanningDialogDelegateBrowserTest(bool use_legacy_policies)
: DeepScanningBrowserTestBase(use_legacy_policies) {
DeepScanningDialogViews::SetObserverForTesting(this);
}
......@@ -190,9 +191,7 @@ class DeepScanningDialogDelegateBrowserTest
// Add the wildcard pattern to this policy since malware responses are
// verified for most of these tests.
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckForMalwareOfUploadedContent)
->Append("*");
AddUrlToCheckForMalwareOfUploads("*");
client_ = std::make_unique<policy::MockCloudPolicyClient>();
extensions::SafeBrowsingPrivateEventRouterFactory::GetForProfile(
......@@ -219,7 +218,16 @@ class DeepScanningDialogDelegateBrowserTest
base::ScopedTempDir temp_dir_;
};
IN_PROC_BROWSER_TEST_F(DeepScanningDialogDelegateBrowserTest, Unauthorized) {
class DeepScanningDialogDelegateSimpleBrowserTest
: public DeepScanningDialogDelegateBrowserTest,
public testing::WithParamInterface<bool> {
public:
DeepScanningDialogDelegateSimpleBrowserTest()
: DeepScanningDialogDelegateBrowserTest(GetParam()) {}
};
IN_PROC_BROWSER_TEST_P(DeepScanningDialogDelegateSimpleBrowserTest,
Unauthorized) {
EnableUploadsScanningAndReporting();
DeepScanningDialogDelegate::SetFactoryForTesting(
......@@ -267,7 +275,7 @@ IN_PROC_BROWSER_TEST_F(DeepScanningDialogDelegateBrowserTest, Unauthorized) {
ASSERT_EQ(FakeBinaryUploadServiceStorage()->requests_count(), 1);
}
IN_PROC_BROWSER_TEST_F(DeepScanningDialogDelegateBrowserTest, Files) {
IN_PROC_BROWSER_TEST_P(DeepScanningDialogDelegateSimpleBrowserTest, Files) {
base::ScopedAllowBlockingForTesting allow_blocking;
// Set up delegate and upload service.
......@@ -345,15 +353,115 @@ IN_PROC_BROWSER_TEST_F(DeepScanningDialogDelegateBrowserTest, Files) {
ASSERT_EQ(FakeBinaryUploadServiceStorage()->requests_count(), 3);
}
IN_PROC_BROWSER_TEST_P(DeepScanningDialogDelegateSimpleBrowserTest, Texts) {
// Set up delegate and upload service.
EnableUploadsScanningAndReporting();
DeepScanningDialogDelegate::SetFactoryForTesting(
base::BindRepeating(&MinimalFakeDeepScanningDialogDelegate::Create));
FakeBinaryUploadServiceStorage()->SetAuthorized(true);
// Prepare a complex DLP response to test that the verdict is reported
// correctly in the sensitive data event.
DeepScanningClientResponse response;
DlpDeepScanningVerdict* verdict = response.mutable_dlp_scan_verdict();
verdict->set_status(DlpDeepScanningVerdict::SUCCESS);
DlpDeepScanningVerdict::TriggeredRule* rule1 = verdict->add_triggered_rules();
rule1->set_rule_id(1);
rule1->set_action(DlpDeepScanningVerdict::TriggeredRule::REPORT_ONLY);
rule1->set_rule_resource_name("resource name 1");
rule1->set_rule_severity("severity 1");
DlpDeepScanningVerdict::MatchedDetector* detector1 =
rule1->add_matched_detectors();
detector1->set_detector_id("id1");
detector1->set_detector_type("dlp1");
detector1->set_display_name("display name 1");
DlpDeepScanningVerdict::TriggeredRule* rule2 = verdict->add_triggered_rules();
rule2->set_rule_id(3);
rule2->set_action(DlpDeepScanningVerdict::TriggeredRule::BLOCK);
rule2->set_rule_resource_name("resource rule 2");
rule2->set_rule_severity("severity 2");
DlpDeepScanningVerdict::MatchedDetector* detector2_1 =
rule2->add_matched_detectors();
detector2_1->set_detector_id("id2.1");
detector2_1->set_detector_type("type2.1");
detector2_1->set_display_name("display name 2.1");
DlpDeepScanningVerdict::MatchedDetector* detector2_2 =
rule2->add_matched_detectors();
detector2_2->set_detector_id("id2.2");
detector2_2->set_detector_type("type2.2");
detector2_2->set_display_name("display name 2.2");
FakeBinaryUploadServiceStorage()->SetResponseForText(
BinaryUploadService::Result::SUCCESS, response);
// The DLP verdict means an event should be reported. The content size is
// equal to the length of the concatenated texts ("text1" and "text2") times 2
// since they are wide characters ((5 + 5) * 2 = 20).
EventReportValidator validator(client());
validator.ExpectSensitiveDataEvent(
/*url*/ "about:blank",
/*filename*/ "Text data",
// The hash should not be included for string requests.
/*sha*/ "",
/*trigger*/ SafeBrowsingPrivateEventRouter::kTriggerWebContentUpload,
/*dlp_verdict*/ response.dlp_scan_verdict(),
/*mimetype*/ TextMimeTypes(),
/*size*/ 20);
bool called = false;
base::RunLoop run_loop;
SetQuitClosure(run_loop.QuitClosure());
DeepScanningDialogDelegate::Data data;
data.do_dlp_scan = true;
data.do_malware_scan = true;
data.text.emplace_back(base::UTF8ToUTF16("text1"));
data.text.emplace_back(base::UTF8ToUTF16("text2"));
ASSERT_TRUE(DeepScanningDialogDelegate::IsEnabled(
browser()->profile(), GURL(kTestUrl), &data,
enterprise_connectors::AnalysisConnector::BULK_DATA_ENTRY));
// Start test.
DeepScanningDialogDelegate::ShowForWebContents(
browser()->tab_strip_model()->GetActiveWebContents(), std::move(data),
base::BindLambdaForTesting(
[&called](const DeepScanningDialogDelegate::Data& data,
const DeepScanningDialogDelegate::Result& result) {
ASSERT_TRUE(result.paths_results.empty());
ASSERT_EQ(result.text_results.size(), 2u);
ASSERT_FALSE(result.text_results[0]);
ASSERT_FALSE(result.text_results[1]);
called = true;
}),
DeepScanAccessPoint::UPLOAD);
FakeBinaryUploadServiceStorage()->ReturnAuthorizedResponse();
run_loop.Run();
EXPECT_TRUE(called);
// There should have been 1 request for all texts and 1 for authentication.
ASSERT_EQ(FakeBinaryUploadServiceStorage()->requests_count(), 2);
}
INSTANTIATE_TEST_SUITE_P(,
DeepScanningDialogDelegateSimpleBrowserTest,
testing::Bool());
class DeepScanningDialogDelegatePasswordProtectedFilesBrowserTest
: public DeepScanningDialogDelegateBrowserTest,
public testing::WithParamInterface<AllowPasswordProtectedFilesValues> {
public testing::WithParamInterface<
std::tuple<AllowPasswordProtectedFilesValues, bool>> {
public:
using DeepScanningDialogDelegateBrowserTest::
DeepScanningDialogDelegateBrowserTest;
DeepScanningDialogDelegatePasswordProtectedFilesBrowserTest()
: DeepScanningDialogDelegateBrowserTest(std::get<1>(GetParam())) {}
AllowPasswordProtectedFilesValues allow_password_protected_files() const {
return GetParam();
return std::get<0>(GetParam());
}
bool expected_result() const {
......@@ -438,26 +546,28 @@ IN_PROC_BROWSER_TEST_P(
}
INSTANTIATE_TEST_SUITE_P(
,
DeepScanningDialogDelegatePasswordProtectedFilesBrowserTest,
DeepScanningDialogDelegatePasswordProtectedFilesBrowserTest,
testing::Values(ALLOW_NONE,
ALLOW_DOWNLOADS,
ALLOW_UPLOADS,
ALLOW_UPLOADS_AND_DOWNLOADS));
testing::Combine(testing::Values(ALLOW_NONE,
ALLOW_DOWNLOADS,
ALLOW_UPLOADS,
ALLOW_UPLOADS_AND_DOWNLOADS),
testing::Bool()));
class DeepScanningDialogDelegateBlockUnsupportedFileTypesBrowserTest
: public DeepScanningDialogDelegateBrowserTest,
public testing::WithParamInterface<BlockUnsupportedFiletypesValues> {
public testing::WithParamInterface<
std::tuple<BlockUnsupportedFiletypesValues, bool>> {
public:
using DeepScanningDialogDelegateBrowserTest::
DeepScanningDialogDelegateBrowserTest;
DeepScanningDialogDelegateBlockUnsupportedFileTypesBrowserTest()
: DeepScanningDialogDelegateBrowserTest(std::get<1>(GetParam())) {}
BlockUnsupportedFiletypesValues block_unsupported_file_types() const {
return GetParam();
return std::get<0>(GetParam());
}
bool expected_result() const {
switch (GetParam()) {
switch (block_unsupported_file_types()) {
case BLOCK_UNSUPPORTED_FILETYPES_NONE:
case BLOCK_UNSUPPORTED_FILETYPES_DOWNLOADS:
return true;
......@@ -476,9 +586,7 @@ IN_PROC_BROWSER_TEST_P(
// Set up delegate and upload service.
EnableUploadsScanningAndReporting();
SetBlockUnsupportedFileTypesPolicy(block_unsupported_file_types());
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckForMalwareOfUploadedContent)
->Clear();
ClearUrlsToCheckForMalwareOfUploads();
DeepScanningDialogDelegate::SetFactoryForTesting(
base::BindRepeating(&MinimalFakeDeepScanningDialogDelegate::Create));
......@@ -535,22 +643,25 @@ IN_PROC_BROWSER_TEST_P(
}
INSTANTIATE_TEST_SUITE_P(
,
DeepScanningDialogDelegateBlockUnsupportedFileTypesBrowserTest,
DeepScanningDialogDelegateBlockUnsupportedFileTypesBrowserTest,
testing::Values(BLOCK_UNSUPPORTED_FILETYPES_NONE,
BLOCK_UNSUPPORTED_FILETYPES_DOWNLOADS,
BLOCK_UNSUPPORTED_FILETYPES_UPLOADS,
BLOCK_UNSUPPORTED_FILETYPES_UPLOADS_AND_DOWNLOADS));
testing::Combine(
testing::Values(BLOCK_UNSUPPORTED_FILETYPES_NONE,
BLOCK_UNSUPPORTED_FILETYPES_DOWNLOADS,
BLOCK_UNSUPPORTED_FILETYPES_UPLOADS,
BLOCK_UNSUPPORTED_FILETYPES_UPLOADS_AND_DOWNLOADS),
testing::Bool()));
class DeepScanningDialogDelegateBlockLargeFileTransferBrowserTest
: public DeepScanningDialogDelegateBrowserTest,
public testing::WithParamInterface<BlockLargeFileTransferValues> {
public testing::WithParamInterface<
std::tuple<BlockLargeFileTransferValues, bool>> {
public:
using DeepScanningDialogDelegateBrowserTest::
DeepScanningDialogDelegateBrowserTest;
DeepScanningDialogDelegateBlockLargeFileTransferBrowserTest()
: DeepScanningDialogDelegateBrowserTest(std::get<1>(GetParam())) {}
BlockLargeFileTransferValues block_large_file_transfer() const {
return GetParam();
return std::get<0>(GetParam());
}
bool expected_result() const {
......@@ -633,22 +744,24 @@ IN_PROC_BROWSER_TEST_P(
}
INSTANTIATE_TEST_SUITE_P(
,
DeepScanningDialogDelegateBlockLargeFileTransferBrowserTest,
DeepScanningDialogDelegateBlockLargeFileTransferBrowserTest,
testing::Values(BLOCK_NONE,
BLOCK_LARGE_DOWNLOADS,
BLOCK_LARGE_UPLOADS,
BLOCK_LARGE_UPLOADS_AND_DOWNLOADS));
testing::Combine(testing::Values(BLOCK_NONE,
BLOCK_LARGE_DOWNLOADS,
BLOCK_LARGE_UPLOADS,
BLOCK_LARGE_UPLOADS_AND_DOWNLOADS),
testing::Bool()));
class DeepScanningDialogDelegateDelayDeliveryUntilVerdictTest
: public DeepScanningDialogDelegateBrowserTest,
public testing::WithParamInterface<DelayDeliveryUntilVerdictValues> {
public testing::WithParamInterface<
std::tuple<DelayDeliveryUntilVerdictValues, bool>> {
public:
using DeepScanningDialogDelegateBrowserTest::
DeepScanningDialogDelegateBrowserTest;
DeepScanningDialogDelegateDelayDeliveryUntilVerdictTest()
: DeepScanningDialogDelegateBrowserTest(std::get<1>(GetParam())) {}
DelayDeliveryUntilVerdictValues delay_delivery_until_verdict() const {
return GetParam();
return std::get<0>(GetParam());
}
bool expected_result() const {
......@@ -745,106 +858,12 @@ IN_PROC_BROWSER_TEST_P(DeepScanningDialogDelegateDelayDeliveryUntilVerdictTest,
}
INSTANTIATE_TEST_SUITE_P(
,
DeepScanningDialogDelegateDelayDeliveryUntilVerdictTest,
DeepScanningDialogDelegateDelayDeliveryUntilVerdictTest,
testing::Values(DELAY_NONE,
DELAY_DOWNLOADS,
DELAY_UPLOADS,
DELAY_UPLOADS_AND_DOWNLOADS));
IN_PROC_BROWSER_TEST_F(DeepScanningDialogDelegateBrowserTest, Texts) {
// Set up delegate and upload service.
EnableUploadsScanningAndReporting();
DeepScanningDialogDelegate::SetFactoryForTesting(
base::BindRepeating(&MinimalFakeDeepScanningDialogDelegate::Create));
FakeBinaryUploadServiceStorage()->SetAuthorized(true);
// Prepare a complex DLP response to test that the verdict is reported
// correctly in the sensitive data event.
DeepScanningClientResponse response;
DlpDeepScanningVerdict* verdict = response.mutable_dlp_scan_verdict();
verdict->set_status(DlpDeepScanningVerdict::SUCCESS);
DlpDeepScanningVerdict::TriggeredRule* rule1 = verdict->add_triggered_rules();
rule1->set_rule_id(1);
rule1->set_action(DlpDeepScanningVerdict::TriggeredRule::REPORT_ONLY);
rule1->set_rule_resource_name("resource name 1");
rule1->set_rule_severity("severity 1");
DlpDeepScanningVerdict::MatchedDetector* detector1 =
rule1->add_matched_detectors();
detector1->set_detector_id("id1");
detector1->set_detector_type("dlp1");
detector1->set_display_name("display name 1");
DlpDeepScanningVerdict::TriggeredRule* rule2 = verdict->add_triggered_rules();
rule2->set_rule_id(3);
rule2->set_action(DlpDeepScanningVerdict::TriggeredRule::BLOCK);
rule2->set_rule_resource_name("resource rule 2");
rule2->set_rule_severity("severity 2");
DlpDeepScanningVerdict::MatchedDetector* detector2_1 =
rule2->add_matched_detectors();
detector2_1->set_detector_id("id2.1");
detector2_1->set_detector_type("type2.1");
detector2_1->set_display_name("display name 2.1");
DlpDeepScanningVerdict::MatchedDetector* detector2_2 =
rule2->add_matched_detectors();
detector2_2->set_detector_id("id2.2");
detector2_2->set_detector_type("type2.2");
detector2_2->set_display_name("display name 2.2");
FakeBinaryUploadServiceStorage()->SetResponseForText(
BinaryUploadService::Result::SUCCESS, response);
// The DLP verdict means an event should be reported. The content size is
// equal to the length of the concatenated texts ("text1" and "text2") times 2
// since they are wide characters ((5 + 5) * 2 = 20).
EventReportValidator validator(client());
validator.ExpectSensitiveDataEvent(
/*url*/ "about:blank",
/*filename*/ "Text data",
// The hash should not be included for string requests.
/*sha*/ "",
/*trigger*/ SafeBrowsingPrivateEventRouter::kTriggerWebContentUpload,
/*dlp_verdict*/ response.dlp_scan_verdict(),
/*mimetype*/ TextMimeTypes(),
/*size*/ 20);
bool called = false;
base::RunLoop run_loop;
SetQuitClosure(run_loop.QuitClosure());
DeepScanningDialogDelegate::Data data;
data.do_dlp_scan = true;
data.do_malware_scan = true;
data.text.emplace_back(base::UTF8ToUTF16("text1"));
data.text.emplace_back(base::UTF8ToUTF16("text2"));
ASSERT_TRUE(DeepScanningDialogDelegate::IsEnabled(
browser()->profile(), GURL(kTestUrl), &data,
enterprise_connectors::AnalysisConnector::BULK_DATA_ENTRY));
// Start test.
DeepScanningDialogDelegate::ShowForWebContents(
browser()->tab_strip_model()->GetActiveWebContents(), std::move(data),
base::BindLambdaForTesting(
[&called](const DeepScanningDialogDelegate::Data& data,
const DeepScanningDialogDelegate::Result& result) {
ASSERT_TRUE(result.paths_results.empty());
ASSERT_EQ(result.text_results.size(), 2u);
ASSERT_FALSE(result.text_results[0]);
ASSERT_FALSE(result.text_results[1]);
called = true;
}),
DeepScanAccessPoint::UPLOAD);
FakeBinaryUploadServiceStorage()->ReturnAuthorizedResponse();
run_loop.Run();
EXPECT_TRUE(called);
// There should have been 1 request for all texts and 1 for authentication.
ASSERT_EQ(FakeBinaryUploadServiceStorage()->requests_count(), 2);
}
testing::Combine(testing::Values(DELAY_NONE,
DELAY_DOWNLOADS,
DELAY_UPLOADS,
DELAY_UPLOADS_AND_DOWNLOADS),
testing::Bool()));
} // namespace safe_browsing
......@@ -27,10 +27,8 @@ constexpr base::TimeDelta kNoDelay = base::TimeDelta::FromSeconds(0);
constexpr base::TimeDelta kSmallDelay = base::TimeDelta::FromMilliseconds(300);
constexpr base::TimeDelta kNormalDelay = base::TimeDelta::FromMilliseconds(500);
enum class ScanType { ONLY_DLP, ONLY_MALWARE, DLP_AND_MALWARE };
// Tests the behavior of the dialog in the following ways:
// - It shows the appropriate buttons depending on it's state.
// - It shows the appropriate buttons depending on its state.
// - It transitions from states in the correct order.
// - It respects time constraints (minimum shown time, initial delay, timeout)
// - It is always destroyed, therefore |quit_closure_| is called in the dtor
......@@ -39,14 +37,13 @@ class DeepScanningDialogViewsBehaviorBrowserTest
: public DeepScanningBrowserTestBase,
public DeepScanningDialogViews::TestObserver,
public testing::WithParamInterface<
std::tuple<ScanType, bool, bool, base::TimeDelta>> {
std::tuple<bool, bool, bool, base::TimeDelta>> {
public:
DeepScanningDialogViewsBehaviorBrowserTest() {
DeepScanningDialogViewsBehaviorBrowserTest()
: DeepScanningBrowserTestBase(std::get<0>(GetParam())) {
DeepScanningDialogViews::SetObserverForTesting(this);
bool dlp_result = !dlp_enabled() || dlp_success();
bool malware_result = !malware_enabled() || malware_success();
expected_scan_result_ = dlp_result && malware_result;
expected_scan_result_ = dlp_success() && malware_success();
}
void ConstructorCalled(DeepScanningDialogViews* views,
......@@ -158,14 +155,6 @@ class DeepScanningDialogViewsBehaviorBrowserTest
CallQuitClosure();
}
bool dlp_enabled() const {
return std::get<0>(GetParam()) != ScanType::ONLY_MALWARE;
}
bool malware_enabled() const {
return std::get<0>(GetParam()) != ScanType::ONLY_DLP;
}
bool dlp_success() const { return std::get<1>(GetParam()); }
bool malware_success() const { return std::get<2>(GetParam()); }
......@@ -194,9 +183,11 @@ class DeepScanningDialogViewsBehaviorBrowserTest
// - The "CancelledByUser" metrics are recorded.
class DeepScanningDialogViewsCancelPendingScanBrowserTest
: public DeepScanningBrowserTestBase,
public testing::WithParamInterface<bool>,
public DeepScanningDialogViews::TestObserver {
public:
DeepScanningDialogViewsCancelPendingScanBrowserTest() {
DeepScanningDialogViewsCancelPendingScanBrowserTest()
: DeepScanningBrowserTestBase(GetParam()) {
DeepScanningDialogViews::SetObserverForTesting(this);
}
......@@ -238,9 +229,11 @@ class DeepScanningDialogViewsCancelPendingScanBrowserTest
class DeepScanningDialogViewsWarningBrowserTest
: public DeepScanningBrowserTestBase,
public DeepScanningDialogViews::TestObserver,
public testing::WithParamInterface<std::tuple<base::TimeDelta, bool>> {
public testing::WithParamInterface<
std::tuple<base::TimeDelta, bool, bool>> {
public:
DeepScanningDialogViewsWarningBrowserTest() {
DeepScanningDialogViewsWarningBrowserTest()
: DeepScanningBrowserTestBase(std::get<2>(GetParam())) {
DeepScanningDialogViews::SetObserverForTesting(this);
}
......@@ -304,9 +297,10 @@ class DeepScanningDialogViewsAppearanceBrowserTest
: public DeepScanningBrowserTestBase,
public DeepScanningDialogViews::TestObserver,
public testing::WithParamInterface<
std::tuple<bool, bool, DeepScanAccessPoint>> {
std::tuple<bool, bool, DeepScanAccessPoint, bool>> {
public:
DeepScanningDialogViewsAppearanceBrowserTest() {
DeepScanningDialogViewsAppearanceBrowserTest()
: DeepScanningBrowserTestBase(std::get<3>(GetParam())) {
DeepScanningDialogViews::SetObserverForTesting(this);
}
......@@ -411,28 +405,13 @@ constexpr char kTestUrl[] = "https://google.com";
IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsBehaviorBrowserTest, Test) {
base::ScopedAllowBlockingForTesting allow_blocking;
// The test is wrong if neither DLP or Malware is enabled. This would imply a
// Deep Scanning call site called ShowForWebContents without first checking
// IsEnabled returns true.
EXPECT_TRUE(dlp_enabled() || malware_enabled());
// Setup policies to enable deep scanning, its UI and the responses to be
// simulated.
base::Optional<bool> dlp = base::nullopt;
base::Optional<bool> malware = base::nullopt;
if (dlp_enabled()) {
dlp = dlp_success();
SetDlpPolicy(CHECK_UPLOADS_AND_DOWNLOADS);
}
if (malware_enabled()) {
malware = malware_success();
SetMalwarePolicy(SEND_UPLOADS_AND_DOWNLOADS);
ListPrefUpdate(g_browser_process->local_state(),
prefs::kURLsToCheckForMalwareOfUploadedContent)
->Append("*");
}
SetStatusCallbackResponse(
SimpleDeepScanningClientResponseForTesting(dlp, malware));
SetDlpPolicy(CHECK_UPLOADS_AND_DOWNLOADS);
SetMalwarePolicy(SEND_UPLOADS_AND_DOWNLOADS);
AddUrlToCheckForMalwareOfUploads("*");
SetStatusCallbackResponse(SimpleDeepScanningClientResponseForTesting(
dlp_success(), malware_success()));
// Always set this policy so the UI is shown.
SetWaitPolicy(DELAY_UPLOADS);
......@@ -446,8 +425,8 @@ IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsBehaviorBrowserTest, Test) {
SetQuitClosure(run_loop.QuitClosure());
DeepScanningDialogDelegate::Data data;
data.do_dlp_scan = dlp_enabled();
data.do_malware_scan = malware_enabled();
data.do_dlp_scan = true;
data.do_malware_scan = true;
CreateFilesForTest({"foo.doc"}, {"content"}, &data);
ASSERT_TRUE(DeepScanningDialogDelegate::IsEnabled(
browser()->profile(), GURL(kTestUrl), &data,
......@@ -483,18 +462,16 @@ IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsBehaviorBrowserTest, Test) {
// more than its minimum duration (GetMinimumPendingDialogTime <
// kNormalDelay).
INSTANTIATE_TEST_SUITE_P(
DeepScanningDialogViewsBehaviorBrowserTest,
,
DeepScanningDialogViewsBehaviorBrowserTest,
testing::Combine(
/*scan_type*/ testing::Values(ScanType::ONLY_DLP,
ScanType::ONLY_MALWARE,
ScanType::DLP_AND_MALWARE),
/*use_legacy_policies*/ testing::Bool(),
/*dlp_success*/ testing::Bool(),
/*malware_success*/ testing::Bool(),
/*response_delay*/
testing::Values(kNoDelay, kSmallDelay, kNormalDelay)));
IN_PROC_BROWSER_TEST_F(DeepScanningDialogViewsCancelPendingScanBrowserTest,
IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsCancelPendingScanBrowserTest,
Test) {
base::ScopedAllowBlockingForTesting allow_blocking;
......@@ -542,6 +519,10 @@ IN_PROC_BROWSER_TEST_F(DeepScanningDialogViewsCancelPendingScanBrowserTest,
ValidateMetrics();
}
INSTANTIATE_TEST_SUITE_P(,
DeepScanningDialogViewsCancelPendingScanBrowserTest,
/*use_legacy_policies=*/testing::Bool());
IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsWarningBrowserTest, Test) {
base::ScopedAllowBlockingForTesting allow_blocking;
......@@ -596,10 +577,12 @@ IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsWarningBrowserTest, Test) {
EXPECT_TRUE(called);
}
INSTANTIATE_TEST_SUITE_P(
DeepScanningDialogViewsWarningBrowserTest,
DeepScanningDialogViewsWarningBrowserTest,
testing::Combine(testing::Values(kNoDelay, kSmallDelay), testing::Bool()));
INSTANTIATE_TEST_SUITE_P(,
DeepScanningDialogViewsWarningBrowserTest,
testing::Combine(
/*delay=*/testing::Values(kNoDelay, kSmallDelay),
/*user_bypasses_warning=*/testing::Bool(),
/*use_legacy_policies=*/testing::Bool()));
IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsAppearanceBrowserTest, Test) {
base::ScopedAllowBlockingForTesting allow_blocking;
......@@ -651,7 +634,7 @@ IN_PROC_BROWSER_TEST_P(DeepScanningDialogViewsAppearanceBrowserTest, Test) {
EXPECT_TRUE(called);
}
INSTANTIATE_TEST_SUITE_P(DeepScanningDialogViewsAppearanceBrowserTest,
INSTANTIATE_TEST_SUITE_P(,
DeepScanningDialogViewsAppearanceBrowserTest,
testing::Combine(
/*file_scan=*/testing::Bool(),
......@@ -659,6 +642,7 @@ INSTANTIATE_TEST_SUITE_P(DeepScanningDialogViewsAppearanceBrowserTest,
/*access_point=*/
testing::Values(DeepScanAccessPoint::UPLOAD,
DeepScanAccessPoint::DRAG_AND_DROP,
DeepScanAccessPoint::PASTE)));
DeepScanAccessPoint::PASTE),
/*use_legacy_policies=*/testing::Bool()));
} // namespace safe_browsing
......@@ -31,6 +31,8 @@ base::Value MakeListValue(const std::vector<std::string>& elements) {
base::Value DefaultConnectorSettings() {
base::Value settings(base::Value::Type::DICTIONARY);
settings.SetKey(enterprise_connectors::kKeyServiceProvider,
base::Value("google"));
settings.SetKey(enterprise_connectors::kKeyEnable,
base::Value(base::Value::Type::LIST));
settings.SetKey(enterprise_connectors::kKeyDisable,
......@@ -73,9 +75,47 @@ void AddConnectorUrlPattern(enterprise_connectors::AnalysisConnector connector,
list->Append(std::move(list_element));
}
void ClearConnectorUrlPattern(
enterprise_connectors::AnalysisConnector connector,
bool enable,
base::Value tags) {
ListPrefUpdate settings_list(g_browser_process->local_state(),
ConnectorPref(connector));
DCHECK(settings_list.Get());
if (settings_list->empty())
return;
base::Value* settings = nullptr;
DCHECK(settings_list->Get(0, &settings));
DCHECK(settings);
DCHECK(settings->is_dict());
base::Value* list =
settings->FindListPath(enable ? enterprise_connectors::kKeyEnable
: enterprise_connectors::kKeyDisable);
if (!list)
return;
DCHECK(list->is_list());
for (const base::Value& pattern : list->GetList()) {
DCHECK(pattern.is_dict());
const base::Value* pattern_tags =
pattern.FindKey(enterprise_connectors::kKeyTags);
if (!pattern_tags)
continue;
DCHECK(pattern_tags->is_list());
if (*pattern_tags == tags) {
list->EraseListValue(pattern);
}
}
}
template <typename T>
void SetConnectorField(enterprise_connectors::AnalysisConnector connector,
const char* key,
bool value) {
T value) {
InitConnectorPrefIfEmpty(connector);
ListPrefUpdate settings_list(g_browser_process->local_state(),
ConnectorPref(connector));
......@@ -83,7 +123,7 @@ void SetConnectorField(enterprise_connectors::AnalysisConnector connector,
DCHECK(settings_list->Get(0, &settings));
DCHECK(settings);
DCHECK(settings->is_dict());
settings->SetKey(key, base::Value(std::move(value)));
DCHECK(settings->SetKey(key, base::Value(std::move(value))));
}
} // namespace
......@@ -353,7 +393,6 @@ void SetDlpPolicyForConnectors(CheckContentComplianceValues state) {
// wildcard pattern should also be included in the disable list if the policy
// is disabled for downloads since no scan can occur with the legacy policy
// when it is disabled.
bool enable_uploads =
state == CHECK_UPLOADS || state == CHECK_UPLOADS_AND_DOWNLOADS;
......@@ -405,10 +444,11 @@ void SetMalwarePolicyForConnectors(SendFilesForMalwareCheckValues state) {
void SetDelayDeliveryUntilVerdictPolicyForConnectors(
DelayDeliveryUntilVerdictValues state) {
bool delay_uploads =
state == DELAY_UPLOADS || state == DELAY_UPLOADS_AND_DOWNLOADS;
bool delay_downloads =
state == DELAY_DOWNLOADS || state == DELAY_UPLOADS_AND_DOWNLOADS;
int delay_uploads =
(state == DELAY_UPLOADS || state == DELAY_UPLOADS_AND_DOWNLOADS) ? 1 : 0;
int delay_downloads =
(state == DELAY_DOWNLOADS || state == DELAY_UPLOADS_AND_DOWNLOADS) ? 1
: 0;
SetConnectorField(enterprise_connectors::AnalysisConnector::BULK_DATA_ENTRY,
enterprise_connectors::kKeyBlockUntilVerdict,
delay_uploads);
......@@ -428,10 +468,10 @@ void SetAllowPasswordProtectedFilesPolicyForConnectors(
state == ALLOW_DOWNLOADS || state == ALLOW_UPLOADS_AND_DOWNLOADS;
SetConnectorField(enterprise_connectors::AnalysisConnector::FILE_ATTACHED,
enterprise_connectors::kKeyBlockPasswordProtected,
allow_uploads);
!allow_uploads);
SetConnectorField(enterprise_connectors::AnalysisConnector::FILE_DOWNLOADED,
enterprise_connectors::kKeyBlockPasswordProtected,
allow_downloads);
!allow_downloads);
}
void SetBlockUnsupportedFileTypesPolicyForConnectors(
......@@ -497,4 +537,18 @@ void AddUrlsToNotCheckForMalwareOfDownloadsForConnectors(
MakeListValue(urls), MakeListValue({"malware"}));
}
void ClearUrlsToCheckComplianceOfDownloadsForConnectors() {
ClearConnectorUrlPattern(
enterprise_connectors::AnalysisConnector::FILE_DOWNLOADED, true,
MakeListValue({"dlp"}));
}
void ClearUrlsToCheckForMalwareOfUploadsForConnectors() {
for (auto connector :
{enterprise_connectors::AnalysisConnector::FILE_ATTACHED,
enterprise_connectors::AnalysisConnector::BULK_DATA_ENTRY}) {
ClearConnectorUrlPattern(connector, true, MakeListValue({"malware"}));
}
}
} // namespace safe_browsing
......@@ -126,6 +126,8 @@ void AddUrlsToCheckForMalwareOfUploadsForConnectors(
const std::vector<std::string>& urls);
void AddUrlsToNotCheckForMalwareOfDownloadsForConnectors(
const std::vector<std::string>& urls);
void ClearUrlsToCheckComplianceOfDownloadsForConnectors();
void ClearUrlsToCheckForMalwareOfUploadsForConnectors();
} // namespace safe_browsing
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment