| 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 23 matching lines...) Expand all  Loading... | 
|   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::QuicSimpleServer* g_quic_server = nullptr; |   45 net::QuicSimpleServer* g_quic_server = nullptr; | 
|   45 int g_quic_server_port = 0; |   46 int g_quic_server_port = 0; | 
|   46  |   47  | 
|   47 void SetupQuicInMemoryCache() { |   48 void SetupQuicInMemoryCache() { | 
|   48   static bool setup_done = false; |  | 
|   49   if (setup_done) |  | 
|   50     return; |  | 
|   51   setup_done = true; |  | 
|   52   net::SpdyHeaderBlock headers; |   49   net::SpdyHeaderBlock headers; | 
|   53   headers[kHelloHeaderName] = kHelloHeaderValue; |   50   headers[kHelloHeaderName] = kHelloHeaderValue; | 
|   54   headers[kStatusHeader] =  kHelloStatus; |   51   headers[kStatusHeader] =  kHelloStatus; | 
|   55   net::SpdyHeaderBlock trailers; |   52   net::SpdyHeaderBlock trailers; | 
|   56   trailers[kHelloTrailerName] = kHelloTrailerValue; |   53   trailers[kHelloTrailerName] = kHelloTrailerValue; | 
|   57   net::QuicInMemoryCache::GetInstance()->AddResponse( |   54   g_quic_in_memory_cache = new net::QuicInMemoryCache(); | 
|   58       base::StringPrintf("%s", kTestServerHost), |   55   g_quic_in_memory_cache->AddResponse(base::StringPrintf("%s", kTestServerHost), | 
|   59       kHelloPath, std::move(headers), kHelloBodyValue, std::move(trailers)); |   56                                       kHelloPath, std::move(headers), | 
 |   57                                       kHelloBodyValue, std::move(trailers)); | 
|   60 } |   58 } | 
|   61  |   59  | 
|   62 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |   60 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, | 
|   63                                    base::WaitableEvent* server_started_event) { |   61                                    base::WaitableEvent* server_started_event) { | 
|   64   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |   62   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 
|   65   DCHECK(!g_quic_server); |   63   DCHECK(!g_quic_server); | 
|   66  |   64  | 
|   67   net::QuicConfig config; |   65   net::QuicConfig config; | 
|   68   // Set up server certs. |   66   // Set up server certs. | 
|   69   base::FilePath directory; |   67   base::FilePath directory; | 
|   70   directory = test_files_root; |   68   directory = test_files_root; | 
|   71   std::unique_ptr<net::ProofSourceChromium> proof_source( |   69   std::unique_ptr<net::ProofSourceChromium> proof_source( | 
|   72       new net::ProofSourceChromium()); |   70       new net::ProofSourceChromium()); | 
|   73   CHECK(proof_source->Initialize( |   71   CHECK(proof_source->Initialize( | 
|   74       directory.AppendASCII("quic_test.example.com.crt"), |   72       directory.AppendASCII("quic_test.example.com.crt"), | 
|   75       directory.AppendASCII("quic_test.example.com.key.pkcs8"), |   73       directory.AppendASCII("quic_test.example.com.key.pkcs8"), | 
|   76       directory.AppendASCII("quic_test.example.com.key.sct"))); |   74       directory.AppendASCII("quic_test.example.com.key.sct"))); | 
|   77   g_quic_server = |   75  | 
|   78       new net::QuicSimpleServer(std::move(proof_source), config, |   76   SetupQuicInMemoryCache(); | 
|   79                                 net::QuicCryptoServerConfig::ConfigOptions(), |   77  | 
|   80                                 net::AllSupportedVersions()); |   78   g_quic_server = new net::QuicSimpleServer( | 
 |   79       std::move(proof_source), config, | 
 |   80       net::QuicCryptoServerConfig::ConfigOptions(), net::AllSupportedVersions(), | 
 |   81       g_quic_in_memory_cache); | 
|   81  |   82  | 
|   82   // Start listening on an unbound port. |   83   // Start listening on an unbound port. | 
|   83   int rv = g_quic_server->Listen( |   84   int rv = g_quic_server->Listen( | 
|   84       net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); |   85       net::IPEndPoint(net::IPAddress::IPv4AllZeros(), 0)); | 
|   85   CHECK_GE(rv, 0) << "Quic server fails to start"; |   86   CHECK_GE(rv, 0) << "Quic server fails to start"; | 
|   86   g_quic_server_port = g_quic_server->server_address().port(); |   87   g_quic_server_port = g_quic_server->server_address().port(); | 
|   87  |   88  | 
|   88   SetupQuicInMemoryCache(); |  | 
|   89  |  | 
|   90   server_started_event->Signal(); |   89   server_started_event->Signal(); | 
|   91 } |   90 } | 
|   92  |   91  | 
|   93 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |   92 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { | 
|   94   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |   93   DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 
|   95   g_quic_server->Shutdown(); |   94   g_quic_server->Shutdown(); | 
|   96   delete g_quic_server; |   95   delete g_quic_server; | 
|   97   g_quic_server = nullptr; |   96   g_quic_server = nullptr; | 
 |   97   delete g_quic_in_memory_cache; | 
 |   98   g_quic_in_memory_cache = nullptr; | 
|   98   server_stopped_event->Signal(); |   99   server_stopped_event->Signal(); | 
|   99 } |  100 } | 
|  100  |  101  | 
|  101 bool StartQuicTestServer() { |  102 bool StartQuicTestServer() { | 
|  102   LOG(INFO) << g_quic_server_thread; |  103   LOG(INFO) << g_quic_server_thread; | 
|  103   DCHECK(!g_quic_server_thread); |  104   DCHECK(!g_quic_server_thread); | 
|  104   g_quic_server_thread = new base::Thread("quic server thread"); |  105   g_quic_server_thread = new base::Thread("quic server thread"); | 
|  105   base::Thread::Options thread_options; |  106   base::Thread::Options thread_options; | 
|  106   thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |  107   thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 
|  107   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... | 
|  130   server_stopped_event.Wait(); |  131   server_stopped_event.Wait(); | 
|  131   delete g_quic_server_thread; |  132   delete g_quic_server_thread; | 
|  132   g_quic_server_thread = nullptr; |  133   g_quic_server_thread = nullptr; | 
|  133 } |  134 } | 
|  134  |  135  | 
|  135 int GetQuicTestServerPort() { |  136 int GetQuicTestServerPort() { | 
|  136   return g_quic_server_port; |  137   return g_quic_server_port; | 
|  137 } |  138 } | 
|  138  |  139  | 
|  139 }  // namespace grpc_support |  140 }  // namespace grpc_support | 
| OLD | NEW |