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