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
content / browser / private_aggregation / private_aggregation_internals_handler_impl_unittest.cc [blame]
// 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/private_aggregation/private_aggregation_internals_handler_impl.h"
#include <stddef.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/gmock_move_support.h"
#include "base/test/values_test_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/aggregation_service/aggregation_service_observer.h"
#include "content/browser/aggregation_service/aggregation_service_storage.h"
#include "content/browser/aggregation_service/aggregation_service_test_utils.h"
#include "content/browser/private_aggregation/private_aggregation_internals.mojom.h"
#include "content/browser/private_aggregation/private_aggregation_test_utils.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_web_ui.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
class MockObserver : public private_aggregation_internals::mojom::Observer {
public:
MOCK_METHOD(void, OnRequestStorageModified, (), (override));
MOCK_METHOD(
void,
OnReportHandled,
(private_aggregation_internals::mojom::WebUIAggregatableReportPtr report),
(override));
};
void VerifyWebUIAggregatableReport(
const private_aggregation_internals::mojom::WebUIAggregatableReport&
web_report,
const AggregatableReportRequest& request,
std::optional<AggregationServiceStorage::RequestId> id,
const std::optional<AggregatableReport>& report,
base::Time report_time,
private_aggregation_internals::mojom::ReportStatus status) {
EXPECT_EQ(web_report.id, id);
EXPECT_EQ(web_report.report_time,
report_time.InMillisecondsFSinceUnixEpoch());
EXPECT_EQ(web_report.api_identifier, request.shared_info().api_identifier);
EXPECT_EQ(web_report.api_version, request.shared_info().api_version);
EXPECT_EQ(web_report.report_url, request.GetReportingUrl());
EXPECT_TRUE(web_report.status == status);
ASSERT_EQ(web_report.contributions.size(),
request.payload_contents().contributions.size());
for (size_t i = 0; i < web_report.contributions.size(); ++i) {
EXPECT_EQ(web_report.contributions[i]->bucket,
request.payload_contents().contributions[i].bucket);
EXPECT_EQ(web_report.contributions[i]->value,
request.payload_contents().contributions[i].value);
}
base::Value report_body = base::test::ParseJson(web_report.report_body);
ASSERT_TRUE(report_body.is_dict());
const base::Value::Dict& report_body_dict = report_body.GetDict();
if (report) {
EXPECT_EQ(report_body_dict, report->GetAsJson());
} else {
const std::string* shared_info = report_body_dict.FindString("shared_info");
ASSERT_TRUE(shared_info);
EXPECT_EQ(*shared_info, request.shared_info().SerializeAsJson());
const std::string* payloads =
report_body_dict.FindString("aggregation_service_payloads");
ASSERT_TRUE(payloads);
EXPECT_EQ(*payloads, "Not generated prior to send");
}
}
} // namespace
class PrivateAggregationInternalsHandlerImplTest
: public RenderViewHostTestHarness {
public:
PrivateAggregationInternalsHandlerImplTest() : receiver_(&observer_) {}
protected:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
web_ui_.set_web_contents(web_contents());
StoragePartitionImpl* storage_partition_impl =
static_cast<StoragePartitionImpl*>(web_ui_.GetWebContents()
->GetBrowserContext()
->GetDefaultStoragePartition());
auto aggregation_service = std::make_unique<MockAggregationService>();
aggregation_service_ = aggregation_service.get();
storage_partition_impl->OverrideAggregationServiceForTesting(
std::move(aggregation_service));
auto private_aggregation_manager =
std::make_unique<MockPrivateAggregationManagerImpl>(
storage_partition_impl);
private_aggregation_manager_ = private_aggregation_manager.get();
storage_partition_impl->OverridePrivateAggregationManagerForTesting(
std::move(private_aggregation_manager));
internals_handler_ =
std::make_unique<PrivateAggregationInternalsHandlerImpl>(
&web_ui_, receiver_.BindNewPipeAndPassRemote(),
remote_handler_.BindNewPipeAndPassReceiver());
}
void TearDown() override {
// Resets `internals_handler_` to remove the observer from
// `aggregation_service_` before it's destroyed.
internals_handler_.reset();
aggregation_service_ = nullptr;
private_aggregation_manager_ = nullptr;
web_ui_.set_web_contents(nullptr);
RenderViewHostTestHarness::TearDown();
}
void OnRequestStorageModified() {
internals_handler_->OnRequestStorageModified();
}
void OnReportHandled(const AggregatableReportRequest& request,
std::optional<AggregationServiceStorage::RequestId> id,
const std::optional<AggregatableReport>& report,
base::Time actual_report_time,
AggregationServiceObserver::ReportStatus status) {
internals_handler_->OnReportHandled(request, id, report, actual_report_time,
status);
}
TestWebUI web_ui_;
raw_ptr<MockAggregationService> aggregation_service_;
raw_ptr<MockPrivateAggregationManagerImpl> private_aggregation_manager_;
mojo::Remote<private_aggregation_internals::mojom::Handler> remote_handler_;
MockObserver observer_;
mojo::Receiver<private_aggregation_internals::mojom::Observer> receiver_;
std::unique_ptr<PrivateAggregationInternalsHandlerImpl> internals_handler_;
};
TEST_F(PrivateAggregationInternalsHandlerImplTest, GetReports) {
AggregatableReportRequest request =
aggregation_service::CreateExampleRequest();
AggregationServiceStorage::RequestId id{20};
EXPECT_CALL(*aggregation_service_, GetPendingReportRequestsForWebUI)
.WillOnce([&](base::OnceCallback<void(
std::vector<AggregationServiceStorage::RequestAndId>)>
callback) {
std::move(callback).Run(
AggregatableReportRequestsAndIdsBuilder()
.AddRequestWithID(
aggregation_service::CloneReportRequest(request), id)
.Build());
});
base::RunLoop run_loop;
internals_handler_->GetReports(base::BindLambdaForTesting(
[&](std::vector<
private_aggregation_internals::mojom::WebUIAggregatableReportPtr>
reports) {
ASSERT_EQ(reports.size(), 1u);
VerifyWebUIAggregatableReport(
*reports.front(), request, id, /*report=*/std::nullopt,
request.shared_info().scheduled_report_time,
private_aggregation_internals::mojom::ReportStatus::kPending);
run_loop.Quit();
}));
run_loop.Run();
}
TEST_F(PrivateAggregationInternalsHandlerImplTest, SendReports) {
EXPECT_CALL(*aggregation_service_,
SendReportsForWebUI(
testing::ElementsAre(AggregationServiceStorage::RequestId(5)),
testing::_))
.WillOnce(base::test::RunOnceCallback<1>());
base::RunLoop run_loop;
internals_handler_->SendReports({AggregationServiceStorage::RequestId(5)},
run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(PrivateAggregationInternalsHandlerImplTest, ClearStorage) {
EXPECT_CALL(*aggregation_service_, ClearData)
.WillOnce(base::test::RunOnceCallback<3>());
EXPECT_CALL(*private_aggregation_manager_, ClearBudgetData)
.WillOnce(base::test::RunOnceCallback<3>());
base::RunLoop run_loop;
internals_handler_->ClearStorage(run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(PrivateAggregationInternalsHandlerImplTest, NotifyReportsChanged) {
base::RunLoop run_loop;
EXPECT_CALL(observer_, OnRequestStorageModified)
.WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
OnRequestStorageModified();
run_loop.Run();
}
TEST_F(PrivateAggregationInternalsHandlerImplTest, NotifyReportHandled) {
private_aggregation_internals::mojom::WebUIAggregatableReportPtr web_report;
base::RunLoop run_loop;
EXPECT_CALL(observer_, OnReportHandled)
.WillOnce(testing::DoAll(base::test::RunClosure(run_loop.QuitClosure()),
MoveArg<0>(&web_report)));
AggregatableReportRequest request =
aggregation_service::CreateExampleRequest();
AggregationServiceStorage::RequestId id{5};
aggregation_service::TestHpkeKey hpke_key{/*key_id=*/"id123"};
std::optional<AggregatableReport> report =
AggregatableReport::Provider().CreateFromRequestAndPublicKeys(
request, {hpke_key.GetPublicKey()});
base::Time now = base::Time::Now();
OnReportHandled(request, id, report, /*actual_report_time=*/now,
AggregationServiceObserver::ReportStatus::kSent);
run_loop.Run();
ASSERT_TRUE(web_report);
VerifyWebUIAggregatableReport(
*web_report, request, id, report, now,
private_aggregation_internals::mojom::ReportStatus::kSent);
}
TEST_F(PrivateAggregationInternalsHandlerImplTest, NotifyReportHandled_NoId) {
private_aggregation_internals::mojom::WebUIAggregatableReportPtr web_report;
base::RunLoop run_loop;
EXPECT_CALL(observer_, OnReportHandled)
.WillOnce(testing::DoAll(base::test::RunClosure(run_loop.QuitClosure()),
MoveArg<0>(&web_report)));
AggregatableReportRequest request =
aggregation_service::CreateExampleRequest();
aggregation_service::TestHpkeKey hpke_key{/*key_id=*/"id123"};
std::optional<AggregatableReport> report =
AggregatableReport::Provider().CreateFromRequestAndPublicKeys(
request, {hpke_key.GetPublicKey()});
base::Time now = base::Time::Now();
OnReportHandled(request, /*id=*/std::nullopt, report,
/*actual_report_time=*/now,
AggregationServiceObserver::ReportStatus::kSent);
run_loop.Run();
ASSERT_TRUE(web_report);
VerifyWebUIAggregatableReport(
*web_report, request, /*id=*/std::nullopt, report, now,
private_aggregation_internals::mojom::ReportStatus::kSent);
}
} // namespace content