| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 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 <utility> | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/scoped_vector.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/threading/thread_restrictions.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/test/mock_special_storage_policy.h" | |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 17 #include "content/public/test/test_file_system_context.h" | |
| 18 #include "content/public/test/test_file_system_options.h" | |
| 19 #include "storage/browser/fileapi/external_mount_points.h" | |
| 20 #include "storage/browser/fileapi/file_system_backend.h" | |
| 21 #include "storage/browser/fileapi/file_system_context.h" | |
| 22 #include "storage/browser/fileapi/file_system_operation_runner.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 using storage::FileSystemContext; | |
| 26 using storage::FileSystemOperationRunner; | |
| 27 using storage::FileSystemType; | |
| 28 using storage::FileSystemURL; | |
| 29 | |
| 30 namespace content { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 void GetStatus(bool* done, | |
| 35 base::File::Error *status_out, | |
| 36 base::File::Error status) { | |
| 37 ASSERT_FALSE(*done); | |
| 38 *done = true; | |
| 39 *status_out = status; | |
| 40 } | |
| 41 | |
| 42 void GetCancelStatus(bool* operation_done, | |
| 43 bool* cancel_done, | |
| 44 base::File::Error *status_out, | |
| 45 base::File::Error status) { | |
| 46 // Cancel callback must be always called after the operation's callback. | |
| 47 ASSERT_TRUE(*operation_done); | |
| 48 ASSERT_FALSE(*cancel_done); | |
| 49 *cancel_done = true; | |
| 50 *status_out = status; | |
| 51 } | |
| 52 | |
| 53 void DidOpenFile(base::File file, const base::Closure& on_close_callback) {} | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 class FileSystemOperationRunnerTest : public testing::Test { | |
| 58 protected: | |
| 59 FileSystemOperationRunnerTest() {} | |
| 60 ~FileSystemOperationRunnerTest() override {} | |
| 61 | |
| 62 void SetUp() override { | |
| 63 ASSERT_TRUE(base_.CreateUniqueTempDir()); | |
| 64 base::FilePath base_dir = base_.GetPath(); | |
| 65 file_system_context_ = CreateFileSystemContextForTesting(nullptr, base_dir); | |
| 66 } | |
| 67 | |
| 68 void TearDown() override { | |
| 69 file_system_context_ = nullptr; | |
| 70 base::RunLoop().RunUntilIdle(); | |
| 71 } | |
| 72 | |
| 73 FileSystemURL URL(const std::string& path) { | |
| 74 return file_system_context_->CreateCrackedFileSystemURL( | |
| 75 GURL("http://example.com"), | |
| 76 storage::kFileSystemTypeTemporary, | |
| 77 base::FilePath::FromUTF8Unsafe(path)); | |
| 78 } | |
| 79 | |
| 80 FileSystemOperationRunner* operation_runner() { | |
| 81 return file_system_context_->operation_runner(); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 base::ScopedTempDir base_; | |
| 86 base::MessageLoop message_loop_; | |
| 87 scoped_refptr<FileSystemContext> file_system_context_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunnerTest); | |
| 90 }; | |
| 91 | |
| 92 TEST_F(FileSystemOperationRunnerTest, NotFoundError) { | |
| 93 bool done = false; | |
| 94 base::File::Error status = base::File::FILE_ERROR_FAILED; | |
| 95 | |
| 96 // Regular NOT_FOUND error, which is called asynchronously. | |
| 97 operation_runner()->Truncate(URL("foo"), 0, | |
| 98 base::Bind(&GetStatus, &done, &status)); | |
| 99 ASSERT_FALSE(done); | |
| 100 base::RunLoop().RunUntilIdle(); | |
| 101 ASSERT_TRUE(done); | |
| 102 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status); | |
| 103 } | |
| 104 | |
| 105 TEST_F(FileSystemOperationRunnerTest, InvalidURLError) { | |
| 106 bool done = false; | |
| 107 base::File::Error status = base::File::FILE_ERROR_FAILED; | |
| 108 | |
| 109 // Invalid URL error, which calls DidFinish synchronously. | |
| 110 operation_runner()->Truncate(FileSystemURL(), 0, | |
| 111 base::Bind(&GetStatus, &done, &status)); | |
| 112 // The error call back shouldn't be fired synchronously. | |
| 113 ASSERT_FALSE(done); | |
| 114 | |
| 115 base::RunLoop().RunUntilIdle(); | |
| 116 ASSERT_TRUE(done); | |
| 117 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status); | |
| 118 } | |
| 119 | |
| 120 TEST_F(FileSystemOperationRunnerTest, NotFoundErrorAndCancel) { | |
| 121 bool done = false; | |
| 122 bool cancel_done = false; | |
| 123 base::File::Error status = base::File::FILE_ERROR_FAILED; | |
| 124 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED; | |
| 125 | |
| 126 // Call Truncate with non-existent URL, and try to cancel it immediately | |
| 127 // after that (before its callback is fired). | |
| 128 FileSystemOperationRunner::OperationID id = | |
| 129 operation_runner()->Truncate(URL("foo"), 0, | |
| 130 base::Bind(&GetStatus, &done, &status)); | |
| 131 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus, | |
| 132 &done, &cancel_done, | |
| 133 &cancel_status)); | |
| 134 | |
| 135 ASSERT_FALSE(done); | |
| 136 ASSERT_FALSE(cancel_done); | |
| 137 base::RunLoop().RunUntilIdle(); | |
| 138 | |
| 139 ASSERT_TRUE(done); | |
| 140 ASSERT_TRUE(cancel_done); | |
| 141 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status); | |
| 142 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status); | |
| 143 } | |
| 144 | |
| 145 TEST_F(FileSystemOperationRunnerTest, InvalidURLErrorAndCancel) { | |
| 146 bool done = false; | |
| 147 bool cancel_done = false; | |
| 148 base::File::Error status = base::File::FILE_ERROR_FAILED; | |
| 149 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED; | |
| 150 | |
| 151 // Call Truncate with invalid URL, and try to cancel it immediately | |
| 152 // after that (before its callback is fired). | |
| 153 FileSystemOperationRunner::OperationID id = | |
| 154 operation_runner()->Truncate(FileSystemURL(), 0, | |
| 155 base::Bind(&GetStatus, &done, &status)); | |
| 156 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus, | |
| 157 &done, &cancel_done, | |
| 158 &cancel_status)); | |
| 159 | |
| 160 ASSERT_FALSE(done); | |
| 161 ASSERT_FALSE(cancel_done); | |
| 162 base::RunLoop().RunUntilIdle(); | |
| 163 | |
| 164 ASSERT_TRUE(done); | |
| 165 ASSERT_TRUE(cancel_done); | |
| 166 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status); | |
| 167 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status); | |
| 168 } | |
| 169 | |
| 170 TEST_F(FileSystemOperationRunnerTest, CancelWithInvalidId) { | |
| 171 const FileSystemOperationRunner::OperationID kInvalidId = -1; | |
| 172 bool done = true; // The operation is not running. | |
| 173 bool cancel_done = false; | |
| 174 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED; | |
| 175 operation_runner()->Cancel(kInvalidId, base::Bind(&GetCancelStatus, | |
| 176 &done, &cancel_done, | |
| 177 &cancel_status)); | |
| 178 | |
| 179 ASSERT_TRUE(cancel_done); | |
| 180 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status); | |
| 181 } | |
| 182 | |
| 183 class MultiThreadFileSystemOperationRunnerTest : public testing::Test { | |
| 184 public: | |
| 185 MultiThreadFileSystemOperationRunnerTest() | |
| 186 : thread_bundle_( | |
| 187 content::TestBrowserThreadBundle::REAL_FILE_THREAD | | |
| 188 content::TestBrowserThreadBundle::IO_MAINLOOP) {} | |
| 189 | |
| 190 void SetUp() override { | |
| 191 ASSERT_TRUE(base_.CreateUniqueTempDir()); | |
| 192 | |
| 193 base::FilePath base_dir = base_.GetPath(); | |
| 194 ScopedVector<storage::FileSystemBackend> additional_providers; | |
| 195 file_system_context_ = new FileSystemContext( | |
| 196 base::ThreadTaskRunnerHandle::Get().get(), | |
| 197 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE).get(), | |
| 198 storage::ExternalMountPoints::CreateRefCounted().get(), | |
| 199 make_scoped_refptr(new MockSpecialStoragePolicy()).get(), nullptr, | |
| 200 std::move(additional_providers), | |
| 201 std::vector<storage::URLRequestAutoMountHandler>(), base_dir, | |
| 202 CreateAllowFileAccessOptions()); | |
| 203 | |
| 204 // Disallow IO on the main loop. | |
| 205 base::ThreadRestrictions::SetIOAllowed(false); | |
| 206 } | |
| 207 | |
| 208 void TearDown() override { | |
| 209 base::ThreadRestrictions::SetIOAllowed(true); | |
| 210 file_system_context_ = nullptr; | |
| 211 } | |
| 212 | |
| 213 FileSystemURL URL(const std::string& path) { | |
| 214 return file_system_context_->CreateCrackedFileSystemURL( | |
| 215 GURL("http://example.com"), | |
| 216 storage::kFileSystemTypeTemporary, | |
| 217 base::FilePath::FromUTF8Unsafe(path)); | |
| 218 } | |
| 219 | |
| 220 FileSystemOperationRunner* operation_runner() { | |
| 221 return file_system_context_->operation_runner(); | |
| 222 } | |
| 223 | |
| 224 private: | |
| 225 base::ScopedTempDir base_; | |
| 226 content::TestBrowserThreadBundle thread_bundle_; | |
| 227 scoped_refptr<FileSystemContext> file_system_context_; | |
| 228 | |
| 229 DISALLOW_COPY_AND_ASSIGN(MultiThreadFileSystemOperationRunnerTest); | |
| 230 }; | |
| 231 | |
| 232 TEST_F(MultiThreadFileSystemOperationRunnerTest, OpenAndShutdown) { | |
| 233 // Call OpenFile and immediately shutdown the runner. | |
| 234 operation_runner()->OpenFile( | |
| 235 URL("foo"), | |
| 236 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE, | |
| 237 base::Bind(&DidOpenFile)); | |
| 238 operation_runner()->Shutdown(); | |
| 239 | |
| 240 // Wait until the task posted on FILE thread is done. | |
| 241 base::RunLoop run_loop; | |
| 242 BrowserThread::PostTaskAndReply( | |
| 243 BrowserThread::FILE, FROM_HERE, | |
| 244 base::Bind(&base::DoNothing), | |
| 245 run_loop.QuitClosure()); | |
| 246 run_loop.Run(); | |
| 247 // This should finish without thread assertion failure on debug build. | |
| 248 } | |
| 249 | |
| 250 } // namespace content | |
| OLD | NEW |