blob: f1fa1b1ff7f99509c3f0575730cfd2c9a0218a1e [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]ff875be52013-06-02 23:47:38135 quota_manager_ = new quota::QuotaManager(false /* is_incognito */,
136 data_dir_.path(),
137 base::MessageLoopProxy::current(),
138 base::MessageLoopProxy::current(),
139 storage_policy.get());
[email protected]0c5ebe32011-08-19 22:37:16140
[email protected]1c98fdd2013-05-24 09:45:27141 // Every time we create a new sandbox_file_system helper,
142 // it creates another context, which creates another path manager,
143 // another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49144 // another OFU. We need to pass in the context to skip all that.
[email protected]420fb562013-04-18 01:46:34145 file_system_context_ = CreateFileSystemContextForTesting(
[email protected]0c5ebe32011-08-19 22:37:16146 quota_manager_->proxy(),
[email protected]420fb562013-04-18 01:46:34147 data_dir_.path());
[email protected]0c5ebe32011-08-19 22:37:16148
[email protected]1c98fdd2013-05-24 09:45:27149 sandbox_file_system_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10150
151 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32152 }
153
[email protected]7fd8fa4f2013-02-07 05:43:50154 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55155 quota_manager_ = NULL;
[email protected]1c98fdd2013-05-24 09:45:27156 sandbox_file_system_.TearDown();
[email protected]e7e46732012-01-05 11:45:55157 }
158
[email protected]294dd0312012-05-11 07:35:13159 scoped_ptr<FileSystemOperationContext> LimitedContext(
160 int64 allowed_bytes_growth) {
161 scoped_ptr<FileSystemOperationContext> context(
[email protected]1c98fdd2013-05-24 09:45:27162 sandbox_file_system_.NewOperationContext());
[email protected]294dd0312012-05-11 07:35:13163 context->set_allowed_bytes_growth(allowed_bytes_growth);
164 return context.Pass();
165 }
166
167 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
168 return LimitedContext(kint64max);
169 }
170
[email protected]02a60542012-07-24 20:05:33171 FileSystemOperationContext* NewContext(
[email protected]1c98fdd2013-05-24 09:45:27172 SandboxFileSystemTestHelper* file_system) {
[email protected]c4e6f9c2012-09-09 17:42:10173 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16174 FileSystemOperationContext* context;
[email protected]1c98fdd2013-05-24 09:45:27175 if (file_system)
176 context = file_system->NewOperationContext();
[email protected]0c5ebe32011-08-19 22:37:16177 else
[email protected]1c98fdd2013-05-24 09:45:27178 context = sandbox_file_system_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46179 // Setting allowed_bytes_growth big enough for all tests.
180 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10181 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32182 return context;
183 }
184
[email protected]c4e6f9c2012-09-09 17:42:10185 const ChangeObserverList& change_observers() const {
186 return change_observers_;
187 }
188
189 MockFileChangeObserver* change_observer() {
190 return &change_observer_;
191 }
192
[email protected]0c5ebe32011-08-19 22:37:16193 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49194 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16195 // Use this for tests which need to run in multiple origins; we need a test
196 // helper per origin.
[email protected]1c98fdd2013-05-24 09:45:27197 SandboxFileSystemTestHelper* NewFileSystem(
[email protected]0c5ebe32011-08-19 22:37:16198 const GURL& origin, fileapi::FileSystemType type) {
[email protected]1c98fdd2013-05-24 09:45:27199 SandboxFileSystemTestHelper* file_system =
200 new SandboxFileSystemTestHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16201
[email protected]1c98fdd2013-05-24 09:45:27202 file_system->SetUp(file_system_context_.get());
203 return file_system;
[email protected]0c5ebe32011-08-19 22:37:16204 }
205
[email protected]7878ece2011-09-05 11:41:49206 ObfuscatedFileUtil* ofu() {
[email protected]1c98fdd2013-05-24 09:45:27207 return static_cast<ObfuscatedFileUtil*>(sandbox_file_system_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32208 }
209
[email protected]a3ef4832013-02-02 05:12:33210 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35211 return data_dir_.path();
212 }
213
[email protected]0c5ebe32011-08-19 22:37:16214 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26215 return origin_;
216 }
217
218 fileapi::FileSystemType type() const {
219 return type_;
220 }
221
[email protected]294dd0312012-05-11 07:35:13222 int64 ComputeTotalFileSize() {
[email protected]1c98fdd2013-05-24 09:45:27223 return sandbox_file_system_.ComputeCurrentOriginUsage() -
224 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]294dd0312012-05-11 07:35:13225 }
226
[email protected]0c5ebe32011-08-19 22:37:16227 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30228 int64 quota = -1;
[email protected]ff875be52013-06-02 23:47:38229 quota_status_ =
230 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
231 origin(),
232 sandbox_file_system_.type(),
233 &usage_,
234 &quota);
[email protected]0c5ebe32011-08-19 22:37:16235 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
236 }
237
238 void RevokeUsageCache() {
[email protected]1c98fdd2013-05-24 09:45:27239 quota_manager_->ResetUsageTracker(sandbox_file_system_.storage_type());
240 usage_cache()->Delete(sandbox_file_system_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26241 }
242
243 int64 SizeByQuotaUtil() {
[email protected]1c98fdd2013-05-24 09:45:27244 return sandbox_file_system_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16245 }
246
247 int64 SizeInUsageFile() {
[email protected]4cc586b2013-05-07 12:43:32248 base::MessageLoop::current()->RunUntilIdle();
[email protected]bf97e8b2013-04-14 15:00:13249 int64 usage = 0;
[email protected]1c98fdd2013-05-24 09:45:27250 return usage_cache()->GetUsage(
251 sandbox_file_system_.GetUsageCachePath(), &usage) ? usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16252 }
253
[email protected]13f92f6e2012-08-13 07:39:14254 bool PathExists(const FileSystemURL& url) {
255 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21256 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33257 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21258 base::PlatformFileError error = ofu()->GetFileInfo(
259 context.get(), url, &file_info, &platform_path);
260 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14261 }
262
263 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30264 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14265 }
266
[email protected]0c5ebe32011-08-19 22:37:16267 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31268 FileSystemUsageCache* usage_cache() {
[email protected]1c98fdd2013-05-24 09:45:27269 return sandbox_file_system_.usage_cache();
[email protected]06226172013-03-14 15:39:31270 }
[email protected]0c5ebe32011-08-19 22:37:16271
[email protected]949f25a2012-06-27 01:53:09272 FileSystemURL CreateURLFromUTF8(const std::string& path) {
[email protected]1c98fdd2013-05-24 09:45:27273 return sandbox_file_system_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50274 }
275
[email protected]949f25a2012-06-27 01:53:09276 int64 PathCost(const FileSystemURL& url) {
277 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13278 }
279
[email protected]a3ef4832013-02-02 05:12:33280 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]1c98fdd2013-05-24 09:45:27281 return sandbox_file_system_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50282 }
283
[email protected]d4905e2e2011-05-13 21:56:32284 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39285 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16286 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33287 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49288 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09289 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32290
291 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33292 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49293 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09294 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32295 EXPECT_EQ(data_path, local_path);
296 EXPECT_TRUE(FileExists(data_path));
297 EXPECT_EQ(0, GetSize(data_path));
298
299 const char data[] = "test data";
300 const int length = arraysize(data) - 1;
301
302 if (base::kInvalidPlatformFileValue == file_handle) {
303 bool created = true;
[email protected]403ada82013-01-08 07:51:39304 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32305 file_handle = base::CreatePlatformFile(
306 data_path,
307 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
308 &created,
309 &error);
[email protected]81b7f662011-05-26 00:54:46310 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32311 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
312 EXPECT_FALSE(created);
313 }
314 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
315 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
316
317 base::PlatformFileInfo file_info1;
318 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16319 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49320 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09321 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32322 EXPECT_EQ(data_path, local_path);
323
324 EXPECT_FALSE(file_info0.is_directory);
325 EXPECT_FALSE(file_info1.is_directory);
326 EXPECT_FALSE(file_info0.is_symbolic_link);
327 EXPECT_FALSE(file_info1.is_symbolic_link);
328 EXPECT_EQ(0, file_info0.size);
329 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32330 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32331
[email protected]0c5ebe32011-08-19 22:37:16332 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49333 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09334 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32335 EXPECT_EQ(length * 2, GetSize(data_path));
336
[email protected]0c5ebe32011-08-19 22:37:16337 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49338 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09339 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16340 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32341 }
342
343 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09344 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33345 const std::set<base::FilePath::StringType>& files,
346 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32347 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33348 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32349 for (iter = files.begin(); iter != files.end(); ++iter) {
350 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16351 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32352 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49353 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42354 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32355 &created));
356 ASSERT_FALSE(created);
357 }
358 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16359 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14360 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42361 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32362 }
363 }
364
[email protected]294dd0312012-05-11 07:35:13365 class UsageVerifyHelper {
366 public:
367 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]1c98fdd2013-05-24 09:45:27368 SandboxFileSystemTestHelper* file_system,
[email protected]294dd0312012-05-11 07:35:13369 int64 expected_usage)
370 : context_(context.Pass()),
[email protected]1c98fdd2013-05-24 09:45:27371 sandbox_file_system_(file_system),
[email protected]294dd0312012-05-11 07:35:13372 expected_usage_(expected_usage) {}
373
374 ~UsageVerifyHelper() {
[email protected]4cc586b2013-05-07 12:43:32375 base::MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13376 Check();
377 }
378
379 FileSystemOperationContext* context() {
380 return context_.get();
381 }
382
383 private:
384 void Check() {
385 ASSERT_EQ(expected_usage_,
[email protected]1c98fdd2013-05-24 09:45:27386 sandbox_file_system_->GetCachedOriginUsage());
[email protected]294dd0312012-05-11 07:35:13387 }
388
389 scoped_ptr<FileSystemOperationContext> context_;
[email protected]1c98fdd2013-05-24 09:45:27390 SandboxFileSystemTestHelper* sandbox_file_system_;
[email protected]294dd0312012-05-11 07:35:13391 int64 expected_usage_;
392 };
393
394 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27395 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13396 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
397 LimitedContext(requested_growth),
[email protected]1c98fdd2013-05-24 09:45:27398 &sandbox_file_system_, usage + requested_growth));
[email protected]294dd0312012-05-11 07:35:13399 }
400
401 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27402 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13403 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
[email protected]1c98fdd2013-05-24 09:45:27404 LimitedContext(requested_growth - 1), &sandbox_file_system_, usage));
[email protected]294dd0312012-05-11 07:35:13405 }
406
[email protected]d4905e2e2011-05-13 21:56:32407 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09408 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33409 std::set<base::FilePath::StringType>* files,
410 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32411 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42412 std::vector<DirectoryEntry> entries;
[email protected]d4905e2e2011-05-13 21:56:32413 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30414 AsyncFileTestHelper::ReadDirectory(
415 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32416 EXPECT_EQ(0UL, entries.size());
417
418 files->clear();
[email protected]89ee4272011-05-16 18:45:17419 files->insert(FILE_PATH_LITERAL("first"));
420 files->insert(FILE_PATH_LITERAL("second"));
421 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32422 directories->clear();
[email protected]89ee4272011-05-16 18:45:17423 directories->insert(FILE_PATH_LITERAL("fourth"));
424 directories->insert(FILE_PATH_LITERAL("fifth"));
425 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33426 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32427 for (iter = files->begin(); iter != files->end(); ++iter) {
428 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16429 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32430 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49431 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50432 context.get(),
[email protected]04c899f2013-02-08 08:28:42433 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50434 &created));
[email protected]d4905e2e2011-05-13 21:56:32435 ASSERT_TRUE(created);
436 }
437 for (iter = directories->begin(); iter != directories->end(); ++iter) {
438 bool exclusive = true;
439 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16440 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32441 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49442 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42443 context.get(),
444 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09445 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32446 }
[email protected]949f25a2012-06-27 01:53:09447 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32448 }
449
[email protected]949f25a2012-06-27 01:53:09450 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33451 std::set<base::FilePath::StringType> files;
452 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09453 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32454
455 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42456 std::vector<DirectoryEntry> entries;
[email protected]0c5ebe32011-08-19 22:37:16457 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32458 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30459 AsyncFileTestHelper::ReadDirectory(
460 file_system_context(), root_url, &entries));
[email protected]c83118f2013-05-20 04:35:42461 std::vector<DirectoryEntry>::iterator entry_iter;
[email protected]d4905e2e2011-05-13 21:56:32462 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10463 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32464 for (entry_iter = entries.begin(); entry_iter != entries.end();
465 ++entry_iter) {
[email protected]c83118f2013-05-20 04:35:42466 const DirectoryEntry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31467 std::set<base::FilePath::StringType>::iterator iter =
468 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32469 if (iter != files.end()) {
470 EXPECT_FALSE(entry.is_directory);
471 files.erase(iter);
472 continue;
473 }
[email protected]89ee4272011-05-16 18:45:17474 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32475 EXPECT_FALSE(directories.end() == iter);
476 EXPECT_TRUE(entry.is_directory);
477 directories.erase(iter);
478 }
479 }
480
[email protected]949f25a2012-06-27 01:53:09481 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41482 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32483 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16484
[email protected]2517cfa2011-08-25 05:12:41485 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32486 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49487 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09488 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10489 // Currently we fire no change notifications for Touch.
490 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33491 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32492 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16493 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49494 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09495 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32496 // We compare as time_t here to lower our resolution, to avoid false
497 // negatives caused by conversion to the local filesystem's native
498 // representation and back.
499 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
500
[email protected]0c5ebe32011-08-19 22:37:16501 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32502 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41503 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32504 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49505 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09506 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10507 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16508 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49509 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09510 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32511 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49512 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41513 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32514 }
515
[email protected]81b7f662011-05-26 00:54:46516 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23517 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46518 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33519 base::FilePath root_file_path = source_dir.path();
520 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09521 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46522 int64 src_file_length = 87;
523
524 base::PlatformFileError error_code;
525 bool created = false;
526 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
527 base::PlatformFile file_handle =
528 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50529 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46530 EXPECT_TRUE(created);
531 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
532 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
533 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
534 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
535
536 scoped_ptr<FileSystemOperationContext> context;
537
538 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16539 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46540 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09541 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46542 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10543
544 // We must have observed one (and only one) create_file_count.
545 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
546 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46547 }
548
[email protected]0c5ebe32011-08-19 22:37:16549 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09550 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16551 if (!overwrite) {
552 // Verify that file creation requires sufficient quota for the path.
553 context.reset(NewContext(NULL));
554 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
555 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13556 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09557 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16558 }
559
560 context.reset(NewContext(NULL));
561 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46562 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13563 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09564 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16565
[email protected]13f92f6e2012-08-13 07:39:14566 EXPECT_TRUE(PathExists(dest_url));
567 EXPECT_FALSE(DirectoryExists(dest_url));
568
[email protected]0c5ebe32011-08-19 22:37:16569 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46570 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33571 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49572 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09573 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50574 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46575 EXPECT_TRUE(FileExists(data_path));
576 EXPECT_EQ(src_file_length, GetSize(data_path));
577
578 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09579 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46580 }
581
[email protected]949f25a2012-06-27 01:53:09582 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03583 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
584 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09585 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
586 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03587 }
588
[email protected]949f25a2012-06-27 01:53:09589 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03590 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33591 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03592 base::PlatformFileInfo file_info;
593 context.reset(NewContext(NULL));
594 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09595 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10596 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03597 return file_info.last_modified;
598 }
599
[email protected]949f25a2012-06-27 01:53:09600 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03601 bool copy,
602 bool overwrite) {
603 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42604 const FileSystemURL src_dir_url(
605 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
606 const FileSystemURL dest_dir_url(
607 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03608
[email protected]04c899f2013-02-08 08:28:42609 const FileSystemURL src_file_url(
610 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
611 const FileSystemURL dest_file_url(
612 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03613
614 context.reset(NewContext(NULL));
615 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09616 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03617 context.reset(NewContext(NULL));
618 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09619 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03620
621 bool created = false;
622 context.reset(NewContext(NULL));
623 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09624 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03625 if (overwrite) {
626 context.reset(NewContext(NULL));
627 EXPECT_EQ(base::PLATFORM_FILE_OK,
628 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09629 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03630 }
631
[email protected]949f25a2012-06-27 01:53:09632 ClearTimestamp(src_dir_url);
633 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03634 context.reset(NewContext(NULL));
635 EXPECT_EQ(base::PLATFORM_FILE_OK,
636 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09637 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03638 copy));
[email protected]fad625e2f2011-12-08 05:38:03639 if (copy)
[email protected]949f25a2012-06-27 01:53:09640 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03641 else
[email protected]949f25a2012-06-27 01:53:09642 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
643 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03644 }
645
[email protected]ecdfd6c52012-04-11 13:35:44646 int64 ComputeCurrentUsage() {
[email protected]1c98fdd2013-05-24 09:45:27647 return sandbox_file_system_.ComputeCurrentOriginUsage() -
648 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]02a60542012-07-24 20:05:33649 }
[email protected]45ea0fbbb2012-02-27 22:28:49650
[email protected]07b64872013-02-13 11:46:30651 FileSystemContext* file_system_context() {
[email protected]1c98fdd2013-05-24 09:45:27652 return sandbox_file_system_.file_system_context();
[email protected]07b64872013-02-13 11:46:30653 }
654
[email protected]d4905e2e2011-05-13 21:56:32655 private:
[email protected]ea1a3f62012-11-16 20:34:23656 base::ScopedTempDir data_dir_;
[email protected]4cc586b2013-05-07 12:43:32657 base::MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16658 scoped_refptr<quota::QuotaManager> quota_manager_;
659 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26660 GURL origin_;
661 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03662 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]1c98fdd2013-05-24 09:45:27663 SandboxFileSystemTestHelper sandbox_file_system_;
[email protected]0c5ebe32011-08-19 22:37:16664 quota::QuotaStatusCode quota_status_;
665 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10666 MockFileChangeObserver change_observer_;
667 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32668
[email protected]7878ece2011-09-05 11:41:49669 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32670};
671
[email protected]7878ece2011-09-05 11:41:49672TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32673 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
674 bool created;
[email protected]949f25a2012-06-27 01:53:09675 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16676 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32677 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
678
679 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49680 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09681 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32682 &created));
683
[email protected]0c5ebe32011-08-19 22:37:16684 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32685 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09686 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32687
[email protected]949f25a2012-06-27 01:53:09688 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32689
[email protected]c4e6f9c2012-09-09 17:42:10690 EXPECT_TRUE(change_observer()->HasNoChange());
691
[email protected]0c5ebe32011-08-19 22:37:16692 // Verify that file creation requires sufficient quota for the path.
693 context.reset(NewContext(NULL));
694 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09695 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16696 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49697 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09698 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16699
700 context.reset(NewContext(NULL));
701 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09702 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32703 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49704 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09705 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32706 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10707 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32708 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
709
[email protected]949f25a2012-06-27 01:53:09710 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32711
[email protected]0c5ebe32011-08-19 22:37:16712 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33713 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49714 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09715 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32716 EXPECT_TRUE(file_util::PathExists(local_path));
717
[email protected]0c5ebe32011-08-19 22:37:16718 // Verify that deleting a file isn't stopped by zero quota, and that it frees
719 // up quote from its path.
720 context.reset(NewContext(NULL));
721 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32722 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09723 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10724 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32725 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09726 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16727 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32728
[email protected]0c5ebe32011-08-19 22:37:16729 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32730 bool exclusive = true;
731 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09732 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50733 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42734 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49735 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09736 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10737 // The oepration created 3 directories recursively.
738 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32739
[email protected]0c5ebe32011-08-19 22:37:16740 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32741 file_handle = base::kInvalidPlatformFileValue;
742 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49743 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09744 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32745 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10746 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32747 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
748
[email protected]949f25a2012-06-27 01:53:09749 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32750
[email protected]0c5ebe32011-08-19 22:37:16751 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49752 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09753 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32754 EXPECT_TRUE(file_util::PathExists(local_path));
755
[email protected]0c5ebe32011-08-19 22:37:16756 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32757 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09758 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10759 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32760 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10761
762 // Make sure we have no unexpected changes.
763 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32764}
765
[email protected]7878ece2011-09-05 11:41:49766TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32767 bool created = false;
[email protected]949f25a2012-06-27 01:53:09768 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16769 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32770
771 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09772 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32773
[email protected]0c5ebe32011-08-19 22:37:16774 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32775 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09776 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32777 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10778 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32779
[email protected]0c5ebe32011-08-19 22:37:16780 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33781 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49782 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09783 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32784 EXPECT_EQ(0, GetSize(local_path));
785
[email protected]0c5ebe32011-08-19 22:37:16786 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49787 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09788 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10789 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32790 EXPECT_EQ(10, GetSize(local_path));
791
[email protected]0c5ebe32011-08-19 22:37:16792 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49793 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09794 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32795 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10796 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32797
[email protected]13f92f6e2012-08-13 07:39:14798 EXPECT_FALSE(DirectoryExists(url));
799 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10800
801 // Make sure we have no unexpected changes.
802 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32803}
804
[email protected]ecdfd6c52012-04-11 13:35:44805TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
806 bool created = false;
[email protected]949f25a2012-06-27 01:53:09807 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44808
809 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13810 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09811 AllowUsageIncrease(PathCost(url))->context(),
812 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44813 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13814 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44815
[email protected]ecdfd6c52012-04-11 13:35:44816 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13817 ofu()->Truncate(
818 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09819 url, 1020));
[email protected]294dd0312012-05-11 07:35:13820 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44821
[email protected]ecdfd6c52012-04-11 13:35:44822 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13823 ofu()->Truncate(
824 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09825 url, 0));
[email protected]294dd0312012-05-11 07:35:13826 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44827
[email protected]ecdfd6c52012-04-11 13:35:44828 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13829 ofu()->Truncate(
830 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09831 url, 1021));
[email protected]294dd0312012-05-11 07:35:13832 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44833
[email protected]ecdfd6c52012-04-11 13:35:44834 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13835 ofu()->Truncate(
836 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09837 url, 1020));
[email protected]294dd0312012-05-11 07:35:13838 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44839
[email protected]ecdfd6c52012-04-11 13:35:44840 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13841 ofu()->Truncate(
842 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09843 url, 1020));
[email protected]294dd0312012-05-11 07:35:13844 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44845
[email protected]294dd0312012-05-11 07:35:13846 // quota exceeded
847 {
848 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
849 helper->context()->set_allowed_bytes_growth(
850 helper->context()->allowed_bytes_growth() - 1);
851 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09852 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13853 ASSERT_EQ(1019, ComputeTotalFileSize());
854 }
[email protected]ecdfd6c52012-04-11 13:35:44855
856 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33857 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44858 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13859 ofu()->GetLocalFilePath(
860 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09861 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44862 ASSERT_FALSE(local_path.empty());
863 ASSERT_TRUE(file_util::Delete(local_path, false));
864
[email protected]ecdfd6c52012-04-11 13:35:44865 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13866 ofu()->Truncate(
867 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09868 url, 1234));
[email protected]294dd0312012-05-11 07:35:13869 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44870}
871
[email protected]7878ece2011-09-05 11:41:49872TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09873 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32874 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16875 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32876 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49877 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09878 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10879 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32880
[email protected]0c5ebe32011-08-19 22:37:16881 // Verify that file creation requires sufficient quota for the path.
882 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09883 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32884 created = false;
[email protected]0c5ebe32011-08-19 22:37:16885 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09886 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16887 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09888 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16889 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10890 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16891
892 context.reset(NewContext(NULL));
893 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09894 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32895 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09896 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32897 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10898 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32899
[email protected]949f25a2012-06-27 01:53:09900 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32901
[email protected]0c5ebe32011-08-19 22:37:16902 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32903 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09904 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32905 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10906 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32907
908 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09909 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16910 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32911 bool exclusive = true;
912 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49913 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09914 context.get(),
[email protected]04c899f2013-02-08 08:28:42915 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09916 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10917 // 2 directories: path/ and path/to.
918 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32919
[email protected]0c5ebe32011-08-19 22:37:16920 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32921 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09922 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32923 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14924 EXPECT_FALSE(DirectoryExists(url));
925 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10926 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32927}
928
[email protected]7878ece2011-09-05 11:41:49929TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16930 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32931
932 bool exclusive = false;
933 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09934 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49935 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09936 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32937
[email protected]0c5ebe32011-08-19 22:37:16938 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32939 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08940 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32941
[email protected]007b3f82013-04-09 08:46:45942 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:14943 EXPECT_FALSE(DirectoryExists(url));
944 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16945 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49946 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32947
[email protected]0c5ebe32011-08-19 22:37:16948 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32949 exclusive = false;
950 recursive = true;
[email protected]7878ece2011-09-05 11:41:49951 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09952 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10953 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32954
[email protected]13f92f6e2012-08-13 07:39:14955 EXPECT_TRUE(DirectoryExists(url));
956 EXPECT_TRUE(PathExists(url));
957
[email protected]0c5ebe32011-08-19 22:37:16958 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49959 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42960 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14961
[email protected]0c5ebe32011-08-19 22:37:16962 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09963 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42964 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32965
966 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16967 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06968 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08969 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42970 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10971 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32972
973 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33974 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49975 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09976 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32977 EXPECT_TRUE(local_path.empty());
978 EXPECT_TRUE(file_info.is_directory);
979 EXPECT_FALSE(file_info.is_symbolic_link);
980
981 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16982 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49983 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09984 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10985 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32986
987 exclusive = true;
988 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16989 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49990 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09991 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10992 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32993
[email protected]0c5ebe32011-08-19 22:37:16994 // Verify that deleting a directory isn't stopped by zero quota, and that it
995 // frees up quota from its path.
996 context.reset(NewContext(NULL));
997 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:08998 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10999 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091000 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161001 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321002
[email protected]949f25a2012-06-27 01:53:091003 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321004
[email protected]13f92f6e2012-08-13 07:39:141005 EXPECT_FALSE(DirectoryExists(url));
1006 EXPECT_FALSE(PathExists(url));
1007
[email protected]0c5ebe32011-08-19 22:37:161008 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091009 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491010 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091011 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321012
[email protected]0c5ebe32011-08-19 22:37:161013 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321014 exclusive = true;
1015 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161016 context.reset(NewContext(NULL));
1017 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091018 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491019 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091020 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101021 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161022
1023 context.reset(NewContext(NULL));
1024 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091025 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491026 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091027 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101028 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321029
[email protected]13f92f6e2012-08-13 07:39:141030 EXPECT_TRUE(DirectoryExists(url));
1031 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321032
1033 exclusive = true;
1034 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141035 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491036 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091037 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101038 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321039
1040 exclusive = true;
1041 recursive = false;
[email protected]949f25a2012-06-27 01:53:091042 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141043 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491044 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091045 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101046 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321047
[email protected]949f25a2012-06-27 01:53:091048 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321049
[email protected]13f92f6e2012-08-13 07:39:141050 EXPECT_FALSE(DirectoryExists(url));
1051 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321052
1053 exclusive = true;
1054 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141055 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491056 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091057 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101058 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321059
[email protected]13f92f6e2012-08-13 07:39:141060 EXPECT_TRUE(DirectoryExists(url));
1061 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321062
1063 exclusive = true;
1064 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141065 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491066 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091067 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101068 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321069}
1070
[email protected]7878ece2011-09-05 11:41:491071TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161072 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321073 bool exclusive = true;
1074 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091075 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491076 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091077 context.get(), url, exclusive, recursive));
1078 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321079}
1080
[email protected]7878ece2011-09-05 11:41:491081TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451082 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321083}
1084
[email protected]7878ece2011-09-05 11:41:491085TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091086 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321087}
1088
[email protected]7878ece2011-09-05 11:41:491089TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091090 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161091 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321092
1093 bool created = false;
1094 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091095 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321096 ASSERT_TRUE(created);
1097
[email protected]c83118f2013-05-20 04:35:421098 std::vector<DirectoryEntry> entries;
[email protected]1a647202013-01-21 15:32:251099 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301100 AsyncFileTestHelper::ReadDirectory(
1101 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321102
[email protected]949f25a2012-06-27 01:53:091103 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321104}
1105
[email protected]7878ece2011-09-05 11:41:491106TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091107 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161108 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411109
1110 base::Time last_access_time = base::Time::Now();
1111 base::Time last_modified_time = base::Time::Now();
1112
1113 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321114 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491115 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091116 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321117
[email protected]2517cfa2011-08-25 05:12:411118 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161119 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411120 bool created = false;
1121 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091122 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411123 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091124 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411125
1126 // Now test a directory:
1127 context.reset(NewContext(NULL));
1128 bool exclusive = true;
1129 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091130 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491131 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091132 url, exclusive, recursive));
1133 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161134}
1135
[email protected]7878ece2011-09-05 11:41:491136TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091137 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161138 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1139
[email protected]949f25a2012-06-27 01:53:091140 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161141 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411142 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161143 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091144 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411145 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161146 context->set_allowed_bytes_growth(1024);
1147 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091148 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411149 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091150 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161151 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1152
1153 context->set_allowed_bytes_growth(1024);
1154 bool exclusive = true;
1155 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091156 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331157 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091158 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161159 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421160 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1161 for (iterator iter = components.begin();
1162 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491163 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331164 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161165 }
1166 context.reset(NewContext(NULL));
1167 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491168 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091169 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161170 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321171}
1172
[email protected]7878ece2011-09-05 11:41:491173TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091174 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1175 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161176 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321177
1178 bool is_copy_not_move = false;
1179 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091180 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321181 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101182 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161183 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321184 is_copy_not_move = true;
1185 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091186 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321187 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101188 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091189 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321190 bool exclusive = true;
1191 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161192 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491193 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091194 context.get(),
[email protected]04c899f2013-02-08 08:28:421195 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091196 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101197 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321198 is_copy_not_move = false;
1199 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091200 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321201 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101202 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161203 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321204 is_copy_not_move = true;
1205 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091206 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321207 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101208 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321209}
1210
[email protected]7878ece2011-09-05 11:41:491211TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321212 const int64 kSourceLength = 5;
1213 const int64 kDestLength = 50;
1214
1215 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1216 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1217 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1218 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1219 test_case.is_copy_not_move);
1220 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1221 test_case.source_path);
1222 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1223 test_case.dest_path);
1224 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1225 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161226 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321227
1228 bool exclusive = false;
1229 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091230 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1231 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321232
[email protected]0c5ebe32011-08-19 22:37:161233 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491234 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091235 context.get(),
[email protected]04c899f2013-02-08 08:28:421236 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091237 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161238 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491239 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091240 context.get(),
[email protected]04c899f2013-02-08 08:28:421241 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091242 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321243
[email protected]d4905e2e2011-05-13 21:56:321244 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161245 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321246 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091247 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321248 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161249 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321250 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091251 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321252
1253 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161254 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321255 created = false;
1256 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091257 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321258 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161259 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321260 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091261 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321262 }
1263
[email protected]0c5ebe32011-08-19 22:37:161264 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491265 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091266 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101267
[email protected]d4905e2e2011-05-13 21:56:321268 if (test_case.is_copy_not_move) {
1269 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331270 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161271 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491272 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091273 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321274 EXPECT_EQ(kSourceLength, file_info.size);
1275 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091276 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321277 } else {
1278 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331279 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161280 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491281 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091282 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321283 }
1284 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331285 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491286 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091287 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321288 EXPECT_EQ(kSourceLength, file_info.size);
1289
1290 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091291 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321292 }
1293}
1294
[email protected]7878ece2011-09-05 11:41:491295TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091296 FileSystemURL src_url = CreateURLFromUTF8("src path");
1297 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161298 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1299 bool created = false;
[email protected]7878ece2011-09-05 11:41:491300 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091301 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161302
1303 bool is_copy = true;
1304 // Copy, no overwrite.
1305 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091306 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161307 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091308 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161309 context.reset(NewContext(NULL));
1310 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091311 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161312 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091313 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161314
1315 // Copy, with overwrite.
1316 context.reset(NewContext(NULL));
1317 context->set_allowed_bytes_growth(0);
1318 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091319 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161320}
1321
[email protected]7878ece2011-09-05 11:41:491322TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091323 FileSystemURL src_url = CreateURLFromUTF8("src path");
1324 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161325 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1326 bool created = false;
[email protected]7878ece2011-09-05 11:41:491327 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091328 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161329
1330 bool is_copy = false;
1331 // Move, rename, no overwrite.
1332 context.reset(NewContext(NULL));
1333 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091334 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1335 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161336 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091337 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161338 context.reset(NewContext(NULL));
1339 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091340 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1341 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161342 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091343 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161344
1345 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491346 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091347 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161348
1349 // Move, rename, with overwrite.
1350 context.reset(NewContext(NULL));
1351 context->set_allowed_bytes_growth(0);
1352 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091353 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161354}
1355
[email protected]7878ece2011-09-05 11:41:491356TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091357 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161358 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1359 bool created = false;
[email protected]7878ece2011-09-05 11:41:491360 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091361 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161362
1363 bool exclusive = true;
1364 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091365 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161366 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491367 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091368 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161369
[email protected]04c899f2013-02-08 08:28:421370 FileSystemURL dest_url = FileSystemURLAppend(
1371 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161372
1373 bool is_copy = false;
1374 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1375 // Move, no rename, no overwrite.
1376 context.reset(NewContext(NULL));
1377 context->set_allowed_bytes_growth(allowed_bytes_growth);
1378 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091379 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161380 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1381
1382 // Move, no rename, with overwrite.
1383 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491384 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091385 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161386 context.reset(NewContext(NULL));
1387 context->set_allowed_bytes_growth(allowed_bytes_growth);
1388 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091389 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161390 EXPECT_EQ(
1391 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091392 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161393 context->allowed_bytes_growth());
1394}
1395
[email protected]7878ece2011-09-05 11:41:491396TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461397 TestCopyInForeignFileHelper(false /* overwrite */);
1398 TestCopyInForeignFileHelper(true /* overwrite */);
1399}
1400
[email protected]7878ece2011-09-05 11:41:491401TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161402 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091403 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321404 bool exclusive = true;
1405 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491406 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091407 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321408
[email protected]a3ef4832013-02-02 05:12:331409 std::set<base::FilePath::StringType> files;
1410 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091411 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321412
[email protected]949f25a2012-06-27 01:53:091413 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321414
[email protected]13f92f6e2012-08-13 07:39:141415 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321416 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301417 AsyncFileTestHelper::Copy(
[email protected]1c98fdd2013-05-24 09:45:271418 file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321419
[email protected]949f25a2012-06-27 01:53:091420 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141421 EXPECT_TRUE(DirectoryExists(src_url));
1422 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321423 recursive = true;
1424 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301425 AsyncFileTestHelper::Remove(
1426 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141427 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321428}
[email protected]6b931152011-05-20 21:02:351429
[email protected]7878ece2011-09-05 11:41:491430TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1431 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1432 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161433 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261434 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161435 EXPECT_EQ(origin(), enumerator->Next());
1436 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1437 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1438 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261439 EXPECT_EQ(GURL(), enumerator->Next());
1440 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1441 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1442
1443 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161444 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261445
1446 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1447 SCOPED_TRACE(testing::Message() <<
1448 "Validating kOriginEnumerationTestRecords " << i);
1449 const OriginEnumerationTestRecord& record =
1450 kOriginEnumerationTestRecords[i];
1451 GURL origin_url(record.origin_url);
1452 origins_expected.insert(origin_url);
1453 if (record.has_temporary) {
[email protected]1c98fdd2013-05-24 09:45:271454 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1455 NewFileSystem(origin_url, kFileSystemTypeTemporary));
1456 scoped_ptr<FileSystemOperationContext> context(
1457 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261458 bool created = false;
1459 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501460 ofu()->EnsureFileExists(
1461 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271462 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501463 &created));
[email protected]fcc2d5f2011-05-23 22:06:261464 EXPECT_TRUE(created);
1465 }
1466 if (record.has_persistent) {
[email protected]1c98fdd2013-05-24 09:45:271467 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1468 NewFileSystem(origin_url, kFileSystemTypePersistent));
1469 scoped_ptr<FileSystemOperationContext> context(
1470 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261471 bool created = false;
1472 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501473 ofu()->EnsureFileExists(
1474 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271475 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501476 &created));
[email protected]fcc2d5f2011-05-23 22:06:261477 EXPECT_TRUE(created);
1478 }
1479 }
[email protected]7878ece2011-09-05 11:41:491480 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261481 EXPECT_TRUE(enumerator.get());
1482 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161483 GURL origin_url;
1484 while (!(origin_url = enumerator->Next()).is_empty()) {
1485 origins_found.insert(origin_url);
1486 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261487 bool found = false;
1488 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1489 ++i) {
1490 const OriginEnumerationTestRecord& record =
1491 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161492 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261493 continue;
1494 found = true;
1495 EXPECT_EQ(record.has_temporary,
1496 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1497 EXPECT_EQ(record.has_persistent,
1498 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1499 }
[email protected]0c5ebe32011-08-19 22:37:161500 // Deal with the default filesystem created by the test helper.
1501 if (!found && origin_url == origin()) {
1502 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1503 EXPECT_EQ(true,
1504 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181505 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161506 found = true;
1507 }
[email protected]fcc2d5f2011-05-23 22:06:261508 EXPECT_TRUE(found);
1509 }
1510
1511 std::set<GURL> diff;
1512 std::set_symmetric_difference(origins_expected.begin(),
1513 origins_expected.end(), origins_found.begin(), origins_found.end(),
1514 inserter(diff, diff.begin()));
1515 EXPECT_TRUE(diff.empty());
1516}
[email protected]0c5ebe32011-08-19 22:37:161517
[email protected]7878ece2011-09-05 11:41:491518TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161519 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1520
1521 int64 expected_quota = 0;
1522
[email protected]f83b5b72012-01-27 10:26:561523 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011524 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561525 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331526 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501527 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161528 if (test_case.is_directory) {
1529 bool exclusive = true;
1530 bool recursive = false;
1531 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091532 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501533 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161534 } else {
1535 bool created = false;
1536 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091537 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501538 &created));
[email protected]0c5ebe32011-08-19 22:37:161539 ASSERT_TRUE(created);
1540 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501541 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091542 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501543 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161544 expected_quota += test_case.data_file_size;
1545 }
1546 }
[email protected]022d2702012-05-14 16:04:261547
1548 // Usually raw size in usage cache and the usage returned by QuotaUtil
1549 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161550 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261551 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1552
[email protected]0c5ebe32011-08-19 22:37:161553 RevokeUsageCache();
1554 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261555 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1556
1557 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161558 GetUsageFromQuotaManager();
1559 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261560 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161561 EXPECT_EQ(expected_quota, usage());
1562}
[email protected]34583332011-08-31 08:59:471563
[email protected]7878ece2011-09-05 11:41:491564TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091565 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1566 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471567
1568 scoped_ptr<FileSystemOperationContext> context;
1569 base::PlatformFile file;
1570 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331571 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471572 bool created = false;
1573
1574 // Create a non-empty file.
1575 context.reset(NewContext(NULL));
1576 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491577 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471578 EXPECT_TRUE(created);
1579 context.reset(NewContext(NULL));
1580 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491581 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471582 context.reset(NewContext(NULL));
1583 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491584 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471585 context.get(), kPath1, &file_info, &data_path));
1586 EXPECT_EQ(10, file_info.size);
1587
1588 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491589 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471590
1591 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141592 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471593 context.reset(NewContext(NULL));
1594 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491595 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471596 EXPECT_TRUE(created);
1597 context.reset(NewContext(NULL));
1598 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491599 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471600 context.get(), kPath1, &file_info, &data_path));
1601 EXPECT_EQ(0, file_info.size);
1602
1603 // Make another broken file to |kPath2|.
1604 context.reset(NewContext(NULL));
1605 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491606 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471607 EXPECT_TRUE(created);
1608
1609 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491610 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471611
1612 // Repair broken |kPath1|.
1613 context.reset(NewContext(NULL));
1614 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491615 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471616 base::Time::Now()));
1617 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491618 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471619 EXPECT_TRUE(created);
1620
1621 // Copy from sound |kPath1| to broken |kPath2|.
1622 context.reset(NewContext(NULL));
1623 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491624 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501625 true /* copy */));
[email protected]34583332011-08-31 08:59:471626
[email protected]7878ece2011-09-05 11:41:491627 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471628 context.reset(NewContext(NULL));
1629 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491630 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471631 context.get(), kPath1,
1632 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1633 &file, &created));
1634 EXPECT_TRUE(created);
1635 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1636 EXPECT_EQ(0, file_info.size);
1637 EXPECT_TRUE(base::ClosePlatformFile(file));
1638}
[email protected]9dfdc0e32011-09-02 06:07:441639
[email protected]7878ece2011-09-05 11:41:491640TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091641 const FileSystemURL kPath[] = {
1642 CreateURLFromUTF8("foo"),
1643 CreateURLFromUTF8("bar"),
1644 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441645 };
[email protected]a3ef4832013-02-02 05:12:331646 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441647 scoped_ptr<FileSystemOperationContext> context;
1648
1649 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1650 bool created = false;
1651 context.reset(NewContext(NULL));
1652 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491653 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441654 EXPECT_TRUE(created);
1655 }
1656
[email protected]c83118f2013-05-20 04:35:421657 std::vector<DirectoryEntry> entries;
[email protected]9dfdc0e32011-09-02 06:07:441658 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301659 AsyncFileTestHelper::ReadDirectory(
1660 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441661 EXPECT_EQ(3u, entries.size());
1662
[email protected]a3ef4832013-02-02 05:12:331663 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441664 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491665 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441666 EXPECT_TRUE(file_util::Delete(local_path, false));
1667
[email protected]9dfdc0e32011-09-02 06:07:441668 entries.clear();
1669 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301670 AsyncFileTestHelper::ReadDirectory(
1671 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441672 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1673}
[email protected]fad625e2f2011-12-08 05:38:031674
1675TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1676 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091677 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031678
1679 // Create working directory.
1680 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091681 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031682
1683 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421684 FileSystemURL url(FileSystemURLAppendUTF8(
1685 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031686 bool created = false;
[email protected]949f25a2012-06-27 01:53:091687 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031688 context.reset(NewContext(NULL));
1689 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091690 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031691 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091692 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031693
1694 // non create case.
1695 created = true;
[email protected]949f25a2012-06-27 01:53:091696 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031697 context.reset(NewContext(NULL));
1698 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091699 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031700 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091701 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031702
1703 // fail case.
[email protected]04c899f2013-02-08 08:28:421704 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031705 context.reset(NewContext(NULL));
1706 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091707 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031708
[email protected]949f25a2012-06-27 01:53:091709 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031710 context.reset(NewContext(NULL));
1711 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091712 ofu()->EnsureFileExists(context.get(), url, &created));
1713 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031714
1715 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421716 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391717 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031718 created = false;
[email protected]949f25a2012-06-27 01:53:091719 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031720 context.reset(NewContext(NULL));
1721 EXPECT_EQ(base::PLATFORM_FILE_OK,
1722 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091723 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031724 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1725 &file_handle, &created));
1726 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1727 EXPECT_TRUE(created);
1728 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091729 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031730
1731 // open case.
1732 file_handle = base::kInvalidPlatformFileValue;
1733 created = true;
[email protected]949f25a2012-06-27 01:53:091734 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031735 context.reset(NewContext(NULL));
1736 EXPECT_EQ(base::PLATFORM_FILE_OK,
1737 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091738 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031739 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1740 &file_handle, &created));
1741 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1742 EXPECT_FALSE(created);
1743 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091744 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031745
1746 // fail case
1747 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091748 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031749 context.reset(NewContext(NULL));
1750 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1751 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091752 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031753 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1754 &file_handle, &created));
1755 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091756 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031757
1758 // CreateDirectory, create case.
1759 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421760 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1761 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091762 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031763 context.reset(NewContext(NULL));
1764 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091765 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031766 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091767 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031768
1769 // create subdir case.
1770 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421771 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091772 ClearTimestamp(dir_url);
1773 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031774 context.reset(NewContext(NULL));
1775 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091776 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031777 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091778 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1779 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031780
1781 // fail case.
[email protected]04c899f2013-02-08 08:28:421782 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091783 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031784 context.reset(NewContext(NULL));
1785 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091786 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031787 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091788 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031789
1790 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421791 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1792 FileSystemURL src_path = FileSystemURLAppendUTF8(
1793 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031794 context.reset(NewContext(NULL));
1795 EXPECT_EQ(base::PLATFORM_FILE_OK,
1796 ofu()->EnsureFileExists(context.get(), src_path, &created));
1797 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331798 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031799 context.reset(NewContext(NULL));
1800 EXPECT_EQ(base::PLATFORM_FILE_OK,
1801 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1802
[email protected]949f25a2012-06-27 01:53:091803 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031804 context.reset(NewContext(NULL));
1805 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501806 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131807 src_local_path,
[email protected]949f25a2012-06-27 01:53:091808 url));
1809 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031810}
1811
1812TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1813 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091814 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031815
1816 // Create working directory.
1817 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091818 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031819
1820 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421821 FileSystemURL url = FileSystemURLAppendUTF8(
1822 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031823 bool created = false;
1824 context.reset(NewContext(NULL));
1825 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091826 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031827 EXPECT_TRUE(created);
1828
[email protected]949f25a2012-06-27 01:53:091829 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031830 context.reset(NewContext(NULL));
1831 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091832 ofu()->DeleteFile(context.get(), url));
1833 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031834
1835 // fail case.
[email protected]949f25a2012-06-27 01:53:091836 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031837 context.reset(NewContext(NULL));
1838 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091839 ofu()->DeleteFile(context.get(), url));
1840 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031841
[email protected]bab213be2013-01-23 15:13:081842 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421843 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1844 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031845 context.reset(NewContext(NULL));
1846 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091847 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031848 created = false;
1849 context.reset(NewContext(NULL));
1850 EXPECT_EQ(base::PLATFORM_FILE_OK,
1851 ofu()->EnsureFileExists(context.get(), file_path, &created));
1852 EXPECT_TRUE(created);
1853
[email protected]949f25a2012-06-27 01:53:091854 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031855 context.reset(NewContext(NULL));
1856 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081857 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091858 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031859
1860 // delete case.
1861 context.reset(NewContext(NULL));
1862 EXPECT_EQ(base::PLATFORM_FILE_OK,
1863 ofu()->DeleteFile(context.get(), file_path));
1864
[email protected]949f25a2012-06-27 01:53:091865 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031866 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081867 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091868 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031869}
1870
1871TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1872 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091873 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031874 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091875 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031876 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091877 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031878 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091879 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031880}
[email protected]a3938912012-03-27 14:00:551881
1882TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091883 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421884 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1885 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551886
1887 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1888 EXPECT_EQ(base::PLATFORM_FILE_OK,
1889 ofu()->CreateDirectory(context.get(), dir, false, false));
1890
1891 bool created = false;
1892 context.reset(NewContext(NULL));
1893 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091894 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551895 EXPECT_TRUE(created);
1896
1897 context.reset(NewContext(NULL));
1898 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091899 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551900
[email protected]a3ef4832013-02-02 05:12:331901 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551902 context.reset(NewContext(NULL));
1903 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091904 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551905 EXPECT_FALSE(file_path.empty());
1906
1907 context.reset(NewContext(NULL));
1908 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091909 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551910 base::Time::Now() + base::TimeDelta::FromHours(1),
1911 base::Time()));
1912
1913 context.reset(NewContext(NULL));
1914 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1915 ofu()->CreateFileEnumerator(context.get(), dir, false));
1916
1917 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331918 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551919 while (!(file_path_each = file_enum->Next()).empty()) {
1920 context.reset(NewContext(NULL));
1921 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331922 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551923 EXPECT_EQ(base::PLATFORM_FILE_OK,
1924 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421925 FileSystemURL::CreateForTest(
1926 dir.origin(),
1927 dir.mount_type(),
1928 file_path_each),
[email protected]a3938912012-03-27 14:00:551929 &file_info, &file_path));
1930 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1931 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1932 EXPECT_EQ(file_info.size, file_enum->Size());
1933 ++count;
1934 }
1935 EXPECT_EQ(2, count);
1936}
[email protected]294dd0312012-05-11 07:35:131937
[email protected]58ac5272013-02-15 10:05:031938// crbug.com/176470
1939#if defined(OS_WIN) || defined(OS_ANDROID)
1940#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1941#else
1942#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1943#endif
1944TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091945 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1946 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1947 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1948 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131949 bool created;
1950
1951 int64 expected_total_file_size = 0;
1952 ASSERT_EQ(base::PLATFORM_FILE_OK,
1953 ofu()->EnsureFileExists(
1954 AllowUsageIncrease(PathCost(from_file))->context(),
1955 from_file, &created));
1956 ASSERT_TRUE(created);
1957 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1958
1959 ASSERT_EQ(base::PLATFORM_FILE_OK,
1960 ofu()->EnsureFileExists(
1961 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1962 obstacle_file, &created));
1963 ASSERT_TRUE(created);
1964 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1965
1966 int64 from_file_size = 1020;
1967 expected_total_file_size += from_file_size;
1968 ASSERT_EQ(base::PLATFORM_FILE_OK,
1969 ofu()->Truncate(
1970 AllowUsageIncrease(from_file_size)->context(),
1971 from_file, from_file_size));
1972 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1973
1974 int64 obstacle_file_size = 1;
1975 expected_total_file_size += obstacle_file_size;
1976 ASSERT_EQ(base::PLATFORM_FILE_OK,
1977 ofu()->Truncate(
1978 AllowUsageIncrease(obstacle_file_size)->context(),
1979 obstacle_file, obstacle_file_size));
1980 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1981
1982 int64 to_file1_size = from_file_size;
1983 expected_total_file_size += to_file1_size;
1984 ASSERT_EQ(base::PLATFORM_FILE_OK,
1985 ofu()->CopyOrMoveFile(
1986 AllowUsageIncrease(
1987 PathCost(to_file1) + to_file1_size)->context(),
1988 from_file, to_file1, true /* copy */));
1989 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1990
1991 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1992 ofu()->CopyOrMoveFile(
1993 DisallowUsageIncrease(
1994 PathCost(to_file2) + from_file_size)->context(),
1995 from_file, to_file2, true /* copy */));
1996 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1997
1998 int64 old_obstacle_file_size = obstacle_file_size;
1999 obstacle_file_size = from_file_size;
2000 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2001 ASSERT_EQ(base::PLATFORM_FILE_OK,
2002 ofu()->CopyOrMoveFile(
2003 AllowUsageIncrease(
2004 obstacle_file_size - old_obstacle_file_size)->context(),
2005 from_file, obstacle_file, true /* copy */));
2006 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2007
2008 int64 old_from_file_size = from_file_size;
2009 from_file_size = old_from_file_size - 1;
2010 expected_total_file_size += from_file_size - old_from_file_size;
2011 ASSERT_EQ(base::PLATFORM_FILE_OK,
2012 ofu()->Truncate(
2013 AllowUsageIncrease(
2014 from_file_size - old_from_file_size)->context(),
2015 from_file, from_file_size));
2016 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2017
2018 // quota exceeded
2019 {
2020 old_obstacle_file_size = obstacle_file_size;
2021 obstacle_file_size = from_file_size;
2022 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2023 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2024 obstacle_file_size - old_obstacle_file_size);
2025 helper->context()->set_allowed_bytes_growth(
2026 helper->context()->allowed_bytes_growth() - 1);
2027 ASSERT_EQ(base::PLATFORM_FILE_OK,
2028 ofu()->CopyOrMoveFile(
2029 helper->context(),
2030 from_file, obstacle_file, true /* copy */));
2031 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2032 }
2033}
2034
2035TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092036 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2037 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2038 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132039 bool created;
2040
2041 int64 expected_total_file_size = 0;
2042 ASSERT_EQ(base::PLATFORM_FILE_OK,
2043 ofu()->EnsureFileExists(
2044 AllowUsageIncrease(PathCost(from_file))->context(),
2045 from_file, &created));
2046 ASSERT_TRUE(created);
2047 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2048
2049 int64 from_file_size = 1020;
2050 expected_total_file_size += from_file_size;
2051 ASSERT_EQ(base::PLATFORM_FILE_OK,
2052 ofu()->Truncate(
2053 AllowUsageIncrease(from_file_size)->context(),
2054 from_file, from_file_size));
2055 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2056
2057 int64 to_file_size ALLOW_UNUSED = from_file_size;
2058 from_file_size = 0;
2059 ASSERT_EQ(base::PLATFORM_FILE_OK,
2060 ofu()->CopyOrMoveFile(
2061 AllowUsageIncrease(-PathCost(from_file) +
2062 PathCost(to_file))->context(),
2063 from_file, to_file, false /* move */));
2064 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2065
2066 ASSERT_EQ(base::PLATFORM_FILE_OK,
2067 ofu()->EnsureFileExists(
2068 AllowUsageIncrease(PathCost(from_file))->context(),
2069 from_file, &created));
2070 ASSERT_TRUE(created);
2071 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2072
2073 ASSERT_EQ(base::PLATFORM_FILE_OK,
2074 ofu()->EnsureFileExists(
2075 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2076 obstacle_file, &created));
2077 ASSERT_TRUE(created);
2078 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2079
2080 from_file_size = 1020;
2081 expected_total_file_size += from_file_size;
2082 ASSERT_EQ(base::PLATFORM_FILE_OK,
2083 ofu()->Truncate(
2084 AllowUsageIncrease(from_file_size)->context(),
2085 from_file, from_file_size));
2086 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2087
2088 int64 obstacle_file_size = 1;
2089 expected_total_file_size += obstacle_file_size;
2090 ASSERT_EQ(base::PLATFORM_FILE_OK,
2091 ofu()->Truncate(
2092 AllowUsageIncrease(1)->context(),
2093 obstacle_file, obstacle_file_size));
2094 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2095
2096 int64 old_obstacle_file_size = obstacle_file_size;
2097 obstacle_file_size = from_file_size;
2098 from_file_size = 0;
2099 expected_total_file_size -= old_obstacle_file_size;
2100 ASSERT_EQ(base::PLATFORM_FILE_OK,
2101 ofu()->CopyOrMoveFile(
2102 AllowUsageIncrease(
2103 -old_obstacle_file_size - PathCost(from_file))->context(),
2104 from_file, obstacle_file,
2105 false /* move */));
2106 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2107
2108 ASSERT_EQ(base::PLATFORM_FILE_OK,
2109 ofu()->EnsureFileExists(
2110 AllowUsageIncrease(PathCost(from_file))->context(),
2111 from_file, &created));
2112 ASSERT_TRUE(created);
2113 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2114
2115 from_file_size = 10;
2116 expected_total_file_size += from_file_size;
2117 ASSERT_EQ(base::PLATFORM_FILE_OK,
2118 ofu()->Truncate(
2119 AllowUsageIncrease(from_file_size)->context(),
2120 from_file, from_file_size));
2121 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2122
2123 // quota exceeded even after operation
2124 old_obstacle_file_size = obstacle_file_size;
2125 obstacle_file_size = from_file_size;
2126 from_file_size = 0;
2127 expected_total_file_size -= old_obstacle_file_size;
2128 scoped_ptr<FileSystemOperationContext> context =
2129 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2130 ASSERT_EQ(base::PLATFORM_FILE_OK,
2131 ofu()->CopyOrMoveFile(
2132 context.get(), from_file, obstacle_file, false /* move */));
2133 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2134 context.reset();
2135}
2136
2137TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092138 FileSystemURL dir(CreateURLFromUTF8("dir"));
2139 FileSystemURL file(CreateURLFromUTF8("file"));
2140 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2141 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132142 bool created;
2143
2144 ASSERT_EQ(base::PLATFORM_FILE_OK,
2145 ofu()->EnsureFileExists(
2146 AllowUsageIncrease(PathCost(file))->context(),
2147 file, &created));
2148 ASSERT_TRUE(created);
2149 ASSERT_EQ(0, ComputeTotalFileSize());
2150
2151 ASSERT_EQ(base::PLATFORM_FILE_OK,
2152 ofu()->CreateDirectory(
2153 AllowUsageIncrease(PathCost(dir))->context(),
2154 dir, false, false));
2155 ASSERT_EQ(0, ComputeTotalFileSize());
2156
2157 ASSERT_EQ(base::PLATFORM_FILE_OK,
2158 ofu()->EnsureFileExists(
2159 AllowUsageIncrease(PathCost(dfile1))->context(),
2160 dfile1, &created));
2161 ASSERT_TRUE(created);
2162 ASSERT_EQ(0, ComputeTotalFileSize());
2163
2164 ASSERT_EQ(base::PLATFORM_FILE_OK,
2165 ofu()->EnsureFileExists(
2166 AllowUsageIncrease(PathCost(dfile2))->context(),
2167 dfile2, &created));
2168 ASSERT_TRUE(created);
2169 ASSERT_EQ(0, ComputeTotalFileSize());
2170
2171 ASSERT_EQ(base::PLATFORM_FILE_OK,
2172 ofu()->Truncate(
2173 AllowUsageIncrease(340)->context(),
2174 file, 340));
2175 ASSERT_EQ(340, ComputeTotalFileSize());
2176
2177 ASSERT_EQ(base::PLATFORM_FILE_OK,
2178 ofu()->Truncate(
2179 AllowUsageIncrease(1020)->context(),
2180 dfile1, 1020));
2181 ASSERT_EQ(1360, ComputeTotalFileSize());
2182
2183 ASSERT_EQ(base::PLATFORM_FILE_OK,
2184 ofu()->Truncate(
2185 AllowUsageIncrease(120)->context(),
2186 dfile2, 120));
2187 ASSERT_EQ(1480, ComputeTotalFileSize());
2188
2189 ASSERT_EQ(base::PLATFORM_FILE_OK,
2190 ofu()->DeleteFile(
2191 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2192 file));
2193 ASSERT_EQ(1140, ComputeTotalFileSize());
2194
2195 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302196 AsyncFileTestHelper::Remove(
2197 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132198 ASSERT_EQ(0, ComputeTotalFileSize());
2199}
[email protected]7d78be12012-05-24 07:07:262200
2201TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092202 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262203 base::PlatformFile file_handle;
2204 bool created;
2205
2206 // Creating a file.
2207 ASSERT_EQ(base::PLATFORM_FILE_OK,
2208 ofu()->EnsureFileExists(
2209 AllowUsageIncrease(PathCost(file))->context(),
2210 file, &created));
2211 ASSERT_TRUE(created);
2212 ASSERT_EQ(0, ComputeTotalFileSize());
2213
2214 // Opening it, which shouldn't change the usage.
2215 ASSERT_EQ(base::PLATFORM_FILE_OK,
2216 ofu()->CreateOrOpen(
2217 AllowUsageIncrease(0)->context(), file,
2218 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2219 &file_handle, &created));
2220 ASSERT_EQ(0, ComputeTotalFileSize());
2221 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2222
2223 const int length = 33;
2224 ASSERT_EQ(base::PLATFORM_FILE_OK,
2225 ofu()->Truncate(
2226 AllowUsageIncrease(length)->context(), file, length));
2227 ASSERT_EQ(length, ComputeTotalFileSize());
2228
2229 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2230 ASSERT_EQ(base::PLATFORM_FILE_OK,
2231 ofu()->CreateOrOpen(
2232 AllowUsageIncrease(-length)->context(), file,
2233 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2234 &file_handle, &created));
2235 ASSERT_EQ(0, ComputeTotalFileSize());
2236 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2237
2238 // Extending the file again.
2239 ASSERT_EQ(base::PLATFORM_FILE_OK,
2240 ofu()->Truncate(
2241 AllowUsageIncrease(length)->context(), file, length));
2242 ASSERT_EQ(length, ComputeTotalFileSize());
2243
2244 // Opening it with TRUNCATED flag, which should truncate the file size.
2245 ASSERT_EQ(base::PLATFORM_FILE_OK,
2246 ofu()->CreateOrOpen(
2247 AllowUsageIncrease(-length)->context(), file,
2248 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2249 &file_handle, &created));
2250 ASSERT_EQ(0, ComputeTotalFileSize());
2251 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2252}
[email protected]c4e6f9c2012-09-09 17:42:102253
2254} // namespace fileapi