blob: acba0b52965f6716a7b1b32bf3750cb01fba5d99 [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"
[email protected]17258682013-06-05 13:38:4028#include "webkit/browser/fileapi/sandbox_origin_database.h"
[email protected]c6f9203a2013-05-28 02:08:0729#include "webkit/browser/fileapi/test_file_set.h"
[email protected]7660ec92013-05-30 05:12:3930#include "webkit/browser/quota/mock_special_storage_policy.h"
31#include "webkit/browser/quota/quota_manager.h"
32#include "webkit/common/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3233
[email protected]c4e6f9c2012-09-09 17:42:1034namespace fileapi {
[email protected]d4905e2e2011-05-13 21:56:3235
36namespace {
37
[email protected]a3ef4832013-02-02 05:12:3338bool FileExists(const base::FilePath& path) {
[email protected]d4905e2e2011-05-13 21:56:3239 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
40}
41
[email protected]a3ef4832013-02-02 05:12:3342int64 GetSize(const base::FilePath& path) {
[email protected]81b7f662011-05-26 00:54:4643 int64 size;
44 EXPECT_TRUE(file_util::GetFileSize(path, &size));
45 return size;
46}
47
[email protected]d4905e2e2011-05-13 21:56:3248// After a move, the dest exists and the source doesn't.
49// After a copy, both source and dest exist.
50struct CopyMoveTestCaseRecord {
51 bool is_copy_not_move;
52 const char source_path[64];
53 const char dest_path[64];
54 bool cause_overwrite;
55};
56
57const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
58 // This is the combinatoric set of:
59 // rename vs. same-name
60 // different directory vs. same directory
61 // overwrite vs. no-overwrite
62 // copy vs. move
63 // We can never be called with source and destination paths identical, so
64 // those cases are omitted.
65 {true, "dir0/file0", "dir0/file1", false},
66 {false, "dir0/file0", "dir0/file1", false},
67 {true, "dir0/file0", "dir0/file1", true},
68 {false, "dir0/file0", "dir0/file1", true},
69
70 {true, "dir0/file0", "dir1/file0", false},
71 {false, "dir0/file0", "dir1/file0", false},
72 {true, "dir0/file0", "dir1/file0", true},
73 {false, "dir0/file0", "dir1/file0", true},
74 {true, "dir0/file0", "dir1/file1", false},
75 {false, "dir0/file0", "dir1/file1", false},
76 {true, "dir0/file0", "dir1/file1", true},
77 {false, "dir0/file0", "dir1/file1", true},
78};
79
[email protected]fcc2d5f2011-05-23 22:06:2680struct OriginEnumerationTestRecord {
81 std::string origin_url;
82 bool has_temporary;
83 bool has_persistent;
84};
85
86const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
87 {"http://example.com", false, true},
88 {"http://example1.com", true, false},
89 {"https://example1.com", true, true},
90 {"file://", false, true},
91 {"http://example.com:8000", false, true},
92};
93
[email protected]04c899f2013-02-08 08:28:4294FileSystemURL FileSystemURLAppend(
[email protected]023ad6ab2013-02-17 05:07:2395 const FileSystemURL& url, const base::FilePath::StringType& child) {
[email protected]04c899f2013-02-08 08:28:4296 return FileSystemURL::CreateForTest(
97 url.origin(), url.mount_type(), url.virtual_path().Append(child));
98}
99
100FileSystemURL FileSystemURLAppendUTF8(
101 const FileSystemURL& url, const std::string& child) {
102 return FileSystemURL::CreateForTest(
103 url.origin(),
104 url.mount_type(),
[email protected]023ad6ab2013-02-17 05:07:23105 url.virtual_path().Append(base::FilePath::FromUTF8Unsafe(child)));
[email protected]04c899f2013-02-08 08:28:42106}
107
108FileSystemURL FileSystemURLDirName(const FileSystemURL& url) {
109 return FileSystemURL::CreateForTest(
[email protected]8a020f62013-02-18 08:05:44110 url.origin(), url.mount_type(), VirtualPath::DirName(url.virtual_path()));
[email protected]04c899f2013-02-08 08:28:42111}
112
[email protected]d4905e2e2011-05-13 21:56:32113} // namespace (anonymous)
114
115// TODO(ericu): The vast majority of this and the other FSFU subclass tests
116// could theoretically be shared. It would basically be a FSFU interface
117// compliance test, and only the subclass-specific bits that look into the
118// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49119class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32120 public:
[email protected]7878ece2011-09-05 11:41:49121 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26122 : origin_(GURL("http://www.example.com")),
123 type_(kFileSystemTypeTemporary),
[email protected]e0b9dda2013-04-30 01:05:43124 weak_factory_(this),
[email protected]1c98fdd2013-05-24 09:45:27125 sandbox_file_system_(origin_, type_),
[email protected]0c5ebe32011-08-19 22:37:16126 quota_status_(quota::kQuotaStatusUnknown),
127 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32128 }
129
[email protected]7fd8fa4f2013-02-07 05:43:50130 virtual void SetUp() {
[email protected]d4905e2e2011-05-13 21:56:32131 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
132
[email protected]e7e46732012-01-05 11:45:55133 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
134 new quota::MockSpecialStoragePolicy();
135
[email protected]cadac622013-06-11 16:46:36136 quota_manager_ =
137 new quota::QuotaManager(false /* is_incognito */,
138 data_dir_.path(),
139 base::MessageLoopProxy::current().get(),
140 base::MessageLoopProxy::current().get(),
141 storage_policy.get());
[email protected]0c5ebe32011-08-19 22:37:16142
[email protected]1c98fdd2013-05-24 09:45:27143 // Every time we create a new sandbox_file_system helper,
144 // it creates another context, which creates another path manager,
145 // another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49146 // another OFU. We need to pass in the context to skip all that.
[email protected]420fb562013-04-18 01:46:34147 file_system_context_ = CreateFileSystemContextForTesting(
[email protected]0c5ebe32011-08-19 22:37:16148 quota_manager_->proxy(),
[email protected]420fb562013-04-18 01:46:34149 data_dir_.path());
[email protected]0c5ebe32011-08-19 22:37:16150
[email protected]1c98fdd2013-05-24 09:45:27151 sandbox_file_system_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10152
153 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32154 }
155
[email protected]7fd8fa4f2013-02-07 05:43:50156 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55157 quota_manager_ = NULL;
[email protected]1c98fdd2013-05-24 09:45:27158 sandbox_file_system_.TearDown();
[email protected]e7e46732012-01-05 11:45:55159 }
160
[email protected]294dd0312012-05-11 07:35:13161 scoped_ptr<FileSystemOperationContext> LimitedContext(
162 int64 allowed_bytes_growth) {
163 scoped_ptr<FileSystemOperationContext> context(
[email protected]1c98fdd2013-05-24 09:45:27164 sandbox_file_system_.NewOperationContext());
[email protected]294dd0312012-05-11 07:35:13165 context->set_allowed_bytes_growth(allowed_bytes_growth);
166 return context.Pass();
167 }
168
169 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
170 return LimitedContext(kint64max);
171 }
172
[email protected]02a60542012-07-24 20:05:33173 FileSystemOperationContext* NewContext(
[email protected]1c98fdd2013-05-24 09:45:27174 SandboxFileSystemTestHelper* file_system) {
[email protected]c4e6f9c2012-09-09 17:42:10175 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16176 FileSystemOperationContext* context;
[email protected]1c98fdd2013-05-24 09:45:27177 if (file_system)
178 context = file_system->NewOperationContext();
[email protected]0c5ebe32011-08-19 22:37:16179 else
[email protected]1c98fdd2013-05-24 09:45:27180 context = sandbox_file_system_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46181 // Setting allowed_bytes_growth big enough for all tests.
182 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10183 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32184 return context;
185 }
186
[email protected]c4e6f9c2012-09-09 17:42:10187 const ChangeObserverList& change_observers() const {
188 return change_observers_;
189 }
190
191 MockFileChangeObserver* change_observer() {
192 return &change_observer_;
193 }
194
[email protected]0c5ebe32011-08-19 22:37:16195 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49196 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16197 // Use this for tests which need to run in multiple origins; we need a test
198 // helper per origin.
[email protected]1c98fdd2013-05-24 09:45:27199 SandboxFileSystemTestHelper* NewFileSystem(
[email protected]0c5ebe32011-08-19 22:37:16200 const GURL& origin, fileapi::FileSystemType type) {
[email protected]1c98fdd2013-05-24 09:45:27201 SandboxFileSystemTestHelper* file_system =
202 new SandboxFileSystemTestHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16203
[email protected]1c98fdd2013-05-24 09:45:27204 file_system->SetUp(file_system_context_.get());
205 return file_system;
[email protected]0c5ebe32011-08-19 22:37:16206 }
207
[email protected]7878ece2011-09-05 11:41:49208 ObfuscatedFileUtil* ofu() {
[email protected]1c98fdd2013-05-24 09:45:27209 return static_cast<ObfuscatedFileUtil*>(sandbox_file_system_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32210 }
211
[email protected]a3ef4832013-02-02 05:12:33212 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35213 return data_dir_.path();
214 }
215
[email protected]0c5ebe32011-08-19 22:37:16216 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26217 return origin_;
218 }
219
220 fileapi::FileSystemType type() const {
221 return type_;
222 }
223
[email protected]294dd0312012-05-11 07:35:13224 int64 ComputeTotalFileSize() {
[email protected]1c98fdd2013-05-24 09:45:27225 return sandbox_file_system_.ComputeCurrentOriginUsage() -
226 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]294dd0312012-05-11 07:35:13227 }
228
[email protected]0c5ebe32011-08-19 22:37:16229 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30230 int64 quota = -1;
[email protected]ff875be52013-06-02 23:47:38231 quota_status_ =
232 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
233 origin(),
234 sandbox_file_system_.type(),
235 &usage_,
236 &quota);
[email protected]0c5ebe32011-08-19 22:37:16237 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
238 }
239
240 void RevokeUsageCache() {
[email protected]1c98fdd2013-05-24 09:45:27241 quota_manager_->ResetUsageTracker(sandbox_file_system_.storage_type());
242 usage_cache()->Delete(sandbox_file_system_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26243 }
244
245 int64 SizeByQuotaUtil() {
[email protected]1c98fdd2013-05-24 09:45:27246 return sandbox_file_system_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16247 }
248
249 int64 SizeInUsageFile() {
[email protected]4cc586b2013-05-07 12:43:32250 base::MessageLoop::current()->RunUntilIdle();
[email protected]bf97e8b2013-04-14 15:00:13251 int64 usage = 0;
[email protected]1c98fdd2013-05-24 09:45:27252 return usage_cache()->GetUsage(
253 sandbox_file_system_.GetUsageCachePath(), &usage) ? usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16254 }
255
[email protected]13f92f6e2012-08-13 07:39:14256 bool PathExists(const FileSystemURL& url) {
257 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21258 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33259 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21260 base::PlatformFileError error = ofu()->GetFileInfo(
261 context.get(), url, &file_info, &platform_path);
262 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14263 }
264
265 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30266 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14267 }
268
[email protected]0c5ebe32011-08-19 22:37:16269 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31270 FileSystemUsageCache* usage_cache() {
[email protected]1c98fdd2013-05-24 09:45:27271 return sandbox_file_system_.usage_cache();
[email protected]06226172013-03-14 15:39:31272 }
[email protected]0c5ebe32011-08-19 22:37:16273
[email protected]949f25a2012-06-27 01:53:09274 FileSystemURL CreateURLFromUTF8(const std::string& path) {
[email protected]1c98fdd2013-05-24 09:45:27275 return sandbox_file_system_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50276 }
277
[email protected]949f25a2012-06-27 01:53:09278 int64 PathCost(const FileSystemURL& url) {
279 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13280 }
281
[email protected]a3ef4832013-02-02 05:12:33282 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]1c98fdd2013-05-24 09:45:27283 return sandbox_file_system_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50284 }
285
[email protected]d4905e2e2011-05-13 21:56:32286 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39287 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16288 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33289 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49290 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09291 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32292
293 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33294 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49295 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09296 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32297 EXPECT_EQ(data_path, local_path);
298 EXPECT_TRUE(FileExists(data_path));
299 EXPECT_EQ(0, GetSize(data_path));
300
301 const char data[] = "test data";
302 const int length = arraysize(data) - 1;
303
304 if (base::kInvalidPlatformFileValue == file_handle) {
305 bool created = true;
[email protected]403ada82013-01-08 07:51:39306 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32307 file_handle = base::CreatePlatformFile(
308 data_path,
309 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
310 &created,
311 &error);
[email protected]81b7f662011-05-26 00:54:46312 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32313 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
314 EXPECT_FALSE(created);
315 }
316 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
317 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
318
319 base::PlatformFileInfo file_info1;
320 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16321 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49322 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09323 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32324 EXPECT_EQ(data_path, local_path);
325
326 EXPECT_FALSE(file_info0.is_directory);
327 EXPECT_FALSE(file_info1.is_directory);
328 EXPECT_FALSE(file_info0.is_symbolic_link);
329 EXPECT_FALSE(file_info1.is_symbolic_link);
330 EXPECT_EQ(0, file_info0.size);
331 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32332 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32333
[email protected]0c5ebe32011-08-19 22:37:16334 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49335 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09336 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32337 EXPECT_EQ(length * 2, GetSize(data_path));
338
[email protected]0c5ebe32011-08-19 22:37:16339 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49340 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09341 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16342 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32343 }
344
345 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09346 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33347 const std::set<base::FilePath::StringType>& files,
348 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32349 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33350 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32351 for (iter = files.begin(); iter != files.end(); ++iter) {
352 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16353 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32354 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49355 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42356 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32357 &created));
358 ASSERT_FALSE(created);
359 }
360 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16361 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14362 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42363 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32364 }
365 }
366
[email protected]294dd0312012-05-11 07:35:13367 class UsageVerifyHelper {
368 public:
369 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]1c98fdd2013-05-24 09:45:27370 SandboxFileSystemTestHelper* file_system,
[email protected]294dd0312012-05-11 07:35:13371 int64 expected_usage)
372 : context_(context.Pass()),
[email protected]1c98fdd2013-05-24 09:45:27373 sandbox_file_system_(file_system),
[email protected]294dd0312012-05-11 07:35:13374 expected_usage_(expected_usage) {}
375
376 ~UsageVerifyHelper() {
[email protected]4cc586b2013-05-07 12:43:32377 base::MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13378 Check();
379 }
380
381 FileSystemOperationContext* context() {
382 return context_.get();
383 }
384
385 private:
386 void Check() {
387 ASSERT_EQ(expected_usage_,
[email protected]1c98fdd2013-05-24 09:45:27388 sandbox_file_system_->GetCachedOriginUsage());
[email protected]294dd0312012-05-11 07:35:13389 }
390
391 scoped_ptr<FileSystemOperationContext> context_;
[email protected]1c98fdd2013-05-24 09:45:27392 SandboxFileSystemTestHelper* sandbox_file_system_;
[email protected]294dd0312012-05-11 07:35:13393 int64 expected_usage_;
394 };
395
396 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27397 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13398 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
399 LimitedContext(requested_growth),
[email protected]1c98fdd2013-05-24 09:45:27400 &sandbox_file_system_, usage + requested_growth));
[email protected]294dd0312012-05-11 07:35:13401 }
402
403 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27404 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13405 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
[email protected]1c98fdd2013-05-24 09:45:27406 LimitedContext(requested_growth - 1), &sandbox_file_system_, usage));
[email protected]294dd0312012-05-11 07:35:13407 }
408
[email protected]d4905e2e2011-05-13 21:56:32409 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09410 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33411 std::set<base::FilePath::StringType>* files,
412 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32413 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42414 std::vector<DirectoryEntry> entries;
[email protected]d4905e2e2011-05-13 21:56:32415 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30416 AsyncFileTestHelper::ReadDirectory(
417 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32418 EXPECT_EQ(0UL, entries.size());
419
420 files->clear();
[email protected]89ee4272011-05-16 18:45:17421 files->insert(FILE_PATH_LITERAL("first"));
422 files->insert(FILE_PATH_LITERAL("second"));
423 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32424 directories->clear();
[email protected]89ee4272011-05-16 18:45:17425 directories->insert(FILE_PATH_LITERAL("fourth"));
426 directories->insert(FILE_PATH_LITERAL("fifth"));
427 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33428 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32429 for (iter = files->begin(); iter != files->end(); ++iter) {
430 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16431 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32432 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49433 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50434 context.get(),
[email protected]04c899f2013-02-08 08:28:42435 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50436 &created));
[email protected]d4905e2e2011-05-13 21:56:32437 ASSERT_TRUE(created);
438 }
439 for (iter = directories->begin(); iter != directories->end(); ++iter) {
440 bool exclusive = true;
441 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16442 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32443 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49444 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42445 context.get(),
446 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09447 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32448 }
[email protected]949f25a2012-06-27 01:53:09449 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32450 }
451
[email protected]949f25a2012-06-27 01:53:09452 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33453 std::set<base::FilePath::StringType> files;
454 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09455 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32456
457 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42458 std::vector<DirectoryEntry> entries;
[email protected]0c5ebe32011-08-19 22:37:16459 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32460 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30461 AsyncFileTestHelper::ReadDirectory(
462 file_system_context(), root_url, &entries));
[email protected]c83118f2013-05-20 04:35:42463 std::vector<DirectoryEntry>::iterator entry_iter;
[email protected]d4905e2e2011-05-13 21:56:32464 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10465 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32466 for (entry_iter = entries.begin(); entry_iter != entries.end();
467 ++entry_iter) {
[email protected]c83118f2013-05-20 04:35:42468 const DirectoryEntry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31469 std::set<base::FilePath::StringType>::iterator iter =
470 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32471 if (iter != files.end()) {
472 EXPECT_FALSE(entry.is_directory);
473 files.erase(iter);
474 continue;
475 }
[email protected]89ee4272011-05-16 18:45:17476 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32477 EXPECT_FALSE(directories.end() == iter);
478 EXPECT_TRUE(entry.is_directory);
479 directories.erase(iter);
480 }
481 }
482
[email protected]949f25a2012-06-27 01:53:09483 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41484 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32485 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16486
[email protected]2517cfa2011-08-25 05:12:41487 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32488 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49489 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09490 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10491 // Currently we fire no change notifications for Touch.
492 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33493 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32494 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16495 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49496 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09497 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32498 // We compare as time_t here to lower our resolution, to avoid false
499 // negatives caused by conversion to the local filesystem's native
500 // representation and back.
501 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
502
[email protected]0c5ebe32011-08-19 22:37:16503 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32504 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41505 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32506 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49507 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09508 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10509 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16510 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49511 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09512 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32513 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49514 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41515 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32516 }
517
[email protected]81b7f662011-05-26 00:54:46518 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23519 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46520 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33521 base::FilePath root_file_path = source_dir.path();
522 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09523 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46524 int64 src_file_length = 87;
525
526 base::PlatformFileError error_code;
527 bool created = false;
528 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
529 base::PlatformFile file_handle =
530 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50531 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46532 EXPECT_TRUE(created);
533 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
534 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
535 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
536 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
537
538 scoped_ptr<FileSystemOperationContext> context;
539
540 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16541 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46542 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09543 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46544 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10545
546 // We must have observed one (and only one) create_file_count.
547 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
548 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46549 }
550
[email protected]0c5ebe32011-08-19 22:37:16551 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09552 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16553 if (!overwrite) {
554 // Verify that file creation requires sufficient quota for the path.
555 context.reset(NewContext(NULL));
556 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
557 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13558 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09559 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16560 }
561
562 context.reset(NewContext(NULL));
563 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46564 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13565 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09566 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16567
[email protected]13f92f6e2012-08-13 07:39:14568 EXPECT_TRUE(PathExists(dest_url));
569 EXPECT_FALSE(DirectoryExists(dest_url));
570
[email protected]0c5ebe32011-08-19 22:37:16571 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46572 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33573 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49574 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09575 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50576 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46577 EXPECT_TRUE(FileExists(data_path));
578 EXPECT_EQ(src_file_length, GetSize(data_path));
579
580 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09581 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46582 }
583
[email protected]949f25a2012-06-27 01:53:09584 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03585 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
586 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09587 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
588 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03589 }
590
[email protected]949f25a2012-06-27 01:53:09591 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03592 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33593 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03594 base::PlatformFileInfo file_info;
595 context.reset(NewContext(NULL));
596 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09597 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10598 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03599 return file_info.last_modified;
600 }
601
[email protected]949f25a2012-06-27 01:53:09602 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03603 bool copy,
604 bool overwrite) {
605 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42606 const FileSystemURL src_dir_url(
607 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
608 const FileSystemURL dest_dir_url(
609 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03610
[email protected]04c899f2013-02-08 08:28:42611 const FileSystemURL src_file_url(
612 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
613 const FileSystemURL dest_file_url(
614 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03615
616 context.reset(NewContext(NULL));
617 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09618 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03619 context.reset(NewContext(NULL));
620 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09621 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03622
623 bool created = false;
624 context.reset(NewContext(NULL));
625 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09626 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03627 if (overwrite) {
628 context.reset(NewContext(NULL));
629 EXPECT_EQ(base::PLATFORM_FILE_OK,
630 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09631 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03632 }
633
[email protected]949f25a2012-06-27 01:53:09634 ClearTimestamp(src_dir_url);
635 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03636 context.reset(NewContext(NULL));
637 EXPECT_EQ(base::PLATFORM_FILE_OK,
638 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09639 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03640 copy));
[email protected]fad625e2f2011-12-08 05:38:03641 if (copy)
[email protected]949f25a2012-06-27 01:53:09642 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03643 else
[email protected]949f25a2012-06-27 01:53:09644 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
645 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03646 }
647
[email protected]ecdfd6c52012-04-11 13:35:44648 int64 ComputeCurrentUsage() {
[email protected]1c98fdd2013-05-24 09:45:27649 return sandbox_file_system_.ComputeCurrentOriginUsage() -
650 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]02a60542012-07-24 20:05:33651 }
[email protected]45ea0fbbb2012-02-27 22:28:49652
[email protected]07b64872013-02-13 11:46:30653 FileSystemContext* file_system_context() {
[email protected]1c98fdd2013-05-24 09:45:27654 return sandbox_file_system_.file_system_context();
[email protected]07b64872013-02-13 11:46:30655 }
656
[email protected]d4905e2e2011-05-13 21:56:32657 private:
[email protected]ea1a3f62012-11-16 20:34:23658 base::ScopedTempDir data_dir_;
[email protected]4cc586b2013-05-07 12:43:32659 base::MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16660 scoped_refptr<quota::QuotaManager> quota_manager_;
661 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26662 GURL origin_;
663 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03664 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]1c98fdd2013-05-24 09:45:27665 SandboxFileSystemTestHelper sandbox_file_system_;
[email protected]0c5ebe32011-08-19 22:37:16666 quota::QuotaStatusCode quota_status_;
667 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10668 MockFileChangeObserver change_observer_;
669 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32670
[email protected]7878ece2011-09-05 11:41:49671 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32672};
673
[email protected]7878ece2011-09-05 11:41:49674TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32675 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
676 bool created;
[email protected]949f25a2012-06-27 01:53:09677 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16678 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32679 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
680
681 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49682 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09683 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32684 &created));
685
[email protected]0c5ebe32011-08-19 22:37:16686 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32687 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09688 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32689
[email protected]949f25a2012-06-27 01:53:09690 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32691
[email protected]c4e6f9c2012-09-09 17:42:10692 EXPECT_TRUE(change_observer()->HasNoChange());
693
[email protected]0c5ebe32011-08-19 22:37:16694 // Verify that file creation requires sufficient quota for the path.
695 context.reset(NewContext(NULL));
696 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09697 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16698 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49699 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09700 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16701
702 context.reset(NewContext(NULL));
703 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09704 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32705 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49706 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09707 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32708 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10709 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32710 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
711
[email protected]949f25a2012-06-27 01:53:09712 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32713
[email protected]0c5ebe32011-08-19 22:37:16714 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33715 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09717 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32718 EXPECT_TRUE(file_util::PathExists(local_path));
719
[email protected]0c5ebe32011-08-19 22:37:16720 // Verify that deleting a file isn't stopped by zero quota, and that it frees
721 // up quote from its path.
722 context.reset(NewContext(NULL));
723 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32724 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09725 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10726 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32727 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09728 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16729 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32730
[email protected]0c5ebe32011-08-19 22:37:16731 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32732 bool exclusive = true;
733 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09734 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50735 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42736 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49737 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09738 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10739 // The oepration created 3 directories recursively.
740 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32741
[email protected]0c5ebe32011-08-19 22:37:16742 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32743 file_handle = base::kInvalidPlatformFileValue;
744 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49745 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09746 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32747 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10748 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32749 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
750
[email protected]949f25a2012-06-27 01:53:09751 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32752
[email protected]0c5ebe32011-08-19 22:37:16753 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49754 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09755 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32756 EXPECT_TRUE(file_util::PathExists(local_path));
757
[email protected]0c5ebe32011-08-19 22:37:16758 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32759 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09760 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10761 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32762 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10763
764 // Make sure we have no unexpected changes.
765 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32766}
767
[email protected]7878ece2011-09-05 11:41:49768TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32769 bool created = false;
[email protected]949f25a2012-06-27 01:53:09770 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16771 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32772
773 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09774 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32775
[email protected]0c5ebe32011-08-19 22:37:16776 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32777 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09778 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32779 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10780 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32781
[email protected]0c5ebe32011-08-19 22:37:16782 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33783 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49784 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09785 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32786 EXPECT_EQ(0, GetSize(local_path));
787
[email protected]0c5ebe32011-08-19 22:37:16788 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49789 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09790 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10791 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32792 EXPECT_EQ(10, GetSize(local_path));
793
[email protected]0c5ebe32011-08-19 22:37:16794 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49795 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09796 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32797 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10798 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32799
[email protected]13f92f6e2012-08-13 07:39:14800 EXPECT_FALSE(DirectoryExists(url));
801 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10802
803 // Make sure we have no unexpected changes.
804 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32805}
806
[email protected]ecdfd6c52012-04-11 13:35:44807TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
808 bool created = false;
[email protected]949f25a2012-06-27 01:53:09809 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44810
811 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13812 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09813 AllowUsageIncrease(PathCost(url))->context(),
814 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44815 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13816 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44817
[email protected]ecdfd6c52012-04-11 13:35:44818 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13819 ofu()->Truncate(
820 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09821 url, 1020));
[email protected]294dd0312012-05-11 07:35:13822 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44823
[email protected]ecdfd6c52012-04-11 13:35:44824 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13825 ofu()->Truncate(
826 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09827 url, 0));
[email protected]294dd0312012-05-11 07:35:13828 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44829
[email protected]ecdfd6c52012-04-11 13:35:44830 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13831 ofu()->Truncate(
832 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09833 url, 1021));
[email protected]294dd0312012-05-11 07:35:13834 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44835
[email protected]ecdfd6c52012-04-11 13:35:44836 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13837 ofu()->Truncate(
838 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09839 url, 1020));
[email protected]294dd0312012-05-11 07:35:13840 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44841
[email protected]ecdfd6c52012-04-11 13:35:44842 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13843 ofu()->Truncate(
844 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09845 url, 1020));
[email protected]294dd0312012-05-11 07:35:13846 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44847
[email protected]294dd0312012-05-11 07:35:13848 // quota exceeded
849 {
850 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
851 helper->context()->set_allowed_bytes_growth(
852 helper->context()->allowed_bytes_growth() - 1);
853 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09854 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13855 ASSERT_EQ(1019, ComputeTotalFileSize());
856 }
[email protected]ecdfd6c52012-04-11 13:35:44857
858 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33859 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44860 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13861 ofu()->GetLocalFilePath(
862 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09863 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44864 ASSERT_FALSE(local_path.empty());
865 ASSERT_TRUE(file_util::Delete(local_path, false));
866
[email protected]ecdfd6c52012-04-11 13:35:44867 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13868 ofu()->Truncate(
869 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09870 url, 1234));
[email protected]294dd0312012-05-11 07:35:13871 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44872}
873
[email protected]7878ece2011-09-05 11:41:49874TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09875 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32876 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16877 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32878 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49879 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09880 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10881 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32882
[email protected]0c5ebe32011-08-19 22:37:16883 // Verify that file creation requires sufficient quota for the path.
884 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09885 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32886 created = false;
[email protected]0c5ebe32011-08-19 22:37:16887 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09888 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16889 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09890 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16891 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10892 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16893
894 context.reset(NewContext(NULL));
895 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09896 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32897 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09898 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32899 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10900 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32901
[email protected]949f25a2012-06-27 01:53:09902 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32903
[email protected]0c5ebe32011-08-19 22:37:16904 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32905 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09906 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32907 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10908 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32909
910 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09911 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16912 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32913 bool exclusive = true;
914 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49915 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09916 context.get(),
[email protected]04c899f2013-02-08 08:28:42917 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09918 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10919 // 2 directories: path/ and path/to.
920 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32921
[email protected]0c5ebe32011-08-19 22:37:16922 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32923 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09924 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32925 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14926 EXPECT_FALSE(DirectoryExists(url));
927 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10928 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32929}
930
[email protected]7878ece2011-09-05 11:41:49931TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16932 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32933
934 bool exclusive = false;
935 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09936 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49937 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09938 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32939
[email protected]0c5ebe32011-08-19 22:37:16940 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32941 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08942 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32943
[email protected]007b3f82013-04-09 08:46:45944 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:14945 EXPECT_FALSE(DirectoryExists(url));
946 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16947 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49948 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32949
[email protected]0c5ebe32011-08-19 22:37:16950 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32951 exclusive = false;
952 recursive = true;
[email protected]7878ece2011-09-05 11:41:49953 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09954 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10955 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32956
[email protected]13f92f6e2012-08-13 07:39:14957 EXPECT_TRUE(DirectoryExists(url));
958 EXPECT_TRUE(PathExists(url));
959
[email protected]0c5ebe32011-08-19 22:37:16960 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49961 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42962 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14963
[email protected]0c5ebe32011-08-19 22:37:16964 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09965 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42966 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32967
968 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16969 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06970 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08971 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42972 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10973 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32974
975 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33976 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49977 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09978 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32979 EXPECT_TRUE(local_path.empty());
980 EXPECT_TRUE(file_info.is_directory);
981 EXPECT_FALSE(file_info.is_symbolic_link);
982
983 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16984 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49985 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09986 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10987 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32988
989 exclusive = true;
990 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16991 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49992 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09993 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10994 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32995
[email protected]0c5ebe32011-08-19 22:37:16996 // Verify that deleting a directory isn't stopped by zero quota, and that it
997 // frees up quota from its path.
998 context.reset(NewContext(NULL));
999 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:081000 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101001 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091002 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161003 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321004
[email protected]949f25a2012-06-27 01:53:091005 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321006
[email protected]13f92f6e2012-08-13 07:39:141007 EXPECT_FALSE(DirectoryExists(url));
1008 EXPECT_FALSE(PathExists(url));
1009
[email protected]0c5ebe32011-08-19 22:37:161010 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091011 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491012 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091013 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321014
[email protected]0c5ebe32011-08-19 22:37:161015 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321016 exclusive = true;
1017 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161018 context.reset(NewContext(NULL));
1019 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091020 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491021 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091022 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101023 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161024
1025 context.reset(NewContext(NULL));
1026 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091027 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491028 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091029 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101030 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321031
[email protected]13f92f6e2012-08-13 07:39:141032 EXPECT_TRUE(DirectoryExists(url));
1033 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321034
1035 exclusive = true;
1036 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141037 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491038 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091039 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101040 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321041
1042 exclusive = true;
1043 recursive = false;
[email protected]949f25a2012-06-27 01:53:091044 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141045 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491046 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091047 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101048 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321049
[email protected]949f25a2012-06-27 01:53:091050 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321051
[email protected]13f92f6e2012-08-13 07:39:141052 EXPECT_FALSE(DirectoryExists(url));
1053 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321054
1055 exclusive = true;
1056 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141057 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491058 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091059 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101060 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321061
[email protected]13f92f6e2012-08-13 07:39:141062 EXPECT_TRUE(DirectoryExists(url));
1063 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321064
1065 exclusive = true;
1066 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141067 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491068 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091069 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101070 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321071}
1072
[email protected]7878ece2011-09-05 11:41:491073TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161074 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321075 bool exclusive = true;
1076 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091077 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491078 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091079 context.get(), url, exclusive, recursive));
1080 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321081}
1082
[email protected]7878ece2011-09-05 11:41:491083TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451084 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321085}
1086
[email protected]7878ece2011-09-05 11:41:491087TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091088 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321089}
1090
[email protected]7878ece2011-09-05 11:41:491091TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091092 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161093 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321094
1095 bool created = false;
1096 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091097 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321098 ASSERT_TRUE(created);
1099
[email protected]c83118f2013-05-20 04:35:421100 std::vector<DirectoryEntry> entries;
[email protected]1a647202013-01-21 15:32:251101 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301102 AsyncFileTestHelper::ReadDirectory(
1103 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321104
[email protected]949f25a2012-06-27 01:53:091105 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321106}
1107
[email protected]7878ece2011-09-05 11:41:491108TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091109 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161110 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411111
1112 base::Time last_access_time = base::Time::Now();
1113 base::Time last_modified_time = base::Time::Now();
1114
1115 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321116 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491117 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091118 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321119
[email protected]2517cfa2011-08-25 05:12:411120 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161121 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411122 bool created = false;
1123 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091124 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411125 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091126 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411127
1128 // Now test a directory:
1129 context.reset(NewContext(NULL));
1130 bool exclusive = true;
1131 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091132 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491133 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091134 url, exclusive, recursive));
1135 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161136}
1137
[email protected]7878ece2011-09-05 11:41:491138TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091139 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161140 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1141
[email protected]949f25a2012-06-27 01:53:091142 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161143 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411144 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161145 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091146 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411147 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161148 context->set_allowed_bytes_growth(1024);
1149 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091150 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411151 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091152 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161153 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1154
1155 context->set_allowed_bytes_growth(1024);
1156 bool exclusive = true;
1157 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091158 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331159 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091160 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161161 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421162 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1163 for (iterator iter = components.begin();
1164 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491165 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331166 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161167 }
1168 context.reset(NewContext(NULL));
1169 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091171 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161172 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321173}
1174
[email protected]7878ece2011-09-05 11:41:491175TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091176 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1177 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161178 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321179
1180 bool is_copy_not_move = false;
1181 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091182 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321183 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101184 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161185 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321186 is_copy_not_move = true;
1187 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091188 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321189 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101190 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091191 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321192 bool exclusive = true;
1193 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161194 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491195 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091196 context.get(),
[email protected]04c899f2013-02-08 08:28:421197 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091198 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101199 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321200 is_copy_not_move = false;
1201 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091202 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321203 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101204 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161205 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321206 is_copy_not_move = true;
1207 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091208 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321209 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101210 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321211}
1212
[email protected]7878ece2011-09-05 11:41:491213TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321214 const int64 kSourceLength = 5;
1215 const int64 kDestLength = 50;
1216
1217 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1218 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1219 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1220 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1221 test_case.is_copy_not_move);
1222 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1223 test_case.source_path);
1224 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1225 test_case.dest_path);
1226 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1227 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161228 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321229
1230 bool exclusive = false;
1231 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091232 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1233 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321234
[email protected]0c5ebe32011-08-19 22:37:161235 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491236 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091237 context.get(),
[email protected]04c899f2013-02-08 08:28:421238 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091239 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161240 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491241 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091242 context.get(),
[email protected]04c899f2013-02-08 08:28:421243 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091244 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321245
[email protected]d4905e2e2011-05-13 21:56:321246 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161247 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321248 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091249 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321250 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161251 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321252 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091253 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321254
1255 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161256 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321257 created = false;
1258 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091259 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321260 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161261 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321262 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091263 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321264 }
1265
[email protected]0c5ebe32011-08-19 22:37:161266 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491267 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091268 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101269
[email protected]d4905e2e2011-05-13 21:56:321270 if (test_case.is_copy_not_move) {
1271 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331272 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161273 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491274 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091275 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321276 EXPECT_EQ(kSourceLength, file_info.size);
1277 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091278 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321279 } else {
1280 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331281 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161282 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491283 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091284 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321285 }
1286 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331287 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491288 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091289 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321290 EXPECT_EQ(kSourceLength, file_info.size);
1291
1292 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091293 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321294 }
1295}
1296
[email protected]7878ece2011-09-05 11:41:491297TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091298 FileSystemURL src_url = CreateURLFromUTF8("src path");
1299 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161300 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1301 bool created = false;
[email protected]7878ece2011-09-05 11:41:491302 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091303 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161304
1305 bool is_copy = true;
1306 // Copy, no overwrite.
1307 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091308 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161309 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091310 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161311 context.reset(NewContext(NULL));
1312 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091313 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161314 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091315 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161316
1317 // Copy, with overwrite.
1318 context.reset(NewContext(NULL));
1319 context->set_allowed_bytes_growth(0);
1320 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091321 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161322}
1323
[email protected]7878ece2011-09-05 11:41:491324TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091325 FileSystemURL src_url = CreateURLFromUTF8("src path");
1326 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161327 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1328 bool created = false;
[email protected]7878ece2011-09-05 11:41:491329 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091330 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161331
1332 bool is_copy = false;
1333 // Move, rename, no overwrite.
1334 context.reset(NewContext(NULL));
1335 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091336 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1337 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161338 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091339 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161340 context.reset(NewContext(NULL));
1341 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091342 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1343 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161344 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091345 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161346
1347 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491348 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091349 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161350
1351 // Move, rename, with overwrite.
1352 context.reset(NewContext(NULL));
1353 context->set_allowed_bytes_growth(0);
1354 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091355 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161356}
1357
[email protected]7878ece2011-09-05 11:41:491358TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091359 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161360 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1361 bool created = false;
[email protected]7878ece2011-09-05 11:41:491362 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091363 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161364
1365 bool exclusive = true;
1366 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091367 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161368 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491369 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091370 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161371
[email protected]04c899f2013-02-08 08:28:421372 FileSystemURL dest_url = FileSystemURLAppend(
1373 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161374
1375 bool is_copy = false;
1376 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1377 // Move, no rename, no overwrite.
1378 context.reset(NewContext(NULL));
1379 context->set_allowed_bytes_growth(allowed_bytes_growth);
1380 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091381 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161382 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1383
1384 // Move, no rename, with overwrite.
1385 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491386 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091387 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161388 context.reset(NewContext(NULL));
1389 context->set_allowed_bytes_growth(allowed_bytes_growth);
1390 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091391 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161392 EXPECT_EQ(
1393 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091394 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161395 context->allowed_bytes_growth());
1396}
1397
[email protected]7878ece2011-09-05 11:41:491398TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461399 TestCopyInForeignFileHelper(false /* overwrite */);
1400 TestCopyInForeignFileHelper(true /* overwrite */);
1401}
1402
[email protected]7878ece2011-09-05 11:41:491403TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161404 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091405 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321406 bool exclusive = true;
1407 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491408 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091409 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321410
[email protected]a3ef4832013-02-02 05:12:331411 std::set<base::FilePath::StringType> files;
1412 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091413 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321414
[email protected]949f25a2012-06-27 01:53:091415 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321416
[email protected]13f92f6e2012-08-13 07:39:141417 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321418 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301419 AsyncFileTestHelper::Copy(
[email protected]1c98fdd2013-05-24 09:45:271420 file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321421
[email protected]949f25a2012-06-27 01:53:091422 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141423 EXPECT_TRUE(DirectoryExists(src_url));
1424 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321425 recursive = true;
1426 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301427 AsyncFileTestHelper::Remove(
1428 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141429 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321430}
[email protected]6b931152011-05-20 21:02:351431
[email protected]7878ece2011-09-05 11:41:491432TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1433 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1434 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161435 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261436 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161437 EXPECT_EQ(origin(), enumerator->Next());
1438 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1439 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1440 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261441 EXPECT_EQ(GURL(), enumerator->Next());
1442 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1443 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1444
1445 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161446 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261447
1448 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1449 SCOPED_TRACE(testing::Message() <<
1450 "Validating kOriginEnumerationTestRecords " << i);
1451 const OriginEnumerationTestRecord& record =
1452 kOriginEnumerationTestRecords[i];
1453 GURL origin_url(record.origin_url);
1454 origins_expected.insert(origin_url);
1455 if (record.has_temporary) {
[email protected]1c98fdd2013-05-24 09:45:271456 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1457 NewFileSystem(origin_url, kFileSystemTypeTemporary));
1458 scoped_ptr<FileSystemOperationContext> context(
1459 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261460 bool created = false;
1461 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501462 ofu()->EnsureFileExists(
1463 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271464 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501465 &created));
[email protected]fcc2d5f2011-05-23 22:06:261466 EXPECT_TRUE(created);
1467 }
1468 if (record.has_persistent) {
[email protected]1c98fdd2013-05-24 09:45:271469 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1470 NewFileSystem(origin_url, kFileSystemTypePersistent));
1471 scoped_ptr<FileSystemOperationContext> context(
1472 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261473 bool created = false;
1474 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501475 ofu()->EnsureFileExists(
1476 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271477 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501478 &created));
[email protected]fcc2d5f2011-05-23 22:06:261479 EXPECT_TRUE(created);
1480 }
1481 }
[email protected]7878ece2011-09-05 11:41:491482 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261483 EXPECT_TRUE(enumerator.get());
1484 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161485 GURL origin_url;
1486 while (!(origin_url = enumerator->Next()).is_empty()) {
1487 origins_found.insert(origin_url);
1488 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261489 bool found = false;
1490 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1491 ++i) {
1492 const OriginEnumerationTestRecord& record =
1493 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161494 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261495 continue;
1496 found = true;
1497 EXPECT_EQ(record.has_temporary,
1498 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1499 EXPECT_EQ(record.has_persistent,
1500 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1501 }
[email protected]0c5ebe32011-08-19 22:37:161502 // Deal with the default filesystem created by the test helper.
1503 if (!found && origin_url == origin()) {
1504 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1505 EXPECT_EQ(true,
1506 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181507 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161508 found = true;
1509 }
[email protected]fcc2d5f2011-05-23 22:06:261510 EXPECT_TRUE(found);
1511 }
1512
1513 std::set<GURL> diff;
1514 std::set_symmetric_difference(origins_expected.begin(),
1515 origins_expected.end(), origins_found.begin(), origins_found.end(),
1516 inserter(diff, diff.begin()));
1517 EXPECT_TRUE(diff.empty());
1518}
[email protected]0c5ebe32011-08-19 22:37:161519
[email protected]7878ece2011-09-05 11:41:491520TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161521 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1522
1523 int64 expected_quota = 0;
1524
[email protected]f83b5b72012-01-27 10:26:561525 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011526 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561527 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331528 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501529 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161530 if (test_case.is_directory) {
1531 bool exclusive = true;
1532 bool recursive = false;
1533 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091534 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501535 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161536 } else {
1537 bool created = false;
1538 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091539 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501540 &created));
[email protected]0c5ebe32011-08-19 22:37:161541 ASSERT_TRUE(created);
1542 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501543 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091544 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501545 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161546 expected_quota += test_case.data_file_size;
1547 }
1548 }
[email protected]022d2702012-05-14 16:04:261549
1550 // Usually raw size in usage cache and the usage returned by QuotaUtil
1551 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161552 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261553 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1554
[email protected]0c5ebe32011-08-19 22:37:161555 RevokeUsageCache();
1556 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261557 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1558
1559 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161560 GetUsageFromQuotaManager();
1561 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261562 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161563 EXPECT_EQ(expected_quota, usage());
1564}
[email protected]34583332011-08-31 08:59:471565
[email protected]7878ece2011-09-05 11:41:491566TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091567 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1568 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471569
1570 scoped_ptr<FileSystemOperationContext> context;
1571 base::PlatformFile file;
1572 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331573 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471574 bool created = false;
1575
1576 // Create a non-empty file.
1577 context.reset(NewContext(NULL));
1578 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491579 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471580 EXPECT_TRUE(created);
1581 context.reset(NewContext(NULL));
1582 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491583 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471584 context.reset(NewContext(NULL));
1585 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491586 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471587 context.get(), kPath1, &file_info, &data_path));
1588 EXPECT_EQ(10, file_info.size);
1589
1590 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491591 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471592
1593 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141594 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471595 context.reset(NewContext(NULL));
1596 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491597 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471598 EXPECT_TRUE(created);
1599 context.reset(NewContext(NULL));
1600 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491601 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471602 context.get(), kPath1, &file_info, &data_path));
1603 EXPECT_EQ(0, file_info.size);
1604
1605 // Make another broken file to |kPath2|.
1606 context.reset(NewContext(NULL));
1607 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491608 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471609 EXPECT_TRUE(created);
1610
1611 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491612 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471613
1614 // Repair broken |kPath1|.
1615 context.reset(NewContext(NULL));
1616 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491617 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471618 base::Time::Now()));
1619 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491620 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471621 EXPECT_TRUE(created);
1622
1623 // Copy from sound |kPath1| to broken |kPath2|.
1624 context.reset(NewContext(NULL));
1625 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491626 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501627 true /* copy */));
[email protected]34583332011-08-31 08:59:471628
[email protected]7878ece2011-09-05 11:41:491629 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471630 context.reset(NewContext(NULL));
1631 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491632 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471633 context.get(), kPath1,
1634 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1635 &file, &created));
1636 EXPECT_TRUE(created);
1637 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1638 EXPECT_EQ(0, file_info.size);
1639 EXPECT_TRUE(base::ClosePlatformFile(file));
1640}
[email protected]9dfdc0e32011-09-02 06:07:441641
[email protected]7878ece2011-09-05 11:41:491642TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091643 const FileSystemURL kPath[] = {
1644 CreateURLFromUTF8("foo"),
1645 CreateURLFromUTF8("bar"),
1646 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441647 };
[email protected]a3ef4832013-02-02 05:12:331648 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441649 scoped_ptr<FileSystemOperationContext> context;
1650
1651 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1652 bool created = false;
1653 context.reset(NewContext(NULL));
1654 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491655 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441656 EXPECT_TRUE(created);
1657 }
1658
[email protected]c83118f2013-05-20 04:35:421659 std::vector<DirectoryEntry> entries;
[email protected]9dfdc0e32011-09-02 06:07:441660 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301661 AsyncFileTestHelper::ReadDirectory(
1662 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441663 EXPECT_EQ(3u, entries.size());
1664
[email protected]a3ef4832013-02-02 05:12:331665 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441666 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491667 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441668 EXPECT_TRUE(file_util::Delete(local_path, false));
1669
[email protected]9dfdc0e32011-09-02 06:07:441670 entries.clear();
1671 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301672 AsyncFileTestHelper::ReadDirectory(
1673 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441674 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1675}
[email protected]fad625e2f2011-12-08 05:38:031676
1677TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1678 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091679 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031680
1681 // Create working directory.
1682 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091683 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031684
1685 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421686 FileSystemURL url(FileSystemURLAppendUTF8(
1687 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031688 bool created = false;
[email protected]949f25a2012-06-27 01:53:091689 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031690 context.reset(NewContext(NULL));
1691 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091692 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031693 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091694 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031695
1696 // non create case.
1697 created = true;
[email protected]949f25a2012-06-27 01:53:091698 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031699 context.reset(NewContext(NULL));
1700 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091701 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031702 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091703 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031704
1705 // fail case.
[email protected]04c899f2013-02-08 08:28:421706 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031707 context.reset(NewContext(NULL));
1708 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091709 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031710
[email protected]949f25a2012-06-27 01:53:091711 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031712 context.reset(NewContext(NULL));
1713 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091714 ofu()->EnsureFileExists(context.get(), url, &created));
1715 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031716
1717 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421718 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391719 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031720 created = false;
[email protected]949f25a2012-06-27 01:53:091721 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031722 context.reset(NewContext(NULL));
1723 EXPECT_EQ(base::PLATFORM_FILE_OK,
1724 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091725 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031726 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1727 &file_handle, &created));
1728 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1729 EXPECT_TRUE(created);
1730 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091731 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031732
1733 // open case.
1734 file_handle = base::kInvalidPlatformFileValue;
1735 created = true;
[email protected]949f25a2012-06-27 01:53:091736 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031737 context.reset(NewContext(NULL));
1738 EXPECT_EQ(base::PLATFORM_FILE_OK,
1739 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091740 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031741 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1742 &file_handle, &created));
1743 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1744 EXPECT_FALSE(created);
1745 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091746 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031747
1748 // fail case
1749 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091750 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031751 context.reset(NewContext(NULL));
1752 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1753 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091754 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031755 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1756 &file_handle, &created));
1757 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091758 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031759
1760 // CreateDirectory, create case.
1761 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421762 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1763 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091764 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031765 context.reset(NewContext(NULL));
1766 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091767 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031768 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091769 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031770
1771 // create subdir case.
1772 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421773 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091774 ClearTimestamp(dir_url);
1775 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031776 context.reset(NewContext(NULL));
1777 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091778 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031779 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091780 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1781 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031782
1783 // fail case.
[email protected]04c899f2013-02-08 08:28:421784 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091785 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031786 context.reset(NewContext(NULL));
1787 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091788 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031789 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091790 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031791
1792 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421793 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1794 FileSystemURL src_path = FileSystemURLAppendUTF8(
1795 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031796 context.reset(NewContext(NULL));
1797 EXPECT_EQ(base::PLATFORM_FILE_OK,
1798 ofu()->EnsureFileExists(context.get(), src_path, &created));
1799 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331800 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031801 context.reset(NewContext(NULL));
1802 EXPECT_EQ(base::PLATFORM_FILE_OK,
1803 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1804
[email protected]949f25a2012-06-27 01:53:091805 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031806 context.reset(NewContext(NULL));
1807 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501808 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131809 src_local_path,
[email protected]949f25a2012-06-27 01:53:091810 url));
1811 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031812}
1813
1814TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1815 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091816 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031817
1818 // Create working directory.
1819 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091820 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031821
1822 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421823 FileSystemURL url = FileSystemURLAppendUTF8(
1824 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031825 bool created = false;
1826 context.reset(NewContext(NULL));
1827 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091828 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031829 EXPECT_TRUE(created);
1830
[email protected]949f25a2012-06-27 01:53:091831 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031832 context.reset(NewContext(NULL));
1833 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091834 ofu()->DeleteFile(context.get(), url));
1835 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031836
1837 // fail case.
[email protected]949f25a2012-06-27 01:53:091838 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031839 context.reset(NewContext(NULL));
1840 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091841 ofu()->DeleteFile(context.get(), url));
1842 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031843
[email protected]bab213be2013-01-23 15:13:081844 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421845 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1846 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031847 context.reset(NewContext(NULL));
1848 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091849 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031850 created = false;
1851 context.reset(NewContext(NULL));
1852 EXPECT_EQ(base::PLATFORM_FILE_OK,
1853 ofu()->EnsureFileExists(context.get(), file_path, &created));
1854 EXPECT_TRUE(created);
1855
[email protected]949f25a2012-06-27 01:53:091856 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031857 context.reset(NewContext(NULL));
1858 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081859 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091860 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031861
1862 // delete case.
1863 context.reset(NewContext(NULL));
1864 EXPECT_EQ(base::PLATFORM_FILE_OK,
1865 ofu()->DeleteFile(context.get(), file_path));
1866
[email protected]949f25a2012-06-27 01:53:091867 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031868 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081869 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091870 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031871}
1872
1873TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1874 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091875 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031876 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091877 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031878 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091879 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031880 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091881 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031882}
[email protected]a3938912012-03-27 14:00:551883
1884TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091885 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421886 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1887 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551888
1889 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1890 EXPECT_EQ(base::PLATFORM_FILE_OK,
1891 ofu()->CreateDirectory(context.get(), dir, false, false));
1892
1893 bool created = false;
1894 context.reset(NewContext(NULL));
1895 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091896 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551897 EXPECT_TRUE(created);
1898
1899 context.reset(NewContext(NULL));
1900 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091901 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551902
[email protected]a3ef4832013-02-02 05:12:331903 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551904 context.reset(NewContext(NULL));
1905 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091906 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551907 EXPECT_FALSE(file_path.empty());
1908
1909 context.reset(NewContext(NULL));
1910 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091911 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551912 base::Time::Now() + base::TimeDelta::FromHours(1),
1913 base::Time()));
1914
1915 context.reset(NewContext(NULL));
1916 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1917 ofu()->CreateFileEnumerator(context.get(), dir, false));
1918
1919 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331920 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551921 while (!(file_path_each = file_enum->Next()).empty()) {
1922 context.reset(NewContext(NULL));
1923 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331924 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551925 EXPECT_EQ(base::PLATFORM_FILE_OK,
1926 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421927 FileSystemURL::CreateForTest(
1928 dir.origin(),
1929 dir.mount_type(),
1930 file_path_each),
[email protected]a3938912012-03-27 14:00:551931 &file_info, &file_path));
1932 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1933 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1934 EXPECT_EQ(file_info.size, file_enum->Size());
1935 ++count;
1936 }
1937 EXPECT_EQ(2, count);
1938}
[email protected]294dd0312012-05-11 07:35:131939
[email protected]58ac5272013-02-15 10:05:031940// crbug.com/176470
1941#if defined(OS_WIN) || defined(OS_ANDROID)
1942#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1943#else
1944#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1945#endif
1946TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091947 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1948 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1949 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1950 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131951 bool created;
1952
1953 int64 expected_total_file_size = 0;
1954 ASSERT_EQ(base::PLATFORM_FILE_OK,
1955 ofu()->EnsureFileExists(
1956 AllowUsageIncrease(PathCost(from_file))->context(),
1957 from_file, &created));
1958 ASSERT_TRUE(created);
1959 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1960
1961 ASSERT_EQ(base::PLATFORM_FILE_OK,
1962 ofu()->EnsureFileExists(
1963 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1964 obstacle_file, &created));
1965 ASSERT_TRUE(created);
1966 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1967
1968 int64 from_file_size = 1020;
1969 expected_total_file_size += from_file_size;
1970 ASSERT_EQ(base::PLATFORM_FILE_OK,
1971 ofu()->Truncate(
1972 AllowUsageIncrease(from_file_size)->context(),
1973 from_file, from_file_size));
1974 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1975
1976 int64 obstacle_file_size = 1;
1977 expected_total_file_size += obstacle_file_size;
1978 ASSERT_EQ(base::PLATFORM_FILE_OK,
1979 ofu()->Truncate(
1980 AllowUsageIncrease(obstacle_file_size)->context(),
1981 obstacle_file, obstacle_file_size));
1982 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1983
1984 int64 to_file1_size = from_file_size;
1985 expected_total_file_size += to_file1_size;
1986 ASSERT_EQ(base::PLATFORM_FILE_OK,
1987 ofu()->CopyOrMoveFile(
1988 AllowUsageIncrease(
1989 PathCost(to_file1) + to_file1_size)->context(),
1990 from_file, to_file1, true /* copy */));
1991 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1992
1993 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1994 ofu()->CopyOrMoveFile(
1995 DisallowUsageIncrease(
1996 PathCost(to_file2) + from_file_size)->context(),
1997 from_file, to_file2, true /* copy */));
1998 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1999
2000 int64 old_obstacle_file_size = obstacle_file_size;
2001 obstacle_file_size = from_file_size;
2002 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2003 ASSERT_EQ(base::PLATFORM_FILE_OK,
2004 ofu()->CopyOrMoveFile(
2005 AllowUsageIncrease(
2006 obstacle_file_size - old_obstacle_file_size)->context(),
2007 from_file, obstacle_file, true /* copy */));
2008 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2009
2010 int64 old_from_file_size = from_file_size;
2011 from_file_size = old_from_file_size - 1;
2012 expected_total_file_size += from_file_size - old_from_file_size;
2013 ASSERT_EQ(base::PLATFORM_FILE_OK,
2014 ofu()->Truncate(
2015 AllowUsageIncrease(
2016 from_file_size - old_from_file_size)->context(),
2017 from_file, from_file_size));
2018 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2019
2020 // quota exceeded
2021 {
2022 old_obstacle_file_size = obstacle_file_size;
2023 obstacle_file_size = from_file_size;
2024 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2025 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2026 obstacle_file_size - old_obstacle_file_size);
2027 helper->context()->set_allowed_bytes_growth(
2028 helper->context()->allowed_bytes_growth() - 1);
2029 ASSERT_EQ(base::PLATFORM_FILE_OK,
2030 ofu()->CopyOrMoveFile(
2031 helper->context(),
2032 from_file, obstacle_file, true /* copy */));
2033 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2034 }
2035}
2036
2037TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092038 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2039 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2040 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132041 bool created;
2042
2043 int64 expected_total_file_size = 0;
2044 ASSERT_EQ(base::PLATFORM_FILE_OK,
2045 ofu()->EnsureFileExists(
2046 AllowUsageIncrease(PathCost(from_file))->context(),
2047 from_file, &created));
2048 ASSERT_TRUE(created);
2049 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2050
2051 int64 from_file_size = 1020;
2052 expected_total_file_size += from_file_size;
2053 ASSERT_EQ(base::PLATFORM_FILE_OK,
2054 ofu()->Truncate(
2055 AllowUsageIncrease(from_file_size)->context(),
2056 from_file, from_file_size));
2057 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2058
2059 int64 to_file_size ALLOW_UNUSED = from_file_size;
2060 from_file_size = 0;
2061 ASSERT_EQ(base::PLATFORM_FILE_OK,
2062 ofu()->CopyOrMoveFile(
2063 AllowUsageIncrease(-PathCost(from_file) +
2064 PathCost(to_file))->context(),
2065 from_file, to_file, false /* move */));
2066 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2067
2068 ASSERT_EQ(base::PLATFORM_FILE_OK,
2069 ofu()->EnsureFileExists(
2070 AllowUsageIncrease(PathCost(from_file))->context(),
2071 from_file, &created));
2072 ASSERT_TRUE(created);
2073 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2074
2075 ASSERT_EQ(base::PLATFORM_FILE_OK,
2076 ofu()->EnsureFileExists(
2077 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2078 obstacle_file, &created));
2079 ASSERT_TRUE(created);
2080 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2081
2082 from_file_size = 1020;
2083 expected_total_file_size += from_file_size;
2084 ASSERT_EQ(base::PLATFORM_FILE_OK,
2085 ofu()->Truncate(
2086 AllowUsageIncrease(from_file_size)->context(),
2087 from_file, from_file_size));
2088 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2089
2090 int64 obstacle_file_size = 1;
2091 expected_total_file_size += obstacle_file_size;
2092 ASSERT_EQ(base::PLATFORM_FILE_OK,
2093 ofu()->Truncate(
2094 AllowUsageIncrease(1)->context(),
2095 obstacle_file, obstacle_file_size));
2096 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2097
2098 int64 old_obstacle_file_size = obstacle_file_size;
2099 obstacle_file_size = from_file_size;
2100 from_file_size = 0;
2101 expected_total_file_size -= old_obstacle_file_size;
2102 ASSERT_EQ(base::PLATFORM_FILE_OK,
2103 ofu()->CopyOrMoveFile(
2104 AllowUsageIncrease(
2105 -old_obstacle_file_size - PathCost(from_file))->context(),
2106 from_file, obstacle_file,
2107 false /* move */));
2108 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2109
2110 ASSERT_EQ(base::PLATFORM_FILE_OK,
2111 ofu()->EnsureFileExists(
2112 AllowUsageIncrease(PathCost(from_file))->context(),
2113 from_file, &created));
2114 ASSERT_TRUE(created);
2115 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2116
2117 from_file_size = 10;
2118 expected_total_file_size += from_file_size;
2119 ASSERT_EQ(base::PLATFORM_FILE_OK,
2120 ofu()->Truncate(
2121 AllowUsageIncrease(from_file_size)->context(),
2122 from_file, from_file_size));
2123 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2124
2125 // quota exceeded even after operation
2126 old_obstacle_file_size = obstacle_file_size;
2127 obstacle_file_size = from_file_size;
2128 from_file_size = 0;
2129 expected_total_file_size -= old_obstacle_file_size;
2130 scoped_ptr<FileSystemOperationContext> context =
2131 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2132 ASSERT_EQ(base::PLATFORM_FILE_OK,
2133 ofu()->CopyOrMoveFile(
2134 context.get(), from_file, obstacle_file, false /* move */));
2135 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2136 context.reset();
2137}
2138
2139TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092140 FileSystemURL dir(CreateURLFromUTF8("dir"));
2141 FileSystemURL file(CreateURLFromUTF8("file"));
2142 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2143 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132144 bool created;
2145
2146 ASSERT_EQ(base::PLATFORM_FILE_OK,
2147 ofu()->EnsureFileExists(
2148 AllowUsageIncrease(PathCost(file))->context(),
2149 file, &created));
2150 ASSERT_TRUE(created);
2151 ASSERT_EQ(0, ComputeTotalFileSize());
2152
2153 ASSERT_EQ(base::PLATFORM_FILE_OK,
2154 ofu()->CreateDirectory(
2155 AllowUsageIncrease(PathCost(dir))->context(),
2156 dir, false, false));
2157 ASSERT_EQ(0, ComputeTotalFileSize());
2158
2159 ASSERT_EQ(base::PLATFORM_FILE_OK,
2160 ofu()->EnsureFileExists(
2161 AllowUsageIncrease(PathCost(dfile1))->context(),
2162 dfile1, &created));
2163 ASSERT_TRUE(created);
2164 ASSERT_EQ(0, ComputeTotalFileSize());
2165
2166 ASSERT_EQ(base::PLATFORM_FILE_OK,
2167 ofu()->EnsureFileExists(
2168 AllowUsageIncrease(PathCost(dfile2))->context(),
2169 dfile2, &created));
2170 ASSERT_TRUE(created);
2171 ASSERT_EQ(0, ComputeTotalFileSize());
2172
2173 ASSERT_EQ(base::PLATFORM_FILE_OK,
2174 ofu()->Truncate(
2175 AllowUsageIncrease(340)->context(),
2176 file, 340));
2177 ASSERT_EQ(340, ComputeTotalFileSize());
2178
2179 ASSERT_EQ(base::PLATFORM_FILE_OK,
2180 ofu()->Truncate(
2181 AllowUsageIncrease(1020)->context(),
2182 dfile1, 1020));
2183 ASSERT_EQ(1360, ComputeTotalFileSize());
2184
2185 ASSERT_EQ(base::PLATFORM_FILE_OK,
2186 ofu()->Truncate(
2187 AllowUsageIncrease(120)->context(),
2188 dfile2, 120));
2189 ASSERT_EQ(1480, ComputeTotalFileSize());
2190
2191 ASSERT_EQ(base::PLATFORM_FILE_OK,
2192 ofu()->DeleteFile(
2193 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2194 file));
2195 ASSERT_EQ(1140, ComputeTotalFileSize());
2196
2197 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302198 AsyncFileTestHelper::Remove(
2199 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132200 ASSERT_EQ(0, ComputeTotalFileSize());
2201}
[email protected]7d78be12012-05-24 07:07:262202
2203TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092204 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262205 base::PlatformFile file_handle;
2206 bool created;
2207
2208 // Creating a file.
2209 ASSERT_EQ(base::PLATFORM_FILE_OK,
2210 ofu()->EnsureFileExists(
2211 AllowUsageIncrease(PathCost(file))->context(),
2212 file, &created));
2213 ASSERT_TRUE(created);
2214 ASSERT_EQ(0, ComputeTotalFileSize());
2215
2216 // Opening it, which shouldn't change the usage.
2217 ASSERT_EQ(base::PLATFORM_FILE_OK,
2218 ofu()->CreateOrOpen(
2219 AllowUsageIncrease(0)->context(), file,
2220 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2221 &file_handle, &created));
2222 ASSERT_EQ(0, ComputeTotalFileSize());
2223 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2224
2225 const int length = 33;
2226 ASSERT_EQ(base::PLATFORM_FILE_OK,
2227 ofu()->Truncate(
2228 AllowUsageIncrease(length)->context(), file, length));
2229 ASSERT_EQ(length, ComputeTotalFileSize());
2230
2231 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2232 ASSERT_EQ(base::PLATFORM_FILE_OK,
2233 ofu()->CreateOrOpen(
2234 AllowUsageIncrease(-length)->context(), file,
2235 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2236 &file_handle, &created));
2237 ASSERT_EQ(0, ComputeTotalFileSize());
2238 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2239
2240 // Extending the file again.
2241 ASSERT_EQ(base::PLATFORM_FILE_OK,
2242 ofu()->Truncate(
2243 AllowUsageIncrease(length)->context(), file, length));
2244 ASSERT_EQ(length, ComputeTotalFileSize());
2245
2246 // Opening it with TRUNCATED flag, which should truncate the file size.
2247 ASSERT_EQ(base::PLATFORM_FILE_OK,
2248 ofu()->CreateOrOpen(
2249 AllowUsageIncrease(-length)->context(), file,
2250 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2251 &file_handle, &created));
2252 ASSERT_EQ(0, ComputeTotalFileSize());
2253 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2254}
[email protected]c4e6f9c2012-09-09 17:42:102255
[email protected]17258682013-06-05 13:38:402256TEST_F(ObfuscatedFileUtilTest, MaybeDropDatabasesAliveCase) {
2257 base::ScopedTempDir data_dir;
2258 ASSERT_TRUE(data_dir.CreateUniqueTempDir());
2259 ObfuscatedFileUtil file_util(NULL,
2260 data_dir.path(),
2261 base::MessageLoopProxy::current());
2262 file_util.InitOriginDatabase(true /*create*/);
2263 ASSERT_TRUE(file_util.origin_database_ != NULL);
2264
2265 // Callback to Drop DB is called while ObfuscatedFileUtilTest is still alive.
2266 file_util.db_flush_delay_seconds_ = 0;
2267 file_util.MarkUsed();
2268 base::MessageLoop::current()->RunUntilIdle();
2269
2270 ASSERT_TRUE(file_util.origin_database_ == NULL);
2271}
2272
2273TEST_F(ObfuscatedFileUtilTest, MaybeDropDatabasesAlreadyDeletedCase) {
2274 // Run message loop after OFU is already deleted to make sure callback doesn't
2275 // cause a crash for use after free.
2276 {
2277 base::ScopedTempDir data_dir;
2278 ASSERT_TRUE(data_dir.CreateUniqueTempDir());
2279 ObfuscatedFileUtil file_util(NULL,
2280 data_dir.path(),
2281 base::MessageLoopProxy::current());
2282 file_util.InitOriginDatabase(true /*create*/);
2283 file_util.db_flush_delay_seconds_ = 0;
2284 file_util.MarkUsed();
2285 }
2286
2287 // At this point the callback is still in the message queue but OFU is gone.
2288 base::MessageLoop::current()->RunUntilIdle();
2289}
2290
[email protected]c4e6f9c2012-09-09 17:42:102291} // namespace fileapi