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