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