1 // Copyright 2014 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.net.Network; 8 9 import org.jni_zero.JNINamespace; 10 import org.jni_zero.NativeMethods; 11 import org.json.JSONException; 12 import org.json.JSONObject; 13 14 import org.chromium.net.impl.CronetEngineBuilderImpl; 15 import org.chromium.net.impl.CronetUrlRequest; 16 import org.chromium.net.impl.CronetUrlRequestContext; 17 18 /** Utilities for Cronet testing */ 19 @JNINamespace("cronet") 20 public class CronetTestUtil { 21 // QUIC test domain must match the certificate used 22 // (quic-chain.pem and quic-leaf-cert.key), and the file served ( 23 // components/cronet/android/test/assets/test/quic_data/simple.txt). 24 static final String QUIC_FAKE_HOST = "test.example.com"; 25 private static final String[] TEST_DOMAINS = {QUIC_FAKE_HOST}; 26 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; 27 28 /** 29 * Generates rules for customized DNS mapping for testing hostnames used by test servers, 30 * namely: 31 * <ul> 32 * <li>{@link QuicTestServer#getServerHost}</li> 33 * </ul> 34 * Maps the test hostnames to 127.0.0.1. 35 */ generateHostResolverRules()36 public static JSONObject generateHostResolverRules() throws JSONException { 37 return generateHostResolverRules(LOOPBACK_ADDRESS); 38 } 39 40 /** 41 * Generates rules for customized DNS mapping for testing hostnames used by test servers, 42 * namely: 43 * <ul> 44 * <li>{@link QuicTestServer#getServerHost}</li> 45 * </ul> 46 * @param destination host to map to 47 */ generateHostResolverRules(String destination)48 public static JSONObject generateHostResolverRules(String destination) throws JSONException { 49 StringBuilder rules = new StringBuilder(); 50 for (String domain : TEST_DOMAINS) { 51 rules.append("MAP " + domain + " " + destination + ","); 52 } 53 return new JSONObject().put("host_resolver_rules", rules); 54 } 55 56 /** Prepare {@code cronetEngine}'s network thread so libcronet_test code can run on it. */ 57 public static class NetworkThreadTestConnector { 58 private final CronetUrlRequestContext mRequestContext; 59 NetworkThreadTestConnector(CronetEngine cronetEngine)60 public NetworkThreadTestConnector(CronetEngine cronetEngine) { 61 mRequestContext = (CronetUrlRequestContext) cronetEngine; 62 CronetTestUtilJni.get() 63 .prepareNetworkThread(mRequestContext.getUrlRequestContextAdapter()); 64 } 65 shutdown()66 public void shutdown() { 67 CronetTestUtilJni.get() 68 .cleanupNetworkThread(mRequestContext.getUrlRequestContextAdapter()); 69 } 70 } 71 72 /** 73 * Returns the value of load flags in |urlRequest|. 74 * @param urlRequest is the UrlRequest object of interest. 75 */ getLoadFlags(UrlRequest urlRequest)76 public static int getLoadFlags(UrlRequest urlRequest) { 77 return CronetTestUtilJni.get() 78 .getLoadFlags(((CronetUrlRequest) urlRequest).getUrlRequestAdapterForTesting()); 79 } 80 doesURLRequestContextExistForTesting( CronetEngine engine, Network network)81 public static boolean doesURLRequestContextExistForTesting( 82 CronetEngine engine, Network network) { 83 CronetUrlRequestContext context = (CronetUrlRequestContext) engine; 84 return CronetTestUtilJni.get() 85 .uRLRequestContextExistsForTesting( 86 context.getUrlRequestContextAdapter(), network.getNetworkHandle()); 87 } 88 setMockCertVerifierForTesting( ExperimentalCronetEngine.Builder builder, long mockCertVerifier)89 public static void setMockCertVerifierForTesting( 90 ExperimentalCronetEngine.Builder builder, long mockCertVerifier) { 91 getCronetEngineBuilderImpl(builder).setMockCertVerifierForTesting(mockCertVerifier); 92 } 93 getCronetEngineBuilderImpl( ExperimentalCronetEngine.Builder builder)94 static CronetEngineBuilderImpl getCronetEngineBuilderImpl( 95 ExperimentalCronetEngine.Builder builder) { 96 return (CronetEngineBuilderImpl) 97 ((ExperimentalOptionsTranslatingCronetEngineBuilder) builder.getBuilderDelegate()) 98 .getDelegate(); 99 } 100 101 /** Returns whether the device supports calling nativeGetTaggedBytes(). */ nativeCanGetTaggedBytes()102 public static boolean nativeCanGetTaggedBytes() { 103 return CronetTestUtilJni.get().canGetTaggedBytes(); 104 } 105 106 /** 107 * Query the system to find out how many bytes were received with tag 108 * {@code expectedTag} for our UID. 109 * @param expectedTag the tag to query for. 110 * @return the count of received bytes. 111 */ nativeGetTaggedBytes(int expectedTag)112 public static long nativeGetTaggedBytes(int expectedTag) { 113 return CronetTestUtilJni.get().getTaggedBytes(expectedTag); 114 } 115 116 @NativeMethods("cronet_tests") 117 interface Natives { canGetTaggedBytes()118 boolean canGetTaggedBytes(); 119 getTaggedBytes(int expectedTag)120 long getTaggedBytes(int expectedTag); 121 getLoadFlags(long urlRequestAdapter)122 int getLoadFlags(long urlRequestAdapter); 123 prepareNetworkThread(long contextAdapter)124 void prepareNetworkThread(long contextAdapter); 125 cleanupNetworkThread(long contextAdapter)126 void cleanupNetworkThread(long contextAdapter); 127 uRLRequestContextExistsForTesting(long contextAdapter, long networkHandle)128 boolean uRLRequestContextExistsForTesting(long contextAdapter, long networkHandle); 129 } 130 } 131