blob: 98472a5b135c939b4822aded503ec484a430756d [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]bf97e8b2013-04-14 15:00:13249 int64 usage = 0;
250 return usage_cache()->GetUsage(test_helper_.GetUsageCachePath(), &usage) ?
251 usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16252 }
253
[email protected]13f92f6e2012-08-13 07:39:14254 bool PathExists(const FileSystemURL& url) {
255 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21256 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33257 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21258 base::PlatformFileError error = ofu()->GetFileInfo(
259 context.get(), url, &file_info, &platform_path);
260 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14261 }
262
263 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30264 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14265 }
266
[email protected]0c5ebe32011-08-19 22:37:16267 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31268 FileSystemUsageCache* usage_cache() {
269 return test_helper_.usage_cache();
270 }
[email protected]0c5ebe32011-08-19 22:37:16271
[email protected]949f25a2012-06-27 01:53:09272 FileSystemURL CreateURLFromUTF8(const std::string& path) {
273 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50274 }
275
[email protected]949f25a2012-06-27 01:53:09276 int64 PathCost(const FileSystemURL& url) {
277 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13278 }
279
[email protected]a3ef4832013-02-02 05:12:33280 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]949f25a2012-06-27 01:53:09281 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50282 }
283
[email protected]d4905e2e2011-05-13 21:56:32284 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39285 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16286 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33287 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49288 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09289 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32290
291 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33292 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49293 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09294 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32295 EXPECT_EQ(data_path, local_path);
296 EXPECT_TRUE(FileExists(data_path));
297 EXPECT_EQ(0, GetSize(data_path));
298
299 const char data[] = "test data";
300 const int length = arraysize(data) - 1;
301
302 if (base::kInvalidPlatformFileValue == file_handle) {
303 bool created = true;
[email protected]403ada82013-01-08 07:51:39304 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32305 file_handle = base::CreatePlatformFile(
306 data_path,
307 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
308 &created,
309 &error);
[email protected]81b7f662011-05-26 00:54:46310 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32311 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
312 EXPECT_FALSE(created);
313 }
314 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
315 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
316
317 base::PlatformFileInfo file_info1;
318 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16319 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49320 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09321 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32322 EXPECT_EQ(data_path, local_path);
323
324 EXPECT_FALSE(file_info0.is_directory);
325 EXPECT_FALSE(file_info1.is_directory);
326 EXPECT_FALSE(file_info0.is_symbolic_link);
327 EXPECT_FALSE(file_info1.is_symbolic_link);
328 EXPECT_EQ(0, file_info0.size);
329 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32330 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32331
[email protected]0c5ebe32011-08-19 22:37:16332 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49333 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09334 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32335 EXPECT_EQ(length * 2, GetSize(data_path));
336
[email protected]0c5ebe32011-08-19 22:37:16337 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49338 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09339 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16340 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32341 }
342
343 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09344 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33345 const std::set<base::FilePath::StringType>& files,
346 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32347 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33348 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32349 for (iter = files.begin(); iter != files.end(); ++iter) {
350 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16351 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32352 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49353 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42354 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32355 &created));
356 ASSERT_FALSE(created);
357 }
358 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16359 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14360 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42361 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32362 }
363 }
364
[email protected]294dd0312012-05-11 07:35:13365 class UsageVerifyHelper {
366 public:
367 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33368 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13369 int64 expected_usage)
370 : context_(context.Pass()),
371 test_helper_(test_helper),
372 expected_usage_(expected_usage) {}
373
374 ~UsageVerifyHelper() {
[email protected]8eee1b62013-01-08 02:26:44375 MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13376 Check();
377 }
378
379 FileSystemOperationContext* context() {
380 return context_.get();
381 }
382
383 private:
384 void Check() {
385 ASSERT_EQ(expected_usage_,
386 test_helper_->GetCachedOriginUsage());
387 }
388
389 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33390 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13391 int64 expected_usage_;
392 };
393
394 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
395 int64 usage = test_helper_.GetCachedOriginUsage();
396 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
397 LimitedContext(requested_growth),
398 &test_helper_, usage + requested_growth));
399 }
400
401 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
402 int64 usage = test_helper_.GetCachedOriginUsage();
403 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
404 LimitedContext(requested_growth - 1), &test_helper_, usage));
405 }
406
[email protected]d4905e2e2011-05-13 21:56:32407 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09408 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33409 std::set<base::FilePath::StringType>* files,
410 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32411 scoped_ptr<FileSystemOperationContext> context;
[email protected]d4905e2e2011-05-13 21:56:32412 std::vector<base::FileUtilProxy::Entry> entries;
413 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30414 AsyncFileTestHelper::ReadDirectory(
415 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32416 EXPECT_EQ(0UL, entries.size());
417
418 files->clear();
[email protected]89ee4272011-05-16 18:45:17419 files->insert(FILE_PATH_LITERAL("first"));
420 files->insert(FILE_PATH_LITERAL("second"));
421 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32422 directories->clear();
[email protected]89ee4272011-05-16 18:45:17423 directories->insert(FILE_PATH_LITERAL("fourth"));
424 directories->insert(FILE_PATH_LITERAL("fifth"));
425 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33426 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32427 for (iter = files->begin(); iter != files->end(); ++iter) {
428 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16429 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32430 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49431 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50432 context.get(),
[email protected]04c899f2013-02-08 08:28:42433 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50434 &created));
[email protected]d4905e2e2011-05-13 21:56:32435 ASSERT_TRUE(created);
436 }
437 for (iter = directories->begin(); iter != directories->end(); ++iter) {
438 bool exclusive = true;
439 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16440 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32441 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49442 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42443 context.get(),
444 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09445 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32446 }
[email protected]949f25a2012-06-27 01:53:09447 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32448 }
449
[email protected]949f25a2012-06-27 01:53:09450 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33451 std::set<base::FilePath::StringType> files;
452 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09453 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32454
455 scoped_ptr<FileSystemOperationContext> context;
456 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16457 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32458 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30459 AsyncFileTestHelper::ReadDirectory(
460 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32461 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
462 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10463 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32464 for (entry_iter = entries.begin(); entry_iter != entries.end();
465 ++entry_iter) {
466 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31467 std::set<base::FilePath::StringType>::iterator iter =
468 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32469 if (iter != files.end()) {
470 EXPECT_FALSE(entry.is_directory);
471 files.erase(iter);
472 continue;
473 }
[email protected]89ee4272011-05-16 18:45:17474 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32475 EXPECT_FALSE(directories.end() == iter);
476 EXPECT_TRUE(entry.is_directory);
477 directories.erase(iter);
478 }
479 }
480
[email protected]949f25a2012-06-27 01:53:09481 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41482 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32483 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16484
[email protected]2517cfa2011-08-25 05:12:41485 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32486 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49487 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09488 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10489 // Currently we fire no change notifications for Touch.
490 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33491 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32492 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16493 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49494 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09495 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32496 // We compare as time_t here to lower our resolution, to avoid false
497 // negatives caused by conversion to the local filesystem's native
498 // representation and back.
499 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
500
[email protected]0c5ebe32011-08-19 22:37:16501 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32502 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41503 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32504 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49505 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09506 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10507 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16508 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49509 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09510 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32511 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49512 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41513 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32514 }
515
[email protected]81b7f662011-05-26 00:54:46516 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23517 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46518 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33519 base::FilePath root_file_path = source_dir.path();
520 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09521 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46522 int64 src_file_length = 87;
523
524 base::PlatformFileError error_code;
525 bool created = false;
526 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
527 base::PlatformFile file_handle =
528 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50529 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46530 EXPECT_TRUE(created);
531 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
532 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
533 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
534 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
535
536 scoped_ptr<FileSystemOperationContext> context;
537
538 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16539 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46540 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09541 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46542 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10543
544 // We must have observed one (and only one) create_file_count.
545 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
546 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46547 }
548
[email protected]0c5ebe32011-08-19 22:37:16549 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09550 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16551 if (!overwrite) {
552 // Verify that file creation requires sufficient quota for the path.
553 context.reset(NewContext(NULL));
554 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
555 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13556 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09557 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16558 }
559
560 context.reset(NewContext(NULL));
561 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46562 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13563 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09564 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16565
[email protected]13f92f6e2012-08-13 07:39:14566 EXPECT_TRUE(PathExists(dest_url));
567 EXPECT_FALSE(DirectoryExists(dest_url));
568
[email protected]0c5ebe32011-08-19 22:37:16569 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46570 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33571 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49572 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09573 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50574 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46575 EXPECT_TRUE(FileExists(data_path));
576 EXPECT_EQ(src_file_length, GetSize(data_path));
577
578 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09579 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46580 }
581
[email protected]949f25a2012-06-27 01:53:09582 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03583 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
584 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09585 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
586 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03587 }
588
[email protected]949f25a2012-06-27 01:53:09589 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03590 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33591 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03592 base::PlatformFileInfo file_info;
593 context.reset(NewContext(NULL));
594 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09595 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10596 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03597 return file_info.last_modified;
598 }
599
[email protected]949f25a2012-06-27 01:53:09600 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03601 bool copy,
602 bool overwrite) {
603 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42604 const FileSystemURL src_dir_url(
605 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
606 const FileSystemURL dest_dir_url(
607 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03608
[email protected]04c899f2013-02-08 08:28:42609 const FileSystemURL src_file_url(
610 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
611 const FileSystemURL dest_file_url(
612 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03613
614 context.reset(NewContext(NULL));
615 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09616 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03617 context.reset(NewContext(NULL));
618 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09619 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03620
621 bool created = false;
622 context.reset(NewContext(NULL));
623 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09624 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03625 if (overwrite) {
626 context.reset(NewContext(NULL));
627 EXPECT_EQ(base::PLATFORM_FILE_OK,
628 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09629 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03630 }
631
[email protected]949f25a2012-06-27 01:53:09632 ClearTimestamp(src_dir_url);
633 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03634 context.reset(NewContext(NULL));
635 EXPECT_EQ(base::PLATFORM_FILE_OK,
636 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09637 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03638 copy));
[email protected]fad625e2f2011-12-08 05:38:03639 if (copy)
[email protected]949f25a2012-06-27 01:53:09640 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03641 else
[email protected]949f25a2012-06-27 01:53:09642 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
643 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03644 }
645
[email protected]ecdfd6c52012-04-11 13:35:44646 int64 ComputeCurrentUsage() {
[email protected]06226172013-03-14 15:39:31647 return test_helper_.ComputeCurrentOriginUsage() -
648 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]ecdfd6c52012-04-11 13:35:44649 }
650
[email protected]02a60542012-07-24 20:05:33651 const LocalFileSystemTestOriginHelper& test_helper() const {
652 return test_helper_;
653 }
[email protected]45ea0fbbb2012-02-27 22:28:49654
[email protected]07b64872013-02-13 11:46:30655 FileSystemContext* file_system_context() {
656 return test_helper_.file_system_context();
657 }
658
[email protected]d4905e2e2011-05-13 21:56:32659 private:
[email protected]ea1a3f62012-11-16 20:34:23660 base::ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01661 MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16662 scoped_refptr<quota::QuotaManager> quota_manager_;
663 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26664 GURL origin_;
665 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03666 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]02a60542012-07-24 20:05:33667 LocalFileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16668 quota::QuotaStatusCode quota_status_;
669 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10670 MockFileChangeObserver change_observer_;
671 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32672
[email protected]7878ece2011-09-05 11:41:49673 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32674};
675
[email protected]7878ece2011-09-05 11:41:49676TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32677 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
678 bool created;
[email protected]949f25a2012-06-27 01:53:09679 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16680 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32681 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
682
683 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49684 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09685 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32686 &created));
687
[email protected]0c5ebe32011-08-19 22:37:16688 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32689 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09690 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32691
[email protected]949f25a2012-06-27 01:53:09692 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32693
[email protected]c4e6f9c2012-09-09 17:42:10694 EXPECT_TRUE(change_observer()->HasNoChange());
695
[email protected]0c5ebe32011-08-19 22:37:16696 // Verify that file creation requires sufficient quota for the path.
697 context.reset(NewContext(NULL));
698 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09699 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16700 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49701 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09702 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16703
704 context.reset(NewContext(NULL));
705 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09706 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32707 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49708 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09709 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32710 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10711 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32712 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
713
[email protected]949f25a2012-06-27 01:53:09714 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32715
[email protected]0c5ebe32011-08-19 22:37:16716 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33717 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49718 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09719 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32720 EXPECT_TRUE(file_util::PathExists(local_path));
721
[email protected]0c5ebe32011-08-19 22:37:16722 // Verify that deleting a file isn't stopped by zero quota, and that it frees
723 // up quote from its path.
724 context.reset(NewContext(NULL));
725 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32726 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09727 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10728 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32729 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09730 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16731 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32732
[email protected]0c5ebe32011-08-19 22:37:16733 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32734 bool exclusive = true;
735 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09736 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50737 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42738 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49739 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09740 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10741 // The oepration created 3 directories recursively.
742 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32743
[email protected]0c5ebe32011-08-19 22:37:16744 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32745 file_handle = base::kInvalidPlatformFileValue;
746 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49747 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09748 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32749 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10750 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32751 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
752
[email protected]949f25a2012-06-27 01:53:09753 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32754
[email protected]0c5ebe32011-08-19 22:37:16755 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49756 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09757 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32758 EXPECT_TRUE(file_util::PathExists(local_path));
759
[email protected]0c5ebe32011-08-19 22:37:16760 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32761 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09762 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10763 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32764 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10765
766 // Make sure we have no unexpected changes.
767 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32768}
769
[email protected]7878ece2011-09-05 11:41:49770TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32771 bool created = false;
[email protected]949f25a2012-06-27 01:53:09772 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16773 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32774
775 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09776 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32777
[email protected]0c5ebe32011-08-19 22:37:16778 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32779 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09780 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32781 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10782 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32783
[email protected]0c5ebe32011-08-19 22:37:16784 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33785 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49786 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09787 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32788 EXPECT_EQ(0, GetSize(local_path));
789
[email protected]0c5ebe32011-08-19 22:37:16790 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49791 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09792 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10793 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32794 EXPECT_EQ(10, GetSize(local_path));
795
[email protected]0c5ebe32011-08-19 22:37:16796 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49797 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09798 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32799 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10800 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32801
[email protected]13f92f6e2012-08-13 07:39:14802 EXPECT_FALSE(DirectoryExists(url));
803 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10804
805 // Make sure we have no unexpected changes.
806 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32807}
808
[email protected]ecdfd6c52012-04-11 13:35:44809TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
810 bool created = false;
[email protected]949f25a2012-06-27 01:53:09811 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44812
813 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13814 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09815 AllowUsageIncrease(PathCost(url))->context(),
816 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44817 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13818 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44819
[email protected]ecdfd6c52012-04-11 13:35:44820 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13821 ofu()->Truncate(
822 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09823 url, 1020));
[email protected]294dd0312012-05-11 07:35:13824 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44825
[email protected]ecdfd6c52012-04-11 13:35:44826 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13827 ofu()->Truncate(
828 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09829 url, 0));
[email protected]294dd0312012-05-11 07:35:13830 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44831
[email protected]ecdfd6c52012-04-11 13:35:44832 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13833 ofu()->Truncate(
834 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09835 url, 1021));
[email protected]294dd0312012-05-11 07:35:13836 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44837
[email protected]ecdfd6c52012-04-11 13:35:44838 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13839 ofu()->Truncate(
840 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09841 url, 1020));
[email protected]294dd0312012-05-11 07:35:13842 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44843
[email protected]ecdfd6c52012-04-11 13:35:44844 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13845 ofu()->Truncate(
846 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09847 url, 1020));
[email protected]294dd0312012-05-11 07:35:13848 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44849
[email protected]294dd0312012-05-11 07:35:13850 // quota exceeded
851 {
852 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
853 helper->context()->set_allowed_bytes_growth(
854 helper->context()->allowed_bytes_growth() - 1);
855 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09856 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13857 ASSERT_EQ(1019, ComputeTotalFileSize());
858 }
[email protected]ecdfd6c52012-04-11 13:35:44859
860 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33861 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44862 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13863 ofu()->GetLocalFilePath(
864 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09865 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44866 ASSERT_FALSE(local_path.empty());
867 ASSERT_TRUE(file_util::Delete(local_path, false));
868
[email protected]ecdfd6c52012-04-11 13:35:44869 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13870 ofu()->Truncate(
871 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09872 url, 1234));
[email protected]294dd0312012-05-11 07:35:13873 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44874}
875
[email protected]7878ece2011-09-05 11:41:49876TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09877 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32878 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16879 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32880 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49881 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09882 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10883 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32884
[email protected]0c5ebe32011-08-19 22:37:16885 // Verify that file creation requires sufficient quota for the path.
886 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09887 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32888 created = false;
[email protected]0c5ebe32011-08-19 22:37:16889 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09890 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16891 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09892 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16893 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10894 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16895
896 context.reset(NewContext(NULL));
897 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09898 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32899 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09900 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32901 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10902 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32903
[email protected]949f25a2012-06-27 01:53:09904 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32905
[email protected]0c5ebe32011-08-19 22:37:16906 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32907 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09908 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32909 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10910 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32911
912 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09913 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16914 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32915 bool exclusive = true;
916 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49917 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09918 context.get(),
[email protected]04c899f2013-02-08 08:28:42919 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09920 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10921 // 2 directories: path/ and path/to.
922 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32923
[email protected]0c5ebe32011-08-19 22:37:16924 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32925 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09926 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32927 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14928 EXPECT_FALSE(DirectoryExists(url));
929 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10930 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32931}
932
[email protected]7878ece2011-09-05 11:41:49933TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16934 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32935
936 bool exclusive = false;
937 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09938 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49939 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09940 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32941
[email protected]0c5ebe32011-08-19 22:37:16942 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32943 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08944 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32945
[email protected]007b3f82013-04-09 08:46:45946 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:14947 EXPECT_FALSE(DirectoryExists(url));
948 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16949 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49950 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32951
[email protected]0c5ebe32011-08-19 22:37:16952 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32953 exclusive = false;
954 recursive = true;
[email protected]7878ece2011-09-05 11:41:49955 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09956 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10957 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32958
[email protected]13f92f6e2012-08-13 07:39:14959 EXPECT_TRUE(DirectoryExists(url));
960 EXPECT_TRUE(PathExists(url));
961
[email protected]0c5ebe32011-08-19 22:37:16962 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49963 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42964 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14965
[email protected]0c5ebe32011-08-19 22:37:16966 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09967 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42968 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32969
970 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16971 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06972 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08973 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42974 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10975 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32976
977 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33978 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49979 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09980 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32981 EXPECT_TRUE(local_path.empty());
982 EXPECT_TRUE(file_info.is_directory);
983 EXPECT_FALSE(file_info.is_symbolic_link);
984
985 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16986 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49987 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09988 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10989 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32990
991 exclusive = true;
992 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16993 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49994 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09995 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10996 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32997
[email protected]0c5ebe32011-08-19 22:37:16998 // Verify that deleting a directory isn't stopped by zero quota, and that it
999 // frees up quota from its path.
1000 context.reset(NewContext(NULL));
1001 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:081002 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101003 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091004 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161005 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321006
[email protected]949f25a2012-06-27 01:53:091007 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321008
[email protected]13f92f6e2012-08-13 07:39:141009 EXPECT_FALSE(DirectoryExists(url));
1010 EXPECT_FALSE(PathExists(url));
1011
[email protected]0c5ebe32011-08-19 22:37:161012 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091013 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491014 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091015 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321016
[email protected]0c5ebe32011-08-19 22:37:161017 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321018 exclusive = true;
1019 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161020 context.reset(NewContext(NULL));
1021 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091022 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491023 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091024 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101025 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161026
1027 context.reset(NewContext(NULL));
1028 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091029 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491030 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091031 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101032 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321033
[email protected]13f92f6e2012-08-13 07:39:141034 EXPECT_TRUE(DirectoryExists(url));
1035 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321036
1037 exclusive = true;
1038 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141039 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491040 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091041 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101042 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321043
1044 exclusive = true;
1045 recursive = false;
[email protected]949f25a2012-06-27 01:53:091046 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141047 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491048 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091049 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101050 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321051
[email protected]949f25a2012-06-27 01:53:091052 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321053
[email protected]13f92f6e2012-08-13 07:39:141054 EXPECT_FALSE(DirectoryExists(url));
1055 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321056
1057 exclusive = true;
1058 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141059 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491060 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091061 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101062 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321063
[email protected]13f92f6e2012-08-13 07:39:141064 EXPECT_TRUE(DirectoryExists(url));
1065 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321066
1067 exclusive = true;
1068 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141069 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491070 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091071 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101072 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321073}
1074
[email protected]7878ece2011-09-05 11:41:491075TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161076 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321077 bool exclusive = true;
1078 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091079 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491080 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091081 context.get(), url, exclusive, recursive));
1082 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321083}
1084
[email protected]7878ece2011-09-05 11:41:491085TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451086 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321087}
1088
[email protected]7878ece2011-09-05 11:41:491089TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091090 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321091}
1092
[email protected]7878ece2011-09-05 11:41:491093TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091094 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161095 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321096
1097 bool created = false;
1098 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091099 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321100 ASSERT_TRUE(created);
1101
[email protected]d4905e2e2011-05-13 21:56:321102 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]1a647202013-01-21 15:32:251103 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301104 AsyncFileTestHelper::ReadDirectory(
1105 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321106
[email protected]949f25a2012-06-27 01:53:091107 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321108}
1109
[email protected]7878ece2011-09-05 11:41:491110TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091111 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161112 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411113
1114 base::Time last_access_time = base::Time::Now();
1115 base::Time last_modified_time = base::Time::Now();
1116
1117 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321118 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491119 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091120 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321121
[email protected]2517cfa2011-08-25 05:12:411122 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161123 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411124 bool created = false;
1125 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091126 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411127 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091128 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411129
1130 // Now test a directory:
1131 context.reset(NewContext(NULL));
1132 bool exclusive = true;
1133 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091134 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491135 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091136 url, exclusive, recursive));
1137 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161138}
1139
[email protected]7878ece2011-09-05 11:41:491140TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091141 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161142 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1143
[email protected]949f25a2012-06-27 01:53:091144 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161145 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411146 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161147 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091148 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411149 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161150 context->set_allowed_bytes_growth(1024);
1151 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091152 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411153 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091154 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161155 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1156
1157 context->set_allowed_bytes_growth(1024);
1158 bool exclusive = true;
1159 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091160 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331161 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091162 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161163 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421164 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1165 for (iterator iter = components.begin();
1166 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491167 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331168 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161169 }
1170 context.reset(NewContext(NULL));
1171 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491172 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091173 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161174 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321175}
1176
[email protected]7878ece2011-09-05 11:41:491177TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091178 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1179 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161180 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321181
1182 bool is_copy_not_move = false;
1183 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091184 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321185 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101186 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161187 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321188 is_copy_not_move = true;
1189 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091190 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321191 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101192 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091193 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321194 bool exclusive = true;
1195 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161196 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491197 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091198 context.get(),
[email protected]04c899f2013-02-08 08:28:421199 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091200 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101201 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321202 is_copy_not_move = false;
1203 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091204 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321205 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101206 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161207 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321208 is_copy_not_move = true;
1209 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091210 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321211 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101212 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321213}
1214
[email protected]7878ece2011-09-05 11:41:491215TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321216 const int64 kSourceLength = 5;
1217 const int64 kDestLength = 50;
1218
1219 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1220 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1221 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1222 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1223 test_case.is_copy_not_move);
1224 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1225 test_case.source_path);
1226 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1227 test_case.dest_path);
1228 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1229 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161230 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321231
1232 bool exclusive = false;
1233 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091234 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1235 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321236
[email protected]0c5ebe32011-08-19 22:37:161237 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491238 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091239 context.get(),
[email protected]04c899f2013-02-08 08:28:421240 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091241 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161242 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491243 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091244 context.get(),
[email protected]04c899f2013-02-08 08:28:421245 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091246 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321247
[email protected]d4905e2e2011-05-13 21:56:321248 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161249 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321250 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091251 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321252 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161253 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321254 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091255 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321256
1257 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161258 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321259 created = false;
1260 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091261 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321262 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161263 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321264 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091265 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321266 }
1267
[email protected]0c5ebe32011-08-19 22:37:161268 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491269 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091270 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101271
[email protected]d4905e2e2011-05-13 21:56:321272 if (test_case.is_copy_not_move) {
1273 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331274 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161275 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491276 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091277 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321278 EXPECT_EQ(kSourceLength, file_info.size);
1279 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091280 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321281 } else {
1282 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331283 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161284 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491285 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091286 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321287 }
1288 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331289 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491290 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091291 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321292 EXPECT_EQ(kSourceLength, file_info.size);
1293
1294 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091295 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321296 }
1297}
1298
[email protected]7878ece2011-09-05 11:41:491299TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091300 FileSystemURL src_url = CreateURLFromUTF8("src path");
1301 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161302 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1303 bool created = false;
[email protected]7878ece2011-09-05 11:41:491304 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091305 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161306
1307 bool is_copy = true;
1308 // Copy, no overwrite.
1309 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091310 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161311 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091312 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161313 context.reset(NewContext(NULL));
1314 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091315 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161316 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091317 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161318
1319 // Copy, with overwrite.
1320 context.reset(NewContext(NULL));
1321 context->set_allowed_bytes_growth(0);
1322 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091323 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161324}
1325
[email protected]7878ece2011-09-05 11:41:491326TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091327 FileSystemURL src_url = CreateURLFromUTF8("src path");
1328 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161329 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1330 bool created = false;
[email protected]7878ece2011-09-05 11:41:491331 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091332 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161333
1334 bool is_copy = false;
1335 // Move, rename, no overwrite.
1336 context.reset(NewContext(NULL));
1337 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091338 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1339 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161340 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091341 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161342 context.reset(NewContext(NULL));
1343 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091344 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1345 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161346 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091347 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161348
1349 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491350 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091351 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161352
1353 // Move, rename, with overwrite.
1354 context.reset(NewContext(NULL));
1355 context->set_allowed_bytes_growth(0);
1356 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091357 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161358}
1359
[email protected]7878ece2011-09-05 11:41:491360TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091361 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161362 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1363 bool created = false;
[email protected]7878ece2011-09-05 11:41:491364 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091365 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161366
1367 bool exclusive = true;
1368 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091369 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161370 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491371 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091372 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161373
[email protected]04c899f2013-02-08 08:28:421374 FileSystemURL dest_url = FileSystemURLAppend(
1375 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161376
1377 bool is_copy = false;
1378 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1379 // Move, no rename, no overwrite.
1380 context.reset(NewContext(NULL));
1381 context->set_allowed_bytes_growth(allowed_bytes_growth);
1382 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091383 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161384 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1385
1386 // Move, no rename, with overwrite.
1387 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491388 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091389 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161390 context.reset(NewContext(NULL));
1391 context->set_allowed_bytes_growth(allowed_bytes_growth);
1392 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091393 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161394 EXPECT_EQ(
1395 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091396 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161397 context->allowed_bytes_growth());
1398}
1399
[email protected]7878ece2011-09-05 11:41:491400TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461401 TestCopyInForeignFileHelper(false /* overwrite */);
1402 TestCopyInForeignFileHelper(true /* overwrite */);
1403}
1404
[email protected]7878ece2011-09-05 11:41:491405TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161406 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091407 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321408 bool exclusive = true;
1409 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491410 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091411 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321412
[email protected]a3ef4832013-02-02 05:12:331413 std::set<base::FilePath::StringType> files;
1414 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091415 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321416
[email protected]949f25a2012-06-27 01:53:091417 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321418
[email protected]13f92f6e2012-08-13 07:39:141419 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321420 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301421 AsyncFileTestHelper::Copy(
1422 test_helper().file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321423
[email protected]949f25a2012-06-27 01:53:091424 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141425 EXPECT_TRUE(DirectoryExists(src_url));
1426 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321427 recursive = true;
1428 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301429 AsyncFileTestHelper::Remove(
1430 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141431 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321432}
[email protected]6b931152011-05-20 21:02:351433
[email protected]7878ece2011-09-05 11:41:491434TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1435 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1436 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161437 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261438 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161439 EXPECT_EQ(origin(), enumerator->Next());
1440 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1441 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1442 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261443 EXPECT_EQ(GURL(), enumerator->Next());
1444 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1445 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1446
1447 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161448 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261449
1450 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1451 SCOPED_TRACE(testing::Message() <<
1452 "Validating kOriginEnumerationTestRecords " << i);
1453 const OriginEnumerationTestRecord& record =
1454 kOriginEnumerationTestRecords[i];
1455 GURL origin_url(record.origin_url);
1456 origins_expected.insert(origin_url);
1457 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331458 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161459 NewHelper(origin_url, kFileSystemTypeTemporary));
1460 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261461 bool created = false;
1462 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501463 ofu()->EnsureFileExists(
1464 context.get(),
[email protected]949f25a2012-06-27 01:53:091465 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501466 &created));
[email protected]fcc2d5f2011-05-23 22:06:261467 EXPECT_TRUE(created);
1468 }
1469 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331470 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161471 NewHelper(origin_url, kFileSystemTypePersistent));
1472 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261473 bool created = false;
1474 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501475 ofu()->EnsureFileExists(
1476 context.get(),
[email protected]949f25a2012-06-27 01:53:091477 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501478 &created));
[email protected]fcc2d5f2011-05-23 22:06:261479 EXPECT_TRUE(created);
1480 }
1481 }
[email protected]7878ece2011-09-05 11:41:491482 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261483 EXPECT_TRUE(enumerator.get());
1484 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161485 GURL origin_url;
1486 while (!(origin_url = enumerator->Next()).is_empty()) {
1487 origins_found.insert(origin_url);
1488 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261489 bool found = false;
1490 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1491 ++i) {
1492 const OriginEnumerationTestRecord& record =
1493 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161494 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261495 continue;
1496 found = true;
1497 EXPECT_EQ(record.has_temporary,
1498 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1499 EXPECT_EQ(record.has_persistent,
1500 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1501 }
[email protected]0c5ebe32011-08-19 22:37:161502 // Deal with the default filesystem created by the test helper.
1503 if (!found && origin_url == origin()) {
1504 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1505 EXPECT_EQ(true,
1506 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181507 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161508 found = true;
1509 }
[email protected]fcc2d5f2011-05-23 22:06:261510 EXPECT_TRUE(found);
1511 }
1512
1513 std::set<GURL> diff;
1514 std::set_symmetric_difference(origins_expected.begin(),
1515 origins_expected.end(), origins_found.begin(), origins_found.end(),
1516 inserter(diff, diff.begin()));
1517 EXPECT_TRUE(diff.empty());
1518}
[email protected]0c5ebe32011-08-19 22:37:161519
[email protected]7878ece2011-09-05 11:41:491520TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161521 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1522
1523 int64 expected_quota = 0;
1524
[email protected]f83b5b72012-01-27 10:26:561525 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011526 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561527 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331528 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501529 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161530 if (test_case.is_directory) {
1531 bool exclusive = true;
1532 bool recursive = false;
1533 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091534 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501535 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161536 } else {
1537 bool created = false;
1538 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091539 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501540 &created));
[email protected]0c5ebe32011-08-19 22:37:161541 ASSERT_TRUE(created);
1542 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501543 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091544 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501545 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161546 expected_quota += test_case.data_file_size;
1547 }
1548 }
[email protected]022d2702012-05-14 16:04:261549
1550 // Usually raw size in usage cache and the usage returned by QuotaUtil
1551 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161552 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261553 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1554
[email protected]0c5ebe32011-08-19 22:37:161555 RevokeUsageCache();
1556 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261557 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1558
1559 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161560 GetUsageFromQuotaManager();
1561 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261562 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161563 EXPECT_EQ(expected_quota, usage());
1564}
[email protected]34583332011-08-31 08:59:471565
[email protected]7878ece2011-09-05 11:41:491566TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091567 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1568 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471569
1570 scoped_ptr<FileSystemOperationContext> context;
1571 base::PlatformFile file;
1572 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331573 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471574 bool created = false;
1575
1576 // Create a non-empty file.
1577 context.reset(NewContext(NULL));
1578 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491579 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471580 EXPECT_TRUE(created);
1581 context.reset(NewContext(NULL));
1582 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491583 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471584 context.reset(NewContext(NULL));
1585 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491586 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471587 context.get(), kPath1, &file_info, &data_path));
1588 EXPECT_EQ(10, file_info.size);
1589
1590 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491591 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471592
1593 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141594 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471595 context.reset(NewContext(NULL));
1596 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491597 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471598 EXPECT_TRUE(created);
1599 context.reset(NewContext(NULL));
1600 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491601 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471602 context.get(), kPath1, &file_info, &data_path));
1603 EXPECT_EQ(0, file_info.size);
1604
1605 // Make another broken file to |kPath2|.
1606 context.reset(NewContext(NULL));
1607 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491608 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471609 EXPECT_TRUE(created);
1610
1611 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491612 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471613
1614 // Repair broken |kPath1|.
1615 context.reset(NewContext(NULL));
1616 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491617 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471618 base::Time::Now()));
1619 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491620 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471621 EXPECT_TRUE(created);
1622
1623 // Copy from sound |kPath1| to broken |kPath2|.
1624 context.reset(NewContext(NULL));
1625 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491626 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501627 true /* copy */));
[email protected]34583332011-08-31 08:59:471628
[email protected]7878ece2011-09-05 11:41:491629 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471630 context.reset(NewContext(NULL));
1631 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491632 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471633 context.get(), kPath1,
1634 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1635 &file, &created));
1636 EXPECT_TRUE(created);
1637 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1638 EXPECT_EQ(0, file_info.size);
1639 EXPECT_TRUE(base::ClosePlatformFile(file));
1640}
[email protected]9dfdc0e32011-09-02 06:07:441641
[email protected]7878ece2011-09-05 11:41:491642TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091643 const FileSystemURL kPath[] = {
1644 CreateURLFromUTF8("foo"),
1645 CreateURLFromUTF8("bar"),
1646 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441647 };
[email protected]a3ef4832013-02-02 05:12:331648 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441649 scoped_ptr<FileSystemOperationContext> context;
1650
1651 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1652 bool created = false;
1653 context.reset(NewContext(NULL));
1654 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491655 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441656 EXPECT_TRUE(created);
1657 }
1658
[email protected]9dfdc0e32011-09-02 06:07:441659 std::vector<base::FileUtilProxy::Entry> entries;
1660 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301661 AsyncFileTestHelper::ReadDirectory(
1662 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441663 EXPECT_EQ(3u, entries.size());
1664
[email protected]a3ef4832013-02-02 05:12:331665 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441666 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491667 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441668 EXPECT_TRUE(file_util::Delete(local_path, false));
1669
[email protected]9dfdc0e32011-09-02 06:07:441670 entries.clear();
1671 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301672 AsyncFileTestHelper::ReadDirectory(
1673 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441674 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1675}
[email protected]fad625e2f2011-12-08 05:38:031676
1677TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1678 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091679 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031680
1681 // Create working directory.
1682 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091683 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031684
1685 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421686 FileSystemURL url(FileSystemURLAppendUTF8(
1687 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031688 bool created = false;
[email protected]949f25a2012-06-27 01:53:091689 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031690 context.reset(NewContext(NULL));
1691 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091692 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031693 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091694 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031695
1696 // non create case.
1697 created = true;
[email protected]949f25a2012-06-27 01:53:091698 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031699 context.reset(NewContext(NULL));
1700 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091701 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031702 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091703 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031704
1705 // fail case.
[email protected]04c899f2013-02-08 08:28:421706 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031707 context.reset(NewContext(NULL));
1708 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091709 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031710
[email protected]949f25a2012-06-27 01:53:091711 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031712 context.reset(NewContext(NULL));
1713 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091714 ofu()->EnsureFileExists(context.get(), url, &created));
1715 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031716
1717 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421718 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391719 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031720 created = false;
[email protected]949f25a2012-06-27 01:53:091721 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031722 context.reset(NewContext(NULL));
1723 EXPECT_EQ(base::PLATFORM_FILE_OK,
1724 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091725 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031726 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1727 &file_handle, &created));
1728 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1729 EXPECT_TRUE(created);
1730 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091731 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031732
1733 // open case.
1734 file_handle = base::kInvalidPlatformFileValue;
1735 created = true;
[email protected]949f25a2012-06-27 01:53:091736 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031737 context.reset(NewContext(NULL));
1738 EXPECT_EQ(base::PLATFORM_FILE_OK,
1739 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091740 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031741 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1742 &file_handle, &created));
1743 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1744 EXPECT_FALSE(created);
1745 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091746 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031747
1748 // fail case
1749 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091750 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031751 context.reset(NewContext(NULL));
1752 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1753 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091754 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031755 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1756 &file_handle, &created));
1757 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091758 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031759
1760 // CreateDirectory, create case.
1761 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421762 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1763 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091764 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031765 context.reset(NewContext(NULL));
1766 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091767 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031768 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091769 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031770
1771 // create subdir case.
1772 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421773 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091774 ClearTimestamp(dir_url);
1775 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031776 context.reset(NewContext(NULL));
1777 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091778 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031779 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091780 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1781 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031782
1783 // fail case.
[email protected]04c899f2013-02-08 08:28:421784 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091785 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031786 context.reset(NewContext(NULL));
1787 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091788 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031789 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091790 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031791
1792 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421793 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1794 FileSystemURL src_path = FileSystemURLAppendUTF8(
1795 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031796 context.reset(NewContext(NULL));
1797 EXPECT_EQ(base::PLATFORM_FILE_OK,
1798 ofu()->EnsureFileExists(context.get(), src_path, &created));
1799 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331800 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031801 context.reset(NewContext(NULL));
1802 EXPECT_EQ(base::PLATFORM_FILE_OK,
1803 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1804
[email protected]949f25a2012-06-27 01:53:091805 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031806 context.reset(NewContext(NULL));
1807 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501808 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131809 src_local_path,
[email protected]949f25a2012-06-27 01:53:091810 url));
1811 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031812}
1813
1814TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1815 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091816 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031817
1818 // Create working directory.
1819 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091820 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031821
1822 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421823 FileSystemURL url = FileSystemURLAppendUTF8(
1824 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031825 bool created = false;
1826 context.reset(NewContext(NULL));
1827 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091828 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031829 EXPECT_TRUE(created);
1830
[email protected]949f25a2012-06-27 01:53:091831 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031832 context.reset(NewContext(NULL));
1833 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091834 ofu()->DeleteFile(context.get(), url));
1835 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031836
1837 // fail case.
[email protected]949f25a2012-06-27 01:53:091838 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031839 context.reset(NewContext(NULL));
1840 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091841 ofu()->DeleteFile(context.get(), url));
1842 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031843
[email protected]bab213be2013-01-23 15:13:081844 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421845 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1846 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031847 context.reset(NewContext(NULL));
1848 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091849 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031850 created = false;
1851 context.reset(NewContext(NULL));
1852 EXPECT_EQ(base::PLATFORM_FILE_OK,
1853 ofu()->EnsureFileExists(context.get(), file_path, &created));
1854 EXPECT_TRUE(created);
1855
[email protected]949f25a2012-06-27 01:53:091856 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031857 context.reset(NewContext(NULL));
1858 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081859 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091860 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031861
1862 // delete case.
1863 context.reset(NewContext(NULL));
1864 EXPECT_EQ(base::PLATFORM_FILE_OK,
1865 ofu()->DeleteFile(context.get(), file_path));
1866
[email protected]949f25a2012-06-27 01:53:091867 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031868 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081869 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091870 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031871}
1872
1873TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1874 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091875 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031876 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091877 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031878 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091879 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031880 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091881 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031882}
[email protected]a3938912012-03-27 14:00:551883
1884TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091885 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421886 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1887 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551888
1889 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1890 EXPECT_EQ(base::PLATFORM_FILE_OK,
1891 ofu()->CreateDirectory(context.get(), dir, false, false));
1892
1893 bool created = false;
1894 context.reset(NewContext(NULL));
1895 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091896 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551897 EXPECT_TRUE(created);
1898
1899 context.reset(NewContext(NULL));
1900 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091901 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551902
[email protected]a3ef4832013-02-02 05:12:331903 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551904 context.reset(NewContext(NULL));
1905 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091906 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551907 EXPECT_FALSE(file_path.empty());
1908
1909 context.reset(NewContext(NULL));
1910 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091911 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551912 base::Time::Now() + base::TimeDelta::FromHours(1),
1913 base::Time()));
1914
1915 context.reset(NewContext(NULL));
1916 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1917 ofu()->CreateFileEnumerator(context.get(), dir, false));
1918
1919 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331920 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551921 while (!(file_path_each = file_enum->Next()).empty()) {
1922 context.reset(NewContext(NULL));
1923 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331924 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551925 EXPECT_EQ(base::PLATFORM_FILE_OK,
1926 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421927 FileSystemURL::CreateForTest(
1928 dir.origin(),
1929 dir.mount_type(),
1930 file_path_each),
[email protected]a3938912012-03-27 14:00:551931 &file_info, &file_path));
1932 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1933 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1934 EXPECT_EQ(file_info.size, file_enum->Size());
1935 ++count;
1936 }
1937 EXPECT_EQ(2, count);
1938}
[email protected]294dd0312012-05-11 07:35:131939
[email protected]58ac5272013-02-15 10:05:031940// crbug.com/176470
1941#if defined(OS_WIN) || defined(OS_ANDROID)
1942#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1943#else
1944#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1945#endif
1946TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091947 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1948 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1949 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1950 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131951 bool created;
1952
1953 int64 expected_total_file_size = 0;
1954 ASSERT_EQ(base::PLATFORM_FILE_OK,
1955 ofu()->EnsureFileExists(
1956 AllowUsageIncrease(PathCost(from_file))->context(),
1957 from_file, &created));
1958 ASSERT_TRUE(created);
1959 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1960
1961 ASSERT_EQ(base::PLATFORM_FILE_OK,
1962 ofu()->EnsureFileExists(
1963 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1964 obstacle_file, &created));
1965 ASSERT_TRUE(created);
1966 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1967
1968 int64 from_file_size = 1020;
1969 expected_total_file_size += from_file_size;
1970 ASSERT_EQ(base::PLATFORM_FILE_OK,
1971 ofu()->Truncate(
1972 AllowUsageIncrease(from_file_size)->context(),
1973 from_file, from_file_size));
1974 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1975
1976 int64 obstacle_file_size = 1;
1977 expected_total_file_size += obstacle_file_size;
1978 ASSERT_EQ(base::PLATFORM_FILE_OK,
1979 ofu()->Truncate(
1980 AllowUsageIncrease(obstacle_file_size)->context(),
1981 obstacle_file, obstacle_file_size));
1982 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1983
1984 int64 to_file1_size = from_file_size;
1985 expected_total_file_size += to_file1_size;
1986 ASSERT_EQ(base::PLATFORM_FILE_OK,
1987 ofu()->CopyOrMoveFile(
1988 AllowUsageIncrease(
1989 PathCost(to_file1) + to_file1_size)->context(),
1990 from_file, to_file1, true /* copy */));
1991 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1992
1993 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1994 ofu()->CopyOrMoveFile(
1995 DisallowUsageIncrease(
1996 PathCost(to_file2) + from_file_size)->context(),
1997 from_file, to_file2, true /* copy */));
1998 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1999
2000 int64 old_obstacle_file_size = obstacle_file_size;
2001 obstacle_file_size = from_file_size;
2002 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2003 ASSERT_EQ(base::PLATFORM_FILE_OK,
2004 ofu()->CopyOrMoveFile(
2005 AllowUsageIncrease(
2006 obstacle_file_size - old_obstacle_file_size)->context(),
2007 from_file, obstacle_file, true /* copy */));
2008 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2009
2010 int64 old_from_file_size = from_file_size;
2011 from_file_size = old_from_file_size - 1;
2012 expected_total_file_size += from_file_size - old_from_file_size;
2013 ASSERT_EQ(base::PLATFORM_FILE_OK,
2014 ofu()->Truncate(
2015 AllowUsageIncrease(
2016 from_file_size - old_from_file_size)->context(),
2017 from_file, from_file_size));
2018 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2019
2020 // quota exceeded
2021 {
2022 old_obstacle_file_size = obstacle_file_size;
2023 obstacle_file_size = from_file_size;
2024 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2025 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2026 obstacle_file_size - old_obstacle_file_size);
2027 helper->context()->set_allowed_bytes_growth(
2028 helper->context()->allowed_bytes_growth() - 1);
2029 ASSERT_EQ(base::PLATFORM_FILE_OK,
2030 ofu()->CopyOrMoveFile(
2031 helper->context(),
2032 from_file, obstacle_file, true /* copy */));
2033 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2034 }
2035}
2036
2037TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092038 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2039 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2040 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132041 bool created;
2042
2043 int64 expected_total_file_size = 0;
2044 ASSERT_EQ(base::PLATFORM_FILE_OK,
2045 ofu()->EnsureFileExists(
2046 AllowUsageIncrease(PathCost(from_file))->context(),
2047 from_file, &created));
2048 ASSERT_TRUE(created);
2049 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2050
2051 int64 from_file_size = 1020;
2052 expected_total_file_size += from_file_size;
2053 ASSERT_EQ(base::PLATFORM_FILE_OK,
2054 ofu()->Truncate(
2055 AllowUsageIncrease(from_file_size)->context(),
2056 from_file, from_file_size));
2057 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2058
2059 int64 to_file_size ALLOW_UNUSED = from_file_size;
2060 from_file_size = 0;
2061 ASSERT_EQ(base::PLATFORM_FILE_OK,
2062 ofu()->CopyOrMoveFile(
2063 AllowUsageIncrease(-PathCost(from_file) +
2064 PathCost(to_file))->context(),
2065 from_file, to_file, false /* move */));
2066 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2067
2068 ASSERT_EQ(base::PLATFORM_FILE_OK,
2069 ofu()->EnsureFileExists(
2070 AllowUsageIncrease(PathCost(from_file))->context(),
2071 from_file, &created));
2072 ASSERT_TRUE(created);
2073 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2074
2075 ASSERT_EQ(base::PLATFORM_FILE_OK,
2076 ofu()->EnsureFileExists(
2077 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2078 obstacle_file, &created));
2079 ASSERT_TRUE(created);
2080 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2081
2082 from_file_size = 1020;
2083 expected_total_file_size += from_file_size;
2084 ASSERT_EQ(base::PLATFORM_FILE_OK,
2085 ofu()->Truncate(
2086 AllowUsageIncrease(from_file_size)->context(),
2087 from_file, from_file_size));
2088 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2089
2090 int64 obstacle_file_size = 1;
2091 expected_total_file_size += obstacle_file_size;
2092 ASSERT_EQ(base::PLATFORM_FILE_OK,
2093 ofu()->Truncate(
2094 AllowUsageIncrease(1)->context(),
2095 obstacle_file, obstacle_file_size));
2096 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2097
2098 int64 old_obstacle_file_size = obstacle_file_size;
2099 obstacle_file_size = from_file_size;
2100 from_file_size = 0;
2101 expected_total_file_size -= old_obstacle_file_size;
2102 ASSERT_EQ(base::PLATFORM_FILE_OK,
2103 ofu()->CopyOrMoveFile(
2104 AllowUsageIncrease(
2105 -old_obstacle_file_size - PathCost(from_file))->context(),
2106 from_file, obstacle_file,
2107 false /* move */));
2108 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2109
2110 ASSERT_EQ(base::PLATFORM_FILE_OK,
2111 ofu()->EnsureFileExists(
2112 AllowUsageIncrease(PathCost(from_file))->context(),
2113 from_file, &created));
2114 ASSERT_TRUE(created);
2115 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2116
2117 from_file_size = 10;
2118 expected_total_file_size += from_file_size;
2119 ASSERT_EQ(base::PLATFORM_FILE_OK,
2120 ofu()->Truncate(
2121 AllowUsageIncrease(from_file_size)->context(),
2122 from_file, from_file_size));
2123 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2124
2125 // quota exceeded even after operation
2126 old_obstacle_file_size = obstacle_file_size;
2127 obstacle_file_size = from_file_size;
2128 from_file_size = 0;
2129 expected_total_file_size -= old_obstacle_file_size;
2130 scoped_ptr<FileSystemOperationContext> context =
2131 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2132 ASSERT_EQ(base::PLATFORM_FILE_OK,
2133 ofu()->CopyOrMoveFile(
2134 context.get(), from_file, obstacle_file, false /* move */));
2135 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2136 context.reset();
2137}
2138
2139TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092140 FileSystemURL dir(CreateURLFromUTF8("dir"));
2141 FileSystemURL file(CreateURLFromUTF8("file"));
2142 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2143 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132144 bool created;
2145
2146 ASSERT_EQ(base::PLATFORM_FILE_OK,
2147 ofu()->EnsureFileExists(
2148 AllowUsageIncrease(PathCost(file))->context(),
2149 file, &created));
2150 ASSERT_TRUE(created);
2151 ASSERT_EQ(0, ComputeTotalFileSize());
2152
2153 ASSERT_EQ(base::PLATFORM_FILE_OK,
2154 ofu()->CreateDirectory(
2155 AllowUsageIncrease(PathCost(dir))->context(),
2156 dir, false, false));
2157 ASSERT_EQ(0, ComputeTotalFileSize());
2158
2159 ASSERT_EQ(base::PLATFORM_FILE_OK,
2160 ofu()->EnsureFileExists(
2161 AllowUsageIncrease(PathCost(dfile1))->context(),
2162 dfile1, &created));
2163 ASSERT_TRUE(created);
2164 ASSERT_EQ(0, ComputeTotalFileSize());
2165
2166 ASSERT_EQ(base::PLATFORM_FILE_OK,
2167 ofu()->EnsureFileExists(
2168 AllowUsageIncrease(PathCost(dfile2))->context(),
2169 dfile2, &created));
2170 ASSERT_TRUE(created);
2171 ASSERT_EQ(0, ComputeTotalFileSize());
2172
2173 ASSERT_EQ(base::PLATFORM_FILE_OK,
2174 ofu()->Truncate(
2175 AllowUsageIncrease(340)->context(),
2176 file, 340));
2177 ASSERT_EQ(340, ComputeTotalFileSize());
2178
2179 ASSERT_EQ(base::PLATFORM_FILE_OK,
2180 ofu()->Truncate(
2181 AllowUsageIncrease(1020)->context(),
2182 dfile1, 1020));
2183 ASSERT_EQ(1360, ComputeTotalFileSize());
2184
2185 ASSERT_EQ(base::PLATFORM_FILE_OK,
2186 ofu()->Truncate(
2187 AllowUsageIncrease(120)->context(),
2188 dfile2, 120));
2189 ASSERT_EQ(1480, ComputeTotalFileSize());
2190
2191 ASSERT_EQ(base::PLATFORM_FILE_OK,
2192 ofu()->DeleteFile(
2193 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2194 file));
2195 ASSERT_EQ(1140, ComputeTotalFileSize());
2196
2197 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302198 AsyncFileTestHelper::Remove(
2199 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132200 ASSERT_EQ(0, ComputeTotalFileSize());
2201}
[email protected]7d78be12012-05-24 07:07:262202
2203TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092204 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262205 base::PlatformFile file_handle;
2206 bool created;
2207
2208 // Creating a file.
2209 ASSERT_EQ(base::PLATFORM_FILE_OK,
2210 ofu()->EnsureFileExists(
2211 AllowUsageIncrease(PathCost(file))->context(),
2212 file, &created));
2213 ASSERT_TRUE(created);
2214 ASSERT_EQ(0, ComputeTotalFileSize());
2215
2216 // Opening it, which shouldn't change the usage.
2217 ASSERT_EQ(base::PLATFORM_FILE_OK,
2218 ofu()->CreateOrOpen(
2219 AllowUsageIncrease(0)->context(), file,
2220 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2221 &file_handle, &created));
2222 ASSERT_EQ(0, ComputeTotalFileSize());
2223 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2224
2225 const int length = 33;
2226 ASSERT_EQ(base::PLATFORM_FILE_OK,
2227 ofu()->Truncate(
2228 AllowUsageIncrease(length)->context(), file, length));
2229 ASSERT_EQ(length, ComputeTotalFileSize());
2230
2231 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2232 ASSERT_EQ(base::PLATFORM_FILE_OK,
2233 ofu()->CreateOrOpen(
2234 AllowUsageIncrease(-length)->context(), file,
2235 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2236 &file_handle, &created));
2237 ASSERT_EQ(0, ComputeTotalFileSize());
2238 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2239
2240 // Extending the file again.
2241 ASSERT_EQ(base::PLATFORM_FILE_OK,
2242 ofu()->Truncate(
2243 AllowUsageIncrease(length)->context(), file, length));
2244 ASSERT_EQ(length, ComputeTotalFileSize());
2245
2246 // Opening it with TRUNCATED flag, which should truncate the file size.
2247 ASSERT_EQ(base::PLATFORM_FILE_OK,
2248 ofu()->CreateOrOpen(
2249 AllowUsageIncrease(-length)->context(), file,
2250 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2251 &file_handle, &created));
2252 ASSERT_EQ(0, ComputeTotalFileSize());
2253 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2254}
[email protected]c4e6f9c2012-09-09 17:42:102255
2256} // namespace fileapi