blob: 93a40155714ecce019fd1e16be56923db3f30bc5 [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]ff875be52013-06-02 23:47:38136 quota_manager_ = new quota::QuotaManager(false /* is_incognito */,
137 data_dir_.path(),
138 base::MessageLoopProxy::current(),
139 base::MessageLoopProxy::current(),
140 storage_policy.get());
[email protected]0c5ebe32011-08-19 22:37:16141
[email protected]1c98fdd2013-05-24 09:45:27142 // Every time we create a new sandbox_file_system helper,
143 // it creates another context, which creates another path manager,
144 // another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49145 // another OFU. We need to pass in the context to skip all that.
[email protected]420fb562013-04-18 01:46:34146 file_system_context_ = CreateFileSystemContextForTesting(
[email protected]0c5ebe32011-08-19 22:37:16147 quota_manager_->proxy(),
[email protected]420fb562013-04-18 01:46:34148 data_dir_.path());
[email protected]0c5ebe32011-08-19 22:37:16149
[email protected]1c98fdd2013-05-24 09:45:27150 sandbox_file_system_.SetUp(file_system_context_.get());
[email protected]c4e6f9c2012-09-09 17:42:10151
152 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32153 }
154
[email protected]7fd8fa4f2013-02-07 05:43:50155 virtual void TearDown() {
[email protected]e7e46732012-01-05 11:45:55156 quota_manager_ = NULL;
[email protected]1c98fdd2013-05-24 09:45:27157 sandbox_file_system_.TearDown();
[email protected]e7e46732012-01-05 11:45:55158 }
159
[email protected]294dd0312012-05-11 07:35:13160 scoped_ptr<FileSystemOperationContext> LimitedContext(
161 int64 allowed_bytes_growth) {
162 scoped_ptr<FileSystemOperationContext> context(
[email protected]1c98fdd2013-05-24 09:45:27163 sandbox_file_system_.NewOperationContext());
[email protected]294dd0312012-05-11 07:35:13164 context->set_allowed_bytes_growth(allowed_bytes_growth);
165 return context.Pass();
166 }
167
168 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
169 return LimitedContext(kint64max);
170 }
171
[email protected]02a60542012-07-24 20:05:33172 FileSystemOperationContext* NewContext(
[email protected]1c98fdd2013-05-24 09:45:27173 SandboxFileSystemTestHelper* file_system) {
[email protected]c4e6f9c2012-09-09 17:42:10174 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16175 FileSystemOperationContext* context;
[email protected]1c98fdd2013-05-24 09:45:27176 if (file_system)
177 context = file_system->NewOperationContext();
[email protected]0c5ebe32011-08-19 22:37:16178 else
[email protected]1c98fdd2013-05-24 09:45:27179 context = sandbox_file_system_.NewOperationContext();
[email protected]b9566e7c2012-10-29 10:57:46180 // Setting allowed_bytes_growth big enough for all tests.
181 context->set_allowed_bytes_growth(1024 * 1024);
[email protected]c4e6f9c2012-09-09 17:42:10182 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32183 return context;
184 }
185
[email protected]c4e6f9c2012-09-09 17:42:10186 const ChangeObserverList& change_observers() const {
187 return change_observers_;
188 }
189
190 MockFileChangeObserver* change_observer() {
191 return &change_observer_;
192 }
193
[email protected]0c5ebe32011-08-19 22:37:16194 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49195 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16196 // Use this for tests which need to run in multiple origins; we need a test
197 // helper per origin.
[email protected]1c98fdd2013-05-24 09:45:27198 SandboxFileSystemTestHelper* NewFileSystem(
[email protected]0c5ebe32011-08-19 22:37:16199 const GURL& origin, fileapi::FileSystemType type) {
[email protected]1c98fdd2013-05-24 09:45:27200 SandboxFileSystemTestHelper* file_system =
201 new SandboxFileSystemTestHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16202
[email protected]1c98fdd2013-05-24 09:45:27203 file_system->SetUp(file_system_context_.get());
204 return file_system;
[email protected]0c5ebe32011-08-19 22:37:16205 }
206
[email protected]7878ece2011-09-05 11:41:49207 ObfuscatedFileUtil* ofu() {
[email protected]1c98fdd2013-05-24 09:45:27208 return static_cast<ObfuscatedFileUtil*>(sandbox_file_system_.file_util());
[email protected]d4905e2e2011-05-13 21:56:32209 }
210
[email protected]a3ef4832013-02-02 05:12:33211 const base::FilePath& test_directory() const {
[email protected]6b931152011-05-20 21:02:35212 return data_dir_.path();
213 }
214
[email protected]0c5ebe32011-08-19 22:37:16215 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26216 return origin_;
217 }
218
219 fileapi::FileSystemType type() const {
220 return type_;
221 }
222
[email protected]294dd0312012-05-11 07:35:13223 int64 ComputeTotalFileSize() {
[email protected]1c98fdd2013-05-24 09:45:27224 return sandbox_file_system_.ComputeCurrentOriginUsage() -
225 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]294dd0312012-05-11 07:35:13226 }
227
[email protected]0c5ebe32011-08-19 22:37:16228 void GetUsageFromQuotaManager() {
[email protected]07b64872013-02-13 11:46:30229 int64 quota = -1;
[email protected]ff875be52013-06-02 23:47:38230 quota_status_ =
231 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
232 origin(),
233 sandbox_file_system_.type(),
234 &usage_,
235 &quota);
[email protected]0c5ebe32011-08-19 22:37:16236 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
237 }
238
239 void RevokeUsageCache() {
[email protected]1c98fdd2013-05-24 09:45:27240 quota_manager_->ResetUsageTracker(sandbox_file_system_.storage_type());
241 usage_cache()->Delete(sandbox_file_system_.GetUsageCachePath());
[email protected]022d2702012-05-14 16:04:26242 }
243
244 int64 SizeByQuotaUtil() {
[email protected]1c98fdd2013-05-24 09:45:27245 return sandbox_file_system_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16246 }
247
248 int64 SizeInUsageFile() {
[email protected]4cc586b2013-05-07 12:43:32249 base::MessageLoop::current()->RunUntilIdle();
[email protected]bf97e8b2013-04-14 15:00:13250 int64 usage = 0;
[email protected]1c98fdd2013-05-24 09:45:27251 return usage_cache()->GetUsage(
252 sandbox_file_system_.GetUsageCachePath(), &usage) ? usage : -1;
[email protected]0c5ebe32011-08-19 22:37:16253 }
254
[email protected]13f92f6e2012-08-13 07:39:14255 bool PathExists(const FileSystemURL& url) {
256 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]4e7e3882013-01-17 04:14:21257 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33258 base::FilePath platform_path;
[email protected]4e7e3882013-01-17 04:14:21259 base::PlatformFileError error = ofu()->GetFileInfo(
260 context.get(), url, &file_info, &platform_path);
261 return error == base::PLATFORM_FILE_OK;
[email protected]13f92f6e2012-08-13 07:39:14262 }
263
264 bool DirectoryExists(const FileSystemURL& url) {
[email protected]07b64872013-02-13 11:46:30265 return AsyncFileTestHelper::DirectoryExists(file_system_context(), url);
[email protected]13f92f6e2012-08-13 07:39:14266 }
267
[email protected]0c5ebe32011-08-19 22:37:16268 int64 usage() const { return usage_; }
[email protected]06226172013-03-14 15:39:31269 FileSystemUsageCache* usage_cache() {
[email protected]1c98fdd2013-05-24 09:45:27270 return sandbox_file_system_.usage_cache();
[email protected]06226172013-03-14 15:39:31271 }
[email protected]0c5ebe32011-08-19 22:37:16272
[email protected]949f25a2012-06-27 01:53:09273 FileSystemURL CreateURLFromUTF8(const std::string& path) {
[email protected]1c98fdd2013-05-24 09:45:27274 return sandbox_file_system_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50275 }
276
[email protected]949f25a2012-06-27 01:53:09277 int64 PathCost(const FileSystemURL& url) {
278 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13279 }
280
[email protected]a3ef4832013-02-02 05:12:33281 FileSystemURL CreateURL(const base::FilePath& path) {
[email protected]1c98fdd2013-05-24 09:45:27282 return sandbox_file_system_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50283 }
284
[email protected]d4905e2e2011-05-13 21:56:32285 void CheckFileAndCloseHandle(
[email protected]403ada82013-01-08 07:51:39286 const FileSystemURL& url, base::PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16287 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33288 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49289 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09290 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32291
292 base::PlatformFileInfo file_info0;
[email protected]a3ef4832013-02-02 05:12:33293 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49294 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09295 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32296 EXPECT_EQ(data_path, local_path);
297 EXPECT_TRUE(FileExists(data_path));
298 EXPECT_EQ(0, GetSize(data_path));
299
300 const char data[] = "test data";
301 const int length = arraysize(data) - 1;
302
303 if (base::kInvalidPlatformFileValue == file_handle) {
304 bool created = true;
[email protected]403ada82013-01-08 07:51:39305 base::PlatformFileError error;
[email protected]d4905e2e2011-05-13 21:56:32306 file_handle = base::CreatePlatformFile(
307 data_path,
308 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
309 &created,
310 &error);
[email protected]81b7f662011-05-26 00:54:46311 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32312 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
313 EXPECT_FALSE(created);
314 }
315 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
316 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
317
318 base::PlatformFileInfo file_info1;
319 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16320 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49321 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09322 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32323 EXPECT_EQ(data_path, local_path);
324
325 EXPECT_FALSE(file_info0.is_directory);
326 EXPECT_FALSE(file_info1.is_directory);
327 EXPECT_FALSE(file_info0.is_symbolic_link);
328 EXPECT_FALSE(file_info1.is_symbolic_link);
329 EXPECT_EQ(0, file_info0.size);
330 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32331 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32332
[email protected]0c5ebe32011-08-19 22:37:16333 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49334 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09335 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32336 EXPECT_EQ(length * 2, GetSize(data_path));
337
[email protected]0c5ebe32011-08-19 22:37:16338 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49339 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09340 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16341 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32342 }
343
344 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09345 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33346 const std::set<base::FilePath::StringType>& files,
347 const std::set<base::FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32348 scoped_ptr<FileSystemOperationContext> context;
[email protected]a3ef4832013-02-02 05:12:33349 std::set<base::FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32350 for (iter = files.begin(); iter != files.end(); ++iter) {
351 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16352 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32353 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49354 ofu()->EnsureFileExists(
[email protected]04c899f2013-02-08 08:28:42355 context.get(), FileSystemURLAppend(root_url, *iter),
[email protected]d4905e2e2011-05-13 21:56:32356 &created));
357 ASSERT_FALSE(created);
358 }
359 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16360 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14361 EXPECT_TRUE(DirectoryExists(
[email protected]04c899f2013-02-08 08:28:42362 FileSystemURLAppend(root_url, *iter)));
[email protected]d4905e2e2011-05-13 21:56:32363 }
364 }
365
[email protected]294dd0312012-05-11 07:35:13366 class UsageVerifyHelper {
367 public:
368 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]1c98fdd2013-05-24 09:45:27369 SandboxFileSystemTestHelper* file_system,
[email protected]294dd0312012-05-11 07:35:13370 int64 expected_usage)
371 : context_(context.Pass()),
[email protected]1c98fdd2013-05-24 09:45:27372 sandbox_file_system_(file_system),
[email protected]294dd0312012-05-11 07:35:13373 expected_usage_(expected_usage) {}
374
375 ~UsageVerifyHelper() {
[email protected]4cc586b2013-05-07 12:43:32376 base::MessageLoop::current()->RunUntilIdle();
[email protected]294dd0312012-05-11 07:35:13377 Check();
378 }
379
380 FileSystemOperationContext* context() {
381 return context_.get();
382 }
383
384 private:
385 void Check() {
386 ASSERT_EQ(expected_usage_,
[email protected]1c98fdd2013-05-24 09:45:27387 sandbox_file_system_->GetCachedOriginUsage());
[email protected]294dd0312012-05-11 07:35:13388 }
389
390 scoped_ptr<FileSystemOperationContext> context_;
[email protected]1c98fdd2013-05-24 09:45:27391 SandboxFileSystemTestHelper* sandbox_file_system_;
[email protected]294dd0312012-05-11 07:35:13392 int64 expected_usage_;
393 };
394
395 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27396 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13397 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
398 LimitedContext(requested_growth),
[email protected]1c98fdd2013-05-24 09:45:27399 &sandbox_file_system_, usage + requested_growth));
[email protected]294dd0312012-05-11 07:35:13400 }
401
402 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
[email protected]1c98fdd2013-05-24 09:45:27403 int64 usage = sandbox_file_system_.GetCachedOriginUsage();
[email protected]294dd0312012-05-11 07:35:13404 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
[email protected]1c98fdd2013-05-24 09:45:27405 LimitedContext(requested_growth - 1), &sandbox_file_system_, usage));
[email protected]294dd0312012-05-11 07:35:13406 }
407
[email protected]d4905e2e2011-05-13 21:56:32408 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09409 const FileSystemURL& root_url,
[email protected]a3ef4832013-02-02 05:12:33410 std::set<base::FilePath::StringType>* files,
411 std::set<base::FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32412 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42413 std::vector<DirectoryEntry> entries;
[email protected]d4905e2e2011-05-13 21:56:32414 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30415 AsyncFileTestHelper::ReadDirectory(
416 file_system_context(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32417 EXPECT_EQ(0UL, entries.size());
418
419 files->clear();
[email protected]89ee4272011-05-16 18:45:17420 files->insert(FILE_PATH_LITERAL("first"));
421 files->insert(FILE_PATH_LITERAL("second"));
422 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32423 directories->clear();
[email protected]89ee4272011-05-16 18:45:17424 directories->insert(FILE_PATH_LITERAL("fourth"));
425 directories->insert(FILE_PATH_LITERAL("fifth"));
426 directories->insert(FILE_PATH_LITERAL("sixth"));
[email protected]a3ef4832013-02-02 05:12:33427 std::set<base::FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32428 for (iter = files->begin(); iter != files->end(); ++iter) {
429 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16430 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32431 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49432 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50433 context.get(),
[email protected]04c899f2013-02-08 08:28:42434 FileSystemURLAppend(root_url, *iter),
[email protected]08f8feb2012-02-26 11:53:50435 &created));
[email protected]d4905e2e2011-05-13 21:56:32436 ASSERT_TRUE(created);
437 }
438 for (iter = directories->begin(); iter != directories->end(); ++iter) {
439 bool exclusive = true;
440 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16441 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32442 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49443 ofu()->CreateDirectory(
[email protected]04c899f2013-02-08 08:28:42444 context.get(),
445 FileSystemURLAppend(root_url, *iter),
[email protected]949f25a2012-06-27 01:53:09446 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32447 }
[email protected]949f25a2012-06-27 01:53:09448 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32449 }
450
[email protected]949f25a2012-06-27 01:53:09451 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]a3ef4832013-02-02 05:12:33452 std::set<base::FilePath::StringType> files;
453 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09454 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32455
456 scoped_ptr<FileSystemOperationContext> context;
[email protected]c83118f2013-05-20 04:35:42457 std::vector<DirectoryEntry> entries;
[email protected]0c5ebe32011-08-19 22:37:16458 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32459 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:30460 AsyncFileTestHelper::ReadDirectory(
461 file_system_context(), root_url, &entries));
[email protected]c83118f2013-05-20 04:35:42462 std::vector<DirectoryEntry>::iterator entry_iter;
[email protected]d4905e2e2011-05-13 21:56:32463 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10464 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32465 for (entry_iter = entries.begin(); entry_iter != entries.end();
466 ++entry_iter) {
[email protected]c83118f2013-05-20 04:35:42467 const DirectoryEntry& entry = *entry_iter;
[email protected]06226172013-03-14 15:39:31468 std::set<base::FilePath::StringType>::iterator iter =
469 files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32470 if (iter != files.end()) {
471 EXPECT_FALSE(entry.is_directory);
472 files.erase(iter);
473 continue;
474 }
[email protected]89ee4272011-05-16 18:45:17475 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32476 EXPECT_FALSE(directories.end() == iter);
477 EXPECT_TRUE(entry.is_directory);
478 directories.erase(iter);
479 }
480 }
481
[email protected]949f25a2012-06-27 01:53:09482 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41483 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32484 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16485
[email protected]2517cfa2011-08-25 05:12:41486 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32487 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49488 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09489 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10490 // Currently we fire no change notifications for Touch.
491 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]a3ef4832013-02-02 05:12:33492 base::FilePath local_path;
[email protected]d4905e2e2011-05-13 21:56:32493 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16494 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49495 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09496 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32497 // We compare as time_t here to lower our resolution, to avoid false
498 // negatives caused by conversion to the local filesystem's native
499 // representation and back.
500 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
501
[email protected]0c5ebe32011-08-19 22:37:16502 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32503 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41504 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32505 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49506 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09507 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10508 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16509 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49510 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09511 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32512 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49513 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41514 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32515 }
516
[email protected]81b7f662011-05-26 00:54:46517 void TestCopyInForeignFileHelper(bool overwrite) {
[email protected]ea1a3f62012-11-16 20:34:23518 base::ScopedTempDir source_dir;
[email protected]81b7f662011-05-26 00:54:46519 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]a3ef4832013-02-02 05:12:33520 base::FilePath root_file_path = source_dir.path();
521 base::FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09522 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46523 int64 src_file_length = 87;
524
525 base::PlatformFileError error_code;
526 bool created = false;
527 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
528 base::PlatformFile file_handle =
529 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50530 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46531 EXPECT_TRUE(created);
532 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
533 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
534 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
535 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
536
537 scoped_ptr<FileSystemOperationContext> context;
538
539 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16540 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46541 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09542 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46543 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10544
545 // We must have observed one (and only one) create_file_count.
546 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
547 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46548 }
549
[email protected]0c5ebe32011-08-19 22:37:16550 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09551 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16552 if (!overwrite) {
553 // Verify that file creation requires sufficient quota for the path.
554 context.reset(NewContext(NULL));
555 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
556 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13557 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09558 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16559 }
560
561 context.reset(NewContext(NULL));
562 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46563 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13564 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09565 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16566
[email protected]13f92f6e2012-08-13 07:39:14567 EXPECT_TRUE(PathExists(dest_url));
568 EXPECT_FALSE(DirectoryExists(dest_url));
569
[email protected]0c5ebe32011-08-19 22:37:16570 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46571 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33572 base::FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49573 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09574 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50575 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46576 EXPECT_TRUE(FileExists(data_path));
577 EXPECT_EQ(src_file_length, GetSize(data_path));
578
579 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09580 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46581 }
582
[email protected]949f25a2012-06-27 01:53:09583 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03584 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
585 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09586 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
587 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03588 }
589
[email protected]949f25a2012-06-27 01:53:09590 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03591 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33592 base::FilePath data_path;
[email protected]fad625e2f2011-12-08 05:38:03593 base::PlatformFileInfo file_info;
594 context.reset(NewContext(NULL));
595 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09596 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10597 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03598 return file_info.last_modified;
599 }
600
[email protected]949f25a2012-06-27 01:53:09601 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03602 bool copy,
603 bool overwrite) {
604 scoped_ptr<FileSystemOperationContext> context;
[email protected]04c899f2013-02-08 08:28:42605 const FileSystemURL src_dir_url(
606 FileSystemURLAppendUTF8(base_dir, "foo_dir"));
607 const FileSystemURL dest_dir_url(
608 FileSystemURLAppendUTF8(base_dir, "bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03609
[email protected]04c899f2013-02-08 08:28:42610 const FileSystemURL src_file_url(
611 FileSystemURLAppendUTF8(src_dir_url, "hoge"));
612 const FileSystemURL dest_file_url(
613 FileSystemURLAppendUTF8(dest_dir_url, "fuga"));
[email protected]fad625e2f2011-12-08 05:38:03614
615 context.reset(NewContext(NULL));
616 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09617 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03618 context.reset(NewContext(NULL));
619 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09620 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03621
622 bool created = false;
623 context.reset(NewContext(NULL));
624 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09625 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03626 if (overwrite) {
627 context.reset(NewContext(NULL));
628 EXPECT_EQ(base::PLATFORM_FILE_OK,
629 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09630 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03631 }
632
[email protected]949f25a2012-06-27 01:53:09633 ClearTimestamp(src_dir_url);
634 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03635 context.reset(NewContext(NULL));
636 EXPECT_EQ(base::PLATFORM_FILE_OK,
637 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09638 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03639 copy));
[email protected]fad625e2f2011-12-08 05:38:03640 if (copy)
[email protected]949f25a2012-06-27 01:53:09641 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03642 else
[email protected]949f25a2012-06-27 01:53:09643 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
644 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03645 }
646
[email protected]ecdfd6c52012-04-11 13:35:44647 int64 ComputeCurrentUsage() {
[email protected]1c98fdd2013-05-24 09:45:27648 return sandbox_file_system_.ComputeCurrentOriginUsage() -
649 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
[email protected]02a60542012-07-24 20:05:33650 }
[email protected]45ea0fbbb2012-02-27 22:28:49651
[email protected]07b64872013-02-13 11:46:30652 FileSystemContext* file_system_context() {
[email protected]1c98fdd2013-05-24 09:45:27653 return sandbox_file_system_.file_system_context();
[email protected]07b64872013-02-13 11:46:30654 }
655
[email protected]d4905e2e2011-05-13 21:56:32656 private:
[email protected]ea1a3f62012-11-16 20:34:23657 base::ScopedTempDir data_dir_;
[email protected]4cc586b2013-05-07 12:43:32658 base::MessageLoop message_loop_;
[email protected]0c5ebe32011-08-19 22:37:16659 scoped_refptr<quota::QuotaManager> quota_manager_;
660 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26661 GURL origin_;
662 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03663 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]1c98fdd2013-05-24 09:45:27664 SandboxFileSystemTestHelper sandbox_file_system_;
[email protected]0c5ebe32011-08-19 22:37:16665 quota::QuotaStatusCode quota_status_;
666 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10667 MockFileChangeObserver change_observer_;
668 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32669
[email protected]7878ece2011-09-05 11:41:49670 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32671};
672
[email protected]7878ece2011-09-05 11:41:49673TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32674 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
675 bool created;
[email protected]949f25a2012-06-27 01:53:09676 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16677 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32678 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
679
680 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49681 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09682 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32683 &created));
684
[email protected]0c5ebe32011-08-19 22:37:16685 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32686 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09687 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32688
[email protected]949f25a2012-06-27 01:53:09689 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32690
[email protected]c4e6f9c2012-09-09 17:42:10691 EXPECT_TRUE(change_observer()->HasNoChange());
692
[email protected]0c5ebe32011-08-19 22:37:16693 // Verify that file creation requires sufficient quota for the path.
694 context.reset(NewContext(NULL));
695 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09696 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16697 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49698 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09699 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16700
701 context.reset(NewContext(NULL));
702 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09703 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32704 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49705 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09706 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32707 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10708 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32709 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
710
[email protected]949f25a2012-06-27 01:53:09711 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32712
[email protected]0c5ebe32011-08-19 22:37:16713 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33714 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49715 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09716 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32717 EXPECT_TRUE(file_util::PathExists(local_path));
718
[email protected]0c5ebe32011-08-19 22:37:16719 // Verify that deleting a file isn't stopped by zero quota, and that it frees
720 // up quote from its path.
721 context.reset(NewContext(NULL));
722 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32723 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09724 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10725 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32726 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09727 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16728 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32729
[email protected]0c5ebe32011-08-19 22:37:16730 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32731 bool exclusive = true;
732 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09733 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50734 "series/of/directories");
[email protected]04c899f2013-02-08 08:28:42735 url = FileSystemURLAppendUTF8(directory_url, "file name");
[email protected]7878ece2011-09-05 11:41:49736 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09737 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10738 // The oepration created 3 directories recursively.
739 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32740
[email protected]0c5ebe32011-08-19 22:37:16741 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32742 file_handle = base::kInvalidPlatformFileValue;
743 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49744 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09745 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32746 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10747 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32748 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
749
[email protected]949f25a2012-06-27 01:53:09750 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32751
[email protected]0c5ebe32011-08-19 22:37:16752 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49753 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09754 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32755 EXPECT_TRUE(file_util::PathExists(local_path));
756
[email protected]0c5ebe32011-08-19 22:37:16757 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32758 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09759 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10760 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32761 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10762
763 // Make sure we have no unexpected changes.
764 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32765}
766
[email protected]7878ece2011-09-05 11:41:49767TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32768 bool created = false;
[email protected]949f25a2012-06-27 01:53:09769 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16770 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32771
772 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09773 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32774
[email protected]0c5ebe32011-08-19 22:37:16775 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32776 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09777 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32778 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10779 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32780
[email protected]0c5ebe32011-08-19 22:37:16781 context.reset(NewContext(NULL));
[email protected]a3ef4832013-02-02 05:12:33782 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49783 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09784 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32785 EXPECT_EQ(0, GetSize(local_path));
786
[email protected]0c5ebe32011-08-19 22:37:16787 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49788 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09789 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10790 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32791 EXPECT_EQ(10, GetSize(local_path));
792
[email protected]0c5ebe32011-08-19 22:37:16793 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49794 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09795 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32796 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10797 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32798
[email protected]13f92f6e2012-08-13 07:39:14799 EXPECT_FALSE(DirectoryExists(url));
800 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10801
802 // Make sure we have no unexpected changes.
803 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32804}
805
[email protected]ecdfd6c52012-04-11 13:35:44806TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
807 bool created = false;
[email protected]949f25a2012-06-27 01:53:09808 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44809
810 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13811 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09812 AllowUsageIncrease(PathCost(url))->context(),
813 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44814 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13815 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44816
[email protected]ecdfd6c52012-04-11 13:35:44817 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13818 ofu()->Truncate(
819 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09820 url, 1020));
[email protected]294dd0312012-05-11 07:35:13821 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44822
[email protected]ecdfd6c52012-04-11 13:35:44823 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13824 ofu()->Truncate(
825 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09826 url, 0));
[email protected]294dd0312012-05-11 07:35:13827 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44828
[email protected]ecdfd6c52012-04-11 13:35:44829 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13830 ofu()->Truncate(
831 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09832 url, 1021));
[email protected]294dd0312012-05-11 07:35:13833 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44834
[email protected]ecdfd6c52012-04-11 13:35:44835 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13836 ofu()->Truncate(
837 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09838 url, 1020));
[email protected]294dd0312012-05-11 07:35:13839 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44840
[email protected]ecdfd6c52012-04-11 13:35:44841 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13842 ofu()->Truncate(
843 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09844 url, 1020));
[email protected]294dd0312012-05-11 07:35:13845 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44846
[email protected]294dd0312012-05-11 07:35:13847 // quota exceeded
848 {
849 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
850 helper->context()->set_allowed_bytes_growth(
851 helper->context()->allowed_bytes_growth() - 1);
852 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09853 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13854 ASSERT_EQ(1019, ComputeTotalFileSize());
855 }
[email protected]ecdfd6c52012-04-11 13:35:44856
857 // Delete backing file to make following truncation fail.
[email protected]a3ef4832013-02-02 05:12:33858 base::FilePath local_path;
[email protected]ecdfd6c52012-04-11 13:35:44859 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13860 ofu()->GetLocalFilePath(
861 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09862 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44863 ASSERT_FALSE(local_path.empty());
864 ASSERT_TRUE(file_util::Delete(local_path, false));
865
[email protected]ecdfd6c52012-04-11 13:35:44866 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13867 ofu()->Truncate(
868 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09869 url, 1234));
[email protected]294dd0312012-05-11 07:35:13870 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44871}
872
[email protected]7878ece2011-09-05 11:41:49873TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09874 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32875 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16876 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32877 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49878 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09879 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10880 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32881
[email protected]0c5ebe32011-08-19 22:37:16882 // Verify that file creation requires sufficient quota for the path.
883 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09884 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32885 created = false;
[email protected]0c5ebe32011-08-19 22:37:16886 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09887 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16888 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09889 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16890 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10891 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16892
893 context.reset(NewContext(NULL));
894 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09895 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32896 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09897 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32898 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10899 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32900
[email protected]949f25a2012-06-27 01:53:09901 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32902
[email protected]0c5ebe32011-08-19 22:37:16903 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32904 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09905 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32906 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10907 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32908
909 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09910 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16911 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32912 bool exclusive = true;
913 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49914 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09915 context.get(),
[email protected]04c899f2013-02-08 08:28:42916 FileSystemURLDirName(url),
[email protected]949f25a2012-06-27 01:53:09917 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10918 // 2 directories: path/ and path/to.
919 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32920
[email protected]0c5ebe32011-08-19 22:37:16921 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32922 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09923 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32924 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14925 EXPECT_FALSE(DirectoryExists(url));
926 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10927 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32928}
929
[email protected]7878ece2011-09-05 11:41:49930TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16931 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32932
933 bool exclusive = false;
934 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09935 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49936 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09937 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32938
[email protected]0c5ebe32011-08-19 22:37:16939 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32940 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]bab213be2013-01-23 15:13:08941 ofu()->DeleteDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32942
[email protected]007b3f82013-04-09 08:46:45943 FileSystemURL root = CreateURLFromUTF8(std::string());
[email protected]13f92f6e2012-08-13 07:39:14944 EXPECT_FALSE(DirectoryExists(url));
945 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16946 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49947 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32948
[email protected]0c5ebe32011-08-19 22:37:16949 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32950 exclusive = false;
951 recursive = true;
[email protected]7878ece2011-09-05 11:41:49952 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09953 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10954 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32955
[email protected]13f92f6e2012-08-13 07:39:14956 EXPECT_TRUE(DirectoryExists(url));
957 EXPECT_TRUE(PathExists(url));
958
[email protected]0c5ebe32011-08-19 22:37:16959 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49960 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]04c899f2013-02-08 08:28:42961 EXPECT_TRUE(DirectoryExists(FileSystemURLDirName(url)));
[email protected]13f92f6e2012-08-13 07:39:14962
[email protected]0c5ebe32011-08-19 22:37:16963 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09964 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
[email protected]04c899f2013-02-08 08:28:42965 FileSystemURLDirName(url)));
[email protected]d4905e2e2011-05-13 21:56:32966
967 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16968 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06969 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:08970 ofu()->DeleteDirectory(context.get(),
[email protected]04c899f2013-02-08 08:28:42971 FileSystemURLDirName(url)));
[email protected]c4e6f9c2012-09-09 17:42:10972 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32973
974 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:33975 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49976 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09977 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32978 EXPECT_TRUE(local_path.empty());
979 EXPECT_TRUE(file_info.is_directory);
980 EXPECT_FALSE(file_info.is_symbolic_link);
981
982 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16983 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49984 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09985 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10986 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32987
988 exclusive = true;
989 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16990 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49991 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09992 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10993 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32994
[email protected]0c5ebe32011-08-19 22:37:16995 // Verify that deleting a directory isn't stopped by zero quota, and that it
996 // frees up quota from its path.
997 context.reset(NewContext(NULL));
998 context->set_allowed_bytes_growth(0);
[email protected]bab213be2013-01-23 15:13:08999 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:101000 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:091001 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:161002 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321003
[email protected]949f25a2012-06-27 01:53:091004 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:321005
[email protected]13f92f6e2012-08-13 07:39:141006 EXPECT_FALSE(DirectoryExists(url));
1007 EXPECT_FALSE(PathExists(url));
1008
[email protected]0c5ebe32011-08-19 22:37:161009 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091010 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:491011 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091012 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321013
[email protected]0c5ebe32011-08-19 22:37:161014 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:321015 exclusive = true;
1016 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:161017 context.reset(NewContext(NULL));
1018 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091019 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491020 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091021 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101022 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161023
1024 context.reset(NewContext(NULL));
1025 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091026 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491027 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091028 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101029 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321030
[email protected]13f92f6e2012-08-13 07:39:141031 EXPECT_TRUE(DirectoryExists(url));
1032 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321033
1034 exclusive = true;
1035 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141036 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491037 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091038 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101039 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321040
1041 exclusive = true;
1042 recursive = false;
[email protected]949f25a2012-06-27 01:53:091043 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141044 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491045 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091046 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101047 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321048
[email protected]949f25a2012-06-27 01:53:091049 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321050
[email protected]13f92f6e2012-08-13 07:39:141051 EXPECT_FALSE(DirectoryExists(url));
1052 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321053
1054 exclusive = true;
1055 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141056 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491057 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091058 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101059 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321060
[email protected]13f92f6e2012-08-13 07:39:141061 EXPECT_TRUE(DirectoryExists(url));
1062 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321063
1064 exclusive = true;
1065 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141066 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491067 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091068 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101069 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321070}
1071
[email protected]7878ece2011-09-05 11:41:491072TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161073 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321074 bool exclusive = true;
1075 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091076 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491077 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091078 context.get(), url, exclusive, recursive));
1079 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321080}
1081
[email protected]7878ece2011-09-05 11:41:491082TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]007b3f82013-04-09 08:46:451083 TestReadDirectoryHelper(CreateURLFromUTF8(std::string()));
[email protected]d4905e2e2011-05-13 21:56:321084}
1085
[email protected]7878ece2011-09-05 11:41:491086TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091087 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321088}
1089
[email protected]7878ece2011-09-05 11:41:491090TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091091 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161092 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321093
1094 bool created = false;
1095 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091096 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321097 ASSERT_TRUE(created);
1098
[email protected]c83118f2013-05-20 04:35:421099 std::vector<DirectoryEntry> entries;
[email protected]1a647202013-01-21 15:32:251100 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY,
[email protected]07b64872013-02-13 11:46:301101 AsyncFileTestHelper::ReadDirectory(
1102 file_system_context(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321103
[email protected]949f25a2012-06-27 01:53:091104 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321105}
1106
[email protected]7878ece2011-09-05 11:41:491107TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091108 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161109 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411110
1111 base::Time last_access_time = base::Time::Now();
1112 base::Time last_modified_time = base::Time::Now();
1113
1114 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321115 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491116 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091117 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321118
[email protected]2517cfa2011-08-25 05:12:411119 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161120 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411121 bool created = false;
1122 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091123 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411124 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091125 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411126
1127 // Now test a directory:
1128 context.reset(NewContext(NULL));
1129 bool exclusive = true;
1130 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091131 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491132 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091133 url, exclusive, recursive));
1134 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161135}
1136
[email protected]7878ece2011-09-05 11:41:491137TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091138 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161139 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1140
[email protected]949f25a2012-06-27 01:53:091141 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161142 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411143 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161144 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091145 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411146 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161147 context->set_allowed_bytes_growth(1024);
1148 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091149 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411150 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091151 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161152 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1153
1154 context->set_allowed_bytes_growth(1024);
1155 bool exclusive = true;
1156 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091157 url = CreateURLFromUTF8("directory/to/use");
[email protected]a3ef4832013-02-02 05:12:331158 std::vector<base::FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091159 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161160 path_cost = 0;
[email protected]04c899f2013-02-08 08:28:421161 typedef std::vector<base::FilePath::StringType>::iterator iterator;
1162 for (iterator iter = components.begin();
1163 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491164 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]a3ef4832013-02-02 05:12:331165 base::FilePath(*iter));
[email protected]0c5ebe32011-08-19 22:37:161166 }
1167 context.reset(NewContext(NULL));
1168 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491169 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091170 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161171 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321172}
1173
[email protected]7878ece2011-09-05 11:41:491174TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091175 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1176 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161177 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321178
1179 bool is_copy_not_move = false;
1180 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091181 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321182 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101183 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161184 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321185 is_copy_not_move = true;
1186 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091187 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321188 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101189 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091190 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321191 bool exclusive = true;
1192 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161193 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491194 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091195 context.get(),
[email protected]04c899f2013-02-08 08:28:421196 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091197 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101198 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321199 is_copy_not_move = false;
1200 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091201 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321202 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101203 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161204 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321205 is_copy_not_move = true;
1206 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091207 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321208 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101209 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321210}
1211
[email protected]7878ece2011-09-05 11:41:491212TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321213 const int64 kSourceLength = 5;
1214 const int64 kDestLength = 50;
1215
1216 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1217 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1218 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1219 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1220 test_case.is_copy_not_move);
1221 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1222 test_case.source_path);
1223 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1224 test_case.dest_path);
1225 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1226 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161227 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321228
1229 bool exclusive = false;
1230 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091231 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1232 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321233
[email protected]0c5ebe32011-08-19 22:37:161234 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491235 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091236 context.get(),
[email protected]04c899f2013-02-08 08:28:421237 FileSystemURLDirName(source_url),
[email protected]949f25a2012-06-27 01:53:091238 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161239 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491240 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091241 context.get(),
[email protected]04c899f2013-02-08 08:28:421242 FileSystemURLDirName(dest_url),
[email protected]949f25a2012-06-27 01:53:091243 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321244
[email protected]d4905e2e2011-05-13 21:56:321245 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161246 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321247 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091248 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321249 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161250 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321251 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091252 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321253
1254 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161255 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321256 created = false;
1257 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091258 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321259 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161260 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321261 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091262 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321263 }
1264
[email protected]0c5ebe32011-08-19 22:37:161265 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491266 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091267 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101268
[email protected]d4905e2e2011-05-13 21:56:321269 if (test_case.is_copy_not_move) {
1270 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331271 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161272 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491273 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091274 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321275 EXPECT_EQ(kSourceLength, file_info.size);
1276 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091277 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321278 } else {
1279 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331280 base::FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161281 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491282 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091283 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321284 }
1285 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331286 base::FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491287 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091288 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321289 EXPECT_EQ(kSourceLength, file_info.size);
1290
1291 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091292 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321293 }
1294}
1295
[email protected]7878ece2011-09-05 11:41:491296TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091297 FileSystemURL src_url = CreateURLFromUTF8("src path");
1298 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161299 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1300 bool created = false;
[email protected]7878ece2011-09-05 11:41:491301 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091302 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161303
1304 bool is_copy = true;
1305 // Copy, no overwrite.
1306 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091307 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161308 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091309 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161310 context.reset(NewContext(NULL));
1311 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091312 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161313 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091314 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161315
1316 // Copy, with overwrite.
1317 context.reset(NewContext(NULL));
1318 context->set_allowed_bytes_growth(0);
1319 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091320 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161321}
1322
[email protected]7878ece2011-09-05 11:41:491323TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091324 FileSystemURL src_url = CreateURLFromUTF8("src path");
1325 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161326 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1327 bool created = false;
[email protected]7878ece2011-09-05 11:41:491328 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091329 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161330
1331 bool is_copy = false;
1332 // Move, rename, no overwrite.
1333 context.reset(NewContext(NULL));
1334 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091335 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1336 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161337 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091338 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161339 context.reset(NewContext(NULL));
1340 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091341 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1342 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161343 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091344 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161345
1346 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491347 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091348 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161349
1350 // Move, rename, with overwrite.
1351 context.reset(NewContext(NULL));
1352 context->set_allowed_bytes_growth(0);
1353 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091354 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161355}
1356
[email protected]7878ece2011-09-05 11:41:491357TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091358 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161359 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1360 bool created = false;
[email protected]7878ece2011-09-05 11:41:491361 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091362 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161363
1364 bool exclusive = true;
1365 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091366 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161367 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491368 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091369 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161370
[email protected]04c899f2013-02-08 08:28:421371 FileSystemURL dest_url = FileSystemURLAppend(
1372 dir_url, src_url.path().value());
[email protected]0c5ebe32011-08-19 22:37:161373
1374 bool is_copy = false;
1375 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1376 // Move, no rename, no overwrite.
1377 context.reset(NewContext(NULL));
1378 context->set_allowed_bytes_growth(allowed_bytes_growth);
1379 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091380 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161381 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1382
1383 // Move, no rename, with overwrite.
1384 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491385 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091386 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161387 context.reset(NewContext(NULL));
1388 context->set_allowed_bytes_growth(allowed_bytes_growth);
1389 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091390 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161391 EXPECT_EQ(
1392 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091393 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161394 context->allowed_bytes_growth());
1395}
1396
[email protected]7878ece2011-09-05 11:41:491397TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461398 TestCopyInForeignFileHelper(false /* overwrite */);
1399 TestCopyInForeignFileHelper(true /* overwrite */);
1400}
1401
[email protected]7878ece2011-09-05 11:41:491402TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161403 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091404 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321405 bool exclusive = true;
1406 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491407 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091408 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321409
[email protected]a3ef4832013-02-02 05:12:331410 std::set<base::FilePath::StringType> files;
1411 std::set<base::FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091412 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321413
[email protected]949f25a2012-06-27 01:53:091414 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321415
[email protected]13f92f6e2012-08-13 07:39:141416 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321417 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301418 AsyncFileTestHelper::Copy(
[email protected]1c98fdd2013-05-24 09:45:271419 file_system_context(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321420
[email protected]949f25a2012-06-27 01:53:091421 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141422 EXPECT_TRUE(DirectoryExists(src_url));
1423 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321424 recursive = true;
1425 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301426 AsyncFileTestHelper::Remove(
1427 file_system_context(), dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141428 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321429}
[email protected]6b931152011-05-20 21:02:351430
[email protected]7878ece2011-09-05 11:41:491431TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1432 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1433 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161434 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261435 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161436 EXPECT_EQ(origin(), enumerator->Next());
1437 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1438 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1439 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261440 EXPECT_EQ(GURL(), enumerator->Next());
1441 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1442 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1443
1444 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161445 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261446
1447 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1448 SCOPED_TRACE(testing::Message() <<
1449 "Validating kOriginEnumerationTestRecords " << i);
1450 const OriginEnumerationTestRecord& record =
1451 kOriginEnumerationTestRecords[i];
1452 GURL origin_url(record.origin_url);
1453 origins_expected.insert(origin_url);
1454 if (record.has_temporary) {
[email protected]1c98fdd2013-05-24 09:45:271455 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1456 NewFileSystem(origin_url, kFileSystemTypeTemporary));
1457 scoped_ptr<FileSystemOperationContext> context(
1458 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261459 bool created = false;
1460 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501461 ofu()->EnsureFileExists(
1462 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271463 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501464 &created));
[email protected]fcc2d5f2011-05-23 22:06:261465 EXPECT_TRUE(created);
1466 }
1467 if (record.has_persistent) {
[email protected]1c98fdd2013-05-24 09:45:271468 scoped_ptr<SandboxFileSystemTestHelper> file_system(
1469 NewFileSystem(origin_url, kFileSystemTypePersistent));
1470 scoped_ptr<FileSystemOperationContext> context(
1471 NewContext(file_system.get()));
[email protected]fcc2d5f2011-05-23 22:06:261472 bool created = false;
1473 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501474 ofu()->EnsureFileExists(
1475 context.get(),
[email protected]1c98fdd2013-05-24 09:45:271476 file_system->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501477 &created));
[email protected]fcc2d5f2011-05-23 22:06:261478 EXPECT_TRUE(created);
1479 }
1480 }
[email protected]7878ece2011-09-05 11:41:491481 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261482 EXPECT_TRUE(enumerator.get());
1483 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161484 GURL origin_url;
1485 while (!(origin_url = enumerator->Next()).is_empty()) {
1486 origins_found.insert(origin_url);
1487 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261488 bool found = false;
1489 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1490 ++i) {
1491 const OriginEnumerationTestRecord& record =
1492 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161493 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261494 continue;
1495 found = true;
1496 EXPECT_EQ(record.has_temporary,
1497 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1498 EXPECT_EQ(record.has_persistent,
1499 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1500 }
[email protected]0c5ebe32011-08-19 22:37:161501 // Deal with the default filesystem created by the test helper.
1502 if (!found && origin_url == origin()) {
1503 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1504 EXPECT_EQ(true,
1505 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181506 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161507 found = true;
1508 }
[email protected]fcc2d5f2011-05-23 22:06:261509 EXPECT_TRUE(found);
1510 }
1511
1512 std::set<GURL> diff;
1513 std::set_symmetric_difference(origins_expected.begin(),
1514 origins_expected.end(), origins_found.begin(), origins_found.end(),
1515 inserter(diff, diff.begin()));
1516 EXPECT_TRUE(diff.empty());
1517}
[email protected]0c5ebe32011-08-19 22:37:161518
[email protected]7878ece2011-09-05 11:41:491519TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161520 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1521
1522 int64 expected_quota = 0;
1523
[email protected]f83b5b72012-01-27 10:26:561524 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]9eaa331b2012-12-10 23:31:011525 SCOPED_TRACE(testing::Message() << "Creating kRegularTestCase " << i);
[email protected]f83b5b72012-01-27 10:26:561526 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]a3ef4832013-02-02 05:12:331527 base::FilePath file_path(test_case.path);
[email protected]08f8feb2012-02-26 11:53:501528 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161529 if (test_case.is_directory) {
1530 bool exclusive = true;
1531 bool recursive = false;
1532 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091533 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501534 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161535 } else {
1536 bool created = false;
1537 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091538 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501539 &created));
[email protected]0c5ebe32011-08-19 22:37:161540 ASSERT_TRUE(created);
1541 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501542 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091543 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501544 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161545 expected_quota += test_case.data_file_size;
1546 }
1547 }
[email protected]022d2702012-05-14 16:04:261548
1549 // Usually raw size in usage cache and the usage returned by QuotaUtil
1550 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161551 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261552 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1553
[email protected]0c5ebe32011-08-19 22:37:161554 RevokeUsageCache();
1555 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261556 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1557
1558 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161559 GetUsageFromQuotaManager();
1560 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261561 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161562 EXPECT_EQ(expected_quota, usage());
1563}
[email protected]34583332011-08-31 08:59:471564
[email protected]7878ece2011-09-05 11:41:491565TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091566 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1567 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471568
1569 scoped_ptr<FileSystemOperationContext> context;
1570 base::PlatformFile file;
1571 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331572 base::FilePath data_path;
[email protected]34583332011-08-31 08:59:471573 bool created = false;
1574
1575 // Create a non-empty file.
1576 context.reset(NewContext(NULL));
1577 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491578 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471579 EXPECT_TRUE(created);
1580 context.reset(NewContext(NULL));
1581 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491582 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471583 context.reset(NewContext(NULL));
1584 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491585 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471586 context.get(), kPath1, &file_info, &data_path));
1587 EXPECT_EQ(10, file_info.size);
1588
1589 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491590 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471591
1592 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141593 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471594 context.reset(NewContext(NULL));
1595 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491596 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471597 EXPECT_TRUE(created);
1598 context.reset(NewContext(NULL));
1599 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491600 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471601 context.get(), kPath1, &file_info, &data_path));
1602 EXPECT_EQ(0, file_info.size);
1603
1604 // Make another broken file to |kPath2|.
1605 context.reset(NewContext(NULL));
1606 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491607 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471608 EXPECT_TRUE(created);
1609
1610 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491611 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471612
1613 // Repair broken |kPath1|.
1614 context.reset(NewContext(NULL));
1615 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491616 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471617 base::Time::Now()));
1618 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491619 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471620 EXPECT_TRUE(created);
1621
1622 // Copy from sound |kPath1| to broken |kPath2|.
1623 context.reset(NewContext(NULL));
1624 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491625 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501626 true /* copy */));
[email protected]34583332011-08-31 08:59:471627
[email protected]7878ece2011-09-05 11:41:491628 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471629 context.reset(NewContext(NULL));
1630 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491631 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471632 context.get(), kPath1,
1633 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1634 &file, &created));
1635 EXPECT_TRUE(created);
1636 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1637 EXPECT_EQ(0, file_info.size);
1638 EXPECT_TRUE(base::ClosePlatformFile(file));
1639}
[email protected]9dfdc0e32011-09-02 06:07:441640
[email protected]7878ece2011-09-05 11:41:491641TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091642 const FileSystemURL kPath[] = {
1643 CreateURLFromUTF8("foo"),
1644 CreateURLFromUTF8("bar"),
1645 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441646 };
[email protected]a3ef4832013-02-02 05:12:331647 const FileSystemURL empty_path = CreateURL(base::FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441648 scoped_ptr<FileSystemOperationContext> context;
1649
1650 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1651 bool created = false;
1652 context.reset(NewContext(NULL));
1653 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491654 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441655 EXPECT_TRUE(created);
1656 }
1657
[email protected]c83118f2013-05-20 04:35:421658 std::vector<DirectoryEntry> entries;
[email protected]9dfdc0e32011-09-02 06:07:441659 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301660 AsyncFileTestHelper::ReadDirectory(
1661 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441662 EXPECT_EQ(3u, entries.size());
1663
[email protected]a3ef4832013-02-02 05:12:331664 base::FilePath local_path;
[email protected]9dfdc0e32011-09-02 06:07:441665 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491666 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441667 EXPECT_TRUE(file_util::Delete(local_path, false));
1668
[email protected]9dfdc0e32011-09-02 06:07:441669 entries.clear();
1670 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:301671 AsyncFileTestHelper::ReadDirectory(
1672 file_system_context(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441673 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1674}
[email protected]fad625e2f2011-12-08 05:38:031675
1676TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1677 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091678 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031679
1680 // Create working directory.
1681 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091682 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031683
1684 // EnsureFileExists, create case.
[email protected]04c899f2013-02-08 08:28:421685 FileSystemURL url(FileSystemURLAppendUTF8(
1686 dir_url, "EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031687 bool created = false;
[email protected]949f25a2012-06-27 01:53:091688 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031689 context.reset(NewContext(NULL));
1690 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091691 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031692 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091693 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031694
1695 // non create case.
1696 created = true;
[email protected]949f25a2012-06-27 01:53:091697 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031698 context.reset(NewContext(NULL));
1699 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091700 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031701 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091702 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031703
1704 // fail case.
[email protected]04c899f2013-02-08 08:28:421705 url = FileSystemURLAppendUTF8(dir_url, "EnsureFileExists_dir");
[email protected]fad625e2f2011-12-08 05:38:031706 context.reset(NewContext(NULL));
1707 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091708 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031709
[email protected]949f25a2012-06-27 01:53:091710 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031711 context.reset(NewContext(NULL));
1712 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091713 ofu()->EnsureFileExists(context.get(), url, &created));
1714 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031715
1716 // CreateOrOpen, create case.
[email protected]04c899f2013-02-08 08:28:421717 url = FileSystemURLAppendUTF8(dir_url, "CreateOrOpen_file");
[email protected]403ada82013-01-08 07:51:391718 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
[email protected]fad625e2f2011-12-08 05:38:031719 created = false;
[email protected]949f25a2012-06-27 01:53:091720 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031721 context.reset(NewContext(NULL));
1722 EXPECT_EQ(base::PLATFORM_FILE_OK,
1723 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091724 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031725 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1726 &file_handle, &created));
1727 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1728 EXPECT_TRUE(created);
1729 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091730 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031731
1732 // open case.
1733 file_handle = base::kInvalidPlatformFileValue;
1734 created = true;
[email protected]949f25a2012-06-27 01:53:091735 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031736 context.reset(NewContext(NULL));
1737 EXPECT_EQ(base::PLATFORM_FILE_OK,
1738 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091739 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031740 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1741 &file_handle, &created));
1742 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1743 EXPECT_FALSE(created);
1744 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091745 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031746
1747 // fail case
1748 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091749 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031750 context.reset(NewContext(NULL));
1751 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1752 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091753 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031754 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1755 &file_handle, &created));
1756 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091757 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031758
1759 // CreateDirectory, create case.
1760 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]04c899f2013-02-08 08:28:421761 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
1762 FileSystemURL subdir_url(FileSystemURLAppendUTF8(url, "subdir"));
[email protected]949f25a2012-06-27 01:53:091763 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031764 context.reset(NewContext(NULL));
1765 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091766 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031767 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091768 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031769
1770 // create subdir case.
1771 // Creating CreateDirectory_dir/subdir2.
[email protected]04c899f2013-02-08 08:28:421772 subdir_url = FileSystemURLAppendUTF8(url, "subdir2");
[email protected]949f25a2012-06-27 01:53:091773 ClearTimestamp(dir_url);
1774 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031775 context.reset(NewContext(NULL));
1776 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091777 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031778 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091779 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1780 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031781
1782 // fail case.
[email protected]04c899f2013-02-08 08:28:421783 url = FileSystemURLAppendUTF8(dir_url, "CreateDirectory_dir");
[email protected]949f25a2012-06-27 01:53:091784 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031785 context.reset(NewContext(NULL));
1786 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091787 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031788 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091789 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031790
1791 // CopyInForeignFile, create case.
[email protected]04c899f2013-02-08 08:28:421792 url = FileSystemURLAppendUTF8(dir_url, "CopyInForeignFile_file");
1793 FileSystemURL src_path = FileSystemURLAppendUTF8(
1794 dir_url, "CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031795 context.reset(NewContext(NULL));
1796 EXPECT_EQ(base::PLATFORM_FILE_OK,
1797 ofu()->EnsureFileExists(context.get(), src_path, &created));
1798 EXPECT_TRUE(created);
[email protected]a3ef4832013-02-02 05:12:331799 base::FilePath src_local_path;
[email protected]fad625e2f2011-12-08 05:38:031800 context.reset(NewContext(NULL));
1801 EXPECT_EQ(base::PLATFORM_FILE_OK,
1802 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1803
[email protected]949f25a2012-06-27 01:53:091804 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031805 context.reset(NewContext(NULL));
1806 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501807 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131808 src_local_path,
[email protected]949f25a2012-06-27 01:53:091809 url));
1810 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031811}
1812
1813TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1814 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091815 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031816
1817 // Create working directory.
1818 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091819 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031820
1821 // DeleteFile, delete case.
[email protected]04c899f2013-02-08 08:28:421822 FileSystemURL url = FileSystemURLAppendUTF8(
1823 dir_url, "DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031824 bool created = false;
1825 context.reset(NewContext(NULL));
1826 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091827 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031828 EXPECT_TRUE(created);
1829
[email protected]949f25a2012-06-27 01:53:091830 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031831 context.reset(NewContext(NULL));
1832 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091833 ofu()->DeleteFile(context.get(), url));
1834 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031835
1836 // fail case.
[email protected]949f25a2012-06-27 01:53:091837 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031838 context.reset(NewContext(NULL));
1839 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091840 ofu()->DeleteFile(context.get(), url));
1841 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031842
[email protected]bab213be2013-01-23 15:13:081843 // DeleteDirectory, fail case.
[email protected]04c899f2013-02-08 08:28:421844 url = FileSystemURLAppendUTF8(dir_url, "DeleteDirectory_dir");
1845 FileSystemURL file_path(FileSystemURLAppendUTF8(url, "pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031846 context.reset(NewContext(NULL));
1847 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091848 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031849 created = false;
1850 context.reset(NewContext(NULL));
1851 EXPECT_EQ(base::PLATFORM_FILE_OK,
1852 ofu()->EnsureFileExists(context.get(), file_path, &created));
1853 EXPECT_TRUE(created);
1854
[email protected]949f25a2012-06-27 01:53:091855 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031856 context.reset(NewContext(NULL));
1857 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]bab213be2013-01-23 15:13:081858 ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091859 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031860
1861 // delete case.
1862 context.reset(NewContext(NULL));
1863 EXPECT_EQ(base::PLATFORM_FILE_OK,
1864 ofu()->DeleteFile(context.get(), file_path));
1865
[email protected]949f25a2012-06-27 01:53:091866 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031867 context.reset(NewContext(NULL));
[email protected]bab213be2013-01-23 15:13:081868 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->DeleteDirectory(context.get(), url));
[email protected]949f25a2012-06-27 01:53:091869 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031870}
1871
1872TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1873 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091874 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031875 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091876 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031877 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091878 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031879 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091880 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031881}
[email protected]a3938912012-03-27 14:00:551882
1883TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091884 FileSystemURL dir = CreateURLFromUTF8("foo");
[email protected]04c899f2013-02-08 08:28:421885 FileSystemURL url1 = FileSystemURLAppendUTF8(dir, "bar");
1886 FileSystemURL url2 = FileSystemURLAppendUTF8(dir, "baz");
[email protected]a3938912012-03-27 14:00:551887
1888 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1889 EXPECT_EQ(base::PLATFORM_FILE_OK,
1890 ofu()->CreateDirectory(context.get(), dir, false, false));
1891
1892 bool created = false;
1893 context.reset(NewContext(NULL));
1894 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091895 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551896 EXPECT_TRUE(created);
1897
1898 context.reset(NewContext(NULL));
1899 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091900 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551901
[email protected]a3ef4832013-02-02 05:12:331902 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551903 context.reset(NewContext(NULL));
1904 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091905 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551906 EXPECT_FALSE(file_path.empty());
1907
1908 context.reset(NewContext(NULL));
1909 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091910 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551911 base::Time::Now() + base::TimeDelta::FromHours(1),
1912 base::Time()));
1913
1914 context.reset(NewContext(NULL));
1915 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1916 ofu()->CreateFileEnumerator(context.get(), dir, false));
1917
1918 int count = 0;
[email protected]a3ef4832013-02-02 05:12:331919 base::FilePath file_path_each;
[email protected]a3938912012-03-27 14:00:551920 while (!(file_path_each = file_enum->Next()).empty()) {
1921 context.reset(NewContext(NULL));
1922 base::PlatformFileInfo file_info;
[email protected]a3ef4832013-02-02 05:12:331923 base::FilePath file_path;
[email protected]a3938912012-03-27 14:00:551924 EXPECT_EQ(base::PLATFORM_FILE_OK,
1925 ofu()->GetFileInfo(context.get(),
[email protected]04c899f2013-02-08 08:28:421926 FileSystemURL::CreateForTest(
1927 dir.origin(),
1928 dir.mount_type(),
1929 file_path_each),
[email protected]a3938912012-03-27 14:00:551930 &file_info, &file_path));
1931 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1932 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1933 EXPECT_EQ(file_info.size, file_enum->Size());
1934 ++count;
1935 }
1936 EXPECT_EQ(2, count);
1937}
[email protected]294dd0312012-05-11 07:35:131938
[email protected]58ac5272013-02-15 10:05:031939// crbug.com/176470
1940#if defined(OS_WIN) || defined(OS_ANDROID)
1941#define MAYBE_TestQuotaOnCopyFile DISABLED_TestQuotaOnCopyFile
1942#else
1943#define MAYBE_TestQuotaOnCopyFile TestQuotaOnCopyFile
1944#endif
1945TEST_F(ObfuscatedFileUtilTest, MAYBE_TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091946 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1947 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1948 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1949 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131950 bool created;
1951
1952 int64 expected_total_file_size = 0;
1953 ASSERT_EQ(base::PLATFORM_FILE_OK,
1954 ofu()->EnsureFileExists(
1955 AllowUsageIncrease(PathCost(from_file))->context(),
1956 from_file, &created));
1957 ASSERT_TRUE(created);
1958 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1959
1960 ASSERT_EQ(base::PLATFORM_FILE_OK,
1961 ofu()->EnsureFileExists(
1962 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1963 obstacle_file, &created));
1964 ASSERT_TRUE(created);
1965 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1966
1967 int64 from_file_size = 1020;
1968 expected_total_file_size += from_file_size;
1969 ASSERT_EQ(base::PLATFORM_FILE_OK,
1970 ofu()->Truncate(
1971 AllowUsageIncrease(from_file_size)->context(),
1972 from_file, from_file_size));
1973 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1974
1975 int64 obstacle_file_size = 1;
1976 expected_total_file_size += obstacle_file_size;
1977 ASSERT_EQ(base::PLATFORM_FILE_OK,
1978 ofu()->Truncate(
1979 AllowUsageIncrease(obstacle_file_size)->context(),
1980 obstacle_file, obstacle_file_size));
1981 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1982
1983 int64 to_file1_size = from_file_size;
1984 expected_total_file_size += to_file1_size;
1985 ASSERT_EQ(base::PLATFORM_FILE_OK,
1986 ofu()->CopyOrMoveFile(
1987 AllowUsageIncrease(
1988 PathCost(to_file1) + to_file1_size)->context(),
1989 from_file, to_file1, true /* copy */));
1990 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1991
1992 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1993 ofu()->CopyOrMoveFile(
1994 DisallowUsageIncrease(
1995 PathCost(to_file2) + from_file_size)->context(),
1996 from_file, to_file2, true /* copy */));
1997 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1998
1999 int64 old_obstacle_file_size = obstacle_file_size;
2000 obstacle_file_size = from_file_size;
2001 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2002 ASSERT_EQ(base::PLATFORM_FILE_OK,
2003 ofu()->CopyOrMoveFile(
2004 AllowUsageIncrease(
2005 obstacle_file_size - old_obstacle_file_size)->context(),
2006 from_file, obstacle_file, true /* copy */));
2007 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2008
2009 int64 old_from_file_size = from_file_size;
2010 from_file_size = old_from_file_size - 1;
2011 expected_total_file_size += from_file_size - old_from_file_size;
2012 ASSERT_EQ(base::PLATFORM_FILE_OK,
2013 ofu()->Truncate(
2014 AllowUsageIncrease(
2015 from_file_size - old_from_file_size)->context(),
2016 from_file, from_file_size));
2017 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2018
2019 // quota exceeded
2020 {
2021 old_obstacle_file_size = obstacle_file_size;
2022 obstacle_file_size = from_file_size;
2023 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2024 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2025 obstacle_file_size - old_obstacle_file_size);
2026 helper->context()->set_allowed_bytes_growth(
2027 helper->context()->allowed_bytes_growth() - 1);
2028 ASSERT_EQ(base::PLATFORM_FILE_OK,
2029 ofu()->CopyOrMoveFile(
2030 helper->context(),
2031 from_file, obstacle_file, true /* copy */));
2032 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2033 }
2034}
2035
2036TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092037 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2038 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2039 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132040 bool created;
2041
2042 int64 expected_total_file_size = 0;
2043 ASSERT_EQ(base::PLATFORM_FILE_OK,
2044 ofu()->EnsureFileExists(
2045 AllowUsageIncrease(PathCost(from_file))->context(),
2046 from_file, &created));
2047 ASSERT_TRUE(created);
2048 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2049
2050 int64 from_file_size = 1020;
2051 expected_total_file_size += from_file_size;
2052 ASSERT_EQ(base::PLATFORM_FILE_OK,
2053 ofu()->Truncate(
2054 AllowUsageIncrease(from_file_size)->context(),
2055 from_file, from_file_size));
2056 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2057
2058 int64 to_file_size ALLOW_UNUSED = from_file_size;
2059 from_file_size = 0;
2060 ASSERT_EQ(base::PLATFORM_FILE_OK,
2061 ofu()->CopyOrMoveFile(
2062 AllowUsageIncrease(-PathCost(from_file) +
2063 PathCost(to_file))->context(),
2064 from_file, to_file, false /* move */));
2065 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2066
2067 ASSERT_EQ(base::PLATFORM_FILE_OK,
2068 ofu()->EnsureFileExists(
2069 AllowUsageIncrease(PathCost(from_file))->context(),
2070 from_file, &created));
2071 ASSERT_TRUE(created);
2072 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2073
2074 ASSERT_EQ(base::PLATFORM_FILE_OK,
2075 ofu()->EnsureFileExists(
2076 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2077 obstacle_file, &created));
2078 ASSERT_TRUE(created);
2079 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2080
2081 from_file_size = 1020;
2082 expected_total_file_size += from_file_size;
2083 ASSERT_EQ(base::PLATFORM_FILE_OK,
2084 ofu()->Truncate(
2085 AllowUsageIncrease(from_file_size)->context(),
2086 from_file, from_file_size));
2087 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2088
2089 int64 obstacle_file_size = 1;
2090 expected_total_file_size += obstacle_file_size;
2091 ASSERT_EQ(base::PLATFORM_FILE_OK,
2092 ofu()->Truncate(
2093 AllowUsageIncrease(1)->context(),
2094 obstacle_file, obstacle_file_size));
2095 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2096
2097 int64 old_obstacle_file_size = obstacle_file_size;
2098 obstacle_file_size = from_file_size;
2099 from_file_size = 0;
2100 expected_total_file_size -= old_obstacle_file_size;
2101 ASSERT_EQ(base::PLATFORM_FILE_OK,
2102 ofu()->CopyOrMoveFile(
2103 AllowUsageIncrease(
2104 -old_obstacle_file_size - PathCost(from_file))->context(),
2105 from_file, obstacle_file,
2106 false /* move */));
2107 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2108
2109 ASSERT_EQ(base::PLATFORM_FILE_OK,
2110 ofu()->EnsureFileExists(
2111 AllowUsageIncrease(PathCost(from_file))->context(),
2112 from_file, &created));
2113 ASSERT_TRUE(created);
2114 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2115
2116 from_file_size = 10;
2117 expected_total_file_size += from_file_size;
2118 ASSERT_EQ(base::PLATFORM_FILE_OK,
2119 ofu()->Truncate(
2120 AllowUsageIncrease(from_file_size)->context(),
2121 from_file, from_file_size));
2122 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2123
2124 // quota exceeded even after operation
2125 old_obstacle_file_size = obstacle_file_size;
2126 obstacle_file_size = from_file_size;
2127 from_file_size = 0;
2128 expected_total_file_size -= old_obstacle_file_size;
2129 scoped_ptr<FileSystemOperationContext> context =
2130 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2131 ASSERT_EQ(base::PLATFORM_FILE_OK,
2132 ofu()->CopyOrMoveFile(
2133 context.get(), from_file, obstacle_file, false /* move */));
2134 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2135 context.reset();
2136}
2137
2138TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092139 FileSystemURL dir(CreateURLFromUTF8("dir"));
2140 FileSystemURL file(CreateURLFromUTF8("file"));
2141 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2142 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132143 bool created;
2144
2145 ASSERT_EQ(base::PLATFORM_FILE_OK,
2146 ofu()->EnsureFileExists(
2147 AllowUsageIncrease(PathCost(file))->context(),
2148 file, &created));
2149 ASSERT_TRUE(created);
2150 ASSERT_EQ(0, ComputeTotalFileSize());
2151
2152 ASSERT_EQ(base::PLATFORM_FILE_OK,
2153 ofu()->CreateDirectory(
2154 AllowUsageIncrease(PathCost(dir))->context(),
2155 dir, false, false));
2156 ASSERT_EQ(0, ComputeTotalFileSize());
2157
2158 ASSERT_EQ(base::PLATFORM_FILE_OK,
2159 ofu()->EnsureFileExists(
2160 AllowUsageIncrease(PathCost(dfile1))->context(),
2161 dfile1, &created));
2162 ASSERT_TRUE(created);
2163 ASSERT_EQ(0, ComputeTotalFileSize());
2164
2165 ASSERT_EQ(base::PLATFORM_FILE_OK,
2166 ofu()->EnsureFileExists(
2167 AllowUsageIncrease(PathCost(dfile2))->context(),
2168 dfile2, &created));
2169 ASSERT_TRUE(created);
2170 ASSERT_EQ(0, ComputeTotalFileSize());
2171
2172 ASSERT_EQ(base::PLATFORM_FILE_OK,
2173 ofu()->Truncate(
2174 AllowUsageIncrease(340)->context(),
2175 file, 340));
2176 ASSERT_EQ(340, ComputeTotalFileSize());
2177
2178 ASSERT_EQ(base::PLATFORM_FILE_OK,
2179 ofu()->Truncate(
2180 AllowUsageIncrease(1020)->context(),
2181 dfile1, 1020));
2182 ASSERT_EQ(1360, ComputeTotalFileSize());
2183
2184 ASSERT_EQ(base::PLATFORM_FILE_OK,
2185 ofu()->Truncate(
2186 AllowUsageIncrease(120)->context(),
2187 dfile2, 120));
2188 ASSERT_EQ(1480, ComputeTotalFileSize());
2189
2190 ASSERT_EQ(base::PLATFORM_FILE_OK,
2191 ofu()->DeleteFile(
2192 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2193 file));
2194 ASSERT_EQ(1140, ComputeTotalFileSize());
2195
2196 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]07b64872013-02-13 11:46:302197 AsyncFileTestHelper::Remove(
2198 file_system_context(), dir, true /* recursive */));
[email protected]294dd0312012-05-11 07:35:132199 ASSERT_EQ(0, ComputeTotalFileSize());
2200}
[email protected]7d78be12012-05-24 07:07:262201
2202TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092203 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262204 base::PlatformFile file_handle;
2205 bool created;
2206
2207 // Creating a file.
2208 ASSERT_EQ(base::PLATFORM_FILE_OK,
2209 ofu()->EnsureFileExists(
2210 AllowUsageIncrease(PathCost(file))->context(),
2211 file, &created));
2212 ASSERT_TRUE(created);
2213 ASSERT_EQ(0, ComputeTotalFileSize());
2214
2215 // Opening it, which shouldn't change the usage.
2216 ASSERT_EQ(base::PLATFORM_FILE_OK,
2217 ofu()->CreateOrOpen(
2218 AllowUsageIncrease(0)->context(), file,
2219 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2220 &file_handle, &created));
2221 ASSERT_EQ(0, ComputeTotalFileSize());
2222 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2223
2224 const int length = 33;
2225 ASSERT_EQ(base::PLATFORM_FILE_OK,
2226 ofu()->Truncate(
2227 AllowUsageIncrease(length)->context(), file, length));
2228 ASSERT_EQ(length, ComputeTotalFileSize());
2229
2230 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2231 ASSERT_EQ(base::PLATFORM_FILE_OK,
2232 ofu()->CreateOrOpen(
2233 AllowUsageIncrease(-length)->context(), file,
2234 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2235 &file_handle, &created));
2236 ASSERT_EQ(0, ComputeTotalFileSize());
2237 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2238
2239 // Extending the file again.
2240 ASSERT_EQ(base::PLATFORM_FILE_OK,
2241 ofu()->Truncate(
2242 AllowUsageIncrease(length)->context(), file, length));
2243 ASSERT_EQ(length, ComputeTotalFileSize());
2244
2245 // Opening it with TRUNCATED flag, which should truncate the file size.
2246 ASSERT_EQ(base::PLATFORM_FILE_OK,
2247 ofu()->CreateOrOpen(
2248 AllowUsageIncrease(-length)->context(), file,
2249 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2250 &file_handle, &created));
2251 ASSERT_EQ(0, ComputeTotalFileSize());
2252 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2253}
[email protected]c4e6f9c2012-09-09 17:42:102254
[email protected]17258682013-06-05 13:38:402255TEST_F(ObfuscatedFileUtilTest, MaybeDropDatabasesAliveCase) {
2256 base::ScopedTempDir data_dir;
2257 ASSERT_TRUE(data_dir.CreateUniqueTempDir());
2258 ObfuscatedFileUtil file_util(NULL,
2259 data_dir.path(),
2260 base::MessageLoopProxy::current());
2261 file_util.InitOriginDatabase(true /*create*/);
2262 ASSERT_TRUE(file_util.origin_database_ != NULL);
2263
2264 // Callback to Drop DB is called while ObfuscatedFileUtilTest is still alive.
2265 file_util.db_flush_delay_seconds_ = 0;
2266 file_util.MarkUsed();
2267 base::MessageLoop::current()->RunUntilIdle();
2268
2269 ASSERT_TRUE(file_util.origin_database_ == NULL);
2270}
2271
2272TEST_F(ObfuscatedFileUtilTest, MaybeDropDatabasesAlreadyDeletedCase) {
2273 // Run message loop after OFU is already deleted to make sure callback doesn't
2274 // cause a crash for use after free.
2275 {
2276 base::ScopedTempDir data_dir;
2277 ASSERT_TRUE(data_dir.CreateUniqueTempDir());
2278 ObfuscatedFileUtil file_util(NULL,
2279 data_dir.path(),
2280 base::MessageLoopProxy::current());
2281 file_util.InitOriginDatabase(true /*create*/);
2282 file_util.db_flush_delay_seconds_ = 0;
2283 file_util.MarkUsed();
2284 }
2285
2286 // At this point the callback is still in the message queue but OFU is gone.
2287 base::MessageLoop::current()->RunUntilIdle();
2288}
2289
[email protected]c4e6f9c2012-09-09 17:42:102290} // namespace fileapi