blob: 9d7b380393d099e1de98c25631a8ac3aac97eaa5 [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]e7e46732012-01-05 11:45:5522#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4923#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]0c5ebe32011-08-19 22:37:1624#include "webkit/quota/mock_special_storage_policy.h"
25#include "webkit/quota/quota_manager.h"
26#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3227
28using namespace fileapi;
29
30namespace {
31
32FilePath UTF8ToFilePath(const std::string& str) {
33 FilePath::StringType result;
34#if defined(OS_POSIX)
35 result = str;
36#elif defined(OS_WIN)
37 result = base::SysUTF8ToWide(str);
38#endif
39 return FilePath(result);
40}
41
42bool FileExists(const FilePath& path) {
43 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
44}
45
[email protected]81b7f662011-05-26 00:54:4646int64 GetSize(const FilePath& path) {
47 int64 size;
48 EXPECT_TRUE(file_util::GetFileSize(path, &size));
49 return size;
50}
51
[email protected]d4905e2e2011-05-13 21:56:3252// After a move, the dest exists and the source doesn't.
53// After a copy, both source and dest exist.
54struct CopyMoveTestCaseRecord {
55 bool is_copy_not_move;
56 const char source_path[64];
57 const char dest_path[64];
58 bool cause_overwrite;
59};
60
61const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
62 // This is the combinatoric set of:
63 // rename vs. same-name
64 // different directory vs. same directory
65 // overwrite vs. no-overwrite
66 // copy vs. move
67 // We can never be called with source and destination paths identical, so
68 // those cases are omitted.
69 {true, "dir0/file0", "dir0/file1", false},
70 {false, "dir0/file0", "dir0/file1", false},
71 {true, "dir0/file0", "dir0/file1", true},
72 {false, "dir0/file0", "dir0/file1", true},
73
74 {true, "dir0/file0", "dir1/file0", false},
75 {false, "dir0/file0", "dir1/file0", false},
76 {true, "dir0/file0", "dir1/file0", true},
77 {false, "dir0/file0", "dir1/file0", true},
78 {true, "dir0/file0", "dir1/file1", false},
79 {false, "dir0/file0", "dir1/file1", false},
80 {true, "dir0/file0", "dir1/file1", true},
81 {false, "dir0/file0", "dir1/file1", true},
82};
83
[email protected]6b931152011-05-20 21:02:3584struct MigrationTestCaseRecord {
85 bool is_directory;
86 const FilePath::CharType path[64];
87 int64 data_file_size;
88};
89
90const MigrationTestCaseRecord kMigrationTestCases[] = {
91 {true, FILE_PATH_LITERAL("dir a"), 0},
92 {true, FILE_PATH_LITERAL("dir a/dir a"), 0},
93 {true, FILE_PATH_LITERAL("dir a/dir d"), 0},
94 {true, FILE_PATH_LITERAL("dir a/dir d/dir e"), 0},
95 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir f"), 0},
96 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g"), 0},
97 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir h"), 0},
98 {true, FILE_PATH_LITERAL("dir b"), 0},
99 {true, FILE_PATH_LITERAL("dir b/dir a"), 0},
100 {true, FILE_PATH_LITERAL("dir c"), 0},
101 {false, FILE_PATH_LITERAL("file 0"), 38},
102 {false, FILE_PATH_LITERAL("file 2"), 60},
103 {false, FILE_PATH_LITERAL("file 3"), 0},
104 {false, FILE_PATH_LITERAL("dir a/file 0"), 39},
105 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40},
106 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41},
107 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42},
108 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50},
109};
110
[email protected]fcc2d5f2011-05-23 22:06:26111struct OriginEnumerationTestRecord {
112 std::string origin_url;
113 bool has_temporary;
114 bool has_persistent;
115};
116
117const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
118 {"http://example.com", false, true},
119 {"http://example1.com", true, false},
120 {"https://example1.com", true, true},
121 {"file://", false, true},
122 {"http://example.com:8000", false, true},
123};
124
[email protected]d4905e2e2011-05-13 21:56:32125} // namespace (anonymous)
126
127// TODO(ericu): The vast majority of this and the other FSFU subclass tests
128// could theoretically be shared. It would basically be a FSFU interface
129// compliance test, and only the subclass-specific bits that look into the
130// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49131class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32132 public:
[email protected]7878ece2011-09-05 11:41:49133 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26134 : origin_(GURL("http://www.example.com")),
135 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03136 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16137 test_helper_(origin_, type_),
138 quota_status_(quota::kQuotaStatusUnknown),
139 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32140 }
141
142 void SetUp() {
143 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
144
[email protected]e7e46732012-01-05 11:45:55145 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
146 new quota::MockSpecialStoragePolicy();
147
[email protected]0c5ebe32011-08-19 22:37:16148 quota_manager_ = new quota::QuotaManager(
149 false /* is_incognito */,
150 data_dir_.path(),
151 base::MessageLoopProxy::current(),
152 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55153 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16154
155 // Every time we create a new helper, it creates another context, which
156 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49157 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16158 file_system_context_ = new FileSystemContext(
159 base::MessageLoopProxy::current(),
160 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55161 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16162 quota_manager_->proxy(),
163 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55164 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16165
[email protected]7878ece2011-09-05 11:41:49166 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
[email protected]e7e46732012-01-05 11:45:55167 file_system_context_->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16168
169 test_helper_.SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49170 obfuscated_file_util_.get());
[email protected]d4905e2e2011-05-13 21:56:32171 }
172
[email protected]e7e46732012-01-05 11:45:55173 void TearDown() {
174 quota_manager_ = NULL;
175 test_helper_.TearDown();
176 }
177
[email protected]0c5ebe32011-08-19 22:37:16178 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
179 FileSystemOperationContext* context;
180 if (helper)
181 context = helper->NewOperationContext();
182 else
183 context = test_helper_.NewOperationContext();
184 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]d4905e2e2011-05-13 21:56:32185 return context;
186 }
187
[email protected]0c5ebe32011-08-19 22:37:16188 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49189 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16190 // Use this for tests which need to run in multiple origins; we need a test
191 // helper per origin.
192 FileSystemTestOriginHelper* NewHelper(
193 const GURL& origin, fileapi::FileSystemType type) {
194 FileSystemTestOriginHelper* helper =
195 new FileSystemTestOriginHelper(origin, type);
196
197 helper->SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49198 obfuscated_file_util_.get());
[email protected]0c5ebe32011-08-19 22:37:16199 return helper;
200 }
201
[email protected]7878ece2011-09-05 11:41:49202 ObfuscatedFileUtil* ofu() {
203 return obfuscated_file_util_.get();
[email protected]d4905e2e2011-05-13 21:56:32204 }
205
[email protected]6b931152011-05-20 21:02:35206 const FilePath& test_directory() const {
207 return data_dir_.path();
208 }
209
[email protected]0c5ebe32011-08-19 22:37:16210 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26211 return origin_;
212 }
213
214 fileapi::FileSystemType type() const {
215 return type_;
216 }
217
[email protected]0c5ebe32011-08-19 22:37:16218 void GetUsageFromQuotaManager() {
219 quota_manager_->GetUsageAndQuota(
220 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03221 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
222 weak_factory_.GetWeakPtr()));
[email protected]0c5ebe32011-08-19 22:37:16223 MessageLoop::current()->RunAllPending();
224 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
225 }
226
227 void RevokeUsageCache() {
228 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
229 ASSERT_TRUE(test_helper_.RevokeUsageCache());
230 }
231
232 int64 SizeInUsageFile() {
233 return test_helper_.GetCachedOriginUsage();
234 }
235
236 int64 usage() const { return usage_; }
237
238 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
239 EXPECT_EQ(quota::kQuotaStatusOk, status);
240 quota_status_ = status;
241 usage_ = usage;
242 }
243
[email protected]d4905e2e2011-05-13 21:56:32244 void CheckFileAndCloseHandle(
245 const FilePath& virtual_path, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16246 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32247 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49248 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32249 context.get(), virtual_path, &local_path));
250
251 base::PlatformFileInfo file_info0;
252 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49253 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32254 context.get(), virtual_path, &file_info0, &data_path));
255 EXPECT_EQ(data_path, local_path);
256 EXPECT_TRUE(FileExists(data_path));
257 EXPECT_EQ(0, GetSize(data_path));
258
259 const char data[] = "test data";
260 const int length = arraysize(data) - 1;
261
262 if (base::kInvalidPlatformFileValue == file_handle) {
263 bool created = true;
264 PlatformFileError error;
265 file_handle = base::CreatePlatformFile(
266 data_path,
267 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
268 &created,
269 &error);
[email protected]81b7f662011-05-26 00:54:46270 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32271 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
272 EXPECT_FALSE(created);
273 }
274 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
275 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
276
277 base::PlatformFileInfo file_info1;
278 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16279 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49280 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32281 context.get(), virtual_path, &file_info1, &data_path));
282 EXPECT_EQ(data_path, local_path);
283
284 EXPECT_FALSE(file_info0.is_directory);
285 EXPECT_FALSE(file_info1.is_directory);
286 EXPECT_FALSE(file_info0.is_symbolic_link);
287 EXPECT_FALSE(file_info1.is_symbolic_link);
288 EXPECT_EQ(0, file_info0.size);
289 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32290 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32291
[email protected]0c5ebe32011-08-19 22:37:16292 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49293 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32294 context.get(), virtual_path, length * 2));
295 EXPECT_EQ(length * 2, GetSize(data_path));
296
[email protected]0c5ebe32011-08-19 22:37:16297 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49298 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]0c5ebe32011-08-19 22:37:16299 context.get(), virtual_path, 0));
300 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32301 }
302
303 void ValidateTestDirectory(
304 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17305 const std::set<FilePath::StringType>& files,
306 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32307 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17308 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32309 for (iter = files.begin(); iter != files.end(); ++iter) {
310 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16311 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32312 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49313 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17314 context.get(), root_path.Append(*iter),
[email protected]d4905e2e2011-05-13 21:56:32315 &created));
316 ASSERT_FALSE(created);
317 }
318 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16319 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49320 EXPECT_TRUE(ofu()->DirectoryExists(context.get(),
[email protected]89ee4272011-05-16 18:45:17321 root_path.Append(*iter)));
[email protected]d4905e2e2011-05-13 21:56:32322 }
323 }
324
325 void FillTestDirectory(
326 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17327 std::set<FilePath::StringType>* files,
328 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32329 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16330 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32331 std::vector<base::FileUtilProxy::Entry> entries;
332 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49333 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32334 EXPECT_EQ(0UL, entries.size());
335
336 files->clear();
[email protected]89ee4272011-05-16 18:45:17337 files->insert(FILE_PATH_LITERAL("first"));
338 files->insert(FILE_PATH_LITERAL("second"));
339 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32340 directories->clear();
[email protected]89ee4272011-05-16 18:45:17341 directories->insert(FILE_PATH_LITERAL("fourth"));
342 directories->insert(FILE_PATH_LITERAL("fifth"));
343 directories->insert(FILE_PATH_LITERAL("sixth"));
344 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32345 for (iter = files->begin(); iter != files->end(); ++iter) {
346 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16347 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32348 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49349 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17350 context.get(), root_path.Append(*iter), &created));
[email protected]d4905e2e2011-05-13 21:56:32351 ASSERT_TRUE(created);
352 }
353 for (iter = directories->begin(); iter != directories->end(); ++iter) {
354 bool exclusive = true;
355 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16356 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32357 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49358 ofu()->CreateDirectory(
[email protected]89ee4272011-05-16 18:45:17359 context.get(), root_path.Append(*iter), exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32360 }
361 ValidateTestDirectory(root_path, *files, *directories);
362 }
363
364 void TestReadDirectoryHelper(const FilePath& root_path) {
[email protected]89ee4272011-05-16 18:45:17365 std::set<FilePath::StringType> files;
366 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:32367 FillTestDirectory(root_path, &files, &directories);
368
369 scoped_ptr<FileSystemOperationContext> context;
370 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16371 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32372 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49373 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32374 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
375 EXPECT_EQ(files.size() + directories.size(), entries.size());
376 for (entry_iter = entries.begin(); entry_iter != entries.end();
377 ++entry_iter) {
378 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17379 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32380 if (iter != files.end()) {
381 EXPECT_FALSE(entry.is_directory);
382 files.erase(iter);
383 continue;
384 }
[email protected]89ee4272011-05-16 18:45:17385 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32386 EXPECT_FALSE(directories.end() == iter);
387 EXPECT_TRUE(entry.is_directory);
388 directories.erase(iter);
389 }
390 }
391
[email protected]2517cfa2011-08-25 05:12:41392 void TestTouchHelper(const FilePath& path, bool is_file) {
393 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32394 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16395
[email protected]2517cfa2011-08-25 05:12:41396 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32397 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49398 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32399 context.get(), path, last_access_time, last_modified_time));
400 FilePath local_path;
401 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16402 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49403 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32404 context.get(), path, &file_info, &local_path));
405 // We compare as time_t here to lower our resolution, to avoid false
406 // negatives caused by conversion to the local filesystem's native
407 // representation and back.
408 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
409
[email protected]0c5ebe32011-08-19 22:37:16410 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32411 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41412 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32413 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49414 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32415 context.get(), path, last_access_time, last_modified_time));
[email protected]0c5ebe32011-08-19 22:37:16416 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49417 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32418 context.get(), path, &file_info, &local_path));
419 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49420 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41421 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32422 }
423
[email protected]81b7f662011-05-26 00:54:46424 void TestCopyInForeignFileHelper(bool overwrite) {
425 ScopedTempDir source_dir;
426 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
427 FilePath root_path = source_dir.path();
428 FilePath src_path = root_path.AppendASCII("file_name");
429 FilePath dest_path(FILE_PATH_LITERAL("new file"));
430 int64 src_file_length = 87;
431
432 base::PlatformFileError error_code;
433 bool created = false;
434 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
435 base::PlatformFile file_handle =
436 base::CreatePlatformFile(
437 src_path, file_flags, &created, &error_code);
438 EXPECT_TRUE(created);
439 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
440 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
441 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
442 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
443
444 scoped_ptr<FileSystemOperationContext> context;
445
446 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16447 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46448 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49449 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]81b7f662011-05-26 00:54:46450 EXPECT_TRUE(created);
451 }
452
[email protected]0c5ebe32011-08-19 22:37:16453 const int64 path_cost =
[email protected]7878ece2011-09-05 11:41:49454 ObfuscatedFileUtil::ComputeFilePathCost(dest_path);
[email protected]0c5ebe32011-08-19 22:37:16455 if (!overwrite) {
456 // Verify that file creation requires sufficient quota for the path.
457 context.reset(NewContext(NULL));
458 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
459 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49460 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16461 }
462
463 context.reset(NewContext(NULL));
464 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46465 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49466 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16467
468 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49469 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16470 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49471 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16472 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46473 base::PlatformFileInfo file_info;
474 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49475 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]81b7f662011-05-26 00:54:46476 context.get(), dest_path, &file_info, &data_path));
477 EXPECT_NE(data_path, src_path);
478 EXPECT_TRUE(FileExists(data_path));
479 EXPECT_EQ(src_file_length, GetSize(data_path));
480
481 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49482 ofu()->DeleteFile(context.get(), dest_path));
[email protected]81b7f662011-05-26 00:54:46483 }
484
[email protected]fad625e2f2011-12-08 05:38:03485 void ClearTimestamp(const FilePath& path) {
486 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
487 EXPECT_EQ(base::PLATFORM_FILE_OK,
488 ofu()->Touch(context.get(), path, base::Time(), base::Time()));
489 EXPECT_EQ(base::Time(), GetModifiedTime(path));
490 }
491
492 base::Time GetModifiedTime(const FilePath& path) {
493 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
494 FilePath data_path;
495 base::PlatformFileInfo file_info;
496 context.reset(NewContext(NULL));
497 EXPECT_EQ(base::PLATFORM_FILE_OK,
498 ofu()->GetFileInfo(context.get(), path, &file_info, &data_path));
499 return file_info.last_modified;
500 }
501
502 void TestDirectoryTimestampHelper(const FilePath& base_dir,
503 bool copy,
504 bool overwrite) {
505 scoped_ptr<FileSystemOperationContext> context;
506 const FilePath src_dir_path(base_dir.AppendASCII("foo_dir"));
507 const FilePath dest_dir_path(base_dir.AppendASCII("bar_dir"));
508
509 const FilePath src_file_path(src_dir_path.AppendASCII("hoge"));
510 const FilePath dest_file_path(dest_dir_path.AppendASCII("fuga"));
511
512 context.reset(NewContext(NULL));
513 EXPECT_EQ(base::PLATFORM_FILE_OK,
514 ofu()->CreateDirectory(context.get(), src_dir_path, true, true));
515 context.reset(NewContext(NULL));
516 EXPECT_EQ(base::PLATFORM_FILE_OK,
517 ofu()->CreateDirectory(context.get(), dest_dir_path, true, true));
518
519 bool created = false;
520 context.reset(NewContext(NULL));
521 EXPECT_EQ(base::PLATFORM_FILE_OK,
522 ofu()->EnsureFileExists(context.get(), src_file_path, &created));
523 if (overwrite) {
524 context.reset(NewContext(NULL));
525 EXPECT_EQ(base::PLATFORM_FILE_OK,
526 ofu()->EnsureFileExists(context.get(),
527 dest_file_path, &created));
528 }
529
530 ClearTimestamp(src_dir_path);
531 ClearTimestamp(dest_dir_path);
532 context.reset(NewContext(NULL));
533 EXPECT_EQ(base::PLATFORM_FILE_OK,
534 ofu()->CopyOrMoveFile(context.get(),
535 src_file_path, dest_file_path,
536 copy));
537
538 if (copy)
539 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path));
540 else
541 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path));
542 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path));
543 }
544
[email protected]d4905e2e2011-05-13 21:56:32545 private:
546 ScopedTempDir data_dir_;
[email protected]7878ece2011-09-05 11:41:49547 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16548 scoped_refptr<quota::QuotaManager> quota_manager_;
549 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26550 GURL origin_;
551 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03552 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26553 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16554 quota::QuotaStatusCode quota_status_;
555 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32556
[email protected]7878ece2011-09-05 11:41:49557 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32558};
559
[email protected]7878ece2011-09-05 11:41:49560TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32561 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
562 bool created;
563 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16564 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32565 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
566
567 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49568 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32569 context.get(), path, file_flags, &file_handle,
570 &created));
571
[email protected]0c5ebe32011-08-19 22:37:16572 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32573 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49574 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32575
576 path = UTF8ToFilePath("test file");
577
[email protected]0c5ebe32011-08-19 22:37:16578 // Verify that file creation requires sufficient quota for the path.
579 context.reset(NewContext(NULL));
580 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49581 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16582 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49583 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16584 context.get(), path, file_flags, &file_handle, &created));
585
586 context.reset(NewContext(NULL));
587 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49588 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32589 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49590 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32591 context.get(), path, file_flags, &file_handle, &created));
592 ASSERT_TRUE(created);
593 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
594
595 CheckFileAndCloseHandle(path, file_handle);
596
[email protected]0c5ebe32011-08-19 22:37:16597 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32598 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49599 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32600 context.get(), path, &local_path));
601 EXPECT_TRUE(file_util::PathExists(local_path));
602
[email protected]0c5ebe32011-08-19 22:37:16603 // Verify that deleting a file isn't stopped by zero quota, and that it frees
604 // up quote from its path.
605 context.reset(NewContext(NULL));
606 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32607 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49608 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32609 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]7878ece2011-09-05 11:41:49610 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16611 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32612
[email protected]0c5ebe32011-08-19 22:37:16613 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32614 bool exclusive = true;
615 bool recursive = true;
616 FilePath directory_path = UTF8ToFilePath("series/of/directories");
617 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49618 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32619 context.get(), directory_path, exclusive, recursive));
620
[email protected]0c5ebe32011-08-19 22:37:16621 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32622 file_handle = base::kInvalidPlatformFileValue;
623 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49624 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32625 context.get(), path, file_flags, &file_handle, &created));
626 ASSERT_TRUE(created);
627 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
628
629 CheckFileAndCloseHandle(path, file_handle);
630
[email protected]0c5ebe32011-08-19 22:37:16631 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49632 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32633 context.get(), path, &local_path));
634 EXPECT_TRUE(file_util::PathExists(local_path));
635
[email protected]0c5ebe32011-08-19 22:37:16636 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32637 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49638 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32639 EXPECT_FALSE(file_util::PathExists(local_path));
640}
641
[email protected]7878ece2011-09-05 11:41:49642TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32643 bool created = false;
644 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16645 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32646
647 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49648 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32649
[email protected]0c5ebe32011-08-19 22:37:16650 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32651 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49652 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32653 ASSERT_TRUE(created);
654
[email protected]0c5ebe32011-08-19 22:37:16655 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32656 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49657 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32658 context.get(), path, &local_path));
659 EXPECT_EQ(0, GetSize(local_path));
660
[email protected]0c5ebe32011-08-19 22:37:16661 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49662 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32663 context.get(), path, 10));
664 EXPECT_EQ(10, GetSize(local_path));
665
[email protected]0c5ebe32011-08-19 22:37:16666 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49667 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32668 context.get(), path, 1));
669 EXPECT_EQ(1, GetSize(local_path));
670
[email protected]0c5ebe32011-08-19 22:37:16671 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49672 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16673 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49674 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32675}
676
[email protected]7878ece2011-09-05 11:41:49677TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]d4905e2e2011-05-13 21:56:32678 FilePath path = UTF8ToFilePath("fake/file");
679 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16680 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32681 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49682 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32683 context.get(), path, &created));
684
[email protected]0c5ebe32011-08-19 22:37:16685 // Verify that file creation requires sufficient quota for the path.
686 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32687 path = UTF8ToFilePath("test file");
688 created = false;
[email protected]0c5ebe32011-08-19 22:37:16689 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49690 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16691 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49692 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16693 ASSERT_FALSE(created);
694
695 context.reset(NewContext(NULL));
696 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49697 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32698 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49699 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32700 ASSERT_TRUE(created);
701
702 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
703
[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_FALSE(created);
708
709 // Also test in a subdirectory.
710 path = UTF8ToFilePath("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16711 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32712 bool exclusive = true;
713 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49714 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32715 context.get(), path.DirName(), exclusive, recursive));
716
[email protected]0c5ebe32011-08-19 22:37:16717 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32718 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49719 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32720 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16721 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49722 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16723 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49724 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32725}
726
[email protected]7878ece2011-09-05 11:41:49727TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16728 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32729
730 bool exclusive = false;
731 bool recursive = false;
732 FilePath path = UTF8ToFilePath("foo/bar");
[email protected]7878ece2011-09-05 11:41:49733 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32734 context.get(), path, exclusive, recursive));
735
[email protected]0c5ebe32011-08-19 22:37:16736 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32737 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49738 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32739
740 FilePath root = UTF8ToFilePath("");
[email protected]0c5ebe32011-08-19 22:37:16741 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49742 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16743 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49744 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16745 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49746 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32747
[email protected]0c5ebe32011-08-19 22:37:16748 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32749 exclusive = false;
750 recursive = true;
[email protected]7878ece2011-09-05 11:41:49751 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32752 context.get(), path, exclusive, recursive));
753
[email protected]0c5ebe32011-08-19 22:37:16754 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49755 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16756 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49757 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16758 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49759 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]0c5ebe32011-08-19 22:37:16760 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49761 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16762 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49763 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32764
765 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16766 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06767 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49768 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32769
770 base::PlatformFileInfo file_info;
771 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49772 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32773 context.get(), path, &file_info, &local_path));
774 EXPECT_TRUE(local_path.empty());
775 EXPECT_TRUE(file_info.is_directory);
776 EXPECT_FALSE(file_info.is_symbolic_link);
777
778 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16779 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49780 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32781 context.get(), path, exclusive, recursive));
782
783 exclusive = true;
784 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16785 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49786 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32787 context.get(), path, exclusive, recursive));
788
[email protected]0c5ebe32011-08-19 22:37:16789 // Verify that deleting a directory isn't stopped by zero quota, and that it
790 // frees up quota from its path.
791 context.reset(NewContext(NULL));
792 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32793 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49794 ofu()->DeleteSingleDirectory(context.get(), path));
795 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16796 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32797
798 path = UTF8ToFilePath("foo/bop");
799
[email protected]0c5ebe32011-08-19 22:37:16800 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49801 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16802 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49803 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16804 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49805 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
806 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32807 context.get(), path, &file_info, &local_path));
808
[email protected]0c5ebe32011-08-19 22:37:16809 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32810 exclusive = true;
811 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16812 context.reset(NewContext(NULL));
813 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49814 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
815 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16816 context.get(), path, exclusive, recursive));
817
818 context.reset(NewContext(NULL));
819 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49820 ObfuscatedFileUtil::ComputeFilePathCost(path));
821 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32822 context.get(), path, exclusive, recursive));
823
[email protected]0c5ebe32011-08-19 22:37:16824 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49825 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16826 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49827 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32828
829 exclusive = true;
830 recursive = false;
[email protected]7878ece2011-09-05 11:41:49831 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32832 context.get(), path, exclusive, recursive));
833
834 exclusive = true;
835 recursive = false;
836 path = UTF8ToFilePath("foo");
[email protected]7878ece2011-09-05 11:41:49837 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32838 context.get(), path, exclusive, recursive));
839
840 path = UTF8ToFilePath("blah");
841
[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_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32846
847 exclusive = true;
848 recursive = false;
[email protected]7878ece2011-09-05 11:41:49849 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32850 context.get(), path, exclusive, recursive));
851
[email protected]0c5ebe32011-08-19 22:37:16852 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49853 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16854 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49855 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32856
857 exclusive = true;
858 recursive = false;
[email protected]7878ece2011-09-05 11:41:49859 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32860 context.get(), path, exclusive, recursive));
861}
862
[email protected]7878ece2011-09-05 11:41:49863TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16864 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32865 bool exclusive = true;
866 bool recursive = true;
867 FilePath path = UTF8ToFilePath("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49868 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32869 context.get(), path, exclusive, recursive));
870 TestReadDirectoryHelper(path);
871}
872
[email protected]7878ece2011-09-05 11:41:49873TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]d4905e2e2011-05-13 21:56:32874 TestReadDirectoryHelper(UTF8ToFilePath(""));
875}
876
[email protected]7878ece2011-09-05 11:41:49877TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]d4905e2e2011-05-13 21:56:32878 TestReadDirectoryHelper(UTF8ToFilePath("/"));
879}
880
[email protected]7878ece2011-09-05 11:41:49881TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]d4905e2e2011-05-13 21:56:32882 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16883 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32884
885 bool created = false;
886 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49887 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32888 ASSERT_TRUE(created);
889
[email protected]0c5ebe32011-08-19 22:37:16890 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32891 std::vector<base::FileUtilProxy::Entry> entries;
892 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49893 ofu()->ReadDirectory(context.get(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32894
[email protected]7878ece2011-09-05 11:41:49895 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32896}
897
[email protected]7878ece2011-09-05 11:41:49898TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]2517cfa2011-08-25 05:12:41899 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16900 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41901
902 base::Time last_access_time = base::Time::Now();
903 base::Time last_modified_time = base::Time::Now();
904
905 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:32906 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49907 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32908 context.get(), path, last_access_time, last_modified_time));
909
[email protected]2517cfa2011-08-25 05:12:41910 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:16911 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41912 bool created = false;
913 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49914 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41915 ASSERT_TRUE(created);
916 TestTouchHelper(path, true);
917
918 // Now test a directory:
919 context.reset(NewContext(NULL));
920 bool exclusive = true;
921 bool recursive = false;
922 path = UTF8ToFilePath("dir");
[email protected]7878ece2011-09-05 11:41:49923 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:41924 path, exclusive, recursive));
925 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:16926}
927
[email protected]7878ece2011-09-05 11:41:49928TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:16929 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16930 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
931
[email protected]0c5ebe32011-08-19 22:37:16932 path = UTF8ToFilePath("file name");
933 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:41934 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16935 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49936 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41937 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:16938 context->set_allowed_bytes_growth(1024);
939 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49940 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41941 EXPECT_TRUE(created);
[email protected]7878ece2011-09-05 11:41:49942 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:16943 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
944
945 context->set_allowed_bytes_growth(1024);
946 bool exclusive = true;
947 bool recursive = true;
948 path = UTF8ToFilePath("directory/to/use");
949 std::vector<FilePath::StringType> components;
950 path.GetComponents(&components);
951 path_cost = 0;
952 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
953 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:49954 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:16955 FilePath(*iter));
956 }
957 context.reset(NewContext(NULL));
958 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:49959 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16960 context.get(), path, exclusive, recursive));
961 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32962}
963
[email protected]7878ece2011-09-05 11:41:49964TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]d4905e2e2011-05-13 21:56:32965 FilePath source_path = UTF8ToFilePath("path0.txt");
966 FilePath dest_path = UTF8ToFilePath("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:16967 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32968
969 bool is_copy_not_move = false;
970 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49971 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32972 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16973 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32974 is_copy_not_move = true;
975 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49976 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32977 is_copy_not_move));
978 source_path = UTF8ToFilePath("dir/dir/file");
979 bool exclusive = true;
980 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16981 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49982 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32983 context.get(), source_path.DirName(), exclusive, recursive));
984 is_copy_not_move = false;
985 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49986 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32987 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16988 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32989 is_copy_not_move = true;
990 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49991 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32992 is_copy_not_move));
993}
994
[email protected]7878ece2011-09-05 11:41:49995TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:32996 const int64 kSourceLength = 5;
997 const int64 kDestLength = 50;
998
999 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1000 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1001 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1002 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1003 test_case.is_copy_not_move);
1004 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1005 test_case.source_path);
1006 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1007 test_case.dest_path);
1008 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1009 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161010 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321011
1012 bool exclusive = false;
1013 bool recursive = true;
1014 FilePath source_path = UTF8ToFilePath(test_case.source_path);
1015 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
1016
[email protected]0c5ebe32011-08-19 22:37:161017 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491018 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321019 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161020 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491021 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321022 context.get(), dest_path.DirName(), exclusive, recursive));
1023
[email protected]d4905e2e2011-05-13 21:56:321024 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161025 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321026 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491027 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321028 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161029 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321030 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491031 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321032
1033 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161034 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321035 created = false;
1036 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491037 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321038 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161039 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321040 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491041 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321042 }
1043
[email protected]0c5ebe32011-08-19 22:37:161044 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491045 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:321046 source_path, dest_path, test_case.is_copy_not_move));
1047 if (test_case.is_copy_not_move) {
1048 base::PlatformFileInfo file_info;
1049 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161050 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491051 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321052 context.get(), source_path, &file_info, &local_path));
1053 EXPECT_EQ(kSourceLength, file_info.size);
1054 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491055 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:321056 } else {
1057 base::PlatformFileInfo file_info;
1058 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161059 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491060 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321061 context.get(), source_path, &file_info, &local_path));
1062 }
1063 base::PlatformFileInfo file_info;
1064 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491065 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321066 context.get(), dest_path, &file_info, &local_path));
1067 EXPECT_EQ(kSourceLength, file_info.size);
1068
1069 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491070 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321071 }
1072}
1073
[email protected]7878ece2011-09-05 11:41:491074TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:161075 FilePath src_path = UTF8ToFilePath("src path");
1076 FilePath dest_path = UTF8ToFilePath("destination path");
1077 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1078 bool created = false;
[email protected]7878ece2011-09-05 11:41:491079 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161080 context.get(), src_path, &created));
1081
1082 bool is_copy = true;
1083 // Copy, no overwrite.
1084 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491085 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161086 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491087 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161088 context.reset(NewContext(NULL));
1089 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491090 ObfuscatedFileUtil::ComputeFilePathCost(dest_path));
[email protected]0c5ebe32011-08-19 22:37:161091 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491092 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161093
1094 // Copy, with overwrite.
1095 context.reset(NewContext(NULL));
1096 context->set_allowed_bytes_growth(0);
1097 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491098 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161099}
1100
[email protected]7878ece2011-09-05 11:41:491101TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]0c5ebe32011-08-19 22:37:161102 FilePath src_path = UTF8ToFilePath("src path");
1103 FilePath dest_path = UTF8ToFilePath("destination path");
1104 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1105 bool created = false;
[email protected]7878ece2011-09-05 11:41:491106 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161107 context.get(), src_path, &created));
1108
1109 bool is_copy = false;
1110 // Move, rename, no overwrite.
1111 context.reset(NewContext(NULL));
1112 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491113 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1114 ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161115 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491116 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161117 context.reset(NewContext(NULL));
1118 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491119 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1120 ObfuscatedFileUtil::ComputeFilePathCost(src_path));
[email protected]0c5ebe32011-08-19 22:37:161121 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491122 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161123
1124 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491125 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161126 context.get(), src_path, &created));
1127
1128 // Move, rename, with overwrite.
1129 context.reset(NewContext(NULL));
1130 context->set_allowed_bytes_growth(0);
1131 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491132 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161133}
1134
[email protected]7878ece2011-09-05 11:41:491135TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]0c5ebe32011-08-19 22:37:161136 FilePath src_path = UTF8ToFilePath("src path");
1137 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1138 bool created = false;
[email protected]7878ece2011-09-05 11:41:491139 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161140 context.get(), src_path, &created));
1141
1142 bool exclusive = true;
1143 bool recursive = false;
1144 FilePath dir_path = UTF8ToFilePath("directory path");
1145 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491146 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161147 context.get(), dir_path, exclusive, recursive));
1148
1149 FilePath dest_path = dir_path.Append(src_path);
1150
1151 bool is_copy = false;
1152 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1153 // Move, no rename, no overwrite.
1154 context.reset(NewContext(NULL));
1155 context->set_allowed_bytes_growth(allowed_bytes_growth);
1156 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491157 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161158 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1159
1160 // Move, no rename, with overwrite.
1161 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491162 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161163 context.get(), src_path, &created));
1164 context.reset(NewContext(NULL));
1165 context->set_allowed_bytes_growth(allowed_bytes_growth);
1166 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491167 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161168 EXPECT_EQ(
1169 allowed_bytes_growth +
[email protected]7878ece2011-09-05 11:41:491170 ObfuscatedFileUtil::ComputeFilePathCost(src_path),
[email protected]0c5ebe32011-08-19 22:37:161171 context->allowed_bytes_growth());
1172}
1173
[email protected]7878ece2011-09-05 11:41:491174TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461175 TestCopyInForeignFileHelper(false /* overwrite */);
1176 TestCopyInForeignFileHelper(true /* overwrite */);
1177}
1178
[email protected]7878ece2011-09-05 11:41:491179TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161180 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321181 FilePath src_path = UTF8ToFilePath("source dir");
1182 bool exclusive = true;
1183 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491184 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321185 context.get(), src_path, exclusive, recursive));
1186
[email protected]89ee4272011-05-16 18:45:171187 std::set<FilePath::StringType> files;
1188 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321189 FillTestDirectory(src_path, &files, &directories);
1190
1191 FilePath dest_path = UTF8ToFilePath("destination dir");
1192
[email protected]0c5ebe32011-08-19 22:37:161193 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491194 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161195 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321196 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491197 ofu()->Copy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321198
1199 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161200 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491201 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161202 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491203 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161204 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321205 recursive = true;
1206 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491207 ofu()->Delete(context.get(), dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161208 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491209 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321210}
[email protected]6b931152011-05-20 21:02:351211
[email protected]7878ece2011-09-05 11:41:491212TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351213 ScopedTempDir source_dir;
1214 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1215 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1216 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1217
1218 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1219 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1220 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1221 FilePath local_src_path = root_path.Append(test_case.path);
1222 if (test_case.is_directory) {
1223 ASSERT_TRUE(
1224 file_util::CreateDirectory(local_src_path));
1225 } else {
1226 base::PlatformFileError error_code;
1227 bool created = false;
1228 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
1229 base::PlatformFile file_handle =
1230 base::CreatePlatformFile(
1231 local_src_path, file_flags, &created, &error_code);
1232 EXPECT_TRUE(created);
[email protected]81b7f662011-05-26 00:54:461233 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]6b931152011-05-20 21:02:351234 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
1235 ASSERT_TRUE(
1236 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
1237 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1238 }
1239 }
1240
[email protected]7878ece2011-09-05 11:41:491241 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351242
1243 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161244 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491245 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]6b931152011-05-20 21:02:351246 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1247 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
1248 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1249 FilePath local_data_path = new_root.Append(test_case.path);
1250#if defined(OS_WIN)
1251 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
1252#endif
[email protected]0c5ebe32011-08-19 22:37:161253 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491254 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351255 FilePath data_path;
1256 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1257 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491258 ofu()->GetFileInfo(context.get(), FilePath(test_case.path),
1259 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351260 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491261 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351262 } else {
1263 base::PlatformFileInfo platform_file_info;
1264 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1265 local_data_path.value());
1266 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1267 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1268 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1269 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161270 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351271 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491272 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1273 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351274 }
1275 }
1276}
[email protected]fcc2d5f2011-05-23 22:06:261277
[email protected]7878ece2011-09-05 11:41:491278TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1279 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1280 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161281 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261282 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161283 EXPECT_EQ(origin(), enumerator->Next());
1284 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1285 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1286 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261287 EXPECT_EQ(GURL(), enumerator->Next());
1288 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1289 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1290
1291 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161292 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261293
1294 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1295 SCOPED_TRACE(testing::Message() <<
1296 "Validating kOriginEnumerationTestRecords " << i);
1297 const OriginEnumerationTestRecord& record =
1298 kOriginEnumerationTestRecords[i];
1299 GURL origin_url(record.origin_url);
1300 origins_expected.insert(origin_url);
1301 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161302 scoped_ptr<FileSystemTestOriginHelper> helper(
1303 NewHelper(origin_url, kFileSystemTypeTemporary));
1304 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261305 context->set_src_origin_url(origin_url);
1306 context->set_src_type(kFileSystemTypeTemporary);
1307 bool created = false;
1308 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491309 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261310 FilePath().AppendASCII("file"), &created));
1311 EXPECT_TRUE(created);
1312 }
1313 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161314 scoped_ptr<FileSystemTestOriginHelper> helper(
1315 NewHelper(origin_url, kFileSystemTypePersistent));
1316 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261317 context->set_src_origin_url(origin_url);
1318 context->set_src_type(kFileSystemTypePersistent);
1319 bool created = false;
1320 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491321 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261322 FilePath().AppendASCII("file"), &created));
1323 EXPECT_TRUE(created);
1324 }
1325 }
[email protected]7878ece2011-09-05 11:41:491326 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261327 EXPECT_TRUE(enumerator.get());
1328 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161329 GURL origin_url;
1330 while (!(origin_url = enumerator->Next()).is_empty()) {
1331 origins_found.insert(origin_url);
1332 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261333 bool found = false;
1334 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1335 ++i) {
1336 const OriginEnumerationTestRecord& record =
1337 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161338 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261339 continue;
1340 found = true;
1341 EXPECT_EQ(record.has_temporary,
1342 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1343 EXPECT_EQ(record.has_persistent,
1344 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1345 }
[email protected]0c5ebe32011-08-19 22:37:161346 // Deal with the default filesystem created by the test helper.
1347 if (!found && origin_url == origin()) {
1348 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1349 EXPECT_EQ(true,
1350 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181351 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161352 found = true;
1353 }
[email protected]fcc2d5f2011-05-23 22:06:261354 EXPECT_TRUE(found);
1355 }
1356
1357 std::set<GURL> diff;
1358 std::set_symmetric_difference(origins_expected.begin(),
1359 origins_expected.end(), origins_found.begin(), origins_found.end(),
1360 inserter(diff, diff.begin()));
1361 EXPECT_TRUE(diff.empty());
1362}
[email protected]0c5ebe32011-08-19 22:37:161363
[email protected]7878ece2011-09-05 11:41:491364TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161365 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1366
1367 int64 expected_quota = 0;
1368
1369 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1370 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1371 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1372 FilePath path(test_case.path);
[email protected]7878ece2011-09-05 11:41:491373 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:161374 if (test_case.is_directory) {
1375 bool exclusive = true;
1376 bool recursive = false;
1377 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491378 ofu()->CreateDirectory(context.get(), path, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161379 } else {
1380 bool created = false;
1381 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491382 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:161383 ASSERT_TRUE(created);
1384 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491385 ofu()->Truncate(context.get(), path,
[email protected]0c5ebe32011-08-19 22:37:161386 test_case.data_file_size));
1387 expected_quota += test_case.data_file_size;
1388 }
1389 }
1390 EXPECT_EQ(expected_quota, SizeInUsageFile());
1391 RevokeUsageCache();
1392 EXPECT_EQ(-1, SizeInUsageFile());
1393 GetUsageFromQuotaManager();
1394 EXPECT_EQ(expected_quota, SizeInUsageFile());
1395 EXPECT_EQ(expected_quota, usage());
1396}
[email protected]34583332011-08-31 08:59:471397
[email protected]7878ece2011-09-05 11:41:491398TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]34583332011-08-31 08:59:471399 const FilePath kPath1 = FilePath().AppendASCII("hoge");
1400 const FilePath kPath2 = FilePath().AppendASCII("fuga");
1401
1402 scoped_ptr<FileSystemOperationContext> context;
1403 base::PlatformFile file;
1404 base::PlatformFileInfo file_info;
1405 FilePath data_path;
1406 bool created = false;
1407
1408 // Create a non-empty file.
1409 context.reset(NewContext(NULL));
1410 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491411 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471412 EXPECT_TRUE(created);
1413 context.reset(NewContext(NULL));
1414 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491415 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471416 context.reset(NewContext(NULL));
1417 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491418 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471419 context.get(), kPath1, &file_info, &data_path));
1420 EXPECT_EQ(10, file_info.size);
1421
1422 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491423 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471424
1425 // Try to get file info of broken file.
1426 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491427 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471428 context.reset(NewContext(NULL));
1429 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491430 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471431 EXPECT_TRUE(created);
1432 context.reset(NewContext(NULL));
1433 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491434 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471435 context.get(), kPath1, &file_info, &data_path));
1436 EXPECT_EQ(0, file_info.size);
1437
1438 // Make another broken file to |kPath2|.
1439 context.reset(NewContext(NULL));
1440 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491441 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471442 EXPECT_TRUE(created);
1443
1444 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491445 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471446
1447 // Repair broken |kPath1|.
1448 context.reset(NewContext(NULL));
1449 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491450 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471451 base::Time::Now()));
1452 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491453 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471454 EXPECT_TRUE(created);
1455
1456 // Copy from sound |kPath1| to broken |kPath2|.
1457 context.reset(NewContext(NULL));
1458 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491459 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]34583332011-08-31 08:59:471460 true /* copy */));
1461
[email protected]7878ece2011-09-05 11:41:491462 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471463 context.reset(NewContext(NULL));
1464 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491465 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471466 context.get(), kPath1,
1467 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1468 &file, &created));
1469 EXPECT_TRUE(created);
1470 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1471 EXPECT_EQ(0, file_info.size);
1472 EXPECT_TRUE(base::ClosePlatformFile(file));
1473}
[email protected]9dfdc0e32011-09-02 06:07:441474
[email protected]7878ece2011-09-05 11:41:491475TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]9dfdc0e32011-09-02 06:07:441476 const FilePath kPath[] = {
1477 FilePath().AppendASCII("foo"),
1478 FilePath().AppendASCII("bar"),
1479 FilePath().AppendASCII("baz")
1480 };
1481 scoped_ptr<FileSystemOperationContext> context;
1482
1483 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1484 bool created = false;
1485 context.reset(NewContext(NULL));
1486 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491487 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441488 EXPECT_TRUE(created);
1489 }
1490
1491 context.reset(NewContext(NULL));
1492 std::vector<base::FileUtilProxy::Entry> entries;
1493 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491494 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441495 EXPECT_EQ(3u, entries.size());
1496
1497 context.reset(NewContext(NULL));
1498 FilePath local_path;
1499 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491500 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441501 EXPECT_TRUE(file_util::Delete(local_path, false));
1502
1503 context.reset(NewContext(NULL));
1504 entries.clear();
1505 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491506 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441507 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1508}
[email protected]fad625e2f2011-12-08 05:38:031509
1510TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1511 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1512 const FilePath dir_path(FILE_PATH_LITERAL("foo_dir"));
1513
1514 // Create working directory.
1515 EXPECT_EQ(base::PLATFORM_FILE_OK,
1516 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1517
1518 // EnsureFileExists, create case.
1519 FilePath path(dir_path.AppendASCII("EnsureFileExists_file"));
1520 bool created = false;
1521 ClearTimestamp(dir_path);
1522 context.reset(NewContext(NULL));
1523 EXPECT_EQ(base::PLATFORM_FILE_OK,
1524 ofu()->EnsureFileExists(context.get(), path, &created));
1525 EXPECT_TRUE(created);
1526 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1527
1528 // non create case.
1529 created = true;
1530 ClearTimestamp(dir_path);
1531 context.reset(NewContext(NULL));
1532 EXPECT_EQ(base::PLATFORM_FILE_OK,
1533 ofu()->EnsureFileExists(context.get(), path, &created));
1534 EXPECT_FALSE(created);
1535 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1536
1537 // fail case.
1538 path = dir_path.AppendASCII("EnsureFileExists_dir");
1539 context.reset(NewContext(NULL));
1540 EXPECT_EQ(base::PLATFORM_FILE_OK,
1541 ofu()->CreateDirectory(context.get(), path, false, false));
1542
1543 ClearTimestamp(dir_path);
1544 context.reset(NewContext(NULL));
1545 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
1546 ofu()->EnsureFileExists(context.get(), path, &created));
1547 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1548
1549 // CreateOrOpen, create case.
1550 path = dir_path.AppendASCII("CreateOrOpen_file");
1551 PlatformFile file_handle = base::kInvalidPlatformFileValue;
1552 created = false;
1553 ClearTimestamp(dir_path);
1554 context.reset(NewContext(NULL));
1555 EXPECT_EQ(base::PLATFORM_FILE_OK,
1556 ofu()->CreateOrOpen(
1557 context.get(), path,
1558 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1559 &file_handle, &created));
1560 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1561 EXPECT_TRUE(created);
1562 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1563 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1564
1565 // open case.
1566 file_handle = base::kInvalidPlatformFileValue;
1567 created = true;
1568 ClearTimestamp(dir_path);
1569 context.reset(NewContext(NULL));
1570 EXPECT_EQ(base::PLATFORM_FILE_OK,
1571 ofu()->CreateOrOpen(
1572 context.get(), path,
1573 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1574 &file_handle, &created));
1575 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1576 EXPECT_FALSE(created);
1577 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1578 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1579
1580 // fail case
1581 file_handle = base::kInvalidPlatformFileValue;
1582 ClearTimestamp(dir_path);
1583 context.reset(NewContext(NULL));
1584 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1585 ofu()->CreateOrOpen(
1586 context.get(), path,
1587 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1588 &file_handle, &created));
1589 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
1590 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1591
1592 // CreateDirectory, create case.
1593 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
1594 path = dir_path.AppendASCII("CreateDirectory_dir");
1595 FilePath subdir_path(path.AppendASCII("subdir"));
1596 ClearTimestamp(dir_path);
1597 context.reset(NewContext(NULL));
1598 EXPECT_EQ(base::PLATFORM_FILE_OK,
1599 ofu()->CreateDirectory(context.get(), subdir_path,
1600 true /* exclusive */, true /* recursive */));
1601 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1602
1603 // create subdir case.
1604 // Creating CreateDirectory_dir/subdir2.
1605 subdir_path = path.AppendASCII("subdir2");
1606 ClearTimestamp(dir_path);
1607 ClearTimestamp(path);
1608 context.reset(NewContext(NULL));
1609 EXPECT_EQ(base::PLATFORM_FILE_OK,
1610 ofu()->CreateDirectory(context.get(), subdir_path,
1611 true /* exclusive */, true /* recursive */));
1612 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1613 EXPECT_NE(base::Time(), GetModifiedTime(path));
1614
1615 // fail case.
1616 path = dir_path.AppendASCII("CreateDirectory_dir");
1617 ClearTimestamp(dir_path);
1618 context.reset(NewContext(NULL));
1619 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1620 ofu()->CreateDirectory(context.get(), path,
1621 true /* exclusive */, true /* recursive */));
1622 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1623
1624 // CopyInForeignFile, create case.
1625 path = dir_path.AppendASCII("CopyInForeignFile_file");
1626 FilePath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file");
1627 context.reset(NewContext(NULL));
1628 EXPECT_EQ(base::PLATFORM_FILE_OK,
1629 ofu()->EnsureFileExists(context.get(), src_path, &created));
1630 EXPECT_TRUE(created);
1631 FilePath src_local_path;
1632 context.reset(NewContext(NULL));
1633 EXPECT_EQ(base::PLATFORM_FILE_OK,
1634 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1635
1636 ClearTimestamp(dir_path);
1637 context.reset(NewContext(NULL));
1638 EXPECT_EQ(base::PLATFORM_FILE_OK,
1639 ofu()->CopyInForeignFile(context.get(), src_local_path, path));
1640 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1641}
1642
1643TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1644 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1645 const FilePath dir_path(FILE_PATH_LITERAL("foo_dir"));
1646
1647 // Create working directory.
1648 EXPECT_EQ(base::PLATFORM_FILE_OK,
1649 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1650
1651 // DeleteFile, delete case.
1652 FilePath path = dir_path.AppendASCII("DeleteFile_file");
1653 bool created = false;
1654 context.reset(NewContext(NULL));
1655 EXPECT_EQ(base::PLATFORM_FILE_OK,
1656 ofu()->EnsureFileExists(context.get(), path, &created));
1657 EXPECT_TRUE(created);
1658
1659 ClearTimestamp(dir_path);
1660 context.reset(NewContext(NULL));
1661 EXPECT_EQ(base::PLATFORM_FILE_OK,
1662 ofu()->DeleteFile(context.get(), path));
1663 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1664
1665 // fail case.
1666 ClearTimestamp(dir_path);
1667 context.reset(NewContext(NULL));
1668 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
1669 ofu()->DeleteFile(context.get(), path));
1670 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1671
1672 // DeleteSingleDirectory, fail case.
1673 path = dir_path.AppendASCII("DeleteSingleDirectory_dir");
1674 FilePath file_path(path.AppendASCII("pakeratta"));
1675 context.reset(NewContext(NULL));
1676 EXPECT_EQ(base::PLATFORM_FILE_OK,
1677 ofu()->CreateDirectory(context.get(), path, true, true));
1678 created = false;
1679 context.reset(NewContext(NULL));
1680 EXPECT_EQ(base::PLATFORM_FILE_OK,
1681 ofu()->EnsureFileExists(context.get(), file_path, &created));
1682 EXPECT_TRUE(created);
1683
1684 ClearTimestamp(dir_path);
1685 context.reset(NewContext(NULL));
1686 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
1687 ofu()->DeleteSingleDirectory(context.get(), path));
1688 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1689
1690 // delete case.
1691 context.reset(NewContext(NULL));
1692 EXPECT_EQ(base::PLATFORM_FILE_OK,
1693 ofu()->DeleteFile(context.get(), file_path));
1694
1695 ClearTimestamp(dir_path);
1696 context.reset(NewContext(NULL));
1697 EXPECT_EQ(base::PLATFORM_FILE_OK,
1698 ofu()->DeleteSingleDirectory(context.get(), path));
1699 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1700}
1701
1702TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1703 TestDirectoryTimestampHelper(
1704 FilePath(FILE_PATH_LITERAL("copy overwrite")), true, true);
1705 TestDirectoryTimestampHelper(
1706 FilePath(FILE_PATH_LITERAL("copy non-overwrite")), true, false);
1707 TestDirectoryTimestampHelper(
1708 FilePath(FILE_PATH_LITERAL("move overwrite")), false, true);
1709 TestDirectoryTimestampHelper(
1710 FilePath(FILE_PATH_LITERAL("move non-overwrite")), false, false);
1711}