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
|
// 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 "content/browser/indexed_db/instance/bucket_context.h"
#include <memory>
#include <string>
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/task/updateable_sequenced_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/services/storage/privileged/mojom/indexed_db_internals_types.mojom-shared.h"
#include "components/services/storage/privileged/mojom/indexed_db_internals_types.mojom.h"
#include "content/browser/indexed_db/instance/fake_transaction.h"
#include "storage/browser/test/mock_quota_manager_proxy.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
namespace content::indexed_db {
using testing::SizeIs;
using ITS = storage::mojom::IdbTransactionState;
class BucketContextTest : public testing::Test {
public:
BucketContextTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
BucketContextTest(const BucketContextTest&) = delete;
BucketContextTest& operator=(const BucketContextTest&) = delete;
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
quota_policy_ = base::MakeRefCounted<storage::MockSpecialStoragePolicy>();
quota_manager_ = base::MakeRefCounted<storage::MockQuotaManager>(
/*is_incognito=*/false, temp_dir_.GetPath(),
base::SingleThreadTaskRunner::GetCurrentDefault().get(),
quota_policy_.get());
quota_manager_proxy_ = base::MakeRefCounted<storage::MockQuotaManagerProxy>(
quota_manager_.get(),
base::SingleThreadTaskRunner::GetCurrentDefault().get());
storage::BucketInfo bucket_info = quota_manager_->CreateBucket(
storage::BucketInitParams::ForDefaultBucket(
blink::StorageKey::CreateFromStringForTesting(
"https://example.com")));
bucket_context_ = std::make_unique<BucketContext>(
bucket_info, base::FilePath(), BucketContext::Delegate(),
scoped_refptr<base::UpdateableSequencedTaskRunner>(),
quota_manager_proxy_,
/*blob_storage_context=*/mojo::NullRemote(),
/*file_system_access_context=*/mojo::NullRemote());
}
void SetQuotaLeft(int64_t quota_manager_response) {
quota_manager_->SetQuota(bucket_context_->bucket_locator().storage_key,
quota_manager_response);
}
protected:
base::test::TaskEnvironment task_environment_;
base::ScopedTempDir temp_dir_;
scoped_refptr<storage::MockSpecialStoragePolicy> quota_policy_;
scoped_refptr<storage::MockQuotaManager> quota_manager_;
scoped_refptr<storage::MockQuotaManagerProxy> quota_manager_proxy_;
std::unique_ptr<BucketContext> bucket_context_;
};
TEST_F(BucketContextTest, CanUseDiskSpaceQueuing) {
base::HistogramTester tester;
// Request space 3 times consecutively. The requests should coalesce.
SetQuotaLeft(100);
base::test::TestFuture<bool> success_future;
base::test::TestFuture<bool> success_future2;
base::test::TestFuture<bool> success_future3;
bucket_context_->CheckCanUseDiskSpace(30, success_future.GetCallback());
bucket_context_->CheckCanUseDiskSpace(30, success_future2.GetCallback());
bucket_context_->CheckCanUseDiskSpace(50, success_future3.GetCallback());
ASSERT_FALSE(success_future.IsReady());
ASSERT_FALSE(success_future2.IsReady());
ASSERT_FALSE(success_future3.IsReady());
EXPECT_TRUE(success_future.Get());
// We know these requests coalesced because only the first request waited
// (via `Get()`), yet all 3 requests are now ready. The first two requests
// succeed but the third fails.
ASSERT_TRUE(success_future2.IsReady());
ASSERT_TRUE(success_future3.IsReady());
EXPECT_TRUE(success_future2.Get());
EXPECT_FALSE(success_future3.Get());
tester.ExpectTotalCount("IndexedDB.QuotaCheckTime2.Success", 1);
}
TEST_F(BucketContextTest, CanUseDiskSpaceCaching) {
// Verify the limited authority that BucketContext has to approve
// disk usage without checking the quota manager. First set the quota manager
// to report a large amount of disk space, but request even more --- the usage
// shouldn't be approved.
constexpr const int64_t kLargeSpace = 120;
SetQuotaLeft(kLargeSpace);
{
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(kLargeSpace + 1,
success_future.GetCallback());
EXPECT_FALSE(success_future.IsReady());
EXPECT_FALSE(success_future.Get());
}
// Second, simulate something using up a lot of the quota.
// `CheckCanUseDiskSpace` will fudge and not check with the QuotaManager, so
// this usage is also approved.
SetQuotaLeft(10);
{
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(kLargeSpace / 2 + 1,
success_future.GetCallback());
EXPECT_TRUE(success_future.IsReady());
EXPECT_TRUE(success_future.Get());
}
// Next, request the same amount of space again. `CheckCanUseDiskSpace` does
// need to double check with the QuotaManager this time as its limited
// authority has been exhausted, and hence this usage will not be approved.
{
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(kLargeSpace / 2 + 1,
success_future.GetCallback());
EXPECT_FALSE(success_future.IsReady());
EXPECT_FALSE(success_future.Get());
}
// Set a large amount of disk space again, request a little.
SetQuotaLeft(kLargeSpace);
{
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(kLargeSpace / 4,
success_future.GetCallback());
EXPECT_FALSE(success_future.IsReady());
EXPECT_TRUE(success_future.Get());
}
// Wait for the cached value to expire. The next request should be approved,
// but only after going to the QuotaManager again.
task_environment_.FastForwardBy(BucketContext::kBucketSpaceCacheTimeLimit *
2);
{
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(kLargeSpace / 4,
success_future.GetCallback());
EXPECT_FALSE(success_future.IsReady());
EXPECT_TRUE(success_future.Get());
}
}
TEST_F(BucketContextTest, CanUseDiskSpaceWarmUp) {
constexpr const int64_t kLargeSpace = 120;
SetQuotaLeft(kLargeSpace);
bucket_context_->CheckCanUseDiskSpace(120, {});
base::RunLoop().RunUntilIdle();
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(120, success_future.GetCallback());
EXPECT_TRUE(success_future.IsReady());
EXPECT_TRUE(success_future.Get());
}
TEST_F(BucketContextTest, BucketSpaceDecay) {
constexpr const int64_t kLargeSpace = 120;
SetQuotaLeft(kLargeSpace);
base::test::TestFuture<bool> success_future;
bucket_context_->CheckCanUseDiskSpace(1, success_future.GetCallback());
EXPECT_TRUE(success_future.Get());
const int64_t can_allot = bucket_context_->GetBucketSpaceToAllot();
EXPECT_LE(can_allot, 120);
task_environment_.FastForwardBy(BucketContext::kBucketSpaceCacheTimeLimit /
2);
const int64_t decayed_can_allot = bucket_context_->GetBucketSpaceToAllot();
EXPECT_LT(decayed_can_allot, can_allot);
EXPECT_GT(decayed_can_allot, 0);
task_environment_.FastForwardBy(BucketContext::kBucketSpaceCacheTimeLimit /
2);
EXPECT_EQ(bucket_context_->GetBucketSpaceToAllot(), 0);
}
// Verifies state history is calculated correctly based on snapshots.
TEST_F(BucketContextTest, MetadataRecordingStateHistory) {
bucket_context_->StartMetadataRecording();
// Helper function to make a idb internals metadata snapshot with a single
// transaction.
auto make_snapshot = [](std::u16string db_name, int64_t tid, ITS state,
double age) {
storage::mojom::IdbBucketMetadataPtr metadata =
storage::mojom::IdbBucketMetadata::New();
auto db = storage::mojom::IdbDatabaseMetadata::New();
db->name = db_name;
auto tx = storage::mojom::IdbTransactionMetadata::New();
tx->tid = tid;
tx->state = state;
tx->age = age;
tx->connection_id = 0;
db->transactions.push_back(std::move(tx));
metadata->databases.push_back(std::move(db));
// Add to a second parallel connection_id to test that it doesn't interfere.
db = storage::mojom::IdbDatabaseMetadata::New();
db->name = db_name;
tx = storage::mojom::IdbTransactionMetadata::New();
tx->connection_id = 1;
tx->tid = tid;
tx->state = state;
tx->age = age * 2;
db->transactions.push_back(std::move(tx));
metadata->databases.push_back(std::move(db));
return metadata;
};
bucket_context_->metadata_recording_buffer_.push_back(
make_snapshot(u"database0", /* tid = */ 1, ITS::kStarted,
/* age = */ 0));
// Add another transaction with a different tid to ensure it does not
// interfere.
auto tx = storage::mojom::IdbTransactionMetadata::New();
tx->tid = 2;
tx->state = ITS::kRunning;
tx->age = 4;
bucket_context_->metadata_recording_buffer_[1]
->databases[0]
->transactions.push_back(std::move(tx));
bucket_context_->metadata_recording_buffer_.push_back(
make_snapshot(u"database0", /* tid = */ 1, ITS::kRunning,
/* age = */ 10));
bucket_context_->metadata_recording_buffer_.push_back(
make_snapshot(u"database0", /* tid = */ 1, ITS::kCommitting,
/* age = */ 20));
bucket_context_->metadata_recording_buffer_.push_back(
make_snapshot(u"database0", /* tid = */ 1, ITS::kRunning,
/* age = */ 30));
bucket_context_->metadata_recording_buffer_.push_back(
make_snapshot(u"database0", /* tid = */ 1, ITS::kFinished,
/* age = */ 50));
std::vector<storage::mojom::IdbBucketMetadataPtr> result =
bucket_context_->StopMetadataRecording();
EXPECT_THAT(result, SizeIs(6));
// Snapshot 0
EXPECT_THAT(result[0]->databases, SizeIs(0));
// Snapshot 1.
tx = std::move(result[1]->databases[0]->transactions[0]);
EXPECT_THAT(tx->state_history, SizeIs(1));
EXPECT_EQ(tx->state_history[0]->state, ITS::kStarted);
EXPECT_EQ(tx->state_history[0]->duration, 0);
// Snapshot 2.
tx = std::move(result[2]->databases[0]->transactions[0]);
EXPECT_THAT(tx->state_history, SizeIs(2));
EXPECT_EQ(tx->state_history[0]->state, ITS::kStarted);
EXPECT_EQ(tx->state_history[0]->duration, 10);
EXPECT_EQ(tx->state_history[1]->state, ITS::kRunning);
EXPECT_EQ(tx->state_history[1]->duration, 0);
// Snapshot 3.
tx = std::move(result[3]->databases[0]->transactions[0]);
EXPECT_THAT(tx->state_history, SizeIs(3));
EXPECT_EQ(tx->state_history[0]->state, ITS::kStarted);
EXPECT_EQ(tx->state_history[0]->duration, 10);
EXPECT_EQ(tx->state_history[1]->state, ITS::kRunning);
EXPECT_EQ(tx->state_history[1]->duration, 10);
EXPECT_EQ(tx->state_history[2]->state, ITS::kCommitting);
EXPECT_EQ(tx->state_history[2]->duration, 0);
// Snapshot 4.
tx = std::move(result[4]->databases[0]->transactions[0]);
EXPECT_THAT(tx->state_history, SizeIs(4));
EXPECT_EQ(tx->state_history[0]->state, ITS::kStarted);
EXPECT_EQ(tx->state_history[0]->duration, 10);
EXPECT_EQ(tx->state_history[1]->state, ITS::kRunning);
EXPECT_EQ(tx->state_history[1]->duration, 10);
EXPECT_EQ(tx->state_history[2]->state, ITS::kCommitting);
EXPECT_EQ(tx->state_history[2]->duration, 10);
EXPECT_EQ(tx->state_history[3]->state, ITS::kRunning);
EXPECT_EQ(tx->state_history[3]->duration, 0);
// Snapshot 5.
tx = std::move(result[5]->databases[0]->transactions[0]);
EXPECT_THAT(tx->state_history, SizeIs(5));
EXPECT_EQ(tx->state_history[0]->state, ITS::kStarted);
EXPECT_EQ(tx->state_history[0]->duration, 10);
EXPECT_EQ(tx->state_history[1]->state, ITS::kRunning);
EXPECT_EQ(tx->state_history[1]->duration, 10);
EXPECT_EQ(tx->state_history[2]->state, ITS::kCommitting);
EXPECT_EQ(tx->state_history[2]->duration, 10);
EXPECT_EQ(tx->state_history[3]->state, ITS::kRunning);
EXPECT_EQ(tx->state_history[3]->duration, 20);
EXPECT_EQ(tx->state_history[4]->state, ITS::kFinished);
EXPECT_EQ(tx->state_history[4]->duration, 0);
}
} // namespace content::indexed_db
|