blob: 387f28e0f5be9296f52874c090f9da597c353543 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 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
[email protected]d4905e2e2011-05-13 21:56:325#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]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3213#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1614#include "base/message_loop.h"
[email protected]d4905e2e2011-05-13 21:56:3215#include "base/platform_file.h"
[email protected]d4905e2e2011-05-13 21:56:3216#include "testing/gtest/include/gtest/gtest.h"
[email protected]07b64872013-02-13 11:46:3017#include "webkit/fileapi/async_file_test_helper.h"
[email protected]6ef0c3912013-01-25 22:46:3418#include "webkit/fileapi/external_mount_points.h"
[email protected]d4905e2e2011-05-13 21:56:3219#include "webkit/fileapi/file_system_context.h"
20#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1021#include "webkit/fileapi/file_system_task_runners.h"
[email protected]0c5ebe32011-08-19 22:37:1622#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]02a60542012-07-24 20:05:3323#include "webkit/fileapi/local_file_system_test_helper.h"
[email protected]c4e6f9c2012-09-09 17:42:1024#include "webkit/fileapi/mock_file_change_observer.h"
[email protected]e7e46732012-01-05 11:45:5525#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4926#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5627#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1628#include "webkit/quota/mock_special_storage_policy.h"
29#include "webkit/quota/quota_manager.h"
30#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3231
[email protected]c4e6f9c2012-09-09 17:42:1032namespace fileapi {
[email protected]d4905e2e2011-05-13 21:56:3233
34namespace {
35
[email protected]a3ef4832013-02-02 05:12:3336bool FileExists(const base::FilePath& path) {
[email protected]d4905e2e2011-05-13 21:56:3237 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
38}
39
[email protected]a3ef4832013-02-02 05:12:3340int64 GetSize(const base::FilePath& path) {
[email protected]81b7f662011-05-26 00:54:4641 int64 size;
42 EXPECT_TRUE(file_util::GetFileSize(path, &size));
43 return size;
44}
45
[email protected]d4905e2e2011-05-13 21:56:3246// After a move, the dest exists and the source doesn't.
47// After a copy, both source and dest exist.
48struct CopyMoveTestCaseRecord {
49 bool is_copy_not_move;
50 const char source_path[64];
51 const char dest_path[64];
52 bool cause_overwrite;
53};
54
55const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
56 // This is the combinatoric set of:
57 // rename vs. same-name
58 // different directory vs. same directory
59 // overwrite vs. no-overwrite
60 // copy vs. move
61 // We can never be called with source and destination paths identical, so
62 // those cases are omitted.
63 {true, "dir0/file0", "dir0/file1", false},
64 {false, "dir0/file0", "dir0/file1", false},
65 {true, "dir0/file0", "dir0/file1", true},
66 {false, "dir0/file0", "dir0/file1", true},
67
68 {true, "dir0/file0", "dir1/file0", false},
69 {false, "dir0/file0", "dir1/file0", false},
70 {true, "dir0/file0", "dir1/file0", true},
71 {false, "dir0/file0", "dir1/file0", true},
72 {true, "dir0/file0", "dir1/file1", false},
73 {false, "dir0/file0", "dir1/file1", false},
74 {true, "dir0/file0", "dir1/file1", true},
75 {false, "dir0/file0", "dir1/file1", true},
76};
77
[email protected]fcc2d5f2011-05-23 22:06:2678struct OriginEnumerationTestRecord {
79 std::string origin_url;
80 bool has_temporary;
81 bool has_persistent;
82};
83
84const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
85 {"http://example.com", false, true},
86 {"http://example1.com", true, false},
87 {"https://example1.com", true, true},
88 {"file://", false, true},
89 {"http://example.com:8000", false, true},
90};
91
[email protected]04c899f2013-02-08 08:28:4292FileSystemURL FileSystemURLAppend(
[email protected]023ad6ab2013-02-17 05:07:2393 const FileSystemURL& url, const base::FilePath::StringType& child) {
[email protected]04c899f2013-02-08 08:28:4294 return FileSystemURL::CreateForTest(
95 url.origin(), url.mount_type(), url.virtual_path().Append(child));
96}
97
98FileSystemURL FileSystemURLAppendUTF8(
99 const FileSystemURL& url, const std::string& child) {
100 return FileSystemURL::CreateForTest(
101 url.origin(),
102 url.mount_type(),
[email protected]023ad6ab2013-02-17 05:07:23103 url.virtual_path().Append(base::FilePath::FromUTF8Unsafe(child)));
[email protected]04c899f2013-02-08 08:28:42104}
105
106FileSystemURL FileSystemURLDirName(const FileSystemURL& url) {
107 return FileSystemURL::CreateForTest(
[email protected]8a020f62013-02-18 08:05:44108 url.origin(), url.mount_type(), VirtualPath::DirName(url.virtual_path()));
[email protected]04c899f2013-02-08 08:28:42109}
110
[email protected]d4905e2e2011-05-13 21:56:32111} // namespace (anonymous)
112
113// TODO(ericu): The vast majority of this and the other FSFU subclass tests
114// could theoretically be shared. It would basically be a FSFU interface
115// compliance test, and only the subclass-specific bits that look into the
116// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49117class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32118 public:
[email protected]7878ece2011-09-05 11:41:49119 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26120 : origin_(GURL("http://www.example.com")),
121 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03122 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16123 test_helper_(origin_, type_),
124 quota_status_(quota::kQuotaStatusUnknown),
125 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32126 }
127
[email protected]7fd8fa4f2013-02-07 05:43:50128 virtual void SetUp() {
[email protected]d4905e2e2011-05-13 21:56:32129 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
130
[email protected]e7e46732012-01-05 11:45:55131 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
132 new quota::MockSpecialStoragePolicy();
133
[email protected]0c5ebe32011-08-19 22:37:16134 quota_manager_ = new quota::QuotaManager(
135 false /* is_incognito */,
136 data_dir_.path(),
137 base::MessageLoopProxy::current(),
138 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55139 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16140
141 // Every time we create a new helper, it creates another context, which
142 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49143 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16144 file_system_context_ = new FileSystemContext(
[email protected]dc57ec82012-08-07 03:50:10145 FileSystemTaskRunners::CreateMockTaskRunners(),
[email protected]6ef0c3912013-01-25 22:46:34146 ExternalMountPoints::CreateRefCounted().get(),
[email protected]e7e46732012-01-05 11:45:55147 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16148 quota_manager_->proxy(),
149 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55150 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16151
[email protected]4f056a872013-01-23 04:24:36152 test_helper_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10153
154 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32155 }
156
[email protected]7fd8fa4f2013-02-07 05:43:50157 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55158 quota_manager_ = NULL;
159 test_helper_.TearDown();
160 }
161
[email protected]294dd0312012-05-11 07:35:13162 scoped_ptr<FileSystemOperationContext> LimitedContext(
163 int64 allowed_bytes_growth) {
164 scoped_ptr<FileSystemOperationContext> context(
165 test_helper_.NewOperationContext());
166 context->set_allowed_bytes_growth(allowed_bytes_growth);
167 return context.Pass();
168 }
169
170 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
171 return LimitedContext(kint64max);
172 }
173
[email protected]02a60542012-07-24 20:05:33174 FileSystemOperationContext* NewContext(
175 LocalFileSystemTestOriginHelper* helper) {
[email protected]c4e6f9c2012-09-09 17:42:10176 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16177 FileSystemOperationContext* context;
178 if (helper)
179 context = helper->NewOperationContext();
180 else
181 context = test_helper_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46182 // Setting allowed_bytes_growth big enough for all tests.
183 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10184 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32185 return context;
186 }
187
[email protected]c4e6f9c2012-09-09 17:42:10188 const ChangeObserverList& change_observers() const {
189 return change_observers_;
190 }
191
192 MockFileChangeObserver* change_observer() {
193 return &change_observer_;
194 }
195
[email protected]0c5ebe32011-08-19 22:37:16196 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49197 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16198 // Use this for tests which need to run in multiple origins; we need a test
199 // helper per origin.
[email protected]02a60542012-07-24 20:05:33200 LocalFileSystemTestOriginHelper* NewHelper(
[email protected]0c5ebe32011-08-19 22:37:16201 const GURL& origin, fileapi::FileSystemType type) {
[email protected]02a60542012-07-24 20:05:33202 LocalFileSystemTestOriginHelper* helper =
203 new LocalFileSystemTestOriginHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16204
[email protected]4f056a872013-01-23 04:24:36205 helper->SetUp(file_system_context_.get());
[email protected]0c5ebe32011-08-19 22:37:16206 return helper;
207 }
208
[email protected]7878ece2011-09-05 11:41:49209 ObfuscatedFileUtil* ofu() {
[email protected]4f056a872013-01-23 04:24:36210 return static_cast<ObfuscatedFileUtil*>(test_helper_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32211 }
212
[email protected]a3ef4832013-02-02 05:12:33213 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35214 return data_dir_.path();
215 }
216
[email protected]0c5ebe32011-08-19 22:37:16217 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26218 return origin_;
219 }
220
221 fileapi::FileSystemType type() const {
222 return type_;
223 }
224
[email protected]294dd0312012-05-11 07:35:13225 int64 ComputeTotalFileSize() {
226 return test_helper_.ComputeCurrentOriginUsage() -
227 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
228 }
229
[email protected]0c5ebe32011-08-19 22:37:16230 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30231 int64 quota = -1;
232 quota_status_ = AsyncFileTestHelper::GetUsageAndQuota(
233 quota_manager_, origin(), test_helper_.type(),
234 &usage_, &quota);
[email protected]0c5ebe32011-08-19 22:37:16235 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
236 }
237
238 void RevokeUsageCache() {
239 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]06226172013-03-14 15:39:31240 usage_cache()->Delete(test_helper_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26241 }
242
243 int64 SizeByQuotaUtil() {
244 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16245 }
246
247 int64 SizeInUsageFile() {
[email protected]8eee1b62013-01-08 02:26:44248 MessageLoop::current()->RunUntilIdle();
[email protected]06226172013-03-14 15:39:31249 return usage_cache()->GetUsage(test_helper_.GetUsageCachePath());
[email protected]0c5ebe32011-08-19 22:37:16250 }
251
[email protected]13f92f6e2012-08-13 07:39:14252 bool PathExists(const FileSystemURL& url) {
253 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21254 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33255 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21256 base::PlatformFileError error = ofu()->GetFileInfo(
257 context.get(), url, &file_info, &platform_path);
258 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14259 }
260
261 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30262 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14263 }
264
[email protected]0c5ebe32011-08-19 22:37:16265 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31266 FileSystemUsageCache* usage_cache() {
267 return test_helper_.usage_cache();
268 }
[email protected]0c5ebe32011-08-19 22:37:16269
[email protected]949f25a2012-06-27 01:53:09270 FileSystemURL CreateURLFromUTF8(const std::string& path) {
271 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50272 }
273
[email protected]949f25a2012-06-27 01:53:09274 int64 PathCost(const FileSystemURL& url) {
275 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13276 }
277
[email protected]a3ef4832013-02-02 05:12:33278 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]949f25a2012-06-27 01:53:09279 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50280 }
281
[email protected]d4905e2e2011-05-13 21:56:32282 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39283 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16284 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33285 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49286 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09287 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32288
289 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33290 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49291 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09292 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32293 EXPECT_EQ(data_path, local_path);
294 EXPECT_TRUE(FileExists(data_path));
295 EXPECT_EQ(0, GetSize(data_path));
296
297 const char data[] = "test data";
298 const int length = arraysize(data) - 1;
299
300 if (base::kInvalidPlatformFileValue == file_handle) {
301 bool created = true;
[email protected]403ada82013-01-08 07:51:39302 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32303 file_handle = base::CreatePlatformFile(
304 data_path,
305 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
306 &created,
307 &error);
[email protected]81b7f662011-05-26 00:54:46308 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32309 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
310 EXPECT_FALSE(created);
311 }
312 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
313 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
314
315 base::PlatformFileInfo file_info1;
316 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16317 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49318 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09319 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32320 EXPECT_EQ(data_path, local_path);
321
322 EXPECT_FALSE(file_info0.is_directory);
323 EXPECT_FALSE(file_info1.is_directory);
324 EXPECT_FALSE(file_info0.is_symbolic_link);
325 EXPECT_FALSE(file_info1.is_symbolic_link);
326 EXPECT_EQ(0, file_info0.size);
327 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32328 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32329
[email protected]0c5ebe32011-08-19 22:37:16330 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49331 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09332 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32333 EXPECT_EQ(length * 2, GetSize(data_path));
334
[email protected]0c5ebe32011-08-19 22:37:16335 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49336 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09337 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16338 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32339 }
340
341 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09342 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33343 const std::set<base::FilePath::StringType>& files,
344 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32345 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33346 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32347 for (iter = files.begin(); iter != files.end(); ++iter) {
348 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16349 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32350 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49351 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42352 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32353 &created));
354 ASSERT_FALSE(created);
355 }
356 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16357 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14358 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42359 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32360 }
361 }
362
[email protected]294dd0312012-05-11 07:35:13363 class UsageVerifyHelper {
364 public:
365 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33366 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13367 int64 expected_usage)
368 : context_(context.Pass()),
369 test_helper_(test_helper),
370 expected_usage_(expected_usage) {}
371
372 ~UsageVerifyHelper() {
[email protected]8eee1b62013-01-08 02:26:44373 MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13374 Check();
375 }
376
377 FileSystemOperationContext* context() {
378 return context_.get();
379 }
380
381 private:
382 void Check() {
383 ASSERT_EQ(expected_usage_,
384 test_helper_->GetCachedOriginUsage());
385 }
386
387 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33388 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13389 int64 expected_usage_;
390 };
391
392 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
393 int64 usage = test_helper_.GetCachedOriginUsage();
394 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
395 LimitedContext(requested_growth),
396 &test_helper_, usage + requested_growth));
397 }
398
399 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
400 int64 usage = test_helper_.GetCachedOriginUsage();
401 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
402 LimitedContext(requested_growth - 1), &test_helper_, usage));
403 }
404
[email protected]d4905e2e2011-05-13 21:56:32405 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09406 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33407 std::set<base::FilePath::StringType>* files,
408 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32409 scoped_ptr<FileSystemOperationContext> context;
[email protected]d4905e2e2011-05-13 21:56:32410 std::vector<base::FileUtilProxy::Entry> entries;
411 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30412 AsyncFileTestHelper::ReadDirectory(
413 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32414 EXPECT_EQ(0UL, entries.size());
415
416 files->clear();
[email protected]89ee4272011-05-16 18:45:17417 files->insert(FILE_PATH_LITERAL("first"));
418 files->insert(FILE_PATH_LITERAL("second"));
419 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32420 directories->clear();
[email protected]89ee4272011-05-16 18:45:17421 directories->insert(FILE_PATH_LITERAL("fourth"));
422 directories->insert(FILE_PATH_LITERAL("fifth"));
423 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33424 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32425 for (iter = files->begin(); iter != files->end(); ++iter) {
426 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16427 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32428 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49429 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50430 context.get(),
[email protected]04c899f2013-02-08 08:28:42431 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50432 &created));
[email protected]d4905e2e2011-05-13 21:56:32433 ASSERT_TRUE(created);
434 }
435 for (iter = directories->begin(); iter != directories->end(); ++iter) {
436 bool exclusive = true;
437 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16438 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32439 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49440 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42441 context.get(),
442 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09443 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32444 }
[email protected]949f25a2012-06-27 01:53:09445 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32446 }
447
[email protected]949f25a2012-06-27 01:53:09448 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33449 std::set<base::FilePath::StringType> files;
450 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09451 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32452
453 scoped_ptr<FileSystemOperationContext> context;
454 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16455 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32456 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30457 AsyncFileTestHelper::ReadDirectory(
458 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32459 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
460 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10461 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32462 for (entry_iter = entries.begin(); entry_iter != entries.end();
463 ++entry_iter) {
464 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31465 std::set<base::FilePath::StringType>::iterator iter =
466 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32467 if (iter != files.end()) {
468 EXPECT_FALSE(entry.is_directory);
469 files.erase(iter);
470 continue;
471 }
[email protected]89ee4272011-05-16 18:45:17472 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32473 EXPECT_FALSE(directories.end() == iter);
474 EXPECT_TRUE(entry.is_directory);
475 directories.erase(iter);
476 }
477 }
478
[email protected]949f25a2012-06-27 01:53:09479 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41480 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32481 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16482
[email protected]2517cfa2011-08-25 05:12:41483 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32484 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49485 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09486 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10487 // Currently we fire no change notifications for Touch.
488 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33489 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32490 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16491 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49492 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09493 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32494 // We compare as time_t here to lower our resolution, to avoid false
495 // negatives caused by conversion to the local filesystem's native
496 // representation and back.
497 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
498
[email protected]0c5ebe32011-08-19 22:37:16499 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32500 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41501 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32502 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49503 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09504 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10505 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16506 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49507 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09508 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32509 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49510 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41511 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32512 }
513
[email protected]81b7f662011-05-26 00:54:46514 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23515 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46516 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33517 base::FilePath root_file_path = source_dir.path();
518 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09519 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46520 int64 src_file_length = 87;
521
522 base::PlatformFileError error_code;
523 bool created = false;
524 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
525 base::PlatformFile file_handle =
526 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50527 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46528 EXPECT_TRUE(created);
529 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
530 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
531 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
532 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
533
534 scoped_ptr<FileSystemOperationContext> context;
535
536 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16537 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46538 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09539 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46540 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10541
542 // We must have observed one (and only one) create_file_count.
543 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
544 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46545 }
546
[email protected]0c5ebe32011-08-19 22:37:16547 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09548 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16549 if (!overwrite) {
550 // Verify that file creation requires sufficient quota for the path.
551 context.reset(NewContext(NULL));
552 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
553 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13554 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09555 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16556 }
557
558 context.reset(NewContext(NULL));
559 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46560 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13561 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09562 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16563
[email protected]13f92f6e2012-08-13 07:39:14564 EXPECT_TRUE(PathExists(dest_url));
565 EXPECT_FALSE(DirectoryExists(dest_url));
566
[email protected]0c5ebe32011-08-19 22:37:16567 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46568 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33569 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49570 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09571 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50572 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46573 EXPECT_TRUE(FileExists(data_path));
574 EXPECT_EQ(src_file_length, GetSize(data_path));
575
576 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09577 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46578 }
579
[email protected]949f25a2012-06-27 01:53:09580 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03581 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
582 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09583 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
584 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03585 }
586
[email protected]949f25a2012-06-27 01:53:09587 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03588 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33589 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03590 base::PlatformFileInfo file_info;
591 context.reset(NewContext(NULL));
592 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09593 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10594 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03595 return file_info.last_modified;
596 }
597
[email protected]949f25a2012-06-27 01:53:09598 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03599 bool copy,
600 bool overwrite) {
601 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42602 const FileSystemURL src_dir_url(
603 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
604 const FileSystemURL dest_dir_url(
605 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03606
[email protected]04c899f2013-02-08 08:28:42607 const FileSystemURL src_file_url(
608 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
609 const FileSystemURL dest_file_url(
610 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03611
612 context.reset(NewContext(NULL));
613 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09614 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03615 context.reset(NewContext(NULL));
616 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09617 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03618
619 bool created = false;
620 context.reset(NewContext(NULL));
621 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09622 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03623 if (overwrite) {
624 context.reset(NewContext(NULL));
625 EXPECT_EQ(base::PLATFORM_FILE_OK,
626 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09627 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03628 }
629
[email protected]949f25a2012-06-27 01:53:09630 ClearTimestamp(src_dir_url);
631 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03632 context.reset(NewContext(NULL));
633 EXPECT_EQ(base::PLATFORM_FILE_OK,
634 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09635 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03636 copy));
[email protected]fad625e2f2011-12-08 05:38:03637 if (copy)
[email protected]949f25a2012-06-27 01:53:09638 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03639 else
[email protected]949f25a2012-06-27 01:53:09640 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
641 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03642 }
643
[email protected]ecdfd6c52012-04-11 13:35:44644 int64 ComputeCurrentUsage() {
[email protected]06226172013-03-14 15:39:31645 return test_helper_.ComputeCurrentOriginUsage() -
646 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]ecdfd6c52012-04-11 13:35:44647 }
648
[email protected]02a60542012-07-24 20:05:33649 const LocalFileSystemTestOriginHelper& test_helper() const {
650 return test_helper_;
651 }
[email protected]45ea0fbbb2012-02-27 22:28:49652
[email protected]07b64872013-02-13 11:46:30653 FileSystemContext* file_system_context() {
654 return test_helper_.file_system_context();
655 }
656
[email protected]d4905e2e2011-05-13 21:56:32657 private:
[email protected]ea1a3f62012-11-16 20:34:23658 base::ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01659 MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16660 scoped_refptr<quota::QuotaManager> quota_manager_;
661 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26662 GURL origin_;
663 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03664 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]02a60542012-07-24 20:05:33665 LocalFileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16666 quota::QuotaStatusCode quota_status_;
667 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10668 MockFileChangeObserver change_observer_;
669 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32670
[email protected]7878ece2011-09-05 11:41:49671 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32672};
673
[email protected]7878ece2011-09-05 11:41:49674TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32675 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
676 bool created;
[email protected]949f25a2012-06-27 01:53:09677 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16678 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32679 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
680
681 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49682 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09683 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32684 &created));
685
[email protected]0c5ebe32011-08-19 22:37:16686 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32687 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09688 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32689
[email protected]949f25a2012-06-27 01:53:09690 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32691
[email protected]c4e6f9c2012-09-09 17:42:10692 EXPECT_TRUE(change_observer()->HasNoChange());
693
[email protected]0c5ebe32011-08-19 22:37:16694 // Verify that file creation requires sufficient quota for the path.
695 context.reset(NewContext(NULL));
696 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09697 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16698 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49699 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09700 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16701
702 context.reset(NewContext(NULL));
703 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09704 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32705 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49706 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09707 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32708 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10709 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32710 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
711
[email protected]949f25a2012-06-27 01:53:09712 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32713
[email protected]0c5ebe32011-08-19 22:37:16714 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33715 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09717 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32718 EXPECT_TRUE(file_util::PathExists(local_path));
719
[email protected]0c5ebe32011-08-19 22:37:16720 // Verify that deleting a file isn't stopped by zero quota, and that it frees
721 // up quote from its path.
722 context.reset(NewContext(NULL));
723 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32724 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09725 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10726 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32727 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09728 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16729 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32730
[email protected]0c5ebe32011-08-19 22:37:16731 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32732 bool exclusive = true;
733 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09734 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50735 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42736 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49737 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09738 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10739 // The oepration created 3 directories recursively.
740 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32741
[email protected]0c5ebe32011-08-19 22:37:16742 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32743 file_handle = base::kInvalidPlatformFileValue;
744 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49745 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09746 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32747 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10748 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32749 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
750
[email protected]949f25a2012-06-27 01:53:09751 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32752
[email protected]0c5ebe32011-08-19 22:37:16753 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49754 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09755 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32756 EXPECT_TRUE(file_util::PathExists(local_path));
757
[email protected]0c5ebe32011-08-19 22:37:16758 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32759 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09760 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10761 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32762 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10763
764 // Make sure we have no unexpected changes.
765 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32766}
767
[email protected]7878ece2011-09-05 11:41:49768TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32769 bool created = false;
[email protected]949f25a2012-06-27 01:53:09770 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16771 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32772
773 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09774 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32775
[email protected]0c5ebe32011-08-19 22:37:16776 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32777 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09778 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32779 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10780 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32781
[email protected]0c5ebe32011-08-19 22:37:16782 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33783 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49784 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09785 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32786 EXPECT_EQ(0, GetSize(local_path));
787
[email protected]0c5ebe32011-08-19 22:37:16788 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49789 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09790 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10791 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32792 EXPECT_EQ(10, GetSize(local_path));
793
[email protected]0c5ebe32011-08-19 22:37:16794 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49795 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09796 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32797 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10798 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32799
[email protected]13f92f6e2012-08-13 07:39:14800 EXPECT_FALSE(DirectoryExists(url));
801 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10802
803 // Make sure we have no unexpected changes.
804 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32805}
806
[email protected]ecdfd6c52012-04-11 13:35:44807TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
808 bool created = false;
[email protected]949f25a2012-06-27 01:53:09809 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44810
811 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13812 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09813 AllowUsageIncrease(PathCost(url))->context(),
814 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44815 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13816 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44817
[email protected]ecdfd6c52012-04-11 13:35:44818 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13819 ofu()->Truncate(
820 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09821 url, 1020));
[email protected]294dd0312012-05-11 07:35:13822 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44823
[email protected]ecdfd6c52012-04-11 13:35:44824 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13825 ofu()->Truncate(
826 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09827 url, 0));
[email protected]294dd0312012-05-11 07:35:13828 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44829
[email protected]ecdfd6c52012-04-11 13:35:44830 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13831 ofu()->Truncate(
832 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09833 url, 1021));
[email protected]294dd0312012-05-11 07:35:13834 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44835
[email protected]ecdfd6c52012-04-11 13:35:44836 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13837 ofu()->Truncate(
838 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09839 url, 1020));
[email protected]294dd0312012-05-11 07:35:13840 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44841
[email protected]ecdfd6c52012-04-11 13:35:44842 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13843 ofu()->Truncate(
844 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09845 url, 1020));
[email protected]294dd0312012-05-11 07:35:13846 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44847
[email protected]294dd0312012-05-11 07:35:13848 // quota exceeded
849 {
850 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
851 helper->context()->set_allowed_bytes_growth(
852 helper->context()->allowed_bytes_growth() - 1);
853 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09854 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13855 ASSERT_EQ(1019, ComputeTotalFileSize());
856 }
[email protected]ecdfd6c52012-04-11 13:35:44857
858 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33859 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44860 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13861 ofu()->GetLocalFilePath(
862 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09863 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44864 ASSERT_FALSE(local_path.empty());
865 ASSERT_TRUE(file_util::Delete(local_path, false));
866
[email protected]ecdfd6c52012-04-11 13:35:44867 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13868 ofu()->Truncate(
869 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09870 url, 1234));
[email protected]294dd0312012-05-11 07:35:13871 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44872}
873
[email protected]7878ece2011-09-05 11:41:49874TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09875 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32876 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16877 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32878 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49879 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09880 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10881 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32882
[email protected]0c5ebe32011-08-19 22:37:16883 // Verify that file creation requires sufficient quota for the path.
884 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09885 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32886 created = false;
[email protected]0c5ebe32011-08-19 22:37:16887 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09888 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16889 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09890 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16891 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10892 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16893
894 context.reset(NewContext(NULL));
895 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09896 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32897 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09898 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32899 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10900 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32901
[email protected]949f25a2012-06-27 01:53:09902 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32903
[email protected]0c5ebe32011-08-19 22:37:16904 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32905 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09906 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32907 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10908 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32909
910 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09911 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16912 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32913 bool exclusive = true;
914 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49915 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09916 context.get(),
[email protected]04c899f2013-02-08 08:28:42917 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09918 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10919 // 2 directories: path/ and path/to.
920 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32921
[email protected]0c5ebe32011-08-19 22:37:16922 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32923 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09924 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32925 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14926 EXPECT_FALSE(DirectoryExists(url));
927 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10928 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32929}
930
[email protected]7878ece2011-09-05 11:41:49931TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16932 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32933
934 bool exclusive = false;
935 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09936 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49937 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09938 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32939
[email protected]0c5ebe32011-08-19 22:37:16940 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32941 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08942 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32943
[email protected]949f25a2012-06-27 01:53:09944 FileSystemURL root = CreateURLFromUTF8("");
[email protected]13f92f6e2012-08-13 07:39:14945 EXPECT_FALSE(DirectoryExists(url));
946 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16947 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49948 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32949
[email protected]0c5ebe32011-08-19 22:37:16950 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32951 exclusive = false;
952 recursive = true;
[email protected]7878ece2011-09-05 11:41:49953 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09954 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10955 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32956
[email protected]13f92f6e2012-08-13 07:39:14957 EXPECT_TRUE(DirectoryExists(url));
958 EXPECT_TRUE(PathExists(url));
959
[email protected]0c5ebe32011-08-19 22:37:16960 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49961 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42962 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14963
[email protected]0c5ebe32011-08-19 22:37:16964 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09965 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42966 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32967
968 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16969 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06970 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08971 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42972 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10973 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32974
975 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33976 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49977 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09978 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32979 EXPECT_TRUE(local_path.empty());
980 EXPECT_TRUE(file_info.is_directory);
981 EXPECT_FALSE(file_info.is_symbolic_link);
982
983 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16984 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49985 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09986 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10987 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32988
989 exclusive = true;
990 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16991 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49992 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09993 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10994 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32995
[email protected]0c5ebe32011-08-19 22:37:16996 // Verify that deleting a directory isn't stopped by zero quota, and that it
997 // frees up quota from its path.
998 context.reset(NewContext(NULL));
999 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:081000 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101001 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091002 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161003 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321004
[email protected]949f25a2012-06-27 01:53:091005 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321006
[email protected]13f92f6e2012-08-13 07:39:141007 EXPECT_FALSE(DirectoryExists(url));
1008 EXPECT_FALSE(PathExists(url));
1009
[email protected]0c5ebe32011-08-19 22:37:161010 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091011 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491012 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091013 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321014
[email protected]0c5ebe32011-08-19 22:37:161015 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321016 exclusive = true;
1017 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161018 context.reset(NewContext(NULL));
1019 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091020 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491021 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091022 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101023 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161024
1025 context.reset(NewContext(NULL));
1026 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091027 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491028 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091029 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101030 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321031
[email protected]13f92f6e2012-08-13 07:39:141032 EXPECT_TRUE(DirectoryExists(url));
1033 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321034
1035 exclusive = true;
1036 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141037 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491038 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091039 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101040 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321041
1042 exclusive = true;
1043 recursive = false;
[email protected]949f25a2012-06-27 01:53:091044 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141045 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491046 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091047 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101048 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321049
[email protected]949f25a2012-06-27 01:53:091050 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321051
[email protected]13f92f6e2012-08-13 07:39:141052 EXPECT_FALSE(DirectoryExists(url));
1053 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321054
1055 exclusive = true;
1056 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141057 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491058 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091059 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101060 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321061
[email protected]13f92f6e2012-08-13 07:39:141062 EXPECT_TRUE(DirectoryExists(url));
1063 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321064
1065 exclusive = true;
1066 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141067 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491068 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091069 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101070 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321071}
1072
[email protected]7878ece2011-09-05 11:41:491073TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161074 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321075 bool exclusive = true;
1076 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091077 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491078 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091079 context.get(), url, exclusive, recursive));
1080 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321081}
1082
[email protected]7878ece2011-09-05 11:41:491083TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]949f25a2012-06-27 01:53:091084 TestReadDirectoryHelper(CreateURLFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:321085}
1086
[email protected]7878ece2011-09-05 11:41:491087TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091088 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321089}
1090
[email protected]7878ece2011-09-05 11:41:491091TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091092 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161093 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321094
1095 bool created = false;
1096 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091097 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321098 ASSERT_TRUE(created);
1099
[email protected]d4905e2e2011-05-13 21:56:321100 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]1a647202013-01-21 15:32:251101 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301102 AsyncFileTestHelper::ReadDirectory(
1103 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321104
[email protected]949f25a2012-06-27 01:53:091105 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321106}
1107
[email protected]7878ece2011-09-05 11:41:491108TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091109 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161110 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411111
1112 base::Time last_access_time = base::Time::Now();
1113 base::Time last_modified_time = base::Time::Now();
1114
1115 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321116 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491117 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091118 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321119
[email protected]2517cfa2011-08-25 05:12:411120 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161121 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411122 bool created = false;
1123 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091124 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411125 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091126 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411127
1128 // Now test a directory:
1129 context.reset(NewContext(NULL));
1130 bool exclusive = true;
1131 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091132 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491133 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091134 url, exclusive, recursive));
1135 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161136}
1137
[email protected]7878ece2011-09-05 11:41:491138TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091139 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161140 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1141
[email protected]949f25a2012-06-27 01:53:091142 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161143 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411144 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161145 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091146 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411147 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161148 context->set_allowed_bytes_growth(1024);
1149 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091150 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411151 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091152 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161153 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1154
1155 context->set_allowed_bytes_growth(1024);
1156 bool exclusive = true;
1157 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091158 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331159 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091160 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161161 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421162 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1163 for (iterator iter = components.begin();
1164 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491165 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331166 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161167 }
1168 context.reset(NewContext(NULL));
1169 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091171 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161172 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321173}
1174
[email protected]7878ece2011-09-05 11:41:491175TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091176 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1177 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161178 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321179
1180 bool is_copy_not_move = false;
1181 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091182 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321183 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101184 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161185 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321186 is_copy_not_move = true;
1187 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091188 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321189 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101190 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091191 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321192 bool exclusive = true;
1193 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161194 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491195 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091196 context.get(),
[email protected]04c899f2013-02-08 08:28:421197 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091198 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101199 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321200 is_copy_not_move = false;
1201 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091202 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321203 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101204 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161205 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321206 is_copy_not_move = true;
1207 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091208 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321209 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101210 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321211}
1212
[email protected]7878ece2011-09-05 11:41:491213TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321214 const int64 kSourceLength = 5;
1215 const int64 kDestLength = 50;
1216
1217 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1218 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1219 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1220 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1221 test_case.is_copy_not_move);
1222 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1223 test_case.source_path);
1224 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1225 test_case.dest_path);
1226 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1227 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161228 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321229
1230 bool exclusive = false;
1231 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091232 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1233 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321234
[email protected]0c5ebe32011-08-19 22:37:161235 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491236 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091237 context.get(),
[email protected]04c899f2013-02-08 08:28:421238 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091239 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161240 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491241 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091242 context.get(),
[email protected]04c899f2013-02-08 08:28:421243 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091244 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321245
[email protected]d4905e2e2011-05-13 21:56:321246 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161247 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321248 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091249 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321250 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161251 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321252 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091253 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321254
1255 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161256 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321257 created = false;
1258 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091259 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321260 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161261 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321262 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091263 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321264 }
1265
[email protected]0c5ebe32011-08-19 22:37:161266 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491267 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091268 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101269
[email protected]d4905e2e2011-05-13 21:56:321270 if (test_case.is_copy_not_move) {
1271 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331272 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161273 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491274 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091275 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321276 EXPECT_EQ(kSourceLength, file_info.size);
1277 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091278 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321279 } else {
1280 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331281 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161282 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491283 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091284 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321285 }
1286 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331287 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491288 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091289 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321290 EXPECT_EQ(kSourceLength, file_info.size);
1291
1292 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091293 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321294 }
1295}
1296
[email protected]7878ece2011-09-05 11:41:491297TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091298 FileSystemURL src_url = CreateURLFromUTF8("src path");
1299 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161300 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1301 bool created = false;
[email protected]7878ece2011-09-05 11:41:491302 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091303 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161304
1305 bool is_copy = true;
1306 // Copy, no overwrite.
1307 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091308 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161309 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091310 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161311 context.reset(NewContext(NULL));
1312 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091313 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161314 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091315 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161316
1317 // Copy, with overwrite.
1318 context.reset(NewContext(NULL));
1319 context->set_allowed_bytes_growth(0);
1320 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091321 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161322}
1323
[email protected]7878ece2011-09-05 11:41:491324TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091325 FileSystemURL src_url = CreateURLFromUTF8("src path");
1326 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161327 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1328 bool created = false;
[email protected]7878ece2011-09-05 11:41:491329 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091330 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161331
1332 bool is_copy = false;
1333 // Move, rename, no overwrite.
1334 context.reset(NewContext(NULL));
1335 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091336 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1337 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161338 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091339 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161340 context.reset(NewContext(NULL));
1341 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091342 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1343 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161344 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091345 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161346
1347 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491348 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091349 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161350
1351 // Move, rename, with overwrite.
1352 context.reset(NewContext(NULL));
1353 context->set_allowed_bytes_growth(0);
1354 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091355 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161356}
1357
[email protected]7878ece2011-09-05 11:41:491358TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091359 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161360 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1361 bool created = false;
[email protected]7878ece2011-09-05 11:41:491362 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091363 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161364
1365 bool exclusive = true;
1366 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091367 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161368 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491369 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091370 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161371
[email protected]04c899f2013-02-08 08:28:421372 FileSystemURL dest_url = FileSystemURLAppend(
1373 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161374
1375 bool is_copy = false;
1376 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1377 // Move, no rename, no overwrite.
1378 context.reset(NewContext(NULL));
1379 context->set_allowed_bytes_growth(allowed_bytes_growth);
1380 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091381 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161382 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1383
1384 // Move, no rename, with overwrite.
1385 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491386 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091387 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161388 context.reset(NewContext(NULL));
1389 context->set_allowed_bytes_growth(allowed_bytes_growth);
1390 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091391 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161392 EXPECT_EQ(
1393 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091394 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161395 context->allowed_bytes_growth());
1396}
1397
[email protected]7878ece2011-09-05 11:41:491398TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461399 TestCopyInForeignFileHelper(false /* overwrite */);
1400 TestCopyInForeignFileHelper(true /* overwrite */);
1401}
1402
[email protected]7878ece2011-09-05 11:41:491403TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161404 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091405 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321406 bool exclusive = true;
1407 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491408 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091409 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321410
[email protected]a3ef4832013-02-02 05:12:331411 std::set<base::FilePath::StringType> files;
1412 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091413 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321414
[email protected]949f25a2012-06-27 01:53:091415 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321416
[email protected]13f92f6e2012-08-13 07:39:141417 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321418 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301419 AsyncFileTestHelper::Copy(
1420 test_helper().file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321421
[email protected]949f25a2012-06-27 01:53:091422 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141423 EXPECT_TRUE(DirectoryExists(src_url));
1424 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321425 recursive = true;
1426 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301427 AsyncFileTestHelper::Remove(
1428 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141429 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321430}
[email protected]6b931152011-05-20 21:02:351431
[email protected]7878ece2011-09-05 11:41:491432TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1433 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1434 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161435 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261436 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161437 EXPECT_EQ(origin(), enumerator->Next());
1438 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1439 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1440 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261441 EXPECT_EQ(GURL(), enumerator->Next());
1442 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1443 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1444
1445 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161446 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261447
1448 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1449 SCOPED_TRACE(testing::Message() <<
1450 "Validating kOriginEnumerationTestRecords " << i);
1451 const OriginEnumerationTestRecord& record =
1452 kOriginEnumerationTestRecords[i];
1453 GURL origin_url(record.origin_url);
1454 origins_expected.insert(origin_url);
1455 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331456 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161457 NewHelper(origin_url, kFileSystemTypeTemporary));
1458 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261459 bool created = false;
1460 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501461 ofu()->EnsureFileExists(
1462 context.get(),
[email protected]949f25a2012-06-27 01:53:091463 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501464 &created));
[email protected]fcc2d5f2011-05-23 22:06:261465 EXPECT_TRUE(created);
1466 }
1467 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331468 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161469 NewHelper(origin_url, kFileSystemTypePersistent));
1470 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261471 bool created = false;
1472 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501473 ofu()->EnsureFileExists(
1474 context.get(),
[email protected]949f25a2012-06-27 01:53:091475 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501476 &created));
[email protected]fcc2d5f2011-05-23 22:06:261477 EXPECT_TRUE(created);
1478 }
1479 }
[email protected]7878ece2011-09-05 11:41:491480 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261481 EXPECT_TRUE(enumerator.get());
1482 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161483 GURL origin_url;
1484 while (!(origin_url = enumerator->Next()).is_empty()) {
1485 origins_found.insert(origin_url);
1486 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261487 bool found = false;
1488 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1489 ++i) {
1490 const OriginEnumerationTestRecord& record =
1491 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161492 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261493 continue;
1494 found = true;
1495 EXPECT_EQ(record.has_temporary,
1496 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1497 EXPECT_EQ(record.has_persistent,
1498 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1499 }
[email protected]0c5ebe32011-08-19 22:37:161500 // Deal with the default filesystem created by the test helper.
1501 if (!found && origin_url == origin()) {
1502 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1503 EXPECT_EQ(true,
1504 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181505 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161506 found = true;
1507 }
[email protected]fcc2d5f2011-05-23 22:06:261508 EXPECT_TRUE(found);
1509 }
1510
1511 std::set<GURL> diff;
1512 std::set_symmetric_difference(origins_expected.begin(),
1513 origins_expected.end(), origins_found.begin(), origins_found.end(),
1514 inserter(diff, diff.begin()));
1515 EXPECT_TRUE(diff.empty());
1516}
[email protected]0c5ebe32011-08-19 22:37:161517
[email protected]7878ece2011-09-05 11:41:491518TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161519 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1520
1521 int64 expected_quota = 0;
1522
[email protected]f83b5b72012-01-27 10:26:561523 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011524 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561525 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331526 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501527 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161528 if (test_case.is_directory) {
1529 bool exclusive = true;
1530 bool recursive = false;
1531 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091532 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501533 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161534 } else {
1535 bool created = false;
1536 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091537 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501538 &created));
[email protected]0c5ebe32011-08-19 22:37:161539 ASSERT_TRUE(created);
1540 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501541 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091542 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501543 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161544 expected_quota += test_case.data_file_size;
1545 }
1546 }
[email protected]022d2702012-05-14 16:04:261547
1548 // Usually raw size in usage cache and the usage returned by QuotaUtil
1549 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161550 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261551 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1552
[email protected]0c5ebe32011-08-19 22:37:161553 RevokeUsageCache();
1554 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261555 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1556
1557 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161558 GetUsageFromQuotaManager();
1559 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261560 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161561 EXPECT_EQ(expected_quota, usage());
1562}
[email protected]34583332011-08-31 08:59:471563
[email protected]7878ece2011-09-05 11:41:491564TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091565 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1566 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471567
1568 scoped_ptr<FileSystemOperationContext> context;
1569 base::PlatformFile file;
1570 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331571 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471572 bool created = false;
1573
1574 // Create a non-empty file.
1575 context.reset(NewContext(NULL));
1576 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491577 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471578 EXPECT_TRUE(created);
1579 context.reset(NewContext(NULL));
1580 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491581 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471582 context.reset(NewContext(NULL));
1583 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491584 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471585 context.get(), kPath1, &file_info, &data_path));
1586 EXPECT_EQ(10, file_info.size);
1587
1588 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491589 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471590
1591 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141592 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471593 context.reset(NewContext(NULL));
1594 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491595 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471596 EXPECT_TRUE(created);
1597 context.reset(NewContext(NULL));
1598 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491599 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471600 context.get(), kPath1, &file_info, &data_path));
1601 EXPECT_EQ(0, file_info.size);
1602
1603 // Make another broken file to |kPath2|.
1604 context.reset(NewContext(NULL));
1605 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491606 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471607 EXPECT_TRUE(created);
1608
1609 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491610 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471611
1612 // Repair broken |kPath1|.
1613 context.reset(NewContext(NULL));
1614 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491615 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471616 base::Time::Now()));
1617 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491618 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471619 EXPECT_TRUE(created);
1620
1621 // Copy from sound |kPath1| to broken |kPath2|.
1622 context.reset(NewContext(NULL));
1623 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491624 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501625 true /* copy */));
[email protected]34583332011-08-31 08:59:471626
[email protected]7878ece2011-09-05 11:41:491627 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471628 context.reset(NewContext(NULL));
1629 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491630 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471631 context.get(), kPath1,
1632 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1633 &file, &created));
1634 EXPECT_TRUE(created);
1635 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1636 EXPECT_EQ(0, file_info.size);
1637 EXPECT_TRUE(base::ClosePlatformFile(file));
1638}
[email protected]9dfdc0e32011-09-02 06:07:441639
[email protected]7878ece2011-09-05 11:41:491640TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091641 const FileSystemURL kPath[] = {
1642 CreateURLFromUTF8("foo"),
1643 CreateURLFromUTF8("bar"),
1644 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441645 };
[email protected]a3ef4832013-02-02 05:12:331646 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441647 scoped_ptr<FileSystemOperationContext> context;
1648
1649 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1650 bool created = false;
1651 context.reset(NewContext(NULL));
1652 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491653 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441654 EXPECT_TRUE(created);
1655 }
1656
[email protected]9dfdc0e32011-09-02 06:07:441657 std::vector<base::FileUtilProxy::Entry> entries;
1658 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301659 AsyncFileTestHelper::ReadDirectory(
1660 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441661 EXPECT_EQ(3u, entries.size());
1662
[email protected]a3ef4832013-02-02 05:12:331663 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441664 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491665 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441666 EXPECT_TRUE(file_util::Delete(local_path, false));
1667
[email protected]9dfdc0e32011-09-02 06:07:441668 entries.clear();
1669 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301670 AsyncFileTestHelper::ReadDirectory(
1671 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441672 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1673}
[email protected]fad625e2f2011-12-08 05:38:031674
1675TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1676 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091677 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031678
1679 // Create working directory.
1680 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091681 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031682
1683 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421684 FileSystemURL url(FileSystemURLAppendUTF8(
1685 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031686 bool created = false;
[email protected]949f25a2012-06-27 01:53:091687 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031688 context.reset(NewContext(NULL));
1689 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091690 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031691 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091692 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031693
1694 // non create case.
1695 created = true;
[email protected]949f25a2012-06-27 01:53:091696 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031697 context.reset(NewContext(NULL));
1698 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091699 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031700 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091701 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031702
1703 // fail case.
[email protected]04c899f2013-02-08 08:28:421704 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031705 context.reset(NewContext(NULL));
1706 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091707 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031708
[email protected]949f25a2012-06-27 01:53:091709 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031710 context.reset(NewContext(NULL));
1711 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091712 ofu()->EnsureFileExists(context.get(), url, &created));
1713 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031714
1715 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421716 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391717 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031718 created = false;
[email protected]949f25a2012-06-27 01:53:091719 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031720 context.reset(NewContext(NULL));
1721 EXPECT_EQ(base::PLATFORM_FILE_OK,
1722 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091723 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031724 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1725 &file_handle, &created));
1726 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1727 EXPECT_TRUE(created);
1728 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091729 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031730
1731 // open case.
1732 file_handle = base::kInvalidPlatformFileValue;
1733 created = true;
[email protected]949f25a2012-06-27 01:53:091734 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031735 context.reset(NewContext(NULL));
1736 EXPECT_EQ(base::PLATFORM_FILE_OK,
1737 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091738 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031739 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1740 &file_handle, &created));
1741 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1742 EXPECT_FALSE(created);
1743 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091744 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031745
1746 // fail case
1747 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091748 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031749 context.reset(NewContext(NULL));
1750 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1751 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091752 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031753 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1754 &file_handle, &created));
1755 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091756 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031757
1758 // CreateDirectory, create case.
1759 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421760 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1761 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091762 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031763 context.reset(NewContext(NULL));
1764 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091765 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031766 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091767 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031768
1769 // create subdir case.
1770 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421771 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091772 ClearTimestamp(dir_url);
1773 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031774 context.reset(NewContext(NULL));
1775 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091776 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031777 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091778 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1779 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031780
1781 // fail case.
[email protected]04c899f2013-02-08 08:28:421782 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091783 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031784 context.reset(NewContext(NULL));
1785 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091786 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031787 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091788 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031789
1790 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421791 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1792 FileSystemURL src_path = FileSystemURLAppendUTF8(
1793 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031794 context.reset(NewContext(NULL));
1795 EXPECT_EQ(base::PLATFORM_FILE_OK,
1796 ofu()->EnsureFileExists(context.get(), src_path, &created));
1797 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331798 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031799 context.reset(NewContext(NULL));
1800 EXPECT_EQ(base::PLATFORM_FILE_OK,
1801 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1802
[email protected]949f25a2012-06-27 01:53:091803 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031804 context.reset(NewContext(NULL));
1805 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501806 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131807 src_local_path,
[email protected]949f25a2012-06-27 01:53:091808 url));
1809 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031810}
1811
1812TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1813 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091814 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031815
1816 // Create working directory.
1817 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091818 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031819
1820 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421821 FileSystemURL url = FileSystemURLAppendUTF8(
1822 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031823 bool created = false;
1824 context.reset(NewContext(NULL));
1825 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091826 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031827 EXPECT_TRUE(created);
1828
[email protected]949f25a2012-06-27 01:53:091829 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031830 context.reset(NewContext(NULL));
1831 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091832 ofu()->DeleteFile(context.get(), url));
1833 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031834
1835 // fail case.
[email protected]949f25a2012-06-27 01:53:091836 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031837 context.reset(NewContext(NULL));
1838 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091839 ofu()->DeleteFile(context.get(), url));
1840 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031841
[email protected]bab213be2013-01-23 15:13:081842 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421843 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1844 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031845 context.reset(NewContext(NULL));
1846 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091847 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031848 created = false;
1849 context.reset(NewContext(NULL));
1850 EXPECT_EQ(base::PLATFORM_FILE_OK,
1851 ofu()->EnsureFileExists(context.get(), file_path, &created));
1852 EXPECT_TRUE(created);
1853
[email protected]949f25a2012-06-27 01:53:091854 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031855 context.reset(NewContext(NULL));
1856 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081857 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091858 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031859
1860 // delete case.
1861 context.reset(NewContext(NULL));
1862 EXPECT_EQ(base::PLATFORM_FILE_OK,
1863 ofu()->DeleteFile(context.get(), file_path));
1864
[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]bab213be2013-01-23 15:13:081867 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091868 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031869}
1870
1871TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1872 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091873 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031874 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091875 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031876 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091877 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031878 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091879 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031880}
[email protected]a3938912012-03-27 14:00:551881
1882TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091883 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421884 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1885 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551886
1887 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1888 EXPECT_EQ(base::PLATFORM_FILE_OK,
1889 ofu()->CreateDirectory(context.get(), dir, false, false));
1890
1891 bool created = false;
1892 context.reset(NewContext(NULL));
1893 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091894 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551895 EXPECT_TRUE(created);
1896
1897 context.reset(NewContext(NULL));
1898 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091899 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551900
[email protected]a3ef4832013-02-02 05:12:331901 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551902 context.reset(NewContext(NULL));
1903 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091904 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551905 EXPECT_FALSE(file_path.empty());
1906
1907 context.reset(NewContext(NULL));
1908 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091909 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551910 base::Time::Now() + base::TimeDelta::FromHours(1),
1911 base::Time()));
1912
1913 context.reset(NewContext(NULL));
1914 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1915 ofu()->CreateFileEnumerator(context.get(), dir, false));
1916
1917 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331918 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551919 while (!(file_path_each = file_enum->Next()).empty()) {
1920 context.reset(NewContext(NULL));
1921 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331922 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551923 EXPECT_EQ(base::PLATFORM_FILE_OK,
1924 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421925 FileSystemURL::CreateForTest(
1926 dir.origin(),
1927 dir.mount_type(),
1928 file_path_each),
[email protected]a3938912012-03-27 14:00:551929 &file_info, &file_path));
1930 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1931 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1932 EXPECT_EQ(file_info.size, file_enum->Size());
1933 ++count;
1934 }
1935 EXPECT_EQ(2, count);
1936}
[email protected]294dd0312012-05-11 07:35:131937
[email protected]58ac5272013-02-15 10:05:031938// crbug.com/176470
1939#if defined(OS_WIN) || defined(OS_ANDROID)
1940#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1941#else
1942#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1943#endif
1944TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091945 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1946 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1947 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1948 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131949 bool created;
1950
1951 int64 expected_total_file_size = 0;
1952 ASSERT_EQ(base::PLATFORM_FILE_OK,
1953 ofu()->EnsureFileExists(
1954 AllowUsageIncrease(PathCost(from_file))->context(),
1955 from_file, &created));
1956 ASSERT_TRUE(created);
1957 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1958
1959 ASSERT_EQ(base::PLATFORM_FILE_OK,
1960 ofu()->EnsureFileExists(
1961 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1962 obstacle_file, &created));
1963 ASSERT_TRUE(created);
1964 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1965
1966 int64 from_file_size = 1020;
1967 expected_total_file_size += from_file_size;
1968 ASSERT_EQ(base::PLATFORM_FILE_OK,
1969 ofu()->Truncate(
1970 AllowUsageIncrease(from_file_size)->context(),
1971 from_file, from_file_size));
1972 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1973
1974 int64 obstacle_file_size = 1;
1975 expected_total_file_size += obstacle_file_size;
1976 ASSERT_EQ(base::PLATFORM_FILE_OK,
1977 ofu()->Truncate(
1978 AllowUsageIncrease(obstacle_file_size)->context(),
1979 obstacle_file, obstacle_file_size));
1980 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1981
1982 int64 to_file1_size = from_file_size;
1983 expected_total_file_size += to_file1_size;
1984 ASSERT_EQ(base::PLATFORM_FILE_OK,
1985 ofu()->CopyOrMoveFile(
1986 AllowUsageIncrease(
1987 PathCost(to_file1) + to_file1_size)->context(),
1988 from_file, to_file1, true /* copy */));
1989 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1990
1991 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1992 ofu()->CopyOrMoveFile(
1993 DisallowUsageIncrease(
1994 PathCost(to_file2) + from_file_size)->context(),
1995 from_file, to_file2, true /* copy */));
1996 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1997
1998 int64 old_obstacle_file_size = obstacle_file_size;
1999 obstacle_file_size = from_file_size;
2000 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2001 ASSERT_EQ(base::PLATFORM_FILE_OK,
2002 ofu()->CopyOrMoveFile(
2003 AllowUsageIncrease(
2004 obstacle_file_size - old_obstacle_file_size)->context(),
2005 from_file, obstacle_file, true /* copy */));
2006 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2007
2008 int64 old_from_file_size = from_file_size;
2009 from_file_size = old_from_file_size - 1;
2010 expected_total_file_size += from_file_size - old_from_file_size;
2011 ASSERT_EQ(base::PLATFORM_FILE_OK,
2012 ofu()->Truncate(
2013 AllowUsageIncrease(
2014 from_file_size - old_from_file_size)->context(),
2015 from_file, from_file_size));
2016 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2017
2018 // quota exceeded
2019 {
2020 old_obstacle_file_size = obstacle_file_size;
2021 obstacle_file_size = from_file_size;
2022 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2023 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2024 obstacle_file_size - old_obstacle_file_size);
2025 helper->context()->set_allowed_bytes_growth(
2026 helper->context()->allowed_bytes_growth() - 1);
2027 ASSERT_EQ(base::PLATFORM_FILE_OK,
2028 ofu()->CopyOrMoveFile(
2029 helper->context(),
2030 from_file, obstacle_file, true /* copy */));
2031 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2032 }
2033}
2034
2035TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092036 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2037 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2038 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132039 bool created;
2040
2041 int64 expected_total_file_size = 0;
2042 ASSERT_EQ(base::PLATFORM_FILE_OK,
2043 ofu()->EnsureFileExists(
2044 AllowUsageIncrease(PathCost(from_file))->context(),
2045 from_file, &created));
2046 ASSERT_TRUE(created);
2047 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2048
2049 int64 from_file_size = 1020;
2050 expected_total_file_size += from_file_size;
2051 ASSERT_EQ(base::PLATFORM_FILE_OK,
2052 ofu()->Truncate(
2053 AllowUsageIncrease(from_file_size)->context(),
2054 from_file, from_file_size));
2055 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2056
2057 int64 to_file_size ALLOW_UNUSED = from_file_size;
2058 from_file_size = 0;
2059 ASSERT_EQ(base::PLATFORM_FILE_OK,
2060 ofu()->CopyOrMoveFile(
2061 AllowUsageIncrease(-PathCost(from_file) +
2062 PathCost(to_file))->context(),
2063 from_file, to_file, false /* move */));
2064 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2065
2066 ASSERT_EQ(base::PLATFORM_FILE_OK,
2067 ofu()->EnsureFileExists(
2068 AllowUsageIncrease(PathCost(from_file))->context(),
2069 from_file, &created));
2070 ASSERT_TRUE(created);
2071 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2072
2073 ASSERT_EQ(base::PLATFORM_FILE_OK,
2074 ofu()->EnsureFileExists(
2075 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2076 obstacle_file, &created));
2077 ASSERT_TRUE(created);
2078 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2079
2080 from_file_size = 1020;
2081 expected_total_file_size += from_file_size;
2082 ASSERT_EQ(base::PLATFORM_FILE_OK,
2083 ofu()->Truncate(
2084 AllowUsageIncrease(from_file_size)->context(),
2085 from_file, from_file_size));
2086 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2087
2088 int64 obstacle_file_size = 1;
2089 expected_total_file_size += obstacle_file_size;
2090 ASSERT_EQ(base::PLATFORM_FILE_OK,
2091 ofu()->Truncate(
2092 AllowUsageIncrease(1)->context(),
2093 obstacle_file, obstacle_file_size));
2094 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2095
2096 int64 old_obstacle_file_size = obstacle_file_size;
2097 obstacle_file_size = from_file_size;
2098 from_file_size = 0;
2099 expected_total_file_size -= old_obstacle_file_size;
2100 ASSERT_EQ(base::PLATFORM_FILE_OK,
2101 ofu()->CopyOrMoveFile(
2102 AllowUsageIncrease(
2103 -old_obstacle_file_size - PathCost(from_file))->context(),
2104 from_file, obstacle_file,
2105 false /* move */));
2106 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2107
2108 ASSERT_EQ(base::PLATFORM_FILE_OK,
2109 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
2115 from_file_size = 10;
2116 expected_total_file_size += from_file_size;
2117 ASSERT_EQ(base::PLATFORM_FILE_OK,
2118 ofu()->Truncate(
2119 AllowUsageIncrease(from_file_size)->context(),
2120 from_file, from_file_size));
2121 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2122
2123 // quota exceeded even after operation
2124 old_obstacle_file_size = obstacle_file_size;
2125 obstacle_file_size = from_file_size;
2126 from_file_size = 0;
2127 expected_total_file_size -= old_obstacle_file_size;
2128 scoped_ptr<FileSystemOperationContext> context =
2129 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2130 ASSERT_EQ(base::PLATFORM_FILE_OK,
2131 ofu()->CopyOrMoveFile(
2132 context.get(), from_file, obstacle_file, false /* move */));
2133 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2134 context.reset();
2135}
2136
2137TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092138 FileSystemURL dir(CreateURLFromUTF8("dir"));
2139 FileSystemURL file(CreateURLFromUTF8("file"));
2140 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2141 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132142 bool created;
2143
2144 ASSERT_EQ(base::PLATFORM_FILE_OK,
2145 ofu()->EnsureFileExists(
2146 AllowUsageIncrease(PathCost(file))->context(),
2147 file, &created));
2148 ASSERT_TRUE(created);
2149 ASSERT_EQ(0, ComputeTotalFileSize());
2150
2151 ASSERT_EQ(base::PLATFORM_FILE_OK,
2152 ofu()->CreateDirectory(
2153 AllowUsageIncrease(PathCost(dir))->context(),
2154 dir, false, false));
2155 ASSERT_EQ(0, ComputeTotalFileSize());
2156
2157 ASSERT_EQ(base::PLATFORM_FILE_OK,
2158 ofu()->EnsureFileExists(
2159 AllowUsageIncrease(PathCost(dfile1))->context(),
2160 dfile1, &created));
2161 ASSERT_TRUE(created);
2162 ASSERT_EQ(0, ComputeTotalFileSize());
2163
2164 ASSERT_EQ(base::PLATFORM_FILE_OK,
2165 ofu()->EnsureFileExists(
2166 AllowUsageIncrease(PathCost(dfile2))->context(),
2167 dfile2, &created));
2168 ASSERT_TRUE(created);
2169 ASSERT_EQ(0, ComputeTotalFileSize());
2170
2171 ASSERT_EQ(base::PLATFORM_FILE_OK,
2172 ofu()->Truncate(
2173 AllowUsageIncrease(340)->context(),
2174 file, 340));
2175 ASSERT_EQ(340, ComputeTotalFileSize());
2176
2177 ASSERT_EQ(base::PLATFORM_FILE_OK,
2178 ofu()->Truncate(
2179 AllowUsageIncrease(1020)->context(),
2180 dfile1, 1020));
2181 ASSERT_EQ(1360, ComputeTotalFileSize());
2182
2183 ASSERT_EQ(base::PLATFORM_FILE_OK,
2184 ofu()->Truncate(
2185 AllowUsageIncrease(120)->context(),
2186 dfile2, 120));
2187 ASSERT_EQ(1480, ComputeTotalFileSize());
2188
2189 ASSERT_EQ(base::PLATFORM_FILE_OK,
2190 ofu()->DeleteFile(
2191 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2192 file));
2193 ASSERT_EQ(1140, ComputeTotalFileSize());
2194
2195 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302196 AsyncFileTestHelper::Remove(
2197 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132198 ASSERT_EQ(0, ComputeTotalFileSize());
2199}
[email protected]7d78be12012-05-24 07:07:262200
2201TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092202 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262203 base::PlatformFile file_handle;
2204 bool created;
2205
2206 // Creating a file.
2207 ASSERT_EQ(base::PLATFORM_FILE_OK,
2208 ofu()->EnsureFileExists(
2209 AllowUsageIncrease(PathCost(file))->context(),
2210 file, &created));
2211 ASSERT_TRUE(created);
2212 ASSERT_EQ(0, ComputeTotalFileSize());
2213
2214 // Opening it, which shouldn't change the usage.
2215 ASSERT_EQ(base::PLATFORM_FILE_OK,
2216 ofu()->CreateOrOpen(
2217 AllowUsageIncrease(0)->context(), file,
2218 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2219 &file_handle, &created));
2220 ASSERT_EQ(0, ComputeTotalFileSize());
2221 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2222
2223 const int length = 33;
2224 ASSERT_EQ(base::PLATFORM_FILE_OK,
2225 ofu()->Truncate(
2226 AllowUsageIncrease(length)->context(), file, length));
2227 ASSERT_EQ(length, ComputeTotalFileSize());
2228
2229 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2230 ASSERT_EQ(base::PLATFORM_FILE_OK,
2231 ofu()->CreateOrOpen(
2232 AllowUsageIncrease(-length)->context(), file,
2233 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2234 &file_handle, &created));
2235 ASSERT_EQ(0, ComputeTotalFileSize());
2236 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2237
2238 // Extending the file again.
2239 ASSERT_EQ(base::PLATFORM_FILE_OK,
2240 ofu()->Truncate(
2241 AllowUsageIncrease(length)->context(), file, length));
2242 ASSERT_EQ(length, ComputeTotalFileSize());
2243
2244 // Opening it with TRUNCATED flag, which should truncate the file size.
2245 ASSERT_EQ(base::PLATFORM_FILE_OK,
2246 ofu()->CreateOrOpen(
2247 AllowUsageIncrease(-length)->context(), file,
2248 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2249 &file_handle, &created));
2250 ASSERT_EQ(0, ComputeTotalFileSize());
2251 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2252}
[email protected]c4e6f9c2012-09-09 17:42:102253
2254} // namespace fileapi