[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 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 | |
| 9 | #include "base/file_path.h" |
| 10 | #include "base/file_util.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 12 | #include "base/memory/scoped_callback_factory.h" |
| 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] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 20 | #include "webkit/fileapi/file_system_path_manager.h" |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 21 | #include "webkit/fileapi/file_system_test_helper.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 22 | #include "webkit/fileapi/file_system_usage_cache.h" |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 23 | #include "webkit/fileapi/obfuscated_file_util.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 24 | #include "webkit/quota/mock_special_storage_policy.h" |
| 25 | #include "webkit/quota/quota_manager.h" |
| 26 | #include "webkit/quota/quota_types.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 27 | |
| 28 | using namespace fileapi; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | FilePath UTF8ToFilePath(const std::string& str) { |
| 33 | FilePath::StringType result; |
| 34 | #if defined(OS_POSIX) |
| 35 | result = str; |
| 36 | #elif defined(OS_WIN) |
| 37 | result = base::SysUTF8ToWide(str); |
| 38 | #endif |
| 39 | return FilePath(result); |
| 40 | } |
| 41 | |
| 42 | bool FileExists(const FilePath& path) { |
| 43 | return file_util::PathExists(path) && !file_util::DirectoryExists(path); |
| 44 | } |
| 45 | |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 46 | int64 GetSize(const FilePath& path) { |
| 47 | int64 size; |
| 48 | EXPECT_TRUE(file_util::GetFileSize(path, &size)); |
| 49 | return size; |
| 50 | } |
| 51 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 52 | // After a move, the dest exists and the source doesn't. |
| 53 | // After a copy, both source and dest exist. |
| 54 | struct CopyMoveTestCaseRecord { |
| 55 | bool is_copy_not_move; |
| 56 | const char source_path[64]; |
| 57 | const char dest_path[64]; |
| 58 | bool cause_overwrite; |
| 59 | }; |
| 60 | |
| 61 | const CopyMoveTestCaseRecord kCopyMoveTestCases[] = { |
| 62 | // This is the combinatoric set of: |
| 63 | // rename vs. same-name |
| 64 | // different directory vs. same directory |
| 65 | // overwrite vs. no-overwrite |
| 66 | // copy vs. move |
| 67 | // We can never be called with source and destination paths identical, so |
| 68 | // those cases are omitted. |
| 69 | {true, "dir0/file0", "dir0/file1", false}, |
| 70 | {false, "dir0/file0", "dir0/file1", false}, |
| 71 | {true, "dir0/file0", "dir0/file1", true}, |
| 72 | {false, "dir0/file0", "dir0/file1", true}, |
| 73 | |
| 74 | {true, "dir0/file0", "dir1/file0", false}, |
| 75 | {false, "dir0/file0", "dir1/file0", false}, |
| 76 | {true, "dir0/file0", "dir1/file0", true}, |
| 77 | {false, "dir0/file0", "dir1/file0", true}, |
| 78 | {true, "dir0/file0", "dir1/file1", false}, |
| 79 | {false, "dir0/file0", "dir1/file1", false}, |
| 80 | {true, "dir0/file0", "dir1/file1", true}, |
| 81 | {false, "dir0/file0", "dir1/file1", true}, |
| 82 | }; |
| 83 | |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 84 | struct MigrationTestCaseRecord { |
| 85 | bool is_directory; |
| 86 | const FilePath::CharType path[64]; |
| 87 | int64 data_file_size; |
| 88 | }; |
| 89 | |
| 90 | const MigrationTestCaseRecord kMigrationTestCases[] = { |
| 91 | {true, FILE_PATH_LITERAL("dir a"), 0}, |
| 92 | {true, FILE_PATH_LITERAL("dir a/dir a"), 0}, |
| 93 | {true, FILE_PATH_LITERAL("dir a/dir d"), 0}, |
| 94 | {true, FILE_PATH_LITERAL("dir a/dir d/dir e"), 0}, |
| 95 | {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir f"), 0}, |
| 96 | {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g"), 0}, |
| 97 | {true, FILE_PATH_LITERAL("dir a/dir d/dir e/dir h"), 0}, |
| 98 | {true, FILE_PATH_LITERAL("dir b"), 0}, |
| 99 | {true, FILE_PATH_LITERAL("dir b/dir a"), 0}, |
| 100 | {true, FILE_PATH_LITERAL("dir c"), 0}, |
| 101 | {false, FILE_PATH_LITERAL("file 0"), 38}, |
| 102 | {false, FILE_PATH_LITERAL("file 2"), 60}, |
| 103 | {false, FILE_PATH_LITERAL("file 3"), 0}, |
| 104 | {false, FILE_PATH_LITERAL("dir a/file 0"), 39}, |
| 105 | {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40}, |
| 106 | {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41}, |
| 107 | {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42}, |
| 108 | {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50}, |
| 109 | }; |
| 110 | |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 111 | struct OriginEnumerationTestRecord { |
| 112 | std::string origin_url; |
| 113 | bool has_temporary; |
| 114 | bool has_persistent; |
| 115 | }; |
| 116 | |
| 117 | const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = { |
| 118 | {"http://example.com", false, true}, |
| 119 | {"http://example1.com", true, false}, |
| 120 | {"https://example1.com", true, true}, |
| 121 | {"file://", false, true}, |
| 122 | {"http://example.com:8000", false, true}, |
| 123 | }; |
| 124 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 125 | } // namespace (anonymous) |
| 126 | |
| 127 | // TODO(ericu): The vast majority of this and the other FSFU subclass tests |
| 128 | // could theoretically be shared. It would basically be a FSFU interface |
| 129 | // compliance test, and only the subclass-specific bits that look into the |
| 130 | // implementation would need to be written per-subclass. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 131 | class ObfuscatedFileUtilTest : public testing::Test { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 132 | public: |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 133 | ObfuscatedFileUtilTest() |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 134 | : origin_(GURL("http://www.example.com")), |
| 135 | type_(kFileSystemTypeTemporary), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 136 | callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 137 | test_helper_(origin_, type_), |
| 138 | quota_status_(quota::kQuotaStatusUnknown), |
| 139 | usage_(-1) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void SetUp() { |
| 143 | ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 144 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 145 | quota_manager_ = new quota::QuotaManager( |
| 146 | false /* is_incognito */, |
| 147 | data_dir_.path(), |
| 148 | base::MessageLoopProxy::current(), |
| 149 | base::MessageLoopProxy::current(), |
| 150 | NULL /* special storage policy */); |
| 151 | |
| 152 | // Every time we create a new helper, it creates another context, which |
| 153 | // creates another path manager, another sandbox_mount_point_provider, and |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 154 | // another OFU. We need to pass in the context to skip all that. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 155 | file_system_context_ = new FileSystemContext( |
| 156 | base::MessageLoopProxy::current(), |
| 157 | base::MessageLoopProxy::current(), |
| 158 | new quota::MockSpecialStoragePolicy(), |
| 159 | quota_manager_->proxy(), |
| 160 | data_dir_.path(), |
| 161 | false /* incognito */, |
| 162 | true /* allow_file_access_from_files */, |
| 163 | false /* unlimited_quota */, |
| 164 | NULL /* path_manager */); |
| 165 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 166 | obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>( |
| 167 | file_system_context_->path_manager()->GetFileUtil(type_)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 168 | |
| 169 | test_helper_.SetUp(file_system_context_.get(), |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 170 | obfuscated_file_util_.get()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 173 | FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) { |
| 174 | FileSystemOperationContext* context; |
| 175 | if (helper) |
| 176 | context = helper->NewOperationContext(); |
| 177 | else |
| 178 | context = test_helper_.NewOperationContext(); |
| 179 | context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 180 | return context; |
| 181 | } |
| 182 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 183 | // 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^] | 184 | // and obfuscated_file_util_. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 185 | // Use this for tests which need to run in multiple origins; we need a test |
| 186 | // helper per origin. |
| 187 | FileSystemTestOriginHelper* NewHelper( |
| 188 | const GURL& origin, fileapi::FileSystemType type) { |
| 189 | FileSystemTestOriginHelper* helper = |
| 190 | new FileSystemTestOriginHelper(origin, type); |
| 191 | |
| 192 | helper->SetUp(file_system_context_.get(), |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 193 | obfuscated_file_util_.get()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 194 | return helper; |
| 195 | } |
| 196 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 197 | ObfuscatedFileUtil* ofu() { |
| 198 | return obfuscated_file_util_.get(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 201 | const FilePath& test_directory() const { |
| 202 | return data_dir_.path(); |
| 203 | } |
| 204 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 205 | const GURL& origin() const { |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 206 | return origin_; |
| 207 | } |
| 208 | |
| 209 | fileapi::FileSystemType type() const { |
| 210 | return type_; |
| 211 | } |
| 212 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 213 | void GetUsageFromQuotaManager() { |
| 214 | quota_manager_->GetUsageAndQuota( |
| 215 | origin(), test_helper_.storage_type(), |
| 216 | callback_factory_.NewCallback( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 217 | &ObfuscatedFileUtilTest::OnGetUsage)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 218 | MessageLoop::current()->RunAllPending(); |
| 219 | EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); |
| 220 | } |
| 221 | |
| 222 | void RevokeUsageCache() { |
| 223 | quota_manager_->ResetUsageTracker(test_helper_.storage_type()); |
| 224 | ASSERT_TRUE(test_helper_.RevokeUsageCache()); |
| 225 | } |
| 226 | |
| 227 | int64 SizeInUsageFile() { |
| 228 | return test_helper_.GetCachedOriginUsage(); |
| 229 | } |
| 230 | |
| 231 | int64 usage() const { return usage_; } |
| 232 | |
| 233 | void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { |
| 234 | EXPECT_EQ(quota::kQuotaStatusOk, status); |
| 235 | quota_status_ = status; |
| 236 | usage_ = usage; |
| 237 | } |
| 238 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 239 | void CheckFileAndCloseHandle( |
| 240 | const FilePath& virtual_path, PlatformFile file_handle) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 241 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 242 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 243 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 244 | context.get(), virtual_path, &local_path)); |
| 245 | |
| 246 | base::PlatformFileInfo file_info0; |
| 247 | FilePath data_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 248 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 249 | context.get(), virtual_path, &file_info0, &data_path)); |
| 250 | EXPECT_EQ(data_path, local_path); |
| 251 | EXPECT_TRUE(FileExists(data_path)); |
| 252 | EXPECT_EQ(0, GetSize(data_path)); |
| 253 | |
| 254 | const char data[] = "test data"; |
| 255 | const int length = arraysize(data) - 1; |
| 256 | |
| 257 | if (base::kInvalidPlatformFileValue == file_handle) { |
| 258 | bool created = true; |
| 259 | PlatformFileError error; |
| 260 | file_handle = base::CreatePlatformFile( |
| 261 | data_path, |
| 262 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 263 | &created, |
| 264 | &error); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 265 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 266 | ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 267 | EXPECT_FALSE(created); |
| 268 | } |
| 269 | ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); |
| 270 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 271 | |
| 272 | base::PlatformFileInfo file_info1; |
| 273 | EXPECT_EQ(length, GetSize(data_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 274 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 275 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 276 | context.get(), virtual_path, &file_info1, &data_path)); |
| 277 | EXPECT_EQ(data_path, local_path); |
| 278 | |
| 279 | EXPECT_FALSE(file_info0.is_directory); |
| 280 | EXPECT_FALSE(file_info1.is_directory); |
| 281 | EXPECT_FALSE(file_info0.is_symbolic_link); |
| 282 | EXPECT_FALSE(file_info1.is_symbolic_link); |
| 283 | EXPECT_EQ(0, file_info0.size); |
| 284 | EXPECT_EQ(length, file_info1.size); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 285 | EXPECT_LE(file_info0.last_modified, file_info1.last_modified); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 286 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 287 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 288 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 289 | context.get(), virtual_path, length * 2)); |
| 290 | EXPECT_EQ(length * 2, GetSize(data_path)); |
| 291 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 292 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 293 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 294 | context.get(), virtual_path, 0)); |
| 295 | EXPECT_EQ(0, GetSize(data_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void ValidateTestDirectory( |
| 299 | const FilePath& root_path, |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 300 | const std::set<FilePath::StringType>& files, |
| 301 | const std::set<FilePath::StringType>& directories) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 302 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 303 | std::set<FilePath::StringType>::const_iterator iter; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 304 | for (iter = files.begin(); iter != files.end(); ++iter) { |
| 305 | bool created = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 306 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 307 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 308 | ofu()->EnsureFileExists( |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 309 | context.get(), root_path.Append(*iter), |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 310 | &created)); |
| 311 | ASSERT_FALSE(created); |
| 312 | } |
| 313 | for (iter = directories.begin(); iter != directories.end(); ++iter) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 314 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 315 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 316 | root_path.Append(*iter))); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | void FillTestDirectory( |
| 321 | const FilePath& root_path, |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 322 | std::set<FilePath::StringType>* files, |
| 323 | std::set<FilePath::StringType>* directories) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 324 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 325 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 326 | std::vector<base::FileUtilProxy::Entry> entries; |
| 327 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 328 | ofu()->ReadDirectory(context.get(), root_path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 329 | EXPECT_EQ(0UL, entries.size()); |
| 330 | |
| 331 | files->clear(); |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 332 | files->insert(FILE_PATH_LITERAL("first")); |
| 333 | files->insert(FILE_PATH_LITERAL("second")); |
| 334 | files->insert(FILE_PATH_LITERAL("third")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 335 | directories->clear(); |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 336 | directories->insert(FILE_PATH_LITERAL("fourth")); |
| 337 | directories->insert(FILE_PATH_LITERAL("fifth")); |
| 338 | directories->insert(FILE_PATH_LITERAL("sixth")); |
| 339 | std::set<FilePath::StringType>::iterator iter; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 340 | for (iter = files->begin(); iter != files->end(); ++iter) { |
| 341 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 342 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 343 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 344 | ofu()->EnsureFileExists( |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 345 | context.get(), root_path.Append(*iter), &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 346 | ASSERT_TRUE(created); |
| 347 | } |
| 348 | for (iter = directories->begin(); iter != directories->end(); ++iter) { |
| 349 | bool exclusive = true; |
| 350 | bool recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 351 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 352 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 353 | ofu()->CreateDirectory( |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 354 | context.get(), root_path.Append(*iter), exclusive, recursive)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 355 | } |
| 356 | ValidateTestDirectory(root_path, *files, *directories); |
| 357 | } |
| 358 | |
| 359 | void TestReadDirectoryHelper(const FilePath& root_path) { |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 360 | std::set<FilePath::StringType> files; |
| 361 | std::set<FilePath::StringType> directories; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 362 | FillTestDirectory(root_path, &files, &directories); |
| 363 | |
| 364 | scoped_ptr<FileSystemOperationContext> context; |
| 365 | std::vector<base::FileUtilProxy::Entry> entries; |
[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 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 368 | ofu()->ReadDirectory(context.get(), root_path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 369 | std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; |
| 370 | EXPECT_EQ(files.size() + directories.size(), entries.size()); |
| 371 | for (entry_iter = entries.begin(); entry_iter != entries.end(); |
| 372 | ++entry_iter) { |
| 373 | const base::FileUtilProxy::Entry& entry = *entry_iter; |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 374 | std::set<FilePath::StringType>::iterator iter = files.find(entry.name); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 375 | if (iter != files.end()) { |
| 376 | EXPECT_FALSE(entry.is_directory); |
| 377 | files.erase(iter); |
| 378 | continue; |
| 379 | } |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 380 | iter = directories.find(entry.name); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 381 | EXPECT_FALSE(directories.end() == iter); |
| 382 | EXPECT_TRUE(entry.is_directory); |
| 383 | directories.erase(iter); |
| 384 | } |
| 385 | } |
| 386 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 387 | void TestTouchHelper(const FilePath& path, bool is_file) { |
| 388 | base::Time last_access_time = base::Time::Now(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 389 | base::Time last_modified_time = base::Time::Now(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 390 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 391 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 392 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 393 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 394 | context.get(), path, last_access_time, last_modified_time)); |
| 395 | FilePath local_path; |
| 396 | base::PlatformFileInfo file_info; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 397 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 398 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 399 | context.get(), path, &file_info, &local_path)); |
| 400 | // We compare as time_t here to lower our resolution, to avoid false |
| 401 | // negatives caused by conversion to the local filesystem's native |
| 402 | // representation and back. |
| 403 | EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
| 404 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 405 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 406 | last_modified_time += base::TimeDelta::FromHours(1); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 407 | last_access_time += base::TimeDelta::FromHours(14); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 408 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 409 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 410 | context.get(), path, last_access_time, last_modified_time)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 411 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 412 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 413 | context.get(), path, &file_info, &local_path)); |
| 414 | EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 415 | if (is_file) // Directories in OFU don't support atime. |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 416 | EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 419 | void TestCopyInForeignFileHelper(bool overwrite) { |
| 420 | ScopedTempDir source_dir; |
| 421 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 422 | FilePath root_path = source_dir.path(); |
| 423 | FilePath src_path = root_path.AppendASCII("file_name"); |
| 424 | FilePath dest_path(FILE_PATH_LITERAL("new file")); |
| 425 | int64 src_file_length = 87; |
| 426 | |
| 427 | base::PlatformFileError error_code; |
| 428 | bool created = false; |
| 429 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 430 | base::PlatformFile file_handle = |
| 431 | base::CreatePlatformFile( |
| 432 | src_path, file_flags, &created, &error_code); |
| 433 | EXPECT_TRUE(created); |
| 434 | ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 435 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 436 | ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); |
| 437 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 438 | |
| 439 | scoped_ptr<FileSystemOperationContext> context; |
| 440 | |
| 441 | if (overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 442 | context.reset(NewContext(NULL)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 443 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 444 | ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 445 | EXPECT_TRUE(created); |
| 446 | } |
| 447 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 448 | const int64 path_cost = |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 449 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 450 | if (!overwrite) { |
| 451 | // Verify that file creation requires sufficient quota for the path. |
| 452 | context.reset(NewContext(NULL)); |
| 453 | context->set_allowed_bytes_growth(path_cost + src_file_length - 1); |
| 454 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 455 | ofu()->CopyInForeignFile(context.get(), src_path, dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | context.reset(NewContext(NULL)); |
| 459 | context->set_allowed_bytes_growth(path_cost + src_file_length); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 460 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 461 | ofu()->CopyInForeignFile(context.get(), src_path, dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 462 | |
| 463 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 464 | EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 465 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 466 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 467 | context.reset(NewContext(NULL)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 468 | base::PlatformFileInfo file_info; |
| 469 | FilePath data_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 470 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 471 | context.get(), dest_path, &file_info, &data_path)); |
| 472 | EXPECT_NE(data_path, src_path); |
| 473 | EXPECT_TRUE(FileExists(data_path)); |
| 474 | EXPECT_EQ(src_file_length, GetSize(data_path)); |
| 475 | |
| 476 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 477 | ofu()->DeleteFile(context.get(), dest_path)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 480 | private: |
| 481 | ScopedTempDir data_dir_; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 482 | scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 483 | scoped_refptr<quota::QuotaManager> quota_manager_; |
| 484 | scoped_refptr<FileSystemContext> file_system_context_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 485 | GURL origin_; |
| 486 | fileapi::FileSystemType type_; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 487 | base::ScopedCallbackFactory<ObfuscatedFileUtilTest> |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 488 | callback_factory_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 489 | FileSystemTestOriginHelper test_helper_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 490 | quota::QuotaStatusCode quota_status_; |
| 491 | int64 usage_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 492 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 493 | DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 494 | }; |
| 495 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 496 | TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 497 | base::PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 498 | bool created; |
| 499 | FilePath path = UTF8ToFilePath("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 500 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 501 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 502 | |
| 503 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 504 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 505 | context.get(), path, file_flags, &file_handle, |
| 506 | &created)); |
| 507 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 508 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 509 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 510 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 511 | |
| 512 | path = UTF8ToFilePath("test file"); |
| 513 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 514 | // Verify that file creation requires sufficient quota for the path. |
| 515 | context.reset(NewContext(NULL)); |
| 516 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 517 | ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 518 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 519 | ofu()->CreateOrOpen( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 520 | context.get(), path, file_flags, &file_handle, &created)); |
| 521 | |
| 522 | context.reset(NewContext(NULL)); |
| 523 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 524 | ObfuscatedFileUtil::ComputeFilePathCost(path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 525 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 526 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 527 | context.get(), path, file_flags, &file_handle, &created)); |
| 528 | ASSERT_TRUE(created); |
| 529 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 530 | |
| 531 | CheckFileAndCloseHandle(path, file_handle); |
| 532 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 533 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 534 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 535 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 536 | context.get(), path, &local_path)); |
| 537 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 538 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 539 | // Verify that deleting a file isn't stopped by zero quota, and that it frees |
| 540 | // up quote from its path. |
| 541 | context.reset(NewContext(NULL)); |
| 542 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 543 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 544 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 545 | EXPECT_FALSE(file_util::PathExists(local_path)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 546 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 547 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 548 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 549 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 550 | bool exclusive = true; |
| 551 | bool recursive = true; |
| 552 | FilePath directory_path = UTF8ToFilePath("series/of/directories"); |
| 553 | path = directory_path.AppendASCII("file name"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 554 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 555 | context.get(), directory_path, exclusive, recursive)); |
| 556 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 557 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 558 | file_handle = base::kInvalidPlatformFileValue; |
| 559 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 560 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 561 | context.get(), path, file_flags, &file_handle, &created)); |
| 562 | ASSERT_TRUE(created); |
| 563 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 564 | |
| 565 | CheckFileAndCloseHandle(path, file_handle); |
| 566 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 567 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 568 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 569 | context.get(), path, &local_path)); |
| 570 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 571 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 572 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 573 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 574 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 575 | EXPECT_FALSE(file_util::PathExists(local_path)); |
| 576 | } |
| 577 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 578 | TEST_F(ObfuscatedFileUtilTest, TestTruncate) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 579 | bool created = false; |
| 580 | FilePath path = UTF8ToFilePath("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 581 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 582 | |
| 583 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 584 | ofu()->Truncate(context.get(), path, 4)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 585 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 586 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 587 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 588 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 589 | ASSERT_TRUE(created); |
| 590 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 591 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 592 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 593 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 594 | context.get(), path, &local_path)); |
| 595 | EXPECT_EQ(0, GetSize(local_path)); |
| 596 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 597 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 598 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 599 | context.get(), path, 10)); |
| 600 | EXPECT_EQ(10, GetSize(local_path)); |
| 601 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 602 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 603 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 604 | context.get(), path, 1)); |
| 605 | EXPECT_EQ(1, GetSize(local_path)); |
| 606 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 607 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 608 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 609 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 610 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 611 | } |
| 612 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 613 | TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 614 | FilePath path = UTF8ToFilePath("fake/file"); |
| 615 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 616 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 617 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 618 | ofu()->EnsureFileExists( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 619 | context.get(), path, &created)); |
| 620 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 621 | // Verify that file creation requires sufficient quota for the path. |
| 622 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 623 | path = UTF8ToFilePath("test file"); |
| 624 | created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 625 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 626 | ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 627 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 628 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 629 | ASSERT_FALSE(created); |
| 630 | |
| 631 | context.reset(NewContext(NULL)); |
| 632 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 633 | ObfuscatedFileUtil::ComputeFilePathCost(path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 634 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 635 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 636 | ASSERT_TRUE(created); |
| 637 | |
| 638 | CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); |
| 639 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 640 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 641 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 642 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 643 | ASSERT_FALSE(created); |
| 644 | |
| 645 | // Also test in a subdirectory. |
| 646 | path = UTF8ToFilePath("path/to/file.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 647 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 648 | bool exclusive = true; |
| 649 | bool recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 650 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 651 | context.get(), path.DirName(), exclusive, recursive)); |
| 652 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 653 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 654 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 655 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 656 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 657 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 658 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 659 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 660 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 663 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 664 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 665 | |
| 666 | bool exclusive = false; |
| 667 | bool recursive = false; |
| 668 | FilePath path = UTF8ToFilePath("foo/bar"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 669 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 670 | context.get(), path, exclusive, recursive)); |
| 671 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 672 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 673 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 674 | ofu()->DeleteSingleDirectory(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 675 | |
| 676 | FilePath root = UTF8ToFilePath(""); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 677 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 678 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 679 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 680 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[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_TRUE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 683 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 684 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 685 | exclusive = false; |
| 686 | recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 687 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 688 | context.get(), path, exclusive, recursive)); |
| 689 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 690 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 691 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 692 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 693 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 694 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 695 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 696 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 697 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 698 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 699 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 700 | |
| 701 | // Can't remove a non-empty directory. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 702 | context.reset(NewContext(NULL)); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 703 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 704 | ofu()->DeleteSingleDirectory(context.get(), path.DirName())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 705 | |
| 706 | base::PlatformFileInfo file_info; |
| 707 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 708 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 709 | context.get(), path, &file_info, &local_path)); |
| 710 | EXPECT_TRUE(local_path.empty()); |
| 711 | EXPECT_TRUE(file_info.is_directory); |
| 712 | EXPECT_FALSE(file_info.is_symbolic_link); |
| 713 | |
| 714 | // Same create again should succeed, since exclusive is false. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 715 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 716 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 717 | context.get(), path, exclusive, recursive)); |
| 718 | |
| 719 | exclusive = true; |
| 720 | recursive = true; |
[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_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 723 | context.get(), path, exclusive, recursive)); |
| 724 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 725 | // Verify that deleting a directory isn't stopped by zero quota, and that it |
| 726 | // frees up quota from its path. |
| 727 | context.reset(NewContext(NULL)); |
| 728 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 729 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 730 | ofu()->DeleteSingleDirectory(context.get(), path)); |
| 731 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 732 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 733 | |
| 734 | path = UTF8ToFilePath("foo/bop"); |
| 735 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 736 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 737 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 738 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 739 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 740 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 741 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
| 742 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 743 | context.get(), path, &file_info, &local_path)); |
| 744 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 745 | // Verify that file creation requires sufficient quota for the path. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 746 | exclusive = true; |
| 747 | recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 748 | context.reset(NewContext(NULL)); |
| 749 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 750 | ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
| 751 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 752 | context.get(), path, exclusive, recursive)); |
| 753 | |
| 754 | context.reset(NewContext(NULL)); |
| 755 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 756 | ObfuscatedFileUtil::ComputeFilePathCost(path)); |
| 757 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 758 | context.get(), path, exclusive, recursive)); |
| 759 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 760 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 761 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 762 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 763 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 764 | |
| 765 | exclusive = true; |
| 766 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 767 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 768 | context.get(), path, exclusive, recursive)); |
| 769 | |
| 770 | exclusive = true; |
| 771 | recursive = false; |
| 772 | path = UTF8ToFilePath("foo"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 773 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 774 | context.get(), path, exclusive, recursive)); |
| 775 | |
| 776 | path = UTF8ToFilePath("blah"); |
| 777 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 778 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 779 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 780 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 781 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 782 | |
| 783 | exclusive = true; |
| 784 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 785 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 786 | context.get(), path, exclusive, recursive)); |
| 787 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 788 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 789 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 790 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 791 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 792 | |
| 793 | exclusive = true; |
| 794 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 795 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 796 | context.get(), path, exclusive, recursive)); |
| 797 | } |
| 798 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 799 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 800 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 801 | bool exclusive = true; |
| 802 | bool recursive = true; |
| 803 | FilePath path = UTF8ToFilePath("directory/to/use"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 804 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 805 | context.get(), path, exclusive, recursive)); |
| 806 | TestReadDirectoryHelper(path); |
| 807 | } |
| 808 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 809 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 810 | TestReadDirectoryHelper(UTF8ToFilePath("")); |
| 811 | } |
| 812 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 813 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 814 | TestReadDirectoryHelper(UTF8ToFilePath("/")); |
| 815 | } |
| 816 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 817 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 818 | FilePath path = UTF8ToFilePath("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 819 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 820 | |
| 821 | bool created = false; |
| 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_TRUE(created); |
| 825 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 826 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 827 | std::vector<base::FileUtilProxy::Entry> entries; |
| 828 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 829 | ofu()->ReadDirectory(context.get(), path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 830 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 831 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 832 | } |
| 833 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 834 | TEST_F(ObfuscatedFileUtilTest, TestTouch) { |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 835 | FilePath path = UTF8ToFilePath("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 836 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 837 | |
| 838 | base::Time last_access_time = base::Time::Now(); |
| 839 | base::Time last_modified_time = base::Time::Now(); |
| 840 | |
| 841 | // It's not there yet. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 842 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 843 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 844 | context.get(), path, last_access_time, last_modified_time)); |
| 845 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 846 | // OK, now create it. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 847 | context.reset(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 848 | bool created = false; |
| 849 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 850 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 851 | ASSERT_TRUE(created); |
| 852 | TestTouchHelper(path, true); |
| 853 | |
| 854 | // Now test a directory: |
| 855 | context.reset(NewContext(NULL)); |
| 856 | bool exclusive = true; |
| 857 | bool recursive = false; |
| 858 | path = UTF8ToFilePath("dir"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 859 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(), |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 860 | path, exclusive, recursive)); |
| 861 | TestTouchHelper(path, false); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 862 | } |
| 863 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 864 | TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 865 | FilePath path = UTF8ToFilePath("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 866 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 867 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 868 | path = UTF8ToFilePath("file name"); |
| 869 | context->set_allowed_bytes_growth(5); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 870 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 871 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 872 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 873 | EXPECT_FALSE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 874 | context->set_allowed_bytes_growth(1024); |
| 875 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 876 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 877 | EXPECT_TRUE(created); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 878 | int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 879 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
| 880 | |
| 881 | context->set_allowed_bytes_growth(1024); |
| 882 | bool exclusive = true; |
| 883 | bool recursive = true; |
| 884 | path = UTF8ToFilePath("directory/to/use"); |
| 885 | std::vector<FilePath::StringType> components; |
| 886 | path.GetComponents(&components); |
| 887 | path_cost = 0; |
| 888 | for (std::vector<FilePath::StringType>::iterator iter = components.begin(); |
| 889 | iter != components.end(); ++iter) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 890 | path_cost += ObfuscatedFileUtil::ComputeFilePathCost( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 891 | FilePath(*iter)); |
| 892 | } |
| 893 | context.reset(NewContext(NULL)); |
| 894 | context->set_allowed_bytes_growth(1024); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 895 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 896 | context.get(), path, exclusive, recursive)); |
| 897 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 898 | } |
| 899 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 900 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 901 | FilePath source_path = UTF8ToFilePath("path0.txt"); |
| 902 | FilePath dest_path = UTF8ToFilePath("path1.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 903 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 904 | |
| 905 | bool is_copy_not_move = false; |
| 906 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 907 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 908 | is_copy_not_move)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 909 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 910 | is_copy_not_move = true; |
| 911 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 912 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 913 | is_copy_not_move)); |
| 914 | source_path = UTF8ToFilePath("dir/dir/file"); |
| 915 | bool exclusive = true; |
| 916 | bool recursive = true; |
[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 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 919 | context.get(), source_path.DirName(), exclusive, recursive)); |
| 920 | is_copy_not_move = false; |
| 921 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 922 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 923 | is_copy_not_move)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 924 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 925 | is_copy_not_move = true; |
| 926 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 927 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 928 | is_copy_not_move)); |
| 929 | } |
| 930 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 931 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 932 | const int64 kSourceLength = 5; |
| 933 | const int64 kDestLength = 50; |
| 934 | |
| 935 | for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { |
| 936 | SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); |
| 937 | const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; |
| 938 | SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << |
| 939 | test_case.is_copy_not_move); |
| 940 | SCOPED_TRACE(testing::Message() << "\t source_path " << |
| 941 | test_case.source_path); |
| 942 | SCOPED_TRACE(testing::Message() << "\t dest_path " << |
| 943 | test_case.dest_path); |
| 944 | SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << |
| 945 | test_case.cause_overwrite); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 946 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 947 | |
| 948 | bool exclusive = false; |
| 949 | bool recursive = true; |
| 950 | FilePath source_path = UTF8ToFilePath(test_case.source_path); |
| 951 | FilePath dest_path = UTF8ToFilePath(test_case.dest_path); |
| 952 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 953 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 954 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 955 | context.get(), source_path.DirName(), exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 956 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 957 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 958 | context.get(), dest_path.DirName(), exclusive, recursive)); |
| 959 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 960 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 961 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 962 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 963 | ofu()->EnsureFileExists(context.get(), source_path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 964 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 965 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 966 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 967 | ofu()->Truncate(context.get(), source_path, kSourceLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 968 | |
| 969 | if (test_case.cause_overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 970 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 971 | created = false; |
| 972 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 973 | ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 974 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 975 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 976 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 977 | ofu()->Truncate(context.get(), dest_path, kDestLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 978 | } |
| 979 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 980 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 981 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(), |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 982 | source_path, dest_path, test_case.is_copy_not_move)); |
| 983 | if (test_case.is_copy_not_move) { |
| 984 | base::PlatformFileInfo file_info; |
| 985 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 986 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 987 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 988 | context.get(), source_path, &file_info, &local_path)); |
| 989 | EXPECT_EQ(kSourceLength, file_info.size); |
| 990 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 991 | ofu()->DeleteFile(context.get(), source_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 992 | } else { |
| 993 | base::PlatformFileInfo file_info; |
| 994 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 995 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 996 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 997 | context.get(), source_path, &file_info, &local_path)); |
| 998 | } |
| 999 | base::PlatformFileInfo file_info; |
| 1000 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1001 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1002 | context.get(), dest_path, &file_info, &local_path)); |
| 1003 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1004 | |
| 1005 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1006 | ofu()->DeleteFile(context.get(), dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1010 | TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1011 | FilePath src_path = UTF8ToFilePath("src path"); |
| 1012 | FilePath dest_path = UTF8ToFilePath("destination path"); |
| 1013 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1014 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1015 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1016 | context.get(), src_path, &created)); |
| 1017 | |
| 1018 | bool is_copy = true; |
| 1019 | // Copy, no overwrite. |
| 1020 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1021 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1022 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1023 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1024 | context.reset(NewContext(NULL)); |
| 1025 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1026 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1027 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1028 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1029 | |
| 1030 | // Copy, with overwrite. |
| 1031 | context.reset(NewContext(NULL)); |
| 1032 | context->set_allowed_bytes_growth(0); |
| 1033 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1034 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1035 | } |
| 1036 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1037 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1038 | FilePath src_path = UTF8ToFilePath("src path"); |
| 1039 | FilePath dest_path = UTF8ToFilePath("destination path"); |
| 1040 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1041 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1042 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1043 | context.get(), src_path, &created)); |
| 1044 | |
| 1045 | bool is_copy = false; |
| 1046 | // Move, rename, no overwrite. |
| 1047 | context.reset(NewContext(NULL)); |
| 1048 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1049 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - |
| 1050 | ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1051 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1052 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1053 | context.reset(NewContext(NULL)); |
| 1054 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1055 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - |
| 1056 | ObfuscatedFileUtil::ComputeFilePathCost(src_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1057 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1058 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1059 | |
| 1060 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1061 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1062 | context.get(), src_path, &created)); |
| 1063 | |
| 1064 | // Move, rename, with overwrite. |
| 1065 | context.reset(NewContext(NULL)); |
| 1066 | context->set_allowed_bytes_growth(0); |
| 1067 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1068 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1069 | } |
| 1070 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1071 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1072 | FilePath src_path = UTF8ToFilePath("src path"); |
| 1073 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1074 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1075 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1076 | context.get(), src_path, &created)); |
| 1077 | |
| 1078 | bool exclusive = true; |
| 1079 | bool recursive = false; |
| 1080 | FilePath dir_path = UTF8ToFilePath("directory path"); |
| 1081 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1082 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1083 | context.get(), dir_path, exclusive, recursive)); |
| 1084 | |
| 1085 | FilePath dest_path = dir_path.Append(src_path); |
| 1086 | |
| 1087 | bool is_copy = false; |
| 1088 | int64 allowed_bytes_growth = -1000; // Over quota, this should still work. |
| 1089 | // Move, no rename, no overwrite. |
| 1090 | context.reset(NewContext(NULL)); |
| 1091 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1092 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1093 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1094 | EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); |
| 1095 | |
| 1096 | // Move, no rename, with overwrite. |
| 1097 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1098 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1099 | context.get(), src_path, &created)); |
| 1100 | context.reset(NewContext(NULL)); |
| 1101 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1102 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1103 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1104 | EXPECT_EQ( |
| 1105 | allowed_bytes_growth + |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1106 | ObfuscatedFileUtil::ComputeFilePathCost(src_path), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1107 | context->allowed_bytes_growth()); |
| 1108 | } |
| 1109 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1110 | TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) { |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 1111 | TestCopyInForeignFileHelper(false /* overwrite */); |
| 1112 | TestCopyInForeignFileHelper(true /* overwrite */); |
| 1113 | } |
| 1114 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1115 | TEST_F(ObfuscatedFileUtilTest, TestEnumerator) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1116 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1117 | FilePath src_path = UTF8ToFilePath("source dir"); |
| 1118 | bool exclusive = true; |
| 1119 | bool recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1120 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1121 | context.get(), src_path, exclusive, recursive)); |
| 1122 | |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 1123 | std::set<FilePath::StringType> files; |
| 1124 | std::set<FilePath::StringType> directories; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1125 | FillTestDirectory(src_path, &files, &directories); |
| 1126 | |
| 1127 | FilePath dest_path = UTF8ToFilePath("destination dir"); |
| 1128 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1129 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1130 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1131 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1132 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1133 | ofu()->Copy(context.get(), src_path, dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1134 | |
| 1135 | ValidateTestDirectory(dest_path, files, directories); |
[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 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1138 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1139 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1140 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1141 | recursive = true; |
| 1142 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1143 | ofu()->Delete(context.get(), dest_path, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1144 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1145 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1146 | } |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1147 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1148 | TEST_F(ObfuscatedFileUtilTest, TestMigration) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1149 | ScopedTempDir source_dir; |
| 1150 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 1151 | FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); |
| 1152 | ASSERT_TRUE(file_util::CreateDirectory(root_path)); |
| 1153 | |
| 1154 | for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1155 | SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
| 1156 | const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1157 | FilePath local_src_path = root_path.Append(test_case.path); |
| 1158 | if (test_case.is_directory) { |
| 1159 | ASSERT_TRUE( |
| 1160 | file_util::CreateDirectory(local_src_path)); |
| 1161 | } else { |
| 1162 | base::PlatformFileError error_code; |
| 1163 | bool created = false; |
| 1164 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 1165 | base::PlatformFile file_handle = |
| 1166 | base::CreatePlatformFile( |
| 1167 | local_src_path, file_flags, &created, &error_code); |
| 1168 | EXPECT_TRUE(created); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 1169 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1170 | ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 1171 | ASSERT_TRUE( |
| 1172 | base::TruncatePlatformFile(file_handle, test_case.data_file_size)); |
| 1173 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1174 | } |
| 1175 | } |
| 1176 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1177 | EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1178 | |
| 1179 | FilePath new_root = |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1180 | test_directory().AppendASCII("File System").AppendASCII("000").Append( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1181 | ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1182 | for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1183 | SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); |
| 1184 | const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1185 | FilePath local_data_path = new_root.Append(test_case.path); |
| 1186 | #if defined(OS_WIN) |
| 1187 | local_data_path = local_data_path.NormalizeWindowsPathSeparators(); |
| 1188 | #endif |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1189 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1190 | base::PlatformFileInfo ofu_file_info; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1191 | FilePath data_path; |
| 1192 | SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); |
| 1193 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1194 | ofu()->GetFileInfo(context.get(), FilePath(test_case.path), |
| 1195 | &ofu_file_info, &data_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1196 | if (test_case.is_directory) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1197 | EXPECT_TRUE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1198 | } else { |
| 1199 | base::PlatformFileInfo platform_file_info; |
| 1200 | SCOPED_TRACE(testing::Message() << "local_data_path is " << |
| 1201 | local_data_path.value()); |
| 1202 | SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); |
| 1203 | ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); |
| 1204 | EXPECT_EQ(test_case.data_file_size, platform_file_info.size); |
| 1205 | EXPECT_FALSE(platform_file_info.is_directory); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1206 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1207 | EXPECT_EQ(local_data_path, data_path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1208 | EXPECT_EQ(platform_file_info.size, ofu_file_info.size); |
| 1209 | EXPECT_FALSE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1213 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1214 | TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) { |
| 1215 | scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> |
| 1216 | enumerator(ofu()->CreateOriginEnumerator()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1217 | // The test helper starts out with a single filesystem. |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1218 | EXPECT_TRUE(enumerator.get()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1219 | EXPECT_EQ(origin(), enumerator->Next()); |
| 1220 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1221 | EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1222 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1223 | EXPECT_EQ(GURL(), enumerator->Next()); |
| 1224 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1225 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1226 | |
| 1227 | std::set<GURL> origins_expected; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1228 | origins_expected.insert(origin()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1229 | |
| 1230 | for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { |
| 1231 | SCOPED_TRACE(testing::Message() << |
| 1232 | "Validating kOriginEnumerationTestRecords " << i); |
| 1233 | const OriginEnumerationTestRecord& record = |
| 1234 | kOriginEnumerationTestRecords[i]; |
| 1235 | GURL origin_url(record.origin_url); |
| 1236 | origins_expected.insert(origin_url); |
| 1237 | if (record.has_temporary) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1238 | scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1239 | NewHelper(origin_url, kFileSystemTypeTemporary)); |
| 1240 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1241 | context->set_src_origin_url(origin_url); |
| 1242 | context->set_src_type(kFileSystemTypeTemporary); |
| 1243 | bool created = false; |
| 1244 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1245 | ofu()->EnsureFileExists(context.get(), |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1246 | FilePath().AppendASCII("file"), &created)); |
| 1247 | EXPECT_TRUE(created); |
| 1248 | } |
| 1249 | if (record.has_persistent) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1250 | scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1251 | NewHelper(origin_url, kFileSystemTypePersistent)); |
| 1252 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1253 | context->set_src_origin_url(origin_url); |
| 1254 | context->set_src_type(kFileSystemTypePersistent); |
| 1255 | bool created = false; |
| 1256 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1257 | ofu()->EnsureFileExists(context.get(), |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1258 | FilePath().AppendASCII("file"), &created)); |
| 1259 | EXPECT_TRUE(created); |
| 1260 | } |
| 1261 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1262 | enumerator.reset(ofu()->CreateOriginEnumerator()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1263 | EXPECT_TRUE(enumerator.get()); |
| 1264 | std::set<GURL> origins_found; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1265 | GURL origin_url; |
| 1266 | while (!(origin_url = enumerator->Next()).is_empty()) { |
| 1267 | origins_found.insert(origin_url); |
| 1268 | SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1269 | bool found = false; |
| 1270 | for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); |
| 1271 | ++i) { |
| 1272 | const OriginEnumerationTestRecord& record = |
| 1273 | kOriginEnumerationTestRecords[i]; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1274 | if (GURL(record.origin_url) != origin_url) |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1275 | continue; |
| 1276 | found = true; |
| 1277 | EXPECT_EQ(record.has_temporary, |
| 1278 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1279 | EXPECT_EQ(record.has_persistent, |
| 1280 | enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1281 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1282 | // Deal with the default filesystem created by the test helper. |
| 1283 | if (!found && origin_url == origin()) { |
| 1284 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1285 | EXPECT_EQ(true, |
| 1286 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1287 | EXPECT_EQ(false, |
| 1288 | enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1289 | found = true; |
| 1290 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1291 | EXPECT_TRUE(found); |
| 1292 | } |
| 1293 | |
| 1294 | std::set<GURL> diff; |
| 1295 | std::set_symmetric_difference(origins_expected.begin(), |
| 1296 | origins_expected.end(), origins_found.begin(), origins_found.end(), |
| 1297 | inserter(diff, diff.begin())); |
| 1298 | EXPECT_TRUE(diff.empty()); |
| 1299 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1300 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1301 | TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1302 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1303 | |
| 1304 | int64 expected_quota = 0; |
| 1305 | |
| 1306 | for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1307 | SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
| 1308 | const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1309 | FilePath path(test_case.path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1310 | expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1311 | if (test_case.is_directory) { |
| 1312 | bool exclusive = true; |
| 1313 | bool recursive = false; |
| 1314 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1315 | ofu()->CreateDirectory(context.get(), path, exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1316 | } else { |
| 1317 | bool created = false; |
| 1318 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1319 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1320 | ASSERT_TRUE(created); |
| 1321 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1322 | ofu()->Truncate(context.get(), path, |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1323 | test_case.data_file_size)); |
| 1324 | expected_quota += test_case.data_file_size; |
| 1325 | } |
| 1326 | } |
| 1327 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
| 1328 | RevokeUsageCache(); |
| 1329 | EXPECT_EQ(-1, SizeInUsageFile()); |
| 1330 | GetUsageFromQuotaManager(); |
| 1331 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
| 1332 | EXPECT_EQ(expected_quota, usage()); |
| 1333 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1334 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1335 | TEST_F(ObfuscatedFileUtilTest, TestInconsistency) { |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1336 | const FilePath kPath1 = FilePath().AppendASCII("hoge"); |
| 1337 | const FilePath kPath2 = FilePath().AppendASCII("fuga"); |
| 1338 | |
| 1339 | scoped_ptr<FileSystemOperationContext> context; |
| 1340 | base::PlatformFile file; |
| 1341 | base::PlatformFileInfo file_info; |
| 1342 | FilePath data_path; |
| 1343 | bool created = false; |
| 1344 | |
| 1345 | // Create a non-empty file. |
| 1346 | context.reset(NewContext(NULL)); |
| 1347 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1348 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1349 | EXPECT_TRUE(created); |
| 1350 | context.reset(NewContext(NULL)); |
| 1351 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1352 | ofu()->Truncate(context.get(), kPath1, 10)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1353 | context.reset(NewContext(NULL)); |
| 1354 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1355 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1356 | context.get(), kPath1, &file_info, &data_path)); |
| 1357 | EXPECT_EQ(10, file_info.size); |
| 1358 | |
| 1359 | // Destroy database to make inconsistency between database and filesystem. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1360 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1361 | |
| 1362 | // Try to get file info of broken file. |
| 1363 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1364 | EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1365 | context.reset(NewContext(NULL)); |
| 1366 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1367 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1368 | EXPECT_TRUE(created); |
| 1369 | context.reset(NewContext(NULL)); |
| 1370 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1371 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1372 | context.get(), kPath1, &file_info, &data_path)); |
| 1373 | EXPECT_EQ(0, file_info.size); |
| 1374 | |
| 1375 | // Make another broken file to |kPath2|. |
| 1376 | context.reset(NewContext(NULL)); |
| 1377 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1378 | ofu()->EnsureFileExists(context.get(), kPath2, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1379 | EXPECT_TRUE(created); |
| 1380 | |
| 1381 | // Destroy again. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1382 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1383 | |
| 1384 | // Repair broken |kPath1|. |
| 1385 | context.reset(NewContext(NULL)); |
| 1386 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1387 | ofu()->Touch(context.get(), kPath1, base::Time::Now(), |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1388 | base::Time::Now())); |
| 1389 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1390 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1391 | EXPECT_TRUE(created); |
| 1392 | |
| 1393 | // Copy from sound |kPath1| to broken |kPath2|. |
| 1394 | context.reset(NewContext(NULL)); |
| 1395 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1396 | ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2, |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1397 | true /* copy */)); |
| 1398 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1399 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1400 | context.reset(NewContext(NULL)); |
| 1401 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1402 | ofu()->CreateOrOpen( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1403 | context.get(), kPath1, |
| 1404 | base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE, |
| 1405 | &file, &created)); |
| 1406 | EXPECT_TRUE(created); |
| 1407 | EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); |
| 1408 | EXPECT_EQ(0, file_info.size); |
| 1409 | EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 1410 | } |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1411 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1412 | TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) { |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1413 | const FilePath kPath[] = { |
| 1414 | FilePath().AppendASCII("foo"), |
| 1415 | FilePath().AppendASCII("bar"), |
| 1416 | FilePath().AppendASCII("baz") |
| 1417 | }; |
| 1418 | scoped_ptr<FileSystemOperationContext> context; |
| 1419 | |
| 1420 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) { |
| 1421 | bool created = false; |
| 1422 | context.reset(NewContext(NULL)); |
| 1423 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1424 | ofu()->EnsureFileExists(context.get(), kPath[i], &created)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1425 | EXPECT_TRUE(created); |
| 1426 | } |
| 1427 | |
| 1428 | context.reset(NewContext(NULL)); |
| 1429 | std::vector<base::FileUtilProxy::Entry> entries; |
| 1430 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1431 | ofu()->ReadDirectory(context.get(), FilePath(), &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1432 | EXPECT_EQ(3u, entries.size()); |
| 1433 | |
| 1434 | context.reset(NewContext(NULL)); |
| 1435 | FilePath local_path; |
| 1436 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1437 | ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1438 | EXPECT_TRUE(file_util::Delete(local_path, false)); |
| 1439 | |
| 1440 | context.reset(NewContext(NULL)); |
| 1441 | entries.clear(); |
| 1442 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame^] | 1443 | ofu()->ReadDirectory(context.get(), FilePath(), &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1444 | EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); |
| 1445 | } |