blob: ec40c671465f8070d8f162cfd3dc8a09520a2a9f [file] [log] [blame]
[email protected]d4905e2e2011-05-13 21:56:321// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// 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
9#include "base/file_path.h"
10#include "base/file_util.h"
11#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1612#include "base/memory/scoped_callback_factory.h"
13#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]0c5ebe32011-08-19 22:37:1620#include "webkit/fileapi/file_system_path_manager.h"
[email protected]fcc2d5f2011-05-23 22:06:2621#include "webkit/fileapi/file_system_test_helper.h"
[email protected]0c5ebe32011-08-19 22:37:1622#include "webkit/fileapi/file_system_usage_cache.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]0c5ebe32011-08-19 22:37:16136 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
137 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]0c5ebe32011-08-19 22:37:16145 quota_manager_ = new quota::QuotaManager(
146 false /* is_incognito */,
147 data_dir_.path(),
148 base::MessageLoopProxy::current(),
149 base::MessageLoopProxy::current(),
150 NULL /* special storage policy */);
151
152 // Every time we create a new helper, it creates another context, which
153 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49154 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16155 file_system_context_ = new FileSystemContext(
156 base::MessageLoopProxy::current(),
157 base::MessageLoopProxy::current(),
158 new quota::MockSpecialStoragePolicy(),
159 quota_manager_->proxy(),
160 data_dir_.path(),
161 false /* incognito */,
162 true /* allow_file_access_from_files */,
163 false /* unlimited_quota */,
164 NULL /* path_manager */);
165
[email protected]7878ece2011-09-05 11:41:49166 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
167 file_system_context_->path_manager()->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]0c5ebe32011-08-19 22:37:16173 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
174 FileSystemOperationContext* context;
175 if (helper)
176 context = helper->NewOperationContext();
177 else
178 context = test_helper_.NewOperationContext();
179 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]d4905e2e2011-05-13 21:56:32180 return context;
181 }
182
[email protected]0c5ebe32011-08-19 22:37:16183 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49184 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16185 // Use this for tests which need to run in multiple origins; we need a test
186 // helper per origin.
187 FileSystemTestOriginHelper* NewHelper(
188 const GURL& origin, fileapi::FileSystemType type) {
189 FileSystemTestOriginHelper* helper =
190 new FileSystemTestOriginHelper(origin, type);
191
192 helper->SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49193 obfuscated_file_util_.get());
[email protected]0c5ebe32011-08-19 22:37:16194 return helper;
195 }
196
[email protected]7878ece2011-09-05 11:41:49197 ObfuscatedFileUtil* ofu() {
198 return obfuscated_file_util_.get();
[email protected]d4905e2e2011-05-13 21:56:32199 }
200
[email protected]6b931152011-05-20 21:02:35201 const FilePath& test_directory() const {
202 return data_dir_.path();
203 }
204
[email protected]0c5ebe32011-08-19 22:37:16205 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26206 return origin_;
207 }
208
209 fileapi::FileSystemType type() const {
210 return type_;
211 }
212
[email protected]0c5ebe32011-08-19 22:37:16213 void GetUsageFromQuotaManager() {
214 quota_manager_->GetUsageAndQuota(
215 origin(), test_helper_.storage_type(),
216 callback_factory_.NewCallback(
[email protected]7878ece2011-09-05 11:41:49217 &ObfuscatedFileUtilTest::OnGetUsage));
[email protected]0c5ebe32011-08-19 22:37:16218 MessageLoop::current()->RunAllPending();
219 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
220 }
221
222 void RevokeUsageCache() {
223 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
224 ASSERT_TRUE(test_helper_.RevokeUsageCache());
225 }
226
227 int64 SizeInUsageFile() {
228 return test_helper_.GetCachedOriginUsage();
229 }
230
231 int64 usage() const { return usage_; }
232
233 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
234 EXPECT_EQ(quota::kQuotaStatusOk, status);
235 quota_status_ = status;
236 usage_ = usage;
237 }
238
[email protected]d4905e2e2011-05-13 21:56:32239 void CheckFileAndCloseHandle(
240 const FilePath& virtual_path, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16241 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32242 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49243 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32244 context.get(), virtual_path, &local_path));
245
246 base::PlatformFileInfo file_info0;
247 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49248 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32249 context.get(), virtual_path, &file_info0, &data_path));
250 EXPECT_EQ(data_path, local_path);
251 EXPECT_TRUE(FileExists(data_path));
252 EXPECT_EQ(0, GetSize(data_path));
253
254 const char data[] = "test data";
255 const int length = arraysize(data) - 1;
256
257 if (base::kInvalidPlatformFileValue == file_handle) {
258 bool created = true;
259 PlatformFileError error;
260 file_handle = base::CreatePlatformFile(
261 data_path,
262 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
263 &created,
264 &error);
[email protected]81b7f662011-05-26 00:54:46265 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32266 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
267 EXPECT_FALSE(created);
268 }
269 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
270 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
271
272 base::PlatformFileInfo file_info1;
273 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16274 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49275 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32276 context.get(), virtual_path, &file_info1, &data_path));
277 EXPECT_EQ(data_path, local_path);
278
279 EXPECT_FALSE(file_info0.is_directory);
280 EXPECT_FALSE(file_info1.is_directory);
281 EXPECT_FALSE(file_info0.is_symbolic_link);
282 EXPECT_FALSE(file_info1.is_symbolic_link);
283 EXPECT_EQ(0, file_info0.size);
284 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32285 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32286
[email protected]0c5ebe32011-08-19 22:37:16287 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49288 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32289 context.get(), virtual_path, length * 2));
290 EXPECT_EQ(length * 2, GetSize(data_path));
291
[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]0c5ebe32011-08-19 22:37:16294 context.get(), virtual_path, 0));
295 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32296 }
297
298 void ValidateTestDirectory(
299 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17300 const std::set<FilePath::StringType>& files,
301 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32302 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17303 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32304 for (iter = files.begin(); iter != files.end(); ++iter) {
305 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16306 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32307 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49308 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17309 context.get(), root_path.Append(*iter),
[email protected]d4905e2e2011-05-13 21:56:32310 &created));
311 ASSERT_FALSE(created);
312 }
313 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16314 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49315 EXPECT_TRUE(ofu()->DirectoryExists(context.get(),
[email protected]89ee4272011-05-16 18:45:17316 root_path.Append(*iter)));
[email protected]d4905e2e2011-05-13 21:56:32317 }
318 }
319
320 void FillTestDirectory(
321 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17322 std::set<FilePath::StringType>* files,
323 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32324 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16325 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32326 std::vector<base::FileUtilProxy::Entry> entries;
327 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49328 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32329 EXPECT_EQ(0UL, entries.size());
330
331 files->clear();
[email protected]89ee4272011-05-16 18:45:17332 files->insert(FILE_PATH_LITERAL("first"));
333 files->insert(FILE_PATH_LITERAL("second"));
334 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32335 directories->clear();
[email protected]89ee4272011-05-16 18:45:17336 directories->insert(FILE_PATH_LITERAL("fourth"));
337 directories->insert(FILE_PATH_LITERAL("fifth"));
338 directories->insert(FILE_PATH_LITERAL("sixth"));
339 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32340 for (iter = files->begin(); iter != files->end(); ++iter) {
341 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16342 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32343 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49344 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17345 context.get(), root_path.Append(*iter), &created));
[email protected]d4905e2e2011-05-13 21:56:32346 ASSERT_TRUE(created);
347 }
348 for (iter = directories->begin(); iter != directories->end(); ++iter) {
349 bool exclusive = true;
350 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16351 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32352 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49353 ofu()->CreateDirectory(
[email protected]89ee4272011-05-16 18:45:17354 context.get(), root_path.Append(*iter), exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32355 }
356 ValidateTestDirectory(root_path, *files, *directories);
357 }
358
359 void TestReadDirectoryHelper(const FilePath& root_path) {
[email protected]89ee4272011-05-16 18:45:17360 std::set<FilePath::StringType> files;
361 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:32362 FillTestDirectory(root_path, &files, &directories);
363
364 scoped_ptr<FileSystemOperationContext> context;
365 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16366 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32367 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49368 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32369 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
370 EXPECT_EQ(files.size() + directories.size(), entries.size());
371 for (entry_iter = entries.begin(); entry_iter != entries.end();
372 ++entry_iter) {
373 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17374 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32375 if (iter != files.end()) {
376 EXPECT_FALSE(entry.is_directory);
377 files.erase(iter);
378 continue;
379 }
[email protected]89ee4272011-05-16 18:45:17380 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32381 EXPECT_FALSE(directories.end() == iter);
382 EXPECT_TRUE(entry.is_directory);
383 directories.erase(iter);
384 }
385 }
386
[email protected]2517cfa2011-08-25 05:12:41387 void TestTouchHelper(const FilePath& path, bool is_file) {
388 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32389 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16390
[email protected]2517cfa2011-08-25 05:12:41391 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32392 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49393 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32394 context.get(), path, last_access_time, last_modified_time));
395 FilePath local_path;
396 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16397 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49398 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32399 context.get(), path, &file_info, &local_path));
400 // We compare as time_t here to lower our resolution, to avoid false
401 // negatives caused by conversion to the local filesystem's native
402 // representation and back.
403 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
404
[email protected]0c5ebe32011-08-19 22:37:16405 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32406 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41407 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32408 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49409 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32410 context.get(), path, last_access_time, last_modified_time));
[email protected]0c5ebe32011-08-19 22:37:16411 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49412 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32413 context.get(), path, &file_info, &local_path));
414 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49415 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41416 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32417 }
418
[email protected]81b7f662011-05-26 00:54:46419 void TestCopyInForeignFileHelper(bool overwrite) {
420 ScopedTempDir source_dir;
421 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
422 FilePath root_path = source_dir.path();
423 FilePath src_path = root_path.AppendASCII("file_name");
424 FilePath dest_path(FILE_PATH_LITERAL("new file"));
425 int64 src_file_length = 87;
426
427 base::PlatformFileError error_code;
428 bool created = false;
429 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
430 base::PlatformFile file_handle =
431 base::CreatePlatformFile(
432 src_path, file_flags, &created, &error_code);
433 EXPECT_TRUE(created);
434 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
435 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
436 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
437 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
438
439 scoped_ptr<FileSystemOperationContext> context;
440
441 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16442 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46443 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49444 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]81b7f662011-05-26 00:54:46445 EXPECT_TRUE(created);
446 }
447
[email protected]0c5ebe32011-08-19 22:37:16448 const int64 path_cost =
[email protected]7878ece2011-09-05 11:41:49449 ObfuscatedFileUtil::ComputeFilePathCost(dest_path);
[email protected]0c5ebe32011-08-19 22:37:16450 if (!overwrite) {
451 // Verify that file creation requires sufficient quota for the path.
452 context.reset(NewContext(NULL));
453 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
454 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49455 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16456 }
457
458 context.reset(NewContext(NULL));
459 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46460 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49461 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16462
463 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49464 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16465 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49466 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16467 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46468 base::PlatformFileInfo file_info;
469 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49470 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]81b7f662011-05-26 00:54:46471 context.get(), dest_path, &file_info, &data_path));
472 EXPECT_NE(data_path, src_path);
473 EXPECT_TRUE(FileExists(data_path));
474 EXPECT_EQ(src_file_length, GetSize(data_path));
475
476 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49477 ofu()->DeleteFile(context.get(), dest_path));
[email protected]81b7f662011-05-26 00:54:46478 }
479
[email protected]d4905e2e2011-05-13 21:56:32480 private:
481 ScopedTempDir data_dir_;
[email protected]7878ece2011-09-05 11:41:49482 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16483 scoped_refptr<quota::QuotaManager> quota_manager_;
484 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26485 GURL origin_;
486 fileapi::FileSystemType type_;
[email protected]7878ece2011-09-05 11:41:49487 base::ScopedCallbackFactory<ObfuscatedFileUtilTest>
[email protected]0c5ebe32011-08-19 22:37:16488 callback_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26489 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16490 quota::QuotaStatusCode quota_status_;
491 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32492
[email protected]7878ece2011-09-05 11:41:49493 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32494};
495
[email protected]7878ece2011-09-05 11:41:49496TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32497 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
498 bool created;
499 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16500 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32501 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
502
503 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49504 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32505 context.get(), path, file_flags, &file_handle,
506 &created));
507
[email protected]0c5ebe32011-08-19 22:37:16508 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32509 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49510 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32511
512 path = UTF8ToFilePath("test file");
513
[email protected]0c5ebe32011-08-19 22:37:16514 // Verify that file creation requires sufficient quota for the path.
515 context.reset(NewContext(NULL));
516 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49517 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16518 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49519 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16520 context.get(), path, file_flags, &file_handle, &created));
521
522 context.reset(NewContext(NULL));
523 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49524 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32525 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49526 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32527 context.get(), path, file_flags, &file_handle, &created));
528 ASSERT_TRUE(created);
529 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
530
531 CheckFileAndCloseHandle(path, file_handle);
532
[email protected]0c5ebe32011-08-19 22:37:16533 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32534 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49535 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32536 context.get(), path, &local_path));
537 EXPECT_TRUE(file_util::PathExists(local_path));
538
[email protected]0c5ebe32011-08-19 22:37:16539 // Verify that deleting a file isn't stopped by zero quota, and that it frees
540 // up quote from its path.
541 context.reset(NewContext(NULL));
542 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32543 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49544 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32545 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]7878ece2011-09-05 11:41:49546 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16547 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32548
[email protected]0c5ebe32011-08-19 22:37:16549 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32550 bool exclusive = true;
551 bool recursive = true;
552 FilePath directory_path = UTF8ToFilePath("series/of/directories");
553 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49554 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32555 context.get(), directory_path, exclusive, recursive));
556
[email protected]0c5ebe32011-08-19 22:37:16557 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32558 file_handle = base::kInvalidPlatformFileValue;
559 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49560 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32561 context.get(), path, file_flags, &file_handle, &created));
562 ASSERT_TRUE(created);
563 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
564
565 CheckFileAndCloseHandle(path, file_handle);
566
[email protected]0c5ebe32011-08-19 22:37:16567 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49568 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32569 context.get(), path, &local_path));
570 EXPECT_TRUE(file_util::PathExists(local_path));
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_OK,
[email protected]7878ece2011-09-05 11:41:49574 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32575 EXPECT_FALSE(file_util::PathExists(local_path));
576}
577
[email protected]7878ece2011-09-05 11:41:49578TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32579 bool created = false;
580 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16581 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32582
583 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49584 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32585
[email protected]0c5ebe32011-08-19 22:37:16586 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32587 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49588 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32589 ASSERT_TRUE(created);
590
[email protected]0c5ebe32011-08-19 22:37:16591 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32592 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49593 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32594 context.get(), path, &local_path));
595 EXPECT_EQ(0, GetSize(local_path));
596
[email protected]0c5ebe32011-08-19 22:37:16597 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49598 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32599 context.get(), path, 10));
600 EXPECT_EQ(10, GetSize(local_path));
601
[email protected]0c5ebe32011-08-19 22:37:16602 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49603 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32604 context.get(), path, 1));
605 EXPECT_EQ(1, GetSize(local_path));
606
[email protected]0c5ebe32011-08-19 22:37:16607 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49608 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16609 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49610 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32611}
612
[email protected]7878ece2011-09-05 11:41:49613TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]d4905e2e2011-05-13 21:56:32614 FilePath path = UTF8ToFilePath("fake/file");
615 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16616 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32617 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49618 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32619 context.get(), path, &created));
620
[email protected]0c5ebe32011-08-19 22:37:16621 // Verify that file creation requires sufficient quota for the path.
622 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32623 path = UTF8ToFilePath("test file");
624 created = false;
[email protected]0c5ebe32011-08-19 22:37:16625 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49626 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16627 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49628 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16629 ASSERT_FALSE(created);
630
631 context.reset(NewContext(NULL));
632 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49633 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32634 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49635 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32636 ASSERT_TRUE(created);
637
638 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
639
[email protected]0c5ebe32011-08-19 22:37:16640 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32641 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49642 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32643 ASSERT_FALSE(created);
644
645 // Also test in a subdirectory.
646 path = UTF8ToFilePath("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16647 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32648 bool exclusive = true;
649 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49650 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32651 context.get(), path.DirName(), exclusive, recursive));
652
[email protected]0c5ebe32011-08-19 22:37:16653 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32654 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49655 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32656 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16657 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49658 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16659 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49660 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32661}
662
[email protected]7878ece2011-09-05 11:41:49663TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16664 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32665
666 bool exclusive = false;
667 bool recursive = false;
668 FilePath path = UTF8ToFilePath("foo/bar");
[email protected]7878ece2011-09-05 11:41:49669 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32670 context.get(), path, exclusive, recursive));
671
[email protected]0c5ebe32011-08-19 22:37:16672 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32673 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49674 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32675
676 FilePath root = UTF8ToFilePath("");
[email protected]0c5ebe32011-08-19 22:37:16677 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49678 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16679 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49680 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16681 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49682 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32683
[email protected]0c5ebe32011-08-19 22:37:16684 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32685 exclusive = false;
686 recursive = true;
[email protected]7878ece2011-09-05 11:41:49687 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32688 context.get(), path, exclusive, recursive));
689
[email protected]0c5ebe32011-08-19 22:37:16690 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49691 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16692 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49693 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16694 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49695 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]0c5ebe32011-08-19 22:37:16696 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49697 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16698 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49699 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32700
701 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16702 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06703 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49704 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32705
706 base::PlatformFileInfo file_info;
707 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49708 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32709 context.get(), path, &file_info, &local_path));
710 EXPECT_TRUE(local_path.empty());
711 EXPECT_TRUE(file_info.is_directory);
712 EXPECT_FALSE(file_info.is_symbolic_link);
713
714 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16715 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32717 context.get(), path, exclusive, recursive));
718
719 exclusive = true;
720 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16721 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49722 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32723 context.get(), path, exclusive, recursive));
724
[email protected]0c5ebe32011-08-19 22:37:16725 // Verify that deleting a directory isn't stopped by zero quota, and that it
726 // frees up quota from its path.
727 context.reset(NewContext(NULL));
728 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32729 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49730 ofu()->DeleteSingleDirectory(context.get(), path));
731 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16732 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32733
734 path = UTF8ToFilePath("foo/bop");
735
[email protected]0c5ebe32011-08-19 22:37:16736 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49737 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16738 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49739 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16740 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49741 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
742 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32743 context.get(), path, &file_info, &local_path));
744
[email protected]0c5ebe32011-08-19 22:37:16745 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32746 exclusive = true;
747 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16748 context.reset(NewContext(NULL));
749 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49750 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
751 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16752 context.get(), path, exclusive, recursive));
753
754 context.reset(NewContext(NULL));
755 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49756 ObfuscatedFileUtil::ComputeFilePathCost(path));
757 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32758 context.get(), path, exclusive, recursive));
759
[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));
[email protected]0c5ebe32011-08-19 22:37:16762 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49763 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32764
765 exclusive = true;
766 recursive = false;
[email protected]7878ece2011-09-05 11:41:49767 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32768 context.get(), path, exclusive, recursive));
769
770 exclusive = true;
771 recursive = false;
772 path = UTF8ToFilePath("foo");
[email protected]7878ece2011-09-05 11:41:49773 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32774 context.get(), path, exclusive, recursive));
775
776 path = UTF8ToFilePath("blah");
777
[email protected]0c5ebe32011-08-19 22:37:16778 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49779 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16780 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49781 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32782
783 exclusive = true;
784 recursive = false;
[email protected]7878ece2011-09-05 11:41:49785 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32786 context.get(), path, exclusive, recursive));
787
[email protected]0c5ebe32011-08-19 22:37:16788 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49789 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16790 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49791 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32792
793 exclusive = true;
794 recursive = false;
[email protected]7878ece2011-09-05 11:41:49795 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32796 context.get(), path, exclusive, recursive));
797}
798
[email protected]7878ece2011-09-05 11:41:49799TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16800 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32801 bool exclusive = true;
802 bool recursive = true;
803 FilePath path = UTF8ToFilePath("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49804 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32805 context.get(), path, exclusive, recursive));
806 TestReadDirectoryHelper(path);
807}
808
[email protected]7878ece2011-09-05 11:41:49809TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]d4905e2e2011-05-13 21:56:32810 TestReadDirectoryHelper(UTF8ToFilePath(""));
811}
812
[email protected]7878ece2011-09-05 11:41:49813TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]d4905e2e2011-05-13 21:56:32814 TestReadDirectoryHelper(UTF8ToFilePath("/"));
815}
816
[email protected]7878ece2011-09-05 11:41:49817TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]d4905e2e2011-05-13 21:56:32818 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16819 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32820
821 bool created = false;
822 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49823 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32824 ASSERT_TRUE(created);
825
[email protected]0c5ebe32011-08-19 22:37:16826 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32827 std::vector<base::FileUtilProxy::Entry> entries;
828 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49829 ofu()->ReadDirectory(context.get(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32830
[email protected]7878ece2011-09-05 11:41:49831 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32832}
833
[email protected]7878ece2011-09-05 11:41:49834TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]2517cfa2011-08-25 05:12:41835 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16836 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41837
838 base::Time last_access_time = base::Time::Now();
839 base::Time last_modified_time = base::Time::Now();
840
841 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:32842 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49843 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32844 context.get(), path, last_access_time, last_modified_time));
845
[email protected]2517cfa2011-08-25 05:12:41846 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:16847 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41848 bool created = false;
849 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49850 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41851 ASSERT_TRUE(created);
852 TestTouchHelper(path, true);
853
854 // Now test a directory:
855 context.reset(NewContext(NULL));
856 bool exclusive = true;
857 bool recursive = false;
858 path = UTF8ToFilePath("dir");
[email protected]7878ece2011-09-05 11:41:49859 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:41860 path, exclusive, recursive));
861 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:16862}
863
[email protected]7878ece2011-09-05 11:41:49864TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:16865 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16866 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
867
[email protected]0c5ebe32011-08-19 22:37:16868 path = UTF8ToFilePath("file name");
869 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:41870 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16871 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49872 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41873 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:16874 context->set_allowed_bytes_growth(1024);
875 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49876 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41877 EXPECT_TRUE(created);
[email protected]7878ece2011-09-05 11:41:49878 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:16879 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
880
881 context->set_allowed_bytes_growth(1024);
882 bool exclusive = true;
883 bool recursive = true;
884 path = UTF8ToFilePath("directory/to/use");
885 std::vector<FilePath::StringType> components;
886 path.GetComponents(&components);
887 path_cost = 0;
888 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
889 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:49890 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:16891 FilePath(*iter));
892 }
893 context.reset(NewContext(NULL));
894 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:49895 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16896 context.get(), path, exclusive, recursive));
897 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32898}
899
[email protected]7878ece2011-09-05 11:41:49900TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]d4905e2e2011-05-13 21:56:32901 FilePath source_path = UTF8ToFilePath("path0.txt");
902 FilePath dest_path = UTF8ToFilePath("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:16903 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32904
905 bool is_copy_not_move = false;
906 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49907 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32908 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16909 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32910 is_copy_not_move = true;
911 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49912 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32913 is_copy_not_move));
914 source_path = UTF8ToFilePath("dir/dir/file");
915 bool exclusive = true;
916 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16917 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49918 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32919 context.get(), source_path.DirName(), exclusive, recursive));
920 is_copy_not_move = false;
921 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49922 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32923 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16924 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32925 is_copy_not_move = true;
926 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49927 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32928 is_copy_not_move));
929}
930
[email protected]7878ece2011-09-05 11:41:49931TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:32932 const int64 kSourceLength = 5;
933 const int64 kDestLength = 50;
934
935 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
936 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
937 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
938 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
939 test_case.is_copy_not_move);
940 SCOPED_TRACE(testing::Message() << "\t source_path " <<
941 test_case.source_path);
942 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
943 test_case.dest_path);
944 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
945 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:16946 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32947
948 bool exclusive = false;
949 bool recursive = true;
950 FilePath source_path = UTF8ToFilePath(test_case.source_path);
951 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
952
[email protected]0c5ebe32011-08-19 22:37:16953 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49954 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32955 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:16956 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49957 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32958 context.get(), dest_path.DirName(), exclusive, recursive));
959
[email protected]d4905e2e2011-05-13 21:56:32960 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16961 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32962 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49963 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:32964 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16965 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32966 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49967 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:32968
969 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16970 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32971 created = false;
972 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49973 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:32974 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16975 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32976 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49977 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:32978 }
979
[email protected]0c5ebe32011-08-19 22:37:16980 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49981 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:32982 source_path, dest_path, test_case.is_copy_not_move));
983 if (test_case.is_copy_not_move) {
984 base::PlatformFileInfo file_info;
985 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:16986 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49987 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32988 context.get(), source_path, &file_info, &local_path));
989 EXPECT_EQ(kSourceLength, file_info.size);
990 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49991 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:32992 } else {
993 base::PlatformFileInfo file_info;
994 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:16995 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49996 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32997 context.get(), source_path, &file_info, &local_path));
998 }
999 base::PlatformFileInfo file_info;
1000 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491001 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321002 context.get(), dest_path, &file_info, &local_path));
1003 EXPECT_EQ(kSourceLength, file_info.size);
1004
1005 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491006 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321007 }
1008}
1009
[email protected]7878ece2011-09-05 11:41:491010TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:161011 FilePath src_path = UTF8ToFilePath("src path");
1012 FilePath dest_path = UTF8ToFilePath("destination path");
1013 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1014 bool created = false;
[email protected]7878ece2011-09-05 11:41:491015 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161016 context.get(), src_path, &created));
1017
1018 bool is_copy = true;
1019 // Copy, no overwrite.
1020 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491021 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161022 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491023 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161024 context.reset(NewContext(NULL));
1025 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491026 ObfuscatedFileUtil::ComputeFilePathCost(dest_path));
[email protected]0c5ebe32011-08-19 22:37:161027 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491028 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161029
1030 // Copy, with overwrite.
1031 context.reset(NewContext(NULL));
1032 context->set_allowed_bytes_growth(0);
1033 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491034 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161035}
1036
[email protected]7878ece2011-09-05 11:41:491037TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]0c5ebe32011-08-19 22:37:161038 FilePath src_path = UTF8ToFilePath("src path");
1039 FilePath dest_path = UTF8ToFilePath("destination path");
1040 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1041 bool created = false;
[email protected]7878ece2011-09-05 11:41:491042 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161043 context.get(), src_path, &created));
1044
1045 bool is_copy = false;
1046 // Move, rename, no overwrite.
1047 context.reset(NewContext(NULL));
1048 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491049 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1050 ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161051 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491052 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161053 context.reset(NewContext(NULL));
1054 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491055 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1056 ObfuscatedFileUtil::ComputeFilePathCost(src_path));
[email protected]0c5ebe32011-08-19 22:37:161057 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491058 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161059
1060 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491061 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161062 context.get(), src_path, &created));
1063
1064 // Move, rename, with overwrite.
1065 context.reset(NewContext(NULL));
1066 context->set_allowed_bytes_growth(0);
1067 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491068 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161069}
1070
[email protected]7878ece2011-09-05 11:41:491071TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]0c5ebe32011-08-19 22:37:161072 FilePath src_path = UTF8ToFilePath("src path");
1073 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1074 bool created = false;
[email protected]7878ece2011-09-05 11:41:491075 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161076 context.get(), src_path, &created));
1077
1078 bool exclusive = true;
1079 bool recursive = false;
1080 FilePath dir_path = UTF8ToFilePath("directory path");
1081 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491082 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161083 context.get(), dir_path, exclusive, recursive));
1084
1085 FilePath dest_path = dir_path.Append(src_path);
1086
1087 bool is_copy = false;
1088 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1089 // Move, no rename, no overwrite.
1090 context.reset(NewContext(NULL));
1091 context->set_allowed_bytes_growth(allowed_bytes_growth);
1092 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491093 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161094 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1095
1096 // Move, no rename, with overwrite.
1097 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491098 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161099 context.get(), src_path, &created));
1100 context.reset(NewContext(NULL));
1101 context->set_allowed_bytes_growth(allowed_bytes_growth);
1102 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491103 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161104 EXPECT_EQ(
1105 allowed_bytes_growth +
[email protected]7878ece2011-09-05 11:41:491106 ObfuscatedFileUtil::ComputeFilePathCost(src_path),
[email protected]0c5ebe32011-08-19 22:37:161107 context->allowed_bytes_growth());
1108}
1109
[email protected]7878ece2011-09-05 11:41:491110TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461111 TestCopyInForeignFileHelper(false /* overwrite */);
1112 TestCopyInForeignFileHelper(true /* overwrite */);
1113}
1114
[email protected]7878ece2011-09-05 11:41:491115TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161116 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321117 FilePath src_path = UTF8ToFilePath("source dir");
1118 bool exclusive = true;
1119 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491120 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321121 context.get(), src_path, exclusive, recursive));
1122
[email protected]89ee4272011-05-16 18:45:171123 std::set<FilePath::StringType> files;
1124 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321125 FillTestDirectory(src_path, &files, &directories);
1126
1127 FilePath dest_path = UTF8ToFilePath("destination dir");
1128
[email protected]0c5ebe32011-08-19 22:37:161129 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491130 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161131 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321132 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491133 ofu()->Copy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321134
1135 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161136 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491137 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161138 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491139 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161140 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321141 recursive = true;
1142 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491143 ofu()->Delete(context.get(), dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161144 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491145 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321146}
[email protected]6b931152011-05-20 21:02:351147
[email protected]7878ece2011-09-05 11:41:491148TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351149 ScopedTempDir source_dir;
1150 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1151 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1152 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1153
1154 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1155 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1156 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1157 FilePath local_src_path = root_path.Append(test_case.path);
1158 if (test_case.is_directory) {
1159 ASSERT_TRUE(
1160 file_util::CreateDirectory(local_src_path));
1161 } else {
1162 base::PlatformFileError error_code;
1163 bool created = false;
1164 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
1165 base::PlatformFile file_handle =
1166 base::CreatePlatformFile(
1167 local_src_path, file_flags, &created, &error_code);
1168 EXPECT_TRUE(created);
[email protected]81b7f662011-05-26 00:54:461169 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]6b931152011-05-20 21:02:351170 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
1171 ASSERT_TRUE(
1172 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
1173 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1174 }
1175 }
1176
[email protected]7878ece2011-09-05 11:41:491177 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351178
1179 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161180 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491181 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]6b931152011-05-20 21:02:351182 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1183 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
1184 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1185 FilePath local_data_path = new_root.Append(test_case.path);
1186#if defined(OS_WIN)
1187 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
1188#endif
[email protected]0c5ebe32011-08-19 22:37:161189 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491190 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351191 FilePath data_path;
1192 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1193 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491194 ofu()->GetFileInfo(context.get(), FilePath(test_case.path),
1195 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351196 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491197 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351198 } else {
1199 base::PlatformFileInfo platform_file_info;
1200 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1201 local_data_path.value());
1202 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1203 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1204 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1205 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161206 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351207 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491208 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1209 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351210 }
1211 }
1212}
[email protected]fcc2d5f2011-05-23 22:06:261213
[email protected]7878ece2011-09-05 11:41:491214TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1215 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1216 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161217 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261218 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161219 EXPECT_EQ(origin(), enumerator->Next());
1220 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1221 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1222 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261223 EXPECT_EQ(GURL(), enumerator->Next());
1224 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1225 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1226
1227 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161228 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261229
1230 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1231 SCOPED_TRACE(testing::Message() <<
1232 "Validating kOriginEnumerationTestRecords " << i);
1233 const OriginEnumerationTestRecord& record =
1234 kOriginEnumerationTestRecords[i];
1235 GURL origin_url(record.origin_url);
1236 origins_expected.insert(origin_url);
1237 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161238 scoped_ptr<FileSystemTestOriginHelper> helper(
1239 NewHelper(origin_url, kFileSystemTypeTemporary));
1240 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261241 context->set_src_origin_url(origin_url);
1242 context->set_src_type(kFileSystemTypeTemporary);
1243 bool created = false;
1244 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491245 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261246 FilePath().AppendASCII("file"), &created));
1247 EXPECT_TRUE(created);
1248 }
1249 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161250 scoped_ptr<FileSystemTestOriginHelper> helper(
1251 NewHelper(origin_url, kFileSystemTypePersistent));
1252 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261253 context->set_src_origin_url(origin_url);
1254 context->set_src_type(kFileSystemTypePersistent);
1255 bool created = false;
1256 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491257 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261258 FilePath().AppendASCII("file"), &created));
1259 EXPECT_TRUE(created);
1260 }
1261 }
[email protected]7878ece2011-09-05 11:41:491262 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261263 EXPECT_TRUE(enumerator.get());
1264 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161265 GURL origin_url;
1266 while (!(origin_url = enumerator->Next()).is_empty()) {
1267 origins_found.insert(origin_url);
1268 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261269 bool found = false;
1270 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1271 ++i) {
1272 const OriginEnumerationTestRecord& record =
1273 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161274 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261275 continue;
1276 found = true;
1277 EXPECT_EQ(record.has_temporary,
1278 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1279 EXPECT_EQ(record.has_persistent,
1280 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1281 }
[email protected]0c5ebe32011-08-19 22:37:161282 // Deal with the default filesystem created by the test helper.
1283 if (!found && origin_url == origin()) {
1284 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1285 EXPECT_EQ(true,
1286 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1287 EXPECT_EQ(false,
1288 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1289 found = true;
1290 }
[email protected]fcc2d5f2011-05-23 22:06:261291 EXPECT_TRUE(found);
1292 }
1293
1294 std::set<GURL> diff;
1295 std::set_symmetric_difference(origins_expected.begin(),
1296 origins_expected.end(), origins_found.begin(), origins_found.end(),
1297 inserter(diff, diff.begin()));
1298 EXPECT_TRUE(diff.empty());
1299}
[email protected]0c5ebe32011-08-19 22:37:161300
[email protected]7878ece2011-09-05 11:41:491301TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161302 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1303
1304 int64 expected_quota = 0;
1305
1306 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1307 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1308 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1309 FilePath path(test_case.path);
[email protected]7878ece2011-09-05 11:41:491310 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:161311 if (test_case.is_directory) {
1312 bool exclusive = true;
1313 bool recursive = false;
1314 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491315 ofu()->CreateDirectory(context.get(), path, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161316 } else {
1317 bool created = false;
1318 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491319 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:161320 ASSERT_TRUE(created);
1321 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491322 ofu()->Truncate(context.get(), path,
[email protected]0c5ebe32011-08-19 22:37:161323 test_case.data_file_size));
1324 expected_quota += test_case.data_file_size;
1325 }
1326 }
1327 EXPECT_EQ(expected_quota, SizeInUsageFile());
1328 RevokeUsageCache();
1329 EXPECT_EQ(-1, SizeInUsageFile());
1330 GetUsageFromQuotaManager();
1331 EXPECT_EQ(expected_quota, SizeInUsageFile());
1332 EXPECT_EQ(expected_quota, usage());
1333}
[email protected]34583332011-08-31 08:59:471334
[email protected]7878ece2011-09-05 11:41:491335TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]34583332011-08-31 08:59:471336 const FilePath kPath1 = FilePath().AppendASCII("hoge");
1337 const FilePath kPath2 = FilePath().AppendASCII("fuga");
1338
1339 scoped_ptr<FileSystemOperationContext> context;
1340 base::PlatformFile file;
1341 base::PlatformFileInfo file_info;
1342 FilePath data_path;
1343 bool created = false;
1344
1345 // Create a non-empty file.
1346 context.reset(NewContext(NULL));
1347 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491348 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471349 EXPECT_TRUE(created);
1350 context.reset(NewContext(NULL));
1351 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491352 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471353 context.reset(NewContext(NULL));
1354 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491355 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471356 context.get(), kPath1, &file_info, &data_path));
1357 EXPECT_EQ(10, file_info.size);
1358
1359 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491360 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471361
1362 // Try to get file info of broken file.
1363 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491364 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471365 context.reset(NewContext(NULL));
1366 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491367 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471368 EXPECT_TRUE(created);
1369 context.reset(NewContext(NULL));
1370 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491371 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471372 context.get(), kPath1, &file_info, &data_path));
1373 EXPECT_EQ(0, file_info.size);
1374
1375 // Make another broken file to |kPath2|.
1376 context.reset(NewContext(NULL));
1377 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491378 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471379 EXPECT_TRUE(created);
1380
1381 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491382 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471383
1384 // Repair broken |kPath1|.
1385 context.reset(NewContext(NULL));
1386 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491387 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471388 base::Time::Now()));
1389 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491390 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471391 EXPECT_TRUE(created);
1392
1393 // Copy from sound |kPath1| to broken |kPath2|.
1394 context.reset(NewContext(NULL));
1395 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491396 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]34583332011-08-31 08:59:471397 true /* copy */));
1398
[email protected]7878ece2011-09-05 11:41:491399 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471400 context.reset(NewContext(NULL));
1401 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491402 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471403 context.get(), kPath1,
1404 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1405 &file, &created));
1406 EXPECT_TRUE(created);
1407 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1408 EXPECT_EQ(0, file_info.size);
1409 EXPECT_TRUE(base::ClosePlatformFile(file));
1410}
[email protected]9dfdc0e32011-09-02 06:07:441411
[email protected]7878ece2011-09-05 11:41:491412TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]9dfdc0e32011-09-02 06:07:441413 const FilePath kPath[] = {
1414 FilePath().AppendASCII("foo"),
1415 FilePath().AppendASCII("bar"),
1416 FilePath().AppendASCII("baz")
1417 };
1418 scoped_ptr<FileSystemOperationContext> context;
1419
1420 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1421 bool created = false;
1422 context.reset(NewContext(NULL));
1423 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491424 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441425 EXPECT_TRUE(created);
1426 }
1427
1428 context.reset(NewContext(NULL));
1429 std::vector<base::FileUtilProxy::Entry> entries;
1430 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491431 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441432 EXPECT_EQ(3u, entries.size());
1433
1434 context.reset(NewContext(NULL));
1435 FilePath local_path;
1436 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491437 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441438 EXPECT_TRUE(file_util::Delete(local_path, false));
1439
1440 context.reset(NewContext(NULL));
1441 entries.clear();
1442 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491443 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441444 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1445}