| 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/grpc_support/test/quic_test_server.h" | 5 #include "components/grpc_support/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/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "net/base/ip_address.h" | 16 #include "net/base/ip_address.h" |
| 17 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 18 #include "net/quic/chromium/crypto/proof_source_chromium.h" | 18 #include "net/quic/chromium/crypto/proof_source_chromium.h" |
| 19 #include "net/spdy/spdy_header_block.h" | 19 #include "net/spdy/spdy_header_block.h" |
| 20 #include "net/test/test_data_directory.h" | 20 #include "net/test/test_data_directory.h" |
| 21 #include "net/tools/quic/quic_in_memory_cache.h" | 21 #include "net/tools/quic/quic_http_response_cache.h" |
| 22 #include "net/tools/quic/quic_simple_server.h" | 22 #include "net/tools/quic/quic_simple_server.h" |
| 23 | 23 |
| 24 namespace grpc_support { | 24 namespace grpc_support { |
| 25 | 25 |
| 26 // This must match the certificate used (quic_test.example.com.crt and | 26 // This must match the certificate used (quic_test.example.com.crt and |
| 27 // quic_test.example.com.key.pkcs8). | 27 // quic_test.example.com.key.pkcs8). |
| 28 const char kTestServerHost[] = "test.example.com"; | 28 const char kTestServerHost[] = "test.example.com"; |
| 29 const char kTestServerUrl[] = "https://test.example.com/hello.txt"; | 29 const char kTestServerUrl[] = "https://test.example.com/hello.txt"; |
| 30 | 30 |
| 31 const char kStatusHeader[] = ":status"; | 31 const char kStatusHeader[] = ":status"; |
| 32 | 32 |
| 33 const char kHelloPath[] = "/hello.txt"; | 33 const char kHelloPath[] = "/hello.txt"; |
| 34 const char kHelloBodyValue[] = "Hello from QUIC Server"; | 34 const char kHelloBodyValue[] = "Hello from QUIC Server"; |
| 35 const char kHelloStatus[] = "200"; | 35 const char kHelloStatus[] = "200"; |
| 36 | 36 |
| 37 const char kHelloHeaderName[] = "hello_header"; | 37 const char kHelloHeaderName[] = "hello_header"; |
| 38 const char kHelloHeaderValue[] = "hello header value"; | 38 const char kHelloHeaderValue[] = "hello header value"; |
| 39 | 39 |
| 40 const char kHelloTrailerName[] = "hello_trailer"; | 40 const char kHelloTrailerName[] = "hello_trailer"; |
| 41 const char kHelloTrailerValue[] = "hello trailer value"; | 41 const char kHelloTrailerValue[] = "hello trailer value"; |
| 42 | 42 |
| 43 base::Thread* g_quic_server_thread = nullptr; | 43 base::Thread* g_quic_server_thread = nullptr; |
| 44 net::QuicInMemoryCache* g_quic_in_memory_cache = nullptr; | 44 net::QuicHttpResponseCache* g_quic_response_cache = nullptr; |
| 45 net::QuicSimpleServer* g_quic_server = nullptr; | 45 net::QuicSimpleServer* g_quic_server = nullptr; |
| 46 int g_quic_server_port = 0; | 46 int g_quic_server_port = 0; |
| 47 | 47 |
| 48 void SetupQuicInMemoryCache() { | 48 void SetupQuicHttpResponseCache() { |
| 49 net::SpdyHeaderBlock headers; | 49 net::SpdyHeaderBlock headers; |
| 50 headers[kHelloHeaderName] = kHelloHeaderValue; | 50 headers[kHelloHeaderName] = kHelloHeaderValue; |
| 51 headers[kStatusHeader] = kHelloStatus; | 51 headers[kStatusHeader] = kHelloStatus; |
| 52 net::SpdyHeaderBlock trailers; | 52 net::SpdyHeaderBlock trailers; |
| 53 trailers[kHelloTrailerName] = kHelloTrailerValue; | 53 trailers[kHelloTrailerName] = kHelloTrailerValue; |
| 54 g_quic_in_memory_cache = new net::QuicInMemoryCache(); | 54 g_quic_response_cache = new net::QuicHttpResponseCache(); |
| 55 g_quic_in_memory_cache->AddResponse(base::StringPrintf("%s", kTestServerHost), | 55 g_quic_response_cache->AddResponse(base::StringPrintf("%s", kTestServerHost), |
| 56 kHelloPath, std::move(headers), | 56 kHelloPath, std::move(headers), |
| 57 kHelloBodyValue, std::move(trailers)); | 57 kHelloBodyValue, std::move(trailers)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, | 60 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |
| 61 base::WaitableEvent* server_started_event) { | 61 base::WaitableEvent* server_started_event) { |
| 62 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 62 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 63 DCHECK(!g_quic_server); | 63 DCHECK(!g_quic_server); |
| 64 | 64 |
| 65 net::QuicConfig config; | 65 net::QuicConfig config; |
| 66 // Set up server certs. | 66 // Set up server certs. |
| 67 base::FilePath directory; | 67 base::FilePath directory; |
| 68 directory = test_files_root; | 68 directory = test_files_root; |
| 69 std::unique_ptr<net::ProofSourceChromium> proof_source( | 69 std::unique_ptr<net::ProofSourceChromium> proof_source( |
| 70 new net::ProofSourceChromium()); | 70 new net::ProofSourceChromium()); |
| 71 CHECK(proof_source->Initialize( | 71 CHECK(proof_source->Initialize( |
| 72 directory.AppendASCII("quic_test.example.com.crt"), | 72 directory.AppendASCII("quic_test.example.com.crt"), |
| 73 directory.AppendASCII("quic_test.example.com.key.pkcs8"), | 73 directory.AppendASCII("quic_test.example.com.key.pkcs8"), |
| 74 directory.AppendASCII("quic_test.example.com.key.sct"))); | 74 directory.AppendASCII("quic_test.example.com.key.sct"))); |
| 75 | 75 |
| 76 SetupQuicInMemoryCache(); | 76 SetupQuicHttpResponseCache(); |
| 77 | 77 |
| 78 g_quic_server = new net::QuicSimpleServer( | 78 g_quic_server = new net::QuicSimpleServer( |
| 79 std::move(proof_source), config, | 79 std::move(proof_source), config, |
| 80 net::QuicCryptoServerConfig::ConfigOptions(), net::AllSupportedVersions(), | 80 net::QuicCryptoServerConfig::ConfigOptions(), net::AllSupportedVersions(), |
| 81 g_quic_in_memory_cache); | 81 g_quic_response_cache); |
| 82 | 82 |
| 83 // Start listening on an unbound port. | 83 // Start listening on an unbound port. |
| 84 int rv = g_quic_server->Listen( | 84 int rv = g_quic_server->Listen( |
| 85 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); | 85 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); |
| 86 CHECK_GE(rv, 0) << "Quic server fails to start"; | 86 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 87 g_quic_server_port = g_quic_server->server_address().port(); | 87 g_quic_server_port = g_quic_server->server_address().port(); |
| 88 | 88 |
| 89 server_started_event->Signal(); | 89 server_started_event->Signal(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { | 92 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |
| 93 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 93 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 94 g_quic_server->Shutdown(); | 94 g_quic_server->Shutdown(); |
| 95 delete g_quic_server; | 95 delete g_quic_server; |
| 96 g_quic_server = nullptr; | 96 g_quic_server = nullptr; |
| 97 delete g_quic_in_memory_cache; | 97 delete g_quic_response_cache; |
| 98 g_quic_in_memory_cache = nullptr; | 98 g_quic_response_cache = nullptr; |
| 99 server_stopped_event->Signal(); | 99 server_stopped_event->Signal(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool StartQuicTestServer() { | 102 bool StartQuicTestServer() { |
| 103 LOG(INFO) << g_quic_server_thread; | 103 LOG(INFO) << g_quic_server_thread; |
| 104 DCHECK(!g_quic_server_thread); | 104 DCHECK(!g_quic_server_thread); |
| 105 g_quic_server_thread = new base::Thread("quic server thread"); | 105 g_quic_server_thread = new base::Thread("quic server thread"); |
| 106 base::Thread::Options thread_options; | 106 base::Thread::Options thread_options; |
| 107 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 107 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 108 bool started = g_quic_server_thread->StartWithOptions(thread_options); | 108 bool started = g_quic_server_thread->StartWithOptions(thread_options); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 131 server_stopped_event.Wait(); | 131 server_stopped_event.Wait(); |
| 132 delete g_quic_server_thread; | 132 delete g_quic_server_thread; |
| 133 g_quic_server_thread = nullptr; | 133 g_quic_server_thread = nullptr; |
| 134 } | 134 } |
| 135 | 135 |
| 136 int GetQuicTestServerPort() { | 136 int GetQuicTestServerPort() { |
| 137 return g_quic_server_port; | 137 return g_quic_server_port; |
| 138 } | 138 } |
| 139 | 139 |
| 140 } // namespace grpc_support | 140 } // namespace grpc_support |
| OLD | NEW |