forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_directory_unittest.cc
171 lines (137 loc) · 6.22 KB
/
storage_directory_unittest.cc
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
// 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 <memory>
#include <set>
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "storage/browser/quota/quota_client_type.h"
#include "storage/browser/quota/storage_directory.h"
#include "storage/browser/quota/storage_directory_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/quota/quota_types.mojom-shared.h"
namespace storage {
class StorageDirectoryTest : public testing::Test {
public:
void SetUp() override {
ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
storage_directory_ =
std::make_unique<StorageDirectory>(temp_directory_.GetPath());
}
void TearDown() override { ASSERT_TRUE(temp_directory_.Delete()); }
protected:
base::ScopedTempDir temp_directory_;
std::unique_ptr<StorageDirectory> storage_directory_;
};
TEST_F(StorageDirectoryTest, CreateDirectory) {
base::FilePath storage_path = storage_directory_->path();
EXPECT_FALSE(base::PathExists(storage_path));
ASSERT_TRUE(storage_directory_->Create());
EXPECT_TRUE(base::PathExists(storage_path));
// Should still return true if it already exists.
ASSERT_TRUE(storage_directory_->Create());
EXPECT_TRUE(base::PathExists(storage_path));
}
TEST_F(StorageDirectoryTest, DoomAndClearStorage) {
base::FilePath storage_path = storage_directory_->path();
ASSERT_TRUE(storage_directory_->Create());
EXPECT_TRUE(base::PathExists(storage_path));
// Write data into directory.
base::WriteFile(storage_path.AppendASCII("FakeStorage"), "dummy_content");
ASSERT_TRUE(storage_directory_->Doom());
EXPECT_FALSE(base::PathExists(storage_path));
std::set<base::FilePath> directories =
storage_directory_->EnumerateDoomedDirectoriesForTesting();
EXPECT_EQ(directories.size(), 1u);
storage_directory_->ClearDoomed();
directories = storage_directory_->EnumerateDoomedDirectoriesForTesting();
EXPECT_EQ(directories.size(), 0u);
}
TEST_F(StorageDirectoryTest, ClearDoomedMultiple) {
base::FilePath storage_path = storage_directory_->path();
// Create and doom storage directory multiple times.
for (unsigned int i = 0; i < 5; i++) {
ASSERT_TRUE(storage_directory_->Create());
ASSERT_TRUE(storage_directory_->Doom());
}
std::set<base::FilePath> directories =
storage_directory_->EnumerateDoomedDirectoriesForTesting();
EXPECT_EQ(directories.size(), 5u);
storage_directory_->ClearDoomed();
directories = storage_directory_->EnumerateDoomedDirectoriesForTesting();
EXPECT_EQ(directories.size(), 0u);
}
TEST_F(StorageDirectoryTest, CreateBucketDirectory) {
BucketLocator example_bucket(
BucketId(123),
blink::StorageKey::CreateFromStringForTesting("http://example/"),
blink::mojom::StorageType::kTemporary, /*is_default=*/false);
base::FilePath bucket_path =
CreateBucketPath(storage_directory_->path().DirName(), example_bucket);
EXPECT_FALSE(base::PathExists(bucket_path));
ASSERT_TRUE(storage_directory_->CreateBucket(example_bucket));
EXPECT_TRUE(base::PathExists(bucket_path));
// Should still return true if it already exists.
ASSERT_TRUE(storage_directory_->CreateBucket(example_bucket));
EXPECT_TRUE(base::PathExists(bucket_path));
}
TEST_F(StorageDirectoryTest, DoomAndClearBucketDirectory) {
BucketLocator bucket1(
BucketId(1),
blink::StorageKey::CreateFromStringForTesting("http://example/"),
blink::mojom::StorageType::kTemporary, /*is_default=*/false);
BucketLocator bucket2(
BucketId(2),
blink::StorageKey::CreateFromStringForTesting("http://example/"),
blink::mojom::StorageType::kTemporary, /*is_default=*/false);
// Create directories for buckets.
ASSERT_TRUE(storage_directory_->CreateBucket(bucket1));
ASSERT_TRUE(storage_directory_->CreateBucket(bucket2));
// Write data into bucket directories.
base::FilePath bucket1_idb_path =
CreateClientBucketPath(storage_directory_->path().DirName(), bucket1,
QuotaClientType::kIndexedDatabase);
base::FilePath bucket2_idb_path =
CreateClientBucketPath(storage_directory_->path().DirName(), bucket2,
QuotaClientType::kIndexedDatabase);
ASSERT_TRUE(base::CreateDirectory(bucket1_idb_path));
ASSERT_TRUE(base::CreateDirectory(bucket2_idb_path));
ASSERT_TRUE(base::WriteFile(bucket1_idb_path.AppendASCII("FakeStorage"),
"fake_content"));
ASSERT_TRUE(base::WriteFile(bucket2_idb_path.AppendASCII("FakeStorage"),
"fake_content"));
ASSERT_TRUE(storage_directory_->DoomBucket(bucket1));
EXPECT_FALSE(base::PathExists(bucket1_idb_path));
EXPECT_TRUE(base::PathExists(bucket2_idb_path));
std::set<base::FilePath> directories =
storage_directory_->EnumerateDoomedBucketsForTesting();
EXPECT_EQ(directories.size(), 1u);
storage_directory_->ClearDoomedBuckets();
directories = storage_directory_->EnumerateDoomedBucketsForTesting();
EXPECT_EQ(directories.size(), 0u);
}
TEST_F(StorageDirectoryTest, ClearMultipleDoomedBuckets) {
BucketLocator bucket1(
BucketId(1),
blink::StorageKey::CreateFromStringForTesting("http://example/"),
blink::mojom::StorageType::kTemporary, /*is_default=*/false);
BucketLocator bucket2(
BucketId(2),
blink::StorageKey::CreateFromStringForTesting("http://example/"),
blink::mojom::StorageType::kTemporary, /*is_default=*/false);
ASSERT_TRUE(storage_directory_->CreateBucket(bucket1));
ASSERT_TRUE(storage_directory_->CreateBucket(bucket2));
ASSERT_TRUE(storage_directory_->DoomBucket(bucket1));
ASSERT_TRUE(storage_directory_->DoomBucket(bucket2));
std::set<base::FilePath> directories =
storage_directory_->EnumerateDoomedBucketsForTesting();
EXPECT_EQ(directories.size(), 2u);
storage_directory_->ClearDoomedBuckets();
directories = storage_directory_->EnumerateDoomedBucketsForTesting();
EXPECT_EQ(directories.size(), 0u);
}
} // namespace storage