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