[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 | |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 10 | #include "base/file_path.h" |
| 11 | #include "base/file_util.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 13 | #include "base/message_loop.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 14 | #include "base/platform_file.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 15 | #include "base/scoped_temp_dir.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 16 | #include "base/sys_string_conversions.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | #include "webkit/fileapi/file_system_context.h" |
| 19 | #include "webkit/fileapi/file_system_operation_context.h" |
[email protected] | 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] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 136 | weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 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(), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 215 | base::Bind(&ObfuscatedFileUtilTest::OnGetUsage, |
| 216 | weak_factory_.GetWeakPtr())); |
[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] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame^] | 479 | void ClearTimestamp(const FilePath& path) { |
| 480 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 481 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 482 | ofu()->Touch(context.get(), path, base::Time(), base::Time())); |
| 483 | EXPECT_EQ(base::Time(), GetModifiedTime(path)); |
| 484 | } |
| 485 | |
| 486 | base::Time GetModifiedTime(const FilePath& path) { |
| 487 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 488 | FilePath data_path; |
| 489 | base::PlatformFileInfo file_info; |
| 490 | context.reset(NewContext(NULL)); |
| 491 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 492 | ofu()->GetFileInfo(context.get(), path, &file_info, &data_path)); |
| 493 | return file_info.last_modified; |
| 494 | } |
| 495 | |
| 496 | void TestDirectoryTimestampHelper(const FilePath& base_dir, |
| 497 | bool copy, |
| 498 | bool overwrite) { |
| 499 | scoped_ptr<FileSystemOperationContext> context; |
| 500 | const FilePath src_dir_path(base_dir.AppendASCII("foo_dir")); |
| 501 | const FilePath dest_dir_path(base_dir.AppendASCII("bar_dir")); |
| 502 | |
| 503 | const FilePath src_file_path(src_dir_path.AppendASCII("hoge")); |
| 504 | const FilePath dest_file_path(dest_dir_path.AppendASCII("fuga")); |
| 505 | |
| 506 | context.reset(NewContext(NULL)); |
| 507 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 508 | ofu()->CreateDirectory(context.get(), src_dir_path, true, true)); |
| 509 | context.reset(NewContext(NULL)); |
| 510 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 511 | ofu()->CreateDirectory(context.get(), dest_dir_path, true, true)); |
| 512 | |
| 513 | bool created = false; |
| 514 | context.reset(NewContext(NULL)); |
| 515 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 516 | ofu()->EnsureFileExists(context.get(), src_file_path, &created)); |
| 517 | if (overwrite) { |
| 518 | context.reset(NewContext(NULL)); |
| 519 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 520 | ofu()->EnsureFileExists(context.get(), |
| 521 | dest_file_path, &created)); |
| 522 | } |
| 523 | |
| 524 | ClearTimestamp(src_dir_path); |
| 525 | ClearTimestamp(dest_dir_path); |
| 526 | context.reset(NewContext(NULL)); |
| 527 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 528 | ofu()->CopyOrMoveFile(context.get(), |
| 529 | src_file_path, dest_file_path, |
| 530 | copy)); |
| 531 | |
| 532 | if (copy) |
| 533 | EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_path)); |
| 534 | else |
| 535 | EXPECT_NE(base::Time(), GetModifiedTime(src_dir_path)); |
| 536 | EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_path)); |
| 537 | } |
| 538 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 539 | private: |
| 540 | ScopedTempDir data_dir_; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 541 | scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 542 | scoped_refptr<quota::QuotaManager> quota_manager_; |
| 543 | scoped_refptr<FileSystemContext> file_system_context_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 544 | GURL origin_; |
| 545 | fileapi::FileSystemType type_; |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 546 | base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 547 | FileSystemTestOriginHelper test_helper_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 548 | quota::QuotaStatusCode quota_status_; |
| 549 | int64 usage_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 550 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 551 | DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 552 | }; |
| 553 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 554 | TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 555 | base::PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 556 | bool created; |
| 557 | FilePath path = UTF8ToFilePath("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 558 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 559 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 560 | |
| 561 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 562 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 563 | context.get(), path, file_flags, &file_handle, |
| 564 | &created)); |
| 565 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 566 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 567 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 568 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 569 | |
| 570 | path = UTF8ToFilePath("test file"); |
| 571 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 572 | // Verify that file creation requires sufficient quota for the path. |
| 573 | context.reset(NewContext(NULL)); |
| 574 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 575 | ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 576 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 577 | ofu()->CreateOrOpen( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 578 | context.get(), path, file_flags, &file_handle, &created)); |
| 579 | |
| 580 | context.reset(NewContext(NULL)); |
| 581 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 582 | ObfuscatedFileUtil::ComputeFilePathCost(path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 583 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 584 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 585 | context.get(), path, file_flags, &file_handle, &created)); |
| 586 | ASSERT_TRUE(created); |
| 587 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 588 | |
| 589 | CheckFileAndCloseHandle(path, file_handle); |
| 590 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 591 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 592 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 593 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 594 | context.get(), path, &local_path)); |
| 595 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 596 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 597 | // Verify that deleting a file isn't stopped by zero quota, and that it frees |
| 598 | // up quote from its path. |
| 599 | context.reset(NewContext(NULL)); |
| 600 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 601 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 602 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 603 | EXPECT_FALSE(file_util::PathExists(local_path)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 604 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 605 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 606 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 607 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 608 | bool exclusive = true; |
| 609 | bool recursive = true; |
| 610 | FilePath directory_path = UTF8ToFilePath("series/of/directories"); |
| 611 | path = directory_path.AppendASCII("file name"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 612 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 613 | context.get(), directory_path, exclusive, recursive)); |
| 614 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 615 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 616 | file_handle = base::kInvalidPlatformFileValue; |
| 617 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 618 | ofu()->CreateOrOpen( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 619 | context.get(), path, file_flags, &file_handle, &created)); |
| 620 | ASSERT_TRUE(created); |
| 621 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 622 | |
| 623 | CheckFileAndCloseHandle(path, file_handle); |
| 624 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 625 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 626 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 627 | context.get(), path, &local_path)); |
| 628 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 629 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 630 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 631 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 632 | ofu()->DeleteFile(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 633 | EXPECT_FALSE(file_util::PathExists(local_path)); |
| 634 | } |
| 635 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 636 | TEST_F(ObfuscatedFileUtilTest, TestTruncate) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 637 | bool created = false; |
| 638 | FilePath path = UTF8ToFilePath("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 639 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 640 | |
| 641 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 642 | ofu()->Truncate(context.get(), path, 4)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 643 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 644 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 645 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 646 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 647 | ASSERT_TRUE(created); |
| 648 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 649 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 650 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 651 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 652 | context.get(), path, &local_path)); |
| 653 | EXPECT_EQ(0, GetSize(local_path)); |
| 654 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 655 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 656 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 657 | context.get(), path, 10)); |
| 658 | EXPECT_EQ(10, GetSize(local_path)); |
| 659 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 660 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 661 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 662 | context.get(), path, 1)); |
| 663 | EXPECT_EQ(1, GetSize(local_path)); |
| 664 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 665 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 666 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 667 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 668 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 669 | } |
| 670 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 671 | TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 672 | FilePath path = UTF8ToFilePath("fake/file"); |
| 673 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 674 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 675 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 676 | ofu()->EnsureFileExists( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 677 | context.get(), path, &created)); |
| 678 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 679 | // Verify that file creation requires sufficient quota for the path. |
| 680 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 681 | path = UTF8ToFilePath("test file"); |
| 682 | created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 683 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 684 | ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 685 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 686 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 687 | ASSERT_FALSE(created); |
| 688 | |
| 689 | context.reset(NewContext(NULL)); |
| 690 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 691 | ObfuscatedFileUtil::ComputeFilePathCost(path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 692 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 693 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 694 | ASSERT_TRUE(created); |
| 695 | |
| 696 | CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); |
| 697 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 698 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 699 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 700 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 701 | ASSERT_FALSE(created); |
| 702 | |
| 703 | // Also test in a subdirectory. |
| 704 | path = UTF8ToFilePath("path/to/file.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 705 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 706 | bool exclusive = true; |
| 707 | bool recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 708 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 709 | context.get(), path.DirName(), exclusive, recursive)); |
| 710 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 711 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 712 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 713 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 714 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 715 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 716 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 717 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 718 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 719 | } |
| 720 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 721 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 722 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 723 | |
| 724 | bool exclusive = false; |
| 725 | bool recursive = false; |
| 726 | FilePath path = UTF8ToFilePath("foo/bar"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 727 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 728 | context.get(), path, exclusive, recursive)); |
| 729 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 730 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 731 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 732 | ofu()->DeleteSingleDirectory(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 733 | |
| 734 | FilePath root = UTF8ToFilePath(""); |
[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(), root)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 741 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 742 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 743 | exclusive = false; |
| 744 | recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 745 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 746 | context.get(), path, exclusive, recursive)); |
| 747 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 748 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 749 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 750 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 751 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 752 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 753 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 754 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 755 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 756 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 757 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 758 | |
| 759 | // Can't remove a non-empty directory. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 760 | context.reset(NewContext(NULL)); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 761 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 762 | ofu()->DeleteSingleDirectory(context.get(), path.DirName())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 763 | |
| 764 | base::PlatformFileInfo file_info; |
| 765 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 766 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 767 | context.get(), path, &file_info, &local_path)); |
| 768 | EXPECT_TRUE(local_path.empty()); |
| 769 | EXPECT_TRUE(file_info.is_directory); |
| 770 | EXPECT_FALSE(file_info.is_symbolic_link); |
| 771 | |
| 772 | // Same create again should succeed, since exclusive is false. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 773 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 774 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 775 | context.get(), path, exclusive, recursive)); |
| 776 | |
| 777 | exclusive = true; |
| 778 | recursive = true; |
[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_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 781 | context.get(), path, exclusive, recursive)); |
| 782 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 783 | // Verify that deleting a directory isn't stopped by zero quota, and that it |
| 784 | // frees up quota from its path. |
| 785 | context.reset(NewContext(NULL)); |
| 786 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 787 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 788 | ofu()->DeleteSingleDirectory(context.get(), path)); |
| 789 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 790 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 791 | |
| 792 | path = UTF8ToFilePath("foo/bop"); |
| 793 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 794 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 795 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 796 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 797 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 798 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 799 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
| 800 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 801 | context.get(), path, &file_info, &local_path)); |
| 802 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 803 | // Verify that file creation requires sufficient quota for the path. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 804 | exclusive = true; |
| 805 | recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 806 | context.reset(NewContext(NULL)); |
| 807 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 808 | ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
| 809 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 810 | context.get(), path, exclusive, recursive)); |
| 811 | |
| 812 | context.reset(NewContext(NULL)); |
| 813 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 814 | ObfuscatedFileUtil::ComputeFilePathCost(path)); |
| 815 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 816 | context.get(), path, exclusive, recursive)); |
| 817 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 818 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 819 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 820 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 821 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 822 | |
| 823 | exclusive = true; |
| 824 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 825 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 826 | context.get(), path, exclusive, recursive)); |
| 827 | |
| 828 | exclusive = true; |
| 829 | recursive = false; |
| 830 | path = UTF8ToFilePath("foo"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 831 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 832 | context.get(), path, exclusive, recursive)); |
| 833 | |
| 834 | path = UTF8ToFilePath("blah"); |
| 835 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 836 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 837 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 838 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 839 | EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 840 | |
| 841 | exclusive = true; |
| 842 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 843 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 844 | context.get(), path, exclusive, recursive)); |
| 845 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 846 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 847 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 848 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 849 | EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 850 | |
| 851 | exclusive = true; |
| 852 | recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 853 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 854 | context.get(), path, exclusive, recursive)); |
| 855 | } |
| 856 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 857 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 858 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 859 | bool exclusive = true; |
| 860 | bool recursive = true; |
| 861 | FilePath path = UTF8ToFilePath("directory/to/use"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 862 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 863 | context.get(), path, exclusive, recursive)); |
| 864 | TestReadDirectoryHelper(path); |
| 865 | } |
| 866 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 867 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 868 | TestReadDirectoryHelper(UTF8ToFilePath("")); |
| 869 | } |
| 870 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 871 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 872 | TestReadDirectoryHelper(UTF8ToFilePath("/")); |
| 873 | } |
| 874 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 875 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 876 | FilePath path = UTF8ToFilePath("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 877 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 878 | |
| 879 | bool created = false; |
| 880 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 881 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 882 | ASSERT_TRUE(created); |
| 883 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 884 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 885 | std::vector<base::FileUtilProxy::Entry> entries; |
| 886 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 887 | ofu()->ReadDirectory(context.get(), path, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 888 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 889 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 890 | } |
| 891 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 892 | TEST_F(ObfuscatedFileUtilTest, TestTouch) { |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 893 | FilePath path = UTF8ToFilePath("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 894 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 895 | |
| 896 | base::Time last_access_time = base::Time::Now(); |
| 897 | base::Time last_modified_time = base::Time::Now(); |
| 898 | |
| 899 | // It's not there yet. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 900 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 901 | ofu()->Touch( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 902 | context.get(), path, last_access_time, last_modified_time)); |
| 903 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 904 | // OK, now create it. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 905 | context.reset(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 906 | bool created = false; |
| 907 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 908 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 909 | ASSERT_TRUE(created); |
| 910 | TestTouchHelper(path, true); |
| 911 | |
| 912 | // Now test a directory: |
| 913 | context.reset(NewContext(NULL)); |
| 914 | bool exclusive = true; |
| 915 | bool recursive = false; |
| 916 | path = UTF8ToFilePath("dir"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 917 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(), |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 918 | path, exclusive, recursive)); |
| 919 | TestTouchHelper(path, false); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 920 | } |
| 921 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 922 | TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 923 | FilePath path = UTF8ToFilePath("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 924 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 925 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 926 | path = UTF8ToFilePath("file name"); |
| 927 | context->set_allowed_bytes_growth(5); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 928 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 929 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 930 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 931 | EXPECT_FALSE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 932 | context->set_allowed_bytes_growth(1024); |
| 933 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 934 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 935 | EXPECT_TRUE(created); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 936 | int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 937 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
| 938 | |
| 939 | context->set_allowed_bytes_growth(1024); |
| 940 | bool exclusive = true; |
| 941 | bool recursive = true; |
| 942 | path = UTF8ToFilePath("directory/to/use"); |
| 943 | std::vector<FilePath::StringType> components; |
| 944 | path.GetComponents(&components); |
| 945 | path_cost = 0; |
| 946 | for (std::vector<FilePath::StringType>::iterator iter = components.begin(); |
| 947 | iter != components.end(); ++iter) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 948 | path_cost += ObfuscatedFileUtil::ComputeFilePathCost( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 949 | FilePath(*iter)); |
| 950 | } |
| 951 | context.reset(NewContext(NULL)); |
| 952 | context->set_allowed_bytes_growth(1024); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 953 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 954 | context.get(), path, exclusive, recursive)); |
| 955 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 956 | } |
| 957 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 958 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 959 | FilePath source_path = UTF8ToFilePath("path0.txt"); |
| 960 | FilePath dest_path = UTF8ToFilePath("path1.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 961 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 962 | |
| 963 | bool is_copy_not_move = false; |
| 964 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 965 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 966 | is_copy_not_move)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 967 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 968 | is_copy_not_move = true; |
| 969 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 970 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 971 | is_copy_not_move)); |
| 972 | source_path = UTF8ToFilePath("dir/dir/file"); |
| 973 | bool exclusive = true; |
| 974 | bool recursive = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 975 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 976 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 977 | context.get(), source_path.DirName(), exclusive, recursive)); |
| 978 | is_copy_not_move = false; |
| 979 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 980 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 981 | is_copy_not_move)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 982 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 983 | is_copy_not_move = true; |
| 984 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 985 | ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 986 | is_copy_not_move)); |
| 987 | } |
| 988 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 989 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 990 | const int64 kSourceLength = 5; |
| 991 | const int64 kDestLength = 50; |
| 992 | |
| 993 | for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { |
| 994 | SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); |
| 995 | const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; |
| 996 | SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << |
| 997 | test_case.is_copy_not_move); |
| 998 | SCOPED_TRACE(testing::Message() << "\t source_path " << |
| 999 | test_case.source_path); |
| 1000 | SCOPED_TRACE(testing::Message() << "\t dest_path " << |
| 1001 | test_case.dest_path); |
| 1002 | SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << |
| 1003 | test_case.cause_overwrite); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1004 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1005 | |
| 1006 | bool exclusive = false; |
| 1007 | bool recursive = true; |
| 1008 | FilePath source_path = UTF8ToFilePath(test_case.source_path); |
| 1009 | FilePath dest_path = UTF8ToFilePath(test_case.dest_path); |
| 1010 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1011 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1012 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1013 | context.get(), source_path.DirName(), exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1014 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1015 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1016 | context.get(), dest_path.DirName(), exclusive, recursive)); |
| 1017 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1018 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1019 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1020 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1021 | ofu()->EnsureFileExists(context.get(), source_path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1022 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1023 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1024 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1025 | ofu()->Truncate(context.get(), source_path, kSourceLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1026 | |
| 1027 | if (test_case.cause_overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1028 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1029 | created = false; |
| 1030 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1031 | ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1032 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1033 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1034 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1035 | ofu()->Truncate(context.get(), dest_path, kDestLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1036 | } |
| 1037 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1038 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1039 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(), |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1040 | source_path, dest_path, test_case.is_copy_not_move)); |
| 1041 | if (test_case.is_copy_not_move) { |
| 1042 | base::PlatformFileInfo file_info; |
| 1043 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1044 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1045 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1046 | context.get(), source_path, &file_info, &local_path)); |
| 1047 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1048 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1049 | ofu()->DeleteFile(context.get(), source_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1050 | } else { |
| 1051 | base::PlatformFileInfo file_info; |
| 1052 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1053 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1054 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1055 | context.get(), source_path, &file_info, &local_path)); |
| 1056 | } |
| 1057 | base::PlatformFileInfo file_info; |
| 1058 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1059 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1060 | context.get(), dest_path, &file_info, &local_path)); |
| 1061 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1062 | |
| 1063 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1064 | ofu()->DeleteFile(context.get(), dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1068 | TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1069 | FilePath src_path = UTF8ToFilePath("src path"); |
| 1070 | FilePath dest_path = UTF8ToFilePath("destination path"); |
| 1071 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1072 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1073 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1074 | context.get(), src_path, &created)); |
| 1075 | |
| 1076 | bool is_copy = true; |
| 1077 | // Copy, no overwrite. |
| 1078 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1079 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1080 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1081 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1082 | context.reset(NewContext(NULL)); |
| 1083 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1084 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1085 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1086 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1087 | |
| 1088 | // Copy, with overwrite. |
| 1089 | context.reset(NewContext(NULL)); |
| 1090 | context->set_allowed_bytes_growth(0); |
| 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 | } |
| 1094 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1095 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1096 | FilePath src_path = UTF8ToFilePath("src path"); |
| 1097 | FilePath dest_path = UTF8ToFilePath("destination path"); |
| 1098 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1099 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1100 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1101 | context.get(), src_path, &created)); |
| 1102 | |
| 1103 | bool is_copy = false; |
| 1104 | // Move, rename, no overwrite. |
| 1105 | context.reset(NewContext(NULL)); |
| 1106 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1107 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - |
| 1108 | ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1109 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1110 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1111 | context.reset(NewContext(NULL)); |
| 1112 | context->set_allowed_bytes_growth( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1113 | ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - |
| 1114 | ObfuscatedFileUtil::ComputeFilePathCost(src_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1115 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1116 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1117 | |
| 1118 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1119 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1120 | context.get(), src_path, &created)); |
| 1121 | |
| 1122 | // Move, rename, with overwrite. |
| 1123 | context.reset(NewContext(NULL)); |
| 1124 | context->set_allowed_bytes_growth(0); |
| 1125 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1126 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1127 | } |
| 1128 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1129 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1130 | FilePath src_path = UTF8ToFilePath("src path"); |
| 1131 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1132 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1133 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1134 | context.get(), src_path, &created)); |
| 1135 | |
| 1136 | bool exclusive = true; |
| 1137 | bool recursive = false; |
| 1138 | FilePath dir_path = UTF8ToFilePath("directory path"); |
| 1139 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1140 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1141 | context.get(), dir_path, exclusive, recursive)); |
| 1142 | |
| 1143 | FilePath dest_path = dir_path.Append(src_path); |
| 1144 | |
| 1145 | bool is_copy = false; |
| 1146 | int64 allowed_bytes_growth = -1000; // Over quota, this should still work. |
| 1147 | // Move, no rename, no overwrite. |
| 1148 | context.reset(NewContext(NULL)); |
| 1149 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1150 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1151 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1152 | EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); |
| 1153 | |
| 1154 | // Move, no rename, with overwrite. |
| 1155 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1156 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1157 | context.get(), src_path, &created)); |
| 1158 | context.reset(NewContext(NULL)); |
| 1159 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1160 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1161 | ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1162 | EXPECT_EQ( |
| 1163 | allowed_bytes_growth + |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1164 | ObfuscatedFileUtil::ComputeFilePathCost(src_path), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1165 | context->allowed_bytes_growth()); |
| 1166 | } |
| 1167 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1168 | TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) { |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 1169 | TestCopyInForeignFileHelper(false /* overwrite */); |
| 1170 | TestCopyInForeignFileHelper(true /* overwrite */); |
| 1171 | } |
| 1172 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1173 | TEST_F(ObfuscatedFileUtilTest, TestEnumerator) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1174 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1175 | FilePath src_path = UTF8ToFilePath("source dir"); |
| 1176 | bool exclusive = true; |
| 1177 | bool recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1178 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1179 | context.get(), src_path, exclusive, recursive)); |
| 1180 | |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 1181 | std::set<FilePath::StringType> files; |
| 1182 | std::set<FilePath::StringType> directories; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1183 | FillTestDirectory(src_path, &files, &directories); |
| 1184 | |
| 1185 | FilePath dest_path = UTF8ToFilePath("destination dir"); |
| 1186 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1187 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1188 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1189 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1190 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1191 | ofu()->Copy(context.get(), src_path, dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1192 | |
| 1193 | ValidateTestDirectory(dest_path, files, directories); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1194 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1195 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1196 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1197 | EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1198 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1199 | recursive = true; |
| 1200 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1201 | ofu()->Delete(context.get(), dest_path, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1202 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1203 | EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1204 | } |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1205 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1206 | TEST_F(ObfuscatedFileUtilTest, TestMigration) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1207 | ScopedTempDir source_dir; |
| 1208 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 1209 | FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); |
| 1210 | ASSERT_TRUE(file_util::CreateDirectory(root_path)); |
| 1211 | |
| 1212 | for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1213 | SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
| 1214 | const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1215 | FilePath local_src_path = root_path.Append(test_case.path); |
| 1216 | if (test_case.is_directory) { |
| 1217 | ASSERT_TRUE( |
| 1218 | file_util::CreateDirectory(local_src_path)); |
| 1219 | } else { |
| 1220 | base::PlatformFileError error_code; |
| 1221 | bool created = false; |
| 1222 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 1223 | base::PlatformFile file_handle = |
| 1224 | base::CreatePlatformFile( |
| 1225 | local_src_path, file_flags, &created, &error_code); |
| 1226 | EXPECT_TRUE(created); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 1227 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1228 | ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 1229 | ASSERT_TRUE( |
| 1230 | base::TruncatePlatformFile(file_handle, test_case.data_file_size)); |
| 1231 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1232 | } |
| 1233 | } |
| 1234 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1235 | EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1236 | |
| 1237 | FilePath new_root = |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1238 | test_directory().AppendASCII("File System").AppendASCII("000").Append( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1239 | ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1240 | for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1241 | SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); |
| 1242 | const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1243 | FilePath local_data_path = new_root.Append(test_case.path); |
| 1244 | #if defined(OS_WIN) |
| 1245 | local_data_path = local_data_path.NormalizeWindowsPathSeparators(); |
| 1246 | #endif |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1247 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1248 | base::PlatformFileInfo ofu_file_info; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1249 | FilePath data_path; |
| 1250 | SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); |
| 1251 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1252 | ofu()->GetFileInfo(context.get(), FilePath(test_case.path), |
| 1253 | &ofu_file_info, &data_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1254 | if (test_case.is_directory) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1255 | EXPECT_TRUE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1256 | } else { |
| 1257 | base::PlatformFileInfo platform_file_info; |
| 1258 | SCOPED_TRACE(testing::Message() << "local_data_path is " << |
| 1259 | local_data_path.value()); |
| 1260 | SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); |
| 1261 | ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); |
| 1262 | EXPECT_EQ(test_case.data_file_size, platform_file_info.size); |
| 1263 | EXPECT_FALSE(platform_file_info.is_directory); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1264 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1265 | EXPECT_EQ(local_data_path, data_path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1266 | EXPECT_EQ(platform_file_info.size, ofu_file_info.size); |
| 1267 | EXPECT_FALSE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1271 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1272 | TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) { |
| 1273 | scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> |
| 1274 | enumerator(ofu()->CreateOriginEnumerator()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1275 | // The test helper starts out with a single filesystem. |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1276 | EXPECT_TRUE(enumerator.get()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1277 | EXPECT_EQ(origin(), enumerator->Next()); |
| 1278 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1279 | EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1280 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1281 | EXPECT_EQ(GURL(), enumerator->Next()); |
| 1282 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1283 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1284 | |
| 1285 | std::set<GURL> origins_expected; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1286 | origins_expected.insert(origin()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1287 | |
| 1288 | for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { |
| 1289 | SCOPED_TRACE(testing::Message() << |
| 1290 | "Validating kOriginEnumerationTestRecords " << i); |
| 1291 | const OriginEnumerationTestRecord& record = |
| 1292 | kOriginEnumerationTestRecords[i]; |
| 1293 | GURL origin_url(record.origin_url); |
| 1294 | origins_expected.insert(origin_url); |
| 1295 | if (record.has_temporary) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1296 | scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1297 | NewHelper(origin_url, kFileSystemTypeTemporary)); |
| 1298 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1299 | context->set_src_origin_url(origin_url); |
| 1300 | context->set_src_type(kFileSystemTypeTemporary); |
| 1301 | bool created = false; |
| 1302 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1303 | ofu()->EnsureFileExists(context.get(), |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1304 | FilePath().AppendASCII("file"), &created)); |
| 1305 | EXPECT_TRUE(created); |
| 1306 | } |
| 1307 | if (record.has_persistent) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1308 | scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1309 | NewHelper(origin_url, kFileSystemTypePersistent)); |
| 1310 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1311 | context->set_src_origin_url(origin_url); |
| 1312 | context->set_src_type(kFileSystemTypePersistent); |
| 1313 | bool created = false; |
| 1314 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1315 | ofu()->EnsureFileExists(context.get(), |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1316 | FilePath().AppendASCII("file"), &created)); |
| 1317 | EXPECT_TRUE(created); |
| 1318 | } |
| 1319 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1320 | enumerator.reset(ofu()->CreateOriginEnumerator()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1321 | EXPECT_TRUE(enumerator.get()); |
| 1322 | std::set<GURL> origins_found; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1323 | GURL origin_url; |
| 1324 | while (!(origin_url = enumerator->Next()).is_empty()) { |
| 1325 | origins_found.insert(origin_url); |
| 1326 | SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1327 | bool found = false; |
| 1328 | for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); |
| 1329 | ++i) { |
| 1330 | const OriginEnumerationTestRecord& record = |
| 1331 | kOriginEnumerationTestRecords[i]; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1332 | if (GURL(record.origin_url) != origin_url) |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1333 | continue; |
| 1334 | found = true; |
| 1335 | EXPECT_EQ(record.has_temporary, |
| 1336 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1337 | EXPECT_EQ(record.has_persistent, |
| 1338 | enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1339 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1340 | // Deal with the default filesystem created by the test helper. |
| 1341 | if (!found && origin_url == origin()) { |
| 1342 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1343 | EXPECT_EQ(true, |
| 1344 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
[email protected] | 34161bb | 2011-10-13 12:36:18 | [diff] [blame] | 1345 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1346 | found = true; |
| 1347 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1348 | EXPECT_TRUE(found); |
| 1349 | } |
| 1350 | |
| 1351 | std::set<GURL> diff; |
| 1352 | std::set_symmetric_difference(origins_expected.begin(), |
| 1353 | origins_expected.end(), origins_found.begin(), origins_found.end(), |
| 1354 | inserter(diff, diff.begin())); |
| 1355 | EXPECT_TRUE(diff.empty()); |
| 1356 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1357 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1358 | TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1359 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1360 | |
| 1361 | int64 expected_quota = 0; |
| 1362 | |
| 1363 | for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1364 | SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
| 1365 | const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1366 | FilePath path(test_case.path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1367 | expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1368 | if (test_case.is_directory) { |
| 1369 | bool exclusive = true; |
| 1370 | bool recursive = false; |
| 1371 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1372 | ofu()->CreateDirectory(context.get(), path, exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1373 | } else { |
| 1374 | bool created = false; |
| 1375 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1376 | ofu()->EnsureFileExists(context.get(), path, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1377 | ASSERT_TRUE(created); |
| 1378 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1379 | ofu()->Truncate(context.get(), path, |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1380 | test_case.data_file_size)); |
| 1381 | expected_quota += test_case.data_file_size; |
| 1382 | } |
| 1383 | } |
| 1384 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
| 1385 | RevokeUsageCache(); |
| 1386 | EXPECT_EQ(-1, SizeInUsageFile()); |
| 1387 | GetUsageFromQuotaManager(); |
| 1388 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
| 1389 | EXPECT_EQ(expected_quota, usage()); |
| 1390 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1391 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1392 | TEST_F(ObfuscatedFileUtilTest, TestInconsistency) { |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1393 | const FilePath kPath1 = FilePath().AppendASCII("hoge"); |
| 1394 | const FilePath kPath2 = FilePath().AppendASCII("fuga"); |
| 1395 | |
| 1396 | scoped_ptr<FileSystemOperationContext> context; |
| 1397 | base::PlatformFile file; |
| 1398 | base::PlatformFileInfo file_info; |
| 1399 | FilePath data_path; |
| 1400 | bool created = false; |
| 1401 | |
| 1402 | // Create a non-empty file. |
| 1403 | context.reset(NewContext(NULL)); |
| 1404 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1405 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1406 | EXPECT_TRUE(created); |
| 1407 | context.reset(NewContext(NULL)); |
| 1408 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1409 | ofu()->Truncate(context.get(), kPath1, 10)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1410 | context.reset(NewContext(NULL)); |
| 1411 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1412 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1413 | context.get(), kPath1, &file_info, &data_path)); |
| 1414 | EXPECT_EQ(10, file_info.size); |
| 1415 | |
| 1416 | // Destroy database to make inconsistency between database and filesystem. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1417 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1418 | |
| 1419 | // Try to get file info of broken file. |
| 1420 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1421 | EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1422 | context.reset(NewContext(NULL)); |
| 1423 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1424 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1425 | EXPECT_TRUE(created); |
| 1426 | context.reset(NewContext(NULL)); |
| 1427 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1428 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1429 | context.get(), kPath1, &file_info, &data_path)); |
| 1430 | EXPECT_EQ(0, file_info.size); |
| 1431 | |
| 1432 | // Make another broken file to |kPath2|. |
| 1433 | context.reset(NewContext(NULL)); |
| 1434 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1435 | ofu()->EnsureFileExists(context.get(), kPath2, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1436 | EXPECT_TRUE(created); |
| 1437 | |
| 1438 | // Destroy again. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1439 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1440 | |
| 1441 | // Repair broken |kPath1|. |
| 1442 | context.reset(NewContext(NULL)); |
| 1443 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1444 | ofu()->Touch(context.get(), kPath1, base::Time::Now(), |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1445 | base::Time::Now())); |
| 1446 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1447 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1448 | EXPECT_TRUE(created); |
| 1449 | |
| 1450 | // Copy from sound |kPath1| to broken |kPath2|. |
| 1451 | context.reset(NewContext(NULL)); |
| 1452 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1453 | ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2, |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1454 | true /* copy */)); |
| 1455 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1456 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1457 | context.reset(NewContext(NULL)); |
| 1458 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1459 | ofu()->CreateOrOpen( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1460 | context.get(), kPath1, |
| 1461 | base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE, |
| 1462 | &file, &created)); |
| 1463 | EXPECT_TRUE(created); |
| 1464 | EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); |
| 1465 | EXPECT_EQ(0, file_info.size); |
| 1466 | EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 1467 | } |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1468 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1469 | TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) { |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1470 | const FilePath kPath[] = { |
| 1471 | FilePath().AppendASCII("foo"), |
| 1472 | FilePath().AppendASCII("bar"), |
| 1473 | FilePath().AppendASCII("baz") |
| 1474 | }; |
| 1475 | scoped_ptr<FileSystemOperationContext> context; |
| 1476 | |
| 1477 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) { |
| 1478 | bool created = false; |
| 1479 | context.reset(NewContext(NULL)); |
| 1480 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1481 | ofu()->EnsureFileExists(context.get(), kPath[i], &created)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1482 | EXPECT_TRUE(created); |
| 1483 | } |
| 1484 | |
| 1485 | context.reset(NewContext(NULL)); |
| 1486 | std::vector<base::FileUtilProxy::Entry> entries; |
| 1487 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1488 | ofu()->ReadDirectory(context.get(), FilePath(), &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1489 | EXPECT_EQ(3u, entries.size()); |
| 1490 | |
| 1491 | context.reset(NewContext(NULL)); |
| 1492 | FilePath local_path; |
| 1493 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1494 | ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1495 | EXPECT_TRUE(file_util::Delete(local_path, false)); |
| 1496 | |
| 1497 | context.reset(NewContext(NULL)); |
| 1498 | entries.clear(); |
| 1499 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1500 | ofu()->ReadDirectory(context.get(), FilePath(), &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1501 | EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); |
| 1502 | } |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame^] | 1503 | |
| 1504 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) { |
| 1505 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1506 | const FilePath dir_path(FILE_PATH_LITERAL("foo_dir")); |
| 1507 | |
| 1508 | // Create working directory. |
| 1509 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1510 | ofu()->CreateDirectory(context.get(), dir_path, false, false)); |
| 1511 | |
| 1512 | // EnsureFileExists, create case. |
| 1513 | FilePath path(dir_path.AppendASCII("EnsureFileExists_file")); |
| 1514 | bool created = false; |
| 1515 | ClearTimestamp(dir_path); |
| 1516 | context.reset(NewContext(NULL)); |
| 1517 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1518 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1519 | EXPECT_TRUE(created); |
| 1520 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1521 | |
| 1522 | // non create case. |
| 1523 | created = true; |
| 1524 | ClearTimestamp(dir_path); |
| 1525 | context.reset(NewContext(NULL)); |
| 1526 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1527 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1528 | EXPECT_FALSE(created); |
| 1529 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1530 | |
| 1531 | // fail case. |
| 1532 | path = dir_path.AppendASCII("EnsureFileExists_dir"); |
| 1533 | context.reset(NewContext(NULL)); |
| 1534 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1535 | ofu()->CreateDirectory(context.get(), path, false, false)); |
| 1536 | |
| 1537 | ClearTimestamp(dir_path); |
| 1538 | context.reset(NewContext(NULL)); |
| 1539 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, |
| 1540 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1541 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1542 | |
| 1543 | // CreateOrOpen, create case. |
| 1544 | path = dir_path.AppendASCII("CreateOrOpen_file"); |
| 1545 | PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 1546 | created = false; |
| 1547 | ClearTimestamp(dir_path); |
| 1548 | context.reset(NewContext(NULL)); |
| 1549 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1550 | ofu()->CreateOrOpen( |
| 1551 | context.get(), path, |
| 1552 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| 1553 | &file_handle, &created)); |
| 1554 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1555 | EXPECT_TRUE(created); |
| 1556 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1557 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1558 | |
| 1559 | // open case. |
| 1560 | file_handle = base::kInvalidPlatformFileValue; |
| 1561 | created = true; |
| 1562 | ClearTimestamp(dir_path); |
| 1563 | context.reset(NewContext(NULL)); |
| 1564 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1565 | ofu()->CreateOrOpen( |
| 1566 | context.get(), path, |
| 1567 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 1568 | &file_handle, &created)); |
| 1569 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1570 | EXPECT_FALSE(created); |
| 1571 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1572 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1573 | |
| 1574 | // fail case |
| 1575 | file_handle = base::kInvalidPlatformFileValue; |
| 1576 | ClearTimestamp(dir_path); |
| 1577 | context.reset(NewContext(NULL)); |
| 1578 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
| 1579 | ofu()->CreateOrOpen( |
| 1580 | context.get(), path, |
| 1581 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| 1582 | &file_handle, &created)); |
| 1583 | EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle); |
| 1584 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1585 | |
| 1586 | // CreateDirectory, create case. |
| 1587 | // Creating CreateDirectory_dir and CreateDirectory_dir/subdir. |
| 1588 | path = dir_path.AppendASCII("CreateDirectory_dir"); |
| 1589 | FilePath subdir_path(path.AppendASCII("subdir")); |
| 1590 | ClearTimestamp(dir_path); |
| 1591 | context.reset(NewContext(NULL)); |
| 1592 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1593 | ofu()->CreateDirectory(context.get(), subdir_path, |
| 1594 | true /* exclusive */, true /* recursive */)); |
| 1595 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1596 | |
| 1597 | // create subdir case. |
| 1598 | // Creating CreateDirectory_dir/subdir2. |
| 1599 | subdir_path = path.AppendASCII("subdir2"); |
| 1600 | ClearTimestamp(dir_path); |
| 1601 | ClearTimestamp(path); |
| 1602 | context.reset(NewContext(NULL)); |
| 1603 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1604 | ofu()->CreateDirectory(context.get(), subdir_path, |
| 1605 | true /* exclusive */, true /* recursive */)); |
| 1606 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1607 | EXPECT_NE(base::Time(), GetModifiedTime(path)); |
| 1608 | |
| 1609 | // fail case. |
| 1610 | path = dir_path.AppendASCII("CreateDirectory_dir"); |
| 1611 | ClearTimestamp(dir_path); |
| 1612 | context.reset(NewContext(NULL)); |
| 1613 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
| 1614 | ofu()->CreateDirectory(context.get(), path, |
| 1615 | true /* exclusive */, true /* recursive */)); |
| 1616 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1617 | |
| 1618 | // CopyInForeignFile, create case. |
| 1619 | path = dir_path.AppendASCII("CopyInForeignFile_file"); |
| 1620 | FilePath src_path = dir_path.AppendASCII("CopyInForeignFile_src_file"); |
| 1621 | context.reset(NewContext(NULL)); |
| 1622 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1623 | ofu()->EnsureFileExists(context.get(), src_path, &created)); |
| 1624 | EXPECT_TRUE(created); |
| 1625 | FilePath src_local_path; |
| 1626 | context.reset(NewContext(NULL)); |
| 1627 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1628 | ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path)); |
| 1629 | |
| 1630 | ClearTimestamp(dir_path); |
| 1631 | context.reset(NewContext(NULL)); |
| 1632 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1633 | ofu()->CopyInForeignFile(context.get(), src_local_path, path)); |
| 1634 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1635 | } |
| 1636 | |
| 1637 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) { |
| 1638 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1639 | const FilePath dir_path(FILE_PATH_LITERAL("foo_dir")); |
| 1640 | |
| 1641 | // Create working directory. |
| 1642 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1643 | ofu()->CreateDirectory(context.get(), dir_path, false, false)); |
| 1644 | |
| 1645 | // DeleteFile, delete case. |
| 1646 | FilePath path = dir_path.AppendASCII("DeleteFile_file"); |
| 1647 | bool created = false; |
| 1648 | context.reset(NewContext(NULL)); |
| 1649 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1650 | ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1651 | EXPECT_TRUE(created); |
| 1652 | |
| 1653 | ClearTimestamp(dir_path); |
| 1654 | context.reset(NewContext(NULL)); |
| 1655 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1656 | ofu()->DeleteFile(context.get(), path)); |
| 1657 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1658 | |
| 1659 | // fail case. |
| 1660 | ClearTimestamp(dir_path); |
| 1661 | context.reset(NewContext(NULL)); |
| 1662 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 1663 | ofu()->DeleteFile(context.get(), path)); |
| 1664 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1665 | |
| 1666 | // DeleteSingleDirectory, fail case. |
| 1667 | path = dir_path.AppendASCII("DeleteSingleDirectory_dir"); |
| 1668 | FilePath file_path(path.AppendASCII("pakeratta")); |
| 1669 | context.reset(NewContext(NULL)); |
| 1670 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1671 | ofu()->CreateDirectory(context.get(), path, true, true)); |
| 1672 | created = false; |
| 1673 | context.reset(NewContext(NULL)); |
| 1674 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1675 | ofu()->EnsureFileExists(context.get(), file_path, &created)); |
| 1676 | EXPECT_TRUE(created); |
| 1677 | |
| 1678 | ClearTimestamp(dir_path); |
| 1679 | context.reset(NewContext(NULL)); |
| 1680 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
| 1681 | ofu()->DeleteSingleDirectory(context.get(), path)); |
| 1682 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_path)); |
| 1683 | |
| 1684 | // delete case. |
| 1685 | context.reset(NewContext(NULL)); |
| 1686 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1687 | ofu()->DeleteFile(context.get(), file_path)); |
| 1688 | |
| 1689 | ClearTimestamp(dir_path); |
| 1690 | context.reset(NewContext(NULL)); |
| 1691 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1692 | ofu()->DeleteSingleDirectory(context.get(), path)); |
| 1693 | EXPECT_NE(base::Time(), GetModifiedTime(dir_path)); |
| 1694 | } |
| 1695 | |
| 1696 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) { |
| 1697 | TestDirectoryTimestampHelper( |
| 1698 | FilePath(FILE_PATH_LITERAL("copy overwrite")), true, true); |
| 1699 | TestDirectoryTimestampHelper( |
| 1700 | FilePath(FILE_PATH_LITERAL("copy non-overwrite")), true, false); |
| 1701 | TestDirectoryTimestampHelper( |
| 1702 | FilePath(FILE_PATH_LITERAL("move overwrite")), false, true); |
| 1703 | TestDirectoryTimestampHelper( |
| 1704 | FilePath(FILE_PATH_LITERAL("move non-overwrite")), false, false); |
| 1705 | } |