blob: a7f9c4b3f12ac068eee106a6a4a886fbd93ac6ee [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d4905e2e2011-05-13 21:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fcc2d5f2011-05-23 22:06:265#include <algorithm>
[email protected]d4905e2e2011-05-13 21:56:326#include <set>
7#include <string>
8
[email protected]4d99be52011-10-18 14:11:039#include "base/bind.h"
[email protected]d4905e2e2011-05-13 21:56:3210#include "base/file_path.h"
11#include "base/file_util.h"
12#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1613#include "base/message_loop.h"
[email protected]d4905e2e2011-05-13 21:56:3214#include "base/platform_file.h"
[email protected]e0785902011-05-19 23:34:1715#include "base/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3216#include "base/sys_string_conversions.h"
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webkit/fileapi/file_system_context.h"
19#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1020#include "webkit/fileapi/file_system_task_runners.h"
[email protected]0c5ebe32011-08-19 22:37:1621#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]aa437fa2012-03-03 02:09:0722#include "webkit/fileapi/file_util_helper.h"
[email protected]02a60542012-07-24 20:05:3323#include "webkit/fileapi/local_file_system_test_helper.h"
[email protected]c4e6f9c2012-09-09 17:42:1024#include "webkit/fileapi/mock_file_change_observer.h"
[email protected]e7e46732012-01-05 11:45:5525#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4926#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5627#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1628#include "webkit/quota/mock_special_storage_policy.h"
29#include "webkit/quota/quota_manager.h"
30#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3231
[email protected]c4e6f9c2012-09-09 17:42:1032namespace fileapi {
[email protected]d4905e2e2011-05-13 21:56:3233
34namespace {
35
[email protected]d4905e2e2011-05-13 21:56:3236bool FileExists(const FilePath& path) {
37 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
38}
39
[email protected]81b7f662011-05-26 00:54:4640int64 GetSize(const FilePath& path) {
41 int64 size;
42 EXPECT_TRUE(file_util::GetFileSize(path, &size));
43 return size;
44}
45
[email protected]d4905e2e2011-05-13 21:56:3246// After a move, the dest exists and the source doesn't.
47// After a copy, both source and dest exist.
48struct CopyMoveTestCaseRecord {
49 bool is_copy_not_move;
50 const char source_path[64];
51 const char dest_path[64];
52 bool cause_overwrite;
53};
54
55const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
56 // This is the combinatoric set of:
57 // rename vs. same-name
58 // different directory vs. same directory
59 // overwrite vs. no-overwrite
60 // copy vs. move
61 // We can never be called with source and destination paths identical, so
62 // those cases are omitted.
63 {true, "dir0/file0", "dir0/file1", false},
64 {false, "dir0/file0", "dir0/file1", false},
65 {true, "dir0/file0", "dir0/file1", true},
66 {false, "dir0/file0", "dir0/file1", true},
67
68 {true, "dir0/file0", "dir1/file0", false},
69 {false, "dir0/file0", "dir1/file0", false},
70 {true, "dir0/file0", "dir1/file0", true},
71 {false, "dir0/file0", "dir1/file0", true},
72 {true, "dir0/file0", "dir1/file1", false},
73 {false, "dir0/file0", "dir1/file1", false},
74 {true, "dir0/file0", "dir1/file1", true},
75 {false, "dir0/file0", "dir1/file1", true},
76};
77
[email protected]fcc2d5f2011-05-23 22:06:2678struct OriginEnumerationTestRecord {
79 std::string origin_url;
80 bool has_temporary;
81 bool has_persistent;
82};
83
84const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
85 {"http://example.com", false, true},
86 {"http://example1.com", true, false},
87 {"https://example1.com", true, true},
88 {"file://", false, true},
89 {"http://example.com:8000", false, true},
90};
91
[email protected]d4905e2e2011-05-13 21:56:3292} // namespace (anonymous)
93
94// TODO(ericu): The vast majority of this and the other FSFU subclass tests
95// could theoretically be shared. It would basically be a FSFU interface
96// compliance test, and only the subclass-specific bits that look into the
97// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:4998class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:3299 public:
[email protected]7878ece2011-09-05 11:41:49100 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26101 : origin_(GURL("http://www.example.com")),
102 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03103 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16104 test_helper_(origin_, type_),
105 quota_status_(quota::kQuotaStatusUnknown),
106 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32107 }
108
109 void SetUp() {
110 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
111
[email protected]e7e46732012-01-05 11:45:55112 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
113 new quota::MockSpecialStoragePolicy();
114
[email protected]0c5ebe32011-08-19 22:37:16115 quota_manager_ = new quota::QuotaManager(
116 false /* is_incognito */,
117 data_dir_.path(),
118 base::MessageLoopProxy::current(),
119 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55120 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16121
122 // Every time we create a new helper, it creates another context, which
123 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49124 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16125 file_system_context_ = new FileSystemContext(
[email protected]dc57ec82012-08-07 03:50:10126 FileSystemTaskRunners::CreateMockTaskRunners(),
[email protected]e7e46732012-01-05 11:45:55127 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16128 quota_manager_->proxy(),
129 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55130 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16131
[email protected]7878ece2011-09-05 11:41:49132 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
[email protected]e7e46732012-01-05 11:45:55133 file_system_context_->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16134
135 test_helper_.SetUp(file_system_context_.get(),
[email protected]3cfc10f2012-05-24 01:20:41136 obfuscated_file_util_);
[email protected]c4e6f9c2012-09-09 17:42:10137
138 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
[email protected]d4905e2e2011-05-13 21:56:32139 }
140
[email protected]e7e46732012-01-05 11:45:55141 void TearDown() {
142 quota_manager_ = NULL;
143 test_helper_.TearDown();
144 }
145
[email protected]294dd0312012-05-11 07:35:13146 scoped_ptr<FileSystemOperationContext> LimitedContext(
147 int64 allowed_bytes_growth) {
148 scoped_ptr<FileSystemOperationContext> context(
149 test_helper_.NewOperationContext());
150 context->set_allowed_bytes_growth(allowed_bytes_growth);
151 return context.Pass();
152 }
153
154 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
155 return LimitedContext(kint64max);
156 }
157
[email protected]02a60542012-07-24 20:05:33158 FileSystemOperationContext* NewContext(
159 LocalFileSystemTestOriginHelper* helper) {
[email protected]c4e6f9c2012-09-09 17:42:10160 change_observer()->ResetCount();
[email protected]0c5ebe32011-08-19 22:37:16161 FileSystemOperationContext* context;
162 if (helper)
163 context = helper->NewOperationContext();
164 else
165 context = test_helper_.NewOperationContext();
166 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]c4e6f9c2012-09-09 17:42:10167 context->set_change_observers(change_observers());
[email protected]d4905e2e2011-05-13 21:56:32168 return context;
169 }
170
[email protected]c4e6f9c2012-09-09 17:42:10171 const ChangeObserverList& change_observers() const {
172 return change_observers_;
173 }
174
175 MockFileChangeObserver* change_observer() {
176 return &change_observer_;
177 }
178
[email protected]0c5ebe32011-08-19 22:37:16179 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49180 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16181 // Use this for tests which need to run in multiple origins; we need a test
182 // helper per origin.
[email protected]02a60542012-07-24 20:05:33183 LocalFileSystemTestOriginHelper* NewHelper(
[email protected]0c5ebe32011-08-19 22:37:16184 const GURL& origin, fileapi::FileSystemType type) {
[email protected]02a60542012-07-24 20:05:33185 LocalFileSystemTestOriginHelper* helper =
186 new LocalFileSystemTestOriginHelper(origin, type);
[email protected]0c5ebe32011-08-19 22:37:16187
188 helper->SetUp(file_system_context_.get(),
[email protected]3cfc10f2012-05-24 01:20:41189 obfuscated_file_util_);
[email protected]0c5ebe32011-08-19 22:37:16190 return helper;
191 }
192
[email protected]7878ece2011-09-05 11:41:49193 ObfuscatedFileUtil* ofu() {
[email protected]3cfc10f2012-05-24 01:20:41194 return obfuscated_file_util_;
[email protected]d4905e2e2011-05-13 21:56:32195 }
196
[email protected]6b931152011-05-20 21:02:35197 const FilePath& test_directory() const {
198 return data_dir_.path();
199 }
200
[email protected]0c5ebe32011-08-19 22:37:16201 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26202 return origin_;
203 }
204
205 fileapi::FileSystemType type() const {
206 return type_;
207 }
208
[email protected]294dd0312012-05-11 07:35:13209 int64 ComputeTotalFileSize() {
210 return test_helper_.ComputeCurrentOriginUsage() -
211 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
212 }
213
[email protected]0c5ebe32011-08-19 22:37:16214 void GetUsageFromQuotaManager() {
215 quota_manager_->GetUsageAndQuota(
216 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03217 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
218 weak_factory_.GetWeakPtr()));
[email protected]0c5ebe32011-08-19 22:37:16219 MessageLoop::current()->RunAllPending();
220 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
221 }
222
223 void RevokeUsageCache() {
224 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]022d2702012-05-14 16:04:26225 file_util::Delete(test_helper_.GetUsageCachePath(), false);
226 }
227
228 int64 SizeByQuotaUtil() {
229 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16230 }
231
232 int64 SizeInUsageFile() {
[email protected]022d2702012-05-14 16:04:26233 return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath());
[email protected]0c5ebe32011-08-19 22:37:16234 }
235
[email protected]13f92f6e2012-08-13 07:39:14236 bool PathExists(const FileSystemURL& url) {
237 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
238 return FileUtilHelper::PathExists(context.get(), ofu(), url);
239 }
240
241 bool DirectoryExists(const FileSystemURL& url) {
242 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
243 return FileUtilHelper::DirectoryExists(context.get(), ofu(), url);
244 }
245
[email protected]0c5ebe32011-08-19 22:37:16246 int64 usage() const { return usage_; }
247
[email protected]949f25a2012-06-27 01:53:09248 FileSystemURL CreateURLFromUTF8(const std::string& path) {
249 return test_helper_.CreateURLFromUTF8(path);
[email protected]08f8feb2012-02-26 11:53:50250 }
251
[email protected]949f25a2012-06-27 01:53:09252 int64 PathCost(const FileSystemURL& url) {
253 return ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]294dd0312012-05-11 07:35:13254 }
255
[email protected]949f25a2012-06-27 01:53:09256 FileSystemURL CreateURL(const FilePath& path) {
257 return test_helper_.CreateURL(path);
[email protected]08f8feb2012-02-26 11:53:50258 }
259
[email protected]0c5ebe32011-08-19 22:37:16260 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
261 EXPECT_EQ(quota::kQuotaStatusOk, status);
262 quota_status_ = status;
263 usage_ = usage;
264 }
265
[email protected]d4905e2e2011-05-13 21:56:32266 void CheckFileAndCloseHandle(
[email protected]949f25a2012-06-27 01:53:09267 const FileSystemURL& url, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16268 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32269 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49270 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09271 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32272
273 base::PlatformFileInfo file_info0;
274 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49275 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09276 context.get(), url, &file_info0, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32277 EXPECT_EQ(data_path, local_path);
278 EXPECT_TRUE(FileExists(data_path));
279 EXPECT_EQ(0, GetSize(data_path));
280
281 const char data[] = "test data";
282 const int length = arraysize(data) - 1;
283
284 if (base::kInvalidPlatformFileValue == file_handle) {
285 bool created = true;
286 PlatformFileError error;
287 file_handle = base::CreatePlatformFile(
288 data_path,
289 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
290 &created,
291 &error);
[email protected]81b7f662011-05-26 00:54:46292 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32293 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
294 EXPECT_FALSE(created);
295 }
296 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
297 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
298
299 base::PlatformFileInfo file_info1;
300 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16301 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49302 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09303 context.get(), url, &file_info1, &data_path));
[email protected]d4905e2e2011-05-13 21:56:32304 EXPECT_EQ(data_path, local_path);
305
306 EXPECT_FALSE(file_info0.is_directory);
307 EXPECT_FALSE(file_info1.is_directory);
308 EXPECT_FALSE(file_info0.is_symbolic_link);
309 EXPECT_FALSE(file_info1.is_symbolic_link);
310 EXPECT_EQ(0, file_info0.size);
311 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32312 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32313
[email protected]0c5ebe32011-08-19 22:37:16314 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49315 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09316 context.get(), url, length * 2));
[email protected]d4905e2e2011-05-13 21:56:32317 EXPECT_EQ(length * 2, GetSize(data_path));
318
[email protected]0c5ebe32011-08-19 22:37:16319 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49320 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09321 context.get(), url, 0));
[email protected]0c5ebe32011-08-19 22:37:16322 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32323 }
324
325 void ValidateTestDirectory(
[email protected]949f25a2012-06-27 01:53:09326 const FileSystemURL& root_url,
[email protected]89ee4272011-05-16 18:45:17327 const std::set<FilePath::StringType>& files,
328 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32329 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17330 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32331 for (iter = files.begin(); iter != files.end(); ++iter) {
332 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16333 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32334 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49335 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09336 context.get(), root_url.WithPath(root_url.path().Append(*iter)),
[email protected]d4905e2e2011-05-13 21:56:32337 &created));
338 ASSERT_FALSE(created);
339 }
340 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16341 context.reset(NewContext(NULL));
[email protected]13f92f6e2012-08-13 07:39:14342 EXPECT_TRUE(DirectoryExists(
[email protected]949f25a2012-06-27 01:53:09343 root_url.WithPath(root_url.path().Append(*iter))));
[email protected]d4905e2e2011-05-13 21:56:32344 }
345 }
346
[email protected]294dd0312012-05-11 07:35:13347 class UsageVerifyHelper {
348 public:
349 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
[email protected]02a60542012-07-24 20:05:33350 LocalFileSystemTestOriginHelper* test_helper,
[email protected]294dd0312012-05-11 07:35:13351 int64 expected_usage)
352 : context_(context.Pass()),
353 test_helper_(test_helper),
354 expected_usage_(expected_usage) {}
355
356 ~UsageVerifyHelper() {
357 Check();
358 }
359
360 FileSystemOperationContext* context() {
361 return context_.get();
362 }
363
364 private:
365 void Check() {
366 ASSERT_EQ(expected_usage_,
367 test_helper_->GetCachedOriginUsage());
368 }
369
370 scoped_ptr<FileSystemOperationContext> context_;
[email protected]02a60542012-07-24 20:05:33371 LocalFileSystemTestOriginHelper* test_helper_;
[email protected]294dd0312012-05-11 07:35:13372 int64 expected_usage_;
373 };
374
375 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
376 int64 usage = test_helper_.GetCachedOriginUsage();
377 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
378 LimitedContext(requested_growth),
379 &test_helper_, usage + requested_growth));
380 }
381
382 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
383 int64 usage = test_helper_.GetCachedOriginUsage();
384 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
385 LimitedContext(requested_growth - 1), &test_helper_, usage));
386 }
387
[email protected]d4905e2e2011-05-13 21:56:32388 void FillTestDirectory(
[email protected]949f25a2012-06-27 01:53:09389 const FileSystemURL& root_url,
[email protected]89ee4272011-05-16 18:45:17390 std::set<FilePath::StringType>* files,
391 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32392 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16393 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32394 std::vector<base::FileUtilProxy::Entry> entries;
395 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55396 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:09397 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32398 EXPECT_EQ(0UL, entries.size());
399
400 files->clear();
[email protected]89ee4272011-05-16 18:45:17401 files->insert(FILE_PATH_LITERAL("first"));
402 files->insert(FILE_PATH_LITERAL("second"));
403 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32404 directories->clear();
[email protected]89ee4272011-05-16 18:45:17405 directories->insert(FILE_PATH_LITERAL("fourth"));
406 directories->insert(FILE_PATH_LITERAL("fifth"));
407 directories->insert(FILE_PATH_LITERAL("sixth"));
408 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32409 for (iter = files->begin(); iter != files->end(); ++iter) {
410 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16411 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32412 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49413 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50414 context.get(),
[email protected]949f25a2012-06-27 01:53:09415 root_url.WithPath(root_url.path().Append(*iter)),
[email protected]08f8feb2012-02-26 11:53:50416 &created));
[email protected]d4905e2e2011-05-13 21:56:32417 ASSERT_TRUE(created);
418 }
419 for (iter = directories->begin(); iter != directories->end(); ++iter) {
420 bool exclusive = true;
421 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16422 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32423 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49424 ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09425 context.get(), root_url.WithPath(root_url.path().Append(*iter)),
426 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32427 }
[email protected]949f25a2012-06-27 01:53:09428 ValidateTestDirectory(root_url, *files, *directories);
[email protected]d4905e2e2011-05-13 21:56:32429 }
430
[email protected]949f25a2012-06-27 01:53:09431 void TestReadDirectoryHelper(const FileSystemURL& root_url) {
[email protected]89ee4272011-05-16 18:45:17432 std::set<FilePath::StringType> files;
433 std::set<FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:09434 FillTestDirectory(root_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:32435
436 scoped_ptr<FileSystemOperationContext> context;
437 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16438 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32439 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55440 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:09441 context.get(), ofu(), root_url, &entries));
[email protected]d4905e2e2011-05-13 21:56:32442 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
443 EXPECT_EQ(files.size() + directories.size(), entries.size());
[email protected]c4e6f9c2012-09-09 17:42:10444 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32445 for (entry_iter = entries.begin(); entry_iter != entries.end();
446 ++entry_iter) {
447 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17448 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32449 if (iter != files.end()) {
450 EXPECT_FALSE(entry.is_directory);
451 files.erase(iter);
452 continue;
453 }
[email protected]89ee4272011-05-16 18:45:17454 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32455 EXPECT_FALSE(directories.end() == iter);
456 EXPECT_TRUE(entry.is_directory);
457 directories.erase(iter);
458 }
459 }
460
[email protected]949f25a2012-06-27 01:53:09461 void TestTouchHelper(const FileSystemURL& url, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41462 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32463 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16464
[email protected]2517cfa2011-08-25 05:12:41465 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32466 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49467 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09468 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10469 // Currently we fire no change notifications for Touch.
470 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32471 FilePath local_path;
472 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16473 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49474 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09475 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32476 // We compare as time_t here to lower our resolution, to avoid false
477 // negatives caused by conversion to the local filesystem's native
478 // representation and back.
479 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
480
[email protected]0c5ebe32011-08-19 22:37:16481 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32482 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41483 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32484 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49485 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:09486 context.get(), url, last_access_time, last_modified_time));
[email protected]c4e6f9c2012-09-09 17:42:10487 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16488 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49489 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09490 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32491 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49492 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41493 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32494 }
495
[email protected]81b7f662011-05-26 00:54:46496 void TestCopyInForeignFileHelper(bool overwrite) {
497 ScopedTempDir source_dir;
498 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]08f8feb2012-02-26 11:53:50499 FilePath root_file_path = source_dir.path();
500 FilePath src_file_path = root_file_path.AppendASCII("file_name");
[email protected]949f25a2012-06-27 01:53:09501 FileSystemURL dest_url = CreateURLFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46502 int64 src_file_length = 87;
503
504 base::PlatformFileError error_code;
505 bool created = false;
506 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
507 base::PlatformFile file_handle =
508 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50509 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46510 EXPECT_TRUE(created);
511 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
512 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
513 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
514 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
515
516 scoped_ptr<FileSystemOperationContext> context;
517
518 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16519 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46520 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09521 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]81b7f662011-05-26 00:54:46522 EXPECT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10523
524 // We must have observed one (and only one) create_file_count.
525 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
526 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]81b7f662011-05-26 00:54:46527 }
528
[email protected]0c5ebe32011-08-19 22:37:16529 const int64 path_cost =
[email protected]949f25a2012-06-27 01:53:09530 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path());
[email protected]0c5ebe32011-08-19 22:37:16531 if (!overwrite) {
532 // Verify that file creation requires sufficient quota for the path.
533 context.reset(NewContext(NULL));
534 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
535 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13536 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09537 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16538 }
539
540 context.reset(NewContext(NULL));
541 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46542 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13543 ofu()->CopyInForeignFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09544 src_file_path, dest_url));
[email protected]0c5ebe32011-08-19 22:37:16545
[email protected]13f92f6e2012-08-13 07:39:14546 EXPECT_TRUE(PathExists(dest_url));
547 EXPECT_FALSE(DirectoryExists(dest_url));
548
[email protected]0c5ebe32011-08-19 22:37:16549 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46550 base::PlatformFileInfo file_info;
551 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49552 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09553 context.get(), dest_url, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50554 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46555 EXPECT_TRUE(FileExists(data_path));
556 EXPECT_EQ(src_file_length, GetSize(data_path));
557
558 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09559 ofu()->DeleteFile(context.get(), dest_url));
[email protected]81b7f662011-05-26 00:54:46560 }
561
[email protected]949f25a2012-06-27 01:53:09562 void ClearTimestamp(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03563 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
564 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09565 ofu()->Touch(context.get(), url, base::Time(), base::Time()));
566 EXPECT_EQ(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:03567 }
568
[email protected]949f25a2012-06-27 01:53:09569 base::Time GetModifiedTime(const FileSystemURL& url) {
[email protected]fad625e2f2011-12-08 05:38:03570 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
571 FilePath data_path;
572 base::PlatformFileInfo file_info;
573 context.reset(NewContext(NULL));
574 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09575 ofu()->GetFileInfo(context.get(), url, &file_info, &data_path));
[email protected]c4e6f9c2012-09-09 17:42:10576 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]fad625e2f2011-12-08 05:38:03577 return file_info.last_modified;
578 }
579
[email protected]949f25a2012-06-27 01:53:09580 void TestDirectoryTimestampHelper(const FileSystemURL& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03581 bool copy,
582 bool overwrite) {
583 scoped_ptr<FileSystemOperationContext> context;
[email protected]949f25a2012-06-27 01:53:09584 const FileSystemURL src_dir_url(base_dir.WithPath(
585 base_dir.path().AppendASCII("foo_dir")));
586 const FileSystemURL dest_dir_url(base_dir.WithPath(
587 base_dir.path().AppendASCII("bar_dir")));
[email protected]fad625e2f2011-12-08 05:38:03588
[email protected]949f25a2012-06-27 01:53:09589 const FileSystemURL src_file_url(src_dir_url.WithPath(
590 src_dir_url.path().AppendASCII("hoge")));
591 const FileSystemURL dest_file_url(dest_dir_url.WithPath(
592 dest_dir_url.path().AppendASCII("fuga")));
[email protected]fad625e2f2011-12-08 05:38:03593
594 context.reset(NewContext(NULL));
595 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09596 ofu()->CreateDirectory(context.get(), src_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03597 context.reset(NewContext(NULL));
598 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09599 ofu()->CreateDirectory(context.get(), dest_dir_url, true, true));
[email protected]fad625e2f2011-12-08 05:38:03600
601 bool created = false;
602 context.reset(NewContext(NULL));
603 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09604 ofu()->EnsureFileExists(context.get(), src_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03605 if (overwrite) {
606 context.reset(NewContext(NULL));
607 EXPECT_EQ(base::PLATFORM_FILE_OK,
608 ofu()->EnsureFileExists(context.get(),
[email protected]949f25a2012-06-27 01:53:09609 dest_file_url, &created));
[email protected]fad625e2f2011-12-08 05:38:03610 }
611
[email protected]949f25a2012-06-27 01:53:09612 ClearTimestamp(src_dir_url);
613 ClearTimestamp(dest_dir_url);
[email protected]fad625e2f2011-12-08 05:38:03614 context.reset(NewContext(NULL));
615 EXPECT_EQ(base::PLATFORM_FILE_OK,
616 ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:09617 src_file_url, dest_file_url,
[email protected]fad625e2f2011-12-08 05:38:03618 copy));
[email protected]fad625e2f2011-12-08 05:38:03619 if (copy)
[email protected]949f25a2012-06-27 01:53:09620 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03621 else
[email protected]949f25a2012-06-27 01:53:09622 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url));
623 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url));
[email protected]fad625e2f2011-12-08 05:38:03624 }
625
[email protected]ecdfd6c52012-04-11 13:35:44626 int64 ComputeCurrentUsage() {
627 return test_helper().ComputeCurrentOriginUsage() -
628 test_helper().ComputeCurrentDirectoryDatabaseUsage();
629 }
630
[email protected]02a60542012-07-24 20:05:33631 const LocalFileSystemTestOriginHelper& test_helper() const {
632 return test_helper_;
633 }
[email protected]45ea0fbbb2012-02-27 22:28:49634
[email protected]d4905e2e2011-05-13 21:56:32635 private:
636 ScopedTempDir data_dir_;
[email protected]3ed847a62012-06-07 01:20:01637 MessageLoop message_loop_;
[email protected]3cfc10f2012-05-24 01:20:41638 ObfuscatedFileUtil* obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16639 scoped_refptr<quota::QuotaManager> quota_manager_;
640 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26641 GURL origin_;
642 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03643 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]02a60542012-07-24 20:05:33644 LocalFileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16645 quota::QuotaStatusCode quota_status_;
646 int64 usage_;
[email protected]c4e6f9c2012-09-09 17:42:10647 MockFileChangeObserver change_observer_;
648 ChangeObserverList change_observers_;
[email protected]d4905e2e2011-05-13 21:56:32649
[email protected]7878ece2011-09-05 11:41:49650 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32651};
652
[email protected]7878ece2011-09-05 11:41:49653TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32654 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
655 bool created;
[email protected]949f25a2012-06-27 01:53:09656 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16657 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32658 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
659
660 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49661 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09662 context.get(), url, file_flags, &file_handle,
[email protected]d4905e2e2011-05-13 21:56:32663 &created));
664
[email protected]0c5ebe32011-08-19 22:37:16665 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32666 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09667 ofu()->DeleteFile(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32668
[email protected]949f25a2012-06-27 01:53:09669 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32670
[email protected]c4e6f9c2012-09-09 17:42:10671 EXPECT_TRUE(change_observer()->HasNoChange());
672
[email protected]0c5ebe32011-08-19 22:37:16673 // Verify that file creation requires sufficient quota for the path.
674 context.reset(NewContext(NULL));
675 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09676 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16677 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49678 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09679 context.get(), url, file_flags, &file_handle, &created));
[email protected]0c5ebe32011-08-19 22:37:16680
681 context.reset(NewContext(NULL));
682 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09683 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32684 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49685 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09686 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32687 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10688 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32689 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
690
[email protected]949f25a2012-06-27 01:53:09691 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32692
[email protected]0c5ebe32011-08-19 22:37:16693 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32694 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49695 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09696 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32697 EXPECT_TRUE(file_util::PathExists(local_path));
698
[email protected]0c5ebe32011-08-19 22:37:16699 // Verify that deleting a file isn't stopped by zero quota, and that it frees
700 // up quote from its path.
701 context.reset(NewContext(NULL));
702 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32703 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09704 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10705 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32706 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]949f25a2012-06-27 01:53:09707 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16708 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32709
[email protected]0c5ebe32011-08-19 22:37:16710 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32711 bool exclusive = true;
712 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:09713 FileSystemURL directory_url = CreateURLFromUTF8(
[email protected]08f8feb2012-02-26 11:53:50714 "series/of/directories");
[email protected]949f25a2012-06-27 01:53:09715 url = directory_url.WithPath(directory_url.path().AppendASCII("file name"));
[email protected]7878ece2011-09-05 11:41:49716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09717 context.get(), directory_url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10718 // The oepration created 3 directories recursively.
719 EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32720
[email protected]0c5ebe32011-08-19 22:37:16721 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32722 file_handle = base::kInvalidPlatformFileValue;
723 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49724 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:09725 context.get(), url, file_flags, &file_handle, &created));
[email protected]d4905e2e2011-05-13 21:56:32726 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10727 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32728 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
729
[email protected]949f25a2012-06-27 01:53:09730 CheckFileAndCloseHandle(url, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32731
[email protected]0c5ebe32011-08-19 22:37:16732 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49733 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09734 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32735 EXPECT_TRUE(file_util::PathExists(local_path));
736
[email protected]0c5ebe32011-08-19 22:37:16737 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32738 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09739 ofu()->DeleteFile(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10740 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
[email protected]d4905e2e2011-05-13 21:56:32741 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10742
743 // Make sure we have no unexpected changes.
744 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32745}
746
[email protected]7878ece2011-09-05 11:41:49747TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32748 bool created = false;
[email protected]949f25a2012-06-27 01:53:09749 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16750 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32751
752 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09753 ofu()->Truncate(context.get(), url, 4));
[email protected]d4905e2e2011-05-13 21:56:32754
[email protected]0c5ebe32011-08-19 22:37:16755 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32756 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09757 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32758 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10759 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32760
[email protected]0c5ebe32011-08-19 22:37:16761 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32762 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49763 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]949f25a2012-06-27 01:53:09764 context.get(), url, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32765 EXPECT_EQ(0, GetSize(local_path));
766
[email protected]0c5ebe32011-08-19 22:37:16767 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49768 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09769 context.get(), url, 10));
[email protected]c4e6f9c2012-09-09 17:42:10770 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32771 EXPECT_EQ(10, GetSize(local_path));
772
[email protected]0c5ebe32011-08-19 22:37:16773 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49774 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]949f25a2012-06-27 01:53:09775 context.get(), url, 1));
[email protected]d4905e2e2011-05-13 21:56:32776 EXPECT_EQ(1, GetSize(local_path));
[email protected]c4e6f9c2012-09-09 17:42:10777 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
[email protected]d4905e2e2011-05-13 21:56:32778
[email protected]13f92f6e2012-08-13 07:39:14779 EXPECT_FALSE(DirectoryExists(url));
780 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10781
782 // Make sure we have no unexpected changes.
783 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32784}
785
[email protected]ecdfd6c52012-04-11 13:35:44786TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
787 bool created = false;
[email protected]949f25a2012-06-27 01:53:09788 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44789
790 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13791 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09792 AllowUsageIncrease(PathCost(url))->context(),
793 url, &created));
[email protected]ecdfd6c52012-04-11 13:35:44794 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13795 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44796
[email protected]ecdfd6c52012-04-11 13:35:44797 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13798 ofu()->Truncate(
799 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09800 url, 1020));
[email protected]294dd0312012-05-11 07:35:13801 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44802
[email protected]ecdfd6c52012-04-11 13:35:44803 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13804 ofu()->Truncate(
805 AllowUsageIncrease(-1020)->context(),
[email protected]949f25a2012-06-27 01:53:09806 url, 0));
[email protected]294dd0312012-05-11 07:35:13807 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44808
[email protected]ecdfd6c52012-04-11 13:35:44809 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13810 ofu()->Truncate(
811 DisallowUsageIncrease(1021)->context(),
[email protected]949f25a2012-06-27 01:53:09812 url, 1021));
[email protected]294dd0312012-05-11 07:35:13813 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44814
[email protected]ecdfd6c52012-04-11 13:35:44815 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13816 ofu()->Truncate(
817 AllowUsageIncrease(1020)->context(),
[email protected]949f25a2012-06-27 01:53:09818 url, 1020));
[email protected]294dd0312012-05-11 07:35:13819 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44820
[email protected]ecdfd6c52012-04-11 13:35:44821 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13822 ofu()->Truncate(
823 AllowUsageIncrease(0)->context(),
[email protected]949f25a2012-06-27 01:53:09824 url, 1020));
[email protected]294dd0312012-05-11 07:35:13825 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44826
[email protected]294dd0312012-05-11 07:35:13827 // quota exceeded
828 {
829 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
830 helper->context()->set_allowed_bytes_growth(
831 helper->context()->allowed_bytes_growth() - 1);
832 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09833 ofu()->Truncate(helper->context(), url, 1019));
[email protected]294dd0312012-05-11 07:35:13834 ASSERT_EQ(1019, ComputeTotalFileSize());
835 }
[email protected]ecdfd6c52012-04-11 13:35:44836
837 // Delete backing file to make following truncation fail.
[email protected]ecdfd6c52012-04-11 13:35:44838 FilePath local_path;
839 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13840 ofu()->GetLocalFilePath(
841 UnlimitedContext().get(),
[email protected]949f25a2012-06-27 01:53:09842 url, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44843 ASSERT_FALSE(local_path.empty());
844 ASSERT_TRUE(file_util::Delete(local_path, false));
845
[email protected]ecdfd6c52012-04-11 13:35:44846 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13847 ofu()->Truncate(
848 LimitedContext(1234).get(),
[email protected]949f25a2012-06-27 01:53:09849 url, 1234));
[email protected]294dd0312012-05-11 07:35:13850 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44851}
852
[email protected]7878ece2011-09-05 11:41:49853TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]949f25a2012-06-27 01:53:09854 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32855 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16856 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32857 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49858 ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:09859 context.get(), url, &created));
[email protected]c4e6f9c2012-09-09 17:42:10860 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32861
[email protected]0c5ebe32011-08-19 22:37:16862 // Verify that file creation requires sufficient quota for the path.
863 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09864 url = CreateURLFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32865 created = false;
[email protected]0c5ebe32011-08-19 22:37:16866 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09867 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16868 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:09869 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]0c5ebe32011-08-19 22:37:16870 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10871 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:16872
873 context.reset(NewContext(NULL));
874 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:09875 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]d4905e2e2011-05-13 21:56:32876 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09877 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32878 ASSERT_TRUE(created);
[email protected]c4e6f9c2012-09-09 17:42:10879 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
[email protected]d4905e2e2011-05-13 21:56:32880
[email protected]949f25a2012-06-27 01:53:09881 CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue);
[email protected]d4905e2e2011-05-13 21:56:32882
[email protected]0c5ebe32011-08-19 22:37:16883 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32884 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09885 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32886 ASSERT_FALSE(created);
[email protected]c4e6f9c2012-09-09 17:42:10887 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32888
889 // Also test in a subdirectory.
[email protected]949f25a2012-06-27 01:53:09890 url = CreateURLFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16891 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32892 bool exclusive = true;
893 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49894 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09895 context.get(),
896 url.WithPath(url.path().DirName()),
897 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10898 // 2 directories: path/ and path/to.
899 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32900
[email protected]0c5ebe32011-08-19 22:37:16901 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32902 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09903 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:32904 ASSERT_TRUE(created);
[email protected]13f92f6e2012-08-13 07:39:14905 EXPECT_FALSE(DirectoryExists(url));
906 EXPECT_TRUE(PathExists(url));
[email protected]c4e6f9c2012-09-09 17:42:10907 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32908}
909
[email protected]7878ece2011-09-05 11:41:49910TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16911 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32912
913 bool exclusive = false;
914 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:09915 FileSystemURL url = CreateURLFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49916 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09917 context.get(), url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32918
[email protected]0c5ebe32011-08-19 22:37:16919 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32920 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:09921 ofu()->DeleteSingleDirectory(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:32922
[email protected]949f25a2012-06-27 01:53:09923 FileSystemURL root = CreateURLFromUTF8("");
[email protected]13f92f6e2012-08-13 07:39:14924 EXPECT_FALSE(DirectoryExists(url));
925 EXPECT_FALSE(PathExists(url));
[email protected]0c5ebe32011-08-19 22:37:16926 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49927 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32928
[email protected]0c5ebe32011-08-19 22:37:16929 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32930 exclusive = false;
931 recursive = true;
[email protected]7878ece2011-09-05 11:41:49932 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09933 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10934 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:32935
[email protected]13f92f6e2012-08-13 07:39:14936 EXPECT_TRUE(DirectoryExists(url));
937 EXPECT_TRUE(PathExists(url));
938
[email protected]0c5ebe32011-08-19 22:37:16939 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49940 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]13f92f6e2012-08-13 07:39:14941 EXPECT_TRUE(DirectoryExists(url.WithPath(url.path().DirName())));
942
[email protected]0c5ebe32011-08-19 22:37:16943 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09944 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(),
945 url.WithPath(url.path().DirName())));
[email protected]d4905e2e2011-05-13 21:56:32946
947 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16948 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06949 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]949f25a2012-06-27 01:53:09950 ofu()->DeleteSingleDirectory(context.get(),
951 url.WithPath(url.path().DirName())));
[email protected]c4e6f9c2012-09-09 17:42:10952 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32953
954 base::PlatformFileInfo file_info;
955 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49956 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09957 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32958 EXPECT_TRUE(local_path.empty());
959 EXPECT_TRUE(file_info.is_directory);
960 EXPECT_FALSE(file_info.is_symbolic_link);
961
962 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16963 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49964 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09965 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10966 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32967
968 exclusive = true;
969 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16970 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49971 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:09972 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:10973 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:32974
[email protected]0c5ebe32011-08-19 22:37:16975 // Verify that deleting a directory isn't stopped by zero quota, and that it
976 // frees up quota from its path.
977 context.reset(NewContext(NULL));
978 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32979 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:09980 ofu()->DeleteSingleDirectory(context.get(), url));
[email protected]c4e6f9c2012-09-09 17:42:10981 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
[email protected]949f25a2012-06-27 01:53:09982 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()),
[email protected]0c5ebe32011-08-19 22:37:16983 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32984
[email protected]949f25a2012-06-27 01:53:09985 url = CreateURLFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:32986
[email protected]13f92f6e2012-08-13 07:39:14987 EXPECT_FALSE(DirectoryExists(url));
988 EXPECT_FALSE(PathExists(url));
989
[email protected]0c5ebe32011-08-19 22:37:16990 context.reset(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:09991 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]7878ece2011-09-05 11:41:49992 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:09993 context.get(), url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:32994
[email protected]0c5ebe32011-08-19 22:37:16995 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32996 exclusive = true;
997 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16998 context.reset(NewContext(NULL));
999 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091000 ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1);
[email protected]7878ece2011-09-05 11:41:491001 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091002 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101003 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161004
1005 context.reset(NewContext(NULL));
1006 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091007 ObfuscatedFileUtil::ComputeFilePathCost(url.path()));
[email protected]7878ece2011-09-05 11:41:491008 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091009 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101010 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321011
[email protected]13f92f6e2012-08-13 07:39:141012 EXPECT_TRUE(DirectoryExists(url));
1013 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321014
1015 exclusive = true;
1016 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141017 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491018 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091019 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101020 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321021
1022 exclusive = true;
1023 recursive = false;
[email protected]949f25a2012-06-27 01:53:091024 url = CreateURLFromUTF8("foo");
[email protected]13f92f6e2012-08-13 07:39:141025 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491026 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091027 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101028 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321029
[email protected]949f25a2012-06-27 01:53:091030 url = CreateURLFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:321031
[email protected]13f92f6e2012-08-13 07:39:141032 EXPECT_FALSE(DirectoryExists(url));
1033 EXPECT_FALSE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321034
1035 exclusive = true;
1036 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141037 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491038 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091039 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101040 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321041
[email protected]13f92f6e2012-08-13 07:39:141042 EXPECT_TRUE(DirectoryExists(url));
1043 EXPECT_TRUE(PathExists(url));
[email protected]d4905e2e2011-05-13 21:56:321044
1045 exclusive = true;
1046 recursive = false;
[email protected]13f92f6e2012-08-13 07:39:141047 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491048 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091049 context.get(), url, exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101050 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321051}
1052
[email protected]7878ece2011-09-05 11:41:491053TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:161054 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321055 bool exclusive = true;
1056 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091057 FileSystemURL url = CreateURLFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:491058 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091059 context.get(), url, exclusive, recursive));
1060 TestReadDirectoryHelper(url);
[email protected]d4905e2e2011-05-13 21:56:321061}
1062
[email protected]7878ece2011-09-05 11:41:491063TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]949f25a2012-06-27 01:53:091064 TestReadDirectoryHelper(CreateURLFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:321065}
1066
[email protected]7878ece2011-09-05 11:41:491067TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]949f25a2012-06-27 01:53:091068 TestReadDirectoryHelper(CreateURLFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321069}
1070
[email protected]7878ece2011-09-05 11:41:491071TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]949f25a2012-06-27 01:53:091072 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161073 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321074
1075 bool created = false;
1076 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091077 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]d4905e2e2011-05-13 21:56:321078 ASSERT_TRUE(created);
1079
[email protected]0c5ebe32011-08-19 22:37:161080 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321081 std::vector<base::FileUtilProxy::Entry> entries;
1082 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]a3938912012-03-27 14:00:551083 FileUtilHelper::ReadDirectory(
[email protected]949f25a2012-06-27 01:53:091084 context.get(), ofu(), url, &entries));
[email protected]d4905e2e2011-05-13 21:56:321085
[email protected]949f25a2012-06-27 01:53:091086 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url));
[email protected]d4905e2e2011-05-13 21:56:321087}
1088
[email protected]7878ece2011-09-05 11:41:491089TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]949f25a2012-06-27 01:53:091090 FileSystemURL url = CreateURLFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161091 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411092
1093 base::Time last_access_time = base::Time::Now();
1094 base::Time last_modified_time = base::Time::Now();
1095
1096 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321097 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491098 ofu()->Touch(
[email protected]949f25a2012-06-27 01:53:091099 context.get(), url, last_access_time, last_modified_time));
[email protected]d4905e2e2011-05-13 21:56:321100
[email protected]2517cfa2011-08-25 05:12:411101 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161102 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411103 bool created = false;
1104 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091105 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411106 ASSERT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091107 TestTouchHelper(url, true);
[email protected]2517cfa2011-08-25 05:12:411108
1109 // Now test a directory:
1110 context.reset(NewContext(NULL));
1111 bool exclusive = true;
1112 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091113 url = CreateURLFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491114 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]949f25a2012-06-27 01:53:091115 url, exclusive, recursive));
1116 TestTouchHelper(url, false);
[email protected]0c5ebe32011-08-19 22:37:161117}
1118
[email protected]7878ece2011-09-05 11:41:491119TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091120 FileSystemURL url = CreateURLFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161121 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1122
[email protected]949f25a2012-06-27 01:53:091123 url = CreateURLFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161124 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411125 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161126 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091127 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411128 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161129 context->set_allowed_bytes_growth(1024);
1130 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091131 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]2517cfa2011-08-25 05:12:411132 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091133 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path());
[email protected]0c5ebe32011-08-19 22:37:161134 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1135
1136 context->set_allowed_bytes_growth(1024);
1137 bool exclusive = true;
1138 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091139 url = CreateURLFromUTF8("directory/to/use");
[email protected]0c5ebe32011-08-19 22:37:161140 std::vector<FilePath::StringType> components;
[email protected]949f25a2012-06-27 01:53:091141 url.path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161142 path_cost = 0;
1143 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
1144 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491145 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:161146 FilePath(*iter));
1147 }
1148 context.reset(NewContext(NULL));
1149 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491150 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091151 context.get(), url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161152 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321153}
1154
[email protected]7878ece2011-09-05 11:41:491155TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]949f25a2012-06-27 01:53:091156 FileSystemURL source_url = CreateURLFromUTF8("path0.txt");
1157 FileSystemURL dest_url = CreateURLFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161158 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321159
1160 bool is_copy_not_move = false;
1161 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091162 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321163 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101164 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161165 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321166 is_copy_not_move = true;
1167 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091168 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321169 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101170 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]949f25a2012-06-27 01:53:091171 source_url = CreateURLFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321172 bool exclusive = true;
1173 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161174 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491175 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091176 context.get(),
1177 source_url.WithPath(source_url.path().DirName()),
1178 exclusive, recursive));
[email protected]c4e6f9c2012-09-09 17:42:101179 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
[email protected]d4905e2e2011-05-13 21:56:321180 is_copy_not_move = false;
1181 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091182 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321183 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101184 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]0c5ebe32011-08-19 22:37:161185 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321186 is_copy_not_move = true;
1187 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091188 ofu()->CopyOrMoveFile(context.get(), source_url, dest_url,
[email protected]d4905e2e2011-05-13 21:56:321189 is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101190 EXPECT_TRUE(change_observer()->HasNoChange());
[email protected]d4905e2e2011-05-13 21:56:321191}
1192
[email protected]7878ece2011-09-05 11:41:491193TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321194 const int64 kSourceLength = 5;
1195 const int64 kDestLength = 50;
1196
1197 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1198 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1199 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1200 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1201 test_case.is_copy_not_move);
1202 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1203 test_case.source_path);
1204 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1205 test_case.dest_path);
1206 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1207 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161208 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321209
1210 bool exclusive = false;
1211 bool recursive = true;
[email protected]949f25a2012-06-27 01:53:091212 FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path);
1213 FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321214
[email protected]0c5ebe32011-08-19 22:37:161215 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491216 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091217 context.get(),
1218 source_url.WithPath(source_url.path().DirName()),
1219 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161220 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491221 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091222 context.get(),
1223 dest_url.WithPath(dest_url.path().DirName()),
1224 exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321225
[email protected]d4905e2e2011-05-13 21:56:321226 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161227 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321228 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091229 ofu()->EnsureFileExists(context.get(), source_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321230 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161231 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321232 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091233 ofu()->Truncate(context.get(), source_url, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321234
1235 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161236 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321237 created = false;
1238 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091239 ofu()->EnsureFileExists(context.get(), dest_url, &created));
[email protected]d4905e2e2011-05-13 21:56:321240 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161241 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321242 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091243 ofu()->Truncate(context.get(), dest_url, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321244 }
1245
[email protected]0c5ebe32011-08-19 22:37:161246 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491247 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]949f25a2012-06-27 01:53:091248 source_url, dest_url, test_case.is_copy_not_move));
[email protected]c4e6f9c2012-09-09 17:42:101249
[email protected]d4905e2e2011-05-13 21:56:321250 if (test_case.is_copy_not_move) {
1251 base::PlatformFileInfo file_info;
1252 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161253 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491254 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091255 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321256 EXPECT_EQ(kSourceLength, file_info.size);
1257 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091258 ofu()->DeleteFile(context.get(), source_url));
[email protected]d4905e2e2011-05-13 21:56:321259 } else {
1260 base::PlatformFileInfo file_info;
1261 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161262 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491263 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091264 context.get(), source_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321265 }
1266 base::PlatformFileInfo file_info;
1267 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491268 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]949f25a2012-06-27 01:53:091269 context.get(), dest_url, &file_info, &local_path));
[email protected]d4905e2e2011-05-13 21:56:321270 EXPECT_EQ(kSourceLength, file_info.size);
1271
1272 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091273 ofu()->DeleteFile(context.get(), dest_url));
[email protected]d4905e2e2011-05-13 21:56:321274 }
1275}
1276
[email protected]7878ece2011-09-05 11:41:491277TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]949f25a2012-06-27 01:53:091278 FileSystemURL src_url = CreateURLFromUTF8("src path");
1279 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161280 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1281 bool created = false;
[email protected]7878ece2011-09-05 11:41:491282 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091283 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161284
1285 bool is_copy = true;
1286 // Copy, no overwrite.
1287 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091288 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161289 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091290 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161291 context.reset(NewContext(NULL));
1292 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091293 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161294 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091295 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161296
1297 // Copy, with overwrite.
1298 context.reset(NewContext(NULL));
1299 context->set_allowed_bytes_growth(0);
1300 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091301 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161302}
1303
[email protected]7878ece2011-09-05 11:41:491304TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]949f25a2012-06-27 01:53:091305 FileSystemURL src_url = CreateURLFromUTF8("src path");
1306 FileSystemURL dest_url = CreateURLFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161307 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1308 bool created = false;
[email protected]7878ece2011-09-05 11:41:491309 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091310 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161311
1312 bool is_copy = false;
1313 // Move, rename, no overwrite.
1314 context.reset(NewContext(NULL));
1315 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091316 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1317 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161318 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]949f25a2012-06-27 01:53:091319 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161320 context.reset(NewContext(NULL));
1321 context->set_allowed_bytes_growth(
[email protected]949f25a2012-06-27 01:53:091322 ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) -
1323 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161324 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091325 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161326
1327 context.reset(NewContext(NULL));
[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 // Move, rename, with overwrite.
1332 context.reset(NewContext(NULL));
1333 context->set_allowed_bytes_growth(0);
1334 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091335 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161336}
1337
[email protected]7878ece2011-09-05 11:41:491338TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]949f25a2012-06-27 01:53:091339 FileSystemURL src_url = CreateURLFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161340 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1341 bool created = false;
[email protected]7878ece2011-09-05 11:41:491342 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091343 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161344
1345 bool exclusive = true;
1346 bool recursive = false;
[email protected]949f25a2012-06-27 01:53:091347 FileSystemURL dir_url = CreateURLFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161348 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491349 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091350 context.get(), dir_url, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161351
[email protected]949f25a2012-06-27 01:53:091352 FileSystemURL dest_url = dir_url.WithPath(
1353 dir_url.path().Append(src_url.path()));
[email protected]0c5ebe32011-08-19 22:37:161354
1355 bool is_copy = false;
1356 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1357 // Move, no rename, no overwrite.
1358 context.reset(NewContext(NULL));
1359 context->set_allowed_bytes_growth(allowed_bytes_growth);
1360 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091361 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161362 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1363
1364 // Move, no rename, with overwrite.
1365 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491366 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]949f25a2012-06-27 01:53:091367 context.get(), src_url, &created));
[email protected]0c5ebe32011-08-19 22:37:161368 context.reset(NewContext(NULL));
1369 context->set_allowed_bytes_growth(allowed_bytes_growth);
1370 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091371 ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161372 EXPECT_EQ(
1373 allowed_bytes_growth +
[email protected]949f25a2012-06-27 01:53:091374 ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()),
[email protected]0c5ebe32011-08-19 22:37:161375 context->allowed_bytes_growth());
1376}
1377
[email protected]7878ece2011-09-05 11:41:491378TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461379 TestCopyInForeignFileHelper(false /* overwrite */);
1380 TestCopyInForeignFileHelper(true /* overwrite */);
1381}
1382
[email protected]7878ece2011-09-05 11:41:491383TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161384 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091385 FileSystemURL src_url = CreateURLFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321386 bool exclusive = true;
1387 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491388 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]949f25a2012-06-27 01:53:091389 context.get(), src_url, exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:321390
[email protected]89ee4272011-05-16 18:45:171391 std::set<FilePath::StringType> files;
1392 std::set<FilePath::StringType> directories;
[email protected]949f25a2012-06-27 01:53:091393 FillTestDirectory(src_url, &files, &directories);
[email protected]d4905e2e2011-05-13 21:56:321394
[email protected]949f25a2012-06-27 01:53:091395 FileSystemURL dest_url = CreateURLFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321396
[email protected]13f92f6e2012-08-13 07:39:141397 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]0c5ebe32011-08-19 22:37:161398 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321399 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091400 test_helper().SameFileUtilCopy(context.get(), src_url, dest_url));
[email protected]d4905e2e2011-05-13 21:56:321401
[email protected]949f25a2012-06-27 01:53:091402 ValidateTestDirectory(dest_url, files, directories);
[email protected]13f92f6e2012-08-13 07:39:141403 EXPECT_TRUE(DirectoryExists(src_url));
1404 EXPECT_TRUE(DirectoryExists(dest_url));
[email protected]0c5ebe32011-08-19 22:37:161405 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321406 recursive = true;
1407 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]aa437fa2012-03-03 02:09:071408 FileUtilHelper::Delete(context.get(), ofu(),
[email protected]949f25a2012-06-27 01:53:091409 dest_url, recursive));
[email protected]13f92f6e2012-08-13 07:39:141410 EXPECT_FALSE(DirectoryExists(dest_url));
[email protected]d4905e2e2011-05-13 21:56:321411}
[email protected]6b931152011-05-20 21:02:351412
[email protected]7878ece2011-09-05 11:41:491413TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351414 ScopedTempDir source_dir;
1415 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1416 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1417 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1418
[email protected]f83b5b72012-01-27 10:26:561419 test::SetUpRegularTestCases(root_path);
[email protected]6b931152011-05-20 21:02:351420
[email protected]7878ece2011-09-05 11:41:491421 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351422
1423 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161424 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491425 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]f83b5b72012-01-27 10:26:561426 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]6b931152011-05-20 21:02:351427 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561428 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]6b931152011-05-20 21:02:351429 FilePath local_data_path = new_root.Append(test_case.path);
[email protected]d9034ed22012-02-10 02:04:401430 local_data_path = local_data_path.NormalizePathSeparators();
[email protected]0c5ebe32011-08-19 22:37:161431 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491432 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351433 FilePath data_path;
1434 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1435 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091436 ofu()->GetFileInfo(context.get(), CreateURL(FilePath(test_case.path)),
[email protected]7878ece2011-09-05 11:41:491437 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351438 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491439 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351440 } else {
1441 base::PlatformFileInfo platform_file_info;
1442 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1443 local_data_path.value());
1444 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1445 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1446 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1447 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161448 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351449 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491450 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1451 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351452 }
1453 }
1454}
[email protected]fcc2d5f2011-05-23 22:06:261455
[email protected]7878ece2011-09-05 11:41:491456TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1457 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1458 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161459 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261460 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161461 EXPECT_EQ(origin(), enumerator->Next());
1462 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1463 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1464 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261465 EXPECT_EQ(GURL(), enumerator->Next());
1466 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1467 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1468
1469 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161470 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261471
1472 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1473 SCOPED_TRACE(testing::Message() <<
1474 "Validating kOriginEnumerationTestRecords " << i);
1475 const OriginEnumerationTestRecord& record =
1476 kOriginEnumerationTestRecords[i];
1477 GURL origin_url(record.origin_url);
1478 origins_expected.insert(origin_url);
1479 if (record.has_temporary) {
[email protected]02a60542012-07-24 20:05:331480 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161481 NewHelper(origin_url, kFileSystemTypeTemporary));
1482 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261483 bool created = false;
1484 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501485 ofu()->EnsureFileExists(
1486 context.get(),
[email protected]949f25a2012-06-27 01:53:091487 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501488 &created));
[email protected]fcc2d5f2011-05-23 22:06:261489 EXPECT_TRUE(created);
1490 }
1491 if (record.has_persistent) {
[email protected]02a60542012-07-24 20:05:331492 scoped_ptr<LocalFileSystemTestOriginHelper> helper(
[email protected]0c5ebe32011-08-19 22:37:161493 NewHelper(origin_url, kFileSystemTypePersistent));
1494 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261495 bool created = false;
1496 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501497 ofu()->EnsureFileExists(
1498 context.get(),
[email protected]949f25a2012-06-27 01:53:091499 helper->CreateURLFromUTF8("file"),
[email protected]08f8feb2012-02-26 11:53:501500 &created));
[email protected]fcc2d5f2011-05-23 22:06:261501 EXPECT_TRUE(created);
1502 }
1503 }
[email protected]7878ece2011-09-05 11:41:491504 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261505 EXPECT_TRUE(enumerator.get());
1506 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161507 GURL origin_url;
1508 while (!(origin_url = enumerator->Next()).is_empty()) {
1509 origins_found.insert(origin_url);
1510 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261511 bool found = false;
1512 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1513 ++i) {
1514 const OriginEnumerationTestRecord& record =
1515 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161516 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261517 continue;
1518 found = true;
1519 EXPECT_EQ(record.has_temporary,
1520 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1521 EXPECT_EQ(record.has_persistent,
1522 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1523 }
[email protected]0c5ebe32011-08-19 22:37:161524 // Deal with the default filesystem created by the test helper.
1525 if (!found && origin_url == origin()) {
1526 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1527 EXPECT_EQ(true,
1528 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181529 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161530 found = true;
1531 }
[email protected]fcc2d5f2011-05-23 22:06:261532 EXPECT_TRUE(found);
1533 }
1534
1535 std::set<GURL> diff;
1536 std::set_symmetric_difference(origins_expected.begin(),
1537 origins_expected.end(), origins_found.begin(), origins_found.end(),
1538 inserter(diff, diff.begin()));
1539 EXPECT_TRUE(diff.empty());
1540}
[email protected]0c5ebe32011-08-19 22:37:161541
[email protected]7878ece2011-09-05 11:41:491542TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161543 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1544
1545 int64 expected_quota = 0;
1546
[email protected]f83b5b72012-01-27 10:26:561547 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]0c5ebe32011-08-19 22:37:161548 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561549 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]08f8feb2012-02-26 11:53:501550 FilePath file_path(test_case.path);
1551 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161552 if (test_case.is_directory) {
1553 bool exclusive = true;
1554 bool recursive = false;
1555 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091556 ofu()->CreateDirectory(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501557 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161558 } else {
1559 bool created = false;
1560 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091561 ofu()->EnsureFileExists(context.get(), CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501562 &created));
[email protected]0c5ebe32011-08-19 22:37:161563 ASSERT_TRUE(created);
1564 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501565 ofu()->Truncate(context.get(),
[email protected]949f25a2012-06-27 01:53:091566 CreateURL(file_path),
[email protected]08f8feb2012-02-26 11:53:501567 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161568 expected_quota += test_case.data_file_size;
1569 }
1570 }
[email protected]022d2702012-05-14 16:04:261571
1572 // Usually raw size in usage cache and the usage returned by QuotaUtil
1573 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161574 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261575 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1576
[email protected]0c5ebe32011-08-19 22:37:161577 RevokeUsageCache();
1578 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261579 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1580
1581 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161582 GetUsageFromQuotaManager();
1583 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261584 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161585 EXPECT_EQ(expected_quota, usage());
1586}
[email protected]34583332011-08-31 08:59:471587
[email protected]7878ece2011-09-05 11:41:491588TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]949f25a2012-06-27 01:53:091589 const FileSystemURL kPath1 = CreateURLFromUTF8("hoge");
1590 const FileSystemURL kPath2 = CreateURLFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471591
1592 scoped_ptr<FileSystemOperationContext> context;
1593 base::PlatformFile file;
1594 base::PlatformFileInfo file_info;
1595 FilePath data_path;
1596 bool created = false;
1597
1598 // Create a non-empty file.
1599 context.reset(NewContext(NULL));
1600 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491601 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471602 EXPECT_TRUE(created);
1603 context.reset(NewContext(NULL));
1604 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491605 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471606 context.reset(NewContext(NULL));
1607 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491608 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471609 context.get(), kPath1, &file_info, &data_path));
1610 EXPECT_EQ(10, file_info.size);
1611
1612 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491613 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471614
1615 // Try to get file info of broken file.
[email protected]13f92f6e2012-08-13 07:39:141616 EXPECT_FALSE(PathExists(kPath1));
[email protected]34583332011-08-31 08:59:471617 context.reset(NewContext(NULL));
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 context.reset(NewContext(NULL));
1622 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491623 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471624 context.get(), kPath1, &file_info, &data_path));
1625 EXPECT_EQ(0, file_info.size);
1626
1627 // Make another broken file to |kPath2|.
1628 context.reset(NewContext(NULL));
1629 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491630 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471631 EXPECT_TRUE(created);
1632
1633 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491634 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471635
1636 // Repair broken |kPath1|.
1637 context.reset(NewContext(NULL));
1638 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491639 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471640 base::Time::Now()));
1641 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491642 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471643 EXPECT_TRUE(created);
1644
1645 // Copy from sound |kPath1| to broken |kPath2|.
1646 context.reset(NewContext(NULL));
1647 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491648 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501649 true /* copy */));
[email protected]34583332011-08-31 08:59:471650
[email protected]7878ece2011-09-05 11:41:491651 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471652 context.reset(NewContext(NULL));
1653 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491654 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471655 context.get(), kPath1,
1656 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1657 &file, &created));
1658 EXPECT_TRUE(created);
1659 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1660 EXPECT_EQ(0, file_info.size);
1661 EXPECT_TRUE(base::ClosePlatformFile(file));
1662}
[email protected]9dfdc0e32011-09-02 06:07:441663
[email protected]7878ece2011-09-05 11:41:491664TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]949f25a2012-06-27 01:53:091665 const FileSystemURL kPath[] = {
1666 CreateURLFromUTF8("foo"),
1667 CreateURLFromUTF8("bar"),
1668 CreateURLFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441669 };
[email protected]949f25a2012-06-27 01:53:091670 const FileSystemURL empty_path = CreateURL(FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441671 scoped_ptr<FileSystemOperationContext> context;
1672
1673 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1674 bool created = false;
1675 context.reset(NewContext(NULL));
1676 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491677 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441678 EXPECT_TRUE(created);
1679 }
1680
1681 context.reset(NewContext(NULL));
1682 std::vector<base::FileUtilProxy::Entry> entries;
1683 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551684 FileUtilHelper::ReadDirectory(
1685 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441686 EXPECT_EQ(3u, entries.size());
1687
1688 context.reset(NewContext(NULL));
1689 FilePath local_path;
1690 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491691 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441692 EXPECT_TRUE(file_util::Delete(local_path, false));
1693
1694 context.reset(NewContext(NULL));
1695 entries.clear();
1696 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551697 FileUtilHelper::ReadDirectory(
1698 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441699 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1700}
[email protected]fad625e2f2011-12-08 05:38:031701
1702TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1703 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091704 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031705
1706 // Create working directory.
1707 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091708 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031709
1710 // EnsureFileExists, create case.
[email protected]949f25a2012-06-27 01:53:091711 FileSystemURL url(dir_url.WithPath(
1712 dir_url.path().AppendASCII("EnsureFileExists_file")));
[email protected]fad625e2f2011-12-08 05:38:031713 bool created = false;
[email protected]949f25a2012-06-27 01:53:091714 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031715 context.reset(NewContext(NULL));
1716 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091717 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031718 EXPECT_TRUE(created);
[email protected]949f25a2012-06-27 01:53:091719 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031720
1721 // non create case.
1722 created = true;
[email protected]949f25a2012-06-27 01:53:091723 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031724 context.reset(NewContext(NULL));
1725 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091726 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031727 EXPECT_FALSE(created);
[email protected]949f25a2012-06-27 01:53:091728 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031729
1730 // fail case.
[email protected]949f25a2012-06-27 01:53:091731 url = dir_url.WithPath(dir_url.path().AppendASCII("EnsureFileExists_dir"));
[email protected]fad625e2f2011-12-08 05:38:031732 context.reset(NewContext(NULL));
1733 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091734 ofu()->CreateDirectory(context.get(), url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031735
[email protected]949f25a2012-06-27 01:53:091736 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031737 context.reset(NewContext(NULL));
1738 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
[email protected]949f25a2012-06-27 01:53:091739 ofu()->EnsureFileExists(context.get(), url, &created));
1740 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031741
1742 // CreateOrOpen, create case.
[email protected]949f25a2012-06-27 01:53:091743 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateOrOpen_file"));
[email protected]fad625e2f2011-12-08 05:38:031744 PlatformFile file_handle = base::kInvalidPlatformFileValue;
1745 created = false;
[email protected]949f25a2012-06-27 01:53:091746 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031747 context.reset(NewContext(NULL));
1748 EXPECT_EQ(base::PLATFORM_FILE_OK,
1749 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091750 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031751 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1752 &file_handle, &created));
1753 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1754 EXPECT_TRUE(created);
1755 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091756 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031757
1758 // open case.
1759 file_handle = base::kInvalidPlatformFileValue;
1760 created = true;
[email protected]949f25a2012-06-27 01:53:091761 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031762 context.reset(NewContext(NULL));
1763 EXPECT_EQ(base::PLATFORM_FILE_OK,
1764 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091765 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031766 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1767 &file_handle, &created));
1768 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1769 EXPECT_FALSE(created);
1770 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
[email protected]949f25a2012-06-27 01:53:091771 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031772
1773 // fail case
1774 file_handle = base::kInvalidPlatformFileValue;
[email protected]949f25a2012-06-27 01:53:091775 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031776 context.reset(NewContext(NULL));
1777 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1778 ofu()->CreateOrOpen(
[email protected]949f25a2012-06-27 01:53:091779 context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031780 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1781 &file_handle, &created));
1782 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
[email protected]949f25a2012-06-27 01:53:091783 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031784
1785 // CreateDirectory, create case.
1786 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
[email protected]949f25a2012-06-27 01:53:091787 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir"));
1788 FileSystemURL subdir_url(url.WithPath(url.path().AppendASCII("subdir")));
1789 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031790 context.reset(NewContext(NULL));
1791 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091792 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031793 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091794 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031795
1796 // create subdir case.
1797 // Creating CreateDirectory_dir/subdir2.
[email protected]949f25a2012-06-27 01:53:091798 subdir_url = url.WithPath(url.path().AppendASCII("subdir2"));
1799 ClearTimestamp(dir_url);
1800 ClearTimestamp(url);
[email protected]fad625e2f2011-12-08 05:38:031801 context.reset(NewContext(NULL));
1802 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091803 ofu()->CreateDirectory(context.get(), subdir_url,
[email protected]fad625e2f2011-12-08 05:38:031804 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091805 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
1806 EXPECT_NE(base::Time(), GetModifiedTime(url));
[email protected]fad625e2f2011-12-08 05:38:031807
1808 // fail case.
[email protected]949f25a2012-06-27 01:53:091809 url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir"));
1810 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031811 context.reset(NewContext(NULL));
1812 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
[email protected]949f25a2012-06-27 01:53:091813 ofu()->CreateDirectory(context.get(), url,
[email protected]fad625e2f2011-12-08 05:38:031814 true /* exclusive */, true /* recursive */));
[email protected]949f25a2012-06-27 01:53:091815 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031816
1817 // CopyInForeignFile, create case.
[email protected]949f25a2012-06-27 01:53:091818 url = dir_url.WithPath(dir_url.path().AppendASCII("CopyInForeignFile_file"));
1819 FileSystemURL src_path = dir_url.WithPath(
1820 dir_url.path().AppendASCII("CopyInForeignFile_src_file"));
[email protected]fad625e2f2011-12-08 05:38:031821 context.reset(NewContext(NULL));
1822 EXPECT_EQ(base::PLATFORM_FILE_OK,
1823 ofu()->EnsureFileExists(context.get(), src_path, &created));
1824 EXPECT_TRUE(created);
1825 FilePath src_local_path;
1826 context.reset(NewContext(NULL));
1827 EXPECT_EQ(base::PLATFORM_FILE_OK,
1828 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
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]08f8feb2012-02-26 11:53:501833 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131834 src_local_path,
[email protected]949f25a2012-06-27 01:53:091835 url));
1836 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031837}
1838
1839TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1840 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]949f25a2012-06-27 01:53:091841 const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031842
1843 // Create working directory.
1844 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091845 ofu()->CreateDirectory(context.get(), dir_url, false, false));
[email protected]fad625e2f2011-12-08 05:38:031846
1847 // DeleteFile, delete case.
[email protected]949f25a2012-06-27 01:53:091848 FileSystemURL url = dir_url.WithPath(
1849 dir_url.path().AppendASCII("DeleteFile_file"));
[email protected]fad625e2f2011-12-08 05:38:031850 bool created = false;
1851 context.reset(NewContext(NULL));
1852 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091853 ofu()->EnsureFileExists(context.get(), url, &created));
[email protected]fad625e2f2011-12-08 05:38:031854 EXPECT_TRUE(created);
1855
[email protected]949f25a2012-06-27 01:53:091856 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031857 context.reset(NewContext(NULL));
1858 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091859 ofu()->DeleteFile(context.get(), url));
1860 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031861
1862 // fail case.
[email protected]949f25a2012-06-27 01:53:091863 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031864 context.reset(NewContext(NULL));
1865 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]949f25a2012-06-27 01:53:091866 ofu()->DeleteFile(context.get(), url));
1867 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031868
1869 // DeleteSingleDirectory, fail case.
[email protected]949f25a2012-06-27 01:53:091870 url = dir_url.WithPath(
1871 dir_url.path().AppendASCII("DeleteSingleDirectory_dir"));
1872 FileSystemURL file_path(url.WithPath(url.path().AppendASCII("pakeratta")));
[email protected]fad625e2f2011-12-08 05:38:031873 context.reset(NewContext(NULL));
1874 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091875 ofu()->CreateDirectory(context.get(), url, true, true));
[email protected]fad625e2f2011-12-08 05:38:031876 created = false;
1877 context.reset(NewContext(NULL));
1878 EXPECT_EQ(base::PLATFORM_FILE_OK,
1879 ofu()->EnsureFileExists(context.get(), file_path, &created));
1880 EXPECT_TRUE(created);
1881
[email protected]949f25a2012-06-27 01:53:091882 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031883 context.reset(NewContext(NULL));
1884 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]949f25a2012-06-27 01:53:091885 ofu()->DeleteSingleDirectory(context.get(), url));
1886 EXPECT_EQ(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031887
1888 // delete case.
1889 context.reset(NewContext(NULL));
1890 EXPECT_EQ(base::PLATFORM_FILE_OK,
1891 ofu()->DeleteFile(context.get(), file_path));
1892
[email protected]949f25a2012-06-27 01:53:091893 ClearTimestamp(dir_url);
[email protected]fad625e2f2011-12-08 05:38:031894 context.reset(NewContext(NULL));
1895 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091896 ofu()->DeleteSingleDirectory(context.get(), url));
1897 EXPECT_NE(base::Time(), GetModifiedTime(dir_url));
[email protected]fad625e2f2011-12-08 05:38:031898}
1899
1900TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1901 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091902 CreateURLFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031903 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091904 CreateURLFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031905 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091906 CreateURLFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031907 TestDirectoryTimestampHelper(
[email protected]949f25a2012-06-27 01:53:091908 CreateURLFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031909}
[email protected]a3938912012-03-27 14:00:551910
1911TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
[email protected]949f25a2012-06-27 01:53:091912 FileSystemURL dir = CreateURLFromUTF8("foo");
1913 FileSystemURL url1 = dir.WithPath(dir.path().AppendASCII("bar"));
1914 FileSystemURL url2 = dir.WithPath(dir.path().AppendASCII("baz"));
[email protected]a3938912012-03-27 14:00:551915
1916 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1917 EXPECT_EQ(base::PLATFORM_FILE_OK,
1918 ofu()->CreateDirectory(context.get(), dir, false, false));
1919
1920 bool created = false;
1921 context.reset(NewContext(NULL));
1922 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091923 ofu()->EnsureFileExists(context.get(), url1, &created));
[email protected]a3938912012-03-27 14:00:551924 EXPECT_TRUE(created);
1925
1926 context.reset(NewContext(NULL));
1927 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091928 ofu()->CreateDirectory(context.get(), url2, false, false));
[email protected]a3938912012-03-27 14:00:551929
1930 FilePath file_path;
1931 context.reset(NewContext(NULL));
1932 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091933 ofu()->GetLocalFilePath(context.get(), url1, &file_path));
[email protected]a3938912012-03-27 14:00:551934 EXPECT_FALSE(file_path.empty());
1935
1936 context.reset(NewContext(NULL));
1937 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]949f25a2012-06-27 01:53:091938 ofu()->Touch(context.get(), url1,
[email protected]a3938912012-03-27 14:00:551939 base::Time::Now() + base::TimeDelta::FromHours(1),
1940 base::Time()));
1941
1942 context.reset(NewContext(NULL));
1943 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1944 ofu()->CreateFileEnumerator(context.get(), dir, false));
1945
1946 int count = 0;
1947 FilePath file_path_each;
1948 while (!(file_path_each = file_enum->Next()).empty()) {
1949 context.reset(NewContext(NULL));
1950 base::PlatformFileInfo file_info;
1951 FilePath file_path;
1952 EXPECT_EQ(base::PLATFORM_FILE_OK,
1953 ofu()->GetFileInfo(context.get(),
[email protected]949f25a2012-06-27 01:53:091954 dir.WithPath(file_path_each),
[email protected]a3938912012-03-27 14:00:551955 &file_info, &file_path));
1956 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1957 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1958 EXPECT_EQ(file_info.size, file_enum->Size());
1959 ++count;
1960 }
1961 EXPECT_EQ(2, count);
1962}
[email protected]294dd0312012-05-11 07:35:131963
1964TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
[email protected]949f25a2012-06-27 01:53:091965 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
1966 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
1967 FileSystemURL to_file1(CreateURLFromUTF8("tofile1"));
1968 FileSystemURL to_file2(CreateURLFromUTF8("tofile2"));
[email protected]294dd0312012-05-11 07:35:131969 bool created;
1970
1971 int64 expected_total_file_size = 0;
1972 ASSERT_EQ(base::PLATFORM_FILE_OK,
1973 ofu()->EnsureFileExists(
1974 AllowUsageIncrease(PathCost(from_file))->context(),
1975 from_file, &created));
1976 ASSERT_TRUE(created);
1977 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1978
1979 ASSERT_EQ(base::PLATFORM_FILE_OK,
1980 ofu()->EnsureFileExists(
1981 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1982 obstacle_file, &created));
1983 ASSERT_TRUE(created);
1984 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1985
1986 int64 from_file_size = 1020;
1987 expected_total_file_size += from_file_size;
1988 ASSERT_EQ(base::PLATFORM_FILE_OK,
1989 ofu()->Truncate(
1990 AllowUsageIncrease(from_file_size)->context(),
1991 from_file, from_file_size));
1992 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1993
1994 int64 obstacle_file_size = 1;
1995 expected_total_file_size += obstacle_file_size;
1996 ASSERT_EQ(base::PLATFORM_FILE_OK,
1997 ofu()->Truncate(
1998 AllowUsageIncrease(obstacle_file_size)->context(),
1999 obstacle_file, obstacle_file_size));
2000 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2001
2002 int64 to_file1_size = from_file_size;
2003 expected_total_file_size += to_file1_size;
2004 ASSERT_EQ(base::PLATFORM_FILE_OK,
2005 ofu()->CopyOrMoveFile(
2006 AllowUsageIncrease(
2007 PathCost(to_file1) + to_file1_size)->context(),
2008 from_file, to_file1, true /* copy */));
2009 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2010
2011 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
2012 ofu()->CopyOrMoveFile(
2013 DisallowUsageIncrease(
2014 PathCost(to_file2) + from_file_size)->context(),
2015 from_file, to_file2, true /* copy */));
2016 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2017
2018 int64 old_obstacle_file_size = obstacle_file_size;
2019 obstacle_file_size = from_file_size;
2020 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2021 ASSERT_EQ(base::PLATFORM_FILE_OK,
2022 ofu()->CopyOrMoveFile(
2023 AllowUsageIncrease(
2024 obstacle_file_size - old_obstacle_file_size)->context(),
2025 from_file, obstacle_file, true /* copy */));
2026 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2027
2028 int64 old_from_file_size = from_file_size;
2029 from_file_size = old_from_file_size - 1;
2030 expected_total_file_size += from_file_size - old_from_file_size;
2031 ASSERT_EQ(base::PLATFORM_FILE_OK,
2032 ofu()->Truncate(
2033 AllowUsageIncrease(
2034 from_file_size - old_from_file_size)->context(),
2035 from_file, from_file_size));
2036 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2037
2038 // quota exceeded
2039 {
2040 old_obstacle_file_size = obstacle_file_size;
2041 obstacle_file_size = from_file_size;
2042 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
2043 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
2044 obstacle_file_size - old_obstacle_file_size);
2045 helper->context()->set_allowed_bytes_growth(
2046 helper->context()->allowed_bytes_growth() - 1);
2047 ASSERT_EQ(base::PLATFORM_FILE_OK,
2048 ofu()->CopyOrMoveFile(
2049 helper->context(),
2050 from_file, obstacle_file, true /* copy */));
2051 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2052 }
2053}
2054
2055TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
[email protected]949f25a2012-06-27 01:53:092056 FileSystemURL from_file(CreateURLFromUTF8("fromfile"));
2057 FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile"));
2058 FileSystemURL to_file(CreateURLFromUTF8("tofile"));
[email protected]294dd0312012-05-11 07:35:132059 bool created;
2060
2061 int64 expected_total_file_size = 0;
2062 ASSERT_EQ(base::PLATFORM_FILE_OK,
2063 ofu()->EnsureFileExists(
2064 AllowUsageIncrease(PathCost(from_file))->context(),
2065 from_file, &created));
2066 ASSERT_TRUE(created);
2067 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2068
2069 int64 from_file_size = 1020;
2070 expected_total_file_size += from_file_size;
2071 ASSERT_EQ(base::PLATFORM_FILE_OK,
2072 ofu()->Truncate(
2073 AllowUsageIncrease(from_file_size)->context(),
2074 from_file, from_file_size));
2075 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2076
2077 int64 to_file_size ALLOW_UNUSED = from_file_size;
2078 from_file_size = 0;
2079 ASSERT_EQ(base::PLATFORM_FILE_OK,
2080 ofu()->CopyOrMoveFile(
2081 AllowUsageIncrease(-PathCost(from_file) +
2082 PathCost(to_file))->context(),
2083 from_file, to_file, false /* move */));
2084 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2085
2086 ASSERT_EQ(base::PLATFORM_FILE_OK,
2087 ofu()->EnsureFileExists(
2088 AllowUsageIncrease(PathCost(from_file))->context(),
2089 from_file, &created));
2090 ASSERT_TRUE(created);
2091 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2092
2093 ASSERT_EQ(base::PLATFORM_FILE_OK,
2094 ofu()->EnsureFileExists(
2095 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2096 obstacle_file, &created));
2097 ASSERT_TRUE(created);
2098 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2099
2100 from_file_size = 1020;
2101 expected_total_file_size += from_file_size;
2102 ASSERT_EQ(base::PLATFORM_FILE_OK,
2103 ofu()->Truncate(
2104 AllowUsageIncrease(from_file_size)->context(),
2105 from_file, from_file_size));
2106 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2107
2108 int64 obstacle_file_size = 1;
2109 expected_total_file_size += obstacle_file_size;
2110 ASSERT_EQ(base::PLATFORM_FILE_OK,
2111 ofu()->Truncate(
2112 AllowUsageIncrease(1)->context(),
2113 obstacle_file, obstacle_file_size));
2114 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2115
2116 int64 old_obstacle_file_size = obstacle_file_size;
2117 obstacle_file_size = from_file_size;
2118 from_file_size = 0;
2119 expected_total_file_size -= old_obstacle_file_size;
2120 ASSERT_EQ(base::PLATFORM_FILE_OK,
2121 ofu()->CopyOrMoveFile(
2122 AllowUsageIncrease(
2123 -old_obstacle_file_size - PathCost(from_file))->context(),
2124 from_file, obstacle_file,
2125 false /* move */));
2126 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2127
2128 ASSERT_EQ(base::PLATFORM_FILE_OK,
2129 ofu()->EnsureFileExists(
2130 AllowUsageIncrease(PathCost(from_file))->context(),
2131 from_file, &created));
2132 ASSERT_TRUE(created);
2133 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2134
2135 from_file_size = 10;
2136 expected_total_file_size += from_file_size;
2137 ASSERT_EQ(base::PLATFORM_FILE_OK,
2138 ofu()->Truncate(
2139 AllowUsageIncrease(from_file_size)->context(),
2140 from_file, from_file_size));
2141 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2142
2143 // quota exceeded even after operation
2144 old_obstacle_file_size = obstacle_file_size;
2145 obstacle_file_size = from_file_size;
2146 from_file_size = 0;
2147 expected_total_file_size -= old_obstacle_file_size;
2148 scoped_ptr<FileSystemOperationContext> context =
2149 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2150 ASSERT_EQ(base::PLATFORM_FILE_OK,
2151 ofu()->CopyOrMoveFile(
2152 context.get(), from_file, obstacle_file, false /* move */));
2153 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2154 context.reset();
2155}
2156
2157TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
[email protected]949f25a2012-06-27 01:53:092158 FileSystemURL dir(CreateURLFromUTF8("dir"));
2159 FileSystemURL file(CreateURLFromUTF8("file"));
2160 FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1"));
2161 FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2"));
[email protected]294dd0312012-05-11 07:35:132162 bool created;
2163
2164 ASSERT_EQ(base::PLATFORM_FILE_OK,
2165 ofu()->EnsureFileExists(
2166 AllowUsageIncrease(PathCost(file))->context(),
2167 file, &created));
2168 ASSERT_TRUE(created);
2169 ASSERT_EQ(0, ComputeTotalFileSize());
2170
2171 ASSERT_EQ(base::PLATFORM_FILE_OK,
2172 ofu()->CreateDirectory(
2173 AllowUsageIncrease(PathCost(dir))->context(),
2174 dir, false, false));
2175 ASSERT_EQ(0, ComputeTotalFileSize());
2176
2177 ASSERT_EQ(base::PLATFORM_FILE_OK,
2178 ofu()->EnsureFileExists(
2179 AllowUsageIncrease(PathCost(dfile1))->context(),
2180 dfile1, &created));
2181 ASSERT_TRUE(created);
2182 ASSERT_EQ(0, ComputeTotalFileSize());
2183
2184 ASSERT_EQ(base::PLATFORM_FILE_OK,
2185 ofu()->EnsureFileExists(
2186 AllowUsageIncrease(PathCost(dfile2))->context(),
2187 dfile2, &created));
2188 ASSERT_TRUE(created);
2189 ASSERT_EQ(0, ComputeTotalFileSize());
2190
2191 ASSERT_EQ(base::PLATFORM_FILE_OK,
2192 ofu()->Truncate(
2193 AllowUsageIncrease(340)->context(),
2194 file, 340));
2195 ASSERT_EQ(340, ComputeTotalFileSize());
2196
2197 ASSERT_EQ(base::PLATFORM_FILE_OK,
2198 ofu()->Truncate(
2199 AllowUsageIncrease(1020)->context(),
2200 dfile1, 1020));
2201 ASSERT_EQ(1360, ComputeTotalFileSize());
2202
2203 ASSERT_EQ(base::PLATFORM_FILE_OK,
2204 ofu()->Truncate(
2205 AllowUsageIncrease(120)->context(),
2206 dfile2, 120));
2207 ASSERT_EQ(1480, ComputeTotalFileSize());
2208
2209 ASSERT_EQ(base::PLATFORM_FILE_OK,
2210 ofu()->DeleteFile(
2211 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2212 file));
2213 ASSERT_EQ(1140, ComputeTotalFileSize());
2214
2215 ASSERT_EQ(base::PLATFORM_FILE_OK,
2216 FileUtilHelper::Delete(
2217 AllowUsageIncrease(-PathCost(dir) -
2218 PathCost(dfile1) -
2219 PathCost(dfile2) -
2220 1020 - 120)->context(),
2221 ofu(), dir, true));
2222 ASSERT_EQ(0, ComputeTotalFileSize());
2223}
[email protected]7d78be12012-05-24 07:07:262224
2225TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
[email protected]949f25a2012-06-27 01:53:092226 FileSystemURL file(CreateURLFromUTF8("file"));
[email protected]7d78be12012-05-24 07:07:262227 base::PlatformFile file_handle;
2228 bool created;
2229
2230 // Creating a file.
2231 ASSERT_EQ(base::PLATFORM_FILE_OK,
2232 ofu()->EnsureFileExists(
2233 AllowUsageIncrease(PathCost(file))->context(),
2234 file, &created));
2235 ASSERT_TRUE(created);
2236 ASSERT_EQ(0, ComputeTotalFileSize());
2237
2238 // Opening it, which shouldn't change the usage.
2239 ASSERT_EQ(base::PLATFORM_FILE_OK,
2240 ofu()->CreateOrOpen(
2241 AllowUsageIncrease(0)->context(), file,
2242 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2243 &file_handle, &created));
2244 ASSERT_EQ(0, ComputeTotalFileSize());
2245 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2246
2247 const int length = 33;
2248 ASSERT_EQ(base::PLATFORM_FILE_OK,
2249 ofu()->Truncate(
2250 AllowUsageIncrease(length)->context(), file, length));
2251 ASSERT_EQ(length, ComputeTotalFileSize());
2252
2253 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2254 ASSERT_EQ(base::PLATFORM_FILE_OK,
2255 ofu()->CreateOrOpen(
2256 AllowUsageIncrease(-length)->context(), file,
2257 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2258 &file_handle, &created));
2259 ASSERT_EQ(0, ComputeTotalFileSize());
2260 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2261
2262 // Extending the file again.
2263 ASSERT_EQ(base::PLATFORM_FILE_OK,
2264 ofu()->Truncate(
2265 AllowUsageIncrease(length)->context(), file, length));
2266 ASSERT_EQ(length, ComputeTotalFileSize());
2267
2268 // Opening it with TRUNCATED flag, which should truncate the file size.
2269 ASSERT_EQ(base::PLATFORM_FILE_OK,
2270 ofu()->CreateOrOpen(
2271 AllowUsageIncrease(-length)->context(), file,
2272 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2273 &file_handle, &created));
2274 ASSERT_EQ(0, ComputeTotalFileSize());
2275 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2276}
[email protected]c4e6f9c2012-09-09 17:42:102277
2278} // namespace fileapi