blob: beb0f198f9c0dd2fc740272ecf9def27bf3a1181 [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]aa437fa2012-03-03 02:09:0722#include "webkit/fileapi/file_util_helper.h"
[email protected]e7e46732012-01-05 11:45:5523#include "webkit/fileapi/mock_file_system_options.h"
[email protected]7878ece2011-09-05 11:41:4924#include "webkit/fileapi/obfuscated_file_util.h"
[email protected]f83b5b72012-01-27 10:26:5625#include "webkit/fileapi/test_file_set.h"
[email protected]0c5ebe32011-08-19 22:37:1626#include "webkit/quota/mock_special_storage_policy.h"
27#include "webkit/quota/quota_manager.h"
28#include "webkit/quota/quota_types.h"
[email protected]d4905e2e2011-05-13 21:56:3229
30using namespace fileapi;
31
32namespace {
33
[email protected]d4905e2e2011-05-13 21:56:3234bool FileExists(const FilePath& path) {
35 return file_util::PathExists(path) && !file_util::DirectoryExists(path);
36}
37
[email protected]81b7f662011-05-26 00:54:4638int64 GetSize(const FilePath& path) {
39 int64 size;
40 EXPECT_TRUE(file_util::GetFileSize(path, &size));
41 return size;
42}
43
[email protected]d4905e2e2011-05-13 21:56:3244// After a move, the dest exists and the source doesn't.
45// After a copy, both source and dest exist.
46struct CopyMoveTestCaseRecord {
47 bool is_copy_not_move;
48 const char source_path[64];
49 const char dest_path[64];
50 bool cause_overwrite;
51};
52
53const CopyMoveTestCaseRecord kCopyMoveTestCases[] = {
54 // This is the combinatoric set of:
55 // rename vs. same-name
56 // different directory vs. same directory
57 // overwrite vs. no-overwrite
58 // copy vs. move
59 // We can never be called with source and destination paths identical, so
60 // those cases are omitted.
61 {true, "dir0/file0", "dir0/file1", false},
62 {false, "dir0/file0", "dir0/file1", false},
63 {true, "dir0/file0", "dir0/file1", true},
64 {false, "dir0/file0", "dir0/file1", true},
65
66 {true, "dir0/file0", "dir1/file0", false},
67 {false, "dir0/file0", "dir1/file0", false},
68 {true, "dir0/file0", "dir1/file0", true},
69 {false, "dir0/file0", "dir1/file0", true},
70 {true, "dir0/file0", "dir1/file1", false},
71 {false, "dir0/file0", "dir1/file1", false},
72 {true, "dir0/file0", "dir1/file1", true},
73 {false, "dir0/file0", "dir1/file1", true},
74};
75
[email protected]fcc2d5f2011-05-23 22:06:2676struct OriginEnumerationTestRecord {
77 std::string origin_url;
78 bool has_temporary;
79 bool has_persistent;
80};
81
82const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
83 {"http://example.com", false, true},
84 {"http://example1.com", true, false},
85 {"https://example1.com", true, true},
86 {"file://", false, true},
87 {"http://example.com:8000", false, true},
88};
89
[email protected]d4905e2e2011-05-13 21:56:3290} // namespace (anonymous)
91
92// TODO(ericu): The vast majority of this and the other FSFU subclass tests
93// could theoretically be shared. It would basically be a FSFU interface
94// compliance test, and only the subclass-specific bits that look into the
95// implementation would need to be written per-subclass.
[email protected]7878ece2011-09-05 11:41:4996class ObfuscatedFileUtilTest : public testing::Test {
[email protected]d4905e2e2011-05-13 21:56:3297 public:
[email protected]7878ece2011-09-05 11:41:4998 ObfuscatedFileUtilTest()
[email protected]fcc2d5f2011-05-23 22:06:2699 : origin_(GURL("http://www.example.com")),
100 type_(kFileSystemTypeTemporary),
[email protected]4d99be52011-10-18 14:11:03101 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]0c5ebe32011-08-19 22:37:16102 test_helper_(origin_, type_),
103 quota_status_(quota::kQuotaStatusUnknown),
104 usage_(-1) {
[email protected]d4905e2e2011-05-13 21:56:32105 }
106
107 void SetUp() {
108 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
109
[email protected]e7e46732012-01-05 11:45:55110 scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
111 new quota::MockSpecialStoragePolicy();
112
[email protected]0c5ebe32011-08-19 22:37:16113 quota_manager_ = new quota::QuotaManager(
114 false /* is_incognito */,
115 data_dir_.path(),
116 base::MessageLoopProxy::current(),
117 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55118 storage_policy);
[email protected]0c5ebe32011-08-19 22:37:16119
120 // Every time we create a new helper, it creates another context, which
121 // creates another path manager, another sandbox_mount_point_provider, and
[email protected]7878ece2011-09-05 11:41:49122 // another OFU. We need to pass in the context to skip all that.
[email protected]0c5ebe32011-08-19 22:37:16123 file_system_context_ = new FileSystemContext(
124 base::MessageLoopProxy::current(),
125 base::MessageLoopProxy::current(),
[email protected]e7e46732012-01-05 11:45:55126 storage_policy,
[email protected]0c5ebe32011-08-19 22:37:16127 quota_manager_->proxy(),
128 data_dir_.path(),
[email protected]e7e46732012-01-05 11:45:55129 CreateAllowFileAccessOptions());
[email protected]0c5ebe32011-08-19 22:37:16130
[email protected]7878ece2011-09-05 11:41:49131 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>(
[email protected]e7e46732012-01-05 11:45:55132 file_system_context_->GetFileUtil(type_));
[email protected]0c5ebe32011-08-19 22:37:16133
134 test_helper_.SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49135 obfuscated_file_util_.get());
[email protected]d4905e2e2011-05-13 21:56:32136 }
137
[email protected]e7e46732012-01-05 11:45:55138 void TearDown() {
139 quota_manager_ = NULL;
140 test_helper_.TearDown();
141 }
142
[email protected]294dd0312012-05-11 07:35:13143 scoped_ptr<FileSystemOperationContext> LimitedContext(
144 int64 allowed_bytes_growth) {
145 scoped_ptr<FileSystemOperationContext> context(
146 test_helper_.NewOperationContext());
147 context->set_allowed_bytes_growth(allowed_bytes_growth);
148 return context.Pass();
149 }
150
151 scoped_ptr<FileSystemOperationContext> UnlimitedContext() {
152 return LimitedContext(kint64max);
153 }
154
[email protected]0c5ebe32011-08-19 22:37:16155 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
156 FileSystemOperationContext* context;
157 if (helper)
158 context = helper->NewOperationContext();
159 else
160 context = test_helper_.NewOperationContext();
161 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
[email protected]d4905e2e2011-05-13 21:56:32162 return context;
163 }
164
[email protected]0c5ebe32011-08-19 22:37:16165 // This can only be used after SetUp has run and created file_system_context_
[email protected]7878ece2011-09-05 11:41:49166 // and obfuscated_file_util_.
[email protected]0c5ebe32011-08-19 22:37:16167 // Use this for tests which need to run in multiple origins; we need a test
168 // helper per origin.
169 FileSystemTestOriginHelper* NewHelper(
170 const GURL& origin, fileapi::FileSystemType type) {
171 FileSystemTestOriginHelper* helper =
172 new FileSystemTestOriginHelper(origin, type);
173
174 helper->SetUp(file_system_context_.get(),
[email protected]7878ece2011-09-05 11:41:49175 obfuscated_file_util_.get());
[email protected]0c5ebe32011-08-19 22:37:16176 return helper;
177 }
178
[email protected]7878ece2011-09-05 11:41:49179 ObfuscatedFileUtil* ofu() {
180 return obfuscated_file_util_.get();
[email protected]d4905e2e2011-05-13 21:56:32181 }
182
[email protected]6b931152011-05-20 21:02:35183 const FilePath& test_directory() const {
184 return data_dir_.path();
185 }
186
[email protected]0c5ebe32011-08-19 22:37:16187 const GURL& origin() const {
[email protected]fcc2d5f2011-05-23 22:06:26188 return origin_;
189 }
190
191 fileapi::FileSystemType type() const {
192 return type_;
193 }
194
[email protected]294dd0312012-05-11 07:35:13195 int64 ComputeTotalFileSize() {
196 return test_helper_.ComputeCurrentOriginUsage() -
197 test_helper_.ComputeCurrentDirectoryDatabaseUsage();
198 }
199
[email protected]0c5ebe32011-08-19 22:37:16200 void GetUsageFromQuotaManager() {
201 quota_manager_->GetUsageAndQuota(
202 origin(), test_helper_.storage_type(),
[email protected]4d99be52011-10-18 14:11:03203 base::Bind(&ObfuscatedFileUtilTest::OnGetUsage,
204 weak_factory_.GetWeakPtr()));
[email protected]0c5ebe32011-08-19 22:37:16205 MessageLoop::current()->RunAllPending();
206 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
207 }
208
209 void RevokeUsageCache() {
210 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
211 ASSERT_TRUE(test_helper_.RevokeUsageCache());
212 }
213
214 int64 SizeInUsageFile() {
215 return test_helper_.GetCachedOriginUsage();
216 }
217
218 int64 usage() const { return usage_; }
219
[email protected]08f8feb2012-02-26 11:53:50220 FileSystemPath CreatePathFromUTF8(const std::string& path) {
221 return test_helper_.CreatePathFromUTF8(path);
222 }
223
[email protected]294dd0312012-05-11 07:35:13224 int64 PathCost(const FileSystemPath& path) {
225 return ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path());
226 }
227
[email protected]08f8feb2012-02-26 11:53:50228 FileSystemPath CreatePath(const FilePath& path) {
229 return test_helper_.CreatePath(path);
230 }
231
[email protected]0c5ebe32011-08-19 22:37:16232 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
233 EXPECT_EQ(quota::kQuotaStatusOk, status);
234 quota_status_ = status;
235 usage_ = usage;
236 }
237
[email protected]d4905e2e2011-05-13 21:56:32238 void CheckFileAndCloseHandle(
[email protected]08f8feb2012-02-26 11:53:50239 const FileSystemPath& virtual_path, PlatformFile file_handle) {
[email protected]0c5ebe32011-08-19 22:37:16240 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32241 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49242 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32243 context.get(), virtual_path, &local_path));
244
245 base::PlatformFileInfo file_info0;
246 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49247 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32248 context.get(), virtual_path, &file_info0, &data_path));
249 EXPECT_EQ(data_path, local_path);
250 EXPECT_TRUE(FileExists(data_path));
251 EXPECT_EQ(0, GetSize(data_path));
252
253 const char data[] = "test data";
254 const int length = arraysize(data) - 1;
255
256 if (base::kInvalidPlatformFileValue == file_handle) {
257 bool created = true;
258 PlatformFileError error;
259 file_handle = base::CreatePlatformFile(
260 data_path,
261 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
262 &created,
263 &error);
[email protected]81b7f662011-05-26 00:54:46264 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
[email protected]d4905e2e2011-05-13 21:56:32265 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
266 EXPECT_FALSE(created);
267 }
268 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
269 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
270
271 base::PlatformFileInfo file_info1;
272 EXPECT_EQ(length, GetSize(data_path));
[email protected]0c5ebe32011-08-19 22:37:16273 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49274 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32275 context.get(), virtual_path, &file_info1, &data_path));
276 EXPECT_EQ(data_path, local_path);
277
278 EXPECT_FALSE(file_info0.is_directory);
279 EXPECT_FALSE(file_info1.is_directory);
280 EXPECT_FALSE(file_info0.is_symbolic_link);
281 EXPECT_FALSE(file_info1.is_symbolic_link);
282 EXPECT_EQ(0, file_info0.size);
283 EXPECT_EQ(length, file_info1.size);
[email protected]d4905e2e2011-05-13 21:56:32284 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
[email protected]d4905e2e2011-05-13 21:56:32285
[email protected]0c5ebe32011-08-19 22:37:16286 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49287 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32288 context.get(), virtual_path, length * 2));
289 EXPECT_EQ(length * 2, GetSize(data_path));
290
[email protected]0c5ebe32011-08-19 22:37:16291 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49292 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]0c5ebe32011-08-19 22:37:16293 context.get(), virtual_path, 0));
294 EXPECT_EQ(0, GetSize(data_path));
[email protected]d4905e2e2011-05-13 21:56:32295 }
296
297 void ValidateTestDirectory(
[email protected]08f8feb2012-02-26 11:53:50298 const FileSystemPath& root_path,
[email protected]89ee4272011-05-16 18:45:17299 const std::set<FilePath::StringType>& files,
300 const std::set<FilePath::StringType>& directories) {
[email protected]d4905e2e2011-05-13 21:56:32301 scoped_ptr<FileSystemOperationContext> context;
[email protected]89ee4272011-05-16 18:45:17302 std::set<FilePath::StringType>::const_iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32303 for (iter = files.begin(); iter != files.end(); ++iter) {
304 bool created = true;
[email protected]0c5ebe32011-08-19 22:37:16305 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32306 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49307 ofu()->EnsureFileExists(
[email protected]89ee4272011-05-16 18:45:17308 context.get(), root_path.Append(*iter),
[email protected]d4905e2e2011-05-13 21:56:32309 &created));
310 ASSERT_FALSE(created);
311 }
312 for (iter = directories.begin(); iter != directories.end(); ++iter) {
[email protected]0c5ebe32011-08-19 22:37:16313 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49314 EXPECT_TRUE(ofu()->DirectoryExists(context.get(),
[email protected]89ee4272011-05-16 18:45:17315 root_path.Append(*iter)));
[email protected]d4905e2e2011-05-13 21:56:32316 }
317 }
318
[email protected]294dd0312012-05-11 07:35:13319 class UsageVerifyHelper {
320 public:
321 UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context,
322 FileSystemTestOriginHelper* test_helper,
323 int64 expected_usage)
324 : context_(context.Pass()),
325 test_helper_(test_helper),
326 expected_usage_(expected_usage) {}
327
328 ~UsageVerifyHelper() {
329 Check();
330 }
331
332 FileSystemOperationContext* context() {
333 return context_.get();
334 }
335
336 private:
337 void Check() {
338 ASSERT_EQ(expected_usage_,
339 test_helper_->GetCachedOriginUsage());
340 }
341
342 scoped_ptr<FileSystemOperationContext> context_;
343 FileSystemTestOriginHelper* test_helper_;
344 int64 growth_;
345 int64 expected_usage_;
346 };
347
348 scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) {
349 int64 usage = test_helper_.GetCachedOriginUsage();
350 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
351 LimitedContext(requested_growth),
352 &test_helper_, usage + requested_growth));
353 }
354
355 scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) {
356 int64 usage = test_helper_.GetCachedOriginUsage();
357 return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper(
358 LimitedContext(requested_growth - 1), &test_helper_, usage));
359 }
360
[email protected]d4905e2e2011-05-13 21:56:32361 void FillTestDirectory(
[email protected]08f8feb2012-02-26 11:53:50362 const FileSystemPath& root_path,
[email protected]89ee4272011-05-16 18:45:17363 std::set<FilePath::StringType>* files,
364 std::set<FilePath::StringType>* directories) {
[email protected]d4905e2e2011-05-13 21:56:32365 scoped_ptr<FileSystemOperationContext> context;
[email protected]0c5ebe32011-08-19 22:37:16366 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32367 std::vector<base::FileUtilProxy::Entry> entries;
368 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55369 FileUtilHelper::ReadDirectory(
370 context.get(), ofu(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32371 EXPECT_EQ(0UL, entries.size());
372
373 files->clear();
[email protected]89ee4272011-05-16 18:45:17374 files->insert(FILE_PATH_LITERAL("first"));
375 files->insert(FILE_PATH_LITERAL("second"));
376 files->insert(FILE_PATH_LITERAL("third"));
[email protected]d4905e2e2011-05-13 21:56:32377 directories->clear();
[email protected]89ee4272011-05-16 18:45:17378 directories->insert(FILE_PATH_LITERAL("fourth"));
379 directories->insert(FILE_PATH_LITERAL("fifth"));
380 directories->insert(FILE_PATH_LITERAL("sixth"));
381 std::set<FilePath::StringType>::iterator iter;
[email protected]d4905e2e2011-05-13 21:56:32382 for (iter = files->begin(); iter != files->end(); ++iter) {
383 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16384 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32385 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49386 ofu()->EnsureFileExists(
[email protected]08f8feb2012-02-26 11:53:50387 context.get(),
388 root_path.Append(*iter),
389 &created));
[email protected]d4905e2e2011-05-13 21:56:32390 ASSERT_TRUE(created);
391 }
392 for (iter = directories->begin(); iter != directories->end(); ++iter) {
393 bool exclusive = true;
394 bool recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16395 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32396 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49397 ofu()->CreateDirectory(
[email protected]89ee4272011-05-16 18:45:17398 context.get(), root_path.Append(*iter), exclusive, recursive));
[email protected]d4905e2e2011-05-13 21:56:32399 }
400 ValidateTestDirectory(root_path, *files, *directories);
401 }
402
[email protected]08f8feb2012-02-26 11:53:50403 void TestReadDirectoryHelper(const FileSystemPath& root_path) {
[email protected]89ee4272011-05-16 18:45:17404 std::set<FilePath::StringType> files;
405 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:32406 FillTestDirectory(root_path, &files, &directories);
407
408 scoped_ptr<FileSystemOperationContext> context;
409 std::vector<base::FileUtilProxy::Entry> entries;
[email protected]0c5ebe32011-08-19 22:37:16410 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32411 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:55412 FileUtilHelper::ReadDirectory(
413 context.get(), ofu(), root_path, &entries));
[email protected]d4905e2e2011-05-13 21:56:32414 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
415 EXPECT_EQ(files.size() + directories.size(), entries.size());
416 for (entry_iter = entries.begin(); entry_iter != entries.end();
417 ++entry_iter) {
418 const base::FileUtilProxy::Entry& entry = *entry_iter;
[email protected]89ee4272011-05-16 18:45:17419 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32420 if (iter != files.end()) {
421 EXPECT_FALSE(entry.is_directory);
422 files.erase(iter);
423 continue;
424 }
[email protected]89ee4272011-05-16 18:45:17425 iter = directories.find(entry.name);
[email protected]d4905e2e2011-05-13 21:56:32426 EXPECT_FALSE(directories.end() == iter);
427 EXPECT_TRUE(entry.is_directory);
428 directories.erase(iter);
429 }
430 }
431
[email protected]08f8feb2012-02-26 11:53:50432 void TestTouchHelper(const FileSystemPath& path, bool is_file) {
[email protected]2517cfa2011-08-25 05:12:41433 base::Time last_access_time = base::Time::Now();
[email protected]d4905e2e2011-05-13 21:56:32434 base::Time last_modified_time = base::Time::Now();
[email protected]0c5ebe32011-08-19 22:37:16435
[email protected]2517cfa2011-08-25 05:12:41436 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32437 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49438 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32439 context.get(), path, last_access_time, last_modified_time));
440 FilePath local_path;
441 base::PlatformFileInfo file_info;
[email protected]0c5ebe32011-08-19 22:37:16442 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49443 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32444 context.get(), path, &file_info, &local_path));
445 // We compare as time_t here to lower our resolution, to avoid false
446 // negatives caused by conversion to the local filesystem's native
447 // representation and back.
448 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
449
[email protected]0c5ebe32011-08-19 22:37:16450 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32451 last_modified_time += base::TimeDelta::FromHours(1);
[email protected]2517cfa2011-08-25 05:12:41452 last_access_time += base::TimeDelta::FromHours(14);
[email protected]d4905e2e2011-05-13 21:56:32453 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49454 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:32455 context.get(), path, last_access_time, last_modified_time));
[email protected]0c5ebe32011-08-19 22:37:16456 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49457 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32458 context.get(), path, &file_info, &local_path));
459 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
[email protected]7878ece2011-09-05 11:41:49460 if (is_file) // Directories in OFU don't support atime.
[email protected]2517cfa2011-08-25 05:12:41461 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT());
[email protected]d4905e2e2011-05-13 21:56:32462 }
463
[email protected]81b7f662011-05-26 00:54:46464 void TestCopyInForeignFileHelper(bool overwrite) {
465 ScopedTempDir source_dir;
466 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
[email protected]08f8feb2012-02-26 11:53:50467 FilePath root_file_path = source_dir.path();
468 FilePath src_file_path = root_file_path.AppendASCII("file_name");
469 FileSystemPath dest_path = CreatePathFromUTF8("new file");
[email protected]81b7f662011-05-26 00:54:46470 int64 src_file_length = 87;
471
472 base::PlatformFileError error_code;
473 bool created = false;
474 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
475 base::PlatformFile file_handle =
476 base::CreatePlatformFile(
[email protected]08f8feb2012-02-26 11:53:50477 src_file_path, file_flags, &created, &error_code);
[email protected]81b7f662011-05-26 00:54:46478 EXPECT_TRUE(created);
479 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
480 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
481 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
482 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
483
484 scoped_ptr<FileSystemOperationContext> context;
485
486 if (overwrite) {
[email protected]0c5ebe32011-08-19 22:37:16487 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46488 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49489 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]81b7f662011-05-26 00:54:46490 EXPECT_TRUE(created);
491 }
492
[email protected]0c5ebe32011-08-19 22:37:16493 const int64 path_cost =
[email protected]08f8feb2012-02-26 11:53:50494 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path());
[email protected]0c5ebe32011-08-19 22:37:16495 if (!overwrite) {
496 // Verify that file creation requires sufficient quota for the path.
497 context.reset(NewContext(NULL));
498 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
499 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13500 ofu()->CopyInForeignFile(context.get(),
501 src_file_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16502 }
503
504 context.reset(NewContext(NULL));
505 context->set_allowed_bytes_growth(path_cost + src_file_length);
[email protected]81b7f662011-05-26 00:54:46506 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13507 ofu()->CopyInForeignFile(context.get(),
508 src_file_path, dest_path));
[email protected]0c5ebe32011-08-19 22:37:16509
510 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49511 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16512 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49513 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:16514 context.reset(NewContext(NULL));
[email protected]81b7f662011-05-26 00:54:46515 base::PlatformFileInfo file_info;
516 FilePath data_path;
[email protected]7878ece2011-09-05 11:41:49517 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]81b7f662011-05-26 00:54:46518 context.get(), dest_path, &file_info, &data_path));
[email protected]08f8feb2012-02-26 11:53:50519 EXPECT_NE(data_path, src_file_path);
[email protected]81b7f662011-05-26 00:54:46520 EXPECT_TRUE(FileExists(data_path));
521 EXPECT_EQ(src_file_length, GetSize(data_path));
522
523 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49524 ofu()->DeleteFile(context.get(), dest_path));
[email protected]81b7f662011-05-26 00:54:46525 }
526
[email protected]08f8feb2012-02-26 11:53:50527 void ClearTimestamp(const FileSystemPath& path) {
[email protected]fad625e2f2011-12-08 05:38:03528 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
529 EXPECT_EQ(base::PLATFORM_FILE_OK,
530 ofu()->Touch(context.get(), path, base::Time(), base::Time()));
531 EXPECT_EQ(base::Time(), GetModifiedTime(path));
532 }
533
[email protected]08f8feb2012-02-26 11:53:50534 base::Time GetModifiedTime(const FileSystemPath& path) {
[email protected]fad625e2f2011-12-08 05:38:03535 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
536 FilePath data_path;
537 base::PlatformFileInfo file_info;
538 context.reset(NewContext(NULL));
539 EXPECT_EQ(base::PLATFORM_FILE_OK,
540 ofu()->GetFileInfo(context.get(), path, &file_info, &data_path));
541 return file_info.last_modified;
542 }
543
[email protected]08f8feb2012-02-26 11:53:50544 void TestDirectoryTimestampHelper(const FileSystemPath& base_dir,
[email protected]fad625e2f2011-12-08 05:38:03545 bool copy,
546 bool overwrite) {
547 scoped_ptr<FileSystemOperationContext> context;
[email protected]08f8feb2012-02-26 11:53:50548 const FileSystemPath src_dir_path(base_dir.AppendASCII("foo_dir"));
549 const FileSystemPath dest_dir_path(base_dir.AppendASCII("bar_dir"));
[email protected]fad625e2f2011-12-08 05:38:03550
[email protected]08f8feb2012-02-26 11:53:50551 const FileSystemPath src_file_path(src_dir_path.AppendASCII("hoge"));
552 const FileSystemPath dest_file_path(dest_dir_path.AppendASCII("fuga"));
[email protected]fad625e2f2011-12-08 05:38:03553
554 context.reset(NewContext(NULL));
555 EXPECT_EQ(base::PLATFORM_FILE_OK,
556 ofu()->CreateDirectory(context.get(), src_dir_path, true, true));
557 context.reset(NewContext(NULL));
558 EXPECT_EQ(base::PLATFORM_FILE_OK,
559 ofu()->CreateDirectory(context.get(), dest_dir_path, true, true));
560
561 bool created = false;
562 context.reset(NewContext(NULL));
563 EXPECT_EQ(base::PLATFORM_FILE_OK,
564 ofu()->EnsureFileExists(context.get(), src_file_path, &created));
565 if (overwrite) {
566 context.reset(NewContext(NULL));
567 EXPECT_EQ(base::PLATFORM_FILE_OK,
568 ofu()->EnsureFileExists(context.get(),
569 dest_file_path, &created));
570 }
571
572 ClearTimestamp(src_dir_path);
573 ClearTimestamp(dest_dir_path);
574 context.reset(NewContext(NULL));
575 EXPECT_EQ(base::PLATFORM_FILE_OK,
576 ofu()->CopyOrMoveFile(context.get(),
577 src_file_path, dest_file_path,
578 copy));
579
580 if (copy)
581 EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path));
582 else
583 EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path));
584 EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path));
585 }
586
[email protected]ecdfd6c52012-04-11 13:35:44587 int64 ComputeCurrentUsage() {
588 return test_helper().ComputeCurrentOriginUsage() -
589 test_helper().ComputeCurrentDirectoryDatabaseUsage();
590 }
591
[email protected]45ea0fbbb2012-02-27 22:28:49592 const FileSystemTestOriginHelper& test_helper() const { return test_helper_; }
593
[email protected]d4905e2e2011-05-13 21:56:32594 private:
595 ScopedTempDir data_dir_;
[email protected]7878ece2011-09-05 11:41:49596 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_;
[email protected]0c5ebe32011-08-19 22:37:16597 scoped_refptr<quota::QuotaManager> quota_manager_;
598 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]fcc2d5f2011-05-23 22:06:26599 GURL origin_;
600 fileapi::FileSystemType type_;
[email protected]4d99be52011-10-18 14:11:03601 base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_;
[email protected]fcc2d5f2011-05-23 22:06:26602 FileSystemTestOriginHelper test_helper_;
[email protected]0c5ebe32011-08-19 22:37:16603 quota::QuotaStatusCode quota_status_;
604 int64 usage_;
[email protected]d4905e2e2011-05-13 21:56:32605
[email protected]7878ece2011-09-05 11:41:49606 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest);
[email protected]d4905e2e2011-05-13 21:56:32607};
608
[email protected]7878ece2011-09-05 11:41:49609TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) {
[email protected]d4905e2e2011-05-13 21:56:32610 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
611 bool created;
[email protected]08f8feb2012-02-26 11:53:50612 FileSystemPath path = CreatePathFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:16613 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32614 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
615
616 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49617 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32618 context.get(), path, file_flags, &file_handle,
619 &created));
620
[email protected]0c5ebe32011-08-19 22:37:16621 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32622 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49623 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32624
[email protected]08f8feb2012-02-26 11:53:50625 path = CreatePathFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32626
[email protected]0c5ebe32011-08-19 22:37:16627 // Verify that file creation requires sufficient quota for the path.
628 context.reset(NewContext(NULL));
629 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50630 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16631 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49632 ofu()->CreateOrOpen(
[email protected]0c5ebe32011-08-19 22:37:16633 context.get(), path, file_flags, &file_handle, &created));
634
635 context.reset(NewContext(NULL));
636 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50637 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()));
[email protected]d4905e2e2011-05-13 21:56:32638 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49639 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32640 context.get(), path, file_flags, &file_handle, &created));
641 ASSERT_TRUE(created);
642 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
643
644 CheckFileAndCloseHandle(path, file_handle);
645
[email protected]0c5ebe32011-08-19 22:37:16646 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32647 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49648 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32649 context.get(), path, &local_path));
650 EXPECT_TRUE(file_util::PathExists(local_path));
651
[email protected]0c5ebe32011-08-19 22:37:16652 // Verify that deleting a file isn't stopped by zero quota, and that it frees
653 // up quote from its path.
654 context.reset(NewContext(NULL));
655 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32656 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49657 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32658 EXPECT_FALSE(file_util::PathExists(local_path));
[email protected]08f8feb2012-02-26 11:53:50659 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()),
[email protected]0c5ebe32011-08-19 22:37:16660 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32661
[email protected]0c5ebe32011-08-19 22:37:16662 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32663 bool exclusive = true;
664 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:50665 FileSystemPath directory_path = CreatePathFromUTF8(
666 "series/of/directories");
[email protected]d4905e2e2011-05-13 21:56:32667 path = directory_path.AppendASCII("file name");
[email protected]7878ece2011-09-05 11:41:49668 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32669 context.get(), directory_path, exclusive, recursive));
670
[email protected]0c5ebe32011-08-19 22:37:16671 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32672 file_handle = base::kInvalidPlatformFileValue;
673 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49674 ofu()->CreateOrOpen(
[email protected]d4905e2e2011-05-13 21:56:32675 context.get(), path, file_flags, &file_handle, &created));
676 ASSERT_TRUE(created);
677 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
678
679 CheckFileAndCloseHandle(path, file_handle);
680
[email protected]0c5ebe32011-08-19 22:37:16681 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49682 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32683 context.get(), path, &local_path));
684 EXPECT_TRUE(file_util::PathExists(local_path));
685
[email protected]0c5ebe32011-08-19 22:37:16686 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32687 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49688 ofu()->DeleteFile(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32689 EXPECT_FALSE(file_util::PathExists(local_path));
690}
691
[email protected]7878ece2011-09-05 11:41:49692TEST_F(ObfuscatedFileUtilTest, TestTruncate) {
[email protected]d4905e2e2011-05-13 21:56:32693 bool created = false;
[email protected]08f8feb2012-02-26 11:53:50694 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:16695 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32696
697 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49698 ofu()->Truncate(context.get(), path, 4));
[email protected]d4905e2e2011-05-13 21:56:32699
[email protected]0c5ebe32011-08-19 22:37:16700 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32701 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49702 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32703 ASSERT_TRUE(created);
704
[email protected]0c5ebe32011-08-19 22:37:16705 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32706 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49707 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(
[email protected]d4905e2e2011-05-13 21:56:32708 context.get(), path, &local_path));
709 EXPECT_EQ(0, GetSize(local_path));
710
[email protected]0c5ebe32011-08-19 22:37:16711 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49712 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32713 context.get(), path, 10));
714 EXPECT_EQ(10, GetSize(local_path));
715
[email protected]0c5ebe32011-08-19 22:37:16716 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49717 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate(
[email protected]d4905e2e2011-05-13 21:56:32718 context.get(), path, 1));
719 EXPECT_EQ(1, GetSize(local_path));
720
[email protected]0c5ebe32011-08-19 22:37:16721 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49722 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16723 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49724 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32725}
726
[email protected]ecdfd6c52012-04-11 13:35:44727TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) {
728 bool created = false;
729 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]ecdfd6c52012-04-11 13:35:44730
731 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13732 ofu()->EnsureFileExists(
733 AllowUsageIncrease(PathCost(path))->context(),
734 path, &created));
[email protected]ecdfd6c52012-04-11 13:35:44735 ASSERT_TRUE(created);
[email protected]294dd0312012-05-11 07:35:13736 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44737
[email protected]ecdfd6c52012-04-11 13:35:44738 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13739 ofu()->Truncate(
740 AllowUsageIncrease(1020)->context(),
741 path, 1020));
742 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44743
[email protected]ecdfd6c52012-04-11 13:35:44744 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13745 ofu()->Truncate(
746 AllowUsageIncrease(-1020)->context(),
747 path, 0));
748 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44749
[email protected]ecdfd6c52012-04-11 13:35:44750 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]294dd0312012-05-11 07:35:13751 ofu()->Truncate(
752 DisallowUsageIncrease(1021)->context(),
753 path, 1021));
754 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44755
[email protected]ecdfd6c52012-04-11 13:35:44756 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13757 ofu()->Truncate(
758 AllowUsageIncrease(1020)->context(),
759 path, 1020));
760 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44761
[email protected]ecdfd6c52012-04-11 13:35:44762 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13763 ofu()->Truncate(
764 AllowUsageIncrease(0)->context(),
765 path, 1020));
766 ASSERT_EQ(1020, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44767
[email protected]294dd0312012-05-11 07:35:13768 // quota exceeded
769 {
770 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1);
771 helper->context()->set_allowed_bytes_growth(
772 helper->context()->allowed_bytes_growth() - 1);
773 EXPECT_EQ(base::PLATFORM_FILE_OK,
774 ofu()->Truncate(helper->context(), path, 1019));
775 ASSERT_EQ(1019, ComputeTotalFileSize());
776 }
[email protected]ecdfd6c52012-04-11 13:35:44777
778 // Delete backing file to make following truncation fail.
[email protected]ecdfd6c52012-04-11 13:35:44779 FilePath local_path;
780 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]294dd0312012-05-11 07:35:13781 ofu()->GetLocalFilePath(
782 UnlimitedContext().get(),
783 path, &local_path));
[email protected]ecdfd6c52012-04-11 13:35:44784 ASSERT_FALSE(local_path.empty());
785 ASSERT_TRUE(file_util::Delete(local_path, false));
786
[email protected]ecdfd6c52012-04-11 13:35:44787 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]294dd0312012-05-11 07:35:13788 ofu()->Truncate(
789 LimitedContext(1234).get(),
790 path, 1234));
791 ASSERT_EQ(0, ComputeTotalFileSize());
[email protected]ecdfd6c52012-04-11 13:35:44792}
793
[email protected]7878ece2011-09-05 11:41:49794TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) {
[email protected]08f8feb2012-02-26 11:53:50795 FileSystemPath path = CreatePathFromUTF8("fake/file");
[email protected]d4905e2e2011-05-13 21:56:32796 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:16797 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32798 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49799 ofu()->EnsureFileExists(
[email protected]d4905e2e2011-05-13 21:56:32800 context.get(), path, &created));
801
[email protected]0c5ebe32011-08-19 22:37:16802 // Verify that file creation requires sufficient quota for the path.
803 context.reset(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:50804 path = CreatePathFromUTF8("test file");
[email protected]d4905e2e2011-05-13 21:56:32805 created = false;
[email protected]0c5ebe32011-08-19 22:37:16806 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50807 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:16808 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:49809 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]0c5ebe32011-08-19 22:37:16810 ASSERT_FALSE(created);
811
812 context.reset(NewContext(NULL));
813 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50814 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()));
[email protected]d4905e2e2011-05-13 21:56:32815 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49816 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32817 ASSERT_TRUE(created);
818
819 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
820
[email protected]0c5ebe32011-08-19 22:37:16821 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32822 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49823 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32824 ASSERT_FALSE(created);
825
826 // Also test in a subdirectory.
[email protected]08f8feb2012-02-26 11:53:50827 path = CreatePathFromUTF8("path/to/file.txt");
[email protected]0c5ebe32011-08-19 22:37:16828 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32829 bool exclusive = true;
830 bool recursive = true;
[email protected]7878ece2011-09-05 11:41:49831 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32832 context.get(), path.DirName(), exclusive, recursive));
833
[email protected]0c5ebe32011-08-19 22:37:16834 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32835 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49836 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:32837 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:16838 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49839 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16840 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49841 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32842}
843
[email protected]7878ece2011-09-05 11:41:49844TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) {
[email protected]0c5ebe32011-08-19 22:37:16845 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32846
847 bool exclusive = false;
848 bool recursive = false;
[email protected]08f8feb2012-02-26 11:53:50849 FileSystemPath path = CreatePathFromUTF8("foo/bar");
[email protected]7878ece2011-09-05 11:41:49850 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32851 context.get(), path, exclusive, recursive));
852
[email protected]0c5ebe32011-08-19 22:37:16853 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32854 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:49855 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32856
[email protected]08f8feb2012-02-26 11:53:50857 FileSystemPath root = CreatePathFromUTF8("");
[email protected]0c5ebe32011-08-19 22:37:16858 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49859 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16860 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49861 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16862 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49863 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]d4905e2e2011-05-13 21:56:32864
[email protected]0c5ebe32011-08-19 22:37:16865 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32866 exclusive = false;
867 recursive = true;
[email protected]7878ece2011-09-05 11:41:49868 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32869 context.get(), path, exclusive, recursive));
870
[email protected]0c5ebe32011-08-19 22:37:16871 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49872 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16873 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49874 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16875 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49876 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root));
[email protected]0c5ebe32011-08-19 22:37:16877 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49878 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName()));
[email protected]0c5ebe32011-08-19 22:37:16879 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49880 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32881
882 // Can't remove a non-empty directory.
[email protected]0c5ebe32011-08-19 22:37:16883 context.reset(NewContext(NULL));
[email protected]c7a4a10f2011-05-19 04:56:06884 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
[email protected]7878ece2011-09-05 11:41:49885 ofu()->DeleteSingleDirectory(context.get(), path.DirName()));
[email protected]d4905e2e2011-05-13 21:56:32886
887 base::PlatformFileInfo file_info;
888 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:49889 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32890 context.get(), path, &file_info, &local_path));
891 EXPECT_TRUE(local_path.empty());
892 EXPECT_TRUE(file_info.is_directory);
893 EXPECT_FALSE(file_info.is_symbolic_link);
894
895 // Same create again should succeed, since exclusive is false.
[email protected]0c5ebe32011-08-19 22:37:16896 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49897 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32898 context.get(), path, exclusive, recursive));
899
900 exclusive = true;
901 recursive = true;
[email protected]0c5ebe32011-08-19 22:37:16902 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49903 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32904 context.get(), path, exclusive, recursive));
905
[email protected]0c5ebe32011-08-19 22:37:16906 // Verify that deleting a directory isn't stopped by zero quota, and that it
907 // frees up quota from its path.
908 context.reset(NewContext(NULL));
909 context->set_allowed_bytes_growth(0);
[email protected]d4905e2e2011-05-13 21:56:32910 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:49911 ofu()->DeleteSingleDirectory(context.get(), path));
[email protected]08f8feb2012-02-26 11:53:50912 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()),
[email protected]0c5ebe32011-08-19 22:37:16913 context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:32914
[email protected]08f8feb2012-02-26 11:53:50915 path = CreatePathFromUTF8("foo/bop");
[email protected]d4905e2e2011-05-13 21:56:32916
[email protected]0c5ebe32011-08-19 22:37:16917 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49918 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16919 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49920 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16921 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49922 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
923 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:32924 context.get(), path, &file_info, &local_path));
925
[email protected]0c5ebe32011-08-19 22:37:16926 // Verify that file creation requires sufficient quota for the path.
[email protected]d4905e2e2011-05-13 21:56:32927 exclusive = true;
928 recursive = false;
[email protected]0c5ebe32011-08-19 22:37:16929 context.reset(NewContext(NULL));
930 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50931 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1);
[email protected]7878ece2011-09-05 11:41:49932 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:16933 context.get(), path, exclusive, recursive));
934
935 context.reset(NewContext(NULL));
936 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:50937 ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()));
[email protected]7878ece2011-09-05 11:41:49938 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32939 context.get(), path, exclusive, recursive));
940
[email protected]0c5ebe32011-08-19 22:37:16941 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49942 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16943 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49944 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32945
946 exclusive = true;
947 recursive = false;
[email protected]7878ece2011-09-05 11:41:49948 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32949 context.get(), path, exclusive, recursive));
950
951 exclusive = true;
952 recursive = false;
[email protected]08f8feb2012-02-26 11:53:50953 path = CreatePathFromUTF8("foo");
[email protected]7878ece2011-09-05 11:41:49954 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32955 context.get(), path, exclusive, recursive));
956
[email protected]08f8feb2012-02-26 11:53:50957 path = CreatePathFromUTF8("blah");
[email protected]d4905e2e2011-05-13 21:56:32958
[email protected]0c5ebe32011-08-19 22:37:16959 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49960 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16961 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49962 EXPECT_FALSE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32963
964 exclusive = true;
965 recursive = false;
[email protected]7878ece2011-09-05 11:41:49966 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32967 context.get(), path, exclusive, recursive));
968
[email protected]0c5ebe32011-08-19 22:37:16969 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49970 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path));
[email protected]0c5ebe32011-08-19 22:37:16971 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:49972 EXPECT_TRUE(ofu()->PathExists(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:32973
974 exclusive = true;
975 recursive = false;
[email protected]7878ece2011-09-05 11:41:49976 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32977 context.get(), path, exclusive, recursive));
978}
979
[email protected]7878ece2011-09-05 11:41:49980TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) {
[email protected]0c5ebe32011-08-19 22:37:16981 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:32982 bool exclusive = true;
983 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:50984 FileSystemPath path = CreatePathFromUTF8("directory/to/use");
[email protected]7878ece2011-09-05 11:41:49985 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:32986 context.get(), path, exclusive, recursive));
987 TestReadDirectoryHelper(path);
988}
989
[email protected]7878ece2011-09-05 11:41:49990TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) {
[email protected]08f8feb2012-02-26 11:53:50991 TestReadDirectoryHelper(CreatePathFromUTF8(""));
[email protected]d4905e2e2011-05-13 21:56:32992}
993
[email protected]7878ece2011-09-05 11:41:49994TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) {
[email protected]08f8feb2012-02-26 11:53:50995 TestReadDirectoryHelper(CreatePathFromUTF8("/"));
[email protected]d4905e2e2011-05-13 21:56:32996}
997
[email protected]7878ece2011-09-05 11:41:49998TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) {
[email protected]08f8feb2012-02-26 11:53:50999 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161000 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321001
1002 bool created = false;
1003 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491004 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]d4905e2e2011-05-13 21:56:321005 ASSERT_TRUE(created);
1006
[email protected]0c5ebe32011-08-19 22:37:161007 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321008 std::vector<base::FileUtilProxy::Entry> entries;
1009 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]a3938912012-03-27 14:00:551010 FileUtilHelper::ReadDirectory(
1011 context.get(), ofu(), path, &entries));
[email protected]d4905e2e2011-05-13 21:56:321012
[email protected]7878ece2011-09-05 11:41:491013 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path));
[email protected]d4905e2e2011-05-13 21:56:321014}
1015
[email protected]7878ece2011-09-05 11:41:491016TEST_F(ObfuscatedFileUtilTest, TestTouch) {
[email protected]08f8feb2012-02-26 11:53:501017 FileSystemPath path = CreatePathFromUTF8("file");
[email protected]0c5ebe32011-08-19 22:37:161018 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411019
1020 base::Time last_access_time = base::Time::Now();
1021 base::Time last_modified_time = base::Time::Now();
1022
1023 // It's not there yet.
[email protected]d4905e2e2011-05-13 21:56:321024 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491025 ofu()->Touch(
[email protected]d4905e2e2011-05-13 21:56:321026 context.get(), path, last_access_time, last_modified_time));
1027
[email protected]2517cfa2011-08-25 05:12:411028 // OK, now create it.
[email protected]0c5ebe32011-08-19 22:37:161029 context.reset(NewContext(NULL));
[email protected]2517cfa2011-08-25 05:12:411030 bool created = false;
1031 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491032 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:411033 ASSERT_TRUE(created);
1034 TestTouchHelper(path, true);
1035
1036 // Now test a directory:
1037 context.reset(NewContext(NULL));
1038 bool exclusive = true;
1039 bool recursive = false;
[email protected]08f8feb2012-02-26 11:53:501040 path = CreatePathFromUTF8("dir");
[email protected]7878ece2011-09-05 11:41:491041 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(),
[email protected]2517cfa2011-08-25 05:12:411042 path, exclusive, recursive));
1043 TestTouchHelper(path, false);
[email protected]0c5ebe32011-08-19 22:37:161044}
1045
[email protected]7878ece2011-09-05 11:41:491046TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) {
[email protected]08f8feb2012-02-26 11:53:501047 FileSystemPath path = CreatePathFromUTF8("fake/file");
[email protected]0c5ebe32011-08-19 22:37:161048 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1049
[email protected]08f8feb2012-02-26 11:53:501050 path = CreatePathFromUTF8("file name");
[email protected]0c5ebe32011-08-19 22:37:161051 context->set_allowed_bytes_growth(5);
[email protected]2517cfa2011-08-25 05:12:411052 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161053 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491054 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:411055 EXPECT_FALSE(created);
[email protected]0c5ebe32011-08-19 22:37:161056 context->set_allowed_bytes_growth(1024);
1057 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491058 ofu()->EnsureFileExists(context.get(), path, &created));
[email protected]2517cfa2011-08-25 05:12:411059 EXPECT_TRUE(created);
[email protected]08f8feb2012-02-26 11:53:501060 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(
1061 path.internal_path());
[email protected]0c5ebe32011-08-19 22:37:161062 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
1063
1064 context->set_allowed_bytes_growth(1024);
1065 bool exclusive = true;
1066 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:501067 path = CreatePathFromUTF8("directory/to/use");
[email protected]0c5ebe32011-08-19 22:37:161068 std::vector<FilePath::StringType> components;
[email protected]08f8feb2012-02-26 11:53:501069 path.internal_path().GetComponents(&components);
[email protected]0c5ebe32011-08-19 22:37:161070 path_cost = 0;
1071 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
1072 iter != components.end(); ++iter) {
[email protected]7878ece2011-09-05 11:41:491073 path_cost += ObfuscatedFileUtil::ComputeFilePathCost(
[email protected]0c5ebe32011-08-19 22:37:161074 FilePath(*iter));
1075 }
1076 context.reset(NewContext(NULL));
1077 context->set_allowed_bytes_growth(1024);
[email protected]7878ece2011-09-05 11:41:491078 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161079 context.get(), path, exclusive, recursive));
1080 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
[email protected]d4905e2e2011-05-13 21:56:321081}
1082
[email protected]7878ece2011-09-05 11:41:491083TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) {
[email protected]08f8feb2012-02-26 11:53:501084 FileSystemPath source_path = CreatePathFromUTF8("path0.txt");
1085 FileSystemPath dest_path = CreatePathFromUTF8("path1.txt");
[email protected]0c5ebe32011-08-19 22:37:161086 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321087
1088 bool is_copy_not_move = false;
1089 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491090 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321091 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:161092 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321093 is_copy_not_move = true;
1094 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491095 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321096 is_copy_not_move));
[email protected]08f8feb2012-02-26 11:53:501097 source_path = CreatePathFromUTF8("dir/dir/file");
[email protected]d4905e2e2011-05-13 21:56:321098 bool exclusive = true;
1099 bool recursive = true;
[email protected]0c5ebe32011-08-19 22:37:161100 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491101 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321102 context.get(), source_path.DirName(), exclusive, recursive));
1103 is_copy_not_move = false;
1104 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491105 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321106 is_copy_not_move));
[email protected]0c5ebe32011-08-19 22:37:161107 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321108 is_copy_not_move = true;
1109 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491110 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path,
[email protected]d4905e2e2011-05-13 21:56:321111 is_copy_not_move));
1112}
1113
[email protected]7878ece2011-09-05 11:41:491114TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) {
[email protected]d4905e2e2011-05-13 21:56:321115 const int64 kSourceLength = 5;
1116 const int64 kDestLength = 50;
1117
1118 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
1119 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
1120 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
1121 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
1122 test_case.is_copy_not_move);
1123 SCOPED_TRACE(testing::Message() << "\t source_path " <<
1124 test_case.source_path);
1125 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
1126 test_case.dest_path);
1127 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
1128 test_case.cause_overwrite);
[email protected]0c5ebe32011-08-19 22:37:161129 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321130
1131 bool exclusive = false;
1132 bool recursive = true;
[email protected]08f8feb2012-02-26 11:53:501133 FileSystemPath source_path = CreatePathFromUTF8(test_case.source_path);
1134 FileSystemPath dest_path = CreatePathFromUTF8(test_case.dest_path);
[email protected]d4905e2e2011-05-13 21:56:321135
[email protected]0c5ebe32011-08-19 22:37:161136 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491137 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321138 context.get(), source_path.DirName(), exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161139 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491140 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321141 context.get(), dest_path.DirName(), exclusive, recursive));
1142
[email protected]d4905e2e2011-05-13 21:56:321143 bool created = false;
[email protected]0c5ebe32011-08-19 22:37:161144 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321145 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491146 ofu()->EnsureFileExists(context.get(), source_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321147 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161148 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321149 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491150 ofu()->Truncate(context.get(), source_path, kSourceLength));
[email protected]d4905e2e2011-05-13 21:56:321151
1152 if (test_case.cause_overwrite) {
[email protected]0c5ebe32011-08-19 22:37:161153 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321154 created = false;
1155 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491156 ofu()->EnsureFileExists(context.get(), dest_path, &created));
[email protected]d4905e2e2011-05-13 21:56:321157 ASSERT_TRUE(created);
[email protected]0c5ebe32011-08-19 22:37:161158 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321159 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491160 ofu()->Truncate(context.get(), dest_path, kDestLength));
[email protected]d4905e2e2011-05-13 21:56:321161 }
1162
[email protected]0c5ebe32011-08-19 22:37:161163 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491164 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(),
[email protected]d4905e2e2011-05-13 21:56:321165 source_path, dest_path, test_case.is_copy_not_move));
1166 if (test_case.is_copy_not_move) {
1167 base::PlatformFileInfo file_info;
1168 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161169 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321171 context.get(), source_path, &file_info, &local_path));
1172 EXPECT_EQ(kSourceLength, file_info.size);
1173 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491174 ofu()->DeleteFile(context.get(), source_path));
[email protected]d4905e2e2011-05-13 21:56:321175 } else {
1176 base::PlatformFileInfo file_info;
1177 FilePath local_path;
[email protected]0c5ebe32011-08-19 22:37:161178 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491179 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321180 context.get(), source_path, &file_info, &local_path));
1181 }
1182 base::PlatformFileInfo file_info;
1183 FilePath local_path;
[email protected]7878ece2011-09-05 11:41:491184 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo(
[email protected]d4905e2e2011-05-13 21:56:321185 context.get(), dest_path, &file_info, &local_path));
1186 EXPECT_EQ(kSourceLength, file_info.size);
1187
1188 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491189 ofu()->DeleteFile(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321190 }
1191}
1192
[email protected]7878ece2011-09-05 11:41:491193TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) {
[email protected]08f8feb2012-02-26 11:53:501194 FileSystemPath src_path = CreatePathFromUTF8("src path");
1195 FileSystemPath dest_path = CreatePathFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161196 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1197 bool created = false;
[email protected]7878ece2011-09-05 11:41:491198 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161199 context.get(), src_path, &created));
1200
1201 bool is_copy = true;
1202 // Copy, no overwrite.
1203 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501204 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161205 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491206 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161207 context.reset(NewContext(NULL));
1208 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501209 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()));
[email protected]0c5ebe32011-08-19 22:37:161210 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491211 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161212
1213 // Copy, with overwrite.
1214 context.reset(NewContext(NULL));
1215 context->set_allowed_bytes_growth(0);
1216 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491217 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161218}
1219
[email protected]7878ece2011-09-05 11:41:491220TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) {
[email protected]08f8feb2012-02-26 11:53:501221 FileSystemPath src_path = CreatePathFromUTF8("src path");
1222 FileSystemPath dest_path = CreatePathFromUTF8("destination path");
[email protected]0c5ebe32011-08-19 22:37:161223 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1224 bool created = false;
[email protected]7878ece2011-09-05 11:41:491225 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161226 context.get(), src_path, &created));
1227
1228 bool is_copy = false;
1229 // Move, rename, no overwrite.
1230 context.reset(NewContext(NULL));
1231 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501232 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) -
1233 ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()) - 1);
[email protected]0c5ebe32011-08-19 22:37:161234 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
[email protected]7878ece2011-09-05 11:41:491235 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161236 context.reset(NewContext(NULL));
1237 context->set_allowed_bytes_growth(
[email protected]08f8feb2012-02-26 11:53:501238 ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) -
1239 ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()));
[email protected]0c5ebe32011-08-19 22:37:161240 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491241 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161242
1243 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491244 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161245 context.get(), src_path, &created));
1246
1247 // Move, rename, with overwrite.
1248 context.reset(NewContext(NULL));
1249 context->set_allowed_bytes_growth(0);
1250 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491251 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161252}
1253
[email protected]7878ece2011-09-05 11:41:491254TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) {
[email protected]08f8feb2012-02-26 11:53:501255 FileSystemPath src_path = CreatePathFromUTF8("src path");
[email protected]0c5ebe32011-08-19 22:37:161256 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1257 bool created = false;
[email protected]7878ece2011-09-05 11:41:491258 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161259 context.get(), src_path, &created));
1260
1261 bool exclusive = true;
1262 bool recursive = false;
[email protected]08f8feb2012-02-26 11:53:501263 FileSystemPath dir_path = CreatePathFromUTF8("directory path");
[email protected]0c5ebe32011-08-19 22:37:161264 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491265 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]0c5ebe32011-08-19 22:37:161266 context.get(), dir_path, exclusive, recursive));
1267
[email protected]08f8feb2012-02-26 11:53:501268 FileSystemPath dest_path = dir_path.Append(src_path.internal_path());
[email protected]0c5ebe32011-08-19 22:37:161269
1270 bool is_copy = false;
1271 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1272 // Move, no rename, no overwrite.
1273 context.reset(NewContext(NULL));
1274 context->set_allowed_bytes_growth(allowed_bytes_growth);
1275 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491276 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161277 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1278
1279 // Move, no rename, with overwrite.
1280 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491281 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists(
[email protected]0c5ebe32011-08-19 22:37:161282 context.get(), src_path, &created));
1283 context.reset(NewContext(NULL));
1284 context->set_allowed_bytes_growth(allowed_bytes_growth);
1285 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491286 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
[email protected]0c5ebe32011-08-19 22:37:161287 EXPECT_EQ(
1288 allowed_bytes_growth +
[email protected]08f8feb2012-02-26 11:53:501289 ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()),
[email protected]0c5ebe32011-08-19 22:37:161290 context->allowed_bytes_growth());
1291}
1292
[email protected]7878ece2011-09-05 11:41:491293TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) {
[email protected]81b7f662011-05-26 00:54:461294 TestCopyInForeignFileHelper(false /* overwrite */);
1295 TestCopyInForeignFileHelper(true /* overwrite */);
1296}
1297
[email protected]7878ece2011-09-05 11:41:491298TEST_F(ObfuscatedFileUtilTest, TestEnumerator) {
[email protected]0c5ebe32011-08-19 22:37:161299 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:501300 FileSystemPath src_path = CreatePathFromUTF8("source dir");
[email protected]d4905e2e2011-05-13 21:56:321301 bool exclusive = true;
1302 bool recursive = false;
[email protected]7878ece2011-09-05 11:41:491303 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(
[email protected]d4905e2e2011-05-13 21:56:321304 context.get(), src_path, exclusive, recursive));
1305
[email protected]89ee4272011-05-16 18:45:171306 std::set<FilePath::StringType> files;
1307 std::set<FilePath::StringType> directories;
[email protected]d4905e2e2011-05-13 21:56:321308 FillTestDirectory(src_path, &files, &directories);
1309
[email protected]08f8feb2012-02-26 11:53:501310 FileSystemPath dest_path = CreatePathFromUTF8("destination dir");
[email protected]d4905e2e2011-05-13 21:56:321311
[email protected]0c5ebe32011-08-19 22:37:161312 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491313 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161314 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321315 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]45ea0fbbb2012-02-27 22:28:491316 test_helper().SameFileUtilCopy(context.get(), src_path, dest_path));
[email protected]d4905e2e2011-05-13 21:56:321317
1318 ValidateTestDirectory(dest_path, files, directories);
[email protected]0c5ebe32011-08-19 22:37:161319 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491320 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path));
[email protected]0c5ebe32011-08-19 22:37:161321 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491322 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]0c5ebe32011-08-19 22:37:161323 context.reset(NewContext(NULL));
[email protected]d4905e2e2011-05-13 21:56:321324 recursive = true;
1325 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]aa437fa2012-03-03 02:09:071326 FileUtilHelper::Delete(context.get(), ofu(),
1327 dest_path, recursive));
[email protected]0c5ebe32011-08-19 22:37:161328 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491329 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path));
[email protected]d4905e2e2011-05-13 21:56:321330}
[email protected]6b931152011-05-20 21:02:351331
[email protected]7878ece2011-09-05 11:41:491332TEST_F(ObfuscatedFileUtilTest, TestMigration) {
[email protected]6b931152011-05-20 21:02:351333 ScopedTempDir source_dir;
1334 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
1335 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
1336 ASSERT_TRUE(file_util::CreateDirectory(root_path));
1337
[email protected]f83b5b72012-01-27 10:26:561338 test::SetUpRegularTestCases(root_path);
[email protected]6b931152011-05-20 21:02:351339
[email protected]7878ece2011-09-05 11:41:491340 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path));
[email protected]6b931152011-05-20 21:02:351341
1342 FilePath new_root =
[email protected]0c5ebe32011-08-19 22:37:161343 test_directory().AppendASCII("File System").AppendASCII("000").Append(
[email protected]7878ece2011-09-05 11:41:491344 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
[email protected]f83b5b72012-01-27 10:26:561345 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]6b931152011-05-20 21:02:351346 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561347 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]6b931152011-05-20 21:02:351348 FilePath local_data_path = new_root.Append(test_case.path);
[email protected]d9034ed22012-02-10 02:04:401349 local_data_path = local_data_path.NormalizePathSeparators();
[email protected]0c5ebe32011-08-19 22:37:161350 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491351 base::PlatformFileInfo ofu_file_info;
[email protected]6b931152011-05-20 21:02:351352 FilePath data_path;
1353 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
1354 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501355 ofu()->GetFileInfo(context.get(), CreatePath(FilePath(test_case.path)),
[email protected]7878ece2011-09-05 11:41:491356 &ofu_file_info, &data_path));
[email protected]6b931152011-05-20 21:02:351357 if (test_case.is_directory) {
[email protected]7878ece2011-09-05 11:41:491358 EXPECT_TRUE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351359 } else {
1360 base::PlatformFileInfo platform_file_info;
1361 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
1362 local_data_path.value());
1363 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
1364 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
1365 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
1366 EXPECT_FALSE(platform_file_info.is_directory);
[email protected]0c5ebe32011-08-19 22:37:161367 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]6b931152011-05-20 21:02:351368 EXPECT_EQ(local_data_path, data_path);
[email protected]7878ece2011-09-05 11:41:491369 EXPECT_EQ(platform_file_info.size, ofu_file_info.size);
1370 EXPECT_FALSE(ofu_file_info.is_directory);
[email protected]6b931152011-05-20 21:02:351371 }
1372 }
1373}
[email protected]fcc2d5f2011-05-23 22:06:261374
[email protected]7878ece2011-09-05 11:41:491375TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) {
1376 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator>
1377 enumerator(ofu()->CreateOriginEnumerator());
[email protected]0c5ebe32011-08-19 22:37:161378 // The test helper starts out with a single filesystem.
[email protected]fcc2d5f2011-05-23 22:06:261379 EXPECT_TRUE(enumerator.get());
[email protected]0c5ebe32011-08-19 22:37:161380 EXPECT_EQ(origin(), enumerator->Next());
1381 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1382 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1383 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]fcc2d5f2011-05-23 22:06:261384 EXPECT_EQ(GURL(), enumerator->Next());
1385 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1386 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
1387
1388 std::set<GURL> origins_expected;
[email protected]0c5ebe32011-08-19 22:37:161389 origins_expected.insert(origin());
[email protected]fcc2d5f2011-05-23 22:06:261390
1391 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
1392 SCOPED_TRACE(testing::Message() <<
1393 "Validating kOriginEnumerationTestRecords " << i);
1394 const OriginEnumerationTestRecord& record =
1395 kOriginEnumerationTestRecords[i];
1396 GURL origin_url(record.origin_url);
1397 origins_expected.insert(origin_url);
1398 if (record.has_temporary) {
[email protected]0c5ebe32011-08-19 22:37:161399 scoped_ptr<FileSystemTestOriginHelper> helper(
1400 NewHelper(origin_url, kFileSystemTypeTemporary));
1401 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261402 bool created = false;
1403 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501404 ofu()->EnsureFileExists(
1405 context.get(),
1406 helper->CreatePathFromUTF8("file"),
1407 &created));
[email protected]fcc2d5f2011-05-23 22:06:261408 EXPECT_TRUE(created);
1409 }
1410 if (record.has_persistent) {
[email protected]0c5ebe32011-08-19 22:37:161411 scoped_ptr<FileSystemTestOriginHelper> helper(
1412 NewHelper(origin_url, kFileSystemTypePersistent));
1413 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
[email protected]fcc2d5f2011-05-23 22:06:261414 bool created = false;
1415 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501416 ofu()->EnsureFileExists(
1417 context.get(),
1418 helper->CreatePathFromUTF8("file"),
1419 &created));
[email protected]fcc2d5f2011-05-23 22:06:261420 EXPECT_TRUE(created);
1421 }
1422 }
[email protected]7878ece2011-09-05 11:41:491423 enumerator.reset(ofu()->CreateOriginEnumerator());
[email protected]fcc2d5f2011-05-23 22:06:261424 EXPECT_TRUE(enumerator.get());
1425 std::set<GURL> origins_found;
[email protected]0c5ebe32011-08-19 22:37:161426 GURL origin_url;
1427 while (!(origin_url = enumerator->Next()).is_empty()) {
1428 origins_found.insert(origin_url);
1429 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
[email protected]fcc2d5f2011-05-23 22:06:261430 bool found = false;
1431 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
1432 ++i) {
1433 const OriginEnumerationTestRecord& record =
1434 kOriginEnumerationTestRecords[i];
[email protected]0c5ebe32011-08-19 22:37:161435 if (GURL(record.origin_url) != origin_url)
[email protected]fcc2d5f2011-05-23 22:06:261436 continue;
1437 found = true;
1438 EXPECT_EQ(record.has_temporary,
1439 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1440 EXPECT_EQ(record.has_persistent,
1441 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1442 }
[email protected]0c5ebe32011-08-19 22:37:161443 // Deal with the default filesystem created by the test helper.
1444 if (!found && origin_url == origin()) {
1445 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1446 EXPECT_EQ(true,
1447 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
[email protected]34161bb2011-10-13 12:36:181448 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
[email protected]0c5ebe32011-08-19 22:37:161449 found = true;
1450 }
[email protected]fcc2d5f2011-05-23 22:06:261451 EXPECT_TRUE(found);
1452 }
1453
1454 std::set<GURL> diff;
1455 std::set_symmetric_difference(origins_expected.begin(),
1456 origins_expected.end(), origins_found.begin(), origins_found.end(),
1457 inserter(diff, diff.begin()));
1458 EXPECT_TRUE(diff.empty());
1459}
[email protected]0c5ebe32011-08-19 22:37:161460
[email protected]7878ece2011-09-05 11:41:491461TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) {
[email protected]0c5ebe32011-08-19 22:37:161462 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1463
1464 int64 expected_quota = 0;
1465
[email protected]f83b5b72012-01-27 10:26:561466 for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) {
[email protected]0c5ebe32011-08-19 22:37:161467 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
[email protected]f83b5b72012-01-27 10:26:561468 const test::TestCaseRecord& test_case = test::kRegularTestCases[i];
[email protected]08f8feb2012-02-26 11:53:501469 FilePath file_path(test_case.path);
1470 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path);
[email protected]0c5ebe32011-08-19 22:37:161471 if (test_case.is_directory) {
1472 bool exclusive = true;
1473 bool recursive = false;
1474 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501475 ofu()->CreateDirectory(context.get(), CreatePath(file_path),
1476 exclusive, recursive));
[email protected]0c5ebe32011-08-19 22:37:161477 } else {
1478 bool created = false;
1479 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501480 ofu()->EnsureFileExists(context.get(), CreatePath(file_path),
1481 &created));
[email protected]0c5ebe32011-08-19 22:37:161482 ASSERT_TRUE(created);
1483 ASSERT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501484 ofu()->Truncate(context.get(),
1485 CreatePath(file_path),
1486 test_case.data_file_size));
[email protected]0c5ebe32011-08-19 22:37:161487 expected_quota += test_case.data_file_size;
1488 }
1489 }
1490 EXPECT_EQ(expected_quota, SizeInUsageFile());
1491 RevokeUsageCache();
1492 EXPECT_EQ(-1, SizeInUsageFile());
1493 GetUsageFromQuotaManager();
1494 EXPECT_EQ(expected_quota, SizeInUsageFile());
1495 EXPECT_EQ(expected_quota, usage());
1496}
[email protected]34583332011-08-31 08:59:471497
[email protected]7878ece2011-09-05 11:41:491498TEST_F(ObfuscatedFileUtilTest, TestInconsistency) {
[email protected]08f8feb2012-02-26 11:53:501499 const FileSystemPath kPath1 = CreatePathFromUTF8("hoge");
1500 const FileSystemPath kPath2 = CreatePathFromUTF8("fuga");
[email protected]34583332011-08-31 08:59:471501
1502 scoped_ptr<FileSystemOperationContext> context;
1503 base::PlatformFile file;
1504 base::PlatformFileInfo file_info;
1505 FilePath data_path;
1506 bool created = false;
1507
1508 // Create a non-empty file.
1509 context.reset(NewContext(NULL));
1510 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491511 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471512 EXPECT_TRUE(created);
1513 context.reset(NewContext(NULL));
1514 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491515 ofu()->Truncate(context.get(), kPath1, 10));
[email protected]34583332011-08-31 08:59:471516 context.reset(NewContext(NULL));
1517 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491518 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471519 context.get(), kPath1, &file_info, &data_path));
1520 EXPECT_EQ(10, file_info.size);
1521
1522 // Destroy database to make inconsistency between database and filesystem.
[email protected]7878ece2011-09-05 11:41:491523 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471524
1525 // Try to get file info of broken file.
1526 context.reset(NewContext(NULL));
[email protected]7878ece2011-09-05 11:41:491527 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1));
[email protected]34583332011-08-31 08:59:471528 context.reset(NewContext(NULL));
1529 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491530 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471531 EXPECT_TRUE(created);
1532 context.reset(NewContext(NULL));
1533 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491534 ofu()->GetFileInfo(
[email protected]34583332011-08-31 08:59:471535 context.get(), kPath1, &file_info, &data_path));
1536 EXPECT_EQ(0, file_info.size);
1537
1538 // Make another broken file to |kPath2|.
1539 context.reset(NewContext(NULL));
1540 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491541 ofu()->EnsureFileExists(context.get(), kPath2, &created));
[email protected]34583332011-08-31 08:59:471542 EXPECT_TRUE(created);
1543
1544 // Destroy again.
[email protected]7878ece2011-09-05 11:41:491545 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471546
1547 // Repair broken |kPath1|.
1548 context.reset(NewContext(NULL));
1549 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
[email protected]7878ece2011-09-05 11:41:491550 ofu()->Touch(context.get(), kPath1, base::Time::Now(),
[email protected]34583332011-08-31 08:59:471551 base::Time::Now()));
1552 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491553 ofu()->EnsureFileExists(context.get(), kPath1, &created));
[email protected]34583332011-08-31 08:59:471554 EXPECT_TRUE(created);
1555
1556 // Copy from sound |kPath1| to broken |kPath2|.
1557 context.reset(NewContext(NULL));
1558 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491559 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2,
[email protected]08f8feb2012-02-26 11:53:501560 true /* copy */));
[email protected]34583332011-08-31 08:59:471561
[email protected]7878ece2011-09-05 11:41:491562 ofu()->DestroyDirectoryDatabase(origin(), type());
[email protected]34583332011-08-31 08:59:471563 context.reset(NewContext(NULL));
1564 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491565 ofu()->CreateOrOpen(
[email protected]34583332011-08-31 08:59:471566 context.get(), kPath1,
1567 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE,
1568 &file, &created));
1569 EXPECT_TRUE(created);
1570 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info));
1571 EXPECT_EQ(0, file_info.size);
1572 EXPECT_TRUE(base::ClosePlatformFile(file));
1573}
[email protected]9dfdc0e32011-09-02 06:07:441574
[email protected]7878ece2011-09-05 11:41:491575TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) {
[email protected]08f8feb2012-02-26 11:53:501576 const FileSystemPath kPath[] = {
1577 CreatePathFromUTF8("foo"),
1578 CreatePathFromUTF8("bar"),
1579 CreatePathFromUTF8("baz")
[email protected]9dfdc0e32011-09-02 06:07:441580 };
[email protected]08f8feb2012-02-26 11:53:501581 const FileSystemPath empty_path = CreatePath(FilePath());
[email protected]9dfdc0e32011-09-02 06:07:441582 scoped_ptr<FileSystemOperationContext> context;
1583
1584 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) {
1585 bool created = false;
1586 context.reset(NewContext(NULL));
1587 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491588 ofu()->EnsureFileExists(context.get(), kPath[i], &created));
[email protected]9dfdc0e32011-09-02 06:07:441589 EXPECT_TRUE(created);
1590 }
1591
1592 context.reset(NewContext(NULL));
1593 std::vector<base::FileUtilProxy::Entry> entries;
1594 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551595 FileUtilHelper::ReadDirectory(
1596 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441597 EXPECT_EQ(3u, entries.size());
1598
1599 context.reset(NewContext(NULL));
1600 FilePath local_path;
1601 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]7878ece2011-09-05 11:41:491602 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path));
[email protected]9dfdc0e32011-09-02 06:07:441603 EXPECT_TRUE(file_util::Delete(local_path, false));
1604
1605 context.reset(NewContext(NULL));
1606 entries.clear();
1607 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]a3938912012-03-27 14:00:551608 FileUtilHelper::ReadDirectory(
1609 context.get(), ofu(), empty_path, &entries));
[email protected]9dfdc0e32011-09-02 06:07:441610 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size());
1611}
[email protected]fad625e2f2011-12-08 05:38:031612
1613TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) {
1614 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:501615 const FileSystemPath dir_path = CreatePathFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031616
1617 // Create working directory.
1618 EXPECT_EQ(base::PLATFORM_FILE_OK,
1619 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1620
1621 // EnsureFileExists, create case.
[email protected]08f8feb2012-02-26 11:53:501622 FileSystemPath path(dir_path.AppendASCII("EnsureFileExists_file"));
[email protected]fad625e2f2011-12-08 05:38:031623 bool created = false;
1624 ClearTimestamp(dir_path);
1625 context.reset(NewContext(NULL));
1626 EXPECT_EQ(base::PLATFORM_FILE_OK,
1627 ofu()->EnsureFileExists(context.get(), path, &created));
1628 EXPECT_TRUE(created);
1629 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1630
1631 // non create case.
1632 created = true;
1633 ClearTimestamp(dir_path);
1634 context.reset(NewContext(NULL));
1635 EXPECT_EQ(base::PLATFORM_FILE_OK,
1636 ofu()->EnsureFileExists(context.get(), path, &created));
1637 EXPECT_FALSE(created);
1638 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1639
1640 // fail case.
1641 path = dir_path.AppendASCII("EnsureFileExists_dir");
1642 context.reset(NewContext(NULL));
1643 EXPECT_EQ(base::PLATFORM_FILE_OK,
1644 ofu()->CreateDirectory(context.get(), path, false, false));
1645
1646 ClearTimestamp(dir_path);
1647 context.reset(NewContext(NULL));
1648 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE,
1649 ofu()->EnsureFileExists(context.get(), path, &created));
1650 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1651
1652 // CreateOrOpen, create case.
1653 path = dir_path.AppendASCII("CreateOrOpen_file");
1654 PlatformFile file_handle = base::kInvalidPlatformFileValue;
1655 created = false;
1656 ClearTimestamp(dir_path);
1657 context.reset(NewContext(NULL));
1658 EXPECT_EQ(base::PLATFORM_FILE_OK,
1659 ofu()->CreateOrOpen(
1660 context.get(), path,
1661 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1662 &file_handle, &created));
1663 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1664 EXPECT_TRUE(created);
1665 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1666 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1667
1668 // open case.
1669 file_handle = base::kInvalidPlatformFileValue;
1670 created = true;
1671 ClearTimestamp(dir_path);
1672 context.reset(NewContext(NULL));
1673 EXPECT_EQ(base::PLATFORM_FILE_OK,
1674 ofu()->CreateOrOpen(
1675 context.get(), path,
1676 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
1677 &file_handle, &created));
1678 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
1679 EXPECT_FALSE(created);
1680 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
1681 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1682
1683 // fail case
1684 file_handle = base::kInvalidPlatformFileValue;
1685 ClearTimestamp(dir_path);
1686 context.reset(NewContext(NULL));
1687 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1688 ofu()->CreateOrOpen(
1689 context.get(), path,
1690 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
1691 &file_handle, &created));
1692 EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle);
1693 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1694
1695 // CreateDirectory, create case.
1696 // Creating CreateDirectory_dir and CreateDirectory_dir/subdir.
1697 path = dir_path.AppendASCII("CreateDirectory_dir");
[email protected]08f8feb2012-02-26 11:53:501698 FileSystemPath subdir_path(path.AppendASCII("subdir"));
[email protected]fad625e2f2011-12-08 05:38:031699 ClearTimestamp(dir_path);
1700 context.reset(NewContext(NULL));
1701 EXPECT_EQ(base::PLATFORM_FILE_OK,
1702 ofu()->CreateDirectory(context.get(), subdir_path,
1703 true /* exclusive */, true /* recursive */));
1704 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1705
1706 // create subdir case.
1707 // Creating CreateDirectory_dir/subdir2.
1708 subdir_path = path.AppendASCII("subdir2");
1709 ClearTimestamp(dir_path);
1710 ClearTimestamp(path);
1711 context.reset(NewContext(NULL));
1712 EXPECT_EQ(base::PLATFORM_FILE_OK,
1713 ofu()->CreateDirectory(context.get(), subdir_path,
1714 true /* exclusive */, true /* recursive */));
1715 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1716 EXPECT_NE(base::Time(), GetModifiedTime(path));
1717
1718 // fail case.
1719 path = dir_path.AppendASCII("CreateDirectory_dir");
1720 ClearTimestamp(dir_path);
1721 context.reset(NewContext(NULL));
1722 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS,
1723 ofu()->CreateDirectory(context.get(), path,
1724 true /* exclusive */, true /* recursive */));
1725 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1726
1727 // CopyInForeignFile, create case.
1728 path = dir_path.AppendASCII("CopyInForeignFile_file");
[email protected]08f8feb2012-02-26 11:53:501729 FileSystemPath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file");
[email protected]fad625e2f2011-12-08 05:38:031730 context.reset(NewContext(NULL));
1731 EXPECT_EQ(base::PLATFORM_FILE_OK,
1732 ofu()->EnsureFileExists(context.get(), src_path, &created));
1733 EXPECT_TRUE(created);
1734 FilePath src_local_path;
1735 context.reset(NewContext(NULL));
1736 EXPECT_EQ(base::PLATFORM_FILE_OK,
1737 ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path));
1738
1739 ClearTimestamp(dir_path);
1740 context.reset(NewContext(NULL));
1741 EXPECT_EQ(base::PLATFORM_FILE_OK,
[email protected]08f8feb2012-02-26 11:53:501742 ofu()->CopyInForeignFile(context.get(),
[email protected]294dd0312012-05-11 07:35:131743 src_local_path,
[email protected]08f8feb2012-02-26 11:53:501744 path));
[email protected]fad625e2f2011-12-08 05:38:031745 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1746}
1747
1748TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) {
1749 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
[email protected]08f8feb2012-02-26 11:53:501750 const FileSystemPath dir_path = CreatePathFromUTF8("foo_dir");
[email protected]fad625e2f2011-12-08 05:38:031751
1752 // Create working directory.
1753 EXPECT_EQ(base::PLATFORM_FILE_OK,
1754 ofu()->CreateDirectory(context.get(), dir_path, false, false));
1755
1756 // DeleteFile, delete case.
[email protected]08f8feb2012-02-26 11:53:501757 FileSystemPath path = dir_path.AppendASCII("DeleteFile_file");
[email protected]fad625e2f2011-12-08 05:38:031758 bool created = false;
1759 context.reset(NewContext(NULL));
1760 EXPECT_EQ(base::PLATFORM_FILE_OK,
1761 ofu()->EnsureFileExists(context.get(), path, &created));
1762 EXPECT_TRUE(created);
1763
1764 ClearTimestamp(dir_path);
1765 context.reset(NewContext(NULL));
1766 EXPECT_EQ(base::PLATFORM_FILE_OK,
1767 ofu()->DeleteFile(context.get(), path));
1768 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1769
1770 // fail case.
1771 ClearTimestamp(dir_path);
1772 context.reset(NewContext(NULL));
1773 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
1774 ofu()->DeleteFile(context.get(), path));
1775 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1776
1777 // DeleteSingleDirectory, fail case.
1778 path = dir_path.AppendASCII("DeleteSingleDirectory_dir");
[email protected]08f8feb2012-02-26 11:53:501779 FileSystemPath file_path(path.AppendASCII("pakeratta"));
[email protected]fad625e2f2011-12-08 05:38:031780 context.reset(NewContext(NULL));
1781 EXPECT_EQ(base::PLATFORM_FILE_OK,
1782 ofu()->CreateDirectory(context.get(), path, true, true));
1783 created = false;
1784 context.reset(NewContext(NULL));
1785 EXPECT_EQ(base::PLATFORM_FILE_OK,
1786 ofu()->EnsureFileExists(context.get(), file_path, &created));
1787 EXPECT_TRUE(created);
1788
1789 ClearTimestamp(dir_path);
1790 context.reset(NewContext(NULL));
1791 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
1792 ofu()->DeleteSingleDirectory(context.get(), path));
1793 EXPECT_EQ(base::Time(), GetModifiedTime(dir_path));
1794
1795 // delete case.
1796 context.reset(NewContext(NULL));
1797 EXPECT_EQ(base::PLATFORM_FILE_OK,
1798 ofu()->DeleteFile(context.get(), file_path));
1799
1800 ClearTimestamp(dir_path);
1801 context.reset(NewContext(NULL));
1802 EXPECT_EQ(base::PLATFORM_FILE_OK,
1803 ofu()->DeleteSingleDirectory(context.get(), path));
1804 EXPECT_NE(base::Time(), GetModifiedTime(dir_path));
1805}
1806
1807TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) {
1808 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501809 CreatePathFromUTF8("copy overwrite"), true, true);
[email protected]fad625e2f2011-12-08 05:38:031810 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501811 CreatePathFromUTF8("copy non-overwrite"), true, false);
[email protected]fad625e2f2011-12-08 05:38:031812 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501813 CreatePathFromUTF8("move overwrite"), false, true);
[email protected]fad625e2f2011-12-08 05:38:031814 TestDirectoryTimestampHelper(
[email protected]08f8feb2012-02-26 11:53:501815 CreatePathFromUTF8("move non-overwrite"), false, false);
[email protected]fad625e2f2011-12-08 05:38:031816}
[email protected]a3938912012-03-27 14:00:551817
1818TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) {
1819 FileSystemPath dir = CreatePathFromUTF8("foo");
1820 FileSystemPath path1 = dir.AppendASCII("bar");
1821 FileSystemPath path2 = dir.AppendASCII("baz");
1822
1823 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1824 EXPECT_EQ(base::PLATFORM_FILE_OK,
1825 ofu()->CreateDirectory(context.get(), dir, false, false));
1826
1827 bool created = false;
1828 context.reset(NewContext(NULL));
1829 EXPECT_EQ(base::PLATFORM_FILE_OK,
1830 ofu()->EnsureFileExists(context.get(), path1, &created));
1831 EXPECT_TRUE(created);
1832
1833 context.reset(NewContext(NULL));
1834 EXPECT_EQ(base::PLATFORM_FILE_OK,
1835 ofu()->CreateDirectory(context.get(), path2, false, false));
1836
1837 FilePath file_path;
1838 context.reset(NewContext(NULL));
1839 EXPECT_EQ(base::PLATFORM_FILE_OK,
1840 ofu()->GetLocalFilePath(context.get(), path1, &file_path));
1841 EXPECT_FALSE(file_path.empty());
1842
1843 context.reset(NewContext(NULL));
1844 EXPECT_EQ(base::PLATFORM_FILE_OK,
1845 ofu()->Touch(context.get(), path1,
1846 base::Time::Now() + base::TimeDelta::FromHours(1),
1847 base::Time()));
1848
1849 context.reset(NewContext(NULL));
1850 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
1851 ofu()->CreateFileEnumerator(context.get(), dir, false));
1852
1853 int count = 0;
1854 FilePath file_path_each;
1855 while (!(file_path_each = file_enum->Next()).empty()) {
1856 context.reset(NewContext(NULL));
1857 base::PlatformFileInfo file_info;
1858 FilePath file_path;
1859 EXPECT_EQ(base::PLATFORM_FILE_OK,
1860 ofu()->GetFileInfo(context.get(),
1861 dir.WithInternalPath(file_path_each),
1862 &file_info, &file_path));
1863 EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory());
1864 EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime());
1865 EXPECT_EQ(file_info.size, file_enum->Size());
1866 ++count;
1867 }
1868 EXPECT_EQ(2, count);
1869}
[email protected]294dd0312012-05-11 07:35:131870
1871TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) {
1872 FileSystemPath from_file(CreatePathFromUTF8("fromfile"));
1873 FileSystemPath obstacle_file(CreatePathFromUTF8("obstaclefile"));
1874 FileSystemPath to_file1(CreatePathFromUTF8("tofile1"));
1875 FileSystemPath to_file2(CreatePathFromUTF8("tofile2"));
1876 bool created;
1877
1878 int64 expected_total_file_size = 0;
1879 ASSERT_EQ(base::PLATFORM_FILE_OK,
1880 ofu()->EnsureFileExists(
1881 AllowUsageIncrease(PathCost(from_file))->context(),
1882 from_file, &created));
1883 ASSERT_TRUE(created);
1884 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1885
1886 ASSERT_EQ(base::PLATFORM_FILE_OK,
1887 ofu()->EnsureFileExists(
1888 AllowUsageIncrease(PathCost(obstacle_file))->context(),
1889 obstacle_file, &created));
1890 ASSERT_TRUE(created);
1891 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1892
1893 int64 from_file_size = 1020;
1894 expected_total_file_size += from_file_size;
1895 ASSERT_EQ(base::PLATFORM_FILE_OK,
1896 ofu()->Truncate(
1897 AllowUsageIncrease(from_file_size)->context(),
1898 from_file, from_file_size));
1899 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1900
1901 int64 obstacle_file_size = 1;
1902 expected_total_file_size += obstacle_file_size;
1903 ASSERT_EQ(base::PLATFORM_FILE_OK,
1904 ofu()->Truncate(
1905 AllowUsageIncrease(obstacle_file_size)->context(),
1906 obstacle_file, obstacle_file_size));
1907 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1908
1909 int64 to_file1_size = from_file_size;
1910 expected_total_file_size += to_file1_size;
1911 ASSERT_EQ(base::PLATFORM_FILE_OK,
1912 ofu()->CopyOrMoveFile(
1913 AllowUsageIncrease(
1914 PathCost(to_file1) + to_file1_size)->context(),
1915 from_file, to_file1, true /* copy */));
1916 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1917
1918 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1919 ofu()->CopyOrMoveFile(
1920 DisallowUsageIncrease(
1921 PathCost(to_file2) + from_file_size)->context(),
1922 from_file, to_file2, true /* copy */));
1923 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1924
1925 int64 old_obstacle_file_size = obstacle_file_size;
1926 obstacle_file_size = from_file_size;
1927 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1928 ASSERT_EQ(base::PLATFORM_FILE_OK,
1929 ofu()->CopyOrMoveFile(
1930 AllowUsageIncrease(
1931 obstacle_file_size - old_obstacle_file_size)->context(),
1932 from_file, obstacle_file, true /* copy */));
1933 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1934
1935 int64 old_from_file_size = from_file_size;
1936 from_file_size = old_from_file_size - 1;
1937 expected_total_file_size += from_file_size - old_from_file_size;
1938 ASSERT_EQ(base::PLATFORM_FILE_OK,
1939 ofu()->Truncate(
1940 AllowUsageIncrease(
1941 from_file_size - old_from_file_size)->context(),
1942 from_file, from_file_size));
1943 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1944
1945 // quota exceeded
1946 {
1947 old_obstacle_file_size = obstacle_file_size;
1948 obstacle_file_size = from_file_size;
1949 expected_total_file_size += obstacle_file_size - old_obstacle_file_size;
1950 scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(
1951 obstacle_file_size - old_obstacle_file_size);
1952 helper->context()->set_allowed_bytes_growth(
1953 helper->context()->allowed_bytes_growth() - 1);
1954 ASSERT_EQ(base::PLATFORM_FILE_OK,
1955 ofu()->CopyOrMoveFile(
1956 helper->context(),
1957 from_file, obstacle_file, true /* copy */));
1958 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1959 }
1960}
1961
1962TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) {
1963 FileSystemPath from_file(CreatePathFromUTF8("fromfile"));
1964 FileSystemPath obstacle_file(CreatePathFromUTF8("obstaclefile"));
1965 FileSystemPath to_file(CreatePathFromUTF8("tofile"));
1966 bool created;
1967
1968 int64 expected_total_file_size = 0;
1969 ASSERT_EQ(base::PLATFORM_FILE_OK,
1970 ofu()->EnsureFileExists(
1971 AllowUsageIncrease(PathCost(from_file))->context(),
1972 from_file, &created));
1973 ASSERT_TRUE(created);
1974 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1975
1976 int64 from_file_size = 1020;
1977 expected_total_file_size += from_file_size;
1978 ASSERT_EQ(base::PLATFORM_FILE_OK,
1979 ofu()->Truncate(
1980 AllowUsageIncrease(from_file_size)->context(),
1981 from_file, from_file_size));
1982 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1983
1984 int64 to_file_size ALLOW_UNUSED = from_file_size;
1985 from_file_size = 0;
1986 ASSERT_EQ(base::PLATFORM_FILE_OK,
1987 ofu()->CopyOrMoveFile(
1988 AllowUsageIncrease(-PathCost(from_file) +
1989 PathCost(to_file))->context(),
1990 from_file, to_file, false /* move */));
1991 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1992
1993 ASSERT_EQ(base::PLATFORM_FILE_OK,
1994 ofu()->EnsureFileExists(
1995 AllowUsageIncrease(PathCost(from_file))->context(),
1996 from_file, &created));
1997 ASSERT_TRUE(created);
1998 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
1999
2000 ASSERT_EQ(base::PLATFORM_FILE_OK,
2001 ofu()->EnsureFileExists(
2002 AllowUsageIncrease(PathCost(obstacle_file))->context(),
2003 obstacle_file, &created));
2004 ASSERT_TRUE(created);
2005 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2006
2007 from_file_size = 1020;
2008 expected_total_file_size += from_file_size;
2009 ASSERT_EQ(base::PLATFORM_FILE_OK,
2010 ofu()->Truncate(
2011 AllowUsageIncrease(from_file_size)->context(),
2012 from_file, from_file_size));
2013 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2014
2015 int64 obstacle_file_size = 1;
2016 expected_total_file_size += obstacle_file_size;
2017 ASSERT_EQ(base::PLATFORM_FILE_OK,
2018 ofu()->Truncate(
2019 AllowUsageIncrease(1)->context(),
2020 obstacle_file, obstacle_file_size));
2021 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2022
2023 int64 old_obstacle_file_size = obstacle_file_size;
2024 obstacle_file_size = from_file_size;
2025 from_file_size = 0;
2026 expected_total_file_size -= old_obstacle_file_size;
2027 ASSERT_EQ(base::PLATFORM_FILE_OK,
2028 ofu()->CopyOrMoveFile(
2029 AllowUsageIncrease(
2030 -old_obstacle_file_size - PathCost(from_file))->context(),
2031 from_file, obstacle_file,
2032 false /* move */));
2033 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2034
2035 ASSERT_EQ(base::PLATFORM_FILE_OK,
2036 ofu()->EnsureFileExists(
2037 AllowUsageIncrease(PathCost(from_file))->context(),
2038 from_file, &created));
2039 ASSERT_TRUE(created);
2040 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2041
2042 from_file_size = 10;
2043 expected_total_file_size += from_file_size;
2044 ASSERT_EQ(base::PLATFORM_FILE_OK,
2045 ofu()->Truncate(
2046 AllowUsageIncrease(from_file_size)->context(),
2047 from_file, from_file_size));
2048 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2049
2050 // quota exceeded even after operation
2051 old_obstacle_file_size = obstacle_file_size;
2052 obstacle_file_size = from_file_size;
2053 from_file_size = 0;
2054 expected_total_file_size -= old_obstacle_file_size;
2055 scoped_ptr<FileSystemOperationContext> context =
2056 LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1);
2057 ASSERT_EQ(base::PLATFORM_FILE_OK,
2058 ofu()->CopyOrMoveFile(
2059 context.get(), from_file, obstacle_file, false /* move */));
2060 ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize());
2061 context.reset();
2062}
2063
2064TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) {
2065 FileSystemPath dir(CreatePathFromUTF8("dir"));
2066 FileSystemPath file(CreatePathFromUTF8("file"));
2067 FileSystemPath dfile1(CreatePathFromUTF8("dir/dfile1"));
2068 FileSystemPath dfile2(CreatePathFromUTF8("dir/dfile2"));
2069 bool created;
2070
2071 ASSERT_EQ(base::PLATFORM_FILE_OK,
2072 ofu()->EnsureFileExists(
2073 AllowUsageIncrease(PathCost(file))->context(),
2074 file, &created));
2075 ASSERT_TRUE(created);
2076 ASSERT_EQ(0, ComputeTotalFileSize());
2077
2078 ASSERT_EQ(base::PLATFORM_FILE_OK,
2079 ofu()->CreateDirectory(
2080 AllowUsageIncrease(PathCost(dir))->context(),
2081 dir, false, false));
2082 ASSERT_EQ(0, ComputeTotalFileSize());
2083
2084 ASSERT_EQ(base::PLATFORM_FILE_OK,
2085 ofu()->EnsureFileExists(
2086 AllowUsageIncrease(PathCost(dfile1))->context(),
2087 dfile1, &created));
2088 ASSERT_TRUE(created);
2089 ASSERT_EQ(0, ComputeTotalFileSize());
2090
2091 ASSERT_EQ(base::PLATFORM_FILE_OK,
2092 ofu()->EnsureFileExists(
2093 AllowUsageIncrease(PathCost(dfile2))->context(),
2094 dfile2, &created));
2095 ASSERT_TRUE(created);
2096 ASSERT_EQ(0, ComputeTotalFileSize());
2097
2098 ASSERT_EQ(base::PLATFORM_FILE_OK,
2099 ofu()->Truncate(
2100 AllowUsageIncrease(340)->context(),
2101 file, 340));
2102 ASSERT_EQ(340, ComputeTotalFileSize());
2103
2104 ASSERT_EQ(base::PLATFORM_FILE_OK,
2105 ofu()->Truncate(
2106 AllowUsageIncrease(1020)->context(),
2107 dfile1, 1020));
2108 ASSERT_EQ(1360, ComputeTotalFileSize());
2109
2110 ASSERT_EQ(base::PLATFORM_FILE_OK,
2111 ofu()->Truncate(
2112 AllowUsageIncrease(120)->context(),
2113 dfile2, 120));
2114 ASSERT_EQ(1480, ComputeTotalFileSize());
2115
2116 ASSERT_EQ(base::PLATFORM_FILE_OK,
2117 ofu()->DeleteFile(
2118 AllowUsageIncrease(-PathCost(file) - 340)->context(),
2119 file));
2120 ASSERT_EQ(1140, ComputeTotalFileSize());
2121
2122 ASSERT_EQ(base::PLATFORM_FILE_OK,
2123 FileUtilHelper::Delete(
2124 AllowUsageIncrease(-PathCost(dir) -
2125 PathCost(dfile1) -
2126 PathCost(dfile2) -
2127 1020 - 120)->context(),
2128 ofu(), dir, true));
2129 ASSERT_EQ(0, ComputeTotalFileSize());
2130}