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