blob: af46597e33c1db8839d4a9f260728d53cdfd4d88 [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
[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]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]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]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 */,
[email protected]0c5ebe32011-08-19 22:37:16163 NULL /* path_manager */);
164
[email protected]7878ece2011-09-05 11:41:49165 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
166 file_system_context_->path_manager()->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16167
168 test_helper_.SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49169 obfuscated_file_util_.get());
[email protected]d4905e2e2011-05-13 21:56:32170 }
171
[email protected]0c5ebe32011-08-19 22:37:16172 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
173 FileSystemOperationContext* context;
174 if (helper)
175 context = helper->NewOperationContext();
176 else
177 context = test_helper_.NewOperationContext();
178 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]d4905e2e2011-05-13 21:56:32179 return context;
180 }
181
[email protected]0c5ebe32011-08-19 22:37:16182 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49183 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16184 // Use this for tests which need to run in multiple origins; we need a test
185 // helper per origin.
186 FileSystemTestOriginHelper* NewHelper(
187 const GURL& origin, fileapi::FileSystemType type) {
188 FileSystemTestOriginHelper* helper =
189 new FileSystemTestOriginHelper(origin, type);
190
191 helper->SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49192 obfuscated_file_util_.get());
[email protected]0c5ebe32011-08-19 22:37:16193 return helper;
194 }
195
[email protected]7878ece2011-09-05 11:41:49196 ObfuscatedFileUtil* ofu() {
197 return obfuscated_file_util_.get();
[email protected]d4905e2e2011-05-13 21:56:32198 }
199
[email protected]6b931152011-05-20 21:02:35200 const FilePath& test_directory() const {
201 return data_dir_.path();
202 }
203
[email protected]0c5ebe32011-08-19 22:37:16204 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26205 return origin_;
206 }
207
208 fileapi::FileSystemType type() const {
209 return type_;
210 }
211
[email protected]0c5ebe32011-08-19 22:37:16212 void GetUsageFromQuotaManager() {
213 quota_manager_->GetUsageAndQuota(
214 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03215 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
216 weak_factory_.GetWeakPtr()));
[email protected]0c5ebe32011-08-19 22:37:16217 MessageLoop::current()->RunAllPending();
218 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
219 }
220
221 void RevokeUsageCache() {
222 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
223 ASSERT_TRUE(test_helper_.RevokeUsageCache());
224 }
225
226 int64 SizeInUsageFile() {
227 return test_helper_.GetCachedOriginUsage();
228 }
229
230 int64 usage() const { return usage_; }
231
232 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
233 EXPECT_EQ(quota::kQuotaStatusOk, status);
234 quota_status_ = status;
235 usage_ = usage;
236 }
237
[email protected]d4905e2e2011-05-13 21:56:32238 void CheckFileAndCloseHandle(
239 const FilePath& virtual_path, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16240 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32241 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49242 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32243 context.get(), virtual_path, &local_path));
244
245 base::PlatformFileInfo file_info0;
246 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49247 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32248 context.get(), virtual_path, &file_info0, &data_path));
249 EXPECT_EQ(data_path, local_path);
250 EXPECT_TRUE(FileExists(data_path));
251 EXPECT_EQ(0, GetSize(data_path));
252
253 const char data[] = "test data";
254 const int length = arraysize(data) - 1;
255
256 if (base::kInvalidPlatformFileValue == file_handle) {
257 bool created = true;
258 PlatformFileError error;
259 file_handle = base::CreatePlatformFile(
260 data_path,
261 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
262 &created,
263 &error);
[email protected]81b7f662011-05-26 00:54:46264 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32265 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
266 EXPECT_FALSE(created);
267 }
268 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
269 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
270
271 base::PlatformFileInfo file_info1;
272 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16273 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49274 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32275 context.get(), virtual_path, &file_info1, &data_path));
276 EXPECT_EQ(data_path, local_path);
277
278 EXPECT_FALSE(file_info0.is_directory);
279 EXPECT_FALSE(file_info1.is_directory);
280 EXPECT_FALSE(file_info0.is_symbolic_link);
281 EXPECT_FALSE(file_info1.is_symbolic_link);
282 EXPECT_EQ(0, file_info0.size);
283 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32284 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32285
[email protected]0c5ebe32011-08-19 22:37:16286 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49287 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32288 context.get(), virtual_path, length * 2));
289 EXPECT_EQ(length * 2, GetSize(data_path));
290
[email protected]0c5ebe32011-08-19 22:37:16291 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49292 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]0c5ebe32011-08-19 22:37:16293 context.get(), virtual_path, 0));
294 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32295 }
296
297 void ValidateTestDirectory(
298 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17299 const std::set<FilePath::StringType>& files,
300 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32301 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17302 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32303 for (iter = files.begin(); iter != files.end(); ++iter) {
304 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16305 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32306 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49307 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17308 context.get(), root_path.Append(*iter),
[email protected]d4905e2e2011-05-13 21:56:32309 &created));
310 ASSERT_FALSE(created);
311 }
312 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16313 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49314 EXPECT_TRUE(ofu()->DirectoryExists(context.get(),
[email protected]89ee4272011-05-16 18:45:17315 root_path.Append(*iter)));
[email protected]d4905e2e2011-05-13 21:56:32316 }
317 }
318
319 void FillTestDirectory(
320 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17321 std::set<FilePath::StringType>* files,
322 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32323 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16324 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32325 std::vector<base::FileUtilProxy::Entry> entries;
326 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49327 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32328 EXPECT_EQ(0UL, entries.size());
329
330 files->clear();
[email protected]89ee4272011-05-16 18:45:17331 files->insert(FILE_PATH_LITERAL("first"));
332 files->insert(FILE_PATH_LITERAL("second"));
333 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32334 directories->clear();
[email protected]89ee4272011-05-16 18:45:17335 directories->insert(FILE_PATH_LITERAL("fourth"));
336 directories->insert(FILE_PATH_LITERAL("fifth"));
337 directories->insert(FILE_PATH_LITERAL("sixth"));
338 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32339 for (iter = files->begin(); iter != files->end(); ++iter) {
340 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16341 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32342 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49343 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17344 context.get(), root_path.Append(*iter), &created));
[email protected]d4905e2e2011-05-13 21:56:32345 ASSERT_TRUE(created);
346 }
347 for (iter = directories->begin(); iter != directories->end(); ++iter) {
348 bool exclusive = true;
349 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16350 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32351 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49352 ofu()->CreateDirectory(
[email protected]89ee4272011-05-16 18:45:17353 context.get(), root_path.Append(*iter), exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32354 }
355 ValidateTestDirectory(root_path, *files, *directories);
356 }
357
358 void TestReadDirectoryHelper(const FilePath& root_path) {
[email protected]89ee4272011-05-16 18:45:17359 std::set<FilePath::StringType> files;
360 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:32361 FillTestDirectory(root_path, &files, &directories);
362
363 scoped_ptr<FileSystemOperationContext> context;
364 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16365 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32366 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49367 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32368 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
369 EXPECT_EQ(files.size() + directories.size(), entries.size());
370 for (entry_iter = entries.begin(); entry_iter != entries.end();
371 ++entry_iter) {
372 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17373 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32374 if (iter != files.end()) {
375 EXPECT_FALSE(entry.is_directory);
376 files.erase(iter);
377 continue;
378 }
[email protected]89ee4272011-05-16 18:45:17379 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32380 EXPECT_FALSE(directories.end() == iter);
381 EXPECT_TRUE(entry.is_directory);
382 directories.erase(iter);
383 }
384 }
385
[email protected]2517cfa2011-08-25 05:12:41386 void TestTouchHelper(const FilePath& path, bool is_file) {
387 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32388 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16389
[email protected]2517cfa2011-08-25 05:12:41390 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32391 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49392 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32393 context.get(), path, last_access_time, last_modified_time));
394 FilePath local_path;
395 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16396 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49397 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32398 context.get(), path, &file_info, &local_path));
399 // We compare as time_t here to lower our resolution, to avoid false
400 // negatives caused by conversion to the local filesystem's native
401 // representation and back.
402 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
403
[email protected]0c5ebe32011-08-19 22:37:16404 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32405 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41406 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32407 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49408 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32409 context.get(), path, last_access_time, last_modified_time));
[email protected]0c5ebe32011-08-19 22:37:16410 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49411 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32412 context.get(), path, &file_info, &local_path));
413 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49414 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41415 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32416 }
417
[email protected]81b7f662011-05-26 00:54:46418 void TestCopyInForeignFileHelper(bool overwrite) {
419 ScopedTempDir source_dir;
420 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
421 FilePath root_path = source_dir.path();
422 FilePath src_path = root_path.AppendASCII("file_name");
423 FilePath dest_path(FILE_PATH_LITERAL("new file"));
424 int64 src_file_length = 87;
425
426 base::PlatformFileError error_code;
427 bool created = false;
428 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
429 base::PlatformFile file_handle =
430 base::CreatePlatformFile(
431 src_path, file_flags, &created, &error_code);
432 EXPECT_TRUE(created);
433 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
434 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
435 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
436 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
437
438 scoped_ptr<FileSystemOperationContext> context;
439
440 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16441 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46442 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49443 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]81b7f662011-05-26 00:54:46444 EXPECT_TRUE(created);
445 }
446
[email protected]0c5ebe32011-08-19 22:37:16447 const int64 path_cost =
[email protected]7878ece2011-09-05 11:41:49448 ObfuscatedFileUtil::ComputeFilePathCost(dest_path);
[email protected]0c5ebe32011-08-19 22:37:16449 if (!overwrite) {
450 // Verify that file creation requires sufficient quota for the path.
451 context.reset(NewContext(NULL));
452 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
453 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49454 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16455 }
456
457 context.reset(NewContext(NULL));
458 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46459 EXPECT_EQ(base::PLATFORM_FILE_OK,
[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 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49463 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16464 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49465 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16466 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46467 base::PlatformFileInfo file_info;
468 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49469 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]81b7f662011-05-26 00:54:46470 context.get(), dest_path, &file_info, &data_path));
471 EXPECT_NE(data_path, src_path);
472 EXPECT_TRUE(FileExists(data_path));
473 EXPECT_EQ(src_file_length, GetSize(data_path));
474
475 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49476 ofu()->DeleteFile(context.get(), dest_path));
[email protected]81b7f662011-05-26 00:54:46477 }
478
[email protected]fad625e2f2011-12-08 05:38:03479 void ClearTimestamp(const FilePath& path) {
480 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
481 EXPECT_EQ(base::PLATFORM_FILE_OK,
482 ofu()->Touch(context.get(), path, base::Time(), base::Time()));
483 EXPECT_EQ(base::Time(), GetModifiedTime(path));
484 }
485
486 base::Time GetModifiedTime(const FilePath& path) {
487 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
488 FilePath data_path;
489 base::PlatformFileInfo file_info;
490 context.reset(NewContext(NULL));
491 EXPECT_EQ(base::PLATFORM_FILE_OK,
492 ofu()->GetFileInfo(context.get(), path, &file_info, &data_path));
493 return file_info.last_modified;
494 }
495
496 void TestDirectoryTimestampHelper(const FilePath& base_dir,
497 bool copy,
498 bool overwrite) {
499 scoped_ptr<FileSystemOperationContext> context;
500 const FilePath src_dir_path(base_dir.AppendASCII("foo_dir"));
501 const FilePath dest_dir_path(base_dir.AppendASCII("bar_dir"));
502
503 const FilePath src_file_path(src_dir_path.AppendASCII("hoge"));
504 const FilePath dest_file_path(dest_dir_path.AppendASCII("fuga"));
505
506 context.reset(NewContext(NULL));
507 EXPECT_EQ(base::PLATFORM_FILE_OK,
508 ofu()->CreateDirectory(context.get(), src_dir_path, true, true));
509 context.reset(NewContext(NULL));
510 EXPECT_EQ(base::PLATFORM_FILE_OK,
511 ofu()->CreateDirectory(context.get(), dest_dir_path, true, true));
512
513 bool created = false;
514 context.reset(NewContext(NULL));
515 EXPECT_EQ(base::PLATFORM_FILE_OK,
516 ofu()->EnsureFileExists(context.get(), src_file_path, &created));
517 if (overwrite) {
518 context.reset(NewContext(NULL));
519 EXPECT_EQ(base::PLATFORM_FILE_OK,
520 ofu()->EnsureFileExists(context.get(),
521 dest_file_path, &created));
522 }
523
524 ClearTimestamp(src_dir_path);
525 ClearTimestamp(dest_dir_path);
526 context.reset(NewContext(NULL));
527 EXPECT_EQ(base::PLATFORM_FILE_OK,
528 ofu()->CopyOrMoveFile(context.get(),
529 src_file_path, dest_file_path,
530 copy));
531
532 if (copy)
533 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path));
534 else
535 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path));
536 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path));
537 }
538
[email protected]d4905e2e2011-05-13 21:56:32539 private:
540 ScopedTempDir data_dir_;
[email protected]7878ece2011-09-05 11:41:49541 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16542 scoped_refptr<quota::QuotaManager> quota_manager_;
543 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26544 GURL origin_;
545 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03546 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26547 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16548 quota::QuotaStatusCode quota_status_;
549 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32550
[email protected]7878ece2011-09-05 11:41:49551 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32552};
553
[email protected]7878ece2011-09-05 11:41:49554TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32555 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
556 bool created;
557 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16558 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32559 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
560
561 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49562 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32563 context.get(), path, file_flags, &file_handle,
564 &created));
565
[email protected]0c5ebe32011-08-19 22:37:16566 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32567 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49568 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32569
570 path = UTF8ToFilePath("test file");
571
[email protected]0c5ebe32011-08-19 22:37:16572 // Verify that file creation requires sufficient quota for the path.
573 context.reset(NewContext(NULL));
574 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49575 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16576 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49577 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16578 context.get(), path, file_flags, &file_handle, &created));
579
580 context.reset(NewContext(NULL));
581 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49582 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32583 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49584 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32585 context.get(), path, file_flags, &file_handle, &created));
586 ASSERT_TRUE(created);
587 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
588
589 CheckFileAndCloseHandle(path, file_handle);
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_TRUE(file_util::PathExists(local_path));
596
[email protected]0c5ebe32011-08-19 22:37:16597 // Verify that deleting a file isn't stopped by zero quota, and that it frees
598 // up quote from its path.
599 context.reset(NewContext(NULL));
600 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32601 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49602 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32603 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]7878ece2011-09-05 11:41:49604 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16605 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32606
[email protected]0c5ebe32011-08-19 22:37:16607 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32608 bool exclusive = true;
609 bool recursive = true;
610 FilePath directory_path = UTF8ToFilePath("series/of/directories");
611 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49612 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32613 context.get(), directory_path, exclusive, recursive));
614
[email protected]0c5ebe32011-08-19 22:37:16615 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32616 file_handle = base::kInvalidPlatformFileValue;
617 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49618 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32619 context.get(), path, file_flags, &file_handle, &created));
620 ASSERT_TRUE(created);
621 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
622
623 CheckFileAndCloseHandle(path, file_handle);
624
[email protected]0c5ebe32011-08-19 22:37:16625 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49626 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32627 context.get(), path, &local_path));
628 EXPECT_TRUE(file_util::PathExists(local_path));
629
[email protected]0c5ebe32011-08-19 22:37:16630 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32631 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49632 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32633 EXPECT_FALSE(file_util::PathExists(local_path));
634}
635
[email protected]7878ece2011-09-05 11:41:49636TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32637 bool created = false;
638 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16639 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32640
641 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49642 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32643
[email protected]0c5ebe32011-08-19 22:37:16644 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32645 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49646 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32647 ASSERT_TRUE(created);
648
[email protected]0c5ebe32011-08-19 22:37:16649 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32650 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49651 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32652 context.get(), path, &local_path));
653 EXPECT_EQ(0, GetSize(local_path));
654
[email protected]0c5ebe32011-08-19 22:37:16655 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49656 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32657 context.get(), path, 10));
658 EXPECT_EQ(10, GetSize(local_path));
659
[email protected]0c5ebe32011-08-19 22:37:16660 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49661 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32662 context.get(), path, 1));
663 EXPECT_EQ(1, GetSize(local_path));
664
[email protected]0c5ebe32011-08-19 22:37:16665 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49666 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16667 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49668 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32669}
670
[email protected]7878ece2011-09-05 11:41:49671TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]d4905e2e2011-05-13 21:56:32672 FilePath path = UTF8ToFilePath("fake/file");
673 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16674 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32675 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49676 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32677 context.get(), path, &created));
678
[email protected]0c5ebe32011-08-19 22:37:16679 // Verify that file creation requires sufficient quota for the path.
680 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32681 path = UTF8ToFilePath("test file");
682 created = false;
[email protected]0c5ebe32011-08-19 22:37:16683 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49684 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16685 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49686 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16687 ASSERT_FALSE(created);
688
689 context.reset(NewContext(NULL));
690 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49691 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32692 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49693 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32694 ASSERT_TRUE(created);
695
696 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
697
[email protected]0c5ebe32011-08-19 22:37:16698 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32699 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49700 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32701 ASSERT_FALSE(created);
702
703 // Also test in a subdirectory.
704 path = UTF8ToFilePath("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16705 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32706 bool exclusive = true;
707 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49708 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32709 context.get(), path.DirName(), exclusive, recursive));
710
[email protected]0c5ebe32011-08-19 22:37:16711 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32712 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49713 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32714 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16715 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49716 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16717 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49718 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32719}
720
[email protected]7878ece2011-09-05 11:41:49721TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16722 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32723
724 bool exclusive = false;
725 bool recursive = false;
726 FilePath path = UTF8ToFilePath("foo/bar");
[email protected]7878ece2011-09-05 11:41:49727 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32728 context.get(), path, exclusive, recursive));
729
[email protected]0c5ebe32011-08-19 22:37:16730 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32731 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49732 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32733
734 FilePath root = UTF8ToFilePath("");
[email protected]0c5ebe32011-08-19 22:37:16735 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49736 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16737 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49738 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16739 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49740 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32741
[email protected]0c5ebe32011-08-19 22:37:16742 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32743 exclusive = false;
744 recursive = true;
[email protected]7878ece2011-09-05 11:41:49745 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32746 context.get(), path, exclusive, recursive));
747
[email protected]0c5ebe32011-08-19 22:37:16748 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49749 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16750 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49751 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16752 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49753 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[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.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16756 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49757 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32758
759 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16760 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06761 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49762 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32763
764 base::PlatformFileInfo file_info;
765 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49766 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32767 context.get(), path, &file_info, &local_path));
768 EXPECT_TRUE(local_path.empty());
769 EXPECT_TRUE(file_info.is_directory);
770 EXPECT_FALSE(file_info.is_symbolic_link);
771
772 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16773 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49774 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32775 context.get(), path, exclusive, recursive));
776
777 exclusive = true;
778 recursive = true;
[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_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32781 context.get(), path, exclusive, recursive));
782
[email protected]0c5ebe32011-08-19 22:37:16783 // Verify that deleting a directory isn't stopped by zero quota, and that it
784 // frees up quota from its path.
785 context.reset(NewContext(NULL));
786 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32787 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49788 ofu()->DeleteSingleDirectory(context.get(), path));
789 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16790 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32791
792 path = UTF8ToFilePath("foo/bop");
793
[email protected]0c5ebe32011-08-19 22:37:16794 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49795 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16796 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49797 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16798 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49799 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
800 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32801 context.get(), path, &file_info, &local_path));
802
[email protected]0c5ebe32011-08-19 22:37:16803 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32804 exclusive = true;
805 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16806 context.reset(NewContext(NULL));
807 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49808 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
809 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16810 context.get(), path, exclusive, recursive));
811
812 context.reset(NewContext(NULL));
813 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49814 ObfuscatedFileUtil::ComputeFilePathCost(path));
815 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32816 context.get(), path, exclusive, recursive));
817
[email protected]0c5ebe32011-08-19 22:37:16818 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49819 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16820 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49821 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32822
823 exclusive = true;
824 recursive = false;
[email protected]7878ece2011-09-05 11:41:49825 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32826 context.get(), path, exclusive, recursive));
827
828 exclusive = true;
829 recursive = false;
830 path = UTF8ToFilePath("foo");
[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 path = UTF8ToFilePath("blah");
835
[email protected]0c5ebe32011-08-19 22:37:16836 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49837 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16838 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49839 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32840
841 exclusive = true;
842 recursive = false;
[email protected]7878ece2011-09-05 11:41:49843 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32844 context.get(), path, exclusive, recursive));
845
[email protected]0c5ebe32011-08-19 22:37:16846 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49847 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16848 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49849 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32850
851 exclusive = true;
852 recursive = false;
[email protected]7878ece2011-09-05 11:41:49853 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32854 context.get(), path, exclusive, recursive));
855}
856
[email protected]7878ece2011-09-05 11:41:49857TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16858 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32859 bool exclusive = true;
860 bool recursive = true;
861 FilePath path = UTF8ToFilePath("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49862 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32863 context.get(), path, exclusive, recursive));
864 TestReadDirectoryHelper(path);
865}
866
[email protected]7878ece2011-09-05 11:41:49867TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]d4905e2e2011-05-13 21:56:32868 TestReadDirectoryHelper(UTF8ToFilePath(""));
869}
870
[email protected]7878ece2011-09-05 11:41:49871TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]d4905e2e2011-05-13 21:56:32872 TestReadDirectoryHelper(UTF8ToFilePath("/"));
873}
874
[email protected]7878ece2011-09-05 11:41:49875TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]d4905e2e2011-05-13 21:56:32876 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16877 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32878
879 bool created = false;
880 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49881 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32882 ASSERT_TRUE(created);
883
[email protected]0c5ebe32011-08-19 22:37:16884 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32885 std::vector<base::FileUtilProxy::Entry> entries;
886 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49887 ofu()->ReadDirectory(context.get(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32888
[email protected]7878ece2011-09-05 11:41:49889 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32890}
891
[email protected]7878ece2011-09-05 11:41:49892TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]2517cfa2011-08-25 05:12:41893 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16894 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41895
896 base::Time last_access_time = base::Time::Now();
897 base::Time last_modified_time = base::Time::Now();
898
899 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:32900 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49901 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32902 context.get(), path, last_access_time, last_modified_time));
903
[email protected]2517cfa2011-08-25 05:12:41904 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:16905 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41906 bool created = false;
907 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49908 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41909 ASSERT_TRUE(created);
910 TestTouchHelper(path, true);
911
912 // Now test a directory:
913 context.reset(NewContext(NULL));
914 bool exclusive = true;
915 bool recursive = false;
916 path = UTF8ToFilePath("dir");
[email protected]7878ece2011-09-05 11:41:49917 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:41918 path, exclusive, recursive));
919 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:16920}
921
[email protected]7878ece2011-09-05 11:41:49922TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:16923 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16924 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
925
[email protected]0c5ebe32011-08-19 22:37:16926 path = UTF8ToFilePath("file name");
927 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:41928 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16929 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49930 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41931 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:16932 context->set_allowed_bytes_growth(1024);
933 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49934 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41935 EXPECT_TRUE(created);
[email protected]7878ece2011-09-05 11:41:49936 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:16937 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
938
939 context->set_allowed_bytes_growth(1024);
940 bool exclusive = true;
941 bool recursive = true;
942 path = UTF8ToFilePath("directory/to/use");
943 std::vector<FilePath::StringType> components;
944 path.GetComponents(&components);
945 path_cost = 0;
946 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
947 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:49948 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:16949 FilePath(*iter));
950 }
951 context.reset(NewContext(NULL));
952 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:49953 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16954 context.get(), path, exclusive, recursive));
955 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32956}
957
[email protected]7878ece2011-09-05 11:41:49958TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]d4905e2e2011-05-13 21:56:32959 FilePath source_path = UTF8ToFilePath("path0.txt");
960 FilePath dest_path = UTF8ToFilePath("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:16961 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32962
963 bool is_copy_not_move = false;
964 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49965 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32966 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16967 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32968 is_copy_not_move = true;
969 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49970 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32971 is_copy_not_move));
972 source_path = UTF8ToFilePath("dir/dir/file");
973 bool exclusive = true;
974 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16975 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49976 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32977 context.get(), source_path.DirName(), exclusive, recursive));
978 is_copy_not_move = false;
979 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49980 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32981 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16982 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32983 is_copy_not_move = true;
984 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49985 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32986 is_copy_not_move));
987}
988
[email protected]7878ece2011-09-05 11:41:49989TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:32990 const int64 kSourceLength = 5;
991 const int64 kDestLength = 50;
992
993 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
994 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
995 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
996 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
997 test_case.is_copy_not_move);
998 SCOPED_TRACE(testing::Message() << "\t source_path " <<
999 test_case.source_path);
1000 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1001 test_case.dest_path);
1002 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1003 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161004 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321005
1006 bool exclusive = false;
1007 bool recursive = true;
1008 FilePath source_path = UTF8ToFilePath(test_case.source_path);
1009 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
1010
[email protected]0c5ebe32011-08-19 22:37:161011 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491012 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321013 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161014 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491015 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321016 context.get(), dest_path.DirName(), exclusive, recursive));
1017
[email protected]d4905e2e2011-05-13 21:56:321018 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161019 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321020 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491021 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321022 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161023 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321024 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491025 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321026
1027 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161028 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321029 created = false;
1030 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491031 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321032 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161033 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321034 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491035 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321036 }
1037
[email protected]0c5ebe32011-08-19 22:37:161038 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491039 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:321040 source_path, dest_path, test_case.is_copy_not_move));
1041 if (test_case.is_copy_not_move) {
1042 base::PlatformFileInfo file_info;
1043 FilePath local_path;
[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()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321046 context.get(), source_path, &file_info, &local_path));
1047 EXPECT_EQ(kSourceLength, file_info.size);
1048 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491049 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:321050 } else {
1051 base::PlatformFileInfo file_info;
1052 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161053 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491054 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321055 context.get(), source_path, &file_info, &local_path));
1056 }
1057 base::PlatformFileInfo file_info;
1058 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491059 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321060 context.get(), dest_path, &file_info, &local_path));
1061 EXPECT_EQ(kSourceLength, file_info.size);
1062
1063 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491064 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321065 }
1066}
1067
[email protected]7878ece2011-09-05 11:41:491068TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:161069 FilePath src_path = UTF8ToFilePath("src path");
1070 FilePath dest_path = UTF8ToFilePath("destination path");
1071 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1072 bool created = false;
[email protected]7878ece2011-09-05 11:41:491073 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161074 context.get(), src_path, &created));
1075
1076 bool is_copy = true;
1077 // Copy, no overwrite.
1078 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491079 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161080 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491081 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161082 context.reset(NewContext(NULL));
1083 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491084 ObfuscatedFileUtil::ComputeFilePathCost(dest_path));
[email protected]0c5ebe32011-08-19 22:37:161085 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491086 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161087
1088 // Copy, with overwrite.
1089 context.reset(NewContext(NULL));
1090 context->set_allowed_bytes_growth(0);
1091 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
[email protected]7878ece2011-09-05 11:41:491095TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]0c5ebe32011-08-19 22:37:161096 FilePath src_path = UTF8ToFilePath("src path");
1097 FilePath dest_path = UTF8ToFilePath("destination path");
1098 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1099 bool created = false;
[email protected]7878ece2011-09-05 11:41:491100 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161101 context.get(), src_path, &created));
1102
1103 bool is_copy = false;
1104 // Move, rename, no overwrite.
1105 context.reset(NewContext(NULL));
1106 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491107 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1108 ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161109 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491110 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161111 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));
[email protected]0c5ebe32011-08-19 22:37:161115 EXPECT_EQ(base::PLATFORM_FILE_OK,
[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
1118 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491119 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161120 context.get(), src_path, &created));
1121
1122 // Move, rename, with overwrite.
1123 context.reset(NewContext(NULL));
1124 context->set_allowed_bytes_growth(0);
1125 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491126 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161127}
1128
[email protected]7878ece2011-09-05 11:41:491129TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]0c5ebe32011-08-19 22:37:161130 FilePath src_path = UTF8ToFilePath("src path");
1131 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1132 bool created = false;
[email protected]7878ece2011-09-05 11:41:491133 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161134 context.get(), src_path, &created));
1135
1136 bool exclusive = true;
1137 bool recursive = false;
1138 FilePath dir_path = UTF8ToFilePath("directory path");
1139 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491140 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161141 context.get(), dir_path, exclusive, recursive));
1142
1143 FilePath dest_path = dir_path.Append(src_path);
1144
1145 bool is_copy = false;
1146 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1147 // Move, no rename, no overwrite.
1148 context.reset(NewContext(NULL));
1149 context->set_allowed_bytes_growth(allowed_bytes_growth);
1150 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491151 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161152 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1153
1154 // Move, no rename, with overwrite.
1155 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491156 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161157 context.get(), src_path, &created));
1158 context.reset(NewContext(NULL));
1159 context->set_allowed_bytes_growth(allowed_bytes_growth);
1160 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491161 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161162 EXPECT_EQ(
1163 allowed_bytes_growth +
[email protected]7878ece2011-09-05 11:41:491164 ObfuscatedFileUtil::ComputeFilePathCost(src_path),
[email protected]0c5ebe32011-08-19 22:37:161165 context->allowed_bytes_growth());
1166}
1167
[email protected]7878ece2011-09-05 11:41:491168TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461169 TestCopyInForeignFileHelper(false /* overwrite */);
1170 TestCopyInForeignFileHelper(true /* overwrite */);
1171}
1172
[email protected]7878ece2011-09-05 11:41:491173TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161174 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321175 FilePath src_path = UTF8ToFilePath("source dir");
1176 bool exclusive = true;
1177 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491178 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321179 context.get(), src_path, exclusive, recursive));
1180
[email protected]89ee4272011-05-16 18:45:171181 std::set<FilePath::StringType> files;
1182 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321183 FillTestDirectory(src_path, &files, &directories);
1184
1185 FilePath dest_path = UTF8ToFilePath("destination dir");
1186
[email protected]0c5ebe32011-08-19 22:37:161187 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491188 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161189 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321190 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491191 ofu()->Copy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321192
1193 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161194 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491195 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161196 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491197 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161198 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321199 recursive = true;
1200 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491201 ofu()->Delete(context.get(), dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161202 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491203 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321204}
[email protected]6b931152011-05-20 21:02:351205
[email protected]7878ece2011-09-05 11:41:491206TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351207 ScopedTempDir source_dir;
1208 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1209 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1210 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1211
1212 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1213 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1214 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1215 FilePath local_src_path = root_path.Append(test_case.path);
1216 if (test_case.is_directory) {
1217 ASSERT_TRUE(
1218 file_util::CreateDirectory(local_src_path));
1219 } else {
1220 base::PlatformFileError error_code;
1221 bool created = false;
1222 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
1223 base::PlatformFile file_handle =
1224 base::CreatePlatformFile(
1225 local_src_path, file_flags, &created, &error_code);
1226 EXPECT_TRUE(created);
[email protected]81b7f662011-05-26 00:54:461227 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]6b931152011-05-20 21:02:351228 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
1229 ASSERT_TRUE(
1230 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
1231 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1232 }
1233 }
1234
[email protected]7878ece2011-09-05 11:41:491235 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351236
1237 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161238 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491239 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]6b931152011-05-20 21:02:351240 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1241 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
1242 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1243 FilePath local_data_path = new_root.Append(test_case.path);
1244#if defined(OS_WIN)
1245 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
1246#endif
[email protected]0c5ebe32011-08-19 22:37:161247 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491248 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351249 FilePath data_path;
1250 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1251 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491252 ofu()->GetFileInfo(context.get(), FilePath(test_case.path),
1253 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351254 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491255 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351256 } else {
1257 base::PlatformFileInfo platform_file_info;
1258 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1259 local_data_path.value());
1260 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1261 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1262 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1263 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161264 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351265 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491266 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1267 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351268 }
1269 }
1270}
[email protected]fcc2d5f2011-05-23 22:06:261271
[email protected]7878ece2011-09-05 11:41:491272TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1273 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1274 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161275 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261276 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161277 EXPECT_EQ(origin(), enumerator->Next());
1278 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1279 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1280 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261281 EXPECT_EQ(GURL(), enumerator->Next());
1282 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1283 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1284
1285 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161286 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261287
1288 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1289 SCOPED_TRACE(testing::Message() <<
1290 "Validating kOriginEnumerationTestRecords " << i);
1291 const OriginEnumerationTestRecord& record =
1292 kOriginEnumerationTestRecords[i];
1293 GURL origin_url(record.origin_url);
1294 origins_expected.insert(origin_url);
1295 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161296 scoped_ptr<FileSystemTestOriginHelper> helper(
1297 NewHelper(origin_url, kFileSystemTypeTemporary));
1298 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261299 context->set_src_origin_url(origin_url);
1300 context->set_src_type(kFileSystemTypeTemporary);
1301 bool created = false;
1302 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491303 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261304 FilePath().AppendASCII("file"), &created));
1305 EXPECT_TRUE(created);
1306 }
1307 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161308 scoped_ptr<FileSystemTestOriginHelper> helper(
1309 NewHelper(origin_url, kFileSystemTypePersistent));
1310 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261311 context->set_src_origin_url(origin_url);
1312 context->set_src_type(kFileSystemTypePersistent);
1313 bool created = false;
1314 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491315 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261316 FilePath().AppendASCII("file"), &created));
1317 EXPECT_TRUE(created);
1318 }
1319 }
[email protected]7878ece2011-09-05 11:41:491320 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261321 EXPECT_TRUE(enumerator.get());
1322 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161323 GURL origin_url;
1324 while (!(origin_url = enumerator->Next()).is_empty()) {
1325 origins_found.insert(origin_url);
1326 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261327 bool found = false;
1328 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1329 ++i) {
1330 const OriginEnumerationTestRecord& record =
1331 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161332 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261333 continue;
1334 found = true;
1335 EXPECT_EQ(record.has_temporary,
1336 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1337 EXPECT_EQ(record.has_persistent,
1338 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1339 }
[email protected]0c5ebe32011-08-19 22:37:161340 // Deal with the default filesystem created by the test helper.
1341 if (!found && origin_url == origin()) {
1342 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1343 EXPECT_EQ(true,
1344 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181345 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161346 found = true;
1347 }
[email protected]fcc2d5f2011-05-23 22:06:261348 EXPECT_TRUE(found);
1349 }
1350
1351 std::set<GURL> diff;
1352 std::set_symmetric_difference(origins_expected.begin(),
1353 origins_expected.end(), origins_found.begin(), origins_found.end(),
1354 inserter(diff, diff.begin()));
1355 EXPECT_TRUE(diff.empty());
1356}
[email protected]0c5ebe32011-08-19 22:37:161357
[email protected]7878ece2011-09-05 11:41:491358TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161359 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1360
1361 int64 expected_quota = 0;
1362
1363 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1364 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1365 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1366 FilePath path(test_case.path);
[email protected]7878ece2011-09-05 11:41:491367 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:161368 if (test_case.is_directory) {
1369 bool exclusive = true;
1370 bool recursive = false;
1371 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491372 ofu()->CreateDirectory(context.get(), path, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161373 } else {
1374 bool created = false;
1375 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491376 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:161377 ASSERT_TRUE(created);
1378 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491379 ofu()->Truncate(context.get(), path,
[email protected]0c5ebe32011-08-19 22:37:161380 test_case.data_file_size));
1381 expected_quota += test_case.data_file_size;
1382 }
1383 }
1384 EXPECT_EQ(expected_quota, SizeInUsageFile());
1385 RevokeUsageCache();
1386 EXPECT_EQ(-1, SizeInUsageFile());
1387 GetUsageFromQuotaManager();
1388 EXPECT_EQ(expected_quota, SizeInUsageFile());
1389 EXPECT_EQ(expected_quota, usage());
1390}
[email protected]34583332011-08-31 08:59:471391
[email protected]7878ece2011-09-05 11:41:491392TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]34583332011-08-31 08:59:471393 const FilePath kPath1 = FilePath().AppendASCII("hoge");
1394 const FilePath kPath2 = FilePath().AppendASCII("fuga");
1395
1396 scoped_ptr<FileSystemOperationContext> context;
1397 base::PlatformFile file;
1398 base::PlatformFileInfo file_info;
1399 FilePath data_path;
1400 bool created = false;
1401
1402 // Create a non-empty file.
1403 context.reset(NewContext(NULL));
1404 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491405 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471406 EXPECT_TRUE(created);
1407 context.reset(NewContext(NULL));
1408 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491409 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471410 context.reset(NewContext(NULL));
1411 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491412 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471413 context.get(), kPath1, &file_info, &data_path));
1414 EXPECT_EQ(10, file_info.size);
1415
1416 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491417 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471418
1419 // Try to get file info of broken file.
1420 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491421 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471422 context.reset(NewContext(NULL));
1423 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491424 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471425 EXPECT_TRUE(created);
1426 context.reset(NewContext(NULL));
1427 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491428 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471429 context.get(), kPath1, &file_info, &data_path));
1430 EXPECT_EQ(0, file_info.size);
1431
1432 // Make another broken file to |kPath2|.
1433 context.reset(NewContext(NULL));
1434 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491435 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471436 EXPECT_TRUE(created);
1437
1438 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491439 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471440
1441 // Repair broken |kPath1|.
1442 context.reset(NewContext(NULL));
1443 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491444 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471445 base::Time::Now()));
1446 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491447 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471448 EXPECT_TRUE(created);
1449
1450 // Copy from sound |kPath1| to broken |kPath2|.
1451 context.reset(NewContext(NULL));
1452 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491453 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]34583332011-08-31 08:59:471454 true /* copy */));
1455
[email protected]7878ece2011-09-05 11:41:491456 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471457 context.reset(NewContext(NULL));
1458 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491459 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471460 context.get(), kPath1,
1461 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1462 &file, &created));
1463 EXPECT_TRUE(created);
1464 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1465 EXPECT_EQ(0, file_info.size);
1466 EXPECT_TRUE(base::ClosePlatformFile(file));
1467}
[email protected]9dfdc0e32011-09-02 06:07:441468
[email protected]7878ece2011-09-05 11:41:491469TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]9dfdc0e32011-09-02 06:07:441470 const FilePath kPath[] = {
1471 FilePath().AppendASCII("foo"),
1472 FilePath().AppendASCII("bar"),
1473 FilePath().AppendASCII("baz")
1474 };
1475 scoped_ptr<FileSystemOperationContext> context;
1476
1477 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1478 bool created = false;
1479 context.reset(NewContext(NULL));
1480 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491481 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441482 EXPECT_TRUE(created);
1483 }
1484
1485 context.reset(NewContext(NULL));
1486 std::vector<base::FileUtilProxy::Entry> entries;
1487 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491488 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441489 EXPECT_EQ(3u, entries.size());
1490
1491 context.reset(NewContext(NULL));
1492 FilePath local_path;
1493 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491494 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441495 EXPECT_TRUE(file_util::Delete(local_path, false));
1496
1497 context.reset(NewContext(NULL));
1498 entries.clear();
1499 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491500 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441501 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1502}
[email protected]fad625e2f2011-12-08 05:38:031503
1504TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1505 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1506 const FilePath dir_path(FILE_PATH_LITERAL("foo_dir"));
1507
1508 // Create working directory.
1509 EXPECT_EQ(base::PLATFORM_FILE_OK,
1510 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1511
1512 // EnsureFileExists, create case.
1513 FilePath path(dir_path.AppendASCII("EnsureFileExists_file"));
1514 bool created = false;
1515 ClearTimestamp(dir_path);
1516 context.reset(NewContext(NULL));
1517 EXPECT_EQ(base::PLATFORM_FILE_OK,
1518 ofu()->EnsureFileExists(context.get(), path, &created));
1519 EXPECT_TRUE(created);
1520 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1521
1522 // non create case.
1523 created = true;
1524 ClearTimestamp(dir_path);
1525 context.reset(NewContext(NULL));
1526 EXPECT_EQ(base::PLATFORM_FILE_OK,
1527 ofu()->EnsureFileExists(context.get(), path, &created));
1528 EXPECT_FALSE(created);
1529 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1530
1531 // fail case.
1532 path = dir_path.AppendASCII("EnsureFileExists_dir");
1533 context.reset(NewContext(NULL));
1534 EXPECT_EQ(base::PLATFORM_FILE_OK,
1535 ofu()->CreateDirectory(context.get(), path, false, false));
1536
1537 ClearTimestamp(dir_path);
1538 context.reset(NewContext(NULL));
1539 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
1540 ofu()->EnsureFileExists(context.get(), path, &created));
1541 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1542
1543 // CreateOrOpen, create case.
1544 path = dir_path.AppendASCII("CreateOrOpen_file");
1545 PlatformFile file_handle = base::kInvalidPlatformFileValue;
1546 created = false;
1547 ClearTimestamp(dir_path);
1548 context.reset(NewContext(NULL));
1549 EXPECT_EQ(base::PLATFORM_FILE_OK,
1550 ofu()->CreateOrOpen(
1551 context.get(), path,
1552 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1553 &file_handle, &created));
1554 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1555 EXPECT_TRUE(created);
1556 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1557 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1558
1559 // open case.
1560 file_handle = base::kInvalidPlatformFileValue;
1561 created = true;
1562 ClearTimestamp(dir_path);
1563 context.reset(NewContext(NULL));
1564 EXPECT_EQ(base::PLATFORM_FILE_OK,
1565 ofu()->CreateOrOpen(
1566 context.get(), path,
1567 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1568 &file_handle, &created));
1569 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1570 EXPECT_FALSE(created);
1571 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1572 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1573
1574 // fail case
1575 file_handle = base::kInvalidPlatformFileValue;
1576 ClearTimestamp(dir_path);
1577 context.reset(NewContext(NULL));
1578 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1579 ofu()->CreateOrOpen(
1580 context.get(), path,
1581 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1582 &file_handle, &created));
1583 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
1584 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1585
1586 // CreateDirectory, create case.
1587 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
1588 path = dir_path.AppendASCII("CreateDirectory_dir");
1589 FilePath subdir_path(path.AppendASCII("subdir"));
1590 ClearTimestamp(dir_path);
1591 context.reset(NewContext(NULL));
1592 EXPECT_EQ(base::PLATFORM_FILE_OK,
1593 ofu()->CreateDirectory(context.get(), subdir_path,
1594 true /* exclusive */, true /* recursive */));
1595 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1596
1597 // create subdir case.
1598 // Creating CreateDirectory_dir/subdir2.
1599 subdir_path = path.AppendASCII("subdir2");
1600 ClearTimestamp(dir_path);
1601 ClearTimestamp(path);
1602 context.reset(NewContext(NULL));
1603 EXPECT_EQ(base::PLATFORM_FILE_OK,
1604 ofu()->CreateDirectory(context.get(), subdir_path,
1605 true /* exclusive */, true /* recursive */));
1606 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1607 EXPECT_NE(base::Time(), GetModifiedTime(path));
1608
1609 // fail case.
1610 path = dir_path.AppendASCII("CreateDirectory_dir");
1611 ClearTimestamp(dir_path);
1612 context.reset(NewContext(NULL));
1613 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1614 ofu()->CreateDirectory(context.get(), path,
1615 true /* exclusive */, true /* recursive */));
1616 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1617
1618 // CopyInForeignFile, create case.
1619 path = dir_path.AppendASCII("CopyInForeignFile_file");
1620 FilePath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file");
1621 context.reset(NewContext(NULL));
1622 EXPECT_EQ(base::PLATFORM_FILE_OK,
1623 ofu()->EnsureFileExists(context.get(), src_path, &created));
1624 EXPECT_TRUE(created);
1625 FilePath src_local_path;
1626 context.reset(NewContext(NULL));
1627 EXPECT_EQ(base::PLATFORM_FILE_OK,
1628 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1629
1630 ClearTimestamp(dir_path);
1631 context.reset(NewContext(NULL));
1632 EXPECT_EQ(base::PLATFORM_FILE_OK,
1633 ofu()->CopyInForeignFile(context.get(), src_local_path, path));
1634 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1635}
1636
1637TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1638 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1639 const FilePath dir_path(FILE_PATH_LITERAL("foo_dir"));
1640
1641 // Create working directory.
1642 EXPECT_EQ(base::PLATFORM_FILE_OK,
1643 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1644
1645 // DeleteFile, delete case.
1646 FilePath path = dir_path.AppendASCII("DeleteFile_file");
1647 bool created = false;
1648 context.reset(NewContext(NULL));
1649 EXPECT_EQ(base::PLATFORM_FILE_OK,
1650 ofu()->EnsureFileExists(context.get(), path, &created));
1651 EXPECT_TRUE(created);
1652
1653 ClearTimestamp(dir_path);
1654 context.reset(NewContext(NULL));
1655 EXPECT_EQ(base::PLATFORM_FILE_OK,
1656 ofu()->DeleteFile(context.get(), path));
1657 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1658
1659 // fail case.
1660 ClearTimestamp(dir_path);
1661 context.reset(NewContext(NULL));
1662 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
1663 ofu()->DeleteFile(context.get(), path));
1664 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1665
1666 // DeleteSingleDirectory, fail case.
1667 path = dir_path.AppendASCII("DeleteSingleDirectory_dir");
1668 FilePath file_path(path.AppendASCII("pakeratta"));
1669 context.reset(NewContext(NULL));
1670 EXPECT_EQ(base::PLATFORM_FILE_OK,
1671 ofu()->CreateDirectory(context.get(), path, true, true));
1672 created = false;
1673 context.reset(NewContext(NULL));
1674 EXPECT_EQ(base::PLATFORM_FILE_OK,
1675 ofu()->EnsureFileExists(context.get(), file_path, &created));
1676 EXPECT_TRUE(created);
1677
1678 ClearTimestamp(dir_path);
1679 context.reset(NewContext(NULL));
1680 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
1681 ofu()->DeleteSingleDirectory(context.get(), path));
1682 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1683
1684 // delete case.
1685 context.reset(NewContext(NULL));
1686 EXPECT_EQ(base::PLATFORM_FILE_OK,
1687 ofu()->DeleteFile(context.get(), file_path));
1688
1689 ClearTimestamp(dir_path);
1690 context.reset(NewContext(NULL));
1691 EXPECT_EQ(base::PLATFORM_FILE_OK,
1692 ofu()->DeleteSingleDirectory(context.get(), path));
1693 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1694}
1695
1696TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1697 TestDirectoryTimestampHelper(
1698 FilePath(FILE_PATH_LITERAL("copy overwrite")), true, true);
1699 TestDirectoryTimestampHelper(
1700 FilePath(FILE_PATH_LITERAL("copy non-overwrite")), true, false);
1701 TestDirectoryTimestampHelper(
1702 FilePath(FILE_PATH_LITERAL("move overwrite")), false, true);
1703 TestDirectoryTimestampHelper(
1704 FilePath(FILE_PATH_LITERAL("move non-overwrite")), false, false);
1705}