blob: 3881370c52bda5b98a709b32c09e8c125590017d [file] [log] [blame]
[email protected]d4905e2e2011-05-13 21:56:321// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fcc2d5f2011-05-23 22:06:265#include <algorithm>
[email protected]d4905e2e2011-05-13 21:56:326#include <set>
7#include <string>
8
9#include "base/file_path.h"
10#include "base/file_util.h"
11#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1612#include "base/memory/scoped_callback_factory.h"
13#include "base/message_loop.h"
[email protected]d4905e2e2011-05-13 21:56:3214#include "base/platform_file.h"
[email protected]e0785902011-05-19 23:34:1715#include "base/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3216#include "base/sys_string_conversions.h"
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webkit/fileapi/file_system_context.h"
19#include "webkit/fileapi/file_system_operation_context.h"
[email protected]0c5ebe32011-08-19 22:37:1620#include "webkit/fileapi/file_system_path_manager.h"
[email protected]fcc2d5f2011-05-23 22:06:2621#include "webkit/fileapi/file_system_test_helper.h"
[email protected]0c5ebe32011-08-19 22:37:1622#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]7878ece2011-09-05 11:41:4923#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]0c5ebe32011-08-19 22:37:1624#include "webkit/quota/mock_special_storage_policy.h"
25#include "webkit/quota/quota_manager.h"
26#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3227
28using namespace fileapi;
29
30namespace {
31
32FilePath UTF8ToFilePath(const std::string& str) {
33 FilePath::StringType result;
34#if defined(OS_POSIX)
35 result = str;
36#elif defined(OS_WIN)
37 result = base::SysUTF8ToWide(str);
38#endif
39 return FilePath(result);
40}
41
42bool FileExists(const FilePath& path) {
43 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
44}
45
[email protected]81b7f662011-05-26 00:54:4646int64 GetSize(const FilePath& path) {
47 int64 size;
48 EXPECT_TRUE(file_util::GetFileSize(path, &size));
49 return size;
50}
51
[email protected]d4905e2e2011-05-13 21:56:3252// After a move, the dest exists and the source doesn't.
53// After a copy, both source and dest exist.
54struct CopyMoveTestCaseRecord {
55 bool is_copy_not_move;
56 const char source_path[64];
57 const char dest_path[64];
58 bool cause_overwrite;
59};
60
61const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
62 // This is the combinatoric set of:
63 // rename vs. same-name
64 // different directory vs. same directory
65 // overwrite vs. no-overwrite
66 // copy vs. move
67 // We can never be called with source and destination paths identical, so
68 // those cases are omitted.
69 {true, "dir0/file0", "dir0/file1", false},
70 {false, "dir0/file0", "dir0/file1", false},
71 {true, "dir0/file0", "dir0/file1", true},
72 {false, "dir0/file0", "dir0/file1", true},
73
74 {true, "dir0/file0", "dir1/file0", false},
75 {false, "dir0/file0", "dir1/file0", false},
76 {true, "dir0/file0", "dir1/file0", true},
77 {false, "dir0/file0", "dir1/file0", true},
78 {true, "dir0/file0", "dir1/file1", false},
79 {false, "dir0/file0", "dir1/file1", false},
80 {true, "dir0/file0", "dir1/file1", true},
81 {false, "dir0/file0", "dir1/file1", true},
82};
83
[email protected]6b931152011-05-20 21:02:3584struct MigrationTestCaseRecord {
85 bool is_directory;
86 const FilePath::CharType path[64];
87 int64 data_file_size;
88};
89
90const MigrationTestCaseRecord kMigrationTestCases[] = {
91 {true, FILE_PATH_LITERAL("dir a"), 0},
92 {true, FILE_PATH_LITERAL("dir a/dir a"), 0},
93 {true, FILE_PATH_LITERAL("dir a/dir d"), 0},
94 {true, FILE_PATH_LITERAL("dir a/dir d/dir e"), 0},
95 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir f"), 0},
96 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g"), 0},
97 {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir h"), 0},
98 {true, FILE_PATH_LITERAL("dir b"), 0},
99 {true, FILE_PATH_LITERAL("dir b/dir a"), 0},
100 {true, FILE_PATH_LITERAL("dir c"), 0},
101 {false, FILE_PATH_LITERAL("file 0"), 38},
102 {false, FILE_PATH_LITERAL("file 2"), 60},
103 {false, FILE_PATH_LITERAL("file 3"), 0},
104 {false, FILE_PATH_LITERAL("dir a/file 0"), 39},
105 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40},
106 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41},
107 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42},
108 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50},
109};
110
[email protected]fcc2d5f2011-05-23 22:06:26111struct OriginEnumerationTestRecord {
112 std::string origin_url;
113 bool has_temporary;
114 bool has_persistent;
115};
116
117const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
118 {"http://example.com", false, true},
119 {"http://example1.com", true, false},
120 {"https://example1.com", true, true},
121 {"file://", false, true},
122 {"http://example.com:8000", false, true},
123};
124
[email protected]d4905e2e2011-05-13 21:56:32125} // namespace (anonymous)
126
127// TODO(ericu): The vast majority of this and the other FSFU subclass tests
128// could theoretically be shared. It would basically be a FSFU interface
129// compliance test, and only the subclass-specific bits that look into the
130// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49131class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32132 public:
[email protected]7878ece2011-09-05 11:41:49133 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26134 : origin_(GURL("http://www.example.com")),
135 type_(kFileSystemTypeTemporary),
[email protected]0c5ebe32011-08-19 22:37:16136 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
137 test_helper_(origin_, type_),
138 quota_status_(quota::kQuotaStatusUnknown),
139 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32140 }
141
142 void SetUp() {
143 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
144
[email protected]0c5ebe32011-08-19 22:37:16145 quota_manager_ = new quota::QuotaManager(
146 false /* is_incognito */,
147 data_dir_.path(),
148 base::MessageLoopProxy::current(),
149 base::MessageLoopProxy::current(),
150 NULL /* special storage policy */);
151
152 // Every time we create a new helper, it creates another context, which
153 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49154 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16155 file_system_context_ = new FileSystemContext(
156 base::MessageLoopProxy::current(),
157 base::MessageLoopProxy::current(),
158 new quota::MockSpecialStoragePolicy(),
159 quota_manager_->proxy(),
160 data_dir_.path(),
161 false /* incognito */,
162 true /* allow_file_access_from_files */,
[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(),
215 callback_factory_.NewCallback(
[email protected]7878ece2011-09-05 11:41:49216 &ObfuscatedFileUtilTest::OnGetUsage));
[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]d4905e2e2011-05-13 21:56:32479 private:
480 ScopedTempDir data_dir_;
[email protected]7878ece2011-09-05 11:41:49481 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16482 scoped_refptr<quota::QuotaManager> quota_manager_;
483 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26484 GURL origin_;
485 fileapi::FileSystemType type_;
[email protected]7878ece2011-09-05 11:41:49486 base::ScopedCallbackFactory<ObfuscatedFileUtilTest>
[email protected]0c5ebe32011-08-19 22:37:16487 callback_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26488 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16489 quota::QuotaStatusCode quota_status_;
490 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32491
[email protected]7878ece2011-09-05 11:41:49492 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32493};
494
[email protected]7878ece2011-09-05 11:41:49495TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32496 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
497 bool created;
498 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16499 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32500 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
501
502 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49503 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32504 context.get(), path, file_flags, &file_handle,
505 &created));
506
[email protected]0c5ebe32011-08-19 22:37:16507 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32508 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49509 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32510
511 path = UTF8ToFilePath("test file");
512
[email protected]0c5ebe32011-08-19 22:37:16513 // Verify that file creation requires sufficient quota for the path.
514 context.reset(NewContext(NULL));
515 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49516 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16517 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49518 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16519 context.get(), path, file_flags, &file_handle, &created));
520
521 context.reset(NewContext(NULL));
522 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49523 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32524 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49525 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32526 context.get(), path, file_flags, &file_handle, &created));
527 ASSERT_TRUE(created);
528 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
529
530 CheckFileAndCloseHandle(path, file_handle);
531
[email protected]0c5ebe32011-08-19 22:37:16532 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32533 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49534 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32535 context.get(), path, &local_path));
536 EXPECT_TRUE(file_util::PathExists(local_path));
537
[email protected]0c5ebe32011-08-19 22:37:16538 // Verify that deleting a file isn't stopped by zero quota, and that it frees
539 // up quote from its path.
540 context.reset(NewContext(NULL));
541 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32542 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49543 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32544 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]7878ece2011-09-05 11:41:49545 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16546 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32547
[email protected]0c5ebe32011-08-19 22:37:16548 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32549 bool exclusive = true;
550 bool recursive = true;
551 FilePath directory_path = UTF8ToFilePath("series/of/directories");
552 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49553 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32554 context.get(), directory_path, exclusive, recursive));
555
[email protected]0c5ebe32011-08-19 22:37:16556 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32557 file_handle = base::kInvalidPlatformFileValue;
558 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49559 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32560 context.get(), path, file_flags, &file_handle, &created));
561 ASSERT_TRUE(created);
562 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
563
564 CheckFileAndCloseHandle(path, file_handle);
565
[email protected]0c5ebe32011-08-19 22:37:16566 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49567 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32568 context.get(), path, &local_path));
569 EXPECT_TRUE(file_util::PathExists(local_path));
570
[email protected]0c5ebe32011-08-19 22:37:16571 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32572 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49573 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32574 EXPECT_FALSE(file_util::PathExists(local_path));
575}
576
[email protected]7878ece2011-09-05 11:41:49577TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32578 bool created = false;
579 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16580 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32581
582 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49583 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32584
[email protected]0c5ebe32011-08-19 22:37:16585 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32586 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49587 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32588 ASSERT_TRUE(created);
589
[email protected]0c5ebe32011-08-19 22:37:16590 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32591 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49592 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32593 context.get(), path, &local_path));
594 EXPECT_EQ(0, GetSize(local_path));
595
[email protected]0c5ebe32011-08-19 22:37:16596 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49597 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32598 context.get(), path, 10));
599 EXPECT_EQ(10, GetSize(local_path));
600
[email protected]0c5ebe32011-08-19 22:37:16601 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49602 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32603 context.get(), path, 1));
604 EXPECT_EQ(1, GetSize(local_path));
605
[email protected]0c5ebe32011-08-19 22:37:16606 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49607 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16608 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49609 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32610}
611
[email protected]7878ece2011-09-05 11:41:49612TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]d4905e2e2011-05-13 21:56:32613 FilePath path = UTF8ToFilePath("fake/file");
614 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16615 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32616 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49617 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32618 context.get(), path, &created));
619
[email protected]0c5ebe32011-08-19 22:37:16620 // Verify that file creation requires sufficient quota for the path.
621 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32622 path = UTF8ToFilePath("test file");
623 created = false;
[email protected]0c5ebe32011-08-19 22:37:16624 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49625 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16626 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49627 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16628 ASSERT_FALSE(created);
629
630 context.reset(NewContext(NULL));
631 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49632 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32633 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49634 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32635 ASSERT_TRUE(created);
636
637 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
638
[email protected]0c5ebe32011-08-19 22:37:16639 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32640 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49641 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32642 ASSERT_FALSE(created);
643
644 // Also test in a subdirectory.
645 path = UTF8ToFilePath("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16646 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32647 bool exclusive = true;
648 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49649 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32650 context.get(), path.DirName(), exclusive, recursive));
651
[email protected]0c5ebe32011-08-19 22:37:16652 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32653 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49654 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32655 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16656 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49657 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16658 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49659 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32660}
661
[email protected]7878ece2011-09-05 11:41:49662TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16663 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32664
665 bool exclusive = false;
666 bool recursive = false;
667 FilePath path = UTF8ToFilePath("foo/bar");
[email protected]7878ece2011-09-05 11:41:49668 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32669 context.get(), path, exclusive, recursive));
670
[email protected]0c5ebe32011-08-19 22:37:16671 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32672 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49673 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32674
675 FilePath root = UTF8ToFilePath("");
[email protected]0c5ebe32011-08-19 22:37:16676 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49677 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16678 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49679 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16680 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49681 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32682
[email protected]0c5ebe32011-08-19 22:37:16683 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32684 exclusive = false;
685 recursive = true;
[email protected]7878ece2011-09-05 11:41:49686 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32687 context.get(), path, exclusive, recursive));
688
[email protected]0c5ebe32011-08-19 22:37:16689 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49690 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16691 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49692 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16693 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49694 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]0c5ebe32011-08-19 22:37:16695 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49696 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16697 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49698 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32699
700 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16701 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06702 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49703 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32704
705 base::PlatformFileInfo file_info;
706 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49707 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32708 context.get(), path, &file_info, &local_path));
709 EXPECT_TRUE(local_path.empty());
710 EXPECT_TRUE(file_info.is_directory);
711 EXPECT_FALSE(file_info.is_symbolic_link);
712
713 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16714 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49715 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32716 context.get(), path, exclusive, recursive));
717
718 exclusive = true;
719 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16720 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49721 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32722 context.get(), path, exclusive, recursive));
723
[email protected]0c5ebe32011-08-19 22:37:16724 // Verify that deleting a directory isn't stopped by zero quota, and that it
725 // frees up quota from its path.
726 context.reset(NewContext(NULL));
727 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32728 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49729 ofu()->DeleteSingleDirectory(context.get(), path));
730 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16731 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32732
733 path = UTF8ToFilePath("foo/bop");
734
[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(), path));
741 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32742 context.get(), path, &file_info, &local_path));
743
[email protected]0c5ebe32011-08-19 22:37:16744 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32745 exclusive = true;
746 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16747 context.reset(NewContext(NULL));
748 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49749 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
750 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16751 context.get(), path, exclusive, recursive));
752
753 context.reset(NewContext(NULL));
754 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49755 ObfuscatedFileUtil::ComputeFilePathCost(path));
756 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32757 context.get(), path, exclusive, recursive));
758
[email protected]0c5ebe32011-08-19 22:37:16759 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49760 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16761 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49762 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32763
764 exclusive = true;
765 recursive = false;
[email protected]7878ece2011-09-05 11:41:49766 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32767 context.get(), path, exclusive, recursive));
768
769 exclusive = true;
770 recursive = false;
771 path = UTF8ToFilePath("foo");
[email protected]7878ece2011-09-05 11:41:49772 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32773 context.get(), path, exclusive, recursive));
774
775 path = UTF8ToFilePath("blah");
776
[email protected]0c5ebe32011-08-19 22:37:16777 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49778 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16779 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49780 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32781
782 exclusive = true;
783 recursive = false;
[email protected]7878ece2011-09-05 11:41:49784 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32785 context.get(), path, exclusive, recursive));
786
[email protected]0c5ebe32011-08-19 22:37:16787 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49788 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16789 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49790 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32791
792 exclusive = true;
793 recursive = false;
[email protected]7878ece2011-09-05 11:41:49794 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32795 context.get(), path, exclusive, recursive));
796}
797
[email protected]7878ece2011-09-05 11:41:49798TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16799 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32800 bool exclusive = true;
801 bool recursive = true;
802 FilePath path = UTF8ToFilePath("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49803 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32804 context.get(), path, exclusive, recursive));
805 TestReadDirectoryHelper(path);
806}
807
[email protected]7878ece2011-09-05 11:41:49808TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]d4905e2e2011-05-13 21:56:32809 TestReadDirectoryHelper(UTF8ToFilePath(""));
810}
811
[email protected]7878ece2011-09-05 11:41:49812TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]d4905e2e2011-05-13 21:56:32813 TestReadDirectoryHelper(UTF8ToFilePath("/"));
814}
815
[email protected]7878ece2011-09-05 11:41:49816TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]d4905e2e2011-05-13 21:56:32817 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16818 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32819
820 bool created = false;
821 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49822 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32823 ASSERT_TRUE(created);
824
[email protected]0c5ebe32011-08-19 22:37:16825 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32826 std::vector<base::FileUtilProxy::Entry> entries;
827 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49828 ofu()->ReadDirectory(context.get(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32829
[email protected]7878ece2011-09-05 11:41:49830 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32831}
832
[email protected]7878ece2011-09-05 11:41:49833TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]2517cfa2011-08-25 05:12:41834 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16835 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41836
837 base::Time last_access_time = base::Time::Now();
838 base::Time last_modified_time = base::Time::Now();
839
840 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:32841 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49842 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32843 context.get(), path, last_access_time, last_modified_time));
844
[email protected]2517cfa2011-08-25 05:12:41845 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:16846 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41847 bool created = false;
848 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49849 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41850 ASSERT_TRUE(created);
851 TestTouchHelper(path, true);
852
853 // Now test a directory:
854 context.reset(NewContext(NULL));
855 bool exclusive = true;
856 bool recursive = false;
857 path = UTF8ToFilePath("dir");
[email protected]7878ece2011-09-05 11:41:49858 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:41859 path, exclusive, recursive));
860 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:16861}
862
[email protected]7878ece2011-09-05 11:41:49863TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:16864 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16865 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
866
[email protected]0c5ebe32011-08-19 22:37:16867 path = UTF8ToFilePath("file name");
868 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:41869 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16870 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49871 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41872 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:16873 context->set_allowed_bytes_growth(1024);
874 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49875 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41876 EXPECT_TRUE(created);
[email protected]7878ece2011-09-05 11:41:49877 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:16878 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
879
880 context->set_allowed_bytes_growth(1024);
881 bool exclusive = true;
882 bool recursive = true;
883 path = UTF8ToFilePath("directory/to/use");
884 std::vector<FilePath::StringType> components;
885 path.GetComponents(&components);
886 path_cost = 0;
887 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
888 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:49889 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:16890 FilePath(*iter));
891 }
892 context.reset(NewContext(NULL));
893 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:49894 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16895 context.get(), path, exclusive, recursive));
896 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32897}
898
[email protected]7878ece2011-09-05 11:41:49899TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]d4905e2e2011-05-13 21:56:32900 FilePath source_path = UTF8ToFilePath("path0.txt");
901 FilePath dest_path = UTF8ToFilePath("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:16902 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32903
904 bool is_copy_not_move = false;
905 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49906 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32907 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16908 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32909 is_copy_not_move = true;
910 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49911 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32912 is_copy_not_move));
913 source_path = UTF8ToFilePath("dir/dir/file");
914 bool exclusive = true;
915 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16916 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49917 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32918 context.get(), source_path.DirName(), exclusive, recursive));
919 is_copy_not_move = false;
920 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49921 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32922 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16923 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32924 is_copy_not_move = true;
925 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49926 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32927 is_copy_not_move));
928}
929
[email protected]7878ece2011-09-05 11:41:49930TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:32931 const int64 kSourceLength = 5;
932 const int64 kDestLength = 50;
933
934 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
935 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
936 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
937 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
938 test_case.is_copy_not_move);
939 SCOPED_TRACE(testing::Message() << "\t source_path " <<
940 test_case.source_path);
941 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
942 test_case.dest_path);
943 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
944 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:16945 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32946
947 bool exclusive = false;
948 bool recursive = true;
949 FilePath source_path = UTF8ToFilePath(test_case.source_path);
950 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
951
[email protected]0c5ebe32011-08-19 22:37:16952 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49953 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32954 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:16955 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49956 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32957 context.get(), dest_path.DirName(), exclusive, recursive));
958
[email protected]d4905e2e2011-05-13 21:56:32959 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16960 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32961 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49962 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:32963 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16964 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32965 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49966 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:32967
968 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16969 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32970 created = false;
971 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49972 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:32973 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16974 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32975 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49976 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:32977 }
978
[email protected]0c5ebe32011-08-19 22:37:16979 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49980 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:32981 source_path, dest_path, test_case.is_copy_not_move));
982 if (test_case.is_copy_not_move) {
983 base::PlatformFileInfo file_info;
984 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:16985 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49986 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32987 context.get(), source_path, &file_info, &local_path));
988 EXPECT_EQ(kSourceLength, file_info.size);
989 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49990 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:32991 } else {
992 base::PlatformFileInfo file_info;
993 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:16994 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49995 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32996 context.get(), source_path, &file_info, &local_path));
997 }
998 base::PlatformFileInfo file_info;
999 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491000 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321001 context.get(), dest_path, &file_info, &local_path));
1002 EXPECT_EQ(kSourceLength, file_info.size);
1003
1004 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491005 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321006 }
1007}
1008
[email protected]7878ece2011-09-05 11:41:491009TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:161010 FilePath src_path = UTF8ToFilePath("src path");
1011 FilePath dest_path = UTF8ToFilePath("destination path");
1012 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1013 bool created = false;
[email protected]7878ece2011-09-05 11:41:491014 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161015 context.get(), src_path, &created));
1016
1017 bool is_copy = true;
1018 // Copy, no overwrite.
1019 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491020 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161021 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491022 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161023 context.reset(NewContext(NULL));
1024 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491025 ObfuscatedFileUtil::ComputeFilePathCost(dest_path));
[email protected]0c5ebe32011-08-19 22:37:161026 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491027 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161028
1029 // Copy, with overwrite.
1030 context.reset(NewContext(NULL));
1031 context->set_allowed_bytes_growth(0);
1032 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491033 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161034}
1035
[email protected]7878ece2011-09-05 11:41:491036TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]0c5ebe32011-08-19 22:37:161037 FilePath src_path = UTF8ToFilePath("src path");
1038 FilePath dest_path = UTF8ToFilePath("destination path");
1039 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1040 bool created = false;
[email protected]7878ece2011-09-05 11:41:491041 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161042 context.get(), src_path, &created));
1043
1044 bool is_copy = false;
1045 // Move, rename, no overwrite.
1046 context.reset(NewContext(NULL));
1047 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491048 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1049 ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161050 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491051 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161052 context.reset(NewContext(NULL));
1053 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491054 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1055 ObfuscatedFileUtil::ComputeFilePathCost(src_path));
[email protected]0c5ebe32011-08-19 22:37:161056 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491057 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161058
1059 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491060 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161061 context.get(), src_path, &created));
1062
1063 // Move, rename, with overwrite.
1064 context.reset(NewContext(NULL));
1065 context->set_allowed_bytes_growth(0);
1066 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491067 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161068}
1069
[email protected]7878ece2011-09-05 11:41:491070TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]0c5ebe32011-08-19 22:37:161071 FilePath src_path = UTF8ToFilePath("src path");
1072 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1073 bool created = false;
[email protected]7878ece2011-09-05 11:41:491074 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161075 context.get(), src_path, &created));
1076
1077 bool exclusive = true;
1078 bool recursive = false;
1079 FilePath dir_path = UTF8ToFilePath("directory path");
1080 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491081 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161082 context.get(), dir_path, exclusive, recursive));
1083
1084 FilePath dest_path = dir_path.Append(src_path);
1085
1086 bool is_copy = false;
1087 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1088 // Move, no rename, no overwrite.
1089 context.reset(NewContext(NULL));
1090 context->set_allowed_bytes_growth(allowed_bytes_growth);
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 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1094
1095 // Move, no rename, with overwrite.
1096 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491097 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161098 context.get(), src_path, &created));
1099 context.reset(NewContext(NULL));
1100 context->set_allowed_bytes_growth(allowed_bytes_growth);
1101 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491102 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161103 EXPECT_EQ(
1104 allowed_bytes_growth +
[email protected]7878ece2011-09-05 11:41:491105 ObfuscatedFileUtil::ComputeFilePathCost(src_path),
[email protected]0c5ebe32011-08-19 22:37:161106 context->allowed_bytes_growth());
1107}
1108
[email protected]7878ece2011-09-05 11:41:491109TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461110 TestCopyInForeignFileHelper(false /* overwrite */);
1111 TestCopyInForeignFileHelper(true /* overwrite */);
1112}
1113
[email protected]7878ece2011-09-05 11:41:491114TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161115 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321116 FilePath src_path = UTF8ToFilePath("source dir");
1117 bool exclusive = true;
1118 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491119 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321120 context.get(), src_path, exclusive, recursive));
1121
[email protected]89ee4272011-05-16 18:45:171122 std::set<FilePath::StringType> files;
1123 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321124 FillTestDirectory(src_path, &files, &directories);
1125
1126 FilePath dest_path = UTF8ToFilePath("destination dir");
1127
[email protected]0c5ebe32011-08-19 22:37:161128 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491129 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161130 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321131 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491132 ofu()->Copy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321133
1134 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161135 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491136 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161137 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491138 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161139 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321140 recursive = true;
1141 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491142 ofu()->Delete(context.get(), dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161143 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491144 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321145}
[email protected]6b931152011-05-20 21:02:351146
[email protected]7878ece2011-09-05 11:41:491147TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351148 ScopedTempDir source_dir;
1149 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1150 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1151 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1152
1153 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1154 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1155 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1156 FilePath local_src_path = root_path.Append(test_case.path);
1157 if (test_case.is_directory) {
1158 ASSERT_TRUE(
1159 file_util::CreateDirectory(local_src_path));
1160 } else {
1161 base::PlatformFileError error_code;
1162 bool created = false;
1163 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
1164 base::PlatformFile file_handle =
1165 base::CreatePlatformFile(
1166 local_src_path, file_flags, &created, &error_code);
1167 EXPECT_TRUE(created);
[email protected]81b7f662011-05-26 00:54:461168 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]6b931152011-05-20 21:02:351169 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
1170 ASSERT_TRUE(
1171 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
1172 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1173 }
1174 }
1175
[email protected]7878ece2011-09-05 11:41:491176 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351177
1178 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161179 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491180 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]6b931152011-05-20 21:02:351181 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1182 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
1183 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1184 FilePath local_data_path = new_root.Append(test_case.path);
1185#if defined(OS_WIN)
1186 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
1187#endif
[email protected]0c5ebe32011-08-19 22:37:161188 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491189 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351190 FilePath data_path;
1191 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1192 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491193 ofu()->GetFileInfo(context.get(), FilePath(test_case.path),
1194 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351195 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491196 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351197 } else {
1198 base::PlatformFileInfo platform_file_info;
1199 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1200 local_data_path.value());
1201 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1202 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1203 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1204 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161205 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351206 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491207 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1208 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351209 }
1210 }
1211}
[email protected]fcc2d5f2011-05-23 22:06:261212
[email protected]7878ece2011-09-05 11:41:491213TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1214 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1215 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161216 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261217 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161218 EXPECT_EQ(origin(), enumerator->Next());
1219 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1220 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1221 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261222 EXPECT_EQ(GURL(), enumerator->Next());
1223 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1224 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1225
1226 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161227 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261228
1229 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1230 SCOPED_TRACE(testing::Message() <<
1231 "Validating kOriginEnumerationTestRecords " << i);
1232 const OriginEnumerationTestRecord& record =
1233 kOriginEnumerationTestRecords[i];
1234 GURL origin_url(record.origin_url);
1235 origins_expected.insert(origin_url);
1236 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161237 scoped_ptr<FileSystemTestOriginHelper> helper(
1238 NewHelper(origin_url, kFileSystemTypeTemporary));
1239 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261240 context->set_src_origin_url(origin_url);
1241 context->set_src_type(kFileSystemTypeTemporary);
1242 bool created = false;
1243 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491244 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261245 FilePath().AppendASCII("file"), &created));
1246 EXPECT_TRUE(created);
1247 }
1248 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161249 scoped_ptr<FileSystemTestOriginHelper> helper(
1250 NewHelper(origin_url, kFileSystemTypePersistent));
1251 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261252 context->set_src_origin_url(origin_url);
1253 context->set_src_type(kFileSystemTypePersistent);
1254 bool created = false;
1255 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491256 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261257 FilePath().AppendASCII("file"), &created));
1258 EXPECT_TRUE(created);
1259 }
1260 }
[email protected]7878ece2011-09-05 11:41:491261 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261262 EXPECT_TRUE(enumerator.get());
1263 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161264 GURL origin_url;
1265 while (!(origin_url = enumerator->Next()).is_empty()) {
1266 origins_found.insert(origin_url);
1267 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261268 bool found = false;
1269 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1270 ++i) {
1271 const OriginEnumerationTestRecord& record =
1272 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161273 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261274 continue;
1275 found = true;
1276 EXPECT_EQ(record.has_temporary,
1277 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1278 EXPECT_EQ(record.has_persistent,
1279 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1280 }
[email protected]0c5ebe32011-08-19 22:37:161281 // Deal with the default filesystem created by the test helper.
1282 if (!found && origin_url == origin()) {
1283 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1284 EXPECT_EQ(true,
1285 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181286 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161287 found = true;
1288 }
[email protected]fcc2d5f2011-05-23 22:06:261289 EXPECT_TRUE(found);
1290 }
1291
1292 std::set<GURL> diff;
1293 std::set_symmetric_difference(origins_expected.begin(),
1294 origins_expected.end(), origins_found.begin(), origins_found.end(),
1295 inserter(diff, diff.begin()));
1296 EXPECT_TRUE(diff.empty());
1297}
[email protected]0c5ebe32011-08-19 22:37:161298
[email protected]7878ece2011-09-05 11:41:491299TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161300 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1301
1302 int64 expected_quota = 0;
1303
1304 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1305 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1306 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1307 FilePath path(test_case.path);
[email protected]7878ece2011-09-05 11:41:491308 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:161309 if (test_case.is_directory) {
1310 bool exclusive = true;
1311 bool recursive = false;
1312 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491313 ofu()->CreateDirectory(context.get(), path, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161314 } else {
1315 bool created = false;
1316 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491317 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:161318 ASSERT_TRUE(created);
1319 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491320 ofu()->Truncate(context.get(), path,
[email protected]0c5ebe32011-08-19 22:37:161321 test_case.data_file_size));
1322 expected_quota += test_case.data_file_size;
1323 }
1324 }
1325 EXPECT_EQ(expected_quota, SizeInUsageFile());
1326 RevokeUsageCache();
1327 EXPECT_EQ(-1, SizeInUsageFile());
1328 GetUsageFromQuotaManager();
1329 EXPECT_EQ(expected_quota, SizeInUsageFile());
1330 EXPECT_EQ(expected_quota, usage());
1331}
[email protected]34583332011-08-31 08:59:471332
[email protected]7878ece2011-09-05 11:41:491333TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]34583332011-08-31 08:59:471334 const FilePath kPath1 = FilePath().AppendASCII("hoge");
1335 const FilePath kPath2 = FilePath().AppendASCII("fuga");
1336
1337 scoped_ptr<FileSystemOperationContext> context;
1338 base::PlatformFile file;
1339 base::PlatformFileInfo file_info;
1340 FilePath data_path;
1341 bool created = false;
1342
1343 // Create a non-empty file.
1344 context.reset(NewContext(NULL));
1345 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491346 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471347 EXPECT_TRUE(created);
1348 context.reset(NewContext(NULL));
1349 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491350 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471351 context.reset(NewContext(NULL));
1352 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491353 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471354 context.get(), kPath1, &file_info, &data_path));
1355 EXPECT_EQ(10, file_info.size);
1356
1357 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491358 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471359
1360 // Try to get file info of broken file.
1361 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491362 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471363 context.reset(NewContext(NULL));
1364 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491365 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471366 EXPECT_TRUE(created);
1367 context.reset(NewContext(NULL));
1368 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491369 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471370 context.get(), kPath1, &file_info, &data_path));
1371 EXPECT_EQ(0, file_info.size);
1372
1373 // Make another broken file to |kPath2|.
1374 context.reset(NewContext(NULL));
1375 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491376 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471377 EXPECT_TRUE(created);
1378
1379 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491380 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471381
1382 // Repair broken |kPath1|.
1383 context.reset(NewContext(NULL));
1384 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491385 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471386 base::Time::Now()));
1387 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491388 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471389 EXPECT_TRUE(created);
1390
1391 // Copy from sound |kPath1| to broken |kPath2|.
1392 context.reset(NewContext(NULL));
1393 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491394 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]34583332011-08-31 08:59:471395 true /* copy */));
1396
[email protected]7878ece2011-09-05 11:41:491397 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471398 context.reset(NewContext(NULL));
1399 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491400 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471401 context.get(), kPath1,
1402 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1403 &file, &created));
1404 EXPECT_TRUE(created);
1405 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1406 EXPECT_EQ(0, file_info.size);
1407 EXPECT_TRUE(base::ClosePlatformFile(file));
1408}
[email protected]9dfdc0e32011-09-02 06:07:441409
[email protected]7878ece2011-09-05 11:41:491410TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]9dfdc0e32011-09-02 06:07:441411 const FilePath kPath[] = {
1412 FilePath().AppendASCII("foo"),
1413 FilePath().AppendASCII("bar"),
1414 FilePath().AppendASCII("baz")
1415 };
1416 scoped_ptr<FileSystemOperationContext> context;
1417
1418 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1419 bool created = false;
1420 context.reset(NewContext(NULL));
1421 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491422 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441423 EXPECT_TRUE(created);
1424 }
1425
1426 context.reset(NewContext(NULL));
1427 std::vector<base::FileUtilProxy::Entry> entries;
1428 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491429 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441430 EXPECT_EQ(3u, entries.size());
1431
1432 context.reset(NewContext(NULL));
1433 FilePath local_path;
1434 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491435 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441436 EXPECT_TRUE(file_util::Delete(local_path, false));
1437
1438 context.reset(NewContext(NULL));
1439 entries.clear();
1440 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491441 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441442 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1443}