1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.net; 6 7 import android.content.Context; 8 9 import org.jni_zero.JNINamespace; 10 import org.jni_zero.NativeMethods; 11 12 import org.chromium.base.ContextUtils; 13 import org.chromium.base.test.util.UrlUtils; 14 15 /** Wrapper class to start a Quic test server. */ 16 @JNINamespace("cronet") 17 public final class QuicTestServer { 18 private static final String TAG = QuicTestServer.class.getSimpleName(); 19 20 private static final String CERT_USED = "quic-chain.pem"; 21 private static final String KEY_USED = "quic-leaf-cert.key"; 22 private static final String[] CERTS_USED = {CERT_USED}; 23 24 private static boolean sServerRunning; 25 26 /* 27 * Starts the server. 28 */ startQuicTestServer(Context context)29 public static void startQuicTestServer(Context context) { 30 if (sServerRunning) { 31 throw new IllegalStateException("Quic server is already running"); 32 } 33 TestFilesInstaller.installIfNeeded(context); 34 QuicTestServerJni.get() 35 .startQuicTestServer( 36 TestFilesInstaller.getInstalledPath(context), 37 UrlUtils.getIsolatedTestRoot()); 38 sServerRunning = true; 39 } 40 41 /** Shuts down the server. No-op if the server is already shut down. */ shutdownQuicTestServer()42 public static void shutdownQuicTestServer() { 43 if (!sServerRunning) { 44 return; 45 } 46 QuicTestServerJni.get().shutdownQuicTestServer(); 47 sServerRunning = false; 48 } 49 getServerURL()50 public static String getServerURL() { 51 return "https://" + getServerHost() + ":" + getServerPort(); 52 } 53 getServerHost()54 public static String getServerHost() { 55 return CronetTestUtil.QUIC_FAKE_HOST; 56 } 57 getServerPort()58 public static int getServerPort() { 59 return QuicTestServerJni.get().getServerPort(); 60 } 61 delayResponse(String path, int delayInSeconds)62 public static void delayResponse(String path, int delayInSeconds) { 63 QuicTestServerJni.get().delayResponse(path, delayInSeconds); 64 } 65 getServerCert()66 public static final String getServerCert() { 67 return CERT_USED; 68 } 69 getServerCertKey()70 public static final String getServerCertKey() { 71 return KEY_USED; 72 } 73 createMockCertVerifier()74 public static long createMockCertVerifier() { 75 TestFilesInstaller.installIfNeeded(ContextUtils.getApplicationContext()); 76 return MockCertVerifier.createMockCertVerifier(CERTS_USED, true); 77 } 78 79 @NativeMethods("cronet_tests") 80 interface Natives { 81 /* 82 * Runs a quic test server synchronously. 83 */ startQuicTestServer(String filePath, String testDataDir)84 void startQuicTestServer(String filePath, String testDataDir); 85 86 /* 87 * Shutdowns the quic test-server synchronously. 88 * 89 * Calling this without calling startQuicTestServer first will lead to unexpected 90 * behavior if not compiled in debug mode. 91 */ shutdownQuicTestServer()92 void shutdownQuicTestServer(); 93 getServerPort()94 int getServerPort(); 95 96 /* 97 * Responses for path will be delayed by delayInSeconds. 98 * 99 * Ideally this wouldn't take a delay. Instead, it should provide a synchronization 100 * mechanism that allows the caller to unblock the request. This would require changes all 101 * the way down to QUICHE though. 102 */ delayResponse(String path, int delayInSeconds)103 void delayResponse(String path, int delayInSeconds); 104 } 105 } 106