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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "storage/browser/blob/blob_url_registry.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/unguessable_token.h"
#include "blob_url_registry.h"
#include "net/base/features.h"
#include "storage/browser/test/fake_blob.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace storage {
namespace {
std::string UuidFromBlob(mojo::PendingRemote<blink::mojom::Blob> pending_blob) {
mojo::Remote<blink::mojom::Blob> blob(std::move(pending_blob));
base::RunLoop loop;
std::string received_uuid;
blob->GetInternalUUID(base::BindOnce(
[](base::OnceClosure quit_closure, std::string* uuid_out,
const std::string& uuid) {
*uuid_out = uuid;
std::move(quit_closure).Run();
},
loop.QuitClosure(), &received_uuid));
loop.Run();
return received_uuid;
}
enum class PartitionedBlobUrlTestCase {
kPartitioningDisabled,
kPartitioningEnabled,
};
class BlobUrlRegistryTestP
: public testing::Test,
public testing::WithParamInterface<PartitionedBlobUrlTestCase> {
public:
void SetUp() override {
test_case_ = GetParam();
InitializeScopedFeatureList();
}
void InitializeScopedFeatureList() {
scoped_feature_list_.InitWithFeatureState(
net::features::kThirdPartyStoragePartitioning,
StoragePartitioningEnabled());
}
bool StoragePartitioningEnabled() {
switch (test_case_) {
case PartitionedBlobUrlTestCase::kPartitioningEnabled:
return true;
default:
return false;
}
}
private:
PartitionedBlobUrlTestCase test_case_;
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_P(BlobUrlRegistryTestP, URLRegistration) {
const std::string kBlobId1 = "Blob1";
const std::string kType = "type1";
const std::string kDisposition = "disp1";
const std::string kBlobId2 = "Blob2";
const GURL kURL1 = GURL("blob://Blob1");
const GURL kURL2 = GURL("blob://Blob2");
base::UnguessableToken kTokenId1 = base::UnguessableToken::Create();
base::UnguessableToken kTokenId2 = base::UnguessableToken::Create();
net::SchemefulSite kTopLevelSite1 =
net::SchemefulSite(GURL("https://example.com"));
net::SchemefulSite kTopLevelSite2 =
net::SchemefulSite(GURL("https://foobar.com"));
const blink::StorageKey storageKey1 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(kURL1));
const blink::StorageKey storageKey2 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(kURL2));
base::test::SingleThreadTaskEnvironment task_environment_;
FakeBlob blob1(kBlobId1);
FakeBlob blob2(kBlobId2);
BlobUrlRegistry registry;
EXPECT_EQ(registry.IsUrlMapped(kURL1, storageKey1),
BlobUrlRegistry::MappingStatus::kNotMappedOther);
EXPECT_FALSE(registry.GetBlobFromUrl(kURL1));
EXPECT_FALSE(registry.RemoveUrlMapping(kURL1, storageKey1));
EXPECT_EQ(0u, registry.url_count());
EXPECT_TRUE(registry.AddUrlMapping(kURL1, blob1.Clone(), storageKey1,
storageKey1.origin(), /*rph_id=*/0,
kTokenId1, kTopLevelSite1));
EXPECT_FALSE(registry.AddUrlMapping(kURL1, blob2.Clone(), storageKey1,
storageKey1.origin(), /*rph_id=*/0,
kTokenId1, kTopLevelSite1));
EXPECT_EQ(kTokenId1, registry.GetUnsafeAgentClusterID(kURL1));
EXPECT_EQ(kTopLevelSite1, registry.GetUnsafeTopLevelSite(kURL1));
EXPECT_EQ(registry.IsUrlMapped(kURL1, storageKey1),
BlobUrlRegistry::MappingStatus::kIsMapped);
EXPECT_EQ(kBlobId1, UuidFromBlob(registry.GetBlobFromUrl(kURL1)));
EXPECT_TRUE(registry.GetBlobFromUrl(kURL1));
EXPECT_EQ(1u, registry.url_count());
EXPECT_TRUE(registry.AddUrlMapping(kURL2, blob2.Clone(), storageKey2,
storageKey2.origin(), /*rph_id=*/0,
kTokenId2, kTopLevelSite2));
EXPECT_EQ(kTokenId2, registry.GetUnsafeAgentClusterID(kURL2));
EXPECT_EQ(kTopLevelSite2, registry.GetUnsafeTopLevelSite(kURL2));
EXPECT_EQ(2u, registry.url_count());
EXPECT_TRUE(registry.RemoveUrlMapping(kURL2, storageKey2));
EXPECT_EQ(registry.IsUrlMapped(kURL2, storageKey2),
BlobUrlRegistry::MappingStatus::kNotMappedOther);
EXPECT_EQ(std::nullopt, registry.GetUnsafeAgentClusterID(kURL2));
EXPECT_EQ(std::nullopt, registry.GetUnsafeTopLevelSite(kURL2));
// Both urls point to the same blob.
EXPECT_TRUE(registry.AddUrlMapping(kURL2, blob1.Clone(), storageKey2,
storageKey2.origin(), /*rph_id=*/0,
kTokenId2, kTopLevelSite2));
EXPECT_EQ(kTokenId2, registry.GetUnsafeAgentClusterID(kURL2));
EXPECT_EQ(kTopLevelSite2, registry.GetUnsafeTopLevelSite(kURL2));
EXPECT_EQ(UuidFromBlob(registry.GetBlobFromUrl(kURL1)),
UuidFromBlob(registry.GetBlobFromUrl(kURL2)));
EXPECT_TRUE(registry.RemoveUrlMapping(kURL2, storageKey2));
// Test using a storage key that doesn't correspond to the Blob URL.
EXPECT_NE(storageKey1, storageKey2);
EXPECT_EQ(registry.IsUrlMapped(kURL1, storageKey2),
BlobUrlRegistry::MappingStatus::kNotMappedOther);
EXPECT_FALSE(registry.RemoveUrlMapping(kURL1, storageKey2));
EXPECT_EQ(registry.IsUrlMapped(kURL1, storageKey1),
BlobUrlRegistry::MappingStatus::kIsMapped);
EXPECT_TRUE(registry.RemoveUrlMapping(kURL1, storageKey1));
EXPECT_EQ(0u, registry.url_count());
// Now do some tests with third-party storage keys>
if (StoragePartitioningEnabled()) {
blink::StorageKey partitionedStorageKey1 =
blink::StorageKey::Create(url::Origin::Create(kURL1), kTopLevelSite1,
blink::mojom::AncestorChainBit::kCrossSite);
blink::StorageKey partitionedStorageKey2 =
blink::StorageKey::Create(url::Origin::Create(kURL1), kTopLevelSite2,
blink::mojom::AncestorChainBit::kCrossSite);
EXPECT_TRUE(
registry.AddUrlMapping(kURL1, blob1.Clone(), partitionedStorageKey1,
partitionedStorageKey1.origin(), /*rph_id=*/0,
kTokenId1, kTopLevelSite1));
EXPECT_EQ(registry.IsUrlMapped(kURL1, partitionedStorageKey1),
BlobUrlRegistry::MappingStatus::kIsMapped);
EXPECT_EQ(kBlobId1, UuidFromBlob(registry.GetBlobFromUrl(kURL1)));
EXPECT_TRUE(registry.GetBlobFromUrl(kURL1));
EXPECT_EQ(registry.IsUrlMapped(kURL1, partitionedStorageKey2),
BlobUrlRegistry::MappingStatus::kNotMappedOther);
EXPECT_FALSE(registry.RemoveUrlMapping(kURL1, partitionedStorageKey2));
EXPECT_EQ(registry.IsUrlMapped(kURL1, partitionedStorageKey1),
BlobUrlRegistry::MappingStatus::kIsMapped);
EXPECT_TRUE(registry.RemoveUrlMapping(kURL1, partitionedStorageKey1));
}
EXPECT_EQ(0u, registry.url_count());
}
INSTANTIATE_TEST_SUITE_P(
BlobUrlRegistryTests,
BlobUrlRegistryTestP,
::testing::Values(PartitionedBlobUrlTestCase::kPartitioningDisabled,
PartitionedBlobUrlTestCase::kPartitioningEnabled));
} // namespace
} // namespace storage
|