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