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