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
content / browser / interest_group / test_interest_group_manager_impl.cc [blame]
// Copyright 2023 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/interest_group/test_interest_group_manager_impl.h"
#include <optional>
#include <vector>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "content/browser/interest_group/interest_group_k_anonymity_manager.h"
#include "content/browser/interest_group/interest_group_manager_impl.h"
#include "content/browser/interest_group/storage_interest_group.h"
#include "content/public/browser/k_anonymity_service_delegate.h"
#include "content/services/auction_worklet/public/mojom/bidder_worklet.mojom.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/interest_group/interest_group.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
TestInterestGroupManagerImpl::TestInterestGroupManagerImpl(
const url::Origin& expected_frame_origin,
network::mojom::ClientSecurityStatePtr expected_client_security_state,
scoped_refptr<network::SharedURLLoaderFactory> expected_url_loader_factory)
: InterestGroupManagerImpl(
base::FilePath(),
/*in_memory=*/true,
InterestGroupManagerImpl::ProcessMode::kDedicated,
/*url_loader_factory=*/nullptr,
/*k_anonymity_service_callback=*/
base::BindLambdaForTesting(
[]() -> KAnonymityServiceDelegate* { return nullptr; })),
expected_frame_origin_(expected_frame_origin),
expected_client_security_state_(
std::move(expected_client_security_state)),
expected_url_loader_factory_(std::move(expected_url_loader_factory)) {
AddInterestGroupObserver(this);
set_k_anonymity_manager_for_testing(
std::make_unique<InterestGroupKAnonymityManager>(
/*interest_group_manager=*/this,
/*k_anonymity_service_callback=*/base::BindLambdaForTesting(
[&]() -> KAnonymityServiceDelegate* { return this; })));
}
TestInterestGroupManagerImpl::~TestInterestGroupManagerImpl() {
// Need to replace the InterestGroupKAnonymityManager to avoid dangling
// pointer warnings, since `this` is the `k_anonymity_service` and is being
// torn down before the old InterestGroupKAnonymityManager.
set_k_anonymity_manager_for_testing(
std::make_unique<InterestGroupKAnonymityManager>(
/*interest_group_manager=*/this,
/*k_anonymity_service_callback=*/base::BindLambdaForTesting(
[]() -> KAnonymityServiceDelegate* { return nullptr; })));
RemoveInterestGroupObserver(this);
}
void TestInterestGroupManagerImpl::EnqueueReports(
ReportType report_type,
std::vector<GURL> report_urls,
FrameTreeNodeId frame_tree_node_id,
const url::Origin& frame_origin,
const network::mojom::ClientSecurityState& client_security_state,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
EXPECT_EQ(expected_frame_origin_, frame_origin);
EXPECT_EQ(*expected_client_security_state_, client_security_state);
EXPECT_EQ(expected_url_loader_factory_.get(), url_loader_factory.get());
if (use_real_enqueue_reports_) {
InterestGroupManagerImpl::EnqueueReports(
report_type, std::move(report_urls), frame_tree_node_id, frame_origin,
client_security_state, url_loader_factory);
} else {
for (auto& report_url : report_urls) {
reports_.push_back(Report{report_type, std::move(report_url)});
}
}
}
void TestInterestGroupManagerImpl::EnqueueRealTimeReports(
std::map<url::Origin, RealTimeReportingContributions> contributions,
AdAuctionPageDataCallback ad_auction_page_data_callback,
FrameTreeNodeId frame_tree_node_id,
const url::Origin& frame_origin,
const network::mojom::ClientSecurityState& client_security_state,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
EXPECT_EQ(expected_frame_origin_, frame_origin);
EXPECT_EQ(*expected_client_security_state_, client_security_state);
EXPECT_EQ(expected_url_loader_factory_.get(), url_loader_factory.get());
if (use_real_enqueue_reports_) {
InterestGroupManagerImpl::EnqueueRealTimeReports(
std::move(contributions), std::move(ad_auction_page_data_callback),
frame_tree_node_id, frame_origin, client_security_state,
url_loader_factory);
} else {
real_time_contributions_ = std::move(contributions);
}
}
void TestInterestGroupManagerImpl::OnInterestGroupAccessed(
base::optional_ref<const std::string> devtools_auction_id,
base::Time access_time,
AccessType type,
const url::Origin& owner_origin,
const std::string& name,
base::optional_ref<const url::Origin> component_seller_origin,
std::optional<double> bid,
base::optional_ref<const std::string> bid_currency) {
if (type == AccessType::kBid) {
interest_groups_that_bid_.emplace_back(owner_origin, name);
}
}
void TestInterestGroupManagerImpl::JoinSet(
std::string id,
base::OnceCallback<void(bool)> callback) {
joined_k_anon_sets_.emplace_back(std::move(id));
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), true));
}
void TestInterestGroupManagerImpl::QuerySets(
std::vector<std::string> ids,
base::OnceCallback<void(std::vector<bool>)> callback) {
// Return that nothing is k-anonymous.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
std::vector<bool>(ids.size(), false)));
}
base::TimeDelta TestInterestGroupManagerImpl::GetJoinInterval() {
return base::Seconds(1);
}
base::TimeDelta TestInterestGroupManagerImpl::GetQueryInterval() {
return base::Seconds(1);
}
void TestInterestGroupManagerImpl::ClearLoggedData() {
reports_.clear();
interest_groups_that_bid_.clear();
}
void TestInterestGroupManagerImpl::ExpectReports(
const std::vector<Report>& expected_reports) {
EXPECT_THAT(reports_, testing::UnorderedElementsAreArray(expected_reports));
reports_.clear();
}
void TestInterestGroupManagerImpl::set_use_real_enqueue_reports(
bool use_real_enqueue_reports) {
use_real_enqueue_reports_ = use_real_enqueue_reports;
}
std::vector<GURL> TestInterestGroupManagerImpl::TakeReportUrlsOfType(
ReportType report_type) {
std::vector<GURL> out;
auto it = reports_.begin();
while (it != reports_.end()) {
if (it->report_type == report_type) {
out.push_back(std::move(it->report_url));
it = reports_.erase(it);
continue;
}
++it;
}
return out;
}
std::map<url::Origin,
TestInterestGroupManagerImpl::RealTimeReportingContributions>
TestInterestGroupManagerImpl::TakeRealTimeContributions() {
return std::move(real_time_contributions_);
}
std::vector<blink::InterestGroupKey>
TestInterestGroupManagerImpl::TakeInterestGroupsThatBid() {
return std::exchange(interest_groups_that_bid_, {});
}
std::vector<std::string> TestInterestGroupManagerImpl::TakeJoinedKAnonSets() {
return std::exchange(joined_k_anon_sets_, {});
}
std::optional<SingleStorageInterestGroup>
TestInterestGroupManagerImpl::BlockingGetInterestGroup(
const url::Origin& owner,
const std::string& name) {
base::RunLoop run_loop;
std::optional<SingleStorageInterestGroup> out;
GetInterestGroup(
{owner, name},
base::BindLambdaForTesting(
[&](std::optional<SingleStorageInterestGroup> interest_group) {
out = std::move(interest_group);
run_loop.Quit();
}));
run_loop.Run();
return out;
}
} // namespace content