blob: 15b320cc5c68342bed361ed15673ca1f332a58b9 [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]fcc2d5f2011-05-23 22:06:2620#include "webkit/fileapi/file_system_test_helper.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]e7e46732012-01-05 11:45:5523#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4924#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5625#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1626#include "webkit/quota/mock_special_storage_policy.h"
27#include "webkit/quota/quota_manager.h"
28#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3229
30using namespace fileapi;
31
32namespace {
33
[email protected]d4905e2e2011-05-13 21:56:3234bool FileExists(const FilePath& path) {
35 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
36}
37
[email protected]81b7f662011-05-26 00:54:4638int64 GetSize(const FilePath& path) {
39 int64 size;
40 EXPECT_TRUE(file_util::GetFileSize(path, &size));
41 return size;
42}
43
[email protected]d4905e2e2011-05-13 21:56:3244// After a move, the dest exists and the source doesn't.
45// After a copy, both source and dest exist.
46struct CopyMoveTestCaseRecord {
47 bool is_copy_not_move;
48 const char source_path[64];
49 const char dest_path[64];
50 bool cause_overwrite;
51};
52
53const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
54 // This is the combinatoric set of:
55 // rename vs. same-name
56 // different directory vs. same directory
57 // overwrite vs. no-overwrite
58 // copy vs. move
59 // We can never be called with source and destination paths identical, so
60 // those cases are omitted.
61 {true, "dir0/file0", "dir0/file1", false},
62 {false, "dir0/file0", "dir0/file1", false},
63 {true, "dir0/file0", "dir0/file1", true},
64 {false, "dir0/file0", "dir0/file1", true},
65
66 {true, "dir0/file0", "dir1/file0", false},
67 {false, "dir0/file0", "dir1/file0", false},
68 {true, "dir0/file0", "dir1/file0", true},
69 {false, "dir0/file0", "dir1/file0", true},
70 {true, "dir0/file0", "dir1/file1", false},
71 {false, "dir0/file0", "dir1/file1", false},
72 {true, "dir0/file0", "dir1/file1", true},
73 {false, "dir0/file0", "dir1/file1", true},
74};
75
[email protected]fcc2d5f2011-05-23 22:06:2676struct OriginEnumerationTestRecord {
77 std::string origin_url;
78 bool has_temporary;
79 bool has_persistent;
80};
81
82const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
83 {"http://example.com", false, true},
84 {"http://example1.com", true, false},
85 {"https://example1.com", true, true},
86 {"file://", false, true},
87 {"http://example.com:8000", false, true},
88};
89
[email protected]d4905e2e2011-05-13 21:56:3290} // namespace (anonymous)
91
92// TODO(ericu): The vast majority of this and the other FSFU subclass tests
93// could theoretically be shared. It would basically be a FSFU interface
94// compliance test, and only the subclass-specific bits that look into the
95// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:4996class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:3297 public:
[email protected]7878ece2011-09-05 11:41:4998 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:2699 : origin_(GURL("http://www.example.com")),
100 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03101 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16102 test_helper_(origin_, type_),
103 quota_status_(quota::kQuotaStatusUnknown),
104 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32105 }
106
107 void SetUp() {
108 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
109
[email protected]e7e46732012-01-05 11:45:55110 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
111 new quota::MockSpecialStoragePolicy();
112
[email protected]0c5ebe32011-08-19 22:37:16113 quota_manager_ = new quota::QuotaManager(
114 false /* is_incognito */,
115 data_dir_.path(),
116 base::MessageLoopProxy::current(),
117 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55118 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16119
120 // Every time we create a new helper, it creates another context, which
121 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49122 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16123 file_system_context_ = new FileSystemContext(
124 base::MessageLoopProxy::current(),
125 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55126 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16127 quota_manager_->proxy(),
128 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55129 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16130
[email protected]7878ece2011-09-05 11:41:49131 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
[email protected]e7e46732012-01-05 11:45:55132 file_system_context_->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16133
134 test_helper_.SetUp(file_system_context_.get(),
[email protected]3cfc10f2012-05-24 01:20:41135 obfuscated_file_util_);
[email protected]d4905e2e2011-05-13 21:56:32136 }
137
[email protected]e7e46732012-01-05 11:45:55138 void TearDown() {
139 quota_manager_ = NULL;
140 test_helper_.TearDown();
141 }
142
[email protected]294dd0312012-05-11 07:35:13143 scoped_ptr<FileSystemOperationContext> LimitedContext(
144 int64 allowed_bytes_growth) {
145 scoped_ptr<FileSystemOperationContext> context(
146 test_helper_.NewOperationContext());
147 context->set_allowed_bytes_growth(allowed_bytes_growth);
148 return context.Pass();
149 }
150
151 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
152 return LimitedContext(kint64max);
153 }
154
[email protected]0c5ebe32011-08-19 22:37:16155 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
156 FileSystemOperationContext* context;
157 if (helper)
158 context = helper->NewOperationContext();
159 else
160 context = test_helper_.NewOperationContext();
161 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]d4905e2e2011-05-13 21:56:32162 return context;
163 }
164
[email protected]0c5ebe32011-08-19 22:37:16165 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49166 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16167 // Use this for tests which need to run in multiple origins; we need a test
168 // helper per origin.
169 FileSystemTestOriginHelper* NewHelper(
170 const GURL& origin, fileapi::FileSystemType type) {
171 FileSystemTestOriginHelper* helper =
172 new FileSystemTestOriginHelper(origin, type);
173
174 helper->SetUp(file_system_context_.get(),
[email protected]3cfc10f2012-05-24 01:20:41175 obfuscated_file_util_);
[email protected]0c5ebe32011-08-19 22:37:16176 return helper;
177 }
178
[email protected]7878ece2011-09-05 11:41:49179 ObfuscatedFileUtil* ofu() {
[email protected]3cfc10f2012-05-24 01:20:41180 return obfuscated_file_util_;
[email protected]d4905e2e2011-05-13 21:56:32181 }
182
[email protected]6b931152011-05-20 21:02:35183 const FilePath& test_directory() const {
184 return data_dir_.path();
185 }
186
[email protected]0c5ebe32011-08-19 22:37:16187 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26188 return origin_;
189 }
190
191 fileapi::FileSystemType type() const {
192 return type_;
193 }
194
[email protected]294dd0312012-05-11 07:35:13195 int64 ComputeTotalFileSize() {
196 return test_helper_.ComputeCurrentOriginUsage() -
197 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
198 }
199
[email protected]0c5ebe32011-08-19 22:37:16200 void GetUsageFromQuotaManager() {
201 quota_manager_->GetUsageAndQuota(
202 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03203 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
204 weak_factory_.GetWeakPtr()));
[email protected]0c5ebe32011-08-19 22:37:16205 MessageLoop::current()->RunAllPending();
206 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
207 }
208
209 void RevokeUsageCache() {
210 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
[email protected]022d2702012-05-14 16:04:26211 file_util::Delete(test_helper_.GetUsageCachePath(), false);
212 }
213
214 int64 SizeByQuotaUtil() {
215 return test_helper_.GetCachedOriginUsage();
[email protected]0c5ebe32011-08-19 22:37:16216 }
217
218 int64 SizeInUsageFile() {
[email protected]022d2702012-05-14 16:04:26219 return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath());
[email protected]0c5ebe32011-08-19 22:37:16220 }
221
222 int64 usage() const { return usage_; }
223
[email protected]08f8feb2012-02-26 11:53:50224 FileSystemPath CreatePathFromUTF8(const std::string& path) {
225 return test_helper_.CreatePathFromUTF8(path);
226 }
227
[email protected]294dd0312012-05-11 07:35:13228 int64 PathCost(const FileSystemPath& path) {
229 return ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path());
230 }
231
[email protected]08f8feb2012-02-26 11:53:50232 FileSystemPath CreatePath(const FilePath& path) {
233 return test_helper_.CreatePath(path);
234 }
235
[email protected]0c5ebe32011-08-19 22:37:16236 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
237 EXPECT_EQ(quota::kQuotaStatusOk, status);
238 quota_status_ = status;
239 usage_ = usage;
240 }
241
[email protected]d4905e2e2011-05-13 21:56:32242 void CheckFileAndCloseHandle(
[email protected]08f8feb2012-02-26 11:53:50243 const FileSystemPath& virtual_path, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16244 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32245 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49246 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32247 context.get(), virtual_path, &local_path));
248
249 base::PlatformFileInfo file_info0;
250 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49251 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32252 context.get(), virtual_path, &file_info0, &data_path));
253 EXPECT_EQ(data_path, local_path);
254 EXPECT_TRUE(FileExists(data_path));
255 EXPECT_EQ(0, GetSize(data_path));
256
257 const char data[] = "test data";
258 const int length = arraysize(data) - 1;
259
260 if (base::kInvalidPlatformFileValue == file_handle) {
261 bool created = true;
262 PlatformFileError error;
263 file_handle = base::CreatePlatformFile(
264 data_path,
265 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
266 &created,
267 &error);
[email protected]81b7f662011-05-26 00:54:46268 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32269 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
270 EXPECT_FALSE(created);
271 }
272 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
273 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
274
275 base::PlatformFileInfo file_info1;
276 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16277 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49278 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32279 context.get(), virtual_path, &file_info1, &data_path));
280 EXPECT_EQ(data_path, local_path);
281
282 EXPECT_FALSE(file_info0.is_directory);
283 EXPECT_FALSE(file_info1.is_directory);
284 EXPECT_FALSE(file_info0.is_symbolic_link);
285 EXPECT_FALSE(file_info1.is_symbolic_link);
286 EXPECT_EQ(0, file_info0.size);
287 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32288 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32289
[email protected]0c5ebe32011-08-19 22:37:16290 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49291 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32292 context.get(), virtual_path, length * 2));
293 EXPECT_EQ(length * 2, GetSize(data_path));
294
[email protected]0c5ebe32011-08-19 22:37:16295 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49296 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]0c5ebe32011-08-19 22:37:16297 context.get(), virtual_path, 0));
298 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32299 }
300
301 void ValidateTestDirectory(
[email protected]08f8feb2012-02-26 11:53:50302 const FileSystemPath& root_path,
[email protected]89ee4272011-05-16 18:45:17303 const std::set<FilePath::StringType>& files,
304 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32305 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17306 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32307 for (iter = files.begin(); iter != files.end(); ++iter) {
308 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16309 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32310 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49311 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17312 context.get(), root_path.Append(*iter),
[email protected]d4905e2e2011-05-13 21:56:32313 &created));
314 ASSERT_FALSE(created);
315 }
316 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16317 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49318 EXPECT_TRUE(ofu()->DirectoryExists(context.get(),
[email protected]89ee4272011-05-16 18:45:17319 root_path.Append(*iter)));
[email protected]d4905e2e2011-05-13 21:56:32320 }
321 }
322
[email protected]294dd0312012-05-11 07:35:13323 class UsageVerifyHelper {
324 public:
325 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
326 FileSystemTestOriginHelper* test_helper,
327 int64 expected_usage)
328 : context_(context.Pass()),
329 test_helper_(test_helper),
330 expected_usage_(expected_usage) {}
331
332 ~UsageVerifyHelper() {
333 Check();
334 }
335
336 FileSystemOperationContext* context() {
337 return context_.get();
338 }
339
340 private:
341 void Check() {
342 ASSERT_EQ(expected_usage_,
343 test_helper_->GetCachedOriginUsage());
344 }
345
346 scoped_ptr<FileSystemOperationContext> context_;
347 FileSystemTestOriginHelper* test_helper_;
348 int64 growth_;
349 int64 expected_usage_;
350 };
351
352 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
353 int64 usage = test_helper_.GetCachedOriginUsage();
354 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
355 LimitedContext(requested_growth),
356 &test_helper_, usage + requested_growth));
357 }
358
359 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
360 int64 usage = test_helper_.GetCachedOriginUsage();
361 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
362 LimitedContext(requested_growth - 1), &test_helper_, usage));
363 }
364
[email protected]d4905e2e2011-05-13 21:56:32365 void FillTestDirectory(
[email protected]08f8feb2012-02-26 11:53:50366 const FileSystemPath& root_path,
[email protected]89ee4272011-05-16 18:45:17367 std::set<FilePath::StringType>* files,
368 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32369 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16370 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32371 std::vector<base::FileUtilProxy::Entry> entries;
372 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55373 FileUtilHelper::ReadDirectory(
374 context.get(), ofu(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32375 EXPECT_EQ(0UL, entries.size());
376
377 files->clear();
[email protected]89ee4272011-05-16 18:45:17378 files->insert(FILE_PATH_LITERAL("first"));
379 files->insert(FILE_PATH_LITERAL("second"));
380 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32381 directories->clear();
[email protected]89ee4272011-05-16 18:45:17382 directories->insert(FILE_PATH_LITERAL("fourth"));
383 directories->insert(FILE_PATH_LITERAL("fifth"));
384 directories->insert(FILE_PATH_LITERAL("sixth"));
385 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32386 for (iter = files->begin(); iter != files->end(); ++iter) {
387 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16388 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32389 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49390 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50391 context.get(),
392 root_path.Append(*iter),
393 &created));
[email protected]d4905e2e2011-05-13 21:56:32394 ASSERT_TRUE(created);
395 }
396 for (iter = directories->begin(); iter != directories->end(); ++iter) {
397 bool exclusive = true;
398 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16399 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32400 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49401 ofu()->CreateDirectory(
[email protected]89ee4272011-05-16 18:45:17402 context.get(), root_path.Append(*iter), exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32403 }
404 ValidateTestDirectory(root_path, *files, *directories);
405 }
406
[email protected]08f8feb2012-02-26 11:53:50407 void TestReadDirectoryHelper(const FileSystemPath& root_path) {
[email protected]89ee4272011-05-16 18:45:17408 std::set<FilePath::StringType> files;
409 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:32410 FillTestDirectory(root_path, &files, &directories);
411
412 scoped_ptr<FileSystemOperationContext> context;
413 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16414 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32415 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55416 FileUtilHelper::ReadDirectory(
417 context.get(), ofu(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32418 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
419 EXPECT_EQ(files.size() + directories.size(), entries.size());
420 for (entry_iter = entries.begin(); entry_iter != entries.end();
421 ++entry_iter) {
422 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17423 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32424 if (iter != files.end()) {
425 EXPECT_FALSE(entry.is_directory);
426 files.erase(iter);
427 continue;
428 }
[email protected]89ee4272011-05-16 18:45:17429 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32430 EXPECT_FALSE(directories.end() == iter);
431 EXPECT_TRUE(entry.is_directory);
432 directories.erase(iter);
433 }
434 }
435
[email protected]08f8feb2012-02-26 11:53:50436 void TestTouchHelper(const FileSystemPath& path, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41437 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32438 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16439
[email protected]2517cfa2011-08-25 05:12:41440 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32441 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49442 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32443 context.get(), path, last_access_time, last_modified_time));
444 FilePath local_path;
445 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16446 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49447 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32448 context.get(), path, &file_info, &local_path));
449 // We compare as time_t here to lower our resolution, to avoid false
450 // negatives caused by conversion to the local filesystem's native
451 // representation and back.
452 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
453
[email protected]0c5ebe32011-08-19 22:37:16454 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32455 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41456 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32457 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49458 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32459 context.get(), path, last_access_time, last_modified_time));
[email protected]0c5ebe32011-08-19 22:37:16460 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49461 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32462 context.get(), path, &file_info, &local_path));
463 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49464 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41465 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32466 }
467
[email protected]81b7f662011-05-26 00:54:46468 void TestCopyInForeignFileHelper(bool overwrite) {
469 ScopedTempDir source_dir;
470 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]08f8feb2012-02-26 11:53:50471 FilePath root_file_path = source_dir.path();
472 FilePath src_file_path = root_file_path.AppendASCII("file_name");
473 FileSystemPath dest_path = CreatePathFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46474 int64 src_file_length = 87;
475
476 base::PlatformFileError error_code;
477 bool created = false;
478 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
479 base::PlatformFile file_handle =
480 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50481 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46482 EXPECT_TRUE(created);
483 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
484 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
485 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
486 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
487
488 scoped_ptr<FileSystemOperationContext> context;
489
490 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16491 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46492 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49493 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]81b7f662011-05-26 00:54:46494 EXPECT_TRUE(created);
495 }
496
[email protected]0c5ebe32011-08-19 22:37:16497 const int64 path_cost =
[email protected]08f8feb2012-02-26 11:53:50498 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path());
[email protected]0c5ebe32011-08-19 22:37:16499 if (!overwrite) {
500 // Verify that file creation requires sufficient quota for the path.
501 context.reset(NewContext(NULL));
502 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
503 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13504 ofu()->CopyInForeignFile(context.get(),
505 src_file_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16506 }
507
508 context.reset(NewContext(NULL));
509 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46510 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13511 ofu()->CopyInForeignFile(context.get(),
512 src_file_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16513
514 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49515 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16516 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49517 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16518 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46519 base::PlatformFileInfo file_info;
520 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49521 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]81b7f662011-05-26 00:54:46522 context.get(), dest_path, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50523 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46524 EXPECT_TRUE(FileExists(data_path));
525 EXPECT_EQ(src_file_length, GetSize(data_path));
526
527 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49528 ofu()->DeleteFile(context.get(), dest_path));
[email protected]81b7f662011-05-26 00:54:46529 }
530
[email protected]08f8feb2012-02-26 11:53:50531 void ClearTimestamp(const FileSystemPath& path) {
[email protected]fad625e2f2011-12-08 05:38:03532 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
533 EXPECT_EQ(base::PLATFORM_FILE_OK,
534 ofu()->Touch(context.get(), path, base::Time(), base::Time()));
535 EXPECT_EQ(base::Time(), GetModifiedTime(path));
536 }
537
[email protected]08f8feb2012-02-26 11:53:50538 base::Time GetModifiedTime(const FileSystemPath& path) {
[email protected]fad625e2f2011-12-08 05:38:03539 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
540 FilePath data_path;
541 base::PlatformFileInfo file_info;
542 context.reset(NewContext(NULL));
543 EXPECT_EQ(base::PLATFORM_FILE_OK,
544 ofu()->GetFileInfo(context.get(), path, &file_info, &data_path));
545 return file_info.last_modified;
546 }
547
[email protected]08f8feb2012-02-26 11:53:50548 void TestDirectoryTimestampHelper(const FileSystemPath& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03549 bool copy,
550 bool overwrite) {
551 scoped_ptr<FileSystemOperationContext> context;
[email protected]08f8feb2012-02-26 11:53:50552 const FileSystemPath src_dir_path(base_dir.AppendASCII("foo_dir"));
553 const FileSystemPath dest_dir_path(base_dir.AppendASCII("bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03554
[email protected]08f8feb2012-02-26 11:53:50555 const FileSystemPath src_file_path(src_dir_path.AppendASCII("hoge"));
556 const FileSystemPath dest_file_path(dest_dir_path.AppendASCII("fuga"));
[email protected]fad625e2f2011-12-08 05:38:03557
558 context.reset(NewContext(NULL));
559 EXPECT_EQ(base::PLATFORM_FILE_OK,
560 ofu()->CreateDirectory(context.get(), src_dir_path, true, true));
561 context.reset(NewContext(NULL));
562 EXPECT_EQ(base::PLATFORM_FILE_OK,
563 ofu()->CreateDirectory(context.get(), dest_dir_path, true, true));
564
565 bool created = false;
566 context.reset(NewContext(NULL));
567 EXPECT_EQ(base::PLATFORM_FILE_OK,
568 ofu()->EnsureFileExists(context.get(), src_file_path, &created));
569 if (overwrite) {
570 context.reset(NewContext(NULL));
571 EXPECT_EQ(base::PLATFORM_FILE_OK,
572 ofu()->EnsureFileExists(context.get(),
573 dest_file_path, &created));
574 }
575
576 ClearTimestamp(src_dir_path);
577 ClearTimestamp(dest_dir_path);
578 context.reset(NewContext(NULL));
579 EXPECT_EQ(base::PLATFORM_FILE_OK,
580 ofu()->CopyOrMoveFile(context.get(),
581 src_file_path, dest_file_path,
582 copy));
583
584 if (copy)
585 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path));
586 else
587 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path));
588 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path));
589 }
590
[email protected]ecdfd6c52012-04-11 13:35:44591 int64 ComputeCurrentUsage() {
592 return test_helper().ComputeCurrentOriginUsage() -
593 test_helper().ComputeCurrentDirectoryDatabaseUsage();
594 }
595
[email protected]45ea0fbbb2012-02-27 22:28:49596 const FileSystemTestOriginHelper& test_helper() const { return test_helper_; }
597
[email protected]d4905e2e2011-05-13 21:56:32598 private:
599 ScopedTempDir data_dir_;
[email protected]3cfc10f2012-05-24 01:20:41600 ObfuscatedFileUtil* obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16601 scoped_refptr<quota::QuotaManager> quota_manager_;
602 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26603 GURL origin_;
604 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03605 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26606 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16607 quota::QuotaStatusCode quota_status_;
608 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32609
[email protected]7878ece2011-09-05 11:41:49610 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32611};
612
[email protected]7878ece2011-09-05 11:41:49613TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32614 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
615 bool created;
[email protected]08f8feb2012-02-26 11:53:50616 FileSystemPath path = CreatePathFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16617 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32618 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
619
620 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49621 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32622 context.get(), path, file_flags, &file_handle,
623 &created));
624
[email protected]0c5ebe32011-08-19 22:37:16625 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32626 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49627 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32628
[email protected]08f8feb2012-02-26 11:53:50629 path = CreatePathFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32630
[email protected]0c5ebe32011-08-19 22:37:16631 // Verify that file creation requires sufficient quota for the path.
632 context.reset(NewContext(NULL));
633 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50634 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16635 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49636 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16637 context.get(), path, file_flags, &file_handle, &created));
638
639 context.reset(NewContext(NULL));
640 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50641 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()));
[email protected]d4905e2e2011-05-13 21:56:32642 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49643 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32644 context.get(), path, file_flags, &file_handle, &created));
645 ASSERT_TRUE(created);
646 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
647
648 CheckFileAndCloseHandle(path, file_handle);
649
[email protected]0c5ebe32011-08-19 22:37:16650 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32651 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49652 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32653 context.get(), path, &local_path));
654 EXPECT_TRUE(file_util::PathExists(local_path));
655
[email protected]0c5ebe32011-08-19 22:37:16656 // Verify that deleting a file isn't stopped by zero quota, and that it frees
657 // up quote from its path.
658 context.reset(NewContext(NULL));
659 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32660 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49661 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32662 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]08f8feb2012-02-26 11:53:50663 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()),
[email protected]0c5ebe32011-08-19 22:37:16664 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32665
[email protected]0c5ebe32011-08-19 22:37:16666 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32667 bool exclusive = true;
668 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:50669 FileSystemPath directory_path = CreatePathFromUTF8(
670 "series/of/directories");
[email protected]d4905e2e2011-05-13 21:56:32671 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49672 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32673 context.get(), directory_path, exclusive, recursive));
674
[email protected]0c5ebe32011-08-19 22:37:16675 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32676 file_handle = base::kInvalidPlatformFileValue;
677 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49678 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32679 context.get(), path, file_flags, &file_handle, &created));
680 ASSERT_TRUE(created);
681 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
682
683 CheckFileAndCloseHandle(path, file_handle);
684
[email protected]0c5ebe32011-08-19 22:37:16685 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49686 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32687 context.get(), path, &local_path));
688 EXPECT_TRUE(file_util::PathExists(local_path));
689
[email protected]0c5ebe32011-08-19 22:37:16690 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32691 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49692 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32693 EXPECT_FALSE(file_util::PathExists(local_path));
694}
695
[email protected]7878ece2011-09-05 11:41:49696TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32697 bool created = false;
[email protected]08f8feb2012-02-26 11:53:50698 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16699 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32700
701 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49702 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32703
[email protected]0c5ebe32011-08-19 22:37:16704 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32705 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49706 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32707 ASSERT_TRUE(created);
708
[email protected]0c5ebe32011-08-19 22:37:16709 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32710 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49711 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32712 context.get(), path, &local_path));
713 EXPECT_EQ(0, GetSize(local_path));
714
[email protected]0c5ebe32011-08-19 22:37:16715 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32717 context.get(), path, 10));
718 EXPECT_EQ(10, GetSize(local_path));
719
[email protected]0c5ebe32011-08-19 22:37:16720 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49721 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32722 context.get(), path, 1));
723 EXPECT_EQ(1, GetSize(local_path));
724
[email protected]0c5ebe32011-08-19 22:37:16725 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49726 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16727 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49728 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32729}
730
[email protected]ecdfd6c52012-04-11 13:35:44731TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
732 bool created = false;
733 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44734
735 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13736 ofu()->EnsureFileExists(
737 AllowUsageIncrease(PathCost(path))->context(),
738 path, &created));
[email protected]ecdfd6c52012-04-11 13:35:44739 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13740 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44741
[email protected]ecdfd6c52012-04-11 13:35:44742 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13743 ofu()->Truncate(
744 AllowUsageIncrease(1020)->context(),
745 path, 1020));
746 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44747
[email protected]ecdfd6c52012-04-11 13:35:44748 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13749 ofu()->Truncate(
750 AllowUsageIncrease(-1020)->context(),
751 path, 0));
752 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44753
[email protected]ecdfd6c52012-04-11 13:35:44754 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13755 ofu()->Truncate(
756 DisallowUsageIncrease(1021)->context(),
757 path, 1021));
758 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44759
[email protected]ecdfd6c52012-04-11 13:35:44760 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13761 ofu()->Truncate(
762 AllowUsageIncrease(1020)->context(),
763 path, 1020));
764 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44765
[email protected]ecdfd6c52012-04-11 13:35:44766 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13767 ofu()->Truncate(
768 AllowUsageIncrease(0)->context(),
769 path, 1020));
770 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44771
[email protected]294dd0312012-05-11 07:35:13772 // quota exceeded
773 {
774 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
775 helper->context()->set_allowed_bytes_growth(
776 helper->context()->allowed_bytes_growth() - 1);
777 EXPECT_EQ(base::PLATFORM_FILE_OK,
778 ofu()->Truncate(helper->context(), path, 1019));
779 ASSERT_EQ(1019, ComputeTotalFileSize());
780 }
[email protected]ecdfd6c52012-04-11 13:35:44781
782 // Delete backing file to make following truncation fail.
[email protected]ecdfd6c52012-04-11 13:35:44783 FilePath local_path;
784 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13785 ofu()->GetLocalFilePath(
786 UnlimitedContext().get(),
787 path, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44788 ASSERT_FALSE(local_path.empty());
789 ASSERT_TRUE(file_util::Delete(local_path, false));
790
[email protected]ecdfd6c52012-04-11 13:35:44791 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13792 ofu()->Truncate(
793 LimitedContext(1234).get(),
794 path, 1234));
795 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44796}
797
[email protected]7878ece2011-09-05 11:41:49798TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]08f8feb2012-02-26 11:53:50799 FileSystemPath path = CreatePathFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32800 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16801 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32802 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49803 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32804 context.get(), path, &created));
805
[email protected]0c5ebe32011-08-19 22:37:16806 // Verify that file creation requires sufficient quota for the path.
807 context.reset(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:50808 path = CreatePathFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32809 created = false;
[email protected]0c5ebe32011-08-19 22:37:16810 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50811 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16812 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49813 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16814 ASSERT_FALSE(created);
815
816 context.reset(NewContext(NULL));
817 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50818 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()));
[email protected]d4905e2e2011-05-13 21:56:32819 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49820 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32821 ASSERT_TRUE(created);
822
823 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
824
[email protected]0c5ebe32011-08-19 22:37:16825 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32826 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49827 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32828 ASSERT_FALSE(created);
829
830 // Also test in a subdirectory.
[email protected]08f8feb2012-02-26 11:53:50831 path = CreatePathFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16832 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32833 bool exclusive = true;
834 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49835 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32836 context.get(), path.DirName(), exclusive, recursive));
837
[email protected]0c5ebe32011-08-19 22:37:16838 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32839 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49840 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32841 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16842 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49843 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16844 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49845 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32846}
847
[email protected]7878ece2011-09-05 11:41:49848TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16849 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32850
851 bool exclusive = false;
852 bool recursive = false;
[email protected]08f8feb2012-02-26 11:53:50853 FileSystemPath path = CreatePathFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49854 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32855 context.get(), path, exclusive, recursive));
856
[email protected]0c5ebe32011-08-19 22:37:16857 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32858 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49859 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32860
[email protected]08f8feb2012-02-26 11:53:50861 FileSystemPath root = CreatePathFromUTF8("");
[email protected]0c5ebe32011-08-19 22:37:16862 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49863 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16864 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49865 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16866 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49867 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32868
[email protected]0c5ebe32011-08-19 22:37:16869 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32870 exclusive = false;
871 recursive = true;
[email protected]7878ece2011-09-05 11:41:49872 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32873 context.get(), path, exclusive, recursive));
874
[email protected]0c5ebe32011-08-19 22:37:16875 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49876 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16877 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49878 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16879 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49880 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]0c5ebe32011-08-19 22:37:16881 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49882 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16883 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49884 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32885
886 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16887 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06888 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49889 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32890
891 base::PlatformFileInfo file_info;
892 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49893 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32894 context.get(), path, &file_info, &local_path));
895 EXPECT_TRUE(local_path.empty());
896 EXPECT_TRUE(file_info.is_directory);
897 EXPECT_FALSE(file_info.is_symbolic_link);
898
899 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16900 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49901 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32902 context.get(), path, exclusive, recursive));
903
904 exclusive = true;
905 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16906 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49907 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32908 context.get(), path, exclusive, recursive));
909
[email protected]0c5ebe32011-08-19 22:37:16910 // Verify that deleting a directory isn't stopped by zero quota, and that it
911 // frees up quota from its path.
912 context.reset(NewContext(NULL));
913 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32914 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49915 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]08f8feb2012-02-26 11:53:50916 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()),
[email protected]0c5ebe32011-08-19 22:37:16917 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32918
[email protected]08f8feb2012-02-26 11:53:50919 path = CreatePathFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:32920
[email protected]0c5ebe32011-08-19 22:37:16921 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49922 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16923 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49924 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16925 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49926 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
927 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32928 context.get(), path, &file_info, &local_path));
929
[email protected]0c5ebe32011-08-19 22:37:16930 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32931 exclusive = true;
932 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16933 context.reset(NewContext(NULL));
934 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50935 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1);
[email protected]7878ece2011-09-05 11:41:49936 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16937 context.get(), path, exclusive, recursive));
938
939 context.reset(NewContext(NULL));
940 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50941 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()));
[email protected]7878ece2011-09-05 11:41:49942 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32943 context.get(), path, exclusive, recursive));
944
[email protected]0c5ebe32011-08-19 22:37:16945 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49946 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16947 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49948 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32949
950 exclusive = true;
951 recursive = false;
[email protected]7878ece2011-09-05 11:41:49952 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32953 context.get(), path, exclusive, recursive));
954
955 exclusive = true;
956 recursive = false;
[email protected]08f8feb2012-02-26 11:53:50957 path = CreatePathFromUTF8("foo");
[email protected]7878ece2011-09-05 11:41:49958 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32959 context.get(), path, exclusive, recursive));
960
[email protected]08f8feb2012-02-26 11:53:50961 path = CreatePathFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:32962
[email protected]0c5ebe32011-08-19 22:37:16963 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49964 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16965 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49966 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32967
968 exclusive = true;
969 recursive = false;
[email protected]7878ece2011-09-05 11:41:49970 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32971 context.get(), path, exclusive, recursive));
972
[email protected]0c5ebe32011-08-19 22:37:16973 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49974 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16975 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49976 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32977
978 exclusive = true;
979 recursive = false;
[email protected]7878ece2011-09-05 11:41:49980 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32981 context.get(), path, exclusive, recursive));
982}
983
[email protected]7878ece2011-09-05 11:41:49984TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16985 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32986 bool exclusive = true;
987 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:50988 FileSystemPath path = CreatePathFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49989 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32990 context.get(), path, exclusive, recursive));
991 TestReadDirectoryHelper(path);
992}
993
[email protected]7878ece2011-09-05 11:41:49994TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]08f8feb2012-02-26 11:53:50995 TestReadDirectoryHelper(CreatePathFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:32996}
997
[email protected]7878ece2011-09-05 11:41:49998TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]08f8feb2012-02-26 11:53:50999 TestReadDirectoryHelper(CreatePathFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:321000}
1001
[email protected]7878ece2011-09-05 11:41:491002TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]08f8feb2012-02-26 11:53:501003 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161004 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321005
1006 bool created = false;
1007 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491008 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:321009 ASSERT_TRUE(created);
1010
[email protected]0c5ebe32011-08-19 22:37:161011 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321012 std::vector<base::FileUtilProxy::Entry> entries;
1013 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]a3938912012-03-27 14:00:551014 FileUtilHelper::ReadDirectory(
1015 context.get(), ofu(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:321016
[email protected]7878ece2011-09-05 11:41:491017 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:321018}
1019
[email protected]7878ece2011-09-05 11:41:491020TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]08f8feb2012-02-26 11:53:501021 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161022 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411023
1024 base::Time last_access_time = base::Time::Now();
1025 base::Time last_modified_time = base::Time::Now();
1026
1027 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321028 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491029 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:321030 context.get(), path, last_access_time, last_modified_time));
1031
[email protected]2517cfa2011-08-25 05:12:411032 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161033 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411034 bool created = false;
1035 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491036 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:411037 ASSERT_TRUE(created);
1038 TestTouchHelper(path, true);
1039
1040 // Now test a directory:
1041 context.reset(NewContext(NULL));
1042 bool exclusive = true;
1043 bool recursive = false;
[email protected]08f8feb2012-02-26 11:53:501044 path = CreatePathFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491045 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:411046 path, exclusive, recursive));
1047 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:161048}
1049
[email protected]7878ece2011-09-05 11:41:491050TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]08f8feb2012-02-26 11:53:501051 FileSystemPath path = CreatePathFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161052 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1053
[email protected]08f8feb2012-02-26 11:53:501054 path = CreatePathFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161055 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411056 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161057 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491058 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:411059 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161060 context->set_allowed_bytes_growth(1024);
1061 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491062 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:411063 EXPECT_TRUE(created);
[email protected]08f8feb2012-02-26 11:53:501064 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(
1065 path.internal_path());
[email protected]0c5ebe32011-08-19 22:37:161066 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1067
1068 context->set_allowed_bytes_growth(1024);
1069 bool exclusive = true;
1070 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:501071 path = CreatePathFromUTF8("directory/to/use");
[email protected]0c5ebe32011-08-19 22:37:161072 std::vector<FilePath::StringType> components;
[email protected]08f8feb2012-02-26 11:53:501073 path.internal_path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161074 path_cost = 0;
1075 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
1076 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491077 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:161078 FilePath(*iter));
1079 }
1080 context.reset(NewContext(NULL));
1081 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491082 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161083 context.get(), path, exclusive, recursive));
1084 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321085}
1086
[email protected]7878ece2011-09-05 11:41:491087TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]08f8feb2012-02-26 11:53:501088 FileSystemPath source_path = CreatePathFromUTF8("path0.txt");
1089 FileSystemPath dest_path = CreatePathFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161090 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321091
1092 bool is_copy_not_move = false;
1093 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491094 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321095 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:161096 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321097 is_copy_not_move = true;
1098 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491099 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321100 is_copy_not_move));
[email protected]08f8feb2012-02-26 11:53:501101 source_path = CreatePathFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321102 bool exclusive = true;
1103 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161104 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491105 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321106 context.get(), source_path.DirName(), exclusive, recursive));
1107 is_copy_not_move = false;
1108 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491109 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321110 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:161111 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321112 is_copy_not_move = true;
1113 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491114 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321115 is_copy_not_move));
1116}
1117
[email protected]7878ece2011-09-05 11:41:491118TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321119 const int64 kSourceLength = 5;
1120 const int64 kDestLength = 50;
1121
1122 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1123 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1124 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1125 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1126 test_case.is_copy_not_move);
1127 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1128 test_case.source_path);
1129 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1130 test_case.dest_path);
1131 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1132 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161133 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321134
1135 bool exclusive = false;
1136 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:501137 FileSystemPath source_path = CreatePathFromUTF8(test_case.source_path);
1138 FileSystemPath dest_path = CreatePathFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321139
[email protected]0c5ebe32011-08-19 22:37:161140 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491141 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321142 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161143 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491144 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321145 context.get(), dest_path.DirName(), exclusive, recursive));
1146
[email protected]d4905e2e2011-05-13 21:56:321147 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161148 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321149 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491150 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321151 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161152 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321153 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491154 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321155
1156 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161157 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321158 created = false;
1159 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491160 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321161 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161162 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321163 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491164 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321165 }
1166
[email protected]0c5ebe32011-08-19 22:37:161167 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491168 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:321169 source_path, dest_path, test_case.is_copy_not_move));
1170 if (test_case.is_copy_not_move) {
1171 base::PlatformFileInfo file_info;
1172 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161173 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491174 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321175 context.get(), source_path, &file_info, &local_path));
1176 EXPECT_EQ(kSourceLength, file_info.size);
1177 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491178 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:321179 } else {
1180 base::PlatformFileInfo file_info;
1181 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161182 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491183 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321184 context.get(), source_path, &file_info, &local_path));
1185 }
1186 base::PlatformFileInfo file_info;
1187 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491188 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321189 context.get(), dest_path, &file_info, &local_path));
1190 EXPECT_EQ(kSourceLength, file_info.size);
1191
1192 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491193 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321194 }
1195}
1196
[email protected]7878ece2011-09-05 11:41:491197TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]08f8feb2012-02-26 11:53:501198 FileSystemPath src_path = CreatePathFromUTF8("src path");
1199 FileSystemPath dest_path = CreatePathFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161200 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1201 bool created = false;
[email protected]7878ece2011-09-05 11:41:491202 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161203 context.get(), src_path, &created));
1204
1205 bool is_copy = true;
1206 // Copy, no overwrite.
1207 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501208 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161209 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491210 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161211 context.reset(NewContext(NULL));
1212 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501213 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()));
[email protected]0c5ebe32011-08-19 22:37:161214 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491215 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161216
1217 // Copy, with overwrite.
1218 context.reset(NewContext(NULL));
1219 context->set_allowed_bytes_growth(0);
1220 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491221 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161222}
1223
[email protected]7878ece2011-09-05 11:41:491224TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]08f8feb2012-02-26 11:53:501225 FileSystemPath src_path = CreatePathFromUTF8("src path");
1226 FileSystemPath dest_path = CreatePathFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161227 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1228 bool created = false;
[email protected]7878ece2011-09-05 11:41:491229 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161230 context.get(), src_path, &created));
1231
1232 bool is_copy = false;
1233 // Move, rename, no overwrite.
1234 context.reset(NewContext(NULL));
1235 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501236 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) -
1237 ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161238 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491239 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161240 context.reset(NewContext(NULL));
1241 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501242 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) -
1243 ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()));
[email protected]0c5ebe32011-08-19 22:37:161244 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491245 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161246
1247 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491248 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161249 context.get(), src_path, &created));
1250
1251 // Move, rename, with overwrite.
1252 context.reset(NewContext(NULL));
1253 context->set_allowed_bytes_growth(0);
1254 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491255 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161256}
1257
[email protected]7878ece2011-09-05 11:41:491258TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]08f8feb2012-02-26 11:53:501259 FileSystemPath src_path = CreatePathFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161260 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1261 bool created = false;
[email protected]7878ece2011-09-05 11:41:491262 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161263 context.get(), src_path, &created));
1264
1265 bool exclusive = true;
1266 bool recursive = false;
[email protected]08f8feb2012-02-26 11:53:501267 FileSystemPath dir_path = CreatePathFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161268 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491269 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161270 context.get(), dir_path, exclusive, recursive));
1271
[email protected]08f8feb2012-02-26 11:53:501272 FileSystemPath dest_path = dir_path.Append(src_path.internal_path());
[email protected]0c5ebe32011-08-19 22:37:161273
1274 bool is_copy = false;
1275 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1276 // Move, no rename, no overwrite.
1277 context.reset(NewContext(NULL));
1278 context->set_allowed_bytes_growth(allowed_bytes_growth);
1279 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491280 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161281 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1282
1283 // Move, no rename, with overwrite.
1284 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491285 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161286 context.get(), src_path, &created));
1287 context.reset(NewContext(NULL));
1288 context->set_allowed_bytes_growth(allowed_bytes_growth);
1289 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491290 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161291 EXPECT_EQ(
1292 allowed_bytes_growth +
[email protected]08f8feb2012-02-26 11:53:501293 ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()),
[email protected]0c5ebe32011-08-19 22:37:161294 context->allowed_bytes_growth());
1295}
1296
[email protected]7878ece2011-09-05 11:41:491297TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461298 TestCopyInForeignFileHelper(false /* overwrite */);
1299 TestCopyInForeignFileHelper(true /* overwrite */);
1300}
1301
[email protected]7878ece2011-09-05 11:41:491302TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161303 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:501304 FileSystemPath src_path = CreatePathFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321305 bool exclusive = true;
1306 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491307 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321308 context.get(), src_path, exclusive, recursive));
1309
[email protected]89ee4272011-05-16 18:45:171310 std::set<FilePath::StringType> files;
1311 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321312 FillTestDirectory(src_path, &files, &directories);
1313
[email protected]08f8feb2012-02-26 11:53:501314 FileSystemPath dest_path = CreatePathFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321315
[email protected]0c5ebe32011-08-19 22:37:161316 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491317 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161318 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321319 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]45ea0fbbb2012-02-27 22:28:491320 test_helper().SameFileUtilCopy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321321
1322 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161323 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491324 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161325 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491326 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161327 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321328 recursive = true;
1329 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]aa437fa2012-03-03 02:09:071330 FileUtilHelper::Delete(context.get(), ofu(),
1331 dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161332 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491333 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321334}
[email protected]6b931152011-05-20 21:02:351335
[email protected]7878ece2011-09-05 11:41:491336TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351337 ScopedTempDir source_dir;
1338 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1339 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1340 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1341
[email protected]f83b5b72012-01-27 10:26:561342 test::SetUpRegularTestCases(root_path);
[email protected]6b931152011-05-20 21:02:351343
[email protected]7878ece2011-09-05 11:41:491344 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351345
1346 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161347 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491348 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]f83b5b72012-01-27 10:26:561349 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]6b931152011-05-20 21:02:351350 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561351 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]6b931152011-05-20 21:02:351352 FilePath local_data_path = new_root.Append(test_case.path);
[email protected]d9034ed22012-02-10 02:04:401353 local_data_path = local_data_path.NormalizePathSeparators();
[email protected]0c5ebe32011-08-19 22:37:161354 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491355 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351356 FilePath data_path;
1357 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1358 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501359 ofu()->GetFileInfo(context.get(), CreatePath(FilePath(test_case.path)),
[email protected]7878ece2011-09-05 11:41:491360 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351361 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491362 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351363 } else {
1364 base::PlatformFileInfo platform_file_info;
1365 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1366 local_data_path.value());
1367 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1368 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1369 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1370 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161371 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351372 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491373 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1374 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351375 }
1376 }
1377}
[email protected]fcc2d5f2011-05-23 22:06:261378
[email protected]7878ece2011-09-05 11:41:491379TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1380 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1381 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161382 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261383 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161384 EXPECT_EQ(origin(), enumerator->Next());
1385 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1386 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1387 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261388 EXPECT_EQ(GURL(), enumerator->Next());
1389 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1390 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1391
1392 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161393 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261394
1395 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1396 SCOPED_TRACE(testing::Message() <<
1397 "Validating kOriginEnumerationTestRecords " << i);
1398 const OriginEnumerationTestRecord& record =
1399 kOriginEnumerationTestRecords[i];
1400 GURL origin_url(record.origin_url);
1401 origins_expected.insert(origin_url);
1402 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161403 scoped_ptr<FileSystemTestOriginHelper> helper(
1404 NewHelper(origin_url, kFileSystemTypeTemporary));
1405 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261406 bool created = false;
1407 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501408 ofu()->EnsureFileExists(
1409 context.get(),
1410 helper->CreatePathFromUTF8("file"),
1411 &created));
[email protected]fcc2d5f2011-05-23 22:06:261412 EXPECT_TRUE(created);
1413 }
1414 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161415 scoped_ptr<FileSystemTestOriginHelper> helper(
1416 NewHelper(origin_url, kFileSystemTypePersistent));
1417 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261418 bool created = false;
1419 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501420 ofu()->EnsureFileExists(
1421 context.get(),
1422 helper->CreatePathFromUTF8("file"),
1423 &created));
[email protected]fcc2d5f2011-05-23 22:06:261424 EXPECT_TRUE(created);
1425 }
1426 }
[email protected]7878ece2011-09-05 11:41:491427 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261428 EXPECT_TRUE(enumerator.get());
1429 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161430 GURL origin_url;
1431 while (!(origin_url = enumerator->Next()).is_empty()) {
1432 origins_found.insert(origin_url);
1433 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261434 bool found = false;
1435 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1436 ++i) {
1437 const OriginEnumerationTestRecord& record =
1438 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161439 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261440 continue;
1441 found = true;
1442 EXPECT_EQ(record.has_temporary,
1443 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1444 EXPECT_EQ(record.has_persistent,
1445 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1446 }
[email protected]0c5ebe32011-08-19 22:37:161447 // Deal with the default filesystem created by the test helper.
1448 if (!found && origin_url == origin()) {
1449 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1450 EXPECT_EQ(true,
1451 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181452 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161453 found = true;
1454 }
[email protected]fcc2d5f2011-05-23 22:06:261455 EXPECT_TRUE(found);
1456 }
1457
1458 std::set<GURL> diff;
1459 std::set_symmetric_difference(origins_expected.begin(),
1460 origins_expected.end(), origins_found.begin(), origins_found.end(),
1461 inserter(diff, diff.begin()));
1462 EXPECT_TRUE(diff.empty());
1463}
[email protected]0c5ebe32011-08-19 22:37:161464
[email protected]7878ece2011-09-05 11:41:491465TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161466 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1467
1468 int64 expected_quota = 0;
1469
[email protected]f83b5b72012-01-27 10:26:561470 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]0c5ebe32011-08-19 22:37:161471 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561472 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]08f8feb2012-02-26 11:53:501473 FilePath file_path(test_case.path);
1474 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161475 if (test_case.is_directory) {
1476 bool exclusive = true;
1477 bool recursive = false;
1478 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501479 ofu()->CreateDirectory(context.get(), CreatePath(file_path),
1480 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161481 } else {
1482 bool created = false;
1483 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501484 ofu()->EnsureFileExists(context.get(), CreatePath(file_path),
1485 &created));
[email protected]0c5ebe32011-08-19 22:37:161486 ASSERT_TRUE(created);
1487 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501488 ofu()->Truncate(context.get(),
1489 CreatePath(file_path),
1490 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161491 expected_quota += test_case.data_file_size;
1492 }
1493 }
[email protected]022d2702012-05-14 16:04:261494
1495 // Usually raw size in usage cache and the usage returned by QuotaUtil
1496 // should be same.
[email protected]0c5ebe32011-08-19 22:37:161497 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261498 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1499
[email protected]0c5ebe32011-08-19 22:37:161500 RevokeUsageCache();
1501 EXPECT_EQ(-1, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261502 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
1503
1504 // This should reconstruct the cache.
[email protected]0c5ebe32011-08-19 22:37:161505 GetUsageFromQuotaManager();
1506 EXPECT_EQ(expected_quota, SizeInUsageFile());
[email protected]022d2702012-05-14 16:04:261507 EXPECT_EQ(expected_quota, SizeByQuotaUtil());
[email protected]0c5ebe32011-08-19 22:37:161508 EXPECT_EQ(expected_quota, usage());
1509}
[email protected]34583332011-08-31 08:59:471510
[email protected]7878ece2011-09-05 11:41:491511TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]08f8feb2012-02-26 11:53:501512 const FileSystemPath kPath1 = CreatePathFromUTF8("hoge");
1513 const FileSystemPath kPath2 = CreatePathFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471514
1515 scoped_ptr<FileSystemOperationContext> context;
1516 base::PlatformFile file;
1517 base::PlatformFileInfo file_info;
1518 FilePath data_path;
1519 bool created = false;
1520
1521 // Create a non-empty file.
1522 context.reset(NewContext(NULL));
1523 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491524 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471525 EXPECT_TRUE(created);
1526 context.reset(NewContext(NULL));
1527 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491528 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471529 context.reset(NewContext(NULL));
1530 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491531 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471532 context.get(), kPath1, &file_info, &data_path));
1533 EXPECT_EQ(10, file_info.size);
1534
1535 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491536 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471537
1538 // Try to get file info of broken file.
1539 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491540 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471541 context.reset(NewContext(NULL));
1542 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491543 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471544 EXPECT_TRUE(created);
1545 context.reset(NewContext(NULL));
1546 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491547 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471548 context.get(), kPath1, &file_info, &data_path));
1549 EXPECT_EQ(0, file_info.size);
1550
1551 // Make another broken file to |kPath2|.
1552 context.reset(NewContext(NULL));
1553 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491554 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471555 EXPECT_TRUE(created);
1556
1557 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491558 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471559
1560 // Repair broken |kPath1|.
1561 context.reset(NewContext(NULL));
1562 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491563 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471564 base::Time::Now()));
1565 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491566 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471567 EXPECT_TRUE(created);
1568
1569 // Copy from sound |kPath1| to broken |kPath2|.
1570 context.reset(NewContext(NULL));
1571 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491572 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501573 true /* copy */));
[email protected]34583332011-08-31 08:59:471574
[email protected]7878ece2011-09-05 11:41:491575 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471576 context.reset(NewContext(NULL));
1577 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491578 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471579 context.get(), kPath1,
1580 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1581 &file, &created));
1582 EXPECT_TRUE(created);
1583 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1584 EXPECT_EQ(0, file_info.size);
1585 EXPECT_TRUE(base::ClosePlatformFile(file));
1586}
[email protected]9dfdc0e32011-09-02 06:07:441587
[email protected]7878ece2011-09-05 11:41:491588TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]08f8feb2012-02-26 11:53:501589 const FileSystemPath kPath[] = {
1590 CreatePathFromUTF8("foo"),
1591 CreatePathFromUTF8("bar"),
1592 CreatePathFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441593 };
[email protected]08f8feb2012-02-26 11:53:501594 const FileSystemPath empty_path = CreatePath(FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441595 scoped_ptr<FileSystemOperationContext> context;
1596
1597 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1598 bool created = false;
1599 context.reset(NewContext(NULL));
1600 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491601 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441602 EXPECT_TRUE(created);
1603 }
1604
1605 context.reset(NewContext(NULL));
1606 std::vector<base::FileUtilProxy::Entry> entries;
1607 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551608 FileUtilHelper::ReadDirectory(
1609 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441610 EXPECT_EQ(3u, entries.size());
1611
1612 context.reset(NewContext(NULL));
1613 FilePath local_path;
1614 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491615 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441616 EXPECT_TRUE(file_util::Delete(local_path, false));
1617
1618 context.reset(NewContext(NULL));
1619 entries.clear();
1620 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551621 FileUtilHelper::ReadDirectory(
1622 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441623 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1624}
[email protected]fad625e2f2011-12-08 05:38:031625
1626TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1627 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:501628 const FileSystemPath dir_path = CreatePathFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031629
1630 // Create working directory.
1631 EXPECT_EQ(base::PLATFORM_FILE_OK,
1632 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1633
1634 // EnsureFileExists, create case.
[email protected]08f8feb2012-02-26 11:53:501635 FileSystemPath path(dir_path.AppendASCII("EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031636 bool created = false;
1637 ClearTimestamp(dir_path);
1638 context.reset(NewContext(NULL));
1639 EXPECT_EQ(base::PLATFORM_FILE_OK,
1640 ofu()->EnsureFileExists(context.get(), path, &created));
1641 EXPECT_TRUE(created);
1642 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1643
1644 // non create case.
1645 created = true;
1646 ClearTimestamp(dir_path);
1647 context.reset(NewContext(NULL));
1648 EXPECT_EQ(base::PLATFORM_FILE_OK,
1649 ofu()->EnsureFileExists(context.get(), path, &created));
1650 EXPECT_FALSE(created);
1651 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1652
1653 // fail case.
1654 path = dir_path.AppendASCII("EnsureFileExists_dir");
1655 context.reset(NewContext(NULL));
1656 EXPECT_EQ(base::PLATFORM_FILE_OK,
1657 ofu()->CreateDirectory(context.get(), path, false, false));
1658
1659 ClearTimestamp(dir_path);
1660 context.reset(NewContext(NULL));
1661 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
1662 ofu()->EnsureFileExists(context.get(), path, &created));
1663 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1664
1665 // CreateOrOpen, create case.
1666 path = dir_path.AppendASCII("CreateOrOpen_file");
1667 PlatformFile file_handle = base::kInvalidPlatformFileValue;
1668 created = false;
1669 ClearTimestamp(dir_path);
1670 context.reset(NewContext(NULL));
1671 EXPECT_EQ(base::PLATFORM_FILE_OK,
1672 ofu()->CreateOrOpen(
1673 context.get(), path,
1674 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1675 &file_handle, &created));
1676 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1677 EXPECT_TRUE(created);
1678 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1679 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1680
1681 // open case.
1682 file_handle = base::kInvalidPlatformFileValue;
1683 created = true;
1684 ClearTimestamp(dir_path);
1685 context.reset(NewContext(NULL));
1686 EXPECT_EQ(base::PLATFORM_FILE_OK,
1687 ofu()->CreateOrOpen(
1688 context.get(), path,
1689 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1690 &file_handle, &created));
1691 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1692 EXPECT_FALSE(created);
1693 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1694 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1695
1696 // fail case
1697 file_handle = base::kInvalidPlatformFileValue;
1698 ClearTimestamp(dir_path);
1699 context.reset(NewContext(NULL));
1700 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1701 ofu()->CreateOrOpen(
1702 context.get(), path,
1703 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1704 &file_handle, &created));
1705 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
1706 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1707
1708 // CreateDirectory, create case.
1709 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
1710 path = dir_path.AppendASCII("CreateDirectory_dir");
[email protected]08f8feb2012-02-26 11:53:501711 FileSystemPath subdir_path(path.AppendASCII("subdir"));
[email protected]fad625e2f2011-12-08 05:38:031712 ClearTimestamp(dir_path);
1713 context.reset(NewContext(NULL));
1714 EXPECT_EQ(base::PLATFORM_FILE_OK,
1715 ofu()->CreateDirectory(context.get(), subdir_path,
1716 true /* exclusive */, true /* recursive */));
1717 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1718
1719 // create subdir case.
1720 // Creating CreateDirectory_dir/subdir2.
1721 subdir_path = path.AppendASCII("subdir2");
1722 ClearTimestamp(dir_path);
1723 ClearTimestamp(path);
1724 context.reset(NewContext(NULL));
1725 EXPECT_EQ(base::PLATFORM_FILE_OK,
1726 ofu()->CreateDirectory(context.get(), subdir_path,
1727 true /* exclusive */, true /* recursive */));
1728 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1729 EXPECT_NE(base::Time(), GetModifiedTime(path));
1730
1731 // fail case.
1732 path = dir_path.AppendASCII("CreateDirectory_dir");
1733 ClearTimestamp(dir_path);
1734 context.reset(NewContext(NULL));
1735 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1736 ofu()->CreateDirectory(context.get(), path,
1737 true /* exclusive */, true /* recursive */));
1738 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1739
1740 // CopyInForeignFile, create case.
1741 path = dir_path.AppendASCII("CopyInForeignFile_file");
[email protected]08f8feb2012-02-26 11:53:501742 FileSystemPath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031743 context.reset(NewContext(NULL));
1744 EXPECT_EQ(base::PLATFORM_FILE_OK,
1745 ofu()->EnsureFileExists(context.get(), src_path, &created));
1746 EXPECT_TRUE(created);
1747 FilePath src_local_path;
1748 context.reset(NewContext(NULL));
1749 EXPECT_EQ(base::PLATFORM_FILE_OK,
1750 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1751
1752 ClearTimestamp(dir_path);
1753 context.reset(NewContext(NULL));
1754 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501755 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131756 src_local_path,
[email protected]08f8feb2012-02-26 11:53:501757 path));
[email protected]fad625e2f2011-12-08 05:38:031758 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1759}
1760
1761TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1762 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:501763 const FileSystemPath dir_path = CreatePathFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031764
1765 // Create working directory.
1766 EXPECT_EQ(base::PLATFORM_FILE_OK,
1767 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1768
1769 // DeleteFile, delete case.
[email protected]08f8feb2012-02-26 11:53:501770 FileSystemPath path = dir_path.AppendASCII("DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031771 bool created = false;
1772 context.reset(NewContext(NULL));
1773 EXPECT_EQ(base::PLATFORM_FILE_OK,
1774 ofu()->EnsureFileExists(context.get(), path, &created));
1775 EXPECT_TRUE(created);
1776
1777 ClearTimestamp(dir_path);
1778 context.reset(NewContext(NULL));
1779 EXPECT_EQ(base::PLATFORM_FILE_OK,
1780 ofu()->DeleteFile(context.get(), path));
1781 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1782
1783 // fail case.
1784 ClearTimestamp(dir_path);
1785 context.reset(NewContext(NULL));
1786 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
1787 ofu()->DeleteFile(context.get(), path));
1788 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1789
1790 // DeleteSingleDirectory, fail case.
1791 path = dir_path.AppendASCII("DeleteSingleDirectory_dir");
[email protected]08f8feb2012-02-26 11:53:501792 FileSystemPath file_path(path.AppendASCII("pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031793 context.reset(NewContext(NULL));
1794 EXPECT_EQ(base::PLATFORM_FILE_OK,
1795 ofu()->CreateDirectory(context.get(), path, true, true));
1796 created = false;
1797 context.reset(NewContext(NULL));
1798 EXPECT_EQ(base::PLATFORM_FILE_OK,
1799 ofu()->EnsureFileExists(context.get(), file_path, &created));
1800 EXPECT_TRUE(created);
1801
1802 ClearTimestamp(dir_path);
1803 context.reset(NewContext(NULL));
1804 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
1805 ofu()->DeleteSingleDirectory(context.get(), path));
1806 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1807
1808 // delete case.
1809 context.reset(NewContext(NULL));
1810 EXPECT_EQ(base::PLATFORM_FILE_OK,
1811 ofu()->DeleteFile(context.get(), file_path));
1812
1813 ClearTimestamp(dir_path);
1814 context.reset(NewContext(NULL));
1815 EXPECT_EQ(base::PLATFORM_FILE_OK,
1816 ofu()->DeleteSingleDirectory(context.get(), path));
1817 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1818}
1819
1820TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1821 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501822 CreatePathFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031823 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501824 CreatePathFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031825 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501826 CreatePathFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031827 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501828 CreatePathFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031829}
[email protected]a3938912012-03-27 14:00:551830
1831TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
1832 FileSystemPath dir = CreatePathFromUTF8("foo");
1833 FileSystemPath path1 = dir.AppendASCII("bar");
1834 FileSystemPath path2 = dir.AppendASCII("baz");
1835
1836 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1837 EXPECT_EQ(base::PLATFORM_FILE_OK,
1838 ofu()->CreateDirectory(context.get(), dir, false, false));
1839
1840 bool created = false;
1841 context.reset(NewContext(NULL));
1842 EXPECT_EQ(base::PLATFORM_FILE_OK,
1843 ofu()->EnsureFileExists(context.get(), path1, &created));
1844 EXPECT_TRUE(created);
1845
1846 context.reset(NewContext(NULL));
1847 EXPECT_EQ(base::PLATFORM_FILE_OK,
1848 ofu()->CreateDirectory(context.get(), path2, false, false));
1849
1850 FilePath file_path;
1851 context.reset(NewContext(NULL));
1852 EXPECT_EQ(base::PLATFORM_FILE_OK,
1853 ofu()->GetLocalFilePath(context.get(), path1, &file_path));
1854 EXPECT_FALSE(file_path.empty());
1855
1856 context.reset(NewContext(NULL));
1857 EXPECT_EQ(base::PLATFORM_FILE_OK,
1858 ofu()->Touch(context.get(), path1,
1859 base::Time::Now() + base::TimeDelta::FromHours(1),
1860 base::Time()));
1861
1862 context.reset(NewContext(NULL));
1863 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1864 ofu()->CreateFileEnumerator(context.get(), dir, false));
1865
1866 int count = 0;
1867 FilePath file_path_each;
1868 while (!(file_path_each = file_enum->Next()).empty()) {
1869 context.reset(NewContext(NULL));
1870 base::PlatformFileInfo file_info;
1871 FilePath file_path;
1872 EXPECT_EQ(base::PLATFORM_FILE_OK,
1873 ofu()->GetFileInfo(context.get(),
1874 dir.WithInternalPath(file_path_each),
1875 &file_info, &file_path));
1876 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1877 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1878 EXPECT_EQ(file_info.size, file_enum->Size());
1879 ++count;
1880 }
1881 EXPECT_EQ(2, count);
1882}
[email protected]294dd0312012-05-11 07:35:131883
1884TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
1885 FileSystemPath from_file(CreatePathFromUTF8("fromfile"));
1886 FileSystemPath obstacle_file(CreatePathFromUTF8("obstaclefile"));
1887 FileSystemPath to_file1(CreatePathFromUTF8("tofile1"));
1888 FileSystemPath to_file2(CreatePathFromUTF8("tofile2"));
1889 bool created;
1890
1891 int64 expected_total_file_size = 0;
1892 ASSERT_EQ(base::PLATFORM_FILE_OK,
1893 ofu()->EnsureFileExists(
1894 AllowUsageIncrease(PathCost(from_file))->context(),
1895 from_file, &created));
1896 ASSERT_TRUE(created);
1897 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1898
1899 ASSERT_EQ(base::PLATFORM_FILE_OK,
1900 ofu()->EnsureFileExists(
1901 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1902 obstacle_file, &created));
1903 ASSERT_TRUE(created);
1904 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1905
1906 int64 from_file_size = 1020;
1907 expected_total_file_size += from_file_size;
1908 ASSERT_EQ(base::PLATFORM_FILE_OK,
1909 ofu()->Truncate(
1910 AllowUsageIncrease(from_file_size)->context(),
1911 from_file, from_file_size));
1912 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1913
1914 int64 obstacle_file_size = 1;
1915 expected_total_file_size += obstacle_file_size;
1916 ASSERT_EQ(base::PLATFORM_FILE_OK,
1917 ofu()->Truncate(
1918 AllowUsageIncrease(obstacle_file_size)->context(),
1919 obstacle_file, obstacle_file_size));
1920 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1921
1922 int64 to_file1_size = from_file_size;
1923 expected_total_file_size += to_file1_size;
1924 ASSERT_EQ(base::PLATFORM_FILE_OK,
1925 ofu()->CopyOrMoveFile(
1926 AllowUsageIncrease(
1927 PathCost(to_file1) + to_file1_size)->context(),
1928 from_file, to_file1, true /* copy */));
1929 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1930
1931 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1932 ofu()->CopyOrMoveFile(
1933 DisallowUsageIncrease(
1934 PathCost(to_file2) + from_file_size)->context(),
1935 from_file, to_file2, true /* copy */));
1936 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1937
1938 int64 old_obstacle_file_size = obstacle_file_size;
1939 obstacle_file_size = from_file_size;
1940 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1941 ASSERT_EQ(base::PLATFORM_FILE_OK,
1942 ofu()->CopyOrMoveFile(
1943 AllowUsageIncrease(
1944 obstacle_file_size - old_obstacle_file_size)->context(),
1945 from_file, obstacle_file, true /* copy */));
1946 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1947
1948 int64 old_from_file_size = from_file_size;
1949 from_file_size = old_from_file_size - 1;
1950 expected_total_file_size += from_file_size - old_from_file_size;
1951 ASSERT_EQ(base::PLATFORM_FILE_OK,
1952 ofu()->Truncate(
1953 AllowUsageIncrease(
1954 from_file_size - old_from_file_size)->context(),
1955 from_file, from_file_size));
1956 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1957
1958 // quota exceeded
1959 {
1960 old_obstacle_file_size = obstacle_file_size;
1961 obstacle_file_size = from_file_size;
1962 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1963 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
1964 obstacle_file_size - old_obstacle_file_size);
1965 helper->context()->set_allowed_bytes_growth(
1966 helper->context()->allowed_bytes_growth() - 1);
1967 ASSERT_EQ(base::PLATFORM_FILE_OK,
1968 ofu()->CopyOrMoveFile(
1969 helper->context(),
1970 from_file, obstacle_file, true /* copy */));
1971 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1972 }
1973}
1974
1975TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
1976 FileSystemPath from_file(CreatePathFromUTF8("fromfile"));
1977 FileSystemPath obstacle_file(CreatePathFromUTF8("obstaclefile"));
1978 FileSystemPath to_file(CreatePathFromUTF8("tofile"));
1979 bool created;
1980
1981 int64 expected_total_file_size = 0;
1982 ASSERT_EQ(base::PLATFORM_FILE_OK,
1983 ofu()->EnsureFileExists(
1984 AllowUsageIncrease(PathCost(from_file))->context(),
1985 from_file, &created));
1986 ASSERT_TRUE(created);
1987 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1988
1989 int64 from_file_size = 1020;
1990 expected_total_file_size += from_file_size;
1991 ASSERT_EQ(base::PLATFORM_FILE_OK,
1992 ofu()->Truncate(
1993 AllowUsageIncrease(from_file_size)->context(),
1994 from_file, from_file_size));
1995 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1996
1997 int64 to_file_size ALLOW_UNUSED = from_file_size;
1998 from_file_size = 0;
1999 ASSERT_EQ(base::PLATFORM_FILE_OK,
2000 ofu()->CopyOrMoveFile(
2001 AllowUsageIncrease(-PathCost(from_file) +
2002 PathCost(to_file))->context(),
2003 from_file, to_file, false /* move */));
2004 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2005
2006 ASSERT_EQ(base::PLATFORM_FILE_OK,
2007 ofu()->EnsureFileExists(
2008 AllowUsageIncrease(PathCost(from_file))->context(),
2009 from_file, &created));
2010 ASSERT_TRUE(created);
2011 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2012
2013 ASSERT_EQ(base::PLATFORM_FILE_OK,
2014 ofu()->EnsureFileExists(
2015 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2016 obstacle_file, &created));
2017 ASSERT_TRUE(created);
2018 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2019
2020 from_file_size = 1020;
2021 expected_total_file_size += from_file_size;
2022 ASSERT_EQ(base::PLATFORM_FILE_OK,
2023 ofu()->Truncate(
2024 AllowUsageIncrease(from_file_size)->context(),
2025 from_file, from_file_size));
2026 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2027
2028 int64 obstacle_file_size = 1;
2029 expected_total_file_size += obstacle_file_size;
2030 ASSERT_EQ(base::PLATFORM_FILE_OK,
2031 ofu()->Truncate(
2032 AllowUsageIncrease(1)->context(),
2033 obstacle_file, obstacle_file_size));
2034 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2035
2036 int64 old_obstacle_file_size = obstacle_file_size;
2037 obstacle_file_size = from_file_size;
2038 from_file_size = 0;
2039 expected_total_file_size -= old_obstacle_file_size;
2040 ASSERT_EQ(base::PLATFORM_FILE_OK,
2041 ofu()->CopyOrMoveFile(
2042 AllowUsageIncrease(
2043 -old_obstacle_file_size - PathCost(from_file))->context(),
2044 from_file, obstacle_file,
2045 false /* move */));
2046 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2047
2048 ASSERT_EQ(base::PLATFORM_FILE_OK,
2049 ofu()->EnsureFileExists(
2050 AllowUsageIncrease(PathCost(from_file))->context(),
2051 from_file, &created));
2052 ASSERT_TRUE(created);
2053 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2054
2055 from_file_size = 10;
2056 expected_total_file_size += from_file_size;
2057 ASSERT_EQ(base::PLATFORM_FILE_OK,
2058 ofu()->Truncate(
2059 AllowUsageIncrease(from_file_size)->context(),
2060 from_file, from_file_size));
2061 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2062
2063 // quota exceeded even after operation
2064 old_obstacle_file_size = obstacle_file_size;
2065 obstacle_file_size = from_file_size;
2066 from_file_size = 0;
2067 expected_total_file_size -= old_obstacle_file_size;
2068 scoped_ptr<FileSystemOperationContext> context =
2069 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2070 ASSERT_EQ(base::PLATFORM_FILE_OK,
2071 ofu()->CopyOrMoveFile(
2072 context.get(), from_file, obstacle_file, false /* move */));
2073 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2074 context.reset();
2075}
2076
2077TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
2078 FileSystemPath dir(CreatePathFromUTF8("dir"));
2079 FileSystemPath file(CreatePathFromUTF8("file"));
2080 FileSystemPath dfile1(CreatePathFromUTF8("dir/dfile1"));
2081 FileSystemPath dfile2(CreatePathFromUTF8("dir/dfile2"));
2082 bool created;
2083
2084 ASSERT_EQ(base::PLATFORM_FILE_OK,
2085 ofu()->EnsureFileExists(
2086 AllowUsageIncrease(PathCost(file))->context(),
2087 file, &created));
2088 ASSERT_TRUE(created);
2089 ASSERT_EQ(0, ComputeTotalFileSize());
2090
2091 ASSERT_EQ(base::PLATFORM_FILE_OK,
2092 ofu()->CreateDirectory(
2093 AllowUsageIncrease(PathCost(dir))->context(),
2094 dir, false, false));
2095 ASSERT_EQ(0, ComputeTotalFileSize());
2096
2097 ASSERT_EQ(base::PLATFORM_FILE_OK,
2098 ofu()->EnsureFileExists(
2099 AllowUsageIncrease(PathCost(dfile1))->context(),
2100 dfile1, &created));
2101 ASSERT_TRUE(created);
2102 ASSERT_EQ(0, ComputeTotalFileSize());
2103
2104 ASSERT_EQ(base::PLATFORM_FILE_OK,
2105 ofu()->EnsureFileExists(
2106 AllowUsageIncrease(PathCost(dfile2))->context(),
2107 dfile2, &created));
2108 ASSERT_TRUE(created);
2109 ASSERT_EQ(0, ComputeTotalFileSize());
2110
2111 ASSERT_EQ(base::PLATFORM_FILE_OK,
2112 ofu()->Truncate(
2113 AllowUsageIncrease(340)->context(),
2114 file, 340));
2115 ASSERT_EQ(340, ComputeTotalFileSize());
2116
2117 ASSERT_EQ(base::PLATFORM_FILE_OK,
2118 ofu()->Truncate(
2119 AllowUsageIncrease(1020)->context(),
2120 dfile1, 1020));
2121 ASSERT_EQ(1360, ComputeTotalFileSize());
2122
2123 ASSERT_EQ(base::PLATFORM_FILE_OK,
2124 ofu()->Truncate(
2125 AllowUsageIncrease(120)->context(),
2126 dfile2, 120));
2127 ASSERT_EQ(1480, ComputeTotalFileSize());
2128
2129 ASSERT_EQ(base::PLATFORM_FILE_OK,
2130 ofu()->DeleteFile(
2131 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2132 file));
2133 ASSERT_EQ(1140, ComputeTotalFileSize());
2134
2135 ASSERT_EQ(base::PLATFORM_FILE_OK,
2136 FileUtilHelper::Delete(
2137 AllowUsageIncrease(-PathCost(dir) -
2138 PathCost(dfile1) -
2139 PathCost(dfile2) -
2140 1020 - 120)->context(),
2141 ofu(), dir, true));
2142 ASSERT_EQ(0, ComputeTotalFileSize());
2143}
[email protected]7d78be12012-05-24 07:07:262144
2145TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) {
2146 FileSystemPath file(CreatePathFromUTF8("file"));
2147 base::PlatformFile file_handle;
2148 bool created;
2149
2150 // Creating a file.
2151 ASSERT_EQ(base::PLATFORM_FILE_OK,
2152 ofu()->EnsureFileExists(
2153 AllowUsageIncrease(PathCost(file))->context(),
2154 file, &created));
2155 ASSERT_TRUE(created);
2156 ASSERT_EQ(0, ComputeTotalFileSize());
2157
2158 // Opening it, which shouldn't change the usage.
2159 ASSERT_EQ(base::PLATFORM_FILE_OK,
2160 ofu()->CreateOrOpen(
2161 AllowUsageIncrease(0)->context(), file,
2162 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
2163 &file_handle, &created));
2164 ASSERT_EQ(0, ComputeTotalFileSize());
2165 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2166
2167 const int length = 33;
2168 ASSERT_EQ(base::PLATFORM_FILE_OK,
2169 ofu()->Truncate(
2170 AllowUsageIncrease(length)->context(), file, length));
2171 ASSERT_EQ(length, ComputeTotalFileSize());
2172
2173 // Opening it with CREATE_ALWAYS flag, which should truncate the file size.
2174 ASSERT_EQ(base::PLATFORM_FILE_OK,
2175 ofu()->CreateOrOpen(
2176 AllowUsageIncrease(-length)->context(), file,
2177 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
2178 &file_handle, &created));
2179 ASSERT_EQ(0, ComputeTotalFileSize());
2180 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2181
2182 // Extending the file again.
2183 ASSERT_EQ(base::PLATFORM_FILE_OK,
2184 ofu()->Truncate(
2185 AllowUsageIncrease(length)->context(), file, length));
2186 ASSERT_EQ(length, ComputeTotalFileSize());
2187
2188 // Opening it with TRUNCATED flag, which should truncate the file size.
2189 ASSERT_EQ(base::PLATFORM_FILE_OK,
2190 ofu()->CreateOrOpen(
2191 AllowUsageIncrease(-length)->context(), file,
2192 base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
2193 &file_handle, &created));
2194 ASSERT_EQ(0, ComputeTotalFileSize());
2195 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
2196}