blob: 0450772457047f11ef0c68637bccab43d46c0c37 [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
5#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]c6f9203a2013-05-28 02:08:0717#include "webkit/browser/fileapi/async_file_test_helper.h"
[email protected]f25e1132013-05-24 13:58:0418#include "webkit/browser/fileapi/external_mount_points.h"
[email protected]c6f9203a2013-05-28 02:08:0719#include "webkit/browser/fileapi/file_system_context.h"
[email protected]c4298d02013-05-20 05:42:5220#include "webkit/browser/fileapi/file_system_mount_point_provider.h"
[email protected]c6f9203a2013-05-28 02:08:0721#include "webkit/browser/fileapi/file_system_operation_context.h"
[email protected]f25e1132013-05-24 13:58:0422#include "webkit/browser/fileapi/file_system_task_runners.h"
[email protected]c814b5692013-05-20 11:37:4623#include "webkit/browser/fileapi/file_system_usage_cache.h"
[email protected]f25e1132013-05-24 13:58:0424#include "webkit/browser/fileapi/mock_file_change_observer.h"
25#include "webkit/browser/fileapi/mock_file_system_context.h"
[email protected]28f051c32013-05-21 05:15:2626#include "webkit/browser/fileapi/obfuscated_file_util.h"
[email protected]c6f9203a2013-05-28 02:08:0727#include "webkit/browser/fileapi/sandbox_file_system_test_helper.h"
28#include "webkit/browser/fileapi/test_file_set.h"
[email protected]7660ec92013-05-30 05:12:3929#include "webkit/browser/quota/mock_special_storage_policy.h"
30#include "webkit/browser/quota/quota_manager.h"
31#include "webkit/common/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]1c98fdd2013-05-24 09:45:27124 sandbox_file_system_(origin_, type_),
[email protected]0c5ebe32011-08-19 22:37:16125 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
[email protected]1c98fdd2013-05-24 09:45:27142 // Every time we create a new sandbox_file_system helper,
143 // it creates another context, which creates another path manager,
144 // another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49145 // another OFU. We need to pass in the context to skip all that.
[email protected]420fb562013-04-18 01:46:34146 file_system_context_ = CreateFileSystemContextForTesting(
[email protected]0c5ebe32011-08-19 22:37:16147 quota_manager_->proxy(),
[email protected]420fb562013-04-18 01:46:34148 data_dir_.path());
[email protected]0c5ebe32011-08-19 22:37:16149
[email protected]1c98fdd2013-05-24 09:45:27150 sandbox_file_system_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10151
152 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32153 }
154
[email protected]7fd8fa4f2013-02-07 05:43:50155 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55156 quota_manager_ = NULL;
[email protected]1c98fdd2013-05-24 09:45:27157 sandbox_file_system_.TearDown();
[email protected]e7e46732012-01-05 11:45:55158 }
159
[email protected]294dd0312012-05-11 07:35:13160 scoped_ptr<FileSystemOperationContext> LimitedContext(
161 int64 allowed_bytes_growth) {
162 scoped_ptr<FileSystemOperationContext> context(
[email protected]1c98fdd2013-05-24 09:45:27163 sandbox_file_system_.NewOperationContext());
[email protected]294dd0312012-05-11 07:35:13164 context->set_allowed_bytes_growth(allowed_bytes_growth);
165 return context.Pass();
166 }
167
168 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
169 return LimitedContext(kint64max);
170 }
171
[email protected]02a60542012-07-24 20:05:33172 FileSystemOperationContext* NewContext(
[email protected]1c98fdd2013-05-24 09:45:27173 SandboxFileSystemTestHelper* file_system) {
[email protected]c4e6f9c2012-09-09 17:42:10174 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16175 FileSystemOperationContext* context;
[email protected]1c98fdd2013-05-24 09:45:27176 if (file_system)
177 context = file_system->NewOperationContext();
[email protected]0c5ebe32011-08-19 22:37:16178 else
[email protected]1c98fdd2013-05-24 09:45:27179 context = sandbox_file_system_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46180 // Setting allowed_bytes_growth big enough for all tests.
181 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10182 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32183 return context;
184 }
185
[email protected]c4e6f9c2012-09-09 17:42:10186 const ChangeObserverList& change_observers() const {
187 return change_observers_;
188 }
189
190 MockFileChangeObserver* change_observer() {
191 return &change_observer_;
192 }
193
[email protected]0c5ebe32011-08-19 22:37:16194 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49195 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16196 // Use this for tests which need to run in multiple origins; we need a test
197 // helper per origin.
[email protected]1c98fdd2013-05-24 09:45:27198 SandboxFileSystemTestHelper* NewFileSystem(
[email protected]0c5ebe32011-08-19 22:37:16199 const GURL& origin, fileapi::FileSystemType type) {
[email protected]1c98fdd2013-05-24 09:45:27200 SandboxFileSystemTestHelper* file_system =
201 new SandboxFileSystemTestHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16202
[email protected]1c98fdd2013-05-24 09:45:27203 file_system->SetUp(file_system_context_.get());
204 return file_system;
[email protected]0c5ebe32011-08-19 22:37:16205 }
206
[email protected]7878ece2011-09-05 11:41:49207 ObfuscatedFileUtil* ofu() {
[email protected]1c98fdd2013-05-24 09:45:27208 return static_cast<ObfuscatedFileUtil*>(sandbox_file_system_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32209 }
210
[email protected]a3ef4832013-02-02 05:12:33211 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35212 return data_dir_.path();
213 }
214
[email protected]0c5ebe32011-08-19 22:37:16215 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26216 return origin_;
217 }
218
219 fileapi::FileSystemType type() const {
220 return type_;
221 }
222
[email protected]294dd0312012-05-11 07:35:13223 int64 ComputeTotalFileSize() {
[email protected]1c98fdd2013-05-24 09:45:27224 return sandbox_file_system_.ComputeCurrentOriginUsage() -
225 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]294dd0312012-05-11 07:35:13226 }
227
[email protected]0c5ebe32011-08-19 22:37:16228 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30229 int64 quota = -1;
230 quota_status_ = AsyncFileTestHelper::GetUsageAndQuota(
[email protected]1c98fdd2013-05-24 09:45:27231 quota_manager_, origin(), sandbox_file_system_.type(),
[email protected]07b64872013-02-13 11:46:30232 &usage_, &quota);
[email protected]0c5ebe32011-08-19 22:37:16233 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
234 }
235
236 void RevokeUsageCache() {
[email protected]1c98fdd2013-05-24 09:45:27237 quota_manager_->ResetUsageTracker(sandbox_file_system_.storage_type());
238 usage_cache()->Delete(sandbox_file_system_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26239 }
240
241 int64 SizeByQuotaUtil() {
[email protected]1c98fdd2013-05-24 09:45:27242 return sandbox_file_system_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16243 }
244
245 int64 SizeInUsageFile() {
[email protected]4cc586b2013-05-07 12:43:32246 base::MessageLoop::current()->RunUntilIdle();
[email protected]bf97e8b2013-04-14 15:00:13247 int64 usage = 0;
[email protected]1c98fdd2013-05-24 09:45:27248 return usage_cache()->GetUsage(
249 sandbox_file_system_.GetUsageCachePath(), &usage) ? usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16250 }
251
[email protected]13f92f6e2012-08-13 07:39:14252 bool PathExists(const FileSystemURL& url) {
253 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21254 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33255 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21256 base::PlatformFileError error = ofu()->GetFileInfo(
257 context.get(), url, &file_info, &platform_path);
258 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14259 }
260
261 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30262 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14263 }
264
[email protected]0c5ebe32011-08-19 22:37:16265 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31266 FileSystemUsageCache* usage_cache() {
[email protected]1c98fdd2013-05-24 09:45:27267 return sandbox_file_system_.usage_cache();
[email protected]06226172013-03-14 15:39:31268 }
[email protected]0c5ebe32011-08-19 22:37:16269
[email protected]949f25a2012-06-27 01:53:09270 FileSystemURL CreateURLFromUTF8(const std::string& path) {
[email protected]1c98fdd2013-05-24 09:45:27271 return sandbox_file_system_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50272 }
273
[email protected]949f25a2012-06-27 01:53:09274 int64 PathCost(const FileSystemURL& url) {
275 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13276 }
277
[email protected]a3ef4832013-02-02 05:12:33278 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]1c98fdd2013-05-24 09:45:27279 return sandbox_file_system_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50280 }
281
[email protected]d4905e2e2011-05-13 21:56:32282 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39283 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16284 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33285 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49286 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09287 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32288
289 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33290 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49291 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09292 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32293 EXPECT_EQ(data_path, local_path);
294 EXPECT_TRUE(FileExists(data_path));
295 EXPECT_EQ(0, GetSize(data_path));
296
297 const char data[] = "test data";
298 const int length = arraysize(data) - 1;
299
300 if (base::kInvalidPlatformFileValue == file_handle) {
301 bool created = true;
[email protected]403ada82013-01-08 07:51:39302 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32303 file_handle = base::CreatePlatformFile(
304 data_path,
305 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
306 &created,
307 &error);
[email protected]81b7f662011-05-26 00:54:46308 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32309 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
310 EXPECT_FALSE(created);
311 }
312 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
313 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
314
315 base::PlatformFileInfo file_info1;
316 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16317 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49318 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09319 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32320 EXPECT_EQ(data_path, local_path);
321
322 EXPECT_FALSE(file_info0.is_directory);
323 EXPECT_FALSE(file_info1.is_directory);
324 EXPECT_FALSE(file_info0.is_symbolic_link);
325 EXPECT_FALSE(file_info1.is_symbolic_link);
326 EXPECT_EQ(0, file_info0.size);
327 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32328 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32329
[email protected]0c5ebe32011-08-19 22:37:16330 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49331 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09332 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32333 EXPECT_EQ(length * 2, GetSize(data_path));
334
[email protected]0c5ebe32011-08-19 22:37:16335 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49336 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09337 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16338 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32339 }
340
341 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09342 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33343 const std::set<base::FilePath::StringType>& files,
344 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32345 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33346 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32347 for (iter = files.begin(); iter != files.end(); ++iter) {
348 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16349 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32350 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49351 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42352 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32353 &created));
354 ASSERT_FALSE(created);
355 }
356 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16357 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14358 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42359 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32360 }
361 }
362
[email protected]294dd0312012-05-11 07:35:13363 class UsageVerifyHelper {
364 public:
365 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]1c98fdd2013-05-24 09:45:27366 SandboxFileSystemTestHelper* file_system,
[email protected]294dd0312012-05-11 07:35:13367 int64 expected_usage)
368 : context_(context.Pass()),
[email protected]1c98fdd2013-05-24 09:45:27369 sandbox_file_system_(file_system),
[email protected]294dd0312012-05-11 07:35:13370 expected_usage_(expected_usage) {}
371
372 ~UsageVerifyHelper() {
[email protected]4cc586b2013-05-07 12:43:32373 base::MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13374 Check();
375 }
376
377 FileSystemOperationContext* context() {
378 return context_.get();
379 }
380
381 private:
382 void Check() {
383 ASSERT_EQ(expected_usage_,
[email protected]1c98fdd2013-05-24 09:45:27384 sandbox_file_system_->GetCachedOriginUsage());
[email protected]294dd0312012-05-11 07:35:13385 }
386
387 scoped_ptr<FileSystemOperationContext> context_;
[email protected]1c98fdd2013-05-24 09:45:27388 SandboxFileSystemTestHelper* sandbox_file_system_;
[email protected]294dd0312012-05-11 07:35:13389 int64 expected_usage_;
390 };
391
392 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27393 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13394 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
395 LimitedContext(requested_growth),
[email protected]1c98fdd2013-05-24 09:45:27396 &sandbox_file_system_, usage + requested_growth));
[email protected]294dd0312012-05-11 07:35:13397 }
398
399 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27400 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13401 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
[email protected]1c98fdd2013-05-24 09:45:27402 LimitedContext(requested_growth - 1), &sandbox_file_system_, usage));
[email protected]294dd0312012-05-11 07:35:13403 }
404
[email protected]d4905e2e2011-05-13 21:56:32405 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09406 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33407 std::set<base::FilePath::StringType>* files,
408 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32409 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42410 std::vector<DirectoryEntry> entries;
[email protected]d4905e2e2011-05-13 21:56:32411 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30412 AsyncFileTestHelper::ReadDirectory(
413 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32414 EXPECT_EQ(0UL, entries.size());
415
416 files->clear();
[email protected]89ee4272011-05-16 18:45:17417 files->insert(FILE_PATH_LITERAL("first"));
418 files->insert(FILE_PATH_LITERAL("second"));
419 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32420 directories->clear();
[email protected]89ee4272011-05-16 18:45:17421 directories->insert(FILE_PATH_LITERAL("fourth"));
422 directories->insert(FILE_PATH_LITERAL("fifth"));
423 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33424 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32425 for (iter = files->begin(); iter != files->end(); ++iter) {
426 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16427 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32428 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49429 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50430 context.get(),
[email protected]04c899f2013-02-08 08:28:42431 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50432 &created));
[email protected]d4905e2e2011-05-13 21:56:32433 ASSERT_TRUE(created);
434 }
435 for (iter = directories->begin(); iter != directories->end(); ++iter) {
436 bool exclusive = true;
437 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16438 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32439 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49440 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42441 context.get(),
442 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09443 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32444 }
[email protected]949f25a2012-06-27 01:53:09445 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32446 }
447
[email protected]949f25a2012-06-27 01:53:09448 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33449 std::set<base::FilePath::StringType> files;
450 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09451 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32452
453 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42454 std::vector<DirectoryEntry> entries;
[email protected]0c5ebe32011-08-19 22:37:16455 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32456 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30457 AsyncFileTestHelper::ReadDirectory(
458 file_system_context(), root_url, &entries));
[email protected]c83118f2013-05-20 04:35:42459 std::vector<DirectoryEntry>::iterator entry_iter;
[email protected]d4905e2e2011-05-13 21:56:32460 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10461 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32462 for (entry_iter = entries.begin(); entry_iter != entries.end();
463 ++entry_iter) {
[email protected]c83118f2013-05-20 04:35:42464 const DirectoryEntry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31465 std::set<base::FilePath::StringType>::iterator iter =
466 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32467 if (iter != files.end()) {
468 EXPECT_FALSE(entry.is_directory);
469 files.erase(iter);
470 continue;
471 }
[email protected]89ee4272011-05-16 18:45:17472 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32473 EXPECT_FALSE(directories.end() == iter);
474 EXPECT_TRUE(entry.is_directory);
475 directories.erase(iter);
476 }
477 }
478
[email protected]949f25a2012-06-27 01:53:09479 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41480 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32481 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16482
[email protected]2517cfa2011-08-25 05:12:41483 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32484 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49485 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09486 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10487 // Currently we fire no change notifications for Touch.
488 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33489 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32490 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16491 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49492 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09493 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32494 // We compare as time_t here to lower our resolution, to avoid false
495 // negatives caused by conversion to the local filesystem's native
496 // representation and back.
497 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
498
[email protected]0c5ebe32011-08-19 22:37:16499 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32500 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41501 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32502 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49503 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09504 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10505 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16506 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49507 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09508 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32509 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49510 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41511 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32512 }
513
[email protected]81b7f662011-05-26 00:54:46514 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23515 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46516 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33517 base::FilePath root_file_path = source_dir.path();
518 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09519 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46520 int64 src_file_length = 87;
521
522 base::PlatformFileError error_code;
523 bool created = false;
524 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
525 base::PlatformFile file_handle =
526 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50527 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46528 EXPECT_TRUE(created);
529 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
530 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
531 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
532 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
533
534 scoped_ptr<FileSystemOperationContext> context;
535
536 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16537 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46538 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09539 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46540 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10541
542 // We must have observed one (and only one) create_file_count.
543 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
544 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46545 }
546
[email protected]0c5ebe32011-08-19 22:37:16547 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09548 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16549 if (!overwrite) {
550 // Verify that file creation requires sufficient quota for the path.
551 context.reset(NewContext(NULL));
552 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
553 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13554 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09555 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16556 }
557
558 context.reset(NewContext(NULL));
559 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46560 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13561 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09562 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16563
[email protected]13f92f6e2012-08-13 07:39:14564 EXPECT_TRUE(PathExists(dest_url));
565 EXPECT_FALSE(DirectoryExists(dest_url));
566
[email protected]0c5ebe32011-08-19 22:37:16567 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46568 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33569 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49570 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09571 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50572 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46573 EXPECT_TRUE(FileExists(data_path));
574 EXPECT_EQ(src_file_length, GetSize(data_path));
575
576 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09577 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46578 }
579
[email protected]949f25a2012-06-27 01:53:09580 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03581 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
582 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09583 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
584 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03585 }
586
[email protected]949f25a2012-06-27 01:53:09587 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03588 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33589 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03590 base::PlatformFileInfo file_info;
591 context.reset(NewContext(NULL));
592 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09593 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10594 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03595 return file_info.last_modified;
596 }
597
[email protected]949f25a2012-06-27 01:53:09598 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03599 bool copy,
600 bool overwrite) {
601 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42602 const FileSystemURL src_dir_url(
603 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
604 const FileSystemURL dest_dir_url(
605 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03606
[email protected]04c899f2013-02-08 08:28:42607 const FileSystemURL src_file_url(
608 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
609 const FileSystemURL dest_file_url(
610 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03611
612 context.reset(NewContext(NULL));
613 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09614 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03615 context.reset(NewContext(NULL));
616 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09617 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03618
619 bool created = false;
620 context.reset(NewContext(NULL));
621 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09622 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03623 if (overwrite) {
624 context.reset(NewContext(NULL));
625 EXPECT_EQ(base::PLATFORM_FILE_OK,
626 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09627 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03628 }
629
[email protected]949f25a2012-06-27 01:53:09630 ClearTimestamp(src_dir_url);
631 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03632 context.reset(NewContext(NULL));
633 EXPECT_EQ(base::PLATFORM_FILE_OK,
634 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09635 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03636 copy));
[email protected]fad625e2f2011-12-08 05:38:03637 if (copy)
[email protected]949f25a2012-06-27 01:53:09638 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03639 else
[email protected]949f25a2012-06-27 01:53:09640 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
641 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03642 }
643
[email protected]ecdfd6c52012-04-11 13:35:44644 int64 ComputeCurrentUsage() {
[email protected]1c98fdd2013-05-24 09:45:27645 return sandbox_file_system_.ComputeCurrentOriginUsage() -
646 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]02a60542012-07-24 20:05:33647 }
[email protected]45ea0fbbb2012-02-27 22:28:49648
[email protected]07b64872013-02-13 11:46:30649 FileSystemContext* file_system_context() {
[email protected]1c98fdd2013-05-24 09:45:27650 return sandbox_file_system_.file_system_context();
[email protected]07b64872013-02-13 11:46:30651 }
652
[email protected]d4905e2e2011-05-13 21:56:32653 private:
[email protected]ea1a3f62012-11-16 20:34:23654 base::ScopedTempDir data_dir_;
[email protected]4cc586b2013-05-07 12:43:32655 base::MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16656 scoped_refptr<quota::QuotaManager> quota_manager_;
657 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26658 GURL origin_;
659 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03660 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]1c98fdd2013-05-24 09:45:27661 SandboxFileSystemTestHelper sandbox_file_system_;
[email protected]0c5ebe32011-08-19 22:37:16662 quota::QuotaStatusCode quota_status_;
663 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10664 MockFileChangeObserver change_observer_;
665 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32666
[email protected]7878ece2011-09-05 11:41:49667 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32668};
669
[email protected]7878ece2011-09-05 11:41:49670TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32671 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
672 bool created;
[email protected]949f25a2012-06-27 01:53:09673 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16674 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32675 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
676
677 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49678 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09679 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32680 &created));
681
[email protected]0c5ebe32011-08-19 22:37:16682 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32683 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09684 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32685
[email protected]949f25a2012-06-27 01:53:09686 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32687
[email protected]c4e6f9c2012-09-09 17:42:10688 EXPECT_TRUE(change_observer()->HasNoChange());
689
[email protected]0c5ebe32011-08-19 22:37:16690 // Verify that file creation requires sufficient quota for the path.
691 context.reset(NewContext(NULL));
692 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09693 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16694 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49695 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09696 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16697
698 context.reset(NewContext(NULL));
699 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09700 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32701 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49702 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09703 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32704 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10705 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32706 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
707
[email protected]949f25a2012-06-27 01:53:09708 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32709
[email protected]0c5ebe32011-08-19 22:37:16710 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33711 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49712 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09713 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32714 EXPECT_TRUE(file_util::PathExists(local_path));
715
[email protected]0c5ebe32011-08-19 22:37:16716 // Verify that deleting a file isn't stopped by zero quota, and that it frees
717 // up quote from its path.
718 context.reset(NewContext(NULL));
719 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32720 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09721 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10722 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32723 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09724 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16725 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32726
[email protected]0c5ebe32011-08-19 22:37:16727 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32728 bool exclusive = true;
729 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09730 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50731 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42732 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49733 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09734 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10735 // The oepration created 3 directories recursively.
736 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32737
[email protected]0c5ebe32011-08-19 22:37:16738 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32739 file_handle = base::kInvalidPlatformFileValue;
740 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49741 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09742 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32743 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10744 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32745 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
746
[email protected]949f25a2012-06-27 01:53:09747 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32748
[email protected]0c5ebe32011-08-19 22:37:16749 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49750 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09751 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32752 EXPECT_TRUE(file_util::PathExists(local_path));
753
[email protected]0c5ebe32011-08-19 22:37:16754 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32755 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09756 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10757 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32758 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10759
760 // Make sure we have no unexpected changes.
761 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32762}
763
[email protected]7878ece2011-09-05 11:41:49764TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32765 bool created = false;
[email protected]949f25a2012-06-27 01:53:09766 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16767 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32768
769 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09770 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32771
[email protected]0c5ebe32011-08-19 22:37:16772 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32773 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09774 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32775 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10776 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32777
[email protected]0c5ebe32011-08-19 22:37:16778 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33779 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49780 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09781 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32782 EXPECT_EQ(0, GetSize(local_path));
783
[email protected]0c5ebe32011-08-19 22:37:16784 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49785 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09786 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10787 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32788 EXPECT_EQ(10, GetSize(local_path));
789
[email protected]0c5ebe32011-08-19 22:37:16790 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49791 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09792 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32793 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10794 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32795
[email protected]13f92f6e2012-08-13 07:39:14796 EXPECT_FALSE(DirectoryExists(url));
797 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10798
799 // Make sure we have no unexpected changes.
800 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32801}
802
[email protected]ecdfd6c52012-04-11 13:35:44803TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
804 bool created = false;
[email protected]949f25a2012-06-27 01:53:09805 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44806
807 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13808 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09809 AllowUsageIncrease(PathCost(url))->context(),
810 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44811 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13812 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44813
[email protected]ecdfd6c52012-04-11 13:35:44814 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13815 ofu()->Truncate(
816 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09817 url, 1020));
[email protected]294dd0312012-05-11 07:35:13818 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44819
[email protected]ecdfd6c52012-04-11 13:35:44820 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13821 ofu()->Truncate(
822 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09823 url, 0));
[email protected]294dd0312012-05-11 07:35:13824 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44825
[email protected]ecdfd6c52012-04-11 13:35:44826 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13827 ofu()->Truncate(
828 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09829 url, 1021));
[email protected]294dd0312012-05-11 07:35:13830 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44831
[email protected]ecdfd6c52012-04-11 13:35:44832 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13833 ofu()->Truncate(
834 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09835 url, 1020));
[email protected]294dd0312012-05-11 07:35:13836 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44837
[email protected]ecdfd6c52012-04-11 13:35:44838 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13839 ofu()->Truncate(
840 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09841 url, 1020));
[email protected]294dd0312012-05-11 07:35:13842 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44843
[email protected]294dd0312012-05-11 07:35:13844 // quota exceeded
845 {
846 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
847 helper->context()->set_allowed_bytes_growth(
848 helper->context()->allowed_bytes_growth() - 1);
849 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09850 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13851 ASSERT_EQ(1019, ComputeTotalFileSize());
852 }
[email protected]ecdfd6c52012-04-11 13:35:44853
854 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33855 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44856 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13857 ofu()->GetLocalFilePath(
858 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09859 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44860 ASSERT_FALSE(local_path.empty());
861 ASSERT_TRUE(file_util::Delete(local_path, false));
862
[email protected]ecdfd6c52012-04-11 13:35:44863 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13864 ofu()->Truncate(
865 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09866 url, 1234));
[email protected]294dd0312012-05-11 07:35:13867 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44868}
869
[email protected]7878ece2011-09-05 11:41:49870TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09871 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32872 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16873 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32874 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49875 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09876 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10877 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32878
[email protected]0c5ebe32011-08-19 22:37:16879 // Verify that file creation requires sufficient quota for the path.
880 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09881 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32882 created = false;
[email protected]0c5ebe32011-08-19 22:37:16883 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09884 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16885 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09886 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16887 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10888 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16889
890 context.reset(NewContext(NULL));
891 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09892 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32893 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09894 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32895 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10896 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32897
[email protected]949f25a2012-06-27 01:53:09898 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32899
[email protected]0c5ebe32011-08-19 22:37:16900 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32901 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09902 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32903 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10904 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32905
906 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09907 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16908 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32909 bool exclusive = true;
910 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49911 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09912 context.get(),
[email protected]04c899f2013-02-08 08:28:42913 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09914 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10915 // 2 directories: path/ and path/to.
916 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32917
[email protected]0c5ebe32011-08-19 22:37:16918 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32919 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09920 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32921 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14922 EXPECT_FALSE(DirectoryExists(url));
923 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10924 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32925}
926
[email protected]7878ece2011-09-05 11:41:49927TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16928 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32929
930 bool exclusive = false;
931 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09932 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49933 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09934 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32935
[email protected]0c5ebe32011-08-19 22:37:16936 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32937 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08938 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32939
[email protected]007b3f82013-04-09 08:46:45940 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:14941 EXPECT_FALSE(DirectoryExists(url));
942 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16943 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49944 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32945
[email protected]0c5ebe32011-08-19 22:37:16946 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32947 exclusive = false;
948 recursive = true;
[email protected]7878ece2011-09-05 11:41:49949 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09950 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10951 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32952
[email protected]13f92f6e2012-08-13 07:39:14953 EXPECT_TRUE(DirectoryExists(url));
954 EXPECT_TRUE(PathExists(url));
955
[email protected]0c5ebe32011-08-19 22:37:16956 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49957 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42958 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14959
[email protected]0c5ebe32011-08-19 22:37:16960 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09961 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42962 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32963
964 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16965 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06966 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08967 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42968 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10969 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32970
971 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33972 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49973 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09974 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32975 EXPECT_TRUE(local_path.empty());
976 EXPECT_TRUE(file_info.is_directory);
977 EXPECT_FALSE(file_info.is_symbolic_link);
978
979 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16980 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49981 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09982 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10983 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32984
985 exclusive = true;
986 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16987 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49988 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09989 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10990 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32991
[email protected]0c5ebe32011-08-19 22:37:16992 // Verify that deleting a directory isn't stopped by zero quota, and that it
993 // frees up quota from its path.
994 context.reset(NewContext(NULL));
995 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:08996 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10997 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:09998 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16999 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321000
[email protected]949f25a2012-06-27 01:53:091001 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321002
[email protected]13f92f6e2012-08-13 07:39:141003 EXPECT_FALSE(DirectoryExists(url));
1004 EXPECT_FALSE(PathExists(url));
1005
[email protected]0c5ebe32011-08-19 22:37:161006 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091007 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491008 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091009 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321010
[email protected]0c5ebe32011-08-19 22:37:161011 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321012 exclusive = true;
1013 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161014 context.reset(NewContext(NULL));
1015 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091016 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491017 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091018 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101019 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161020
1021 context.reset(NewContext(NULL));
1022 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091023 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491024 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091025 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101026 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321027
[email protected]13f92f6e2012-08-13 07:39:141028 EXPECT_TRUE(DirectoryExists(url));
1029 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321030
1031 exclusive = true;
1032 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141033 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491034 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091035 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101036 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321037
1038 exclusive = true;
1039 recursive = false;
[email protected]949f25a2012-06-27 01:53:091040 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141041 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491042 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091043 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101044 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321045
[email protected]949f25a2012-06-27 01:53:091046 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321047
[email protected]13f92f6e2012-08-13 07:39:141048 EXPECT_FALSE(DirectoryExists(url));
1049 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321050
1051 exclusive = true;
1052 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141053 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491054 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091055 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101056 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321057
[email protected]13f92f6e2012-08-13 07:39:141058 EXPECT_TRUE(DirectoryExists(url));
1059 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321060
1061 exclusive = true;
1062 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141063 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491064 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091065 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101066 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321067}
1068
[email protected]7878ece2011-09-05 11:41:491069TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161070 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321071 bool exclusive = true;
1072 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091073 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491074 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091075 context.get(), url, exclusive, recursive));
1076 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321077}
1078
[email protected]7878ece2011-09-05 11:41:491079TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451080 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321081}
1082
[email protected]7878ece2011-09-05 11:41:491083TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091084 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321085}
1086
[email protected]7878ece2011-09-05 11:41:491087TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091088 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161089 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321090
1091 bool created = false;
1092 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091093 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321094 ASSERT_TRUE(created);
1095
[email protected]c83118f2013-05-20 04:35:421096 std::vector<DirectoryEntry> entries;
[email protected]1a647202013-01-21 15:32:251097 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301098 AsyncFileTestHelper::ReadDirectory(
1099 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321100
[email protected]949f25a2012-06-27 01:53:091101 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321102}
1103
[email protected]7878ece2011-09-05 11:41:491104TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091105 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161106 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411107
1108 base::Time last_access_time = base::Time::Now();
1109 base::Time last_modified_time = base::Time::Now();
1110
1111 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321112 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491113 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091114 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321115
[email protected]2517cfa2011-08-25 05:12:411116 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161117 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411118 bool created = false;
1119 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091120 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411121 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091122 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411123
1124 // Now test a directory:
1125 context.reset(NewContext(NULL));
1126 bool exclusive = true;
1127 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091128 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491129 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091130 url, exclusive, recursive));
1131 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161132}
1133
[email protected]7878ece2011-09-05 11:41:491134TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091135 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161136 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1137
[email protected]949f25a2012-06-27 01:53:091138 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161139 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411140 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161141 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091142 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411143 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161144 context->set_allowed_bytes_growth(1024);
1145 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091146 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411147 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091148 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161149 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1150
1151 context->set_allowed_bytes_growth(1024);
1152 bool exclusive = true;
1153 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091154 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331155 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091156 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161157 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421158 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1159 for (iterator iter = components.begin();
1160 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491161 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331162 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161163 }
1164 context.reset(NewContext(NULL));
1165 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491166 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091167 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161168 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321169}
1170
[email protected]7878ece2011-09-05 11:41:491171TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091172 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1173 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161174 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321175
1176 bool is_copy_not_move = false;
1177 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091178 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321179 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101180 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161181 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321182 is_copy_not_move = true;
1183 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091184 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321185 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101186 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091187 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321188 bool exclusive = true;
1189 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161190 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491191 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091192 context.get(),
[email protected]04c899f2013-02-08 08:28:421193 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091194 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101195 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321196 is_copy_not_move = false;
1197 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091198 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321199 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101200 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161201 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321202 is_copy_not_move = true;
1203 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091204 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321205 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101206 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321207}
1208
[email protected]7878ece2011-09-05 11:41:491209TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321210 const int64 kSourceLength = 5;
1211 const int64 kDestLength = 50;
1212
1213 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1214 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1215 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1216 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1217 test_case.is_copy_not_move);
1218 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1219 test_case.source_path);
1220 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1221 test_case.dest_path);
1222 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1223 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161224 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321225
1226 bool exclusive = false;
1227 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091228 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1229 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321230
[email protected]0c5ebe32011-08-19 22:37:161231 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491232 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091233 context.get(),
[email protected]04c899f2013-02-08 08:28:421234 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091235 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161236 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491237 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091238 context.get(),
[email protected]04c899f2013-02-08 08:28:421239 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091240 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321241
[email protected]d4905e2e2011-05-13 21:56:321242 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161243 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321244 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091245 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321246 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161247 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321248 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091249 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321250
1251 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161252 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321253 created = false;
1254 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091255 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321256 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161257 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321258 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091259 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321260 }
1261
[email protected]0c5ebe32011-08-19 22:37:161262 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491263 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091264 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101265
[email protected]d4905e2e2011-05-13 21:56:321266 if (test_case.is_copy_not_move) {
1267 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331268 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161269 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491270 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091271 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321272 EXPECT_EQ(kSourceLength, file_info.size);
1273 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091274 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321275 } else {
1276 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331277 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161278 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491279 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091280 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321281 }
1282 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331283 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491284 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091285 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321286 EXPECT_EQ(kSourceLength, file_info.size);
1287
1288 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091289 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321290 }
1291}
1292
[email protected]7878ece2011-09-05 11:41:491293TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091294 FileSystemURL src_url = CreateURLFromUTF8("src path");
1295 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161296 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1297 bool created = false;
[email protected]7878ece2011-09-05 11:41:491298 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091299 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161300
1301 bool is_copy = true;
1302 // Copy, no overwrite.
1303 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091304 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161305 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091306 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161307 context.reset(NewContext(NULL));
1308 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091309 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161310 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091311 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161312
1313 // Copy, with overwrite.
1314 context.reset(NewContext(NULL));
1315 context->set_allowed_bytes_growth(0);
1316 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091317 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161318}
1319
[email protected]7878ece2011-09-05 11:41:491320TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091321 FileSystemURL src_url = CreateURLFromUTF8("src path");
1322 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161323 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1324 bool created = false;
[email protected]7878ece2011-09-05 11:41:491325 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091326 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161327
1328 bool is_copy = false;
1329 // Move, rename, no overwrite.
1330 context.reset(NewContext(NULL));
1331 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091332 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1333 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161334 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091335 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161336 context.reset(NewContext(NULL));
1337 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091338 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1339 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161340 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091341 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161342
1343 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491344 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091345 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161346
1347 // Move, rename, with overwrite.
1348 context.reset(NewContext(NULL));
1349 context->set_allowed_bytes_growth(0);
1350 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091351 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161352}
1353
[email protected]7878ece2011-09-05 11:41:491354TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091355 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161356 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1357 bool created = false;
[email protected]7878ece2011-09-05 11:41:491358 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091359 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161360
1361 bool exclusive = true;
1362 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091363 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161364 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491365 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091366 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161367
[email protected]04c899f2013-02-08 08:28:421368 FileSystemURL dest_url = FileSystemURLAppend(
1369 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161370
1371 bool is_copy = false;
1372 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1373 // Move, no rename, no overwrite.
1374 context.reset(NewContext(NULL));
1375 context->set_allowed_bytes_growth(allowed_bytes_growth);
1376 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091377 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161378 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1379
1380 // Move, no rename, with overwrite.
1381 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491382 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091383 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161384 context.reset(NewContext(NULL));
1385 context->set_allowed_bytes_growth(allowed_bytes_growth);
1386 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091387 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161388 EXPECT_EQ(
1389 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091390 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161391 context->allowed_bytes_growth());
1392}
1393
[email protected]7878ece2011-09-05 11:41:491394TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461395 TestCopyInForeignFileHelper(false /* overwrite */);
1396 TestCopyInForeignFileHelper(true /* overwrite */);
1397}
1398
[email protected]7878ece2011-09-05 11:41:491399TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161400 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091401 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321402 bool exclusive = true;
1403 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491404 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091405 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321406
[email protected]a3ef4832013-02-02 05:12:331407 std::set<base::FilePath::StringType> files;
1408 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091409 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321410
[email protected]949f25a2012-06-27 01:53:091411 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321412
[email protected]13f92f6e2012-08-13 07:39:141413 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321414 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301415 AsyncFileTestHelper::Copy(
[email protected]1c98fdd2013-05-24 09:45:271416 file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321417
[email protected]949f25a2012-06-27 01:53:091418 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141419 EXPECT_TRUE(DirectoryExists(src_url));
1420 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321421 recursive = true;
1422 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301423 AsyncFileTestHelper::Remove(
1424 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141425 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321426}
[email protected]6b931152011-05-20 21:02:351427
[email protected]7878ece2011-09-05 11:41:491428TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1429 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1430 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161431 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261432 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161433 EXPECT_EQ(origin(), enumerator->Next());
1434 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1435 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1436 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261437 EXPECT_EQ(GURL(), enumerator->Next());
1438 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1439 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1440
1441 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161442 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261443
1444 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1445 SCOPED_TRACE(testing::Message() <<
1446 "Validating kOriginEnumerationTestRecords " << i);
1447 const OriginEnumerationTestRecord& record =
1448 kOriginEnumerationTestRecords[i];
1449 GURL origin_url(record.origin_url);
1450 origins_expected.insert(origin_url);
1451 if (record.has_temporary) {
[email protected]1c98fdd2013-05-24 09:45:271452 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1453 NewFileSystem(origin_url, kFileSystemTypeTemporary));
1454 scoped_ptr<FileSystemOperationContext> context(
1455 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261456 bool created = false;
1457 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501458 ofu()->EnsureFileExists(
1459 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271460 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501461 &created));
[email protected]fcc2d5f2011-05-23 22:06:261462 EXPECT_TRUE(created);
1463 }
1464 if (record.has_persistent) {
[email protected]1c98fdd2013-05-24 09:45:271465 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1466 NewFileSystem(origin_url, kFileSystemTypePersistent));
1467 scoped_ptr<FileSystemOperationContext> context(
1468 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261469 bool created = false;
1470 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501471 ofu()->EnsureFileExists(
1472 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271473 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501474 &created));
[email protected]fcc2d5f2011-05-23 22:06:261475 EXPECT_TRUE(created);
1476 }
1477 }
[email protected]7878ece2011-09-05 11:41:491478 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261479 EXPECT_TRUE(enumerator.get());
1480 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161481 GURL origin_url;
1482 while (!(origin_url = enumerator->Next()).is_empty()) {
1483 origins_found.insert(origin_url);
1484 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261485 bool found = false;
1486 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1487 ++i) {
1488 const OriginEnumerationTestRecord& record =
1489 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161490 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261491 continue;
1492 found = true;
1493 EXPECT_EQ(record.has_temporary,
1494 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1495 EXPECT_EQ(record.has_persistent,
1496 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1497 }
[email protected]0c5ebe32011-08-19 22:37:161498 // Deal with the default filesystem created by the test helper.
1499 if (!found && origin_url == origin()) {
1500 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1501 EXPECT_EQ(true,
1502 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181503 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161504 found = true;
1505 }
[email protected]fcc2d5f2011-05-23 22:06:261506 EXPECT_TRUE(found);
1507 }
1508
1509 std::set<GURL> diff;
1510 std::set_symmetric_difference(origins_expected.begin(),
1511 origins_expected.end(), origins_found.begin(), origins_found.end(),
1512 inserter(diff, diff.begin()));
1513 EXPECT_TRUE(diff.empty());
1514}
[email protected]0c5ebe32011-08-19 22:37:161515
[email protected]7878ece2011-09-05 11:41:491516TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161517 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1518
1519 int64 expected_quota = 0;
1520
[email protected]f83b5b72012-01-27 10:26:561521 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011522 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561523 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331524 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501525 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161526 if (test_case.is_directory) {
1527 bool exclusive = true;
1528 bool recursive = false;
1529 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091530 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501531 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161532 } else {
1533 bool created = false;
1534 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091535 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501536 &created));
[email protected]0c5ebe32011-08-19 22:37:161537 ASSERT_TRUE(created);
1538 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501539 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091540 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501541 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161542 expected_quota += test_case.data_file_size;
1543 }
1544 }
[email protected]022d2702012-05-14 16:04:261545
1546 // Usually raw size in usage cache and the usage returned by QuotaUtil
1547 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161548 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261549 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1550
[email protected]0c5ebe32011-08-19 22:37:161551 RevokeUsageCache();
1552 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261553 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1554
1555 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161556 GetUsageFromQuotaManager();
1557 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261558 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161559 EXPECT_EQ(expected_quota, usage());
1560}
[email protected]34583332011-08-31 08:59:471561
[email protected]7878ece2011-09-05 11:41:491562TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091563 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1564 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471565
1566 scoped_ptr<FileSystemOperationContext> context;
1567 base::PlatformFile file;
1568 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331569 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471570 bool created = false;
1571
1572 // Create a non-empty file.
1573 context.reset(NewContext(NULL));
1574 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491575 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471576 EXPECT_TRUE(created);
1577 context.reset(NewContext(NULL));
1578 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491579 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471580 context.reset(NewContext(NULL));
1581 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491582 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471583 context.get(), kPath1, &file_info, &data_path));
1584 EXPECT_EQ(10, file_info.size);
1585
1586 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491587 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471588
1589 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141590 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471591 context.reset(NewContext(NULL));
1592 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491593 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471594 EXPECT_TRUE(created);
1595 context.reset(NewContext(NULL));
1596 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491597 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471598 context.get(), kPath1, &file_info, &data_path));
1599 EXPECT_EQ(0, file_info.size);
1600
1601 // Make another broken file to |kPath2|.
1602 context.reset(NewContext(NULL));
1603 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491604 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471605 EXPECT_TRUE(created);
1606
1607 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491608 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471609
1610 // Repair broken |kPath1|.
1611 context.reset(NewContext(NULL));
1612 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491613 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471614 base::Time::Now()));
1615 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491616 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471617 EXPECT_TRUE(created);
1618
1619 // Copy from sound |kPath1| to broken |kPath2|.
1620 context.reset(NewContext(NULL));
1621 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491622 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501623 true /* copy */));
[email protected]34583332011-08-31 08:59:471624
[email protected]7878ece2011-09-05 11:41:491625 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471626 context.reset(NewContext(NULL));
1627 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491628 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471629 context.get(), kPath1,
1630 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1631 &file, &created));
1632 EXPECT_TRUE(created);
1633 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1634 EXPECT_EQ(0, file_info.size);
1635 EXPECT_TRUE(base::ClosePlatformFile(file));
1636}
[email protected]9dfdc0e32011-09-02 06:07:441637
[email protected]7878ece2011-09-05 11:41:491638TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091639 const FileSystemURL kPath[] = {
1640 CreateURLFromUTF8("foo"),
1641 CreateURLFromUTF8("bar"),
1642 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441643 };
[email protected]a3ef4832013-02-02 05:12:331644 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441645 scoped_ptr<FileSystemOperationContext> context;
1646
1647 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1648 bool created = false;
1649 context.reset(NewContext(NULL));
1650 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491651 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441652 EXPECT_TRUE(created);
1653 }
1654
[email protected]c83118f2013-05-20 04:35:421655 std::vector<DirectoryEntry> entries;
[email protected]9dfdc0e32011-09-02 06:07:441656 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301657 AsyncFileTestHelper::ReadDirectory(
1658 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441659 EXPECT_EQ(3u, entries.size());
1660
[email protected]a3ef4832013-02-02 05:12:331661 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441662 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491663 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441664 EXPECT_TRUE(file_util::Delete(local_path, false));
1665
[email protected]9dfdc0e32011-09-02 06:07:441666 entries.clear();
1667 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301668 AsyncFileTestHelper::ReadDirectory(
1669 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441670 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1671}
[email protected]fad625e2f2011-12-08 05:38:031672
1673TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1674 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091675 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031676
1677 // Create working directory.
1678 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091679 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031680
1681 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421682 FileSystemURL url(FileSystemURLAppendUTF8(
1683 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031684 bool created = false;
[email protected]949f25a2012-06-27 01:53:091685 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031686 context.reset(NewContext(NULL));
1687 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091688 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031689 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091690 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031691
1692 // non create case.
1693 created = true;
[email protected]949f25a2012-06-27 01:53:091694 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031695 context.reset(NewContext(NULL));
1696 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091697 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031698 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091699 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031700
1701 // fail case.
[email protected]04c899f2013-02-08 08:28:421702 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031703 context.reset(NewContext(NULL));
1704 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091705 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031706
[email protected]949f25a2012-06-27 01:53:091707 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031708 context.reset(NewContext(NULL));
1709 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091710 ofu()->EnsureFileExists(context.get(), url, &created));
1711 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031712
1713 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421714 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391715 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031716 created = false;
[email protected]949f25a2012-06-27 01:53:091717 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031718 context.reset(NewContext(NULL));
1719 EXPECT_EQ(base::PLATFORM_FILE_OK,
1720 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091721 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031722 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1723 &file_handle, &created));
1724 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1725 EXPECT_TRUE(created);
1726 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091727 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031728
1729 // open case.
1730 file_handle = base::kInvalidPlatformFileValue;
1731 created = true;
[email protected]949f25a2012-06-27 01:53:091732 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031733 context.reset(NewContext(NULL));
1734 EXPECT_EQ(base::PLATFORM_FILE_OK,
1735 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091736 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031737 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1738 &file_handle, &created));
1739 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1740 EXPECT_FALSE(created);
1741 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091742 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031743
1744 // fail case
1745 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091746 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031747 context.reset(NewContext(NULL));
1748 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1749 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091750 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031751 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1752 &file_handle, &created));
1753 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091754 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031755
1756 // CreateDirectory, create case.
1757 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421758 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1759 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091760 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031761 context.reset(NewContext(NULL));
1762 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091763 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031764 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091765 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031766
1767 // create subdir case.
1768 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421769 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091770 ClearTimestamp(dir_url);
1771 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031772 context.reset(NewContext(NULL));
1773 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091774 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031775 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091776 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1777 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031778
1779 // fail case.
[email protected]04c899f2013-02-08 08:28:421780 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091781 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031782 context.reset(NewContext(NULL));
1783 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091784 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031785 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091786 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031787
1788 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421789 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1790 FileSystemURL src_path = FileSystemURLAppendUTF8(
1791 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031792 context.reset(NewContext(NULL));
1793 EXPECT_EQ(base::PLATFORM_FILE_OK,
1794 ofu()->EnsureFileExists(context.get(), src_path, &created));
1795 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331796 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031797 context.reset(NewContext(NULL));
1798 EXPECT_EQ(base::PLATFORM_FILE_OK,
1799 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1800
[email protected]949f25a2012-06-27 01:53:091801 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031802 context.reset(NewContext(NULL));
1803 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501804 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131805 src_local_path,
[email protected]949f25a2012-06-27 01:53:091806 url));
1807 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031808}
1809
1810TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1811 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091812 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031813
1814 // Create working directory.
1815 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091816 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031817
1818 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421819 FileSystemURL url = FileSystemURLAppendUTF8(
1820 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031821 bool created = false;
1822 context.reset(NewContext(NULL));
1823 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091824 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031825 EXPECT_TRUE(created);
1826
[email protected]949f25a2012-06-27 01:53:091827 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031828 context.reset(NewContext(NULL));
1829 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091830 ofu()->DeleteFile(context.get(), url));
1831 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031832
1833 // fail case.
[email protected]949f25a2012-06-27 01:53:091834 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031835 context.reset(NewContext(NULL));
1836 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091837 ofu()->DeleteFile(context.get(), url));
1838 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031839
[email protected]bab213be2013-01-23 15:13:081840 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421841 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1842 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031843 context.reset(NewContext(NULL));
1844 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091845 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031846 created = false;
1847 context.reset(NewContext(NULL));
1848 EXPECT_EQ(base::PLATFORM_FILE_OK,
1849 ofu()->EnsureFileExists(context.get(), file_path, &created));
1850 EXPECT_TRUE(created);
1851
[email protected]949f25a2012-06-27 01:53:091852 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031853 context.reset(NewContext(NULL));
1854 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081855 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091856 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031857
1858 // delete case.
1859 context.reset(NewContext(NULL));
1860 EXPECT_EQ(base::PLATFORM_FILE_OK,
1861 ofu()->DeleteFile(context.get(), file_path));
1862
[email protected]949f25a2012-06-27 01:53:091863 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031864 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081865 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091866 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031867}
1868
1869TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1870 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091871 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031872 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091873 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031874 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091875 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031876 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091877 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031878}
[email protected]a3938912012-03-27 14:00:551879
1880TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091881 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421882 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1883 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551884
1885 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1886 EXPECT_EQ(base::PLATFORM_FILE_OK,
1887 ofu()->CreateDirectory(context.get(), dir, false, false));
1888
1889 bool created = false;
1890 context.reset(NewContext(NULL));
1891 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091892 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551893 EXPECT_TRUE(created);
1894
1895 context.reset(NewContext(NULL));
1896 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091897 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551898
[email protected]a3ef4832013-02-02 05:12:331899 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551900 context.reset(NewContext(NULL));
1901 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091902 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551903 EXPECT_FALSE(file_path.empty());
1904
1905 context.reset(NewContext(NULL));
1906 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091907 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551908 base::Time::Now() + base::TimeDelta::FromHours(1),
1909 base::Time()));
1910
1911 context.reset(NewContext(NULL));
1912 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1913 ofu()->CreateFileEnumerator(context.get(), dir, false));
1914
1915 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331916 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551917 while (!(file_path_each = file_enum->Next()).empty()) {
1918 context.reset(NewContext(NULL));
1919 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331920 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551921 EXPECT_EQ(base::PLATFORM_FILE_OK,
1922 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421923 FileSystemURL::CreateForTest(
1924 dir.origin(),
1925 dir.mount_type(),
1926 file_path_each),
[email protected]a3938912012-03-27 14:00:551927 &file_info, &file_path));
1928 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1929 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1930 EXPECT_EQ(file_info.size, file_enum->Size());
1931 ++count;
1932 }
1933 EXPECT_EQ(2, count);
1934}
[email protected]294dd0312012-05-11 07:35:131935
[email protected]58ac5272013-02-15 10:05:031936// crbug.com/176470
1937#if defined(OS_WIN) || defined(OS_ANDROID)
1938#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1939#else
1940#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1941#endif
1942TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091943 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1944 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1945 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1946 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131947 bool created;
1948
1949 int64 expected_total_file_size = 0;
1950 ASSERT_EQ(base::PLATFORM_FILE_OK,
1951 ofu()->EnsureFileExists(
1952 AllowUsageIncrease(PathCost(from_file))->context(),
1953 from_file, &created));
1954 ASSERT_TRUE(created);
1955 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1956
1957 ASSERT_EQ(base::PLATFORM_FILE_OK,
1958 ofu()->EnsureFileExists(
1959 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1960 obstacle_file, &created));
1961 ASSERT_TRUE(created);
1962 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1963
1964 int64 from_file_size = 1020;
1965 expected_total_file_size += from_file_size;
1966 ASSERT_EQ(base::PLATFORM_FILE_OK,
1967 ofu()->Truncate(
1968 AllowUsageIncrease(from_file_size)->context(),
1969 from_file, from_file_size));
1970 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1971
1972 int64 obstacle_file_size = 1;
1973 expected_total_file_size += obstacle_file_size;
1974 ASSERT_EQ(base::PLATFORM_FILE_OK,
1975 ofu()->Truncate(
1976 AllowUsageIncrease(obstacle_file_size)->context(),
1977 obstacle_file, obstacle_file_size));
1978 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1979
1980 int64 to_file1_size = from_file_size;
1981 expected_total_file_size += to_file1_size;
1982 ASSERT_EQ(base::PLATFORM_FILE_OK,
1983 ofu()->CopyOrMoveFile(
1984 AllowUsageIncrease(
1985 PathCost(to_file1) + to_file1_size)->context(),
1986 from_file, to_file1, true /* copy */));
1987 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1988
1989 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1990 ofu()->CopyOrMoveFile(
1991 DisallowUsageIncrease(
1992 PathCost(to_file2) + from_file_size)->context(),
1993 from_file, to_file2, true /* copy */));
1994 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1995
1996 int64 old_obstacle_file_size = obstacle_file_size;
1997 obstacle_file_size = from_file_size;
1998 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1999 ASSERT_EQ(base::PLATFORM_FILE_OK,
2000 ofu()->CopyOrMoveFile(
2001 AllowUsageIncrease(
2002 obstacle_file_size - old_obstacle_file_size)->context(),
2003 from_file, obstacle_file, true /* copy */));
2004 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2005
2006 int64 old_from_file_size = from_file_size;
2007 from_file_size = old_from_file_size - 1;
2008 expected_total_file_size += from_file_size - old_from_file_size;
2009 ASSERT_EQ(base::PLATFORM_FILE_OK,
2010 ofu()->Truncate(
2011 AllowUsageIncrease(
2012 from_file_size - old_from_file_size)->context(),
2013 from_file, from_file_size));
2014 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2015
2016 // quota exceeded
2017 {
2018 old_obstacle_file_size = obstacle_file_size;
2019 obstacle_file_size = from_file_size;
2020 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2021 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2022 obstacle_file_size - old_obstacle_file_size);
2023 helper->context()->set_allowed_bytes_growth(
2024 helper->context()->allowed_bytes_growth() - 1);
2025 ASSERT_EQ(base::PLATFORM_FILE_OK,
2026 ofu()->CopyOrMoveFile(
2027 helper->context(),
2028 from_file, obstacle_file, true /* copy */));
2029 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2030 }
2031}
2032
2033TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092034 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2035 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2036 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132037 bool created;
2038
2039 int64 expected_total_file_size = 0;
2040 ASSERT_EQ(base::PLATFORM_FILE_OK,
2041 ofu()->EnsureFileExists(
2042 AllowUsageIncrease(PathCost(from_file))->context(),
2043 from_file, &created));
2044 ASSERT_TRUE(created);
2045 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2046
2047 int64 from_file_size = 1020;
2048 expected_total_file_size += from_file_size;
2049 ASSERT_EQ(base::PLATFORM_FILE_OK,
2050 ofu()->Truncate(
2051 AllowUsageIncrease(from_file_size)->context(),
2052 from_file, from_file_size));
2053 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2054
2055 int64 to_file_size ALLOW_UNUSED = from_file_size;
2056 from_file_size = 0;
2057 ASSERT_EQ(base::PLATFORM_FILE_OK,
2058 ofu()->CopyOrMoveFile(
2059 AllowUsageIncrease(-PathCost(from_file) +
2060 PathCost(to_file))->context(),
2061 from_file, to_file, false /* move */));
2062 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2063
2064 ASSERT_EQ(base::PLATFORM_FILE_OK,
2065 ofu()->EnsureFileExists(
2066 AllowUsageIncrease(PathCost(from_file))->context(),
2067 from_file, &created));
2068 ASSERT_TRUE(created);
2069 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2070
2071 ASSERT_EQ(base::PLATFORM_FILE_OK,
2072 ofu()->EnsureFileExists(
2073 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2074 obstacle_file, &created));
2075 ASSERT_TRUE(created);
2076 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2077
2078 from_file_size = 1020;
2079 expected_total_file_size += from_file_size;
2080 ASSERT_EQ(base::PLATFORM_FILE_OK,
2081 ofu()->Truncate(
2082 AllowUsageIncrease(from_file_size)->context(),
2083 from_file, from_file_size));
2084 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2085
2086 int64 obstacle_file_size = 1;
2087 expected_total_file_size += obstacle_file_size;
2088 ASSERT_EQ(base::PLATFORM_FILE_OK,
2089 ofu()->Truncate(
2090 AllowUsageIncrease(1)->context(),
2091 obstacle_file, obstacle_file_size));
2092 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2093
2094 int64 old_obstacle_file_size = obstacle_file_size;
2095 obstacle_file_size = from_file_size;
2096 from_file_size = 0;
2097 expected_total_file_size -= old_obstacle_file_size;
2098 ASSERT_EQ(base::PLATFORM_FILE_OK,
2099 ofu()->CopyOrMoveFile(
2100 AllowUsageIncrease(
2101 -old_obstacle_file_size - PathCost(from_file))->context(),
2102 from_file, obstacle_file,
2103 false /* move */));
2104 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2105
2106 ASSERT_EQ(base::PLATFORM_FILE_OK,
2107 ofu()->EnsureFileExists(
2108 AllowUsageIncrease(PathCost(from_file))->context(),
2109 from_file, &created));
2110 ASSERT_TRUE(created);
2111 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2112
2113 from_file_size = 10;
2114 expected_total_file_size += from_file_size;
2115 ASSERT_EQ(base::PLATFORM_FILE_OK,
2116 ofu()->Truncate(
2117 AllowUsageIncrease(from_file_size)->context(),
2118 from_file, from_file_size));
2119 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2120
2121 // quota exceeded even after operation
2122 old_obstacle_file_size = obstacle_file_size;
2123 obstacle_file_size = from_file_size;
2124 from_file_size = 0;
2125 expected_total_file_size -= old_obstacle_file_size;
2126 scoped_ptr<FileSystemOperationContext> context =
2127 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2128 ASSERT_EQ(base::PLATFORM_FILE_OK,
2129 ofu()->CopyOrMoveFile(
2130 context.get(), from_file, obstacle_file, false /* move */));
2131 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2132 context.reset();
2133}
2134
2135TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092136 FileSystemURL dir(CreateURLFromUTF8("dir"));
2137 FileSystemURL file(CreateURLFromUTF8("file"));
2138 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2139 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132140 bool created;
2141
2142 ASSERT_EQ(base::PLATFORM_FILE_OK,
2143 ofu()->EnsureFileExists(
2144 AllowUsageIncrease(PathCost(file))->context(),
2145 file, &created));
2146 ASSERT_TRUE(created);
2147 ASSERT_EQ(0, ComputeTotalFileSize());
2148
2149 ASSERT_EQ(base::PLATFORM_FILE_OK,
2150 ofu()->CreateDirectory(
2151 AllowUsageIncrease(PathCost(dir))->context(),
2152 dir, false, false));
2153 ASSERT_EQ(0, ComputeTotalFileSize());
2154
2155 ASSERT_EQ(base::PLATFORM_FILE_OK,
2156 ofu()->EnsureFileExists(
2157 AllowUsageIncrease(PathCost(dfile1))->context(),
2158 dfile1, &created));
2159 ASSERT_TRUE(created);
2160 ASSERT_EQ(0, ComputeTotalFileSize());
2161
2162 ASSERT_EQ(base::PLATFORM_FILE_OK,
2163 ofu()->EnsureFileExists(
2164 AllowUsageIncrease(PathCost(dfile2))->context(),
2165 dfile2, &created));
2166 ASSERT_TRUE(created);
2167 ASSERT_EQ(0, ComputeTotalFileSize());
2168
2169 ASSERT_EQ(base::PLATFORM_FILE_OK,
2170 ofu()->Truncate(
2171 AllowUsageIncrease(340)->context(),
2172 file, 340));
2173 ASSERT_EQ(340, ComputeTotalFileSize());
2174
2175 ASSERT_EQ(base::PLATFORM_FILE_OK,
2176 ofu()->Truncate(
2177 AllowUsageIncrease(1020)->context(),
2178 dfile1, 1020));
2179 ASSERT_EQ(1360, ComputeTotalFileSize());
2180
2181 ASSERT_EQ(base::PLATFORM_FILE_OK,
2182 ofu()->Truncate(
2183 AllowUsageIncrease(120)->context(),
2184 dfile2, 120));
2185 ASSERT_EQ(1480, ComputeTotalFileSize());
2186
2187 ASSERT_EQ(base::PLATFORM_FILE_OK,
2188 ofu()->DeleteFile(
2189 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2190 file));
2191 ASSERT_EQ(1140, ComputeTotalFileSize());
2192
2193 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302194 AsyncFileTestHelper::Remove(
2195 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132196 ASSERT_EQ(0, ComputeTotalFileSize());
2197}
[email protected]7d78be12012-05-24 07:07:262198
2199TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092200 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262201 base::PlatformFile file_handle;
2202 bool created;
2203
2204 // Creating a file.
2205 ASSERT_EQ(base::PLATFORM_FILE_OK,
2206 ofu()->EnsureFileExists(
2207 AllowUsageIncrease(PathCost(file))->context(),
2208 file, &created));
2209 ASSERT_TRUE(created);
2210 ASSERT_EQ(0, ComputeTotalFileSize());
2211
2212 // Opening it, which shouldn't change the usage.
2213 ASSERT_EQ(base::PLATFORM_FILE_OK,
2214 ofu()->CreateOrOpen(
2215 AllowUsageIncrease(0)->context(), file,
2216 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2217 &file_handle, &created));
2218 ASSERT_EQ(0, ComputeTotalFileSize());
2219 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2220
2221 const int length = 33;
2222 ASSERT_EQ(base::PLATFORM_FILE_OK,
2223 ofu()->Truncate(
2224 AllowUsageIncrease(length)->context(), file, length));
2225 ASSERT_EQ(length, ComputeTotalFileSize());
2226
2227 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2228 ASSERT_EQ(base::PLATFORM_FILE_OK,
2229 ofu()->CreateOrOpen(
2230 AllowUsageIncrease(-length)->context(), file,
2231 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2232 &file_handle, &created));
2233 ASSERT_EQ(0, ComputeTotalFileSize());
2234 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2235
2236 // Extending the file again.
2237 ASSERT_EQ(base::PLATFORM_FILE_OK,
2238 ofu()->Truncate(
2239 AllowUsageIncrease(length)->context(), file, length));
2240 ASSERT_EQ(length, ComputeTotalFileSize());
2241
2242 // Opening it with TRUNCATED flag, which should truncate the file size.
2243 ASSERT_EQ(base::PLATFORM_FILE_OK,
2244 ofu()->CreateOrOpen(
2245 AllowUsageIncrease(-length)->context(), file,
2246 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2247 &file_handle, &created));
2248 ASSERT_EQ(0, ComputeTotalFileSize());
2249 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2250}
[email protected]c4e6f9c2012-09-09 17:42:102251
2252} // namespace fileapi