blob: ef0f2bccb5da0ca1011a4c52eb75caacf3f26122 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d4905e2e2011-05-13 21:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4905e2e2011-05-13 21:56:325#include <set>
6#include <string>
[email protected]403ada82013-01-08 07:51:397#include <vector>
[email protected]d4905e2e2011-05-13 21:56:328
[email protected]4d99be52011-10-18 14:11:039#include "base/bind.h"
[email protected]d4905e2e2011-05-13 21:56:3210#include "base/file_path.h"
11#include "base/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3213#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1614#include "base/message_loop.h"
[email protected]d4905e2e2011-05-13 21:56:3215#include "base/platform_file.h"
[email protected]d4905e2e2011-05-13 21:56:3216#include "testing/gtest/include/gtest/gtest.h"
[email protected]6ef0c3912013-01-25 22:46:3417#include "webkit/fileapi/external_mount_points.h"
[email protected]d4905e2e2011-05-13 21:56:3218#include "webkit/fileapi/file_system_context.h"
19#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1020#include "webkit/fileapi/file_system_task_runners.h"
[email protected]0c5ebe32011-08-19 22:37:1621#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]aa437fa2012-03-03 02:09:0722#include "webkit/fileapi/file_util_helper.h"
[email protected]02a60542012-07-24 20:05:3323#include "webkit/fileapi/local_file_system_test_helper.h"
[email protected]c4e6f9c2012-09-09 17:42:1024#include "webkit/fileapi/mock_file_change_observer.h"
[email protected]e7e46732012-01-05 11:45:5525#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4926#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5627#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1628#include "webkit/quota/mock_special_storage_policy.h"
29#include "webkit/quota/quota_manager.h"
30#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3231
[email protected]c4e6f9c2012-09-09 17:42:1032namespace fileapi {
[email protected]d4905e2e2011-05-13 21:56:3233
34namespace {
35
[email protected]a3ef4832013-02-02 05:12:3336bool FileExists(const base::FilePath& path) {
[email protected]d4905e2e2011-05-13 21:56:3237 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
38}
39
[email protected]a3ef4832013-02-02 05:12:3340int64 GetSize(const base::FilePath& path) {
[email protected]81b7f662011-05-26 00:54:4641 int64 size;
42 EXPECT_TRUE(file_util::GetFileSize(path, &size));
43 return size;
44}
45
[email protected]d4905e2e2011-05-13 21:56:3246// After a move, the dest exists and the source doesn't.
47// After a copy, both source and dest exist.
48struct CopyMoveTestCaseRecord {
49 bool is_copy_not_move;
50 const char source_path[64];
51 const char dest_path[64];
52 bool cause_overwrite;
53};
54
55const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
56 // This is the combinatoric set of:
57 // rename vs. same-name
58 // different directory vs. same directory
59 // overwrite vs. no-overwrite
60 // copy vs. move
61 // We can never be called with source and destination paths identical, so
62 // those cases are omitted.
63 {true, "dir0/file0", "dir0/file1", false},
64 {false, "dir0/file0", "dir0/file1", false},
65 {true, "dir0/file0", "dir0/file1", true},
66 {false, "dir0/file0", "dir0/file1", true},
67
68 {true, "dir0/file0", "dir1/file0", false},
69 {false, "dir0/file0", "dir1/file0", false},
70 {true, "dir0/file0", "dir1/file0", true},
71 {false, "dir0/file0", "dir1/file0", true},
72 {true, "dir0/file0", "dir1/file1", false},
73 {false, "dir0/file0", "dir1/file1", false},
74 {true, "dir0/file0", "dir1/file1", true},
75 {false, "dir0/file0", "dir1/file1", true},
76};
77
[email protected]fcc2d5f2011-05-23 22:06:2678struct OriginEnumerationTestRecord {
79 std::string origin_url;
80 bool has_temporary;
81 bool has_persistent;
82};
83
84const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
85 {"http://example.com", false, true},
86 {"http://example1.com", true, false},
87 {"https://example1.com", true, true},
88 {"file://", false, true},
89 {"http://example.com:8000", false, true},
90};
91
[email protected]d4905e2e2011-05-13 21:56:3292} // namespace (anonymous)
93
94// TODO(ericu): The vast majority of this and the other FSFU subclass tests
95// could theoretically be shared. It would basically be a FSFU interface
96// compliance test, and only the subclass-specific bits that look into the
97// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:4998class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:3299 public:
[email protected]7878ece2011-09-05 11:41:49100 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26101 : origin_(GURL("http://www.example.com")),
102 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03103 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16104 test_helper_(origin_, type_),
105 quota_status_(quota::kQuotaStatusUnknown),
106 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32107 }
108
[email protected]7fd8fa4f2013-02-07 05:43:50109 virtual void SetUp() {
[email protected]d4905e2e2011-05-13 21:56:32110 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
111
[email protected]e7e46732012-01-05 11:45:55112 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
113 new quota::MockSpecialStoragePolicy();
114
[email protected]0c5ebe32011-08-19 22:37:16115 quota_manager_ = new quota::QuotaManager(
116 false /* is_incognito */,
117 data_dir_.path(),
118 base::MessageLoopProxy::current(),
119 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55120 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16121
122 // Every time we create a new helper, it creates another context, which
123 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49124 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16125 file_system_context_ = new FileSystemContext(
[email protected]dc57ec82012-08-07 03:50:10126 FileSystemTaskRunners::CreateMockTaskRunners(),
[email protected]6ef0c3912013-01-25 22:46:34127 ExternalMountPoints::CreateRefCounted().get(),
[email protected]e7e46732012-01-05 11:45:55128 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16129 quota_manager_->proxy(),
130 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55131 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16132
[email protected]4f056a872013-01-23 04:24:36133 test_helper_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10134
135 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32136 }
137
[email protected]7fd8fa4f2013-02-07 05:43:50138 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55139 quota_manager_ = NULL;
140 test_helper_.TearDown();
141 }
142
[email protected]294dd0312012-05-11 07:35:13143 scoped_ptr<FileSystemOperationContext> LimitedContext(
144 int64 allowed_bytes_growth) {
145 scoped_ptr<FileSystemOperationContext> context(
146 test_helper_.NewOperationContext());
147 context->set_allowed_bytes_growth(allowed_bytes_growth);
148 return context.Pass();
149 }
150
151 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
152 return LimitedContext(kint64max);
153 }
154
[email protected]02a60542012-07-24 20:05:33155 FileSystemOperationContext* NewContext(
156 LocalFileSystemTestOriginHelper* helper) {
[email protected]c4e6f9c2012-09-09 17:42:10157 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16158 FileSystemOperationContext* context;
159 if (helper)
160 context = helper->NewOperationContext();
161 else
162 context = test_helper_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46163 // Setting allowed_bytes_growth big enough for all tests.
164 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10165 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32166 return context;
167 }
168
[email protected]c4e6f9c2012-09-09 17:42:10169 const ChangeObserverList& change_observers() const {
170 return change_observers_;
171 }
172
173 MockFileChangeObserver* change_observer() {
174 return &change_observer_;
175 }
176
[email protected]0c5ebe32011-08-19 22:37:16177 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49178 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16179 // Use this for tests which need to run in multiple origins; we need a test
180 // helper per origin.
[email protected]02a60542012-07-24 20:05:33181 LocalFileSystemTestOriginHelper* NewHelper(
[email protected]0c5ebe32011-08-19 22:37:16182 const GURL& origin, fileapi::FileSystemType type) {
[email protected]02a60542012-07-24 20:05:33183 LocalFileSystemTestOriginHelper* helper =
184 new LocalFileSystemTestOriginHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16185
[email protected]4f056a872013-01-23 04:24:36186 helper->SetUp(file_system_context_.get());
[email protected]0c5ebe32011-08-19 22:37:16187 return helper;
188 }
189
[email protected]7878ece2011-09-05 11:41:49190 ObfuscatedFileUtil* ofu() {
[email protected]4f056a872013-01-23 04:24:36191 return static_cast<ObfuscatedFileUtil*>(test_helper_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32192 }
193
[email protected]a3ef4832013-02-02 05:12:33194 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35195 return data_dir_.path();
196 }
197
[email protected]0c5ebe32011-08-19 22:37:16198 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26199 return origin_;
200 }
201
202 fileapi::FileSystemType type() const {
203 return type_;
204 }
205
[email protected]294dd0312012-05-11 07:35:13206 int64 ComputeTotalFileSize() {
207 return test_helper_.ComputeCurrentOriginUsage() -
208 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
209 }
210
[email protected]0c5ebe32011-08-19 22:37:16211 void GetUsageFromQuotaManager() {
212 quota_manager_->GetUsageAndQuota(
213 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03214 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
215 weak_factory_.GetWeakPtr()));
[email protected]4e1c7e52012-12-16 01:06:36216 MessageLoop::current()->RunUntilIdle();
[email protected]0c5ebe32011-08-19 22:37:16217 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
218 }
219
220 void RevokeUsageCache() {
221 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]022d2702012-05-14 16:04:26222 file_util::Delete(test_helper_.GetUsageCachePath(), false);
223 }
224
225 int64 SizeByQuotaUtil() {
226 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16227 }
228
229 int64 SizeInUsageFile() {
[email protected]8eee1b62013-01-08 02:26:44230 MessageLoop::current()->RunUntilIdle();
[email protected]022d2702012-05-14 16:04:26231 return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath());
[email protected]0c5ebe32011-08-19 22:37:16232 }
233
[email protected]13f92f6e2012-08-13 07:39:14234 bool PathExists(const FileSystemURL& url) {
235 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21236 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33237 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21238 base::PlatformFileError error = ofu()->GetFileInfo(
239 context.get(), url, &file_info, &platform_path);
240 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14241 }
242
243 bool DirectoryExists(const FileSystemURL& url) {
244 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
245 return FileUtilHelper::DirectoryExists(context.get(), ofu(), url);
246 }
247
[email protected]0c5ebe32011-08-19 22:37:16248 int64 usage() const { return usage_; }
249
[email protected]949f25a2012-06-27 01:53:09250 FileSystemURL CreateURLFromUTF8(const std::string& path) {
251 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50252 }
253
[email protected]949f25a2012-06-27 01:53:09254 int64 PathCost(const FileSystemURL& url) {
255 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13256 }
257
[email protected]a3ef4832013-02-02 05:12:33258 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]949f25a2012-06-27 01:53:09259 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50260 }
261
[email protected]0c5ebe32011-08-19 22:37:16262 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
263 EXPECT_EQ(quota::kQuotaStatusOk, status);
264 quota_status_ = status;
265 usage_ = usage;
266 }
267
[email protected]d4905e2e2011-05-13 21:56:32268 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39269 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16270 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33271 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49272 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09273 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32274
275 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33276 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49277 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09278 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32279 EXPECT_EQ(data_path, local_path);
280 EXPECT_TRUE(FileExists(data_path));
281 EXPECT_EQ(0, GetSize(data_path));
282
283 const char data[] = "test data";
284 const int length = arraysize(data) - 1;
285
286 if (base::kInvalidPlatformFileValue == file_handle) {
287 bool created = true;
[email protected]403ada82013-01-08 07:51:39288 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32289 file_handle = base::CreatePlatformFile(
290 data_path,
291 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
292 &created,
293 &error);
[email protected]81b7f662011-05-26 00:54:46294 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32295 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
296 EXPECT_FALSE(created);
297 }
298 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
299 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
300
301 base::PlatformFileInfo file_info1;
302 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16303 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49304 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09305 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32306 EXPECT_EQ(data_path, local_path);
307
308 EXPECT_FALSE(file_info0.is_directory);
309 EXPECT_FALSE(file_info1.is_directory);
310 EXPECT_FALSE(file_info0.is_symbolic_link);
311 EXPECT_FALSE(file_info1.is_symbolic_link);
312 EXPECT_EQ(0, file_info0.size);
313 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32314 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32315
[email protected]0c5ebe32011-08-19 22:37:16316 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49317 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09318 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32319 EXPECT_EQ(length * 2, GetSize(data_path));
320
[email protected]0c5ebe32011-08-19 22:37:16321 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49322 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09323 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16324 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32325 }
326
327 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09328 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33329 const std::set<base::FilePath::StringType>& files,
330 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32331 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33332 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32333 for (iter = files.begin(); iter != files.end(); ++iter) {
334 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16335 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32336 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49337 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09338 context.get(), root_url.WithPath(root_url.path().Append(*iter)),
[email protected]d4905e2e2011-05-13 21:56:32339 &created));
340 ASSERT_FALSE(created);
341 }
342 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16343 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14344 EXPECT_TRUE(DirectoryExists(
[email protected]949f25a2012-06-27 01:53:09345 root_url.WithPath(root_url.path().Append(*iter))));
[email protected]d4905e2e2011-05-13 21:56:32346 }
347 }
348
[email protected]294dd0312012-05-11 07:35:13349 class UsageVerifyHelper {
350 public:
351 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33352 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13353 int64 expected_usage)
354 : context_(context.Pass()),
355 test_helper_(test_helper),
356 expected_usage_(expected_usage) {}
357
358 ~UsageVerifyHelper() {
[email protected]8eee1b62013-01-08 02:26:44359 MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13360 Check();
361 }
362
363 FileSystemOperationContext* context() {
364 return context_.get();
365 }
366
367 private:
368 void Check() {
369 ASSERT_EQ(expected_usage_,
370 test_helper_->GetCachedOriginUsage());
371 }
372
373 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33374 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13375 int64 expected_usage_;
376 };
377
378 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
379 int64 usage = test_helper_.GetCachedOriginUsage();
380 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
381 LimitedContext(requested_growth),
382 &test_helper_, usage + requested_growth));
383 }
384
385 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
386 int64 usage = test_helper_.GetCachedOriginUsage();
387 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
388 LimitedContext(requested_growth - 1), &test_helper_, usage));
389 }
390
[email protected]d4905e2e2011-05-13 21:56:32391 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09392 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33393 std::set<base::FilePath::StringType>* files,
394 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32395 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16396 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32397 std::vector<base::FileUtilProxy::Entry> entries;
398 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55399 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:09400 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32401 EXPECT_EQ(0UL, entries.size());
402
403 files->clear();
[email protected]89ee4272011-05-16 18:45:17404 files->insert(FILE_PATH_LITERAL("first"));
405 files->insert(FILE_PATH_LITERAL("second"));
406 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32407 directories->clear();
[email protected]89ee4272011-05-16 18:45:17408 directories->insert(FILE_PATH_LITERAL("fourth"));
409 directories->insert(FILE_PATH_LITERAL("fifth"));
410 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33411 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32412 for (iter = files->begin(); iter != files->end(); ++iter) {
413 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16414 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32415 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49416 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50417 context.get(),
[email protected]949f25a2012-06-27 01:53:09418 root_url.WithPath(root_url.path().Append(*iter)),
[email protected]08f8feb2012-02-26 11:53:50419 &created));
[email protected]d4905e2e2011-05-13 21:56:32420 ASSERT_TRUE(created);
421 }
422 for (iter = directories->begin(); iter != directories->end(); ++iter) {
423 bool exclusive = true;
424 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16425 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32426 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49427 ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09428 context.get(), root_url.WithPath(root_url.path().Append(*iter)),
429 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32430 }
[email protected]949f25a2012-06-27 01:53:09431 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32432 }
433
[email protected]949f25a2012-06-27 01:53:09434 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33435 std::set<base::FilePath::StringType> files;
436 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09437 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32438
439 scoped_ptr<FileSystemOperationContext> context;
440 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16441 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32442 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55443 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:09444 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32445 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
446 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10447 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32448 for (entry_iter = entries.begin(); entry_iter != entries.end();
449 ++entry_iter) {
450 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]a3ef4832013-02-02 05:12:33451 std::set<base::FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32452 if (iter != files.end()) {
453 EXPECT_FALSE(entry.is_directory);
454 files.erase(iter);
455 continue;
456 }
[email protected]89ee4272011-05-16 18:45:17457 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32458 EXPECT_FALSE(directories.end() == iter);
459 EXPECT_TRUE(entry.is_directory);
460 directories.erase(iter);
461 }
462 }
463
[email protected]949f25a2012-06-27 01:53:09464 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41465 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32466 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16467
[email protected]2517cfa2011-08-25 05:12:41468 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32469 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49470 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09471 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10472 // Currently we fire no change notifications for Touch.
473 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33474 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32475 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16476 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49477 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09478 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32479 // We compare as time_t here to lower our resolution, to avoid false
480 // negatives caused by conversion to the local filesystem's native
481 // representation and back.
482 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
483
[email protected]0c5ebe32011-08-19 22:37:16484 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32485 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41486 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32487 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49488 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09489 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10490 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16491 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49492 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09493 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32494 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49495 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41496 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32497 }
498
[email protected]81b7f662011-05-26 00:54:46499 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23500 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46501 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33502 base::FilePath root_file_path = source_dir.path();
503 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09504 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46505 int64 src_file_length = 87;
506
507 base::PlatformFileError error_code;
508 bool created = false;
509 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
510 base::PlatformFile file_handle =
511 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50512 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46513 EXPECT_TRUE(created);
514 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
515 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
516 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
517 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
518
519 scoped_ptr<FileSystemOperationContext> context;
520
521 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16522 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46523 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09524 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46525 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10526
527 // We must have observed one (and only one) create_file_count.
528 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
529 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46530 }
531
[email protected]0c5ebe32011-08-19 22:37:16532 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09533 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16534 if (!overwrite) {
535 // Verify that file creation requires sufficient quota for the path.
536 context.reset(NewContext(NULL));
537 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
538 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13539 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09540 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16541 }
542
543 context.reset(NewContext(NULL));
544 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46545 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13546 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09547 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16548
[email protected]13f92f6e2012-08-13 07:39:14549 EXPECT_TRUE(PathExists(dest_url));
550 EXPECT_FALSE(DirectoryExists(dest_url));
551
[email protected]0c5ebe32011-08-19 22:37:16552 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46553 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33554 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49555 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09556 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50557 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46558 EXPECT_TRUE(FileExists(data_path));
559 EXPECT_EQ(src_file_length, GetSize(data_path));
560
561 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09562 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46563 }
564
[email protected]949f25a2012-06-27 01:53:09565 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03566 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
567 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09568 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
569 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03570 }
571
[email protected]949f25a2012-06-27 01:53:09572 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03573 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33574 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03575 base::PlatformFileInfo file_info;
576 context.reset(NewContext(NULL));
577 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09578 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10579 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03580 return file_info.last_modified;
581 }
582
[email protected]949f25a2012-06-27 01:53:09583 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03584 bool copy,
585 bool overwrite) {
586 scoped_ptr<FileSystemOperationContext> context;
[email protected]949f25a2012-06-27 01:53:09587 const FileSystemURL src_dir_url(base_dir.WithPath(
588 base_dir.path().AppendASCII("foo_dir")));
589 const FileSystemURL dest_dir_url(base_dir.WithPath(
590 base_dir.path().AppendASCII("bar_dir")));
[email protected]fad625e2f2011-12-08 05:38:03591
[email protected]949f25a2012-06-27 01:53:09592 const FileSystemURL src_file_url(src_dir_url.WithPath(
593 src_dir_url.path().AppendASCII("hoge")));
594 const FileSystemURL dest_file_url(dest_dir_url.WithPath(
595 dest_dir_url.path().AppendASCII("fuga")));
[email protected]fad625e2f2011-12-08 05:38:03596
597 context.reset(NewContext(NULL));
598 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09599 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03600 context.reset(NewContext(NULL));
601 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09602 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03603
604 bool created = false;
605 context.reset(NewContext(NULL));
606 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09607 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03608 if (overwrite) {
609 context.reset(NewContext(NULL));
610 EXPECT_EQ(base::PLATFORM_FILE_OK,
611 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09612 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03613 }
614
[email protected]949f25a2012-06-27 01:53:09615 ClearTimestamp(src_dir_url);
616 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03617 context.reset(NewContext(NULL));
618 EXPECT_EQ(base::PLATFORM_FILE_OK,
619 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09620 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03621 copy));
[email protected]fad625e2f2011-12-08 05:38:03622 if (copy)
[email protected]949f25a2012-06-27 01:53:09623 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03624 else
[email protected]949f25a2012-06-27 01:53:09625 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
626 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03627 }
628
[email protected]ecdfd6c52012-04-11 13:35:44629 int64 ComputeCurrentUsage() {
630 return test_helper().ComputeCurrentOriginUsage() -
631 test_helper().ComputeCurrentDirectoryDatabaseUsage();
632 }
633
[email protected]02a60542012-07-24 20:05:33634 const LocalFileSystemTestOriginHelper& test_helper() const {
635 return test_helper_;
636 }
[email protected]45ea0fbbb2012-02-27 22:28:49637
[email protected]d4905e2e2011-05-13 21:56:32638 private:
[email protected]ea1a3f62012-11-16 20:34:23639 base::ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01640 MessageLoop message_loop_;
[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]a3ef4832013-02-02 05:12:33696 base::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]a3ef4832013-02-02 05:12:33764 base::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]a3ef4832013-02-02 05:12:33840 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44841 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]bab213be2013-01-23 15:13:08923 ofu()->DeleteDirectory(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]bab213be2013-01-23 15:13:08952 ofu()->DeleteDirectory(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;
[email protected]a3ef4832013-02-02 05:12:33957 base::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]bab213be2013-01-23 15:13:08981 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10982 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:09983 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16984 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32985
[email protected]949f25a2012-06-27 01:53:09986 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:32987
[email protected]13f92f6e2012-08-13 07:39:14988 EXPECT_FALSE(DirectoryExists(url));
989 EXPECT_FALSE(PathExists(url));
990
[email protected]0c5ebe32011-08-19 22:37:16991 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09992 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:49993 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09994 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32995
[email protected]0c5ebe32011-08-19 22:37:16996 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32997 exclusive = true;
998 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16999 context.reset(NewContext(NULL));
1000 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091001 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491002 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091003 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101004 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161005
1006 context.reset(NewContext(NULL));
1007 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091008 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491009 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091010 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101011 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321012
[email protected]13f92f6e2012-08-13 07:39:141013 EXPECT_TRUE(DirectoryExists(url));
1014 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321015
1016 exclusive = true;
1017 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141018 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491019 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091020 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101021 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321022
1023 exclusive = true;
1024 recursive = false;
[email protected]949f25a2012-06-27 01:53:091025 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141026 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491027 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091028 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101029 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321030
[email protected]949f25a2012-06-27 01:53:091031 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321032
[email protected]13f92f6e2012-08-13 07:39:141033 EXPECT_FALSE(DirectoryExists(url));
1034 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321035
1036 exclusive = true;
1037 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141038 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491039 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091040 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101041 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321042
[email protected]13f92f6e2012-08-13 07:39:141043 EXPECT_TRUE(DirectoryExists(url));
1044 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321045
1046 exclusive = true;
1047 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141048 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491049 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091050 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101051 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321052}
1053
[email protected]7878ece2011-09-05 11:41:491054TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161055 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321056 bool exclusive = true;
1057 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091058 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491059 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091060 context.get(), url, exclusive, recursive));
1061 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321062}
1063
[email protected]7878ece2011-09-05 11:41:491064TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]949f25a2012-06-27 01:53:091065 TestReadDirectoryHelper(CreateURLFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:321066}
1067
[email protected]7878ece2011-09-05 11:41:491068TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091069 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321070}
1071
[email protected]7878ece2011-09-05 11:41:491072TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091073 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161074 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321075
1076 bool created = false;
1077 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091078 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321079 ASSERT_TRUE(created);
1080
[email protected]0c5ebe32011-08-19 22:37:161081 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321082 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]1a647202013-01-21 15:32:251083 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]a3938912012-03-27 14:00:551084 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:091085 context.get(), ofu(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321086
[email protected]949f25a2012-06-27 01:53:091087 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321088}
1089
[email protected]7878ece2011-09-05 11:41:491090TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091091 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161092 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411093
1094 base::Time last_access_time = base::Time::Now();
1095 base::Time last_modified_time = base::Time::Now();
1096
1097 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321098 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491099 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091100 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321101
[email protected]2517cfa2011-08-25 05:12:411102 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161103 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411104 bool created = false;
1105 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091106 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411107 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091108 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411109
1110 // Now test a directory:
1111 context.reset(NewContext(NULL));
1112 bool exclusive = true;
1113 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091114 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491115 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091116 url, exclusive, recursive));
1117 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161118}
1119
[email protected]7878ece2011-09-05 11:41:491120TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091121 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161122 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1123
[email protected]949f25a2012-06-27 01:53:091124 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161125 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411126 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161127 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091128 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411129 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161130 context->set_allowed_bytes_growth(1024);
1131 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091132 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411133 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091134 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161135 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1136
1137 context->set_allowed_bytes_growth(1024);
1138 bool exclusive = true;
1139 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091140 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331141 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091142 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161143 path_cost = 0;
[email protected]a3ef4832013-02-02 05:12:331144 for (std::vector<base::FilePath::StringType>::iterator iter = components.begin();
[email protected]0c5ebe32011-08-19 22:37:161145 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491146 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331147 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161148 }
1149 context.reset(NewContext(NULL));
1150 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491151 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091152 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161153 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321154}
1155
[email protected]7878ece2011-09-05 11:41:491156TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091157 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1158 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161159 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321160
1161 bool is_copy_not_move = false;
1162 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091163 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321164 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101165 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161166 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321167 is_copy_not_move = true;
1168 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091169 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321170 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101171 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091172 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321173 bool exclusive = true;
1174 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161175 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491176 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091177 context.get(),
1178 source_url.WithPath(source_url.path().DirName()),
1179 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101180 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321181 is_copy_not_move = false;
1182 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091183 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321184 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101185 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161186 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321187 is_copy_not_move = true;
1188 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091189 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321190 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101191 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321192}
1193
[email protected]7878ece2011-09-05 11:41:491194TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321195 const int64 kSourceLength = 5;
1196 const int64 kDestLength = 50;
1197
1198 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1199 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1200 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1201 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1202 test_case.is_copy_not_move);
1203 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1204 test_case.source_path);
1205 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1206 test_case.dest_path);
1207 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1208 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161209 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321210
1211 bool exclusive = false;
1212 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091213 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1214 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321215
[email protected]0c5ebe32011-08-19 22:37:161216 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491217 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091218 context.get(),
1219 source_url.WithPath(source_url.path().DirName()),
1220 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161221 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491222 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091223 context.get(),
1224 dest_url.WithPath(dest_url.path().DirName()),
1225 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321226
[email protected]d4905e2e2011-05-13 21:56:321227 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161228 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321229 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091230 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321231 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161232 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321233 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091234 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321235
1236 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161237 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321238 created = false;
1239 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091240 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321241 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161242 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321243 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091244 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321245 }
1246
[email protected]0c5ebe32011-08-19 22:37:161247 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491248 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091249 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101250
[email protected]d4905e2e2011-05-13 21:56:321251 if (test_case.is_copy_not_move) {
1252 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331253 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161254 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491255 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091256 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321257 EXPECT_EQ(kSourceLength, file_info.size);
1258 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091259 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321260 } else {
1261 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331262 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161263 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491264 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091265 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321266 }
1267 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331268 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491269 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091270 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321271 EXPECT_EQ(kSourceLength, file_info.size);
1272
1273 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091274 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321275 }
1276}
1277
[email protected]7878ece2011-09-05 11:41:491278TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091279 FileSystemURL src_url = CreateURLFromUTF8("src path");
1280 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161281 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1282 bool created = false;
[email protected]7878ece2011-09-05 11:41:491283 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091284 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161285
1286 bool is_copy = true;
1287 // Copy, no overwrite.
1288 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091289 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161290 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091291 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161292 context.reset(NewContext(NULL));
1293 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091294 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161295 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091296 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161297
1298 // Copy, with overwrite.
1299 context.reset(NewContext(NULL));
1300 context->set_allowed_bytes_growth(0);
1301 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091302 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161303}
1304
[email protected]7878ece2011-09-05 11:41:491305TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091306 FileSystemURL src_url = CreateURLFromUTF8("src path");
1307 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161308 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1309 bool created = false;
[email protected]7878ece2011-09-05 11:41:491310 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091311 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161312
1313 bool is_copy = false;
1314 // Move, rename, no overwrite.
1315 context.reset(NewContext(NULL));
1316 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091317 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1318 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161319 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091320 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161321 context.reset(NewContext(NULL));
1322 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091323 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1324 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161325 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091326 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161327
1328 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491329 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091330 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161331
1332 // Move, rename, with overwrite.
1333 context.reset(NewContext(NULL));
1334 context->set_allowed_bytes_growth(0);
1335 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091336 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161337}
1338
[email protected]7878ece2011-09-05 11:41:491339TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091340 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161341 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1342 bool created = false;
[email protected]7878ece2011-09-05 11:41:491343 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091344 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161345
1346 bool exclusive = true;
1347 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091348 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161349 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491350 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091351 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161352
[email protected]949f25a2012-06-27 01:53:091353 FileSystemURL dest_url = dir_url.WithPath(
1354 dir_url.path().Append(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161355
1356 bool is_copy = false;
1357 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1358 // Move, no rename, no overwrite.
1359 context.reset(NewContext(NULL));
1360 context->set_allowed_bytes_growth(allowed_bytes_growth);
1361 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091362 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161363 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1364
1365 // Move, no rename, with overwrite.
1366 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491367 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091368 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161369 context.reset(NewContext(NULL));
1370 context->set_allowed_bytes_growth(allowed_bytes_growth);
1371 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091372 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161373 EXPECT_EQ(
1374 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091375 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161376 context->allowed_bytes_growth());
1377}
1378
[email protected]7878ece2011-09-05 11:41:491379TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461380 TestCopyInForeignFileHelper(false /* overwrite */);
1381 TestCopyInForeignFileHelper(true /* overwrite */);
1382}
1383
[email protected]7878ece2011-09-05 11:41:491384TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161385 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091386 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321387 bool exclusive = true;
1388 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491389 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091390 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321391
[email protected]a3ef4832013-02-02 05:12:331392 std::set<base::FilePath::StringType> files;
1393 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091394 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321395
[email protected]949f25a2012-06-27 01:53:091396 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321397
[email protected]13f92f6e2012-08-13 07:39:141398 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]0c5ebe32011-08-19 22:37:161399 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321400 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091401 test_helper().SameFileUtilCopy(context.get(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321402
[email protected]949f25a2012-06-27 01:53:091403 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141404 EXPECT_TRUE(DirectoryExists(src_url));
1405 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]0c5ebe32011-08-19 22:37:161406 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321407 recursive = true;
1408 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]aa437fa2012-03-03 02:09:071409 FileUtilHelper::Delete(context.get(), ofu(),
[email protected]949f25a2012-06-27 01:53:091410 dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141411 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321412}
[email protected]6b931152011-05-20 21:02:351413
[email protected]7878ece2011-09-05 11:41:491414TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1415 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1416 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161417 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261418 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161419 EXPECT_EQ(origin(), enumerator->Next());
1420 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1421 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1422 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261423 EXPECT_EQ(GURL(), enumerator->Next());
1424 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1425 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1426
1427 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161428 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261429
1430 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1431 SCOPED_TRACE(testing::Message() <<
1432 "Validating kOriginEnumerationTestRecords " << i);
1433 const OriginEnumerationTestRecord& record =
1434 kOriginEnumerationTestRecords[i];
1435 GURL origin_url(record.origin_url);
1436 origins_expected.insert(origin_url);
1437 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331438 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161439 NewHelper(origin_url, kFileSystemTypeTemporary));
1440 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261441 bool created = false;
1442 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501443 ofu()->EnsureFileExists(
1444 context.get(),
[email protected]949f25a2012-06-27 01:53:091445 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501446 &created));
[email protected]fcc2d5f2011-05-23 22:06:261447 EXPECT_TRUE(created);
1448 }
1449 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331450 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161451 NewHelper(origin_url, kFileSystemTypePersistent));
1452 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261453 bool created = false;
1454 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501455 ofu()->EnsureFileExists(
1456 context.get(),
[email protected]949f25a2012-06-27 01:53:091457 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501458 &created));
[email protected]fcc2d5f2011-05-23 22:06:261459 EXPECT_TRUE(created);
1460 }
1461 }
[email protected]7878ece2011-09-05 11:41:491462 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261463 EXPECT_TRUE(enumerator.get());
1464 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161465 GURL origin_url;
1466 while (!(origin_url = enumerator->Next()).is_empty()) {
1467 origins_found.insert(origin_url);
1468 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261469 bool found = false;
1470 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1471 ++i) {
1472 const OriginEnumerationTestRecord& record =
1473 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161474 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261475 continue;
1476 found = true;
1477 EXPECT_EQ(record.has_temporary,
1478 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1479 EXPECT_EQ(record.has_persistent,
1480 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1481 }
[email protected]0c5ebe32011-08-19 22:37:161482 // Deal with the default filesystem created by the test helper.
1483 if (!found && origin_url == origin()) {
1484 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1485 EXPECT_EQ(true,
1486 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181487 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161488 found = true;
1489 }
[email protected]fcc2d5f2011-05-23 22:06:261490 EXPECT_TRUE(found);
1491 }
1492
1493 std::set<GURL> diff;
1494 std::set_symmetric_difference(origins_expected.begin(),
1495 origins_expected.end(), origins_found.begin(), origins_found.end(),
1496 inserter(diff, diff.begin()));
1497 EXPECT_TRUE(diff.empty());
1498}
[email protected]0c5ebe32011-08-19 22:37:161499
[email protected]7878ece2011-09-05 11:41:491500TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161501 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1502
1503 int64 expected_quota = 0;
1504
[email protected]f83b5b72012-01-27 10:26:561505 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011506 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561507 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331508 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501509 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161510 if (test_case.is_directory) {
1511 bool exclusive = true;
1512 bool recursive = false;
1513 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091514 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501515 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161516 } else {
1517 bool created = false;
1518 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091519 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501520 &created));
[email protected]0c5ebe32011-08-19 22:37:161521 ASSERT_TRUE(created);
1522 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501523 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091524 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501525 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161526 expected_quota += test_case.data_file_size;
1527 }
1528 }
[email protected]022d2702012-05-14 16:04:261529
1530 // Usually raw size in usage cache and the usage returned by QuotaUtil
1531 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161532 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261533 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1534
[email protected]0c5ebe32011-08-19 22:37:161535 RevokeUsageCache();
1536 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261537 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1538
1539 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161540 GetUsageFromQuotaManager();
1541 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261542 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161543 EXPECT_EQ(expected_quota, usage());
1544}
[email protected]34583332011-08-31 08:59:471545
[email protected]7878ece2011-09-05 11:41:491546TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091547 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1548 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471549
1550 scoped_ptr<FileSystemOperationContext> context;
1551 base::PlatformFile file;
1552 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331553 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471554 bool created = false;
1555
1556 // Create a non-empty file.
1557 context.reset(NewContext(NULL));
1558 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491559 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471560 EXPECT_TRUE(created);
1561 context.reset(NewContext(NULL));
1562 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491563 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471564 context.reset(NewContext(NULL));
1565 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491566 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471567 context.get(), kPath1, &file_info, &data_path));
1568 EXPECT_EQ(10, file_info.size);
1569
1570 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491571 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471572
1573 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141574 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471575 context.reset(NewContext(NULL));
1576 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491577 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471578 EXPECT_TRUE(created);
1579 context.reset(NewContext(NULL));
1580 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491581 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471582 context.get(), kPath1, &file_info, &data_path));
1583 EXPECT_EQ(0, file_info.size);
1584
1585 // Make another broken file to |kPath2|.
1586 context.reset(NewContext(NULL));
1587 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491588 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471589 EXPECT_TRUE(created);
1590
1591 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491592 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471593
1594 // Repair broken |kPath1|.
1595 context.reset(NewContext(NULL));
1596 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491597 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471598 base::Time::Now()));
1599 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491600 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471601 EXPECT_TRUE(created);
1602
1603 // Copy from sound |kPath1| to broken |kPath2|.
1604 context.reset(NewContext(NULL));
1605 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491606 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501607 true /* copy */));
[email protected]34583332011-08-31 08:59:471608
[email protected]7878ece2011-09-05 11:41:491609 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471610 context.reset(NewContext(NULL));
1611 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491612 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471613 context.get(), kPath1,
1614 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1615 &file, &created));
1616 EXPECT_TRUE(created);
1617 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1618 EXPECT_EQ(0, file_info.size);
1619 EXPECT_TRUE(base::ClosePlatformFile(file));
1620}
[email protected]9dfdc0e32011-09-02 06:07:441621
[email protected]7878ece2011-09-05 11:41:491622TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091623 const FileSystemURL kPath[] = {
1624 CreateURLFromUTF8("foo"),
1625 CreateURLFromUTF8("bar"),
1626 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441627 };
[email protected]a3ef4832013-02-02 05:12:331628 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441629 scoped_ptr<FileSystemOperationContext> context;
1630
1631 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1632 bool created = false;
1633 context.reset(NewContext(NULL));
1634 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491635 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441636 EXPECT_TRUE(created);
1637 }
1638
1639 context.reset(NewContext(NULL));
1640 std::vector<base::FileUtilProxy::Entry> entries;
1641 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551642 FileUtilHelper::ReadDirectory(
1643 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441644 EXPECT_EQ(3u, entries.size());
1645
1646 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:331647 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441648 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491649 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441650 EXPECT_TRUE(file_util::Delete(local_path, false));
1651
1652 context.reset(NewContext(NULL));
1653 entries.clear();
1654 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551655 FileUtilHelper::ReadDirectory(
1656 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441657 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1658}
[email protected]fad625e2f2011-12-08 05:38:031659
1660TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1661 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091662 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031663
1664 // Create working directory.
1665 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091666 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031667
1668 // EnsureFileExists, create case.
[email protected]949f25a2012-06-27 01:53:091669 FileSystemURL url(dir_url.WithPath(
1670 dir_url.path().AppendASCII("EnsureFileExists_file")));
[email protected]fad625e2f2011-12-08 05:38:031671 bool created = false;
[email protected]949f25a2012-06-27 01:53:091672 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031673 context.reset(NewContext(NULL));
1674 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091675 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031676 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091677 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031678
1679 // non create case.
1680 created = true;
[email protected]949f25a2012-06-27 01:53:091681 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031682 context.reset(NewContext(NULL));
1683 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091684 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031685 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091686 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031687
1688 // fail case.
[email protected]949f25a2012-06-27 01:53:091689 url = dir_url.WithPath(dir_url.path().AppendASCII("EnsureFileExists_dir"));
[email protected]fad625e2f2011-12-08 05:38:031690 context.reset(NewContext(NULL));
1691 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091692 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031693
[email protected]949f25a2012-06-27 01:53:091694 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031695 context.reset(NewContext(NULL));
1696 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091697 ofu()->EnsureFileExists(context.get(), url, &created));
1698 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031699
1700 // CreateOrOpen, create case.
[email protected]949f25a2012-06-27 01:53:091701 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateOrOpen_file"));
[email protected]403ada82013-01-08 07:51:391702 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031703 created = false;
[email protected]949f25a2012-06-27 01:53:091704 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031705 context.reset(NewContext(NULL));
1706 EXPECT_EQ(base::PLATFORM_FILE_OK,
1707 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091708 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031709 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1710 &file_handle, &created));
1711 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1712 EXPECT_TRUE(created);
1713 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091714 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031715
1716 // open case.
1717 file_handle = base::kInvalidPlatformFileValue;
1718 created = true;
[email protected]949f25a2012-06-27 01:53:091719 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031720 context.reset(NewContext(NULL));
1721 EXPECT_EQ(base::PLATFORM_FILE_OK,
1722 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091723 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031724 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1725 &file_handle, &created));
1726 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1727 EXPECT_FALSE(created);
1728 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091729 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031730
1731 // fail case
1732 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091733 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031734 context.reset(NewContext(NULL));
1735 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1736 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091737 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031738 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1739 &file_handle, &created));
1740 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091741 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031742
1743 // CreateDirectory, create case.
1744 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]949f25a2012-06-27 01:53:091745 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir"));
1746 FileSystemURL subdir_url(url.WithPath(url.path().AppendASCII("subdir")));
1747 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031748 context.reset(NewContext(NULL));
1749 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091750 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031751 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091752 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031753
1754 // create subdir case.
1755 // Creating CreateDirectory_dir/subdir2.
[email protected]949f25a2012-06-27 01:53:091756 subdir_url = url.WithPath(url.path().AppendASCII("subdir2"));
1757 ClearTimestamp(dir_url);
1758 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031759 context.reset(NewContext(NULL));
1760 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091761 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031762 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091763 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1764 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031765
1766 // fail case.
[email protected]949f25a2012-06-27 01:53:091767 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir"));
1768 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031769 context.reset(NewContext(NULL));
1770 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091771 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031772 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091773 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031774
1775 // CopyInForeignFile, create case.
[email protected]949f25a2012-06-27 01:53:091776 url = dir_url.WithPath(dir_url.path().AppendASCII("CopyInForeignFile_file"));
1777 FileSystemURL src_path = dir_url.WithPath(
1778 dir_url.path().AppendASCII("CopyInForeignFile_src_file"));
[email protected]fad625e2f2011-12-08 05:38:031779 context.reset(NewContext(NULL));
1780 EXPECT_EQ(base::PLATFORM_FILE_OK,
1781 ofu()->EnsureFileExists(context.get(), src_path, &created));
1782 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331783 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031784 context.reset(NewContext(NULL));
1785 EXPECT_EQ(base::PLATFORM_FILE_OK,
1786 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1787
[email protected]949f25a2012-06-27 01:53:091788 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031789 context.reset(NewContext(NULL));
1790 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501791 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131792 src_local_path,
[email protected]949f25a2012-06-27 01:53:091793 url));
1794 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031795}
1796
1797TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1798 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091799 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031800
1801 // Create working directory.
1802 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091803 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031804
1805 // DeleteFile, delete case.
[email protected]949f25a2012-06-27 01:53:091806 FileSystemURL url = dir_url.WithPath(
1807 dir_url.path().AppendASCII("DeleteFile_file"));
[email protected]fad625e2f2011-12-08 05:38:031808 bool created = false;
1809 context.reset(NewContext(NULL));
1810 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091811 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031812 EXPECT_TRUE(created);
1813
[email protected]949f25a2012-06-27 01:53:091814 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031815 context.reset(NewContext(NULL));
1816 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091817 ofu()->DeleteFile(context.get(), url));
1818 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031819
1820 // fail case.
[email protected]949f25a2012-06-27 01:53:091821 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031822 context.reset(NewContext(NULL));
1823 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091824 ofu()->DeleteFile(context.get(), url));
1825 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031826
[email protected]bab213be2013-01-23 15:13:081827 // DeleteDirectory, fail case.
1828 url = dir_url.WithPath(dir_url.path().AppendASCII("DeleteDirectory_dir"));
[email protected]949f25a2012-06-27 01:53:091829 FileSystemURL file_path(url.WithPath(url.path().AppendASCII("pakeratta")));
[email protected]fad625e2f2011-12-08 05:38:031830 context.reset(NewContext(NULL));
1831 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091832 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031833 created = false;
1834 context.reset(NewContext(NULL));
1835 EXPECT_EQ(base::PLATFORM_FILE_OK,
1836 ofu()->EnsureFileExists(context.get(), file_path, &created));
1837 EXPECT_TRUE(created);
1838
[email protected]949f25a2012-06-27 01:53:091839 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031840 context.reset(NewContext(NULL));
1841 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081842 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091843 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031844
1845 // delete case.
1846 context.reset(NewContext(NULL));
1847 EXPECT_EQ(base::PLATFORM_FILE_OK,
1848 ofu()->DeleteFile(context.get(), file_path));
1849
[email protected]949f25a2012-06-27 01:53:091850 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031851 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081852 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091853 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031854}
1855
1856TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1857 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091858 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031859 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091860 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031861 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091862 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031863 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091864 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031865}
[email protected]a3938912012-03-27 14:00:551866
1867TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091868 FileSystemURL dir = CreateURLFromUTF8("foo");
1869 FileSystemURL url1 = dir.WithPath(dir.path().AppendASCII("bar"));
1870 FileSystemURL url2 = dir.WithPath(dir.path().AppendASCII("baz"));
[email protected]a3938912012-03-27 14:00:551871
1872 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1873 EXPECT_EQ(base::PLATFORM_FILE_OK,
1874 ofu()->CreateDirectory(context.get(), dir, false, false));
1875
1876 bool created = false;
1877 context.reset(NewContext(NULL));
1878 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091879 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551880 EXPECT_TRUE(created);
1881
1882 context.reset(NewContext(NULL));
1883 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091884 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551885
[email protected]a3ef4832013-02-02 05:12:331886 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551887 context.reset(NewContext(NULL));
1888 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091889 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551890 EXPECT_FALSE(file_path.empty());
1891
1892 context.reset(NewContext(NULL));
1893 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091894 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551895 base::Time::Now() + base::TimeDelta::FromHours(1),
1896 base::Time()));
1897
1898 context.reset(NewContext(NULL));
1899 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1900 ofu()->CreateFileEnumerator(context.get(), dir, false));
1901
1902 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331903 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551904 while (!(file_path_each = file_enum->Next()).empty()) {
1905 context.reset(NewContext(NULL));
1906 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331907 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551908 EXPECT_EQ(base::PLATFORM_FILE_OK,
1909 ofu()->GetFileInfo(context.get(),
[email protected]949f25a2012-06-27 01:53:091910 dir.WithPath(file_path_each),
[email protected]a3938912012-03-27 14:00:551911 &file_info, &file_path));
1912 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1913 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1914 EXPECT_EQ(file_info.size, file_enum->Size());
1915 ++count;
1916 }
1917 EXPECT_EQ(2, count);
1918}
[email protected]294dd0312012-05-11 07:35:131919
1920TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091921 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1922 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1923 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1924 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131925 bool created;
1926
1927 int64 expected_total_file_size = 0;
1928 ASSERT_EQ(base::PLATFORM_FILE_OK,
1929 ofu()->EnsureFileExists(
1930 AllowUsageIncrease(PathCost(from_file))->context(),
1931 from_file, &created));
1932 ASSERT_TRUE(created);
1933 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1934
1935 ASSERT_EQ(base::PLATFORM_FILE_OK,
1936 ofu()->EnsureFileExists(
1937 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1938 obstacle_file, &created));
1939 ASSERT_TRUE(created);
1940 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1941
1942 int64 from_file_size = 1020;
1943 expected_total_file_size += from_file_size;
1944 ASSERT_EQ(base::PLATFORM_FILE_OK,
1945 ofu()->Truncate(
1946 AllowUsageIncrease(from_file_size)->context(),
1947 from_file, from_file_size));
1948 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1949
1950 int64 obstacle_file_size = 1;
1951 expected_total_file_size += obstacle_file_size;
1952 ASSERT_EQ(base::PLATFORM_FILE_OK,
1953 ofu()->Truncate(
1954 AllowUsageIncrease(obstacle_file_size)->context(),
1955 obstacle_file, obstacle_file_size));
1956 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1957
1958 int64 to_file1_size = from_file_size;
1959 expected_total_file_size += to_file1_size;
1960 ASSERT_EQ(base::PLATFORM_FILE_OK,
1961 ofu()->CopyOrMoveFile(
1962 AllowUsageIncrease(
1963 PathCost(to_file1) + to_file1_size)->context(),
1964 from_file, to_file1, true /* copy */));
1965 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1966
1967 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1968 ofu()->CopyOrMoveFile(
1969 DisallowUsageIncrease(
1970 PathCost(to_file2) + from_file_size)->context(),
1971 from_file, to_file2, true /* copy */));
1972 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1973
1974 int64 old_obstacle_file_size = obstacle_file_size;
1975 obstacle_file_size = from_file_size;
1976 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1977 ASSERT_EQ(base::PLATFORM_FILE_OK,
1978 ofu()->CopyOrMoveFile(
1979 AllowUsageIncrease(
1980 obstacle_file_size - old_obstacle_file_size)->context(),
1981 from_file, obstacle_file, true /* copy */));
1982 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1983
1984 int64 old_from_file_size = from_file_size;
1985 from_file_size = old_from_file_size - 1;
1986 expected_total_file_size += from_file_size - old_from_file_size;
1987 ASSERT_EQ(base::PLATFORM_FILE_OK,
1988 ofu()->Truncate(
1989 AllowUsageIncrease(
1990 from_file_size - old_from_file_size)->context(),
1991 from_file, from_file_size));
1992 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1993
1994 // quota exceeded
1995 {
1996 old_obstacle_file_size = obstacle_file_size;
1997 obstacle_file_size = from_file_size;
1998 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1999 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2000 obstacle_file_size - old_obstacle_file_size);
2001 helper->context()->set_allowed_bytes_growth(
2002 helper->context()->allowed_bytes_growth() - 1);
2003 ASSERT_EQ(base::PLATFORM_FILE_OK,
2004 ofu()->CopyOrMoveFile(
2005 helper->context(),
2006 from_file, obstacle_file, true /* copy */));
2007 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2008 }
2009}
2010
2011TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092012 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2013 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2014 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132015 bool created;
2016
2017 int64 expected_total_file_size = 0;
2018 ASSERT_EQ(base::PLATFORM_FILE_OK,
2019 ofu()->EnsureFileExists(
2020 AllowUsageIncrease(PathCost(from_file))->context(),
2021 from_file, &created));
2022 ASSERT_TRUE(created);
2023 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2024
2025 int64 from_file_size = 1020;
2026 expected_total_file_size += from_file_size;
2027 ASSERT_EQ(base::PLATFORM_FILE_OK,
2028 ofu()->Truncate(
2029 AllowUsageIncrease(from_file_size)->context(),
2030 from_file, from_file_size));
2031 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2032
2033 int64 to_file_size ALLOW_UNUSED = from_file_size;
2034 from_file_size = 0;
2035 ASSERT_EQ(base::PLATFORM_FILE_OK,
2036 ofu()->CopyOrMoveFile(
2037 AllowUsageIncrease(-PathCost(from_file) +
2038 PathCost(to_file))->context(),
2039 from_file, to_file, false /* move */));
2040 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2041
2042 ASSERT_EQ(base::PLATFORM_FILE_OK,
2043 ofu()->EnsureFileExists(
2044 AllowUsageIncrease(PathCost(from_file))->context(),
2045 from_file, &created));
2046 ASSERT_TRUE(created);
2047 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2048
2049 ASSERT_EQ(base::PLATFORM_FILE_OK,
2050 ofu()->EnsureFileExists(
2051 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2052 obstacle_file, &created));
2053 ASSERT_TRUE(created);
2054 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2055
2056 from_file_size = 1020;
2057 expected_total_file_size += from_file_size;
2058 ASSERT_EQ(base::PLATFORM_FILE_OK,
2059 ofu()->Truncate(
2060 AllowUsageIncrease(from_file_size)->context(),
2061 from_file, from_file_size));
2062 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2063
2064 int64 obstacle_file_size = 1;
2065 expected_total_file_size += obstacle_file_size;
2066 ASSERT_EQ(base::PLATFORM_FILE_OK,
2067 ofu()->Truncate(
2068 AllowUsageIncrease(1)->context(),
2069 obstacle_file, obstacle_file_size));
2070 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2071
2072 int64 old_obstacle_file_size = obstacle_file_size;
2073 obstacle_file_size = from_file_size;
2074 from_file_size = 0;
2075 expected_total_file_size -= old_obstacle_file_size;
2076 ASSERT_EQ(base::PLATFORM_FILE_OK,
2077 ofu()->CopyOrMoveFile(
2078 AllowUsageIncrease(
2079 -old_obstacle_file_size - PathCost(from_file))->context(),
2080 from_file, obstacle_file,
2081 false /* move */));
2082 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2083
2084 ASSERT_EQ(base::PLATFORM_FILE_OK,
2085 ofu()->EnsureFileExists(
2086 AllowUsageIncrease(PathCost(from_file))->context(),
2087 from_file, &created));
2088 ASSERT_TRUE(created);
2089 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2090
2091 from_file_size = 10;
2092 expected_total_file_size += from_file_size;
2093 ASSERT_EQ(base::PLATFORM_FILE_OK,
2094 ofu()->Truncate(
2095 AllowUsageIncrease(from_file_size)->context(),
2096 from_file, from_file_size));
2097 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2098
2099 // quota exceeded even after operation
2100 old_obstacle_file_size = obstacle_file_size;
2101 obstacle_file_size = from_file_size;
2102 from_file_size = 0;
2103 expected_total_file_size -= old_obstacle_file_size;
2104 scoped_ptr<FileSystemOperationContext> context =
2105 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2106 ASSERT_EQ(base::PLATFORM_FILE_OK,
2107 ofu()->CopyOrMoveFile(
2108 context.get(), from_file, obstacle_file, false /* move */));
2109 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2110 context.reset();
2111}
2112
2113TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092114 FileSystemURL dir(CreateURLFromUTF8("dir"));
2115 FileSystemURL file(CreateURLFromUTF8("file"));
2116 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2117 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132118 bool created;
2119
2120 ASSERT_EQ(base::PLATFORM_FILE_OK,
2121 ofu()->EnsureFileExists(
2122 AllowUsageIncrease(PathCost(file))->context(),
2123 file, &created));
2124 ASSERT_TRUE(created);
2125 ASSERT_EQ(0, ComputeTotalFileSize());
2126
2127 ASSERT_EQ(base::PLATFORM_FILE_OK,
2128 ofu()->CreateDirectory(
2129 AllowUsageIncrease(PathCost(dir))->context(),
2130 dir, false, false));
2131 ASSERT_EQ(0, ComputeTotalFileSize());
2132
2133 ASSERT_EQ(base::PLATFORM_FILE_OK,
2134 ofu()->EnsureFileExists(
2135 AllowUsageIncrease(PathCost(dfile1))->context(),
2136 dfile1, &created));
2137 ASSERT_TRUE(created);
2138 ASSERT_EQ(0, ComputeTotalFileSize());
2139
2140 ASSERT_EQ(base::PLATFORM_FILE_OK,
2141 ofu()->EnsureFileExists(
2142 AllowUsageIncrease(PathCost(dfile2))->context(),
2143 dfile2, &created));
2144 ASSERT_TRUE(created);
2145 ASSERT_EQ(0, ComputeTotalFileSize());
2146
2147 ASSERT_EQ(base::PLATFORM_FILE_OK,
2148 ofu()->Truncate(
2149 AllowUsageIncrease(340)->context(),
2150 file, 340));
2151 ASSERT_EQ(340, ComputeTotalFileSize());
2152
2153 ASSERT_EQ(base::PLATFORM_FILE_OK,
2154 ofu()->Truncate(
2155 AllowUsageIncrease(1020)->context(),
2156 dfile1, 1020));
2157 ASSERT_EQ(1360, ComputeTotalFileSize());
2158
2159 ASSERT_EQ(base::PLATFORM_FILE_OK,
2160 ofu()->Truncate(
2161 AllowUsageIncrease(120)->context(),
2162 dfile2, 120));
2163 ASSERT_EQ(1480, ComputeTotalFileSize());
2164
2165 ASSERT_EQ(base::PLATFORM_FILE_OK,
2166 ofu()->DeleteFile(
2167 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2168 file));
2169 ASSERT_EQ(1140, ComputeTotalFileSize());
2170
2171 ASSERT_EQ(base::PLATFORM_FILE_OK,
2172 FileUtilHelper::Delete(
2173 AllowUsageIncrease(-PathCost(dir) -
2174 PathCost(dfile1) -
2175 PathCost(dfile2) -
2176 1020 - 120)->context(),
2177 ofu(), dir, true));
2178 ASSERT_EQ(0, ComputeTotalFileSize());
2179}
[email protected]7d78be12012-05-24 07:07:262180
2181TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092182 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262183 base::PlatformFile file_handle;
2184 bool created;
2185
2186 // Creating a file.
2187 ASSERT_EQ(base::PLATFORM_FILE_OK,
2188 ofu()->EnsureFileExists(
2189 AllowUsageIncrease(PathCost(file))->context(),
2190 file, &created));
2191 ASSERT_TRUE(created);
2192 ASSERT_EQ(0, ComputeTotalFileSize());
2193
2194 // Opening it, which shouldn't change the usage.
2195 ASSERT_EQ(base::PLATFORM_FILE_OK,
2196 ofu()->CreateOrOpen(
2197 AllowUsageIncrease(0)->context(), file,
2198 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2199 &file_handle, &created));
2200 ASSERT_EQ(0, ComputeTotalFileSize());
2201 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2202
2203 const int length = 33;
2204 ASSERT_EQ(base::PLATFORM_FILE_OK,
2205 ofu()->Truncate(
2206 AllowUsageIncrease(length)->context(), file, length));
2207 ASSERT_EQ(length, ComputeTotalFileSize());
2208
2209 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2210 ASSERT_EQ(base::PLATFORM_FILE_OK,
2211 ofu()->CreateOrOpen(
2212 AllowUsageIncrease(-length)->context(), file,
2213 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2214 &file_handle, &created));
2215 ASSERT_EQ(0, ComputeTotalFileSize());
2216 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2217
2218 // Extending the file again.
2219 ASSERT_EQ(base::PLATFORM_FILE_OK,
2220 ofu()->Truncate(
2221 AllowUsageIncrease(length)->context(), file, length));
2222 ASSERT_EQ(length, ComputeTotalFileSize());
2223
2224 // Opening it with TRUNCATED flag, which should truncate the file size.
2225 ASSERT_EQ(base::PLATFORM_FILE_OK,
2226 ofu()->CreateOrOpen(
2227 AllowUsageIncrease(-length)->context(), file,
2228 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2229 &file_handle, &created));
2230 ASSERT_EQ(0, ComputeTotalFileSize());
2231 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2232}
[email protected]c4e6f9c2012-09-09 17:42:102233
2234} // namespace fileapi