| OLD | NEW |
| 1 // Copyright 2015 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/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/android/path_utils.h" | |
| 10 #include "base/bind.h" | 7 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 14 #include "components/cronet/android/test/cronet_test_util.h" | |
| 15 #include "jni/QuicTestServer_jni.h" | |
| 16 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
| 17 #include "net/base/test_data_directory.h" | 14 #include "net/base/test_data_directory.h" |
| 18 #include "net/quic/crypto/proof_source_chromium.h" | 15 #include "net/quic/crypto/proof_source_chromium.h" |
| 19 #include "net/tools/quic/quic_in_memory_cache.h" | 16 #include "net/tools/quic/quic_in_memory_cache.h" |
| 20 #include "net/tools/quic/quic_simple_server.h" | 17 #include "net/tools/quic/quic_simple_server.h" |
| 21 | 18 |
| 22 namespace cronet { | 19 namespace cronet { |
| 23 | 20 |
| 24 namespace { | 21 // This must match the certificate used (quic_test.example.com.crt and |
| 25 | 22 // quic_test.example.com.key.pkcs8). |
| 23 const char kFakeQuicDomain[] = "test.example.com"; |
| 26 static const int kServerPort = 6121; | 24 static const int kServerPort = 6121; |
| 27 | 25 |
| 28 base::Thread* g_quic_server_thread = nullptr; | 26 base::Thread* g_quic_server_thread = nullptr; |
| 29 net::QuicSimpleServer* g_quic_server = nullptr; | 27 net::QuicSimpleServer* g_quic_server = nullptr; |
| 30 | 28 |
| 31 void StartOnServerThread(const base::FilePath& test_files_root) { | 29 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |
| 30 base::WaitableEvent* server_started_event) { |
| 32 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 31 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 33 DCHECK(!g_quic_server); | 32 DCHECK(!g_quic_server); |
| 34 | 33 |
| 35 // Set up in-memory cache. | 34 // Set up in-memory cache. |
| 35 /* |
| 36 base::FilePath file_dir = test_files_root.Append("quic_data"); | 36 base::FilePath file_dir = test_files_root.Append("quic_data"); |
| 37 CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; | 37 CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; |
| 38 net::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( | 38 net::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( |
| 39 file_dir.value()); | 39 file_dir.value()); |
| 40 */ |
| 40 net::IPAddressNumber ip; | 41 net::IPAddressNumber ip; |
| 41 net::ParseIPLiteralToNumber(kFakeQuicDomain, &ip); | 42 net::ParseIPLiteralToNumber(kFakeQuicDomain, &ip); |
| 42 net::QuicConfig config; | 43 net::QuicConfig config; |
| 43 | 44 |
| 44 // Set up server certs. | 45 // Set up server certs. |
| 45 base::FilePath directory; | 46 base::FilePath directory; |
| 46 CHECK(base::android::GetExternalStorageDirectory(&directory)); | 47 // CHECK(base::android::GetExternalStorageDirectory(&directory)); |
| 47 directory = directory.Append("net/data/ssl/certificates"); | 48 // directory = directory.Append("net/data/ssl/certificates"); |
| 49 directory = test_files_root; |
| 48 // TODO(xunjieli): Use scoped_ptr when crbug.com/545474 is fixed. | 50 // TODO(xunjieli): Use scoped_ptr when crbug.com/545474 is fixed. |
| 49 net::ProofSourceChromium* proof_source = new net::ProofSourceChromium(); | 51 net::ProofSourceChromium* proof_source = new net::ProofSourceChromium(); |
| 50 CHECK(proof_source->Initialize( | 52 CHECK(proof_source->Initialize( |
| 51 directory.Append("quic_test.example.com.crt"), | 53 directory.Append("quic_test.example.com.crt"), |
| 52 directory.Append("quic_test.example.com.key.pkcs8"), | 54 directory.Append("quic_test.example.com.key.pkcs8"), |
| 53 directory.Append("quic_test.example.com.key.sct"))); | 55 directory.Append("quic_test.example.com.key.sct"))); |
| 54 g_quic_server = new net::QuicSimpleServer(proof_source, config, | 56 g_quic_server = new net::QuicSimpleServer(proof_source, config, |
| 55 net::QuicSupportedVersions()); | 57 net::QuicSupportedVersions()); |
| 56 | 58 |
| 57 // Start listening. | 59 // Start listening. |
| 58 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); | 60 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); |
| 59 CHECK_GE(rv, 0) << "Quic server fails to start"; | 61 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 60 JNIEnv* env = base::android::AttachCurrentThread(); | 62 server_started_event->Signal(); |
| 61 Java_QuicTestServer_onServerStarted(env); | |
| 62 } | 63 } |
| 63 | 64 |
| 64 void ShutdownOnServerThread() { | 65 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |
| 65 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 66 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 66 g_quic_server->Shutdown(); | 67 g_quic_server->Shutdown(); |
| 67 delete g_quic_server; | 68 delete g_quic_server; |
| 69 g_quic_server = nullptr; |
| 70 server_stopped_event->Signal(); |
| 68 } | 71 } |
| 69 | 72 |
| 70 } // namespace | |
| 71 | |
| 72 // Quic server is currently hardcoded to run on port 6121 of the localhost on | 73 // Quic server is currently hardcoded to run on port 6121 of the localhost on |
| 73 // the device. | 74 // the device. |
| 74 void StartQuicTestServer(JNIEnv* env, | 75 bool StartQuicTestServer() { |
| 75 const JavaParamRef<jclass>& /*jcaller*/, | |
| 76 const JavaParamRef<jstring>& jtest_files_root) { | |
| 77 DCHECK(!g_quic_server_thread); | 76 DCHECK(!g_quic_server_thread); |
| 78 g_quic_server_thread = new base::Thread("quic server thread"); | 77 g_quic_server_thread = new base::Thread("quic server thread"); |
| 79 base::Thread::Options thread_options; | 78 base::Thread::Options thread_options; |
| 80 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 79 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 81 bool started = g_quic_server_thread->StartWithOptions(thread_options); | 80 bool started = g_quic_server_thread->StartWithOptions(thread_options); |
| 82 DCHECK(started); | 81 DCHECK(started); |
| 83 base::FilePath test_files_root( | 82 base::FilePath test_files_root; |
| 84 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 83 if (!PathService::Get(base::DIR_EXE, &test_files_root)) |
| 84 return false; |
| 85 |
| 86 base::WaitableEvent server_started_event(true, false); |
| 85 g_quic_server_thread->task_runner()->PostTask( | 87 g_quic_server_thread->task_runner()->PostTask( |
| 86 FROM_HERE, base::Bind(&StartOnServerThread, test_files_root)); | 88 FROM_HERE, base::Bind(&StartQuicServerOnServerThread, test_files_root, |
| 89 &server_started_event)); |
| 90 server_started_event.Wait(); |
| 91 return true; |
| 87 } | 92 } |
| 88 | 93 |
| 89 void ShutdownQuicTestServer(JNIEnv* env, | 94 void ShutdownQuicTestServer() { |
| 90 const JavaParamRef<jclass>& /*jcaller*/) { | |
| 91 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 95 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 96 base::WaitableEvent server_stopped_event(true, false); |
| 92 g_quic_server_thread->task_runner()->PostTask( | 97 g_quic_server_thread->task_runner()->PostTask( |
| 93 FROM_HERE, base::Bind(&ShutdownOnServerThread)); | 98 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event)); |
| 99 server_stopped_event.Wait(); |
| 94 delete g_quic_server_thread; | 100 delete g_quic_server_thread; |
| 95 } | 101 g_quic_server_thread = nullptr; |
| 96 | |
| 97 ScopedJavaLocalRef<jstring> GetServerHost( | |
| 98 JNIEnv* env, | |
| 99 const JavaParamRef<jclass>& /*jcaller*/) { | |
| 100 return base::android::ConvertUTF8ToJavaString(env, kFakeQuicDomain); | |
| 101 } | |
| 102 | |
| 103 int GetServerPort(JNIEnv* env, const JavaParamRef<jclass>& /*jcaller*/) { | |
| 104 return kServerPort; | |
| 105 } | |
| 106 | |
| 107 bool RegisterQuicTestServer(JNIEnv* env) { | |
| 108 return RegisterNativesImpl(env); | |
| 109 } | 102 } |
| 110 | 103 |
| 111 } // namespace cronet | 104 } // namespace cronet |
| OLD | NEW |