| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/fileapi/file_system_path_manager.h" | 5 #include "webkit/fileapi/file_system_path_manager.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_callback_factory.h" | 13 #include "base/memory/scoped_callback_factory.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_temp_dir.h" | 15 #include "base/memory/scoped_temp_dir.h" |
| 16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 17 #include "base/message_loop_proxy.h" | 17 #include "base/message_loop_proxy.h" |
| 18 #include "base/sys_string_conversions.h" |
| 19 #include "base/utf_string_conversions.h" |
| 18 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "webkit/fileapi/file_system_util.h" | 22 #include "webkit/fileapi/file_system_util.h" |
| 21 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 23 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
| 22 #include "webkit/quota/special_storage_policy.h" | 24 #include "webkit/quota/special_storage_policy.h" |
| 23 | 25 |
| 24 using namespace fileapi; | 26 using namespace fileapi; |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 52 { fileapi::kFileSystemTypePersistent, "http://bar.com/", | 54 { fileapi::kFileSystemTypePersistent, "http://bar.com/", |
| 53 "http_bar.com_0" PS "Persistent" }, | 55 "http_bar.com_0" PS "Persistent" }, |
| 54 { fileapi::kFileSystemTypeTemporary, "https://foo:2/", | 56 { fileapi::kFileSystemTypeTemporary, "https://foo:2/", |
| 55 "https_foo_2" PS "Temporary" }, | 57 "https_foo_2" PS "Temporary" }, |
| 56 { fileapi::kFileSystemTypePersistent, "https://foo:2/", | 58 { fileapi::kFileSystemTypePersistent, "https://foo:2/", |
| 57 "https_foo_2" PS "Persistent" }, | 59 "https_foo_2" PS "Persistent" }, |
| 58 { fileapi::kFileSystemTypeTemporary, "https://bar.com/", | 60 { fileapi::kFileSystemTypeTemporary, "https://bar.com/", |
| 59 "https_bar.com_0" PS "Temporary" }, | 61 "https_bar.com_0" PS "Temporary" }, |
| 60 { fileapi::kFileSystemTypePersistent, "https://bar.com/", | 62 { fileapi::kFileSystemTypePersistent, "https://bar.com/", |
| 61 "https_bar.com_0" PS "Persistent" }, | 63 "https_bar.com_0" PS "Persistent" }, |
| 64 #if defined(OS_CHROMEOS) |
| 65 { fileapi::kFileSystemTypeExternal, "chrome-extension://foo/", |
| 66 "chrome-extension__0" PS "External" }, |
| 67 #endif |
| 62 }; | 68 }; |
| 63 | 69 |
| 64 const struct RootPathFileURITest { | 70 const struct RootPathFileURITest { |
| 65 fileapi::FileSystemType type; | 71 fileapi::FileSystemType type; |
| 66 const char* origin_url; | 72 const char* origin_url; |
| 67 const char* expected_path; | 73 const char* expected_path; |
| 74 const char* virtual_path; |
| 68 } kRootPathFileURITestCases[] = { | 75 } kRootPathFileURITestCases[] = { |
| 69 { fileapi::kFileSystemTypeTemporary, "file:///", | 76 { fileapi::kFileSystemTypeTemporary, "file:///", |
| 70 "file__0" PS "Temporary" }, | 77 "file__0" PS "Temporary", NULL }, |
| 71 { fileapi::kFileSystemTypePersistent, "file:///", | 78 { fileapi::kFileSystemTypePersistent, "file:///", |
| 72 "file__0" PS "Persistent" }, | 79 "file__0" PS "Persistent", NULL }, |
| 73 // TODO(zelidrag): Add fileapi::kFileSystemTypeLocal test cases here once | 80 #if defined(OS_CHROMEOS) |
| 74 // we fix ChromeOS build of this test. | 81 { fileapi::kFileSystemTypeExternal, "chrome-extension://foo/", |
| 82 "chrome-extension__0" PS "External", "testing" }, |
| 83 #endif |
| 75 }; | 84 }; |
| 76 | 85 |
| 77 const struct CheckValidPathTest { | 86 const struct CheckValidPathTest { |
| 78 FilePath::StringType path; | 87 FilePath::StringType path; |
| 79 bool expected_valid; | 88 bool expected_valid; |
| 80 } kCheckValidPathTestCases[] = { | 89 } kCheckValidPathTestCases[] = { |
| 81 { FILE_PATH_LITERAL("//tmp/foo.txt"), false, }, | 90 { FILE_PATH_LITERAL("//tmp/foo.txt"), false, }, |
| 82 { FILE_PATH_LITERAL("//etc/hosts"), false, }, | 91 { FILE_PATH_LITERAL("//etc/hosts"), false, }, |
| 83 { FILE_PATH_LITERAL("foo.txt"), true, }, | 92 { FILE_PATH_LITERAL("foo.txt"), true, }, |
| 84 { FILE_PATH_LITERAL("a/b/c"), true, }, | 93 { FILE_PATH_LITERAL("a/b/c"), true, }, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 { FILE_PATH_LITERAL("ab|.txt"), true, }, | 165 { FILE_PATH_LITERAL("ab|.txt"), true, }, |
| 157 { FILE_PATH_LITERAL("\\ab"), true, }, | 166 { FILE_PATH_LITERAL("\\ab"), true, }, |
| 158 { FILE_PATH_LITERAL("/ab"), true, }, | 167 { FILE_PATH_LITERAL("/ab"), true, }, |
| 159 { FILE_PATH_LITERAL("<ab"), true, }, | 168 { FILE_PATH_LITERAL("<ab"), true, }, |
| 160 { FILE_PATH_LITERAL(">ab"), true, }, | 169 { FILE_PATH_LITERAL(">ab"), true, }, |
| 161 { FILE_PATH_LITERAL(":ab"), true, }, | 170 { FILE_PATH_LITERAL(":ab"), true, }, |
| 162 { FILE_PATH_LITERAL("?ab"), true, }, | 171 { FILE_PATH_LITERAL("?ab"), true, }, |
| 163 { FILE_PATH_LITERAL("|ab"), true, }, | 172 { FILE_PATH_LITERAL("|ab"), true, }, |
| 164 }; | 173 }; |
| 165 | 174 |
| 175 FilePath UTF8ToFilePath(const std::string& str) { |
| 176 FilePath::StringType result; |
| 177 #if defined(OS_POSIX) |
| 178 result = base::SysWideToNativeMB(UTF8ToWide(str)); |
| 179 #elif defined(OS_WIN) |
| 180 result = UTF8ToUTF16(str); |
| 181 #endif |
| 182 return FilePath(result); |
| 183 } |
| 184 |
| 166 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { | 185 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { |
| 167 public: | 186 public: |
| 168 virtual bool IsStorageProtected(const GURL& origin) { | 187 virtual bool IsStorageProtected(const GURL& origin) { |
| 169 return false; | 188 return false; |
| 170 } | 189 } |
| 171 | 190 |
| 172 virtual bool IsStorageUnlimited(const GURL& origin) { | 191 virtual bool IsStorageUnlimited(const GURL& origin) { |
| 173 return true; | 192 return true; |
| 174 } | 193 } |
| 175 | 194 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 190 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 209 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 191 root_path_callback_status_ = false; | 210 root_path_callback_status_ = false; |
| 192 root_path_.clear(); | 211 root_path_.clear(); |
| 193 file_system_name_.clear(); | 212 file_system_name_.clear(); |
| 194 } | 213 } |
| 195 | 214 |
| 196 protected: | 215 protected: |
| 197 FileSystemPathManager* NewPathManager( | 216 FileSystemPathManager* NewPathManager( |
| 198 bool incognito, | 217 bool incognito, |
| 199 bool allow_file_access) { | 218 bool allow_file_access) { |
| 200 return new FileSystemPathManager( | 219 FileSystemPathManager* manager = new FileSystemPathManager( |
| 201 base::MessageLoopProxy::CreateForCurrentThread(), | 220 base::MessageLoopProxy::CreateForCurrentThread(), |
| 202 data_dir_.path(), | 221 data_dir_.path(), |
| 203 scoped_refptr<quota::SpecialStoragePolicy>( | 222 scoped_refptr<quota::SpecialStoragePolicy>( |
| 204 new TestSpecialStoragePolicy()), | 223 new TestSpecialStoragePolicy()), |
| 205 incognito, | 224 incognito, |
| 206 allow_file_access); | 225 allow_file_access); |
| 226 #if defined(OS_CHROMEOS) |
| 227 fileapi::ExternalFileSystemMountPointProvider* ext_provider = |
| 228 manager->external_provider(); |
| 229 ext_provider->AddMountPoint(FilePath("/tmp/testing")); |
| 230 #endif |
| 231 return manager; |
| 207 } | 232 } |
| 208 | 233 |
| 209 void OnGetRootPath(bool success, | 234 void OnGetRootPath(bool success, |
| 210 const FilePath& root_path, | 235 const FilePath& root_path, |
| 211 const std::string& name) { | 236 const std::string& name) { |
| 212 root_path_callback_status_ = success; | 237 root_path_callback_status_ = success; |
| 213 root_path_ = root_path; | 238 root_path_ = root_path; |
| 214 file_system_name_ = name; | 239 file_system_name_ = name; |
| 215 } | 240 } |
| 216 | 241 |
| 217 bool GetRootPath(FileSystemPathManager* manager, | 242 bool GetRootPath(FileSystemPathManager* manager, |
| 218 const GURL& origin_url, | 243 const GURL& origin_url, |
| 219 fileapi::FileSystemType type, | 244 fileapi::FileSystemType type, |
| 220 bool create, | 245 bool create, |
| 221 FilePath* root_path) { | 246 FilePath* root_path) { |
| 222 manager->GetFileSystemRootPath(origin_url, type, create, | 247 manager->ValidateFileSystemRootAndGetURL(origin_url, type, create, |
| 223 callback_factory_.NewCallback( | 248 callback_factory_.NewCallback( |
| 224 &FileSystemPathManagerTest::OnGetRootPath)); | 249 &FileSystemPathManagerTest::OnGetRootPath)); |
| 225 MessageLoop::current()->RunAllPending(); | 250 MessageLoop::current()->RunAllPending(); |
| 226 if (root_path) | 251 if (root_path) |
| 227 *root_path = root_path_; | 252 *root_path = root_path_; |
| 228 return root_path_callback_status_; | 253 return root_path_callback_status_; |
| 229 } | 254 } |
| 230 | 255 |
| 231 FilePath data_path() { return data_dir_.path(); } | 256 FilePath data_path() { return data_dir_.path(); } |
| 232 FilePath file_system_path() { | 257 FilePath file_system_path() { |
| 233 return data_dir_.path().Append( | 258 return data_dir_.path().Append( |
| 234 SandboxMountPointProvider::kFileSystemDirectory); | 259 SandboxMountPointProvider::kFileSystemDirectory); |
| 235 } | 260 } |
| 261 FilePath external_file_system_path() { |
| 262 return UTF8ToFilePath(std::string(fileapi::kExternalDir)); |
| 263 } |
| 264 FilePath external_file_path_root() { |
| 265 return UTF8ToFilePath(std::string("/tmp")); |
| 266 } |
| 236 | 267 |
| 237 private: | 268 private: |
| 238 ScopedTempDir data_dir_; | 269 ScopedTempDir data_dir_; |
| 239 base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_; | 270 base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_; |
| 240 | 271 |
| 241 bool root_path_callback_status_; | 272 bool root_path_callback_status_; |
| 242 FilePath root_path_; | 273 FilePath root_path_; |
| 243 std::string file_system_name_; | 274 std::string file_system_name_; |
| 244 | 275 |
| 245 DISALLOW_COPY_AND_ASSIGN(FileSystemPathManagerTest); | 276 DISALLOW_COPY_AND_ASSIGN(FileSystemPathManagerTest); |
| 246 }; | 277 }; |
| 247 | 278 |
| 248 TEST_F(FileSystemPathManagerTest, GetRootPathCreateAndExamine) { | 279 TEST_F(FileSystemPathManagerTest, GetRootPathCreateAndExamine) { |
| 249 std::vector<FilePath> returned_root_path( | 280 std::vector<FilePath> returned_root_path( |
| 250 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | 281 ARRAYSIZE_UNSAFE(kRootPathTestCases)); |
| 251 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); | 282 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); |
| 252 | 283 |
| 253 // Create a new root directory. | 284 // Create a new root directory. |
| 254 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 285 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
| 255 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " | 286 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " |
| 256 << kRootPathTestCases[i].expected_path); | 287 << kRootPathTestCases[i].expected_path); |
| 257 | 288 |
| 258 FilePath root_path; | 289 FilePath root_path; |
| 259 EXPECT_TRUE(GetRootPath(manager.get(), | 290 EXPECT_TRUE(GetRootPath(manager.get(), |
| 260 GURL(kRootPathTestCases[i].origin_url), | 291 GURL(kRootPathTestCases[i].origin_url), |
| 261 kRootPathTestCases[i].type, | 292 kRootPathTestCases[i].type, |
| 262 true /* create */, &root_path)); | 293 true /* create */, &root_path)); |
| 263 | 294 |
| 264 FilePath expected = file_system_path().AppendASCII( | 295 if (kRootPathTestCases[i].type != fileapi::kFileSystemTypeExternal) { |
| 265 kRootPathTestCases[i].expected_path); | 296 FilePath expected = file_system_path().AppendASCII( |
| 266 EXPECT_EQ(expected.value(), root_path.DirName().value()); | 297 kRootPathTestCases[i].expected_path); |
| 267 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 298 EXPECT_EQ(expected.value(), root_path.DirName().value()); |
| 299 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
| 300 } else { |
| 301 // External file system root path is virtual one and does not match |
| 302 // anything from the actual file system. |
| 303 EXPECT_EQ(external_file_system_path().value(), |
| 304 root_path.value()); |
| 305 } |
| 268 ASSERT_TRUE(returned_root_path.size() > i); | 306 ASSERT_TRUE(returned_root_path.size() > i); |
| 269 returned_root_path[i] = root_path; | 307 returned_root_path[i] = root_path; |
| 270 } | 308 } |
| 271 | 309 |
| 272 // Get the root directory with create=false and see if we get the | 310 // Get the root directory with create=false and see if we get the |
| 273 // same directory. | 311 // same directory. |
| 274 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 312 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
| 275 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | 313 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " |
| 276 << kRootPathTestCases[i].expected_path); | 314 << kRootPathTestCases[i].expected_path); |
| 277 | 315 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 scoped_ptr<FileSystemPathManager> manager(NewPathManager( | 386 scoped_ptr<FileSystemPathManager> manager(NewPathManager( |
| 349 false, true /* allow_file_access_from_files */)); | 387 false, true /* allow_file_access_from_files */)); |
| 350 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | 388 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { |
| 351 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" | 389 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" |
| 352 << i << " " << kRootPathFileURITestCases[i].expected_path); | 390 << i << " " << kRootPathFileURITestCases[i].expected_path); |
| 353 FilePath root_path; | 391 FilePath root_path; |
| 354 EXPECT_TRUE(GetRootPath(manager.get(), | 392 EXPECT_TRUE(GetRootPath(manager.get(), |
| 355 GURL(kRootPathFileURITestCases[i].origin_url), | 393 GURL(kRootPathFileURITestCases[i].origin_url), |
| 356 kRootPathFileURITestCases[i].type, | 394 kRootPathFileURITestCases[i].type, |
| 357 true /* create */, &root_path)); | 395 true /* create */, &root_path)); |
| 358 FilePath expected = file_system_path().AppendASCII( | 396 if (kRootPathFileURITestCases[i].type != fileapi::kFileSystemTypeExternal) { |
| 359 kRootPathFileURITestCases[i].expected_path); | 397 FilePath expected = file_system_path().AppendASCII( |
| 360 EXPECT_EQ(expected.value(), root_path.DirName().value()); | 398 kRootPathFileURITestCases[i].expected_path); |
| 361 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 399 EXPECT_EQ(expected.value(), root_path.DirName().value()); |
| 400 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
| 401 } else { |
| 402 EXPECT_EQ(external_file_path_root().value(), root_path.value()); |
| 403 } |
| 362 } | 404 } |
| 363 } | 405 } |
| 364 | 406 |
| 365 TEST_F(FileSystemPathManagerTest, IsRestrictedName) { | 407 TEST_F(FileSystemPathManagerTest, IsRestrictedName) { |
| 366 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); | 408 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); |
| 367 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) { | 409 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) { |
| 368 SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " " | 410 SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " " |
| 369 << kIsRestrictedNameTestCases[i].name); | 411 << kIsRestrictedNameTestCases[i].name); |
| 370 FilePath name(kIsRestrictedNameTestCases[i].name); | 412 FilePath name(kIsRestrictedNameTestCases[i].name); |
| 371 EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous, | 413 EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous, |
| 372 manager->IsRestrictedFileName(kFileSystemTypeTemporary, name)); | 414 manager->IsRestrictedFileName(kFileSystemTypeTemporary, name)); |
| 373 } | 415 } |
| 374 } | 416 } |
| OLD | NEW |