blob: de5cd501a4150aeffdb5659ce3f78083203f3fe7 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d4905e2e2011-05-13 21:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d4905e2e2011-05-13 21:56:325#include <set>
6#include <string>
[email protected]403ada82013-01-08 07:51:397#include <vector>
[email protected]d4905e2e2011-05-13 21:56:328
[email protected]4d99be52011-10-18 14:11:039#include "base/bind.h"
[email protected]d4905e2e2011-05-13 21:56:3210#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:2312#include "base/files/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3213#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1614#include "base/message_loop.h"
[email protected]d4905e2e2011-05-13 21:56:3215#include "base/platform_file.h"
[email protected]d4905e2e2011-05-13 21:56:3216#include "testing/gtest/include/gtest/gtest.h"
[email protected]07b64872013-02-13 11:46:3017#include "webkit/fileapi/async_file_test_helper.h"
[email protected]6ef0c3912013-01-25 22:46:3418#include "webkit/fileapi/external_mount_points.h"
[email protected]d4905e2e2011-05-13 21:56:3219#include "webkit/fileapi/file_system_context.h"
[email protected]29eb12c2013-05-17 17:03:3920#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]d4905e2e2011-05-13 21:56:3221#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1022#include "webkit/fileapi/file_system_task_runners.h"
[email protected]0c5ebe32011-08-19 22:37:1623#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]02a60542012-07-24 20:05:3324#include "webkit/fileapi/local_file_system_test_helper.h"
[email protected]c4e6f9c2012-09-09 17:42:1025#include "webkit/fileapi/mock_file_change_observer.h"
[email protected]420fb562013-04-18 01:46:3426#include "webkit/fileapi/mock_file_system_context.h"
[email protected]7878ece2011-09-05 11:41:4927#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5628#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1629#include "webkit/quota/mock_special_storage_policy.h"
30#include "webkit/quota/quota_manager.h"
31#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3232
[email protected]c4e6f9c2012-09-09 17:42:1033namespace fileapi {
[email protected]d4905e2e2011-05-13 21:56:3234
35namespace {
36
[email protected]a3ef4832013-02-02 05:12:3337bool FileExists(const base::FilePath& path) {
[email protected]d4905e2e2011-05-13 21:56:3238 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
39}
40
[email protected]a3ef4832013-02-02 05:12:3341int64 GetSize(const base::FilePath& path) {
[email protected]81b7f662011-05-26 00:54:4642 int64 size;
43 EXPECT_TRUE(file_util::GetFileSize(path, &size));
44 return size;
45}
46
[email protected]d4905e2e2011-05-13 21:56:3247// After a move, the dest exists and the source doesn't.
48// After a copy, both source and dest exist.
49struct CopyMoveTestCaseRecord {
50 bool is_copy_not_move;
51 const char source_path[64];
52 const char dest_path[64];
53 bool cause_overwrite;
54};
55
56const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
57 // This is the combinatoric set of:
58 // rename vs. same-name
59 // different directory vs. same directory
60 // overwrite vs. no-overwrite
61 // copy vs. move
62 // We can never be called with source and destination paths identical, so
63 // those cases are omitted.
64 {true, "dir0/file0", "dir0/file1", false},
65 {false, "dir0/file0", "dir0/file1", false},
66 {true, "dir0/file0", "dir0/file1", true},
67 {false, "dir0/file0", "dir0/file1", true},
68
69 {true, "dir0/file0", "dir1/file0", false},
70 {false, "dir0/file0", "dir1/file0", false},
71 {true, "dir0/file0", "dir1/file0", true},
72 {false, "dir0/file0", "dir1/file0", true},
73 {true, "dir0/file0", "dir1/file1", false},
74 {false, "dir0/file0", "dir1/file1", false},
75 {true, "dir0/file0", "dir1/file1", true},
76 {false, "dir0/file0", "dir1/file1", true},
77};
78
[email protected]fcc2d5f2011-05-23 22:06:2679struct OriginEnumerationTestRecord {
80 std::string origin_url;
81 bool has_temporary;
82 bool has_persistent;
83};
84
85const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
86 {"http://example.com", false, true},
87 {"http://example1.com", true, false},
88 {"https://example1.com", true, true},
89 {"file://", false, true},
90 {"http://example.com:8000", false, true},
91};
92
[email protected]04c899f2013-02-08 08:28:4293FileSystemURL FileSystemURLAppend(
[email protected]023ad6ab2013-02-17 05:07:2394 const FileSystemURL& url, const base::FilePath::StringType& child) {
[email protected]04c899f2013-02-08 08:28:4295 return FileSystemURL::CreateForTest(
96 url.origin(), url.mount_type(), url.virtual_path().Append(child));
97}
98
99FileSystemURL FileSystemURLAppendUTF8(
100 const FileSystemURL& url, const std::string& child) {
101 return FileSystemURL::CreateForTest(
102 url.origin(),
103 url.mount_type(),
[email protected]023ad6ab2013-02-17 05:07:23104 url.virtual_path().Append(base::FilePath::FromUTF8Unsafe(child)));
[email protected]04c899f2013-02-08 08:28:42105}
106
107FileSystemURL FileSystemURLDirName(const FileSystemURL& url) {
108 return FileSystemURL::CreateForTest(
[email protected]8a020f62013-02-18 08:05:44109 url.origin(), url.mount_type(), VirtualPath::DirName(url.virtual_path()));
[email protected]04c899f2013-02-08 08:28:42110}
111
[email protected]d4905e2e2011-05-13 21:56:32112} // namespace (anonymous)
113
114// TODO(ericu): The vast majority of this and the other FSFU subclass tests
115// could theoretically be shared. It would basically be a FSFU interface
116// compliance test, and only the subclass-specific bits that look into the
117// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49118class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32119 public:
[email protected]7878ece2011-09-05 11:41:49120 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26121 : origin_(GURL("http://www.example.com")),
122 type_(kFileSystemTypeTemporary),
[email protected]e0b9dda2013-04-30 01:05:43123 weak_factory_(this),
[email protected]0c5ebe32011-08-19 22:37:16124 test_helper_(origin_, type_),
125 quota_status_(quota::kQuotaStatusUnknown),
126 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32127 }
128
[email protected]7fd8fa4f2013-02-07 05:43:50129 virtual void SetUp() {
[email protected]d4905e2e2011-05-13 21:56:32130 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
131
[email protected]e7e46732012-01-05 11:45:55132 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
133 new quota::MockSpecialStoragePolicy();
134
[email protected]0c5ebe32011-08-19 22:37:16135 quota_manager_ = new quota::QuotaManager(
136 false /* is_incognito */,
137 data_dir_.path(),
138 base::MessageLoopProxy::current(),
139 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55140 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16141
142 // Every time we create a new helper, it creates another context, which
143 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49144 // another OFU. We need to pass in the context to skip all that.
[email protected]420fb562013-04-18 01:46:34145 file_system_context_ = CreateFileSystemContextForTesting(
[email protected]0c5ebe32011-08-19 22:37:16146 quota_manager_->proxy(),
[email protected]420fb562013-04-18 01:46:34147 data_dir_.path());
[email protected]0c5ebe32011-08-19 22:37:16148
[email protected]4f056a872013-01-23 04:24:36149 test_helper_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10150
151 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32152 }
153
[email protected]7fd8fa4f2013-02-07 05:43:50154 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55155 quota_manager_ = NULL;
156 test_helper_.TearDown();
157 }
158
[email protected]294dd0312012-05-11 07:35:13159 scoped_ptr<FileSystemOperationContext> LimitedContext(
160 int64 allowed_bytes_growth) {
161 scoped_ptr<FileSystemOperationContext> context(
162 test_helper_.NewOperationContext());
163 context->set_allowed_bytes_growth(allowed_bytes_growth);
164 return context.Pass();
165 }
166
167 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
168 return LimitedContext(kint64max);
169 }
170
[email protected]02a60542012-07-24 20:05:33171 FileSystemOperationContext* NewContext(
172 LocalFileSystemTestOriginHelper* helper) {
[email protected]c4e6f9c2012-09-09 17:42:10173 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16174 FileSystemOperationContext* context;
175 if (helper)
176 context = helper->NewOperationContext();
177 else
178 context = test_helper_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46179 // Setting allowed_bytes_growth big enough for all tests.
180 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10181 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32182 return context;
183 }
184
[email protected]c4e6f9c2012-09-09 17:42:10185 const ChangeObserverList& change_observers() const {
186 return change_observers_;
187 }
188
189 MockFileChangeObserver* change_observer() {
190 return &change_observer_;
191 }
192
[email protected]0c5ebe32011-08-19 22:37:16193 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49194 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16195 // Use this for tests which need to run in multiple origins; we need a test
196 // helper per origin.
[email protected]02a60542012-07-24 20:05:33197 LocalFileSystemTestOriginHelper* NewHelper(
[email protected]0c5ebe32011-08-19 22:37:16198 const GURL& origin, fileapi::FileSystemType type) {
[email protected]02a60542012-07-24 20:05:33199 LocalFileSystemTestOriginHelper* helper =
200 new LocalFileSystemTestOriginHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16201
[email protected]4f056a872013-01-23 04:24:36202 helper->SetUp(file_system_context_.get());
[email protected]0c5ebe32011-08-19 22:37:16203 return helper;
204 }
205
[email protected]7878ece2011-09-05 11:41:49206 ObfuscatedFileUtil* ofu() {
[email protected]4f056a872013-01-23 04:24:36207 return static_cast<ObfuscatedFileUtil*>(test_helper_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32208 }
209
[email protected]a3ef4832013-02-02 05:12:33210 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35211 return data_dir_.path();
212 }
213
[email protected]0c5ebe32011-08-19 22:37:16214 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26215 return origin_;
216 }
217
218 fileapi::FileSystemType type() const {
219 return type_;
220 }
221
[email protected]294dd0312012-05-11 07:35:13222 int64 ComputeTotalFileSize() {
223 return test_helper_.ComputeCurrentOriginUsage() -
224 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
225 }
226
[email protected]0c5ebe32011-08-19 22:37:16227 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30228 int64 quota = -1;
229 quota_status_ = AsyncFileTestHelper::GetUsageAndQuota(
230 quota_manager_, origin(), test_helper_.type(),
231 &usage_, &quota);
[email protected]0c5ebe32011-08-19 22:37:16232 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
233 }
234
235 void RevokeUsageCache() {
236 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]06226172013-03-14 15:39:31237 usage_cache()->Delete(test_helper_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26238 }
239
240 int64 SizeByQuotaUtil() {
241 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16242 }
243
244 int64 SizeInUsageFile() {
[email protected]4cc586b2013-05-07 12:43:32245 base::MessageLoop::current()->RunUntilIdle();
[email protected]bf97e8b2013-04-14 15:00:13246 int64 usage = 0;
247 return usage_cache()->GetUsage(test_helper_.GetUsageCachePath(), &usage) ?
248 usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16249 }
250
[email protected]13f92f6e2012-08-13 07:39:14251 bool PathExists(const FileSystemURL& url) {
252 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21253 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33254 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21255 base::PlatformFileError error = ofu()->GetFileInfo(
256 context.get(), url, &file_info, &platform_path);
257 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14258 }
259
260 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30261 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14262 }
263
[email protected]0c5ebe32011-08-19 22:37:16264 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31265 FileSystemUsageCache* usage_cache() {
266 return test_helper_.usage_cache();
267 }
[email protected]0c5ebe32011-08-19 22:37:16268
[email protected]949f25a2012-06-27 01:53:09269 FileSystemURL CreateURLFromUTF8(const std::string& path) {
270 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50271 }
272
[email protected]949f25a2012-06-27 01:53:09273 int64 PathCost(const FileSystemURL& url) {
274 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13275 }
276
[email protected]a3ef4832013-02-02 05:12:33277 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]949f25a2012-06-27 01:53:09278 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50279 }
280
[email protected]d4905e2e2011-05-13 21:56:32281 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39282 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16283 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33284 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49285 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09286 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32287
288 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33289 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49290 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09291 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32292 EXPECT_EQ(data_path, local_path);
293 EXPECT_TRUE(FileExists(data_path));
294 EXPECT_EQ(0, GetSize(data_path));
295
296 const char data[] = "test data";
297 const int length = arraysize(data) - 1;
298
299 if (base::kInvalidPlatformFileValue == file_handle) {
300 bool created = true;
[email protected]403ada82013-01-08 07:51:39301 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32302 file_handle = base::CreatePlatformFile(
303 data_path,
304 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
305 &created,
306 &error);
[email protected]81b7f662011-05-26 00:54:46307 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32308 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
309 EXPECT_FALSE(created);
310 }
311 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
312 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
313
314 base::PlatformFileInfo file_info1;
315 EXPECT_EQ(length, GetSize(data_path));
[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()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09318 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32319 EXPECT_EQ(data_path, local_path);
320
321 EXPECT_FALSE(file_info0.is_directory);
322 EXPECT_FALSE(file_info1.is_directory);
323 EXPECT_FALSE(file_info0.is_symbolic_link);
324 EXPECT_FALSE(file_info1.is_symbolic_link);
325 EXPECT_EQ(0, file_info0.size);
326 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32327 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32328
[email protected]0c5ebe32011-08-19 22:37:16329 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49330 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09331 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32332 EXPECT_EQ(length * 2, GetSize(data_path));
333
[email protected]0c5ebe32011-08-19 22:37:16334 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49335 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09336 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16337 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32338 }
339
340 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09341 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33342 const std::set<base::FilePath::StringType>& files,
343 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32344 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33345 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32346 for (iter = files.begin(); iter != files.end(); ++iter) {
347 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16348 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32349 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49350 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42351 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32352 &created));
353 ASSERT_FALSE(created);
354 }
355 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16356 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14357 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42358 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32359 }
360 }
361
[email protected]294dd0312012-05-11 07:35:13362 class UsageVerifyHelper {
363 public:
364 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33365 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13366 int64 expected_usage)
367 : context_(context.Pass()),
368 test_helper_(test_helper),
369 expected_usage_(expected_usage) {}
370
371 ~UsageVerifyHelper() {
[email protected]4cc586b2013-05-07 12:43:32372 base::MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13373 Check();
374 }
375
376 FileSystemOperationContext* context() {
377 return context_.get();
378 }
379
380 private:
381 void Check() {
382 ASSERT_EQ(expected_usage_,
383 test_helper_->GetCachedOriginUsage());
384 }
385
386 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33387 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13388 int64 expected_usage_;
389 };
390
391 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
392 int64 usage = test_helper_.GetCachedOriginUsage();
393 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
394 LimitedContext(requested_growth),
395 &test_helper_, usage + requested_growth));
396 }
397
398 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
399 int64 usage = test_helper_.GetCachedOriginUsage();
400 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
401 LimitedContext(requested_growth - 1), &test_helper_, usage));
402 }
403
[email protected]d4905e2e2011-05-13 21:56:32404 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09405 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33406 std::set<base::FilePath::StringType>* files,
407 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32408 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42409 std::vector<DirectoryEntry> entries;
[email protected]d4905e2e2011-05-13 21:56:32410 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30411 AsyncFileTestHelper::ReadDirectory(
412 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32413 EXPECT_EQ(0UL, entries.size());
414
415 files->clear();
[email protected]89ee4272011-05-16 18:45:17416 files->insert(FILE_PATH_LITERAL("first"));
417 files->insert(FILE_PATH_LITERAL("second"));
418 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32419 directories->clear();
[email protected]89ee4272011-05-16 18:45:17420 directories->insert(FILE_PATH_LITERAL("fourth"));
421 directories->insert(FILE_PATH_LITERAL("fifth"));
422 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33423 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32424 for (iter = files->begin(); iter != files->end(); ++iter) {
425 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16426 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32427 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49428 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50429 context.get(),
[email protected]04c899f2013-02-08 08:28:42430 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50431 &created));
[email protected]d4905e2e2011-05-13 21:56:32432 ASSERT_TRUE(created);
433 }
434 for (iter = directories->begin(); iter != directories->end(); ++iter) {
435 bool exclusive = true;
436 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16437 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32438 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49439 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42440 context.get(),
441 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09442 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32443 }
[email protected]949f25a2012-06-27 01:53:09444 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32445 }
446
[email protected]949f25a2012-06-27 01:53:09447 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33448 std::set<base::FilePath::StringType> files;
449 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09450 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32451
452 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42453 std::vector<DirectoryEntry> entries;
[email protected]0c5ebe32011-08-19 22:37:16454 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32455 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30456 AsyncFileTestHelper::ReadDirectory(
457 file_system_context(), root_url, &entries));
[email protected]c83118f2013-05-20 04:35:42458 std::vector<DirectoryEntry>::iterator entry_iter;
[email protected]d4905e2e2011-05-13 21:56:32459 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10460 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32461 for (entry_iter = entries.begin(); entry_iter != entries.end();
462 ++entry_iter) {
[email protected]c83118f2013-05-20 04:35:42463 const DirectoryEntry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31464 std::set<base::FilePath::StringType>::iterator iter =
465 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32466 if (iter != files.end()) {
467 EXPECT_FALSE(entry.is_directory);
468 files.erase(iter);
469 continue;
470 }
[email protected]89ee4272011-05-16 18:45:17471 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32472 EXPECT_FALSE(directories.end() == iter);
473 EXPECT_TRUE(entry.is_directory);
474 directories.erase(iter);
475 }
476 }
477
[email protected]949f25a2012-06-27 01:53:09478 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41479 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32480 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16481
[email protected]2517cfa2011-08-25 05:12:41482 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32483 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49484 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09485 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10486 // Currently we fire no change notifications for Touch.
487 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33488 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32489 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16490 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49491 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09492 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32493 // We compare as time_t here to lower our resolution, to avoid false
494 // negatives caused by conversion to the local filesystem's native
495 // representation and back.
496 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
497
[email protected]0c5ebe32011-08-19 22:37:16498 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32499 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41500 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32501 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49502 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09503 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10504 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16505 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49506 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09507 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32508 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49509 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41510 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32511 }
512
[email protected]81b7f662011-05-26 00:54:46513 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23514 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46515 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33516 base::FilePath root_file_path = source_dir.path();
517 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09518 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46519 int64 src_file_length = 87;
520
521 base::PlatformFileError error_code;
522 bool created = false;
523 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
524 base::PlatformFile file_handle =
525 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50526 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46527 EXPECT_TRUE(created);
528 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
529 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
530 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
531 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
532
533 scoped_ptr<FileSystemOperationContext> context;
534
535 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16536 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46537 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09538 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46539 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10540
541 // We must have observed one (and only one) create_file_count.
542 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
543 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46544 }
545
[email protected]0c5ebe32011-08-19 22:37:16546 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09547 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16548 if (!overwrite) {
549 // Verify that file creation requires sufficient quota for the path.
550 context.reset(NewContext(NULL));
551 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
552 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13553 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09554 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16555 }
556
557 context.reset(NewContext(NULL));
558 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46559 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13560 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09561 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16562
[email protected]13f92f6e2012-08-13 07:39:14563 EXPECT_TRUE(PathExists(dest_url));
564 EXPECT_FALSE(DirectoryExists(dest_url));
565
[email protected]0c5ebe32011-08-19 22:37:16566 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46567 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33568 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49569 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09570 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50571 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46572 EXPECT_TRUE(FileExists(data_path));
573 EXPECT_EQ(src_file_length, GetSize(data_path));
574
575 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09576 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46577 }
578
[email protected]949f25a2012-06-27 01:53:09579 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03580 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
581 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09582 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
583 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03584 }
585
[email protected]949f25a2012-06-27 01:53:09586 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03587 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33588 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03589 base::PlatformFileInfo file_info;
590 context.reset(NewContext(NULL));
591 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09592 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10593 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03594 return file_info.last_modified;
595 }
596
[email protected]949f25a2012-06-27 01:53:09597 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03598 bool copy,
599 bool overwrite) {
600 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42601 const FileSystemURL src_dir_url(
602 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
603 const FileSystemURL dest_dir_url(
604 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03605
[email protected]04c899f2013-02-08 08:28:42606 const FileSystemURL src_file_url(
607 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
608 const FileSystemURL dest_file_url(
609 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03610
611 context.reset(NewContext(NULL));
612 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09613 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03614 context.reset(NewContext(NULL));
615 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09616 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03617
618 bool created = false;
619 context.reset(NewContext(NULL));
620 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09621 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03622 if (overwrite) {
623 context.reset(NewContext(NULL));
624 EXPECT_EQ(base::PLATFORM_FILE_OK,
625 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09626 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03627 }
628
[email protected]949f25a2012-06-27 01:53:09629 ClearTimestamp(src_dir_url);
630 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03631 context.reset(NewContext(NULL));
632 EXPECT_EQ(base::PLATFORM_FILE_OK,
633 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09634 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03635 copy));
[email protected]fad625e2f2011-12-08 05:38:03636 if (copy)
[email protected]949f25a2012-06-27 01:53:09637 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03638 else
[email protected]949f25a2012-06-27 01:53:09639 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
640 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03641 }
642
[email protected]ecdfd6c52012-04-11 13:35:44643 int64 ComputeCurrentUsage() {
[email protected]06226172013-03-14 15:39:31644 return test_helper_.ComputeCurrentOriginUsage() -
645 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]ecdfd6c52012-04-11 13:35:44646 }
647
[email protected]02a60542012-07-24 20:05:33648 const LocalFileSystemTestOriginHelper& test_helper() const {
649 return test_helper_;
650 }
[email protected]45ea0fbbb2012-02-27 22:28:49651
[email protected]07b64872013-02-13 11:46:30652 FileSystemContext* file_system_context() {
653 return test_helper_.file_system_context();
654 }
655
[email protected]d4905e2e2011-05-13 21:56:32656 private:
[email protected]ea1a3f62012-11-16 20:34:23657 base::ScopedTempDir data_dir_;
[email protected]4cc586b2013-05-07 12:43:32658 base::MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16659 scoped_refptr<quota::QuotaManager> quota_manager_;
660 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26661 GURL origin_;
662 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03663 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]02a60542012-07-24 20:05:33664 LocalFileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16665 quota::QuotaStatusCode quota_status_;
666 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10667 MockFileChangeObserver change_observer_;
668 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32669
[email protected]7878ece2011-09-05 11:41:49670 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32671};
672
[email protected]7878ece2011-09-05 11:41:49673TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32674 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
675 bool created;
[email protected]949f25a2012-06-27 01:53:09676 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16677 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32678 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
679
680 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49681 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09682 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32683 &created));
684
[email protected]0c5ebe32011-08-19 22:37:16685 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32686 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09687 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32688
[email protected]949f25a2012-06-27 01:53:09689 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32690
[email protected]c4e6f9c2012-09-09 17:42:10691 EXPECT_TRUE(change_observer()->HasNoChange());
692
[email protected]0c5ebe32011-08-19 22:37:16693 // Verify that file creation requires sufficient quota for the path.
694 context.reset(NewContext(NULL));
695 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09696 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16697 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49698 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09699 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16700
701 context.reset(NewContext(NULL));
702 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09703 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32704 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49705 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09706 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32707 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10708 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32709 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
710
[email protected]949f25a2012-06-27 01:53:09711 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32712
[email protected]0c5ebe32011-08-19 22:37:16713 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33714 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49715 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09716 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32717 EXPECT_TRUE(file_util::PathExists(local_path));
718
[email protected]0c5ebe32011-08-19 22:37:16719 // Verify that deleting a file isn't stopped by zero quota, and that it frees
720 // up quote from its path.
721 context.reset(NewContext(NULL));
722 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32723 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09724 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10725 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32726 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09727 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16728 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32729
[email protected]0c5ebe32011-08-19 22:37:16730 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32731 bool exclusive = true;
732 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09733 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50734 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42735 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49736 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09737 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10738 // The oepration created 3 directories recursively.
739 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32740
[email protected]0c5ebe32011-08-19 22:37:16741 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32742 file_handle = base::kInvalidPlatformFileValue;
743 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49744 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09745 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32746 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10747 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32748 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
749
[email protected]949f25a2012-06-27 01:53:09750 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32751
[email protected]0c5ebe32011-08-19 22:37:16752 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49753 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09754 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32755 EXPECT_TRUE(file_util::PathExists(local_path));
756
[email protected]0c5ebe32011-08-19 22:37:16757 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32758 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09759 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10760 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32761 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10762
763 // Make sure we have no unexpected changes.
764 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32765}
766
[email protected]7878ece2011-09-05 11:41:49767TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32768 bool created = false;
[email protected]949f25a2012-06-27 01:53:09769 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16770 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32771
772 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09773 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32774
[email protected]0c5ebe32011-08-19 22:37:16775 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32776 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09777 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32778 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10779 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32780
[email protected]0c5ebe32011-08-19 22:37:16781 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33782 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49783 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09784 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32785 EXPECT_EQ(0, GetSize(local_path));
786
[email protected]0c5ebe32011-08-19 22:37:16787 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49788 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09789 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10790 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32791 EXPECT_EQ(10, GetSize(local_path));
792
[email protected]0c5ebe32011-08-19 22:37:16793 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49794 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09795 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32796 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10797 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32798
[email protected]13f92f6e2012-08-13 07:39:14799 EXPECT_FALSE(DirectoryExists(url));
800 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10801
802 // Make sure we have no unexpected changes.
803 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32804}
805
[email protected]ecdfd6c52012-04-11 13:35:44806TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
807 bool created = false;
[email protected]949f25a2012-06-27 01:53:09808 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44809
810 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13811 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09812 AllowUsageIncrease(PathCost(url))->context(),
813 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44814 ASSERT_TRUE(created);
[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 ASSERT_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 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13824 ofu()->Truncate(
825 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09826 url, 0));
[email protected]294dd0312012-05-11 07:35:13827 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44828
[email protected]ecdfd6c52012-04-11 13:35:44829 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13830 ofu()->Truncate(
831 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09832 url, 1021));
[email protected]294dd0312012-05-11 07:35:13833 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44834
[email protected]ecdfd6c52012-04-11 13:35:44835 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13836 ofu()->Truncate(
837 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09838 url, 1020));
[email protected]294dd0312012-05-11 07:35:13839 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44840
[email protected]ecdfd6c52012-04-11 13:35:44841 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13842 ofu()->Truncate(
843 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09844 url, 1020));
[email protected]294dd0312012-05-11 07:35:13845 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44846
[email protected]294dd0312012-05-11 07:35:13847 // quota exceeded
848 {
849 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
850 helper->context()->set_allowed_bytes_growth(
851 helper->context()->allowed_bytes_growth() - 1);
852 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09853 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13854 ASSERT_EQ(1019, ComputeTotalFileSize());
855 }
[email protected]ecdfd6c52012-04-11 13:35:44856
857 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33858 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44859 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13860 ofu()->GetLocalFilePath(
861 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09862 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44863 ASSERT_FALSE(local_path.empty());
864 ASSERT_TRUE(file_util::Delete(local_path, false));
865
[email protected]ecdfd6c52012-04-11 13:35:44866 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13867 ofu()->Truncate(
868 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09869 url, 1234));
[email protected]294dd0312012-05-11 07:35:13870 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44871}
872
[email protected]7878ece2011-09-05 11:41:49873TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09874 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32875 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16876 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32877 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49878 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09879 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10880 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32881
[email protected]0c5ebe32011-08-19 22:37:16882 // Verify that file creation requires sufficient quota for the path.
883 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09884 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32885 created = false;
[email protected]0c5ebe32011-08-19 22:37:16886 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09887 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16888 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09889 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16890 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10891 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16892
893 context.reset(NewContext(NULL));
894 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09895 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32896 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09897 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32898 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10899 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32900
[email protected]949f25a2012-06-27 01:53:09901 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[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_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10907 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32908
909 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09910 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16911 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32912 bool exclusive = true;
913 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49914 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09915 context.get(),
[email protected]04c899f2013-02-08 08:28:42916 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09917 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10918 // 2 directories: path/ and path/to.
919 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[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 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09923 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32924 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14925 EXPECT_FALSE(DirectoryExists(url));
926 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10927 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32928}
929
[email protected]7878ece2011-09-05 11:41:49930TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16931 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32932
933 bool exclusive = false;
934 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09935 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49936 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09937 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32938
[email protected]0c5ebe32011-08-19 22:37:16939 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32940 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08941 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32942
[email protected]007b3f82013-04-09 08:46:45943 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:14944 EXPECT_FALSE(DirectoryExists(url));
945 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16946 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49947 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32948
[email protected]0c5ebe32011-08-19 22:37:16949 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32950 exclusive = false;
951 recursive = true;
[email protected]7878ece2011-09-05 11:41:49952 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09953 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10954 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32955
[email protected]13f92f6e2012-08-13 07:39:14956 EXPECT_TRUE(DirectoryExists(url));
957 EXPECT_TRUE(PathExists(url));
958
[email protected]0c5ebe32011-08-19 22:37:16959 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49960 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42961 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14962
[email protected]0c5ebe32011-08-19 22:37:16963 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09964 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42965 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32966
967 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16968 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06969 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08970 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42971 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10972 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32973
974 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33975 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49976 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09977 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32978 EXPECT_TRUE(local_path.empty());
979 EXPECT_TRUE(file_info.is_directory);
980 EXPECT_FALSE(file_info.is_symbolic_link);
981
982 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16983 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49984 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09985 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10986 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32987
988 exclusive = true;
989 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16990 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49991 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09992 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10993 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32994
[email protected]0c5ebe32011-08-19 22:37:16995 // Verify that deleting a directory isn't stopped by zero quota, and that it
996 // frees up quota from its path.
997 context.reset(NewContext(NULL));
998 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:08999 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101000 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091001 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161002 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321003
[email protected]949f25a2012-06-27 01:53:091004 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321005
[email protected]13f92f6e2012-08-13 07:39:141006 EXPECT_FALSE(DirectoryExists(url));
1007 EXPECT_FALSE(PathExists(url));
1008
[email protected]0c5ebe32011-08-19 22:37:161009 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091010 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491011 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091012 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321013
[email protected]0c5ebe32011-08-19 22:37:161014 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321015 exclusive = true;
1016 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161017 context.reset(NewContext(NULL));
1018 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091019 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491020 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091021 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101022 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161023
1024 context.reset(NewContext(NULL));
1025 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091026 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491027 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091028 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101029 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321030
[email protected]13f92f6e2012-08-13 07:39:141031 EXPECT_TRUE(DirectoryExists(url));
1032 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321033
1034 exclusive = true;
1035 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141036 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491037 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091038 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101039 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321040
1041 exclusive = true;
1042 recursive = false;
[email protected]949f25a2012-06-27 01:53:091043 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141044 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491045 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091046 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101047 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321048
[email protected]949f25a2012-06-27 01:53:091049 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321050
[email protected]13f92f6e2012-08-13 07:39:141051 EXPECT_FALSE(DirectoryExists(url));
1052 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321053
1054 exclusive = true;
1055 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141056 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491057 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091058 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101059 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321060
[email protected]13f92f6e2012-08-13 07:39:141061 EXPECT_TRUE(DirectoryExists(url));
1062 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321063
1064 exclusive = true;
1065 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141066 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491067 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091068 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101069 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321070}
1071
[email protected]7878ece2011-09-05 11:41:491072TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161073 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321074 bool exclusive = true;
1075 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091076 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491077 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091078 context.get(), url, exclusive, recursive));
1079 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321080}
1081
[email protected]7878ece2011-09-05 11:41:491082TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451083 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321084}
1085
[email protected]7878ece2011-09-05 11:41:491086TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091087 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321088}
1089
[email protected]7878ece2011-09-05 11:41:491090TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[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]d4905e2e2011-05-13 21:56:321093
1094 bool created = false;
1095 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091096 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321097 ASSERT_TRUE(created);
1098
[email protected]c83118f2013-05-20 04:35:421099 std::vector<DirectoryEntry> entries;
[email protected]1a647202013-01-21 15:32:251100 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301101 AsyncFileTestHelper::ReadDirectory(
1102 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321103
[email protected]949f25a2012-06-27 01:53:091104 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321105}
1106
[email protected]7878ece2011-09-05 11:41:491107TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091108 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161109 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411110
1111 base::Time last_access_time = base::Time::Now();
1112 base::Time last_modified_time = base::Time::Now();
1113
1114 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321115 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491116 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091117 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321118
[email protected]2517cfa2011-08-25 05:12:411119 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161120 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411121 bool created = false;
1122 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091123 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411124 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091125 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411126
1127 // Now test a directory:
1128 context.reset(NewContext(NULL));
1129 bool exclusive = true;
1130 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091131 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491132 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091133 url, exclusive, recursive));
1134 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161135}
1136
[email protected]7878ece2011-09-05 11:41:491137TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091138 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161139 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1140
[email protected]949f25a2012-06-27 01:53:091141 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161142 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411143 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161144 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091145 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411146 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161147 context->set_allowed_bytes_growth(1024);
1148 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091149 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411150 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091151 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161152 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1153
1154 context->set_allowed_bytes_growth(1024);
1155 bool exclusive = true;
1156 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091157 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331158 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091159 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161160 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421161 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1162 for (iterator iter = components.begin();
1163 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491164 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331165 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161166 }
1167 context.reset(NewContext(NULL));
1168 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491169 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091170 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161171 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321172}
1173
[email protected]7878ece2011-09-05 11:41:491174TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091175 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1176 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161177 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321178
1179 bool is_copy_not_move = false;
1180 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091181 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321182 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101183 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161184 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321185 is_copy_not_move = true;
1186 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091187 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321188 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101189 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091190 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321191 bool exclusive = true;
1192 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161193 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491194 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091195 context.get(),
[email protected]04c899f2013-02-08 08:28:421196 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091197 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101198 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321199 is_copy_not_move = false;
1200 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091201 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321202 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101203 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161204 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321205 is_copy_not_move = true;
1206 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091207 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321208 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101209 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321210}
1211
[email protected]7878ece2011-09-05 11:41:491212TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321213 const int64 kSourceLength = 5;
1214 const int64 kDestLength = 50;
1215
1216 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1217 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1218 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1219 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1220 test_case.is_copy_not_move);
1221 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1222 test_case.source_path);
1223 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1224 test_case.dest_path);
1225 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1226 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161227 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321228
1229 bool exclusive = false;
1230 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091231 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1232 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321233
[email protected]0c5ebe32011-08-19 22:37:161234 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491235 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091236 context.get(),
[email protected]04c899f2013-02-08 08:28:421237 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091238 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161239 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491240 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091241 context.get(),
[email protected]04c899f2013-02-08 08:28:421242 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091243 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321244
[email protected]d4905e2e2011-05-13 21:56:321245 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161246 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321247 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091248 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321249 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161250 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321251 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091252 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321253
1254 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161255 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321256 created = false;
1257 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091258 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321259 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161260 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321261 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091262 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321263 }
1264
[email protected]0c5ebe32011-08-19 22:37:161265 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491266 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091267 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101268
[email protected]d4905e2e2011-05-13 21:56:321269 if (test_case.is_copy_not_move) {
1270 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331271 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161272 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491273 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091274 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321275 EXPECT_EQ(kSourceLength, file_info.size);
1276 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091277 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321278 } else {
1279 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331280 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161281 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491282 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091283 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321284 }
1285 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331286 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491287 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091288 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321289 EXPECT_EQ(kSourceLength, file_info.size);
1290
1291 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091292 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321293 }
1294}
1295
[email protected]7878ece2011-09-05 11:41:491296TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091297 FileSystemURL src_url = CreateURLFromUTF8("src path");
1298 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161299 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1300 bool created = false;
[email protected]7878ece2011-09-05 11:41:491301 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091302 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161303
1304 bool is_copy = true;
1305 // Copy, no overwrite.
1306 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091307 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161308 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091309 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161310 context.reset(NewContext(NULL));
1311 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091312 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161313 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091314 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161315
1316 // Copy, with overwrite.
1317 context.reset(NewContext(NULL));
1318 context->set_allowed_bytes_growth(0);
1319 EXPECT_EQ(base::PLATFORM_FILE_OK,
[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}
1322
[email protected]7878ece2011-09-05 11:41:491323TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091324 FileSystemURL src_url = CreateURLFromUTF8("src path");
1325 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161326 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1327 bool created = false;
[email protected]7878ece2011-09-05 11:41:491328 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091329 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161330
1331 bool is_copy = false;
1332 // Move, rename, no overwrite.
1333 context.reset(NewContext(NULL));
1334 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091335 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1336 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161337 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091338 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161339 context.reset(NewContext(NULL));
1340 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091341 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1342 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161343 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091344 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161345
1346 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491347 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091348 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161349
1350 // Move, rename, with overwrite.
1351 context.reset(NewContext(NULL));
1352 context->set_allowed_bytes_growth(0);
1353 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091354 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161355}
1356
[email protected]7878ece2011-09-05 11:41:491357TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091358 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161359 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1360 bool created = false;
[email protected]7878ece2011-09-05 11:41:491361 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091362 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161363
1364 bool exclusive = true;
1365 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091366 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161367 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491368 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091369 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161370
[email protected]04c899f2013-02-08 08:28:421371 FileSystemURL dest_url = FileSystemURLAppend(
1372 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161373
1374 bool is_copy = false;
1375 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1376 // Move, no rename, no overwrite.
1377 context.reset(NewContext(NULL));
1378 context->set_allowed_bytes_growth(allowed_bytes_growth);
1379 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091380 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161381 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1382
1383 // Move, no rename, with overwrite.
1384 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491385 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091386 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161387 context.reset(NewContext(NULL));
1388 context->set_allowed_bytes_growth(allowed_bytes_growth);
1389 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091390 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161391 EXPECT_EQ(
1392 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091393 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161394 context->allowed_bytes_growth());
1395}
1396
[email protected]7878ece2011-09-05 11:41:491397TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461398 TestCopyInForeignFileHelper(false /* overwrite */);
1399 TestCopyInForeignFileHelper(true /* overwrite */);
1400}
1401
[email protected]7878ece2011-09-05 11:41:491402TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161403 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091404 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321405 bool exclusive = true;
1406 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491407 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091408 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321409
[email protected]a3ef4832013-02-02 05:12:331410 std::set<base::FilePath::StringType> files;
1411 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091412 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321413
[email protected]949f25a2012-06-27 01:53:091414 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321415
[email protected]13f92f6e2012-08-13 07:39:141416 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321417 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301418 AsyncFileTestHelper::Copy(
1419 test_helper().file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321420
[email protected]949f25a2012-06-27 01:53:091421 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141422 EXPECT_TRUE(DirectoryExists(src_url));
1423 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321424 recursive = true;
1425 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301426 AsyncFileTestHelper::Remove(
1427 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141428 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321429}
[email protected]6b931152011-05-20 21:02:351430
[email protected]7878ece2011-09-05 11:41:491431TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1432 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1433 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161434 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261435 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161436 EXPECT_EQ(origin(), enumerator->Next());
1437 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1438 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1439 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261440 EXPECT_EQ(GURL(), enumerator->Next());
1441 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1442 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1443
1444 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161445 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261446
1447 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1448 SCOPED_TRACE(testing::Message() <<
1449 "Validating kOriginEnumerationTestRecords " << i);
1450 const OriginEnumerationTestRecord& record =
1451 kOriginEnumerationTestRecords[i];
1452 GURL origin_url(record.origin_url);
1453 origins_expected.insert(origin_url);
1454 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331455 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161456 NewHelper(origin_url, kFileSystemTypeTemporary));
1457 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261458 bool created = false;
1459 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501460 ofu()->EnsureFileExists(
1461 context.get(),
[email protected]949f25a2012-06-27 01:53:091462 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501463 &created));
[email protected]fcc2d5f2011-05-23 22:06:261464 EXPECT_TRUE(created);
1465 }
1466 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331467 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161468 NewHelper(origin_url, kFileSystemTypePersistent));
1469 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261470 bool created = false;
1471 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501472 ofu()->EnsureFileExists(
1473 context.get(),
[email protected]949f25a2012-06-27 01:53:091474 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501475 &created));
[email protected]fcc2d5f2011-05-23 22:06:261476 EXPECT_TRUE(created);
1477 }
1478 }
[email protected]7878ece2011-09-05 11:41:491479 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261480 EXPECT_TRUE(enumerator.get());
1481 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161482 GURL origin_url;
1483 while (!(origin_url = enumerator->Next()).is_empty()) {
1484 origins_found.insert(origin_url);
1485 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261486 bool found = false;
1487 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1488 ++i) {
1489 const OriginEnumerationTestRecord& record =
1490 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161491 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261492 continue;
1493 found = true;
1494 EXPECT_EQ(record.has_temporary,
1495 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1496 EXPECT_EQ(record.has_persistent,
1497 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1498 }
[email protected]0c5ebe32011-08-19 22:37:161499 // Deal with the default filesystem created by the test helper.
1500 if (!found && origin_url == origin()) {
1501 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1502 EXPECT_EQ(true,
1503 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181504 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161505 found = true;
1506 }
[email protected]fcc2d5f2011-05-23 22:06:261507 EXPECT_TRUE(found);
1508 }
1509
1510 std::set<GURL> diff;
1511 std::set_symmetric_difference(origins_expected.begin(),
1512 origins_expected.end(), origins_found.begin(), origins_found.end(),
1513 inserter(diff, diff.begin()));
1514 EXPECT_TRUE(diff.empty());
1515}
[email protected]0c5ebe32011-08-19 22:37:161516
[email protected]7878ece2011-09-05 11:41:491517TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161518 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1519
1520 int64 expected_quota = 0;
1521
[email protected]f83b5b72012-01-27 10:26:561522 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011523 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561524 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331525 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501526 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161527 if (test_case.is_directory) {
1528 bool exclusive = true;
1529 bool recursive = false;
1530 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091531 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501532 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161533 } else {
1534 bool created = false;
1535 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091536 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501537 &created));
[email protected]0c5ebe32011-08-19 22:37:161538 ASSERT_TRUE(created);
1539 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501540 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091541 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501542 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161543 expected_quota += test_case.data_file_size;
1544 }
1545 }
[email protected]022d2702012-05-14 16:04:261546
1547 // Usually raw size in usage cache and the usage returned by QuotaUtil
1548 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161549 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261550 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1551
[email protected]0c5ebe32011-08-19 22:37:161552 RevokeUsageCache();
1553 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261554 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1555
1556 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161557 GetUsageFromQuotaManager();
1558 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261559 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161560 EXPECT_EQ(expected_quota, usage());
1561}
[email protected]34583332011-08-31 08:59:471562
[email protected]7878ece2011-09-05 11:41:491563TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091564 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1565 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471566
1567 scoped_ptr<FileSystemOperationContext> context;
1568 base::PlatformFile file;
1569 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331570 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471571 bool created = false;
1572
1573 // Create a non-empty file.
1574 context.reset(NewContext(NULL));
1575 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491576 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471577 EXPECT_TRUE(created);
1578 context.reset(NewContext(NULL));
1579 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491580 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471581 context.reset(NewContext(NULL));
1582 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491583 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471584 context.get(), kPath1, &file_info, &data_path));
1585 EXPECT_EQ(10, file_info.size);
1586
1587 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491588 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471589
1590 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141591 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471592 context.reset(NewContext(NULL));
1593 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491594 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471595 EXPECT_TRUE(created);
1596 context.reset(NewContext(NULL));
1597 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491598 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471599 context.get(), kPath1, &file_info, &data_path));
1600 EXPECT_EQ(0, file_info.size);
1601
1602 // Make another broken file to |kPath2|.
1603 context.reset(NewContext(NULL));
1604 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491605 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471606 EXPECT_TRUE(created);
1607
1608 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491609 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471610
1611 // Repair broken |kPath1|.
1612 context.reset(NewContext(NULL));
1613 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491614 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471615 base::Time::Now()));
1616 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491617 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471618 EXPECT_TRUE(created);
1619
1620 // Copy from sound |kPath1| to broken |kPath2|.
1621 context.reset(NewContext(NULL));
1622 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491623 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501624 true /* copy */));
[email protected]34583332011-08-31 08:59:471625
[email protected]7878ece2011-09-05 11:41:491626 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471627 context.reset(NewContext(NULL));
1628 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491629 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471630 context.get(), kPath1,
1631 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1632 &file, &created));
1633 EXPECT_TRUE(created);
1634 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1635 EXPECT_EQ(0, file_info.size);
1636 EXPECT_TRUE(base::ClosePlatformFile(file));
1637}
[email protected]9dfdc0e32011-09-02 06:07:441638
[email protected]7878ece2011-09-05 11:41:491639TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091640 const FileSystemURL kPath[] = {
1641 CreateURLFromUTF8("foo"),
1642 CreateURLFromUTF8("bar"),
1643 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441644 };
[email protected]a3ef4832013-02-02 05:12:331645 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441646 scoped_ptr<FileSystemOperationContext> context;
1647
1648 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1649 bool created = false;
1650 context.reset(NewContext(NULL));
1651 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491652 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441653 EXPECT_TRUE(created);
1654 }
1655
[email protected]c83118f2013-05-20 04:35:421656 std::vector<DirectoryEntry> entries;
[email protected]9dfdc0e32011-09-02 06:07:441657 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301658 AsyncFileTestHelper::ReadDirectory(
1659 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441660 EXPECT_EQ(3u, entries.size());
1661
[email protected]a3ef4832013-02-02 05:12:331662 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441663 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491664 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441665 EXPECT_TRUE(file_util::Delete(local_path, false));
1666
[email protected]9dfdc0e32011-09-02 06:07:441667 entries.clear();
1668 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301669 AsyncFileTestHelper::ReadDirectory(
1670 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441671 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1672}
[email protected]fad625e2f2011-12-08 05:38:031673
1674TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1675 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091676 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031677
1678 // Create working directory.
1679 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091680 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031681
1682 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421683 FileSystemURL url(FileSystemURLAppendUTF8(
1684 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031685 bool created = false;
[email protected]949f25a2012-06-27 01:53:091686 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031687 context.reset(NewContext(NULL));
1688 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091689 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031690 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091691 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031692
1693 // non create case.
1694 created = true;
[email protected]949f25a2012-06-27 01:53:091695 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031696 context.reset(NewContext(NULL));
1697 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091698 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031699 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091700 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031701
1702 // fail case.
[email protected]04c899f2013-02-08 08:28:421703 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031704 context.reset(NewContext(NULL));
1705 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091706 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031707
[email protected]949f25a2012-06-27 01:53:091708 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031709 context.reset(NewContext(NULL));
1710 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091711 ofu()->EnsureFileExists(context.get(), url, &created));
1712 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031713
1714 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421715 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391716 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031717 created = false;
[email protected]949f25a2012-06-27 01:53:091718 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031719 context.reset(NewContext(NULL));
1720 EXPECT_EQ(base::PLATFORM_FILE_OK,
1721 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091722 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031723 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1724 &file_handle, &created));
1725 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1726 EXPECT_TRUE(created);
1727 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091728 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031729
1730 // open case.
1731 file_handle = base::kInvalidPlatformFileValue;
1732 created = true;
[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_OK,
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_OPEN | base::PLATFORM_FILE_WRITE,
1739 &file_handle, &created));
1740 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1741 EXPECT_FALSE(created);
1742 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091743 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031744
1745 // fail case
1746 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091747 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031748 context.reset(NewContext(NULL));
1749 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1750 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091751 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031752 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1753 &file_handle, &created));
1754 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091755 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031756
1757 // CreateDirectory, create case.
1758 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421759 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1760 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091761 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031762 context.reset(NewContext(NULL));
1763 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091764 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031765 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091766 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031767
1768 // create subdir case.
1769 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421770 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091771 ClearTimestamp(dir_url);
1772 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031773 context.reset(NewContext(NULL));
1774 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091775 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031776 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091777 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1778 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031779
1780 // fail case.
[email protected]04c899f2013-02-08 08:28:421781 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091782 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031783 context.reset(NewContext(NULL));
1784 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091785 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031786 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091787 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031788
1789 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421790 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1791 FileSystemURL src_path = FileSystemURLAppendUTF8(
1792 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031793 context.reset(NewContext(NULL));
1794 EXPECT_EQ(base::PLATFORM_FILE_OK,
1795 ofu()->EnsureFileExists(context.get(), src_path, &created));
1796 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331797 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031798 context.reset(NewContext(NULL));
1799 EXPECT_EQ(base::PLATFORM_FILE_OK,
1800 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1801
[email protected]949f25a2012-06-27 01:53:091802 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031803 context.reset(NewContext(NULL));
1804 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501805 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131806 src_local_path,
[email protected]949f25a2012-06-27 01:53:091807 url));
1808 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031809}
1810
1811TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1812 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091813 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031814
1815 // Create working directory.
1816 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091817 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031818
1819 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421820 FileSystemURL url = FileSystemURLAppendUTF8(
1821 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031822 bool created = false;
1823 context.reset(NewContext(NULL));
1824 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091825 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031826 EXPECT_TRUE(created);
1827
[email protected]949f25a2012-06-27 01:53:091828 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031829 context.reset(NewContext(NULL));
1830 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091831 ofu()->DeleteFile(context.get(), url));
1832 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031833
1834 // fail case.
[email protected]949f25a2012-06-27 01:53:091835 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031836 context.reset(NewContext(NULL));
1837 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091838 ofu()->DeleteFile(context.get(), url));
1839 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031840
[email protected]bab213be2013-01-23 15:13:081841 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421842 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1843 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031844 context.reset(NewContext(NULL));
1845 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091846 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031847 created = false;
1848 context.reset(NewContext(NULL));
1849 EXPECT_EQ(base::PLATFORM_FILE_OK,
1850 ofu()->EnsureFileExists(context.get(), file_path, &created));
1851 EXPECT_TRUE(created);
1852
[email protected]949f25a2012-06-27 01:53:091853 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031854 context.reset(NewContext(NULL));
1855 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081856 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091857 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031858
1859 // delete case.
1860 context.reset(NewContext(NULL));
1861 EXPECT_EQ(base::PLATFORM_FILE_OK,
1862 ofu()->DeleteFile(context.get(), file_path));
1863
[email protected]949f25a2012-06-27 01:53:091864 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031865 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081866 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091867 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031868}
1869
1870TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1871 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091872 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031873 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091874 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031875 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091876 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031877 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091878 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031879}
[email protected]a3938912012-03-27 14:00:551880
1881TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091882 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421883 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1884 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551885
1886 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1887 EXPECT_EQ(base::PLATFORM_FILE_OK,
1888 ofu()->CreateDirectory(context.get(), dir, false, false));
1889
1890 bool created = false;
1891 context.reset(NewContext(NULL));
1892 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091893 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551894 EXPECT_TRUE(created);
1895
1896 context.reset(NewContext(NULL));
1897 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091898 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551899
[email protected]a3ef4832013-02-02 05:12:331900 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551901 context.reset(NewContext(NULL));
1902 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091903 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551904 EXPECT_FALSE(file_path.empty());
1905
1906 context.reset(NewContext(NULL));
1907 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091908 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551909 base::Time::Now() + base::TimeDelta::FromHours(1),
1910 base::Time()));
1911
1912 context.reset(NewContext(NULL));
1913 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1914 ofu()->CreateFileEnumerator(context.get(), dir, false));
1915
1916 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331917 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551918 while (!(file_path_each = file_enum->Next()).empty()) {
1919 context.reset(NewContext(NULL));
1920 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331921 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551922 EXPECT_EQ(base::PLATFORM_FILE_OK,
1923 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421924 FileSystemURL::CreateForTest(
1925 dir.origin(),
1926 dir.mount_type(),
1927 file_path_each),
[email protected]a3938912012-03-27 14:00:551928 &file_info, &file_path));
1929 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1930 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1931 EXPECT_EQ(file_info.size, file_enum->Size());
1932 ++count;
1933 }
1934 EXPECT_EQ(2, count);
1935}
[email protected]294dd0312012-05-11 07:35:131936
[email protected]58ac5272013-02-15 10:05:031937// crbug.com/176470
1938#if defined(OS_WIN) || defined(OS_ANDROID)
1939#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1940#else
1941#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1942#endif
1943TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091944 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1945 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1946 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1947 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131948 bool created;
1949
1950 int64 expected_total_file_size = 0;
1951 ASSERT_EQ(base::PLATFORM_FILE_OK,
1952 ofu()->EnsureFileExists(
1953 AllowUsageIncrease(PathCost(from_file))->context(),
1954 from_file, &created));
1955 ASSERT_TRUE(created);
1956 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1957
1958 ASSERT_EQ(base::PLATFORM_FILE_OK,
1959 ofu()->EnsureFileExists(
1960 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1961 obstacle_file, &created));
1962 ASSERT_TRUE(created);
1963 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1964
1965 int64 from_file_size = 1020;
1966 expected_total_file_size += from_file_size;
1967 ASSERT_EQ(base::PLATFORM_FILE_OK,
1968 ofu()->Truncate(
1969 AllowUsageIncrease(from_file_size)->context(),
1970 from_file, from_file_size));
1971 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1972
1973 int64 obstacle_file_size = 1;
1974 expected_total_file_size += obstacle_file_size;
1975 ASSERT_EQ(base::PLATFORM_FILE_OK,
1976 ofu()->Truncate(
1977 AllowUsageIncrease(obstacle_file_size)->context(),
1978 obstacle_file, obstacle_file_size));
1979 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1980
1981 int64 to_file1_size = from_file_size;
1982 expected_total_file_size += to_file1_size;
1983 ASSERT_EQ(base::PLATFORM_FILE_OK,
1984 ofu()->CopyOrMoveFile(
1985 AllowUsageIncrease(
1986 PathCost(to_file1) + to_file1_size)->context(),
1987 from_file, to_file1, true /* copy */));
1988 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1989
1990 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1991 ofu()->CopyOrMoveFile(
1992 DisallowUsageIncrease(
1993 PathCost(to_file2) + from_file_size)->context(),
1994 from_file, to_file2, true /* copy */));
1995 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1996
1997 int64 old_obstacle_file_size = obstacle_file_size;
1998 obstacle_file_size = from_file_size;
1999 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2000 ASSERT_EQ(base::PLATFORM_FILE_OK,
2001 ofu()->CopyOrMoveFile(
2002 AllowUsageIncrease(
2003 obstacle_file_size - old_obstacle_file_size)->context(),
2004 from_file, obstacle_file, true /* copy */));
2005 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2006
2007 int64 old_from_file_size = from_file_size;
2008 from_file_size = old_from_file_size - 1;
2009 expected_total_file_size += from_file_size - old_from_file_size;
2010 ASSERT_EQ(base::PLATFORM_FILE_OK,
2011 ofu()->Truncate(
2012 AllowUsageIncrease(
2013 from_file_size - old_from_file_size)->context(),
2014 from_file, from_file_size));
2015 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2016
2017 // quota exceeded
2018 {
2019 old_obstacle_file_size = obstacle_file_size;
2020 obstacle_file_size = from_file_size;
2021 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2022 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2023 obstacle_file_size - old_obstacle_file_size);
2024 helper->context()->set_allowed_bytes_growth(
2025 helper->context()->allowed_bytes_growth() - 1);
2026 ASSERT_EQ(base::PLATFORM_FILE_OK,
2027 ofu()->CopyOrMoveFile(
2028 helper->context(),
2029 from_file, obstacle_file, true /* copy */));
2030 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2031 }
2032}
2033
2034TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092035 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2036 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2037 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132038 bool created;
2039
2040 int64 expected_total_file_size = 0;
2041 ASSERT_EQ(base::PLATFORM_FILE_OK,
2042 ofu()->EnsureFileExists(
2043 AllowUsageIncrease(PathCost(from_file))->context(),
2044 from_file, &created));
2045 ASSERT_TRUE(created);
2046 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2047
2048 int64 from_file_size = 1020;
2049 expected_total_file_size += from_file_size;
2050 ASSERT_EQ(base::PLATFORM_FILE_OK,
2051 ofu()->Truncate(
2052 AllowUsageIncrease(from_file_size)->context(),
2053 from_file, from_file_size));
2054 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2055
2056 int64 to_file_size ALLOW_UNUSED = from_file_size;
2057 from_file_size = 0;
2058 ASSERT_EQ(base::PLATFORM_FILE_OK,
2059 ofu()->CopyOrMoveFile(
2060 AllowUsageIncrease(-PathCost(from_file) +
2061 PathCost(to_file))->context(),
2062 from_file, to_file, false /* move */));
2063 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2064
2065 ASSERT_EQ(base::PLATFORM_FILE_OK,
2066 ofu()->EnsureFileExists(
2067 AllowUsageIncrease(PathCost(from_file))->context(),
2068 from_file, &created));
2069 ASSERT_TRUE(created);
2070 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2071
2072 ASSERT_EQ(base::PLATFORM_FILE_OK,
2073 ofu()->EnsureFileExists(
2074 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2075 obstacle_file, &created));
2076 ASSERT_TRUE(created);
2077 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2078
2079 from_file_size = 1020;
2080 expected_total_file_size += from_file_size;
2081 ASSERT_EQ(base::PLATFORM_FILE_OK,
2082 ofu()->Truncate(
2083 AllowUsageIncrease(from_file_size)->context(),
2084 from_file, from_file_size));
2085 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2086
2087 int64 obstacle_file_size = 1;
2088 expected_total_file_size += obstacle_file_size;
2089 ASSERT_EQ(base::PLATFORM_FILE_OK,
2090 ofu()->Truncate(
2091 AllowUsageIncrease(1)->context(),
2092 obstacle_file, obstacle_file_size));
2093 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2094
2095 int64 old_obstacle_file_size = obstacle_file_size;
2096 obstacle_file_size = from_file_size;
2097 from_file_size = 0;
2098 expected_total_file_size -= old_obstacle_file_size;
2099 ASSERT_EQ(base::PLATFORM_FILE_OK,
2100 ofu()->CopyOrMoveFile(
2101 AllowUsageIncrease(
2102 -old_obstacle_file_size - PathCost(from_file))->context(),
2103 from_file, obstacle_file,
2104 false /* move */));
2105 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2106
2107 ASSERT_EQ(base::PLATFORM_FILE_OK,
2108 ofu()->EnsureFileExists(
2109 AllowUsageIncrease(PathCost(from_file))->context(),
2110 from_file, &created));
2111 ASSERT_TRUE(created);
2112 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2113
2114 from_file_size = 10;
2115 expected_total_file_size += from_file_size;
2116 ASSERT_EQ(base::PLATFORM_FILE_OK,
2117 ofu()->Truncate(
2118 AllowUsageIncrease(from_file_size)->context(),
2119 from_file, from_file_size));
2120 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2121
2122 // quota exceeded even after operation
2123 old_obstacle_file_size = obstacle_file_size;
2124 obstacle_file_size = from_file_size;
2125 from_file_size = 0;
2126 expected_total_file_size -= old_obstacle_file_size;
2127 scoped_ptr<FileSystemOperationContext> context =
2128 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2129 ASSERT_EQ(base::PLATFORM_FILE_OK,
2130 ofu()->CopyOrMoveFile(
2131 context.get(), from_file, obstacle_file, false /* move */));
2132 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2133 context.reset();
2134}
2135
2136TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092137 FileSystemURL dir(CreateURLFromUTF8("dir"));
2138 FileSystemURL file(CreateURLFromUTF8("file"));
2139 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2140 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132141 bool created;
2142
2143 ASSERT_EQ(base::PLATFORM_FILE_OK,
2144 ofu()->EnsureFileExists(
2145 AllowUsageIncrease(PathCost(file))->context(),
2146 file, &created));
2147 ASSERT_TRUE(created);
2148 ASSERT_EQ(0, ComputeTotalFileSize());
2149
2150 ASSERT_EQ(base::PLATFORM_FILE_OK,
2151 ofu()->CreateDirectory(
2152 AllowUsageIncrease(PathCost(dir))->context(),
2153 dir, false, false));
2154 ASSERT_EQ(0, ComputeTotalFileSize());
2155
2156 ASSERT_EQ(base::PLATFORM_FILE_OK,
2157 ofu()->EnsureFileExists(
2158 AllowUsageIncrease(PathCost(dfile1))->context(),
2159 dfile1, &created));
2160 ASSERT_TRUE(created);
2161 ASSERT_EQ(0, ComputeTotalFileSize());
2162
2163 ASSERT_EQ(base::PLATFORM_FILE_OK,
2164 ofu()->EnsureFileExists(
2165 AllowUsageIncrease(PathCost(dfile2))->context(),
2166 dfile2, &created));
2167 ASSERT_TRUE(created);
2168 ASSERT_EQ(0, ComputeTotalFileSize());
2169
2170 ASSERT_EQ(base::PLATFORM_FILE_OK,
2171 ofu()->Truncate(
2172 AllowUsageIncrease(340)->context(),
2173 file, 340));
2174 ASSERT_EQ(340, ComputeTotalFileSize());
2175
2176 ASSERT_EQ(base::PLATFORM_FILE_OK,
2177 ofu()->Truncate(
2178 AllowUsageIncrease(1020)->context(),
2179 dfile1, 1020));
2180 ASSERT_EQ(1360, ComputeTotalFileSize());
2181
2182 ASSERT_EQ(base::PLATFORM_FILE_OK,
2183 ofu()->Truncate(
2184 AllowUsageIncrease(120)->context(),
2185 dfile2, 120));
2186 ASSERT_EQ(1480, ComputeTotalFileSize());
2187
2188 ASSERT_EQ(base::PLATFORM_FILE_OK,
2189 ofu()->DeleteFile(
2190 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2191 file));
2192 ASSERT_EQ(1140, ComputeTotalFileSize());
2193
2194 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302195 AsyncFileTestHelper::Remove(
2196 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132197 ASSERT_EQ(0, ComputeTotalFileSize());
2198}
[email protected]7d78be12012-05-24 07:07:262199
2200TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092201 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262202 base::PlatformFile file_handle;
2203 bool created;
2204
2205 // Creating a file.
2206 ASSERT_EQ(base::PLATFORM_FILE_OK,
2207 ofu()->EnsureFileExists(
2208 AllowUsageIncrease(PathCost(file))->context(),
2209 file, &created));
2210 ASSERT_TRUE(created);
2211 ASSERT_EQ(0, ComputeTotalFileSize());
2212
2213 // Opening it, which shouldn't change the usage.
2214 ASSERT_EQ(base::PLATFORM_FILE_OK,
2215 ofu()->CreateOrOpen(
2216 AllowUsageIncrease(0)->context(), file,
2217 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2218 &file_handle, &created));
2219 ASSERT_EQ(0, ComputeTotalFileSize());
2220 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2221
2222 const int length = 33;
2223 ASSERT_EQ(base::PLATFORM_FILE_OK,
2224 ofu()->Truncate(
2225 AllowUsageIncrease(length)->context(), file, length));
2226 ASSERT_EQ(length, ComputeTotalFileSize());
2227
2228 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2229 ASSERT_EQ(base::PLATFORM_FILE_OK,
2230 ofu()->CreateOrOpen(
2231 AllowUsageIncrease(-length)->context(), file,
2232 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2233 &file_handle, &created));
2234 ASSERT_EQ(0, ComputeTotalFileSize());
2235 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2236
2237 // Extending the file again.
2238 ASSERT_EQ(base::PLATFORM_FILE_OK,
2239 ofu()->Truncate(
2240 AllowUsageIncrease(length)->context(), file, length));
2241 ASSERT_EQ(length, ComputeTotalFileSize());
2242
2243 // Opening it with TRUNCATED flag, which should truncate the file size.
2244 ASSERT_EQ(base::PLATFORM_FILE_OK,
2245 ofu()->CreateOrOpen(
2246 AllowUsageIncrease(-length)->context(), file,
2247 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2248 &file_handle, &created));
2249 ASSERT_EQ(0, ComputeTotalFileSize());
2250 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2251}
[email protected]c4e6f9c2012-09-09 17:42:102252
2253} // namespace fileapi