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