| 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 "storage/browser/fileapi/sandbox_file_system_backend.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <set> | |
| 11 | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/files/scoped_temp_dir.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/test/scoped_task_environment.h" | |
| 17 #include "base/threading/thread_task_runner_handle.h" | |
| 18 #include "storage/browser/fileapi/file_system_backend.h" | |
| 19 #include "storage/browser/fileapi/file_system_url.h" | |
| 20 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h" | |
| 21 #include "storage/browser/test/test_file_system_options.h" | |
| 22 #include "storage/common/fileapi/file_system_util.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 using storage::FileSystemURL; | |
| 27 using storage::SandboxFileSystemBackend; | |
| 28 using storage::SandboxFileSystemBackendDelegate; | |
| 29 | |
| 30 // PS stands for path separator. | |
| 31 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 32 #define PS "\\" | |
| 33 #else | |
| 34 #define PS "/" | |
| 35 #endif | |
| 36 | |
| 37 namespace content { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 const struct RootPathTest { | |
| 42 storage::FileSystemType type; | |
| 43 const char* origin_url; | |
| 44 const char* expected_path; | |
| 45 } kRootPathTestCases[] = { | |
| 46 {storage::kFileSystemTypeTemporary, "http://foo:1/", "000" PS "t"}, | |
| 47 {storage::kFileSystemTypePersistent, "http://foo:1/", "000" PS "p"}, | |
| 48 {storage::kFileSystemTypeTemporary, "http://bar.com/", "001" PS "t"}, | |
| 49 {storage::kFileSystemTypePersistent, "http://bar.com/", "001" PS "p"}, | |
| 50 {storage::kFileSystemTypeTemporary, "https://foo:2/", "002" PS "t"}, | |
| 51 {storage::kFileSystemTypePersistent, "https://foo:2/", "002" PS "p"}, | |
| 52 {storage::kFileSystemTypeTemporary, "https://bar.com/", "003" PS "t"}, | |
| 53 {storage::kFileSystemTypePersistent, "https://bar.com/", "003" PS "p"}, | |
| 54 }; | |
| 55 | |
| 56 const struct RootPathFileURITest { | |
| 57 storage::FileSystemType type; | |
| 58 const char* origin_url; | |
| 59 const char* expected_path; | |
| 60 } kRootPathFileURITestCases[] = { | |
| 61 {storage::kFileSystemTypeTemporary, "file:///", "000" PS "t"}, | |
| 62 {storage::kFileSystemTypePersistent, "file:///", "000" PS "p"}}; | |
| 63 | |
| 64 void DidOpenFileSystem(base::File::Error* error_out, | |
| 65 const GURL& origin_url, | |
| 66 const std::string& name, | |
| 67 base::File::Error error) { | |
| 68 *error_out = error; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 class SandboxFileSystemBackendTest : public testing::Test { | |
| 74 protected: | |
| 75 void SetUp() override { | |
| 76 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 77 SetUpNewDelegate(CreateAllowFileAccessOptions()); | |
| 78 } | |
| 79 | |
| 80 void SetUpNewDelegate(const storage::FileSystemOptions& options) { | |
| 81 delegate_.reset(new SandboxFileSystemBackendDelegate( | |
| 82 NULL /* quota_manager_proxy */, | |
| 83 base::ThreadTaskRunnerHandle::Get().get(), data_dir_.GetPath(), | |
| 84 NULL /* special_storage_policy */, options)); | |
| 85 } | |
| 86 | |
| 87 void SetUpNewBackend(const storage::FileSystemOptions& options) { | |
| 88 SetUpNewDelegate(options); | |
| 89 backend_.reset(new SandboxFileSystemBackend(delegate_.get())); | |
| 90 } | |
| 91 | |
| 92 storage::SandboxFileSystemBackendDelegate::OriginEnumerator* | |
| 93 CreateOriginEnumerator() const { | |
| 94 return backend_->CreateOriginEnumerator(); | |
| 95 } | |
| 96 | |
| 97 void CreateOriginTypeDirectory(const GURL& origin, | |
| 98 storage::FileSystemType type) { | |
| 99 base::FilePath target = delegate_-> | |
| 100 GetBaseDirectoryForOriginAndType(origin, type, true); | |
| 101 ASSERT_TRUE(!target.empty()); | |
| 102 ASSERT_TRUE(base::DirectoryExists(target)); | |
| 103 } | |
| 104 | |
| 105 bool GetRootPath(const GURL& origin_url, | |
| 106 storage::FileSystemType type, | |
| 107 storage::OpenFileSystemMode mode, | |
| 108 base::FilePath* root_path) { | |
| 109 base::File::Error error = base::File::FILE_OK; | |
| 110 backend_->ResolveURL( | |
| 111 FileSystemURL::CreateForTest(origin_url, type, base::FilePath()), | |
| 112 mode, | |
| 113 base::Bind(&DidOpenFileSystem, &error)); | |
| 114 base::RunLoop().RunUntilIdle(); | |
| 115 if (error != base::File::FILE_OK) | |
| 116 return false; | |
| 117 base::FilePath returned_root_path = | |
| 118 delegate_->GetBaseDirectoryForOriginAndType( | |
| 119 origin_url, type, false /* create */); | |
| 120 if (root_path) | |
| 121 *root_path = returned_root_path; | |
| 122 return !returned_root_path.empty(); | |
| 123 } | |
| 124 | |
| 125 base::FilePath file_system_path() const { | |
| 126 return data_dir_.GetPath().Append( | |
| 127 SandboxFileSystemBackendDelegate::kFileSystemDirectory); | |
| 128 } | |
| 129 | |
| 130 base::ScopedTempDir data_dir_; | |
| 131 base::test::ScopedTaskEnvironment scoped_task_environment_; | |
| 132 std::unique_ptr<storage::SandboxFileSystemBackendDelegate> delegate_; | |
| 133 std::unique_ptr<storage::SandboxFileSystemBackend> backend_; | |
| 134 }; | |
| 135 | |
| 136 TEST_F(SandboxFileSystemBackendTest, Empty) { | |
| 137 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
| 138 std::unique_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> | |
| 139 enumerator(CreateOriginEnumerator()); | |
| 140 ASSERT_TRUE(enumerator->Next().is_empty()); | |
| 141 } | |
| 142 | |
| 143 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) { | |
| 144 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
| 145 const char* temporary_origins[] = { | |
| 146 "http://www.bar.com/", | |
| 147 "http://www.foo.com/", | |
| 148 "http://www.foo.com:1/", | |
| 149 "http://www.example.com:8080/", | |
| 150 "http://www.google.com:80/", | |
| 151 }; | |
| 152 const char* persistent_origins[] = { | |
| 153 "http://www.bar.com/", | |
| 154 "http://www.foo.com:8080/", | |
| 155 "http://www.foo.com:80/", | |
| 156 }; | |
| 157 size_t temporary_size = arraysize(temporary_origins); | |
| 158 size_t persistent_size = arraysize(persistent_origins); | |
| 159 std::set<GURL> temporary_set, persistent_set; | |
| 160 for (size_t i = 0; i < temporary_size; ++i) { | |
| 161 CreateOriginTypeDirectory(GURL(temporary_origins[i]), | |
| 162 storage::kFileSystemTypeTemporary); | |
| 163 temporary_set.insert(GURL(temporary_origins[i])); | |
| 164 } | |
| 165 for (size_t i = 0; i < persistent_size; ++i) { | |
| 166 CreateOriginTypeDirectory(GURL(persistent_origins[i]), | |
| 167 storage::kFileSystemTypePersistent); | |
| 168 persistent_set.insert(GURL(persistent_origins[i])); | |
| 169 } | |
| 170 | |
| 171 std::unique_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> | |
| 172 enumerator(CreateOriginEnumerator()); | |
| 173 size_t temporary_actual_size = 0; | |
| 174 size_t persistent_actual_size = 0; | |
| 175 GURL current; | |
| 176 while (!(current = enumerator->Next()).is_empty()) { | |
| 177 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec()); | |
| 178 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { | |
| 179 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); | |
| 180 ++temporary_actual_size; | |
| 181 } | |
| 182 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { | |
| 183 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); | |
| 184 ++persistent_actual_size; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 EXPECT_EQ(temporary_size, temporary_actual_size); | |
| 189 EXPECT_EQ(persistent_size, persistent_actual_size); | |
| 190 } | |
| 191 | |
| 192 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) { | |
| 193 std::vector<base::FilePath> returned_root_path(arraysize(kRootPathTestCases)); | |
| 194 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
| 195 | |
| 196 // Create a new root directory. | |
| 197 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) { | |
| 198 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " | |
| 199 << kRootPathTestCases[i].expected_path); | |
| 200 | |
| 201 base::FilePath root_path; | |
| 202 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 203 kRootPathTestCases[i].type, | |
| 204 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 205 &root_path)); | |
| 206 | |
| 207 base::FilePath expected = file_system_path().AppendASCII( | |
| 208 kRootPathTestCases[i].expected_path); | |
| 209 EXPECT_EQ(expected.value(), root_path.value()); | |
| 210 EXPECT_TRUE(base::DirectoryExists(root_path)); | |
| 211 ASSERT_TRUE(returned_root_path.size() > i); | |
| 212 returned_root_path[i] = root_path; | |
| 213 } | |
| 214 | |
| 215 // Get the root directory with create=false and see if we get the | |
| 216 // same directory. | |
| 217 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) { | |
| 218 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | |
| 219 << kRootPathTestCases[i].expected_path); | |
| 220 | |
| 221 base::FilePath root_path; | |
| 222 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 223 kRootPathTestCases[i].type, | |
| 224 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
| 225 &root_path)); | |
| 226 ASSERT_TRUE(returned_root_path.size() > i); | |
| 227 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 TEST_F(SandboxFileSystemBackendTest, | |
| 232 GetRootPathCreateAndExamineWithNewBackend) { | |
| 233 std::vector<base::FilePath> returned_root_path(arraysize(kRootPathTestCases)); | |
| 234 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
| 235 | |
| 236 GURL origin_url("http://foo.com:1/"); | |
| 237 | |
| 238 base::FilePath root_path1; | |
| 239 EXPECT_TRUE(GetRootPath(origin_url, | |
| 240 storage::kFileSystemTypeTemporary, | |
| 241 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 242 &root_path1)); | |
| 243 | |
| 244 SetUpNewBackend(CreateDisallowFileAccessOptions()); | |
| 245 base::FilePath root_path2; | |
| 246 EXPECT_TRUE(GetRootPath(origin_url, | |
| 247 storage::kFileSystemTypeTemporary, | |
| 248 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
| 249 &root_path2)); | |
| 250 | |
| 251 EXPECT_EQ(root_path1.value(), root_path2.value()); | |
| 252 } | |
| 253 | |
| 254 TEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) { | |
| 255 SetUpNewBackend(CreateDisallowFileAccessOptions()); | |
| 256 | |
| 257 // Try to get a root directory without creating. | |
| 258 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) { | |
| 259 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " | |
| 260 << kRootPathTestCases[i].expected_path); | |
| 261 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 262 kRootPathTestCases[i].type, | |
| 263 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
| 264 NULL)); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 TEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) { | |
| 269 SetUpNewBackend(CreateIncognitoFileSystemOptions()); | |
| 270 | |
| 271 // Try to get a root directory. | |
| 272 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) { | |
| 273 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " | |
| 274 << kRootPathTestCases[i].expected_path); | |
| 275 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 276 kRootPathTestCases[i].type, | |
| 277 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 278 NULL)); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURI) { | |
| 283 SetUpNewBackend(CreateDisallowFileAccessOptions()); | |
| 284 for (size_t i = 0; i < arraysize(kRootPathFileURITestCases); ++i) { | |
| 285 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" | |
| 286 << i << " " << kRootPathFileURITestCases[i].expected_path); | |
| 287 EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
| 288 kRootPathFileURITestCases[i].type, | |
| 289 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 290 NULL)); | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURIWithAllowFlag) { | |
| 295 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
| 296 for (size_t i = 0; i < arraysize(kRootPathFileURITestCases); ++i) { | |
| 297 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" | |
| 298 << i << " " << kRootPathFileURITestCases[i].expected_path); | |
| 299 base::FilePath root_path; | |
| 300 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
| 301 kRootPathFileURITestCases[i].type, | |
| 302 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
| 303 &root_path)); | |
| 304 base::FilePath expected = file_system_path().AppendASCII( | |
| 305 kRootPathFileURITestCases[i].expected_path); | |
| 306 EXPECT_EQ(expected.value(), root_path.value()); | |
| 307 EXPECT_TRUE(base::DirectoryExists(root_path)); | |
| 308 } | |
| 309 } | |
| 310 | |
| 311 } // namespace content | |
| OLD | NEW |