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