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
|
// Copyright 2025 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/enterprise/signals/profile_signals_collector.h"
#include <array>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/identifiers/profile_id_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/device_signals/core/browser/signals_types.h"
#include "components/device_signals/core/browser/user_permission_service.h"
#include "components/device_signals/core/common/signals_constants.h"
#include "components/enterprise/browser/identifiers/profile_id_service.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
#include "components/policy/core/common/cloud/cloud_policy_util.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kFakeUserEnrollmentDomain[] = "fake.domain.google.com";
constexpr char kFakeProfileId[] = "SomeProfileId";
std::unique_ptr<KeyedService> CreateProfileIdService(
content::BrowserContext* context) {
return std::make_unique<enterprise::ProfileIdService>(kFakeProfileId);
}
} // namespace
namespace device_signals {
class ProfileSignalsCollectorTest : public InProcessBrowserTest {
protected:
void SetUpBrowserContextKeyedServices(
content::BrowserContext* context) override {
enterprise::ProfileIdServiceFactory::GetInstance()->SetTestingFactory(
context, base::BindRepeating(&CreateProfileIdService));
InProcessBrowserTest::SetUpBrowserContextKeyedServices(context);
}
std::unique_ptr<ProfileSignalsCollector> CreateProfileSignalsCollector() {
return std::make_unique<ProfileSignalsCollector>(browser()->profile());
}
void SetFakePolicyAndPrefData() {
auto policy_data = std::make_unique<enterprise_management::PolicyData>();
policy_data->set_managed_by(kFakeUserEnrollmentDomain);
browser()
->profile()
->GetCloudPolicyManager()
->core()
->store()
->set_policy_data_for_testing(std::move(policy_data));
g_browser_process->local_state()->SetBoolean(
prefs::kBuiltInDnsClientEnabled, true);
// Give the testing profile a safe browsing level of "STANDARD_PROTECTION"
browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled,
true);
browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnhanced,
false);
}
// Helper function to check the profile level signals are collected correctly.
void CheckSignalsCollected(ProfileSignalsResponse& response) {
EXPECT_EQ(response.profile_enrollment_domain, kFakeUserEnrollmentDomain);
EXPECT_EQ(response.safe_browsing_protection_level,
safe_browsing::SafeBrowsingState::STANDARD_PROTECTION);
EXPECT_TRUE(response.built_in_dns_client_enabled);
}
std::unique_ptr<ProfileSignalsCollector> signal_collector_ = nullptr;
};
// Test that runs a sanity check on the set of signals supported by this
// collector. Will need to be updated if new signals become supported.
IN_PROC_BROWSER_TEST_F(ProfileSignalsCollectorTest,
SupportedBrowserContextSignalNames) {
auto signals_collector = CreateProfileSignalsCollector();
const std::array<SignalName, 1> supported_signals{
{SignalName::kBrowserContextSignals}};
const auto names_set = signals_collector->GetSupportedSignalNames();
EXPECT_EQ(names_set.size(), supported_signals.size());
for (const auto& signal_name : supported_signals) {
EXPECT_TRUE(names_set.find(signal_name) != names_set.end());
}
}
// Happy path test case for OS signals collection with full permission.
IN_PROC_BROWSER_TEST_F(ProfileSignalsCollectorTest, GetSignal_Success) {
auto signals_collector = CreateProfileSignalsCollector();
SetFakePolicyAndPrefData();
SignalName signal_name = SignalName::kBrowserContextSignals;
SignalsAggregationRequest empty_request;
SignalsAggregationResponse response;
signals_collector->GetSignal(signal_name, UserPermission::kGranted,
empty_request, response, base::DoNothing());
ASSERT_FALSE(response.top_level_error.has_value());
ASSERT_TRUE(response.profile_signals_response);
CheckSignalsCollected(response.profile_signals_response.value());
}
// Tests that an unsupported signal is marked as unsupported.
IN_PROC_BROWSER_TEST_F(ProfileSignalsCollectorTest,
GetBrowserContextSignal_Unsupported) {
auto signals_collector = CreateProfileSignalsCollector();
SignalName signal_name = SignalName::kAntiVirus;
SignalsAggregationRequest empty_request;
SignalsAggregationResponse response;
signals_collector->GetSignal(signal_name, UserPermission::kGranted,
empty_request, response, base::DoNothing());
ASSERT_TRUE(response.top_level_error.has_value());
EXPECT_EQ(response.top_level_error.value(),
SignalCollectionError::kUnsupported);
}
// Tests that signal collection is halted if permission is not sufficient.
IN_PROC_BROWSER_TEST_F(ProfileSignalsCollectorTest, GetSignal_MissingUser) {
auto signals_collector = CreateProfileSignalsCollector();
SignalName signal_name = SignalName::kBrowserContextSignals;
SignalsAggregationRequest empty_request;
SignalsAggregationResponse response;
signals_collector->GetSignal(signal_name, UserPermission::kMissingUser,
empty_request, response, base::DoNothing());
ASSERT_FALSE(response.top_level_error.has_value());
ASSERT_FALSE(response.profile_signals_response);
}
} // namespace device_signals
|