blob: 82c99585046228928bd90cbd360770c93f3cf51b [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"
17#include "webkit/fileapi/file_system_context.h"
18#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1019#include "webkit/fileapi/file_system_task_runners.h"
[email protected]0c5ebe32011-08-19 22:37:1620#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]aa437fa2012-03-03 02:09:0721#include "webkit/fileapi/file_util_helper.h"
[email protected]02a60542012-07-24 20:05:3322#include "webkit/fileapi/local_file_system_test_helper.h"
[email protected]c4e6f9c2012-09-09 17:42:1023#include "webkit/fileapi/mock_file_change_observer.h"
[email protected]e7e46732012-01-05 11:45:5524#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4925#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5626#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1627#include "webkit/quota/mock_special_storage_policy.h"
28#include "webkit/quota/quota_manager.h"
29#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3230
[email protected]c4e6f9c2012-09-09 17:42:1031namespace fileapi {
[email protected]d4905e2e2011-05-13 21:56:3232
33namespace {
34
[email protected]d4905e2e2011-05-13 21:56:3235bool FileExists(const FilePath& path) {
36 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
37}
38
[email protected]81b7f662011-05-26 00:54:4639int64 GetSize(const FilePath& path) {
40 int64 size;
41 EXPECT_TRUE(file_util::GetFileSize(path, &size));
42 return size;
43}
44
[email protected]d4905e2e2011-05-13 21:56:3245// After a move, the dest exists and the source doesn't.
46// After a copy, both source and dest exist.
47struct CopyMoveTestCaseRecord {
48 bool is_copy_not_move;
49 const char source_path[64];
50 const char dest_path[64];
51 bool cause_overwrite;
52};
53
54const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
55 // This is the combinatoric set of:
56 // rename vs. same-name
57 // different directory vs. same directory
58 // overwrite vs. no-overwrite
59 // copy vs. move
60 // We can never be called with source and destination paths identical, so
61 // those cases are omitted.
62 {true, "dir0/file0", "dir0/file1", false},
63 {false, "dir0/file0", "dir0/file1", false},
64 {true, "dir0/file0", "dir0/file1", true},
65 {false, "dir0/file0", "dir0/file1", true},
66
67 {true, "dir0/file0", "dir1/file0", false},
68 {false, "dir0/file0", "dir1/file0", false},
69 {true, "dir0/file0", "dir1/file0", true},
70 {false, "dir0/file0", "dir1/file0", true},
71 {true, "dir0/file0", "dir1/file1", false},
72 {false, "dir0/file0", "dir1/file1", false},
73 {true, "dir0/file0", "dir1/file1", true},
74 {false, "dir0/file0", "dir1/file1", true},
75};
76
[email protected]fcc2d5f2011-05-23 22:06:2677struct OriginEnumerationTestRecord {
78 std::string origin_url;
79 bool has_temporary;
80 bool has_persistent;
81};
82
83const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
84 {"http://example.com", false, true},
85 {"http://example1.com", true, false},
86 {"https://example1.com", true, true},
87 {"file://", false, true},
88 {"http://example.com:8000", false, true},
89};
90
[email protected]d4905e2e2011-05-13 21:56:3291} // namespace (anonymous)
92
93// TODO(ericu): The vast majority of this and the other FSFU subclass tests
94// could theoretically be shared. It would basically be a FSFU interface
95// compliance test, and only the subclass-specific bits that look into the
96// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:4997class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:3298 public:
[email protected]7878ece2011-09-05 11:41:4999 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26100 : origin_(GURL("http://www.example.com")),
101 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03102 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16103 test_helper_(origin_, type_),
104 quota_status_(quota::kQuotaStatusUnknown),
105 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32106 }
107
108 void SetUp() {
109 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
110
[email protected]e7e46732012-01-05 11:45:55111 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
112 new quota::MockSpecialStoragePolicy();
113
[email protected]0c5ebe32011-08-19 22:37:16114 quota_manager_ = new quota::QuotaManager(
115 false /* is_incognito */,
116 data_dir_.path(),
117 base::MessageLoopProxy::current(),
118 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55119 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16120
121 // Every time we create a new helper, it creates another context, which
122 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49123 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16124 file_system_context_ = new FileSystemContext(
[email protected]dc57ec82012-08-07 03:50:10125 FileSystemTaskRunners::CreateMockTaskRunners(),
[email protected]e7e46732012-01-05 11:45:55126 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16127 quota_manager_->proxy(),
128 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55129 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16130
[email protected]7878ece2011-09-05 11:41:49131 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
[email protected]e7e46732012-01-05 11:45:55132 file_system_context_->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16133
134 test_helper_.SetUp(file_system_context_.get(),
[email protected]3cfc10f2012-05-24 01:20:41135 obfuscated_file_util_);
[email protected]c4e6f9c2012-09-09 17:42:10136
137 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32138 }
139
[email protected]e7e46732012-01-05 11:45:55140 void TearDown() {
141 quota_manager_ = NULL;
142 test_helper_.TearDown();
143 }
144
[email protected]294dd0312012-05-11 07:35:13145 scoped_ptr<FileSystemOperationContext> LimitedContext(
146 int64 allowed_bytes_growth) {
147 scoped_ptr<FileSystemOperationContext> context(
148 test_helper_.NewOperationContext());
149 context->set_allowed_bytes_growth(allowed_bytes_growth);
150 return context.Pass();
151 }
152
153 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
154 return LimitedContext(kint64max);
155 }
156
[email protected]02a60542012-07-24 20:05:33157 FileSystemOperationContext* NewContext(
158 LocalFileSystemTestOriginHelper* helper) {
[email protected]c4e6f9c2012-09-09 17:42:10159 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16160 FileSystemOperationContext* context;
161 if (helper)
162 context = helper->NewOperationContext();
163 else
164 context = test_helper_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46165 // Setting allowed_bytes_growth big enough for all tests.
166 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10167 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32168 return context;
169 }
170
[email protected]c4e6f9c2012-09-09 17:42:10171 const ChangeObserverList& change_observers() const {
172 return change_observers_;
173 }
174
175 MockFileChangeObserver* change_observer() {
176 return &change_observer_;
177 }
178
[email protected]0c5ebe32011-08-19 22:37:16179 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49180 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16181 // Use this for tests which need to run in multiple origins; we need a test
182 // helper per origin.
[email protected]02a60542012-07-24 20:05:33183 LocalFileSystemTestOriginHelper* NewHelper(
[email protected]0c5ebe32011-08-19 22:37:16184 const GURL& origin, fileapi::FileSystemType type) {
[email protected]02a60542012-07-24 20:05:33185 LocalFileSystemTestOriginHelper* helper =
186 new LocalFileSystemTestOriginHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16187
188 helper->SetUp(file_system_context_.get(),
[email protected]3cfc10f2012-05-24 01:20:41189 obfuscated_file_util_);
[email protected]0c5ebe32011-08-19 22:37:16190 return helper;
191 }
192
[email protected]7878ece2011-09-05 11:41:49193 ObfuscatedFileUtil* ofu() {
[email protected]3cfc10f2012-05-24 01:20:41194 return obfuscated_file_util_;
[email protected]d4905e2e2011-05-13 21:56:32195 }
196
[email protected]6b931152011-05-20 21:02:35197 const FilePath& test_directory() const {
198 return data_dir_.path();
199 }
200
[email protected]0c5ebe32011-08-19 22:37:16201 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26202 return origin_;
203 }
204
205 fileapi::FileSystemType type() const {
206 return type_;
207 }
208
[email protected]294dd0312012-05-11 07:35:13209 int64 ComputeTotalFileSize() {
210 return test_helper_.ComputeCurrentOriginUsage() -
211 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
212 }
213
[email protected]0c5ebe32011-08-19 22:37:16214 void GetUsageFromQuotaManager() {
215 quota_manager_->GetUsageAndQuota(
216 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03217 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
218 weak_factory_.GetWeakPtr()));
[email protected]4e1c7e52012-12-16 01:06:36219 MessageLoop::current()->RunUntilIdle();
[email protected]0c5ebe32011-08-19 22:37:16220 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
221 }
222
223 void RevokeUsageCache() {
224 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]022d2702012-05-14 16:04:26225 file_util::Delete(test_helper_.GetUsageCachePath(), false);
226 }
227
228 int64 SizeByQuotaUtil() {
229 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16230 }
231
232 int64 SizeInUsageFile() {
[email protected]8eee1b62013-01-08 02:26:44233 MessageLoop::current()->RunUntilIdle();
[email protected]022d2702012-05-14 16:04:26234 return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath());
[email protected]0c5ebe32011-08-19 22:37:16235 }
236
[email protected]13f92f6e2012-08-13 07:39:14237 bool PathExists(const FileSystemURL& url) {
238 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
239 return FileUtilHelper::PathExists(context.get(), ofu(), url);
240 }
241
242 bool DirectoryExists(const FileSystemURL& url) {
243 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
244 return FileUtilHelper::DirectoryExists(context.get(), ofu(), url);
245 }
246
[email protected]0c5ebe32011-08-19 22:37:16247 int64 usage() const { return usage_; }
248
[email protected]949f25a2012-06-27 01:53:09249 FileSystemURL CreateURLFromUTF8(const std::string& path) {
250 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50251 }
252
[email protected]949f25a2012-06-27 01:53:09253 int64 PathCost(const FileSystemURL& url) {
254 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13255 }
256
[email protected]949f25a2012-06-27 01:53:09257 FileSystemURL CreateURL(const FilePath& path) {
258 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50259 }
260
[email protected]0c5ebe32011-08-19 22:37:16261 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
262 EXPECT_EQ(quota::kQuotaStatusOk, status);
263 quota_status_ = status;
264 usage_ = usage;
265 }
266
[email protected]d4905e2e2011-05-13 21:56:32267 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39268 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16269 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32270 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49271 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09272 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32273
274 base::PlatformFileInfo file_info0;
275 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49276 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09277 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32278 EXPECT_EQ(data_path, local_path);
279 EXPECT_TRUE(FileExists(data_path));
280 EXPECT_EQ(0, GetSize(data_path));
281
282 const char data[] = "test data";
283 const int length = arraysize(data) - 1;
284
285 if (base::kInvalidPlatformFileValue == file_handle) {
286 bool created = true;
[email protected]403ada82013-01-08 07:51:39287 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32288 file_handle = base::CreatePlatformFile(
289 data_path,
290 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
291 &created,
292 &error);
[email protected]81b7f662011-05-26 00:54:46293 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32294 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
295 EXPECT_FALSE(created);
296 }
297 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
298 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
299
300 base::PlatformFileInfo file_info1;
301 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16302 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49303 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09304 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32305 EXPECT_EQ(data_path, local_path);
306
307 EXPECT_FALSE(file_info0.is_directory);
308 EXPECT_FALSE(file_info1.is_directory);
309 EXPECT_FALSE(file_info0.is_symbolic_link);
310 EXPECT_FALSE(file_info1.is_symbolic_link);
311 EXPECT_EQ(0, file_info0.size);
312 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32313 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32314
[email protected]0c5ebe32011-08-19 22:37:16315 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49316 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09317 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32318 EXPECT_EQ(length * 2, GetSize(data_path));
319
[email protected]0c5ebe32011-08-19 22:37:16320 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49321 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09322 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16323 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32324 }
325
326 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09327 const FileSystemURL& root_url,
[email protected]89ee4272011-05-16 18:45:17328 const std::set<FilePath::StringType>& files,
329 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32330 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17331 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32332 for (iter = files.begin(); iter != files.end(); ++iter) {
333 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16334 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32335 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49336 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09337 context.get(), root_url.WithPath(root_url.path().Append(*iter)),
[email protected]d4905e2e2011-05-13 21:56:32338 &created));
339 ASSERT_FALSE(created);
340 }
341 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16342 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14343 EXPECT_TRUE(DirectoryExists(
[email protected]949f25a2012-06-27 01:53:09344 root_url.WithPath(root_url.path().Append(*iter))));
[email protected]d4905e2e2011-05-13 21:56:32345 }
346 }
347
[email protected]294dd0312012-05-11 07:35:13348 class UsageVerifyHelper {
349 public:
350 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33351 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13352 int64 expected_usage)
353 : context_(context.Pass()),
354 test_helper_(test_helper),
355 expected_usage_(expected_usage) {}
356
357 ~UsageVerifyHelper() {
[email protected]8eee1b62013-01-08 02:26:44358 MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13359 Check();
360 }
361
362 FileSystemOperationContext* context() {
363 return context_.get();
364 }
365
366 private:
367 void Check() {
368 ASSERT_EQ(expected_usage_,
369 test_helper_->GetCachedOriginUsage());
370 }
371
372 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33373 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13374 int64 expected_usage_;
375 };
376
377 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
378 int64 usage = test_helper_.GetCachedOriginUsage();
379 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
380 LimitedContext(requested_growth),
381 &test_helper_, usage + requested_growth));
382 }
383
384 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
385 int64 usage = test_helper_.GetCachedOriginUsage();
386 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
387 LimitedContext(requested_growth - 1), &test_helper_, usage));
388 }
389
[email protected]d4905e2e2011-05-13 21:56:32390 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09391 const FileSystemURL& root_url,
[email protected]89ee4272011-05-16 18:45:17392 std::set<FilePath::StringType>* files,
393 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32394 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16395 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32396 std::vector<base::FileUtilProxy::Entry> entries;
397 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55398 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:09399 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32400 EXPECT_EQ(0UL, entries.size());
401
402 files->clear();
[email protected]89ee4272011-05-16 18:45:17403 files->insert(FILE_PATH_LITERAL("first"));
404 files->insert(FILE_PATH_LITERAL("second"));
405 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32406 directories->clear();
[email protected]89ee4272011-05-16 18:45:17407 directories->insert(FILE_PATH_LITERAL("fourth"));
408 directories->insert(FILE_PATH_LITERAL("fifth"));
409 directories->insert(FILE_PATH_LITERAL("sixth"));
410 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32411 for (iter = files->begin(); iter != files->end(); ++iter) {
412 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16413 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32414 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49415 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50416 context.get(),
[email protected]949f25a2012-06-27 01:53:09417 root_url.WithPath(root_url.path().Append(*iter)),
[email protected]08f8feb2012-02-26 11:53:50418 &created));
[email protected]d4905e2e2011-05-13 21:56:32419 ASSERT_TRUE(created);
420 }
421 for (iter = directories->begin(); iter != directories->end(); ++iter) {
422 bool exclusive = true;
423 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16424 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32425 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49426 ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09427 context.get(), root_url.WithPath(root_url.path().Append(*iter)),
428 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32429 }
[email protected]949f25a2012-06-27 01:53:09430 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32431 }
432
[email protected]949f25a2012-06-27 01:53:09433 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]89ee4272011-05-16 18:45:17434 std::set<FilePath::StringType> files;
435 std::set<FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09436 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32437
438 scoped_ptr<FileSystemOperationContext> context;
439 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16440 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32441 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55442 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:09443 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32444 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
445 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10446 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32447 for (entry_iter = entries.begin(); entry_iter != entries.end();
448 ++entry_iter) {
449 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17450 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32451 if (iter != files.end()) {
452 EXPECT_FALSE(entry.is_directory);
453 files.erase(iter);
454 continue;
455 }
[email protected]89ee4272011-05-16 18:45:17456 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32457 EXPECT_FALSE(directories.end() == iter);
458 EXPECT_TRUE(entry.is_directory);
459 directories.erase(iter);
460 }
461 }
462
[email protected]949f25a2012-06-27 01:53:09463 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41464 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32465 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16466
[email protected]2517cfa2011-08-25 05:12:41467 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32468 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49469 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09470 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10471 // Currently we fire no change notifications for Touch.
472 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32473 FilePath local_path;
474 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16475 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49476 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09477 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32478 // We compare as time_t here to lower our resolution, to avoid false
479 // negatives caused by conversion to the local filesystem's native
480 // representation and back.
481 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
482
[email protected]0c5ebe32011-08-19 22:37:16483 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32484 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41485 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32486 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49487 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09488 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10489 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16490 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49491 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09492 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32493 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49494 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41495 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32496 }
497
[email protected]81b7f662011-05-26 00:54:46498 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23499 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46500 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]08f8feb2012-02-26 11:53:50501 FilePath root_file_path = source_dir.path();
502 FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09503 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46504 int64 src_file_length = 87;
505
506 base::PlatformFileError error_code;
507 bool created = false;
508 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
509 base::PlatformFile file_handle =
510 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50511 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46512 EXPECT_TRUE(created);
513 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
514 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
515 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
516 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
517
518 scoped_ptr<FileSystemOperationContext> context;
519
520 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16521 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46522 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09523 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46524 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10525
526 // We must have observed one (and only one) create_file_count.
527 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
528 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46529 }
530
[email protected]0c5ebe32011-08-19 22:37:16531 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09532 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16533 if (!overwrite) {
534 // Verify that file creation requires sufficient quota for the path.
535 context.reset(NewContext(NULL));
536 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
537 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13538 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09539 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16540 }
541
542 context.reset(NewContext(NULL));
543 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46544 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13545 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09546 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16547
[email protected]13f92f6e2012-08-13 07:39:14548 EXPECT_TRUE(PathExists(dest_url));
549 EXPECT_FALSE(DirectoryExists(dest_url));
550
[email protected]0c5ebe32011-08-19 22:37:16551 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46552 base::PlatformFileInfo file_info;
553 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49554 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09555 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50556 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46557 EXPECT_TRUE(FileExists(data_path));
558 EXPECT_EQ(src_file_length, GetSize(data_path));
559
560 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09561 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46562 }
563
[email protected]949f25a2012-06-27 01:53:09564 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03565 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
566 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09567 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
568 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03569 }
570
[email protected]949f25a2012-06-27 01:53:09571 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03572 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
573 FilePath data_path;
574 base::PlatformFileInfo file_info;
575 context.reset(NewContext(NULL));
576 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09577 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10578 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03579 return file_info.last_modified;
580 }
581
[email protected]949f25a2012-06-27 01:53:09582 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03583 bool copy,
584 bool overwrite) {
585 scoped_ptr<FileSystemOperationContext> context;
[email protected]949f25a2012-06-27 01:53:09586 const FileSystemURL src_dir_url(base_dir.WithPath(
587 base_dir.path().AppendASCII("foo_dir")));
588 const FileSystemURL dest_dir_url(base_dir.WithPath(
589 base_dir.path().AppendASCII("bar_dir")));
[email protected]fad625e2f2011-12-08 05:38:03590
[email protected]949f25a2012-06-27 01:53:09591 const FileSystemURL src_file_url(src_dir_url.WithPath(
592 src_dir_url.path().AppendASCII("hoge")));
593 const FileSystemURL dest_file_url(dest_dir_url.WithPath(
594 dest_dir_url.path().AppendASCII("fuga")));
[email protected]fad625e2f2011-12-08 05:38:03595
596 context.reset(NewContext(NULL));
597 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09598 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03599 context.reset(NewContext(NULL));
600 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09601 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03602
603 bool created = false;
604 context.reset(NewContext(NULL));
605 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09606 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03607 if (overwrite) {
608 context.reset(NewContext(NULL));
609 EXPECT_EQ(base::PLATFORM_FILE_OK,
610 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09611 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03612 }
613
[email protected]949f25a2012-06-27 01:53:09614 ClearTimestamp(src_dir_url);
615 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03616 context.reset(NewContext(NULL));
617 EXPECT_EQ(base::PLATFORM_FILE_OK,
618 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09619 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03620 copy));
[email protected]fad625e2f2011-12-08 05:38:03621 if (copy)
[email protected]949f25a2012-06-27 01:53:09622 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03623 else
[email protected]949f25a2012-06-27 01:53:09624 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
625 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03626 }
627
[email protected]ecdfd6c52012-04-11 13:35:44628 int64 ComputeCurrentUsage() {
629 return test_helper().ComputeCurrentOriginUsage() -
630 test_helper().ComputeCurrentDirectoryDatabaseUsage();
631 }
632
[email protected]02a60542012-07-24 20:05:33633 const LocalFileSystemTestOriginHelper& test_helper() const {
634 return test_helper_;
635 }
[email protected]45ea0fbbb2012-02-27 22:28:49636
[email protected]d4905e2e2011-05-13 21:56:32637 private:
[email protected]ea1a3f62012-11-16 20:34:23638 base::ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01639 MessageLoop message_loop_;
[email protected]3cfc10f2012-05-24 01:20:41640 ObfuscatedFileUtil* obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16641 scoped_refptr<quota::QuotaManager> quota_manager_;
642 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26643 GURL origin_;
644 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03645 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]02a60542012-07-24 20:05:33646 LocalFileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16647 quota::QuotaStatusCode quota_status_;
648 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10649 MockFileChangeObserver change_observer_;
650 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32651
[email protected]7878ece2011-09-05 11:41:49652 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32653};
654
[email protected]7878ece2011-09-05 11:41:49655TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32656 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
657 bool created;
[email protected]949f25a2012-06-27 01:53:09658 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16659 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32660 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
661
662 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49663 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09664 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32665 &created));
666
[email protected]0c5ebe32011-08-19 22:37:16667 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32668 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09669 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32670
[email protected]949f25a2012-06-27 01:53:09671 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32672
[email protected]c4e6f9c2012-09-09 17:42:10673 EXPECT_TRUE(change_observer()->HasNoChange());
674
[email protected]0c5ebe32011-08-19 22:37:16675 // Verify that file creation requires sufficient quota for the path.
676 context.reset(NewContext(NULL));
677 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09678 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16679 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49680 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09681 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16682
683 context.reset(NewContext(NULL));
684 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09685 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32686 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49687 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09688 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32689 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10690 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32691 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
692
[email protected]949f25a2012-06-27 01:53:09693 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32694
[email protected]0c5ebe32011-08-19 22:37:16695 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32696 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49697 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09698 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32699 EXPECT_TRUE(file_util::PathExists(local_path));
700
[email protected]0c5ebe32011-08-19 22:37:16701 // Verify that deleting a file isn't stopped by zero quota, and that it frees
702 // up quote from its path.
703 context.reset(NewContext(NULL));
704 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32705 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09706 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10707 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32708 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09709 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16710 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32711
[email protected]0c5ebe32011-08-19 22:37:16712 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32713 bool exclusive = true;
714 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09715 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50716 "series/of/directories");
[email protected]949f25a2012-06-27 01:53:09717 url = directory_url.WithPath(directory_url.path().AppendASCII("file name"));
[email protected]7878ece2011-09-05 11:41:49718 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09719 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10720 // The oepration created 3 directories recursively.
721 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32722
[email protected]0c5ebe32011-08-19 22:37:16723 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32724 file_handle = base::kInvalidPlatformFileValue;
725 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49726 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09727 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32728 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10729 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32730 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
731
[email protected]949f25a2012-06-27 01:53:09732 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32733
[email protected]0c5ebe32011-08-19 22:37:16734 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49735 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09736 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32737 EXPECT_TRUE(file_util::PathExists(local_path));
738
[email protected]0c5ebe32011-08-19 22:37:16739 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32740 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09741 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10742 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32743 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10744
745 // Make sure we have no unexpected changes.
746 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32747}
748
[email protected]7878ece2011-09-05 11:41:49749TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32750 bool created = false;
[email protected]949f25a2012-06-27 01:53:09751 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16752 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32753
754 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09755 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32756
[email protected]0c5ebe32011-08-19 22:37:16757 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32758 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09759 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32760 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10761 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32762
[email protected]0c5ebe32011-08-19 22:37:16763 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32764 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49765 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09766 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32767 EXPECT_EQ(0, GetSize(local_path));
768
[email protected]0c5ebe32011-08-19 22:37:16769 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49770 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09771 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10772 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32773 EXPECT_EQ(10, GetSize(local_path));
774
[email protected]0c5ebe32011-08-19 22:37:16775 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49776 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09777 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32778 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10779 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32780
[email protected]13f92f6e2012-08-13 07:39:14781 EXPECT_FALSE(DirectoryExists(url));
782 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10783
784 // Make sure we have no unexpected changes.
785 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32786}
787
[email protected]ecdfd6c52012-04-11 13:35:44788TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
789 bool created = false;
[email protected]949f25a2012-06-27 01:53:09790 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44791
792 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13793 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09794 AllowUsageIncrease(PathCost(url))->context(),
795 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44796 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13797 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44798
[email protected]ecdfd6c52012-04-11 13:35:44799 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13800 ofu()->Truncate(
801 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09802 url, 1020));
[email protected]294dd0312012-05-11 07:35:13803 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44804
[email protected]ecdfd6c52012-04-11 13:35:44805 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13806 ofu()->Truncate(
807 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09808 url, 0));
[email protected]294dd0312012-05-11 07:35:13809 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44810
[email protected]ecdfd6c52012-04-11 13:35:44811 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13812 ofu()->Truncate(
813 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09814 url, 1021));
[email protected]294dd0312012-05-11 07:35:13815 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44816
[email protected]ecdfd6c52012-04-11 13:35:44817 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13818 ofu()->Truncate(
819 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09820 url, 1020));
[email protected]294dd0312012-05-11 07:35:13821 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44822
[email protected]ecdfd6c52012-04-11 13:35:44823 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13824 ofu()->Truncate(
825 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09826 url, 1020));
[email protected]294dd0312012-05-11 07:35:13827 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44828
[email protected]294dd0312012-05-11 07:35:13829 // quota exceeded
830 {
831 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
832 helper->context()->set_allowed_bytes_growth(
833 helper->context()->allowed_bytes_growth() - 1);
834 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09835 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13836 ASSERT_EQ(1019, ComputeTotalFileSize());
837 }
[email protected]ecdfd6c52012-04-11 13:35:44838
839 // Delete backing file to make following truncation fail.
[email protected]ecdfd6c52012-04-11 13:35:44840 FilePath local_path;
841 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13842 ofu()->GetLocalFilePath(
843 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09844 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44845 ASSERT_FALSE(local_path.empty());
846 ASSERT_TRUE(file_util::Delete(local_path, false));
847
[email protected]ecdfd6c52012-04-11 13:35:44848 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13849 ofu()->Truncate(
850 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09851 url, 1234));
[email protected]294dd0312012-05-11 07:35:13852 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44853}
854
[email protected]7878ece2011-09-05 11:41:49855TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09856 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32857 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16858 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32859 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49860 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09861 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10862 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32863
[email protected]0c5ebe32011-08-19 22:37:16864 // Verify that file creation requires sufficient quota for the path.
865 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09866 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32867 created = false;
[email protected]0c5ebe32011-08-19 22:37:16868 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09869 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16870 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09871 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16872 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10873 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16874
875 context.reset(NewContext(NULL));
876 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09877 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32878 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09879 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32880 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10881 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32882
[email protected]949f25a2012-06-27 01:53:09883 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32884
[email protected]0c5ebe32011-08-19 22:37:16885 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32886 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09887 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32888 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10889 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32890
891 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09892 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16893 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32894 bool exclusive = true;
895 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49896 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09897 context.get(),
898 url.WithPath(url.path().DirName()),
899 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10900 // 2 directories: path/ and path/to.
901 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32902
[email protected]0c5ebe32011-08-19 22:37:16903 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32904 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09905 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32906 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14907 EXPECT_FALSE(DirectoryExists(url));
908 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10909 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32910}
911
[email protected]7878ece2011-09-05 11:41:49912TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16913 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32914
915 bool exclusive = false;
916 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09917 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49918 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09919 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32920
[email protected]0c5ebe32011-08-19 22:37:16921 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32922 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09923 ofu()->DeleteSingleDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32924
[email protected]949f25a2012-06-27 01:53:09925 FileSystemURL root = CreateURLFromUTF8("");
[email protected]13f92f6e2012-08-13 07:39:14926 EXPECT_FALSE(DirectoryExists(url));
927 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16928 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49929 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32930
[email protected]0c5ebe32011-08-19 22:37:16931 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32932 exclusive = false;
933 recursive = true;
[email protected]7878ece2011-09-05 11:41:49934 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09935 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10936 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32937
[email protected]13f92f6e2012-08-13 07:39:14938 EXPECT_TRUE(DirectoryExists(url));
939 EXPECT_TRUE(PathExists(url));
940
[email protected]0c5ebe32011-08-19 22:37:16941 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49942 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]13f92f6e2012-08-13 07:39:14943 EXPECT_TRUE(DirectoryExists(url.WithPath(url.path().DirName())));
944
[email protected]0c5ebe32011-08-19 22:37:16945 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09946 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
947 url.WithPath(url.path().DirName())));
[email protected]d4905e2e2011-05-13 21:56:32948
949 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16950 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06951 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]949f25a2012-06-27 01:53:09952 ofu()->DeleteSingleDirectory(context.get(),
953 url.WithPath(url.path().DirName())));
[email protected]c4e6f9c2012-09-09 17:42:10954 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32955
956 base::PlatformFileInfo file_info;
957 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49958 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09959 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32960 EXPECT_TRUE(local_path.empty());
961 EXPECT_TRUE(file_info.is_directory);
962 EXPECT_FALSE(file_info.is_symbolic_link);
963
964 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16965 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49966 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09967 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10968 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32969
970 exclusive = true;
971 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16972 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49973 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09974 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10975 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32976
[email protected]0c5ebe32011-08-19 22:37:16977 // Verify that deleting a directory isn't stopped by zero quota, and that it
978 // frees up quota from its path.
979 context.reset(NewContext(NULL));
980 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32981 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09982 ofu()->DeleteSingleDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10983 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:09984 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16985 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32986
[email protected]949f25a2012-06-27 01:53:09987 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:32988
[email protected]13f92f6e2012-08-13 07:39:14989 EXPECT_FALSE(DirectoryExists(url));
990 EXPECT_FALSE(PathExists(url));
991
[email protected]0c5ebe32011-08-19 22:37:16992 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09993 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:49994 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09995 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32996
[email protected]0c5ebe32011-08-19 22:37:16997 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32998 exclusive = true;
999 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161000 context.reset(NewContext(NULL));
1001 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091002 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491003 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091004 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101005 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161006
1007 context.reset(NewContext(NULL));
1008 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091009 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491010 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091011 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101012 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321013
[email protected]13f92f6e2012-08-13 07:39:141014 EXPECT_TRUE(DirectoryExists(url));
1015 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321016
1017 exclusive = true;
1018 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141019 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491020 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091021 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101022 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321023
1024 exclusive = true;
1025 recursive = false;
[email protected]949f25a2012-06-27 01:53:091026 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141027 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491028 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091029 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101030 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321031
[email protected]949f25a2012-06-27 01:53:091032 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321033
[email protected]13f92f6e2012-08-13 07:39:141034 EXPECT_FALSE(DirectoryExists(url));
1035 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321036
1037 exclusive = true;
1038 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141039 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491040 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091041 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101042 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321043
[email protected]13f92f6e2012-08-13 07:39:141044 EXPECT_TRUE(DirectoryExists(url));
1045 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321046
1047 exclusive = true;
1048 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141049 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491050 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091051 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101052 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321053}
1054
[email protected]7878ece2011-09-05 11:41:491055TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161056 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321057 bool exclusive = true;
1058 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091059 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491060 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091061 context.get(), url, exclusive, recursive));
1062 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321063}
1064
[email protected]7878ece2011-09-05 11:41:491065TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]949f25a2012-06-27 01:53:091066 TestReadDirectoryHelper(CreateURLFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:321067}
1068
[email protected]7878ece2011-09-05 11:41:491069TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091070 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321071}
1072
[email protected]7878ece2011-09-05 11:41:491073TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091074 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161075 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321076
1077 bool created = false;
1078 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091079 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321080 ASSERT_TRUE(created);
1081
[email protected]0c5ebe32011-08-19 22:37:161082 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321083 std::vector<base::FileUtilProxy::Entry> entries;
1084 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]a3938912012-03-27 14:00:551085 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:091086 context.get(), ofu(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321087
[email protected]949f25a2012-06-27 01:53:091088 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321089}
1090
[email protected]7878ece2011-09-05 11:41:491091TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091092 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161093 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411094
1095 base::Time last_access_time = base::Time::Now();
1096 base::Time last_modified_time = base::Time::Now();
1097
1098 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321099 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491100 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091101 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321102
[email protected]2517cfa2011-08-25 05:12:411103 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161104 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411105 bool created = false;
1106 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091107 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411108 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091109 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411110
1111 // Now test a directory:
1112 context.reset(NewContext(NULL));
1113 bool exclusive = true;
1114 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091115 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491116 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091117 url, exclusive, recursive));
1118 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161119}
1120
[email protected]7878ece2011-09-05 11:41:491121TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091122 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161123 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1124
[email protected]949f25a2012-06-27 01:53:091125 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161126 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411127 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161128 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091129 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411130 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161131 context->set_allowed_bytes_growth(1024);
1132 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091133 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411134 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091135 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161136 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1137
1138 context->set_allowed_bytes_growth(1024);
1139 bool exclusive = true;
1140 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091141 url = CreateURLFromUTF8("directory/to/use");
[email protected]0c5ebe32011-08-19 22:37:161142 std::vector<FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091143 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161144 path_cost = 0;
1145 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
1146 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491147 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:161148 FilePath(*iter));
1149 }
1150 context.reset(NewContext(NULL));
1151 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491152 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091153 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161154 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321155}
1156
[email protected]7878ece2011-09-05 11:41:491157TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091158 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1159 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161160 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321161
1162 bool is_copy_not_move = false;
1163 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091164 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321165 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101166 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161167 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321168 is_copy_not_move = true;
1169 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091170 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321171 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101172 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091173 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321174 bool exclusive = true;
1175 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161176 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491177 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091178 context.get(),
1179 source_url.WithPath(source_url.path().DirName()),
1180 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101181 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321182 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]d4905e2e2011-05-13 21:56:321193}
1194
[email protected]7878ece2011-09-05 11:41:491195TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321196 const int64 kSourceLength = 5;
1197 const int64 kDestLength = 50;
1198
1199 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1200 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1201 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1202 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1203 test_case.is_copy_not_move);
1204 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1205 test_case.source_path);
1206 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1207 test_case.dest_path);
1208 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1209 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161210 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321211
1212 bool exclusive = false;
1213 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091214 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1215 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321216
[email protected]0c5ebe32011-08-19 22:37:161217 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491218 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091219 context.get(),
1220 source_url.WithPath(source_url.path().DirName()),
1221 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161222 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491223 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091224 context.get(),
1225 dest_url.WithPath(dest_url.path().DirName()),
1226 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321227
[email protected]d4905e2e2011-05-13 21:56:321228 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161229 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321230 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091231 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321232 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161233 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321234 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091235 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321236
1237 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161238 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321239 created = false;
1240 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091241 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321242 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161243 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321244 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091245 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321246 }
1247
[email protected]0c5ebe32011-08-19 22:37:161248 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491249 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091250 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101251
[email protected]d4905e2e2011-05-13 21:56:321252 if (test_case.is_copy_not_move) {
1253 base::PlatformFileInfo file_info;
1254 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161255 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491256 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091257 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321258 EXPECT_EQ(kSourceLength, file_info.size);
1259 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091260 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321261 } else {
1262 base::PlatformFileInfo file_info;
1263 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161264 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491265 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091266 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321267 }
1268 base::PlatformFileInfo file_info;
1269 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491270 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091271 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321272 EXPECT_EQ(kSourceLength, file_info.size);
1273
1274 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091275 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321276 }
1277}
1278
[email protected]7878ece2011-09-05 11:41:491279TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091280 FileSystemURL src_url = CreateURLFromUTF8("src path");
1281 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161282 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1283 bool created = false;
[email protected]7878ece2011-09-05 11:41:491284 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091285 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161286
1287 bool is_copy = true;
1288 // Copy, no overwrite.
1289 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091290 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161291 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091292 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161293 context.reset(NewContext(NULL));
1294 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091295 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161296 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091297 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161298
1299 // Copy, with overwrite.
1300 context.reset(NewContext(NULL));
1301 context->set_allowed_bytes_growth(0);
1302 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091303 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161304}
1305
[email protected]7878ece2011-09-05 11:41:491306TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091307 FileSystemURL src_url = CreateURLFromUTF8("src path");
1308 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161309 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1310 bool created = false;
[email protected]7878ece2011-09-05 11:41:491311 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091312 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161313
1314 bool is_copy = false;
1315 // Move, rename, no overwrite.
1316 context.reset(NewContext(NULL));
1317 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091318 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1319 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161320 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091321 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161322 context.reset(NewContext(NULL));
1323 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091324 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1325 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161326 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091327 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161328
1329 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491330 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091331 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161332
1333 // Move, rename, with overwrite.
1334 context.reset(NewContext(NULL));
1335 context->set_allowed_bytes_growth(0);
1336 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091337 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161338}
1339
[email protected]7878ece2011-09-05 11:41:491340TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091341 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161342 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1343 bool created = false;
[email protected]7878ece2011-09-05 11:41:491344 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091345 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161346
1347 bool exclusive = true;
1348 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091349 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161350 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491351 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091352 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161353
[email protected]949f25a2012-06-27 01:53:091354 FileSystemURL dest_url = dir_url.WithPath(
1355 dir_url.path().Append(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161356
1357 bool is_copy = false;
1358 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1359 // Move, no rename, no overwrite.
1360 context.reset(NewContext(NULL));
1361 context->set_allowed_bytes_growth(allowed_bytes_growth);
1362 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091363 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161364 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1365
1366 // Move, no rename, with overwrite.
1367 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491368 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091369 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161370 context.reset(NewContext(NULL));
1371 context->set_allowed_bytes_growth(allowed_bytes_growth);
1372 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091373 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161374 EXPECT_EQ(
1375 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091376 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161377 context->allowed_bytes_growth());
1378}
1379
[email protected]7878ece2011-09-05 11:41:491380TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461381 TestCopyInForeignFileHelper(false /* overwrite */);
1382 TestCopyInForeignFileHelper(true /* overwrite */);
1383}
1384
[email protected]7878ece2011-09-05 11:41:491385TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161386 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091387 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321388 bool exclusive = true;
1389 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491390 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091391 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321392
[email protected]89ee4272011-05-16 18:45:171393 std::set<FilePath::StringType> files;
1394 std::set<FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091395 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321396
[email protected]949f25a2012-06-27 01:53:091397 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321398
[email protected]13f92f6e2012-08-13 07:39:141399 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]0c5ebe32011-08-19 22:37:161400 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321401 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091402 test_helper().SameFileUtilCopy(context.get(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321403
[email protected]949f25a2012-06-27 01:53:091404 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141405 EXPECT_TRUE(DirectoryExists(src_url));
1406 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]0c5ebe32011-08-19 22:37:161407 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321408 recursive = true;
1409 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]aa437fa2012-03-03 02:09:071410 FileUtilHelper::Delete(context.get(), ofu(),
[email protected]949f25a2012-06-27 01:53:091411 dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141412 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321413}
[email protected]6b931152011-05-20 21:02:351414
[email protected]7878ece2011-09-05 11:41:491415TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1416 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1417 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161418 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261419 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161420 EXPECT_EQ(origin(), enumerator->Next());
1421 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1422 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1423 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261424 EXPECT_EQ(GURL(), enumerator->Next());
1425 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1426 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1427
1428 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161429 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261430
1431 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1432 SCOPED_TRACE(testing::Message() <<
1433 "Validating kOriginEnumerationTestRecords " << i);
1434 const OriginEnumerationTestRecord& record =
1435 kOriginEnumerationTestRecords[i];
1436 GURL origin_url(record.origin_url);
1437 origins_expected.insert(origin_url);
1438 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331439 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161440 NewHelper(origin_url, kFileSystemTypeTemporary));
1441 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261442 bool created = false;
1443 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501444 ofu()->EnsureFileExists(
1445 context.get(),
[email protected]949f25a2012-06-27 01:53:091446 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501447 &created));
[email protected]fcc2d5f2011-05-23 22:06:261448 EXPECT_TRUE(created);
1449 }
1450 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331451 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161452 NewHelper(origin_url, kFileSystemTypePersistent));
1453 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261454 bool created = false;
1455 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501456 ofu()->EnsureFileExists(
1457 context.get(),
[email protected]949f25a2012-06-27 01:53:091458 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501459 &created));
[email protected]fcc2d5f2011-05-23 22:06:261460 EXPECT_TRUE(created);
1461 }
1462 }
[email protected]7878ece2011-09-05 11:41:491463 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261464 EXPECT_TRUE(enumerator.get());
1465 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161466 GURL origin_url;
1467 while (!(origin_url = enumerator->Next()).is_empty()) {
1468 origins_found.insert(origin_url);
1469 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261470 bool found = false;
1471 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1472 ++i) {
1473 const OriginEnumerationTestRecord& record =
1474 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161475 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261476 continue;
1477 found = true;
1478 EXPECT_EQ(record.has_temporary,
1479 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1480 EXPECT_EQ(record.has_persistent,
1481 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1482 }
[email protected]0c5ebe32011-08-19 22:37:161483 // Deal with the default filesystem created by the test helper.
1484 if (!found && origin_url == origin()) {
1485 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1486 EXPECT_EQ(true,
1487 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181488 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161489 found = true;
1490 }
[email protected]fcc2d5f2011-05-23 22:06:261491 EXPECT_TRUE(found);
1492 }
1493
1494 std::set<GURL> diff;
1495 std::set_symmetric_difference(origins_expected.begin(),
1496 origins_expected.end(), origins_found.begin(), origins_found.end(),
1497 inserter(diff, diff.begin()));
1498 EXPECT_TRUE(diff.empty());
1499}
[email protected]0c5ebe32011-08-19 22:37:161500
[email protected]7878ece2011-09-05 11:41:491501TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161502 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1503
1504 int64 expected_quota = 0;
1505
[email protected]f83b5b72012-01-27 10:26:561506 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011507 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561508 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]08f8feb2012-02-26 11:53:501509 FilePath file_path(test_case.path);
1510 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161511 if (test_case.is_directory) {
1512 bool exclusive = true;
1513 bool recursive = false;
1514 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091515 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501516 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161517 } else {
1518 bool created = false;
1519 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091520 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501521 &created));
[email protected]0c5ebe32011-08-19 22:37:161522 ASSERT_TRUE(created);
1523 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501524 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091525 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501526 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161527 expected_quota += test_case.data_file_size;
1528 }
1529 }
[email protected]022d2702012-05-14 16:04:261530
1531 // Usually raw size in usage cache and the usage returned by QuotaUtil
1532 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161533 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261534 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1535
[email protected]0c5ebe32011-08-19 22:37:161536 RevokeUsageCache();
1537 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261538 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1539
1540 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161541 GetUsageFromQuotaManager();
1542 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261543 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161544 EXPECT_EQ(expected_quota, usage());
1545}
[email protected]34583332011-08-31 08:59:471546
[email protected]7878ece2011-09-05 11:41:491547TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091548 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1549 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471550
1551 scoped_ptr<FileSystemOperationContext> context;
1552 base::PlatformFile file;
1553 base::PlatformFileInfo file_info;
1554 FilePath data_path;
1555 bool created = false;
1556
1557 // Create a non-empty file.
1558 context.reset(NewContext(NULL));
1559 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491560 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471561 EXPECT_TRUE(created);
1562 context.reset(NewContext(NULL));
1563 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491564 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471565 context.reset(NewContext(NULL));
1566 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491567 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471568 context.get(), kPath1, &file_info, &data_path));
1569 EXPECT_EQ(10, file_info.size);
1570
1571 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491572 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471573
1574 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141575 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471576 context.reset(NewContext(NULL));
1577 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491578 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471579 EXPECT_TRUE(created);
1580 context.reset(NewContext(NULL));
1581 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491582 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471583 context.get(), kPath1, &file_info, &data_path));
1584 EXPECT_EQ(0, file_info.size);
1585
1586 // Make another broken file to |kPath2|.
1587 context.reset(NewContext(NULL));
1588 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491589 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471590 EXPECT_TRUE(created);
1591
1592 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491593 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471594
1595 // Repair broken |kPath1|.
1596 context.reset(NewContext(NULL));
1597 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491598 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471599 base::Time::Now()));
1600 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491601 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471602 EXPECT_TRUE(created);
1603
1604 // Copy from sound |kPath1| to broken |kPath2|.
1605 context.reset(NewContext(NULL));
1606 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491607 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501608 true /* copy */));
[email protected]34583332011-08-31 08:59:471609
[email protected]7878ece2011-09-05 11:41:491610 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471611 context.reset(NewContext(NULL));
1612 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491613 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471614 context.get(), kPath1,
1615 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1616 &file, &created));
1617 EXPECT_TRUE(created);
1618 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1619 EXPECT_EQ(0, file_info.size);
1620 EXPECT_TRUE(base::ClosePlatformFile(file));
1621}
[email protected]9dfdc0e32011-09-02 06:07:441622
[email protected]7878ece2011-09-05 11:41:491623TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091624 const FileSystemURL kPath[] = {
1625 CreateURLFromUTF8("foo"),
1626 CreateURLFromUTF8("bar"),
1627 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441628 };
[email protected]949f25a2012-06-27 01:53:091629 const FileSystemURL empty_path = CreateURL(FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441630 scoped_ptr<FileSystemOperationContext> context;
1631
1632 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1633 bool created = false;
1634 context.reset(NewContext(NULL));
1635 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491636 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441637 EXPECT_TRUE(created);
1638 }
1639
1640 context.reset(NewContext(NULL));
1641 std::vector<base::FileUtilProxy::Entry> entries;
1642 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551643 FileUtilHelper::ReadDirectory(
1644 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441645 EXPECT_EQ(3u, entries.size());
1646
1647 context.reset(NewContext(NULL));
1648 FilePath local_path;
1649 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491650 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441651 EXPECT_TRUE(file_util::Delete(local_path, false));
1652
1653 context.reset(NewContext(NULL));
1654 entries.clear();
1655 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551656 FileUtilHelper::ReadDirectory(
1657 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441658 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1659}
[email protected]fad625e2f2011-12-08 05:38:031660
1661TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1662 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091663 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031664
1665 // Create working directory.
1666 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091667 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031668
1669 // EnsureFileExists, create case.
[email protected]949f25a2012-06-27 01:53:091670 FileSystemURL url(dir_url.WithPath(
1671 dir_url.path().AppendASCII("EnsureFileExists_file")));
[email protected]fad625e2f2011-12-08 05:38:031672 bool created = false;
[email protected]949f25a2012-06-27 01:53:091673 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031674 context.reset(NewContext(NULL));
1675 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091676 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031677 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091678 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031679
1680 // non create case.
1681 created = true;
[email protected]949f25a2012-06-27 01:53:091682 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031683 context.reset(NewContext(NULL));
1684 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091685 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031686 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091687 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031688
1689 // fail case.
[email protected]949f25a2012-06-27 01:53:091690 url = dir_url.WithPath(dir_url.path().AppendASCII("EnsureFileExists_dir"));
[email protected]fad625e2f2011-12-08 05:38:031691 context.reset(NewContext(NULL));
1692 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091693 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031694
[email protected]949f25a2012-06-27 01:53:091695 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031696 context.reset(NewContext(NULL));
1697 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091698 ofu()->EnsureFileExists(context.get(), url, &created));
1699 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031700
1701 // CreateOrOpen, create case.
[email protected]949f25a2012-06-27 01:53:091702 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateOrOpen_file"));
[email protected]403ada82013-01-08 07:51:391703 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031704 created = false;
[email protected]949f25a2012-06-27 01:53:091705 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031706 context.reset(NewContext(NULL));
1707 EXPECT_EQ(base::PLATFORM_FILE_OK,
1708 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091709 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031710 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1711 &file_handle, &created));
1712 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1713 EXPECT_TRUE(created);
1714 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091715 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031716
1717 // open case.
1718 file_handle = base::kInvalidPlatformFileValue;
1719 created = true;
[email protected]949f25a2012-06-27 01:53:091720 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031721 context.reset(NewContext(NULL));
1722 EXPECT_EQ(base::PLATFORM_FILE_OK,
1723 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091724 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031725 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1726 &file_handle, &created));
1727 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1728 EXPECT_FALSE(created);
1729 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091730 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031731
1732 // fail case
1733 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091734 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031735 context.reset(NewContext(NULL));
1736 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1737 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091738 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031739 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1740 &file_handle, &created));
1741 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091742 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031743
1744 // CreateDirectory, create case.
1745 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]949f25a2012-06-27 01:53:091746 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir"));
1747 FileSystemURL subdir_url(url.WithPath(url.path().AppendASCII("subdir")));
1748 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031749 context.reset(NewContext(NULL));
1750 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091751 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031752 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091753 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031754
1755 // create subdir case.
1756 // Creating CreateDirectory_dir/subdir2.
[email protected]949f25a2012-06-27 01:53:091757 subdir_url = url.WithPath(url.path().AppendASCII("subdir2"));
1758 ClearTimestamp(dir_url);
1759 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031760 context.reset(NewContext(NULL));
1761 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091762 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031763 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091764 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1765 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031766
1767 // fail case.
[email protected]949f25a2012-06-27 01:53:091768 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir"));
1769 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031770 context.reset(NewContext(NULL));
1771 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091772 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031773 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091774 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031775
1776 // CopyInForeignFile, create case.
[email protected]949f25a2012-06-27 01:53:091777 url = dir_url.WithPath(dir_url.path().AppendASCII("CopyInForeignFile_file"));
1778 FileSystemURL src_path = dir_url.WithPath(
1779 dir_url.path().AppendASCII("CopyInForeignFile_src_file"));
[email protected]fad625e2f2011-12-08 05:38:031780 context.reset(NewContext(NULL));
1781 EXPECT_EQ(base::PLATFORM_FILE_OK,
1782 ofu()->EnsureFileExists(context.get(), src_path, &created));
1783 EXPECT_TRUE(created);
1784 FilePath src_local_path;
1785 context.reset(NewContext(NULL));
1786 EXPECT_EQ(base::PLATFORM_FILE_OK,
1787 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1788
[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_OK,
[email protected]08f8feb2012-02-26 11:53:501792 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131793 src_local_path,
[email protected]949f25a2012-06-27 01:53:091794 url));
1795 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031796}
1797
1798TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1799 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091800 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031801
1802 // Create working directory.
1803 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091804 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031805
1806 // DeleteFile, delete case.
[email protected]949f25a2012-06-27 01:53:091807 FileSystemURL url = dir_url.WithPath(
1808 dir_url.path().AppendASCII("DeleteFile_file"));
[email protected]fad625e2f2011-12-08 05:38:031809 bool created = false;
1810 context.reset(NewContext(NULL));
1811 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091812 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031813 EXPECT_TRUE(created);
1814
[email protected]949f25a2012-06-27 01:53:091815 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031816 context.reset(NewContext(NULL));
1817 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091818 ofu()->DeleteFile(context.get(), url));
1819 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031820
1821 // fail case.
[email protected]949f25a2012-06-27 01:53:091822 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031823 context.reset(NewContext(NULL));
1824 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091825 ofu()->DeleteFile(context.get(), url));
1826 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031827
1828 // DeleteSingleDirectory, fail case.
[email protected]949f25a2012-06-27 01:53:091829 url = dir_url.WithPath(
1830 dir_url.path().AppendASCII("DeleteSingleDirectory_dir"));
1831 FileSystemURL file_path(url.WithPath(url.path().AppendASCII("pakeratta")));
[email protected]fad625e2f2011-12-08 05:38:031832 context.reset(NewContext(NULL));
1833 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091834 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031835 created = false;
1836 context.reset(NewContext(NULL));
1837 EXPECT_EQ(base::PLATFORM_FILE_OK,
1838 ofu()->EnsureFileExists(context.get(), file_path, &created));
1839 EXPECT_TRUE(created);
1840
[email protected]949f25a2012-06-27 01:53:091841 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031842 context.reset(NewContext(NULL));
1843 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]949f25a2012-06-27 01:53:091844 ofu()->DeleteSingleDirectory(context.get(), url));
1845 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031846
1847 // delete case.
1848 context.reset(NewContext(NULL));
1849 EXPECT_EQ(base::PLATFORM_FILE_OK,
1850 ofu()->DeleteFile(context.get(), file_path));
1851
[email protected]949f25a2012-06-27 01:53:091852 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031853 context.reset(NewContext(NULL));
1854 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091855 ofu()->DeleteSingleDirectory(context.get(), url));
1856 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031857}
1858
1859TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1860 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091861 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031862 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091863 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031864 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091865 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031866 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091867 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031868}
[email protected]a3938912012-03-27 14:00:551869
1870TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091871 FileSystemURL dir = CreateURLFromUTF8("foo");
1872 FileSystemURL url1 = dir.WithPath(dir.path().AppendASCII("bar"));
1873 FileSystemURL url2 = dir.WithPath(dir.path().AppendASCII("baz"));
[email protected]a3938912012-03-27 14:00:551874
1875 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1876 EXPECT_EQ(base::PLATFORM_FILE_OK,
1877 ofu()->CreateDirectory(context.get(), dir, false, false));
1878
1879 bool created = false;
1880 context.reset(NewContext(NULL));
1881 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091882 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551883 EXPECT_TRUE(created);
1884
1885 context.reset(NewContext(NULL));
1886 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091887 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551888
1889 FilePath file_path;
1890 context.reset(NewContext(NULL));
1891 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091892 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551893 EXPECT_FALSE(file_path.empty());
1894
1895 context.reset(NewContext(NULL));
1896 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091897 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551898 base::Time::Now() + base::TimeDelta::FromHours(1),
1899 base::Time()));
1900
1901 context.reset(NewContext(NULL));
1902 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1903 ofu()->CreateFileEnumerator(context.get(), dir, false));
1904
1905 int count = 0;
1906 FilePath file_path_each;
1907 while (!(file_path_each = file_enum->Next()).empty()) {
1908 context.reset(NewContext(NULL));
1909 base::PlatformFileInfo file_info;
1910 FilePath file_path;
1911 EXPECT_EQ(base::PLATFORM_FILE_OK,
1912 ofu()->GetFileInfo(context.get(),
[email protected]949f25a2012-06-27 01:53:091913 dir.WithPath(file_path_each),
[email protected]a3938912012-03-27 14:00:551914 &file_info, &file_path));
1915 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1916 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1917 EXPECT_EQ(file_info.size, file_enum->Size());
1918 ++count;
1919 }
1920 EXPECT_EQ(2, count);
1921}
[email protected]294dd0312012-05-11 07:35:131922
1923TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091924 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1925 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1926 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1927 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131928 bool created;
1929
1930 int64 expected_total_file_size = 0;
1931 ASSERT_EQ(base::PLATFORM_FILE_OK,
1932 ofu()->EnsureFileExists(
1933 AllowUsageIncrease(PathCost(from_file))->context(),
1934 from_file, &created));
1935 ASSERT_TRUE(created);
1936 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1937
1938 ASSERT_EQ(base::PLATFORM_FILE_OK,
1939 ofu()->EnsureFileExists(
1940 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1941 obstacle_file, &created));
1942 ASSERT_TRUE(created);
1943 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1944
1945 int64 from_file_size = 1020;
1946 expected_total_file_size += from_file_size;
1947 ASSERT_EQ(base::PLATFORM_FILE_OK,
1948 ofu()->Truncate(
1949 AllowUsageIncrease(from_file_size)->context(),
1950 from_file, from_file_size));
1951 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1952
1953 int64 obstacle_file_size = 1;
1954 expected_total_file_size += obstacle_file_size;
1955 ASSERT_EQ(base::PLATFORM_FILE_OK,
1956 ofu()->Truncate(
1957 AllowUsageIncrease(obstacle_file_size)->context(),
1958 obstacle_file, obstacle_file_size));
1959 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1960
1961 int64 to_file1_size = from_file_size;
1962 expected_total_file_size += to_file1_size;
1963 ASSERT_EQ(base::PLATFORM_FILE_OK,
1964 ofu()->CopyOrMoveFile(
1965 AllowUsageIncrease(
1966 PathCost(to_file1) + to_file1_size)->context(),
1967 from_file, to_file1, true /* copy */));
1968 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1969
1970 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1971 ofu()->CopyOrMoveFile(
1972 DisallowUsageIncrease(
1973 PathCost(to_file2) + from_file_size)->context(),
1974 from_file, to_file2, true /* copy */));
1975 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1976
1977 int64 old_obstacle_file_size = obstacle_file_size;
1978 obstacle_file_size = from_file_size;
1979 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1980 ASSERT_EQ(base::PLATFORM_FILE_OK,
1981 ofu()->CopyOrMoveFile(
1982 AllowUsageIncrease(
1983 obstacle_file_size - old_obstacle_file_size)->context(),
1984 from_file, obstacle_file, true /* copy */));
1985 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1986
1987 int64 old_from_file_size = from_file_size;
1988 from_file_size = old_from_file_size - 1;
1989 expected_total_file_size += from_file_size - old_from_file_size;
1990 ASSERT_EQ(base::PLATFORM_FILE_OK,
1991 ofu()->Truncate(
1992 AllowUsageIncrease(
1993 from_file_size - old_from_file_size)->context(),
1994 from_file, from_file_size));
1995 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1996
1997 // quota exceeded
1998 {
1999 old_obstacle_file_size = obstacle_file_size;
2000 obstacle_file_size = from_file_size;
2001 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2002 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2003 obstacle_file_size - old_obstacle_file_size);
2004 helper->context()->set_allowed_bytes_growth(
2005 helper->context()->allowed_bytes_growth() - 1);
2006 ASSERT_EQ(base::PLATFORM_FILE_OK,
2007 ofu()->CopyOrMoveFile(
2008 helper->context(),
2009 from_file, obstacle_file, true /* copy */));
2010 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2011 }
2012}
2013
2014TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092015 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2016 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2017 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132018 bool created;
2019
2020 int64 expected_total_file_size = 0;
2021 ASSERT_EQ(base::PLATFORM_FILE_OK,
2022 ofu()->EnsureFileExists(
2023 AllowUsageIncrease(PathCost(from_file))->context(),
2024 from_file, &created));
2025 ASSERT_TRUE(created);
2026 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2027
2028 int64 from_file_size = 1020;
2029 expected_total_file_size += from_file_size;
2030 ASSERT_EQ(base::PLATFORM_FILE_OK,
2031 ofu()->Truncate(
2032 AllowUsageIncrease(from_file_size)->context(),
2033 from_file, from_file_size));
2034 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2035
2036 int64 to_file_size ALLOW_UNUSED = from_file_size;
2037 from_file_size = 0;
2038 ASSERT_EQ(base::PLATFORM_FILE_OK,
2039 ofu()->CopyOrMoveFile(
2040 AllowUsageIncrease(-PathCost(from_file) +
2041 PathCost(to_file))->context(),
2042 from_file, to_file, false /* move */));
2043 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2044
2045 ASSERT_EQ(base::PLATFORM_FILE_OK,
2046 ofu()->EnsureFileExists(
2047 AllowUsageIncrease(PathCost(from_file))->context(),
2048 from_file, &created));
2049 ASSERT_TRUE(created);
2050 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2051
2052 ASSERT_EQ(base::PLATFORM_FILE_OK,
2053 ofu()->EnsureFileExists(
2054 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2055 obstacle_file, &created));
2056 ASSERT_TRUE(created);
2057 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2058
2059 from_file_size = 1020;
2060 expected_total_file_size += from_file_size;
2061 ASSERT_EQ(base::PLATFORM_FILE_OK,
2062 ofu()->Truncate(
2063 AllowUsageIncrease(from_file_size)->context(),
2064 from_file, from_file_size));
2065 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2066
2067 int64 obstacle_file_size = 1;
2068 expected_total_file_size += obstacle_file_size;
2069 ASSERT_EQ(base::PLATFORM_FILE_OK,
2070 ofu()->Truncate(
2071 AllowUsageIncrease(1)->context(),
2072 obstacle_file, obstacle_file_size));
2073 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2074
2075 int64 old_obstacle_file_size = obstacle_file_size;
2076 obstacle_file_size = from_file_size;
2077 from_file_size = 0;
2078 expected_total_file_size -= old_obstacle_file_size;
2079 ASSERT_EQ(base::PLATFORM_FILE_OK,
2080 ofu()->CopyOrMoveFile(
2081 AllowUsageIncrease(
2082 -old_obstacle_file_size - PathCost(from_file))->context(),
2083 from_file, obstacle_file,
2084 false /* move */));
2085 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2086
2087 ASSERT_EQ(base::PLATFORM_FILE_OK,
2088 ofu()->EnsureFileExists(
2089 AllowUsageIncrease(PathCost(from_file))->context(),
2090 from_file, &created));
2091 ASSERT_TRUE(created);
2092 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2093
2094 from_file_size = 10;
2095 expected_total_file_size += from_file_size;
2096 ASSERT_EQ(base::PLATFORM_FILE_OK,
2097 ofu()->Truncate(
2098 AllowUsageIncrease(from_file_size)->context(),
2099 from_file, from_file_size));
2100 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2101
2102 // quota exceeded even after operation
2103 old_obstacle_file_size = obstacle_file_size;
2104 obstacle_file_size = from_file_size;
2105 from_file_size = 0;
2106 expected_total_file_size -= old_obstacle_file_size;
2107 scoped_ptr<FileSystemOperationContext> context =
2108 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2109 ASSERT_EQ(base::PLATFORM_FILE_OK,
2110 ofu()->CopyOrMoveFile(
2111 context.get(), from_file, obstacle_file, false /* move */));
2112 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2113 context.reset();
2114}
2115
2116TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092117 FileSystemURL dir(CreateURLFromUTF8("dir"));
2118 FileSystemURL file(CreateURLFromUTF8("file"));
2119 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2120 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132121 bool created;
2122
2123 ASSERT_EQ(base::PLATFORM_FILE_OK,
2124 ofu()->EnsureFileExists(
2125 AllowUsageIncrease(PathCost(file))->context(),
2126 file, &created));
2127 ASSERT_TRUE(created);
2128 ASSERT_EQ(0, ComputeTotalFileSize());
2129
2130 ASSERT_EQ(base::PLATFORM_FILE_OK,
2131 ofu()->CreateDirectory(
2132 AllowUsageIncrease(PathCost(dir))->context(),
2133 dir, false, false));
2134 ASSERT_EQ(0, ComputeTotalFileSize());
2135
2136 ASSERT_EQ(base::PLATFORM_FILE_OK,
2137 ofu()->EnsureFileExists(
2138 AllowUsageIncrease(PathCost(dfile1))->context(),
2139 dfile1, &created));
2140 ASSERT_TRUE(created);
2141 ASSERT_EQ(0, ComputeTotalFileSize());
2142
2143 ASSERT_EQ(base::PLATFORM_FILE_OK,
2144 ofu()->EnsureFileExists(
2145 AllowUsageIncrease(PathCost(dfile2))->context(),
2146 dfile2, &created));
2147 ASSERT_TRUE(created);
2148 ASSERT_EQ(0, ComputeTotalFileSize());
2149
2150 ASSERT_EQ(base::PLATFORM_FILE_OK,
2151 ofu()->Truncate(
2152 AllowUsageIncrease(340)->context(),
2153 file, 340));
2154 ASSERT_EQ(340, ComputeTotalFileSize());
2155
2156 ASSERT_EQ(base::PLATFORM_FILE_OK,
2157 ofu()->Truncate(
2158 AllowUsageIncrease(1020)->context(),
2159 dfile1, 1020));
2160 ASSERT_EQ(1360, ComputeTotalFileSize());
2161
2162 ASSERT_EQ(base::PLATFORM_FILE_OK,
2163 ofu()->Truncate(
2164 AllowUsageIncrease(120)->context(),
2165 dfile2, 120));
2166 ASSERT_EQ(1480, ComputeTotalFileSize());
2167
2168 ASSERT_EQ(base::PLATFORM_FILE_OK,
2169 ofu()->DeleteFile(
2170 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2171 file));
2172 ASSERT_EQ(1140, ComputeTotalFileSize());
2173
2174 ASSERT_EQ(base::PLATFORM_FILE_OK,
2175 FileUtilHelper::Delete(
2176 AllowUsageIncrease(-PathCost(dir) -
2177 PathCost(dfile1) -
2178 PathCost(dfile2) -
2179 1020 - 120)->context(),
2180 ofu(), dir, true));
2181 ASSERT_EQ(0, ComputeTotalFileSize());
2182}
[email protected]7d78be12012-05-24 07:07:262183
2184TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092185 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262186 base::PlatformFile file_handle;
2187 bool created;
2188
2189 // Creating a file.
2190 ASSERT_EQ(base::PLATFORM_FILE_OK,
2191 ofu()->EnsureFileExists(
2192 AllowUsageIncrease(PathCost(file))->context(),
2193 file, &created));
2194 ASSERT_TRUE(created);
2195 ASSERT_EQ(0, ComputeTotalFileSize());
2196
2197 // Opening it, which shouldn't change the usage.
2198 ASSERT_EQ(base::PLATFORM_FILE_OK,
2199 ofu()->CreateOrOpen(
2200 AllowUsageIncrease(0)->context(), file,
2201 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2202 &file_handle, &created));
2203 ASSERT_EQ(0, ComputeTotalFileSize());
2204 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2205
2206 const int length = 33;
2207 ASSERT_EQ(base::PLATFORM_FILE_OK,
2208 ofu()->Truncate(
2209 AllowUsageIncrease(length)->context(), file, length));
2210 ASSERT_EQ(length, ComputeTotalFileSize());
2211
2212 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2213 ASSERT_EQ(base::PLATFORM_FILE_OK,
2214 ofu()->CreateOrOpen(
2215 AllowUsageIncrease(-length)->context(), file,
2216 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2217 &file_handle, &created));
2218 ASSERT_EQ(0, ComputeTotalFileSize());
2219 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2220
2221 // Extending the file again.
2222 ASSERT_EQ(base::PLATFORM_FILE_OK,
2223 ofu()->Truncate(
2224 AllowUsageIncrease(length)->context(), file, length));
2225 ASSERT_EQ(length, ComputeTotalFileSize());
2226
2227 // Opening it with TRUNCATED flag, which should truncate the file size.
2228 ASSERT_EQ(base::PLATFORM_FILE_OK,
2229 ofu()->CreateOrOpen(
2230 AllowUsageIncrease(-length)->context(), file,
2231 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2232 &file_handle, &created));
2233 ASSERT_EQ(0, ComputeTotalFileSize());
2234 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2235}
[email protected]c4e6f9c2012-09-09 17:42:102236
2237} // namespace fileapi