OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
8 | 8 |
9 import org.chromium.base.annotations.CalledByNative; | |
10 import org.chromium.base.annotations.JNINamespace; | 9 import org.chromium.base.annotations.JNINamespace; |
11 import org.chromium.net.impl.ChromiumUrlRequestFactory; | |
12 import org.chromium.net.impl.CronetUrlRequest; | 10 import org.chromium.net.impl.CronetUrlRequest; |
13 import org.chromium.net.impl.CronetUrlRequestContext; | 11 import org.json.JSONException; |
| 12 import org.json.JSONObject; |
14 | 13 |
15 /** | 14 /** |
16 * Utilities for Cronet testing | 15 * Utilities for Cronet testing |
17 */ | 16 */ |
18 @JNINamespace("cronet") | 17 @JNINamespace("cronet") |
19 public class CronetTestUtil { | 18 public class CronetTestUtil { |
20 private static final ConditionVariable sHostResolverBlock = new ConditionVar
iable(); | 19 private static final ConditionVariable sHostResolverBlock = new ConditionVar
iable(); |
21 | 20 |
| 21 static final String SDCH_FAKE_HOST = "fake.sdch.domain"; |
| 22 // QUIC test domain must match the certificate used |
| 23 // (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and |
| 24 // the file served ( |
| 25 // components/cronet/android/test/assets/test/quic_data/simple.txt). |
| 26 static final String QUIC_FAKE_HOST = "test.example.com"; |
| 27 private static final String[] TEST_DOMAINS = {SDCH_FAKE_HOST, QUIC_FAKE_HOST
}; |
| 28 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; |
| 29 |
22 /** | 30 /** |
23 * Registers customized DNS mapping for testing host names used by test serv
ers, namely: | 31 * Generates rules for customized DNS mapping for testing hostnames used by
test servers, |
| 32 * namely: |
24 * <ul> | 33 * <ul> |
25 * <li>{@link QuicTestServer#getServerHost}</li> | 34 * <li>{@link QuicTestServer#getServerHost}</li> |
26 * <li>{@link NativeTestServer#getSdchURL}</li>'s host | 35 * <li>{@link NativeTestServer#getSdchURL}</li>'s host |
27 * </ul> | 36 * </ul> |
28 * @param cronetEngine {@link CronetEngine} that this mapping should apply t
o. | 37 * Maps the test hostnames to 127.0.0.1. |
29 * @param destination host to map to (e.g. 127.0.0.1) | |
30 */ | 38 */ |
31 public static void registerHostResolverProc(CronetEngine cronetEngine, Strin
g destination) { | 39 public static JSONObject generateHostResolverRules() throws JSONException { |
32 long contextAdapter = | 40 return generateHostResolverRules(LOOPBACK_ADDRESS); |
33 ((CronetUrlRequestContext) cronetEngine).getUrlRequestContextAda
pter(); | |
34 nativeRegisterHostResolverProc(contextAdapter, false, destination); | |
35 sHostResolverBlock.block(); | |
36 sHostResolverBlock.close(); | |
37 } | 41 } |
38 | 42 |
39 /** | 43 /** |
40 * Registers customized DNS mapping for testing host names used by test serv
ers. | 44 * Generates rules for customized DNS mapping for testing hostnames used by
test servers, |
41 * @param requestFactory {@link HttpUrlRequestFactory} that this mapping sho
uld apply to. | 45 * namely: |
42 * @param destination host to map to (e.g. 127.0.0.1) | 46 * <ul> |
| 47 * <li>{@link QuicTestServer#getServerHost}</li> |
| 48 * <li>{@link NativeTestServer#getSdchURL}</li>'s host |
| 49 * </ul> |
| 50 * @param destination host to map to |
43 */ | 51 */ |
44 public static void registerHostResolverProc( | 52 public static JSONObject generateHostResolverRules(String destination) throw
s JSONException { |
45 HttpUrlRequestFactory requestFactory, String destination) { | 53 StringBuilder rules = new StringBuilder(); |
46 long contextAdapter = ((ChromiumUrlRequestFactory) requestFactory) | 54 for (String domain : TEST_DOMAINS) { |
47 .getRequestContext() | 55 rules.append("MAP " + domain + " " + destination + ","); |
48 .getUrlRequestContextAdapter(); | 56 } |
49 nativeRegisterHostResolverProc(contextAdapter, true, destination); | 57 return new JSONObject().put("host_resolver_rules", rules); |
50 sHostResolverBlock.block(); | |
51 sHostResolverBlock.close(); | |
52 } | 58 } |
53 | 59 |
54 /** | 60 /** |
55 * Returns the value of load flags in |urlRequest|. | 61 * Returns the value of load flags in |urlRequest|. |
56 * @param urlRequest is the UrlRequest object of interest. | 62 * @param urlRequest is the UrlRequest object of interest. |
57 */ | 63 */ |
58 public static int getLoadFlags(UrlRequest urlRequest) { | 64 public static int getLoadFlags(UrlRequest urlRequest) { |
59 return nativeGetLoadFlags(((CronetUrlRequest) urlRequest).getUrlRequestA
dapterForTesting()); | 65 return nativeGetLoadFlags(((CronetUrlRequest) urlRequest).getUrlRequestA
dapterForTesting()); |
60 } | 66 } |
61 | 67 |
62 @CalledByNative | |
63 private static void onHostResolverProcRegistered() { | |
64 sHostResolverBlock.open(); | |
65 } | |
66 | |
67 private static native void nativeRegisterHostResolverProc( | |
68 long contextAdapter, boolean isLegacyAPI, String destination); | |
69 | |
70 private static native int nativeGetLoadFlags(long urlRequest); | 68 private static native int nativeGetLoadFlags(long urlRequest); |
71 } | 69 } |
OLD | NEW |