Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/PkpTest.java

Issue 2052363002: Enable public key pinning of local trust anchors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: url_request_context_config_unittest fix Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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.suitebuilder.annotation.SmallTest; 7 import android.test.suitebuilder.annotation.SmallTest;
8 8
9 import org.chromium.base.test.util.Feature; 9 import org.chromium.base.test.util.Feature;
10 import org.chromium.net.test.util.CertTestUtil; 10 import org.chromium.net.test.util.CertTestUtil;
(...skipping 10 matching lines...) Expand all
21 21
22 /** 22 /**
23 * Public-Key-Pinning tests of Cronet Java API. 23 * Public-Key-Pinning tests of Cronet Java API.
24 */ 24 */
25 public class PkpTest extends CronetTestBase { 25 public class PkpTest extends CronetTestBase {
26 private static final String CERT_USED = "quic_test.example.com.crt"; 26 private static final String CERT_USED = "quic_test.example.com.crt";
27 private static final String[] CERTS_USED = {CERT_USED}; 27 private static final String[] CERTS_USED = {CERT_USED};
28 private static final int DISTANT_FUTURE = Integer.MAX_VALUE; 28 private static final int DISTANT_FUTURE = Integer.MAX_VALUE;
29 private static final boolean INCLUDE_SUBDOMAINS = true; 29 private static final boolean INCLUDE_SUBDOMAINS = true;
30 private static final boolean EXCLUDE_SUBDOMAINS = false; 30 private static final boolean EXCLUDE_SUBDOMAINS = false;
31 private static final boolean KNOWN_ROOT = true;
32 private static final boolean UNKNOWN_ROOT = false;
33 private static final boolean ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS = true;
34 private static final boolean DISABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS = fals e;
31 35
32 private CronetTestFramework mTestFramework; 36 private CronetTestFramework mTestFramework;
33 private CronetEngine.Builder mBuilder; 37 private CronetEngine.Builder mBuilder;
34 private TestUrlRequestCallback mListener; 38 private TestUrlRequestCallback mListener;
35 private String mServerUrl; // https://test.example.com:6121 39 private String mServerUrl; // https://test.example.com:6121
36 private String mServerHost; // test.example.com 40 private String mServerHost; // test.example.com
37 private String mDomain; // example.com 41 private String mDomain; // example.com
38 42
39 @Override 43 @Override
40 protected void setUp() throws Exception { 44 protected void setUp() throws Exception {
41 super.setUp(); 45 super.setUp();
42 // Start QUIC Test Server 46 // Start QUIC Test Server
43 System.loadLibrary("cronet_tests"); 47 System.loadLibrary("cronet_tests");
44 QuicTestServer.startQuicTestServer(getContext()); 48 QuicTestServer.startQuicTestServer(getContext());
45 mServerUrl = QuicTestServer.getServerURL(); 49 mServerUrl = QuicTestServer.getServerURL();
46 mServerHost = QuicTestServer.getServerHost(); 50 mServerHost = QuicTestServer.getServerHost();
47 mDomain = mServerHost.substring(mServerHost.indexOf('.') + 1, mServerHos t.length()); 51 mDomain = mServerHost.substring(mServerHost.indexOf('.') + 1, mServerHos t.length());
48 createCronetEngineBuilder();
49 } 52 }
50 53
51 @Override 54 @Override
52 protected void tearDown() throws Exception { 55 protected void tearDown() throws Exception {
53 QuicTestServer.shutdownQuicTestServer(); 56 QuicTestServer.shutdownQuicTestServer();
54 shutdownCronetEngine(); 57 shutdownCronetEngine();
55 super.tearDown(); 58 super.tearDown();
56 } 59 }
57 60
58 /** 61 /**
59 * Tests the case when the pin hash does not match. The client is expected t o 62 * Tests the case when the pin hash does not match. The client is expected t o
60 * receive the error response. 63 * receive the error response.
61 * 64 *
62 * @throws Exception 65 * @throws Exception
63 */ 66 */
64 @SmallTest 67 @SmallTest
65 @Feature({"Cronet"}) 68 @Feature({"Cronet"})
66 @OnlyRunNativeCronet 69 @OnlyRunNativeCronet
67 public void testErrorCodeIfPinDoesNotMatch() throws Exception { 70 public void testErrorCodeIfPinDoesNotMatch() throws Exception {
71 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
68 byte[] nonMatchingHash = generateSomeSha256(); 72 byte[] nonMatchingHash = generateSomeSha256();
69 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE); 73 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE);
70 startCronetFramework(); 74 startCronetFramework();
71 registerHostResolver(mTestFramework); 75 registerHostResolver(mTestFramework);
72 sendRequestAndWaitForResult(); 76 sendRequestAndWaitForResult();
73 77
74 assertErrorResponse(); 78 assertErrorResponse();
75 } 79 }
76 80
77 /** 81 /**
78 * Tests the case when the pin hash matches. The client is expected to 82 * Tests the case when the pin hash matches. The client is expected to
79 * receive the successful response with the response code 200. 83 * receive the successful response with the response code 200.
80 * 84 *
81 * @throws Exception 85 * @throws Exception
82 */ 86 */
83 @SmallTest 87 @SmallTest
84 @Feature({"Cronet"}) 88 @Feature({"Cronet"})
85 @OnlyRunNativeCronet 89 @OnlyRunNativeCronet
86 public void testSuccessIfPinMatches() throws Exception { 90 public void testSuccessIfPinMatches() throws Exception {
91 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
87 // Get PKP hash of the real certificate 92 // Get PKP hash of the real certificate
88 X509Certificate cert = readCertFromFileInPemFormat(CERT_USED); 93 X509Certificate cert = readCertFromFileInPemFormat(CERT_USED);
89 byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert); 94 byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert);
90 95
91 addPkpSha256(mServerHost, matchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTU RE); 96 addPkpSha256(mServerHost, matchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTU RE);
92 startCronetFramework(); 97 startCronetFramework();
93 registerHostResolver(mTestFramework); 98 registerHostResolver(mTestFramework);
94 sendRequestAndWaitForResult(); 99 sendRequestAndWaitForResult();
95 100
96 assertSuccessfulResponse(); 101 assertSuccessfulResponse();
97 } 102 }
98 103
99 /** 104 /**
100 * Tests the case when the pin hash does not match and the client accesses t he subdomain of 105 * Tests the case when the pin hash does not match and the client accesses t he subdomain of
101 * the configured PKP host with includeSubdomains flag set to true. The clie nt is 106 * the configured PKP host with includeSubdomains flag set to true. The clie nt is
102 * expected to receive the error response. 107 * expected to receive the error response.
103 * 108 *
104 * @throws Exception 109 * @throws Exception
105 */ 110 */
106 @SmallTest 111 @SmallTest
107 @Feature({"Cronet"}) 112 @Feature({"Cronet"})
108 @OnlyRunNativeCronet 113 @OnlyRunNativeCronet
109 public void testIncludeSubdomainsFlagEqualTrue() throws Exception { 114 public void testIncludeSubdomainsFlagEqualTrue() throws Exception {
115 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
110 byte[] nonMatchingHash = generateSomeSha256(); 116 byte[] nonMatchingHash = generateSomeSha256();
111 addPkpSha256(mDomain, nonMatchingHash, INCLUDE_SUBDOMAINS, DISTANT_FUTUR E); 117 addPkpSha256(mDomain, nonMatchingHash, INCLUDE_SUBDOMAINS, DISTANT_FUTUR E);
112 startCronetFramework(); 118 startCronetFramework();
113 registerHostResolver(mTestFramework); 119 registerHostResolver(mTestFramework);
114 sendRequestAndWaitForResult(); 120 sendRequestAndWaitForResult();
115 121
116 assertErrorResponse(); 122 assertErrorResponse();
117 } 123 }
118 124
119 /** 125 /**
120 * Tests the case when the pin hash does not match and the client accesses t he subdomain of 126 * Tests the case when the pin hash does not match and the client accesses t he subdomain of
121 * the configured PKP host with includeSubdomains flag set to false. The cli ent is expected to 127 * the configured PKP host with includeSubdomains flag set to false. The cli ent is expected to
122 * receive the successful response with the response code 200. 128 * receive the successful response with the response code 200.
123 * 129 *
124 * @throws Exception 130 * @throws Exception
125 */ 131 */
126 @SmallTest 132 @SmallTest
127 @Feature({"Cronet"}) 133 @Feature({"Cronet"})
128 @OnlyRunNativeCronet 134 @OnlyRunNativeCronet
129 public void testIncludeSubdomainsFlagEqualFalse() throws Exception { 135 public void testIncludeSubdomainsFlagEqualFalse() throws Exception {
136 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
130 byte[] nonMatchingHash = generateSomeSha256(); 137 byte[] nonMatchingHash = generateSomeSha256();
131 addPkpSha256(mDomain, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTUR E); 138 addPkpSha256(mDomain, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTUR E);
132 startCronetFramework(); 139 startCronetFramework();
133 registerHostResolver(mTestFramework); 140 registerHostResolver(mTestFramework);
134 sendRequestAndWaitForResult(); 141 sendRequestAndWaitForResult();
135 142
136 assertSuccessfulResponse(); 143 assertSuccessfulResponse();
137 } 144 }
138 145
139 /** 146 /**
140 * Tests the case when the mismatching pin is set for some host that is diff erent from the one 147 * Tests the case when the mismatching pin is set for some host that is diff erent from the one
141 * the client wants to access. In that case the other host pinning policy sh ould not be applied 148 * the client wants to access. In that case the other host pinning policy sh ould not be applied
142 * and the client is expected to receive the successful response with the re sponse code 200. 149 * and the client is expected to receive the successful response with the re sponse code 200.
143 * 150 *
144 * @throws Exception 151 * @throws Exception
145 */ 152 */
146 @SmallTest 153 @SmallTest
147 @Feature({"Cronet"}) 154 @Feature({"Cronet"})
148 @OnlyRunNativeCronet 155 @OnlyRunNativeCronet
149 public void testSuccessIfNoPinSpecified() throws Exception { 156 public void testSuccessIfNoPinSpecified() throws Exception {
157 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
150 byte[] nonMatchingHash = generateSomeSha256(); 158 byte[] nonMatchingHash = generateSomeSha256();
151 addPkpSha256("otherhost.com", nonMatchingHash, INCLUDE_SUBDOMAINS, DISTA NT_FUTURE); 159 addPkpSha256("otherhost.com", nonMatchingHash, INCLUDE_SUBDOMAINS, DISTA NT_FUTURE);
152 startCronetFramework(); 160 startCronetFramework();
153 registerHostResolver(mTestFramework); 161 registerHostResolver(mTestFramework);
154 sendRequestAndWaitForResult(); 162 sendRequestAndWaitForResult();
155 163
156 assertSuccessfulResponse(); 164 assertSuccessfulResponse();
157 } 165 }
158 166
159 /** 167 /**
160 * Tests mismatching pins that will expire in 10 seconds. The pins should be still valid and 168 * Tests mismatching pins that will expire in 10 seconds. The pins should be still valid and
161 * enforced during the request; thus returning PIN mismatch error. 169 * enforced during the request; thus returning PIN mismatch error.
162 * 170 *
163 * @throws Exception 171 * @throws Exception
164 */ 172 */
165 @SmallTest 173 @SmallTest
166 @Feature({"Cronet"}) 174 @Feature({"Cronet"})
167 @OnlyRunNativeCronet 175 @OnlyRunNativeCronet
168 public void testSoonExpiringPin() throws Exception { 176 public void testSoonExpiringPin() throws Exception {
177 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
169 final int tenSecondsAhead = 10; 178 final int tenSecondsAhead = 10;
170 byte[] nonMatchingHash = generateSomeSha256(); 179 byte[] nonMatchingHash = generateSomeSha256();
171 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, tenSecond sAhead); 180 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, tenSecond sAhead);
172 startCronetFramework(); 181 startCronetFramework();
173 registerHostResolver(mTestFramework); 182 registerHostResolver(mTestFramework);
174 sendRequestAndWaitForResult(); 183 sendRequestAndWaitForResult();
175 184
176 assertErrorResponse(); 185 assertErrorResponse();
177 } 186 }
178 187
179 /** 188 /**
180 * Tests mismatching pins that expired 1 second ago. Since the pins have exp ired, they 189 * Tests mismatching pins that expired 1 second ago. Since the pins have exp ired, they
181 * should not be enforced during the request; thus a successful response is expected. 190 * should not be enforced during the request; thus a successful response is expected.
182 * 191 *
183 * @throws Exception 192 * @throws Exception
184 */ 193 */
185 @SmallTest 194 @SmallTest
186 @Feature({"Cronet"}) 195 @Feature({"Cronet"})
187 @OnlyRunNativeCronet 196 @OnlyRunNativeCronet
188 public void testRecentlyExpiredPin() throws Exception { 197 public void testRecentlyExpiredPin() throws Exception {
198 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
189 final int oneSecondAgo = -1; 199 final int oneSecondAgo = -1;
190 byte[] nonMatchingHash = generateSomeSha256(); 200 byte[] nonMatchingHash = generateSomeSha256();
191 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, oneSecond Ago); 201 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, oneSecond Ago);
192 startCronetFramework(); 202 startCronetFramework();
193 registerHostResolver(mTestFramework); 203 registerHostResolver(mTestFramework);
194 sendRequestAndWaitForResult(); 204 sendRequestAndWaitForResult();
195 205
196 assertSuccessfulResponse(); 206 assertSuccessfulResponse();
197 } 207 }
198 208
199 /** 209 /**
210 * Tests that the pinning of local trust anchors is enforced is enforced whe n pinning bypass for
Ryan Sleevi 2016/07/01 01:17:24 typo: is enforced is enforced
kapishnikov 2016/07/01 17:20:54 Done.
211 * local trust anchors is disabled.
212 *
213 * @throws Exception
214 */
215 @SmallTest
216 @Feature({"Cronet"})
217 public void testLocalTrustAnchorPinningEnforced() throws Exception {
218 createCronetEngineBuilder(DISABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, UNKN OWN_ROOT);
219 byte[] nonMatchingHash = generateSomeSha256();
220 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE);
221 startCronetFramework();
222 registerHostResolver(mTestFramework);
223 sendRequestAndWaitForResult();
224
225 assertErrorResponse();
226 }
227
228 /**
229 * Tests that the pinning of local trust anchors is not enforced when pinnin g bypass for local
230 * trust anchors is enabled.
231 *
232 * @throws Exception
233 */
234 @SmallTest
235 @Feature({"Cronet"})
236 public void testLocalTrustAnchorPinningNotEnforced() throws Exception {
237 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, UNKNO WN_ROOT);
238 byte[] nonMatchingHash = generateSomeSha256();
239 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE);
240 startCronetFramework();
241 registerHostResolver(mTestFramework);
242 sendRequestAndWaitForResult();
243
244 assertSuccessfulResponse();
245 }
246
247 /**
200 * Tests that host pinning is not persisted between multiple CronetEngine in stances. 248 * Tests that host pinning is not persisted between multiple CronetEngine in stances.
201 * 249 *
202 * @throws Exception 250 * @throws Exception
203 */ 251 */
204 @SmallTest 252 @SmallTest
205 @Feature({"Cronet"}) 253 @Feature({"Cronet"})
206 @OnlyRunNativeCronet 254 @OnlyRunNativeCronet
207 public void testPinsAreNotPersisted() throws Exception { 255 public void testPinsAreNotPersisted() throws Exception {
256 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
208 byte[] nonMatchingHash = generateSomeSha256(); 257 byte[] nonMatchingHash = generateSomeSha256();
209 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE); 258 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE);
210 startCronetFramework(); 259 startCronetFramework();
211 registerHostResolver(mTestFramework); 260 registerHostResolver(mTestFramework);
212 sendRequestAndWaitForResult(); 261 sendRequestAndWaitForResult();
213 assertErrorResponse(); 262 assertErrorResponse();
214 shutdownCronetEngine(); 263 shutdownCronetEngine();
215 264
216 // Restart Cronet engine and try the same request again. Since the pins are not persisted, 265 // Restart Cronet engine and try the same request again. Since the pins are not persisted,
217 // a successful response is expected. 266 // a successful response is expected.
218 createCronetEngineBuilder(); 267 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
219 startCronetFramework(); 268 startCronetFramework();
220 registerHostResolver(mTestFramework); 269 registerHostResolver(mTestFramework);
221 sendRequestAndWaitForResult(); 270 sendRequestAndWaitForResult();
222 assertSuccessfulResponse(); 271 assertSuccessfulResponse();
223 } 272 }
224 273
225 /** 274 /**
226 * Tests that the client receives {@code InvalidArgumentException} when the pinned host name 275 * Tests that the client receives {@code InvalidArgumentException} when the pinned host name
227 * is invalid. 276 * is invalid.
228 * 277 *
229 * @throws Exception 278 * @throws Exception
230 */ 279 */
231 @SmallTest 280 @SmallTest
232 @Feature({"Cronet"}) 281 @Feature({"Cronet"})
233 public void testHostNameArgumentValidation() throws Exception { 282 public void testHostNameArgumentValidation() throws Exception {
283 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
234 final String label63 = "123456789-123456789-123456789-123456789-12345678 9-123456789-123"; 284 final String label63 = "123456789-123456789-123456789-123456789-12345678 9-123456789-123";
235 final String host255 = label63 + "." + label63 + "." + label63 + "." + l abel63; 285 final String host255 = label63 + "." + label63 + "." + label63 + "." + l abel63;
236 // Valid host names. 286 // Valid host names.
237 assertNoExceptionWhenHostNameIsValid("domain.com"); 287 assertNoExceptionWhenHostNameIsValid("domain.com");
238 assertNoExceptionWhenHostNameIsValid("my-domain.com"); 288 assertNoExceptionWhenHostNameIsValid("my-domain.com");
239 assertNoExceptionWhenHostNameIsValid("section4.domain.info"); 289 assertNoExceptionWhenHostNameIsValid("section4.domain.info");
240 assertNoExceptionWhenHostNameIsValid("44.domain44.info"); 290 assertNoExceptionWhenHostNameIsValid("44.domain44.info");
241 assertNoExceptionWhenHostNameIsValid("very.long.long.long.long.long.long .long.domain.com"); 291 assertNoExceptionWhenHostNameIsValid("very.long.long.long.long.long.long .long.domain.com");
242 assertNoExceptionWhenHostNameIsValid("host"); 292 assertNoExceptionWhenHostNameIsValid("host");
243 assertNoExceptionWhenHostNameIsValid("новости.ру"); 293 assertNoExceptionWhenHostNameIsValid("новости.ру");
(...skipping 30 matching lines...) Expand all
274 assertExceptionWhenHostNameIsInvalid("256.0.0.1"); 324 assertExceptionWhenHostNameIsInvalid("256.0.0.1");
275 assertExceptionWhenHostNameIsInvalid("127.0.0.1.1"); 325 assertExceptionWhenHostNameIsInvalid("127.0.0.1.1");
276 assertExceptionWhenHostNameIsInvalid("127.0.0"); 326 assertExceptionWhenHostNameIsInvalid("127.0.0");
277 assertExceptionWhenHostNameIsInvalid("127.0.0."); 327 assertExceptionWhenHostNameIsInvalid("127.0.0.");
278 assertExceptionWhenHostNameIsInvalid("127.0.0.299"); 328 assertExceptionWhenHostNameIsInvalid("127.0.0.299");
279 } 329 }
280 330
281 /** 331 /**
282 * Tests that NullPointerException is thrown if the host name or the collect ion of pins or 332 * Tests that NullPointerException is thrown if the host name or the collect ion of pins or
283 * the expiration date is null. 333 * the expiration date is null.
334 *
335 * @throws Exception
284 */ 336 */
285 @SmallTest 337 @SmallTest
286 @Feature({"Cronet"}) 338 @Feature({"Cronet"})
287 public void testNullArguments() { 339 public void testNullArguments() throws Exception {
340 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
288 verifyExceptionWhenAddPkpArgumentIsNull(true, false, false); 341 verifyExceptionWhenAddPkpArgumentIsNull(true, false, false);
289 verifyExceptionWhenAddPkpArgumentIsNull(false, true, false); 342 verifyExceptionWhenAddPkpArgumentIsNull(false, true, false);
290 verifyExceptionWhenAddPkpArgumentIsNull(false, false, true); 343 verifyExceptionWhenAddPkpArgumentIsNull(false, false, true);
291 verifyExceptionWhenAddPkpArgumentIsNull(false, false, false); 344 verifyExceptionWhenAddPkpArgumentIsNull(false, false, false);
292 } 345 }
293 346
294 /** 347 /**
295 * Tests that IllegalArgumentException is thrown if SHA1 is passed as the va lue of a pin. 348 * Tests that IllegalArgumentException is thrown if SHA1 is passed as the va lue of a pin.
349 *
350 * @throws Exception
296 */ 351 */
297 @SmallTest 352 @SmallTest
298 @Feature({"Cronet"}) 353 @Feature({"Cronet"})
299 public void testIllegalArgumentExceptionWhenPinValueIsSHA1() { 354 public void testIllegalArgumentExceptionWhenPinValueIsSHA1() throws Exceptio n {
355 createCronetEngineBuilder(ENABLE_PINNING_BYPASS_FOR_LOCAL_ANCHORS, KNOWN _ROOT);
300 byte[] sha1 = new byte[20]; 356 byte[] sha1 = new byte[20];
301 try { 357 try {
302 addPkpSha256(mServerHost, sha1, EXCLUDE_SUBDOMAINS, DISTANT_FUTURE); 358 addPkpSha256(mServerHost, sha1, EXCLUDE_SUBDOMAINS, DISTANT_FUTURE);
303 } catch (IllegalArgumentException ex) { 359 } catch (IllegalArgumentException ex) {
304 // Expected exception 360 // Expected exception
305 return; 361 return;
306 } 362 }
307 fail("Expected IllegalArgumentException with pin value: " + Arrays.toStr ing(sha1)); 363 fail("Expected IllegalArgumentException with pin value: " + Arrays.toStr ing(sha1));
308 } 364 }
309 365
(...skipping 22 matching lines...) Expand all
332 */ 388 */
333 private void assertSuccessfulResponse() { 389 private void assertSuccessfulResponse() {
334 if (mListener.mError != null) { 390 if (mListener.mError != null) {
335 fail("Did not expect an error but got error code " 391 fail("Did not expect an error but got error code "
336 + mListener.mError.getCronetInternalErrorCode()); 392 + mListener.mError.getCronetInternalErrorCode());
337 } 393 }
338 assertNotNull("Expected non-null response from the server", mListener.mR esponseInfo); 394 assertNotNull("Expected non-null response from the server", mListener.mR esponseInfo);
339 assertEquals(200, mListener.mResponseInfo.getHttpStatusCode()); 395 assertEquals(200, mListener.mResponseInfo.getHttpStatusCode());
340 } 396 }
341 397
342 private void createCronetEngineBuilder() throws Exception { 398 private void createCronetEngineBuilder(boolean bypassPinningForLocalAnchors, boolean knownRoot)
399 throws Exception {
343 // Set common CronetEngine parameters 400 // Set common CronetEngine parameters
344 mBuilder = new CronetEngine.Builder(getContext()); 401 mBuilder = new CronetEngine.Builder(getContext());
402 mBuilder.enablePublicKeyPinningBypassForLocalTrustAnchors(bypassPinningF orLocalAnchors);
345 mBuilder.enableQUIC(true); 403 mBuilder.enableQUIC(true);
346 mBuilder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getS erverPort(), 404 mBuilder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getS erverPort(),
347 QuicTestServer.getServerPort()); 405 QuicTestServer.getServerPort());
348 JSONObject quicParams = new JSONObject().put("host_whitelist", "test.exa mple.com"); 406 JSONObject quicParams = new JSONObject().put("host_whitelist", "test.exa mple.com");
349 JSONObject experimentalOptions = new JSONObject().put("QUIC", quicParams ); 407 JSONObject experimentalOptions = new JSONObject().put("QUIC", quicParams );
350 mBuilder.setExperimentalOptions(experimentalOptions.toString()); 408 mBuilder.setExperimentalOptions(experimentalOptions.toString());
351 mBuilder.setStoragePath(CronetTestFramework.getTestStorage(getContext()) ); 409 mBuilder.setStoragePath(CronetTestFramework.getTestStorage(getContext()) );
352 mBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1 000 * 1024); 410 mBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1 000 * 1024);
353 mBuilder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVe rifier(CERTS_USED)); 411 mBuilder.setMockCertVerifierForTesting(
412 MockCertVerifier.createMockCertVerifier(CERTS_USED, knownRoot));
354 } 413 }
355 414
356 private void startCronetFramework() { 415 private void startCronetFramework() {
357 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n ull, mBuilder); 416 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n ull, mBuilder);
358 } 417 }
359 418
360 private void shutdownCronetEngine() { 419 private void shutdownCronetEngine() {
361 if (mTestFramework != null && mTestFramework.mCronetEngine != null) { 420 if (mTestFramework != null && mTestFramework.mCronetEngine != null) {
362 mTestFramework.mCronetEngine.shutdown(); 421 mTestFramework.mCronetEngine.shutdown();
363 } 422 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 if (!shouldThrowNpe) { 489 if (!shouldThrowNpe) {
431 fail("Null pointer exception was not expected: " + ex.toString() ); 490 fail("Null pointer exception was not expected: " + ex.toString() );
432 } 491 }
433 return; 492 return;
434 } 493 }
435 if (shouldThrowNpe) { 494 if (shouldThrowNpe) {
436 fail("NullPointerException was expected"); 495 fail("NullPointerException was expected");
437 } 496 }
438 } 497 }
439 } 498 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698