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