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