OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "net/base/load_timing_info.h" | 13 #include "net/base/load_timing_info.h" |
14 #include "net/base/network_delegate.h" | 14 #include "net/base/network_delegate.h" |
15 #include "net/cert/mock_cert_verifier.h" | 15 #include "net/cert/mock_cert_verifier.h" |
16 #include "net/dns/mapped_host_resolver.h" | 16 #include "net/dns/mapped_host_resolver.h" |
17 #include "net/dns/mock_host_resolver.h" | 17 #include "net/dns/mock_host_resolver.h" |
18 #include "net/log/net_log_event_type.h" | |
19 #include "net/log/test_net_log.h" | |
20 #include "net/log/test_net_log_entry.h" | |
18 #include "net/quic/chromium/crypto/proof_source_chromium.h" | 21 #include "net/quic/chromium/crypto/proof_source_chromium.h" |
19 #include "net/quic/test_tools/crypto_test_utils.h" | 22 #include "net/quic/test_tools/crypto_test_utils.h" |
20 #include "net/test/cert_test_util.h" | 23 #include "net/test/cert_test_util.h" |
21 #include "net/test/gtest_util.h" | 24 #include "net/test/gtest_util.h" |
22 #include "net/test/test_data_directory.h" | 25 #include "net/test/test_data_directory.h" |
23 #include "net/tools/quic/quic_http_response_cache.h" | 26 #include "net/tools/quic/quic_http_response_cache.h" |
24 #include "net/tools/quic/quic_simple_server.h" | 27 #include "net/tools/quic/quic_simple_server.h" |
25 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
26 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
57 verify_result.verified_cert = ImportCertFromFile( | 60 verify_result.verified_cert = ImportCertFromFile( |
58 GetTestCertsDirectory(), "quic_test_ecc.example.com.crt"); | 61 GetTestCertsDirectory(), "quic_test_ecc.example.com.crt"); |
59 cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(), | 62 cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(), |
60 "test.example.com", verify_result, | 63 "test.example.com", verify_result, |
61 OK); | 64 OK); |
62 // To simplify the test, and avoid the race with the HTTP request, we force | 65 // To simplify the test, and avoid the race with the HTTP request, we force |
63 // QUIC for these requests. | 66 // QUIC for these requests. |
64 params->origins_to_force_quic_on.insert(HostPortPair(kTestServerHost, 443)); | 67 params->origins_to_force_quic_on.insert(HostPortPair(kTestServerHost, 443)); |
65 params->cert_verifier = &cert_verifier_; | 68 params->cert_verifier = &cert_verifier_; |
66 params->enable_quic = true; | 69 params->enable_quic = true; |
70 params->enable_server_push_cancellation = true; | |
67 context_->set_host_resolver(host_resolver_.get()); | 71 context_->set_host_resolver(host_resolver_.get()); |
68 context_->set_http_network_session_params(std::move(params)); | 72 context_->set_http_network_session_params(std::move(params)); |
69 context_->set_cert_verifier(&cert_verifier_); | 73 context_->set_cert_verifier(&cert_verifier_); |
74 context_->set_net_log(&net_log_); | |
70 } | 75 } |
71 | 76 |
72 void TearDown() override { | 77 void TearDown() override { |
73 if (server_) | 78 if (server_) |
74 server_->Shutdown(); | 79 server_->Shutdown(); |
75 } | 80 } |
76 | 81 |
77 // Sets a NetworkDelegate to use for |context_|. Must be done before Init(). | 82 // Sets a NetworkDelegate to use for |context_|. Must be done before Init(). |
78 void SetNetworkDelegate(NetworkDelegate* network_delegate) { | 83 void SetNetworkDelegate(NetworkDelegate* network_delegate) { |
79 context_->set_network_delegate(network_delegate); | 84 context_->set_network_delegate(network_delegate); |
80 } | 85 } |
81 | 86 |
82 // Initializes the TestURLRequestContext |context_|. | 87 // Initializes the TestURLRequestContext |context_|. |
83 void Init() { context_->Init(); } | 88 void Init() { context_->Init(); } |
84 | 89 |
85 std::unique_ptr<URLRequest> CreateRequest(const GURL& url, | 90 std::unique_ptr<URLRequest> CreateRequest(const GURL& url, |
86 RequestPriority priority, | 91 RequestPriority priority, |
87 URLRequest::Delegate* delegate) { | 92 URLRequest::Delegate* delegate) { |
88 return context_->CreateRequest(url, priority, delegate); | 93 return context_->CreateRequest(url, priority, delegate); |
89 } | 94 } |
90 | 95 |
96 void ExtractNetLog(NetLogEventType type, | |
97 TestNetLogEntry::List* entry_list) const { | |
98 net::TestNetLogEntry::List entries; | |
99 net_log_.GetEntries(&entries); | |
100 | |
101 for (const auto& entry : entries) { | |
102 if (entry.type == type) | |
103 entry_list->push_back(entry); | |
104 } | |
105 } | |
106 | |
91 private: | 107 private: |
92 void StartQuicServer() { | 108 void StartQuicServer() { |
93 // Set up in-memory cache. | 109 // Set up in-memory cache. |
94 response_cache_.AddSimpleResponse(kTestServerHost, kHelloPath, kHelloStatus, | 110 response_cache_.AddSimpleResponse(kTestServerHost, kHelloPath, kHelloStatus, |
95 kHelloBodyValue); | 111 kHelloBodyValue); |
112 response_cache_.InitializeFromDirectory(ServerPushCacheDirectory()); | |
96 net::QuicConfig config; | 113 net::QuicConfig config; |
97 // Set up server certs. | 114 // Set up server certs. |
98 std::unique_ptr<net::ProofSourceChromium> proof_source( | 115 std::unique_ptr<net::ProofSourceChromium> proof_source( |
99 new net::ProofSourceChromium()); | 116 new net::ProofSourceChromium()); |
100 base::FilePath directory = GetTestCertsDirectory(); | 117 base::FilePath directory = GetTestCertsDirectory(); |
101 CHECK(proof_source->Initialize( | 118 CHECK(proof_source->Initialize( |
102 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.crt")), | 119 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.crt")), |
103 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.pkcs8")), | 120 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.pkcs8")), |
104 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.sct")))); | 121 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.sct")))); |
105 server_.reset(new QuicSimpleServer( | 122 server_.reset(new QuicSimpleServer( |
106 test::crypto_test_utils::ProofSourceForTesting(), config, | 123 test::crypto_test_utils::ProofSourceForTesting(), config, |
107 net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions(), | 124 net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions(), |
108 &response_cache_)); | 125 &response_cache_)); |
109 int rv = server_->Listen( | 126 int rv = server_->Listen( |
110 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); | 127 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); |
111 EXPECT_GE(rv, 0) << "Quic server fails to start"; | 128 EXPECT_GE(rv, 0) << "Quic server fails to start"; |
112 | 129 |
113 std::unique_ptr<MockHostResolver> resolver(new MockHostResolver()); | 130 std::unique_ptr<MockHostResolver> resolver(new MockHostResolver()); |
114 resolver->rules()->AddRule("test.example.com", "127.0.0.1"); | 131 resolver->rules()->AddRule("test.example.com", "127.0.0.1"); |
115 host_resolver_.reset(new MappedHostResolver(std::move(resolver))); | 132 host_resolver_.reset(new MappedHostResolver(std::move(resolver))); |
116 // Use a mapped host resolver so that request for test.example.com | 133 // Use a mapped host resolver so that request for test.example.com |
117 // reach the server running on localhost. | 134 // reach the server running on localhost. |
118 std::string map_rule = "MAP test.example.com test.example.com:" + | 135 std::string map_rule = "MAP test.example.com test.example.com:" + |
119 base::IntToString(server_->server_address().port()); | 136 base::IntToString(server_->server_address().port()); |
120 EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule)); | 137 EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule)); |
121 } | 138 } |
122 | 139 |
140 std::string ServerPushCacheDirectory() { | |
141 base::FilePath path; | |
142 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
143 path = path.AppendASCII("net").AppendASCII("data").AppendASCII( | |
144 "quic_http_response_cache_data_with_push"); | |
145 // The file path is known to be an ascii string. | |
146 return path.MaybeAsASCII(); | |
147 } | |
148 | |
123 std::unique_ptr<MappedHostResolver> host_resolver_; | 149 std::unique_ptr<MappedHostResolver> host_resolver_; |
124 std::unique_ptr<QuicSimpleServer> server_; | 150 std::unique_ptr<QuicSimpleServer> server_; |
125 std::unique_ptr<TestURLRequestContext> context_; | 151 std::unique_ptr<TestURLRequestContext> context_; |
152 TestNetLog net_log_; | |
126 QuicHttpResponseCache response_cache_; | 153 QuicHttpResponseCache response_cache_; |
127 MockCertVerifier cert_verifier_; | 154 MockCertVerifier cert_verifier_; |
128 }; | 155 }; |
129 | 156 |
130 // A URLRequest::Delegate that checks LoadTimingInfo when response headers are | 157 // A URLRequest::Delegate that checks LoadTimingInfo when response headers are |
131 // received. | 158 // received. |
132 class CheckLoadTimingDelegate : public TestDelegate { | 159 class CheckLoadTimingDelegate : public TestDelegate { |
133 public: | 160 public: |
134 CheckLoadTimingDelegate(bool session_reused) | 161 CheckLoadTimingDelegate(bool session_reused) |
135 : session_reused_(session_reused) {} | 162 : session_reused_(session_reused) {} |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | 231 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); |
205 | 232 |
206 request->Start(); | 233 request->Start(); |
207 ASSERT_TRUE(request->is_pending()); | 234 ASSERT_TRUE(request->is_pending()); |
208 base::RunLoop().Run(); | 235 base::RunLoop().Run(); |
209 | 236 |
210 EXPECT_TRUE(request->status().is_success()); | 237 EXPECT_TRUE(request->status().is_success()); |
211 EXPECT_EQ(kHelloBodyValue, delegate.data_received()); | 238 EXPECT_EQ(kHelloBodyValue, delegate.data_received()); |
212 } | 239 } |
213 | 240 |
241 TEST_F(URLRequestQuicTest, CancelPushIfCached) { | |
Zhongyi Shi
2017/02/16 07:39:50
Currently the server push cancellation status is v
Ryan Hamilton
2017/02/16 15:18:08
One idea might be for the QUIC server to expose th
| |
242 base::RunLoop run_loop; | |
243 WaitForCompletionNetworkDelegate network_delegate( | |
244 run_loop.QuitClosure(), /*num_expected_requests=*/2); | |
245 SetNetworkDelegate(&network_delegate); | |
246 | |
247 Init(); | |
248 { | |
249 // Send a request to the pushed url: /kitten-1.jpg to pull the resource into | |
250 // cache. | |
251 CheckLoadTimingDelegate delegate(false); | |
252 std::string url = | |
253 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg"); | |
254 std::unique_ptr<URLRequest> request = | |
255 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
256 | |
257 request->Start(); | |
258 ASSERT_TRUE(request->is_pending()); | |
259 base::RunLoop().Run(); | |
260 | |
261 EXPECT_TRUE(request->status().is_success()); | |
262 } | |
263 | |
264 // Send a request to /index2.html which pushes /kitten-1.jpg and /favicon.ico. | |
265 // Should cancel push for /kitten-1.jpg. | |
266 CheckLoadTimingDelegate delegate(true); | |
267 std::string url = | |
268 base::StringPrintf("https://%s%s", kTestServerHost, "/index2.html"); | |
269 std::unique_ptr<URLRequest> request = | |
270 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
271 | |
272 request->Start(); | |
273 ASSERT_TRUE(request->is_pending()); | |
274 base::RunLoop().Run(); | |
275 | |
276 // Extract net logs to verify push lookup transactions. | |
277 net::TestNetLogEntry::List entries; | |
278 ExtractNetLog(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, &entries); | |
279 | |
280 EXPECT_EQ(4u, entries.size()); | |
281 | |
282 std::string value; | |
283 int net_error; | |
284 std::string push_url_1 = | |
285 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg"); | |
286 std::string push_url_2 = | |
287 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico"); | |
288 | |
289 EXPECT_TRUE(entries[0].GetStringValue("push_url", &value)); | |
290 EXPECT_EQ(value, push_url_1); | |
291 // No net error code for this lookup transaction, the push is found. | |
292 EXPECT_FALSE(entries[1].GetIntegerValue("net_error", &net_error)); | |
293 | |
294 EXPECT_TRUE(entries[2].GetStringValue("push_url", &value)); | |
295 EXPECT_EQ(value, push_url_2); | |
296 // Net error code -400 is found for this lookup transaction, the push is not | |
297 // found in the cache. | |
298 EXPECT_TRUE(entries[3].GetIntegerValue("net_error", &net_error)); | |
299 EXPECT_EQ(net_error, -400); | |
300 | |
301 EXPECT_TRUE(request->status().is_success()); | |
302 } | |
303 | |
304 TEST_F(URLRequestQuicTest, CancelPushIfCached2) { | |
305 base::RunLoop run_loop; | |
306 WaitForCompletionNetworkDelegate network_delegate( | |
307 run_loop.QuitClosure(), /*num_expected_requests=*/3); | |
308 SetNetworkDelegate(&network_delegate); | |
309 | |
310 Init(); | |
311 { | |
312 // Send a request to the pushed url: /kitten-1.jpg to pull the resource into | |
313 // cache. | |
314 CheckLoadTimingDelegate delegate(false); | |
315 std::string url = | |
316 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg"); | |
317 std::unique_ptr<URLRequest> request = | |
318 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
319 | |
320 request->Start(); | |
321 ASSERT_TRUE(request->is_pending()); | |
322 base::RunLoop().Run(); | |
323 | |
324 EXPECT_TRUE(request->status().is_success()); | |
325 EXPECT_NE(network_delegate.total_network_bytes_sent(), 0u); | |
326 } | |
327 | |
328 { | |
329 // Send a request to the pushed url: /favicon.ico to pull the resource into | |
330 // cache. | |
331 CheckLoadTimingDelegate delegate(true); | |
332 std::string url = | |
333 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico"); | |
334 std::unique_ptr<URLRequest> request = | |
335 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
336 | |
337 request->Start(); | |
338 ASSERT_TRUE(request->is_pending()); | |
339 base::RunLoop().Run(); | |
340 | |
341 EXPECT_TRUE(request->status().is_success()); | |
342 } | |
343 | |
344 // Send a request to /index2.html which pushes /kitten-1.jpg and /favicon.ico. | |
345 // Should cancel push for /kitten-1.jpg. | |
346 CheckLoadTimingDelegate delegate(true); | |
347 std::string url = | |
348 base::StringPrintf("https://%s%s", kTestServerHost, "/index2.html"); | |
349 std::unique_ptr<URLRequest> request = | |
350 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
351 | |
352 request->Start(); | |
353 ASSERT_TRUE(request->is_pending()); | |
354 base::RunLoop().Run(); | |
355 | |
356 // Extract net logs to verify push lookup transactions. | |
357 net::TestNetLogEntry::List entries; | |
358 ExtractNetLog(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, &entries); | |
359 | |
360 EXPECT_EQ(4u, entries.size()); | |
361 | |
362 std::string value; | |
363 int net_error; | |
364 std::string push_url_1 = | |
365 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg"); | |
366 std::string push_url_2 = | |
367 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico"); | |
368 | |
369 EXPECT_TRUE(entries[0].GetStringValue("push_url", &value)); | |
370 EXPECT_EQ(value, push_url_1); | |
371 // No net error code for this lookup transaction, the push is found. | |
372 EXPECT_FALSE(entries[1].GetIntegerValue("net_error", &net_error)); | |
373 | |
374 EXPECT_TRUE(entries[2].GetStringValue("push_url", &value)); | |
375 EXPECT_EQ(value, push_url_2); | |
376 // No net error code for this lookup transaction, the push is found. | |
377 EXPECT_FALSE(entries[3].GetIntegerValue("net_error", &net_error)); | |
378 | |
379 EXPECT_TRUE(request->status().is_success()); | |
380 } | |
381 | |
382 TEST_F(URLRequestQuicTest, DoNotCancelPushIfNotFoundInCache) { | |
383 base::RunLoop run_loop; | |
384 WaitForCompletionNetworkDelegate network_delegate( | |
385 run_loop.QuitClosure(), /*num_expected_requests=*/1); | |
386 SetNetworkDelegate(&network_delegate); | |
387 | |
388 Init(); | |
389 // Send a request to /index2.hmtl which pushes /kitten-1.jpg and /favicon.ico | |
390 // and shouldn't cancel any since neither is in cache. | |
391 CheckLoadTimingDelegate delegate(false); | |
392 std::string url = | |
393 base::StringPrintf("https://%s%s", kTestServerHost, "/index2.html"); | |
394 std::unique_ptr<URLRequest> request = | |
395 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
396 | |
397 request->Start(); | |
398 ASSERT_TRUE(request->is_pending()); | |
399 base::RunLoop().Run(); | |
400 | |
401 // Extract net logs to verify push lookup transactions. | |
402 net::TestNetLogEntry::List entries; | |
403 ExtractNetLog(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, &entries); | |
404 | |
405 EXPECT_EQ(4u, entries.size()); | |
406 | |
407 std::string value; | |
408 int net_error; | |
409 std::string push_url_1 = | |
410 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg"); | |
411 std::string push_url_2 = | |
412 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico"); | |
413 | |
414 EXPECT_TRUE(entries[0].GetStringValue("push_url", &value)); | |
415 EXPECT_EQ(value, push_url_1); | |
416 EXPECT_TRUE(entries[1].GetIntegerValue("net_error", &net_error)); | |
417 EXPECT_EQ(net_error, -400); | |
418 | |
419 EXPECT_TRUE(entries[2].GetStringValue("push_url", &value)); | |
420 EXPECT_EQ(value, push_url_2); | |
421 EXPECT_TRUE(entries[3].GetIntegerValue("net_error", &net_error)); | |
422 EXPECT_EQ(net_error, -400); | |
423 | |
424 EXPECT_TRUE(request->status().is_success()); | |
425 } | |
426 | |
214 // Tests that if two requests use the same QUIC session, the second request | 427 // Tests that if two requests use the same QUIC session, the second request |
215 // should not have |LoadTimingInfo::connect_timing|. | 428 // should not have |LoadTimingInfo::connect_timing|. |
216 TEST_F(URLRequestQuicTest, TestTwoRequests) { | 429 TEST_F(URLRequestQuicTest, TestTwoRequests) { |
217 base::RunLoop run_loop; | 430 base::RunLoop run_loop; |
218 WaitForCompletionNetworkDelegate network_delegate( | 431 WaitForCompletionNetworkDelegate network_delegate( |
219 run_loop.QuitClosure(), /*num_expected_requests=*/2); | 432 run_loop.QuitClosure(), /*num_expected_requests=*/2); |
220 SetNetworkDelegate(&network_delegate); | 433 SetNetworkDelegate(&network_delegate); |
221 Init(); | 434 Init(); |
222 CheckLoadTimingDelegate delegate(false); | 435 CheckLoadTimingDelegate delegate(false); |
223 delegate.set_quit_on_complete(false); | 436 delegate.set_quit_on_complete(false); |
(...skipping 12 matching lines...) Expand all Loading... | |
236 ASSERT_TRUE(request2->is_pending()); | 449 ASSERT_TRUE(request2->is_pending()); |
237 run_loop.Run(); | 450 run_loop.Run(); |
238 | 451 |
239 EXPECT_TRUE(request->status().is_success()); | 452 EXPECT_TRUE(request->status().is_success()); |
240 EXPECT_TRUE(request2->status().is_success()); | 453 EXPECT_TRUE(request2->status().is_success()); |
241 EXPECT_EQ(kHelloBodyValue, delegate.data_received()); | 454 EXPECT_EQ(kHelloBodyValue, delegate.data_received()); |
242 EXPECT_EQ(kHelloBodyValue, delegate2.data_received()); | 455 EXPECT_EQ(kHelloBodyValue, delegate2.data_received()); |
243 } | 456 } |
244 | 457 |
245 } // namespace net | 458 } // namespace net |
OLD | NEW |