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