| 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 <memory> | |
| 6 | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "content/public/test/async_file_test_helper.h" | |
| 12 #include "content/public/test/test_file_system_context.h" | |
| 13 #include "content/public/test/test_file_system_options.h" | |
| 14 #include "storage/browser/fileapi/file_system_context.h" | |
| 15 #include "storage/browser/fileapi/isolated_context.h" | |
| 16 #include "storage/browser/fileapi/obfuscated_file_util.h" | |
| 17 #include "storage/browser/fileapi/plugin_private_file_system_backend.h" | |
| 18 #include "storage/common/fileapi/file_system_util.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 using content::AsyncFileTestHelper; | |
| 22 using storage::FileSystemContext; | |
| 23 using storage::FileSystemURL; | |
| 24 using storage::IsolatedContext; | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 const GURL kOrigin1("http://www.example.com"); | |
| 31 const GURL kOrigin2("https://www.example.com"); | |
| 32 const std::string kPlugin1("plugin1"); | |
| 33 const std::string kPlugin2("plugin2"); | |
| 34 const storage::FileSystemType kType = storage::kFileSystemTypePluginPrivate; | |
| 35 const std::string kRootName = "pluginprivate"; | |
| 36 | |
| 37 void DidOpenFileSystem(base::File::Error* error_out, | |
| 38 base::File::Error error) { | |
| 39 *error_out = error; | |
| 40 } | |
| 41 | |
| 42 std::string RegisterFileSystem() { | |
| 43 return IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath( | |
| 44 kType, kRootName, base::FilePath()); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 class PluginPrivateFileSystemBackendTest : public testing::Test { | |
| 50 protected: | |
| 51 void SetUp() override { | |
| 52 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 53 context_ = CreateFileSystemContextForTesting(NULL /* quota_manager_proxy */, | |
| 54 data_dir_.GetPath()); | |
| 55 } | |
| 56 | |
| 57 FileSystemURL CreateURL(const GURL& root_url, const std::string& relative) { | |
| 58 FileSystemURL root = context_->CrackURL(root_url); | |
| 59 return context_->CreateCrackedFileSystemURL( | |
| 60 root.origin(), | |
| 61 root.mount_type(), | |
| 62 root.virtual_path().AppendASCII(relative)); | |
| 63 } | |
| 64 | |
| 65 storage::PluginPrivateFileSystemBackend* backend() const { | |
| 66 return context_->plugin_private_backend(); | |
| 67 } | |
| 68 | |
| 69 const base::FilePath& base_path() const { return backend()->base_path(); } | |
| 70 | |
| 71 base::ScopedTempDir data_dir_; | |
| 72 base::MessageLoop message_loop_; | |
| 73 scoped_refptr<FileSystemContext> context_; | |
| 74 }; | |
| 75 | |
| 76 // TODO(kinuko,nhiroki): There are a lot of duplicate code in these tests. Write | |
| 77 // helper functions to simplify the tests. | |
| 78 | |
| 79 TEST_F(PluginPrivateFileSystemBackendTest, OpenFileSystemBasic) { | |
| 80 const std::string filesystem_id1 = RegisterFileSystem(); | |
| 81 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 82 backend()->OpenPrivateFileSystem( | |
| 83 kOrigin1, | |
| 84 kType, | |
| 85 filesystem_id1, | |
| 86 kPlugin1, | |
| 87 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 88 base::Bind(&DidOpenFileSystem, &error)); | |
| 89 base::RunLoop().RunUntilIdle(); | |
| 90 ASSERT_EQ(base::File::FILE_OK, error); | |
| 91 | |
| 92 // Run this again with FAIL_IF_NONEXISTENT to see if it succeeds. | |
| 93 const std::string filesystem_id2 = RegisterFileSystem(); | |
| 94 error = base::File::FILE_ERROR_FAILED; | |
| 95 backend()->OpenPrivateFileSystem( | |
| 96 kOrigin1, | |
| 97 kType, | |
| 98 filesystem_id2, | |
| 99 kPlugin1, | |
| 100 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
| 101 base::Bind(&DidOpenFileSystem, &error)); | |
| 102 base::RunLoop().RunUntilIdle(); | |
| 103 ASSERT_EQ(base::File::FILE_OK, error); | |
| 104 | |
| 105 const GURL root_url(storage::GetIsolatedFileSystemRootURIString( | |
| 106 kOrigin1, filesystem_id1, kRootName)); | |
| 107 FileSystemURL file = CreateURL(root_url, "foo"); | |
| 108 base::FilePath platform_path; | |
| 109 EXPECT_EQ(base::File::FILE_OK, | |
| 110 AsyncFileTestHelper::CreateFile(context_.get(), file)); | |
| 111 EXPECT_EQ(base::File::FILE_OK, | |
| 112 AsyncFileTestHelper::GetPlatformPath(context_.get(), file, | |
| 113 &platform_path)); | |
| 114 EXPECT_TRUE(base_path().AppendASCII("000").AppendASCII(kPlugin1).IsParent( | |
| 115 platform_path)); | |
| 116 } | |
| 117 | |
| 118 TEST_F(PluginPrivateFileSystemBackendTest, PluginIsolation) { | |
| 119 // Open filesystem for kPlugin1 and kPlugin2. | |
| 120 const std::string filesystem_id1 = RegisterFileSystem(); | |
| 121 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 122 backend()->OpenPrivateFileSystem( | |
| 123 kOrigin1, | |
| 124 kType, | |
| 125 filesystem_id1, | |
| 126 kPlugin1, | |
| 127 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 128 base::Bind(&DidOpenFileSystem, &error)); | |
| 129 base::RunLoop().RunUntilIdle(); | |
| 130 ASSERT_EQ(base::File::FILE_OK, error); | |
| 131 | |
| 132 const std::string filesystem_id2 = RegisterFileSystem(); | |
| 133 error = base::File::FILE_ERROR_FAILED; | |
| 134 backend()->OpenPrivateFileSystem( | |
| 135 kOrigin1, | |
| 136 kType, | |
| 137 filesystem_id2, | |
| 138 kPlugin2, | |
| 139 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 140 base::Bind(&DidOpenFileSystem, &error)); | |
| 141 base::RunLoop().RunUntilIdle(); | |
| 142 ASSERT_EQ(base::File::FILE_OK, error); | |
| 143 | |
| 144 // Create 'foo' in kPlugin1. | |
| 145 const GURL root_url1(storage::GetIsolatedFileSystemRootURIString( | |
| 146 kOrigin1, filesystem_id1, kRootName)); | |
| 147 FileSystemURL file1 = CreateURL(root_url1, "foo"); | |
| 148 EXPECT_EQ(base::File::FILE_OK, | |
| 149 AsyncFileTestHelper::CreateFile(context_.get(), file1)); | |
| 150 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 151 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize)); | |
| 152 | |
| 153 // See the same path is not available in kPlugin2. | |
| 154 const GURL root_url2(storage::GetIsolatedFileSystemRootURIString( | |
| 155 kOrigin1, filesystem_id2, kRootName)); | |
| 156 FileSystemURL file2 = CreateURL(root_url2, "foo"); | |
| 157 EXPECT_FALSE(AsyncFileTestHelper::FileExists( | |
| 158 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize)); | |
| 159 } | |
| 160 | |
| 161 TEST_F(PluginPrivateFileSystemBackendTest, OriginIsolation) { | |
| 162 // Open filesystem for kOrigin1 and kOrigin2. | |
| 163 const std::string filesystem_id1 = RegisterFileSystem(); | |
| 164 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 165 backend()->OpenPrivateFileSystem( | |
| 166 kOrigin1, | |
| 167 kType, | |
| 168 filesystem_id1, | |
| 169 kPlugin1, | |
| 170 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 171 base::Bind(&DidOpenFileSystem, &error)); | |
| 172 base::RunLoop().RunUntilIdle(); | |
| 173 ASSERT_EQ(base::File::FILE_OK, error); | |
| 174 | |
| 175 const std::string filesystem_id2 = RegisterFileSystem(); | |
| 176 error = base::File::FILE_ERROR_FAILED; | |
| 177 backend()->OpenPrivateFileSystem( | |
| 178 kOrigin2, | |
| 179 kType, | |
| 180 filesystem_id2, | |
| 181 kPlugin1, | |
| 182 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 183 base::Bind(&DidOpenFileSystem, &error)); | |
| 184 base::RunLoop().RunUntilIdle(); | |
| 185 ASSERT_EQ(base::File::FILE_OK, error); | |
| 186 | |
| 187 // Create 'foo' in kOrigin1. | |
| 188 const GURL root_url1(storage::GetIsolatedFileSystemRootURIString( | |
| 189 kOrigin1, filesystem_id1, kRootName)); | |
| 190 FileSystemURL file1 = CreateURL(root_url1, "foo"); | |
| 191 EXPECT_EQ(base::File::FILE_OK, | |
| 192 AsyncFileTestHelper::CreateFile(context_.get(), file1)); | |
| 193 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 194 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize)); | |
| 195 | |
| 196 // See the same path is not available in kOrigin2. | |
| 197 const GURL root_url2(storage::GetIsolatedFileSystemRootURIString( | |
| 198 kOrigin2, filesystem_id2, kRootName)); | |
| 199 FileSystemURL file2 = CreateURL(root_url2, "foo"); | |
| 200 EXPECT_FALSE(AsyncFileTestHelper::FileExists( | |
| 201 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize)); | |
| 202 } | |
| 203 | |
| 204 TEST_F(PluginPrivateFileSystemBackendTest, DeleteOriginDirectory) { | |
| 205 // Open filesystem for kOrigin1 and kOrigin2. | |
| 206 const std::string filesystem_id1 = RegisterFileSystem(); | |
| 207 base::File::Error error = base::File::FILE_ERROR_FAILED; | |
| 208 backend()->OpenPrivateFileSystem( | |
| 209 kOrigin1, | |
| 210 kType, | |
| 211 filesystem_id1, | |
| 212 kPlugin1, | |
| 213 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 214 base::Bind(&DidOpenFileSystem, &error)); | |
| 215 base::RunLoop().RunUntilIdle(); | |
| 216 ASSERT_EQ(base::File::FILE_OK, error); | |
| 217 | |
| 218 const std::string filesystem_id2 = RegisterFileSystem(); | |
| 219 error = base::File::FILE_ERROR_FAILED; | |
| 220 backend()->OpenPrivateFileSystem( | |
| 221 kOrigin2, | |
| 222 kType, | |
| 223 filesystem_id2, | |
| 224 kPlugin1, | |
| 225 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 226 base::Bind(&DidOpenFileSystem, &error)); | |
| 227 base::RunLoop().RunUntilIdle(); | |
| 228 ASSERT_EQ(base::File::FILE_OK, error); | |
| 229 | |
| 230 // Create 'foo' in kOrigin1. | |
| 231 const GURL root_url1(storage::GetIsolatedFileSystemRootURIString( | |
| 232 kOrigin1, filesystem_id1, kRootName)); | |
| 233 FileSystemURL file1 = CreateURL(root_url1, "foo"); | |
| 234 EXPECT_EQ(base::File::FILE_OK, | |
| 235 AsyncFileTestHelper::CreateFile(context_.get(), file1)); | |
| 236 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 237 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize)); | |
| 238 | |
| 239 // Create 'foo' in kOrigin2. | |
| 240 const GURL root_url2(storage::GetIsolatedFileSystemRootURIString( | |
| 241 kOrigin2, filesystem_id2, kRootName)); | |
| 242 FileSystemURL file2 = CreateURL(root_url2, "foo"); | |
| 243 EXPECT_EQ(base::File::FILE_OK, | |
| 244 AsyncFileTestHelper::CreateFile(context_.get(), file2)); | |
| 245 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 246 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize)); | |
| 247 | |
| 248 // Delete data for kOrigin1. | |
| 249 error = backend()->DeleteOriginDataOnFileTaskRunner( | |
| 250 context_.get(), NULL, kOrigin1, kType); | |
| 251 EXPECT_EQ(base::File::FILE_OK, error); | |
| 252 | |
| 253 // Confirm 'foo' in kOrigin1 is deleted. | |
| 254 EXPECT_FALSE(AsyncFileTestHelper::FileExists( | |
| 255 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize)); | |
| 256 | |
| 257 // Confirm 'foo' in kOrigin2 is NOT deleted. | |
| 258 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 259 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize)); | |
| 260 | |
| 261 // Re-open filesystem for kOrigin1. | |
| 262 const std::string filesystem_id3 = RegisterFileSystem(); | |
| 263 error = base::File::FILE_ERROR_FAILED; | |
| 264 backend()->OpenPrivateFileSystem( | |
| 265 kOrigin1, | |
| 266 kType, | |
| 267 filesystem_id3, | |
| 268 kPlugin1, | |
| 269 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 270 base::Bind(&DidOpenFileSystem, &error)); | |
| 271 base::RunLoop().RunUntilIdle(); | |
| 272 ASSERT_EQ(base::File::FILE_OK, error); | |
| 273 | |
| 274 // Re-create 'foo' in kOrigin1. | |
| 275 const GURL root_url3(storage::GetIsolatedFileSystemRootURIString( | |
| 276 kOrigin1, filesystem_id3, kRootName)); | |
| 277 FileSystemURL file3 = CreateURL(root_url3, "foo"); | |
| 278 EXPECT_EQ(base::File::FILE_OK, | |
| 279 AsyncFileTestHelper::CreateFile(context_.get(), file3)); | |
| 280 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 281 context_.get(), file3, AsyncFileTestHelper::kDontCheckSize)); | |
| 282 | |
| 283 // Confirm 'foo' in kOrigin1 is re-created. | |
| 284 EXPECT_TRUE(AsyncFileTestHelper::FileExists( | |
| 285 context_.get(), file3, AsyncFileTestHelper::kDontCheckSize)); | |
| 286 } | |
| 287 | |
| 288 } // namespace content | |
| OLD | NEW |