Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_quota_client_unittest.cc
blob3bec0b045320fb612415eb7d82b9b0ffad916473
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <map>
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/test/test_simple_task_runner.h"
12 #include "base/threading/thread.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/indexed_db/indexed_db_context_impl.h"
15 #include "content/browser/indexed_db/indexed_db_quota_client.h"
16 #include "content/browser/quota/mock_quota_manager.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "content/public/test/test_browser_context.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "storage/common/database/database_identifier.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 // Declared to shorten the line lengths.
24 static const storage::StorageType kTemp = storage::kStorageTypeTemporary;
25 static const storage::StorageType kPerm = storage::kStorageTypePersistent;
27 namespace content {
29 // Base class for our test fixtures.
30 class IndexedDBQuotaClientTest : public testing::Test {
31 public:
32 const GURL kOriginA;
33 const GURL kOriginB;
34 const GURL kOriginOther;
36 IndexedDBQuotaClientTest()
37 : kOriginA("http://host"),
38 kOriginB("http://host:8000"),
39 kOriginOther("http://other"),
40 usage_(0),
41 task_runner_(new base::TestSimpleTaskRunner),
42 weak_factory_(this) {
43 browser_context_.reset(new TestBrowserContext());
45 scoped_refptr<storage::QuotaManager> quota_manager =
46 new MockQuotaManager(false /*in_memory*/, browser_context_->GetPath(),
47 base::MessageLoop::current()->task_runner(),
48 base::MessageLoop::current()->task_runner(),
49 browser_context_->GetSpecialStoragePolicy());
51 idb_context_ =
52 new IndexedDBContextImpl(browser_context_->GetPath(),
53 browser_context_->GetSpecialStoragePolicy(),
54 quota_manager->proxy(),
55 task_runner_.get());
56 base::MessageLoop::current()->RunUntilIdle();
57 setup_temp_dir();
60 void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); }
62 void setup_temp_dir() {
63 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
64 base::FilePath indexeddb_dir =
65 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory);
66 ASSERT_TRUE(base::CreateDirectory(indexeddb_dir));
67 idb_context()->set_data_path_for_testing(indexeddb_dir);
70 ~IndexedDBQuotaClientTest() override {
71 FlushIndexedDBTaskRunner();
72 idb_context_ = NULL;
73 browser_context_.reset();
74 base::MessageLoop::current()->RunUntilIdle();
77 int64 GetOriginUsage(storage::QuotaClient* client,
78 const GURL& origin,
79 storage::StorageType type) {
80 usage_ = -1;
81 client->GetOriginUsage(
82 origin,
83 type,
84 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete,
85 weak_factory_.GetWeakPtr()));
86 FlushIndexedDBTaskRunner();
87 base::MessageLoop::current()->RunUntilIdle();
88 EXPECT_GT(usage_, -1);
89 return usage_;
92 const std::set<GURL>& GetOriginsForType(storage::QuotaClient* client,
93 storage::StorageType type) {
94 origins_.clear();
95 client->GetOriginsForType(
96 type,
97 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
98 weak_factory_.GetWeakPtr()));
99 FlushIndexedDBTaskRunner();
100 base::MessageLoop::current()->RunUntilIdle();
101 return origins_;
104 const std::set<GURL>& GetOriginsForHost(storage::QuotaClient* client,
105 storage::StorageType type,
106 const std::string& host) {
107 origins_.clear();
108 client->GetOriginsForHost(
109 type,
110 host,
111 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete,
112 weak_factory_.GetWeakPtr()));
113 FlushIndexedDBTaskRunner();
114 base::MessageLoop::current()->RunUntilIdle();
115 return origins_;
118 storage::QuotaStatusCode DeleteOrigin(storage::QuotaClient* client,
119 const GURL& origin_url) {
120 delete_status_ = storage::kQuotaStatusUnknown;
121 client->DeleteOriginData(
122 origin_url,
123 kTemp,
124 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete,
125 weak_factory_.GetWeakPtr()));
126 FlushIndexedDBTaskRunner();
127 base::MessageLoop::current()->RunUntilIdle();
128 return delete_status_;
131 IndexedDBContextImpl* idb_context() { return idb_context_.get(); }
133 void SetFileSizeTo(const base::FilePath& path, int size) {
134 std::string junk(size, 'a');
135 ASSERT_EQ(size, base::WriteFile(path, junk.c_str(), size));
138 void AddFakeIndexedDB(const GURL& origin, int size) {
139 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting(
140 storage::GetIdentifierFromOrigin(origin));
141 if (!base::CreateDirectory(file_path_origin)) {
142 LOG(ERROR) << "failed to base::CreateDirectory "
143 << file_path_origin.value();
145 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file"));
146 SetFileSizeTo(file_path_origin, size);
147 idb_context()->ResetCaches();
150 private:
151 void OnGetOriginUsageComplete(int64 usage) { usage_ = usage; }
153 void OnGetOriginsComplete(const std::set<GURL>& origins) {
154 origins_ = origins;
157 void OnDeleteOriginComplete(storage::QuotaStatusCode code) {
158 delete_status_ = code;
161 base::ScopedTempDir temp_dir_;
162 int64 usage_;
163 std::set<GURL> origins_;
164 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
165 scoped_refptr<IndexedDBContextImpl> idb_context_;
166 content::TestBrowserThreadBundle thread_bundle_;
167 scoped_ptr<TestBrowserContext> browser_context_;
168 storage::QuotaStatusCode delete_status_;
169 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_;
171 DISALLOW_COPY_AND_ASSIGN(IndexedDBQuotaClientTest);
174 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) {
175 IndexedDBQuotaClient client(idb_context());
177 AddFakeIndexedDB(kOriginA, 6);
178 AddFakeIndexedDB(kOriginB, 3);
179 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp));
180 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
181 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
182 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
184 AddFakeIndexedDB(kOriginA, 1000);
185 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
186 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
187 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp));
188 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
191 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) {
192 IndexedDBQuotaClient client(idb_context());
194 EXPECT_EQ(kOriginA.host(), kOriginB.host());
195 EXPECT_NE(kOriginA.host(), kOriginOther.host());
197 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
198 EXPECT_TRUE(origins.empty());
200 AddFakeIndexedDB(kOriginA, 1000);
201 origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
202 EXPECT_EQ(origins.size(), 1ul);
203 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
205 AddFakeIndexedDB(kOriginB, 1000);
206 origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
207 EXPECT_EQ(origins.size(), 2ul);
208 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
209 EXPECT_TRUE(origins.find(kOriginB) != origins.end());
211 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty());
212 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty());
215 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) {
216 IndexedDBQuotaClient client(idb_context());
218 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty());
219 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
221 AddFakeIndexedDB(kOriginA, 1000);
222 std::set<GURL> origins = GetOriginsForType(&client, kTemp);
223 EXPECT_EQ(origins.size(), 1ul);
224 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
226 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
229 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) {
230 IndexedDBQuotaClient client(idb_context());
232 AddFakeIndexedDB(kOriginA, 1000);
233 AddFakeIndexedDB(kOriginB, 50);
234 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
235 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
237 storage::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA);
238 EXPECT_EQ(storage::kQuotaStatusOk, delete_status);
239 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp));
240 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp));
243 } // namespace content