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