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