OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "content/public/test/async_file_test_helper.h" | 11 #include "content/public/test/async_file_test_helper.h" |
12 #include "content/public/test/test_file_system_context.h" | 12 #include "content/public/test/test_file_system_context.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 #include "webkit/browser/fileapi/file_system_context.h" | 15 #include "webkit/browser/fileapi/file_system_context.h" |
16 #include "webkit/browser/fileapi/file_system_quota_client.h" | 16 #include "webkit/browser/fileapi/file_system_quota_client.h" |
17 #include "webkit/browser/fileapi/file_system_usage_cache.h" | 17 #include "webkit/browser/fileapi/file_system_usage_cache.h" |
18 #include "webkit/browser/fileapi/obfuscated_file_util.h" | 18 #include "webkit/browser/fileapi/obfuscated_file_util.h" |
19 #include "webkit/common/fileapi/file_system_types.h" | 19 #include "webkit/common/fileapi/file_system_types.h" |
20 #include "webkit/common/fileapi/file_system_util.h" | 20 #include "webkit/common/fileapi/file_system_util.h" |
21 #include "webkit/common/quota/quota_types.h" | 21 #include "webkit/common/quota/quota_types.h" |
22 | 22 |
23 using content::AsyncFileTestHelper; | 23 using content::AsyncFileTestHelper; |
24 using fileapi::FileSystemQuotaClient; | 24 using storage::FileSystemQuotaClient; |
25 using fileapi::FileSystemURL; | 25 using storage::FileSystemURL; |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 namespace { | 28 namespace { |
29 | 29 |
30 const char kDummyURL1[] = "http://www.dummy.org"; | 30 const char kDummyURL1[] = "http://www.dummy.org"; |
31 const char kDummyURL2[] = "http://www.example.com"; | 31 const char kDummyURL2[] = "http://www.example.com"; |
32 const char kDummyURL3[] = "http://www.bleh"; | 32 const char kDummyURL3[] = "http://www.bleh"; |
33 | 33 |
34 // Declared to shorten the variable names. | 34 // Declared to shorten the variable names. |
35 const quota::StorageType kTemporary = quota::kStorageTypeTemporary; | 35 const storage::StorageType kTemporary = storage::kStorageTypeTemporary; |
36 const quota::StorageType kPersistent = quota::kStorageTypePersistent; | 36 const storage::StorageType kPersistent = storage::kStorageTypePersistent; |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 class FileSystemQuotaClientTest : public testing::Test { | 40 class FileSystemQuotaClientTest : public testing::Test { |
41 public: | 41 public: |
42 FileSystemQuotaClientTest() | 42 FileSystemQuotaClientTest() |
43 : weak_factory_(this), | 43 : weak_factory_(this), |
44 additional_callback_count_(0), | 44 additional_callback_count_(0), |
45 deletion_status_(quota::kQuotaStatusUnknown) { | 45 deletion_status_(storage::kQuotaStatusUnknown) {} |
46 } | |
47 | 46 |
48 virtual void SetUp() { | 47 virtual void SetUp() { |
49 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 48 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
50 file_system_context_ = CreateFileSystemContextForTesting( | 49 file_system_context_ = CreateFileSystemContextForTesting( |
51 NULL, data_dir_.path()); | 50 NULL, data_dir_.path()); |
52 } | 51 } |
53 | 52 |
54 struct TestFile { | 53 struct TestFile { |
55 bool isDirectory; | 54 bool isDirectory; |
56 const char* name; | 55 const char* name; |
57 int64 size; | 56 int64 size; |
58 const char* origin_url; | 57 const char* origin_url; |
59 quota::StorageType type; | 58 storage::StorageType type; |
60 }; | 59 }; |
61 | 60 |
62 protected: | 61 protected: |
63 FileSystemQuotaClient* NewQuotaClient(bool is_incognito) { | 62 FileSystemQuotaClient* NewQuotaClient(bool is_incognito) { |
64 return new FileSystemQuotaClient(file_system_context_.get(), is_incognito); | 63 return new FileSystemQuotaClient(file_system_context_.get(), is_incognito); |
65 } | 64 } |
66 | 65 |
67 void GetOriginUsageAsync(FileSystemQuotaClient* quota_client, | 66 void GetOriginUsageAsync(FileSystemQuotaClient* quota_client, |
68 const std::string& origin_url, | 67 const std::string& origin_url, |
69 quota::StorageType type) { | 68 storage::StorageType type) { |
70 quota_client->GetOriginUsage( | 69 quota_client->GetOriginUsage( |
71 GURL(origin_url), type, | 70 GURL(origin_url), type, |
72 base::Bind(&FileSystemQuotaClientTest::OnGetUsage, | 71 base::Bind(&FileSystemQuotaClientTest::OnGetUsage, |
73 weak_factory_.GetWeakPtr())); | 72 weak_factory_.GetWeakPtr())); |
74 } | 73 } |
75 | 74 |
76 int64 GetOriginUsage(FileSystemQuotaClient* quota_client, | 75 int64 GetOriginUsage(FileSystemQuotaClient* quota_client, |
77 const std::string& origin_url, | 76 const std::string& origin_url, |
78 quota::StorageType type) { | 77 storage::StorageType type) { |
79 GetOriginUsageAsync(quota_client, origin_url, type); | 78 GetOriginUsageAsync(quota_client, origin_url, type); |
80 base::RunLoop().RunUntilIdle(); | 79 base::RunLoop().RunUntilIdle(); |
81 return usage_; | 80 return usage_; |
82 } | 81 } |
83 | 82 |
84 const std::set<GURL>& GetOriginsForType( | 83 const std::set<GURL>& GetOriginsForType(FileSystemQuotaClient* quota_client, |
85 FileSystemQuotaClient* quota_client, | 84 storage::StorageType type) { |
86 quota::StorageType type) { | |
87 origins_.clear(); | 85 origins_.clear(); |
88 quota_client->GetOriginsForType( | 86 quota_client->GetOriginsForType( |
89 type, | 87 type, |
90 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins, | 88 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins, |
91 weak_factory_.GetWeakPtr())); | 89 weak_factory_.GetWeakPtr())); |
92 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
93 return origins_; | 91 return origins_; |
94 } | 92 } |
95 | 93 |
96 const std::set<GURL>& GetOriginsForHost( | 94 const std::set<GURL>& GetOriginsForHost(FileSystemQuotaClient* quota_client, |
97 FileSystemQuotaClient* quota_client, | 95 storage::StorageType type, |
98 quota::StorageType type, | 96 const std::string& host) { |
99 const std::string& host) { | |
100 origins_.clear(); | 97 origins_.clear(); |
101 quota_client->GetOriginsForHost( | 98 quota_client->GetOriginsForHost( |
102 type, host, | 99 type, host, |
103 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins, | 100 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins, |
104 weak_factory_.GetWeakPtr())); | 101 weak_factory_.GetWeakPtr())); |
105 base::RunLoop().RunUntilIdle(); | 102 base::RunLoop().RunUntilIdle(); |
106 return origins_; | 103 return origins_; |
107 } | 104 } |
108 | 105 |
109 void RunAdditionalOriginUsageTask( | 106 void RunAdditionalOriginUsageTask(FileSystemQuotaClient* quota_client, |
110 FileSystemQuotaClient* quota_client, | 107 const std::string& origin_url, |
111 const std::string& origin_url, | 108 storage::StorageType type) { |
112 quota::StorageType type) { | |
113 quota_client->GetOriginUsage( | 109 quota_client->GetOriginUsage( |
114 GURL(origin_url), type, | 110 GURL(origin_url), type, |
115 base::Bind(&FileSystemQuotaClientTest::OnGetAdditionalUsage, | 111 base::Bind(&FileSystemQuotaClientTest::OnGetAdditionalUsage, |
116 weak_factory_.GetWeakPtr())); | 112 weak_factory_.GetWeakPtr())); |
117 } | 113 } |
118 | 114 |
119 bool CreateFileSystemDirectory(const base::FilePath& file_path, | 115 bool CreateFileSystemDirectory(const base::FilePath& file_path, |
120 const std::string& origin_url, | 116 const std::string& origin_url, |
121 quota::StorageType storage_type) { | 117 storage::StorageType storage_type) { |
122 fileapi::FileSystemType type = | 118 storage::FileSystemType type = |
123 fileapi::QuotaStorageTypeToFileSystemType(storage_type); | 119 storage::QuotaStorageTypeToFileSystemType(storage_type); |
124 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 120 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
125 GURL(origin_url), type, file_path); | 121 GURL(origin_url), type, file_path); |
126 | 122 |
127 base::File::Error result = | 123 base::File::Error result = |
128 AsyncFileTestHelper::CreateDirectory(file_system_context_, url); | 124 AsyncFileTestHelper::CreateDirectory(file_system_context_, url); |
129 return result == base::File::FILE_OK; | 125 return result == base::File::FILE_OK; |
130 } | 126 } |
131 | 127 |
132 bool CreateFileSystemFile(const base::FilePath& file_path, | 128 bool CreateFileSystemFile(const base::FilePath& file_path, |
133 int64 file_size, | 129 int64 file_size, |
134 const std::string& origin_url, | 130 const std::string& origin_url, |
135 quota::StorageType storage_type) { | 131 storage::StorageType storage_type) { |
136 if (file_path.empty()) | 132 if (file_path.empty()) |
137 return false; | 133 return false; |
138 | 134 |
139 fileapi::FileSystemType type = | 135 storage::FileSystemType type = |
140 fileapi::QuotaStorageTypeToFileSystemType(storage_type); | 136 storage::QuotaStorageTypeToFileSystemType(storage_type); |
141 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 137 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
142 GURL(origin_url), type, file_path); | 138 GURL(origin_url), type, file_path); |
143 | 139 |
144 base::File::Error result = | 140 base::File::Error result = |
145 AsyncFileTestHelper::CreateFile(file_system_context_, url); | 141 AsyncFileTestHelper::CreateFile(file_system_context_, url); |
146 if (result != base::File::FILE_OK) | 142 if (result != base::File::FILE_OK) |
147 return false; | 143 return false; |
148 | 144 |
149 result = AsyncFileTestHelper::TruncateFile( | 145 result = AsyncFileTestHelper::TruncateFile( |
150 file_system_context_, url, file_size); | 146 file_system_context_, url, file_size); |
(...skipping 24 matching lines...) Expand all Loading... |
175 } | 171 } |
176 } | 172 } |
177 | 173 |
178 // This is a bit fragile--it depends on the test data always creating a | 174 // This is a bit fragile--it depends on the test data always creating a |
179 // directory before adding a file or directory to it, so that we can just | 175 // directory before adding a file or directory to it, so that we can just |
180 // count the basename of each addition. A recursive creation of a path, which | 176 // count the basename of each addition. A recursive creation of a path, which |
181 // created more than one directory in a single shot, would break this. | 177 // created more than one directory in a single shot, would break this. |
182 int64 ComputeFilePathsCostForOriginAndType(const TestFile* files, | 178 int64 ComputeFilePathsCostForOriginAndType(const TestFile* files, |
183 int num_files, | 179 int num_files, |
184 const std::string& origin_url, | 180 const std::string& origin_url, |
185 quota::StorageType type) { | 181 storage::StorageType type) { |
186 int64 file_paths_cost = 0; | 182 int64 file_paths_cost = 0; |
187 for (int i = 0; i < num_files; i++) { | 183 for (int i = 0; i < num_files; i++) { |
188 if (files[i].type == type && | 184 if (files[i].type == type && |
189 GURL(files[i].origin_url) == GURL(origin_url)) { | 185 GURL(files[i].origin_url) == GURL(origin_url)) { |
190 base::FilePath path = base::FilePath().AppendASCII(files[i].name); | 186 base::FilePath path = base::FilePath().AppendASCII(files[i].name); |
191 if (!path.empty()) { | 187 if (!path.empty()) { |
192 file_paths_cost += fileapi::ObfuscatedFileUtil::ComputeFilePathCost( | 188 file_paths_cost += |
193 path); | 189 storage::ObfuscatedFileUtil::ComputeFilePathCost(path); |
194 } | 190 } |
195 } | 191 } |
196 } | 192 } |
197 return file_paths_cost; | 193 return file_paths_cost; |
198 } | 194 } |
199 | 195 |
200 void DeleteOriginData(FileSystemQuotaClient* quota_client, | 196 void DeleteOriginData(FileSystemQuotaClient* quota_client, |
201 const std::string& origin, | 197 const std::string& origin, |
202 quota::StorageType type) { | 198 storage::StorageType type) { |
203 deletion_status_ = quota::kQuotaStatusUnknown; | 199 deletion_status_ = storage::kQuotaStatusUnknown; |
204 quota_client->DeleteOriginData( | 200 quota_client->DeleteOriginData( |
205 GURL(origin), type, | 201 GURL(origin), type, |
206 base::Bind(&FileSystemQuotaClientTest::OnDeleteOrigin, | 202 base::Bind(&FileSystemQuotaClientTest::OnDeleteOrigin, |
207 weak_factory_.GetWeakPtr())); | 203 weak_factory_.GetWeakPtr())); |
208 } | 204 } |
209 | 205 |
210 int64 usage() const { return usage_; } | 206 int64 usage() const { return usage_; } |
211 quota::QuotaStatusCode status() { return deletion_status_; } | 207 storage::QuotaStatusCode status() { return deletion_status_; } |
212 int additional_callback_count() const { return additional_callback_count_; } | 208 int additional_callback_count() const { return additional_callback_count_; } |
213 void set_additional_callback_count(int count) { | 209 void set_additional_callback_count(int count) { |
214 additional_callback_count_ = count; | 210 additional_callback_count_ = count; |
215 } | 211 } |
216 | 212 |
217 private: | 213 private: |
218 void OnGetUsage(int64 usage) { | 214 void OnGetUsage(int64 usage) { |
219 usage_ = usage; | 215 usage_ = usage; |
220 } | 216 } |
221 | 217 |
222 void OnGetOrigins(const std::set<GURL>& origins) { | 218 void OnGetOrigins(const std::set<GURL>& origins) { |
223 origins_ = origins; | 219 origins_ = origins; |
224 } | 220 } |
225 | 221 |
226 void OnGetAdditionalUsage(int64 usage_unused) { | 222 void OnGetAdditionalUsage(int64 usage_unused) { |
227 ++additional_callback_count_; | 223 ++additional_callback_count_; |
228 } | 224 } |
229 | 225 |
230 void OnDeleteOrigin(quota::QuotaStatusCode status) { | 226 void OnDeleteOrigin(storage::QuotaStatusCode status) { |
231 deletion_status_ = status; | 227 deletion_status_ = status; |
232 } | 228 } |
233 | 229 |
234 base::ScopedTempDir data_dir_; | 230 base::ScopedTempDir data_dir_; |
235 base::MessageLoop message_loop_; | 231 base::MessageLoop message_loop_; |
236 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 232 scoped_refptr<storage::FileSystemContext> file_system_context_; |
237 base::WeakPtrFactory<FileSystemQuotaClientTest> weak_factory_; | 233 base::WeakPtrFactory<FileSystemQuotaClientTest> weak_factory_; |
238 int64 usage_; | 234 int64 usage_; |
239 int additional_callback_count_; | 235 int additional_callback_count_; |
240 std::set<GURL> origins_; | 236 std::set<GURL> origins_; |
241 quota::QuotaStatusCode deletion_status_; | 237 storage::QuotaStatusCode deletion_status_; |
242 | 238 |
243 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaClientTest); | 239 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaClientTest); |
244 }; | 240 }; |
245 | 241 |
246 TEST_F(FileSystemQuotaClientTest, NoFileSystemTest) { | 242 TEST_F(FileSystemQuotaClientTest, NoFileSystemTest) { |
247 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | 243 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); |
248 | 244 |
249 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | 245 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); |
250 } | 246 } |
251 | 247 |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 "http://bar.com/", kTemporary); | 517 "http://bar.com/", kTemporary); |
522 const int64 file_paths_cost_temporary_bar_https = | 518 const int64 file_paths_cost_temporary_bar_https = |
523 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | 519 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), |
524 "https://bar.com/", kTemporary); | 520 "https://bar.com/", kTemporary); |
525 const int64 file_paths_cost_persistent_bar_https = | 521 const int64 file_paths_cost_persistent_bar_https = |
526 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | 522 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), |
527 "https://bar.com/", kPersistent); | 523 "https://bar.com/", kPersistent); |
528 | 524 |
529 DeleteOriginData(quota_client.get(), "http://foo.com/", kTemporary); | 525 DeleteOriginData(quota_client.get(), "http://foo.com/", kTemporary); |
530 base::RunLoop().RunUntilIdle(); | 526 base::RunLoop().RunUntilIdle(); |
531 EXPECT_EQ(quota::kQuotaStatusOk, status()); | 527 EXPECT_EQ(storage::kQuotaStatusOk, status()); |
532 | 528 |
533 DeleteOriginData(quota_client.get(), "http://bar.com/", kPersistent); | 529 DeleteOriginData(quota_client.get(), "http://bar.com/", kPersistent); |
534 base::RunLoop().RunUntilIdle(); | 530 base::RunLoop().RunUntilIdle(); |
535 EXPECT_EQ(quota::kQuotaStatusOk, status()); | 531 EXPECT_EQ(storage::kQuotaStatusOk, status()); |
536 | 532 |
537 DeleteOriginData(quota_client.get(), "http://buz.com/", kTemporary); | 533 DeleteOriginData(quota_client.get(), "http://buz.com/", kTemporary); |
538 base::RunLoop().RunUntilIdle(); | 534 base::RunLoop().RunUntilIdle(); |
539 EXPECT_EQ(quota::kQuotaStatusOk, status()); | 535 EXPECT_EQ(storage::kQuotaStatusOk, status()); |
540 | 536 |
541 EXPECT_EQ(0, GetOriginUsage( | 537 EXPECT_EQ(0, GetOriginUsage( |
542 quota_client.get(), "http://foo.com/", kTemporary)); | 538 quota_client.get(), "http://foo.com/", kTemporary)); |
543 EXPECT_EQ(0, GetOriginUsage( | 539 EXPECT_EQ(0, GetOriginUsage( |
544 quota_client.get(), "http://bar.com/", kPersistent)); | 540 quota_client.get(), "http://bar.com/", kPersistent)); |
545 EXPECT_EQ(0, GetOriginUsage( | 541 EXPECT_EQ(0, GetOriginUsage( |
546 quota_client.get(), "http://buz.com/", kTemporary)); | 542 quota_client.get(), "http://buz.com/", kTemporary)); |
547 | 543 |
548 EXPECT_EQ(2 + file_paths_cost_temporary_foo_https, | 544 EXPECT_EQ(2 + file_paths_cost_temporary_foo_https, |
549 GetOriginUsage(quota_client.get(), | 545 GetOriginUsage(quota_client.get(), |
(...skipping 11 matching lines...) Expand all Loading... |
561 GetOriginUsage(quota_client.get(), | 557 GetOriginUsage(quota_client.get(), |
562 "https://bar.com/", | 558 "https://bar.com/", |
563 kPersistent)); | 559 kPersistent)); |
564 EXPECT_EQ(64 + file_paths_cost_temporary_bar_https, | 560 EXPECT_EQ(64 + file_paths_cost_temporary_bar_https, |
565 GetOriginUsage(quota_client.get(), | 561 GetOriginUsage(quota_client.get(), |
566 "https://bar.com/", | 562 "https://bar.com/", |
567 kTemporary)); | 563 kTemporary)); |
568 } | 564 } |
569 | 565 |
570 } // namespace content | 566 } // namespace content |
OLD | NEW |