| 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 "components/cronet/ios/test/quic_test_server.h" | 5 #include "components/cronet/ios/test/quic_test_server.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "net/base/ip_address.h" | 15 #include "net/base/ip_address.h" |
| 16 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
| 17 #include "net/quic/chromium/crypto/proof_source_chromium.h" | 17 #include "net/quic/chromium/crypto/proof_source_chromium.h" |
| 18 #include "net/spdy/spdy_header_block.h" | 18 #include "net/spdy/spdy_header_block.h" |
| 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 20 #include "net/test/embedded_test_server/http_request.h" |
| 21 #include "net/test/embedded_test_server/http_response.h" |
| 19 #include "net/test/test_data_directory.h" | 22 #include "net/test/test_data_directory.h" |
| 20 #include "net/tools/quic/quic_in_memory_cache.h" | 23 #include "net/tools/quic/quic_in_memory_cache.h" |
| 21 #include "net/tools/quic/quic_simple_server.h" | 24 #include "net/tools/quic/quic_simple_server.h" |
| 22 | 25 |
| 23 namespace cronet { | 26 namespace cronet { |
| 24 | 27 |
| 25 // This must match the certificate used (quic_test.example.com.crt and | 28 // This must match the certificate used (quic_test.example.com.crt and |
| 26 // quic_test.example.com.key.pkcs8). | 29 // quic_test.example.com.key.pkcs8). |
| 27 const char kTestServerDomain[] = "test.example.com"; | 30 const char kTestServerDomain[] = "test.example.com"; |
| 28 const int kTestServerPort = 6121; | 31 const int kTestServerPort = 6121; |
| 29 const char kTestServerHost[] = "test.example.com:6121"; | 32 const char kTestServerHost[] = "test.example.com:6121"; |
| 30 const char kTestServerUrl[] = "https://test.example.com:6121/hello.txt"; | 33 const char kTestServerUrl[] = "https://test.example.com:6121/hello.txt"; |
| 31 | 34 |
| 32 const char kStatusHeader[] = ":status"; | 35 const char kStatusHeader[] = ":status"; |
| 33 | 36 |
| 34 const char kHelloPath[] = "/hello.txt"; | 37 const char kHelloPath[] = "/hello.txt"; |
| 35 const char kHelloBodyValue[] = "Hello from QUIC Server"; | 38 const char kHelloBodyValue[] = "Hello from QUIC Server"; |
| 36 const char kHelloStatus[] = "200"; | 39 const char kHelloStatus[] = "200"; |
| 37 | 40 |
| 38 const char kHelloHeaderName[] = "hello_header"; | 41 const char kHelloHeaderName[] = "hello_header"; |
| 39 const char kHelloHeaderValue[] = "hello header value"; | 42 const char kHelloHeaderValue[] = "hello header value"; |
| 40 | 43 |
| 41 const char kHelloTrailerName[] = "hello_trailer"; | 44 const char kHelloTrailerName[] = "hello_trailer"; |
| 42 const char kHelloTrailerValue[] = "hello trailer value"; | 45 const char kHelloTrailerValue[] = "hello trailer value"; |
| 43 | 46 |
| 44 base::Thread* g_quic_server_thread = nullptr; | 47 base::Thread* g_quic_server_thread = nullptr; |
| 45 net::QuicSimpleServer* g_quic_server = nullptr; | 48 net::QuicSimpleServer* g_quic_server = nullptr; |
| 49 net::EmbeddedTestServer* g_test_server = nullptr; |
| 46 | 50 |
| 47 void SetupQuicInMemoryCache() { | 51 void SetupQuicInMemoryCache() { |
| 48 static bool setup_done = false; | 52 static bool setup_done = false; |
| 49 if (setup_done) | 53 if (setup_done) |
| 50 return; | 54 return; |
| 51 setup_done = true; | 55 setup_done = true; |
| 52 net::SpdyHeaderBlock headers; | 56 net::SpdyHeaderBlock headers; |
| 53 headers.ReplaceOrAppendHeader(kHelloHeaderName, kHelloHeaderValue); | 57 headers.ReplaceOrAppendHeader(kHelloHeaderName, kHelloHeaderValue); |
| 54 headers.ReplaceOrAppendHeader(kStatusHeader, kHelloStatus); | 58 headers.ReplaceOrAppendHeader(kStatusHeader, kHelloStatus); |
| 55 net::SpdyHeaderBlock trailers; | 59 net::SpdyHeaderBlock trailers; |
| 56 trailers.ReplaceOrAppendHeader(kHelloTrailerName, kHelloTrailerValue); | 60 trailers.ReplaceOrAppendHeader(kHelloTrailerName, kHelloTrailerValue); |
| 57 net::QuicInMemoryCache::GetInstance()->AddResponse( | 61 net::QuicInMemoryCache::GetInstance()->AddResponse( |
| 58 kTestServerHost, kHelloPath, std::move(headers), kHelloBodyValue, | 62 kTestServerHost, kHelloPath, std::move(headers), kHelloBodyValue, |
| 59 std::move(trailers)); | 63 std::move(trailers)); |
| 60 } | 64 } |
| 61 | 65 |
| 66 std::unique_ptr<net::test_server::HttpResponse> EchoUserAgentHeaderInRequest( |
| 67 const net::test_server::HttpRequest& request) { |
| 68 auto it = request.headers.find("User-Agent"); |
| 69 DCHECK(it != request.headers.end()); |
| 70 |
| 71 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
| 72 new net::test_server::BasicHttpResponse()); |
| 73 http_response->set_content(it->second); |
| 74 return std::move(http_response); |
| 75 } |
| 76 |
| 62 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, | 77 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |
| 63 base::WaitableEvent* server_started_event) { | 78 base::WaitableEvent* server_started_event) { |
| 64 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 79 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 65 DCHECK(!g_quic_server); | 80 DCHECK(!g_quic_server); |
| 66 | 81 |
| 67 // Set up in-memory cache. | 82 // Set up in-memory cache. |
| 68 SetupQuicInMemoryCache(); | 83 SetupQuicInMemoryCache(); |
| 69 net::QuicConfig config; | 84 net::QuicConfig config; |
| 70 // Set up server certs. | 85 // Set up server certs. |
| 71 base::FilePath directory; | 86 base::FilePath directory; |
| 72 directory = test_files_root; | 87 directory = test_files_root; |
| 73 std::unique_ptr<net::ProofSourceChromium> proof_source( | 88 std::unique_ptr<net::ProofSourceChromium> proof_source( |
| 74 new net::ProofSourceChromium()); | 89 new net::ProofSourceChromium()); |
| 75 CHECK(proof_source->Initialize( | 90 CHECK(proof_source->Initialize( |
| 76 directory.Append("quic_test.example.com.crt"), | 91 directory.Append("quic_test.example.com.crt"), |
| 77 directory.Append("quic_test.example.com.key.pkcs8"), | 92 directory.Append("quic_test.example.com.key.pkcs8"), |
| 78 directory.Append("quic_test.example.com.key.sct"))); | 93 directory.Append("quic_test.example.com.key.sct"))); |
| 79 g_quic_server = | 94 g_quic_server = |
| 80 new net::QuicSimpleServer(std::move(proof_source), config, | 95 new net::QuicSimpleServer(std::move(proof_source), config, |
| 81 net::QuicCryptoServerConfig::ConfigOptions(), | 96 net::QuicCryptoServerConfig::ConfigOptions(), |
| 82 net::AllSupportedVersions()); | 97 net::AllSupportedVersions()); |
| 83 | 98 |
| 84 // Start listening. | 99 // Start listening. |
| 85 int rv = g_quic_server->Listen( | 100 int rv = g_quic_server->Listen( |
| 86 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); | 101 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); |
| 87 CHECK_GE(rv, 0) << "Quic server fails to start"; | 102 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 103 |
| 104 g_test_server = |
| 105 new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS); |
| 106 g_test_server->RegisterRequestHandler( |
| 107 base::Bind(&EchoUserAgentHeaderInRequest)); |
| 108 CHECK(g_test_server->Start()); |
| 88 server_started_event->Signal(); | 109 server_started_event->Signal(); |
| 89 } | 110 } |
| 90 | 111 |
| 91 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { | 112 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |
| 113 // TODO(mef): Delete g_test_server without hanging. |
| 114 // delete g_test_server; |
| 115 g_test_server = nullptr; |
| 116 |
| 92 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 117 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 93 g_quic_server->Shutdown(); | 118 g_quic_server->Shutdown(); |
| 94 delete g_quic_server; | 119 delete g_quic_server; |
| 95 g_quic_server = nullptr; | 120 g_quic_server = nullptr; |
| 121 |
| 96 server_stopped_event->Signal(); | 122 server_stopped_event->Signal(); |
| 97 } | 123 } |
| 98 | 124 |
| 99 // Quic server is currently hardcoded to run on port 6121 of the localhost on | 125 // Quic server is currently hardcoded to run on port 6121 of the localhost on |
| 100 // the device. | 126 // the device. |
| 101 bool StartQuicTestServer() { | 127 bool StartQuicTestServer() { |
| 102 DCHECK(!g_quic_server_thread); | 128 DCHECK(!g_quic_server_thread); |
| 103 g_quic_server_thread = new base::Thread("quic server thread"); | 129 g_quic_server_thread = new base::Thread("quic server thread"); |
| 104 base::Thread::Options thread_options; | 130 base::Thread::Options thread_options; |
| 105 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 131 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 126 base::WaitableEvent server_stopped_event( | 152 base::WaitableEvent server_stopped_event( |
| 127 base::WaitableEvent::ResetPolicy::MANUAL, | 153 base::WaitableEvent::ResetPolicy::MANUAL, |
| 128 base::WaitableEvent::InitialState::NOT_SIGNALED); | 154 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 129 g_quic_server_thread->task_runner()->PostTask( | 155 g_quic_server_thread->task_runner()->PostTask( |
| 130 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event)); | 156 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event)); |
| 131 server_stopped_event.Wait(); | 157 server_stopped_event.Wait(); |
| 132 delete g_quic_server_thread; | 158 delete g_quic_server_thread; |
| 133 g_quic_server_thread = nullptr; | 159 g_quic_server_thread = nullptr; |
| 134 } | 160 } |
| 135 | 161 |
| 162 std::string GetTestServerURL(const std::string& path) { |
| 163 DCHECK(g_test_server); |
| 164 return g_test_server->GetURL(path).spec(); |
| 165 } |
| 166 |
| 136 } // namespace cronet | 167 } // namespace cronet |
| OLD | NEW |