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
|
// 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 "components/browsing_data/content/browsing_data_model_test_util.h"
#include <variant>
#include "components/browsing_data/content/browsing_data_model.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace browsing_data_model_test_util {
namespace {
struct DataKeyDebugStringVisitor {
template <class T>
std::string operator()(const T& data_key);
};
template <>
std::string DataKeyDebugStringVisitor::operator()<url::Origin>(
const url::Origin& origin) {
std::stringstream debug_string;
debug_string << "Origin: " << origin.Serialize();
return debug_string.str();
}
template <>
std::string DataKeyDebugStringVisitor::operator()<blink::StorageKey>(
const blink::StorageKey& storage_key) {
std::stringstream debug_string;
debug_string << "blink::StorageKey: " << storage_key.GetDebugString();
return debug_string.str();
}
template <>
std::string DataKeyDebugStringVisitor::operator()<
content::InterestGroupManager::InterestGroupDataKey>(
const content::InterestGroupManager::InterestGroupDataKey&
interest_group_data_key) {
std::stringstream debug_string;
debug_string << "InterestGroupDataKey: ";
debug_string << "{owner: " << interest_group_data_key.owner.Serialize();
debug_string << " joining_origin: ";
debug_string << interest_group_data_key.joining_origin.Serialize() << "}";
return debug_string.str();
}
template <>
std::string
DataKeyDebugStringVisitor::operator()<content::AttributionDataModel::DataKey>(
const content::AttributionDataModel::DataKey& attribution_data_key) {
std::stringstream debug_string;
debug_string << "AttributionDataKey: ";
debug_string << attribution_data_key.reporting_origin();
return debug_string.str();
}
template <>
std::string DataKeyDebugStringVisitor::operator()<
content::PrivateAggregationDataModel::DataKey>(
const content::PrivateAggregationDataModel::DataKey&
private_aggregation_data_key) {
std::stringstream debug_string;
debug_string << "PrivateAggregationDataKey: ";
debug_string << private_aggregation_data_key.reporting_origin();
return debug_string.str();
}
template <>
std::string
DataKeyDebugStringVisitor::operator()<net::SharedDictionaryIsolationKey>(
const net::SharedDictionaryIsolationKey& shared_dictionary_isolation_key) {
std::stringstream debug_string;
debug_string << "SharedDictionaryIsolationKey: ";
debug_string << "{frame_origin: ";
debug_string << shared_dictionary_isolation_key.frame_origin().Serialize();
debug_string << " top_frame_site: ";
debug_string
<< shared_dictionary_isolation_key.top_frame_site().GetDebugString()
<< "}";
return debug_string.str();
}
template <>
std::string
DataKeyDebugStringVisitor::operator()<browsing_data::SharedWorkerInfo>(
const browsing_data::SharedWorkerInfo& shared_worker_info) {
std::stringstream debug_string;
debug_string << "SharedWorkerInfo: ";
debug_string << "{worker: " << shared_worker_info.worker;
debug_string << " name: " << shared_worker_info.name;
debug_string << " blink::StorageKey: ";
debug_string << shared_worker_info.storage_key.GetDebugString() << "}";
return debug_string.str();
}
template <>
std::string
DataKeyDebugStringVisitor::operator()<content::SessionStorageUsageInfo>(
const content::SessionStorageUsageInfo& session_storage_usage_info) {
std::stringstream debug_string;
debug_string << "SessionStorageUsageInfo: ";
debug_string << "{namespace_id: " << session_storage_usage_info.namespace_id;
debug_string << " blink::StorageKey: ";
debug_string << session_storage_usage_info.storage_key.GetDebugString()
<< "}";
return debug_string.str();
}
template <>
std::string DataKeyDebugStringVisitor::operator()<net::CanonicalCookie>(
const net::CanonicalCookie& cookie) {
std::stringstream debug_string;
debug_string << "CanonicalCookie: {" << cookie.DebugString();
debug_string << " Partitioned: " << cookie.IsPartitioned();
if (cookie.IsPartitioned()) {
debug_string << " Partitioning site: "
<< cookie.PartitionKey()->site().Serialize();
}
debug_string << "}";
return debug_string.str();
}
template <>
std::string DataKeyDebugStringVisitor::operator()<
webid::FederatedIdentityDataModel::DataKey>(
const webid::FederatedIdentityDataModel::DataKey&
federated_identity_data_key) {
std::stringstream debug_string;
debug_string << "FederatedIdentityDataKey: ";
debug_string << "{relying_party_requester: "
<< federated_identity_data_key.relying_party_requester();
debug_string << " relying_party_embedder: "
<< federated_identity_data_key.relying_party_embedder();
debug_string << " identity_provider: "
<< federated_identity_data_key.identity_provider();
debug_string << " account_id: " << federated_identity_data_key.account_id()
<< "}";
return debug_string.str();
}
template <>
std::string
DataKeyDebugStringVisitor::operator()<net::device_bound_sessions::SessionKey>(
const net::device_bound_sessions::SessionKey& session_key) {
std::stringstream debug_string;
debug_string << "net::device_bound_sessions::SessionKey: ";
debug_string << "{site: " << session_key.site;
debug_string << " id: " << session_key.id.value() << "}";
return debug_string.str();
}
struct DataOwnerDebugStringVisitor {
template <class T>
std::string operator()(const T& data_owner);
};
template <>
std::string DataOwnerDebugStringVisitor::operator()<std::string>(
const std::string& host) {
return host;
}
template <>
std::string DataOwnerDebugStringVisitor::operator()<url::Origin>(
const url::Origin& origin) {
return origin.Serialize();
}
} // namespace
BrowsingDataEntry::BrowsingDataEntry(
const BrowsingDataModel::DataOwner& data_owner,
BrowsingDataModel::DataKey data_key,
BrowsingDataModel::DataDetails data_details)
: data_owner(data_owner), data_key(data_key), data_details(data_details) {}
BrowsingDataEntry::BrowsingDataEntry(
const BrowsingDataModel::BrowsingDataEntryView& view)
: data_owner(*view.data_owner),
data_key(*view.data_key),
data_details(*view.data_details) {}
BrowsingDataEntry::~BrowsingDataEntry() = default;
BrowsingDataEntry::BrowsingDataEntry(const BrowsingDataEntry& other) = default;
bool BrowsingDataEntry::operator==(const BrowsingDataEntry& other) const {
return data_owner == other.data_owner && data_key == other.data_key &&
data_details == other.data_details;
}
std::string BrowsingDataEntry::ToDebugString() const {
std::stringstream debug_string;
debug_string << "Data Owner: ";
debug_string << std::visit(DataOwnerDebugStringVisitor(), data_owner);
debug_string << " Data Key: ";
debug_string << std::visit(DataKeyDebugStringVisitor(), data_key);
debug_string << " Storage Types: ";
debug_string << data_details.storage_types.ToEnumBitmask();
debug_string << " Storage Size: ";
debug_string << data_details.storage_size;
debug_string << " Cookie count: ";
debug_string << data_details.cookie_count;
return debug_string.str();
}
void ValidateBrowsingDataEntries(
BrowsingDataModel* model,
const std::vector<BrowsingDataEntry>& expected_entries) {
std::vector<BrowsingDataEntry> model_entries;
for (const auto& entry : *model) {
model_entries.emplace_back(entry);
}
std::string model_entries_debug_string = "[";
for (const auto& entry : model_entries) {
model_entries_debug_string += "\n{";
model_entries_debug_string += entry.ToDebugString();
model_entries_debug_string += "},";
}
model_entries_debug_string += "]";
std::string expected_entries_debug_string = "[";
for (const auto& entry : expected_entries) {
expected_entries_debug_string += "\n{";
expected_entries_debug_string += entry.ToDebugString();
expected_entries_debug_string += "},";
}
expected_entries_debug_string += "]";
SCOPED_TRACE("\nModel Entries:\n" + model_entries_debug_string +
"\nExpected Entries:\n" + expected_entries_debug_string);
EXPECT_THAT(model_entries,
testing::UnorderedElementsAreArray(expected_entries));
}
void ValidateBrowsingDataEntriesNonZeroUsage(
BrowsingDataModel* model,
const std::vector<BrowsingDataEntry>& expected_entries) {
std::vector<BrowsingDataEntry> model_entries;
for (const auto& entry : *model) {
model_entries.emplace_back(entry);
}
EXPECT_EQ(model_entries.size(), expected_entries.size());
for (size_t i = 0; i < expected_entries.size(); i++) {
EXPECT_EQ(expected_entries[i].data_owner, model_entries[i].data_owner);
EXPECT_EQ(expected_entries[i].data_key, model_entries[i].data_key);
EXPECT_EQ(expected_entries[i].data_details.storage_types,
model_entries[i].data_details.storage_types);
EXPECT_EQ(expected_entries[i].data_details.cookie_count,
model_entries[i].data_details.cookie_count);
EXPECT_TRUE(model_entries[i].data_details.storage_size > 0);
}
}
} // namespace browsing_data_model_test_util
|