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