Chromium Code Reviews| 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 |
| 25 | 22 // (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and |
| 23 // the file served ( | |
| 24 // components/cronet/android/test/assets/test/quic_data/simple.txt). | |
|
xunjieli
2016/04/13 15:28:24
nit: simple.txt isn't used.
mef
2016/04/14 14:07:24
Done.
| |
| 25 const char kFakeQuicDomain[] = "test.example.com"; | |
| 26 static const int kServerPort = 6121; | 26 static const int kServerPort = 6121; |
| 27 | 27 |
| 28 base::Thread* g_quic_server_thread = nullptr; | 28 base::Thread* g_quic_server_thread = nullptr; |
| 29 net::QuicSimpleServer* g_quic_server = nullptr; | 29 net::QuicSimpleServer* g_quic_server = nullptr; |
| 30 | 30 |
| 31 void StartOnServerThread(const base::FilePath& test_files_root) { | 31 void StartQuicServerOnServerThread(const base::FilePath& test_files_root, |
| 32 base::WaitableEvent* server_started_event) { | |
| 32 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 33 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 33 DCHECK(!g_quic_server); | 34 DCHECK(!g_quic_server); |
| 34 | 35 |
| 35 // Set up in-memory cache. | 36 // Set up in-memory cache. |
| 37 /* | |
| 36 base::FilePath file_dir = test_files_root.Append("quic_data"); | 38 base::FilePath file_dir = test_files_root.Append("quic_data"); |
| 37 CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; | 39 CHECK(base::PathExists(file_dir)) << "Quic data does not exist"; |
| 38 net::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( | 40 net::QuicInMemoryCache::GetInstance()->InitializeFromDirectory( |
| 39 file_dir.value()); | 41 file_dir.value()); |
|
xunjieli
2016/04/13 15:28:24
If we can't serve files out of disk, can we add so
mef
2016/04/14 14:07:24
Great idea! I'll add that and more tests in the ne
| |
| 42 */ | |
| 40 net::IPAddressNumber ip; | 43 net::IPAddressNumber ip; |
| 41 net::ParseIPLiteralToNumber(kFakeQuicDomain, &ip); | 44 net::ParseIPLiteralToNumber(kFakeQuicDomain, &ip); |
| 42 net::QuicConfig config; | 45 net::QuicConfig config; |
| 43 | 46 |
| 44 // Set up server certs. | 47 // Set up server certs. |
| 45 base::FilePath directory; | 48 base::FilePath directory; |
| 46 CHECK(base::android::GetExternalStorageDirectory(&directory)); | 49 // CHECK(base::android::GetExternalStorageDirectory(&directory)); |
| 47 directory = directory.Append("net/data/ssl/certificates"); | 50 // directory = directory.Append("net/data/ssl/certificates"); |
| 51 directory = test_files_root; | |
| 48 // TODO(xunjieli): Use scoped_ptr when crbug.com/545474 is fixed. | 52 // TODO(xunjieli): Use scoped_ptr when crbug.com/545474 is fixed. |
| 49 net::ProofSourceChromium* proof_source = new net::ProofSourceChromium(); | 53 net::ProofSourceChromium* proof_source = new net::ProofSourceChromium(); |
| 50 CHECK(proof_source->Initialize( | 54 CHECK(proof_source->Initialize( |
| 51 directory.Append("quic_test.example.com.crt"), | 55 directory.Append("quic_test.example.com.crt"), |
| 52 directory.Append("quic_test.example.com.key.pkcs8"), | 56 directory.Append("quic_test.example.com.key.pkcs8"), |
| 53 directory.Append("quic_test.example.com.key.sct"))); | 57 directory.Append("quic_test.example.com.key.sct"))); |
| 54 g_quic_server = new net::QuicSimpleServer(proof_source, config, | 58 g_quic_server = new net::QuicSimpleServer(proof_source, config, |
| 55 net::QuicSupportedVersions()); | 59 net::QuicSupportedVersions()); |
| 56 | 60 |
| 57 // Start listening. | 61 // Start listening. |
| 58 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); | 62 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort)); |
| 59 CHECK_GE(rv, 0) << "Quic server fails to start"; | 63 CHECK_GE(rv, 0) << "Quic server fails to start"; |
| 60 JNIEnv* env = base::android::AttachCurrentThread(); | 64 server_started_event->Signal(); |
| 61 Java_QuicTestServer_onServerStarted(env); | |
| 62 } | 65 } |
| 63 | 66 |
| 64 void ShutdownOnServerThread() { | 67 void ShutdownOnServerThread(base::WaitableEvent* server_stopped_event) { |
| 65 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 68 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 66 g_quic_server->Shutdown(); | 69 g_quic_server->Shutdown(); |
| 67 delete g_quic_server; | 70 delete g_quic_server; |
| 71 g_quic_server = nullptr; | |
| 72 server_stopped_event->Signal(); | |
| 68 } | 73 } |
| 69 | 74 |
| 70 } // namespace | |
| 71 | |
| 72 // Quic server is currently hardcoded to run on port 6121 of the localhost on | 75 // Quic server is currently hardcoded to run on port 6121 of the localhost on |
| 73 // the device. | 76 // the device. |
| 74 void StartQuicTestServer(JNIEnv* env, | 77 bool StartQuicTestServer() { |
| 75 const JavaParamRef<jclass>& /*jcaller*/, | |
| 76 const JavaParamRef<jstring>& jtest_files_root) { | |
| 77 DCHECK(!g_quic_server_thread); | 78 DCHECK(!g_quic_server_thread); |
| 78 g_quic_server_thread = new base::Thread("quic server thread"); | 79 g_quic_server_thread = new base::Thread("quic server thread"); |
| 79 base::Thread::Options thread_options; | 80 base::Thread::Options thread_options; |
| 80 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 81 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 81 bool started = g_quic_server_thread->StartWithOptions(thread_options); | 82 bool started = g_quic_server_thread->StartWithOptions(thread_options); |
| 82 DCHECK(started); | 83 DCHECK(started); |
| 83 base::FilePath test_files_root( | 84 base::FilePath test_files_root; |
| 84 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 85 if (!PathService::Get(base::DIR_EXE, &test_files_root)) |
| 86 return false; | |
| 87 | |
| 88 base::WaitableEvent server_started_event(true, false); | |
| 85 g_quic_server_thread->task_runner()->PostTask( | 89 g_quic_server_thread->task_runner()->PostTask( |
| 86 FROM_HERE, base::Bind(&StartOnServerThread, test_files_root)); | 90 FROM_HERE, base::Bind(&StartQuicServerOnServerThread, test_files_root, |
| 91 &server_started_event)); | |
| 92 server_started_event.Wait(); | |
| 93 return true; | |
| 87 } | 94 } |
| 88 | 95 |
| 89 void ShutdownQuicTestServer(JNIEnv* env, | 96 void ShutdownQuicTestServer() { |
| 90 const JavaParamRef<jclass>& /*jcaller*/) { | |
| 91 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); | 97 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 98 base::WaitableEvent server_stopped_event(true, false); | |
| 92 g_quic_server_thread->task_runner()->PostTask( | 99 g_quic_server_thread->task_runner()->PostTask( |
| 93 FROM_HERE, base::Bind(&ShutdownOnServerThread)); | 100 FROM_HERE, base::Bind(&ShutdownOnServerThread, &server_stopped_event)); |
| 101 server_stopped_event.Wait(); | |
| 94 delete g_quic_server_thread; | 102 delete g_quic_server_thread; |
| 95 } | 103 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 } | 104 } |
| 110 | 105 |
| 111 } // namespace cronet | 106 } // namespace cronet |
| OLD | NEW |