[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 5 | #include <algorithm> |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 6 | #include <set> |
| 7 | #include <string> |
| 8 | |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 10 | #include "base/file_path.h" |
| 11 | #include "base/file_util.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 13 | #include "base/message_loop.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 14 | #include "base/platform_file.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 15 | #include "base/scoped_temp_dir.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 16 | #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] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 20 | #include "webkit/fileapi/file_system_test_helper.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 21 | #include "webkit/fileapi/file_system_usage_cache.h" |
[email protected] | aa437fa | 2012-03-03 02:09:07 | [diff] [blame] | 22 | #include "webkit/fileapi/file_util_helper.h" |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 23 | #include "webkit/fileapi/mock_file_system_options.h" |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 24 | #include "webkit/fileapi/obfuscated_file_util.h" |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 25 | #include "webkit/fileapi/test_file_set.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 26 | #include "webkit/quota/mock_special_storage_policy.h" |
| 27 | #include "webkit/quota/quota_manager.h" |
| 28 | #include "webkit/quota/quota_types.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 29 | |
| 30 | using namespace fileapi; |
| 31 | |
| 32 | namespace { |
| 33 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 34 | bool FileExists(const FilePath& path) { |
| 35 | return file_util::PathExists(path) && !file_util::DirectoryExists(path); |
| 36 | } |
| 37 | |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 38 | int64 GetSize(const FilePath& path) { |
| 39 | int64 size; |
| 40 | EXPECT_TRUE(file_util::GetFileSize(path, &size)); |
| 41 | return size; |
| 42 | } |
| 43 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 44 | // After a move, the dest exists and the source doesn't. |
| 45 | // After a copy, both source and dest exist. |
| 46 | struct 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 | |
| 53 | const 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] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 76 | struct OriginEnumerationTestRecord { |
| 77 | std::string origin_url; |
| 78 | bool has_temporary; |
| 79 | bool has_persistent; |
| 80 | }; |
| 81 | |
| 82 | const 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] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 90 | } // 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] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 96 | class ObfuscatedFileUtilTest : public testing::Test { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 97 | public: |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 98 | ObfuscatedFileUtilTest() |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 99 | : origin_(GURL("http://www.example.com")), |
| 100 | type_(kFileSystemTypeTemporary), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 101 | weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 102 | test_helper_(origin_, type_), |
| 103 | quota_status_(quota::kQuotaStatusUnknown), |
| 104 | usage_(-1) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void SetUp() { |
| 108 | ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 109 | |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 110 | scoped_refptr<quota::SpecialStoragePolicy> storage_policy = |
| 111 | new quota::MockSpecialStoragePolicy(); |
| 112 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 113 | quota_manager_ = new quota::QuotaManager( |
| 114 | false /* is_incognito */, |
| 115 | data_dir_.path(), |
| 116 | base::MessageLoopProxy::current(), |
| 117 | base::MessageLoopProxy::current(), |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 118 | storage_policy); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 119 | |
| 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] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 122 | // another OFU. We need to pass in the context to skip all that. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 123 | file_system_context_ = new FileSystemContext( |
| 124 | base::MessageLoopProxy::current(), |
| 125 | base::MessageLoopProxy::current(), |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 126 | storage_policy, |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 127 | quota_manager_->proxy(), |
| 128 | data_dir_.path(), |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 129 | CreateAllowFileAccessOptions()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 130 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 131 | obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>( |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 132 | file_system_context_->GetFileUtil(type_)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 133 | |
| 134 | test_helper_.SetUp(file_system_context_.get(), |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 135 | obfuscated_file_util_); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 138 | void TearDown() { |
| 139 | quota_manager_ = NULL; |
| 140 | test_helper_.TearDown(); |
| 141 | } |
| 142 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 143 | 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] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 155 | 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] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 162 | return context; |
| 163 | } |
| 164 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 165 | // This can only be used after SetUp has run and created file_system_context_ |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 166 | // and obfuscated_file_util_. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 167 | // 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] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 175 | obfuscated_file_util_); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 176 | return helper; |
| 177 | } |
| 178 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 179 | ObfuscatedFileUtil* ofu() { |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 180 | return obfuscated_file_util_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 183 | const FilePath& test_directory() const { |
| 184 | return data_dir_.path(); |
| 185 | } |
| 186 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 187 | const GURL& origin() const { |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 188 | return origin_; |
| 189 | } |
| 190 | |
| 191 | fileapi::FileSystemType type() const { |
| 192 | return type_; |
| 193 | } |
| 194 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 195 | int64 ComputeTotalFileSize() { |
| 196 | return test_helper_.ComputeCurrentOriginUsage() - |
| 197 | test_helper_.ComputeCurrentDirectoryDatabaseUsage(); |
| 198 | } |
| 199 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 200 | void GetUsageFromQuotaManager() { |
| 201 | quota_manager_->GetUsageAndQuota( |
| 202 | origin(), test_helper_.storage_type(), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 203 | base::Bind(&ObfuscatedFileUtilTest::OnGetUsage, |
| 204 | weak_factory_.GetWeakPtr())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 205 | MessageLoop::current()->RunAllPending(); |
| 206 | EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); |
| 207 | } |
| 208 | |
| 209 | void RevokeUsageCache() { |
| 210 | quota_manager_->ResetUsageTracker(test_helper_.storage_type()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 211 | file_util::Delete(test_helper_.GetUsageCachePath(), false); |
| 212 | } |
| 213 | |
| 214 | int64 SizeByQuotaUtil() { |
| 215 | return test_helper_.GetCachedOriginUsage(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int64 SizeInUsageFile() { |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 219 | return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int64 usage() const { return usage_; } |
| 223 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 224 | FileSystemPath CreatePathFromUTF8(const std::string& path) { |
| 225 | return test_helper_.CreatePathFromUTF8(path); |
| 226 | } |
| 227 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 228 | int64 PathCost(const FileSystemPath& path) { |
| 229 | return ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()); |
| 230 | } |
| 231 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 232 | FileSystemPath CreatePath(const FilePath& path) { |
| 233 | return test_helper_.CreatePath(path); |
| 234 | } |
| 235 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 236 | void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { |
| 237 | EXPECT_EQ(quota::kQuotaStatusOk, status); |
| 238 | quota_status_ = status; |
| 239 | usage_ = usage; |
| 240 | } |
| 241 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 242 | void CheckFileAndCloseHandle( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 243 | const FileSystemPath& virtual_path, PlatformFile file_handle) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 244 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 245 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 246 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 247 | context.get(), virtual_path, &local_path)); |
| 248 | |
| 249 | base::PlatformFileInfo file_info0; |
| 250 | FilePath data_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 251 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 252 | context.get(), virtual_path, &file_info0, &data_path)); |
| 253 | EXPECT_EQ(data_path, local_path); |
| 254 | EXPECT_TRUE(FileExists(data_path)); |
| 255 | EXPECT_EQ(0, GetSize(data_path)); |
| 256 | |
| 257 | const char data[] = "test data"; |
| 258 | const int length = arraysize(data) - 1; |
| 259 | |
| 260 | if (base::kInvalidPlatformFileValue == file_handle) { |
| 261 | bool created = true; |
| 262 | PlatformFileError error; |
| 263 | file_handle = base::CreatePlatformFile( |
| 264 | data_path, |
| 265 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 266 | &created, |
| 267 | &error); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 268 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 269 | ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 270 | EXPECT_FALSE(created); |
| 271 | } |
| 272 | ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); |
| 273 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 274 | |
| 275 | base::PlatformFileInfo file_info1; |
| 276 | EXPECT_EQ(length, GetSize(data_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 277 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 278 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 279 | context.get(), virtual_path, &file_info1, &data_path)); |
| 280 | EXPECT_EQ(data_path, local_path); |
| 281 | |
| 282 | EXPECT_FALSE(file_info0.is_directory); |
| 283 | EXPECT_FALSE(file_info1.is_directory); |
| 284 | EXPECT_FALSE(file_info0.is_symbolic_link); |
| 285 | EXPECT_FALSE(file_info1.is_symbolic_link); |
| 286 | EXPECT_EQ(0, file_info0.size); |
| 287 | EXPECT_EQ(length, file_info1.size); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 288 | EXPECT_LE(file_info0.last_modified, file_info1.last_modified); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 289 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 290 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 291 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 292 | context.get(), virtual_path, length * 2)); |
| 293 | EXPECT_EQ(length * 2, GetSize(data_path)); |
| 294 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 295 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 296 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 297 | context.get(), virtual_path, 0)); |
| 298 | EXPECT_EQ(0, GetSize(data_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void ValidateTestDirectory( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 302 | const FileSystemPath& root_path, |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 303 | const std::set<FilePath::StringType>& files, |
| 304 | const std::set<FilePath::StringType>& directories) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 305 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 306 | std::set<FilePath::StringType>::const_iterator iter; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 307 | for (iter = files.begin(); iter != files.end(); ++iter) { |
| 308 | bool created = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 309 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 310 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 311 | ofu()->EnsureFileExists( |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 312 | context.get(), root_path.Append(*iter), |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 313 | &created)); |
| 314 | ASSERT_FALSE(created); |
| 315 | } |
| 316 | for (iter = directories.begin(); iter != directories.end(); ++iter) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 317 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 318 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 319 | root_path.Append(*iter))); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 323 | class UsageVerifyHelper { |
| 324 | public: |
| 325 | UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context, |
| 326 | FileSystemTestOriginHelper* test_helper, |
| 327 | int64 expected_usage) |
| 328 | : context_(context.Pass()), |
| 329 | test_helper_(test_helper), |
| 330 | expected_usage_(expected_usage) {} |
| 331 | |
| 332 | ~UsageVerifyHelper() { |
| 333 | Check(); |
| 334 | } |
| 335 | |
| 336 | FileSystemOperationContext* context() { |
| 337 | return context_.get(); |
| 338 | } |
| 339 | |
| 340 | private: |
| 341 | void Check() { |
| 342 | ASSERT_EQ(expected_usage_, |
| 343 | test_helper_->GetCachedOriginUsage()); |
| 344 | } |
| 345 | |
| 346 | scoped_ptr<FileSystemOperationContext> context_; |
| 347 | FileSystemTestOriginHelper* test_helper_; |
| 348 | int64 growth_; |
| 349 | int64 expected_usage_; |
| 350 | }; |
| 351 | |
| 352 | scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) { |
| 353 | int64 usage = test_helper_.GetCachedOriginUsage(); |
| 354 | return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper( |
| 355 | LimitedContext(requested_growth), |
| 356 | &test_helper_, usage + requested_growth)); |
| 357 | } |
| 358 | |
| 359 | scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) { |
| 360 | int64 usage = test_helper_.GetCachedOriginUsage(); |
| 361 | return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper( |
| 362 | LimitedContext(requested_growth - 1), &test_helper_, usage)); |
| 363 | } |
| 364 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 365 | void FillTestDirectory( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 366 | const FileSystemPath& root_path, |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 367 | std::set<FilePath::StringType>* files, |
| 368 | std::set<FilePath::StringType>* directories) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 369 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 370 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 371 | std::vector<base::FileUtilProxy::Entry> entries; |
| 372 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 373 | FileUtilHelper::ReadDirectory( |
| 374 | context.get(), ofu(), root_path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 375 | EXPECT_EQ(0UL, entries.size()); |
| 376 | |
| 377 | files->clear(); |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 378 | files->insert(FILE_PATH_LITERAL("first")); |
| 379 | files->insert(FILE_PATH_LITERAL("second")); |
| 380 | files->insert(FILE_PATH_LITERAL("third")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 381 | directories->clear(); |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 382 | directories->insert(FILE_PATH_LITERAL("fourth")); |
| 383 | directories->insert(FILE_PATH_LITERAL("fifth")); |
| 384 | directories->insert(FILE_PATH_LITERAL("sixth")); |
| 385 | std::set<FilePath::StringType>::iterator iter; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 386 | for (iter = files->begin(); iter != files->end(); ++iter) { |
| 387 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 388 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 389 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 390 | ofu()->EnsureFileExists( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 391 | context.get(), |
| 392 | root_path.Append(*iter), |
| 393 | &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 394 | ASSERT_TRUE(created); |
| 395 | } |
| 396 | for (iter = directories->begin(); iter != directories->end(); ++iter) { |
| 397 | bool exclusive = true; |
| 398 | bool recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 399 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 400 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 401 | ofu()->CreateDirectory( |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 402 | context.get(), root_path.Append(*iter), exclusive, recursive)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 403 | } |
| 404 | ValidateTestDirectory(root_path, *files, *directories); |
| 405 | } |
| 406 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 407 | void TestReadDirectoryHelper(const FileSystemPath& root_path) { |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 408 | std::set<FilePath::StringType> files; |
| 409 | std::set<FilePath::StringType> directories; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 410 | FillTestDirectory(root_path, &files, &directories); |
| 411 | |
| 412 | scoped_ptr<FileSystemOperationContext> context; |
| 413 | std::vector<base::FileUtilProxy::Entry> entries; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 414 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 415 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 416 | FileUtilHelper::ReadDirectory( |
| 417 | context.get(), ofu(), root_path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 418 | std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; |
| 419 | EXPECT_EQ(files.size() + directories.size(), entries.size()); |
| 420 | for (entry_iter = entries.begin(); entry_iter != entries.end(); |
| 421 | ++entry_iter) { |
| 422 | const base::FileUtilProxy::Entry& entry = *entry_iter; |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 423 | std::set<FilePath::StringType>::iterator iter = files.find(entry.name); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 424 | if (iter != files.end()) { |
| 425 | EXPECT_FALSE(entry.is_directory); |
| 426 | files.erase(iter); |
| 427 | continue; |
| 428 | } |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 429 | iter = directories.find(entry.name); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 430 | EXPECT_FALSE(directories.end() == iter); |
| 431 | EXPECT_TRUE(entry.is_directory); |
| 432 | directories.erase(iter); |
| 433 | } |
| 434 | } |
| 435 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 436 | void TestTouchHelper(const FileSystemPath& path, bool is_file) { |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 437 | base::Time last_access_time = base::Time::Now(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 438 | base::Time last_modified_time = base::Time::Now(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 439 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 440 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 441 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 442 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 443 | context.get(), path, last_access_time, last_modified_time)); |
| 444 | FilePath local_path; |
| 445 | base::PlatformFileInfo file_info; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 446 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 447 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 448 | context.get(), path, &file_info, &local_path)); |
| 449 | // We compare as time_t here to lower our resolution, to avoid false |
| 450 | // negatives caused by conversion to the local filesystem's native |
| 451 | // representation and back. |
| 452 | EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
| 453 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 454 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 455 | last_modified_time += base::TimeDelta::FromHours(1); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 456 | last_access_time += base::TimeDelta::FromHours(14); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 457 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 458 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 459 | context.get(), path, last_access_time, last_modified_time)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 460 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 461 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 462 | context.get(), path, &file_info, &local_path)); |
| 463 | EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 464 | if (is_file) // Directories in OFU don't support atime. |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 465 | EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 466 | } |
| 467 | |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 468 | void TestCopyInForeignFileHelper(bool overwrite) { |
| 469 | ScopedTempDir source_dir; |
| 470 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 471 | FilePath root_file_path = source_dir.path(); |
| 472 | FilePath src_file_path = root_file_path.AppendASCII("file_name"); |
| 473 | FileSystemPath dest_path = CreatePathFromUTF8("new file"); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 474 | int64 src_file_length = 87; |
| 475 | |
| 476 | base::PlatformFileError error_code; |
| 477 | bool created = false; |
| 478 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 479 | base::PlatformFile file_handle = |
| 480 | base::CreatePlatformFile( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 481 | src_file_path, file_flags, &created, &error_code); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 482 | EXPECT_TRUE(created); |
| 483 | ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 484 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 485 | ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); |
| 486 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 487 | |
| 488 | scoped_ptr<FileSystemOperationContext> context; |
| 489 | |
| 490 | if (overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 491 | context.reset(NewContext(NULL)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 492 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 493 | ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 494 | EXPECT_TRUE(created); |
| 495 | } |
| 496 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 497 | const int64 path_cost = |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 498 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 499 | if (!overwrite) { |
| 500 | // Verify that file creation requires sufficient quota for the path. |
| 501 | context.reset(NewContext(NULL)); |
| 502 | context->set_allowed_bytes_growth(path_cost + src_file_length - 1); |
| 503 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 504 | ofu()->CopyInForeignFile(context.get(), |
| 505 | src_file_path, dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | context.reset(NewContext(NULL)); |
| 509 | context->set_allowed_bytes_growth(path_cost + src_file_length); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 510 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 511 | ofu()->CopyInForeignFile(context.get(), |
| 512 | src_file_path, dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 513 | |
| 514 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 515 | EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 516 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 517 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 518 | context.reset(NewContext(NULL)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 519 | base::PlatformFileInfo file_info; |
| 520 | FilePath data_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 521 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 522 | context.get(), dest_path, &file_info, &data_path)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 523 | EXPECT_NE(data_path, src_file_path); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 524 | EXPECT_TRUE(FileExists(data_path)); |
| 525 | EXPECT_EQ(src_file_length, GetSize(data_path)); |
| 526 | |
| 527 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 528 | ofu()->DeleteFile(context.get(), dest_path)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 529 | } |
| 530 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 531 | void ClearTimestamp(const FileSystemPath& path) { |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 532 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 533 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 534 | ofu()->Touch(context.get(), path, base::Time(), base::Time())); |
| 535 | EXPECT_EQ(base::Time(), GetModifiedTime(path)); |
| 536 | } |
| 537 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 538 | base::Time GetModifiedTime(const FileSystemPath& path) { |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 539 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 540 | FilePath data_path; |
| 541 | base::PlatformFileInfo file_info; |
| 542 | context.reset(NewContext(NULL)); |
| 543 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 544 | ofu()->GetFileInfo(context.get(), path, &file_info, &data_path)); |
| 545 | return file_info.last_modified; |
| 546 | } |
| 547 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 548 | void TestDirectoryTimestampHelper(const FileSystemPath& base_dir, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 549 | bool copy, |
| 550 | bool overwrite) { |
| 551 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 552 | const FileSystemPath src_dir_path(base_dir.AppendASCII("foo_dir")); |
| 553 | const FileSystemPath dest_dir_path(base_dir.AppendASCII("bar_dir")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 554 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 555 | const FileSystemPath src_file_path(src_dir_path.AppendASCII("hoge")); |
| 556 | const FileSystemPath dest_file_path(dest_dir_path.AppendASCII("fuga")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 557 | |
| 558 | context.reset(NewContext(NULL)); |
| 559 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 560 | ofu()->CreateDirectory(context.get(), src_dir_path, true, true)); |
| 561 | context.reset(NewContext(NULL)); |
| 562 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 563 | ofu()->CreateDirectory(context.get(), dest_dir_path, true, true)); |
| 564 | |
| 565 | bool created = false; |
| 566 | context.reset(NewContext(NULL)); |
| 567 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 568 | ofu()->EnsureFileExists(context.get(), src_file_path, &created)); |
| 569 | if (overwrite) { |
| 570 | context.reset(NewContext(NULL)); |
| 571 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 572 | ofu()->EnsureFileExists(context.get(), |
| 573 | dest_file_path, &created)); |
| 574 | } |
| 575 | |
| 576 | ClearTimestamp(src_dir_path); |
| 577 | ClearTimestamp(dest_dir_path); |
| 578 | context.reset(NewContext(NULL)); |
| 579 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 580 | ofu()->CopyOrMoveFile(context.get(), |
| 581 | src_file_path, dest_file_path, |
| 582 | copy)); |
| 583 | |
| 584 | if (copy) |
| 585 | EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path)); |
| 586 | else |
| 587 | EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path)); |
| 588 | EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path)); |
| 589 | } |
| 590 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 591 | int64 ComputeCurrentUsage() { |
| 592 | return test_helper().ComputeCurrentOriginUsage() - |
| 593 | test_helper().ComputeCurrentDirectoryDatabaseUsage(); |
| 594 | } |
| 595 | |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 596 | const FileSystemTestOriginHelper& test_helper() const { return test_helper_; } |
| 597 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 598 | private: |
| 599 | ScopedTempDir data_dir_; |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 600 | ObfuscatedFileUtil* obfuscated_file_util_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 601 | scoped_refptr<quota::QuotaManager> quota_manager_; |
| 602 | scoped_refptr<FileSystemContext> file_system_context_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 603 | GURL origin_; |
| 604 | fileapi::FileSystemType type_; |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 605 | base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 606 | FileSystemTestOriginHelper test_helper_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 607 | quota::QuotaStatusCode quota_status_; |
| 608 | int64 usage_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 609 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 610 | DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 611 | }; |
| 612 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 613 | TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 614 | base::PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 615 | bool created; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 616 | FileSystemPath path = CreatePathFromUTF8("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 617 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 618 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 619 | |
| 620 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 621 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 622 | context.get(), path, file_flags, &file_handle, |
| 623 | &created)); |
| 624 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 625 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 626 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 627 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 628 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 629 | path = CreatePathFromUTF8("test file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 630 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 631 | // Verify that file creation requires sufficient quota for the path. |
| 632 | context.reset(NewContext(NULL)); |
| 633 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 634 | ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 635 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 636 | ofu()->CreateOrOpen( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 637 | context.get(), path, file_flags, &file_handle, &created)); |
| 638 | |
| 639 | context.reset(NewContext(NULL)); |
| 640 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 641 | ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 642 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 643 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 644 | context.get(), path, file_flags, &file_handle, &created)); |
| 645 | ASSERT_TRUE(created); |
| 646 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 647 | |
| 648 | CheckFileAndCloseHandle(path, file_handle); |
| 649 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 650 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 651 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 652 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 653 | context.get(), path, &local_path)); |
| 654 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 655 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 656 | // Verify that deleting a file isn't stopped by zero quota, and that it frees |
| 657 | // up quote from its path. |
| 658 | context.reset(NewContext(NULL)); |
| 659 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 660 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 661 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 662 | EXPECT_FALSE(file_util::PathExists(local_path)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 663 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 664 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 665 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 666 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 667 | bool exclusive = true; |
| 668 | bool recursive = true; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 669 | FileSystemPath directory_path = CreatePathFromUTF8( |
| 670 | "series/of/directories"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 671 | path = directory_path.AppendASCII("file name"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 672 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 673 | context.get(), directory_path, exclusive, recursive)); |
| 674 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 675 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 676 | file_handle = base::kInvalidPlatformFileValue; |
| 677 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 678 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 679 | context.get(), path, file_flags, &file_handle, &created)); |
| 680 | ASSERT_TRUE(created); |
| 681 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 682 | |
| 683 | CheckFileAndCloseHandle(path, file_handle); |
| 684 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 685 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 686 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 687 | context.get(), path, &local_path)); |
| 688 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 689 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 690 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 691 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 692 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 693 | EXPECT_FALSE(file_util::PathExists(local_path)); |
| 694 | } |
| 695 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 696 | TEST_F(ObfuscatedFileUtilTest, TestTruncate) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 697 | bool created = false; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 698 | FileSystemPath path = CreatePathFromUTF8("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 699 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 700 | |
| 701 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 702 | ofu()->Truncate(context.get(), path, 4)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 703 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 704 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 705 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 706 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 707 | ASSERT_TRUE(created); |
| 708 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 709 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 710 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 711 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 712 | context.get(), path, &local_path)); |
| 713 | EXPECT_EQ(0, GetSize(local_path)); |
| 714 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 715 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 716 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 717 | context.get(), path, 10)); |
| 718 | EXPECT_EQ(10, GetSize(local_path)); |
| 719 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 720 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 721 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 722 | context.get(), path, 1)); |
| 723 | EXPECT_EQ(1, GetSize(local_path)); |
| 724 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 725 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 726 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 727 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 728 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 729 | } |
| 730 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 731 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) { |
| 732 | bool created = false; |
| 733 | FileSystemPath path = CreatePathFromUTF8("file"); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 734 | |
| 735 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 736 | ofu()->EnsureFileExists( |
| 737 | AllowUsageIncrease(PathCost(path))->context(), |
| 738 | path, &created)); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 739 | ASSERT_TRUE(created); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 740 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 741 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 742 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 743 | ofu()->Truncate( |
| 744 | AllowUsageIncrease(1020)->context(), |
| 745 | path, 1020)); |
| 746 | ASSERT_EQ(1020, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 747 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 748 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 749 | ofu()->Truncate( |
| 750 | AllowUsageIncrease(-1020)->context(), |
| 751 | path, 0)); |
| 752 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 753 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 754 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 755 | ofu()->Truncate( |
| 756 | DisallowUsageIncrease(1021)->context(), |
| 757 | path, 1021)); |
| 758 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 759 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 760 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 761 | ofu()->Truncate( |
| 762 | AllowUsageIncrease(1020)->context(), |
| 763 | path, 1020)); |
| 764 | ASSERT_EQ(1020, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 765 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 766 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 767 | ofu()->Truncate( |
| 768 | AllowUsageIncrease(0)->context(), |
| 769 | path, 1020)); |
| 770 | ASSERT_EQ(1020, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 771 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 772 | // quota exceeded |
| 773 | { |
| 774 | scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1); |
| 775 | helper->context()->set_allowed_bytes_growth( |
| 776 | helper->context()->allowed_bytes_growth() - 1); |
| 777 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 778 | ofu()->Truncate(helper->context(), path, 1019)); |
| 779 | ASSERT_EQ(1019, ComputeTotalFileSize()); |
| 780 | } |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 781 | |
| 782 | // Delete backing file to make following truncation fail. |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 783 | FilePath local_path; |
| 784 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 785 | ofu()->GetLocalFilePath( |
| 786 | UnlimitedContext().get(), |
| 787 | path, &local_path)); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 788 | ASSERT_FALSE(local_path.empty()); |
| 789 | ASSERT_TRUE(file_util::Delete(local_path, false)); |
| 790 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 791 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 792 | ofu()->Truncate( |
| 793 | LimitedContext(1234).get(), |
| 794 | path, 1234)); |
| 795 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 796 | } |
| 797 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 798 | TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 799 | FileSystemPath path = CreatePathFromUTF8("fake/file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 800 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 801 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 802 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 803 | ofu()->EnsureFileExists( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 804 | context.get(), path, &created)); |
| 805 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 806 | // Verify that file creation requires sufficient quota for the path. |
| 807 | context.reset(NewContext(NULL)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 808 | path = CreatePathFromUTF8("test file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 809 | created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 810 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 811 | ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 812 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 813 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 814 | ASSERT_FALSE(created); |
| 815 | |
| 816 | context.reset(NewContext(NULL)); |
| 817 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 818 | ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 819 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 820 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 821 | ASSERT_TRUE(created); |
| 822 | |
| 823 | CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); |
| 824 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 825 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 826 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 827 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 828 | ASSERT_FALSE(created); |
| 829 | |
| 830 | // Also test in a subdirectory. |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 831 | path = CreatePathFromUTF8("path/to/file.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 832 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 833 | bool exclusive = true; |
| 834 | bool recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 835 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 836 | context.get(), path.DirName(), exclusive, recursive)); |
| 837 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 838 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 839 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 840 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 841 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 842 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 843 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 844 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 845 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 846 | } |
| 847 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 848 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 849 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 850 | |
| 851 | bool exclusive = false; |
| 852 | bool recursive = false; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 853 | FileSystemPath path = CreatePathFromUTF8("foo/bar"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 854 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 855 | context.get(), path, exclusive, recursive)); |
| 856 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 857 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 858 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 859 | ofu()->DeleteSingleDirectory(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 860 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 861 | FileSystemPath root = CreatePathFromUTF8(""); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 862 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 863 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 864 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 865 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 866 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 867 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 868 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 869 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 870 | exclusive = false; |
| 871 | recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 872 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 873 | context.get(), path, exclusive, recursive)); |
| 874 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 875 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 876 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 877 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 878 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 879 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 880 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 881 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 882 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 883 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 884 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 885 | |
| 886 | // Can't remove a non-empty directory. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 887 | context.reset(NewContext(NULL)); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 888 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 889 | ofu()->DeleteSingleDirectory(context.get(), path.DirName())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 890 | |
| 891 | base::PlatformFileInfo file_info; |
| 892 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 893 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 894 | context.get(), path, &file_info, &local_path)); |
| 895 | EXPECT_TRUE(local_path.empty()); |
| 896 | EXPECT_TRUE(file_info.is_directory); |
| 897 | EXPECT_FALSE(file_info.is_symbolic_link); |
| 898 | |
| 899 | // Same create again should succeed, since exclusive is false. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 900 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 901 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 902 | context.get(), path, exclusive, recursive)); |
| 903 | |
| 904 | exclusive = true; |
| 905 | recursive = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 906 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 907 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 908 | context.get(), path, exclusive, recursive)); |
| 909 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 910 | // Verify that deleting a directory isn't stopped by zero quota, and that it |
| 911 | // frees up quota from its path. |
| 912 | context.reset(NewContext(NULL)); |
| 913 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 914 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 915 | ofu()->DeleteSingleDirectory(context.get(), path)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 916 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 917 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 918 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 919 | path = CreatePathFromUTF8("foo/bop"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 920 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 921 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 922 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 923 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 924 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 925 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 926 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
| 927 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 928 | context.get(), path, &file_info, &local_path)); |
| 929 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 930 | // Verify that file creation requires sufficient quota for the path. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 931 | exclusive = true; |
| 932 | recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 933 | context.reset(NewContext(NULL)); |
| 934 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 935 | ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path()) - 1); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 936 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 937 | context.get(), path, exclusive, recursive)); |
| 938 | |
| 939 | context.reset(NewContext(NULL)); |
| 940 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 941 | ObfuscatedFileUtil::ComputeFilePathCost(path.internal_path())); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 942 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 943 | context.get(), path, exclusive, recursive)); |
| 944 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 945 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 946 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 947 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 948 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 949 | |
| 950 | exclusive = true; |
| 951 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 952 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 953 | context.get(), path, exclusive, recursive)); |
| 954 | |
| 955 | exclusive = true; |
| 956 | recursive = false; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 957 | path = CreatePathFromUTF8("foo"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 958 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 959 | context.get(), path, exclusive, recursive)); |
| 960 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 961 | path = CreatePathFromUTF8("blah"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 962 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 963 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 964 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 965 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 966 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 967 | |
| 968 | exclusive = true; |
| 969 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 970 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 971 | context.get(), path, exclusive, recursive)); |
| 972 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 973 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 974 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 975 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 976 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 977 | |
| 978 | exclusive = true; |
| 979 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 980 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 981 | context.get(), path, exclusive, recursive)); |
| 982 | } |
| 983 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 984 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 985 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 986 | bool exclusive = true; |
| 987 | bool recursive = true; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 988 | FileSystemPath path = CreatePathFromUTF8("directory/to/use"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 989 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 990 | context.get(), path, exclusive, recursive)); |
| 991 | TestReadDirectoryHelper(path); |
| 992 | } |
| 993 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 994 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 995 | TestReadDirectoryHelper(CreatePathFromUTF8("")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 996 | } |
| 997 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 998 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 999 | TestReadDirectoryHelper(CreatePathFromUTF8("/")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1000 | } |
| 1001 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1002 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1003 | FileSystemPath path = CreatePathFromUTF8("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1004 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1005 | |
| 1006 | bool created = false; |
| 1007 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1008 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1009 | ASSERT_TRUE(created); |
| 1010 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1011 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1012 | std::vector<base::FileUtilProxy::Entry> entries; |
| 1013 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1014 | FileUtilHelper::ReadDirectory( |
| 1015 | context.get(), ofu(), path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1016 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1017 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1018 | } |
| 1019 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1020 | TEST_F(ObfuscatedFileUtilTest, TestTouch) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1021 | FileSystemPath path = CreatePathFromUTF8("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1022 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1023 | |
| 1024 | base::Time last_access_time = base::Time::Now(); |
| 1025 | base::Time last_modified_time = base::Time::Now(); |
| 1026 | |
| 1027 | // It's not there yet. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1028 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1029 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1030 | context.get(), path, last_access_time, last_modified_time)); |
| 1031 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1032 | // OK, now create it. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1033 | context.reset(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1034 | bool created = false; |
| 1035 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1036 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1037 | ASSERT_TRUE(created); |
| 1038 | TestTouchHelper(path, true); |
| 1039 | |
| 1040 | // Now test a directory: |
| 1041 | context.reset(NewContext(NULL)); |
| 1042 | bool exclusive = true; |
| 1043 | bool recursive = false; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1044 | path = CreatePathFromUTF8("dir"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1045 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(), |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1046 | path, exclusive, recursive)); |
| 1047 | TestTouchHelper(path, false); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1048 | } |
| 1049 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1050 | TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1051 | FileSystemPath path = CreatePathFromUTF8("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1052 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1053 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1054 | path = CreatePathFromUTF8("file name"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1055 | context->set_allowed_bytes_growth(5); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1056 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1057 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1058 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1059 | EXPECT_FALSE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1060 | context->set_allowed_bytes_growth(1024); |
| 1061 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1062 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1063 | EXPECT_TRUE(created); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1064 | int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost( |
| 1065 | path.internal_path()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1066 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
| 1067 | |
| 1068 | context->set_allowed_bytes_growth(1024); |
| 1069 | bool exclusive = true; |
| 1070 | bool recursive = true; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1071 | path = CreatePathFromUTF8("directory/to/use"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1072 | std::vector<FilePath::StringType> components; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1073 | path.internal_path().GetComponents(&components); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1074 | path_cost = 0; |
| 1075 | for (std::vector<FilePath::StringType>::iterator iter = components.begin(); |
| 1076 | iter != components.end(); ++iter) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1077 | path_cost += ObfuscatedFileUtil::ComputeFilePathCost( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1078 | FilePath(*iter)); |
| 1079 | } |
| 1080 | context.reset(NewContext(NULL)); |
| 1081 | context->set_allowed_bytes_growth(1024); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1082 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1083 | context.get(), path, exclusive, recursive)); |
| 1084 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1085 | } |
| 1086 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1087 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1088 | FileSystemPath source_path = CreatePathFromUTF8("path0.txt"); |
| 1089 | FileSystemPath dest_path = CreatePathFromUTF8("path1.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1090 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1091 | |
| 1092 | bool is_copy_not_move = false; |
| 1093 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1094 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1095 | is_copy_not_move)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1096 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1097 | is_copy_not_move = true; |
| 1098 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1099 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1100 | is_copy_not_move)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1101 | source_path = CreatePathFromUTF8("dir/dir/file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1102 | bool exclusive = true; |
| 1103 | bool recursive = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1104 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1105 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1106 | context.get(), source_path.DirName(), exclusive, recursive)); |
| 1107 | is_copy_not_move = false; |
| 1108 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1109 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1110 | is_copy_not_move)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1111 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1112 | is_copy_not_move = true; |
| 1113 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1114 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1115 | is_copy_not_move)); |
| 1116 | } |
| 1117 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1118 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1119 | const int64 kSourceLength = 5; |
| 1120 | const int64 kDestLength = 50; |
| 1121 | |
| 1122 | for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { |
| 1123 | SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); |
| 1124 | const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; |
| 1125 | SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << |
| 1126 | test_case.is_copy_not_move); |
| 1127 | SCOPED_TRACE(testing::Message() << "\t source_path " << |
| 1128 | test_case.source_path); |
| 1129 | SCOPED_TRACE(testing::Message() << "\t dest_path " << |
| 1130 | test_case.dest_path); |
| 1131 | SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << |
| 1132 | test_case.cause_overwrite); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1133 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1134 | |
| 1135 | bool exclusive = false; |
| 1136 | bool recursive = true; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1137 | FileSystemPath source_path = CreatePathFromUTF8(test_case.source_path); |
| 1138 | FileSystemPath dest_path = CreatePathFromUTF8(test_case.dest_path); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1139 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1140 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1141 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1142 | context.get(), source_path.DirName(), exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1143 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1144 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1145 | context.get(), dest_path.DirName(), exclusive, recursive)); |
| 1146 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1147 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1148 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1149 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1150 | ofu()->EnsureFileExists(context.get(), source_path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1151 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1152 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1153 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1154 | ofu()->Truncate(context.get(), source_path, kSourceLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1155 | |
| 1156 | if (test_case.cause_overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1157 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1158 | created = false; |
| 1159 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1160 | ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1161 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1162 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1163 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1164 | ofu()->Truncate(context.get(), dest_path, kDestLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1165 | } |
| 1166 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1167 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1168 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(), |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1169 | source_path, dest_path, test_case.is_copy_not_move)); |
| 1170 | if (test_case.is_copy_not_move) { |
| 1171 | base::PlatformFileInfo file_info; |
| 1172 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1173 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1174 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1175 | context.get(), source_path, &file_info, &local_path)); |
| 1176 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1177 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1178 | ofu()->DeleteFile(context.get(), source_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1179 | } else { |
| 1180 | base::PlatformFileInfo file_info; |
| 1181 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1182 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1183 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1184 | context.get(), source_path, &file_info, &local_path)); |
| 1185 | } |
| 1186 | base::PlatformFileInfo file_info; |
| 1187 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1188 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1189 | context.get(), dest_path, &file_info, &local_path)); |
| 1190 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1191 | |
| 1192 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1193 | ofu()->DeleteFile(context.get(), dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1197 | TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1198 | FileSystemPath src_path = CreatePathFromUTF8("src path"); |
| 1199 | FileSystemPath dest_path = CreatePathFromUTF8("destination path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1200 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1201 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1202 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1203 | context.get(), src_path, &created)); |
| 1204 | |
| 1205 | bool is_copy = true; |
| 1206 | // Copy, no overwrite. |
| 1207 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1208 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1209 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1210 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1211 | context.reset(NewContext(NULL)); |
| 1212 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1213 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1214 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1215 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1216 | |
| 1217 | // Copy, with overwrite. |
| 1218 | context.reset(NewContext(NULL)); |
| 1219 | context->set_allowed_bytes_growth(0); |
| 1220 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1221 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1222 | } |
| 1223 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1224 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1225 | FileSystemPath src_path = CreatePathFromUTF8("src path"); |
| 1226 | FileSystemPath dest_path = CreatePathFromUTF8("destination path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1227 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1228 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1229 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1230 | context.get(), src_path, &created)); |
| 1231 | |
| 1232 | bool is_copy = false; |
| 1233 | // Move, rename, no overwrite. |
| 1234 | context.reset(NewContext(NULL)); |
| 1235 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1236 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) - |
| 1237 | ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1238 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1239 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1240 | context.reset(NewContext(NULL)); |
| 1241 | context->set_allowed_bytes_growth( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1242 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path.internal_path()) - |
| 1243 | ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1244 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1245 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1246 | |
| 1247 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1248 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1249 | context.get(), src_path, &created)); |
| 1250 | |
| 1251 | // Move, rename, with overwrite. |
| 1252 | context.reset(NewContext(NULL)); |
| 1253 | context->set_allowed_bytes_growth(0); |
| 1254 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1255 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1256 | } |
| 1257 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1258 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1259 | FileSystemPath src_path = CreatePathFromUTF8("src path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1260 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1261 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1262 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1263 | context.get(), src_path, &created)); |
| 1264 | |
| 1265 | bool exclusive = true; |
| 1266 | bool recursive = false; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1267 | FileSystemPath dir_path = CreatePathFromUTF8("directory path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1268 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1269 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1270 | context.get(), dir_path, exclusive, recursive)); |
| 1271 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1272 | FileSystemPath dest_path = dir_path.Append(src_path.internal_path()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1273 | |
| 1274 | bool is_copy = false; |
| 1275 | int64 allowed_bytes_growth = -1000; // Over quota, this should still work. |
| 1276 | // Move, no rename, no overwrite. |
| 1277 | context.reset(NewContext(NULL)); |
| 1278 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1279 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1280 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1281 | EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); |
| 1282 | |
| 1283 | // Move, no rename, with overwrite. |
| 1284 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1285 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1286 | context.get(), src_path, &created)); |
| 1287 | context.reset(NewContext(NULL)); |
| 1288 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1289 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1290 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1291 | EXPECT_EQ( |
| 1292 | allowed_bytes_growth + |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1293 | ObfuscatedFileUtil::ComputeFilePathCost(src_path.internal_path()), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1294 | context->allowed_bytes_growth()); |
| 1295 | } |
| 1296 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1297 | TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) { |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 1298 | TestCopyInForeignFileHelper(false /* overwrite */); |
| 1299 | TestCopyInForeignFileHelper(true /* overwrite */); |
| 1300 | } |
| 1301 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1302 | TEST_F(ObfuscatedFileUtilTest, TestEnumerator) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1303 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1304 | FileSystemPath src_path = CreatePathFromUTF8("source dir"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1305 | bool exclusive = true; |
| 1306 | bool recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1307 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1308 | context.get(), src_path, exclusive, recursive)); |
| 1309 | |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 1310 | std::set<FilePath::StringType> files; |
| 1311 | std::set<FilePath::StringType> directories; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1312 | FillTestDirectory(src_path, &files, &directories); |
| 1313 | |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1314 | FileSystemPath dest_path = CreatePathFromUTF8("destination dir"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1315 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1316 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1317 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1318 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1319 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 1320 | test_helper().SameFileUtilCopy(context.get(), src_path, dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1321 | |
| 1322 | ValidateTestDirectory(dest_path, files, directories); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1323 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1324 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1325 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1326 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1327 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1328 | recursive = true; |
| 1329 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | aa437fa | 2012-03-03 02:09:07 | [diff] [blame] | 1330 | FileUtilHelper::Delete(context.get(), ofu(), |
| 1331 | dest_path, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1332 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1333 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1334 | } |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1335 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1336 | TEST_F(ObfuscatedFileUtilTest, TestMigration) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1337 | ScopedTempDir source_dir; |
| 1338 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 1339 | FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); |
| 1340 | ASSERT_TRUE(file_util::CreateDirectory(root_path)); |
| 1341 | |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1342 | test::SetUpRegularTestCases(root_path); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1343 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1344 | EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1345 | |
| 1346 | FilePath new_root = |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1347 | test_directory().AppendASCII("File System").AppendASCII("000").Append( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1348 | ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1349 | for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1350 | SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1351 | const test::TestCaseRecord& test_case = test::kRegularTestCases[i]; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1352 | FilePath local_data_path = new_root.Append(test_case.path); |
[email protected] | d9034ed2 | 2012-02-10 02:04:40 | [diff] [blame] | 1353 | local_data_path = local_data_path.NormalizePathSeparators(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1354 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1355 | base::PlatformFileInfo ofu_file_info; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1356 | FilePath data_path; |
| 1357 | SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); |
| 1358 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1359 | ofu()->GetFileInfo(context.get(), CreatePath(FilePath(test_case.path)), |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1360 | &ofu_file_info, &data_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1361 | if (test_case.is_directory) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1362 | EXPECT_TRUE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1363 | } else { |
| 1364 | base::PlatformFileInfo platform_file_info; |
| 1365 | SCOPED_TRACE(testing::Message() << "local_data_path is " << |
| 1366 | local_data_path.value()); |
| 1367 | SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); |
| 1368 | ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); |
| 1369 | EXPECT_EQ(test_case.data_file_size, platform_file_info.size); |
| 1370 | EXPECT_FALSE(platform_file_info.is_directory); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1371 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1372 | EXPECT_EQ(local_data_path, data_path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1373 | EXPECT_EQ(platform_file_info.size, ofu_file_info.size); |
| 1374 | EXPECT_FALSE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1378 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1379 | TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) { |
| 1380 | scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> |
| 1381 | enumerator(ofu()->CreateOriginEnumerator()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1382 | // The test helper starts out with a single filesystem. |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1383 | EXPECT_TRUE(enumerator.get()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1384 | EXPECT_EQ(origin(), enumerator->Next()); |
| 1385 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1386 | EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1387 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1388 | EXPECT_EQ(GURL(), enumerator->Next()); |
| 1389 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1390 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1391 | |
| 1392 | std::set<GURL> origins_expected; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1393 | origins_expected.insert(origin()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1394 | |
| 1395 | for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { |
| 1396 | SCOPED_TRACE(testing::Message() << |
| 1397 | "Validating kOriginEnumerationTestRecords " << i); |
| 1398 | const OriginEnumerationTestRecord& record = |
| 1399 | kOriginEnumerationTestRecords[i]; |
| 1400 | GURL origin_url(record.origin_url); |
| 1401 | origins_expected.insert(origin_url); |
| 1402 | if (record.has_temporary) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1403 | scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1404 | NewHelper(origin_url, kFileSystemTypeTemporary)); |
| 1405 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1406 | bool created = false; |
| 1407 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1408 | ofu()->EnsureFileExists( |
| 1409 | context.get(), |
| 1410 | helper->CreatePathFromUTF8("file"), |
| 1411 | &created)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1412 | EXPECT_TRUE(created); |
| 1413 | } |
| 1414 | if (record.has_persistent) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1415 | scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1416 | NewHelper(origin_url, kFileSystemTypePersistent)); |
| 1417 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1418 | bool created = false; |
| 1419 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1420 | ofu()->EnsureFileExists( |
| 1421 | context.get(), |
| 1422 | helper->CreatePathFromUTF8("file"), |
| 1423 | &created)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1424 | EXPECT_TRUE(created); |
| 1425 | } |
| 1426 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1427 | enumerator.reset(ofu()->CreateOriginEnumerator()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1428 | EXPECT_TRUE(enumerator.get()); |
| 1429 | std::set<GURL> origins_found; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1430 | GURL origin_url; |
| 1431 | while (!(origin_url = enumerator->Next()).is_empty()) { |
| 1432 | origins_found.insert(origin_url); |
| 1433 | SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1434 | bool found = false; |
| 1435 | for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); |
| 1436 | ++i) { |
| 1437 | const OriginEnumerationTestRecord& record = |
| 1438 | kOriginEnumerationTestRecords[i]; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1439 | if (GURL(record.origin_url) != origin_url) |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1440 | continue; |
| 1441 | found = true; |
| 1442 | EXPECT_EQ(record.has_temporary, |
| 1443 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1444 | EXPECT_EQ(record.has_persistent, |
| 1445 | enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1446 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1447 | // Deal with the default filesystem created by the test helper. |
| 1448 | if (!found && origin_url == origin()) { |
| 1449 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1450 | EXPECT_EQ(true, |
| 1451 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
[email protected] | 34161bb | 2011-10-13 12:36:18 | [diff] [blame] | 1452 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1453 | found = true; |
| 1454 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1455 | EXPECT_TRUE(found); |
| 1456 | } |
| 1457 | |
| 1458 | std::set<GURL> diff; |
| 1459 | std::set_symmetric_difference(origins_expected.begin(), |
| 1460 | origins_expected.end(), origins_found.begin(), origins_found.end(), |
| 1461 | inserter(diff, diff.begin())); |
| 1462 | EXPECT_TRUE(diff.empty()); |
| 1463 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1464 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1465 | TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1466 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1467 | |
| 1468 | int64 expected_quota = 0; |
| 1469 | |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1470 | for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1471 | SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1472 | const test::TestCaseRecord& test_case = test::kRegularTestCases[i]; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1473 | FilePath file_path(test_case.path); |
| 1474 | expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1475 | if (test_case.is_directory) { |
| 1476 | bool exclusive = true; |
| 1477 | bool recursive = false; |
| 1478 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1479 | ofu()->CreateDirectory(context.get(), CreatePath(file_path), |
| 1480 | exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1481 | } else { |
| 1482 | bool created = false; |
| 1483 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1484 | ofu()->EnsureFileExists(context.get(), CreatePath(file_path), |
| 1485 | &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1486 | ASSERT_TRUE(created); |
| 1487 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1488 | ofu()->Truncate(context.get(), |
| 1489 | CreatePath(file_path), |
| 1490 | test_case.data_file_size)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1491 | expected_quota += test_case.data_file_size; |
| 1492 | } |
| 1493 | } |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1494 | |
| 1495 | // Usually raw size in usage cache and the usage returned by QuotaUtil |
| 1496 | // should be same. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1497 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1498 | EXPECT_EQ(expected_quota, SizeByQuotaUtil()); |
| 1499 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1500 | RevokeUsageCache(); |
| 1501 | EXPECT_EQ(-1, SizeInUsageFile()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1502 | EXPECT_EQ(expected_quota, SizeByQuotaUtil()); |
| 1503 | |
| 1504 | // This should reconstruct the cache. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1505 | GetUsageFromQuotaManager(); |
| 1506 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1507 | EXPECT_EQ(expected_quota, SizeByQuotaUtil()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1508 | EXPECT_EQ(expected_quota, usage()); |
| 1509 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1510 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1511 | TEST_F(ObfuscatedFileUtilTest, TestInconsistency) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1512 | const FileSystemPath kPath1 = CreatePathFromUTF8("hoge"); |
| 1513 | const FileSystemPath kPath2 = CreatePathFromUTF8("fuga"); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1514 | |
| 1515 | scoped_ptr<FileSystemOperationContext> context; |
| 1516 | base::PlatformFile file; |
| 1517 | base::PlatformFileInfo file_info; |
| 1518 | FilePath data_path; |
| 1519 | bool created = false; |
| 1520 | |
| 1521 | // Create a non-empty file. |
| 1522 | context.reset(NewContext(NULL)); |
| 1523 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1524 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1525 | EXPECT_TRUE(created); |
| 1526 | context.reset(NewContext(NULL)); |
| 1527 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1528 | ofu()->Truncate(context.get(), kPath1, 10)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1529 | context.reset(NewContext(NULL)); |
| 1530 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1531 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1532 | context.get(), kPath1, &file_info, &data_path)); |
| 1533 | EXPECT_EQ(10, file_info.size); |
| 1534 | |
| 1535 | // Destroy database to make inconsistency between database and filesystem. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1536 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1537 | |
| 1538 | // Try to get file info of broken file. |
| 1539 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1540 | EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1541 | context.reset(NewContext(NULL)); |
| 1542 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1543 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1544 | EXPECT_TRUE(created); |
| 1545 | context.reset(NewContext(NULL)); |
| 1546 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1547 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1548 | context.get(), kPath1, &file_info, &data_path)); |
| 1549 | EXPECT_EQ(0, file_info.size); |
| 1550 | |
| 1551 | // Make another broken file to |kPath2|. |
| 1552 | context.reset(NewContext(NULL)); |
| 1553 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1554 | ofu()->EnsureFileExists(context.get(), kPath2, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1555 | EXPECT_TRUE(created); |
| 1556 | |
| 1557 | // Destroy again. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1558 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1559 | |
| 1560 | // Repair broken |kPath1|. |
| 1561 | context.reset(NewContext(NULL)); |
| 1562 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1563 | ofu()->Touch(context.get(), kPath1, base::Time::Now(), |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1564 | base::Time::Now())); |
| 1565 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1566 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1567 | EXPECT_TRUE(created); |
| 1568 | |
| 1569 | // Copy from sound |kPath1| to broken |kPath2|. |
| 1570 | context.reset(NewContext(NULL)); |
| 1571 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1572 | ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1573 | true /* copy */)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1574 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1575 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1576 | context.reset(NewContext(NULL)); |
| 1577 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1578 | ofu()->CreateOrOpen( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1579 | context.get(), kPath1, |
| 1580 | base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE, |
| 1581 | &file, &created)); |
| 1582 | EXPECT_TRUE(created); |
| 1583 | EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); |
| 1584 | EXPECT_EQ(0, file_info.size); |
| 1585 | EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 1586 | } |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1587 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1588 | TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) { |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1589 | const FileSystemPath kPath[] = { |
| 1590 | CreatePathFromUTF8("foo"), |
| 1591 | CreatePathFromUTF8("bar"), |
| 1592 | CreatePathFromUTF8("baz") |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1593 | }; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1594 | const FileSystemPath empty_path = CreatePath(FilePath()); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1595 | scoped_ptr<FileSystemOperationContext> context; |
| 1596 | |
| 1597 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) { |
| 1598 | bool created = false; |
| 1599 | context.reset(NewContext(NULL)); |
| 1600 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1601 | ofu()->EnsureFileExists(context.get(), kPath[i], &created)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1602 | EXPECT_TRUE(created); |
| 1603 | } |
| 1604 | |
| 1605 | context.reset(NewContext(NULL)); |
| 1606 | std::vector<base::FileUtilProxy::Entry> entries; |
| 1607 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1608 | FileUtilHelper::ReadDirectory( |
| 1609 | context.get(), ofu(), empty_path, &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1610 | EXPECT_EQ(3u, entries.size()); |
| 1611 | |
| 1612 | context.reset(NewContext(NULL)); |
| 1613 | FilePath local_path; |
| 1614 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1615 | ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1616 | EXPECT_TRUE(file_util::Delete(local_path, false)); |
| 1617 | |
| 1618 | context.reset(NewContext(NULL)); |
| 1619 | entries.clear(); |
| 1620 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1621 | FileUtilHelper::ReadDirectory( |
| 1622 | context.get(), ofu(), empty_path, &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1623 | EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); |
| 1624 | } |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1625 | |
| 1626 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) { |
| 1627 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1628 | const FileSystemPath dir_path = CreatePathFromUTF8("foo_dir"); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1629 | |
| 1630 | // Create working directory. |
| 1631 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1632 | ofu()->CreateDirectory(context.get(), dir_path, false, false)); |
| 1633 | |
| 1634 | // EnsureFileExists, create case. |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1635 | FileSystemPath path(dir_path.AppendASCII("EnsureFileExists_file")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1636 | bool created = false; |
| 1637 | ClearTimestamp(dir_path); |
| 1638 | context.reset(NewContext(NULL)); |
| 1639 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1640 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1641 | EXPECT_TRUE(created); |
| 1642 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1643 | |
| 1644 | // non create case. |
| 1645 | created = true; |
| 1646 | ClearTimestamp(dir_path); |
| 1647 | context.reset(NewContext(NULL)); |
| 1648 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1649 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1650 | EXPECT_FALSE(created); |
| 1651 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1652 | |
| 1653 | // fail case. |
| 1654 | path = dir_path.AppendASCII("EnsureFileExists_dir"); |
| 1655 | context.reset(NewContext(NULL)); |
| 1656 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1657 | ofu()->CreateDirectory(context.get(), path, false, false)); |
| 1658 | |
| 1659 | ClearTimestamp(dir_path); |
| 1660 | context.reset(NewContext(NULL)); |
| 1661 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, |
| 1662 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1663 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1664 | |
| 1665 | // CreateOrOpen, create case. |
| 1666 | path = dir_path.AppendASCII("CreateOrOpen_file"); |
| 1667 | PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 1668 | created = false; |
| 1669 | ClearTimestamp(dir_path); |
| 1670 | context.reset(NewContext(NULL)); |
| 1671 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1672 | ofu()->CreateOrOpen( |
| 1673 | context.get(), path, |
| 1674 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| 1675 | &file_handle, &created)); |
| 1676 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1677 | EXPECT_TRUE(created); |
| 1678 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1679 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1680 | |
| 1681 | // open case. |
| 1682 | file_handle = base::kInvalidPlatformFileValue; |
| 1683 | created = true; |
| 1684 | ClearTimestamp(dir_path); |
| 1685 | context.reset(NewContext(NULL)); |
| 1686 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1687 | ofu()->CreateOrOpen( |
| 1688 | context.get(), path, |
| 1689 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 1690 | &file_handle, &created)); |
| 1691 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1692 | EXPECT_FALSE(created); |
| 1693 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1694 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1695 | |
| 1696 | // fail case |
| 1697 | file_handle = base::kInvalidPlatformFileValue; |
| 1698 | ClearTimestamp(dir_path); |
| 1699 | context.reset(NewContext(NULL)); |
| 1700 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
| 1701 | ofu()->CreateOrOpen( |
| 1702 | context.get(), path, |
| 1703 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| 1704 | &file_handle, &created)); |
| 1705 | EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle); |
| 1706 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1707 | |
| 1708 | // CreateDirectory, create case. |
| 1709 | // Creating CreateDirectory_dir and CreateDirectory_dir/subdir. |
| 1710 | path = dir_path.AppendASCII("CreateDirectory_dir"); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1711 | FileSystemPath subdir_path(path.AppendASCII("subdir")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1712 | ClearTimestamp(dir_path); |
| 1713 | context.reset(NewContext(NULL)); |
| 1714 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1715 | ofu()->CreateDirectory(context.get(), subdir_path, |
| 1716 | true /* exclusive */, true /* recursive */)); |
| 1717 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1718 | |
| 1719 | // create subdir case. |
| 1720 | // Creating CreateDirectory_dir/subdir2. |
| 1721 | subdir_path = path.AppendASCII("subdir2"); |
| 1722 | ClearTimestamp(dir_path); |
| 1723 | ClearTimestamp(path); |
| 1724 | context.reset(NewContext(NULL)); |
| 1725 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1726 | ofu()->CreateDirectory(context.get(), subdir_path, |
| 1727 | true /* exclusive */, true /* recursive */)); |
| 1728 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1729 | EXPECT_NE(base::Time(), GetModifiedTime(path)); |
| 1730 | |
| 1731 | // fail case. |
| 1732 | path = dir_path.AppendASCII("CreateDirectory_dir"); |
| 1733 | ClearTimestamp(dir_path); |
| 1734 | context.reset(NewContext(NULL)); |
| 1735 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
| 1736 | ofu()->CreateDirectory(context.get(), path, |
| 1737 | true /* exclusive */, true /* recursive */)); |
| 1738 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1739 | |
| 1740 | // CopyInForeignFile, create case. |
| 1741 | path = dir_path.AppendASCII("CopyInForeignFile_file"); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1742 | FileSystemPath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file"); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1743 | context.reset(NewContext(NULL)); |
| 1744 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1745 | ofu()->EnsureFileExists(context.get(), src_path, &created)); |
| 1746 | EXPECT_TRUE(created); |
| 1747 | FilePath src_local_path; |
| 1748 | context.reset(NewContext(NULL)); |
| 1749 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1750 | ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path)); |
| 1751 | |
| 1752 | ClearTimestamp(dir_path); |
| 1753 | context.reset(NewContext(NULL)); |
| 1754 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1755 | ofu()->CopyInForeignFile(context.get(), |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1756 | src_local_path, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1757 | path)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1758 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1759 | } |
| 1760 | |
| 1761 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) { |
| 1762 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1763 | const FileSystemPath dir_path = CreatePathFromUTF8("foo_dir"); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1764 | |
| 1765 | // Create working directory. |
| 1766 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1767 | ofu()->CreateDirectory(context.get(), dir_path, false, false)); |
| 1768 | |
| 1769 | // DeleteFile, delete case. |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1770 | FileSystemPath path = dir_path.AppendASCII("DeleteFile_file"); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1771 | bool created = false; |
| 1772 | context.reset(NewContext(NULL)); |
| 1773 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1774 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1775 | EXPECT_TRUE(created); |
| 1776 | |
| 1777 | ClearTimestamp(dir_path); |
| 1778 | context.reset(NewContext(NULL)); |
| 1779 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1780 | ofu()->DeleteFile(context.get(), path)); |
| 1781 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1782 | |
| 1783 | // fail case. |
| 1784 | ClearTimestamp(dir_path); |
| 1785 | context.reset(NewContext(NULL)); |
| 1786 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 1787 | ofu()->DeleteFile(context.get(), path)); |
| 1788 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1789 | |
| 1790 | // DeleteSingleDirectory, fail case. |
| 1791 | path = dir_path.AppendASCII("DeleteSingleDirectory_dir"); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1792 | FileSystemPath file_path(path.AppendASCII("pakeratta")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1793 | context.reset(NewContext(NULL)); |
| 1794 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1795 | ofu()->CreateDirectory(context.get(), path, true, true)); |
| 1796 | created = false; |
| 1797 | context.reset(NewContext(NULL)); |
| 1798 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1799 | ofu()->EnsureFileExists(context.get(), file_path, &created)); |
| 1800 | EXPECT_TRUE(created); |
| 1801 | |
| 1802 | ClearTimestamp(dir_path); |
| 1803 | context.reset(NewContext(NULL)); |
| 1804 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
| 1805 | ofu()->DeleteSingleDirectory(context.get(), path)); |
| 1806 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1807 | |
| 1808 | // delete case. |
| 1809 | context.reset(NewContext(NULL)); |
| 1810 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1811 | ofu()->DeleteFile(context.get(), file_path)); |
| 1812 | |
| 1813 | ClearTimestamp(dir_path); |
| 1814 | context.reset(NewContext(NULL)); |
| 1815 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1816 | ofu()->DeleteSingleDirectory(context.get(), path)); |
| 1817 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1818 | } |
| 1819 | |
| 1820 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) { |
| 1821 | TestDirectoryTimestampHelper( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1822 | CreatePathFromUTF8("copy overwrite"), true, true); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1823 | TestDirectoryTimestampHelper( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1824 | CreatePathFromUTF8("copy non-overwrite"), true, false); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1825 | TestDirectoryTimestampHelper( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1826 | CreatePathFromUTF8("move overwrite"), false, true); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1827 | TestDirectoryTimestampHelper( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1828 | CreatePathFromUTF8("move non-overwrite"), false, false); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1829 | } |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1830 | |
| 1831 | TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) { |
| 1832 | FileSystemPath dir = CreatePathFromUTF8("foo"); |
| 1833 | FileSystemPath path1 = dir.AppendASCII("bar"); |
| 1834 | FileSystemPath path2 = dir.AppendASCII("baz"); |
| 1835 | |
| 1836 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1837 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1838 | ofu()->CreateDirectory(context.get(), dir, false, false)); |
| 1839 | |
| 1840 | bool created = false; |
| 1841 | context.reset(NewContext(NULL)); |
| 1842 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1843 | ofu()->EnsureFileExists(context.get(), path1, &created)); |
| 1844 | EXPECT_TRUE(created); |
| 1845 | |
| 1846 | context.reset(NewContext(NULL)); |
| 1847 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1848 | ofu()->CreateDirectory(context.get(), path2, false, false)); |
| 1849 | |
| 1850 | FilePath file_path; |
| 1851 | context.reset(NewContext(NULL)); |
| 1852 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1853 | ofu()->GetLocalFilePath(context.get(), path1, &file_path)); |
| 1854 | EXPECT_FALSE(file_path.empty()); |
| 1855 | |
| 1856 | context.reset(NewContext(NULL)); |
| 1857 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1858 | ofu()->Touch(context.get(), path1, |
| 1859 | base::Time::Now() + base::TimeDelta::FromHours(1), |
| 1860 | base::Time())); |
| 1861 | |
| 1862 | context.reset(NewContext(NULL)); |
| 1863 | scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum( |
| 1864 | ofu()->CreateFileEnumerator(context.get(), dir, false)); |
| 1865 | |
| 1866 | int count = 0; |
| 1867 | FilePath file_path_each; |
| 1868 | while (!(file_path_each = file_enum->Next()).empty()) { |
| 1869 | context.reset(NewContext(NULL)); |
| 1870 | base::PlatformFileInfo file_info; |
| 1871 | FilePath file_path; |
| 1872 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1873 | ofu()->GetFileInfo(context.get(), |
| 1874 | dir.WithInternalPath(file_path_each), |
| 1875 | &file_info, &file_path)); |
| 1876 | EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory()); |
| 1877 | EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime()); |
| 1878 | EXPECT_EQ(file_info.size, file_enum->Size()); |
| 1879 | ++count; |
| 1880 | } |
| 1881 | EXPECT_EQ(2, count); |
| 1882 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1883 | |
| 1884 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) { |
| 1885 | FileSystemPath from_file(CreatePathFromUTF8("fromfile")); |
| 1886 | FileSystemPath obstacle_file(CreatePathFromUTF8("obstaclefile")); |
| 1887 | FileSystemPath to_file1(CreatePathFromUTF8("tofile1")); |
| 1888 | FileSystemPath to_file2(CreatePathFromUTF8("tofile2")); |
| 1889 | bool created; |
| 1890 | |
| 1891 | int64 expected_total_file_size = 0; |
| 1892 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1893 | ofu()->EnsureFileExists( |
| 1894 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 1895 | from_file, &created)); |
| 1896 | ASSERT_TRUE(created); |
| 1897 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1898 | |
| 1899 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1900 | ofu()->EnsureFileExists( |
| 1901 | AllowUsageIncrease(PathCost(obstacle_file))->context(), |
| 1902 | obstacle_file, &created)); |
| 1903 | ASSERT_TRUE(created); |
| 1904 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1905 | |
| 1906 | int64 from_file_size = 1020; |
| 1907 | expected_total_file_size += from_file_size; |
| 1908 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1909 | ofu()->Truncate( |
| 1910 | AllowUsageIncrease(from_file_size)->context(), |
| 1911 | from_file, from_file_size)); |
| 1912 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1913 | |
| 1914 | int64 obstacle_file_size = 1; |
| 1915 | expected_total_file_size += obstacle_file_size; |
| 1916 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1917 | ofu()->Truncate( |
| 1918 | AllowUsageIncrease(obstacle_file_size)->context(), |
| 1919 | obstacle_file, obstacle_file_size)); |
| 1920 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1921 | |
| 1922 | int64 to_file1_size = from_file_size; |
| 1923 | expected_total_file_size += to_file1_size; |
| 1924 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1925 | ofu()->CopyOrMoveFile( |
| 1926 | AllowUsageIncrease( |
| 1927 | PathCost(to_file1) + to_file1_size)->context(), |
| 1928 | from_file, to_file1, true /* copy */)); |
| 1929 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1930 | |
| 1931 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 1932 | ofu()->CopyOrMoveFile( |
| 1933 | DisallowUsageIncrease( |
| 1934 | PathCost(to_file2) + from_file_size)->context(), |
| 1935 | from_file, to_file2, true /* copy */)); |
| 1936 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1937 | |
| 1938 | int64 old_obstacle_file_size = obstacle_file_size; |
| 1939 | obstacle_file_size = from_file_size; |
| 1940 | expected_total_file_size += obstacle_file_size - old_obstacle_file_size; |
| 1941 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1942 | ofu()->CopyOrMoveFile( |
| 1943 | AllowUsageIncrease( |
| 1944 | obstacle_file_size - old_obstacle_file_size)->context(), |
| 1945 | from_file, obstacle_file, true /* copy */)); |
| 1946 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1947 | |
| 1948 | int64 old_from_file_size = from_file_size; |
| 1949 | from_file_size = old_from_file_size - 1; |
| 1950 | expected_total_file_size += from_file_size - old_from_file_size; |
| 1951 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1952 | ofu()->Truncate( |
| 1953 | AllowUsageIncrease( |
| 1954 | from_file_size - old_from_file_size)->context(), |
| 1955 | from_file, from_file_size)); |
| 1956 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1957 | |
| 1958 | // quota exceeded |
| 1959 | { |
| 1960 | old_obstacle_file_size = obstacle_file_size; |
| 1961 | obstacle_file_size = from_file_size; |
| 1962 | expected_total_file_size += obstacle_file_size - old_obstacle_file_size; |
| 1963 | scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease( |
| 1964 | obstacle_file_size - old_obstacle_file_size); |
| 1965 | helper->context()->set_allowed_bytes_growth( |
| 1966 | helper->context()->allowed_bytes_growth() - 1); |
| 1967 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1968 | ofu()->CopyOrMoveFile( |
| 1969 | helper->context(), |
| 1970 | from_file, obstacle_file, true /* copy */)); |
| 1971 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) { |
| 1976 | FileSystemPath from_file(CreatePathFromUTF8("fromfile")); |
| 1977 | FileSystemPath obstacle_file(CreatePathFromUTF8("obstaclefile")); |
| 1978 | FileSystemPath to_file(CreatePathFromUTF8("tofile")); |
| 1979 | bool created; |
| 1980 | |
| 1981 | int64 expected_total_file_size = 0; |
| 1982 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1983 | ofu()->EnsureFileExists( |
| 1984 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 1985 | from_file, &created)); |
| 1986 | ASSERT_TRUE(created); |
| 1987 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1988 | |
| 1989 | int64 from_file_size = 1020; |
| 1990 | expected_total_file_size += from_file_size; |
| 1991 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1992 | ofu()->Truncate( |
| 1993 | AllowUsageIncrease(from_file_size)->context(), |
| 1994 | from_file, from_file_size)); |
| 1995 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1996 | |
| 1997 | int64 to_file_size ALLOW_UNUSED = from_file_size; |
| 1998 | from_file_size = 0; |
| 1999 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2000 | ofu()->CopyOrMoveFile( |
| 2001 | AllowUsageIncrease(-PathCost(from_file) + |
| 2002 | PathCost(to_file))->context(), |
| 2003 | from_file, to_file, false /* move */)); |
| 2004 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2005 | |
| 2006 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2007 | ofu()->EnsureFileExists( |
| 2008 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 2009 | from_file, &created)); |
| 2010 | ASSERT_TRUE(created); |
| 2011 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2012 | |
| 2013 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2014 | ofu()->EnsureFileExists( |
| 2015 | AllowUsageIncrease(PathCost(obstacle_file))->context(), |
| 2016 | obstacle_file, &created)); |
| 2017 | ASSERT_TRUE(created); |
| 2018 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2019 | |
| 2020 | from_file_size = 1020; |
| 2021 | expected_total_file_size += from_file_size; |
| 2022 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2023 | ofu()->Truncate( |
| 2024 | AllowUsageIncrease(from_file_size)->context(), |
| 2025 | from_file, from_file_size)); |
| 2026 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2027 | |
| 2028 | int64 obstacle_file_size = 1; |
| 2029 | expected_total_file_size += obstacle_file_size; |
| 2030 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2031 | ofu()->Truncate( |
| 2032 | AllowUsageIncrease(1)->context(), |
| 2033 | obstacle_file, obstacle_file_size)); |
| 2034 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2035 | |
| 2036 | int64 old_obstacle_file_size = obstacle_file_size; |
| 2037 | obstacle_file_size = from_file_size; |
| 2038 | from_file_size = 0; |
| 2039 | expected_total_file_size -= old_obstacle_file_size; |
| 2040 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2041 | ofu()->CopyOrMoveFile( |
| 2042 | AllowUsageIncrease( |
| 2043 | -old_obstacle_file_size - PathCost(from_file))->context(), |
| 2044 | from_file, obstacle_file, |
| 2045 | false /* move */)); |
| 2046 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2047 | |
| 2048 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2049 | ofu()->EnsureFileExists( |
| 2050 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 2051 | from_file, &created)); |
| 2052 | ASSERT_TRUE(created); |
| 2053 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2054 | |
| 2055 | from_file_size = 10; |
| 2056 | expected_total_file_size += from_file_size; |
| 2057 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2058 | ofu()->Truncate( |
| 2059 | AllowUsageIncrease(from_file_size)->context(), |
| 2060 | from_file, from_file_size)); |
| 2061 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2062 | |
| 2063 | // quota exceeded even after operation |
| 2064 | old_obstacle_file_size = obstacle_file_size; |
| 2065 | obstacle_file_size = from_file_size; |
| 2066 | from_file_size = 0; |
| 2067 | expected_total_file_size -= old_obstacle_file_size; |
| 2068 | scoped_ptr<FileSystemOperationContext> context = |
| 2069 | LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1); |
| 2070 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2071 | ofu()->CopyOrMoveFile( |
| 2072 | context.get(), from_file, obstacle_file, false /* move */)); |
| 2073 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2074 | context.reset(); |
| 2075 | } |
| 2076 | |
| 2077 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) { |
| 2078 | FileSystemPath dir(CreatePathFromUTF8("dir")); |
| 2079 | FileSystemPath file(CreatePathFromUTF8("file")); |
| 2080 | FileSystemPath dfile1(CreatePathFromUTF8("dir/dfile1")); |
| 2081 | FileSystemPath dfile2(CreatePathFromUTF8("dir/dfile2")); |
| 2082 | bool created; |
| 2083 | |
| 2084 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2085 | ofu()->EnsureFileExists( |
| 2086 | AllowUsageIncrease(PathCost(file))->context(), |
| 2087 | file, &created)); |
| 2088 | ASSERT_TRUE(created); |
| 2089 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2090 | |
| 2091 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2092 | ofu()->CreateDirectory( |
| 2093 | AllowUsageIncrease(PathCost(dir))->context(), |
| 2094 | dir, false, false)); |
| 2095 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2096 | |
| 2097 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2098 | ofu()->EnsureFileExists( |
| 2099 | AllowUsageIncrease(PathCost(dfile1))->context(), |
| 2100 | dfile1, &created)); |
| 2101 | ASSERT_TRUE(created); |
| 2102 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2103 | |
| 2104 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2105 | ofu()->EnsureFileExists( |
| 2106 | AllowUsageIncrease(PathCost(dfile2))->context(), |
| 2107 | dfile2, &created)); |
| 2108 | ASSERT_TRUE(created); |
| 2109 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2110 | |
| 2111 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2112 | ofu()->Truncate( |
| 2113 | AllowUsageIncrease(340)->context(), |
| 2114 | file, 340)); |
| 2115 | ASSERT_EQ(340, ComputeTotalFileSize()); |
| 2116 | |
| 2117 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2118 | ofu()->Truncate( |
| 2119 | AllowUsageIncrease(1020)->context(), |
| 2120 | dfile1, 1020)); |
| 2121 | ASSERT_EQ(1360, ComputeTotalFileSize()); |
| 2122 | |
| 2123 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2124 | ofu()->Truncate( |
| 2125 | AllowUsageIncrease(120)->context(), |
| 2126 | dfile2, 120)); |
| 2127 | ASSERT_EQ(1480, ComputeTotalFileSize()); |
| 2128 | |
| 2129 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2130 | ofu()->DeleteFile( |
| 2131 | AllowUsageIncrease(-PathCost(file) - 340)->context(), |
| 2132 | file)); |
| 2133 | ASSERT_EQ(1140, ComputeTotalFileSize()); |
| 2134 | |
| 2135 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2136 | FileUtilHelper::Delete( |
| 2137 | AllowUsageIncrease(-PathCost(dir) - |
| 2138 | PathCost(dfile1) - |
| 2139 | PathCost(dfile2) - |
| 2140 | 1020 - 120)->context(), |
| 2141 | ofu(), dir, true)); |
| 2142 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2143 | } |
[email protected] | 7d78be1 | 2012-05-24 07:07:26 | [diff] [blame^] | 2144 | |
| 2145 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) { |
| 2146 | FileSystemPath file(CreatePathFromUTF8("file")); |
| 2147 | base::PlatformFile file_handle; |
| 2148 | bool created; |
| 2149 | |
| 2150 | // Creating a file. |
| 2151 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2152 | ofu()->EnsureFileExists( |
| 2153 | AllowUsageIncrease(PathCost(file))->context(), |
| 2154 | file, &created)); |
| 2155 | ASSERT_TRUE(created); |
| 2156 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2157 | |
| 2158 | // Opening it, which shouldn't change the usage. |
| 2159 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2160 | ofu()->CreateOrOpen( |
| 2161 | AllowUsageIncrease(0)->context(), file, |
| 2162 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 2163 | &file_handle, &created)); |
| 2164 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2165 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 2166 | |
| 2167 | const int length = 33; |
| 2168 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2169 | ofu()->Truncate( |
| 2170 | AllowUsageIncrease(length)->context(), file, length)); |
| 2171 | ASSERT_EQ(length, ComputeTotalFileSize()); |
| 2172 | |
| 2173 | // Opening it with CREATE_ALWAYS flag, which should truncate the file size. |
| 2174 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2175 | ofu()->CreateOrOpen( |
| 2176 | AllowUsageIncrease(-length)->context(), file, |
| 2177 | base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 2178 | &file_handle, &created)); |
| 2179 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2180 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 2181 | |
| 2182 | // Extending the file again. |
| 2183 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2184 | ofu()->Truncate( |
| 2185 | AllowUsageIncrease(length)->context(), file, length)); |
| 2186 | ASSERT_EQ(length, ComputeTotalFileSize()); |
| 2187 | |
| 2188 | // Opening it with TRUNCATED flag, which should truncate the file size. |
| 2189 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2190 | ofu()->CreateOrOpen( |
| 2191 | AllowUsageIncrease(-length)->context(), file, |
| 2192 | base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE, |
| 2193 | &file_handle, &created)); |
| 2194 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2195 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 2196 | } |