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