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.test.AndroidTestCase; | 7 import android.test.AndroidTestCase; |
8 | 8 |
9 import org.chromium.base.ContextUtils; | 9 import org.chromium.base.ContextUtils; |
10 import org.chromium.base.PathUtils; | 10 import org.chromium.base.PathUtils; |
11 import org.chromium.net.impl.CronetEngineBase; | 11 import org.chromium.net.impl.CronetEngineBase; |
12 import org.chromium.net.impl.JavaCronetEngine; | 12 import org.chromium.net.impl.JavaCronetEngine; |
13 import org.chromium.net.impl.JavaCronetProvider; | 13 import org.chromium.net.impl.JavaCronetProvider; |
14 import org.chromium.net.impl.UserAgent; | 14 import org.chromium.net.impl.UserAgent; |
15 | 15 |
16 import java.lang.annotation.ElementType; | 16 import java.lang.annotation.ElementType; |
17 import java.lang.annotation.Retention; | 17 import java.lang.annotation.Retention; |
18 import java.lang.annotation.RetentionPolicy; | 18 import java.lang.annotation.RetentionPolicy; |
19 import java.lang.annotation.Target; | 19 import java.lang.annotation.Target; |
20 import java.lang.reflect.AnnotatedElement; | 20 import java.lang.reflect.AnnotatedElement; |
21 import java.net.URL; | 21 import java.net.URL; |
22 import java.net.URLStreamHandlerFactory; | |
22 | 23 |
23 /** | 24 /** |
24 * Base test class for all CronetTest based tests. | 25 * Base test class for all CronetTest based tests. |
25 */ | 26 */ |
26 public class CronetTestBase extends AndroidTestCase { | 27 public class CronetTestBase extends AndroidTestCase { |
27 /** | 28 /** |
28 * Name of the file that contains the test server certificate in PEM format. | 29 * Name of the file that contains the test server certificate in PEM format. |
29 */ | 30 */ |
30 static final String SERVER_CERT_PEM = "quic_test.example.com.crt"; | 31 static final String SERVER_CERT_PEM = "quic_test.example.com.crt"; |
31 | 32 |
32 /** | 33 /** |
33 * Name of the file that contains the test server private key in PKCS8 PEM f ormat. | 34 * Name of the file that contains the test server private key in PKCS8 PEM f ormat. |
34 */ | 35 */ |
35 static final String SERVER_KEY_PKCS8_PEM = "quic_test.example.com.key.pkcs8. pem"; | 36 static final String SERVER_KEY_PKCS8_PEM = "quic_test.example.com.key.pkcs8. pem"; |
36 | 37 |
37 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test"; | 38 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test"; |
38 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; | 39 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; |
39 | 40 |
40 private CronetTestFramework mCronetTestFramework; | 41 private CronetTestFramework mCronetTestFramework; |
42 private URLStreamHandlerFactory mStreamHandlerFactory; | |
43 | |
41 // {@code true} when test is being run against system HttpURLConnection impl ementation. | 44 // {@code true} when test is being run against system HttpURLConnection impl ementation. |
42 private boolean mTestingSystemHttpURLConnection; | 45 private boolean mTestingSystemHttpURLConnection; |
43 private boolean mTestingJavaImpl = false; | 46 private boolean mTestingJavaImpl = false; |
44 | 47 |
45 @Override | 48 @Override |
46 protected void setUp() throws Exception { | 49 protected void setUp() throws Exception { |
47 super.setUp(); | 50 super.setUp(); |
48 System.loadLibrary("cronet_tests"); | 51 System.loadLibrary("cronet_tests"); |
49 ContextUtils.initApplicationContext(getContext().getApplicationContext() ); | 52 ContextUtils.initApplicationContext(getContext().getApplicationContext() ); |
50 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); | 53 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); |
51 CronetTestFramework.prepareTestStorage(getContext()); | 54 CronetTestFramework.prepareTestStorage(getContext()); |
52 } | 55 } |
53 | 56 |
54 /** | 57 /** |
55 * Starts the CronetTest framework. | 58 * Starts the CronetTest framework. |
56 */ | 59 */ |
57 protected CronetTestFramework startCronetTestFramework() { | 60 protected CronetTestFramework startCronetTestFramework() { |
58 return startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, null) ; | 61 return startCronetTestFrameworkWithCronetEngineBuilder(null); |
59 } | 62 } |
60 | 63 |
61 /** | 64 /** |
62 * Starts the CronetTest framework and loads the given URL. The URL can be | 65 * Starts the CronetTest framework using the provided CronetEngine.Builder. |
63 * null. | |
64 */ | 66 */ |
65 protected CronetTestFramework startCronetTestFrameworkWithUrl(String url) { | 67 protected CronetTestFramework startCronetTestFrameworkWithCronetEngineBuilde r( |
66 return startCronetTestFrameworkWithUrlAndCronetEngineBuilder(url, null); | 68 ExperimentalCronetEngine.Builder builder) { |
67 } | 69 mCronetTestFramework = new CronetTestFramework(getContext(), builder); |
68 | |
69 /** | |
70 * Starts the CronetTest framework using the provided CronetEngine.Builder | |
71 * and loads the given URL. The URL can be null. | |
72 */ | |
73 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCronetEngine Builder( | |
74 String url, ExperimentalCronetEngine.Builder builder) { | |
75 mCronetTestFramework = new CronetTestFramework(url, null, getContext(), builder); | |
76 return mCronetTestFramework; | 70 return mCronetTestFramework; |
77 } | 71 } |
78 | 72 |
79 /** | |
80 * Starts the CronetTest framework appending the provided command line | |
81 * arguments and loads the given URL. The URL can be null. | |
82 */ | |
83 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCommandLineA rgs( | |
84 String url, String[] commandLineArgs) { | |
85 mCronetTestFramework = new CronetTestFramework(url, commandLineArgs, get Context(), null); | |
86 return mCronetTestFramework; | |
87 } | |
88 | |
89 // Helper method to tell the framework to skip library init during construct ion. | |
90 protected CronetTestFramework startCronetTestFrameworkAndSkipLibraryInit() { | |
91 String[] commandLineArgs = { | |
92 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.NONE}; | |
93 mCronetTestFramework = | |
94 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL ineArgs); | |
95 return mCronetTestFramework; | |
96 } | |
97 | |
98 /** | 73 /** |
99 * Returns {@code true} when test is being run against system HttpURLConnect ion implementation. | 74 * Returns {@code true} when test is being run against system HttpURLConnect ion implementation. |
100 */ | 75 */ |
101 protected boolean testingSystemHttpURLConnection() { | 76 protected boolean testingSystemHttpURLConnection() { |
102 return mTestingSystemHttpURLConnection; | 77 return mTestingSystemHttpURLConnection; |
103 } | 78 } |
104 | 79 |
105 /** | 80 /** |
106 * Returns {@code true} when test is being run against the java implementati on of CronetEngine. | 81 * Returns {@code true} when test is being run against the java implementati on of CronetEngine. |
107 */ | 82 */ |
108 protected boolean testingJavaImpl() { | 83 protected boolean testingJavaImpl() { |
109 return mTestingJavaImpl; | 84 return mTestingJavaImpl; |
110 } | 85 } |
111 | 86 |
112 @Override | 87 @Override |
113 protected void runTest() throws Throwable { | 88 protected void runTest() throws Throwable { |
114 mTestingSystemHttpURLConnection = false; | 89 mTestingSystemHttpURLConnection = false; |
115 mTestingJavaImpl = false; | 90 mTestingJavaImpl = false; |
116 String packageName = getClass().getPackage().getName(); | 91 String packageName = getClass().getPackage().getName(); |
117 if (packageName.equals("org.chromium.net.urlconnection")) { | 92 if (packageName.equals("org.chromium.net.urlconnection")) { |
118 try { | 93 try { |
119 AnnotatedElement method = getClass().getMethod(getName(), (Class []) null); | 94 AnnotatedElement method = getClass().getMethod(getName(), (Class []) null); |
120 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { | 95 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { |
121 // Run with the default HttpURLConnection implementation fir st. | 96 // Run with the default HttpURLConnection implementation fir st. |
122 mTestingSystemHttpURLConnection = true; | 97 mTestingSystemHttpURLConnection = true; |
123 super.runTest(); | 98 super.runTest(); |
124 // Use Cronet's implementation, and run the same test. | 99 // Use Cronet's implementation, and run the same test. |
125 mTestingSystemHttpURLConnection = false; | 100 mTestingSystemHttpURLConnection = false; |
126 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamH andlerFactory); | 101 URL.setURLStreamHandlerFactory(mStreamHandlerFactory); |
127 super.runTest(); | 102 super.runTest(); |
128 } else if (method.isAnnotationPresent(OnlyRunCronetHttpURLConnec tion.class)) { | 103 } else if (method.isAnnotationPresent(OnlyRunCronetHttpURLConnec tion.class)) { |
129 // Run only with Cronet's implementation. | 104 // Run only with Cronet's implementation. |
130 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamH andlerFactory); | 105 URL.setURLStreamHandlerFactory(mStreamHandlerFactory); |
131 super.runTest(); | 106 super.runTest(); |
132 } else { | 107 } else { |
133 // For all other tests. | 108 // For all other tests. |
134 super.runTest(); | 109 super.runTest(); |
135 } | 110 } |
136 } catch (Throwable e) { | 111 } catch (Throwable e) { |
137 throw new Throwable("CronetTestBase#runTest failed.", e); | 112 throw new Throwable("CronetTestBase#runTest failed.", e); |
138 } | 113 } |
139 } else if (packageName.equals("org.chromium.net")) { | 114 } else if (packageName.equals("org.chromium.net")) { |
140 try { | 115 try { |
(...skipping 30 matching lines...) Expand all Loading... | |
171 } | 146 } |
172 | 147 |
173 public void assertResponseEquals(UrlResponseInfo expected, UrlResponseInfo a ctual) { | 148 public void assertResponseEquals(UrlResponseInfo expected, UrlResponseInfo a ctual) { |
174 assertEquals(expected.getAllHeaders(), actual.getAllHeaders()); | 149 assertEquals(expected.getAllHeaders(), actual.getAllHeaders()); |
175 assertEquals(expected.getAllHeadersAsList(), actual.getAllHeadersAsList( )); | 150 assertEquals(expected.getAllHeadersAsList(), actual.getAllHeadersAsList( )); |
176 assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode()); | 151 assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode()); |
177 assertEquals(expected.getHttpStatusText(), actual.getHttpStatusText()); | 152 assertEquals(expected.getHttpStatusText(), actual.getHttpStatusText()); |
178 assertEquals(expected.getUrlChain(), actual.getUrlChain()); | 153 assertEquals(expected.getUrlChain(), actual.getUrlChain()); |
179 assertEquals(expected.getUrl(), actual.getUrl()); | 154 assertEquals(expected.getUrl(), actual.getUrl()); |
180 // Transferred bytes and proxy server are not supported in pure java | 155 // Transferred bytes and proxy server are not supported in pure java |
181 if (!(mCronetTestFramework.mCronetEngine instanceof JavaCronetEngine)) { | 156 if (!testingJavaImpl()) { |
182 assertEquals(expected.getReceivedByteCount(), actual.getReceivedByte Count()); | 157 assertEquals(expected.getReceivedByteCount(), actual.getReceivedByte Count()); |
183 assertEquals(expected.getProxyServer(), actual.getProxyServer()); | 158 assertEquals(expected.getProxyServer(), actual.getProxyServer()); |
184 // This is a place where behavior intentionally differs between nati ve and java | 159 // This is a place where behavior intentionally differs between nati ve and java |
185 assertEquals(expected.getNegotiatedProtocol(), actual.getNegotiatedP rotocol()); | 160 assertEquals(expected.getNegotiatedProtocol(), actual.getNegotiatedP rotocol()); |
186 } | 161 } |
187 } | 162 } |
188 | 163 |
189 public static void assertContains(String expectedSubstring, String actualStr ing) { | 164 public static void assertContains(String expectedSubstring, String actualStr ing) { |
190 assertNotNull(actualString); | 165 assertNotNull(actualString); |
191 if (!actualString.contains(expectedSubstring)) { | 166 if (!actualString.contains(expectedSubstring)) { |
192 fail("String [" + actualString + "] doesn't contain substring [" + e xpectedSubstring | 167 fail("String [" + actualString + "] doesn't contain substring [" + e xpectedSubstring |
193 + "]"); | 168 + "]"); |
194 } | 169 } |
195 } | 170 } |
196 | 171 |
172 public CronetEngine.Builder enableDiskCache(CronetEngine.Builder cronetEngin eBuilder) { | |
173 cronetEngineBuilder.setStoragePath(CronetTestFramework.getTestStorage(ge tContext())); | |
174 cronetEngineBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK , 1000 * 1024); | |
175 return cronetEngineBuilder; | |
176 } | |
177 | |
178 /** | |
179 * Sets the {@link UrlStreamHandlerFactory} from {@code cronetEngine}. This should be called | |
mgersh
2017/05/23 18:12:13
nit: Url... -> URL...
pauljensen
2017/05/25 15:15:14
Done.
mgersh
2017/05/25 18:11:16
This and the one below aren't actually done?
pauljensen
2017/05/26 00:14:03
Done.
| |
180 * during setup() and is installed by {@link runTest()} as the default when Cronet is tested. | |
mgersh
2017/05/23 18:12:13
nit: setup() -> setUp()
pauljensen
2017/05/25 15:15:14
Done.
| |
181 */ | |
182 public void setStreamHandlerFactory(CronetEngine cronetEngine) { | |
183 mStreamHandlerFactory = cronetEngine.createURLStreamHandlerFactory(); | |
184 } | |
185 | |
197 @Target(ElementType.METHOD) | 186 @Target(ElementType.METHOD) |
198 @Retention(RetentionPolicy.RUNTIME) | 187 @Retention(RetentionPolicy.RUNTIME) |
199 public @interface CompareDefaultWithCronet { | 188 public @interface CompareDefaultWithCronet { |
200 } | 189 } |
201 | 190 |
202 @Target(ElementType.METHOD) | 191 @Target(ElementType.METHOD) |
203 @Retention(RetentionPolicy.RUNTIME) | 192 @Retention(RetentionPolicy.RUNTIME) |
204 public @interface OnlyRunCronetHttpURLConnection { | 193 public @interface OnlyRunCronetHttpURLConnection { |
205 } | 194 } |
206 | 195 |
207 @Target(ElementType.METHOD) | 196 @Target(ElementType.METHOD) |
208 @Retention(RetentionPolicy.RUNTIME) | 197 @Retention(RetentionPolicy.RUNTIME) |
209 public @interface OnlyRunNativeCronet {} | 198 public @interface OnlyRunNativeCronet {} |
210 } | 199 } |
OLD | NEW |