1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/file_system_access/file_system_access_lock_manager.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "content/public/test/browser_task_environment.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/test_file_system_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features_generated.h"
namespace content {
using LockType = FileSystemAccessLockManager::LockType;
using storage::FileSystemURL;
static constexpr char kTestMountPoint[] = "testfs";
class FileSystemAccessLockManagerTest : public testing::Test {
public:
FileSystemAccessLockManagerTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {
scoped_feature_list.InitAndEnableFeature(
blink::features::kFileSystemAccessLockingScheme);
}
void SetUp() override {
ASSERT_TRUE(dir_.CreateUniqueTempDir());
// Register an external mount point to test support for virtual paths.
// This maps the virtual path a native local path to make these tests work
// on all platforms. We're not testing more complicated ChromeOS specific
// file system backends here.
storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
kTestMountPoint, storage::kFileSystemTypeLocal,
storage::FileSystemMountOption(), dir_.GetPath());
file_system_context_ = storage::CreateFileSystemContextForTesting(
/*quota_manager_proxy=*/nullptr, dir_.GetPath());
chrome_blob_context_ = base::MakeRefCounted<ChromeBlobStorageContext>();
chrome_blob_context_->InitializeOnIOThread(base::FilePath(),
base::FilePath(), nullptr);
manager_ = base::MakeRefCounted<FileSystemAccessManagerImpl>(
file_system_context_, chrome_blob_context_,
/*permission_context=*/nullptr,
/*off_the_record=*/false);
}
void TearDown() override {
manager_.reset();
task_environment_.RunUntilIdle();
EXPECT_TRUE(dir_.Delete());
}
scoped_refptr<FileSystemAccessLockManager::LockHandle> TakeLockSync(
const storage::FileSystemURL& url,
FileSystemAccessLockManager::LockType lock_type) {
base::test::TestFuture<
scoped_refptr<FileSystemAccessLockManager::LockHandle>>
future;
manager_->TakeLock(url, lock_type, future.GetCallback());
return future.Take();
}
void AssertAncestorLockBehavior(const FileSystemURL& parent_url,
const FileSystemURL& child_url) {
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType ancestor_lock_type = manager_->GetAncestorLockTypeForTesting();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
// Parent cannot take an exclusive lock if child holds an exclusive lock.
{
auto child_lock = TakeLockSync(child_url, exclusive_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_FALSE(TakeLockSync(parent_url, exclusive_lock_type));
}
// Parent can take an ancestor lock if child holds an exclusive lock.
{
auto child_lock = TakeLockSync(child_url, exclusive_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_TRUE(TakeLockSync(parent_url, ancestor_lock_type));
}
// Child cannot take an exclusive lock if parent holds an exclusive lock.
{
auto parent_lock = TakeLockSync(parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_FALSE(TakeLockSync(child_url, exclusive_lock_type));
}
// Child can take an exclusive lock if parent holds an ancestor lock.
{
auto parent_lock = TakeLockSync(parent_url, ancestor_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_TRUE(TakeLockSync(child_url, exclusive_lock_type));
}
// Parent cannot take an exclusive lock if child holds a shared lock.
{
auto child_lock = TakeLockSync(child_url, shared_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_FALSE(TakeLockSync(parent_url, exclusive_lock_type));
}
// Parent can take an ancestor lock if child holds a shared lock.
{
auto child_lock = TakeLockSync(child_url, shared_lock_type);
ASSERT_TRUE(child_lock);
ASSERT_TRUE(TakeLockSync(parent_url, ancestor_lock_type));
}
// Child cannot take a shared lock if parent holds an exclusive lock.
{
auto parent_lock = TakeLockSync(parent_url, exclusive_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_FALSE(TakeLockSync(child_url, shared_lock_type));
}
// Child can take a shared lock if parent holds an ancestor lock.
{
auto parent_lock = TakeLockSync(parent_url, ancestor_lock_type);
ASSERT_TRUE(parent_lock);
ASSERT_TRUE(TakeLockSync(child_url, shared_lock_type));
}
}
protected:
const blink::StorageKey kTestStorageKey =
blink::StorageKey::CreateFromStringForTesting("https://example.com/test");
const storage::BucketLocator kTestBucketLocator =
storage::BucketLocator(storage::BucketId(1),
kTestStorageKey,
blink::mojom::StorageType::kTemporary,
/*is_default=*/false);
BrowserTaskEnvironment task_environment_;
base::ScopedTempDir dir_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
scoped_refptr<ChromeBlobStorageContext> chrome_blob_context_;
scoped_refptr<FileSystemAccessManagerImpl> manager_;
base::test::ScopedFeatureList scoped_feature_list;
};
TEST_F(FileSystemAccessLockManagerTest, ExclusiveLock) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kLocal, path);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
{
auto exclusive_lock = TakeLockSync(url, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Cannot take another lock while the file is exclusively locked.
ASSERT_FALSE(TakeLockSync(url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(url, shared_lock_type));
}
// The exclusive lock has been released and should be available to be
// re-acquired.
ASSERT_TRUE(TakeLockSync(url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SharedLock) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kLocal, path);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type_1 = manager_->CreateSharedLockTypeForTesting();
LockType shared_lock_type_2 = manager_->CreateSharedLockTypeForTesting();
{
auto shared_lock = TakeLockSync(url, shared_lock_type_1);
ASSERT_TRUE(shared_lock);
// Can take another shared lock of the same type, but not an exclusive lock
// or a shared lock of another type.
ASSERT_TRUE(TakeLockSync(url, shared_lock_type_1));
ASSERT_FALSE(TakeLockSync(url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(url, shared_lock_type_2));
}
// The shared locks have been released and we should be available to acquire
// an exclusive lock.
ASSERT_TRUE(TakeLockSync(url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SandboxedFile) {
auto url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe("test/foo/bar"));
url.SetBucket(kTestBucketLocator);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
{
auto exclusive_lock = TakeLockSync(url, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Cannot take another lock while the file is exclusively locked.
ASSERT_FALSE(TakeLockSync(url, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(url, shared_lock_type));
}
// The exclusive lock has been released and should be available to be
// re-acquired.
ASSERT_TRUE(TakeLockSync(url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SandboxedFilesSamePath) {
// Sandboxed files of the same relative path do not lock across sites if the
// BucketLocator is set.
const blink::StorageKey kOtherStorageKey =
blink::StorageKey::CreateFromStringForTesting("https://foo.com/test");
const auto path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto url1 = file_system_context_->CreateCrackedFileSystemURL(
kOtherStorageKey, storage::kFileSystemTypeTemporary, path);
url1.SetBucket(kTestBucketLocator);
auto url2 = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, path);
const storage::BucketLocator kOtherBucketLocator(
storage::BucketId(2), kOtherStorageKey,
blink::mojom::StorageType::kTemporary,
/*is_default=*/false);
url2.SetBucket(kOtherBucketLocator);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
// Take a lock on the file in the first file system.
auto exclusive_lock1 = TakeLockSync(url1, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock1);
ASSERT_FALSE(TakeLockSync(url1, exclusive_lock_type));
// Can still take a lock on the file in the second file system.
auto exclusive_lock2 = TakeLockSync(url2, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock2);
ASSERT_FALSE(TakeLockSync(url2, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, SandboxedFilesDifferentBucket) {
// Sandboxed files of the same relative path do not lock across buckets.
const auto path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto url1 = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, path);
url1.SetBucket(kTestBucketLocator);
auto url2 = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, path);
const storage::BucketLocator kOtherBucketLocator(
storage::BucketId(2), kTestStorageKey,
blink::mojom::StorageType::kTemporary,
/*is_default=*/false);
url2.SetBucket(kOtherBucketLocator);
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
// Take a lock on the file in the first file system.
auto exclusive_lock1 = TakeLockSync(url1, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock1);
ASSERT_FALSE(TakeLockSync(url1, exclusive_lock_type));
// Can still take a lock on the file in the second file system.
auto exclusive_lock2 = TakeLockSync(url2, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock2);
ASSERT_FALSE(TakeLockSync(url2, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, DifferentBackends) {
// We'll use the same path and pretend they're from different backends.
base::FilePath path =
base::FilePath::FromUTF8Unsafe(kTestMountPoint).AppendASCII("foo");
// File on a local file system.
auto local_url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kLocal, path);
// File with the same path on an external file system.
auto external_url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kExternal, path);
EXPECT_EQ(local_url.path(), external_url.virtual_path());
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
// Take a lock on the file in the local file system.
auto local_exclusive_lock = TakeLockSync(local_url, exclusive_lock_type);
ASSERT_TRUE(local_exclusive_lock);
ASSERT_FALSE(TakeLockSync(local_url, exclusive_lock_type));
// Can still take a lock on the file in the external file system.
auto external_exclusive_lock =
TakeLockSync(external_url, exclusive_lock_type);
ASSERT_TRUE(external_exclusive_lock);
ASSERT_FALSE(TakeLockSync(external_url, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, LockAcrossSites) {
base::FilePath path = dir_.GetPath().AppendASCII("foo");
auto url1 = FileSystemURL::CreateForTest(kTestStorageKey,
storage::kFileSystemTypeLocal, path);
// Select the same local file from another site.
auto url2 = FileSystemURL::CreateForTest(
blink::StorageKey::CreateFromStringForTesting("https://foo.com/bar"),
storage::kFileSystemTypeLocal, path);
EXPECT_EQ(url1.path(), url2.path());
EXPECT_NE(url1.storage_key(), url2.storage_key());
LockType exclusive_lock_type = manager_->GetExclusiveLockType();
LockType shared_lock_type = manager_->CreateSharedLockTypeForTesting();
{
auto exclusive_lock = TakeLockSync(url1, exclusive_lock_type);
ASSERT_TRUE(exclusive_lock);
// Other sites cannot access the file while it is exclusively locked.
ASSERT_FALSE(TakeLockSync(url2, exclusive_lock_type));
ASSERT_FALSE(TakeLockSync(url2, shared_lock_type));
}
// The exclusive lock has been released and should be available to be
// re-acquired.
ASSERT_TRUE(TakeLockSync(url2, exclusive_lock_type));
}
TEST_F(FileSystemAccessLockManagerTest, AncestorLocks) {
base::FilePath parent_path = dir_.GetPath().AppendASCII("foo");
auto parent_url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kLocal, parent_path);
auto child_url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kLocal,
parent_path.Append(FILE_PATH_LITERAL("child")));
AssertAncestorLockBehavior(parent_url, child_url);
}
TEST_F(FileSystemAccessLockManagerTest, AncestorLocksExternal) {
base::FilePath parent_path =
base::FilePath::FromUTF8Unsafe(kTestMountPoint).AppendASCII("foo");
auto parent_url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kExternal, parent_path);
auto child_url = manager_->CreateFileSystemURLFromPath(
FileSystemAccessEntryFactory::PathType::kExternal,
parent_path.Append(FILE_PATH_LITERAL("child")));
AssertAncestorLockBehavior(parent_url, child_url);
}
TEST_F(FileSystemAccessLockManagerTest, AncestorLocksSandboxed) {
auto parent_path = base::FilePath::FromUTF8Unsafe("test/foo/bar");
auto parent_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary, parent_path);
parent_url.SetBucket(kTestBucketLocator);
auto child_url = file_system_context_->CreateCrackedFileSystemURL(
kTestStorageKey, storage::kFileSystemTypeTemporary,
parent_path.Append(FILE_PATH_LITERAL("child")));
child_url.SetBucket(kTestBucketLocator);
AssertAncestorLockBehavior(parent_url, child_url);
}
} // namespace content
|