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
content / browser / devtools / devtools_trust_token_browsertest.cc [blame]
// 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 <string_view>
#include "build/build_config.h"
#include "content/browser/devtools/protocol/devtools_protocol_test_support.h"
#include "content/browser/devtools/protocol/network.h"
#include "content/browser/network/trust_token_browsertest.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "services/network/public/cpp/features.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace content {
class DevToolsTrustTokenBrowsertest : public DevToolsProtocolTest,
public TrustTokenBrowsertest {
public:
void SetUpOnMainThread() override {
TrustTokenBrowsertest::SetUpOnMainThread();
DevToolsProtocolTest::SetUpOnMainThread();
}
void TearDownOnMainThread() override {
TrustTokenBrowsertest::TearDownOnMainThread();
DevToolsProtocolTest::TearDownOnMainThread();
}
// The returned view is only valid until the next |SendCommand| call.
const base::Value::List& GetTrustTokensViaProtocol() {
SendCommandSync("Storage.getTrustTokens");
const base::Value* tokens = result()->Find("tokens");
CHECK(tokens);
return tokens->GetList();
}
// Asserts that CDP reports |count| number of tokens for |issuerOrigin|.
void AssertTrustTokensViaProtocol(const std::string& issuerOrigin,
int expectedCount) {
const base::Value::List& tokens = GetTrustTokensViaProtocol();
EXPECT_GT(tokens.size(), 0ul);
for (const auto& token : tokens) {
const auto& token_dict = token.GetDict();
const std::string* issuer = token_dict.FindString("issuerOrigin");
if (*issuer == issuerOrigin) {
const std::optional<int> actualCount = token_dict.FindInt("count");
EXPECT_THAT(actualCount, ::testing::Optional(expectedCount));
return;
}
}
FAIL() << "No trust tokens for issuer " << issuerOrigin;
}
};
// After a successful issuance and redemption, a subsequent redemption against
// the same issuer should hit the redemption record (RR) cache.
IN_PROC_BROWSER_TEST_F(DevToolsTrustTokenBrowsertest,
RedemptionRecordCacheHitIsReportedAsLoadingFinished) {
ProvideRequestHandlerKeyCommitmentsToNetworkService({"a.test"});
// 1. Navigate to a test site, request and redeem a trust token.
ASSERT_TRUE(NavigateToURL(shell(), server_.GetURL("a.test", "/title1.html")));
EXPECT_EQ("Success",
EvalJs(shell(), JsReplace(R"(fetch($1,
{ privateToken: { version: 1, operation: 'token-request' } })
.then(()=>'Success'); )",
server_.GetURL("a.test", "/issue"))));
EXPECT_EQ("Success",
EvalJs(shell(), JsReplace(R"(fetch($1,
{ privateToken: { version: 1, operation: 'token-redemption' } })
.then(()=>'Success'); )",
server_.GetURL("a.test", "/redeem"))));
// 2) Open DevTools and enable Network domain.
Attach();
SendCommandSync("Network.enable");
// Make sure there are no existing DevTools events in the queue.
EXPECT_FALSE(HasExistingNotification());
// 3) Issue another redemption, and verify its served from cache.
EXPECT_EQ("NoModificationAllowedError",
EvalJs(shell(), JsReplace(R"(fetch($1,
{ privateToken: { version: 1, operation: 'token-redemption' } })
.catch(err => err.name); )",
server_.GetURL("a.test", "/redeem"))));
// 4) Verify the request is marked as successful and not as failed.
WaitForNotification("Network.requestServedFromCache", true);
WaitForNotification("Network.loadingFinished", true);
WaitForNotification("Network.trustTokenOperationDone", true);
}
namespace {
bool MatchStatus(const std::string& expected_status,
const base::Value::Dict& params) {
const std::string* actual_status = params.FindString("status");
return expected_status == *actual_status;
}
base::RepeatingCallback<bool(const base::Value::Dict&)> okStatusMatcher =
base::BindRepeating(
&MatchStatus,
protocol::Network::TrustTokenOperationDone::StatusEnum::Ok);
} // namespace
IN_PROC_BROWSER_TEST_F(DevToolsTrustTokenBrowsertest, FetchEndToEnd) {
ProvideRequestHandlerKeyCommitmentsToNetworkService({"a.test"});
// 1) Navigate to a test site.
GURL start_url = server_.GetURL("a.test", "/title1.html");
ASSERT_TRUE(NavigateToURL(shell(), start_url));
// 2) Open DevTools and enable Network domain.
Attach();
SendCommandSync("Network.enable");
// 3) Request and redeem a token, then use the redeemed token in a Signing
// request.
std::string command = R"(
(async () => {
await fetch('/issue', {privateToken: {version: 1,
operation: 'token-request'}});
await fetch('/redeem', {privateToken: {version: 1,
operation: 'token-redemption'}});
await fetch('/sign', {privateToken: {version: 1,
operation: 'send-redemption-record',
issuers: [$1]}});
return 'Success'; })(); )";
// We use EvalJs here, not ExecJs, because EvalJs waits for promises to
// resolve.
EXPECT_EQ(
"Success",
EvalJs(shell(), JsReplace(command, IssuanceOriginFromHost("a.test"))));
// 4) Verify that we received three successful events.
WaitForMatchingNotification("Network.trustTokenOperationDone",
okStatusMatcher);
WaitForMatchingNotification("Network.trustTokenOperationDone",
okStatusMatcher);
WaitForMatchingNotification("Network.trustTokenOperationDone",
okStatusMatcher);
}
IN_PROC_BROWSER_TEST_F(DevToolsTrustTokenBrowsertest, IframeEndToEnd) {
ProvideRequestHandlerKeyCommitmentsToNetworkService({"a.test"});
// 1) Navigate to a test site.
GURL start_url = server_.GetURL("a.test", "/page_with_iframe.html");
ASSERT_TRUE(NavigateToURL(shell(), start_url));
// 2) Open DevTools and enable Network domain.
Attach();
SendCommandSync("Network.enable");
// 3) Request and redeem a token, then use the redeemed token in a Signing
// request.
std::string command = R"(
(async () => {
await fetch('/issue', {privateToken: {version: 1,
operation: 'token-request'}});
await fetch('/redeem', {privateToken: {version: 1,
operation: 'token-redemption'}});
return 'Success'; })(); )";
// We use EvalJs here, not ExecJs, because EvalJs waits for promises to
// resolve.
EXPECT_EQ(
"Success",
EvalJs(shell(), JsReplace(command, IssuanceOriginFromHost("a.test"))));
// 3) Request and redeem a token, then use the redeemed token in a Signing
// request.
auto execute_op_via_iframe = [&](std::string_view path,
std::string_view trust_token) {
// It's important to set the trust token arguments before updating src, as
// the latter triggers a load.
EXPECT_TRUE(ExecJs(
shell(), JsReplace(
R"( const myFrame = document.getElementById('test_iframe');
myFrame.privateToken = $1;
myFrame.src = $2;)",
trust_token, path)));
TestNavigationObserver load_observer(shell()->web_contents());
load_observer.WaitForNavigationFinished();
};
execute_op_via_iframe("/sign", JsReplace(
R"({"version": 1,
"operation": "send-redemption-record",
"issuers": [$1]})",
IssuanceOriginFromHost("a.test")));
// 4) Verify that we received three successful events.
WaitForMatchingNotification("Network.trustTokenOperationDone",
okStatusMatcher);
WaitForMatchingNotification("Network.trustTokenOperationDone",
okStatusMatcher);
WaitForMatchingNotification("Network.trustTokenOperationDone",
okStatusMatcher);
}
// When the server rejects issuance, DevTools gets a failed notification.
IN_PROC_BROWSER_TEST_F(DevToolsTrustTokenBrowsertest,
FailedIssuanceFiresFailedOperationEvent) {
TrustTokenRequestHandler::Options options;
options.issuance_outcome =
TrustTokenRequestHandler::ServerOperationOutcome::kUnconditionalFailure;
request_handler_.UpdateOptions(std::move(options));
// 1) Navigate to a test site.
ProvideRequestHandlerKeyCommitmentsToNetworkService({"a.test"});
GURL start_url = server_.GetURL("a.test", "/title1.html");
ASSERT_TRUE(NavigateToURL(shell(), start_url));
// 2) Open DevTools and enable Network domain.
Attach();
SendCommandSync("Network.enable");
// 3) Request some Trust Tokens.
EXPECT_EQ("OperationError", EvalJs(shell(), R"(fetch('/issue',
{ privateToken: { version: 1, operation: 'token-request' } })
.then(()=>'Success').catch(err => err.name); )"));
// 4) Verify that we received an Trust Token operation failed event.
WaitForMatchingNotification(
"Network.trustTokenOperationDone",
base::BindRepeating(
&MatchStatus,
protocol::Network::TrustTokenOperationDone::StatusEnum::BadResponse));
}
IN_PROC_BROWSER_TEST_F(DevToolsTrustTokenBrowsertest, GetTrustTokens) {
ProvideRequestHandlerKeyCommitmentsToNetworkService({"a.test"});
// 1) Navigate to a test site.
GURL start_url = server_.GetURL("a.test", "/title1.html");
ASSERT_TRUE(NavigateToURL(shell(), start_url));
// 2) Open DevTools.
Attach();
// 3) Call Storage.getTrustTokens and expect none to be there.
EXPECT_EQ(GetTrustTokensViaProtocol().size(), 0ul);
// 4) Request some Trust Tokens.
std::string command = R"(
(async () => {
await fetch('/issue', {privateToken: {version: 1,
operation: 'token-request'}});
return 'Success'; })(); )";
// We use EvalJs here, not ExecJs, because EvalJs waits for promises to
// resolve.
EXPECT_EQ("Success", EvalJs(shell(), command));
// 5) Call Storage.getTrustTokens and expect a Trust Token to be there.
EXPECT_EQ(GetTrustTokensViaProtocol().size(), 1ul);
}
IN_PROC_BROWSER_TEST_F(DevToolsTrustTokenBrowsertest, ClearTrustTokens) {
ProvideRequestHandlerKeyCommitmentsToNetworkService({"a.test"});
// 1) Navigate to a test site.
GURL start_url = server_.GetURL("a.test", "/title1.html");
ASSERT_TRUE(NavigateToURL(shell(), start_url));
// 2) Open DevTools.
Attach();
// 3) Request some Trust Tokens.
std::string command = R"(
(async () => {
await fetch('/issue', {privateToken: {version: 1,
operation: 'token-request'}});
return 'Success'; })(); )";
// We use EvalJs here, not ExecJs, because EvalJs waits for promises to
// resolve.
EXPECT_EQ("Success", EvalJs(shell(), command));
// 4) Call Storage.getTrustTokens and expect a Trust Token to be there.
AssertTrustTokensViaProtocol(IssuanceOriginFromHost("a.test"), 10);
// 5) Call Storage.clearTrustTokens
base::Value::Dict params;
params.Set("issuerOrigin", IssuanceOriginFromHost("a.test"));
auto* result = SendCommandSync("Storage.clearTrustTokens", std::move(params));
EXPECT_THAT(result->FindBool("didDeleteTokens"), ::testing::Optional(true));
// 6) Call Storage.getTrustTokens and expect no Trust Tokens to be there.
// Note that we still get an entry for our 'issuerOrigin', but the actual
// Token count must be 0.
AssertTrustTokensViaProtocol(IssuanceOriginFromHost("a.test"), 0);
}
} // namespace content