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