blob: 51e30f6e8c91ae05444696d0ba5ac9eddce174c5 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d4905e2e2011-05-13 21:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]fcc2d5f2011-05-23 22:06:265#include <algorithm>
[email protected]d4905e2e2011-05-13 21:56:326#include <set>
7#include <string>
8
[email protected]4d99be52011-10-18 14:11:039#include "base/bind.h"
[email protected]d4905e2e2011-05-13 21:56:3210#include "base/file_path.h"
11#include "base/file_util.h"
12#include "base/memory/scoped_ptr.h"
[email protected]0c5ebe32011-08-19 22:37:1613#include "base/message_loop.h"
[email protected]d4905e2e2011-05-13 21:56:3214#include "base/platform_file.h"
[email protected]e0785902011-05-19 23:34:1715#include "base/scoped_temp_dir.h"
[email protected]d4905e2e2011-05-13 21:56:3216#include "base/sys_string_conversions.h"
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webkit/fileapi/file_system_context.h"
19#include "webkit/fileapi/file_system_operation_context.h"
[email protected]fcc2d5f2011-05-23 22:06:2620#include "webkit/fileapi/file_system_test_helper.h"
[email protected]0c5ebe32011-08-19 22:37:1621#include "webkit/fileapi/file_system_usage_cache.h"
[email protected]e7e46732012-01-05 11:45:5522#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4923#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5624#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1625#include "webkit/quota/mock_special_storage_policy.h"
26#include "webkit/quota/quota_manager.h"
27#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3228
29using namespace fileapi;
30
31namespace {
32
33FilePath UTF8ToFilePath(const std::string& str) {
34 FilePath::StringType result;
35#if defined(OS_POSIX)
36 result = str;
37#elif defined(OS_WIN)
38 result = base::SysUTF8ToWide(str);
39#endif
40 return FilePath(result);
41}
42
43bool FileExists(const FilePath& path) {
44 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
45}
46
[email protected]81b7f662011-05-26 00:54:4647int64 GetSize(const FilePath& path) {
48 int64 size;
49 EXPECT_TRUE(file_util::GetFileSize(path, &size));
50 return size;
51}
52
[email protected]d4905e2e2011-05-13 21:56:3253// After a move, the dest exists and the source doesn't.
54// After a copy, both source and dest exist.
55struct CopyMoveTestCaseRecord {
56 bool is_copy_not_move;
57 const char source_path[64];
58 const char dest_path[64];
59 bool cause_overwrite;
60};
61
62const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
63 // This is the combinatoric set of:
64 // rename vs. same-name
65 // different directory vs. same directory
66 // overwrite vs. no-overwrite
67 // copy vs. move
68 // We can never be called with source and destination paths identical, so
69 // those cases are omitted.
70 {true, "dir0/file0", "dir0/file1", false},
71 {false, "dir0/file0", "dir0/file1", false},
72 {true, "dir0/file0", "dir0/file1", true},
73 {false, "dir0/file0", "dir0/file1", true},
74
75 {true, "dir0/file0", "dir1/file0", false},
76 {false, "dir0/file0", "dir1/file0", false},
77 {true, "dir0/file0", "dir1/file0", true},
78 {false, "dir0/file0", "dir1/file0", true},
79 {true, "dir0/file0", "dir1/file1", false},
80 {false, "dir0/file0", "dir1/file1", false},
81 {true, "dir0/file0", "dir1/file1", true},
82 {false, "dir0/file0", "dir1/file1", true},
83};
84
[email protected]fcc2d5f2011-05-23 22:06:2685struct OriginEnumerationTestRecord {
86 std::string origin_url;
87 bool has_temporary;
88 bool has_persistent;
89};
90
91const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
92 {"http://example.com", false, true},
93 {"http://example1.com", true, false},
94 {"https://example1.com", true, true},
95 {"file://", false, true},
96 {"http://example.com:8000", false, true},
97};
98
[email protected]d4905e2e2011-05-13 21:56:3299} // namespace (anonymous)
100
101// TODO(ericu): The vast majority of this and the other FSFU subclass tests
102// could theoretically be shared. It would basically be a FSFU interface
103// compliance test, and only the subclass-specific bits that look into the
104// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:49105class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:32106 public:
[email protected]7878ece2011-09-05 11:41:49107 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:26108 : origin_(GURL("http://www.example.com")),
109 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03110 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16111 test_helper_(origin_, type_),
112 quota_status_(quota::kQuotaStatusUnknown),
113 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32114 }
115
116 void SetUp() {
117 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
118
[email protected]e7e46732012-01-05 11:45:55119 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
120 new quota::MockSpecialStoragePolicy();
121
[email protected]0c5ebe32011-08-19 22:37:16122 quota_manager_ = new quota::QuotaManager(
123 false /* is_incognito */,
124 data_dir_.path(),
125 base::MessageLoopProxy::current(),
126 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55127 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16128
129 // Every time we create a new helper, it creates another context, which
130 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49131 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16132 file_system_context_ = new FileSystemContext(
133 base::MessageLoopProxy::current(),
134 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55135 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16136 quota_manager_->proxy(),
137 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55138 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16139
[email protected]7878ece2011-09-05 11:41:49140 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
[email protected]e7e46732012-01-05 11:45:55141 file_system_context_->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16142
143 test_helper_.SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49144 obfuscated_file_util_.get());
[email protected]d4905e2e2011-05-13 21:56:32145 }
146
[email protected]e7e46732012-01-05 11:45:55147 void TearDown() {
148 quota_manager_ = NULL;
149 test_helper_.TearDown();
150 }
151
[email protected]0c5ebe32011-08-19 22:37:16152 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
153 FileSystemOperationContext* context;
154 if (helper)
155 context = helper->NewOperationContext();
156 else
157 context = test_helper_.NewOperationContext();
158 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]d4905e2e2011-05-13 21:56:32159 return context;
160 }
161
[email protected]0c5ebe32011-08-19 22:37:16162 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49163 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16164 // Use this for tests which need to run in multiple origins; we need a test
165 // helper per origin.
166 FileSystemTestOriginHelper* NewHelper(
167 const GURL& origin, fileapi::FileSystemType type) {
168 FileSystemTestOriginHelper* helper =
169 new FileSystemTestOriginHelper(origin, type);
170
171 helper->SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49172 obfuscated_file_util_.get());
[email protected]0c5ebe32011-08-19 22:37:16173 return helper;
174 }
175
[email protected]7878ece2011-09-05 11:41:49176 ObfuscatedFileUtil* ofu() {
177 return obfuscated_file_util_.get();
[email protected]d4905e2e2011-05-13 21:56:32178 }
179
[email protected]6b931152011-05-20 21:02:35180 const FilePath& test_directory() const {
181 return data_dir_.path();
182 }
183
[email protected]0c5ebe32011-08-19 22:37:16184 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26185 return origin_;
186 }
187
188 fileapi::FileSystemType type() const {
189 return type_;
190 }
191
[email protected]0c5ebe32011-08-19 22:37:16192 void GetUsageFromQuotaManager() {
193 quota_manager_->GetUsageAndQuota(
194 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03195 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
196 weak_factory_.GetWeakPtr()));
[email protected]0c5ebe32011-08-19 22:37:16197 MessageLoop::current()->RunAllPending();
198 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
199 }
200
201 void RevokeUsageCache() {
202 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
203 ASSERT_TRUE(test_helper_.RevokeUsageCache());
204 }
205
206 int64 SizeInUsageFile() {
207 return test_helper_.GetCachedOriginUsage();
208 }
209
210 int64 usage() const { return usage_; }
211
212 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
213 EXPECT_EQ(quota::kQuotaStatusOk, status);
214 quota_status_ = status;
215 usage_ = usage;
216 }
217
[email protected]d4905e2e2011-05-13 21:56:32218 void CheckFileAndCloseHandle(
219 const FilePath& virtual_path, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16220 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32221 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49222 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32223 context.get(), virtual_path, &local_path));
224
225 base::PlatformFileInfo file_info0;
226 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49227 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32228 context.get(), virtual_path, &file_info0, &data_path));
229 EXPECT_EQ(data_path, local_path);
230 EXPECT_TRUE(FileExists(data_path));
231 EXPECT_EQ(0, GetSize(data_path));
232
233 const char data[] = "test data";
234 const int length = arraysize(data) - 1;
235
236 if (base::kInvalidPlatformFileValue == file_handle) {
237 bool created = true;
238 PlatformFileError error;
239 file_handle = base::CreatePlatformFile(
240 data_path,
241 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
242 &created,
243 &error);
[email protected]81b7f662011-05-26 00:54:46244 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32245 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
246 EXPECT_FALSE(created);
247 }
248 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
249 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
250
251 base::PlatformFileInfo file_info1;
252 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16253 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49254 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32255 context.get(), virtual_path, &file_info1, &data_path));
256 EXPECT_EQ(data_path, local_path);
257
258 EXPECT_FALSE(file_info0.is_directory);
259 EXPECT_FALSE(file_info1.is_directory);
260 EXPECT_FALSE(file_info0.is_symbolic_link);
261 EXPECT_FALSE(file_info1.is_symbolic_link);
262 EXPECT_EQ(0, file_info0.size);
263 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32264 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32265
[email protected]0c5ebe32011-08-19 22:37:16266 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49267 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32268 context.get(), virtual_path, length * 2));
269 EXPECT_EQ(length * 2, GetSize(data_path));
270
[email protected]0c5ebe32011-08-19 22:37:16271 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49272 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]0c5ebe32011-08-19 22:37:16273 context.get(), virtual_path, 0));
274 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32275 }
276
277 void ValidateTestDirectory(
278 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17279 const std::set<FilePath::StringType>& files,
280 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32281 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17282 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32283 for (iter = files.begin(); iter != files.end(); ++iter) {
284 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16285 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32286 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49287 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17288 context.get(), root_path.Append(*iter),
[email protected]d4905e2e2011-05-13 21:56:32289 &created));
290 ASSERT_FALSE(created);
291 }
292 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16293 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49294 EXPECT_TRUE(ofu()->DirectoryExists(context.get(),
[email protected]89ee4272011-05-16 18:45:17295 root_path.Append(*iter)));
[email protected]d4905e2e2011-05-13 21:56:32296 }
297 }
298
299 void FillTestDirectory(
300 const FilePath& root_path,
[email protected]89ee4272011-05-16 18:45:17301 std::set<FilePath::StringType>* files,
302 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32303 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16304 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32305 std::vector<base::FileUtilProxy::Entry> entries;
306 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49307 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32308 EXPECT_EQ(0UL, entries.size());
309
310 files->clear();
[email protected]89ee4272011-05-16 18:45:17311 files->insert(FILE_PATH_LITERAL("first"));
312 files->insert(FILE_PATH_LITERAL("second"));
313 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32314 directories->clear();
[email protected]89ee4272011-05-16 18:45:17315 directories->insert(FILE_PATH_LITERAL("fourth"));
316 directories->insert(FILE_PATH_LITERAL("fifth"));
317 directories->insert(FILE_PATH_LITERAL("sixth"));
318 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32319 for (iter = files->begin(); iter != files->end(); ++iter) {
320 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16321 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32322 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49323 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17324 context.get(), root_path.Append(*iter), &created));
[email protected]d4905e2e2011-05-13 21:56:32325 ASSERT_TRUE(created);
326 }
327 for (iter = directories->begin(); iter != directories->end(); ++iter) {
328 bool exclusive = true;
329 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16330 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32331 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49332 ofu()->CreateDirectory(
[email protected]89ee4272011-05-16 18:45:17333 context.get(), root_path.Append(*iter), exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32334 }
335 ValidateTestDirectory(root_path, *files, *directories);
336 }
337
338 void TestReadDirectoryHelper(const FilePath& root_path) {
[email protected]89ee4272011-05-16 18:45:17339 std::set<FilePath::StringType> files;
340 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:32341 FillTestDirectory(root_path, &files, &directories);
342
343 scoped_ptr<FileSystemOperationContext> context;
344 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16345 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32346 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49347 ofu()->ReadDirectory(context.get(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32348 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
349 EXPECT_EQ(files.size() + directories.size(), entries.size());
350 for (entry_iter = entries.begin(); entry_iter != entries.end();
351 ++entry_iter) {
352 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17353 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32354 if (iter != files.end()) {
355 EXPECT_FALSE(entry.is_directory);
356 files.erase(iter);
357 continue;
358 }
[email protected]89ee4272011-05-16 18:45:17359 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32360 EXPECT_FALSE(directories.end() == iter);
361 EXPECT_TRUE(entry.is_directory);
362 directories.erase(iter);
363 }
364 }
365
[email protected]2517cfa2011-08-25 05:12:41366 void TestTouchHelper(const FilePath& path, bool is_file) {
367 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32368 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16369
[email protected]2517cfa2011-08-25 05:12:41370 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32371 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49372 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32373 context.get(), path, last_access_time, last_modified_time));
374 FilePath local_path;
375 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16376 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49377 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32378 context.get(), path, &file_info, &local_path));
379 // We compare as time_t here to lower our resolution, to avoid false
380 // negatives caused by conversion to the local filesystem's native
381 // representation and back.
382 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
383
[email protected]0c5ebe32011-08-19 22:37:16384 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32385 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41386 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32387 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49388 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32389 context.get(), path, last_access_time, last_modified_time));
[email protected]0c5ebe32011-08-19 22:37:16390 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49391 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32392 context.get(), path, &file_info, &local_path));
393 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49394 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41395 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32396 }
397
[email protected]81b7f662011-05-26 00:54:46398 void TestCopyInForeignFileHelper(bool overwrite) {
399 ScopedTempDir source_dir;
400 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
401 FilePath root_path = source_dir.path();
402 FilePath src_path = root_path.AppendASCII("file_name");
403 FilePath dest_path(FILE_PATH_LITERAL("new file"));
404 int64 src_file_length = 87;
405
406 base::PlatformFileError error_code;
407 bool created = false;
408 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
409 base::PlatformFile file_handle =
410 base::CreatePlatformFile(
411 src_path, file_flags, &created, &error_code);
412 EXPECT_TRUE(created);
413 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
414 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
415 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
416 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
417
418 scoped_ptr<FileSystemOperationContext> context;
419
420 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16421 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46422 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49423 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]81b7f662011-05-26 00:54:46424 EXPECT_TRUE(created);
425 }
426
[email protected]0c5ebe32011-08-19 22:37:16427 const int64 path_cost =
[email protected]7878ece2011-09-05 11:41:49428 ObfuscatedFileUtil::ComputeFilePathCost(dest_path);
[email protected]0c5ebe32011-08-19 22:37:16429 if (!overwrite) {
430 // Verify that file creation requires sufficient quota for the path.
431 context.reset(NewContext(NULL));
432 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
433 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49434 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16435 }
436
437 context.reset(NewContext(NULL));
438 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46439 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49440 ofu()->CopyInForeignFile(context.get(), src_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16441
442 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49443 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16444 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49445 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16446 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46447 base::PlatformFileInfo file_info;
448 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49449 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]81b7f662011-05-26 00:54:46450 context.get(), dest_path, &file_info, &data_path));
451 EXPECT_NE(data_path, src_path);
452 EXPECT_TRUE(FileExists(data_path));
453 EXPECT_EQ(src_file_length, GetSize(data_path));
454
455 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49456 ofu()->DeleteFile(context.get(), dest_path));
[email protected]81b7f662011-05-26 00:54:46457 }
458
[email protected]fad625e2f2011-12-08 05:38:03459 void ClearTimestamp(const FilePath& path) {
460 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
461 EXPECT_EQ(base::PLATFORM_FILE_OK,
462 ofu()->Touch(context.get(), path, base::Time(), base::Time()));
463 EXPECT_EQ(base::Time(), GetModifiedTime(path));
464 }
465
466 base::Time GetModifiedTime(const FilePath& path) {
467 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
468 FilePath data_path;
469 base::PlatformFileInfo file_info;
470 context.reset(NewContext(NULL));
471 EXPECT_EQ(base::PLATFORM_FILE_OK,
472 ofu()->GetFileInfo(context.get(), path, &file_info, &data_path));
473 return file_info.last_modified;
474 }
475
476 void TestDirectoryTimestampHelper(const FilePath& base_dir,
477 bool copy,
478 bool overwrite) {
479 scoped_ptr<FileSystemOperationContext> context;
480 const FilePath src_dir_path(base_dir.AppendASCII("foo_dir"));
481 const FilePath dest_dir_path(base_dir.AppendASCII("bar_dir"));
482
483 const FilePath src_file_path(src_dir_path.AppendASCII("hoge"));
484 const FilePath dest_file_path(dest_dir_path.AppendASCII("fuga"));
485
486 context.reset(NewContext(NULL));
487 EXPECT_EQ(base::PLATFORM_FILE_OK,
488 ofu()->CreateDirectory(context.get(), src_dir_path, true, true));
489 context.reset(NewContext(NULL));
490 EXPECT_EQ(base::PLATFORM_FILE_OK,
491 ofu()->CreateDirectory(context.get(), dest_dir_path, true, true));
492
493 bool created = false;
494 context.reset(NewContext(NULL));
495 EXPECT_EQ(base::PLATFORM_FILE_OK,
496 ofu()->EnsureFileExists(context.get(), src_file_path, &created));
497 if (overwrite) {
498 context.reset(NewContext(NULL));
499 EXPECT_EQ(base::PLATFORM_FILE_OK,
500 ofu()->EnsureFileExists(context.get(),
501 dest_file_path, &created));
502 }
503
504 ClearTimestamp(src_dir_path);
505 ClearTimestamp(dest_dir_path);
506 context.reset(NewContext(NULL));
507 EXPECT_EQ(base::PLATFORM_FILE_OK,
508 ofu()->CopyOrMoveFile(context.get(),
509 src_file_path, dest_file_path,
510 copy));
511
512 if (copy)
513 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path));
514 else
515 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path));
516 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path));
517 }
518
[email protected]d4905e2e2011-05-13 21:56:32519 private:
520 ScopedTempDir data_dir_;
[email protected]7878ece2011-09-05 11:41:49521 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16522 scoped_refptr<quota::QuotaManager> quota_manager_;
523 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26524 GURL origin_;
525 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03526 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26527 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16528 quota::QuotaStatusCode quota_status_;
529 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32530
[email protected]7878ece2011-09-05 11:41:49531 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32532};
533
[email protected]7878ece2011-09-05 11:41:49534TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32535 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
536 bool created;
537 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16538 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32539 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
540
541 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49542 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32543 context.get(), path, file_flags, &file_handle,
544 &created));
545
[email protected]0c5ebe32011-08-19 22:37:16546 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32547 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49548 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32549
550 path = UTF8ToFilePath("test file");
551
[email protected]0c5ebe32011-08-19 22:37:16552 // Verify that file creation requires sufficient quota for the path.
553 context.reset(NewContext(NULL));
554 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49555 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16556 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49557 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16558 context.get(), path, file_flags, &file_handle, &created));
559
560 context.reset(NewContext(NULL));
561 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49562 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32563 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49564 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32565 context.get(), path, file_flags, &file_handle, &created));
566 ASSERT_TRUE(created);
567 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
568
569 CheckFileAndCloseHandle(path, file_handle);
570
[email protected]0c5ebe32011-08-19 22:37:16571 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32572 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49573 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32574 context.get(), path, &local_path));
575 EXPECT_TRUE(file_util::PathExists(local_path));
576
[email protected]0c5ebe32011-08-19 22:37:16577 // Verify that deleting a file isn't stopped by zero quota, and that it frees
578 // up quote from its path.
579 context.reset(NewContext(NULL));
580 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32581 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49582 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32583 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]7878ece2011-09-05 11:41:49584 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16585 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32586
[email protected]0c5ebe32011-08-19 22:37:16587 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32588 bool exclusive = true;
589 bool recursive = true;
590 FilePath directory_path = UTF8ToFilePath("series/of/directories");
591 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49592 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32593 context.get(), directory_path, exclusive, recursive));
594
[email protected]0c5ebe32011-08-19 22:37:16595 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32596 file_handle = base::kInvalidPlatformFileValue;
597 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49598 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32599 context.get(), path, file_flags, &file_handle, &created));
600 ASSERT_TRUE(created);
601 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
602
603 CheckFileAndCloseHandle(path, file_handle);
604
[email protected]0c5ebe32011-08-19 22:37:16605 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49606 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32607 context.get(), path, &local_path));
608 EXPECT_TRUE(file_util::PathExists(local_path));
609
[email protected]0c5ebe32011-08-19 22:37:16610 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32611 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49612 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32613 EXPECT_FALSE(file_util::PathExists(local_path));
614}
615
[email protected]7878ece2011-09-05 11:41:49616TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32617 bool created = false;
618 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16619 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32620
621 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49622 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32623
[email protected]0c5ebe32011-08-19 22:37:16624 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32625 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49626 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32627 ASSERT_TRUE(created);
628
[email protected]0c5ebe32011-08-19 22:37:16629 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32630 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49631 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32632 context.get(), path, &local_path));
633 EXPECT_EQ(0, GetSize(local_path));
634
[email protected]0c5ebe32011-08-19 22:37:16635 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49636 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32637 context.get(), path, 10));
638 EXPECT_EQ(10, GetSize(local_path));
639
[email protected]0c5ebe32011-08-19 22:37:16640 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49641 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32642 context.get(), path, 1));
643 EXPECT_EQ(1, GetSize(local_path));
644
[email protected]0c5ebe32011-08-19 22:37:16645 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49646 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16647 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49648 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32649}
650
[email protected]7878ece2011-09-05 11:41:49651TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]d4905e2e2011-05-13 21:56:32652 FilePath path = UTF8ToFilePath("fake/file");
653 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16654 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32655 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49656 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32657 context.get(), path, &created));
658
[email protected]0c5ebe32011-08-19 22:37:16659 // Verify that file creation requires sufficient quota for the path.
660 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32661 path = UTF8ToFilePath("test file");
662 created = false;
[email protected]0c5ebe32011-08-19 22:37:16663 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49664 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
[email protected]0c5ebe32011-08-19 22:37:16665 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49666 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16667 ASSERT_FALSE(created);
668
669 context.reset(NewContext(NULL));
670 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49671 ObfuscatedFileUtil::ComputeFilePathCost(path));
[email protected]d4905e2e2011-05-13 21:56:32672 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49673 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32674 ASSERT_TRUE(created);
675
676 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
677
[email protected]0c5ebe32011-08-19 22:37:16678 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32679 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49680 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32681 ASSERT_FALSE(created);
682
683 // Also test in a subdirectory.
684 path = UTF8ToFilePath("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16685 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32686 bool exclusive = true;
687 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49688 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32689 context.get(), path.DirName(), exclusive, recursive));
690
[email protected]0c5ebe32011-08-19 22:37:16691 context.reset(NewContext(NULL));
[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);
[email protected]0c5ebe32011-08-19 22:37:16695 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49696 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16697 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49698 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32699}
700
[email protected]7878ece2011-09-05 11:41:49701TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16702 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32703
704 bool exclusive = false;
705 bool recursive = false;
706 FilePath path = UTF8ToFilePath("foo/bar");
[email protected]7878ece2011-09-05 11:41:49707 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32708 context.get(), path, exclusive, recursive));
709
[email protected]0c5ebe32011-08-19 22:37:16710 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32711 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49712 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32713
714 FilePath root = UTF8ToFilePath("");
[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_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16719 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49720 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32721
[email protected]0c5ebe32011-08-19 22:37:16722 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32723 exclusive = false;
724 recursive = true;
[email protected]7878ece2011-09-05 11:41:49725 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32726 context.get(), path, exclusive, recursive));
727
[email protected]0c5ebe32011-08-19 22:37:16728 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49729 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16730 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49731 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16732 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49733 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]0c5ebe32011-08-19 22:37:16734 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49735 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16736 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49737 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32738
739 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16740 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06741 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49742 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32743
744 base::PlatformFileInfo file_info;
745 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49746 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32747 context.get(), path, &file_info, &local_path));
748 EXPECT_TRUE(local_path.empty());
749 EXPECT_TRUE(file_info.is_directory);
750 EXPECT_FALSE(file_info.is_symbolic_link);
751
752 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16753 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49754 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32755 context.get(), path, exclusive, recursive));
756
757 exclusive = true;
758 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16759 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49760 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32761 context.get(), path, exclusive, recursive));
762
[email protected]0c5ebe32011-08-19 22:37:16763 // Verify that deleting a directory isn't stopped by zero quota, and that it
764 // frees up quota from its path.
765 context.reset(NewContext(NULL));
766 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32767 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49768 ofu()->DeleteSingleDirectory(context.get(), path));
769 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path),
[email protected]0c5ebe32011-08-19 22:37:16770 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32771
772 path = UTF8ToFilePath("foo/bop");
773
[email protected]0c5ebe32011-08-19 22:37:16774 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49775 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16776 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49777 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16778 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49779 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
780 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32781 context.get(), path, &file_info, &local_path));
782
[email protected]0c5ebe32011-08-19 22:37:16783 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32784 exclusive = true;
785 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16786 context.reset(NewContext(NULL));
787 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49788 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1);
789 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16790 context.get(), path, exclusive, recursive));
791
792 context.reset(NewContext(NULL));
793 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:49794 ObfuscatedFileUtil::ComputeFilePathCost(path));
795 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32796 context.get(), path, exclusive, recursive));
797
[email protected]0c5ebe32011-08-19 22:37:16798 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49799 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16800 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49801 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32802
803 exclusive = true;
804 recursive = false;
[email protected]7878ece2011-09-05 11:41:49805 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32806 context.get(), path, exclusive, recursive));
807
808 exclusive = true;
809 recursive = false;
810 path = UTF8ToFilePath("foo");
[email protected]7878ece2011-09-05 11:41:49811 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32812 context.get(), path, exclusive, recursive));
813
814 path = UTF8ToFilePath("blah");
815
[email protected]0c5ebe32011-08-19 22:37:16816 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49817 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16818 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49819 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32820
821 exclusive = true;
822 recursive = false;
[email protected]7878ece2011-09-05 11:41:49823 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32824 context.get(), path, exclusive, recursive));
825
[email protected]0c5ebe32011-08-19 22:37:16826 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49827 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16828 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49829 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32830
831 exclusive = true;
832 recursive = false;
[email protected]7878ece2011-09-05 11:41:49833 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32834 context.get(), path, exclusive, recursive));
835}
836
[email protected]7878ece2011-09-05 11:41:49837TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16838 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32839 bool exclusive = true;
840 bool recursive = true;
841 FilePath path = UTF8ToFilePath("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49842 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32843 context.get(), path, exclusive, recursive));
844 TestReadDirectoryHelper(path);
845}
846
[email protected]7878ece2011-09-05 11:41:49847TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]d4905e2e2011-05-13 21:56:32848 TestReadDirectoryHelper(UTF8ToFilePath(""));
849}
850
[email protected]7878ece2011-09-05 11:41:49851TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]d4905e2e2011-05-13 21:56:32852 TestReadDirectoryHelper(UTF8ToFilePath("/"));
853}
854
[email protected]7878ece2011-09-05 11:41:49855TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]d4905e2e2011-05-13 21:56:32856 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16857 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32858
859 bool created = false;
860 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49861 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32862 ASSERT_TRUE(created);
863
[email protected]0c5ebe32011-08-19 22:37:16864 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32865 std::vector<base::FileUtilProxy::Entry> entries;
866 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49867 ofu()->ReadDirectory(context.get(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32868
[email protected]7878ece2011-09-05 11:41:49869 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32870}
871
[email protected]7878ece2011-09-05 11:41:49872TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]2517cfa2011-08-25 05:12:41873 FilePath path = UTF8ToFilePath("file");
[email protected]0c5ebe32011-08-19 22:37:16874 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41875
876 base::Time last_access_time = base::Time::Now();
877 base::Time last_modified_time = base::Time::Now();
878
879 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:32880 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49881 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32882 context.get(), path, last_access_time, last_modified_time));
883
[email protected]2517cfa2011-08-25 05:12:41884 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:16885 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:41886 bool created = false;
887 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49888 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41889 ASSERT_TRUE(created);
890 TestTouchHelper(path, true);
891
892 // Now test a directory:
893 context.reset(NewContext(NULL));
894 bool exclusive = true;
895 bool recursive = false;
896 path = UTF8ToFilePath("dir");
[email protected]7878ece2011-09-05 11:41:49897 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:41898 path, exclusive, recursive));
899 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:16900}
901
[email protected]7878ece2011-09-05 11:41:49902TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:16903 FilePath path = UTF8ToFilePath("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16904 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
905
[email protected]0c5ebe32011-08-19 22:37:16906 path = UTF8ToFilePath("file name");
907 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:41908 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16909 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49910 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41911 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:16912 context->set_allowed_bytes_growth(1024);
913 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49914 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:41915 EXPECT_TRUE(created);
[email protected]7878ece2011-09-05 11:41:49916 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:16917 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
918
919 context->set_allowed_bytes_growth(1024);
920 bool exclusive = true;
921 bool recursive = true;
922 path = UTF8ToFilePath("directory/to/use");
923 std::vector<FilePath::StringType> components;
924 path.GetComponents(&components);
925 path_cost = 0;
926 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
927 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:49928 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:16929 FilePath(*iter));
930 }
931 context.reset(NewContext(NULL));
932 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:49933 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16934 context.get(), path, exclusive, recursive));
935 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32936}
937
[email protected]7878ece2011-09-05 11:41:49938TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]d4905e2e2011-05-13 21:56:32939 FilePath source_path = UTF8ToFilePath("path0.txt");
940 FilePath dest_path = UTF8ToFilePath("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:16941 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32942
943 bool is_copy_not_move = false;
944 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49945 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32946 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16947 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32948 is_copy_not_move = true;
949 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49950 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32951 is_copy_not_move));
952 source_path = UTF8ToFilePath("dir/dir/file");
953 bool exclusive = true;
954 bool recursive = true;
[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(), source_path.DirName(), exclusive, recursive));
958 is_copy_not_move = false;
959 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49960 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:32961 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:16962 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32963 is_copy_not_move = true;
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));
967}
968
[email protected]7878ece2011-09-05 11:41:49969TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:32970 const int64 kSourceLength = 5;
971 const int64 kDestLength = 50;
972
973 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
974 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
975 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
976 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
977 test_case.is_copy_not_move);
978 SCOPED_TRACE(testing::Message() << "\t source_path " <<
979 test_case.source_path);
980 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
981 test_case.dest_path);
982 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
983 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:16984 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32985
986 bool exclusive = false;
987 bool recursive = true;
988 FilePath source_path = UTF8ToFilePath(test_case.source_path);
989 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
990
[email protected]0c5ebe32011-08-19 22:37:16991 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49992 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32993 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:16994 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49995 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32996 context.get(), dest_path.DirName(), exclusive, recursive));
997
[email protected]d4905e2e2011-05-13 21:56:32998 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16999 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321000 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491001 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321002 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161003 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321004 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491005 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321006
1007 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161008 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321009 created = false;
1010 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491011 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321012 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161013 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321014 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491015 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321016 }
1017
[email protected]0c5ebe32011-08-19 22:37:161018 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491019 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:321020 source_path, dest_path, test_case.is_copy_not_move));
1021 if (test_case.is_copy_not_move) {
1022 base::PlatformFileInfo file_info;
1023 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161024 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491025 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321026 context.get(), source_path, &file_info, &local_path));
1027 EXPECT_EQ(kSourceLength, file_info.size);
1028 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491029 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:321030 } else {
1031 base::PlatformFileInfo file_info;
1032 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161033 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491034 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321035 context.get(), source_path, &file_info, &local_path));
1036 }
1037 base::PlatformFileInfo file_info;
1038 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491039 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321040 context.get(), dest_path, &file_info, &local_path));
1041 EXPECT_EQ(kSourceLength, file_info.size);
1042
1043 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491044 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321045 }
1046}
1047
[email protected]7878ece2011-09-05 11:41:491048TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]0c5ebe32011-08-19 22:37:161049 FilePath src_path = UTF8ToFilePath("src path");
1050 FilePath dest_path = UTF8ToFilePath("destination path");
1051 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1052 bool created = false;
[email protected]7878ece2011-09-05 11:41:491053 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161054 context.get(), src_path, &created));
1055
1056 bool is_copy = true;
1057 // Copy, no overwrite.
1058 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491059 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161060 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491061 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161062 context.reset(NewContext(NULL));
1063 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491064 ObfuscatedFileUtil::ComputeFilePathCost(dest_path));
[email protected]0c5ebe32011-08-19 22:37:161065 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491066 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161067
1068 // Copy, with overwrite.
1069 context.reset(NewContext(NULL));
1070 context->set_allowed_bytes_growth(0);
1071 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491072 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161073}
1074
[email protected]7878ece2011-09-05 11:41:491075TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]0c5ebe32011-08-19 22:37:161076 FilePath src_path = UTF8ToFilePath("src path");
1077 FilePath dest_path = UTF8ToFilePath("destination path");
1078 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1079 bool created = false;
[email protected]7878ece2011-09-05 11:41:491080 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161081 context.get(), src_path, &created));
1082
1083 bool is_copy = false;
1084 // Move, rename, no overwrite.
1085 context.reset(NewContext(NULL));
1086 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491087 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1088 ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1);
[email protected]0c5ebe32011-08-19 22:37:161089 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491090 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161091 context.reset(NewContext(NULL));
1092 context->set_allowed_bytes_growth(
[email protected]7878ece2011-09-05 11:41:491093 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) -
1094 ObfuscatedFileUtil::ComputeFilePathCost(src_path));
[email protected]0c5ebe32011-08-19 22:37:161095 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491096 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161097
1098 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491099 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161100 context.get(), src_path, &created));
1101
1102 // Move, rename, with overwrite.
1103 context.reset(NewContext(NULL));
1104 context->set_allowed_bytes_growth(0);
1105 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491106 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161107}
1108
[email protected]7878ece2011-09-05 11:41:491109TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]0c5ebe32011-08-19 22:37:161110 FilePath src_path = UTF8ToFilePath("src path");
1111 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1112 bool created = false;
[email protected]7878ece2011-09-05 11:41:491113 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161114 context.get(), src_path, &created));
1115
1116 bool exclusive = true;
1117 bool recursive = false;
1118 FilePath dir_path = UTF8ToFilePath("directory path");
1119 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491120 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161121 context.get(), dir_path, exclusive, recursive));
1122
1123 FilePath dest_path = dir_path.Append(src_path);
1124
1125 bool is_copy = false;
1126 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1127 // Move, no rename, no overwrite.
1128 context.reset(NewContext(NULL));
1129 context->set_allowed_bytes_growth(allowed_bytes_growth);
1130 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491131 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161132 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1133
1134 // Move, no rename, with overwrite.
1135 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491136 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161137 context.get(), src_path, &created));
1138 context.reset(NewContext(NULL));
1139 context->set_allowed_bytes_growth(allowed_bytes_growth);
1140 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491141 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161142 EXPECT_EQ(
1143 allowed_bytes_growth +
[email protected]7878ece2011-09-05 11:41:491144 ObfuscatedFileUtil::ComputeFilePathCost(src_path),
[email protected]0c5ebe32011-08-19 22:37:161145 context->allowed_bytes_growth());
1146}
1147
[email protected]7878ece2011-09-05 11:41:491148TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461149 TestCopyInForeignFileHelper(false /* overwrite */);
1150 TestCopyInForeignFileHelper(true /* overwrite */);
1151}
1152
[email protected]7878ece2011-09-05 11:41:491153TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161154 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321155 FilePath src_path = UTF8ToFilePath("source dir");
1156 bool exclusive = true;
1157 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491158 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321159 context.get(), src_path, exclusive, recursive));
1160
[email protected]89ee4272011-05-16 18:45:171161 std::set<FilePath::StringType> files;
1162 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321163 FillTestDirectory(src_path, &files, &directories);
1164
1165 FilePath dest_path = UTF8ToFilePath("destination dir");
1166
[email protected]0c5ebe32011-08-19 22:37:161167 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491168 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161169 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321170 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491171 ofu()->Copy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321172
1173 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161174 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491175 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161176 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491177 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161178 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321179 recursive = true;
1180 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491181 ofu()->Delete(context.get(), dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161182 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491183 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321184}
[email protected]6b931152011-05-20 21:02:351185
[email protected]7878ece2011-09-05 11:41:491186TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351187 ScopedTempDir source_dir;
1188 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1189 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1190 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1191
[email protected]f83b5b72012-01-27 10:26:561192 test::SetUpRegularTestCases(root_path);
[email protected]6b931152011-05-20 21:02:351193
[email protected]7878ece2011-09-05 11:41:491194 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351195
1196 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161197 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491198 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]f83b5b72012-01-27 10:26:561199 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]6b931152011-05-20 21:02:351200 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561201 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]6b931152011-05-20 21:02:351202 FilePath local_data_path = new_root.Append(test_case.path);
1203#if defined(OS_WIN)
1204 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
1205#endif
[email protected]0c5ebe32011-08-19 22:37:161206 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491207 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351208 FilePath data_path;
1209 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1210 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491211 ofu()->GetFileInfo(context.get(), FilePath(test_case.path),
1212 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351213 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491214 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351215 } else {
1216 base::PlatformFileInfo platform_file_info;
1217 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1218 local_data_path.value());
1219 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1220 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1221 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1222 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161223 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351224 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491225 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1226 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351227 }
1228 }
1229}
[email protected]fcc2d5f2011-05-23 22:06:261230
[email protected]7878ece2011-09-05 11:41:491231TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1232 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1233 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161234 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261235 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161236 EXPECT_EQ(origin(), enumerator->Next());
1237 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1238 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1239 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261240 EXPECT_EQ(GURL(), enumerator->Next());
1241 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1242 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1243
1244 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161245 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261246
1247 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1248 SCOPED_TRACE(testing::Message() <<
1249 "Validating kOriginEnumerationTestRecords " << i);
1250 const OriginEnumerationTestRecord& record =
1251 kOriginEnumerationTestRecords[i];
1252 GURL origin_url(record.origin_url);
1253 origins_expected.insert(origin_url);
1254 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161255 scoped_ptr<FileSystemTestOriginHelper> helper(
1256 NewHelper(origin_url, kFileSystemTypeTemporary));
1257 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261258 context->set_src_origin_url(origin_url);
1259 context->set_src_type(kFileSystemTypeTemporary);
1260 bool created = false;
1261 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491262 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261263 FilePath().AppendASCII("file"), &created));
1264 EXPECT_TRUE(created);
1265 }
1266 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161267 scoped_ptr<FileSystemTestOriginHelper> helper(
1268 NewHelper(origin_url, kFileSystemTypePersistent));
1269 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261270 context->set_src_origin_url(origin_url);
1271 context->set_src_type(kFileSystemTypePersistent);
1272 bool created = false;
1273 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491274 ofu()->EnsureFileExists(context.get(),
[email protected]fcc2d5f2011-05-23 22:06:261275 FilePath().AppendASCII("file"), &created));
1276 EXPECT_TRUE(created);
1277 }
1278 }
[email protected]7878ece2011-09-05 11:41:491279 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261280 EXPECT_TRUE(enumerator.get());
1281 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161282 GURL origin_url;
1283 while (!(origin_url = enumerator->Next()).is_empty()) {
1284 origins_found.insert(origin_url);
1285 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261286 bool found = false;
1287 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1288 ++i) {
1289 const OriginEnumerationTestRecord& record =
1290 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161291 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261292 continue;
1293 found = true;
1294 EXPECT_EQ(record.has_temporary,
1295 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1296 EXPECT_EQ(record.has_persistent,
1297 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1298 }
[email protected]0c5ebe32011-08-19 22:37:161299 // Deal with the default filesystem created by the test helper.
1300 if (!found && origin_url == origin()) {
1301 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1302 EXPECT_EQ(true,
1303 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181304 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161305 found = true;
1306 }
[email protected]fcc2d5f2011-05-23 22:06:261307 EXPECT_TRUE(found);
1308 }
1309
1310 std::set<GURL> diff;
1311 std::set_symmetric_difference(origins_expected.begin(),
1312 origins_expected.end(), origins_found.begin(), origins_found.end(),
1313 inserter(diff, diff.begin()));
1314 EXPECT_TRUE(diff.empty());
1315}
[email protected]0c5ebe32011-08-19 22:37:161316
[email protected]7878ece2011-09-05 11:41:491317TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161318 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1319
1320 int64 expected_quota = 0;
1321
[email protected]f83b5b72012-01-27 10:26:561322 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]0c5ebe32011-08-19 22:37:161323 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561324 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]0c5ebe32011-08-19 22:37:161325 FilePath path(test_case.path);
[email protected]7878ece2011-09-05 11:41:491326 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path);
[email protected]0c5ebe32011-08-19 22:37:161327 if (test_case.is_directory) {
1328 bool exclusive = true;
1329 bool recursive = false;
1330 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491331 ofu()->CreateDirectory(context.get(), path, exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161332 } else {
1333 bool created = false;
1334 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491335 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:161336 ASSERT_TRUE(created);
1337 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491338 ofu()->Truncate(context.get(), path,
[email protected]0c5ebe32011-08-19 22:37:161339 test_case.data_file_size));
1340 expected_quota += test_case.data_file_size;
1341 }
1342 }
1343 EXPECT_EQ(expected_quota, SizeInUsageFile());
1344 RevokeUsageCache();
1345 EXPECT_EQ(-1, SizeInUsageFile());
1346 GetUsageFromQuotaManager();
1347 EXPECT_EQ(expected_quota, SizeInUsageFile());
1348 EXPECT_EQ(expected_quota, usage());
1349}
[email protected]34583332011-08-31 08:59:471350
[email protected]7878ece2011-09-05 11:41:491351TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]34583332011-08-31 08:59:471352 const FilePath kPath1 = FilePath().AppendASCII("hoge");
1353 const FilePath kPath2 = FilePath().AppendASCII("fuga");
1354
1355 scoped_ptr<FileSystemOperationContext> context;
1356 base::PlatformFile file;
1357 base::PlatformFileInfo file_info;
1358 FilePath data_path;
1359 bool created = false;
1360
1361 // Create a non-empty file.
1362 context.reset(NewContext(NULL));
1363 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491364 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471365 EXPECT_TRUE(created);
1366 context.reset(NewContext(NULL));
1367 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491368 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471369 context.reset(NewContext(NULL));
1370 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491371 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471372 context.get(), kPath1, &file_info, &data_path));
1373 EXPECT_EQ(10, file_info.size);
1374
1375 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491376 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471377
1378 // Try to get file info of broken file.
1379 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491380 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471381 context.reset(NewContext(NULL));
1382 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491383 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471384 EXPECT_TRUE(created);
1385 context.reset(NewContext(NULL));
1386 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491387 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471388 context.get(), kPath1, &file_info, &data_path));
1389 EXPECT_EQ(0, file_info.size);
1390
1391 // Make another broken file to |kPath2|.
1392 context.reset(NewContext(NULL));
1393 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491394 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471395 EXPECT_TRUE(created);
1396
1397 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491398 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471399
1400 // Repair broken |kPath1|.
1401 context.reset(NewContext(NULL));
1402 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491403 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471404 base::Time::Now()));
1405 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491406 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471407 EXPECT_TRUE(created);
1408
1409 // Copy from sound |kPath1| to broken |kPath2|.
1410 context.reset(NewContext(NULL));
1411 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491412 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]34583332011-08-31 08:59:471413 true /* copy */));
1414
[email protected]7878ece2011-09-05 11:41:491415 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471416 context.reset(NewContext(NULL));
1417 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491418 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471419 context.get(), kPath1,
1420 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1421 &file, &created));
1422 EXPECT_TRUE(created);
1423 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1424 EXPECT_EQ(0, file_info.size);
1425 EXPECT_TRUE(base::ClosePlatformFile(file));
1426}
[email protected]9dfdc0e32011-09-02 06:07:441427
[email protected]7878ece2011-09-05 11:41:491428TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]9dfdc0e32011-09-02 06:07:441429 const FilePath kPath[] = {
1430 FilePath().AppendASCII("foo"),
1431 FilePath().AppendASCII("bar"),
1432 FilePath().AppendASCII("baz")
1433 };
1434 scoped_ptr<FileSystemOperationContext> context;
1435
1436 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1437 bool created = false;
1438 context.reset(NewContext(NULL));
1439 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491440 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441441 EXPECT_TRUE(created);
1442 }
1443
1444 context.reset(NewContext(NULL));
1445 std::vector<base::FileUtilProxy::Entry> entries;
1446 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491447 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441448 EXPECT_EQ(3u, entries.size());
1449
1450 context.reset(NewContext(NULL));
1451 FilePath local_path;
1452 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491453 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441454 EXPECT_TRUE(file_util::Delete(local_path, false));
1455
1456 context.reset(NewContext(NULL));
1457 entries.clear();
1458 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491459 ofu()->ReadDirectory(context.get(), FilePath(), &entries));
[email protected]9dfdc0e32011-09-02 06:07:441460 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1461}
[email protected]fad625e2f2011-12-08 05:38:031462
1463TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1464 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1465 const FilePath dir_path(FILE_PATH_LITERAL("foo_dir"));
1466
1467 // Create working directory.
1468 EXPECT_EQ(base::PLATFORM_FILE_OK,
1469 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1470
1471 // EnsureFileExists, create case.
1472 FilePath path(dir_path.AppendASCII("EnsureFileExists_file"));
1473 bool created = false;
1474 ClearTimestamp(dir_path);
1475 context.reset(NewContext(NULL));
1476 EXPECT_EQ(base::PLATFORM_FILE_OK,
1477 ofu()->EnsureFileExists(context.get(), path, &created));
1478 EXPECT_TRUE(created);
1479 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1480
1481 // non create case.
1482 created = true;
1483 ClearTimestamp(dir_path);
1484 context.reset(NewContext(NULL));
1485 EXPECT_EQ(base::PLATFORM_FILE_OK,
1486 ofu()->EnsureFileExists(context.get(), path, &created));
1487 EXPECT_FALSE(created);
1488 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1489
1490 // fail case.
1491 path = dir_path.AppendASCII("EnsureFileExists_dir");
1492 context.reset(NewContext(NULL));
1493 EXPECT_EQ(base::PLATFORM_FILE_OK,
1494 ofu()->CreateDirectory(context.get(), path, false, false));
1495
1496 ClearTimestamp(dir_path);
1497 context.reset(NewContext(NULL));
1498 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
1499 ofu()->EnsureFileExists(context.get(), path, &created));
1500 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1501
1502 // CreateOrOpen, create case.
1503 path = dir_path.AppendASCII("CreateOrOpen_file");
1504 PlatformFile file_handle = base::kInvalidPlatformFileValue;
1505 created = false;
1506 ClearTimestamp(dir_path);
1507 context.reset(NewContext(NULL));
1508 EXPECT_EQ(base::PLATFORM_FILE_OK,
1509 ofu()->CreateOrOpen(
1510 context.get(), path,
1511 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1512 &file_handle, &created));
1513 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1514 EXPECT_TRUE(created);
1515 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1516 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1517
1518 // open case.
1519 file_handle = base::kInvalidPlatformFileValue;
1520 created = true;
1521 ClearTimestamp(dir_path);
1522 context.reset(NewContext(NULL));
1523 EXPECT_EQ(base::PLATFORM_FILE_OK,
1524 ofu()->CreateOrOpen(
1525 context.get(), path,
1526 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1527 &file_handle, &created));
1528 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1529 EXPECT_FALSE(created);
1530 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1531 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1532
1533 // fail case
1534 file_handle = base::kInvalidPlatformFileValue;
1535 ClearTimestamp(dir_path);
1536 context.reset(NewContext(NULL));
1537 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1538 ofu()->CreateOrOpen(
1539 context.get(), path,
1540 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1541 &file_handle, &created));
1542 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
1543 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1544
1545 // CreateDirectory, create case.
1546 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
1547 path = dir_path.AppendASCII("CreateDirectory_dir");
1548 FilePath subdir_path(path.AppendASCII("subdir"));
1549 ClearTimestamp(dir_path);
1550 context.reset(NewContext(NULL));
1551 EXPECT_EQ(base::PLATFORM_FILE_OK,
1552 ofu()->CreateDirectory(context.get(), subdir_path,
1553 true /* exclusive */, true /* recursive */));
1554 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1555
1556 // create subdir case.
1557 // Creating CreateDirectory_dir/subdir2.
1558 subdir_path = path.AppendASCII("subdir2");
1559 ClearTimestamp(dir_path);
1560 ClearTimestamp(path);
1561 context.reset(NewContext(NULL));
1562 EXPECT_EQ(base::PLATFORM_FILE_OK,
1563 ofu()->CreateDirectory(context.get(), subdir_path,
1564 true /* exclusive */, true /* recursive */));
1565 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1566 EXPECT_NE(base::Time(), GetModifiedTime(path));
1567
1568 // fail case.
1569 path = dir_path.AppendASCII("CreateDirectory_dir");
1570 ClearTimestamp(dir_path);
1571 context.reset(NewContext(NULL));
1572 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1573 ofu()->CreateDirectory(context.get(), path,
1574 true /* exclusive */, true /* recursive */));
1575 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1576
1577 // CopyInForeignFile, create case.
1578 path = dir_path.AppendASCII("CopyInForeignFile_file");
1579 FilePath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file");
1580 context.reset(NewContext(NULL));
1581 EXPECT_EQ(base::PLATFORM_FILE_OK,
1582 ofu()->EnsureFileExists(context.get(), src_path, &created));
1583 EXPECT_TRUE(created);
1584 FilePath src_local_path;
1585 context.reset(NewContext(NULL));
1586 EXPECT_EQ(base::PLATFORM_FILE_OK,
1587 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1588
1589 ClearTimestamp(dir_path);
1590 context.reset(NewContext(NULL));
1591 EXPECT_EQ(base::PLATFORM_FILE_OK,
1592 ofu()->CopyInForeignFile(context.get(), src_local_path, path));
1593 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1594}
1595
1596TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1597 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1598 const FilePath dir_path(FILE_PATH_LITERAL("foo_dir"));
1599
1600 // Create working directory.
1601 EXPECT_EQ(base::PLATFORM_FILE_OK,
1602 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1603
1604 // DeleteFile, delete case.
1605 FilePath path = dir_path.AppendASCII("DeleteFile_file");
1606 bool created = false;
1607 context.reset(NewContext(NULL));
1608 EXPECT_EQ(base::PLATFORM_FILE_OK,
1609 ofu()->EnsureFileExists(context.get(), path, &created));
1610 EXPECT_TRUE(created);
1611
1612 ClearTimestamp(dir_path);
1613 context.reset(NewContext(NULL));
1614 EXPECT_EQ(base::PLATFORM_FILE_OK,
1615 ofu()->DeleteFile(context.get(), path));
1616 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1617
1618 // fail case.
1619 ClearTimestamp(dir_path);
1620 context.reset(NewContext(NULL));
1621 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
1622 ofu()->DeleteFile(context.get(), path));
1623 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1624
1625 // DeleteSingleDirectory, fail case.
1626 path = dir_path.AppendASCII("DeleteSingleDirectory_dir");
1627 FilePath file_path(path.AppendASCII("pakeratta"));
1628 context.reset(NewContext(NULL));
1629 EXPECT_EQ(base::PLATFORM_FILE_OK,
1630 ofu()->CreateDirectory(context.get(), path, true, true));
1631 created = false;
1632 context.reset(NewContext(NULL));
1633 EXPECT_EQ(base::PLATFORM_FILE_OK,
1634 ofu()->EnsureFileExists(context.get(), file_path, &created));
1635 EXPECT_TRUE(created);
1636
1637 ClearTimestamp(dir_path);
1638 context.reset(NewContext(NULL));
1639 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
1640 ofu()->DeleteSingleDirectory(context.get(), path));
1641 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1642
1643 // delete case.
1644 context.reset(NewContext(NULL));
1645 EXPECT_EQ(base::PLATFORM_FILE_OK,
1646 ofu()->DeleteFile(context.get(), file_path));
1647
1648 ClearTimestamp(dir_path);
1649 context.reset(NewContext(NULL));
1650 EXPECT_EQ(base::PLATFORM_FILE_OK,
1651 ofu()->DeleteSingleDirectory(context.get(), path));
1652 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1653}
1654
1655TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1656 TestDirectoryTimestampHelper(
1657 FilePath(FILE_PATH_LITERAL("copy overwrite")), true, true);
1658 TestDirectoryTimestampHelper(
1659 FilePath(FILE_PATH_LITERAL("copy non-overwrite")), true, false);
1660 TestDirectoryTimestampHelper(
1661 FilePath(FILE_PATH_LITERAL("move overwrite")), false, true);
1662 TestDirectoryTimestampHelper(
1663 FilePath(FILE_PATH_LITERAL("move non-overwrite")), false, false);
1664}