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