blob: 40cee9caa83476c6efba822f402852a98a0f14ab [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_path.h"
11#include "base/file_util.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]6ef0c3912013-01-25 22:46:3417#include "webkit/fileapi/external_mount_points.h"
[email protected]d4905e2e2011-05-13 21:56:3218#include "webkit/fileapi/file_system_context.h"
19#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1020#include "webkit/fileapi/file_system_task_runners.h"
[email protected]0c5ebe32011-08-19 22:37:1621#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]22267a62013-02-07 13:12:5922#include "webkit/fileapi/file_util_helper.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(
93 const FileSystemURL& url, const FilePath::StringType& child) {
94 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(),
103 url.virtual_path().Append(FilePath::FromUTF8Unsafe(child)));
104}
105
106FileSystemURL FileSystemURLDirName(const FileSystemURL& url) {
107 return FileSystemURL::CreateForTest(
108 url.origin(), url.mount_type(), url.virtual_path().DirName());
109}
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]22267a62013-02-07 13:12:59231 quota_manager_->GetUsageAndQuota(
232 origin(), test_helper_.storage_type(),
233 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
234 weak_factory_.GetWeakPtr()));
235 MessageLoop::current()->RunUntilIdle();
[email protected]0c5ebe32011-08-19 22:37:16236 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
237 }
238
239 void RevokeUsageCache() {
240 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]022d2702012-05-14 16:04:26241 file_util::Delete(test_helper_.GetUsageCachePath(), false);
242 }
243
244 int64 SizeByQuotaUtil() {
245 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16246 }
247
248 int64 SizeInUsageFile() {
[email protected]8eee1b62013-01-08 02:26:44249 MessageLoop::current()->RunUntilIdle();
[email protected]022d2702012-05-14 16:04:26250 return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath());
[email protected]0c5ebe32011-08-19 22:37:16251 }
252
[email protected]13f92f6e2012-08-13 07:39:14253 bool PathExists(const FileSystemURL& url) {
254 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21255 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33256 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21257 base::PlatformFileError error = ofu()->GetFileInfo(
258 context.get(), url, &file_info, &platform_path);
259 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14260 }
261
262 bool DirectoryExists(const FileSystemURL& url) {
[email protected]22267a62013-02-07 13:12:59263 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
264 return FileUtilHelper::DirectoryExists(context.get(), ofu(), url);
[email protected]13f92f6e2012-08-13 07:39:14265 }
266
[email protected]0c5ebe32011-08-19 22:37:16267 int64 usage() const { return usage_; }
268
[email protected]949f25a2012-06-27 01:53:09269 FileSystemURL CreateURLFromUTF8(const std::string& path) {
270 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50271 }
272
[email protected]949f25a2012-06-27 01:53:09273 int64 PathCost(const FileSystemURL& url) {
274 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13275 }
276
[email protected]a3ef4832013-02-02 05:12:33277 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]949f25a2012-06-27 01:53:09278 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50279 }
280
[email protected]22267a62013-02-07 13:12:59281 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
282 EXPECT_EQ(quota::kQuotaStatusOk, status);
283 quota_status_ = status;
284 usage_ = usage;
285 }
286
[email protected]d4905e2e2011-05-13 21:56:32287 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39288 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16289 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33290 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49291 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09292 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32293
294 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33295 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49296 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09297 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32298 EXPECT_EQ(data_path, local_path);
299 EXPECT_TRUE(FileExists(data_path));
300 EXPECT_EQ(0, GetSize(data_path));
301
302 const char data[] = "test data";
303 const int length = arraysize(data) - 1;
304
305 if (base::kInvalidPlatformFileValue == file_handle) {
306 bool created = true;
[email protected]403ada82013-01-08 07:51:39307 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32308 file_handle = base::CreatePlatformFile(
309 data_path,
310 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
311 &created,
312 &error);
[email protected]81b7f662011-05-26 00:54:46313 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32314 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
315 EXPECT_FALSE(created);
316 }
317 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
318 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
319
320 base::PlatformFileInfo file_info1;
321 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16322 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49323 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09324 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32325 EXPECT_EQ(data_path, local_path);
326
327 EXPECT_FALSE(file_info0.is_directory);
328 EXPECT_FALSE(file_info1.is_directory);
329 EXPECT_FALSE(file_info0.is_symbolic_link);
330 EXPECT_FALSE(file_info1.is_symbolic_link);
331 EXPECT_EQ(0, file_info0.size);
332 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32333 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32334
[email protected]0c5ebe32011-08-19 22:37:16335 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49336 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09337 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32338 EXPECT_EQ(length * 2, GetSize(data_path));
339
[email protected]0c5ebe32011-08-19 22:37:16340 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49341 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09342 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16343 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32344 }
345
346 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09347 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33348 const std::set<base::FilePath::StringType>& files,
349 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32350 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33351 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32352 for (iter = files.begin(); iter != files.end(); ++iter) {
353 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16354 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32355 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49356 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42357 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32358 &created));
359 ASSERT_FALSE(created);
360 }
361 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16362 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14363 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42364 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32365 }
366 }
367
[email protected]294dd0312012-05-11 07:35:13368 class UsageVerifyHelper {
369 public:
370 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33371 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13372 int64 expected_usage)
373 : context_(context.Pass()),
374 test_helper_(test_helper),
375 expected_usage_(expected_usage) {}
376
377 ~UsageVerifyHelper() {
[email protected]8eee1b62013-01-08 02:26:44378 MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13379 Check();
380 }
381
382 FileSystemOperationContext* context() {
383 return context_.get();
384 }
385
386 private:
387 void Check() {
388 ASSERT_EQ(expected_usage_,
389 test_helper_->GetCachedOriginUsage());
390 }
391
392 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33393 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13394 int64 expected_usage_;
395 };
396
397 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
398 int64 usage = test_helper_.GetCachedOriginUsage();
399 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
400 LimitedContext(requested_growth),
401 &test_helper_, usage + requested_growth));
402 }
403
404 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
405 int64 usage = test_helper_.GetCachedOriginUsage();
406 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
407 LimitedContext(requested_growth - 1), &test_helper_, usage));
408 }
409
[email protected]d4905e2e2011-05-13 21:56:32410 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09411 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33412 std::set<base::FilePath::StringType>* files,
413 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32414 scoped_ptr<FileSystemOperationContext> context;
[email protected]22267a62013-02-07 13:12:59415 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32416 std::vector<base::FileUtilProxy::Entry> entries;
417 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:59418 FileUtilHelper::ReadDirectory(
419 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32420 EXPECT_EQ(0UL, entries.size());
421
422 files->clear();
[email protected]89ee4272011-05-16 18:45:17423 files->insert(FILE_PATH_LITERAL("first"));
424 files->insert(FILE_PATH_LITERAL("second"));
425 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32426 directories->clear();
[email protected]89ee4272011-05-16 18:45:17427 directories->insert(FILE_PATH_LITERAL("fourth"));
428 directories->insert(FILE_PATH_LITERAL("fifth"));
429 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33430 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32431 for (iter = files->begin(); iter != files->end(); ++iter) {
432 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16433 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32434 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49435 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50436 context.get(),
[email protected]04c899f2013-02-08 08:28:42437 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50438 &created));
[email protected]d4905e2e2011-05-13 21:56:32439 ASSERT_TRUE(created);
440 }
441 for (iter = directories->begin(); iter != directories->end(); ++iter) {
442 bool exclusive = true;
443 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16444 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32445 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49446 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42447 context.get(),
448 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09449 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32450 }
[email protected]949f25a2012-06-27 01:53:09451 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32452 }
453
[email protected]949f25a2012-06-27 01:53:09454 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33455 std::set<base::FilePath::StringType> files;
456 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09457 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32458
459 scoped_ptr<FileSystemOperationContext> context;
460 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16461 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32462 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:59463 FileUtilHelper::ReadDirectory(
464 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32465 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
466 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10467 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32468 for (entry_iter = entries.begin(); entry_iter != entries.end();
469 ++entry_iter) {
470 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]a3ef4832013-02-02 05:12:33471 std::set<base::FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32472 if (iter != files.end()) {
473 EXPECT_FALSE(entry.is_directory);
474 files.erase(iter);
475 continue;
476 }
[email protected]89ee4272011-05-16 18:45:17477 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32478 EXPECT_FALSE(directories.end() == iter);
479 EXPECT_TRUE(entry.is_directory);
480 directories.erase(iter);
481 }
482 }
483
[email protected]949f25a2012-06-27 01:53:09484 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41485 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32486 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16487
[email protected]2517cfa2011-08-25 05:12:41488 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32489 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49490 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09491 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10492 // Currently we fire no change notifications for Touch.
493 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33494 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32495 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16496 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49497 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09498 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32499 // We compare as time_t here to lower our resolution, to avoid false
500 // negatives caused by conversion to the local filesystem's native
501 // representation and back.
502 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
503
[email protected]0c5ebe32011-08-19 22:37:16504 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32505 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41506 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32507 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49508 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09509 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10510 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16511 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49512 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09513 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32514 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49515 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41516 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32517 }
518
[email protected]81b7f662011-05-26 00:54:46519 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23520 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46521 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33522 base::FilePath root_file_path = source_dir.path();
523 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09524 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46525 int64 src_file_length = 87;
526
527 base::PlatformFileError error_code;
528 bool created = false;
529 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
530 base::PlatformFile file_handle =
531 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50532 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46533 EXPECT_TRUE(created);
534 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
535 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
536 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
537 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
538
539 scoped_ptr<FileSystemOperationContext> context;
540
541 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16542 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46543 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09544 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46545 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10546
547 // We must have observed one (and only one) create_file_count.
548 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
549 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46550 }
551
[email protected]0c5ebe32011-08-19 22:37:16552 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09553 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16554 if (!overwrite) {
555 // Verify that file creation requires sufficient quota for the path.
556 context.reset(NewContext(NULL));
557 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
558 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13559 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09560 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16561 }
562
563 context.reset(NewContext(NULL));
564 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46565 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13566 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09567 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16568
[email protected]13f92f6e2012-08-13 07:39:14569 EXPECT_TRUE(PathExists(dest_url));
570 EXPECT_FALSE(DirectoryExists(dest_url));
571
[email protected]0c5ebe32011-08-19 22:37:16572 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46573 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33574 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49575 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09576 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50577 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46578 EXPECT_TRUE(FileExists(data_path));
579 EXPECT_EQ(src_file_length, GetSize(data_path));
580
581 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09582 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46583 }
584
[email protected]949f25a2012-06-27 01:53:09585 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03586 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
587 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09588 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
589 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03590 }
591
[email protected]949f25a2012-06-27 01:53:09592 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03593 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33594 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03595 base::PlatformFileInfo file_info;
596 context.reset(NewContext(NULL));
597 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09598 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10599 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03600 return file_info.last_modified;
601 }
602
[email protected]949f25a2012-06-27 01:53:09603 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03604 bool copy,
605 bool overwrite) {
606 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42607 const FileSystemURL src_dir_url(
608 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
609 const FileSystemURL dest_dir_url(
610 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03611
[email protected]04c899f2013-02-08 08:28:42612 const FileSystemURL src_file_url(
613 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
614 const FileSystemURL dest_file_url(
615 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03616
617 context.reset(NewContext(NULL));
618 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09619 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03620 context.reset(NewContext(NULL));
621 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09622 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03623
624 bool created = false;
625 context.reset(NewContext(NULL));
626 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09627 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03628 if (overwrite) {
629 context.reset(NewContext(NULL));
630 EXPECT_EQ(base::PLATFORM_FILE_OK,
631 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09632 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03633 }
634
[email protected]949f25a2012-06-27 01:53:09635 ClearTimestamp(src_dir_url);
636 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03637 context.reset(NewContext(NULL));
638 EXPECT_EQ(base::PLATFORM_FILE_OK,
639 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09640 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03641 copy));
[email protected]fad625e2f2011-12-08 05:38:03642 if (copy)
[email protected]949f25a2012-06-27 01:53:09643 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03644 else
[email protected]949f25a2012-06-27 01:53:09645 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
646 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03647 }
648
[email protected]ecdfd6c52012-04-11 13:35:44649 int64 ComputeCurrentUsage() {
650 return test_helper().ComputeCurrentOriginUsage() -
651 test_helper().ComputeCurrentDirectoryDatabaseUsage();
652 }
653
[email protected]02a60542012-07-24 20:05:33654 const LocalFileSystemTestOriginHelper& test_helper() const {
655 return test_helper_;
656 }
[email protected]45ea0fbbb2012-02-27 22:28:49657
[email protected]d4905e2e2011-05-13 21:56:32658 private:
[email protected]ea1a3f62012-11-16 20:34:23659 base::ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01660 MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16661 scoped_refptr<quota::QuotaManager> quota_manager_;
662 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26663 GURL origin_;
664 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03665 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]02a60542012-07-24 20:05:33666 LocalFileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16667 quota::QuotaStatusCode quota_status_;
668 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10669 MockFileChangeObserver change_observer_;
670 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32671
[email protected]7878ece2011-09-05 11:41:49672 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32673};
674
[email protected]7878ece2011-09-05 11:41:49675TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32676 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
677 bool created;
[email protected]949f25a2012-06-27 01:53:09678 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16679 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32680 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
681
682 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49683 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09684 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32685 &created));
686
[email protected]0c5ebe32011-08-19 22:37:16687 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32688 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09689 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32690
[email protected]949f25a2012-06-27 01:53:09691 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32692
[email protected]c4e6f9c2012-09-09 17:42:10693 EXPECT_TRUE(change_observer()->HasNoChange());
694
[email protected]0c5ebe32011-08-19 22:37:16695 // Verify that file creation requires sufficient quota for the path.
696 context.reset(NewContext(NULL));
697 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09698 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16699 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49700 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09701 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16702
703 context.reset(NewContext(NULL));
704 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09705 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32706 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49707 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09708 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32709 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10710 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32711 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
712
[email protected]949f25a2012-06-27 01:53:09713 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32714
[email protected]0c5ebe32011-08-19 22:37:16715 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33716 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49717 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09718 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32719 EXPECT_TRUE(file_util::PathExists(local_path));
720
[email protected]0c5ebe32011-08-19 22:37:16721 // Verify that deleting a file isn't stopped by zero quota, and that it frees
722 // up quote from its path.
723 context.reset(NewContext(NULL));
724 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32725 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09726 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10727 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32728 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09729 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16730 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32731
[email protected]0c5ebe32011-08-19 22:37:16732 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32733 bool exclusive = true;
734 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09735 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50736 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42737 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49738 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09739 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10740 // The oepration created 3 directories recursively.
741 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32742
[email protected]0c5ebe32011-08-19 22:37:16743 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32744 file_handle = base::kInvalidPlatformFileValue;
745 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49746 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09747 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32748 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10749 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32750 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
751
[email protected]949f25a2012-06-27 01:53:09752 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32753
[email protected]0c5ebe32011-08-19 22:37:16754 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49755 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09756 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32757 EXPECT_TRUE(file_util::PathExists(local_path));
758
[email protected]0c5ebe32011-08-19 22:37:16759 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32760 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09761 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10762 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32763 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10764
765 // Make sure we have no unexpected changes.
766 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32767}
768
[email protected]7878ece2011-09-05 11:41:49769TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32770 bool created = false;
[email protected]949f25a2012-06-27 01:53:09771 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16772 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32773
774 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09775 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32776
[email protected]0c5ebe32011-08-19 22:37:16777 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32778 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09779 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32780 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10781 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32782
[email protected]0c5ebe32011-08-19 22:37:16783 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33784 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49785 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09786 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32787 EXPECT_EQ(0, GetSize(local_path));
788
[email protected]0c5ebe32011-08-19 22:37:16789 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49790 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09791 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10792 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32793 EXPECT_EQ(10, GetSize(local_path));
794
[email protected]0c5ebe32011-08-19 22:37:16795 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49796 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09797 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32798 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10799 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32800
[email protected]13f92f6e2012-08-13 07:39:14801 EXPECT_FALSE(DirectoryExists(url));
802 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10803
804 // Make sure we have no unexpected changes.
805 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32806}
807
[email protected]ecdfd6c52012-04-11 13:35:44808TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
809 bool created = false;
[email protected]949f25a2012-06-27 01:53:09810 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44811
812 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13813 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09814 AllowUsageIncrease(PathCost(url))->context(),
815 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44816 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13817 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44818
[email protected]ecdfd6c52012-04-11 13:35:44819 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13820 ofu()->Truncate(
821 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09822 url, 1020));
[email protected]294dd0312012-05-11 07:35:13823 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44824
[email protected]ecdfd6c52012-04-11 13:35:44825 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13826 ofu()->Truncate(
827 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09828 url, 0));
[email protected]294dd0312012-05-11 07:35:13829 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44830
[email protected]ecdfd6c52012-04-11 13:35:44831 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13832 ofu()->Truncate(
833 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09834 url, 1021));
[email protected]294dd0312012-05-11 07:35:13835 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44836
[email protected]ecdfd6c52012-04-11 13:35:44837 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13838 ofu()->Truncate(
839 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09840 url, 1020));
[email protected]294dd0312012-05-11 07:35:13841 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44842
[email protected]ecdfd6c52012-04-11 13:35:44843 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13844 ofu()->Truncate(
845 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09846 url, 1020));
[email protected]294dd0312012-05-11 07:35:13847 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44848
[email protected]294dd0312012-05-11 07:35:13849 // quota exceeded
850 {
851 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
852 helper->context()->set_allowed_bytes_growth(
853 helper->context()->allowed_bytes_growth() - 1);
854 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09855 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13856 ASSERT_EQ(1019, ComputeTotalFileSize());
857 }
[email protected]ecdfd6c52012-04-11 13:35:44858
859 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33860 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44861 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13862 ofu()->GetLocalFilePath(
863 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09864 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44865 ASSERT_FALSE(local_path.empty());
866 ASSERT_TRUE(file_util::Delete(local_path, false));
867
[email protected]ecdfd6c52012-04-11 13:35:44868 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13869 ofu()->Truncate(
870 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09871 url, 1234));
[email protected]294dd0312012-05-11 07:35:13872 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44873}
874
[email protected]7878ece2011-09-05 11:41:49875TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09876 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32877 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16878 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32879 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49880 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09881 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10882 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32883
[email protected]0c5ebe32011-08-19 22:37:16884 // Verify that file creation requires sufficient quota for the path.
885 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09886 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32887 created = false;
[email protected]0c5ebe32011-08-19 22:37:16888 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09889 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16890 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09891 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16892 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10893 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16894
895 context.reset(NewContext(NULL));
896 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09897 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32898 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09899 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32900 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10901 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32902
[email protected]949f25a2012-06-27 01:53:09903 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32904
[email protected]0c5ebe32011-08-19 22:37:16905 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32906 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09907 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32908 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10909 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32910
911 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09912 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16913 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32914 bool exclusive = true;
915 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49916 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09917 context.get(),
[email protected]04c899f2013-02-08 08:28:42918 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09919 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10920 // 2 directories: path/ and path/to.
921 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32922
[email protected]0c5ebe32011-08-19 22:37:16923 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32924 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09925 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32926 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14927 EXPECT_FALSE(DirectoryExists(url));
928 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10929 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32930}
931
[email protected]7878ece2011-09-05 11:41:49932TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16933 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32934
935 bool exclusive = false;
936 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09937 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49938 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09939 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32940
[email protected]0c5ebe32011-08-19 22:37:16941 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32942 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08943 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32944
[email protected]949f25a2012-06-27 01:53:09945 FileSystemURL root = CreateURLFromUTF8("");
[email protected]13f92f6e2012-08-13 07:39:14946 EXPECT_FALSE(DirectoryExists(url));
947 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16948 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49949 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32950
[email protected]0c5ebe32011-08-19 22:37:16951 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32952 exclusive = false;
953 recursive = true;
[email protected]7878ece2011-09-05 11:41:49954 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09955 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10956 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32957
[email protected]13f92f6e2012-08-13 07:39:14958 EXPECT_TRUE(DirectoryExists(url));
959 EXPECT_TRUE(PathExists(url));
960
[email protected]0c5ebe32011-08-19 22:37:16961 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49962 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42963 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14964
[email protected]0c5ebe32011-08-19 22:37:16965 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09966 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42967 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32968
969 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16970 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06971 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08972 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42973 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10974 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32975
976 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33977 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49978 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09979 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32980 EXPECT_TRUE(local_path.empty());
981 EXPECT_TRUE(file_info.is_directory);
982 EXPECT_FALSE(file_info.is_symbolic_link);
983
984 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16985 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49986 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09987 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10988 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32989
990 exclusive = true;
991 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16992 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49993 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09994 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10995 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32996
[email protected]0c5ebe32011-08-19 22:37:16997 // Verify that deleting a directory isn't stopped by zero quota, and that it
998 // frees up quota from its path.
999 context.reset(NewContext(NULL));
1000 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:081001 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101002 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091003 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161004 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321005
[email protected]949f25a2012-06-27 01:53:091006 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321007
[email protected]13f92f6e2012-08-13 07:39:141008 EXPECT_FALSE(DirectoryExists(url));
1009 EXPECT_FALSE(PathExists(url));
1010
[email protected]0c5ebe32011-08-19 22:37:161011 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091012 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491013 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091014 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321015
[email protected]0c5ebe32011-08-19 22:37:161016 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321017 exclusive = true;
1018 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161019 context.reset(NewContext(NULL));
1020 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091021 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491022 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091023 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101024 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161025
1026 context.reset(NewContext(NULL));
1027 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091028 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491029 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091030 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101031 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321032
[email protected]13f92f6e2012-08-13 07:39:141033 EXPECT_TRUE(DirectoryExists(url));
1034 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321035
1036 exclusive = true;
1037 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141038 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491039 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091040 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101041 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321042
1043 exclusive = true;
1044 recursive = false;
[email protected]949f25a2012-06-27 01:53:091045 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141046 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491047 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091048 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101049 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321050
[email protected]949f25a2012-06-27 01:53:091051 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321052
[email protected]13f92f6e2012-08-13 07:39:141053 EXPECT_FALSE(DirectoryExists(url));
1054 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321055
1056 exclusive = true;
1057 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141058 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491059 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091060 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101061 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321062
[email protected]13f92f6e2012-08-13 07:39:141063 EXPECT_TRUE(DirectoryExists(url));
1064 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321065
1066 exclusive = true;
1067 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141068 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491069 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091070 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101071 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321072}
1073
[email protected]7878ece2011-09-05 11:41:491074TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161075 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321076 bool exclusive = true;
1077 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091078 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491079 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091080 context.get(), url, exclusive, recursive));
1081 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321082}
1083
[email protected]7878ece2011-09-05 11:41:491084TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]949f25a2012-06-27 01:53:091085 TestReadDirectoryHelper(CreateURLFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:321086}
1087
[email protected]7878ece2011-09-05 11:41:491088TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091089 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321090}
1091
[email protected]7878ece2011-09-05 11:41:491092TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091093 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161094 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321095
1096 bool created = false;
1097 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091098 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321099 ASSERT_TRUE(created);
1100
[email protected]22267a62013-02-07 13:12:591101 context.reset(NewContext(NULL));
[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]22267a62013-02-07 13:12:591104 FileUtilHelper::ReadDirectory(
1105 context.get(), ofu(), 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]22267a62013-02-07 13:12:591420 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321421 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:591422 test_helper().SameFileUtilCopy(context.get(), 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]22267a62013-02-07 13:12:591427 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321428 recursive = true;
1429 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:591430 FileUtilHelper::Delete(context.get(), ofu(),
1431 dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141432 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321433}
[email protected]6b931152011-05-20 21:02:351434
[email protected]7878ece2011-09-05 11:41:491435TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1436 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1437 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161438 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261439 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161440 EXPECT_EQ(origin(), enumerator->Next());
1441 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1442 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1443 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261444 EXPECT_EQ(GURL(), enumerator->Next());
1445 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1446 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1447
1448 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161449 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261450
1451 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1452 SCOPED_TRACE(testing::Message() <<
1453 "Validating kOriginEnumerationTestRecords " << i);
1454 const OriginEnumerationTestRecord& record =
1455 kOriginEnumerationTestRecords[i];
1456 GURL origin_url(record.origin_url);
1457 origins_expected.insert(origin_url);
1458 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331459 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161460 NewHelper(origin_url, kFileSystemTypeTemporary));
1461 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261462 bool created = false;
1463 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501464 ofu()->EnsureFileExists(
1465 context.get(),
[email protected]949f25a2012-06-27 01:53:091466 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501467 &created));
[email protected]fcc2d5f2011-05-23 22:06:261468 EXPECT_TRUE(created);
1469 }
1470 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331471 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161472 NewHelper(origin_url, kFileSystemTypePersistent));
1473 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261474 bool created = false;
1475 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501476 ofu()->EnsureFileExists(
1477 context.get(),
[email protected]949f25a2012-06-27 01:53:091478 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501479 &created));
[email protected]fcc2d5f2011-05-23 22:06:261480 EXPECT_TRUE(created);
1481 }
1482 }
[email protected]7878ece2011-09-05 11:41:491483 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261484 EXPECT_TRUE(enumerator.get());
1485 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161486 GURL origin_url;
1487 while (!(origin_url = enumerator->Next()).is_empty()) {
1488 origins_found.insert(origin_url);
1489 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261490 bool found = false;
1491 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1492 ++i) {
1493 const OriginEnumerationTestRecord& record =
1494 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161495 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261496 continue;
1497 found = true;
1498 EXPECT_EQ(record.has_temporary,
1499 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1500 EXPECT_EQ(record.has_persistent,
1501 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1502 }
[email protected]0c5ebe32011-08-19 22:37:161503 // Deal with the default filesystem created by the test helper.
1504 if (!found && origin_url == origin()) {
1505 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1506 EXPECT_EQ(true,
1507 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181508 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161509 found = true;
1510 }
[email protected]fcc2d5f2011-05-23 22:06:261511 EXPECT_TRUE(found);
1512 }
1513
1514 std::set<GURL> diff;
1515 std::set_symmetric_difference(origins_expected.begin(),
1516 origins_expected.end(), origins_found.begin(), origins_found.end(),
1517 inserter(diff, diff.begin()));
1518 EXPECT_TRUE(diff.empty());
1519}
[email protected]0c5ebe32011-08-19 22:37:161520
[email protected]7878ece2011-09-05 11:41:491521TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161522 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1523
1524 int64 expected_quota = 0;
1525
[email protected]f83b5b72012-01-27 10:26:561526 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011527 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561528 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331529 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501530 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161531 if (test_case.is_directory) {
1532 bool exclusive = true;
1533 bool recursive = false;
1534 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091535 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501536 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161537 } else {
1538 bool created = false;
1539 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091540 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501541 &created));
[email protected]0c5ebe32011-08-19 22:37:161542 ASSERT_TRUE(created);
1543 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501544 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091545 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501546 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161547 expected_quota += test_case.data_file_size;
1548 }
1549 }
[email protected]022d2702012-05-14 16:04:261550
1551 // Usually raw size in usage cache and the usage returned by QuotaUtil
1552 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161553 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261554 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1555
[email protected]0c5ebe32011-08-19 22:37:161556 RevokeUsageCache();
1557 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261558 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1559
1560 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161561 GetUsageFromQuotaManager();
1562 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261563 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161564 EXPECT_EQ(expected_quota, usage());
1565}
[email protected]34583332011-08-31 08:59:471566
[email protected]7878ece2011-09-05 11:41:491567TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091568 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1569 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471570
1571 scoped_ptr<FileSystemOperationContext> context;
1572 base::PlatformFile file;
1573 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331574 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471575 bool created = false;
1576
1577 // Create a non-empty file.
1578 context.reset(NewContext(NULL));
1579 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491580 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471581 EXPECT_TRUE(created);
1582 context.reset(NewContext(NULL));
1583 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491584 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471585 context.reset(NewContext(NULL));
1586 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491587 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471588 context.get(), kPath1, &file_info, &data_path));
1589 EXPECT_EQ(10, file_info.size);
1590
1591 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491592 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471593
1594 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141595 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471596 context.reset(NewContext(NULL));
1597 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491598 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471599 EXPECT_TRUE(created);
1600 context.reset(NewContext(NULL));
1601 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491602 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471603 context.get(), kPath1, &file_info, &data_path));
1604 EXPECT_EQ(0, file_info.size);
1605
1606 // Make another broken file to |kPath2|.
1607 context.reset(NewContext(NULL));
1608 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491609 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471610 EXPECT_TRUE(created);
1611
1612 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491613 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471614
1615 // Repair broken |kPath1|.
1616 context.reset(NewContext(NULL));
1617 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491618 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471619 base::Time::Now()));
1620 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491621 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471622 EXPECT_TRUE(created);
1623
1624 // Copy from sound |kPath1| to broken |kPath2|.
1625 context.reset(NewContext(NULL));
1626 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491627 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501628 true /* copy */));
[email protected]34583332011-08-31 08:59:471629
[email protected]7878ece2011-09-05 11:41:491630 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471631 context.reset(NewContext(NULL));
1632 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491633 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471634 context.get(), kPath1,
1635 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1636 &file, &created));
1637 EXPECT_TRUE(created);
1638 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1639 EXPECT_EQ(0, file_info.size);
1640 EXPECT_TRUE(base::ClosePlatformFile(file));
1641}
[email protected]9dfdc0e32011-09-02 06:07:441642
[email protected]7878ece2011-09-05 11:41:491643TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091644 const FileSystemURL kPath[] = {
1645 CreateURLFromUTF8("foo"),
1646 CreateURLFromUTF8("bar"),
1647 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441648 };
[email protected]a3ef4832013-02-02 05:12:331649 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441650 scoped_ptr<FileSystemOperationContext> context;
1651
1652 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1653 bool created = false;
1654 context.reset(NewContext(NULL));
1655 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491656 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441657 EXPECT_TRUE(created);
1658 }
1659
[email protected]22267a62013-02-07 13:12:591660 context.reset(NewContext(NULL));
[email protected]9dfdc0e32011-09-02 06:07:441661 std::vector<base::FileUtilProxy::Entry> entries;
1662 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:591663 FileUtilHelper::ReadDirectory(
1664 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441665 EXPECT_EQ(3u, entries.size());
1666
[email protected]22267a62013-02-07 13:12:591667 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:331668 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441669 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491670 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441671 EXPECT_TRUE(file_util::Delete(local_path, false));
1672
[email protected]22267a62013-02-07 13:12:591673 context.reset(NewContext(NULL));
[email protected]9dfdc0e32011-09-02 06:07:441674 entries.clear();
1675 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:591676 FileUtilHelper::ReadDirectory(
1677 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441678 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1679}
[email protected]fad625e2f2011-12-08 05:38:031680
1681TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1682 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091683 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031684
1685 // Create working directory.
1686 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091687 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031688
1689 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421690 FileSystemURL url(FileSystemURLAppendUTF8(
1691 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031692 bool created = false;
[email protected]949f25a2012-06-27 01:53:091693 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031694 context.reset(NewContext(NULL));
1695 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091696 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031697 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091698 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031699
1700 // non create case.
1701 created = true;
[email protected]949f25a2012-06-27 01:53:091702 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031703 context.reset(NewContext(NULL));
1704 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091705 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031706 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091707 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031708
1709 // fail case.
[email protected]04c899f2013-02-08 08:28:421710 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031711 context.reset(NewContext(NULL));
1712 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091713 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031714
[email protected]949f25a2012-06-27 01:53:091715 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031716 context.reset(NewContext(NULL));
1717 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091718 ofu()->EnsureFileExists(context.get(), url, &created));
1719 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031720
1721 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421722 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391723 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031724 created = false;
[email protected]949f25a2012-06-27 01:53:091725 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031726 context.reset(NewContext(NULL));
1727 EXPECT_EQ(base::PLATFORM_FILE_OK,
1728 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091729 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031730 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1731 &file_handle, &created));
1732 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1733 EXPECT_TRUE(created);
1734 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091735 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031736
1737 // open case.
1738 file_handle = base::kInvalidPlatformFileValue;
1739 created = true;
[email protected]949f25a2012-06-27 01:53:091740 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031741 context.reset(NewContext(NULL));
1742 EXPECT_EQ(base::PLATFORM_FILE_OK,
1743 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091744 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031745 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1746 &file_handle, &created));
1747 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1748 EXPECT_FALSE(created);
1749 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091750 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031751
1752 // fail case
1753 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091754 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031755 context.reset(NewContext(NULL));
1756 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1757 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091758 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031759 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1760 &file_handle, &created));
1761 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091762 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031763
1764 // CreateDirectory, create case.
1765 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421766 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1767 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091768 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031769 context.reset(NewContext(NULL));
1770 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091771 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031772 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091773 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031774
1775 // create subdir case.
1776 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421777 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091778 ClearTimestamp(dir_url);
1779 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031780 context.reset(NewContext(NULL));
1781 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091782 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031783 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091784 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1785 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031786
1787 // fail case.
[email protected]04c899f2013-02-08 08:28:421788 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091789 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031790 context.reset(NewContext(NULL));
1791 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091792 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031793 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091794 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031795
1796 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421797 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1798 FileSystemURL src_path = FileSystemURLAppendUTF8(
1799 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031800 context.reset(NewContext(NULL));
1801 EXPECT_EQ(base::PLATFORM_FILE_OK,
1802 ofu()->EnsureFileExists(context.get(), src_path, &created));
1803 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331804 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031805 context.reset(NewContext(NULL));
1806 EXPECT_EQ(base::PLATFORM_FILE_OK,
1807 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1808
[email protected]949f25a2012-06-27 01:53:091809 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031810 context.reset(NewContext(NULL));
1811 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501812 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131813 src_local_path,
[email protected]949f25a2012-06-27 01:53:091814 url));
1815 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031816}
1817
1818TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1819 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091820 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031821
1822 // Create working directory.
1823 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091824 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031825
1826 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421827 FileSystemURL url = FileSystemURLAppendUTF8(
1828 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031829 bool created = false;
1830 context.reset(NewContext(NULL));
1831 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091832 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031833 EXPECT_TRUE(created);
1834
[email protected]949f25a2012-06-27 01:53:091835 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031836 context.reset(NewContext(NULL));
1837 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091838 ofu()->DeleteFile(context.get(), url));
1839 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031840
1841 // fail case.
[email protected]949f25a2012-06-27 01:53:091842 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031843 context.reset(NewContext(NULL));
1844 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091845 ofu()->DeleteFile(context.get(), url));
1846 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031847
[email protected]bab213be2013-01-23 15:13:081848 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421849 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1850 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031851 context.reset(NewContext(NULL));
1852 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091853 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031854 created = false;
1855 context.reset(NewContext(NULL));
1856 EXPECT_EQ(base::PLATFORM_FILE_OK,
1857 ofu()->EnsureFileExists(context.get(), file_path, &created));
1858 EXPECT_TRUE(created);
1859
[email protected]949f25a2012-06-27 01:53:091860 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031861 context.reset(NewContext(NULL));
1862 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081863 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091864 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031865
1866 // delete case.
1867 context.reset(NewContext(NULL));
1868 EXPECT_EQ(base::PLATFORM_FILE_OK,
1869 ofu()->DeleteFile(context.get(), file_path));
1870
[email protected]949f25a2012-06-27 01:53:091871 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031872 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081873 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091874 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031875}
1876
1877TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1878 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091879 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031880 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091881 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031882 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091883 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031884 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091885 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031886}
[email protected]a3938912012-03-27 14:00:551887
1888TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091889 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421890 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1891 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551892
1893 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1894 EXPECT_EQ(base::PLATFORM_FILE_OK,
1895 ofu()->CreateDirectory(context.get(), dir, false, false));
1896
1897 bool created = false;
1898 context.reset(NewContext(NULL));
1899 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091900 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551901 EXPECT_TRUE(created);
1902
1903 context.reset(NewContext(NULL));
1904 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091905 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551906
[email protected]a3ef4832013-02-02 05:12:331907 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551908 context.reset(NewContext(NULL));
1909 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091910 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551911 EXPECT_FALSE(file_path.empty());
1912
1913 context.reset(NewContext(NULL));
1914 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091915 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551916 base::Time::Now() + base::TimeDelta::FromHours(1),
1917 base::Time()));
1918
1919 context.reset(NewContext(NULL));
1920 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1921 ofu()->CreateFileEnumerator(context.get(), dir, false));
1922
1923 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331924 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551925 while (!(file_path_each = file_enum->Next()).empty()) {
1926 context.reset(NewContext(NULL));
1927 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331928 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551929 EXPECT_EQ(base::PLATFORM_FILE_OK,
1930 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421931 FileSystemURL::CreateForTest(
1932 dir.origin(),
1933 dir.mount_type(),
1934 file_path_each),
[email protected]a3938912012-03-27 14:00:551935 &file_info, &file_path));
1936 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1937 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1938 EXPECT_EQ(file_info.size, file_enum->Size());
1939 ++count;
1940 }
1941 EXPECT_EQ(2, count);
1942}
[email protected]294dd0312012-05-11 07:35:131943
1944TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091945 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1946 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1947 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1948 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131949 bool created;
1950
1951 int64 expected_total_file_size = 0;
1952 ASSERT_EQ(base::PLATFORM_FILE_OK,
1953 ofu()->EnsureFileExists(
1954 AllowUsageIncrease(PathCost(from_file))->context(),
1955 from_file, &created));
1956 ASSERT_TRUE(created);
1957 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1958
1959 ASSERT_EQ(base::PLATFORM_FILE_OK,
1960 ofu()->EnsureFileExists(
1961 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1962 obstacle_file, &created));
1963 ASSERT_TRUE(created);
1964 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1965
1966 int64 from_file_size = 1020;
1967 expected_total_file_size += from_file_size;
1968 ASSERT_EQ(base::PLATFORM_FILE_OK,
1969 ofu()->Truncate(
1970 AllowUsageIncrease(from_file_size)->context(),
1971 from_file, from_file_size));
1972 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1973
1974 int64 obstacle_file_size = 1;
1975 expected_total_file_size += obstacle_file_size;
1976 ASSERT_EQ(base::PLATFORM_FILE_OK,
1977 ofu()->Truncate(
1978 AllowUsageIncrease(obstacle_file_size)->context(),
1979 obstacle_file, obstacle_file_size));
1980 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1981
1982 int64 to_file1_size = from_file_size;
1983 expected_total_file_size += to_file1_size;
1984 ASSERT_EQ(base::PLATFORM_FILE_OK,
1985 ofu()->CopyOrMoveFile(
1986 AllowUsageIncrease(
1987 PathCost(to_file1) + to_file1_size)->context(),
1988 from_file, to_file1, true /* copy */));
1989 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1990
1991 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1992 ofu()->CopyOrMoveFile(
1993 DisallowUsageIncrease(
1994 PathCost(to_file2) + from_file_size)->context(),
1995 from_file, to_file2, true /* copy */));
1996 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1997
1998 int64 old_obstacle_file_size = obstacle_file_size;
1999 obstacle_file_size = from_file_size;
2000 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2001 ASSERT_EQ(base::PLATFORM_FILE_OK,
2002 ofu()->CopyOrMoveFile(
2003 AllowUsageIncrease(
2004 obstacle_file_size - old_obstacle_file_size)->context(),
2005 from_file, obstacle_file, true /* copy */));
2006 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2007
2008 int64 old_from_file_size = from_file_size;
2009 from_file_size = old_from_file_size - 1;
2010 expected_total_file_size += from_file_size - old_from_file_size;
2011 ASSERT_EQ(base::PLATFORM_FILE_OK,
2012 ofu()->Truncate(
2013 AllowUsageIncrease(
2014 from_file_size - old_from_file_size)->context(),
2015 from_file, from_file_size));
2016 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2017
2018 // quota exceeded
2019 {
2020 old_obstacle_file_size = obstacle_file_size;
2021 obstacle_file_size = from_file_size;
2022 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2023 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2024 obstacle_file_size - old_obstacle_file_size);
2025 helper->context()->set_allowed_bytes_growth(
2026 helper->context()->allowed_bytes_growth() - 1);
2027 ASSERT_EQ(base::PLATFORM_FILE_OK,
2028 ofu()->CopyOrMoveFile(
2029 helper->context(),
2030 from_file, obstacle_file, true /* copy */));
2031 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2032 }
2033}
2034
2035TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092036 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2037 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2038 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132039 bool created;
2040
2041 int64 expected_total_file_size = 0;
2042 ASSERT_EQ(base::PLATFORM_FILE_OK,
2043 ofu()->EnsureFileExists(
2044 AllowUsageIncrease(PathCost(from_file))->context(),
2045 from_file, &created));
2046 ASSERT_TRUE(created);
2047 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2048
2049 int64 from_file_size = 1020;
2050 expected_total_file_size += from_file_size;
2051 ASSERT_EQ(base::PLATFORM_FILE_OK,
2052 ofu()->Truncate(
2053 AllowUsageIncrease(from_file_size)->context(),
2054 from_file, from_file_size));
2055 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2056
2057 int64 to_file_size ALLOW_UNUSED = from_file_size;
2058 from_file_size = 0;
2059 ASSERT_EQ(base::PLATFORM_FILE_OK,
2060 ofu()->CopyOrMoveFile(
2061 AllowUsageIncrease(-PathCost(from_file) +
2062 PathCost(to_file))->context(),
2063 from_file, to_file, false /* move */));
2064 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2065
2066 ASSERT_EQ(base::PLATFORM_FILE_OK,
2067 ofu()->EnsureFileExists(
2068 AllowUsageIncrease(PathCost(from_file))->context(),
2069 from_file, &created));
2070 ASSERT_TRUE(created);
2071 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2072
2073 ASSERT_EQ(base::PLATFORM_FILE_OK,
2074 ofu()->EnsureFileExists(
2075 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2076 obstacle_file, &created));
2077 ASSERT_TRUE(created);
2078 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2079
2080 from_file_size = 1020;
2081 expected_total_file_size += from_file_size;
2082 ASSERT_EQ(base::PLATFORM_FILE_OK,
2083 ofu()->Truncate(
2084 AllowUsageIncrease(from_file_size)->context(),
2085 from_file, from_file_size));
2086 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2087
2088 int64 obstacle_file_size = 1;
2089 expected_total_file_size += obstacle_file_size;
2090 ASSERT_EQ(base::PLATFORM_FILE_OK,
2091 ofu()->Truncate(
2092 AllowUsageIncrease(1)->context(),
2093 obstacle_file, obstacle_file_size));
2094 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2095
2096 int64 old_obstacle_file_size = obstacle_file_size;
2097 obstacle_file_size = from_file_size;
2098 from_file_size = 0;
2099 expected_total_file_size -= old_obstacle_file_size;
2100 ASSERT_EQ(base::PLATFORM_FILE_OK,
2101 ofu()->CopyOrMoveFile(
2102 AllowUsageIncrease(
2103 -old_obstacle_file_size - PathCost(from_file))->context(),
2104 from_file, obstacle_file,
2105 false /* move */));
2106 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2107
2108 ASSERT_EQ(base::PLATFORM_FILE_OK,
2109 ofu()->EnsureFileExists(
2110 AllowUsageIncrease(PathCost(from_file))->context(),
2111 from_file, &created));
2112 ASSERT_TRUE(created);
2113 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2114
2115 from_file_size = 10;
2116 expected_total_file_size += from_file_size;
2117 ASSERT_EQ(base::PLATFORM_FILE_OK,
2118 ofu()->Truncate(
2119 AllowUsageIncrease(from_file_size)->context(),
2120 from_file, from_file_size));
2121 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2122
2123 // quota exceeded even after operation
2124 old_obstacle_file_size = obstacle_file_size;
2125 obstacle_file_size = from_file_size;
2126 from_file_size = 0;
2127 expected_total_file_size -= old_obstacle_file_size;
2128 scoped_ptr<FileSystemOperationContext> context =
2129 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2130 ASSERT_EQ(base::PLATFORM_FILE_OK,
2131 ofu()->CopyOrMoveFile(
2132 context.get(), from_file, obstacle_file, false /* move */));
2133 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2134 context.reset();
2135}
2136
2137TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092138 FileSystemURL dir(CreateURLFromUTF8("dir"));
2139 FileSystemURL file(CreateURLFromUTF8("file"));
2140 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2141 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132142 bool created;
2143
2144 ASSERT_EQ(base::PLATFORM_FILE_OK,
2145 ofu()->EnsureFileExists(
2146 AllowUsageIncrease(PathCost(file))->context(),
2147 file, &created));
2148 ASSERT_TRUE(created);
2149 ASSERT_EQ(0, ComputeTotalFileSize());
2150
2151 ASSERT_EQ(base::PLATFORM_FILE_OK,
2152 ofu()->CreateDirectory(
2153 AllowUsageIncrease(PathCost(dir))->context(),
2154 dir, false, false));
2155 ASSERT_EQ(0, ComputeTotalFileSize());
2156
2157 ASSERT_EQ(base::PLATFORM_FILE_OK,
2158 ofu()->EnsureFileExists(
2159 AllowUsageIncrease(PathCost(dfile1))->context(),
2160 dfile1, &created));
2161 ASSERT_TRUE(created);
2162 ASSERT_EQ(0, ComputeTotalFileSize());
2163
2164 ASSERT_EQ(base::PLATFORM_FILE_OK,
2165 ofu()->EnsureFileExists(
2166 AllowUsageIncrease(PathCost(dfile2))->context(),
2167 dfile2, &created));
2168 ASSERT_TRUE(created);
2169 ASSERT_EQ(0, ComputeTotalFileSize());
2170
2171 ASSERT_EQ(base::PLATFORM_FILE_OK,
2172 ofu()->Truncate(
2173 AllowUsageIncrease(340)->context(),
2174 file, 340));
2175 ASSERT_EQ(340, ComputeTotalFileSize());
2176
2177 ASSERT_EQ(base::PLATFORM_FILE_OK,
2178 ofu()->Truncate(
2179 AllowUsageIncrease(1020)->context(),
2180 dfile1, 1020));
2181 ASSERT_EQ(1360, ComputeTotalFileSize());
2182
2183 ASSERT_EQ(base::PLATFORM_FILE_OK,
2184 ofu()->Truncate(
2185 AllowUsageIncrease(120)->context(),
2186 dfile2, 120));
2187 ASSERT_EQ(1480, ComputeTotalFileSize());
2188
2189 ASSERT_EQ(base::PLATFORM_FILE_OK,
2190 ofu()->DeleteFile(
2191 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2192 file));
2193 ASSERT_EQ(1140, ComputeTotalFileSize());
2194
2195 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]22267a62013-02-07 13:12:592196 FileUtilHelper::Delete(
2197 AllowUsageIncrease(-PathCost(dir) -
2198 PathCost(dfile1) -
2199 PathCost(dfile2) -
2200 1020 - 120)->context(),
2201 ofu(), dir, true));
[email protected]294dd0312012-05-11 07:35:132202 ASSERT_EQ(0, ComputeTotalFileSize());
2203}
[email protected]7d78be12012-05-24 07:07:262204
2205TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092206 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262207 base::PlatformFile file_handle;
2208 bool created;
2209
2210 // Creating a file.
2211 ASSERT_EQ(base::PLATFORM_FILE_OK,
2212 ofu()->EnsureFileExists(
2213 AllowUsageIncrease(PathCost(file))->context(),
2214 file, &created));
2215 ASSERT_TRUE(created);
2216 ASSERT_EQ(0, ComputeTotalFileSize());
2217
2218 // Opening it, which shouldn't change the usage.
2219 ASSERT_EQ(base::PLATFORM_FILE_OK,
2220 ofu()->CreateOrOpen(
2221 AllowUsageIncrease(0)->context(), file,
2222 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2223 &file_handle, &created));
2224 ASSERT_EQ(0, ComputeTotalFileSize());
2225 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2226
2227 const int length = 33;
2228 ASSERT_EQ(base::PLATFORM_FILE_OK,
2229 ofu()->Truncate(
2230 AllowUsageIncrease(length)->context(), file, length));
2231 ASSERT_EQ(length, ComputeTotalFileSize());
2232
2233 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2234 ASSERT_EQ(base::PLATFORM_FILE_OK,
2235 ofu()->CreateOrOpen(
2236 AllowUsageIncrease(-length)->context(), file,
2237 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2238 &file_handle, &created));
2239 ASSERT_EQ(0, ComputeTotalFileSize());
2240 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2241
2242 // Extending the file again.
2243 ASSERT_EQ(base::PLATFORM_FILE_OK,
2244 ofu()->Truncate(
2245 AllowUsageIncrease(length)->context(), file, length));
2246 ASSERT_EQ(length, ComputeTotalFileSize());
2247
2248 // Opening it with TRUNCATED flag, which should truncate the file size.
2249 ASSERT_EQ(base::PLATFORM_FILE_OK,
2250 ofu()->CreateOrOpen(
2251 AllowUsageIncrease(-length)->context(), file,
2252 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2253 &file_handle, &created));
2254 ASSERT_EQ(0, ComputeTotalFileSize());
2255 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2256}
[email protected]c4e6f9c2012-09-09 17:42:102257
2258} // namespace fileapi