| 12
 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
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 
 | // Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/private_aggregation/private_aggregation_budget_storage.h"
#include <memory>
#include <utility>
#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/clamped_math.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "content/browser/private_aggregation/proto/private_aggregation_budgets.pb.h"
#include "sql/database.h"
#include "sql/meta_table.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
constexpr char kExampleSerializedOrigin[] = "http://origin.example";
}  // namespace
class PrivateAggregationBudgetStorageTest : public testing::Test {
 public:
  PrivateAggregationBudgetStorageTest()
      : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  void SetUp() override {
    ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
    db_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
        {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
         base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
  }
  void TearDown() override {
    // Ensure destruction tasks are run before the test ends. Otherwise,
    // erroneous memory leaks may be detected.
    CloseDatabase();
    task_environment_.RunUntilIdle();
  }
  base::OnceClosure OpenDatabase(bool run_in_memory,
                                 base::OnceClosure on_done_initializing) {
    base::OnceCallback<void(std::unique_ptr<PrivateAggregationBudgetStorage>)>
        create_cb = base::BindOnce(
            &PrivateAggregationBudgetStorageTest::OnStorageInitialized,
            base::Unretained(this), std::move(on_done_initializing));
    return PrivateAggregationBudgetStorage::CreateAsync(
        db_task_runner_, run_in_memory, storage_directory(),
        std::move(create_cb));
  }
  // Waits for the database to be initialized.
  void OpenDatabaseAndWait(bool exclusively_run_in_memory = false) {
    base::RunLoop run_loop;
    OpenDatabase(exclusively_run_in_memory,
                 /*on_done_initializing=*/run_loop.QuitClosure());
    run_loop.Run();
  }
  void CloseDatabase() { storage_.reset(); }
  void EnsureDbFlushes() {
    // Ensures any pending writes are run.
    task_environment_.FastForwardBy(
        PrivateAggregationBudgetStorage::kFlushDelay);
    task_environment_.RunUntilIdle();
  }
  void VerifyInitializationCountHistogram(size_t expected_count) {
    histogram_tester_.ExpectTotalCount(
        "PrivacySandbox.PrivateAggregation.BudgetStorage."
        "BeginInitializationCount",
        expected_count);
  }
  // Helper for the unique sample case.
  void VerifyInitHistograms(
      PrivateAggregationBudgetStorage::InitStatus init_status,
      bool shutdown_before_finishing_initialization,
      int expected_bucket_count = 1) {
    histogram_tester_.ExpectUniqueSample(
        "PrivacySandbox.PrivateAggregation.BudgetStorage.InitStatus",
        init_status, expected_bucket_count);
    histogram_tester_.ExpectUniqueSample(
        "PrivacySandbox.PrivateAggregation.BudgetStorage."
        "ShutdownBeforeFinishingInitialization",
        shutdown_before_finishing_initialization, expected_bucket_count);
    histogram_tester_.ExpectUniqueTimeSample(
        "PrivacySandbox.PrivateAggregation.BudgetStorage.InitTime",
        base::TimeDelta(), expected_bucket_count);
    VerifyInitializationCountHistogram(
        /*expected_count=*/expected_bucket_count);
  }
  void VerifyDbSizeHistogram(int total_num_samples, int num_zero_samples) {
    CHECK_LE(0, num_zero_samples);
    CHECK_LE(num_zero_samples, total_num_samples);
    constexpr std::string_view kFileSizeHistogram =
        "PrivacySandbox.PrivateAggregation.BudgetStorage.DbSize";
    histogram_tester_.ExpectTotalCount(kFileSizeHistogram, total_num_samples);
    histogram_tester_.ExpectBucketCount(kFileSizeHistogram, 0,
                                        num_zero_samples);
  }
  base::FilePath storage_directory() const { return temp_directory_.GetPath(); }
  base::FilePath db_path() const {
    return storage_directory().Append(FILE_PATH_LITERAL("PrivateAggregation"));
  }
  PrivateAggregationBudgetStorage* storage() { return storage_.get(); }
  base::test::TaskEnvironment& task_environment() { return task_environment_; }
 private:
  void OnStorageInitialized(
      base::OnceClosure on_done_initializing,
      std::unique_ptr<PrivateAggregationBudgetStorage> storage) {
    storage_ = std::move(storage);
    std::move(on_done_initializing).Run();
  }
  base::ScopedTempDir temp_directory_;
  std::unique_ptr<PrivateAggregationBudgetStorage> storage_;
  scoped_refptr<base::SequencedTaskRunner> db_task_runner_;
  base::test::TaskEnvironment task_environment_;
  base::HistogramTester histogram_tester_;
};
TEST_F(PrivateAggregationBudgetStorageTest, DatabaseInitialization) {
  EXPECT_FALSE(base::PathExists(db_path()));
  base::RunLoop run_loop;
  VerifyInitializationCountHistogram(/*expected_count=*/0);
  OpenDatabase(/*run_in_memory=*/false,
               /*on_done_initializing=*/base::BindLambdaForTesting(
                   [&run_loop]() { run_loop.Quit(); }));
  // The count should be increased when the initialization begins.
  VerifyInitializationCountHistogram(/*expected_count=*/1);
  EXPECT_FALSE(storage());
  run_loop.Run();
  EXPECT_TRUE(storage());
  // Even an unused instance should create the directory.
  EXPECT_TRUE(base::PathExists(db_path()));
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false);
  VerifyDbSizeHistogram(/*total_num_samples=*/1, /*num_zero_samples=*/1);
}
TEST_F(PrivateAggregationBudgetStorageTest,
       StorageDirectoryAlreadyCreated_StillInitializes) {
  base::CreateDirectory(storage_directory());
  base::RunLoop run_loop;
  OpenDatabase(/*run_in_memory=*/false,
               /*on_done_initializing=*/run_loop.QuitClosure());
  EXPECT_FALSE(storage());
  run_loop.Run();
  EXPECT_TRUE(storage());
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false);
  VerifyDbSizeHistogram(/*total_num_samples=*/1, /*num_zero_samples=*/1);
}
TEST_F(PrivateAggregationBudgetStorageTest, DbPathCorrupt_FailsToInitialize) {
  // Create a directory where the database will try to write a file.
  base::CreateDirectory(db_path());
  base::RunLoop run_loop;
  OpenDatabase(/*run_in_memory=*/false,
               /*on_done_initializing=*/run_loop.QuitClosure());
  EXPECT_FALSE(storage());
  run_loop.Run();
  EXPECT_FALSE(storage());
  VerifyInitHistograms(
      PrivateAggregationBudgetStorage::InitStatus::kFailedToOpenDbFile,
      /*shutdown_before_finishing_initialization=*/false);
  VerifyDbSizeHistogram(/*total_num_samples=*/0, /*num_zero_samples=*/0);
}
TEST_F(PrivateAggregationBudgetStorageTest, InMemory_StillInitializes) {
  base::RunLoop run_loop;
  OpenDatabase(/*run_in_memory=*/true,
               /*on_done_initializing=*/run_loop.QuitClosure());
  EXPECT_FALSE(storage());
  run_loop.Run();
  EXPECT_TRUE(storage());
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false);
  VerifyDbSizeHistogram(/*total_num_samples=*/0, /*num_zero_samples=*/0);
}
TEST_F(PrivateAggregationBudgetStorageTest, DatabaseReopened_DataPersisted) {
  OpenDatabaseAndWait();
  ASSERT_TRUE(storage());
  // The database should start empty.
  EXPECT_FALSE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                     /*data=*/nullptr));
  storage()->budgets_data()->UpdateData(kExampleSerializedOrigin,
                                        proto::PrivateAggregationBudgets());
  EnsureDbFlushes();
  CloseDatabase();
  OpenDatabaseAndWait();
  EXPECT_TRUE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                    /*data=*/nullptr));
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false,
                       /*expected_bucket_count=*/2);
  VerifyDbSizeHistogram(/*total_num_samples=*/2, /*num_zero_samples=*/1);
}
TEST_F(PrivateAggregationBudgetStorageTest,
       DatabaseClosedBeforeFlush_DataPersisted) {
  OpenDatabaseAndWait();
  ASSERT_TRUE(storage());
  // The database should start empty.
  EXPECT_FALSE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                     /*data=*/nullptr));
  storage()->budgets_data()->UpdateData(kExampleSerializedOrigin,
                                        proto::PrivateAggregationBudgets());
  // Not waiting for DB flush
  CloseDatabase();
  OpenDatabaseAndWait();
  EXPECT_TRUE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                    /*data=*/nullptr));
}
TEST_F(PrivateAggregationBudgetStorageTest,
       InMemoryDatabaseReopened_DataNotPersisted) {
  OpenDatabaseAndWait(/*exclusively_run_in_memory=*/true);
  ASSERT_TRUE(storage());
  // The database should start empty.
  EXPECT_FALSE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                     /*data=*/nullptr));
  storage()->budgets_data()->UpdateData(kExampleSerializedOrigin,
                                        proto::PrivateAggregationBudgets());
  EnsureDbFlushes();
  CloseDatabase();
  OpenDatabaseAndWait(/*exclusively_run_in_memory=*/true);
  EXPECT_FALSE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                     /*data=*/nullptr));
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false,
                       /*expected_bucket_count=*/2);
  VerifyDbSizeHistogram(/*total_num_samples=*/0, /*num_zero_samples=*/0);
}
TEST_F(PrivateAggregationBudgetStorageTest,
       SchemaVersionChanged_DatabaseRazed) {
  OpenDatabaseAndWait();
  ASSERT_TRUE(storage());
  // The database should start empty.
  EXPECT_FALSE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                     /*data=*/nullptr));
  storage()->budgets_data()->UpdateData(kExampleSerializedOrigin,
                                        proto::PrivateAggregationBudgets());
  EnsureDbFlushes();
  CloseDatabase();
  // Wait for database to finish closing.
  task_environment().RunUntilIdle();
  {
    sql::Database raw_db(sql::test::kTestTag);
    EXPECT_TRUE(raw_db.Open(db_path()));
    sql::MetaTable meta;
    // The values here are irrelevant, as the meta table already exists.
    ASSERT_TRUE(meta.Init(&raw_db, /*version=*/1, /*compatible_version=*/1));
    ASSERT_TRUE(meta.SetVersionNumber(meta.GetVersionNumber() + 1));
    ASSERT_TRUE(meta.SetCompatibleVersionNumber(meta.GetVersionNumber() + 1));
  }
  OpenDatabaseAndWait();
  EXPECT_FALSE(storage()->budgets_data()->TryGetData(kExampleSerializedOrigin,
                                                     /*data=*/nullptr));
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false,
                       /*expected_bucket_count=*/2);
  VerifyDbSizeHistogram(/*total_num_samples=*/2, /*num_zero_samples=*/1);
}
TEST_F(PrivateAggregationBudgetStorageTest,
       StorageDestroyedImmediatelyAfterInitialization_DoesNotCrash) {
  base::RunLoop run_loop;
  OpenDatabase(
      /*run_in_memory=*/false,
      /*on_done_initializing=*/base::BindLambdaForTesting([this, &run_loop]() {
        CloseDatabase();
        run_loop.Quit();
      }));
  run_loop.Run();
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false);
  VerifyDbSizeHistogram(/*total_num_samples=*/1, /*num_zero_samples=*/1);
}
TEST_F(PrivateAggregationBudgetStorageTest,
       StorageShutdownImmediatelyAfterCreation_DoesNotCrash) {
  base::RunLoop run_loop;
  base::OnceClosure shutdown = OpenDatabase(
      /*run_in_memory=*/false,
      /*on_done_initializing=*/base::BindLambdaForTesting([this, &run_loop]() {
        CloseDatabase();
        run_loop.Quit();
      }));
  std::move(shutdown).Run();
  run_loop.Run();
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/true);
  VerifyDbSizeHistogram(/*total_num_samples=*/1, /*num_zero_samples=*/1);
}
TEST_F(PrivateAggregationBudgetStorageTest,
       StorageShutdownImmediatelyAfterInitialization_DoesNotCrash) {
  base::RunLoop run_loop;
  base::OnceClosure shutdown = OpenDatabase(
      /*run_in_memory=*/false,
      /*on_done_initializing=*/base::BindLambdaForTesting([&, this]() {
        std::move(shutdown).Run();
        CloseDatabase();
        run_loop.Quit();
      }));
  run_loop.Run();
  VerifyInitHistograms(PrivateAggregationBudgetStorage::InitStatus::kSuccess,
                       /*shutdown_before_finishing_initialization=*/false);
  VerifyDbSizeHistogram(/*total_num_samples=*/1, /*num_zero_samples=*/1);
}
}  // namespace content
 |