Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(693)

Side by Side Diff: net/url_request/url_request_quic_unittest.cc

Issue 2692813002: Server push cancellation: add a finch trial parameter (Closed)
Patch Set: sync with internal code Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
26 #include "net/tools/quic/quic_dispatcher.h"
23 #include "net/tools/quic/quic_http_response_cache.h" 27 #include "net/tools/quic/quic_http_response_cache.h"
28 #include "net/tools/quic/quic_simple_dispatcher.h"
24 #include "net/tools/quic/quic_simple_server.h" 29 #include "net/tools/quic/quic_simple_server.h"
25 #include "net/url_request/url_request.h" 30 #include "net/url_request/url_request.h"
26 #include "net/url_request/url_request_test_util.h" 31 #include "net/url_request/url_request_test_util.h"
27 #include "testing/gmock/include/gmock/gmock.h" 32 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
29 #include "url/gurl.h" 34 #include "url/gurl.h"
30 35
31 namespace net { 36 namespace net {
32 37
33 namespace { 38 namespace {
(...skipping 23 matching lines...) Expand all
57 verify_result.verified_cert = ImportCertFromFile( 62 verify_result.verified_cert = ImportCertFromFile(
58 GetTestCertsDirectory(), "quic_test_ecc.example.com.crt"); 63 GetTestCertsDirectory(), "quic_test_ecc.example.com.crt");
59 cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(), 64 cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(),
60 "test.example.com", verify_result, 65 "test.example.com", verify_result,
61 OK); 66 OK);
62 // To simplify the test, and avoid the race with the HTTP request, we force 67 // To simplify the test, and avoid the race with the HTTP request, we force
63 // QUIC for these requests. 68 // QUIC for these requests.
64 params->origins_to_force_quic_on.insert(HostPortPair(kTestServerHost, 443)); 69 params->origins_to_force_quic_on.insert(HostPortPair(kTestServerHost, 443));
65 params->cert_verifier = &cert_verifier_; 70 params->cert_verifier = &cert_verifier_;
66 params->enable_quic = true; 71 params->enable_quic = true;
72 params->enable_server_push_cancellation = true;
67 context_->set_host_resolver(host_resolver_.get()); 73 context_->set_host_resolver(host_resolver_.get());
68 context_->set_http_network_session_params(std::move(params)); 74 context_->set_http_network_session_params(std::move(params));
69 context_->set_cert_verifier(&cert_verifier_); 75 context_->set_cert_verifier(&cert_verifier_);
76 context_->set_net_log(&net_log_);
70 } 77 }
71 78
72 void TearDown() override { 79 void TearDown() override {
73 if (server_) 80 if (server_)
74 server_->Shutdown(); 81 server_->Shutdown();
75 } 82 }
76 83
77 // Sets a NetworkDelegate to use for |context_|. Must be done before Init(). 84 // Sets a NetworkDelegate to use for |context_|. Must be done before Init().
78 void SetNetworkDelegate(NetworkDelegate* network_delegate) { 85 void SetNetworkDelegate(NetworkDelegate* network_delegate) {
79 context_->set_network_delegate(network_delegate); 86 context_->set_network_delegate(network_delegate);
80 } 87 }
81 88
82 // Initializes the TestURLRequestContext |context_|. 89 // Initializes the TestURLRequestContext |context_|.
83 void Init() { context_->Init(); } 90 void Init() { context_->Init(); }
84 91
85 std::unique_ptr<URLRequest> CreateRequest(const GURL& url, 92 std::unique_ptr<URLRequest> CreateRequest(const GURL& url,
86 RequestPriority priority, 93 RequestPriority priority,
87 URLRequest::Delegate* delegate) { 94 URLRequest::Delegate* delegate) {
88 return context_->CreateRequest(url, priority, delegate); 95 return context_->CreateRequest(url, priority, delegate);
89 } 96 }
90 97
98 void ExtractNetLog(NetLogEventType type,
99 TestNetLogEntry::List* entry_list) const {
100 net::TestNetLogEntry::List entries;
101 net_log_.GetEntries(&entries);
102
103 for (const auto& entry : entries) {
104 if (entry.type == type)
105 entry_list->push_back(entry);
106 }
107 }
108
109 unsigned int GetRstErrorCountReceivedByServer(
110 QuicRstStreamErrorCode error_code) const {
111 return (static_cast<QuicSimpleDispatcher*>(server_->dispatcher()))
112 ->GetRstErrorCount(error_code);
113 }
114
91 private: 115 private:
92 void StartQuicServer() { 116 void StartQuicServer() {
93 // Set up in-memory cache. 117 // Set up in-memory cache.
94 response_cache_.AddSimpleResponse(kTestServerHost, kHelloPath, kHelloStatus, 118 response_cache_.AddSimpleResponse(kTestServerHost, kHelloPath, kHelloStatus,
95 kHelloBodyValue); 119 kHelloBodyValue);
120 response_cache_.InitializeFromDirectory(ServerPushCacheDirectory());
96 net::QuicConfig config; 121 net::QuicConfig config;
97 // Set up server certs. 122 // Set up server certs.
98 std::unique_ptr<net::ProofSourceChromium> proof_source( 123 std::unique_ptr<net::ProofSourceChromium> proof_source(
99 new net::ProofSourceChromium()); 124 new net::ProofSourceChromium());
100 base::FilePath directory = GetTestCertsDirectory(); 125 base::FilePath directory = GetTestCertsDirectory();
101 CHECK(proof_source->Initialize( 126 CHECK(proof_source->Initialize(
102 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.crt")), 127 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.crt")),
103 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.pkcs8")), 128 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.pkcs8")),
104 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.sct")))); 129 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.sct"))));
105 server_.reset(new QuicSimpleServer( 130 server_.reset(new QuicSimpleServer(
106 test::crypto_test_utils::ProofSourceForTesting(), config, 131 test::crypto_test_utils::ProofSourceForTesting(), config,
107 net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions(), 132 net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions(),
108 &response_cache_)); 133 &response_cache_));
109 int rv = server_->Listen( 134 int rv = server_->Listen(
110 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); 135 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort));
111 EXPECT_GE(rv, 0) << "Quic server fails to start"; 136 EXPECT_GE(rv, 0) << "Quic server fails to start";
112 137
113 std::unique_ptr<MockHostResolver> resolver(new MockHostResolver()); 138 std::unique_ptr<MockHostResolver> resolver(new MockHostResolver());
114 resolver->rules()->AddRule("test.example.com", "127.0.0.1"); 139 resolver->rules()->AddRule("test.example.com", "127.0.0.1");
115 host_resolver_.reset(new MappedHostResolver(std::move(resolver))); 140 host_resolver_.reset(new MappedHostResolver(std::move(resolver)));
116 // Use a mapped host resolver so that request for test.example.com 141 // Use a mapped host resolver so that request for test.example.com
117 // reach the server running on localhost. 142 // reach the server running on localhost.
118 std::string map_rule = "MAP test.example.com test.example.com:" + 143 std::string map_rule = "MAP test.example.com test.example.com:" +
119 base::IntToString(server_->server_address().port()); 144 base::IntToString(server_->server_address().port());
120 EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule)); 145 EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule));
121 } 146 }
122 147
148 std::string ServerPushCacheDirectory() {
149 base::FilePath path;
150 PathService::Get(base::DIR_SOURCE_ROOT, &path);
151 path = path.AppendASCII("net").AppendASCII("data").AppendASCII(
152 "quic_http_response_cache_data_with_push");
153 // The file path is known to be an ascii string.
154 return path.MaybeAsASCII();
155 }
156
123 std::unique_ptr<MappedHostResolver> host_resolver_; 157 std::unique_ptr<MappedHostResolver> host_resolver_;
124 std::unique_ptr<QuicSimpleServer> server_; 158 std::unique_ptr<QuicSimpleServer> server_;
125 std::unique_ptr<TestURLRequestContext> context_; 159 std::unique_ptr<TestURLRequestContext> context_;
160 TestNetLog net_log_;
126 QuicHttpResponseCache response_cache_; 161 QuicHttpResponseCache response_cache_;
127 MockCertVerifier cert_verifier_; 162 MockCertVerifier cert_verifier_;
128 }; 163 };
129 164
130 // A URLRequest::Delegate that checks LoadTimingInfo when response headers are 165 // A URLRequest::Delegate that checks LoadTimingInfo when response headers are
131 // received. 166 // received.
132 class CheckLoadTimingDelegate : public TestDelegate { 167 class CheckLoadTimingDelegate : public TestDelegate {
133 public: 168 public:
134 CheckLoadTimingDelegate(bool session_reused) 169 CheckLoadTimingDelegate(bool session_reused)
135 : session_reused_(session_reused) {} 170 : session_reused_(session_reused) {}
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); 239 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
205 240
206 request->Start(); 241 request->Start();
207 ASSERT_TRUE(request->is_pending()); 242 ASSERT_TRUE(request->is_pending());
208 base::RunLoop().Run(); 243 base::RunLoop().Run();
209 244
210 EXPECT_TRUE(request->status().is_success()); 245 EXPECT_TRUE(request->status().is_success());
211 EXPECT_EQ(kHelloBodyValue, delegate.data_received()); 246 EXPECT_EQ(kHelloBodyValue, delegate.data_received());
212 } 247 }
213 248
249 TEST_F(URLRequestQuicTest, CancelPushIfCached) {
250 base::RunLoop run_loop;
251 WaitForCompletionNetworkDelegate network_delegate(
252 run_loop.QuitClosure(), /*num_expected_requests=*/2);
253 SetNetworkDelegate(&network_delegate);
254
255 Init();
256 {
257 // Send a request to the pushed url: /kitten-1.jpg to pull the resource into
258 // cache.
259 CheckLoadTimingDelegate delegate(false);
260 std::string url =
261 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg");
262 std::unique_ptr<URLRequest> request =
263 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
264
265 request->Start();
266 ASSERT_TRUE(request->is_pending());
267 base::RunLoop().Run();
268
269 EXPECT_TRUE(request->status().is_success());
270 }
271
272 // Send a request to /index2.html which pushes /kitten-1.jpg and /favicon.ico.
273 // Should cancel push for /kitten-1.jpg.
274 CheckLoadTimingDelegate delegate(true);
275 std::string url =
276 base::StringPrintf("https://%s%s", kTestServerHost, "/index2.html");
277 std::unique_ptr<URLRequest> request =
278 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
279
280 request->Start();
281 ASSERT_TRUE(request->is_pending());
282 base::RunLoop().Run();
283
284 // Extract net logs on client side to verify push lookup transactions.
285 net::TestNetLogEntry::List entries;
286 ExtractNetLog(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, &entries);
287
288 EXPECT_EQ(4u, entries.size());
289
290 std::string value;
291 int net_error;
292 std::string push_url_1 =
293 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg");
294 std::string push_url_2 =
295 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico");
296
297 EXPECT_TRUE(entries[0].GetStringValue("push_url", &value));
298 EXPECT_EQ(value, push_url_1);
299 // No net error code for this lookup transaction, the push is found.
300 EXPECT_FALSE(entries[1].GetIntegerValue("net_error", &net_error));
301
302 EXPECT_TRUE(entries[2].GetStringValue("push_url", &value));
303 EXPECT_EQ(value, push_url_2);
304 // Net error code -400 is found for this lookup transaction, the push is not
305 // found in the cache.
306 EXPECT_TRUE(entries[3].GetIntegerValue("net_error", &net_error));
307 EXPECT_EQ(net_error, -400);
308
309 EXPECT_TRUE(request->status().is_success());
310
311 // Verify the reset error count received on the server side.
312 EXPECT_EQ(1u, GetRstErrorCountReceivedByServer(QUIC_STREAM_CANCELLED));
313 }
314
315 TEST_F(URLRequestQuicTest, CancelPushIfCached2) {
316 base::RunLoop run_loop;
317 WaitForCompletionNetworkDelegate network_delegate(
318 run_loop.QuitClosure(), /*num_expected_requests=*/3);
319 SetNetworkDelegate(&network_delegate);
320
321 Init();
322 {
323 // Send a request to the pushed url: /kitten-1.jpg to pull the resource into
324 // cache.
325 CheckLoadTimingDelegate delegate(false);
326 std::string url =
327 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg");
328 std::unique_ptr<URLRequest> request =
329 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
330
331 request->Start();
332 ASSERT_TRUE(request->is_pending());
333 base::RunLoop().Run();
334
335 EXPECT_TRUE(request->status().is_success());
336 EXPECT_NE(network_delegate.total_network_bytes_sent(), 0u);
337 }
338
339 {
340 // Send a request to the pushed url: /favicon.ico to pull the resource into
341 // cache.
342 CheckLoadTimingDelegate delegate(true);
343 std::string url =
344 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico");
345 std::unique_ptr<URLRequest> request =
346 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
347
348 request->Start();
349 ASSERT_TRUE(request->is_pending());
350 base::RunLoop().Run();
351
352 EXPECT_TRUE(request->status().is_success());
353 }
354
355 // Send a request to /index2.html which pushes /kitten-1.jpg and /favicon.ico.
356 // Should cancel push for /kitten-1.jpg.
357 CheckLoadTimingDelegate delegate(true);
358 std::string url =
359 base::StringPrintf("https://%s%s", kTestServerHost, "/index2.html");
360 std::unique_ptr<URLRequest> request =
361 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
362
363 request->Start();
364 ASSERT_TRUE(request->is_pending());
365 base::RunLoop().Run();
366
367 // Extract net logs on client side to verify push lookup transactions.
368 net::TestNetLogEntry::List entries;
369 ExtractNetLog(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, &entries);
370
371 EXPECT_EQ(4u, entries.size());
372
373 std::string value;
374 int net_error;
375 std::string push_url_1 =
376 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg");
377 std::string push_url_2 =
378 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico");
379
380 EXPECT_TRUE(entries[0].GetStringValue("push_url", &value));
381 EXPECT_EQ(value, push_url_1);
382 // No net error code for this lookup transaction, the push is found.
383 EXPECT_FALSE(entries[1].GetIntegerValue("net_error", &net_error));
384
385 EXPECT_TRUE(entries[2].GetStringValue("push_url", &value));
386 EXPECT_EQ(value, push_url_2);
387 // No net error code for this lookup transaction, the push is found.
388 EXPECT_FALSE(entries[3].GetIntegerValue("net_error", &net_error));
389
390 EXPECT_TRUE(request->status().is_success());
391
392 // Verify the reset error count received on the server side.
393 EXPECT_EQ(2u, GetRstErrorCountReceivedByServer(QUIC_STREAM_CANCELLED));
394 }
395
396 TEST_F(URLRequestQuicTest, DoNotCancelPushIfNotFoundInCache) {
397 base::RunLoop run_loop;
398 WaitForCompletionNetworkDelegate network_delegate(
399 run_loop.QuitClosure(), /*num_expected_requests=*/1);
400 SetNetworkDelegate(&network_delegate);
401
402 Init();
403 // Send a request to /index2.hmtl which pushes /kitten-1.jpg and /favicon.ico
404 // and shouldn't cancel any since neither is in cache.
405 CheckLoadTimingDelegate delegate(false);
406 std::string url =
407 base::StringPrintf("https://%s%s", kTestServerHost, "/index2.html");
408 std::unique_ptr<URLRequest> request =
409 CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate);
410
411 request->Start();
412 ASSERT_TRUE(request->is_pending());
413 base::RunLoop().Run();
414
415 // Extract net logs on client side to verify push lookup transactions.
416 net::TestNetLogEntry::List entries;
417 ExtractNetLog(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, &entries);
418
419 EXPECT_EQ(4u, entries.size());
420
421 std::string value;
422 int net_error;
423 std::string push_url_1 =
424 base::StringPrintf("https://%s%s", kTestServerHost, "/kitten-1.jpg");
425 std::string push_url_2 =
426 base::StringPrintf("https://%s%s", kTestServerHost, "/favicon.ico");
427
428 EXPECT_TRUE(entries[0].GetStringValue("push_url", &value));
429 EXPECT_EQ(value, push_url_1);
430 EXPECT_TRUE(entries[1].GetIntegerValue("net_error", &net_error));
431 EXPECT_EQ(net_error, -400);
432
433 EXPECT_TRUE(entries[2].GetStringValue("push_url", &value));
434 EXPECT_EQ(value, push_url_2);
435 EXPECT_TRUE(entries[3].GetIntegerValue("net_error", &net_error));
436 EXPECT_EQ(net_error, -400);
437
438 EXPECT_TRUE(request->status().is_success());
439
440 // Verify the reset error count received on the server side.
441 EXPECT_EQ(0u, GetRstErrorCountReceivedByServer(QUIC_STREAM_CANCELLED));
442 }
443
214 // Tests that if two requests use the same QUIC session, the second request 444 // Tests that if two requests use the same QUIC session, the second request
215 // should not have |LoadTimingInfo::connect_timing|. 445 // should not have |LoadTimingInfo::connect_timing|.
216 TEST_F(URLRequestQuicTest, TestTwoRequests) { 446 TEST_F(URLRequestQuicTest, TestTwoRequests) {
217 base::RunLoop run_loop; 447 base::RunLoop run_loop;
218 WaitForCompletionNetworkDelegate network_delegate( 448 WaitForCompletionNetworkDelegate network_delegate(
219 run_loop.QuitClosure(), /*num_expected_requests=*/2); 449 run_loop.QuitClosure(), /*num_expected_requests=*/2);
220 SetNetworkDelegate(&network_delegate); 450 SetNetworkDelegate(&network_delegate);
221 Init(); 451 Init();
222 CheckLoadTimingDelegate delegate(false); 452 CheckLoadTimingDelegate delegate(false);
223 delegate.set_quit_on_complete(false); 453 delegate.set_quit_on_complete(false);
(...skipping 12 matching lines...) Expand all
236 ASSERT_TRUE(request2->is_pending()); 466 ASSERT_TRUE(request2->is_pending());
237 run_loop.Run(); 467 run_loop.Run();
238 468
239 EXPECT_TRUE(request->status().is_success()); 469 EXPECT_TRUE(request->status().is_success());
240 EXPECT_TRUE(request2->status().is_success()); 470 EXPECT_TRUE(request2->status().is_success());
241 EXPECT_EQ(kHelloBodyValue, delegate.data_received()); 471 EXPECT_EQ(kHelloBodyValue, delegate.data_received());
242 EXPECT_EQ(kHelloBodyValue, delegate2.data_received()); 472 EXPECT_EQ(kHelloBodyValue, delegate2.data_received());
243 } 473 }
244 474
245 } // namespace net 475 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698