sandbox_file_system_backend_delegate_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "storage/browser/file_system/sandbox_file_system_backend_delegate.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/files/file.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/test/task_environment.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "storage/browser/file_system/file_system_url.h"
  13. #include "storage/browser/test/mock_quota_manager_proxy.h"
  14. #include "storage/browser/test/test_file_system_options.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "third_party/blink/public/common/storage_key/storage_key.h"
  17. #include "url/gurl.h"
  18. #include "url/origin.h"
  19. namespace storage {
  20. namespace {
  21. FileSystemURL CreateFileSystemURL(const char* path) {
  22. return FileSystemURL::CreateForTest(
  23. blink::StorageKey::CreateFromStringForTesting("http://foo/"),
  24. kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe(path));
  25. }
  26. } // namespace
  27. class SandboxFileSystemBackendDelegateTest : public testing::Test {
  28. protected:
  29. void SetUp() override {
  30. ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
  31. quota_manager_proxy_ = base::MakeRefCounted<MockQuotaManagerProxy>(
  32. nullptr, base::ThreadTaskRunnerHandle::Get());
  33. delegate_ = std::make_unique<SandboxFileSystemBackendDelegate>(
  34. quota_manager_proxy_.get(), base::ThreadTaskRunnerHandle::Get().get(),
  35. data_dir_.GetPath(), /*special_storage_policy=*/nullptr,
  36. CreateAllowFileAccessOptions(), /*env_override=*/nullptr);
  37. }
  38. bool IsAccessValid(const FileSystemURL& url) const {
  39. return delegate_->IsAccessValid(url);
  40. }
  41. void OpenFileSystem(const blink::StorageKey& storage_key,
  42. const absl::optional<BucketLocator>& bucket_locator,
  43. FileSystemType type,
  44. OpenFileSystemMode mode) {
  45. delegate_->OpenFileSystem(
  46. storage_key, bucket_locator, type, mode,
  47. base::BindOnce(
  48. &SandboxFileSystemBackendDelegateTest::OpenFileSystemCallback,
  49. base::Unretained(this)),
  50. GURL());
  51. task_environment_.RunUntilIdle();
  52. }
  53. int callback_count() const { return callback_count_; }
  54. base::File::Error last_error() const { return last_error_; }
  55. MockQuotaManagerProxy* quota_manager_proxy() const {
  56. return quota_manager_proxy_.get();
  57. }
  58. private:
  59. void OpenFileSystemCallback(const GURL& root_url,
  60. const std::string& name,
  61. base::File::Error error) {
  62. ++callback_count_;
  63. last_error_ = error;
  64. }
  65. base::ScopedTempDir data_dir_;
  66. base::test::TaskEnvironment task_environment_;
  67. scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
  68. std::unique_ptr<SandboxFileSystemBackendDelegate> delegate_;
  69. int callback_count_ = 0;
  70. base::File::Error last_error_ = base::File::FILE_OK;
  71. };
  72. TEST_F(SandboxFileSystemBackendDelegateTest, IsAccessValid) {
  73. // Normal case.
  74. EXPECT_TRUE(IsAccessValid(CreateFileSystemURL("a")));
  75. // Access to a path with parent references ('..') should be disallowed.
  76. EXPECT_FALSE(IsAccessValid(CreateFileSystemURL("a/../b")));
  77. // Access from non-allowed scheme should be disallowed.
  78. EXPECT_FALSE(IsAccessValid(FileSystemURL::CreateForTest(
  79. blink::StorageKey::CreateFromStringForTesting("unknown://bar"),
  80. kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe("foo"))));
  81. // Access with restricted name should be disallowed.
  82. EXPECT_FALSE(IsAccessValid(CreateFileSystemURL(".")));
  83. EXPECT_FALSE(IsAccessValid(CreateFileSystemURL("..")));
  84. // This is also disallowed due to Windows XP parent path handling.
  85. EXPECT_FALSE(IsAccessValid(CreateFileSystemURL("...")));
  86. // These are identified as unsafe cases due to weird path handling
  87. // on Windows.
  88. EXPECT_FALSE(IsAccessValid(CreateFileSystemURL(" ..")));
  89. EXPECT_FALSE(IsAccessValid(CreateFileSystemURL(".. ")));
  90. // Similar but safe cases.
  91. EXPECT_TRUE(IsAccessValid(CreateFileSystemURL(" .")));
  92. EXPECT_TRUE(IsAccessValid(CreateFileSystemURL(". ")));
  93. EXPECT_TRUE(IsAccessValid(CreateFileSystemURL("b.")));
  94. EXPECT_TRUE(IsAccessValid(CreateFileSystemURL(".b")));
  95. // A path that looks like a drive letter.
  96. EXPECT_TRUE(IsAccessValid(CreateFileSystemURL("c:")));
  97. }
  98. TEST_F(SandboxFileSystemBackendDelegateTest, OpenFileSystemAccessesStorage) {
  99. EXPECT_EQ(quota_manager_proxy()->notify_storage_accessed_count(), 0);
  100. EXPECT_EQ(callback_count(), 0);
  101. const blink::StorageKey& storage_key =
  102. blink::StorageKey::CreateFromStringForTesting("http://example.com");
  103. // TODO(https://crbug.com/1330608): ensure that this test suite properly
  104. // integrates non-default BucketLocators into OpenFileSystem.
  105. OpenFileSystem(storage_key, /*bucket_locator=*/absl::nullopt,
  106. kFileSystemTypeTemporary,
  107. OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT);
  108. EXPECT_EQ(callback_count(), 1);
  109. EXPECT_EQ(last_error(), base::File::FILE_OK);
  110. EXPECT_EQ(quota_manager_proxy()->notify_storage_accessed_count(), 1);
  111. EXPECT_EQ(quota_manager_proxy()->last_notified_storage_key(), storage_key);
  112. EXPECT_EQ(quota_manager_proxy()->last_notified_type(),
  113. blink::mojom::StorageType::kTemporary);
  114. }
  115. } // namespace storage