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