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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include "content/browser/indexed_db/indexed_db_quota_client.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/base/origin_url_conversions.h"
// Declared to shorten the line lengths.
static const quota::StorageType kTemp = quota::kStorageTypeTemporary;
static const quota::StorageType kPerm = quota::kStorageTypePersistent;
using namespace webkit_database;
namespace content {
// Base class for our test fixtures.
class IndexedDBQuotaClientTest : public testing::Test {
public:
const GURL kOriginA;
const GURL kOriginB;
const GURL kOriginOther;
IndexedDBQuotaClientTest()
: kOriginA("http://host"),
kOriginB("http://host:8000"),
kOriginOther("http://other"),
usage_(0),
weak_factory_(this),
message_loop_(base::MessageLoop::TYPE_IO),
db_thread_(BrowserThread::DB, &message_loop_),
webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_),
file_thread_(BrowserThread::FILE, &message_loop_),
file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
&message_loop_),
io_thread_(BrowserThread::IO, &message_loop_) {
browser_context_.reset(new TestBrowserContext());
idb_context_ = static_cast<IndexedDBContextImpl*>(
BrowserContext::GetDefaultStoragePartition(browser_context_.get())->
GetIndexedDBContext());
message_loop_.RunUntilIdle();
setup_temp_dir();
}
void setup_temp_dir() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::FilePath indexeddb_dir = temp_dir_.path().Append(
IndexedDBContextImpl::kIndexedDBDirectory);
ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir));
idb_context()->set_data_path_for_testing(indexeddb_dir);
}
virtual ~IndexedDBQuotaClientTest() {
// IndexedDBContext needs to be destructed on
// BrowserThread::WEBKIT_DEPRECATED, which is also a member variable of this
// class. Cause IndexedDBContext's destruction now to ensure that it
// doesn't outlive BrowserThread::WEBKIT_DEPRECATED.
idb_context_ = NULL;
browser_context_.reset();
base::MessageLoop::current()->RunUntilIdle();
}
int64 GetOriginUsage(
quota::QuotaClient* client,
const GURL& origin,
quota::StorageType type) {
usage_ = -1;
client->GetOriginUsage(
origin, type,
base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete,
weak_factory_.GetWeakPtr()));
base::MessageLoop::current()->RunUntilIdle();
EXPECT_GT(usage_, -1);
return usage_;
}
const std::set<GURL>& GetOriginsForType(
quota::QuotaClient* client,
quota::StorageType type) {
origins_.clear();
client->GetOriginsForType(
type,
base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
weak_factory_.GetWeakPtr()));
base::MessageLoop::current()->RunUntilIdle();
return origins_;
}
const std::set<GURL>& GetOriginsForHost(
quota::QuotaClient* client,
quota::StorageType type,
const std::string& host) {
origins_.clear();
client->GetOriginsForHost(
type, host,
base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
weak_factory_.GetWeakPtr()));
base::MessageLoop::current()->RunUntilIdle();
return origins_;
}
quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client,
const GURL& origin_url) {
delete_status_ = quota::kQuotaStatusUnknown;
client->DeleteOriginData(
origin_url, kTemp,
base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete,
weak_factory_.GetWeakPtr()));
base::MessageLoop::current()->RunUntilIdle();
return delete_status_;
}
IndexedDBContextImpl* idb_context() { return idb_context_.get(); }
void SetFileSizeTo(const base::FilePath& path, int size) {
std::string junk(size, 'a');
ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size));
}
void AddFakeIndexedDB(const GURL& origin, int size) {
base::FilePath file_path_origin = idb_context()->GetFilePathForTesting(
webkit_base::GetOriginIdentifierFromURL(origin));
if (!file_util::CreateDirectory(file_path_origin)) {
LOG(ERROR) << "failed to file_util::CreateDirectory "
<< file_path_origin.value();
}
file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file"));
SetFileSizeTo(file_path_origin, size);
idb_context()->ResetCaches();
}
private:
void OnGetOriginUsageComplete(int64 usage) {
usage_ = usage;
}
void OnGetOriginsComplete(const std::set<GURL>& origins) {
origins_ = origins;
}
void OnDeleteOriginComplete(quota::QuotaStatusCode code) {
delete_status_ = code;
}
base::ScopedTempDir temp_dir_;
int64 usage_;
std::set<GURL> origins_;
scoped_refptr<IndexedDBContextImpl> idb_context_;
base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_;
base::MessageLoop message_loop_;
BrowserThreadImpl db_thread_;
BrowserThreadImpl webkit_thread_;
BrowserThreadImpl file_thread_;
BrowserThreadImpl file_user_blocking_thread_;
BrowserThreadImpl io_thread_;
scoped_ptr<TestBrowserContext> browser_context_;
quota::QuotaStatusCode delete_status_;
};
TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) {
IndexedDBQuotaClient client(
base::MessageLoopProxy::current(),
idb_context());
AddFakeIndexedDB(kOriginA, 6);
AddFakeIndexedDB(kOriginB, 3);
EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp));
EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
AddFakeIndexedDB(kOriginA, 1000);
EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
}
TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) {
IndexedDBQuotaClient client(
base::MessageLoopProxy::current(),
idb_context());
EXPECT_EQ(kOriginA.host(), kOriginB.host());
EXPECT_NE(kOriginA.host(), kOriginOther.host());
std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
EXPECT_TRUE(origins.empty());
AddFakeIndexedDB(kOriginA, 1000);
origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
EXPECT_EQ(origins.size(), 1ul);
EXPECT_TRUE(origins.find(kOriginA) != origins.end());
AddFakeIndexedDB(kOriginB, 1000);
origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
EXPECT_EQ(origins.size(), 2ul);
EXPECT_TRUE(origins.find(kOriginA) != origins.end());
EXPECT_TRUE(origins.find(kOriginB) != origins.end());
EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty());
EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty());
}
TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) {
IndexedDBQuotaClient client(
base::MessageLoopProxy::current(),
idb_context());
EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty());
EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
AddFakeIndexedDB(kOriginA, 1000);
std::set<GURL> origins = GetOriginsForType(&client, kTemp);
EXPECT_EQ(origins.size(), 1ul);
EXPECT_TRUE(origins.find(kOriginA) != origins.end());
EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
}
TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) {
IndexedDBQuotaClient client(
base::MessageLoopProxy::current(),
idb_context());
AddFakeIndexedDB(kOriginA, 1000);
AddFakeIndexedDB(kOriginB, 50);
EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA);
EXPECT_EQ(quota::kQuotaStatusOk, delete_status);
EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp));
EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
}
} // namespace content
|