blob: 0e643d66391173c646b691b74c446d08438b9747 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c62983a72011-05-09 06:29:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4d99be52011-10-18 14:11:035#include "base/bind.h"
[email protected]c62983a72011-05-09 06:29:596#include "base/basictypes.h"
7#include "base/file_util.h"
[email protected]c62983a72011-05-09 06:29:598#include "base/message_loop.h"
9#include "base/message_loop_proxy.h"
10#include "base/platform_file.h"
[email protected]e0785902011-05-19 23:34:1711#include "base/scoped_temp_dir.h"
[email protected]c62983a72011-05-09 06:29:5912#include "googleurl/src/gurl.h"
13#include "testing/gtest/include/gtest/gtest.h"
14#include "webkit/fileapi/file_system_context.h"
[email protected]6ff17052011-07-12 06:06:0715#include "webkit/fileapi/file_system_operation_context.h"
[email protected]5caeb202011-05-17 07:42:0716#include "webkit/fileapi/file_system_quota_client.h"
[email protected]c62983a72011-05-09 06:29:5917#include "webkit/fileapi/file_system_types.h"
18#include "webkit/fileapi/file_system_usage_cache.h"
19#include "webkit/fileapi/file_system_util.h"
[email protected]e7e46732012-01-05 11:45:5520#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4921#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]e7e46732012-01-05 11:45:5522#include "webkit/fileapi/sandbox_mount_point_provider.h"
[email protected]c62983a72011-05-09 06:29:5923#include "webkit/quota/quota_types.h"
24
[email protected]4ce4e79d2011-05-13 16:27:1525namespace fileapi {
[email protected]c62983a72011-05-09 06:29:5926namespace {
[email protected]4ce4e79d2011-05-13 16:27:1527
[email protected]c62983a72011-05-09 06:29:5928const char kDummyURL1[] = "http://www.dummy.org";
29const char kDummyURL2[] = "http://www.example.com";
30const char kDummyURL3[] = "http://www.bleh";
31
32// Declared to shorten the variable names.
33const quota::StorageType kTemporary = quota::kStorageTypeTemporary;
34const quota::StorageType kPersistent = quota::kStorageTypePersistent;
[email protected]c62983a72011-05-09 06:29:5935
[email protected]4ce4e79d2011-05-13 16:27:1536} // namespace
[email protected]c62983a72011-05-09 06:29:5937
[email protected]5caeb202011-05-17 07:42:0738class FileSystemQuotaClientTest : public testing::Test {
[email protected]c62983a72011-05-09 06:29:5939 public:
[email protected]5caeb202011-05-17 07:42:0740 FileSystemQuotaClientTest()
[email protected]4d99be52011-10-18 14:11:0341 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]4b66b652011-05-17 03:31:3142 additional_callback_count_(0),
43 deletion_status_(quota::kQuotaStatusUnknown) {
[email protected]c62983a72011-05-09 06:29:5944 }
45
46 void SetUp() {
47 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
48 file_system_context_ =
49 new FileSystemContext(
[email protected]edd685f2011-08-15 20:33:4650 base::MessageLoopProxy::current(),
51 base::MessageLoopProxy::current(),
[email protected]c62983a72011-05-09 06:29:5952 NULL, NULL,
[email protected]e7e46732012-01-05 11:45:5553 data_dir_.path(),
54 CreateDisallowFileAccessOptions());
[email protected]c62983a72011-05-09 06:29:5955 }
56
57 struct TestFile {
58 bool isDirectory;
59 const char* name;
60 int64 size;
61 const char* origin_url;
62 quota::StorageType type;
63 };
64
65 protected:
[email protected]5caeb202011-05-17 07:42:0766 FileSystemQuotaClient* NewQuotaClient(bool is_incognito) {
67 return new FileSystemQuotaClient(
[email protected]c62983a72011-05-09 06:29:5968 file_system_context_, is_incognito);
69 }
70
[email protected]5caeb202011-05-17 07:42:0771 void GetOriginUsageAsync(FileSystemQuotaClient* quota_client,
[email protected]ffac1ac42011-07-25 15:03:1572 const std::string& origin_url,
[email protected]c62983a72011-05-09 06:29:5973 quota::StorageType type) {
[email protected]4d99be52011-10-18 14:11:0374 quota_client->GetOriginUsage(
75 GURL(origin_url), type,
76 base::Bind(&FileSystemQuotaClientTest::OnGetUsage,
77 weak_factory_.GetWeakPtr()));
[email protected]c62983a72011-05-09 06:29:5978 }
79
[email protected]5caeb202011-05-17 07:42:0780 int64 GetOriginUsage(FileSystemQuotaClient* quota_client,
[email protected]ffac1ac42011-07-25 15:03:1581 const std::string& origin_url,
[email protected]c62983a72011-05-09 06:29:5982 quota::StorageType type) {
83 GetOriginUsageAsync(quota_client, origin_url, type);
84 MessageLoop::current()->RunAllPending();
85 return usage_;
86 }
87
[email protected]5caeb202011-05-17 07:42:0788 const std::set<GURL>& GetOriginsForType(FileSystemQuotaClient* quota_client,
[email protected]c62983a72011-05-09 06:29:5989 quota::StorageType type) {
90 origins_.clear();
[email protected]4d99be52011-10-18 14:11:0391 quota_client->GetOriginsForType(
92 type,
93 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins,
94 weak_factory_.GetWeakPtr()));
[email protected]c62983a72011-05-09 06:29:5995 MessageLoop::current()->RunAllPending();
96 return origins_;
97 }
98
[email protected]5caeb202011-05-17 07:42:0799 const std::set<GURL>& GetOriginsForHost(FileSystemQuotaClient* quota_client,
[email protected]c62983a72011-05-09 06:29:59100 quota::StorageType type,
[email protected]ffac1ac42011-07-25 15:03:15101 const std::string& host) {
[email protected]c62983a72011-05-09 06:29:59102 origins_.clear();
[email protected]4d99be52011-10-18 14:11:03103 quota_client->GetOriginsForHost(
104 type, host,
105 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins,
106 weak_factory_.GetWeakPtr()));
[email protected]c62983a72011-05-09 06:29:59107 MessageLoop::current()->RunAllPending();
108 return origins_;
109 }
110
[email protected]5caeb202011-05-17 07:42:07111 void RunAdditionalOriginUsageTask(FileSystemQuotaClient* quota_client,
[email protected]ffac1ac42011-07-25 15:03:15112 const std::string& origin_url,
[email protected]c62983a72011-05-09 06:29:59113 quota::StorageType type) {
[email protected]4d99be52011-10-18 14:11:03114 quota_client->GetOriginUsage(
115 GURL(origin_url), type,
116 base::Bind(&FileSystemQuotaClientTest::OnGetAdditionalUsage,
117 weak_factory_.GetWeakPtr()));
[email protected]c62983a72011-05-09 06:29:59118 }
119
[email protected]08f8feb2012-02-26 11:53:50120 FileSystemOperationContext* CreateFileSystemOperationContext() {
[email protected]6ff17052011-07-12 06:06:07121 FileSystemOperationContext* context =
[email protected]08f8feb2012-02-26 11:53:50122 new FileSystemOperationContext(file_system_context_);
[email protected]6ff17052011-07-12 06:06:07123 context->set_allowed_bytes_growth(100000000);
124 return context;
[email protected]c62983a72011-05-09 06:29:59125 }
126
[email protected]08f8feb2012-02-26 11:53:50127 bool CreateFileSystemDirectory(const FilePath& file_path,
[email protected]ffac1ac42011-07-25 15:03:15128 const std::string& origin_url,
[email protected]08f8feb2012-02-26 11:53:50129 quota::StorageType storage_type) {
130 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
131 FileSystemFileUtil* file_util = file_system_context_->GetFileUtil(type);
[email protected]6ff17052011-07-12 06:06:07132
[email protected]949f25a2012-06-27 01:53:09133 FileSystemURL url(GURL(origin_url), type, file_path);
[email protected]6ff17052011-07-12 06:06:07134 scoped_ptr<FileSystemOperationContext> context(
[email protected]08f8feb2012-02-26 11:53:50135 CreateFileSystemOperationContext());
[email protected]6ff17052011-07-12 06:06:07136
137 base::PlatformFileError result =
[email protected]949f25a2012-06-27 01:53:09138 file_util->CreateDirectory(context.get(), url, false, false);
[email protected]6ff17052011-07-12 06:06:07139 if (result != base::PLATFORM_FILE_OK)
140 return false;
141 return true;
142 }
143
[email protected]08f8feb2012-02-26 11:53:50144 bool CreateFileSystemFile(const FilePath& file_path,
[email protected]c62983a72011-05-09 06:29:59145 int64 file_size,
[email protected]ffac1ac42011-07-25 15:03:15146 const std::string& origin_url,
[email protected]08f8feb2012-02-26 11:53:50147 quota::StorageType storage_type) {
148 if (file_path.empty())
[email protected]c62983a72011-05-09 06:29:59149 return false;
150
[email protected]08f8feb2012-02-26 11:53:50151 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type);
[email protected]d6afd112012-07-25 22:55:04152 FileSystemFileUtil* file_util = file_system_context_->
153 sandbox_provider()->GetFileUtil(type);
154
[email protected]949f25a2012-06-27 01:53:09155 FileSystemURL url(GURL(origin_url), type, file_path);
[email protected]6ff17052011-07-12 06:06:07156 scoped_ptr<FileSystemOperationContext> context(
[email protected]08f8feb2012-02-26 11:53:50157 CreateFileSystemOperationContext());
[email protected]6ff17052011-07-12 06:06:07158
159 bool created = false;
160 if (base::PLATFORM_FILE_OK !=
[email protected]949f25a2012-06-27 01:53:09161 file_util->EnsureFileExists(context.get(), url, &created))
[email protected]6ff17052011-07-12 06:06:07162 return false;
163 EXPECT_TRUE(created);
164 if (base::PLATFORM_FILE_OK !=
[email protected]949f25a2012-06-27 01:53:09165 file_util->Truncate(context.get(), url, file_size))
[email protected]6ff17052011-07-12 06:06:07166 return false;
167 return true;
[email protected]c62983a72011-05-09 06:29:59168 }
169
[email protected]6ff17052011-07-12 06:06:07170 void InitializeOriginFiles(FileSystemQuotaClient* quota_client,
171 const TestFile* files,
172 int num_files) {
[email protected]c62983a72011-05-09 06:29:59173 for (int i = 0; i < num_files; i++) {
[email protected]6ff17052011-07-12 06:06:07174 FilePath path = FilePath().AppendASCII(files[i].name);
[email protected]c62983a72011-05-09 06:29:59175 if (files[i].isDirectory) {
176 ASSERT_TRUE(CreateFileSystemDirectory(
[email protected]6ff17052011-07-12 06:06:07177 path, files[i].origin_url, files[i].type));
178 if (path.empty()) {
179 // Create the usage cache.
[email protected]0c5ebe32011-08-19 22:37:16180 // HACK--we always create the root [an empty path] first. If we
181 // create it later, this will fail due to a quota mismatch. If we
182 // call this before we create the root, it succeeds, but hasn't
183 // actually created the cache.
[email protected]6ff17052011-07-12 06:06:07184 ASSERT_EQ(0, GetOriginUsage(
185 quota_client, files[i].origin_url, files[i].type));
186 }
[email protected]c62983a72011-05-09 06:29:59187 } else {
188 ASSERT_TRUE(CreateFileSystemFile(
[email protected]6ff17052011-07-12 06:06:07189 path, files[i].size, files[i].origin_url, files[i].type));
[email protected]c62983a72011-05-09 06:29:59190 }
191 }
192 }
193
[email protected]0c5ebe32011-08-19 22:37:16194 // This is a bit fragile--it depends on the test data always creating a
195 // directory before adding a file or directory to it, so that we can just
196 // count the basename of each addition. A recursive creation of a path, which
197 // created more than one directory in a single shot, would break this.
[email protected]ffac1ac42011-07-25 15:03:15198 int64 ComputeFilePathsCostForOriginAndType(const TestFile* files,
199 int num_files,
200 const std::string& origin_url,
201 quota::StorageType type) {
202 int64 file_paths_cost = 0;
203 for (int i = 0; i < num_files; i++) {
204 if (files[i].type == type &&
205 GURL(files[i].origin_url) == GURL(origin_url)) {
206 FilePath path = FilePath().AppendASCII(files[i].name);
207 if (!path.empty()) {
[email protected]7878ece2011-09-05 11:41:49208 file_paths_cost += ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]ffac1ac42011-07-25 15:03:15209 }
210 }
211 }
212 return file_paths_cost;
213 }
214
[email protected]5caeb202011-05-17 07:42:07215 void DeleteOriginData(FileSystemQuotaClient* quota_client,
[email protected]ffac1ac42011-07-25 15:03:15216 const std::string& origin,
[email protected]4b66b652011-05-17 03:31:31217 quota::StorageType type) {
218 deletion_status_ = quota::kQuotaStatusUnknown;
219 quota_client->DeleteOriginData(
220 GURL(origin), type,
[email protected]4d99be52011-10-18 14:11:03221 base::Bind(&FileSystemQuotaClientTest::OnDeleteOrigin,
222 weak_factory_.GetWeakPtr()));
[email protected]4b66b652011-05-17 03:31:31223 }
224
[email protected]c62983a72011-05-09 06:29:59225 int64 usage() const { return usage_; }
[email protected]4b66b652011-05-17 03:31:31226 quota::QuotaStatusCode status() { return deletion_status_; }
[email protected]c62983a72011-05-09 06:29:59227 int additional_callback_count() const { return additional_callback_count_; }
228 void set_additional_callback_count(int count) {
229 additional_callback_count_ = count;
230 }
231
232 private:
233 void OnGetUsage(int64 usage) {
234 usage_ = usage;
235 }
236
[email protected]4ac23ad22011-08-05 08:39:54237 void OnGetOrigins(const std::set<GURL>& origins,
238 quota::StorageType type) {
[email protected]c62983a72011-05-09 06:29:59239 origins_ = origins;
[email protected]4ac23ad22011-08-05 08:39:54240 type_ = type;
[email protected]c62983a72011-05-09 06:29:59241 }
242
[email protected]4b66b652011-05-17 03:31:31243 void OnGetAdditionalUsage(int64 usage_unused) {
[email protected]c62983a72011-05-09 06:29:59244 ++additional_callback_count_;
245 }
246
[email protected]4b66b652011-05-17 03:31:31247 void OnDeleteOrigin(quota::QuotaStatusCode status) {
248 deletion_status_ = status;
249 }
250
[email protected]c62983a72011-05-09 06:29:59251 ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01252 MessageLoop message_loop_;
[email protected]c62983a72011-05-09 06:29:59253 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]4d99be52011-10-18 14:11:03254 base::WeakPtrFactory<FileSystemQuotaClientTest> weak_factory_;
[email protected]c62983a72011-05-09 06:29:59255 int64 usage_;
256 int additional_callback_count_;
257 std::set<GURL> origins_;
[email protected]4ac23ad22011-08-05 08:39:54258 quota::StorageType type_;
[email protected]4b66b652011-05-17 03:31:31259 quota::QuotaStatusCode deletion_status_;
[email protected]c62983a72011-05-09 06:29:59260
[email protected]5caeb202011-05-17 07:42:07261 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaClientTest);
[email protected]c62983a72011-05-09 06:29:59262};
263
[email protected]5caeb202011-05-17 07:42:07264TEST_F(FileSystemQuotaClientTest, NoFileSystemTest) {
265 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59266
267 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
268}
269
[email protected]5caeb202011-05-17 07:42:07270TEST_F(FileSystemQuotaClientTest, NoFileTest) {
271 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59272 const TestFile kFiles[] = {
273 {true, NULL, 0, kDummyURL1, kTemporary},
274 };
[email protected]6ff17052011-07-12 06:06:07275 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]c62983a72011-05-09 06:29:59276
277 for (int i = 0; i < 2; i++) {
[email protected]0c5ebe32011-08-19 22:37:16278 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
[email protected]c62983a72011-05-09 06:29:59279 }
280}
281
[email protected]5caeb202011-05-17 07:42:07282TEST_F(FileSystemQuotaClientTest, OneFileTest) {
283 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59284 const TestFile kFiles[] = {
285 {true, NULL, 0, kDummyURL1, kTemporary},
286 {false, "foo", 4921, kDummyURL1, kTemporary},
287 };
[email protected]6ff17052011-07-12 06:06:07288 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15289 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType(
290 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
[email protected]c62983a72011-05-09 06:29:59291
292 for (int i = 0; i < 2; i++) {
[email protected]ffac1ac42011-07-25 15:03:15293 EXPECT_EQ(4921 + file_paths_cost,
[email protected]c62983a72011-05-09 06:29:59294 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
295 }
296}
297
[email protected]5caeb202011-05-17 07:42:07298TEST_F(FileSystemQuotaClientTest, TwoFilesTest) {
299 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59300 const TestFile kFiles[] = {
301 {true, NULL, 0, kDummyURL1, kTemporary},
302 {false, "foo", 10310, kDummyURL1, kTemporary},
303 {false, "bar", 41, kDummyURL1, kTemporary},
304 };
[email protected]6ff17052011-07-12 06:06:07305 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15306 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType(
307 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
[email protected]c62983a72011-05-09 06:29:59308
309 for (int i = 0; i < 2; i++) {
[email protected]ffac1ac42011-07-25 15:03:15310 EXPECT_EQ(10310 + 41 + file_paths_cost,
[email protected]c62983a72011-05-09 06:29:59311 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
312 }
313}
314
[email protected]5caeb202011-05-17 07:42:07315TEST_F(FileSystemQuotaClientTest, EmptyFilesTest) {
316 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59317 const TestFile kFiles[] = {
318 {true, NULL, 0, kDummyURL1, kTemporary},
319 {false, "foo", 0, kDummyURL1, kTemporary},
320 {false, "bar", 0, kDummyURL1, kTemporary},
321 {false, "baz", 0, kDummyURL1, kTemporary},
322 };
[email protected]6ff17052011-07-12 06:06:07323 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15324 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType(
325 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
[email protected]c62983a72011-05-09 06:29:59326
327 for (int i = 0; i < 2; i++) {
[email protected]ffac1ac42011-07-25 15:03:15328 EXPECT_EQ(file_paths_cost,
[email protected]c62983a72011-05-09 06:29:59329 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
330 }
331}
332
[email protected]5caeb202011-05-17 07:42:07333TEST_F(FileSystemQuotaClientTest, SubDirectoryTest) {
334 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59335 const TestFile kFiles[] = {
336 {true, NULL, 0, kDummyURL1, kTemporary},
337 {true, "dirtest", 0, kDummyURL1, kTemporary},
338 {false, "dirtest/foo", 11921, kDummyURL1, kTemporary},
339 {false, "bar", 4814, kDummyURL1, kTemporary},
340 };
[email protected]6ff17052011-07-12 06:06:07341 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15342 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType(
343 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
[email protected]c62983a72011-05-09 06:29:59344
345 for (int i = 0; i < 2; i++) {
[email protected]ffac1ac42011-07-25 15:03:15346 EXPECT_EQ(11921 + 4814 + file_paths_cost,
[email protected]c62983a72011-05-09 06:29:59347 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
348 }
349}
350
[email protected]5caeb202011-05-17 07:42:07351TEST_F(FileSystemQuotaClientTest, MultiTypeTest) {
352 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59353 const TestFile kFiles[] = {
354 {true, NULL, 0, kDummyURL1, kTemporary},
355 {true, "dirtest", 0, kDummyURL1, kTemporary},
356 {false, "dirtest/foo", 133, kDummyURL1, kTemporary},
357 {false, "bar", 14, kDummyURL1, kTemporary},
358 {true, NULL, 0, kDummyURL1, kPersistent},
359 {true, "dirtest", 0, kDummyURL1, kPersistent},
360 {false, "dirtest/foo", 193, kDummyURL1, kPersistent},
361 {false, "bar", 9, kDummyURL1, kPersistent},
362 };
[email protected]6ff17052011-07-12 06:06:07363 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15364 const int64 file_paths_cost_temporary = ComputeFilePathsCostForOriginAndType(
365 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
366 const int64 file_paths_cost_persistent = ComputeFilePathsCostForOriginAndType(
367 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
[email protected]c62983a72011-05-09 06:29:59368
369 for (int i = 0; i < 2; i++) {
[email protected]ffac1ac42011-07-25 15:03:15370 EXPECT_EQ(133 + 14 + file_paths_cost_temporary,
[email protected]c62983a72011-05-09 06:29:59371 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
[email protected]ffac1ac42011-07-25 15:03:15372 EXPECT_EQ(193 + 9 + file_paths_cost_persistent,
[email protected]c62983a72011-05-09 06:29:59373 GetOriginUsage(quota_client.get(), kDummyURL1, kPersistent));
374 }
375}
376
[email protected]5caeb202011-05-17 07:42:07377TEST_F(FileSystemQuotaClientTest, MultiDomainTest) {
378 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59379 const TestFile kFiles[] = {
380 {true, NULL, 0, kDummyURL1, kTemporary},
381 {true, "dir1", 0, kDummyURL1, kTemporary},
382 {false, "dir1/foo", 1331, kDummyURL1, kTemporary},
383 {false, "bar", 134, kDummyURL1, kTemporary},
384 {true, NULL, 0, kDummyURL1, kPersistent},
385 {true, "dir2", 0, kDummyURL1, kPersistent},
386 {false, "dir2/foo", 1903, kDummyURL1, kPersistent},
387 {false, "bar", 19, kDummyURL1, kPersistent},
388 {true, NULL, 0, kDummyURL2, kTemporary},
389 {true, "dom", 0, kDummyURL2, kTemporary},
390 {false, "dom/fan", 1319, kDummyURL2, kTemporary},
391 {false, "bar", 113, kDummyURL2, kTemporary},
392 {true, NULL, 0, kDummyURL2, kPersistent},
393 {true, "dom", 0, kDummyURL2, kPersistent},
394 {false, "dom/fan", 2013, kDummyURL2, kPersistent},
395 {false, "baz", 18, kDummyURL2, kPersistent},
396 };
[email protected]6ff17052011-07-12 06:06:07397 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15398 const int64 file_paths_cost_temporary1 = ComputeFilePathsCostForOriginAndType(
399 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
400 const int64 file_paths_cost_persistent1 =
401 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
402 kDummyURL1, kPersistent);
403 const int64 file_paths_cost_temporary2 = ComputeFilePathsCostForOriginAndType(
404 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL2, kTemporary);
405 const int64 file_paths_cost_persistent2 =
406 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
407 kDummyURL2, kPersistent);
[email protected]c62983a72011-05-09 06:29:59408
409 for (int i = 0; i < 2; i++) {
[email protected]ffac1ac42011-07-25 15:03:15410 EXPECT_EQ(1331 + 134 + file_paths_cost_temporary1,
[email protected]c62983a72011-05-09 06:29:59411 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
[email protected]ffac1ac42011-07-25 15:03:15412 EXPECT_EQ(1903 + 19 + file_paths_cost_persistent1,
[email protected]c62983a72011-05-09 06:29:59413 GetOriginUsage(quota_client.get(), kDummyURL1, kPersistent));
[email protected]ffac1ac42011-07-25 15:03:15414 EXPECT_EQ(1319 + 113 + file_paths_cost_temporary2,
[email protected]c62983a72011-05-09 06:29:59415 GetOriginUsage(quota_client.get(), kDummyURL2, kTemporary));
[email protected]ffac1ac42011-07-25 15:03:15416 EXPECT_EQ(2013 + 18 + file_paths_cost_persistent2,
[email protected]c62983a72011-05-09 06:29:59417 GetOriginUsage(quota_client.get(), kDummyURL2, kPersistent));
418 }
419}
420
[email protected]5caeb202011-05-17 07:42:07421TEST_F(FileSystemQuotaClientTest, GetUsage_MultipleTasks) {
422 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59423 const TestFile kFiles[] = {
424 {true, NULL, 0, kDummyURL1, kTemporary},
425 {false, "foo", 11, kDummyURL1, kTemporary},
426 {false, "bar", 22, kDummyURL1, kTemporary},
427 };
[email protected]6ff17052011-07-12 06:06:07428 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15429 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType(
430 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary);
[email protected]c62983a72011-05-09 06:29:59431
432 // Dispatching three GetUsage tasks.
433 set_additional_callback_count(0);
434 GetOriginUsageAsync(quota_client.get(), kDummyURL1, kTemporary);
435 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary);
436 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary);
437 MessageLoop::current()->RunAllPending();
[email protected]ffac1ac42011-07-25 15:03:15438 EXPECT_EQ(11 + 22 + file_paths_cost, usage());
[email protected]c62983a72011-05-09 06:29:59439 EXPECT_EQ(2, additional_callback_count());
440
441 // Once more, in a different order.
442 set_additional_callback_count(0);
443 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary);
444 GetOriginUsageAsync(quota_client.get(), kDummyURL1, kTemporary);
445 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary);
446 MessageLoop::current()->RunAllPending();
[email protected]ffac1ac42011-07-25 15:03:15447 EXPECT_EQ(11 + 22 + file_paths_cost, usage());
[email protected]c62983a72011-05-09 06:29:59448 EXPECT_EQ(2, additional_callback_count());
449}
450
[email protected]5caeb202011-05-17 07:42:07451TEST_F(FileSystemQuotaClientTest, GetOriginsForType) {
452 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59453 const TestFile kFiles[] = {
454 {true, NULL, 0, kDummyURL1, kTemporary},
455 {true, NULL, 0, kDummyURL2, kTemporary},
456 {true, NULL, 0, kDummyURL3, kPersistent},
457 };
[email protected]6ff17052011-07-12 06:06:07458 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]c62983a72011-05-09 06:29:59459
460 std::set<GURL> origins = GetOriginsForType(quota_client.get(), kTemporary);
461 EXPECT_EQ(2U, origins.size());
462 EXPECT_TRUE(origins.find(GURL(kDummyURL1)) != origins.end());
463 EXPECT_TRUE(origins.find(GURL(kDummyURL2)) != origins.end());
464 EXPECT_TRUE(origins.find(GURL(kDummyURL3)) == origins.end());
465}
466
[email protected]5caeb202011-05-17 07:42:07467TEST_F(FileSystemQuotaClientTest, GetOriginsForHost) {
468 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]c62983a72011-05-09 06:29:59469 const char* kURL1 = "http://foo.com/";
470 const char* kURL2 = "https://foo.com/";
471 const char* kURL3 = "http://foo.com:1/";
472 const char* kURL4 = "http://foo2.com/";
473 const char* kURL5 = "http://foo.com:2/";
474 const TestFile kFiles[] = {
475 {true, NULL, 0, kURL1, kTemporary},
476 {true, NULL, 0, kURL2, kTemporary},
477 {true, NULL, 0, kURL3, kTemporary},
478 {true, NULL, 0, kURL4, kTemporary},
479 {true, NULL, 0, kURL5, kPersistent},
480 };
[email protected]6ff17052011-07-12 06:06:07481 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]c62983a72011-05-09 06:29:59482
483 std::set<GURL> origins = GetOriginsForHost(
484 quota_client.get(), kTemporary, "foo.com");
485 EXPECT_EQ(3U, origins.size());
486 EXPECT_TRUE(origins.find(GURL(kURL1)) != origins.end());
487 EXPECT_TRUE(origins.find(GURL(kURL2)) != origins.end());
488 EXPECT_TRUE(origins.find(GURL(kURL3)) != origins.end());
489 EXPECT_TRUE(origins.find(GURL(kURL4)) == origins.end()); // Different host.
490 EXPECT_TRUE(origins.find(GURL(kURL5)) == origins.end()); // Different type.
491}
492
[email protected]5caeb202011-05-17 07:42:07493TEST_F(FileSystemQuotaClientTest, IncognitoTest) {
494 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(true));
[email protected]c62983a72011-05-09 06:29:59495 const TestFile kFiles[] = {
496 {true, NULL, 0, kDummyURL1, kTemporary},
497 {false, "foo", 10, kDummyURL1, kTemporary},
498 };
[email protected]6ff17052011-07-12 06:06:07499 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]c62983a72011-05-09 06:29:59500
501 // Having files in the usual directory wouldn't affect the result
502 // queried in incognito mode.
503 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary));
504 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kPersistent));
505
506 std::set<GURL> origins = GetOriginsForType(quota_client.get(), kTemporary);
507 EXPECT_EQ(0U, origins.size());
508 origins = GetOriginsForHost(quota_client.get(), kTemporary, "www.dummy.org");
509 EXPECT_EQ(0U, origins.size());
510}
[email protected]4ce4e79d2011-05-13 16:27:15511
[email protected]5caeb202011-05-17 07:42:07512TEST_F(FileSystemQuotaClientTest, DeleteOriginTest) {
513 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false));
[email protected]4b66b652011-05-17 03:31:31514 const TestFile kFiles[] = {
515 {true, NULL, 0, "http://foo.com/", kTemporary},
516 {false, "a", 1, "http://foo.com/", kTemporary},
517 {true, NULL, 0, "https://foo.com/", kTemporary},
518 {false, "b", 2, "https://foo.com/", kTemporary},
519 {true, NULL, 0, "http://foo.com/", kPersistent},
520 {false, "c", 4, "http://foo.com/", kPersistent},
521 {true, NULL, 0, "http://bar.com/", kTemporary},
522 {false, "d", 8, "http://bar.com/", kTemporary},
523 {true, NULL, 0, "http://bar.com/", kPersistent},
524 {false, "e", 16, "http://bar.com/", kPersistent},
525 {true, NULL, 0, "https://bar.com/", kPersistent},
526 {false, "f", 32, "https://bar.com/", kPersistent},
527 {true, NULL, 0, "https://bar.com/", kTemporary},
528 {false, "g", 64, "https://bar.com/", kTemporary},
529 };
[email protected]6ff17052011-07-12 06:06:07530 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles));
[email protected]ffac1ac42011-07-25 15:03:15531 const int64 file_paths_cost_temporary_foo_https =
532 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
533 "https://foo.com/", kTemporary);
534 const int64 file_paths_cost_persistent_foo =
535 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
536 "http://foo.com/", kPersistent);
537 const int64 file_paths_cost_temporary_bar =
538 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
539 "http://bar.com/", kTemporary);
540 const int64 file_paths_cost_temporary_bar_https =
541 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
542 "https://bar.com/", kTemporary);
543 const int64 file_paths_cost_persistent_bar_https =
544 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles),
545 "https://bar.com/", kPersistent);
[email protected]4b66b652011-05-17 03:31:31546
547 DeleteOriginData(quota_client.get(), "http://foo.com/", kTemporary);
548 MessageLoop::current()->RunAllPending();
549 EXPECT_EQ(quota::kQuotaStatusOk, status());
550
551 DeleteOriginData(quota_client.get(), "http://bar.com/", kPersistent);
552 MessageLoop::current()->RunAllPending();
553 EXPECT_EQ(quota::kQuotaStatusOk, status());
554
555 DeleteOriginData(quota_client.get(), "http://buz.com/", kTemporary);
556 MessageLoop::current()->RunAllPending();
557 EXPECT_EQ(quota::kQuotaStatusOk, status());
558
559 EXPECT_EQ(0, GetOriginUsage(
560 quota_client.get(), "http://foo.com/", kTemporary));
561 EXPECT_EQ(0, GetOriginUsage(
562 quota_client.get(), "http://bar.com/", kPersistent));
563 EXPECT_EQ(0, GetOriginUsage(
564 quota_client.get(), "http://buz.com/", kTemporary));
565
[email protected]ffac1ac42011-07-25 15:03:15566 EXPECT_EQ(2 + file_paths_cost_temporary_foo_https,
[email protected]4b66b652011-05-17 03:31:31567 GetOriginUsage(quota_client.get(),
568 "https://foo.com/",
569 kTemporary));
[email protected]ffac1ac42011-07-25 15:03:15570 EXPECT_EQ(4 + file_paths_cost_persistent_foo,
[email protected]4b66b652011-05-17 03:31:31571 GetOriginUsage(quota_client.get(),
572 "http://foo.com/",
573 kPersistent));
[email protected]ffac1ac42011-07-25 15:03:15574 EXPECT_EQ(8 + file_paths_cost_temporary_bar,
[email protected]4b66b652011-05-17 03:31:31575 GetOriginUsage(quota_client.get(),
576 "http://bar.com/",
577 kTemporary));
[email protected]ffac1ac42011-07-25 15:03:15578 EXPECT_EQ(32 + file_paths_cost_persistent_bar_https,
[email protected]4b66b652011-05-17 03:31:31579 GetOriginUsage(quota_client.get(),
580 "https://bar.com/",
581 kPersistent));
[email protected]ffac1ac42011-07-25 15:03:15582 EXPECT_EQ(64 + file_paths_cost_temporary_bar_https,
[email protected]4b66b652011-05-17 03:31:31583 GetOriginUsage(quota_client.get(),
584 "https://bar.com/",
585 kTemporary));
586}
587
[email protected]4ce4e79d2011-05-13 16:27:15588} // namespace fileapi