blob: a293bec724a58f19b1aa14c7cc56d33fa6c0d332 [file] [log] [blame]
[email protected]d77ecbf62013-10-02 06:43:081// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]d4905e2e2011-05-13 21:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <set>
6#include <string>
[email protected]403ada82013-01-08 07:51:397#include <vector>
[email protected]d4905e2e2011-05-13 21:56:328
[email protected]4d99be52011-10-18 14:11:039#include "base/bind.h"
[email protected]d4905e2e2011-05-13 21:56:3210#include "base/file_util.h"
[email protected]141bcc52014-01-27 21:36:0011#include "base/files/file.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2313#include "base/files/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3214#include "base/memory/scoped_ptr.h"
[email protected]d4905e2e2011-05-13 21:56:3215#include "base/platform_file.h"
[email protected]f1edfb7e2013-09-13 16:45:1716#include "base/run_loop.h"
[email protected]d77ecbf62013-10-02 06:43:0817#include "content/public/test/sandbox_file_system_test_helper.h"
18#include "content/public/test/test_file_system_context.h"
[email protected]d4905e2e2011-05-13 21:56:3219#include "testing/gtest/include/gtest/gtest.h"
[email protected]c6f9203a2013-05-28 02:08:0720#include "webkit/browser/fileapi/async_file_test_helper.h"
[email protected]f25e1132013-05-24 13:58:0421#include "webkit/browser/fileapi/external_mount_points.h"
[email protected]f19bbf62013-07-09 01:22:3222#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:0723#include "webkit/browser/fileapi/file_system_context.h"
[email protected]c6f9203a2013-05-28 02:08:0724#include "webkit/browser/fileapi/file_system_operation_context.h"
[email protected]c814b5692013-05-20 11:37:4625#include "webkit/browser/fileapi/file_system_usage_cache.h"
[email protected]f25e1132013-05-24 13:58:0426#include "webkit/browser/fileapi/mock_file_change_observer.h"
[email protected]28f051c32013-05-21 05:15:2627#include "webkit/browser/fileapi/obfuscated_file_util.h"
[email protected]501fd0e2013-06-24 12:51:0128#include "webkit/browser/fileapi/sandbox_directory_database.h"
[email protected]c19e2ff2013-10-19 18:15:1029#include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h"
[email protected]4d28164b2013-07-28 08:56:1430#include "webkit/browser/fileapi/sandbox_isolated_origin_database.h"
[email protected]17258682013-06-05 13:38:4031#include "webkit/browser/fileapi/sandbox_origin_database.h"
[email protected]c6f9203a2013-05-28 02:08:0732#include "webkit/browser/fileapi/test_file_set.h"
[email protected]7660ec92013-05-30 05:12:3933#include "webkit/browser/quota/mock_special_storage_policy.h"
34#include "webkit/browser/quota/quota_manager.h"
[email protected]4d28164b2013-07-28 08:56:1435#include "webkit/common/database/database_identifier.h"
[email protected]7660ec92013-05-30 05:12:3936#include "webkit/common/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3237
[email protected]7ade66ee2014-01-16 17:35:5338using fileapi::AsyncFileTestHelper;
39using fileapi::FileSystemContext;
40using fileapi::FileSystemOperation;
41using fileapi::FileSystemOperationContext;
42using fileapi::FileSystemType;
43using fileapi::FileSystemURL;
44using fileapi::ObfuscatedFileUtil;
45using fileapi::SandboxDirectoryDatabase;
46using fileapi::SandboxIsolatedOriginDatabase;
47using fileapi::kFileSystemTypeTemporary;
48using fileapi::kFileSystemTypePersistent;
49
50namespace content {
[email protected]d4905e2e2011-05-13 21:56:3251
52namespace {
53
[email protected]a3ef4832013-02-02 05:12:3354bool FileExists(const base::FilePath& path) {
[email protected]dcd16612013-07-15 20:18:0955 return base::PathExists(path) && !base::DirectoryExists(path);
[email protected]d4905e2e2011-05-13 21:56:3256}
57
[email protected]a3ef4832013-02-02 05:12:3358int64 GetSize(const base::FilePath& path) {
[email protected]81b7f662011-05-26 00:54:4659 int64 size;
[email protected]56285702013-12-04 18:22:4960 EXPECT_TRUE(base::GetFileSize(path, &size));
[email protected]81b7f662011-05-26 00:54:4661 return size;
62}
63
[email protected]d4905e2e2011-05-13 21:56:3264// After a move, the dest exists and the source doesn't.
65// After a copy, both source and dest exist.
66struct CopyMoveTestCaseRecord {
67 bool is_copy_not_move;
68 const char source_path[64];
69 const char dest_path[64];
70 bool cause_overwrite;
71};
72
73const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
74 // This is the combinatoric set of:
75 // rename vs. same-name
76 // different directory vs. same directory
77 // overwrite vs. no-overwrite
78 // copy vs. move
79 // We can never be called with source and destination paths identical, so
80 // those cases are omitted.
81 {true, "dir0/file0", "dir0/file1", false},
82 {false, "dir0/file0", "dir0/file1", false},
83 {true, "dir0/file0", "dir0/file1", true},
84 {false, "dir0/file0", "dir0/file1", true},
85
86 {true, "dir0/file0", "dir1/file0", false},
87 {false, "dir0/file0", "dir1/file0", false},
88 {true, "dir0/file0", "dir1/file0", true},
89 {false, "dir0/file0", "dir1/file0", true},
90 {true, "dir0/file0", "dir1/file1", false},
91 {false, "dir0/file0", "dir1/file1", false},
92 {true, "dir0/file0", "dir1/file1", true},
93 {false, "dir0/file0", "dir1/file1", true},
94};
95
[email protected]fcc2d5f2011-05-23 22:06:2696struct OriginEnumerationTestRecord {
97 std::string origin_url;
98 bool has_temporary;
99 bool has_persistent;
100};
101
102const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
103 {"http://example.com", false, true},
104 {"http://example1.com", true, false},
105 {"https://example1.com", true, true},
106 {"file://", false, true},
107 {"http://example.com:8000", false, true},
108};
109
[email protected]04c899f2013-02-08 08:28:42110FileSystemURL FileSystemURLAppend(
[email protected]023ad6ab2013-02-17 05:07:23111 const FileSystemURL& url, const base::FilePath::StringType& child) {
[email protected]04c899f2013-02-08 08:28:42112 return FileSystemURL::CreateForTest(
113 url.origin(), url.mount_type(), url.virtual_path().Append(child));
114}
115
116FileSystemURL FileSystemURLAppendUTF8(
117 const FileSystemURL& url, const std::string& child) {
118 return FileSystemURL::CreateForTest(
119 url.origin(),
120 url.mount_type(),
[email protected]023ad6ab2013-02-17 05:07:23121 url.virtual_path().Append(base::FilePath::FromUTF8Unsafe(child)));
[email protected]04c899f2013-02-08 08:28:42122}
123
124FileSystemURL FileSystemURLDirName(const FileSystemURL& url) {
125 return FileSystemURL::CreateForTest(
[email protected]7ade66ee2014-01-16 17:35:53126 url.origin(), url.mount_type(),
127 fileapi::VirtualPath::DirName(url.virtual_path()));
[email protected]04c899f2013-02-08 08:28:42128}
129
[email protected]c19e2ff2013-10-19 18:15:10130std::string GetTypeString(FileSystemType type) {
[email protected]7ade66ee2014-01-16 17:35:53131 return fileapi::SandboxFileSystemBackendDelegate::GetTypeString(type);
[email protected]c19e2ff2013-10-19 18:15:10132}
133
134bool HasFileSystemType(
135 ObfuscatedFileUtil::AbstractOriginEnumerator* enumerator,
136 FileSystemType type) {
137 return enumerator->HasTypeDirectory(GetTypeString(type));
138}
139
[email protected]343eac92013-10-11 06:14:26140} // namespace
[email protected]d4905e2e2011-05-13 21:56:32141
142// TODO(ericu): The vast majority of this and the other FSFU subclass tests
143// could theoretically be shared. It would basically be a FSFU interface
144// compliance test, and only the subclass-specific bits that look into the
145// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49146class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32147 public:
[email protected]7878ece2011-09-05 11:41:49148 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26149 : origin_(GURL("http://www.example.com")),
[email protected]7ade66ee2014-01-16 17:35:53150 type_(fileapi::kFileSystemTypeTemporary),
[email protected]e0b9dda2013-04-30 01:05:43151 weak_factory_(this),
[email protected]1c98fdd2013-05-24 09:45:27152 sandbox_file_system_(origin_, type_),
[email protected]0c5ebe32011-08-19 22:37:16153 quota_status_(quota::kQuotaStatusUnknown),
154 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32155 }
156
[email protected]7fd8fa4f2013-02-07 05:43:50157 virtual void SetUp() {
[email protected]d4905e2e2011-05-13 21:56:32158 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
159
[email protected]501fd0e2013-06-24 12:51:01160 storage_policy_ = new quota::MockSpecialStoragePolicy();
[email protected]e7e46732012-01-05 11:45:55161
[email protected]cadac622013-06-11 16:46:36162 quota_manager_ =
163 new quota::QuotaManager(false /* is_incognito */,
164 data_dir_.path(),
165 base::MessageLoopProxy::current().get(),
166 base::MessageLoopProxy::current().get(),
[email protected]501fd0e2013-06-24 12:51:01167 storage_policy_.get());
[email protected]0c5ebe32011-08-19 22:37:16168
[email protected]1c98fdd2013-05-24 09:45:27169 // Every time we create a new sandbox_file_system helper,
170 // it creates another context, which creates another path manager,
[email protected]534173b52013-07-25 12:21:13171 // another sandbox_backend, and another OFU.
172 // We need to pass in the context to skip all that.
[email protected]420fb562013-04-18 01:46:34173 file_system_context_ = CreateFileSystemContextForTesting(
[email protected]0c5ebe32011-08-19 22:37:16174 quota_manager_->proxy(),
[email protected]420fb562013-04-18 01:46:34175 data_dir_.path());
[email protected]0c5ebe32011-08-19 22:37:16176
[email protected]1c98fdd2013-05-24 09:45:27177 sandbox_file_system_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10178
[email protected]7ade66ee2014-01-16 17:35:53179 change_observers_ = fileapi::MockFileChangeObserver::CreateList(
180 &change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32181 }
182
[email protected]7fd8fa4f2013-02-07 05:43:50183 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55184 quota_manager_ = NULL;
[email protected]1c98fdd2013-05-24 09:45:27185 sandbox_file_system_.TearDown();
[email protected]e7e46732012-01-05 11:45:55186 }
187
[email protected]294dd0312012-05-11 07:35:13188 scoped_ptr<FileSystemOperationContext> LimitedContext(
189 int64 allowed_bytes_growth) {
190 scoped_ptr<FileSystemOperationContext> context(
[email protected]1c98fdd2013-05-24 09:45:27191 sandbox_file_system_.NewOperationContext());
[email protected]294dd0312012-05-11 07:35:13192 context->set_allowed_bytes_growth(allowed_bytes_growth);
193 return context.Pass();
194 }
195
196 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
197 return LimitedContext(kint64max);
198 }
199
[email protected]02a60542012-07-24 20:05:33200 FileSystemOperationContext* NewContext(
[email protected]1c98fdd2013-05-24 09:45:27201 SandboxFileSystemTestHelper* file_system) {
[email protected]c4e6f9c2012-09-09 17:42:10202 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16203 FileSystemOperationContext* context;
[email protected]1c98fdd2013-05-24 09:45:27204 if (file_system)
205 context = file_system->NewOperationContext();
[email protected]0c5ebe32011-08-19 22:37:16206 else
[email protected]1c98fdd2013-05-24 09:45:27207 context = sandbox_file_system_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46208 // Setting allowed_bytes_growth big enough for all tests.
209 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10210 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32211 return context;
212 }
213
[email protected]7ade66ee2014-01-16 17:35:53214 const fileapi::ChangeObserverList& change_observers() const {
[email protected]c4e6f9c2012-09-09 17:42:10215 return change_observers_;
216 }
217
[email protected]7ade66ee2014-01-16 17:35:53218 fileapi::MockFileChangeObserver* change_observer() {
[email protected]c4e6f9c2012-09-09 17:42:10219 return &change_observer_;
220 }
221
[email protected]0c5ebe32011-08-19 22:37:16222 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49223 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16224 // Use this for tests which need to run in multiple origins; we need a test
225 // helper per origin.
[email protected]1c98fdd2013-05-24 09:45:27226 SandboxFileSystemTestHelper* NewFileSystem(
[email protected]0c5ebe32011-08-19 22:37:16227 const GURL& origin, fileapi::FileSystemType type) {
[email protected]1c98fdd2013-05-24 09:45:27228 SandboxFileSystemTestHelper* file_system =
229 new SandboxFileSystemTestHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16230
[email protected]1c98fdd2013-05-24 09:45:27231 file_system->SetUp(file_system_context_.get());
232 return file_system;
[email protected]0c5ebe32011-08-19 22:37:16233 }
234
[email protected]7ade66ee2014-01-16 17:35:53235 scoped_ptr<ObfuscatedFileUtil> CreateObfuscatedFileUtil(
236 quota::SpecialStoragePolicy* storage_policy) {
237 return scoped_ptr<ObfuscatedFileUtil>(
238 ObfuscatedFileUtil::CreateForTesting(
239 storage_policy, data_dir_path(),
240 base::MessageLoopProxy::current().get()));
241 }
242
[email protected]7878ece2011-09-05 11:41:49243 ObfuscatedFileUtil* ofu() {
[email protected]1c98fdd2013-05-24 09:45:27244 return static_cast<ObfuscatedFileUtil*>(sandbox_file_system_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32245 }
246
[email protected]a3ef4832013-02-02 05:12:33247 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35248 return data_dir_.path();
249 }
250
[email protected]0c5ebe32011-08-19 22:37:16251 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26252 return origin_;
253 }
254
255 fileapi::FileSystemType type() const {
256 return type_;
257 }
258
[email protected]c19e2ff2013-10-19 18:15:10259 std::string type_string() const {
260 return GetTypeString(type_);
261 }
262
[email protected]294dd0312012-05-11 07:35:13263 int64 ComputeTotalFileSize() {
[email protected]1c98fdd2013-05-24 09:45:27264 return sandbox_file_system_.ComputeCurrentOriginUsage() -
265 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]294dd0312012-05-11 07:35:13266 }
267
[email protected]0c5ebe32011-08-19 22:37:16268 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30269 int64 quota = -1;
[email protected]ff875be52013-06-02 23:47:38270 quota_status_ =
271 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
272 origin(),
273 sandbox_file_system_.type(),
274 &usage_,
275 &quota);
[email protected]0c5ebe32011-08-19 22:37:16276 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
277 }
278
279 void RevokeUsageCache() {
[email protected]1c98fdd2013-05-24 09:45:27280 quota_manager_->ResetUsageTracker(sandbox_file_system_.storage_type());
281 usage_cache()->Delete(sandbox_file_system_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26282 }
283
284 int64 SizeByQuotaUtil() {
[email protected]1c98fdd2013-05-24 09:45:27285 return sandbox_file_system_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16286 }
287
288 int64 SizeInUsageFile() {
[email protected]f1edfb7e2013-09-13 16:45:17289 base::RunLoop().RunUntilIdle();
[email protected]bf97e8b2013-04-14 15:00:13290 int64 usage = 0;
[email protected]1c98fdd2013-05-24 09:45:27291 return usage_cache()->GetUsage(
292 sandbox_file_system_.GetUsageCachePath(), &usage) ? usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16293 }
294
[email protected]13f92f6e2012-08-13 07:39:14295 bool PathExists(const FileSystemURL& url) {
296 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00297 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:33298 base::FilePath platform_path;
[email protected]141bcc52014-01-27 21:36:00299 base::File::Error error = ofu()->GetFileInfo(
[email protected]4e7e3882013-01-17 04:14:21300 context.get(), url, &file_info, &platform_path);
[email protected]141bcc52014-01-27 21:36:00301 return error == base::File::FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14302 }
303
304 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30305 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14306 }
307
[email protected]0c5ebe32011-08-19 22:37:16308 int64 usage() const { return usage_; }
[email protected]7ade66ee2014-01-16 17:35:53309 fileapi::FileSystemUsageCache* usage_cache() {
[email protected]1c98fdd2013-05-24 09:45:27310 return sandbox_file_system_.usage_cache();
[email protected]06226172013-03-14 15:39:31311 }
[email protected]0c5ebe32011-08-19 22:37:16312
[email protected]949f25a2012-06-27 01:53:09313 FileSystemURL CreateURLFromUTF8(const std::string& path) {
[email protected]1c98fdd2013-05-24 09:45:27314 return sandbox_file_system_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50315 }
316
[email protected]949f25a2012-06-27 01:53:09317 int64 PathCost(const FileSystemURL& url) {
318 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13319 }
320
[email protected]a3ef4832013-02-02 05:12:33321 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]1c98fdd2013-05-24 09:45:27322 return sandbox_file_system_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50323 }
324
[email protected]d4905e2e2011-05-13 21:56:32325 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39326 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16327 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33328 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:00329 EXPECT_EQ(base::File::FILE_OK,
330 ofu()->GetLocalFilePath(context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32331
[email protected]141bcc52014-01-27 21:36:00332 base::File::Info file_info0;
[email protected]a3ef4832013-02-02 05:12:33333 base::FilePath data_path;
[email protected]141bcc52014-01-27 21:36:00334 EXPECT_EQ(base::File::FILE_OK,
335 ofu()->GetFileInfo(context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32336 EXPECT_EQ(data_path, local_path);
337 EXPECT_TRUE(FileExists(data_path));
338 EXPECT_EQ(0, GetSize(data_path));
339
340 const char data[] = "test data";
341 const int length = arraysize(data) - 1;
342
343 if (base::kInvalidPlatformFileValue == file_handle) {
[email protected]141bcc52014-01-27 21:36:00344 base::File file(data_path,
345 base::File::FLAG_OPEN | base::File::FLAG_WRITE);
346 ASSERT_TRUE(file.IsValid());
347 EXPECT_FALSE(file.created());
348 file_handle = file.TakePlatformFile();
[email protected]d4905e2e2011-05-13 21:56:32349 }
350 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
351 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
352
[email protected]141bcc52014-01-27 21:36:00353 base::File::Info file_info1;
[email protected]d4905e2e2011-05-13 21:56:32354 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16355 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00356 EXPECT_EQ(base::File::FILE_OK,
357 ofu()->GetFileInfo(context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32358 EXPECT_EQ(data_path, local_path);
359
360 EXPECT_FALSE(file_info0.is_directory);
361 EXPECT_FALSE(file_info1.is_directory);
362 EXPECT_FALSE(file_info0.is_symbolic_link);
363 EXPECT_FALSE(file_info1.is_symbolic_link);
364 EXPECT_EQ(0, file_info0.size);
365 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32366 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32367
[email protected]0c5ebe32011-08-19 22:37:16368 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00369 EXPECT_EQ(base::File::FILE_OK,
370 ofu()->Truncate(context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32371 EXPECT_EQ(length * 2, GetSize(data_path));
372
[email protected]0c5ebe32011-08-19 22:37:16373 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00374 EXPECT_EQ(base::File::FILE_OK,
375 ofu()->Truncate(context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16376 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32377 }
378
379 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09380 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33381 const std::set<base::FilePath::StringType>& files,
382 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32383 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33384 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32385 for (iter = files.begin(); iter != files.end(); ++iter) {
386 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16387 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00388 ASSERT_EQ(base::File::FILE_OK,
389 ofu()->EnsureFileExists(context.get(),
390 FileSystemURLAppend(root_url, *iter),
391 &created));
[email protected]d4905e2e2011-05-13 21:56:32392 ASSERT_FALSE(created);
393 }
394 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16395 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14396 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42397 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32398 }
399 }
400
[email protected]294dd0312012-05-11 07:35:13401 class UsageVerifyHelper {
402 public:
403 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]1c98fdd2013-05-24 09:45:27404 SandboxFileSystemTestHelper* file_system,
[email protected]294dd0312012-05-11 07:35:13405 int64 expected_usage)
406 : context_(context.Pass()),
[email protected]1c98fdd2013-05-24 09:45:27407 sandbox_file_system_(file_system),
[email protected]294dd0312012-05-11 07:35:13408 expected_usage_(expected_usage) {}
409
410 ~UsageVerifyHelper() {
[email protected]f1edfb7e2013-09-13 16:45:17411 base::RunLoop().RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13412 Check();
413 }
414
415 FileSystemOperationContext* context() {
416 return context_.get();
417 }
418
419 private:
420 void Check() {
421 ASSERT_EQ(expected_usage_,
[email protected]1c98fdd2013-05-24 09:45:27422 sandbox_file_system_->GetCachedOriginUsage());
[email protected]294dd0312012-05-11 07:35:13423 }
424
425 scoped_ptr<FileSystemOperationContext> context_;
[email protected]1c98fdd2013-05-24 09:45:27426 SandboxFileSystemTestHelper* sandbox_file_system_;
[email protected]294dd0312012-05-11 07:35:13427 int64 expected_usage_;
428 };
429
430 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27431 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13432 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
433 LimitedContext(requested_growth),
[email protected]1c98fdd2013-05-24 09:45:27434 &sandbox_file_system_, usage + requested_growth));
[email protected]294dd0312012-05-11 07:35:13435 }
436
437 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27438 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13439 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
[email protected]1c98fdd2013-05-24 09:45:27440 LimitedContext(requested_growth - 1), &sandbox_file_system_, usage));
[email protected]294dd0312012-05-11 07:35:13441 }
442
[email protected]d4905e2e2011-05-13 21:56:32443 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09444 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33445 std::set<base::FilePath::StringType>* files,
446 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32447 scoped_ptr<FileSystemOperationContext> context;
[email protected]7ade66ee2014-01-16 17:35:53448 std::vector<fileapi::DirectoryEntry> entries;
[email protected]141bcc52014-01-27 21:36:00449 EXPECT_EQ(base::File::FILE_OK,
450 AsyncFileTestHelper::ReadDirectory(file_system_context(),
451 root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32452 EXPECT_EQ(0UL, entries.size());
453
454 files->clear();
[email protected]89ee4272011-05-16 18:45:17455 files->insert(FILE_PATH_LITERAL("first"));
456 files->insert(FILE_PATH_LITERAL("second"));
457 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32458 directories->clear();
[email protected]89ee4272011-05-16 18:45:17459 directories->insert(FILE_PATH_LITERAL("fourth"));
460 directories->insert(FILE_PATH_LITERAL("fifth"));
461 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33462 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32463 for (iter = files->begin(); iter != files->end(); ++iter) {
464 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16465 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00466 ASSERT_EQ(base::File::FILE_OK,
467 ofu()->EnsureFileExists(context.get(),
468 FileSystemURLAppend(root_url, *iter),
469 &created));
[email protected]d4905e2e2011-05-13 21:56:32470 ASSERT_TRUE(created);
471 }
472 for (iter = directories->begin(); iter != directories->end(); ++iter) {
473 bool exclusive = true;
474 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16475 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00476 EXPECT_EQ(base::File::FILE_OK,
477 ofu()->CreateDirectory(context.get(),
478 FileSystemURLAppend(root_url, *iter),
479 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32480 }
[email protected]949f25a2012-06-27 01:53:09481 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32482 }
483
[email protected]949f25a2012-06-27 01:53:09484 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33485 std::set<base::FilePath::StringType> files;
486 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09487 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32488
489 scoped_ptr<FileSystemOperationContext> context;
[email protected]7ade66ee2014-01-16 17:35:53490 std::vector<fileapi::DirectoryEntry> entries;
[email protected]0c5ebe32011-08-19 22:37:16491 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00492 EXPECT_EQ(base::File::FILE_OK,
[email protected]07b64872013-02-13 11:46:30493 AsyncFileTestHelper::ReadDirectory(
494 file_system_context(), root_url, &entries));
[email protected]7ade66ee2014-01-16 17:35:53495 std::vector<fileapi::DirectoryEntry>::iterator entry_iter;
[email protected]d4905e2e2011-05-13 21:56:32496 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10497 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32498 for (entry_iter = entries.begin(); entry_iter != entries.end();
499 ++entry_iter) {
[email protected]7ade66ee2014-01-16 17:35:53500 const fileapi::DirectoryEntry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31501 std::set<base::FilePath::StringType>::iterator iter =
502 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32503 if (iter != files.end()) {
504 EXPECT_FALSE(entry.is_directory);
505 files.erase(iter);
506 continue;
507 }
[email protected]89ee4272011-05-16 18:45:17508 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32509 EXPECT_FALSE(directories.end() == iter);
510 EXPECT_TRUE(entry.is_directory);
511 directories.erase(iter);
512 }
513 }
514
[email protected]949f25a2012-06-27 01:53:09515 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41516 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32517 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16518
[email protected]2517cfa2011-08-25 05:12:41519 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00520 EXPECT_EQ(base::File::FILE_OK,
521 ofu()->Touch(context.get(), url, last_access_time,
522 last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10523 // Currently we fire no change notifications for Touch.
524 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33525 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:00526 base::File::Info file_info;
[email protected]0c5ebe32011-08-19 22:37:16527 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00528 EXPECT_EQ(base::File::FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09529 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32530 // We compare as time_t here to lower our resolution, to avoid false
531 // negatives caused by conversion to the local filesystem's native
532 // representation and back.
533 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
534
[email protected]0c5ebe32011-08-19 22:37:16535 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32536 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41537 last_access_time += base::TimeDelta::FromHours(14);
[email protected]141bcc52014-01-27 21:36:00538 EXPECT_EQ(base::File::FILE_OK,
539 ofu()->Touch(context.get(), url, last_access_time,
540 last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10541 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16542 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00543 EXPECT_EQ(base::File::FILE_OK,
544 ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32545 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49546 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41547 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32548 }
549
[email protected]81b7f662011-05-26 00:54:46550 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23551 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46552 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33553 base::FilePath root_file_path = source_dir.path();
554 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09555 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46556 int64 src_file_length = 87;
557
[email protected]141bcc52014-01-27 21:36:00558 base::File file(src_file_path,
559 base::File::FLAG_CREATE | base::File::FLAG_WRITE);
560 ASSERT_TRUE(file.IsValid());
561 EXPECT_TRUE(file.created());
562 ASSERT_TRUE(file.SetLength(src_file_length));
563 file.Close();
[email protected]81b7f662011-05-26 00:54:46564
565 scoped_ptr<FileSystemOperationContext> context;
566
567 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16568 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00569 bool created = false;
570 EXPECT_EQ(base::File::FILE_OK,
571 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46572 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10573
574 // We must have observed one (and only one) create_file_count.
575 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
576 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46577 }
578
[email protected]0c5ebe32011-08-19 22:37:16579 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09580 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16581 if (!overwrite) {
582 // Verify that file creation requires sufficient quota for the path.
583 context.reset(NewContext(NULL));
584 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
[email protected]141bcc52014-01-27 21:36:00585 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13586 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09587 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16588 }
589
590 context.reset(NewContext(NULL));
591 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]141bcc52014-01-27 21:36:00592 EXPECT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:13593 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09594 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16595
[email protected]13f92f6e2012-08-13 07:39:14596 EXPECT_TRUE(PathExists(dest_url));
597 EXPECT_FALSE(DirectoryExists(dest_url));
598
[email protected]0c5ebe32011-08-19 22:37:16599 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00600 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:33601 base::FilePath data_path;
[email protected]141bcc52014-01-27 21:36:00602 EXPECT_EQ(base::File::FILE_OK,
603 ofu()->GetFileInfo(context.get(), dest_url, &file_info,
604 &data_path));
[email protected]08f8feb2012-02-26 11:53:50605 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46606 EXPECT_TRUE(FileExists(data_path));
607 EXPECT_EQ(src_file_length, GetSize(data_path));
608
[email protected]141bcc52014-01-27 21:36:00609 EXPECT_EQ(base::File::FILE_OK,
610 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46611 }
612
[email protected]949f25a2012-06-27 01:53:09613 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03614 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00615 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:09616 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
617 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03618 }
619
[email protected]949f25a2012-06-27 01:53:09620 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03621 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33622 base::FilePath data_path;
[email protected]141bcc52014-01-27 21:36:00623 base::File::Info file_info;
[email protected]fad625e2f2011-12-08 05:38:03624 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00625 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:09626 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10627 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03628 return file_info.last_modified;
629 }
630
[email protected]949f25a2012-06-27 01:53:09631 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03632 bool copy,
633 bool overwrite) {
634 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42635 const FileSystemURL src_dir_url(
636 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
637 const FileSystemURL dest_dir_url(
638 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03639
[email protected]04c899f2013-02-08 08:28:42640 const FileSystemURL src_file_url(
641 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
642 const FileSystemURL dest_file_url(
643 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03644
645 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00646 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:09647 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03648 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00649 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:09650 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03651
652 bool created = false;
653 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00654 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:09655 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03656 if (overwrite) {
657 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00658 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:03659 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09660 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03661 }
662
[email protected]949f25a2012-06-27 01:53:09663 ClearTimestamp(src_dir_url);
664 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03665 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00666 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:03667 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09668 src_file_url, dest_file_url,
[email protected]824d3892013-09-25 13:00:30669 FileSystemOperation::OPTION_NONE,
[email protected]fad625e2f2011-12-08 05:38:03670 copy));
[email protected]fad625e2f2011-12-08 05:38:03671 if (copy)
[email protected]949f25a2012-06-27 01:53:09672 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03673 else
[email protected]949f25a2012-06-27 01:53:09674 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
675 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03676 }
677
[email protected]7ade66ee2014-01-16 17:35:53678 void MaybeDropDatabasesAliveCaseTestBody() {
679 scoped_ptr<ObfuscatedFileUtil> file_util = CreateObfuscatedFileUtil(NULL);
680 file_util->InitOriginDatabase(GURL(), true /*create*/);
681 ASSERT_TRUE(file_util->origin_database_ != NULL);
682
683 // Callback to Drop DB is called while ObfuscatedFileUtilTest is
684 // still alive.
685 file_util->db_flush_delay_seconds_ = 0;
686 file_util->MarkUsed();
687 base::RunLoop().RunUntilIdle();
688
689 ASSERT_TRUE(file_util->origin_database_ == NULL);
690 }
691
692 void MaybeDropDatabasesAlreadyDeletedCaseTestBody() {
693 // Run message loop after OFU is already deleted to make sure callback
694 // doesn't cause a crash for use after free.
695 {
696 scoped_ptr<ObfuscatedFileUtil> file_util = CreateObfuscatedFileUtil(NULL);
697 file_util->InitOriginDatabase(GURL(), true /*create*/);
698 file_util->db_flush_delay_seconds_ = 0;
699 file_util->MarkUsed();
700 }
701
702 // At this point the callback is still in the message queue but OFU is gone.
703 base::RunLoop().RunUntilIdle();
704 }
705
706 void DestroyDirectoryDatabase_IsolatedTestBody() {
707 storage_policy_->AddIsolated(origin_);
708 scoped_ptr<ObfuscatedFileUtil> file_util = CreateObfuscatedFileUtil(
709 storage_policy_.get());
710 const FileSystemURL url = FileSystemURL::CreateForTest(
711 origin_, kFileSystemTypePersistent, base::FilePath());
712
713 // Create DirectoryDatabase for isolated origin.
714 SandboxDirectoryDatabase* db =
715 file_util->GetDirectoryDatabase(url, true /* create */);
716 ASSERT_TRUE(db != NULL);
717
718 // Destory it.
719 ASSERT_TRUE(file_util->DestroyDirectoryDatabase(
720 url.origin(), GetTypeString(url.type())));
721 ASSERT_TRUE(file_util->directories_.empty());
722 }
723
724 void GetDirectoryDatabase_IsolatedTestBody() {
725 storage_policy_->AddIsolated(origin_);
726 scoped_ptr<ObfuscatedFileUtil> file_util = CreateObfuscatedFileUtil(
727 storage_policy_.get());
728 const FileSystemURL url = FileSystemURL::CreateForTest(
729 origin_, kFileSystemTypePersistent, base::FilePath());
730
731 // Create DirectoryDatabase for isolated origin.
732 SandboxDirectoryDatabase* db =
733 file_util->GetDirectoryDatabase(url, true /* create */);
734 ASSERT_TRUE(db != NULL);
735 ASSERT_EQ(1U, file_util->directories_.size());
736
737 // Remove isolated.
738 storage_policy_->RemoveIsolated(url.origin());
739
740 // This should still get the same database.
741 SandboxDirectoryDatabase* db2 =
742 file_util->GetDirectoryDatabase(url, false /* create */);
743 ASSERT_EQ(db, db2);
744 }
745
746 void MigrationBackFromIsolatedTestBody() {
747 std::string kFakeDirectoryData("0123456789");
748 base::FilePath old_directory_db_path;
749
750 // Initialize the directory with one origin using
751 // SandboxIsolatedOriginDatabase.
752 {
753 std::string origin_string =
754 webkit_database::GetIdentifierFromOrigin(origin_);
755 SandboxIsolatedOriginDatabase database_old(
756 origin_string, data_dir_path(),
757 base::FilePath(
758 SandboxIsolatedOriginDatabase::kObsoleteOriginDirectory));
759 base::FilePath path;
760 EXPECT_TRUE(database_old.GetPathForOrigin(origin_string, &path));
761 EXPECT_FALSE(path.empty());
762
763 // Populate the origin directory with some fake data.
764 old_directory_db_path = data_dir_path().Append(path);
765 ASSERT_TRUE(base::CreateDirectory(old_directory_db_path));
766 EXPECT_EQ(static_cast<int>(kFakeDirectoryData.size()),
767 file_util::WriteFile(old_directory_db_path.AppendASCII("dummy"),
768 kFakeDirectoryData.data(),
769 kFakeDirectoryData.size()));
770 }
771
772 storage_policy_->AddIsolated(origin_);
773 scoped_ptr<ObfuscatedFileUtil> file_util = CreateObfuscatedFileUtil(
774 storage_policy_.get());
[email protected]141bcc52014-01-27 21:36:00775 base::File::Error error = base::File::FILE_ERROR_FAILED;
[email protected]7ade66ee2014-01-16 17:35:53776 base::FilePath origin_directory = file_util->GetDirectoryForOrigin(
777 origin_, true /* create */, &error);
[email protected]141bcc52014-01-27 21:36:00778 EXPECT_EQ(base::File::FILE_OK, error);
[email protected]7ade66ee2014-01-16 17:35:53779
780 // The database is migrated from the old one.
781 EXPECT_TRUE(base::DirectoryExists(origin_directory));
782 EXPECT_FALSE(base::DirectoryExists(old_directory_db_path));
783
784 // Check we see the same contents in the new origin directory.
785 std::string origin_db_data;
786 EXPECT_TRUE(base::PathExists(origin_directory.AppendASCII("dummy")));
787 EXPECT_TRUE(base::ReadFileToString(
788 origin_directory.AppendASCII("dummy"), &origin_db_data));
789 EXPECT_EQ(kFakeDirectoryData, origin_db_data);
790 }
791
[email protected]ecdfd6c52012-04-11 13:35:44792 int64 ComputeCurrentUsage() {
[email protected]1c98fdd2013-05-24 09:45:27793 return sandbox_file_system_.ComputeCurrentOriginUsage() -
794 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]02a60542012-07-24 20:05:33795 }
[email protected]45ea0fbbb2012-02-27 22:28:49796
[email protected]07b64872013-02-13 11:46:30797 FileSystemContext* file_system_context() {
[email protected]1c98fdd2013-05-24 09:45:27798 return sandbox_file_system_.file_system_context();
[email protected]07b64872013-02-13 11:46:30799 }
800
[email protected]a4e775b2013-06-13 01:51:12801 const base::FilePath& data_dir_path() const {
802 return data_dir_.path();
803 }
804
[email protected]501fd0e2013-06-24 12:51:01805 protected:
[email protected]ea1a3f62012-11-16 20:34:23806 base::ScopedTempDir data_dir_;
[email protected]4cc586b2013-05-07 12:43:32807 base::MessageLoop message_loop_;
[email protected]501fd0e2013-06-24 12:51:01808 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_;
[email protected]0c5ebe32011-08-19 22:37:16809 scoped_refptr<quota::QuotaManager> quota_manager_;
810 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26811 GURL origin_;
812 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03813 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]1c98fdd2013-05-24 09:45:27814 SandboxFileSystemTestHelper sandbox_file_system_;
[email protected]0c5ebe32011-08-19 22:37:16815 quota::QuotaStatusCode quota_status_;
816 int64 usage_;
[email protected]7ade66ee2014-01-16 17:35:53817 fileapi::MockFileChangeObserver change_observer_;
818 fileapi::ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32819
[email protected]343eac92013-10-11 06:14:26820 private:
[email protected]7878ece2011-09-05 11:41:49821 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32822};
823
[email protected]7878ece2011-09-05 11:41:49824TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32825 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
826 bool created;
[email protected]949f25a2012-06-27 01:53:09827 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16828 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32829 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
830
[email protected]141bcc52014-01-27 21:36:00831 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
832 ofu()->CreateOrOpen(context.get(), url, file_flags, &file_handle,
833 &created));
[email protected]d4905e2e2011-05-13 21:56:32834
[email protected]0c5ebe32011-08-19 22:37:16835 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00836 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09837 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32838
[email protected]949f25a2012-06-27 01:53:09839 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32840
[email protected]c4e6f9c2012-09-09 17:42:10841 EXPECT_TRUE(change_observer()->HasNoChange());
842
[email protected]0c5ebe32011-08-19 22:37:16843 // Verify that file creation requires sufficient quota for the path.
844 context.reset(NewContext(NULL));
845 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09846 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]141bcc52014-01-27 21:36:00847 ASSERT_EQ(base::File::FILE_ERROR_NO_SPACE,
848 ofu()->CreateOrOpen(context.get(), url, file_flags,
849 &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16850
851 context.reset(NewContext(NULL));
852 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09853 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]141bcc52014-01-27 21:36:00854 ASSERT_EQ(base::File::FILE_OK,
855 ofu()->CreateOrOpen(context.get(), url, file_flags, &file_handle,
856 &created));
[email protected]d4905e2e2011-05-13 21:56:32857 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10858 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32859 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
860
[email protected]949f25a2012-06-27 01:53:09861 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32862
[email protected]0c5ebe32011-08-19 22:37:16863 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33864 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:00865 EXPECT_EQ(base::File::FILE_OK,
866 ofu()->GetLocalFilePath(context.get(), url, &local_path));
[email protected]7567484142013-07-11 17:36:07867 EXPECT_TRUE(base::PathExists(local_path));
[email protected]d4905e2e2011-05-13 21:56:32868
[email protected]0c5ebe32011-08-19 22:37:16869 // Verify that deleting a file isn't stopped by zero quota, and that it frees
870 // up quote from its path.
871 context.reset(NewContext(NULL));
872 context->set_allowed_bytes_growth(0);
[email protected]141bcc52014-01-27 21:36:00873 EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10874 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]7567484142013-07-11 17:36:07875 EXPECT_FALSE(base::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09876 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]141bcc52014-01-27 21:36:00877 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32878
[email protected]0c5ebe32011-08-19 22:37:16879 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32880 bool exclusive = true;
881 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09882 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50883 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42884 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]141bcc52014-01-27 21:36:00885 EXPECT_EQ(base::File::FILE_OK,
886 ofu()->CreateDirectory(context.get(), directory_url, exclusive,
887 recursive));
[email protected]c4e6f9c2012-09-09 17:42:10888 // The oepration created 3 directories recursively.
889 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32890
[email protected]0c5ebe32011-08-19 22:37:16891 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32892 file_handle = base::kInvalidPlatformFileValue;
[email protected]141bcc52014-01-27 21:36:00893 ASSERT_EQ(base::File::FILE_OK,
894 ofu()->CreateOrOpen(context.get(), url, file_flags, &file_handle,
895 &created));
[email protected]d4905e2e2011-05-13 21:56:32896 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10897 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32898 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
899
[email protected]949f25a2012-06-27 01:53:09900 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32901
[email protected]0c5ebe32011-08-19 22:37:16902 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00903 EXPECT_EQ(base::File::FILE_OK,
904 ofu()->GetLocalFilePath(context.get(), url, &local_path));
[email protected]7567484142013-07-11 17:36:07905 EXPECT_TRUE(base::PathExists(local_path));
[email protected]d4905e2e2011-05-13 21:56:32906
[email protected]0c5ebe32011-08-19 22:37:16907 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00908 EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10909 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]7567484142013-07-11 17:36:07910 EXPECT_FALSE(base::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10911
912 // Make sure we have no unexpected changes.
913 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32914}
915
[email protected]7878ece2011-09-05 11:41:49916TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32917 bool created = false;
[email protected]949f25a2012-06-27 01:53:09918 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16919 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32920
[email protected]141bcc52014-01-27 21:36:00921 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09922 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32923
[email protected]0c5ebe32011-08-19 22:37:16924 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00925 ASSERT_EQ(base::File::FILE_OK,
926 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32927 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10928 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32929
[email protected]0c5ebe32011-08-19 22:37:16930 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33931 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:00932 EXPECT_EQ(base::File::FILE_OK,
933 ofu()->GetLocalFilePath(context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32934 EXPECT_EQ(0, GetSize(local_path));
935
[email protected]0c5ebe32011-08-19 22:37:16936 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00937 EXPECT_EQ(base::File::FILE_OK, ofu()->Truncate(context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10938 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32939 EXPECT_EQ(10, GetSize(local_path));
940
[email protected]0c5ebe32011-08-19 22:37:16941 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:00942 EXPECT_EQ(base::File::FILE_OK, ofu()->Truncate(context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32943 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10944 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32945
[email protected]13f92f6e2012-08-13 07:39:14946 EXPECT_FALSE(DirectoryExists(url));
947 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10948
949 // Make sure we have no unexpected changes.
950 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32951}
952
[email protected]ecdfd6c52012-04-11 13:35:44953TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
954 bool created = false;
[email protected]949f25a2012-06-27 01:53:09955 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44956
[email protected]141bcc52014-01-27 21:36:00957 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:13958 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09959 AllowUsageIncrease(PathCost(url))->context(),
960 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44961 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13962 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44963
[email protected]141bcc52014-01-27 21:36:00964 ASSERT_EQ(base::File::FILE_OK,
965 ofu()->Truncate(AllowUsageIncrease(1020)->context(), url, 1020));
[email protected]294dd0312012-05-11 07:35:13966 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44967
[email protected]141bcc52014-01-27 21:36:00968 ASSERT_EQ(base::File::FILE_OK,
969 ofu()->Truncate(AllowUsageIncrease(-1020)->context(), url, 0));
[email protected]294dd0312012-05-11 07:35:13970 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44971
[email protected]141bcc52014-01-27 21:36:00972 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
973 ofu()->Truncate(DisallowUsageIncrease(1021)->context(),
974 url, 1021));
[email protected]294dd0312012-05-11 07:35:13975 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44976
[email protected]141bcc52014-01-27 21:36:00977 EXPECT_EQ(base::File::FILE_OK,
978 ofu()->Truncate(AllowUsageIncrease(1020)->context(), url, 1020));
[email protected]294dd0312012-05-11 07:35:13979 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44980
[email protected]141bcc52014-01-27 21:36:00981 EXPECT_EQ(base::File::FILE_OK,
982 ofu()->Truncate(AllowUsageIncrease(0)->context(), url, 1020));
[email protected]294dd0312012-05-11 07:35:13983 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44984
[email protected]294dd0312012-05-11 07:35:13985 // quota exceeded
986 {
987 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
988 helper->context()->set_allowed_bytes_growth(
989 helper->context()->allowed_bytes_growth() - 1);
[email protected]141bcc52014-01-27 21:36:00990 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:09991 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13992 ASSERT_EQ(1019, ComputeTotalFileSize());
993 }
[email protected]ecdfd6c52012-04-11 13:35:44994
995 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33996 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:00997 ASSERT_EQ(base::File::FILE_OK,
998 ofu()->GetLocalFilePath(UnlimitedContext().get(), url,
999 &local_path));
[email protected]ecdfd6c52012-04-11 13:35:441000 ASSERT_FALSE(local_path.empty());
[email protected]dd3aa792013-07-16 19:10:231001 ASSERT_TRUE(base::DeleteFile(local_path, false));
[email protected]ecdfd6c52012-04-11 13:35:441002
[email protected]141bcc52014-01-27 21:36:001003 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1004 ofu()->Truncate(LimitedContext(1234).get(), url, 1234));
[email protected]294dd0312012-05-11 07:35:131005 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:441006}
1007
[email protected]7878ece2011-09-05 11:41:491008TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:091009 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:321010 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161011 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001012 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1013 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:101014 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321015
[email protected]0c5ebe32011-08-19 22:37:161016 // Verify that file creation requires sufficient quota for the path.
1017 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091018 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:321019 created = false;
[email protected]0c5ebe32011-08-19 22:37:161020 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091021 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]141bcc52014-01-27 21:36:001022 ASSERT_EQ(base::File::FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091023 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:161024 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:101025 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161026
1027 context.reset(NewContext(NULL));
1028 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091029 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]141bcc52014-01-27 21:36:001030 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091031 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321032 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:101033 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:321034
[email protected]949f25a2012-06-27 01:53:091035 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:321036
[email protected]0c5ebe32011-08-19 22:37:161037 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001038 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091039 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321040 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:101041 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321042
1043 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:091044 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:161045 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321046 bool exclusive = true;
1047 bool recursive = true;
[email protected]141bcc52014-01-27 21:36:001048 EXPECT_EQ(base::File::FILE_OK,
1049 ofu()->CreateDirectory(context.get(), FileSystemURLDirName(url),
1050 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101051 // 2 directories: path/ and path/to.
1052 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321053
[email protected]0c5ebe32011-08-19 22:37:161054 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001055 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091056 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321057 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:141058 EXPECT_FALSE(DirectoryExists(url));
1059 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:101060 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321061}
1062
[email protected]7878ece2011-09-05 11:41:491063TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:161064 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321065
1066 bool exclusive = false;
1067 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091068 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]141bcc52014-01-27 21:36:001069 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1070 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321071
[email protected]0c5ebe32011-08-19 22:37:161072 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001073 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1074 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321075
[email protected]007b3f82013-04-09 08:46:451076 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:141077 EXPECT_FALSE(DirectoryExists(url));
1078 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:161079 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491080 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:321081
[email protected]0c5ebe32011-08-19 22:37:161082 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321083 exclusive = false;
1084 recursive = true;
[email protected]141bcc52014-01-27 21:36:001085 EXPECT_EQ(base::File::FILE_OK,
1086 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101087 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321088
[email protected]13f92f6e2012-08-13 07:39:141089 EXPECT_TRUE(DirectoryExists(url));
1090 EXPECT_TRUE(PathExists(url));
1091
[email protected]0c5ebe32011-08-19 22:37:161092 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491093 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:421094 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:141095
[email protected]0c5ebe32011-08-19 22:37:161096 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091097 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:421098 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:321099
1100 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:161101 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001102 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
1103 ofu()->DeleteDirectory(context.get(),
1104 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:101105 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321106
[email protected]141bcc52014-01-27 21:36:001107 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:331108 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:001109 EXPECT_EQ(base::File::FILE_OK,
1110 ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321111 EXPECT_TRUE(local_path.empty());
1112 EXPECT_TRUE(file_info.is_directory);
1113 EXPECT_FALSE(file_info.is_symbolic_link);
1114
1115 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:161116 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001117 EXPECT_EQ(base::File::FILE_OK,
1118 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101119 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321120
1121 exclusive = true;
1122 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161123 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001124 EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
1125 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101126 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321127
[email protected]0c5ebe32011-08-19 22:37:161128 // Verify that deleting a directory isn't stopped by zero quota, and that it
1129 // frees up quota from its path.
1130 context.reset(NewContext(NULL));
1131 context->set_allowed_bytes_growth(0);
[email protected]141bcc52014-01-27 21:36:001132 EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101133 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091134 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161135 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321136
[email protected]949f25a2012-06-27 01:53:091137 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321138
[email protected]13f92f6e2012-08-13 07:39:141139 EXPECT_FALSE(DirectoryExists(url));
1140 EXPECT_FALSE(PathExists(url));
1141
[email protected]0c5ebe32011-08-19 22:37:161142 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091143 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]141bcc52014-01-27 21:36:001144 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1145 ofu()->GetFileInfo(context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321146
[email protected]0c5ebe32011-08-19 22:37:161147 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321148 exclusive = true;
1149 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161150 context.reset(NewContext(NULL));
1151 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091152 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]141bcc52014-01-27 21:36:001153 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
1154 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101155 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161156
1157 context.reset(NewContext(NULL));
1158 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091159 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]141bcc52014-01-27 21:36:001160 EXPECT_EQ(base::File::FILE_OK,
1161 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101162 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321163
[email protected]13f92f6e2012-08-13 07:39:141164 EXPECT_TRUE(DirectoryExists(url));
1165 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321166
1167 exclusive = true;
1168 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141169 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001170 EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
1171 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101172 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321173
1174 exclusive = true;
1175 recursive = false;
[email protected]949f25a2012-06-27 01:53:091176 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141177 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001178 EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
1179 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101180 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321181
[email protected]949f25a2012-06-27 01:53:091182 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321183
[email protected]13f92f6e2012-08-13 07:39:141184 EXPECT_FALSE(DirectoryExists(url));
1185 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321186
1187 exclusive = true;
1188 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141189 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001190 EXPECT_EQ(base::File::FILE_OK,
1191 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101192 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321193
[email protected]13f92f6e2012-08-13 07:39:141194 EXPECT_TRUE(DirectoryExists(url));
1195 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321196
1197 exclusive = true;
1198 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141199 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001200 EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
1201 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101202 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321203}
1204
[email protected]7878ece2011-09-05 11:41:491205TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161206 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321207 bool exclusive = true;
1208 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091209 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]141bcc52014-01-27 21:36:001210 EXPECT_EQ(base::File::FILE_OK,
1211 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]949f25a2012-06-27 01:53:091212 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321213}
1214
[email protected]7878ece2011-09-05 11:41:491215TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451216 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321217}
1218
[email protected]7878ece2011-09-05 11:41:491219TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091220 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321221}
1222
[email protected]7878ece2011-09-05 11:41:491223TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091224 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161225 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321226
1227 bool created = false;
[email protected]141bcc52014-01-27 21:36:001228 ASSERT_EQ(base::File::FILE_OK,
1229 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321230 ASSERT_TRUE(created);
1231
[email protected]7ade66ee2014-01-16 17:35:531232 std::vector<fileapi::DirectoryEntry> entries;
[email protected]141bcc52014-01-27 21:36:001233 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY,
1234 AsyncFileTestHelper::ReadDirectory(file_system_context(), url,
1235 &entries));
[email protected]d4905e2e2011-05-13 21:56:321236
[email protected]949f25a2012-06-27 01:53:091237 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321238}
1239
[email protected]7878ece2011-09-05 11:41:491240TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091241 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161242 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411243
1244 base::Time last_access_time = base::Time::Now();
1245 base::Time last_modified_time = base::Time::Now();
1246
1247 // It's not there yet.
[email protected]141bcc52014-01-27 21:36:001248 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1249 ofu()->Touch(context.get(), url, last_access_time,
1250 last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321251
[email protected]2517cfa2011-08-25 05:12:411252 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161253 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411254 bool created = false;
[email protected]141bcc52014-01-27 21:36:001255 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091256 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411257 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091258 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411259
1260 // Now test a directory:
1261 context.reset(NewContext(NULL));
1262 bool exclusive = true;
1263 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091264 url = CreateURLFromUTF8("dir");
[email protected]141bcc52014-01-27 21:36:001265 ASSERT_EQ(base::File::FILE_OK,
1266 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]949f25a2012-06-27 01:53:091267 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161268}
1269
[email protected]7878ece2011-09-05 11:41:491270TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091271 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161272 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1273
[email protected]949f25a2012-06-27 01:53:091274 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161275 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411276 bool created = false;
[email protected]141bcc52014-01-27 21:36:001277 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
1278 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411279 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161280 context->set_allowed_bytes_growth(1024);
[email protected]141bcc52014-01-27 21:36:001281 EXPECT_EQ(base::File::FILE_OK,
1282 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411283 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091284 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161285 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1286
1287 context->set_allowed_bytes_growth(1024);
1288 bool exclusive = true;
1289 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091290 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331291 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091292 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161293 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421294 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1295 for (iterator iter = components.begin();
1296 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491297 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331298 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161299 }
1300 context.reset(NewContext(NULL));
1301 context->set_allowed_bytes_growth(1024);
[email protected]141bcc52014-01-27 21:36:001302 EXPECT_EQ(base::File::FILE_OK,
1303 ofu()->CreateDirectory(context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161304 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321305}
1306
[email protected]7878ece2011-09-05 11:41:491307TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091308 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1309 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161310 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321311
1312 bool is_copy_not_move = false;
[email protected]141bcc52014-01-27 21:36:001313 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1314 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
1315 FileSystemOperation::OPTION_NONE,
1316 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101317 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161318 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321319 is_copy_not_move = true;
[email protected]141bcc52014-01-27 21:36:001320 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1321 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
1322 FileSystemOperation::OPTION_NONE,
1323 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101324 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091325 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321326 bool exclusive = true;
1327 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161328 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001329 ASSERT_EQ(base::File::FILE_OK,
1330 ofu()->CreateDirectory(context.get(),
1331 FileSystemURLDirName(source_url),
1332 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101333 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321334 is_copy_not_move = false;
[email protected]141bcc52014-01-27 21:36:001335 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1336 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
1337 FileSystemOperation::OPTION_NONE,
1338 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101339 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161340 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321341 is_copy_not_move = true;
[email protected]141bcc52014-01-27 21:36:001342 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1343 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
1344 FileSystemOperation::OPTION_NONE,
1345 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101346 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321347}
1348
[email protected]7878ece2011-09-05 11:41:491349TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321350 const int64 kSourceLength = 5;
1351 const int64 kDestLength = 50;
1352
1353 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1354 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1355 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1356 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1357 test_case.is_copy_not_move);
1358 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1359 test_case.source_path);
1360 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1361 test_case.dest_path);
1362 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1363 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161364 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321365
1366 bool exclusive = false;
1367 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091368 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1369 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321370
[email protected]0c5ebe32011-08-19 22:37:161371 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001372 ASSERT_EQ(base::File::FILE_OK,
1373 ofu()->CreateDirectory(context.get(),
1374 FileSystemURLDirName(source_url),
1375 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161376 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001377 ASSERT_EQ(base::File::FILE_OK,
1378 ofu()->CreateDirectory(context.get(),
1379 FileSystemURLDirName(dest_url),
1380 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321381
[email protected]d4905e2e2011-05-13 21:56:321382 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161383 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001384 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091385 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321386 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161387 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001388 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091389 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321390
1391 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161392 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321393 created = false;
[email protected]141bcc52014-01-27 21:36:001394 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091395 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321396 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161397 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001398 ASSERT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091399 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321400 }
1401
[email protected]0c5ebe32011-08-19 22:37:161402 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001403 EXPECT_EQ(base::File::FILE_OK,
1404 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
1405 FileSystemOperation::OPTION_NONE,
1406 test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101407
[email protected]d4905e2e2011-05-13 21:56:321408 if (test_case.is_copy_not_move) {
[email protected]141bcc52014-01-27 21:36:001409 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:331410 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161411 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001412 EXPECT_EQ(base::File::FILE_OK,
1413 ofu()->GetFileInfo(context.get(), source_url, &file_info,
1414 &local_path));
[email protected]d4905e2e2011-05-13 21:56:321415 EXPECT_EQ(kSourceLength, file_info.size);
[email protected]141bcc52014-01-27 21:36:001416 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091417 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321418 } else {
[email protected]141bcc52014-01-27 21:36:001419 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:331420 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161421 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001422 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
1423 ofu()->GetFileInfo(context.get(), source_url, &file_info,
1424 &local_path));
[email protected]d4905e2e2011-05-13 21:56:321425 }
[email protected]141bcc52014-01-27 21:36:001426 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:331427 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:001428 EXPECT_EQ(base::File::FILE_OK,
1429 ofu()->GetFileInfo(context.get(), dest_url, &file_info,
1430 &local_path));
[email protected]d4905e2e2011-05-13 21:56:321431 EXPECT_EQ(kSourceLength, file_info.size);
1432
[email protected]141bcc52014-01-27 21:36:001433 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091434 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321435 }
1436}
1437
[email protected]7878ece2011-09-05 11:41:491438TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091439 FileSystemURL src_url = CreateURLFromUTF8("src path");
1440 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161441 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1442 bool created = false;
[email protected]141bcc52014-01-27 21:36:001443 ASSERT_EQ(base::File::FILE_OK,
1444 ofu()->EnsureFileExists(context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161445
1446 bool is_copy = true;
1447 // Copy, no overwrite.
1448 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091449 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]141bcc52014-01-27 21:36:001450 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
1451 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1452 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161453 context.reset(NewContext(NULL));
1454 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091455 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]141bcc52014-01-27 21:36:001456 EXPECT_EQ(base::File::FILE_OK,
1457 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1458 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161459
1460 // Copy, with overwrite.
1461 context.reset(NewContext(NULL));
1462 context->set_allowed_bytes_growth(0);
[email protected]141bcc52014-01-27 21:36:001463 EXPECT_EQ(base::File::FILE_OK,
1464 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1465 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161466}
1467
[email protected]7878ece2011-09-05 11:41:491468TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091469 FileSystemURL src_url = CreateURLFromUTF8("src path");
1470 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161471 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1472 bool created = false;
[email protected]141bcc52014-01-27 21:36:001473 ASSERT_EQ(base::File::FILE_OK,
1474 ofu()->EnsureFileExists(context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161475
1476 bool is_copy = false;
1477 // Move, rename, no overwrite.
1478 context.reset(NewContext(NULL));
1479 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091480 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1481 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]141bcc52014-01-27 21:36:001482 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
1483 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1484 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161485 context.reset(NewContext(NULL));
1486 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091487 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1488 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]141bcc52014-01-27 21:36:001489 EXPECT_EQ(base::File::FILE_OK,
1490 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1491 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161492
1493 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001494 ASSERT_EQ(base::File::FILE_OK,
1495 ofu()->EnsureFileExists(context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161496
1497 // Move, rename, with overwrite.
1498 context.reset(NewContext(NULL));
1499 context->set_allowed_bytes_growth(0);
[email protected]141bcc52014-01-27 21:36:001500 EXPECT_EQ(base::File::FILE_OK,
1501 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1502 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161503}
1504
[email protected]7878ece2011-09-05 11:41:491505TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091506 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161507 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1508 bool created = false;
[email protected]141bcc52014-01-27 21:36:001509 ASSERT_EQ(base::File::FILE_OK,
1510 ofu()->EnsureFileExists(context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161511
1512 bool exclusive = true;
1513 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091514 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161515 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001516 ASSERT_EQ(base::File::FILE_OK,
1517 ofu()->CreateDirectory(context.get(), dir_url, exclusive,
1518 recursive));
[email protected]0c5ebe32011-08-19 22:37:161519
[email protected]04c899f2013-02-08 08:28:421520 FileSystemURL dest_url = FileSystemURLAppend(
1521 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161522
1523 bool is_copy = false;
1524 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1525 // Move, no rename, no overwrite.
1526 context.reset(NewContext(NULL));
1527 context->set_allowed_bytes_growth(allowed_bytes_growth);
[email protected]141bcc52014-01-27 21:36:001528 EXPECT_EQ(base::File::FILE_OK,
1529 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1530 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161531 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1532
1533 // Move, no rename, with overwrite.
1534 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001535 ASSERT_EQ(base::File::FILE_OK,
1536 ofu()->EnsureFileExists(context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161537 context.reset(NewContext(NULL));
1538 context->set_allowed_bytes_growth(allowed_bytes_growth);
[email protected]141bcc52014-01-27 21:36:001539 EXPECT_EQ(base::File::FILE_OK,
1540 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url,
1541 FileSystemOperation::OPTION_NONE, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161542 EXPECT_EQ(
1543 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091544 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161545 context->allowed_bytes_growth());
1546}
1547
[email protected]7878ece2011-09-05 11:41:491548TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461549 TestCopyInForeignFileHelper(false /* overwrite */);
1550 TestCopyInForeignFileHelper(true /* overwrite */);
1551}
1552
[email protected]7878ece2011-09-05 11:41:491553TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161554 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091555 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321556 bool exclusive = true;
1557 bool recursive = false;
[email protected]141bcc52014-01-27 21:36:001558 ASSERT_EQ(base::File::FILE_OK,
1559 ofu()->CreateDirectory(context.get(), src_url, exclusive,
1560 recursive));
[email protected]d4905e2e2011-05-13 21:56:321561
[email protected]a3ef4832013-02-02 05:12:331562 std::set<base::FilePath::StringType> files;
1563 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091564 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321565
[email protected]949f25a2012-06-27 01:53:091566 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321567
[email protected]13f92f6e2012-08-13 07:39:141568 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]141bcc52014-01-27 21:36:001569 ASSERT_EQ(base::File::FILE_OK,
[email protected]07b64872013-02-13 11:46:301570 AsyncFileTestHelper::Copy(
[email protected]1c98fdd2013-05-24 09:45:271571 file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321572
[email protected]949f25a2012-06-27 01:53:091573 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141574 EXPECT_TRUE(DirectoryExists(src_url));
1575 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321576 recursive = true;
[email protected]141bcc52014-01-27 21:36:001577 ASSERT_EQ(base::File::FILE_OK,
[email protected]07b64872013-02-13 11:46:301578 AsyncFileTestHelper::Remove(
1579 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141580 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321581}
[email protected]6b931152011-05-20 21:02:351582
[email protected]7878ece2011-09-05 11:41:491583TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1584 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1585 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161586 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261587 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161588 EXPECT_EQ(origin(), enumerator->Next());
1589 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
[email protected]c19e2ff2013-10-19 18:15:101590 EXPECT_TRUE(HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
1591 EXPECT_FALSE(HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261592 EXPECT_EQ(GURL(), enumerator->Next());
[email protected]c19e2ff2013-10-19 18:15:101593 EXPECT_FALSE(HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
1594 EXPECT_FALSE(HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261595
1596 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161597 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261598
1599 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1600 SCOPED_TRACE(testing::Message() <<
1601 "Validating kOriginEnumerationTestRecords " << i);
1602 const OriginEnumerationTestRecord& record =
1603 kOriginEnumerationTestRecords[i];
1604 GURL origin_url(record.origin_url);
1605 origins_expected.insert(origin_url);
1606 if (record.has_temporary) {
[email protected]1c98fdd2013-05-24 09:45:271607 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1608 NewFileSystem(origin_url, kFileSystemTypeTemporary));
1609 scoped_ptr<FileSystemOperationContext> context(
1610 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261611 bool created = false;
[email protected]141bcc52014-01-27 21:36:001612 ASSERT_EQ(base::File::FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501613 ofu()->EnsureFileExists(
1614 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271615 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501616 &created));
[email protected]fcc2d5f2011-05-23 22:06:261617 EXPECT_TRUE(created);
1618 }
1619 if (record.has_persistent) {
[email protected]1c98fdd2013-05-24 09:45:271620 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1621 NewFileSystem(origin_url, kFileSystemTypePersistent));
1622 scoped_ptr<FileSystemOperationContext> context(
1623 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261624 bool created = false;
[email protected]141bcc52014-01-27 21:36:001625 ASSERT_EQ(base::File::FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501626 ofu()->EnsureFileExists(
1627 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271628 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501629 &created));
[email protected]fcc2d5f2011-05-23 22:06:261630 EXPECT_TRUE(created);
1631 }
1632 }
[email protected]7878ece2011-09-05 11:41:491633 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261634 EXPECT_TRUE(enumerator.get());
1635 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161636 GURL origin_url;
1637 while (!(origin_url = enumerator->Next()).is_empty()) {
1638 origins_found.insert(origin_url);
1639 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261640 bool found = false;
1641 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1642 ++i) {
1643 const OriginEnumerationTestRecord& record =
1644 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161645 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261646 continue;
1647 found = true;
1648 EXPECT_EQ(record.has_temporary,
[email protected]c19e2ff2013-10-19 18:15:101649 HasFileSystemType(enumerator.get(), kFileSystemTypeTemporary));
[email protected]fcc2d5f2011-05-23 22:06:261650 EXPECT_EQ(record.has_persistent,
[email protected]c19e2ff2013-10-19 18:15:101651 HasFileSystemType(enumerator.get(), kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261652 }
[email protected]0c5ebe32011-08-19 22:37:161653 // Deal with the default filesystem created by the test helper.
1654 if (!found && origin_url == origin()) {
1655 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
[email protected]c19e2ff2013-10-19 18:15:101656 EXPECT_TRUE(HasFileSystemType(enumerator.get(),
1657 kFileSystemTypeTemporary));
1658 EXPECT_FALSE(HasFileSystemType(enumerator.get(),
1659 kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161660 found = true;
1661 }
[email protected]fcc2d5f2011-05-23 22:06:261662 EXPECT_TRUE(found);
1663 }
1664
1665 std::set<GURL> diff;
1666 std::set_symmetric_difference(origins_expected.begin(),
1667 origins_expected.end(), origins_found.begin(), origins_found.end(),
1668 inserter(diff, diff.begin()));
1669 EXPECT_TRUE(diff.empty());
1670}
[email protected]0c5ebe32011-08-19 22:37:161671
[email protected]7878ece2011-09-05 11:41:491672TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161673 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1674
1675 int64 expected_quota = 0;
1676
[email protected]7ade66ee2014-01-16 17:35:531677 for (size_t i = 0; i < fileapi::test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011678 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]7ade66ee2014-01-16 17:35:531679 const fileapi::test::TestCaseRecord& test_case =
1680 fileapi::test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331681 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501682 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161683 if (test_case.is_directory) {
1684 bool exclusive = true;
1685 bool recursive = false;
[email protected]141bcc52014-01-27 21:36:001686 ASSERT_EQ(base::File::FILE_OK,
1687 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
1688 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161689 } else {
1690 bool created = false;
[email protected]141bcc52014-01-27 21:36:001691 ASSERT_EQ(base::File::FILE_OK,
1692 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
1693 &created));
[email protected]0c5ebe32011-08-19 22:37:161694 ASSERT_TRUE(created);
[email protected]141bcc52014-01-27 21:36:001695 ASSERT_EQ(base::File::FILE_OK,
1696 ofu()->Truncate(context.get(), CreateURL(file_path),
1697 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161698 expected_quota += test_case.data_file_size;
1699 }
1700 }
[email protected]022d2702012-05-14 16:04:261701
1702 // Usually raw size in usage cache and the usage returned by QuotaUtil
1703 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161704 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261705 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1706
[email protected]0c5ebe32011-08-19 22:37:161707 RevokeUsageCache();
1708 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261709 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1710
1711 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161712 GetUsageFromQuotaManager();
1713 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261714 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161715 EXPECT_EQ(expected_quota, usage());
1716}
[email protected]34583332011-08-31 08:59:471717
[email protected]7878ece2011-09-05 11:41:491718TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091719 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1720 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471721
1722 scoped_ptr<FileSystemOperationContext> context;
1723 base::PlatformFile file;
[email protected]141bcc52014-01-27 21:36:001724 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:331725 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471726 bool created = false;
1727
1728 // Create a non-empty file.
1729 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001730 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491731 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471732 EXPECT_TRUE(created);
1733 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001734 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491735 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471736 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001737 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491738 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471739 context.get(), kPath1, &file_info, &data_path));
1740 EXPECT_EQ(10, file_info.size);
1741
1742 // Destroy database to make inconsistency between database and filesystem.
[email protected]c19e2ff2013-10-19 18:15:101743 ofu()->DestroyDirectoryDatabase(origin(), type_string());
[email protected]34583332011-08-31 08:59:471744
1745 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141746 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471747 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001748 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491749 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471750 EXPECT_TRUE(created);
1751 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001752 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491753 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471754 context.get(), kPath1, &file_info, &data_path));
1755 EXPECT_EQ(0, file_info.size);
1756
1757 // Make another broken file to |kPath2|.
1758 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001759 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491760 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471761 EXPECT_TRUE(created);
1762
1763 // Destroy again.
[email protected]c19e2ff2013-10-19 18:15:101764 ofu()->DestroyDirectoryDatabase(origin(), type_string());
[email protected]34583332011-08-31 08:59:471765
1766 // Repair broken |kPath1|.
1767 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001768 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491769 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471770 base::Time::Now()));
[email protected]141bcc52014-01-27 21:36:001771 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491772 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471773 EXPECT_TRUE(created);
1774
1775 // Copy from sound |kPath1| to broken |kPath2|.
1776 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001777 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491778 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]824d3892013-09-25 13:00:301779 FileSystemOperation::OPTION_NONE,
[email protected]08f8feb2012-02-26 11:53:501780 true /* copy */));
[email protected]34583332011-08-31 08:59:471781
[email protected]c19e2ff2013-10-19 18:15:101782 ofu()->DestroyDirectoryDatabase(origin(), type_string());
[email protected]34583332011-08-31 08:59:471783 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001784 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491785 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471786 context.get(), kPath1,
1787 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1788 &file, &created));
1789 EXPECT_TRUE(created);
[email protected]141bcc52014-01-27 21:36:001790
1791 base::File base_file(file);
1792 EXPECT_TRUE(base_file.GetInfo(&file_info));
[email protected]34583332011-08-31 08:59:471793 EXPECT_EQ(0, file_info.size);
[email protected]34583332011-08-31 08:59:471794}
[email protected]9dfdc0e32011-09-02 06:07:441795
[email protected]7878ece2011-09-05 11:41:491796TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091797 const FileSystemURL kPath[] = {
1798 CreateURLFromUTF8("foo"),
1799 CreateURLFromUTF8("bar"),
1800 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441801 };
[email protected]a3ef4832013-02-02 05:12:331802 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441803 scoped_ptr<FileSystemOperationContext> context;
1804
1805 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1806 bool created = false;
1807 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001808 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491809 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441810 EXPECT_TRUE(created);
1811 }
1812
[email protected]7ade66ee2014-01-16 17:35:531813 std::vector<fileapi::DirectoryEntry> entries;
[email protected]141bcc52014-01-27 21:36:001814 EXPECT_EQ(base::File::FILE_OK,
[email protected]07b64872013-02-13 11:46:301815 AsyncFileTestHelper::ReadDirectory(
1816 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441817 EXPECT_EQ(3u, entries.size());
1818
[email protected]a3ef4832013-02-02 05:12:331819 base::FilePath local_path;
[email protected]141bcc52014-01-27 21:36:001820 EXPECT_EQ(base::File::FILE_OK,
[email protected]7878ece2011-09-05 11:41:491821 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]dd3aa792013-07-16 19:10:231822 EXPECT_TRUE(base::DeleteFile(local_path, false));
[email protected]9dfdc0e32011-09-02 06:07:441823
[email protected]9dfdc0e32011-09-02 06:07:441824 entries.clear();
[email protected]141bcc52014-01-27 21:36:001825 EXPECT_EQ(base::File::FILE_OK,
[email protected]07b64872013-02-13 11:46:301826 AsyncFileTestHelper::ReadDirectory(
1827 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441828 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1829}
[email protected]fad625e2f2011-12-08 05:38:031830
1831TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1832 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091833 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031834
1835 // Create working directory.
[email protected]141bcc52014-01-27 21:36:001836 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091837 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031838
1839 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421840 FileSystemURL url(FileSystemURLAppendUTF8(
1841 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031842 bool created = false;
[email protected]949f25a2012-06-27 01:53:091843 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031844 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001845 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091846 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031847 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091848 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031849
1850 // non create case.
1851 created = true;
[email protected]949f25a2012-06-27 01:53:091852 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031853 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001854 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091855 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031856 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091857 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031858
1859 // fail case.
[email protected]04c899f2013-02-08 08:28:421860 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031861 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001862 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091863 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031864
[email protected]949f25a2012-06-27 01:53:091865 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031866 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001867 EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091868 ofu()->EnsureFileExists(context.get(), url, &created));
1869 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031870
1871 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421872 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391873 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031874 created = false;
[email protected]949f25a2012-06-27 01:53:091875 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031876 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001877 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:031878 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091879 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031880 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1881 &file_handle, &created));
1882 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1883 EXPECT_TRUE(created);
1884 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091885 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031886
1887 // open case.
1888 file_handle = base::kInvalidPlatformFileValue;
1889 created = true;
[email protected]949f25a2012-06-27 01:53:091890 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031891 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001892 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:031893 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091894 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031895 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1896 &file_handle, &created));
1897 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1898 EXPECT_FALSE(created);
1899 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091900 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031901
1902 // fail case
1903 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091904 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031905 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001906 EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
[email protected]fad625e2f2011-12-08 05:38:031907 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091908 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031909 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1910 &file_handle, &created));
1911 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091912 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031913
1914 // CreateDirectory, create case.
1915 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421916 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1917 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091918 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031919 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001920 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091921 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031922 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091923 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031924
1925 // create subdir case.
1926 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421927 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091928 ClearTimestamp(dir_url);
1929 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031930 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001931 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091932 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031933 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091934 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1935 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031936
1937 // fail case.
[email protected]04c899f2013-02-08 08:28:421938 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091939 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031940 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001941 EXPECT_EQ(base::File::FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091942 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031943 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091944 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031945
1946 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421947 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1948 FileSystemURL src_path = FileSystemURLAppendUTF8(
1949 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031950 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001951 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:031952 ofu()->EnsureFileExists(context.get(), src_path, &created));
1953 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331954 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031955 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001956 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:031957 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1958
[email protected]949f25a2012-06-27 01:53:091959 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031960 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001961 EXPECT_EQ(base::File::FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501962 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131963 src_local_path,
[email protected]949f25a2012-06-27 01:53:091964 url));
1965 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031966}
1967
1968TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1969 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091970 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031971
1972 // Create working directory.
[email protected]141bcc52014-01-27 21:36:001973 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091974 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031975
1976 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421977 FileSystemURL url = FileSystemURLAppendUTF8(
1978 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031979 bool created = false;
1980 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001981 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091982 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031983 EXPECT_TRUE(created);
1984
[email protected]949f25a2012-06-27 01:53:091985 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031986 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001987 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:091988 ofu()->DeleteFile(context.get(), url));
1989 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031990
1991 // fail case.
[email protected]949f25a2012-06-27 01:53:091992 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031993 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:001994 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091995 ofu()->DeleteFile(context.get(), url));
1996 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031997
[email protected]bab213be2013-01-23 15:13:081998 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421999 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
2000 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:032001 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002002 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:092003 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:032004 created = false;
2005 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002006 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:032007 ofu()->EnsureFileExists(context.get(), file_path, &created));
2008 EXPECT_TRUE(created);
2009
[email protected]949f25a2012-06-27 01:53:092010 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:032011 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002012 EXPECT_EQ(base::File::FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:082013 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:092014 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:032015
2016 // delete case.
2017 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002018 EXPECT_EQ(base::File::FILE_OK,
[email protected]fad625e2f2011-12-08 05:38:032019 ofu()->DeleteFile(context.get(), file_path));
2020
[email protected]949f25a2012-06-27 01:53:092021 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:032022 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002023 EXPECT_EQ(base::File::FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:092024 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:032025}
2026
2027TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
2028 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:092029 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:032030 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:092031 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:032032 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:092033 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:032034 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:092035 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:032036}
[email protected]a3938912012-03-27 14:00:552037
2038TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:092039 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:422040 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
2041 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:552042
2043 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002044 EXPECT_EQ(base::File::FILE_OK,
[email protected]a3938912012-03-27 14:00:552045 ofu()->CreateDirectory(context.get(), dir, false, false));
2046
2047 bool created = false;
2048 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002049 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:092050 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:552051 EXPECT_TRUE(created);
2052
2053 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002054 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:092055 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:552056
[email protected]a3ef4832013-02-02 05:12:332057 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:552058 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002059 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:092060 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:552061 EXPECT_FALSE(file_path.empty());
2062
2063 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002064 EXPECT_EQ(base::File::FILE_OK,
[email protected]949f25a2012-06-27 01:53:092065 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:552066 base::Time::Now() + base::TimeDelta::FromHours(1),
2067 base::Time()));
2068
2069 context.reset(NewContext(NULL));
[email protected]7ade66ee2014-01-16 17:35:532070 scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> file_enum(
[email protected]a3938912012-03-27 14:00:552071 ofu()->CreateFileEnumerator(context.get(), dir, false));
2072
2073 int count = 0;
[email protected]a3ef4832013-02-02 05:12:332074 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:552075 while (!(file_path_each = file_enum->Next()).empty()) {
2076 context.reset(NewContext(NULL));
[email protected]141bcc52014-01-27 21:36:002077 base::File::Info file_info;
[email protected]a3ef4832013-02-02 05:12:332078 base::FilePath file_path;
[email protected]141bcc52014-01-27 21:36:002079 EXPECT_EQ(base::File::FILE_OK,
[email protected]a3938912012-03-27 14:00:552080 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:422081 FileSystemURL::CreateForTest(
2082 dir.origin(),
2083 dir.mount_type(),
2084 file_path_each),
[email protected]a3938912012-03-27 14:00:552085 &file_info, &file_path));
2086 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
2087 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
2088 EXPECT_EQ(file_info.size, file_enum->Size());
2089 ++count;
2090 }
2091 EXPECT_EQ(2, count);
2092}
[email protected]294dd0312012-05-11 07:35:132093
[email protected]58ac5272013-02-15 10:05:032094// crbug.com/176470
2095#if defined(OS_WIN) || defined(OS_ANDROID)
2096#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
2097#else
2098#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
2099#endif
2100TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:092101 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2102 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2103 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
2104 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:132105 bool created;
2106
2107 int64 expected_total_file_size = 0;
[email protected]141bcc52014-01-27 21:36:002108 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132109 ofu()->EnsureFileExists(
2110 AllowUsageIncrease(PathCost(from_file))->context(),
2111 from_file, &created));
2112 ASSERT_TRUE(created);
2113 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2114
[email protected]141bcc52014-01-27 21:36:002115 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132116 ofu()->EnsureFileExists(
2117 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2118 obstacle_file, &created));
2119 ASSERT_TRUE(created);
2120 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2121
2122 int64 from_file_size = 1020;
2123 expected_total_file_size += from_file_size;
[email protected]141bcc52014-01-27 21:36:002124 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132125 ofu()->Truncate(
2126 AllowUsageIncrease(from_file_size)->context(),
2127 from_file, from_file_size));
2128 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2129
2130 int64 obstacle_file_size = 1;
2131 expected_total_file_size += obstacle_file_size;
[email protected]141bcc52014-01-27 21:36:002132 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132133 ofu()->Truncate(
2134 AllowUsageIncrease(obstacle_file_size)->context(),
2135 obstacle_file, obstacle_file_size));
2136 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2137
2138 int64 to_file1_size = from_file_size;
2139 expected_total_file_size += to_file1_size;
[email protected]141bcc52014-01-27 21:36:002140 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132141 ofu()->CopyOrMoveFile(
2142 AllowUsageIncrease(
2143 PathCost(to_file1) + to_file1_size)->context(),
[email protected]824d3892013-09-25 13:00:302144 from_file, to_file1,
2145 FileSystemOperation::OPTION_NONE,
2146 true /* copy */));
[email protected]294dd0312012-05-11 07:35:132147 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2148
[email protected]141bcc52014-01-27 21:36:002149 ASSERT_EQ(base::File::FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:132150 ofu()->CopyOrMoveFile(
2151 DisallowUsageIncrease(
2152 PathCost(to_file2) + from_file_size)->context(),
[email protected]824d3892013-09-25 13:00:302153 from_file, to_file2, FileSystemOperation::OPTION_NONE,
2154 true /* copy */));
[email protected]294dd0312012-05-11 07:35:132155 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2156
2157 int64 old_obstacle_file_size = obstacle_file_size;
2158 obstacle_file_size = from_file_size;
2159 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
[email protected]141bcc52014-01-27 21:36:002160 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132161 ofu()->CopyOrMoveFile(
2162 AllowUsageIncrease(
2163 obstacle_file_size - old_obstacle_file_size)->context(),
[email protected]824d3892013-09-25 13:00:302164 from_file, obstacle_file,
2165 FileSystemOperation::OPTION_NONE,
2166 true /* copy */));
[email protected]294dd0312012-05-11 07:35:132167 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2168
2169 int64 old_from_file_size = from_file_size;
2170 from_file_size = old_from_file_size - 1;
2171 expected_total_file_size += from_file_size - old_from_file_size;
[email protected]141bcc52014-01-27 21:36:002172 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132173 ofu()->Truncate(
2174 AllowUsageIncrease(
2175 from_file_size - old_from_file_size)->context(),
2176 from_file, from_file_size));
2177 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2178
2179 // quota exceeded
2180 {
2181 old_obstacle_file_size = obstacle_file_size;
2182 obstacle_file_size = from_file_size;
2183 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2184 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2185 obstacle_file_size - old_obstacle_file_size);
2186 helper->context()->set_allowed_bytes_growth(
2187 helper->context()->allowed_bytes_growth() - 1);
[email protected]141bcc52014-01-27 21:36:002188 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132189 ofu()->CopyOrMoveFile(
2190 helper->context(),
[email protected]824d3892013-09-25 13:00:302191 from_file, obstacle_file,
2192 FileSystemOperation::OPTION_NONE,
2193 true /* copy */));
[email protected]294dd0312012-05-11 07:35:132194 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2195 }
2196}
2197
2198TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092199 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2200 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2201 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132202 bool created;
2203
2204 int64 expected_total_file_size = 0;
[email protected]141bcc52014-01-27 21:36:002205 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132206 ofu()->EnsureFileExists(
2207 AllowUsageIncrease(PathCost(from_file))->context(),
2208 from_file, &created));
2209 ASSERT_TRUE(created);
2210 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2211
2212 int64 from_file_size = 1020;
2213 expected_total_file_size += from_file_size;
[email protected]141bcc52014-01-27 21:36:002214 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132215 ofu()->Truncate(
2216 AllowUsageIncrease(from_file_size)->context(),
2217 from_file, from_file_size));
2218 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2219
2220 int64 to_file_size ALLOW_UNUSED = from_file_size;
2221 from_file_size = 0;
[email protected]141bcc52014-01-27 21:36:002222 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132223 ofu()->CopyOrMoveFile(
2224 AllowUsageIncrease(-PathCost(from_file) +
2225 PathCost(to_file))->context(),
[email protected]824d3892013-09-25 13:00:302226 from_file, to_file,
2227 FileSystemOperation::OPTION_NONE,
2228 false /* move */));
[email protected]294dd0312012-05-11 07:35:132229 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2230
[email protected]141bcc52014-01-27 21:36:002231 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132232 ofu()->EnsureFileExists(
2233 AllowUsageIncrease(PathCost(from_file))->context(),
2234 from_file, &created));
2235 ASSERT_TRUE(created);
2236 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2237
[email protected]141bcc52014-01-27 21:36:002238 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132239 ofu()->EnsureFileExists(
2240 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2241 obstacle_file, &created));
2242 ASSERT_TRUE(created);
2243 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2244
2245 from_file_size = 1020;
2246 expected_total_file_size += from_file_size;
[email protected]141bcc52014-01-27 21:36:002247 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132248 ofu()->Truncate(
2249 AllowUsageIncrease(from_file_size)->context(),
2250 from_file, from_file_size));
2251 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2252
2253 int64 obstacle_file_size = 1;
2254 expected_total_file_size += obstacle_file_size;
[email protected]141bcc52014-01-27 21:36:002255 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132256 ofu()->Truncate(
2257 AllowUsageIncrease(1)->context(),
2258 obstacle_file, obstacle_file_size));
2259 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2260
2261 int64 old_obstacle_file_size = obstacle_file_size;
2262 obstacle_file_size = from_file_size;
2263 from_file_size = 0;
2264 expected_total_file_size -= old_obstacle_file_size;
[email protected]141bcc52014-01-27 21:36:002265 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132266 ofu()->CopyOrMoveFile(
2267 AllowUsageIncrease(
2268 -old_obstacle_file_size - PathCost(from_file))->context(),
2269 from_file, obstacle_file,
[email protected]824d3892013-09-25 13:00:302270 FileSystemOperation::OPTION_NONE,
[email protected]294dd0312012-05-11 07:35:132271 false /* move */));
2272 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2273
[email protected]141bcc52014-01-27 21:36:002274 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132275 ofu()->EnsureFileExists(
2276 AllowUsageIncrease(PathCost(from_file))->context(),
2277 from_file, &created));
2278 ASSERT_TRUE(created);
2279 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2280
2281 from_file_size = 10;
2282 expected_total_file_size += from_file_size;
[email protected]141bcc52014-01-27 21:36:002283 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132284 ofu()->Truncate(
2285 AllowUsageIncrease(from_file_size)->context(),
2286 from_file, from_file_size));
2287 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2288
2289 // quota exceeded even after operation
2290 old_obstacle_file_size = obstacle_file_size;
2291 obstacle_file_size = from_file_size;
2292 from_file_size = 0;
2293 expected_total_file_size -= old_obstacle_file_size;
2294 scoped_ptr<FileSystemOperationContext> context =
2295 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
[email protected]141bcc52014-01-27 21:36:002296 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132297 ofu()->CopyOrMoveFile(
[email protected]824d3892013-09-25 13:00:302298 context.get(), from_file, obstacle_file,
2299 FileSystemOperation::OPTION_NONE,
2300 false /* move */));
[email protected]294dd0312012-05-11 07:35:132301 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2302 context.reset();
2303}
2304
2305TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092306 FileSystemURL dir(CreateURLFromUTF8("dir"));
2307 FileSystemURL file(CreateURLFromUTF8("file"));
2308 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2309 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132310 bool created;
2311
[email protected]141bcc52014-01-27 21:36:002312 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132313 ofu()->EnsureFileExists(
2314 AllowUsageIncrease(PathCost(file))->context(),
2315 file, &created));
2316 ASSERT_TRUE(created);
2317 ASSERT_EQ(0, ComputeTotalFileSize());
2318
[email protected]141bcc52014-01-27 21:36:002319 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132320 ofu()->CreateDirectory(
2321 AllowUsageIncrease(PathCost(dir))->context(),
2322 dir, false, false));
2323 ASSERT_EQ(0, ComputeTotalFileSize());
2324
[email protected]141bcc52014-01-27 21:36:002325 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132326 ofu()->EnsureFileExists(
2327 AllowUsageIncrease(PathCost(dfile1))->context(),
2328 dfile1, &created));
2329 ASSERT_TRUE(created);
2330 ASSERT_EQ(0, ComputeTotalFileSize());
2331
[email protected]141bcc52014-01-27 21:36:002332 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132333 ofu()->EnsureFileExists(
2334 AllowUsageIncrease(PathCost(dfile2))->context(),
2335 dfile2, &created));
2336 ASSERT_TRUE(created);
2337 ASSERT_EQ(0, ComputeTotalFileSize());
2338
[email protected]141bcc52014-01-27 21:36:002339 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132340 ofu()->Truncate(
2341 AllowUsageIncrease(340)->context(),
2342 file, 340));
2343 ASSERT_EQ(340, ComputeTotalFileSize());
2344
[email protected]141bcc52014-01-27 21:36:002345 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132346 ofu()->Truncate(
2347 AllowUsageIncrease(1020)->context(),
2348 dfile1, 1020));
2349 ASSERT_EQ(1360, ComputeTotalFileSize());
2350
[email protected]141bcc52014-01-27 21:36:002351 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132352 ofu()->Truncate(
2353 AllowUsageIncrease(120)->context(),
2354 dfile2, 120));
2355 ASSERT_EQ(1480, ComputeTotalFileSize());
2356
[email protected]141bcc52014-01-27 21:36:002357 ASSERT_EQ(base::File::FILE_OK,
[email protected]294dd0312012-05-11 07:35:132358 ofu()->DeleteFile(
2359 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2360 file));
2361 ASSERT_EQ(1140, ComputeTotalFileSize());
2362
[email protected]141bcc52014-01-27 21:36:002363 ASSERT_EQ(base::File::FILE_OK,
[email protected]07b64872013-02-13 11:46:302364 AsyncFileTestHelper::Remove(
2365 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132366 ASSERT_EQ(0, ComputeTotalFileSize());
2367}
[email protected]7d78be12012-05-24 07:07:262368
2369TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092370 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262371 base::PlatformFile file_handle;
2372 bool created;
2373
2374 // Creating a file.
[email protected]141bcc52014-01-27 21:36:002375 ASSERT_EQ(base::File::FILE_OK,
[email protected]7d78be12012-05-24 07:07:262376 ofu()->EnsureFileExists(
2377 AllowUsageIncrease(PathCost(file))->context(),
2378 file, &created));
2379 ASSERT_TRUE(created);
2380 ASSERT_EQ(0, ComputeTotalFileSize());
2381
2382 // Opening it, which shouldn't change the usage.
[email protected]141bcc52014-01-27 21:36:002383 ASSERT_EQ(base::File::FILE_OK,
[email protected]7d78be12012-05-24 07:07:262384 ofu()->CreateOrOpen(
2385 AllowUsageIncrease(0)->context(), file,
2386 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2387 &file_handle, &created));
2388 ASSERT_EQ(0, ComputeTotalFileSize());
2389 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2390
2391 const int length = 33;
[email protected]141bcc52014-01-27 21:36:002392 ASSERT_EQ(base::File::FILE_OK,
[email protected]7d78be12012-05-24 07:07:262393 ofu()->Truncate(
2394 AllowUsageIncrease(length)->context(), file, length));
2395 ASSERT_EQ(length, ComputeTotalFileSize());
2396
2397 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
[email protected]141bcc52014-01-27 21:36:002398 ASSERT_EQ(base::File::FILE_OK,
[email protected]7d78be12012-05-24 07:07:262399 ofu()->CreateOrOpen(
2400 AllowUsageIncrease(-length)->context(), file,
2401 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2402 &file_handle, &created));
2403 ASSERT_EQ(0, ComputeTotalFileSize());
2404 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2405
2406 // Extending the file again.
[email protected]141bcc52014-01-27 21:36:002407 ASSERT_EQ(base::File::FILE_OK,
[email protected]7d78be12012-05-24 07:07:262408 ofu()->Truncate(
2409 AllowUsageIncrease(length)->context(), file, length));
2410 ASSERT_EQ(length, ComputeTotalFileSize());
2411
2412 // Opening it with TRUNCATED flag, which should truncate the file size.
[email protected]141bcc52014-01-27 21:36:002413 ASSERT_EQ(base::File::FILE_OK,
[email protected]7d78be12012-05-24 07:07:262414 ofu()->CreateOrOpen(
2415 AllowUsageIncrease(-length)->context(), file,
2416 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2417 &file_handle, &created));
2418 ASSERT_EQ(0, ComputeTotalFileSize());
2419 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2420}
[email protected]c4e6f9c2012-09-09 17:42:102421
[email protected]17258682013-06-05 13:38:402422TEST_F(ObfuscatedFileUtilTest, MaybeDropDatabasesAliveCase) {
[email protected]7ade66ee2014-01-16 17:35:532423 MaybeDropDatabasesAliveCaseTestBody();
[email protected]17258682013-06-05 13:38:402424}
2425
2426TEST_F(ObfuscatedFileUtilTest, MaybeDropDatabasesAlreadyDeletedCase) {
[email protected]7ade66ee2014-01-16 17:35:532427 MaybeDropDatabasesAlreadyDeletedCaseTestBody();
[email protected]17258682013-06-05 13:38:402428}
2429
[email protected]501fd0e2013-06-24 12:51:012430TEST_F(ObfuscatedFileUtilTest, DestroyDirectoryDatabase_Isolated) {
[email protected]7ade66ee2014-01-16 17:35:532431 DestroyDirectoryDatabase_IsolatedTestBody();
[email protected]501fd0e2013-06-24 12:51:012432}
2433
2434TEST_F(ObfuscatedFileUtilTest, GetDirectoryDatabase_Isolated) {
[email protected]7ade66ee2014-01-16 17:35:532435 GetDirectoryDatabase_IsolatedTestBody();
[email protected]501fd0e2013-06-24 12:51:012436}
2437
[email protected]4d28164b2013-07-28 08:56:142438TEST_F(ObfuscatedFileUtilTest, MigrationBackFromIsolated) {
[email protected]7ade66ee2014-01-16 17:35:532439 MigrationBackFromIsolatedTestBody();
[email protected]4d28164b2013-07-28 08:56:142440}
2441
[email protected]865098692013-09-18 10:54:372442TEST_F(ObfuscatedFileUtilTest, OpenPathInNonDirectory) {
2443 FileSystemURL file(CreateURLFromUTF8("file"));
2444 FileSystemURL path_in_file(CreateURLFromUTF8("file/file"));
2445 bool created;
2446
[email protected]141bcc52014-01-27 21:36:002447 ASSERT_EQ(base::File::FILE_OK,
[email protected]865098692013-09-18 10:54:372448 ofu()->EnsureFileExists(UnlimitedContext().get(), file, &created));
2449 ASSERT_TRUE(created);
2450
2451 created = false;
2452 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
2453 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
[email protected]141bcc52014-01-27 21:36:002454 ASSERT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY,
[email protected]865098692013-09-18 10:54:372455 ofu()->CreateOrOpen(UnlimitedContext().get(),
2456 path_in_file,
2457 file_flags,
2458 &file_handle,
2459 &created));
2460 ASSERT_FALSE(created);
2461 ASSERT_EQ(base::kInvalidPlatformFileValue, file_handle);
2462
[email protected]141bcc52014-01-27 21:36:002463 ASSERT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY,
[email protected]865098692013-09-18 10:54:372464 ofu()->CreateDirectory(UnlimitedContext().get(),
2465 path_in_file,
2466 false /* exclusive */,
2467 false /* recursive */));
2468}
2469
[email protected]48d2c73c2013-11-25 11:23:022470TEST_F(ObfuscatedFileUtilTest, CreateDirectory_NotADirectoryInRecursive) {
2471 FileSystemURL file(CreateURLFromUTF8("file"));
2472 FileSystemURL path_in_file(CreateURLFromUTF8("file/child"));
2473 FileSystemURL path_in_file_in_file(
2474 CreateURLFromUTF8("file/child/grandchild"));
2475 bool created;
2476
[email protected]141bcc52014-01-27 21:36:002477 ASSERT_EQ(base::File::FILE_OK,
[email protected]48d2c73c2013-11-25 11:23:022478 ofu()->EnsureFileExists(UnlimitedContext().get(), file, &created));
2479 ASSERT_TRUE(created);
2480
[email protected]141bcc52014-01-27 21:36:002481 ASSERT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY,
[email protected]48d2c73c2013-11-25 11:23:022482 ofu()->CreateDirectory(UnlimitedContext().get(),
2483 path_in_file,
2484 false /* exclusive */,
2485 true /* recursive */));
[email protected]141bcc52014-01-27 21:36:002486 ASSERT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY,
[email protected]48d2c73c2013-11-25 11:23:022487 ofu()->CreateDirectory(UnlimitedContext().get(),
2488 path_in_file_in_file,
2489 false /* exclusive */,
2490 true /* recursive */));
2491}
2492
[email protected]7ade66ee2014-01-16 17:35:532493} // namespace content