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
content / browser / devtools / protocol / devtools_network_resource_loader_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 "base/files/file_util.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/devtools/protocol/devtools_network_resource_loader.h"
#include "content/browser/loader/url_loader_factory_utils.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/url_loader_factory_params_helper.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_paths.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/public/test/url_loader_monitor.h"
#include "content/shell/browser/shell.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/features.h"
#include "net/base/network_isolation_key.h"
#include "net/cookies/cookie_setting_override.h"
#include "net/cookies/site_for_cookies.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/cpp/resource_request.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
class DevtoolsNetworkResourceLoaderTest : public ContentBrowserTest {
public:
DevtoolsNetworkResourceLoaderTest() = default;
// ContentBrowserTest implementation:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
const GURL url1 = embedded_test_server()->GetURL("a.com", "/title1.html");
EXPECT_TRUE(NavigateToURL(web_contents(), url1));
EXPECT_TRUE(WaitForLoadStop(web_contents()));
base::FilePath test_source_map_path;
base::PathService::Get(content::DIR_TEST_DATA, &test_source_map_path);
test_source_map_path =
test_source_map_path.AppendASCII("devtools").AppendASCII("source.map");
base::ReadFileToString(test_source_map_path, &source_map_contents_);
}
const std::string& source_map_contents() { return source_map_contents_; }
WebContentsImpl* web_contents() const {
return static_cast<WebContentsImpl*>(shell()->web_contents());
}
RenderFrameHostImpl* current_frame_host() {
return web_contents()->GetPrimaryFrameTree().root()->current_frame_host();
}
mojo::Remote<network::mojom::URLLoaderFactory> CreateURLLoaderFactory() {
auto* frame = current_frame_host();
auto params = URLLoaderFactoryParamsHelper::CreateForFrame(
frame, frame->GetLastCommittedOrigin(),
frame->GetIsolationInfoForSubresources(),
frame->BuildClientSecurityState(),
/**coep_reporter=*/mojo::NullRemote(), frame->GetProcess(),
network::mojom::TrustTokenOperationPolicyVerdict::kForbid,
network::mojom::TrustTokenOperationPolicyVerdict::kForbid,
net::CookieSettingOverrides(), "DevtoolsNetworkResourceLoaderTest");
// Let DevTools fetch resources without CORS and ORB. Source maps are valid
// JSON and would otherwise require a CORS fetch + correct response headers.
// See BUG(chromium:1076435) for more context.
params->is_orb_enabled = false;
return mojo::Remote<network::mojom::URLLoaderFactory>(
url_loader_factory::CreatePendingRemote(
ContentBrowserClient::URLLoaderFactoryType::kDevTools,
url_loader_factory::TerminalParams::ForNetworkContext(
current_frame_host()
->GetProcess()
->GetStoragePartition()
->GetNetworkContext(),
std::move(params))));
}
// Repeats |number_of_error_A| times |error_A|, then continues with |error_B|.
static std::unique_ptr<content::URLLoaderInterceptor> SetupRequestFailForURL(
const GURL& url,
net::Error error) {
return std::make_unique<content::URLLoaderInterceptor>(
base::BindLambdaForTesting(
[error,
&url](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url != url) {
return false;
}
params->client->OnComplete(
network::URLLoaderCompletionStatus(error));
return true;
}));
}
static void CheckSuccess(DevtoolsNetworkResourceLoaderTest* test,
base::RunLoop* run_loop,
protocol::DevToolsNetworkResourceLoader* loader,
const net::HttpResponseHeaders* rh,
bool success,
int net_error,
std::string content) {
EXPECT_TRUE(success);
EXPECT_EQ(net_error, net::OK);
ASSERT_TRUE(rh);
EXPECT_EQ(rh->response_code(), 200);
EXPECT_EQ(content, test->source_map_contents());
size_t iterator = 0;
std::string name, value;
EXPECT_FALSE(rh->EnumerateHeaderLines(&iterator, &name, &value));
run_loop->Quit();
}
std::unique_ptr<protocol::DevToolsNetworkResourceLoader> CreateLoader(
GURL url,
protocol::DevToolsNetworkResourceLoader::Caching caching,
protocol::DevToolsNetworkResourceLoader::CompletionCallback callback) {
return protocol::DevToolsNetworkResourceLoader::Create(
CreateURLLoaderFactory(), std::move(url),
current_frame_host()->GetLastCommittedOrigin(),
current_frame_host()->ComputeSiteForCookies(), caching,
protocol::DevToolsNetworkResourceLoader::Credentials::kInclude,
std::move(callback));
}
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
IsolateAllSitesForTesting(command_line);
}
private:
base::test::ScopedFeatureList feature_list_;
std::string source_map_contents_;
};
// Tests that basic download works.
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest, BasicDownload) {
base::RunLoop run_loop;
const GURL source_map_url(
embedded_test_server()->GetURL("a.com", "/devtools/source.map"));
auto loader =
CreateLoader(source_map_url,
protocol::DevToolsNetworkResourceLoader::Caching::kDefault,
base::BindOnce(CheckSuccess, this, &run_loop));
run_loop.Run();
}
// This test is fetching a source map from a cross-origin URL. While the fetch
// isn't a CORS fetch, the source map is JSON, so this tests that ORB is
// disabled.
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest,
BasicDownloadCrossOrigin) {
base::RunLoop run_loop;
const GURL source_map_url(
embedded_test_server()->GetURL("b.com", "/devtools/source.map"));
auto loader =
CreateLoader(source_map_url,
protocol::DevToolsNetworkResourceLoader::Caching::kDefault,
base::BindOnce(CheckSuccess, this, &run_loop));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest,
BasicDownloadCrossOriginNoCache) {
base::RunLoop run_loop;
const GURL source_map_url(
embedded_test_server()->GetURL("b.com", "/devtools/source.map"));
URLLoaderMonitor monitor({source_map_url});
auto loader = CreateLoader(
source_map_url, protocol::DevToolsNetworkResourceLoader::Caching::kBypass,
base::BindOnce(CheckSuccess, this, &run_loop));
run_loop.Run();
std::optional<network::ResourceRequest> request =
monitor.GetRequestInfo(source_map_url);
EXPECT_TRUE(request->load_flags & net::LOAD_BYPASS_CACHE);
}
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest,
BasicDownloadCacheControl) {
base::RunLoop run_loop;
const GURL source_map_url(
embedded_test_server()->GetURL("a.com", "/echoheader?Cache-Control"));
auto complete_callback = base::BindOnce(
[](base::RunLoop* run_loop,
protocol::DevToolsNetworkResourceLoader* loader,
const net::HttpResponseHeaders* rh, bool success, int net_error,
std::string content) {
EXPECT_TRUE(success);
EXPECT_EQ(net_error, net::OK);
EXPECT_EQ(rh->response_code(), 200);
EXPECT_EQ(content, "no-cache");
// Not asserting headers for echoheader response.
run_loop->Quit();
},
&run_loop);
auto loader = CreateLoader(
source_map_url, protocol::DevToolsNetworkResourceLoader::Caching::kBypass,
std::move(complete_callback));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest, HTTPError404) {
base::RunLoop run_loop;
const GURL source_map_url(embedded_test_server()->GetURL("/page404.html"));
auto complete_callback = base::BindOnce(
[](base::RunLoop* run_loop,
protocol::DevToolsNetworkResourceLoader* loader,
const net::HttpResponseHeaders* rh, bool success, int net_error,
std::string content) {
EXPECT_FALSE(success);
ASSERT_EQ(net_error, net::ERR_HTTP_RESPONSE_CODE_FAILURE);
EXPECT_EQ(rh->response_code(), 404);
EXPECT_EQ(content, "");
size_t iterator = 0;
std::string name, value;
EXPECT_TRUE(rh->EnumerateHeaderLines(&iterator, &name, &value));
EXPECT_EQ(name, "Content-type");
EXPECT_EQ(value, "text/html");
EXPECT_FALSE(rh->EnumerateHeaderLines(&iterator, &name, &value));
run_loop->Quit();
},
&run_loop);
auto loader = CreateLoader(
source_map_url, protocol::DevToolsNetworkResourceLoader::Caching::kBypass,
std::move(complete_callback));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest, GenericFailure) {
const GURL source_map_url(
embedded_test_server()->GetURL("a.com", "/devtools/source.map"));
auto interceptor = SetupRequestFailForURL(source_map_url, net::ERR_FAILED);
base::RunLoop run_loop;
auto complete_callback = base::BindOnce(
[](base::RunLoop* run_loop,
protocol::DevToolsNetworkResourceLoader* loader,
const net::HttpResponseHeaders* rh, bool success, int net_error,
std::string content) {
EXPECT_FALSE(success);
EXPECT_EQ(net_error, net::ERR_FAILED);
EXPECT_EQ(content, "");
EXPECT_FALSE(rh);
run_loop->Quit();
},
&run_loop);
auto loader = CreateLoader(
source_map_url, protocol::DevToolsNetworkResourceLoader::Caching::kBypass,
std::move(complete_callback));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(DevtoolsNetworkResourceLoaderTest,
BasicDownloadRequestParams) {
RenderFrameHostImpl* frame = current_frame_host();
const GURL source_map_url(
embedded_test_server()->GetURL("a.com", "/devtools/source.map"));
URLLoaderMonitor monitor({source_map_url});
base::RunLoop run_loop;
auto loader =
CreateLoader(source_map_url,
protocol::DevToolsNetworkResourceLoader::Caching::kDefault,
base::BindOnce(CheckSuccess, this, &run_loop));
run_loop.Run();
std::optional<network::ResourceRequest> request =
monitor.GetRequestInfo(source_map_url);
EXPECT_TRUE(
frame->ComputeSiteForCookies().IsEquivalent(request->site_for_cookies));
EXPECT_EQ(frame->GetLastCommittedOrigin(), request->request_initiator);
EXPECT_FALSE(request->load_flags & net::LOAD_BYPASS_CACHE);
}
} // namespace content