| 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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 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 const char kTestServerSimpleUrl[] = "https://test.example.com/simple.txt"; |
| 44 const char kSimplePath[] = "/simple.txt"; |
| 45 const char kSimpleBodyValue[] = "Simple Hello from QUIC Server"; |
| 46 const char kSimpleStatus[] = "200"; |
| 47 |
| 48 const char kSimpleHeaderName[] = "hello_header"; |
| 49 const char kSimpleHeaderValue[] = "hello header value"; |
| 50 |
| 43 base::Thread* g_quic_server_thread = nullptr; | 51 base::Thread* g_quic_server_thread = nullptr; |
| 44 net::QuicHttpResponseCache* g_quic_response_cache = nullptr; | 52 net::QuicHttpResponseCache* g_quic_response_cache = nullptr; |
| 45 net::QuicSimpleServer* g_quic_server = nullptr; | 53 net::QuicSimpleServer* g_quic_server = nullptr; |
| 46 int g_quic_server_port = 0; | 54 int g_quic_server_port = 0; |
| 47 | 55 |
| 48 void SetupQuicHttpResponseCache() { | 56 void SetupQuicHttpResponseCache() { |
| 49 net::SpdyHeaderBlock headers; | 57 net::SpdyHeaderBlock headers; |
| 50 headers[kHelloHeaderName] = kHelloHeaderValue; | 58 headers[kHelloHeaderName] = kHelloHeaderValue; |
| 51 headers[kStatusHeader] = kHelloStatus; | 59 headers[kStatusHeader] = kHelloStatus; |
| 52 net::SpdyHeaderBlock trailers; | 60 net::SpdyHeaderBlock trailers; |
| 53 trailers[kHelloTrailerName] = kHelloTrailerValue; | 61 trailers[kHelloTrailerName] = kHelloTrailerValue; |
| 54 g_quic_response_cache = new net::QuicHttpResponseCache(); | 62 g_quic_response_cache = new net::QuicHttpResponseCache(); |
| 55 g_quic_response_cache->AddResponse(base::StringPrintf("%s", kTestServerHost), | 63 g_quic_response_cache->AddResponse(base::StringPrintf("%s", kTestServerHost), |
| 56 kHelloPath, std::move(headers), | 64 kHelloPath, std::move(headers), |
| 57 kHelloBodyValue, std::move(trailers)); | 65 kHelloBodyValue, std::move(trailers)); |
| 66 headers[kSimpleHeaderName] = kSimpleHeaderValue; |
| 67 headers[kStatusHeader] = kSimpleStatus; |
| 68 g_quic_response_cache->AddResponse(base::StringPrintf("%s", kTestServerHost), |
| 69 kSimplePath, std::move(headers), |
| 70 kSimpleBodyValue); |
| 58 } | 71 } |
| 59 | 72 |
| 60 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, | 73 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |
| 61 base::WaitableEvent* server_started_event) { | 74 base::WaitableEvent* server_started_event) { |
| 62 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 75 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 63 DCHECK(!g_quic_server); | 76 DCHECK(!g_quic_server); |
| 64 | 77 |
| 65 net::QuicConfig config; | 78 net::QuicConfig config; |
| 66 // Set up server certs. | 79 // Set up server certs. |
| 67 base::FilePath directory; | 80 base::FilePath directory; |
| 68 directory = test_files_root; | 81 directory = test_files_root; |
| 69 std::unique_ptr<net::ProofSourceChromium> proof_source( | 82 std::unique_ptr<net::ProofSourceChromium> proof_source( |
| 70 new net::ProofSourceChromium()); | 83 new net::ProofSourceChromium()); |
| 71 CHECK(proof_source->Initialize( | 84 CHECK(proof_source->Initialize( |
| 72 directory.AppendASCII("quic_test.example.com.crt"), | 85 directory.AppendASCII("quic_test.example.com.crt"), |
| 73 directory.AppendASCII("quic_test.example.com.key.pkcs8"), | 86 directory.AppendASCII("quic_test.example.com.key.pkcs8"), |
| 74 directory.AppendASCII("quic_test.example.com.key.sct"))); | 87 directory.AppendASCII("quic_test.example.com.key.sct"))); |
| 75 | |
| 76 SetupQuicHttpResponseCache(); | 88 SetupQuicHttpResponseCache(); |
| 77 | 89 |
| 78 g_quic_server = new net::QuicSimpleServer( | 90 g_quic_server = new net::QuicSimpleServer( |
| 79 std::move(proof_source), config, | 91 std::move(proof_source), config, |
| 80 net::QuicCryptoServerConfig::ConfigOptions(), net::AllSupportedVersions(), | 92 net::QuicCryptoServerConfig::ConfigOptions(), net::AllSupportedVersions(), |
| 81 g_quic_response_cache); | 93 g_quic_response_cache); |
| 82 | 94 |
| 83 // Start listening on an unbound port. | 95 // Start listening on an unbound port. |
| 84 int rv = g_quic_server->Listen( | 96 int rv = g_quic_server->Listen( |
| 85 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); | 97 net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); |
| 86 CHECK_GE(rv, 0) << "Quic server fails to start"; | 98 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 87 g_quic_server_port = g_quic_server->server_address().port(); | 99 g_quic_server_port = g_quic_server->server_address().port(); |
| 88 | |
| 89 server_started_event->Signal(); | 100 server_started_event->Signal(); |
| 90 } | 101 } |
| 91 | 102 |
| 92 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { | 103 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |
| 93 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 104 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 94 g_quic_server->Shutdown(); | 105 g_quic_server->Shutdown(); |
| 95 delete g_quic_server; | 106 delete g_quic_server; |
| 96 g_quic_server = nullptr; | 107 g_quic_server = nullptr; |
| 97 delete g_quic_response_cache; | 108 delete g_quic_response_cache; |
| 98 g_quic_response_cache = nullptr; | 109 g_quic_response_cache = nullptr; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 server_stopped_event.Wait(); | 142 server_stopped_event.Wait(); |
| 132 delete g_quic_server_thread; | 143 delete g_quic_server_thread; |
| 133 g_quic_server_thread = nullptr; | 144 g_quic_server_thread = nullptr; |
| 134 } | 145 } |
| 135 | 146 |
| 136 int GetQuicTestServerPort() { | 147 int GetQuicTestServerPort() { |
| 137 return g_quic_server_port; | 148 return g_quic_server_port; |
| 138 } | 149 } |
| 139 | 150 |
| 140 } // namespace grpc_support | 151 } // namespace grpc_support |
| OLD | NEW |