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