| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "jni/QuicTestServer_jni.h" | 13 #include "jni/QuicTestServer_jni.h" |
| 14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
| 16 #include "net/quic/crypto/proof_source_chromium.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 namespace { | 22 namespace { |
| 22 | 23 |
| 23 static const char kServerHost[] = "127.0.0.1"; | 24 static const char kServerHost[] = "127.0.0.1"; |
| 24 static const int kServerPort = 6121; | 25 static const int kServerPort = 6121; |
| 25 | 26 |
| 26 base::Thread* g_quic_server_thread = nullptr; | 27 base::Thread* g_quic_server_thread = nullptr; |
| 27 net::tools::QuicSimpleServer* g_quic_server = nullptr; | 28 net::tools::QuicSimpleServer* g_quic_server = nullptr; |
| 28 | 29 |
| 29 void ServeFilesFromDirectory( | 30 net::ProofSource* CreateProofSource(const base::FilePath& cert_path, |
| 30 const base::FilePath& directory) { | 31 const base::FilePath& key_path) { |
| 32 VLOG(0) << "file: " << cert_path.value() |
| 33 << ", key_path: " << key_path.value(); |
| 34 net::ProofSourceChromium* proof_source = new net::ProofSourceChromium(); |
| 35 CHECK(proof_source->Initialize(cert_path, key_path)); |
| 36 return proof_source; |
| 37 } |
| 38 |
| 39 void StartOnServerThread(const base::FilePath& test_files_root) { |
| 31 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 40 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 32 DCHECK(!g_quic_server); | 41 DCHECK(!g_quic_server); |
| 33 base::FilePath file_dir = directory.Append("quic_data"); | 42 |
| 43 // Set up in-memory cache. |
| 44 base::FilePath file_dir = test_files_root.Append("quic_data"); |
| 34 CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; | 45 CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; |
| 35 net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( | 46 net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( |
| 36 file_dir.value()); | 47 file_dir.value()); |
| 37 net::IPAddressNumber ip; | 48 net::IPAddressNumber ip; |
| 38 net::ParseIPLiteralToNumber(kServerHost, &ip); | 49 net::ParseIPLiteralToNumber(kServerHost, &ip); |
| 39 net::QuicConfig config; | 50 net::QuicConfig config; |
| 40 g_quic_server = | 51 g_quic_server = |
| 41 new net::tools::QuicSimpleServer(config, net::QuicSupportedVersions()); | 52 new net::tools::QuicSimpleServer(config, net::QuicSupportedVersions()); |
| 53 |
| 54 // Set up server certs. |
| 55 g_quic_server->SetProofSource(CreateProofSource( |
| 56 test_files_root.Append("quic_certs").Append("leaf_cert.pem"), |
| 57 test_files_root.Append("quic_certs").Append("leaf_cert.pkcs8"))); |
| 58 |
| 59 // Start listening. |
| 42 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); | 60 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); |
| 43 CHECK_GE(rv, 0) << "Quic server fails to start"; | 61 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 44 JNIEnv* env = base::android::AttachCurrentThread(); | 62 JNIEnv* env = base::android::AttachCurrentThread(); |
| 45 Java_QuicTestServer_onServerStarted(env); | 63 Java_QuicTestServer_onServerStarted(env); |
| 46 } | 64 } |
| 47 | 65 |
| 48 void ShutdownOnServerThread() { | 66 void ShutdownOnServerThread() { |
| 49 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 67 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 50 g_quic_server->Shutdown(); | 68 g_quic_server->Shutdown(); |
| 51 delete g_quic_server; | 69 delete g_quic_server; |
| 52 } | 70 } |
| 53 | 71 |
| 54 } // namespace | 72 } // namespace |
| 55 | 73 |
| 56 // Quic server is currently hardcoded to run on port 6121 of the localhost on | 74 // Quic server is currently hardcoded to run on port 6121 of the localhost on |
| 57 // the device. | 75 // the device. |
| 58 void StartQuicTestServer(JNIEnv* env, | 76 void StartQuicTestServer(JNIEnv* env, |
| 59 const JavaParamRef<jclass>& /*jcaller*/, | 77 const JavaParamRef<jclass>& /*jcaller*/, |
| 60 const JavaParamRef<jstring>& jtest_files_root) { | 78 const JavaParamRef<jstring>& jtest_files_root) { |
| 61 DCHECK(!g_quic_server_thread); | 79 DCHECK(!g_quic_server_thread); |
| 62 g_quic_server_thread = new base::Thread("quic server thread"); | 80 g_quic_server_thread = new base::Thread("quic server thread"); |
| 63 base::Thread::Options thread_options; | 81 base::Thread::Options thread_options; |
| 64 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 82 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 65 bool started = g_quic_server_thread->StartWithOptions(thread_options); | 83 bool started = g_quic_server_thread->StartWithOptions(thread_options); |
| 66 DCHECK(started); | 84 DCHECK(started); |
| 67 base::FilePath test_files_root( | 85 base::FilePath test_files_root( |
| 68 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 86 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
| 69 g_quic_server_thread->task_runner()->PostTask( | 87 g_quic_server_thread->task_runner()->PostTask( |
| 70 FROM_HERE, base::Bind(&ServeFilesFromDirectory, test_files_root)); | 88 FROM_HERE, base::Bind(&StartOnServerThread, test_files_root)); |
| 71 } | 89 } |
| 72 | 90 |
| 73 void ShutdownQuicTestServer(JNIEnv* env, | 91 void ShutdownQuicTestServer(JNIEnv* env, |
| 74 const JavaParamRef<jclass>& /*jcaller*/) { | 92 const JavaParamRef<jclass>& /*jcaller*/) { |
| 75 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 93 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 76 g_quic_server_thread->task_runner()->PostTask( | 94 g_quic_server_thread->task_runner()->PostTask( |
| 77 FROM_HERE, base::Bind(&ShutdownOnServerThread)); | 95 FROM_HERE, base::Bind(&ShutdownOnServerThread)); |
| 78 delete g_quic_server_thread; | 96 delete g_quic_server_thread; |
| 79 } | 97 } |
| 80 | 98 |
| 81 ScopedJavaLocalRef<jstring> GetServerHost( | 99 ScopedJavaLocalRef<jstring> GetServerHost( |
| 82 JNIEnv* env, | 100 JNIEnv* env, |
| 83 const JavaParamRef<jclass>& /*jcaller*/) { | 101 const JavaParamRef<jclass>& /*jcaller*/) { |
| 84 return base::android::ConvertUTF8ToJavaString(env, kServerHost); | 102 return base::android::ConvertUTF8ToJavaString(env, kServerHost); |
| 85 } | 103 } |
| 86 | 104 |
| 87 int GetServerPort(JNIEnv* env, const JavaParamRef<jclass>& /*jcaller*/) { | 105 int GetServerPort(JNIEnv* env, const JavaParamRef<jclass>& /*jcaller*/) { |
| 88 return kServerPort; | 106 return kServerPort; |
| 89 } | 107 } |
| 90 | 108 |
| 91 bool RegisterQuicTestServer(JNIEnv* env) { | 109 bool RegisterQuicTestServer(JNIEnv* env) { |
| 92 return RegisterNativesImpl(env); | 110 return RegisterNativesImpl(env); |
| 93 } | 111 } |
| 94 | 112 |
| 95 } // namespace cronet | 113 } // namespace cronet |
| OLD | NEW |