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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
// 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/component_updater/privacy_sandbox_attestations_component_installer.h"
#include <string>
#include <tuple>
#include <utility>
#include "base/check.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/repeating_test_future.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_path_override.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/version.h"
#include "chrome/browser/component_updater/privacy_sandbox_attestations_component_installer_test_util.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/mock_component_updater_service.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations_histograms.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace component_updater {
class PrivacySandboxAttestationsInstallerTest : public testing::Test {
public:
PrivacySandboxAttestationsInstallerTest() {
CHECK(component_install_dir_.CreateUniqueTempDir());
}
protected:
using Installer = PrivacySandboxAttestationsComponentInstallerPolicy;
base::test::TaskEnvironment env_;
base::ScopedTempDir component_install_dir_;
};
class PrivacySandboxAttestationsInstallerFeatureDisabledTest
: public PrivacySandboxAttestationsInstallerTest {
public:
PrivacySandboxAttestationsInstallerFeatureDisabledTest() {
scoped_feature_list_.InitAndDisableFeature(
privacy_sandbox::kEnforcePrivacySandboxAttestations);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(PrivacySandboxAttestationsInstallerFeatureDisabledTest,
DoNotRegisterIfFeatureDisabled) {
component_updater::MockComponentUpdateService mock_update_service;
EXPECT_CALL(mock_update_service, RegisterComponent(testing::_)).Times(0);
RegisterPrivacySandboxAttestationsComponent(&mock_update_service);
env_.RunUntilIdle();
}
TEST_F(PrivacySandboxAttestationsInstallerFeatureDisabledTest,
DeleteExistingFilesIfFeatureDisabled) {
component_updater::MockComponentUpdateService mock_update_service;
EXPECT_CALL(mock_update_service, RegisterComponent(testing::_)).Times(0);
base::ScopedPathOverride user_data_override(
chrome::DIR_USER_DATA, component_install_dir_.GetPath(), true, true);
base::FilePath user_dir;
ASSERT_TRUE(base::PathService::Get(chrome::DIR_USER_DATA, &user_dir));
// Write an attestations list file to simulate that there is existing file
// from previous runs.
base::FilePath install_dir = Installer::GetInstalledDirectory(user_dir);
ASSERT_TRUE(base::CreateDirectory(install_dir));
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
install_dir, "Attestations list"));
base::FilePath install_path = Installer::GetInstalledFilePath(install_dir);
ASSERT_TRUE(base::PathExists(install_dir));
ASSERT_TRUE(base::PathExists(install_path));
RegisterPrivacySandboxAttestationsComponent(&mock_update_service);
env_.RunUntilIdle();
// Existing Privacy Sandbox Enrollment attestations files should be removed
// if the feature is disabled.
EXPECT_FALSE(base::PathExists(install_dir));
EXPECT_FALSE(base::PathExists(install_path));
}
class PrivacySandboxAttestationsInstallerFeatureEnabledTest
: public PrivacySandboxAttestationsInstallerTest {
};
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest,
VerifyInstallation) {
PrivacySandboxAttestationsComponentInstallerPolicy policy(base::DoNothing());
ASSERT_FALSE(policy.VerifyInstallation(base::Value::Dict(),
component_install_dir_.GetPath()));
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
component_install_dir_.GetPath(), "Attestations list"));
EXPECT_TRUE(policy.VerifyInstallation(base::Value::Dict(),
component_install_dir_.GetPath()));
}
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest, OnCustomInstall) {
PrivacySandboxAttestationsComponentInstallerPolicy policy(base::DoNothing());
EXPECT_EQ(
policy.OnCustomInstall(base::Value::Dict(), base::FilePath()).result.code,
0);
}
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest,
RegisterIfFeatureEnabled) {
component_updater::MockComponentUpdateService mock_update_service;
EXPECT_CALL(mock_update_service, RegisterComponent(testing::_));
RegisterPrivacySandboxAttestationsComponent(&mock_update_service);
env_.RunUntilIdle();
}
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest,
InvokeOnAttestationsReadyCallbackOnComponentReady) {
base::test::RepeatingTestFuture<base::Version, base::FilePath, bool> future;
PrivacySandboxAttestationsComponentInstallerPolicy policy(
future.GetCallback());
const base::Version version = base::Version("0.0.1");
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
component_install_dir_.GetPath(), "Attestations list"));
policy.ComponentReadyForTesting(version, component_install_dir_.GetPath(),
base::Value::Dict());
auto [loaded_version, loaded_path, is_pre_installed] = future.Take();
EXPECT_TRUE(loaded_version.IsValid());
EXPECT_EQ(loaded_version, version);
EXPECT_FALSE(is_pre_installed);
EXPECT_EQ(loaded_path,
Installer::GetInstalledFilePath(component_install_dir_.GetPath()));
}
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest,
DoNotInvokeOnAttestationsReadyCallbackIfInvalidVersion) {
base::test::RepeatingTestFuture<base::Version, base::FilePath, bool> future;
PrivacySandboxAttestationsComponentInstallerPolicy policy(
future.GetCallback());
// First call with an invalid version.
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
component_install_dir_.GetPath(), "Attestations list"));
policy.ComponentReadyForTesting(
base::Version(), component_install_dir_.GetPath(), base::Value::Dict());
// Second call with a valid version.
policy.ComponentReadyForTesting(base::Version("0.0.1"),
component_install_dir_.GetPath(),
base::Value::Dict());
// Only the second call succeeded.
auto [loaded_version, loaded_path, is_pre_installed] = future.Take();
EXPECT_TRUE(loaded_version.IsValid());
EXPECT_EQ(loaded_version, base::Version("0.0.1"));
EXPECT_FALSE(is_pre_installed);
EXPECT_EQ(loaded_path,
Installer::GetInstalledFilePath(component_install_dir_.GetPath()));
}
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest,
DoNotInvokeOnAttestationsReadyCallbackIfEmptyPath) {
base::test::RepeatingTestFuture<base::Version, base::FilePath, bool> future;
PrivacySandboxAttestationsComponentInstallerPolicy policy(
future.GetCallback());
// First call with an empty path.
policy.ComponentReadyForTesting(base::Version("0.0.1"), base::FilePath(),
base::Value::Dict());
// Second call with a valid path.
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
component_install_dir_.GetPath(), "Attestations list"));
policy.ComponentReadyForTesting(base::Version("0.0.1"),
component_install_dir_.GetPath(),
base::Value::Dict());
// Only the second call succeeded.
auto [loaded_version, loaded_path, is_pre_installed] = future.Take();
EXPECT_TRUE(loaded_version.IsValid());
EXPECT_EQ(loaded_version, base::Version("0.0.1"));
EXPECT_FALSE(is_pre_installed);
EXPECT_EQ(loaded_path,
Installer::GetInstalledFilePath(component_install_dir_.GetPath()));
}
// Whenever there is an attestations file ready, `ComponentReady()` should
// invoke the stored callback `on_attestations_ready_`, even if this version
// is older than the existing one. The comparison of the passed and existing
// version should be done inside the callback. See
// `PrivacySandboxAttestations::LoadAttestationsInternal()`.
TEST_F(PrivacySandboxAttestationsInstallerFeatureEnabledTest,
CallLoadNewAttestationsFile) {
base::test::RepeatingTestFuture<base::Version, base::FilePath, bool> future;
PrivacySandboxAttestationsComponentInstallerPolicy policy(
future.GetCallback());
// Load the initial version.
base::ScopedTempDir dir_v1;
ASSERT_TRUE(
dir_v1.CreateUniqueTempDirUnderPath(component_install_dir_.GetPath()));
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
dir_v1.GetPath(), "Attestations list 0.01"));
const base::Version version_1 = base::Version("0.0.1");
policy.ComponentReadyForTesting(version_1, dir_v1.GetPath(),
base::Value::Dict());
auto [loaded_version_1, loaded_path_v1, is_pre_installed_v1] = future.Take();
EXPECT_TRUE(loaded_version_1.IsValid());
EXPECT_EQ(loaded_version_1, version_1);
EXPECT_EQ(loaded_path_v1, Installer::GetInstalledFilePath(dir_v1.GetPath()));
// Load the newer version.
base::ScopedTempDir dir_v2;
ASSERT_TRUE(
dir_v2.CreateUniqueTempDirUnderPath(component_install_dir_.GetPath()));
ASSERT_TRUE(WritePrivacySandboxAttestationsFileForTesting(
dir_v2.GetPath(), "Attestations list 0.02"));
const base::Version version_2 = base::Version("0.0.2");
policy.ComponentReadyForTesting(version_2, dir_v2.GetPath(),
base::Value::Dict());
auto [loaded_version_2, loaded_path_v2, is_pre_installed_v2] = future.Take();
EXPECT_TRUE(loaded_version_2.IsValid());
EXPECT_EQ(loaded_version_2, version_2);
EXPECT_EQ(loaded_path_v2, Installer::GetInstalledFilePath(dir_v2.GetPath()));
// Load the initial version again, callback `on_attestations_ready_` should
// still be invoked.
policy.ComponentReadyForTesting(version_1, dir_v1.GetPath(),
base::Value::Dict());
auto [loaded_version_3, loaded_path_v3, is_pre_installed_v3] = future.Take();
EXPECT_TRUE(loaded_version_3.IsValid());
EXPECT_EQ(loaded_version_3, version_1);
EXPECT_EQ(loaded_path_v3, Installer::GetInstalledFilePath(dir_v1.GetPath()));
}
class PrivacySandboxAttestationsHistogramsTest
: public PrivacySandboxAttestationsInstallerFeatureEnabledTest,
public testing::WithParamInterface<std::tuple<bool, bool>> {
public:
std::string GetHistogram() const {
if (BrowserWindowFirstPaintRecorded()) {
return privacy_sandbox::kComponentReadyFromBrowserWindowFirstPaintUMA;
}
if (NonBrowserUIDisplayed()) {
return privacy_sandbox::
kComponentReadyFromApplicationStartWithInterruptionUMA;
}
return privacy_sandbox::kComponentReadyFromApplicationStartUMA;
}
bool NonBrowserUIDisplayed() const { return std::get<0>(GetParam()); }
bool BrowserWindowFirstPaintRecorded() const {
return std::get<1>(GetParam());
}
protected:
void SetUp() override {
// Reset the singleton recorder to avoid interference across test cases.
startup_metric_utils::GetBrowser().ResetSessionForTesting();
if (NonBrowserUIDisplayed()) {
// Simulate a non browser UI, e.g, profile picker has been displayed.
startup_metric_utils::GetBrowser().SetNonBrowserUIDisplayed();
}
if (BrowserWindowFirstPaintRecorded()) {
// Simulate that the browser window paint has shown and been recorded.
startup_metric_utils::GetBrowser().RecordBrowserWindowFirstPaintTicks(
base::TimeTicks::Now());
}
}
base::HistogramTester histogram_tester_;
};
TEST_P(PrivacySandboxAttestationsHistogramsTest,
RecordHistogramWhenComponentReady) {
PrivacySandboxAttestationsComponentInstallerPolicy policy(base::DoNothing());
base::ScopedTempDir dir_v1;
ASSERT_TRUE(
dir_v1.CreateUniqueTempDirUnderPath(component_install_dir_.GetPath()));
const base::Version version_1 = base::Version("0.0.1");
policy.ComponentReadyForTesting(version_1, dir_v1.GetPath(),
base::Value::Dict());
histogram_tester_.ExpectTotalCount(GetHistogram(), 1);
}
TEST_P(PrivacySandboxAttestationsHistogramsTest,
HistogramShouldOnlyRecordedOnce) {
PrivacySandboxAttestationsComponentInstallerPolicy policy(base::DoNothing());
// Load the initial version.
base::ScopedTempDir dir_v1;
ASSERT_TRUE(
dir_v1.CreateUniqueTempDirUnderPath(component_install_dir_.GetPath()));
const base::Version version_1 = base::Version("0.0.1");
policy.ComponentReadyForTesting(version_1, dir_v1.GetPath(),
base::Value::Dict());
histogram_tester_.ExpectTotalCount(GetHistogram(), 1);
// Load the newer version.
base::ScopedTempDir dir_v2;
ASSERT_TRUE(
dir_v2.CreateUniqueTempDirUnderPath(component_install_dir_.GetPath()));
const base::Version version_2 = base::Version("0.0.2");
policy.ComponentReadyForTesting(version_2, dir_v2.GetPath(),
base::Value::Dict());
histogram_tester_.ExpectTotalCount(GetHistogram(), 1);
}
TEST_P(PrivacySandboxAttestationsHistogramsTest,
HistogramNotRecordedIfInvalidInput) {
PrivacySandboxAttestationsComponentInstallerPolicy policy(base::DoNothing());
// Try loading with an empty path.
policy.ComponentReadyForTesting(base::Version("0.0.1"), base::FilePath(),
base::Value::Dict());
histogram_tester_.ExpectTotalCount(GetHistogram(), 0);
// Try loading with an invalid version.
policy.ComponentReadyForTesting(
base::Version(), component_install_dir_.GetPath(), base::Value::Dict());
histogram_tester_.ExpectTotalCount(GetHistogram(), 0);
}
INSTANTIATE_TEST_SUITE_P(
PrivacySandboxAttestationsHistograms,
PrivacySandboxAttestationsHistogramsTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<std::tuple<bool, bool>>& info) {
return base::StringPrintf(
"%s_%s",
std::get<0>(info.param) ? "NonBrowserUIDisplayed" : "NormalStart",
std::get<1>(info.param) ? "BrowserWindowFirstPaintRecorded"
: "NoBrowserWindowFirstPaint");
});
} // namespace component_updater
|