Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <memory> | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "net/base/load_timing_info.h" | |
| 14 #include "net/cert/mock_cert_verifier.h" | |
| 15 #include "net/dns/mapped_host_resolver.h" | |
| 16 #include "net/dns/mock_host_resolver.h" | |
| 17 #include "net/quic/chromium/crypto/proof_source_chromium.h" | |
| 18 #include "net/quic/test_tools/crypto_test_utils.h" | |
| 19 #include "net/test/cert_test_util.h" | |
| 20 #include "net/test/gtest_util.h" | |
| 21 #include "net/test/test_data_directory.h" | |
| 22 #include "net/tools/quic/quic_in_memory_cache.h" | |
| 23 #include "net/tools/quic/quic_simple_server.h" | |
| 24 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" | |
| 25 #include "net/url_request/url_request.h" | |
| 26 #include "net/url_request/url_request_test_util.h" | |
| 27 #include "testing/gmock/include/gmock/gmock.h" | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 #include "url/gurl.h" | |
| 30 | |
| 31 namespace net { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 // This must match the certificate used (quic_test.example.com.crt and | |
| 36 // quic_test.example.com.key.pkcs8). | |
| 37 const int kTestServerPort = 6121; | |
| 38 const char kTestServerHost[] = "test.example.com:6121"; | |
| 39 // Used as a simple response from the server. | |
| 40 const char kHelloPath[] = "/hello.txt"; | |
| 41 const char kHelloBodyValue[] = "Hello from QUIC Server"; | |
| 42 const int kHelloStatus = 200; | |
| 43 | |
| 44 class URLRequestQuicTest : public ::testing::Test { | |
| 45 protected: | |
| 46 URLRequestQuicTest() : context_(new TestURLRequestContext(true)) { | |
| 47 StartQuicServer(); | |
| 48 | |
| 49 std::unique_ptr<HttpNetworkSession::Params> params( | |
| 50 new HttpNetworkSession::Params); | |
| 51 CertVerifyResult verify_result; | |
| 52 verify_result.verified_cert = ImportCertFromFile( | |
| 53 GetTestCertsDirectory(), "quic_test.example.com.crt"); | |
| 54 cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(), | |
| 55 "test.example.com", verify_result, | |
| 56 OK); | |
| 57 verify_result.verified_cert = ImportCertFromFile( | |
| 58 GetTestCertsDirectory(), "quic_test_ecc.example.com.crt"); | |
| 59 cert_verifier_.AddResultForCertAndHost(verify_result.verified_cert.get(), | |
| 60 "test.example.com", verify_result, | |
| 61 OK); | |
| 62 // To simplify the test, and avoid the race with the HTTP request, we force | |
| 63 // QUIC for these requests. | |
| 64 params->origins_to_force_quic_on.insert( | |
| 65 HostPortPair::FromString(kTestServerHost)); | |
| 66 params->cert_verifier = &cert_verifier_; | |
| 67 params->enable_quic = true; | |
| 68 params->host_resolver = host_resolver_.get(); | |
| 69 context_->set_http_network_session_params(std::move(params)); | |
| 70 context_->set_cert_verifier(&cert_verifier_); | |
| 71 context_->Init(); | |
| 72 } | |
| 73 | |
| 74 void TearDown() override { | |
| 75 if (server_) | |
| 76 server_->Shutdown(); | |
| 77 } | |
| 78 | |
| 79 std::unique_ptr<TestURLRequestContext> context_; | |
| 80 | |
| 81 private: | |
| 82 void StartQuicServer() { | |
| 83 // Set up in-memory cache. | |
| 84 test::QuicInMemoryCachePeer::ResetForTests(); | |
| 85 QuicInMemoryCache::GetInstance()->AddSimpleResponse( | |
| 86 kTestServerHost, kHelloPath, kHelloStatus, kHelloBodyValue); | |
| 87 net::QuicConfig config; | |
| 88 // Set up server certs. | |
| 89 std::unique_ptr<net::ProofSourceChromium> proof_source( | |
| 90 new net::ProofSourceChromium()); | |
| 91 base::FilePath directory = GetTestCertsDirectory(); | |
| 92 CHECK(proof_source->Initialize( | |
| 93 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.crt")), | |
| 94 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.pkcs8")), | |
| 95 directory.Append(FILE_PATH_LITERAL("quic_test.example.com.key.sct")))); | |
| 96 server_.reset(new QuicSimpleServer( | |
| 97 test::CryptoTestUtils::ProofSourceForTesting(), config, | |
| 98 net::QuicCryptoServerConfig::ConfigOptions(), AllSupportedVersions())); | |
| 99 int rv = server_->Listen( | |
| 100 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); | |
| 101 EXPECT_GE(rv, 0) << "Quic server fails to start"; | |
| 102 | |
| 103 std::unique_ptr<MockHostResolver> resolver(new MockHostResolver()); | |
| 104 resolver->rules()->AddRule("test.example.com", "127.0.0.1"); | |
| 105 host_resolver_.reset(new MappedHostResolver(std::move(resolver))); | |
| 106 // Use a mapped host resolver so that request for test.example.com (port 80) | |
| 107 // reach the server running on localhost. | |
| 108 std::string map_rule = "MAP test.example.com test.example.com:" + | |
| 109 base::IntToString(server_->server_address().port()); | |
| 110 EXPECT_TRUE(host_resolver_->AddRuleFromString(map_rule)); | |
| 111 } | |
| 112 | |
| 113 std::unique_ptr<MappedHostResolver> host_resolver_; | |
| 114 std::unique_ptr<QuicSimpleServer> server_; | |
| 115 MockCertVerifier cert_verifier_; | |
| 116 }; | |
| 117 | |
| 118 // A URLRequest::Delegate that checks LoadTimingInfo when response headers are | |
| 119 // received. | |
| 120 class CheckLoadTimingDelegate : public TestDelegate { | |
| 121 public: | |
| 122 CheckLoadTimingDelegate(bool session_reused) | |
| 123 : session_reused_(session_reused) {} | |
| 124 void OnResponseStarted(URLRequest* request, int error) override { | |
| 125 TestDelegate::OnResponseStarted(request, error); | |
| 126 LoadTimingInfo load_timing_info; | |
| 127 request->GetLoadTimingInfo(&load_timing_info); | |
| 128 assertLoadTimingValid(load_timing_info, session_reused_); | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 void assertLoadTimingValid(const LoadTimingInfo& load_timing_info, | |
| 133 bool session_reused) { | |
| 134 EXPECT_EQ(session_reused, load_timing_info.socket_reused); | |
| 135 | |
| 136 // If |session_reused| is true, these fields should all be null, non-null | |
| 137 // otherwise. | |
| 138 EXPECT_EQ(session_reused, | |
| 139 load_timing_info.connect_timing.connect_start.is_null()); | |
| 140 EXPECT_EQ(session_reused, | |
| 141 load_timing_info.connect_timing.connect_end.is_null()); | |
| 142 EXPECT_EQ(session_reused, | |
| 143 load_timing_info.connect_timing.ssl_start.is_null()); | |
| 144 EXPECT_EQ(session_reused, | |
| 145 load_timing_info.connect_timing.ssl_end.is_null()); | |
| 146 EXPECT_EQ(load_timing_info.connect_timing.connect_start, | |
| 147 load_timing_info.connect_timing.ssl_start); | |
| 148 EXPECT_EQ(load_timing_info.connect_timing.connect_end, | |
| 149 load_timing_info.connect_timing.ssl_end); | |
| 150 | |
| 151 EXPECT_EQ(session_reused, | |
| 152 load_timing_info.connect_timing.dns_start.is_null()); | |
| 153 EXPECT_EQ(session_reused, | |
| 154 load_timing_info.connect_timing.dns_end.is_null()); | |
| 155 } | |
| 156 | |
| 157 bool session_reused_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(CheckLoadTimingDelegate); | |
| 160 }; | |
| 161 | |
| 162 } // namespace | |
| 163 | |
| 164 TEST_F(URLRequestQuicTest, TestGetRequest) { | |
| 165 CheckLoadTimingDelegate delegate(false); | |
| 166 std::string url = | |
| 167 base::StringPrintf("https://%s%s", kTestServerHost, kHelloPath); | |
| 168 std::unique_ptr<URLRequest> request = | |
| 169 context_->CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
| 170 | |
| 171 request->Start(); | |
| 172 ASSERT_TRUE(request->is_pending()); | |
| 173 base::RunLoop().Run(); | |
| 174 | |
| 175 EXPECT_TRUE(request->status().is_success()); | |
| 176 } | |
| 177 | |
| 178 // Tests that if two requests use the same QUIC session, the second request | |
| 179 // should not have |LoadTimingInfo::connect_timing|. | |
| 180 TEST_F(URLRequestQuicTest, TestTwoRequests) { | |
| 181 CheckLoadTimingDelegate delegate(false); | |
|
Ryan Hamilton
2016/09/13 20:54:08
Thanks for doing this!! We have net/quic/chromium/
xunjieli
2016/09/14 15:07:46
No problem! Yes, I copied over the test fixture se
| |
| 182 std::string url = | |
| 183 base::StringPrintf("https://%s%s", kTestServerHost, kHelloPath); | |
| 184 std::unique_ptr<URLRequest> request = | |
| 185 context_->CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate); | |
| 186 | |
| 187 CheckLoadTimingDelegate delegate2(true); | |
| 188 std::unique_ptr<URLRequest> request2 = | |
| 189 context_->CreateRequest(GURL(url), DEFAULT_PRIORITY, &delegate2); | |
| 190 request->Start(); | |
| 191 request2->Start(); | |
| 192 ASSERT_TRUE(request->is_pending()); | |
| 193 base::RunLoop().Run(); | |
| 194 | |
| 195 EXPECT_TRUE(request->status().is_success()); | |
| 196 EXPECT_TRUE(request2->status().is_success()); | |
| 197 } | |
| 198 | |
| 199 } // namespace net | |
| OLD | NEW |