| 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/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/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "net/base/ip_address.h" | 16 #include "net/base/ip_address.h" |
| 16 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 17 #include "net/quic/chromium/crypto/proof_source_chromium.h" | 18 #include "net/quic/chromium/crypto/proof_source_chromium.h" |
| 18 #include "net/spdy/spdy_header_block.h" | 19 #include "net/spdy/spdy_header_block.h" |
| 19 #include "net/test/test_data_directory.h" | 20 #include "net/test/test_data_directory.h" |
| 20 #include "net/tools/quic/quic_in_memory_cache.h" | 21 #include "net/tools/quic/quic_in_memory_cache.h" |
| 21 #include "net/tools/quic/quic_simple_server.h" | 22 #include "net/tools/quic/quic_simple_server.h" |
| 22 | 23 |
| 23 namespace cronet { | 24 namespace grpc_support { |
| 24 | 25 |
| 25 // 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 |
| 26 // quic_test.example.com.key.pkcs8). | 27 // quic_test.example.com.key.pkcs8). |
| 27 const char kTestServerDomain[] = "test.example.com"; | 28 const char kTestServerHost[] = "test.example.com"; |
| 28 const int kTestServerPort = 6121; | 29 const char kTestServerUrl[] = "https://test.example.com/hello.txt"; |
| 29 const char kTestServerHost[] = "test.example.com:6121"; | |
| 30 const char kTestServerUrl[] = "https://test.example.com:6121/hello.txt"; | |
| 31 | 30 |
| 32 const char kStatusHeader[] = ":status"; | 31 const char kStatusHeader[] = ":status"; |
| 33 | 32 |
| 34 const char kHelloPath[] = "/hello.txt"; | 33 const char kHelloPath[] = "/hello.txt"; |
| 35 const char kHelloBodyValue[] = "Hello from QUIC Server"; | 34 const char kHelloBodyValue[] = "Hello from QUIC Server"; |
| 36 const char kHelloStatus[] = "200"; | 35 const char kHelloStatus[] = "200"; |
| 37 | 36 |
| 38 const char kHelloHeaderName[] = "hello_header"; | 37 const char kHelloHeaderName[] = "hello_header"; |
| 39 const char kHelloHeaderValue[] = "hello header value"; | 38 const char kHelloHeaderValue[] = "hello header value"; |
| 40 | 39 |
| 41 const char kHelloTrailerName[] = "hello_trailer"; | 40 const char kHelloTrailerName[] = "hello_trailer"; |
| 42 const char kHelloTrailerValue[] = "hello trailer value"; | 41 const char kHelloTrailerValue[] = "hello trailer value"; |
| 43 | 42 |
| 44 base::Thread* g_quic_server_thread = nullptr; | 43 base::Thread* g_quic_server_thread = nullptr; |
| 45 net::QuicSimpleServer* g_quic_server = nullptr; | 44 net::QuicSimpleServer* g_quic_server = nullptr; |
| 45 int g_quic_server_port = 0; |
| 46 | 46 |
| 47 void SetupQuicInMemoryCache() { | 47 void SetupQuicInMemoryCache() { |
| 48 static bool setup_done = false; | 48 static bool setup_done = false; |
| 49 if (setup_done) | 49 if (setup_done) |
| 50 return; | 50 return; |
| 51 setup_done = true; | 51 setup_done = true; |
| 52 net::SpdyHeaderBlock headers; | 52 net::SpdyHeaderBlock headers; |
| 53 headers.AppendValueOrAddHeader(kHelloHeaderName, kHelloHeaderValue); | 53 headers[kHelloHeaderName] = kHelloHeaderValue; |
| 54 headers.AppendValueOrAddHeader(kStatusHeader, kHelloStatus); | 54 headers[kStatusHeader] = kHelloStatus; |
| 55 net::SpdyHeaderBlock trailers; | 55 net::SpdyHeaderBlock trailers; |
| 56 trailers.AppendValueOrAddHeader(kHelloTrailerName, kHelloTrailerValue); | 56 trailers[kHelloTrailerName] = kHelloTrailerValue; |
| 57 net::QuicInMemoryCache::GetInstance()->AddResponse( | 57 net::QuicInMemoryCache::GetInstance()->AddResponse( |
| 58 kTestServerHost, kHelloPath, std::move(headers), kHelloBodyValue, | 58 base::StringPrintf("%s", kTestServerHost), |
| 59 std::move(trailers)); | 59 kHelloPath, std::move(headers), kHelloBodyValue, std::move(trailers)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, | 62 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |
| 63 base::WaitableEvent* server_started_event) { | 63 base::WaitableEvent* server_started_event) { |
| 64 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 64 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 65 DCHECK(!g_quic_server); | 65 DCHECK(!g_quic_server); |
| 66 | 66 |
| 67 // Set up in-memory cache. | |
| 68 SetupQuicInMemoryCache(); | |
| 69 net::QuicConfig config; | 67 net::QuicConfig config; |
| 70 // Set up server certs. | 68 // Set up server certs. |
| 71 base::FilePath directory; | 69 base::FilePath directory; |
| 72 directory = test_files_root; | 70 directory = test_files_root; |
| 73 std::unique_ptr<net::ProofSourceChromium> proof_source( | 71 std::unique_ptr<net::ProofSourceChromium> proof_source( |
| 74 new net::ProofSourceChromium()); | 72 new net::ProofSourceChromium()); |
| 75 CHECK(proof_source->Initialize( | 73 CHECK(proof_source->Initialize( |
| 76 directory.Append("quic_test.example.com.crt"), | 74 directory.AppendASCII("quic_test.example.com.crt"), |
| 77 directory.Append("quic_test.example.com.key.pkcs8"), | 75 directory.AppendASCII("quic_test.example.com.key.pkcs8"), |
| 78 directory.Append("quic_test.example.com.key.sct"))); | 76 directory.AppendASCII("quic_test.example.com.key.sct"))); |
| 79 g_quic_server = | 77 g_quic_server = |
| 80 new net::QuicSimpleServer(std::move(proof_source), config, | 78 new net::QuicSimpleServer(std::move(proof_source), config, |
| 81 net::QuicCryptoServerConfig::ConfigOptions(), | 79 net::QuicCryptoServerConfig::ConfigOptions(), |
| 82 net::AllSupportedVersions()); | 80 net::AllSupportedVersions()); |
| 83 | 81 |
| 84 // Start listening. | 82 // Start listening on an unbound port. |
| 85 int rv = g_quic_server->Listen( | 83 int rv = g_quic_server->Listen( |
| 86 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), kTestServerPort)); | 84 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); |
| 87 CHECK_GE(rv, 0) << "Quic server fails to start"; | 85 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 86 g_quic_server_port = g_quic_server->server_address().port(); |
| 87 |
| 88 SetupQuicInMemoryCache(); |
| 89 |
| 88 server_started_event->Signal(); | 90 server_started_event->Signal(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { | 93 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |
| 92 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 94 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 93 g_quic_server->Shutdown(); | 95 g_quic_server->Shutdown(); |
| 94 delete g_quic_server; | 96 delete g_quic_server; |
| 95 g_quic_server = nullptr; | 97 g_quic_server = nullptr; |
| 96 server_stopped_event->Signal(); | 98 server_stopped_event->Signal(); |
| 97 } | 99 } |
| 98 | 100 |
| 99 // Quic server is currently hardcoded to run on port 6121 of the localhost on | |
| 100 // the device. | |
| 101 bool StartQuicTestServer() { | 101 bool StartQuicTestServer() { |
| 102 LOG(INFO) << g_quic_server_thread; |
| 102 DCHECK(!g_quic_server_thread); | 103 DCHECK(!g_quic_server_thread); |
| 103 g_quic_server_thread = new base::Thread("quic server thread"); | 104 g_quic_server_thread = new base::Thread("quic server thread"); |
| 104 base::Thread::Options thread_options; | 105 base::Thread::Options thread_options; |
| 105 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 106 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 106 bool started = g_quic_server_thread->StartWithOptions(thread_options); | 107 bool started = g_quic_server_thread->StartWithOptions(thread_options); |
| 107 DCHECK(started); | 108 DCHECK(started); |
| 108 base::FilePath test_files_root; | 109 base::FilePath test_files_root = net::GetTestCertsDirectory(); |
| 109 if (!PathService::Get(base::DIR_EXE, &test_files_root)) | 110 |
| 110 return false; | |
| 111 base::WaitableEvent server_started_event( | 111 base::WaitableEvent server_started_event( |
| 112 base::WaitableEvent::ResetPolicy::MANUAL, | 112 base::WaitableEvent::ResetPolicy::MANUAL, |
| 113 base::WaitableEvent::InitialState::NOT_SIGNALED); | 113 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 114 g_quic_server_thread->task_runner()->PostTask( | 114 g_quic_server_thread->task_runner()->PostTask( |
| 115 FROM_HERE, base::Bind(&StartQuicServerOnServerThread, | 115 FROM_HERE, base::Bind(&StartQuicServerOnServerThread, test_files_root, |
| 116 test_files_root.Append("net/data/ssl/certificates"), | |
| 117 &server_started_event)); | 116 &server_started_event)); |
| 118 server_started_event.Wait(); | 117 server_started_event.Wait(); |
| 119 return true; | 118 return true; |
| 120 } | 119 } |
| 121 | 120 |
| 122 void ShutdownQuicTestServer() { | 121 void ShutdownQuicTestServer() { |
| 123 if (!g_quic_server_thread) | 122 if (!g_quic_server_thread) |
| 124 return; | 123 return; |
| 125 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 124 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 126 base::WaitableEvent server_stopped_event( | 125 base::WaitableEvent server_stopped_event( |
| 127 base::WaitableEvent::ResetPolicy::MANUAL, | 126 base::WaitableEvent::ResetPolicy::MANUAL, |
| 128 base::WaitableEvent::InitialState::NOT_SIGNALED); | 127 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 129 g_quic_server_thread->task_runner()->PostTask( | 128 g_quic_server_thread->task_runner()->PostTask( |
| 130 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event)); | 129 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event)); |
| 131 server_stopped_event.Wait(); | 130 server_stopped_event.Wait(); |
| 132 delete g_quic_server_thread; | 131 delete g_quic_server_thread; |
| 133 g_quic_server_thread = nullptr; | 132 g_quic_server_thread = nullptr; |
| 134 } | 133 } |
| 135 | 134 |
| 136 } // namespace cronet | 135 int GetQuicTestServerPort() { |
| 136 return g_quic_server_port; |
| 137 } |
| 138 |
| 139 } // namespace grpc_support |
| OLD | NEW |