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

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

Issue 1492583002: Add HttpUrlConnection backed implementation of CronetEngine. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase on HEAD Created 5 years 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 } 55 }
56 56
57 /** 57 /**
58 * Tests the case when the pin hash does not match. The client is expected t o 58 * Tests the case when the pin hash does not match. The client is expected t o
59 * receive the error response. 59 * receive the error response.
60 * 60 *
61 * @throws Exception 61 * @throws Exception
62 */ 62 */
63 @SmallTest 63 @SmallTest
64 @Feature({"Cronet"}) 64 @Feature({"Cronet"})
65 @OnlyRunNativeCronet
65 public void testErrorCodeIfPinDoesNotMatch() throws Exception { 66 public void testErrorCodeIfPinDoesNotMatch() throws Exception {
66 byte[] nonMatchingHash = generateSomeSha256(); 67 byte[] nonMatchingHash = generateSomeSha256();
67 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE); 68 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE);
68 startCronetFramework(); 69 startCronetFramework();
69 registerHostResolver(mTestFramework); 70 registerHostResolver(mTestFramework);
70 sendRequestAndWaitForResult(); 71 sendRequestAndWaitForResult();
71 72
72 assertErrorResponse(); 73 assertErrorResponse();
73 } 74 }
74 75
75 /** 76 /**
76 * Tests the case when the pin hash matches. The client is expected to 77 * Tests the case when the pin hash matches. The client is expected to
77 * receive the successful response with the response code 200. 78 * receive the successful response with the response code 200.
78 * 79 *
79 * @throws Exception 80 * @throws Exception
80 */ 81 */
81 @SmallTest 82 @SmallTest
82 @Feature({"Cronet"}) 83 @Feature({"Cronet"})
84 @OnlyRunNativeCronet
83 public void testSuccessIfPinMatches() throws Exception { 85 public void testSuccessIfPinMatches() throws Exception {
84 // Get PKP hash of the real certificate 86 // Get PKP hash of the real certificate
85 X509Certificate cert = readCertFromFileInPemFormat(CERT_USED); 87 X509Certificate cert = readCertFromFileInPemFormat(CERT_USED);
86 byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert); 88 byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert);
87 89
88 addPkpSha256(mServerHost, matchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTU RE); 90 addPkpSha256(mServerHost, matchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTU RE);
89 startCronetFramework(); 91 startCronetFramework();
90 registerHostResolver(mTestFramework); 92 registerHostResolver(mTestFramework);
91 sendRequestAndWaitForResult(); 93 sendRequestAndWaitForResult();
92 94
93 assertSuccessfulResponse(); 95 assertSuccessfulResponse();
94 } 96 }
95 97
96 /** 98 /**
97 * Tests the case when the pin hash does not match and the client accesses t he subdomain of 99 * Tests the case when the pin hash does not match and the client accesses t he subdomain of
98 * the configured PKP host with includeSubdomains flag set to true. The clie nt is 100 * the configured PKP host with includeSubdomains flag set to true. The clie nt is
99 * expected to receive the error response. 101 * expected to receive the error response.
100 * 102 *
101 * @throws Exception 103 * @throws Exception
102 */ 104 */
103 @SmallTest 105 @SmallTest
104 @Feature({"Cronet"}) 106 @Feature({"Cronet"})
107 @OnlyRunNativeCronet
105 public void testIncludeSubdomainsFlagEqualTrue() throws Exception { 108 public void testIncludeSubdomainsFlagEqualTrue() throws Exception {
106 byte[] nonMatchingHash = generateSomeSha256(); 109 byte[] nonMatchingHash = generateSomeSha256();
107 addPkpSha256(mDomain, nonMatchingHash, INCLUDE_SUBDOMAINS, DISTANT_FUTUR E); 110 addPkpSha256(mDomain, nonMatchingHash, INCLUDE_SUBDOMAINS, DISTANT_FUTUR E);
108 startCronetFramework(); 111 startCronetFramework();
109 registerHostResolver(mTestFramework); 112 registerHostResolver(mTestFramework);
110 sendRequestAndWaitForResult(); 113 sendRequestAndWaitForResult();
111 114
112 assertErrorResponse(); 115 assertErrorResponse();
113 } 116 }
114 117
115 /** 118 /**
116 * Tests the case when the pin hash does not match and the client accesses t he subdomain of 119 * Tests the case when the pin hash does not match and the client accesses t he subdomain of
117 * the configured PKP host with includeSubdomains flag set to false. The cli ent is expected to 120 * the configured PKP host with includeSubdomains flag set to false. The cli ent is expected to
118 * receive the successful response with the response code 200. 121 * receive the successful response with the response code 200.
119 * 122 *
120 * @throws Exception 123 * @throws Exception
121 */ 124 */
122 @SmallTest 125 @SmallTest
123 @Feature({"Cronet"}) 126 @Feature({"Cronet"})
127 @OnlyRunNativeCronet
124 public void testIncludeSubdomainsFlagEqualFalse() throws Exception { 128 public void testIncludeSubdomainsFlagEqualFalse() throws Exception {
125 byte[] nonMatchingHash = generateSomeSha256(); 129 byte[] nonMatchingHash = generateSomeSha256();
126 addPkpSha256(mDomain, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTUR E); 130 addPkpSha256(mDomain, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTUR E);
127 startCronetFramework(); 131 startCronetFramework();
128 registerHostResolver(mTestFramework); 132 registerHostResolver(mTestFramework);
129 sendRequestAndWaitForResult(); 133 sendRequestAndWaitForResult();
130 134
131 assertSuccessfulResponse(); 135 assertSuccessfulResponse();
132 } 136 }
133 137
134 /** 138 /**
135 * Tests the case when the mismatching pin is set for some host that is diff erent from the one 139 * Tests the case when the mismatching pin is set for some host that is diff erent from the one
136 * the client wants to access. In that case the other host pinning policy sh ould not be applied 140 * the client wants to access. In that case the other host pinning policy sh ould not be applied
137 * and the client is expected to receive the successful response with the re sponse code 200. 141 * and the client is expected to receive the successful response with the re sponse code 200.
138 * 142 *
139 * @throws Exception 143 * @throws Exception
140 */ 144 */
141 @SmallTest 145 @SmallTest
142 @Feature({"Cronet"}) 146 @Feature({"Cronet"})
147 @OnlyRunNativeCronet
143 public void testSuccessIfNoPinSpecified() throws Exception { 148 public void testSuccessIfNoPinSpecified() throws Exception {
144 byte[] nonMatchingHash = generateSomeSha256(); 149 byte[] nonMatchingHash = generateSomeSha256();
145 addPkpSha256("otherhost.com", nonMatchingHash, INCLUDE_SUBDOMAINS, DISTA NT_FUTURE); 150 addPkpSha256("otherhost.com", nonMatchingHash, INCLUDE_SUBDOMAINS, DISTA NT_FUTURE);
146 startCronetFramework(); 151 startCronetFramework();
147 registerHostResolver(mTestFramework); 152 registerHostResolver(mTestFramework);
148 sendRequestAndWaitForResult(); 153 sendRequestAndWaitForResult();
149 154
150 assertSuccessfulResponse(); 155 assertSuccessfulResponse();
151 } 156 }
152 157
153 /** 158 /**
154 * Tests mismatching pins that will expire in 10 seconds. The pins should be still valid and 159 * Tests mismatching pins that will expire in 10 seconds. The pins should be still valid and
155 * enforced during the request; thus returning PIN mismatch error. 160 * enforced during the request; thus returning PIN mismatch error.
156 * 161 *
157 * @throws Exception 162 * @throws Exception
158 */ 163 */
159 @SmallTest 164 @SmallTest
160 @Feature({"Cronet"}) 165 @Feature({"Cronet"})
166 @OnlyRunNativeCronet
161 public void testSoonExpiringPin() throws Exception { 167 public void testSoonExpiringPin() throws Exception {
162 final int tenSecondsAhead = 10; 168 final int tenSecondsAhead = 10;
163 byte[] nonMatchingHash = generateSomeSha256(); 169 byte[] nonMatchingHash = generateSomeSha256();
164 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, tenSecond sAhead); 170 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, tenSecond sAhead);
165 startCronetFramework(); 171 startCronetFramework();
166 registerHostResolver(mTestFramework); 172 registerHostResolver(mTestFramework);
167 sendRequestAndWaitForResult(); 173 sendRequestAndWaitForResult();
168 174
169 assertErrorResponse(); 175 assertErrorResponse();
170 } 176 }
171 177
172 /** 178 /**
173 * Tests mismatching pins that expired 1 second ago. Since the pins have exp ired, they 179 * Tests mismatching pins that expired 1 second ago. Since the pins have exp ired, they
174 * should not be enforced during the request; thus a successful response is expected. 180 * should not be enforced during the request; thus a successful response is expected.
175 * 181 *
176 * @throws Exception 182 * @throws Exception
177 */ 183 */
178 @SmallTest 184 @SmallTest
179 @Feature({"Cronet"}) 185 @Feature({"Cronet"})
186 @OnlyRunNativeCronet
180 public void testRecentlyExpiredPin() throws Exception { 187 public void testRecentlyExpiredPin() throws Exception {
181 final int oneSecondAgo = -1; 188 final int oneSecondAgo = -1;
182 byte[] nonMatchingHash = generateSomeSha256(); 189 byte[] nonMatchingHash = generateSomeSha256();
183 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, oneSecond Ago); 190 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, oneSecond Ago);
184 startCronetFramework(); 191 startCronetFramework();
185 registerHostResolver(mTestFramework); 192 registerHostResolver(mTestFramework);
186 sendRequestAndWaitForResult(); 193 sendRequestAndWaitForResult();
187 194
188 assertSuccessfulResponse(); 195 assertSuccessfulResponse();
189 } 196 }
190 197
191 /** 198 /**
192 * Tests that host pinning is not persisted between multiple CronetEngine in stances. 199 * Tests that host pinning is not persisted between multiple CronetEngine in stances.
193 * 200 *
194 * @throws Exception 201 * @throws Exception
195 */ 202 */
196 @SmallTest 203 @SmallTest
197 @Feature({"Cronet"}) 204 @Feature({"Cronet"})
205 @OnlyRunNativeCronet
198 public void testPinsAreNotPersisted() throws Exception { 206 public void testPinsAreNotPersisted() throws Exception {
199 byte[] nonMatchingHash = generateSomeSha256(); 207 byte[] nonMatchingHash = generateSomeSha256();
200 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE); 208 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F UTURE);
201 startCronetFramework(); 209 startCronetFramework();
202 registerHostResolver(mTestFramework); 210 registerHostResolver(mTestFramework);
203 sendRequestAndWaitForResult(); 211 sendRequestAndWaitForResult();
204 assertErrorResponse(); 212 assertErrorResponse();
205 shutdownCronetEngine(); 213 shutdownCronetEngine();
206 214
207 // Restart Cronet engine and try the same request again. Since the pins are not persisted, 215 // Restart Cronet engine and try the same request again. Since the pins are not persisted,
208 // a successful response is expected. 216 // a successful response is expected.
209 createCronetEngineBuilder(); 217 createCronetEngineBuilder();
210 startCronetFramework(); 218 startCronetFramework();
211 registerHostResolver(mTestFramework); 219 registerHostResolver(mTestFramework);
212 sendRequestAndWaitForResult(); 220 sendRequestAndWaitForResult();
213 assertSuccessfulResponse(); 221 assertSuccessfulResponse();
214 } 222 }
215 223
216 /** 224 /**
217 * Tests that the client receives {@code InvalidArgumentException} when the pinned host name 225 * Tests that the client receives {@code InvalidArgumentException} when the pinned host name
218 * is invalid. 226 * is invalid.
219 * 227 *
220 * @throws Exception 228 * @throws Exception
221 */ 229 */
222 @SmallTest 230 @SmallTest
223 @Feature({"Cronet"}) 231 @Feature({"Cronet"})
232 @OnlyRunNativeCronet
224 public void testHostNameArgumentValidation() throws Exception { 233 public void testHostNameArgumentValidation() throws Exception {
225 final String label63 = "123456789-123456789-123456789-123456789-12345678 9-123456789-123"; 234 final String label63 = "123456789-123456789-123456789-123456789-12345678 9-123456789-123";
226 final String host255 = label63 + "." + label63 + "." + label63 + "." + l abel63; 235 final String host255 = label63 + "." + label63 + "." + label63 + "." + l abel63;
227 // Valid host names. 236 // Valid host names.
228 assertNoExceptionWhenHostNameIsValid("domain.com"); 237 assertNoExceptionWhenHostNameIsValid("domain.com");
229 assertNoExceptionWhenHostNameIsValid("my-domain.com"); 238 assertNoExceptionWhenHostNameIsValid("my-domain.com");
230 assertNoExceptionWhenHostNameIsValid("section4.domain.info"); 239 assertNoExceptionWhenHostNameIsValid("section4.domain.info");
231 assertNoExceptionWhenHostNameIsValid("44.domain44.info"); 240 assertNoExceptionWhenHostNameIsValid("44.domain44.info");
232 assertNoExceptionWhenHostNameIsValid("very.long.long.long.long.long.long .long.domain.com"); 241 assertNoExceptionWhenHostNameIsValid("very.long.long.long.long.long.long .long.domain.com");
233 assertNoExceptionWhenHostNameIsValid("host"); 242 assertNoExceptionWhenHostNameIsValid("host");
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 assertExceptionWhenHostNameIsInvalid("127.0.0."); 277 assertExceptionWhenHostNameIsInvalid("127.0.0.");
269 assertExceptionWhenHostNameIsInvalid("127.0.0.299"); 278 assertExceptionWhenHostNameIsInvalid("127.0.0.299");
270 } 279 }
271 280
272 /** 281 /**
273 * Tests that NullPointerException is thrown if the host name or the collect ion of pins or 282 * Tests that NullPointerException is thrown if the host name or the collect ion of pins or
274 * the expiration date is null. 283 * the expiration date is null.
275 */ 284 */
276 @SmallTest 285 @SmallTest
277 @Feature({"Cronet"}) 286 @Feature({"Cronet"})
287 @OnlyRunNativeCronet
pauljensen 2015/12/21 20:27:58 shouldn't this test pass as the checks are in Cron
Charles 2015/12/21 23:36:26 Done.
278 public void testNullArguments() { 288 public void testNullArguments() {
279 verifyExceptionWhenAddPkpArgumentIsNull(true, false, false); 289 verifyExceptionWhenAddPkpArgumentIsNull(true, false, false);
280 verifyExceptionWhenAddPkpArgumentIsNull(false, true, false); 290 verifyExceptionWhenAddPkpArgumentIsNull(false, true, false);
281 verifyExceptionWhenAddPkpArgumentIsNull(false, false, true); 291 verifyExceptionWhenAddPkpArgumentIsNull(false, false, true);
282 verifyExceptionWhenAddPkpArgumentIsNull(false, false, false); 292 verifyExceptionWhenAddPkpArgumentIsNull(false, false, false);
283 } 293 }
284 294
285 /** 295 /**
286 * Tests that IllegalArgumentException is thrown if SHA1 is passed as the va lue of a pin. 296 * Tests that IllegalArgumentException is thrown if SHA1 is passed as the va lue of a pin.
287 */ 297 */
288 @SmallTest 298 @SmallTest
289 @Feature({"Cronet"}) 299 @Feature({"Cronet"})
300 @OnlyRunNativeCronet
290 public void testIllegalArgumentExceptionWhenPinValueIsSHA1() { 301 public void testIllegalArgumentExceptionWhenPinValueIsSHA1() {
291 byte[] sha1 = new byte[20]; 302 byte[] sha1 = new byte[20];
292 try { 303 try {
293 addPkpSha256(mServerHost, sha1, EXCLUDE_SUBDOMAINS, DISTANT_FUTURE); 304 addPkpSha256(mServerHost, sha1, EXCLUDE_SUBDOMAINS, DISTANT_FUTURE);
294 } catch (IllegalArgumentException ex) { 305 } catch (IllegalArgumentException ex) {
295 // Expected exception 306 // Expected exception
296 return; 307 return;
297 } 308 }
298 fail("Expected IllegalArgumentException with pin value: " + Arrays.toStr ing(sha1)); 309 fail("Expected IllegalArgumentException with pin value: " + Arrays.toStr ing(sha1));
299 } 310 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 if (!shouldThrowNpe) { 428 if (!shouldThrowNpe) {
418 fail("Null pointer exception was not expected: " + ex.toString() ); 429 fail("Null pointer exception was not expected: " + ex.toString() );
419 } 430 }
420 return; 431 return;
421 } 432 }
422 if (shouldThrowNpe) { 433 if (shouldThrowNpe) {
423 fail("NullPointerException was expected"); 434 fail("NullPointerException was expected");
424 } 435 }
425 } 436 }
426 } 437 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698