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