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