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