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
|
// Copyright 2023 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/safe_browsing/chrome_ping_manager_factory.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "chrome/browser/safe_browsing/chrome_user_population_helper.h"
#include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.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/safe_browsing/core/browser/ping_manager.h"
#include "components/safe_browsing/core/common/proto/csd.pb.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "content/public/test/browser_task_environment.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using network::GetUploadData;
namespace safe_browsing {
class ChromePingManagerFactoryTest : public testing::Test {
protected:
void SetUp() override;
void TearDown() override;
void RunReportThreatDetailsTest();
void RunShouldFetchAccessTokenForReportTest(bool is_enhanced_protection,
bool is_signed_in,
bool expect_should_fetch);
TestingProfile* SetUpProfile(bool is_enhanced_protection, bool is_signed_in);
bool ShouldSendPersistedReport(Profile* profile);
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfileManager> profile_manager_;
private:
scoped_refptr<safe_browsing::SafeBrowsingService> sb_service_;
ChromePingManagerAllowerForTesting allow_ping_manager_;
};
void ChromePingManagerFactoryTest::SetUp() {
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
ASSERT_TRUE(profile_manager_->SetUp());
ASSERT_TRUE(g_browser_process->profile_manager());
sb_service_ = base::MakeRefCounted<safe_browsing::TestSafeBrowsingService>();
TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(sb_service_.get());
g_browser_process->safe_browsing_service()->Initialize();
}
void ChromePingManagerFactoryTest::TearDown() {
base::RunLoop().RunUntilIdle();
if (TestingBrowserProcess::GetGlobal()->safe_browsing_service()) {
TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown();
TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr);
}
}
TestingProfile* ChromePingManagerFactoryTest::SetUpProfile(
bool is_enhanced_protection,
bool is_signed_in) {
TestingProfile* profile = profile_manager_->CreateTestingProfile(
"testing_profile", IdentityTestEnvironmentProfileAdaptor::
GetIdentityTestEnvironmentFactories());
if (is_enhanced_protection) {
SetSafeBrowsingState(profile->GetPrefs(),
SafeBrowsingState::ENHANCED_PROTECTION);
}
if (is_signed_in) {
IdentityTestEnvironmentProfileAdaptor adaptor(profile);
adaptor.identity_test_env()->MakePrimaryAccountAvailable(
"testing@gmail.com", signin::ConsentLevel::kSync);
}
return profile;
}
void ChromePingManagerFactoryTest::RunShouldFetchAccessTokenForReportTest(
bool is_enhanced_protection,
bool is_signed_in,
bool expect_should_fetch) {
TestingProfile* profile = SetUpProfile(is_enhanced_protection, is_signed_in);
EXPECT_EQ(ChromePingManagerFactory::ShouldFetchAccessTokenForReport(profile),
expect_should_fetch);
}
void ChromePingManagerFactoryTest::RunReportThreatDetailsTest() {
TestingProfile* profile =
SetUpProfile(/*is_enhanced_protection=*/false, /*is_signed_in=*/false);
auto* ping_manager = ChromePingManagerFactory::GetForBrowserContext(profile);
std::string input_report_content;
std::unique_ptr<ClientSafeBrowsingReportRequest> report =
std::make_unique<ClientSafeBrowsingReportRequest>();
// The report must be non-empty. The selected property to set is arbitrary.
report->set_type(ClientSafeBrowsingReportRequest::URL_PHISHING);
EXPECT_TRUE(report->SerializeToString(&input_report_content));
ClientSafeBrowsingReportRequest expected_report;
expected_report.ParseFromString(input_report_content);
*expected_report.mutable_population() =
safe_browsing::GetUserPopulationForProfile(profile);
ChromeUserPopulation::PageLoadToken token =
safe_browsing::GetPageLoadTokenForURL(profile, GURL(""));
expected_report.mutable_population()->mutable_page_load_tokens()->Add()->Swap(
&token);
std::string expected_report_content;
EXPECT_TRUE(expected_report.SerializeToString(&expected_report_content));
network::TestURLLoaderFactory test_url_loader_factory;
test_url_loader_factory.SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
EXPECT_EQ(GetUploadData(request), expected_report_content);
}));
ping_manager->SetURLLoaderFactoryForTesting(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory));
EXPECT_EQ(ping_manager->ReportThreatDetails(std::move(report)),
PingManager::ReportThreatDetailsResult::SUCCESS);
}
bool ChromePingManagerFactoryTest::ShouldSendPersistedReport(Profile* profile) {
return ChromePingManagerFactory::ShouldSendPersistedReport(profile);
}
TEST_F(ChromePingManagerFactoryTest, ReportThreatDetails) {
RunReportThreatDetailsTest();
}
TEST_F(ChromePingManagerFactoryTest, ShouldFetchAccessTokenForReport_Yes) {
RunShouldFetchAccessTokenForReportTest(/*is_enhanced_protection=*/true,
/*is_signed_in=*/true,
/*expect_should_fetch=*/true);
}
TEST_F(ChromePingManagerFactoryTest,
ShouldFetchAccessTokenForReport_NotSignedIn) {
RunShouldFetchAccessTokenForReportTest(/*is_enhanced_protection=*/true,
/*is_signed_in=*/false,
/*expect_should_fetch=*/false);
}
TEST_F(ChromePingManagerFactoryTest,
ShouldFetchAccessTokenForReport_NotEnhancedProtection) {
RunShouldFetchAccessTokenForReportTest(/*is_enhanced_protection=*/false,
/*is_signed_in=*/true,
/*expect_should_fetch=*/false);
}
TEST_F(ChromePingManagerFactoryTest, ShouldSendPersistedReport_Yes) {
TestingProfile* profile =
SetUpProfile(/*is_enhanced_protection=*/true, /*is_signed_in=*/false);
EXPECT_EQ(ShouldSendPersistedReport(profile), true);
}
TEST_F(ChromePingManagerFactoryTest,
ShouldSendPersistedReport_NotEnhancedProtection) {
TestingProfile* profile =
SetUpProfile(/*is_enhanced_protection=*/false, /*is_signed_in=*/false);
EXPECT_EQ(ShouldSendPersistedReport(profile), false);
}
TEST_F(ChromePingManagerFactoryTest, ShouldSendPersistedReport_Incognito) {
TestingProfile* profile =
SetUpProfile(/*is_enhanced_protection=*/true, /*is_signed_in=*/false);
EXPECT_EQ(ShouldSendPersistedReport(
TestingProfile::Builder().BuildIncognito(profile)),
false);
}
TEST_F(ChromePingManagerFactoryTest, NoPingManagerForIncognito) {
TestingProfile* profile = TestingProfile::Builder().BuildIncognito(
profile_manager_->CreateTestingProfile("testing_profile"));
EXPECT_EQ(ChromePingManagerFactory::GetForBrowserContext(profile), nullptr);
}
} // namespace safe_browsing
|