[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] | dc57ec8 | 2012-08-07 03:50:10 | [diff] [blame] | 20 | #include "webkit/fileapi/file_system_task_runners.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 21 | #include "webkit/fileapi/file_system_usage_cache.h" |
[email protected] | aa437fa | 2012-03-03 02:09:07 | [diff] [blame] | 22 | #include "webkit/fileapi/file_util_helper.h" |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 23 | #include "webkit/fileapi/local_file_system_test_helper.h" |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 24 | #include "webkit/fileapi/mock_file_change_observer.h" |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 25 | #include "webkit/fileapi/mock_file_system_options.h" |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 26 | #include "webkit/fileapi/obfuscated_file_util.h" |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 27 | #include "webkit/fileapi/test_file_set.h" |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 28 | #include "webkit/quota/mock_special_storage_policy.h" |
| 29 | #include "webkit/quota/quota_manager.h" |
| 30 | #include "webkit/quota/quota_types.h" |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 31 | |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 32 | namespace fileapi { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 36 | bool FileExists(const FilePath& path) { |
| 37 | return file_util::PathExists(path) && !file_util::DirectoryExists(path); |
| 38 | } |
| 39 | |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 40 | int64 GetSize(const FilePath& path) { |
| 41 | int64 size; |
| 42 | EXPECT_TRUE(file_util::GetFileSize(path, &size)); |
| 43 | return size; |
| 44 | } |
| 45 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 46 | // After a move, the dest exists and the source doesn't. |
| 47 | // After a copy, both source and dest exist. |
| 48 | struct CopyMoveTestCaseRecord { |
| 49 | bool is_copy_not_move; |
| 50 | const char source_path[64]; |
| 51 | const char dest_path[64]; |
| 52 | bool cause_overwrite; |
| 53 | }; |
| 54 | |
| 55 | const CopyMoveTestCaseRecord kCopyMoveTestCases[] = { |
| 56 | // This is the combinatoric set of: |
| 57 | // rename vs. same-name |
| 58 | // different directory vs. same directory |
| 59 | // overwrite vs. no-overwrite |
| 60 | // copy vs. move |
| 61 | // We can never be called with source and destination paths identical, so |
| 62 | // those cases are omitted. |
| 63 | {true, "dir0/file0", "dir0/file1", false}, |
| 64 | {false, "dir0/file0", "dir0/file1", false}, |
| 65 | {true, "dir0/file0", "dir0/file1", true}, |
| 66 | {false, "dir0/file0", "dir0/file1", true}, |
| 67 | |
| 68 | {true, "dir0/file0", "dir1/file0", false}, |
| 69 | {false, "dir0/file0", "dir1/file0", false}, |
| 70 | {true, "dir0/file0", "dir1/file0", true}, |
| 71 | {false, "dir0/file0", "dir1/file0", true}, |
| 72 | {true, "dir0/file0", "dir1/file1", false}, |
| 73 | {false, "dir0/file0", "dir1/file1", false}, |
| 74 | {true, "dir0/file0", "dir1/file1", true}, |
| 75 | {false, "dir0/file0", "dir1/file1", true}, |
| 76 | }; |
| 77 | |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 78 | struct OriginEnumerationTestRecord { |
| 79 | std::string origin_url; |
| 80 | bool has_temporary; |
| 81 | bool has_persistent; |
| 82 | }; |
| 83 | |
| 84 | const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = { |
| 85 | {"http://example.com", false, true}, |
| 86 | {"http://example1.com", true, false}, |
| 87 | {"https://example1.com", true, true}, |
| 88 | {"file://", false, true}, |
| 89 | {"http://example.com:8000", false, true}, |
| 90 | }; |
| 91 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 92 | } // namespace (anonymous) |
| 93 | |
| 94 | // TODO(ericu): The vast majority of this and the other FSFU subclass tests |
| 95 | // could theoretically be shared. It would basically be a FSFU interface |
| 96 | // compliance test, and only the subclass-specific bits that look into the |
| 97 | // implementation would need to be written per-subclass. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 98 | class ObfuscatedFileUtilTest : public testing::Test { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 99 | public: |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 100 | ObfuscatedFileUtilTest() |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 101 | : origin_(GURL("http://www.example.com")), |
| 102 | type_(kFileSystemTypeTemporary), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 103 | weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 104 | test_helper_(origin_, type_), |
| 105 | quota_status_(quota::kQuotaStatusUnknown), |
| 106 | usage_(-1) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void SetUp() { |
| 110 | ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 111 | |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 112 | scoped_refptr<quota::SpecialStoragePolicy> storage_policy = |
| 113 | new quota::MockSpecialStoragePolicy(); |
| 114 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 115 | quota_manager_ = new quota::QuotaManager( |
| 116 | false /* is_incognito */, |
| 117 | data_dir_.path(), |
| 118 | base::MessageLoopProxy::current(), |
| 119 | base::MessageLoopProxy::current(), |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 120 | storage_policy); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 121 | |
| 122 | // Every time we create a new helper, it creates another context, which |
| 123 | // creates another path manager, another sandbox_mount_point_provider, and |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 124 | // another OFU. We need to pass in the context to skip all that. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 125 | file_system_context_ = new FileSystemContext( |
[email protected] | dc57ec8 | 2012-08-07 03:50:10 | [diff] [blame] | 126 | FileSystemTaskRunners::CreateMockTaskRunners(), |
[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 | quota_manager_->proxy(), |
| 129 | data_dir_.path(), |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 130 | CreateAllowFileAccessOptions()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 131 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 132 | obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>( |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 133 | file_system_context_->GetFileUtil(type_)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 134 | |
| 135 | test_helper_.SetUp(file_system_context_.get(), |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 136 | obfuscated_file_util_); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 137 | |
| 138 | change_observers_ = MockFileChangeObserver::CreateList(&change_observer_); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 141 | void TearDown() { |
| 142 | quota_manager_ = NULL; |
| 143 | test_helper_.TearDown(); |
| 144 | } |
| 145 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 146 | scoped_ptr<FileSystemOperationContext> LimitedContext( |
| 147 | int64 allowed_bytes_growth) { |
| 148 | scoped_ptr<FileSystemOperationContext> context( |
| 149 | test_helper_.NewOperationContext()); |
| 150 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 151 | return context.Pass(); |
| 152 | } |
| 153 | |
| 154 | scoped_ptr<FileSystemOperationContext> UnlimitedContext() { |
| 155 | return LimitedContext(kint64max); |
| 156 | } |
| 157 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 158 | FileSystemOperationContext* NewContext( |
| 159 | LocalFileSystemTestOriginHelper* helper) { |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 160 | change_observer()->ResetCount(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 161 | FileSystemOperationContext* context; |
| 162 | if (helper) |
| 163 | context = helper->NewOperationContext(); |
| 164 | else |
| 165 | context = test_helper_.NewOperationContext(); |
| 166 | context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests. |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 167 | context->set_change_observers(change_observers()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 168 | return context; |
| 169 | } |
| 170 | |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 171 | const ChangeObserverList& change_observers() const { |
| 172 | return change_observers_; |
| 173 | } |
| 174 | |
| 175 | MockFileChangeObserver* change_observer() { |
| 176 | return &change_observer_; |
| 177 | } |
| 178 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 179 | // 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] | 180 | // and obfuscated_file_util_. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 181 | // Use this for tests which need to run in multiple origins; we need a test |
| 182 | // helper per origin. |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 183 | LocalFileSystemTestOriginHelper* NewHelper( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 184 | const GURL& origin, fileapi::FileSystemType type) { |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 185 | LocalFileSystemTestOriginHelper* helper = |
| 186 | new LocalFileSystemTestOriginHelper(origin, type); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 187 | |
| 188 | helper->SetUp(file_system_context_.get(), |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 189 | obfuscated_file_util_); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 190 | return helper; |
| 191 | } |
| 192 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 193 | ObfuscatedFileUtil* ofu() { |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 194 | return obfuscated_file_util_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 197 | const FilePath& test_directory() const { |
| 198 | return data_dir_.path(); |
| 199 | } |
| 200 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 201 | const GURL& origin() const { |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 202 | return origin_; |
| 203 | } |
| 204 | |
| 205 | fileapi::FileSystemType type() const { |
| 206 | return type_; |
| 207 | } |
| 208 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 209 | int64 ComputeTotalFileSize() { |
| 210 | return test_helper_.ComputeCurrentOriginUsage() - |
| 211 | test_helper_.ComputeCurrentDirectoryDatabaseUsage(); |
| 212 | } |
| 213 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 214 | void GetUsageFromQuotaManager() { |
| 215 | quota_manager_->GetUsageAndQuota( |
| 216 | origin(), test_helper_.storage_type(), |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 217 | base::Bind(&ObfuscatedFileUtilTest::OnGetUsage, |
| 218 | weak_factory_.GetWeakPtr())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 219 | MessageLoop::current()->RunAllPending(); |
| 220 | EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); |
| 221 | } |
| 222 | |
| 223 | void RevokeUsageCache() { |
| 224 | quota_manager_->ResetUsageTracker(test_helper_.storage_type()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 225 | file_util::Delete(test_helper_.GetUsageCachePath(), false); |
| 226 | } |
| 227 | |
| 228 | int64 SizeByQuotaUtil() { |
| 229 | return test_helper_.GetCachedOriginUsage(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int64 SizeInUsageFile() { |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 233 | return FileSystemUsageCache::GetUsage(test_helper_.GetUsageCachePath()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 236 | bool PathExists(const FileSystemURL& url) { |
| 237 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 238 | return FileUtilHelper::PathExists(context.get(), ofu(), url); |
| 239 | } |
| 240 | |
| 241 | bool DirectoryExists(const FileSystemURL& url) { |
| 242 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 243 | return FileUtilHelper::DirectoryExists(context.get(), ofu(), url); |
| 244 | } |
| 245 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 246 | int64 usage() const { return usage_; } |
| 247 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 248 | FileSystemURL CreateURLFromUTF8(const std::string& path) { |
| 249 | return test_helper_.CreateURLFromUTF8(path); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 252 | int64 PathCost(const FileSystemURL& url) { |
| 253 | return ObfuscatedFileUtil::ComputeFilePathCost(url.path()); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 256 | FileSystemURL CreateURL(const FilePath& path) { |
| 257 | return test_helper_.CreateURL(path); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 260 | void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { |
| 261 | EXPECT_EQ(quota::kQuotaStatusOk, status); |
| 262 | quota_status_ = status; |
| 263 | usage_ = usage; |
| 264 | } |
| 265 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 266 | void CheckFileAndCloseHandle( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 267 | const FileSystemURL& url, PlatformFile file_handle) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 268 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 269 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 270 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 271 | context.get(), url, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 272 | |
| 273 | base::PlatformFileInfo file_info0; |
| 274 | FilePath data_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 275 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 276 | context.get(), url, &file_info0, &data_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 277 | EXPECT_EQ(data_path, local_path); |
| 278 | EXPECT_TRUE(FileExists(data_path)); |
| 279 | EXPECT_EQ(0, GetSize(data_path)); |
| 280 | |
| 281 | const char data[] = "test data"; |
| 282 | const int length = arraysize(data) - 1; |
| 283 | |
| 284 | if (base::kInvalidPlatformFileValue == file_handle) { |
| 285 | bool created = true; |
| 286 | PlatformFileError error; |
| 287 | file_handle = base::CreatePlatformFile( |
| 288 | data_path, |
| 289 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 290 | &created, |
| 291 | &error); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 292 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 293 | ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 294 | EXPECT_FALSE(created); |
| 295 | } |
| 296 | ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); |
| 297 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 298 | |
| 299 | base::PlatformFileInfo file_info1; |
| 300 | EXPECT_EQ(length, GetSize(data_path)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 301 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 302 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 303 | context.get(), url, &file_info1, &data_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 304 | EXPECT_EQ(data_path, local_path); |
| 305 | |
| 306 | EXPECT_FALSE(file_info0.is_directory); |
| 307 | EXPECT_FALSE(file_info1.is_directory); |
| 308 | EXPECT_FALSE(file_info0.is_symbolic_link); |
| 309 | EXPECT_FALSE(file_info1.is_symbolic_link); |
| 310 | EXPECT_EQ(0, file_info0.size); |
| 311 | EXPECT_EQ(length, file_info1.size); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 312 | EXPECT_LE(file_info0.last_modified, file_info1.last_modified); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 313 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 314 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 315 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 316 | context.get(), url, length * 2)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 317 | EXPECT_EQ(length * 2, GetSize(data_path)); |
| 318 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 319 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 320 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 321 | context.get(), url, 0)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 322 | EXPECT_EQ(0, GetSize(data_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void ValidateTestDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 326 | const FileSystemURL& root_url, |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 327 | const std::set<FilePath::StringType>& files, |
| 328 | const std::set<FilePath::StringType>& directories) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 329 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 330 | std::set<FilePath::StringType>::const_iterator iter; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 331 | for (iter = files.begin(); iter != files.end(); ++iter) { |
| 332 | bool created = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 333 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 334 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 335 | ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 336 | context.get(), root_url.WithPath(root_url.path().Append(*iter)), |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 337 | &created)); |
| 338 | ASSERT_FALSE(created); |
| 339 | } |
| 340 | for (iter = directories.begin(); iter != directories.end(); ++iter) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 341 | context.reset(NewContext(NULL)); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 342 | EXPECT_TRUE(DirectoryExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 343 | root_url.WithPath(root_url.path().Append(*iter)))); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 347 | class UsageVerifyHelper { |
| 348 | public: |
| 349 | UsageVerifyHelper(scoped_ptr<FileSystemOperationContext> context, |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 350 | LocalFileSystemTestOriginHelper* test_helper, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 351 | int64 expected_usage) |
| 352 | : context_(context.Pass()), |
| 353 | test_helper_(test_helper), |
| 354 | expected_usage_(expected_usage) {} |
| 355 | |
| 356 | ~UsageVerifyHelper() { |
| 357 | Check(); |
| 358 | } |
| 359 | |
| 360 | FileSystemOperationContext* context() { |
| 361 | return context_.get(); |
| 362 | } |
| 363 | |
| 364 | private: |
| 365 | void Check() { |
| 366 | ASSERT_EQ(expected_usage_, |
| 367 | test_helper_->GetCachedOriginUsage()); |
| 368 | } |
| 369 | |
| 370 | scoped_ptr<FileSystemOperationContext> context_; |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 371 | LocalFileSystemTestOriginHelper* test_helper_; |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 372 | int64 expected_usage_; |
| 373 | }; |
| 374 | |
| 375 | scoped_ptr<UsageVerifyHelper> AllowUsageIncrease(int64 requested_growth) { |
| 376 | int64 usage = test_helper_.GetCachedOriginUsage(); |
| 377 | return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper( |
| 378 | LimitedContext(requested_growth), |
| 379 | &test_helper_, usage + requested_growth)); |
| 380 | } |
| 381 | |
| 382 | scoped_ptr<UsageVerifyHelper> DisallowUsageIncrease(int64 requested_growth) { |
| 383 | int64 usage = test_helper_.GetCachedOriginUsage(); |
| 384 | return scoped_ptr<UsageVerifyHelper>(new UsageVerifyHelper( |
| 385 | LimitedContext(requested_growth - 1), &test_helper_, usage)); |
| 386 | } |
| 387 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 388 | void FillTestDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 389 | const FileSystemURL& root_url, |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 390 | std::set<FilePath::StringType>* files, |
| 391 | std::set<FilePath::StringType>* directories) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 392 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 393 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 394 | std::vector<base::FileUtilProxy::Entry> entries; |
| 395 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 396 | FileUtilHelper::ReadDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 397 | context.get(), ofu(), root_url, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 398 | EXPECT_EQ(0UL, entries.size()); |
| 399 | |
| 400 | files->clear(); |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 401 | files->insert(FILE_PATH_LITERAL("first")); |
| 402 | files->insert(FILE_PATH_LITERAL("second")); |
| 403 | files->insert(FILE_PATH_LITERAL("third")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 404 | directories->clear(); |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 405 | directories->insert(FILE_PATH_LITERAL("fourth")); |
| 406 | directories->insert(FILE_PATH_LITERAL("fifth")); |
| 407 | directories->insert(FILE_PATH_LITERAL("sixth")); |
| 408 | std::set<FilePath::StringType>::iterator iter; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 409 | for (iter = files->begin(); iter != files->end(); ++iter) { |
| 410 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 411 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 412 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 413 | ofu()->EnsureFileExists( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 414 | context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 415 | root_url.WithPath(root_url.path().Append(*iter)), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 416 | &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 417 | ASSERT_TRUE(created); |
| 418 | } |
| 419 | for (iter = directories->begin(); iter != directories->end(); ++iter) { |
| 420 | bool exclusive = true; |
| 421 | bool recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 422 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 423 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 424 | ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 425 | context.get(), root_url.WithPath(root_url.path().Append(*iter)), |
| 426 | exclusive, recursive)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 427 | } |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 428 | ValidateTestDirectory(root_url, *files, *directories); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 431 | void TestReadDirectoryHelper(const FileSystemURL& root_url) { |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 432 | std::set<FilePath::StringType> files; |
| 433 | std::set<FilePath::StringType> directories; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 434 | FillTestDirectory(root_url, &files, &directories); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 435 | |
| 436 | scoped_ptr<FileSystemOperationContext> context; |
| 437 | std::vector<base::FileUtilProxy::Entry> entries; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 438 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 439 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 440 | FileUtilHelper::ReadDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 441 | context.get(), ofu(), root_url, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 442 | std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; |
| 443 | EXPECT_EQ(files.size() + directories.size(), entries.size()); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 444 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 445 | for (entry_iter = entries.begin(); entry_iter != entries.end(); |
| 446 | ++entry_iter) { |
| 447 | const base::FileUtilProxy::Entry& entry = *entry_iter; |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 448 | std::set<FilePath::StringType>::iterator iter = files.find(entry.name); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 449 | if (iter != files.end()) { |
| 450 | EXPECT_FALSE(entry.is_directory); |
| 451 | files.erase(iter); |
| 452 | continue; |
| 453 | } |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 454 | iter = directories.find(entry.name); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 455 | EXPECT_FALSE(directories.end() == iter); |
| 456 | EXPECT_TRUE(entry.is_directory); |
| 457 | directories.erase(iter); |
| 458 | } |
| 459 | } |
| 460 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 461 | void TestTouchHelper(const FileSystemURL& url, bool is_file) { |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 462 | base::Time last_access_time = base::Time::Now(); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 463 | base::Time last_modified_time = base::Time::Now(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 464 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 465 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 466 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 467 | ofu()->Touch( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 468 | context.get(), url, last_access_time, last_modified_time)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 469 | // Currently we fire no change notifications for Touch. |
| 470 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 471 | FilePath local_path; |
| 472 | base::PlatformFileInfo file_info; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 473 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 474 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 475 | context.get(), url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 476 | // We compare as time_t here to lower our resolution, to avoid false |
| 477 | // negatives caused by conversion to the local filesystem's native |
| 478 | // representation and back. |
| 479 | EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
| 480 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 481 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 482 | last_modified_time += base::TimeDelta::FromHours(1); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 483 | last_access_time += base::TimeDelta::FromHours(14); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 484 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 485 | ofu()->Touch( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 486 | context.get(), url, last_access_time, last_modified_time)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 487 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 488 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 489 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 490 | context.get(), url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 491 | EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 492 | if (is_file) // Directories in OFU don't support atime. |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 493 | EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 494 | } |
| 495 | |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 496 | void TestCopyInForeignFileHelper(bool overwrite) { |
| 497 | ScopedTempDir source_dir; |
| 498 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 499 | FilePath root_file_path = source_dir.path(); |
| 500 | FilePath src_file_path = root_file_path.AppendASCII("file_name"); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 501 | FileSystemURL dest_url = CreateURLFromUTF8("new file"); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 502 | int64 src_file_length = 87; |
| 503 | |
| 504 | base::PlatformFileError error_code; |
| 505 | bool created = false; |
| 506 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 507 | base::PlatformFile file_handle = |
| 508 | base::CreatePlatformFile( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 509 | src_file_path, file_flags, &created, &error_code); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 510 | EXPECT_TRUE(created); |
| 511 | ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 512 | ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 513 | ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); |
| 514 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 515 | |
| 516 | scoped_ptr<FileSystemOperationContext> context; |
| 517 | |
| 518 | if (overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 519 | context.reset(NewContext(NULL)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 520 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 521 | ofu()->EnsureFileExists(context.get(), dest_url, &created)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 522 | EXPECT_TRUE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 523 | |
| 524 | // We must have observed one (and only one) create_file_count. |
| 525 | EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count()); |
| 526 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 527 | } |
| 528 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 529 | const int64 path_cost = |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 530 | ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 531 | if (!overwrite) { |
| 532 | // Verify that file creation requires sufficient quota for the path. |
| 533 | context.reset(NewContext(NULL)); |
| 534 | context->set_allowed_bytes_growth(path_cost + src_file_length - 1); |
| 535 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 536 | ofu()->CopyInForeignFile(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 537 | src_file_path, dest_url)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | context.reset(NewContext(NULL)); |
| 541 | context->set_allowed_bytes_growth(path_cost + src_file_length); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 542 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 543 | ofu()->CopyInForeignFile(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 544 | src_file_path, dest_url)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 545 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 546 | EXPECT_TRUE(PathExists(dest_url)); |
| 547 | EXPECT_FALSE(DirectoryExists(dest_url)); |
| 548 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 549 | context.reset(NewContext(NULL)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 550 | base::PlatformFileInfo file_info; |
| 551 | FilePath data_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 552 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 553 | context.get(), dest_url, &file_info, &data_path)); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 554 | EXPECT_NE(data_path, src_file_path); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 555 | EXPECT_TRUE(FileExists(data_path)); |
| 556 | EXPECT_EQ(src_file_length, GetSize(data_path)); |
| 557 | |
| 558 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 559 | ofu()->DeleteFile(context.get(), dest_url)); |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 560 | } |
| 561 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 562 | void ClearTimestamp(const FileSystemURL& url) { |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 563 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 564 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 565 | ofu()->Touch(context.get(), url, base::Time(), base::Time())); |
| 566 | EXPECT_EQ(base::Time(), GetModifiedTime(url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 567 | } |
| 568 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 569 | base::Time GetModifiedTime(const FileSystemURL& url) { |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 570 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 571 | FilePath data_path; |
| 572 | base::PlatformFileInfo file_info; |
| 573 | context.reset(NewContext(NULL)); |
| 574 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 575 | ofu()->GetFileInfo(context.get(), url, &file_info, &data_path)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 576 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 577 | return file_info.last_modified; |
| 578 | } |
| 579 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 580 | void TestDirectoryTimestampHelper(const FileSystemURL& base_dir, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 581 | bool copy, |
| 582 | bool overwrite) { |
| 583 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 584 | const FileSystemURL src_dir_url(base_dir.WithPath( |
| 585 | base_dir.path().AppendASCII("foo_dir"))); |
| 586 | const FileSystemURL dest_dir_url(base_dir.WithPath( |
| 587 | base_dir.path().AppendASCII("bar_dir"))); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 588 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 589 | const FileSystemURL src_file_url(src_dir_url.WithPath( |
| 590 | src_dir_url.path().AppendASCII("hoge"))); |
| 591 | const FileSystemURL dest_file_url(dest_dir_url.WithPath( |
| 592 | dest_dir_url.path().AppendASCII("fuga"))); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 593 | |
| 594 | context.reset(NewContext(NULL)); |
| 595 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 596 | ofu()->CreateDirectory(context.get(), src_dir_url, true, true)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 597 | context.reset(NewContext(NULL)); |
| 598 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 599 | ofu()->CreateDirectory(context.get(), dest_dir_url, true, true)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 600 | |
| 601 | bool created = false; |
| 602 | context.reset(NewContext(NULL)); |
| 603 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 604 | ofu()->EnsureFileExists(context.get(), src_file_url, &created)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 605 | if (overwrite) { |
| 606 | context.reset(NewContext(NULL)); |
| 607 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 608 | ofu()->EnsureFileExists(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 609 | dest_file_url, &created)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 612 | ClearTimestamp(src_dir_url); |
| 613 | ClearTimestamp(dest_dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 614 | context.reset(NewContext(NULL)); |
| 615 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 616 | ofu()->CopyOrMoveFile(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 617 | src_file_url, dest_file_url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 618 | copy)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 619 | if (copy) |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 620 | EXPECT_EQ(base::Time(), GetModifiedTime(src_dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 621 | else |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 622 | EXPECT_NE(base::Time(), GetModifiedTime(src_dir_url)); |
| 623 | EXPECT_NE(base::Time(), GetModifiedTime(dest_dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 624 | } |
| 625 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 626 | int64 ComputeCurrentUsage() { |
| 627 | return test_helper().ComputeCurrentOriginUsage() - |
| 628 | test_helper().ComputeCurrentDirectoryDatabaseUsage(); |
| 629 | } |
| 630 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 631 | const LocalFileSystemTestOriginHelper& test_helper() const { |
| 632 | return test_helper_; |
| 633 | } |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 634 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 635 | private: |
| 636 | ScopedTempDir data_dir_; |
[email protected] | 3ed847a6 | 2012-06-07 01:20:01 | [diff] [blame] | 637 | MessageLoop message_loop_; |
[email protected] | 3cfc10f | 2012-05-24 01:20:41 | [diff] [blame] | 638 | ObfuscatedFileUtil* obfuscated_file_util_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 639 | scoped_refptr<quota::QuotaManager> quota_manager_; |
| 640 | scoped_refptr<FileSystemContext> file_system_context_; |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 641 | GURL origin_; |
| 642 | fileapi::FileSystemType type_; |
[email protected] | 4d99be5 | 2011-10-18 14:11:03 | [diff] [blame] | 643 | base::WeakPtrFactory<ObfuscatedFileUtilTest> weak_factory_; |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 644 | LocalFileSystemTestOriginHelper test_helper_; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 645 | quota::QuotaStatusCode quota_status_; |
| 646 | int64 usage_; |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 647 | MockFileChangeObserver change_observer_; |
| 648 | ChangeObserverList change_observers_; |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 649 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 650 | DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 651 | }; |
| 652 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 653 | TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 654 | base::PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 655 | bool created; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 656 | FileSystemURL url = CreateURLFromUTF8("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 657 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 658 | int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 659 | |
| 660 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 661 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 662 | context.get(), url, file_flags, &file_handle, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 663 | &created)); |
| 664 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 665 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 666 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 667 | ofu()->DeleteFile(context.get(), url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 668 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 669 | url = CreateURLFromUTF8("test file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 670 | |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 671 | EXPECT_TRUE(change_observer()->HasNoChange()); |
| 672 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 673 | // Verify that file creation requires sufficient quota for the path. |
| 674 | context.reset(NewContext(NULL)); |
| 675 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 676 | ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 677 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 678 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 679 | context.get(), url, file_flags, &file_handle, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 680 | |
| 681 | context.reset(NewContext(NULL)); |
| 682 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 683 | ObfuscatedFileUtil::ComputeFilePathCost(url.path())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 684 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 685 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 686 | context.get(), url, file_flags, &file_handle, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 687 | ASSERT_TRUE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 688 | EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 689 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 690 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 691 | CheckFileAndCloseHandle(url, file_handle); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 692 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 693 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 694 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 695 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 696 | context.get(), url, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 697 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 698 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 699 | // Verify that deleting a file isn't stopped by zero quota, and that it frees |
| 700 | // up quote from its path. |
| 701 | context.reset(NewContext(NULL)); |
| 702 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 703 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 704 | ofu()->DeleteFile(context.get(), url)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 705 | EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 706 | EXPECT_FALSE(file_util::PathExists(local_path)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 707 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 708 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 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 | bool exclusive = true; |
| 712 | bool recursive = true; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 713 | FileSystemURL directory_url = CreateURLFromUTF8( |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 714 | "series/of/directories"); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 715 | url = directory_url.WithPath(directory_url.path().AppendASCII("file name")); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 716 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 717 | context.get(), directory_url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 718 | // The oepration created 3 directories recursively. |
| 719 | EXPECT_EQ(3, change_observer()->get_and_reset_create_directory_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 720 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 721 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 722 | file_handle = base::kInvalidPlatformFileValue; |
| 723 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 724 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 725 | context.get(), url, file_flags, &file_handle, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 726 | ASSERT_TRUE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 727 | EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 728 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 729 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 730 | CheckFileAndCloseHandle(url, file_handle); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 731 | |
[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_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 734 | context.get(), url, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 735 | EXPECT_TRUE(file_util::PathExists(local_path)); |
| 736 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 737 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 738 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 739 | ofu()->DeleteFile(context.get(), url)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 740 | EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 741 | EXPECT_FALSE(file_util::PathExists(local_path)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 742 | |
| 743 | // Make sure we have no unexpected changes. |
| 744 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 745 | } |
| 746 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 747 | TEST_F(ObfuscatedFileUtilTest, TestTruncate) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 748 | bool created = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 749 | FileSystemURL url = CreateURLFromUTF8("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 750 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 751 | |
| 752 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 753 | ofu()->Truncate(context.get(), url, 4)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 754 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 755 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 756 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 757 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 758 | ASSERT_TRUE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 759 | EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 760 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 761 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 762 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 763 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 764 | context.get(), url, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 765 | EXPECT_EQ(0, GetSize(local_path)); |
| 766 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 767 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 768 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 769 | context.get(), url, 10)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 770 | EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 771 | EXPECT_EQ(10, GetSize(local_path)); |
| 772 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 773 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 774 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 775 | context.get(), url, 1)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 776 | EXPECT_EQ(1, GetSize(local_path)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 777 | EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 778 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 779 | EXPECT_FALSE(DirectoryExists(url)); |
| 780 | EXPECT_TRUE(PathExists(url)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 781 | |
| 782 | // Make sure we have no unexpected changes. |
| 783 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 784 | } |
| 785 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 786 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnTruncation) { |
| 787 | bool created = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 788 | FileSystemURL url = CreateURLFromUTF8("file"); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 789 | |
| 790 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 791 | ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 792 | AllowUsageIncrease(PathCost(url))->context(), |
| 793 | url, &created)); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 794 | ASSERT_TRUE(created); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 795 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 796 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 797 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 798 | ofu()->Truncate( |
| 799 | AllowUsageIncrease(1020)->context(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 800 | url, 1020)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 801 | ASSERT_EQ(1020, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 802 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 803 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 804 | ofu()->Truncate( |
| 805 | AllowUsageIncrease(-1020)->context(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 806 | url, 0)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 807 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 808 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 809 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 810 | ofu()->Truncate( |
| 811 | DisallowUsageIncrease(1021)->context(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 812 | url, 1021)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 813 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 814 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 815 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 816 | ofu()->Truncate( |
| 817 | AllowUsageIncrease(1020)->context(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 818 | url, 1020)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 819 | ASSERT_EQ(1020, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 820 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 821 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 822 | ofu()->Truncate( |
| 823 | AllowUsageIncrease(0)->context(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 824 | url, 1020)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 825 | ASSERT_EQ(1020, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 826 | |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 827 | // quota exceeded |
| 828 | { |
| 829 | scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease(-1); |
| 830 | helper->context()->set_allowed_bytes_growth( |
| 831 | helper->context()->allowed_bytes_growth() - 1); |
| 832 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 833 | ofu()->Truncate(helper->context(), url, 1019)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 834 | ASSERT_EQ(1019, ComputeTotalFileSize()); |
| 835 | } |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 836 | |
| 837 | // Delete backing file to make following truncation fail. |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 838 | FilePath local_path; |
| 839 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 840 | ofu()->GetLocalFilePath( |
| 841 | UnlimitedContext().get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 842 | url, &local_path)); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 843 | ASSERT_FALSE(local_path.empty()); |
| 844 | ASSERT_TRUE(file_util::Delete(local_path, false)); |
| 845 | |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 846 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 847 | ofu()->Truncate( |
| 848 | LimitedContext(1234).get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 849 | url, 1234)); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 850 | ASSERT_EQ(0, ComputeTotalFileSize()); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 851 | } |
| 852 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 853 | TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 854 | FileSystemURL url = CreateURLFromUTF8("fake/file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 855 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 856 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 857 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 858 | ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 859 | context.get(), url, &created)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 860 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 861 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 862 | // Verify that file creation requires sufficient quota for the path. |
| 863 | context.reset(NewContext(NULL)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 864 | url = CreateURLFromUTF8("test file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 865 | created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 866 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 867 | ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 868 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 869 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 870 | ASSERT_FALSE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 871 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 872 | |
| 873 | context.reset(NewContext(NULL)); |
| 874 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 875 | ObfuscatedFileUtil::ComputeFilePathCost(url.path())); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 876 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 877 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 878 | ASSERT_TRUE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 879 | EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 880 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 881 | CheckFileAndCloseHandle(url, base::kInvalidPlatformFileValue); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 882 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 883 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 884 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 885 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 886 | ASSERT_FALSE(created); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 887 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 888 | |
| 889 | // Also test in a subdirectory. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 890 | url = CreateURLFromUTF8("path/to/file.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 891 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 892 | bool exclusive = true; |
| 893 | bool recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 894 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 895 | context.get(), |
| 896 | url.WithPath(url.path().DirName()), |
| 897 | exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 898 | // 2 directories: path/ and path/to. |
| 899 | EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 900 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 901 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 902 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 903 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 904 | ASSERT_TRUE(created); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 905 | EXPECT_FALSE(DirectoryExists(url)); |
| 906 | EXPECT_TRUE(PathExists(url)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 907 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 908 | } |
| 909 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 910 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 911 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 912 | |
| 913 | bool exclusive = false; |
| 914 | bool recursive = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 915 | FileSystemURL url = CreateURLFromUTF8("foo/bar"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 916 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 917 | context.get(), url, exclusive, recursive)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 918 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 919 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 920 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 921 | ofu()->DeleteSingleDirectory(context.get(), url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 922 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 923 | FileSystemURL root = CreateURLFromUTF8(""); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 924 | EXPECT_FALSE(DirectoryExists(url)); |
| 925 | EXPECT_FALSE(PathExists(url)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 926 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 927 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 928 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 929 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 930 | exclusive = false; |
| 931 | recursive = true; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 932 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 933 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 934 | EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 935 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 936 | EXPECT_TRUE(DirectoryExists(url)); |
| 937 | EXPECT_TRUE(PathExists(url)); |
| 938 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 939 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 940 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root)); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 941 | EXPECT_TRUE(DirectoryExists(url.WithPath(url.path().DirName()))); |
| 942 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 943 | context.reset(NewContext(NULL)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 944 | EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), |
| 945 | url.WithPath(url.path().DirName()))); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 946 | |
| 947 | // Can't remove a non-empty directory. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 948 | context.reset(NewContext(NULL)); |
[email protected] | c7a4a10f | 2011-05-19 04:56:06 | [diff] [blame] | 949 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 950 | ofu()->DeleteSingleDirectory(context.get(), |
| 951 | url.WithPath(url.path().DirName()))); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 952 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 953 | |
| 954 | base::PlatformFileInfo file_info; |
| 955 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 956 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 957 | context.get(), url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 958 | EXPECT_TRUE(local_path.empty()); |
| 959 | EXPECT_TRUE(file_info.is_directory); |
| 960 | EXPECT_FALSE(file_info.is_symbolic_link); |
| 961 | |
| 962 | // Same create again should succeed, since exclusive is false. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 963 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 964 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 965 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 966 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 967 | |
| 968 | exclusive = true; |
| 969 | recursive = true; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 970 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 971 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 972 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 973 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 974 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 975 | // Verify that deleting a directory isn't stopped by zero quota, and that it |
| 976 | // frees up quota from its path. |
| 977 | context.reset(NewContext(NULL)); |
| 978 | context->set_allowed_bytes_growth(0); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 979 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 980 | ofu()->DeleteSingleDirectory(context.get(), url)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 981 | EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count()); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 982 | EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(url.path()), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 983 | context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 984 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 985 | url = CreateURLFromUTF8("foo/bop"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 986 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 987 | EXPECT_FALSE(DirectoryExists(url)); |
| 988 | EXPECT_FALSE(PathExists(url)); |
| 989 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 990 | context.reset(NewContext(NULL)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 991 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 992 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 993 | context.get(), url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 994 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 995 | // Verify that file creation requires sufficient quota for the path. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 996 | exclusive = true; |
| 997 | recursive = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 998 | context.reset(NewContext(NULL)); |
| 999 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1000 | ObfuscatedFileUtil::ComputeFilePathCost(url.path()) - 1); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1001 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1002 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1003 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1004 | |
| 1005 | context.reset(NewContext(NULL)); |
| 1006 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1007 | ObfuscatedFileUtil::ComputeFilePathCost(url.path())); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1008 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1009 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1010 | EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1011 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1012 | EXPECT_TRUE(DirectoryExists(url)); |
| 1013 | EXPECT_TRUE(PathExists(url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1014 | |
| 1015 | exclusive = true; |
| 1016 | recursive = false; |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1017 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1018 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1019 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1020 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1021 | |
| 1022 | exclusive = true; |
| 1023 | recursive = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1024 | url = CreateURLFromUTF8("foo"); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1025 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1026 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1027 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1028 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1029 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1030 | url = CreateURLFromUTF8("blah"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1031 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1032 | EXPECT_FALSE(DirectoryExists(url)); |
| 1033 | EXPECT_FALSE(PathExists(url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1034 | |
| 1035 | exclusive = true; |
| 1036 | recursive = false; |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1037 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1038 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1039 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1040 | EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1041 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1042 | EXPECT_TRUE(DirectoryExists(url)); |
| 1043 | EXPECT_TRUE(PathExists(url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1044 | |
| 1045 | exclusive = true; |
| 1046 | recursive = false; |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1047 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1048 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1049 | context.get(), url, exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1050 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1051 | } |
| 1052 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1053 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1054 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1055 | bool exclusive = true; |
| 1056 | bool recursive = true; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1057 | FileSystemURL url = CreateURLFromUTF8("directory/to/use"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1058 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1059 | context.get(), url, exclusive, recursive)); |
| 1060 | TestReadDirectoryHelper(url); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1061 | } |
| 1062 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1063 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1064 | TestReadDirectoryHelper(CreateURLFromUTF8("")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1065 | } |
| 1066 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1067 | TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1068 | TestReadDirectoryHelper(CreateURLFromUTF8("/")); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1069 | } |
| 1070 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1071 | TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1072 | FileSystemURL url = CreateURLFromUTF8("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1073 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1074 | |
| 1075 | bool created = false; |
| 1076 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1077 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1078 | ASSERT_TRUE(created); |
| 1079 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1080 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1081 | std::vector<base::FileUtilProxy::Entry> entries; |
| 1082 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1083 | FileUtilHelper::ReadDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1084 | context.get(), ofu(), url, &entries)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1085 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1086 | EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1087 | } |
| 1088 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1089 | TEST_F(ObfuscatedFileUtilTest, TestTouch) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1090 | FileSystemURL url = CreateURLFromUTF8("file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1091 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1092 | |
| 1093 | base::Time last_access_time = base::Time::Now(); |
| 1094 | base::Time last_modified_time = base::Time::Now(); |
| 1095 | |
| 1096 | // It's not there yet. |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1097 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1098 | ofu()->Touch( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1099 | context.get(), url, last_access_time, last_modified_time)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1100 | |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1101 | // OK, now create it. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1102 | context.reset(NewContext(NULL)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1103 | bool created = false; |
| 1104 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1105 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1106 | ASSERT_TRUE(created); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1107 | TestTouchHelper(url, true); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1108 | |
| 1109 | // Now test a directory: |
| 1110 | context.reset(NewContext(NULL)); |
| 1111 | bool exclusive = true; |
| 1112 | bool recursive = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1113 | url = CreateURLFromUTF8("dir"); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1114 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1115 | url, exclusive, recursive)); |
| 1116 | TestTouchHelper(url, false); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1117 | } |
| 1118 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1119 | TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1120 | FileSystemURL url = CreateURLFromUTF8("fake/file"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1121 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1122 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1123 | url = CreateURLFromUTF8("file name"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1124 | context->set_allowed_bytes_growth(5); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1125 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1126 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1127 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1128 | EXPECT_FALSE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1129 | context->set_allowed_bytes_growth(1024); |
| 1130 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1131 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | 2517cfa | 2011-08-25 05:12:41 | [diff] [blame] | 1132 | EXPECT_TRUE(created); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1133 | int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(url.path()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1134 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
| 1135 | |
| 1136 | context->set_allowed_bytes_growth(1024); |
| 1137 | bool exclusive = true; |
| 1138 | bool recursive = true; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1139 | url = CreateURLFromUTF8("directory/to/use"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1140 | std::vector<FilePath::StringType> components; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1141 | url.path().GetComponents(&components); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1142 | path_cost = 0; |
| 1143 | for (std::vector<FilePath::StringType>::iterator iter = components.begin(); |
| 1144 | iter != components.end(); ++iter) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1145 | path_cost += ObfuscatedFileUtil::ComputeFilePathCost( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1146 | FilePath(*iter)); |
| 1147 | } |
| 1148 | context.reset(NewContext(NULL)); |
| 1149 | context->set_allowed_bytes_growth(1024); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1150 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1151 | context.get(), url, exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1152 | EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1153 | } |
| 1154 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1155 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1156 | FileSystemURL source_url = CreateURLFromUTF8("path0.txt"); |
| 1157 | FileSystemURL dest_url = CreateURLFromUTF8("path1.txt"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1158 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1159 | |
| 1160 | bool is_copy_not_move = false; |
| 1161 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1162 | ofu()->CopyOrMoveFile(context.get(), source_url, dest_url, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1163 | is_copy_not_move)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1164 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1165 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1166 | is_copy_not_move = true; |
| 1167 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1168 | ofu()->CopyOrMoveFile(context.get(), source_url, dest_url, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1169 | is_copy_not_move)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1170 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1171 | source_url = CreateURLFromUTF8("dir/dir/file"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1172 | bool exclusive = true; |
| 1173 | bool recursive = true; |
[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 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1176 | context.get(), |
| 1177 | source_url.WithPath(source_url.path().DirName()), |
| 1178 | exclusive, recursive)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1179 | EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1180 | is_copy_not_move = false; |
| 1181 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1182 | ofu()->CopyOrMoveFile(context.get(), source_url, dest_url, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1183 | is_copy_not_move)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1184 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1185 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1186 | is_copy_not_move = true; |
| 1187 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1188 | ofu()->CopyOrMoveFile(context.get(), source_url, dest_url, |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1189 | is_copy_not_move)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1190 | EXPECT_TRUE(change_observer()->HasNoChange()); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1191 | } |
| 1192 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1193 | TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) { |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1194 | const int64 kSourceLength = 5; |
| 1195 | const int64 kDestLength = 50; |
| 1196 | |
| 1197 | for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { |
| 1198 | SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); |
| 1199 | const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; |
| 1200 | SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << |
| 1201 | test_case.is_copy_not_move); |
| 1202 | SCOPED_TRACE(testing::Message() << "\t source_path " << |
| 1203 | test_case.source_path); |
| 1204 | SCOPED_TRACE(testing::Message() << "\t dest_path " << |
| 1205 | test_case.dest_path); |
| 1206 | SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << |
| 1207 | test_case.cause_overwrite); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1208 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1209 | |
| 1210 | bool exclusive = false; |
| 1211 | bool recursive = true; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1212 | FileSystemURL source_url = CreateURLFromUTF8(test_case.source_path); |
| 1213 | FileSystemURL dest_url = CreateURLFromUTF8(test_case.dest_path); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1214 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1215 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1216 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1217 | context.get(), |
| 1218 | source_url.WithPath(source_url.path().DirName()), |
| 1219 | exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1220 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1221 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1222 | context.get(), |
| 1223 | dest_url.WithPath(dest_url.path().DirName()), |
| 1224 | exclusive, recursive)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1225 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1226 | bool created = false; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1227 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1228 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1229 | ofu()->EnsureFileExists(context.get(), source_url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1230 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1231 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1232 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1233 | ofu()->Truncate(context.get(), source_url, kSourceLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1234 | |
| 1235 | if (test_case.cause_overwrite) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1236 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1237 | created = false; |
| 1238 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1239 | ofu()->EnsureFileExists(context.get(), dest_url, &created)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1240 | ASSERT_TRUE(created); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1241 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1242 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1243 | ofu()->Truncate(context.get(), dest_url, kDestLength)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1244 | } |
| 1245 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1246 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1247 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1248 | source_url, dest_url, test_case.is_copy_not_move)); |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 1249 | |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1250 | if (test_case.is_copy_not_move) { |
| 1251 | base::PlatformFileInfo file_info; |
| 1252 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1253 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1254 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1255 | context.get(), source_url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1256 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1257 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1258 | ofu()->DeleteFile(context.get(), source_url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1259 | } else { |
| 1260 | base::PlatformFileInfo file_info; |
| 1261 | FilePath local_path; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1262 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1263 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1264 | context.get(), source_url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1265 | } |
| 1266 | base::PlatformFileInfo file_info; |
| 1267 | FilePath local_path; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1268 | EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1269 | context.get(), dest_url, &file_info, &local_path)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1270 | EXPECT_EQ(kSourceLength, file_info.size); |
| 1271 | |
| 1272 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1273 | ofu()->DeleteFile(context.get(), dest_url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1277 | TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1278 | FileSystemURL src_url = CreateURLFromUTF8("src path"); |
| 1279 | FileSystemURL dest_url = CreateURLFromUTF8("destination path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1280 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1281 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1282 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1283 | context.get(), src_url, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1284 | |
| 1285 | bool is_copy = true; |
| 1286 | // Copy, no overwrite. |
| 1287 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1288 | ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1289 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1290 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1291 | context.reset(NewContext(NULL)); |
| 1292 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1293 | ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1294 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1295 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1296 | |
| 1297 | // Copy, with overwrite. |
| 1298 | context.reset(NewContext(NULL)); |
| 1299 | context->set_allowed_bytes_growth(0); |
| 1300 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1301 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1302 | } |
| 1303 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1304 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1305 | FileSystemURL src_url = CreateURLFromUTF8("src path"); |
| 1306 | FileSystemURL dest_url = CreateURLFromUTF8("destination path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1307 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1308 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1309 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1310 | context.get(), src_url, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1311 | |
| 1312 | bool is_copy = false; |
| 1313 | // Move, rename, no overwrite. |
| 1314 | context.reset(NewContext(NULL)); |
| 1315 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1316 | ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - |
| 1317 | ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()) - 1); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1318 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1319 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1320 | context.reset(NewContext(NULL)); |
| 1321 | context->set_allowed_bytes_growth( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1322 | ObfuscatedFileUtil::ComputeFilePathCost(dest_url.path()) - |
| 1323 | ObfuscatedFileUtil::ComputeFilePathCost(src_url.path())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1324 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1325 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1326 | |
| 1327 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1328 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1329 | context.get(), src_url, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1330 | |
| 1331 | // Move, rename, with overwrite. |
| 1332 | context.reset(NewContext(NULL)); |
| 1333 | context->set_allowed_bytes_growth(0); |
| 1334 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1335 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1336 | } |
| 1337 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1338 | TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1339 | FileSystemURL src_url = CreateURLFromUTF8("src path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1340 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1341 | bool created = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1342 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1343 | context.get(), src_url, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1344 | |
| 1345 | bool exclusive = true; |
| 1346 | bool recursive = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1347 | FileSystemURL dir_url = CreateURLFromUTF8("directory path"); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1348 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1349 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1350 | context.get(), dir_url, exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1351 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1352 | FileSystemURL dest_url = dir_url.WithPath( |
| 1353 | dir_url.path().Append(src_url.path())); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1354 | |
| 1355 | bool is_copy = false; |
| 1356 | int64 allowed_bytes_growth = -1000; // Over quota, this should still work. |
| 1357 | // Move, no rename, no overwrite. |
| 1358 | context.reset(NewContext(NULL)); |
| 1359 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1360 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1361 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1362 | EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); |
| 1363 | |
| 1364 | // Move, no rename, with overwrite. |
| 1365 | context.reset(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1366 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1367 | context.get(), src_url, &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1368 | context.reset(NewContext(NULL)); |
| 1369 | context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1370 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1371 | ofu()->CopyOrMoveFile(context.get(), src_url, dest_url, is_copy)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1372 | EXPECT_EQ( |
| 1373 | allowed_bytes_growth + |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1374 | ObfuscatedFileUtil::ComputeFilePathCost(src_url.path()), |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1375 | context->allowed_bytes_growth()); |
| 1376 | } |
| 1377 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1378 | TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) { |
[email protected] | 81b7f66 | 2011-05-26 00:54:46 | [diff] [blame] | 1379 | TestCopyInForeignFileHelper(false /* overwrite */); |
| 1380 | TestCopyInForeignFileHelper(true /* overwrite */); |
| 1381 | } |
| 1382 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1383 | TEST_F(ObfuscatedFileUtilTest, TestEnumerator) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1384 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1385 | FileSystemURL src_url = CreateURLFromUTF8("source dir"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1386 | bool exclusive = true; |
| 1387 | bool recursive = false; |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1388 | ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1389 | context.get(), src_url, exclusive, recursive)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1390 | |
[email protected] | 89ee427 | 2011-05-16 18:45:17 | [diff] [blame] | 1391 | std::set<FilePath::StringType> files; |
| 1392 | std::set<FilePath::StringType> directories; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1393 | FillTestDirectory(src_url, &files, &directories); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1394 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1395 | FileSystemURL dest_url = CreateURLFromUTF8("destination dir"); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1396 | |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1397 | EXPECT_FALSE(DirectoryExists(dest_url)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1398 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1399 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1400 | test_helper().SameFileUtilCopy(context.get(), src_url, dest_url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1401 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1402 | ValidateTestDirectory(dest_url, files, directories); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1403 | EXPECT_TRUE(DirectoryExists(src_url)); |
| 1404 | EXPECT_TRUE(DirectoryExists(dest_url)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1405 | context.reset(NewContext(NULL)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1406 | recursive = true; |
| 1407 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | aa437fa | 2012-03-03 02:09:07 | [diff] [blame] | 1408 | FileUtilHelper::Delete(context.get(), ofu(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1409 | dest_url, recursive)); |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1410 | EXPECT_FALSE(DirectoryExists(dest_url)); |
[email protected] | d4905e2e | 2011-05-13 21:56:32 | [diff] [blame] | 1411 | } |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1412 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1413 | TEST_F(ObfuscatedFileUtilTest, TestMigration) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1414 | ScopedTempDir source_dir; |
| 1415 | ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 1416 | FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); |
| 1417 | ASSERT_TRUE(file_util::CreateDirectory(root_path)); |
| 1418 | |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1419 | test::SetUpRegularTestCases(root_path); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1420 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1421 | EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1422 | |
| 1423 | FilePath new_root = |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1424 | test_directory().AppendASCII("File System").AppendASCII("000").Append( |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1425 | ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1426 | for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) { |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1427 | SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1428 | const test::TestCaseRecord& test_case = test::kRegularTestCases[i]; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1429 | FilePath local_data_path = new_root.Append(test_case.path); |
[email protected] | d9034ed2 | 2012-02-10 02:04:40 | [diff] [blame] | 1430 | local_data_path = local_data_path.NormalizePathSeparators(); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1431 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1432 | base::PlatformFileInfo ofu_file_info; |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1433 | FilePath data_path; |
| 1434 | SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); |
| 1435 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1436 | ofu()->GetFileInfo(context.get(), CreateURL(FilePath(test_case.path)), |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1437 | &ofu_file_info, &data_path)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1438 | if (test_case.is_directory) { |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1439 | EXPECT_TRUE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1440 | } else { |
| 1441 | base::PlatformFileInfo platform_file_info; |
| 1442 | SCOPED_TRACE(testing::Message() << "local_data_path is " << |
| 1443 | local_data_path.value()); |
| 1444 | SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); |
| 1445 | ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); |
| 1446 | EXPECT_EQ(test_case.data_file_size, platform_file_info.size); |
| 1447 | EXPECT_FALSE(platform_file_info.is_directory); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1448 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1449 | EXPECT_EQ(local_data_path, data_path); |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1450 | EXPECT_EQ(platform_file_info.size, ofu_file_info.size); |
| 1451 | EXPECT_FALSE(ofu_file_info.is_directory); |
[email protected] | 6b93115 | 2011-05-20 21:02:35 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1455 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1456 | TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) { |
| 1457 | scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> |
| 1458 | enumerator(ofu()->CreateOriginEnumerator()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1459 | // The test helper starts out with a single filesystem. |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1460 | EXPECT_TRUE(enumerator.get()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1461 | EXPECT_EQ(origin(), enumerator->Next()); |
| 1462 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1463 | EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1464 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1465 | EXPECT_EQ(GURL(), enumerator->Next()); |
| 1466 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1467 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1468 | |
| 1469 | std::set<GURL> origins_expected; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1470 | origins_expected.insert(origin()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1471 | |
| 1472 | for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { |
| 1473 | SCOPED_TRACE(testing::Message() << |
| 1474 | "Validating kOriginEnumerationTestRecords " << i); |
| 1475 | const OriginEnumerationTestRecord& record = |
| 1476 | kOriginEnumerationTestRecords[i]; |
| 1477 | GURL origin_url(record.origin_url); |
| 1478 | origins_expected.insert(origin_url); |
| 1479 | if (record.has_temporary) { |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 1480 | scoped_ptr<LocalFileSystemTestOriginHelper> helper( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1481 | NewHelper(origin_url, kFileSystemTypeTemporary)); |
| 1482 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1483 | bool created = false; |
| 1484 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1485 | ofu()->EnsureFileExists( |
| 1486 | context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1487 | helper->CreateURLFromUTF8("file"), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1488 | &created)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1489 | EXPECT_TRUE(created); |
| 1490 | } |
| 1491 | if (record.has_persistent) { |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 1492 | scoped_ptr<LocalFileSystemTestOriginHelper> helper( |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1493 | NewHelper(origin_url, kFileSystemTypePersistent)); |
| 1494 | scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1495 | bool created = false; |
| 1496 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1497 | ofu()->EnsureFileExists( |
| 1498 | context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1499 | helper->CreateURLFromUTF8("file"), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1500 | &created)); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1501 | EXPECT_TRUE(created); |
| 1502 | } |
| 1503 | } |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1504 | enumerator.reset(ofu()->CreateOriginEnumerator()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1505 | EXPECT_TRUE(enumerator.get()); |
| 1506 | std::set<GURL> origins_found; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1507 | GURL origin_url; |
| 1508 | while (!(origin_url = enumerator->Next()).is_empty()) { |
| 1509 | origins_found.insert(origin_url); |
| 1510 | SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1511 | bool found = false; |
| 1512 | for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); |
| 1513 | ++i) { |
| 1514 | const OriginEnumerationTestRecord& record = |
| 1515 | kOriginEnumerationTestRecords[i]; |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1516 | if (GURL(record.origin_url) != origin_url) |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1517 | continue; |
| 1518 | found = true; |
| 1519 | EXPECT_EQ(record.has_temporary, |
| 1520 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1521 | EXPECT_EQ(record.has_persistent, |
| 1522 | enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1523 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1524 | // Deal with the default filesystem created by the test helper. |
| 1525 | if (!found && origin_url == origin()) { |
| 1526 | ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1527 | EXPECT_EQ(true, |
| 1528 | enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
[email protected] | 34161bb | 2011-10-13 12:36:18 | [diff] [blame] | 1529 | EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1530 | found = true; |
| 1531 | } |
[email protected] | fcc2d5f | 2011-05-23 22:06:26 | [diff] [blame] | 1532 | EXPECT_TRUE(found); |
| 1533 | } |
| 1534 | |
| 1535 | std::set<GURL> diff; |
| 1536 | std::set_symmetric_difference(origins_expected.begin(), |
| 1537 | origins_expected.end(), origins_found.begin(), origins_found.end(), |
| 1538 | inserter(diff, diff.begin())); |
| 1539 | EXPECT_TRUE(diff.empty()); |
| 1540 | } |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1541 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1542 | TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1543 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1544 | |
| 1545 | int64 expected_quota = 0; |
| 1546 | |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1547 | for (size_t i = 0; i < test::kRegularTestCaseSize; ++i) { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1548 | SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
[email protected] | f83b5b7 | 2012-01-27 10:26:56 | [diff] [blame] | 1549 | const test::TestCaseRecord& test_case = test::kRegularTestCases[i]; |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1550 | FilePath file_path(test_case.path); |
| 1551 | expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(file_path); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1552 | if (test_case.is_directory) { |
| 1553 | bool exclusive = true; |
| 1554 | bool recursive = false; |
| 1555 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1556 | ofu()->CreateDirectory(context.get(), CreateURL(file_path), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1557 | exclusive, recursive)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1558 | } else { |
| 1559 | bool created = false; |
| 1560 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1561 | ofu()->EnsureFileExists(context.get(), CreateURL(file_path), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1562 | &created)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1563 | ASSERT_TRUE(created); |
| 1564 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1565 | ofu()->Truncate(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1566 | CreateURL(file_path), |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1567 | test_case.data_file_size)); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1568 | expected_quota += test_case.data_file_size; |
| 1569 | } |
| 1570 | } |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1571 | |
| 1572 | // Usually raw size in usage cache and the usage returned by QuotaUtil |
| 1573 | // should be same. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1574 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1575 | EXPECT_EQ(expected_quota, SizeByQuotaUtil()); |
| 1576 | |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1577 | RevokeUsageCache(); |
| 1578 | EXPECT_EQ(-1, SizeInUsageFile()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1579 | EXPECT_EQ(expected_quota, SizeByQuotaUtil()); |
| 1580 | |
| 1581 | // This should reconstruct the cache. |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1582 | GetUsageFromQuotaManager(); |
| 1583 | EXPECT_EQ(expected_quota, SizeInUsageFile()); |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 1584 | EXPECT_EQ(expected_quota, SizeByQuotaUtil()); |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 1585 | EXPECT_EQ(expected_quota, usage()); |
| 1586 | } |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1587 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1588 | TEST_F(ObfuscatedFileUtilTest, TestInconsistency) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1589 | const FileSystemURL kPath1 = CreateURLFromUTF8("hoge"); |
| 1590 | const FileSystemURL kPath2 = CreateURLFromUTF8("fuga"); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1591 | |
| 1592 | scoped_ptr<FileSystemOperationContext> context; |
| 1593 | base::PlatformFile file; |
| 1594 | base::PlatformFileInfo file_info; |
| 1595 | FilePath data_path; |
| 1596 | bool created = false; |
| 1597 | |
| 1598 | // Create a non-empty file. |
| 1599 | context.reset(NewContext(NULL)); |
| 1600 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1601 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1602 | EXPECT_TRUE(created); |
| 1603 | context.reset(NewContext(NULL)); |
| 1604 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1605 | ofu()->Truncate(context.get(), kPath1, 10)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1606 | context.reset(NewContext(NULL)); |
| 1607 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1608 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1609 | context.get(), kPath1, &file_info, &data_path)); |
| 1610 | EXPECT_EQ(10, file_info.size); |
| 1611 | |
| 1612 | // Destroy database to make inconsistency between database and filesystem. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1613 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1614 | |
| 1615 | // Try to get file info of broken file. |
[email protected] | 13f92f6e | 2012-08-13 07:39:14 | [diff] [blame] | 1616 | EXPECT_FALSE(PathExists(kPath1)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1617 | context.reset(NewContext(NULL)); |
| 1618 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1619 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1620 | EXPECT_TRUE(created); |
| 1621 | context.reset(NewContext(NULL)); |
| 1622 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1623 | ofu()->GetFileInfo( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1624 | context.get(), kPath1, &file_info, &data_path)); |
| 1625 | EXPECT_EQ(0, file_info.size); |
| 1626 | |
| 1627 | // Make another broken file to |kPath2|. |
| 1628 | context.reset(NewContext(NULL)); |
| 1629 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1630 | ofu()->EnsureFileExists(context.get(), kPath2, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1631 | EXPECT_TRUE(created); |
| 1632 | |
| 1633 | // Destroy again. |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1634 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1635 | |
| 1636 | // Repair broken |kPath1|. |
| 1637 | context.reset(NewContext(NULL)); |
| 1638 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1639 | ofu()->Touch(context.get(), kPath1, base::Time::Now(), |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1640 | base::Time::Now())); |
| 1641 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1642 | ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1643 | EXPECT_TRUE(created); |
| 1644 | |
| 1645 | // Copy from sound |kPath1| to broken |kPath2|. |
| 1646 | context.reset(NewContext(NULL)); |
| 1647 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1648 | ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1649 | true /* copy */)); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1650 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1651 | ofu()->DestroyDirectoryDatabase(origin(), type()); |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1652 | context.reset(NewContext(NULL)); |
| 1653 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1654 | ofu()->CreateOrOpen( |
[email protected] | 3458333 | 2011-08-31 08:59:47 | [diff] [blame] | 1655 | context.get(), kPath1, |
| 1656 | base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE, |
| 1657 | &file, &created)); |
| 1658 | EXPECT_TRUE(created); |
| 1659 | EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); |
| 1660 | EXPECT_EQ(0, file_info.size); |
| 1661 | EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 1662 | } |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1663 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1664 | TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1665 | const FileSystemURL kPath[] = { |
| 1666 | CreateURLFromUTF8("foo"), |
| 1667 | CreateURLFromUTF8("bar"), |
| 1668 | CreateURLFromUTF8("baz") |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1669 | }; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1670 | const FileSystemURL empty_path = CreateURL(FilePath()); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1671 | scoped_ptr<FileSystemOperationContext> context; |
| 1672 | |
| 1673 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) { |
| 1674 | bool created = false; |
| 1675 | context.reset(NewContext(NULL)); |
| 1676 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1677 | ofu()->EnsureFileExists(context.get(), kPath[i], &created)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1678 | EXPECT_TRUE(created); |
| 1679 | } |
| 1680 | |
| 1681 | context.reset(NewContext(NULL)); |
| 1682 | std::vector<base::FileUtilProxy::Entry> entries; |
| 1683 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1684 | FileUtilHelper::ReadDirectory( |
| 1685 | context.get(), ofu(), empty_path, &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1686 | EXPECT_EQ(3u, entries.size()); |
| 1687 | |
| 1688 | context.reset(NewContext(NULL)); |
| 1689 | FilePath local_path; |
| 1690 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 1691 | ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1692 | EXPECT_TRUE(file_util::Delete(local_path, false)); |
| 1693 | |
| 1694 | context.reset(NewContext(NULL)); |
| 1695 | entries.clear(); |
| 1696 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1697 | FileUtilHelper::ReadDirectory( |
| 1698 | context.get(), ofu(), empty_path, &entries)); |
[email protected] | 9dfdc0e3 | 2011-09-02 06:07:44 | [diff] [blame] | 1699 | EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); |
| 1700 | } |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1701 | |
| 1702 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCreation) { |
| 1703 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1704 | const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir"); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1705 | |
| 1706 | // Create working directory. |
| 1707 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1708 | ofu()->CreateDirectory(context.get(), dir_url, false, false)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1709 | |
| 1710 | // EnsureFileExists, create case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1711 | FileSystemURL url(dir_url.WithPath( |
| 1712 | dir_url.path().AppendASCII("EnsureFileExists_file"))); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1713 | bool created = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1714 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1715 | context.reset(NewContext(NULL)); |
| 1716 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1717 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1718 | EXPECT_TRUE(created); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1719 | EXPECT_NE(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1720 | |
| 1721 | // non create case. |
| 1722 | created = true; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1723 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1724 | context.reset(NewContext(NULL)); |
| 1725 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1726 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1727 | EXPECT_FALSE(created); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1728 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1729 | |
| 1730 | // fail case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1731 | url = dir_url.WithPath(dir_url.path().AppendASCII("EnsureFileExists_dir")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1732 | context.reset(NewContext(NULL)); |
| 1733 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1734 | ofu()->CreateDirectory(context.get(), url, false, false)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1735 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1736 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1737 | context.reset(NewContext(NULL)); |
| 1738 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1739 | ofu()->EnsureFileExists(context.get(), url, &created)); |
| 1740 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1741 | |
| 1742 | // CreateOrOpen, create case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1743 | url = dir_url.WithPath(dir_url.path().AppendASCII("CreateOrOpen_file")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1744 | PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 1745 | created = false; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1746 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1747 | context.reset(NewContext(NULL)); |
| 1748 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1749 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1750 | context.get(), url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1751 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| 1752 | &file_handle, &created)); |
| 1753 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1754 | EXPECT_TRUE(created); |
| 1755 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1756 | EXPECT_NE(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1757 | |
| 1758 | // open case. |
| 1759 | file_handle = base::kInvalidPlatformFileValue; |
| 1760 | created = true; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1761 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1762 | context.reset(NewContext(NULL)); |
| 1763 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1764 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1765 | context.get(), url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1766 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 1767 | &file_handle, &created)); |
| 1768 | EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1769 | EXPECT_FALSE(created); |
| 1770 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1771 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1772 | |
| 1773 | // fail case |
| 1774 | file_handle = base::kInvalidPlatformFileValue; |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1775 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1776 | context.reset(NewContext(NULL)); |
| 1777 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
| 1778 | ofu()->CreateOrOpen( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1779 | context.get(), url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1780 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
| 1781 | &file_handle, &created)); |
| 1782 | EXPECT_EQ(base::kInvalidPlatformFileValue, file_handle); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1783 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1784 | |
| 1785 | // CreateDirectory, create case. |
| 1786 | // Creating CreateDirectory_dir and CreateDirectory_dir/subdir. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1787 | url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir")); |
| 1788 | FileSystemURL subdir_url(url.WithPath(url.path().AppendASCII("subdir"))); |
| 1789 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1790 | context.reset(NewContext(NULL)); |
| 1791 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1792 | ofu()->CreateDirectory(context.get(), subdir_url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1793 | true /* exclusive */, true /* recursive */)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1794 | EXPECT_NE(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1795 | |
| 1796 | // create subdir case. |
| 1797 | // Creating CreateDirectory_dir/subdir2. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1798 | subdir_url = url.WithPath(url.path().AppendASCII("subdir2")); |
| 1799 | ClearTimestamp(dir_url); |
| 1800 | ClearTimestamp(url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1801 | context.reset(NewContext(NULL)); |
| 1802 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1803 | ofu()->CreateDirectory(context.get(), subdir_url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1804 | true /* exclusive */, true /* recursive */)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1805 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
| 1806 | EXPECT_NE(base::Time(), GetModifiedTime(url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1807 | |
| 1808 | // fail case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1809 | url = dir_url.WithPath(dir_url.path().AppendASCII("CreateDirectory_dir")); |
| 1810 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1811 | context.reset(NewContext(NULL)); |
| 1812 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1813 | ofu()->CreateDirectory(context.get(), url, |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1814 | true /* exclusive */, true /* recursive */)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1815 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1816 | |
| 1817 | // CopyInForeignFile, create case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1818 | url = dir_url.WithPath(dir_url.path().AppendASCII("CopyInForeignFile_file")); |
| 1819 | FileSystemURL src_path = dir_url.WithPath( |
| 1820 | dir_url.path().AppendASCII("CopyInForeignFile_src_file")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1821 | context.reset(NewContext(NULL)); |
| 1822 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1823 | ofu()->EnsureFileExists(context.get(), src_path, &created)); |
| 1824 | EXPECT_TRUE(created); |
| 1825 | FilePath src_local_path; |
| 1826 | context.reset(NewContext(NULL)); |
| 1827 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1828 | ofu()->GetLocalFilePath(context.get(), src_path, &src_local_path)); |
| 1829 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1830 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1831 | context.reset(NewContext(NULL)); |
| 1832 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 1833 | ofu()->CopyInForeignFile(context.get(), |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1834 | src_local_path, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1835 | url)); |
| 1836 | EXPECT_NE(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForDeletion) { |
| 1840 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1841 | const FileSystemURL dir_url = CreateURLFromUTF8("foo_dir"); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1842 | |
| 1843 | // Create working directory. |
| 1844 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1845 | ofu()->CreateDirectory(context.get(), dir_url, false, false)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1846 | |
| 1847 | // DeleteFile, delete case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1848 | FileSystemURL url = dir_url.WithPath( |
| 1849 | dir_url.path().AppendASCII("DeleteFile_file")); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1850 | bool created = false; |
| 1851 | context.reset(NewContext(NULL)); |
| 1852 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1853 | ofu()->EnsureFileExists(context.get(), url, &created)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1854 | EXPECT_TRUE(created); |
| 1855 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1856 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1857 | context.reset(NewContext(NULL)); |
| 1858 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1859 | ofu()->DeleteFile(context.get(), url)); |
| 1860 | EXPECT_NE(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1861 | |
| 1862 | // fail case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1863 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1864 | context.reset(NewContext(NULL)); |
| 1865 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1866 | ofu()->DeleteFile(context.get(), url)); |
| 1867 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1868 | |
| 1869 | // DeleteSingleDirectory, fail case. |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1870 | url = dir_url.WithPath( |
| 1871 | dir_url.path().AppendASCII("DeleteSingleDirectory_dir")); |
| 1872 | FileSystemURL file_path(url.WithPath(url.path().AppendASCII("pakeratta"))); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1873 | context.reset(NewContext(NULL)); |
| 1874 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1875 | ofu()->CreateDirectory(context.get(), url, true, true)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1876 | created = false; |
| 1877 | context.reset(NewContext(NULL)); |
| 1878 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1879 | ofu()->EnsureFileExists(context.get(), file_path, &created)); |
| 1880 | EXPECT_TRUE(created); |
| 1881 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1882 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1883 | context.reset(NewContext(NULL)); |
| 1884 | EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1885 | ofu()->DeleteSingleDirectory(context.get(), url)); |
| 1886 | EXPECT_EQ(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1887 | |
| 1888 | // delete case. |
| 1889 | context.reset(NewContext(NULL)); |
| 1890 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1891 | ofu()->DeleteFile(context.get(), file_path)); |
| 1892 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1893 | ClearTimestamp(dir_url); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1894 | context.reset(NewContext(NULL)); |
| 1895 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1896 | ofu()->DeleteSingleDirectory(context.get(), url)); |
| 1897 | EXPECT_NE(base::Time(), GetModifiedTime(dir_url)); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | TEST_F(ObfuscatedFileUtilTest, TestDirectoryTimestampForCopyAndMove) { |
| 1901 | TestDirectoryTimestampHelper( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1902 | CreateURLFromUTF8("copy overwrite"), true, true); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1903 | TestDirectoryTimestampHelper( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1904 | CreateURLFromUTF8("copy non-overwrite"), true, false); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1905 | TestDirectoryTimestampHelper( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1906 | CreateURLFromUTF8("move overwrite"), false, true); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1907 | TestDirectoryTimestampHelper( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1908 | CreateURLFromUTF8("move non-overwrite"), false, false); |
[email protected] | fad625e2f | 2011-12-08 05:38:03 | [diff] [blame] | 1909 | } |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1910 | |
| 1911 | TEST_F(ObfuscatedFileUtilTest, TestFileEnumeratorTimestamp) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1912 | FileSystemURL dir = CreateURLFromUTF8("foo"); |
| 1913 | FileSystemURL url1 = dir.WithPath(dir.path().AppendASCII("bar")); |
| 1914 | FileSystemURL url2 = dir.WithPath(dir.path().AppendASCII("baz")); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1915 | |
| 1916 | scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1917 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1918 | ofu()->CreateDirectory(context.get(), dir, false, false)); |
| 1919 | |
| 1920 | bool created = false; |
| 1921 | context.reset(NewContext(NULL)); |
| 1922 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1923 | ofu()->EnsureFileExists(context.get(), url1, &created)); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1924 | EXPECT_TRUE(created); |
| 1925 | |
| 1926 | context.reset(NewContext(NULL)); |
| 1927 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1928 | ofu()->CreateDirectory(context.get(), url2, false, false)); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1929 | |
| 1930 | FilePath file_path; |
| 1931 | context.reset(NewContext(NULL)); |
| 1932 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1933 | ofu()->GetLocalFilePath(context.get(), url1, &file_path)); |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1934 | EXPECT_FALSE(file_path.empty()); |
| 1935 | |
| 1936 | context.reset(NewContext(NULL)); |
| 1937 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1938 | ofu()->Touch(context.get(), url1, |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1939 | base::Time::Now() + base::TimeDelta::FromHours(1), |
| 1940 | base::Time())); |
| 1941 | |
| 1942 | context.reset(NewContext(NULL)); |
| 1943 | scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum( |
| 1944 | ofu()->CreateFileEnumerator(context.get(), dir, false)); |
| 1945 | |
| 1946 | int count = 0; |
| 1947 | FilePath file_path_each; |
| 1948 | while (!(file_path_each = file_enum->Next()).empty()) { |
| 1949 | context.reset(NewContext(NULL)); |
| 1950 | base::PlatformFileInfo file_info; |
| 1951 | FilePath file_path; |
| 1952 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1953 | ofu()->GetFileInfo(context.get(), |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1954 | dir.WithPath(file_path_each), |
[email protected] | a393891 | 2012-03-27 14:00:55 | [diff] [blame] | 1955 | &file_info, &file_path)); |
| 1956 | EXPECT_EQ(file_info.is_directory, file_enum->IsDirectory()); |
| 1957 | EXPECT_EQ(file_info.last_modified, file_enum->LastModifiedTime()); |
| 1958 | EXPECT_EQ(file_info.size, file_enum->Size()); |
| 1959 | ++count; |
| 1960 | } |
| 1961 | EXPECT_EQ(2, count); |
| 1962 | } |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1963 | |
| 1964 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnCopyFile) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 1965 | FileSystemURL from_file(CreateURLFromUTF8("fromfile")); |
| 1966 | FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile")); |
| 1967 | FileSystemURL to_file1(CreateURLFromUTF8("tofile1")); |
| 1968 | FileSystemURL to_file2(CreateURLFromUTF8("tofile2")); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 1969 | bool created; |
| 1970 | |
| 1971 | int64 expected_total_file_size = 0; |
| 1972 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1973 | ofu()->EnsureFileExists( |
| 1974 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 1975 | from_file, &created)); |
| 1976 | ASSERT_TRUE(created); |
| 1977 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1978 | |
| 1979 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1980 | ofu()->EnsureFileExists( |
| 1981 | AllowUsageIncrease(PathCost(obstacle_file))->context(), |
| 1982 | obstacle_file, &created)); |
| 1983 | ASSERT_TRUE(created); |
| 1984 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1985 | |
| 1986 | int64 from_file_size = 1020; |
| 1987 | expected_total_file_size += from_file_size; |
| 1988 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1989 | ofu()->Truncate( |
| 1990 | AllowUsageIncrease(from_file_size)->context(), |
| 1991 | from_file, from_file_size)); |
| 1992 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 1993 | |
| 1994 | int64 obstacle_file_size = 1; |
| 1995 | expected_total_file_size += obstacle_file_size; |
| 1996 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1997 | ofu()->Truncate( |
| 1998 | AllowUsageIncrease(obstacle_file_size)->context(), |
| 1999 | obstacle_file, obstacle_file_size)); |
| 2000 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2001 | |
| 2002 | int64 to_file1_size = from_file_size; |
| 2003 | expected_total_file_size += to_file1_size; |
| 2004 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2005 | ofu()->CopyOrMoveFile( |
| 2006 | AllowUsageIncrease( |
| 2007 | PathCost(to_file1) + to_file1_size)->context(), |
| 2008 | from_file, to_file1, true /* copy */)); |
| 2009 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2010 | |
| 2011 | ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 2012 | ofu()->CopyOrMoveFile( |
| 2013 | DisallowUsageIncrease( |
| 2014 | PathCost(to_file2) + from_file_size)->context(), |
| 2015 | from_file, to_file2, true /* copy */)); |
| 2016 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2017 | |
| 2018 | int64 old_obstacle_file_size = obstacle_file_size; |
| 2019 | obstacle_file_size = from_file_size; |
| 2020 | expected_total_file_size += obstacle_file_size - old_obstacle_file_size; |
| 2021 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2022 | ofu()->CopyOrMoveFile( |
| 2023 | AllowUsageIncrease( |
| 2024 | obstacle_file_size - old_obstacle_file_size)->context(), |
| 2025 | from_file, obstacle_file, true /* copy */)); |
| 2026 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2027 | |
| 2028 | int64 old_from_file_size = from_file_size; |
| 2029 | from_file_size = old_from_file_size - 1; |
| 2030 | expected_total_file_size += from_file_size - old_from_file_size; |
| 2031 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2032 | ofu()->Truncate( |
| 2033 | AllowUsageIncrease( |
| 2034 | from_file_size - old_from_file_size)->context(), |
| 2035 | from_file, from_file_size)); |
| 2036 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2037 | |
| 2038 | // quota exceeded |
| 2039 | { |
| 2040 | old_obstacle_file_size = obstacle_file_size; |
| 2041 | obstacle_file_size = from_file_size; |
| 2042 | expected_total_file_size += obstacle_file_size - old_obstacle_file_size; |
| 2043 | scoped_ptr<UsageVerifyHelper> helper = AllowUsageIncrease( |
| 2044 | obstacle_file_size - old_obstacle_file_size); |
| 2045 | helper->context()->set_allowed_bytes_growth( |
| 2046 | helper->context()->allowed_bytes_growth() - 1); |
| 2047 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2048 | ofu()->CopyOrMoveFile( |
| 2049 | helper->context(), |
| 2050 | from_file, obstacle_file, true /* copy */)); |
| 2051 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnMoveFile) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 2056 | FileSystemURL from_file(CreateURLFromUTF8("fromfile")); |
| 2057 | FileSystemURL obstacle_file(CreateURLFromUTF8("obstaclefile")); |
| 2058 | FileSystemURL to_file(CreateURLFromUTF8("tofile")); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 2059 | bool created; |
| 2060 | |
| 2061 | int64 expected_total_file_size = 0; |
| 2062 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2063 | ofu()->EnsureFileExists( |
| 2064 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 2065 | from_file, &created)); |
| 2066 | ASSERT_TRUE(created); |
| 2067 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2068 | |
| 2069 | int64 from_file_size = 1020; |
| 2070 | expected_total_file_size += from_file_size; |
| 2071 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2072 | ofu()->Truncate( |
| 2073 | AllowUsageIncrease(from_file_size)->context(), |
| 2074 | from_file, from_file_size)); |
| 2075 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2076 | |
| 2077 | int64 to_file_size ALLOW_UNUSED = from_file_size; |
| 2078 | from_file_size = 0; |
| 2079 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2080 | ofu()->CopyOrMoveFile( |
| 2081 | AllowUsageIncrease(-PathCost(from_file) + |
| 2082 | PathCost(to_file))->context(), |
| 2083 | from_file, to_file, false /* move */)); |
| 2084 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2085 | |
| 2086 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2087 | ofu()->EnsureFileExists( |
| 2088 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 2089 | from_file, &created)); |
| 2090 | ASSERT_TRUE(created); |
| 2091 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2092 | |
| 2093 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2094 | ofu()->EnsureFileExists( |
| 2095 | AllowUsageIncrease(PathCost(obstacle_file))->context(), |
| 2096 | obstacle_file, &created)); |
| 2097 | ASSERT_TRUE(created); |
| 2098 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2099 | |
| 2100 | from_file_size = 1020; |
| 2101 | expected_total_file_size += from_file_size; |
| 2102 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2103 | ofu()->Truncate( |
| 2104 | AllowUsageIncrease(from_file_size)->context(), |
| 2105 | from_file, from_file_size)); |
| 2106 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2107 | |
| 2108 | int64 obstacle_file_size = 1; |
| 2109 | expected_total_file_size += obstacle_file_size; |
| 2110 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2111 | ofu()->Truncate( |
| 2112 | AllowUsageIncrease(1)->context(), |
| 2113 | obstacle_file, obstacle_file_size)); |
| 2114 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2115 | |
| 2116 | int64 old_obstacle_file_size = obstacle_file_size; |
| 2117 | obstacle_file_size = from_file_size; |
| 2118 | from_file_size = 0; |
| 2119 | expected_total_file_size -= old_obstacle_file_size; |
| 2120 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2121 | ofu()->CopyOrMoveFile( |
| 2122 | AllowUsageIncrease( |
| 2123 | -old_obstacle_file_size - PathCost(from_file))->context(), |
| 2124 | from_file, obstacle_file, |
| 2125 | false /* move */)); |
| 2126 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2127 | |
| 2128 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2129 | ofu()->EnsureFileExists( |
| 2130 | AllowUsageIncrease(PathCost(from_file))->context(), |
| 2131 | from_file, &created)); |
| 2132 | ASSERT_TRUE(created); |
| 2133 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2134 | |
| 2135 | from_file_size = 10; |
| 2136 | expected_total_file_size += from_file_size; |
| 2137 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2138 | ofu()->Truncate( |
| 2139 | AllowUsageIncrease(from_file_size)->context(), |
| 2140 | from_file, from_file_size)); |
| 2141 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2142 | |
| 2143 | // quota exceeded even after operation |
| 2144 | old_obstacle_file_size = obstacle_file_size; |
| 2145 | obstacle_file_size = from_file_size; |
| 2146 | from_file_size = 0; |
| 2147 | expected_total_file_size -= old_obstacle_file_size; |
| 2148 | scoped_ptr<FileSystemOperationContext> context = |
| 2149 | LimitedContext(-old_obstacle_file_size - PathCost(from_file) - 1); |
| 2150 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2151 | ofu()->CopyOrMoveFile( |
| 2152 | context.get(), from_file, obstacle_file, false /* move */)); |
| 2153 | ASSERT_EQ(expected_total_file_size, ComputeTotalFileSize()); |
| 2154 | context.reset(); |
| 2155 | } |
| 2156 | |
| 2157 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 2158 | FileSystemURL dir(CreateURLFromUTF8("dir")); |
| 2159 | FileSystemURL file(CreateURLFromUTF8("file")); |
| 2160 | FileSystemURL dfile1(CreateURLFromUTF8("dir/dfile1")); |
| 2161 | FileSystemURL dfile2(CreateURLFromUTF8("dir/dfile2")); |
[email protected] | 294dd031 | 2012-05-11 07:35:13 | [diff] [blame] | 2162 | bool created; |
| 2163 | |
| 2164 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2165 | ofu()->EnsureFileExists( |
| 2166 | AllowUsageIncrease(PathCost(file))->context(), |
| 2167 | file, &created)); |
| 2168 | ASSERT_TRUE(created); |
| 2169 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2170 | |
| 2171 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2172 | ofu()->CreateDirectory( |
| 2173 | AllowUsageIncrease(PathCost(dir))->context(), |
| 2174 | dir, false, false)); |
| 2175 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2176 | |
| 2177 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2178 | ofu()->EnsureFileExists( |
| 2179 | AllowUsageIncrease(PathCost(dfile1))->context(), |
| 2180 | dfile1, &created)); |
| 2181 | ASSERT_TRUE(created); |
| 2182 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2183 | |
| 2184 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2185 | ofu()->EnsureFileExists( |
| 2186 | AllowUsageIncrease(PathCost(dfile2))->context(), |
| 2187 | dfile2, &created)); |
| 2188 | ASSERT_TRUE(created); |
| 2189 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2190 | |
| 2191 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2192 | ofu()->Truncate( |
| 2193 | AllowUsageIncrease(340)->context(), |
| 2194 | file, 340)); |
| 2195 | ASSERT_EQ(340, ComputeTotalFileSize()); |
| 2196 | |
| 2197 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2198 | ofu()->Truncate( |
| 2199 | AllowUsageIncrease(1020)->context(), |
| 2200 | dfile1, 1020)); |
| 2201 | ASSERT_EQ(1360, ComputeTotalFileSize()); |
| 2202 | |
| 2203 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2204 | ofu()->Truncate( |
| 2205 | AllowUsageIncrease(120)->context(), |
| 2206 | dfile2, 120)); |
| 2207 | ASSERT_EQ(1480, ComputeTotalFileSize()); |
| 2208 | |
| 2209 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2210 | ofu()->DeleteFile( |
| 2211 | AllowUsageIncrease(-PathCost(file) - 340)->context(), |
| 2212 | file)); |
| 2213 | ASSERT_EQ(1140, ComputeTotalFileSize()); |
| 2214 | |
| 2215 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2216 | FileUtilHelper::Delete( |
| 2217 | AllowUsageIncrease(-PathCost(dir) - |
| 2218 | PathCost(dfile1) - |
| 2219 | PathCost(dfile2) - |
| 2220 | 1020 - 120)->context(), |
| 2221 | ofu(), dir, true)); |
| 2222 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2223 | } |
[email protected] | 7d78be1 | 2012-05-24 07:07:26 | [diff] [blame] | 2224 | |
| 2225 | TEST_F(ObfuscatedFileUtilTest, TestQuotaOnOpen) { |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 2226 | FileSystemURL file(CreateURLFromUTF8("file")); |
[email protected] | 7d78be1 | 2012-05-24 07:07:26 | [diff] [blame] | 2227 | base::PlatformFile file_handle; |
| 2228 | bool created; |
| 2229 | |
| 2230 | // Creating a file. |
| 2231 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2232 | ofu()->EnsureFileExists( |
| 2233 | AllowUsageIncrease(PathCost(file))->context(), |
| 2234 | file, &created)); |
| 2235 | ASSERT_TRUE(created); |
| 2236 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2237 | |
| 2238 | // Opening it, which shouldn't change the usage. |
| 2239 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2240 | ofu()->CreateOrOpen( |
| 2241 | AllowUsageIncrease(0)->context(), file, |
| 2242 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 2243 | &file_handle, &created)); |
| 2244 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2245 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 2246 | |
| 2247 | const int length = 33; |
| 2248 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2249 | ofu()->Truncate( |
| 2250 | AllowUsageIncrease(length)->context(), file, length)); |
| 2251 | ASSERT_EQ(length, ComputeTotalFileSize()); |
| 2252 | |
| 2253 | // Opening it with CREATE_ALWAYS flag, which should truncate the file size. |
| 2254 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2255 | ofu()->CreateOrOpen( |
| 2256 | AllowUsageIncrease(-length)->context(), file, |
| 2257 | base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 2258 | &file_handle, &created)); |
| 2259 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2260 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 2261 | |
| 2262 | // Extending the file again. |
| 2263 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2264 | ofu()->Truncate( |
| 2265 | AllowUsageIncrease(length)->context(), file, length)); |
| 2266 | ASSERT_EQ(length, ComputeTotalFileSize()); |
| 2267 | |
| 2268 | // Opening it with TRUNCATED flag, which should truncate the file size. |
| 2269 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 2270 | ofu()->CreateOrOpen( |
| 2271 | AllowUsageIncrease(-length)->context(), file, |
| 2272 | base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE, |
| 2273 | &file_handle, &created)); |
| 2274 | ASSERT_EQ(0, ComputeTotalFileSize()); |
| 2275 | EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 2276 | } |
[email protected] | c4e6f9c | 2012-09-09 17:42:10 | [diff] [blame^] | 2277 | |
| 2278 | } // namespace fileapi |