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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 public void testErrorCodeIfPinDoesNotMatch() throws Exception { | 65 public void testErrorCodeIfPinDoesNotMatch() throws Exception { |
66 byte[] nonMatchingHash = generateSomeSha256(); | 66 byte[] nonMatchingHash = generateSomeSha256(); |
67 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F
UTURE); | 67 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F
UTURE); |
68 startCronetFramework(); | 68 startCronetFramework(); |
69 registerHostResolver(); | 69 registerHostResolver(mTestFramework); |
70 sendRequestAndWaitForResult(); | 70 sendRequestAndWaitForResult(); |
71 | 71 |
72 assertErrorResponse(); | 72 assertErrorResponse(); |
73 } | 73 } |
74 | 74 |
75 /** | 75 /** |
76 * Tests the case when the pin hash matches. The client is expected to | 76 * Tests the case when the pin hash matches. The client is expected to |
77 * receive the successful response with the response code 200. | 77 * receive the successful response with the response code 200. |
78 * | 78 * |
79 * @throws Exception | 79 * @throws Exception |
80 */ | 80 */ |
81 @SmallTest | 81 @SmallTest |
82 @Feature({"Cronet"}) | 82 @Feature({"Cronet"}) |
83 public void testSuccessIfPinMatches() throws Exception { | 83 public void testSuccessIfPinMatches() throws Exception { |
84 // Get PKP hash of the real certificate | 84 // Get PKP hash of the real certificate |
85 X509Certificate cert = readCertFromFileInPemFormat(CERT_USED); | 85 X509Certificate cert = readCertFromFileInPemFormat(CERT_USED); |
86 byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert); | 86 byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert); |
87 | 87 |
88 addPkpSha256(mServerHost, matchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTU
RE); | 88 addPkpSha256(mServerHost, matchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTU
RE); |
89 startCronetFramework(); | 89 startCronetFramework(); |
90 registerHostResolver(); | 90 registerHostResolver(mTestFramework); |
91 sendRequestAndWaitForResult(); | 91 sendRequestAndWaitForResult(); |
92 | 92 |
93 assertSuccessfulResponse(); | 93 assertSuccessfulResponse(); |
94 } | 94 } |
95 | 95 |
96 /** | 96 /** |
97 * Tests the case when the pin hash does not match and the client accesses t
he subdomain of | 97 * 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 | 98 * the configured PKP host with includeSubdomains flag set to true. The clie
nt is |
99 * expected to receive the error response. | 99 * expected to receive the error response. |
100 * | 100 * |
101 * @throws Exception | 101 * @throws Exception |
102 */ | 102 */ |
103 @SmallTest | 103 @SmallTest |
104 @Feature({"Cronet"}) | 104 @Feature({"Cronet"}) |
105 public void testIncludeSubdomainsFlagEqualTrue() throws Exception { | 105 public void testIncludeSubdomainsFlagEqualTrue() throws Exception { |
106 byte[] nonMatchingHash = generateSomeSha256(); | 106 byte[] nonMatchingHash = generateSomeSha256(); |
107 addPkpSha256(mDomain, nonMatchingHash, INCLUDE_SUBDOMAINS, DISTANT_FUTUR
E); | 107 addPkpSha256(mDomain, nonMatchingHash, INCLUDE_SUBDOMAINS, DISTANT_FUTUR
E); |
108 startCronetFramework(); | 108 startCronetFramework(); |
109 registerHostResolver(); | 109 registerHostResolver(mTestFramework); |
110 sendRequestAndWaitForResult(); | 110 sendRequestAndWaitForResult(); |
111 | 111 |
112 assertErrorResponse(); | 112 assertErrorResponse(); |
113 } | 113 } |
114 | 114 |
115 /** | 115 /** |
116 * Tests the case when the pin hash does not match and the client accesses t
he subdomain of | 116 * 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 | 117 * 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. | 118 * receive the successful response with the response code 200. |
119 * | 119 * |
120 * @throws Exception | 120 * @throws Exception |
121 */ | 121 */ |
122 @SmallTest | 122 @SmallTest |
123 @Feature({"Cronet"}) | 123 @Feature({"Cronet"}) |
124 public void testIncludeSubdomainsFlagEqualFalse() throws Exception { | 124 public void testIncludeSubdomainsFlagEqualFalse() throws Exception { |
125 byte[] nonMatchingHash = generateSomeSha256(); | 125 byte[] nonMatchingHash = generateSomeSha256(); |
126 addPkpSha256(mDomain, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTUR
E); | 126 addPkpSha256(mDomain, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_FUTUR
E); |
127 startCronetFramework(); | 127 startCronetFramework(); |
128 registerHostResolver(); | 128 registerHostResolver(mTestFramework); |
129 sendRequestAndWaitForResult(); | 129 sendRequestAndWaitForResult(); |
130 | 130 |
131 assertSuccessfulResponse(); | 131 assertSuccessfulResponse(); |
132 } | 132 } |
133 | 133 |
134 /** | 134 /** |
135 * Tests the case when the mismatching pin is set for some host that is diff
erent from the one | 135 * 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 | 136 * 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. | 137 * and the client is expected to receive the successful response with the re
sponse code 200. |
138 * | 138 * |
139 * @throws Exception | 139 * @throws Exception |
140 */ | 140 */ |
141 @SmallTest | 141 @SmallTest |
142 @Feature({"Cronet"}) | 142 @Feature({"Cronet"}) |
143 public void testSuccessIfNoPinSpecified() throws Exception { | 143 public void testSuccessIfNoPinSpecified() throws Exception { |
144 byte[] nonMatchingHash = generateSomeSha256(); | 144 byte[] nonMatchingHash = generateSomeSha256(); |
145 addPkpSha256("otherhost.com", nonMatchingHash, INCLUDE_SUBDOMAINS, DISTA
NT_FUTURE); | 145 addPkpSha256("otherhost.com", nonMatchingHash, INCLUDE_SUBDOMAINS, DISTA
NT_FUTURE); |
146 startCronetFramework(); | 146 startCronetFramework(); |
147 registerHostResolver(); | 147 registerHostResolver(mTestFramework); |
148 sendRequestAndWaitForResult(); | 148 sendRequestAndWaitForResult(); |
149 | 149 |
150 assertSuccessfulResponse(); | 150 assertSuccessfulResponse(); |
151 } | 151 } |
152 | 152 |
153 /** | 153 /** |
154 * Tests mismatching pins that will expire in 10 seconds. The pins should be
still valid and | 154 * 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. | 155 * enforced during the request; thus returning PIN mismatch error. |
156 * | 156 * |
157 * @throws Exception | 157 * @throws Exception |
158 */ | 158 */ |
159 @SmallTest | 159 @SmallTest |
160 @Feature({"Cronet"}) | 160 @Feature({"Cronet"}) |
161 public void testSoonExpiringPin() throws Exception { | 161 public void testSoonExpiringPin() throws Exception { |
162 final int tenSecondsAhead = 10; | 162 final int tenSecondsAhead = 10; |
163 byte[] nonMatchingHash = generateSomeSha256(); | 163 byte[] nonMatchingHash = generateSomeSha256(); |
164 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, tenSecond
sAhead); | 164 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, tenSecond
sAhead); |
165 startCronetFramework(); | 165 startCronetFramework(); |
166 registerHostResolver(); | 166 registerHostResolver(mTestFramework); |
167 sendRequestAndWaitForResult(); | 167 sendRequestAndWaitForResult(); |
168 | 168 |
169 assertErrorResponse(); | 169 assertErrorResponse(); |
170 } | 170 } |
171 | 171 |
172 /** | 172 /** |
173 * Tests mismatching pins that expired 1 second ago. Since the pins have exp
ired, they | 173 * 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. | 174 * should not be enforced during the request; thus a successful response is
expected. |
175 * | 175 * |
176 * @throws Exception | 176 * @throws Exception |
177 */ | 177 */ |
178 @SmallTest | 178 @SmallTest |
179 @Feature({"Cronet"}) | 179 @Feature({"Cronet"}) |
180 public void testRecentlyExpiredPin() throws Exception { | 180 public void testRecentlyExpiredPin() throws Exception { |
181 final int oneSecondAgo = -1; | 181 final int oneSecondAgo = -1; |
182 byte[] nonMatchingHash = generateSomeSha256(); | 182 byte[] nonMatchingHash = generateSomeSha256(); |
183 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, oneSecond
Ago); | 183 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, oneSecond
Ago); |
184 startCronetFramework(); | 184 startCronetFramework(); |
185 registerHostResolver(); | 185 registerHostResolver(mTestFramework); |
186 sendRequestAndWaitForResult(); | 186 sendRequestAndWaitForResult(); |
187 | 187 |
188 assertSuccessfulResponse(); | 188 assertSuccessfulResponse(); |
189 } | 189 } |
190 | 190 |
191 /** | 191 /** |
192 * Tests that host pinning is not persisted between multiple CronetEngine in
stances. | 192 * Tests that host pinning is not persisted between multiple CronetEngine in
stances. |
193 * | 193 * |
194 * @throws Exception | 194 * @throws Exception |
195 */ | 195 */ |
196 @SmallTest | 196 @SmallTest |
197 @Feature({"Cronet"}) | 197 @Feature({"Cronet"}) |
198 public void testPinsAreNotPersisted() throws Exception { | 198 public void testPinsAreNotPersisted() throws Exception { |
199 byte[] nonMatchingHash = generateSomeSha256(); | 199 byte[] nonMatchingHash = generateSomeSha256(); |
200 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F
UTURE); | 200 addPkpSha256(mServerHost, nonMatchingHash, EXCLUDE_SUBDOMAINS, DISTANT_F
UTURE); |
201 startCronetFramework(); | 201 startCronetFramework(); |
202 registerHostResolver(); | 202 registerHostResolver(mTestFramework); |
203 sendRequestAndWaitForResult(); | 203 sendRequestAndWaitForResult(); |
204 assertErrorResponse(); | 204 assertErrorResponse(); |
205 shutdownCronetEngine(); | 205 shutdownCronetEngine(); |
206 | 206 |
207 // Restart Cronet engine and try the same request again. Since the pins
are not persisted, | 207 // Restart Cronet engine and try the same request again. Since the pins
are not persisted, |
208 // a successful response is expected. | 208 // a successful response is expected. |
209 createCronetEngineBuilder(); | 209 createCronetEngineBuilder(); |
210 startCronetFramework(); | 210 startCronetFramework(); |
211 registerHostResolver(); | 211 registerHostResolver(mTestFramework); |
212 sendRequestAndWaitForResult(); | 212 sendRequestAndWaitForResult(); |
213 assertSuccessfulResponse(); | 213 assertSuccessfulResponse(); |
214 } | 214 } |
215 | 215 |
216 /** | 216 /** |
217 * Tests that the client receives {@code InvalidArgumentException} when the
pinned host name | 217 * Tests that the client receives {@code InvalidArgumentException} when the
pinned host name |
218 * is invalid. | 218 * is invalid. |
219 * | 219 * |
220 * @throws Exception | 220 * @throws Exception |
221 */ | 221 */ |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 private void startCronetFramework() { | 340 private void startCronetFramework() { |
341 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n
ull, mBuilder); | 341 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n
ull, mBuilder); |
342 } | 342 } |
343 | 343 |
344 private void shutdownCronetEngine() { | 344 private void shutdownCronetEngine() { |
345 if (mTestFramework != null && mTestFramework.mCronetEngine != null) { | 345 if (mTestFramework != null && mTestFramework.mCronetEngine != null) { |
346 mTestFramework.mCronetEngine.shutdown(); | 346 mTestFramework.mCronetEngine.shutdown(); |
347 } | 347 } |
348 } | 348 } |
349 | 349 |
350 private void registerHostResolver() { | |
351 long urlRequestContextAdapter = ((CronetUrlRequestContext) mTestFramewor
k.mCronetEngine) | |
352 .getUrlRequestContextAdapter(); | |
353 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, fals
e); | |
354 } | |
355 | |
356 private byte[] generateSomeSha256() { | 350 private byte[] generateSomeSha256() { |
357 byte[] sha256 = new byte[32]; | 351 byte[] sha256 = new byte[32]; |
358 Arrays.fill(sha256, (byte) 58); | 352 Arrays.fill(sha256, (byte) 58); |
359 return sha256; | 353 return sha256; |
360 } | 354 } |
361 | 355 |
362 private void addPkpSha256( | 356 private void addPkpSha256( |
363 String host, byte[] pinHashValue, boolean includeSubdomain, int maxA
geInSec) { | 357 String host, byte[] pinHashValue, boolean includeSubdomain, int maxA
geInSec) { |
364 Set<byte[]> hashes = new HashSet<>(); | 358 Set<byte[]> hashes = new HashSet<>(); |
365 hashes.add(pinHashValue); | 359 hashes.add(pinHashValue); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 if (!shouldThrowNpe) { | 414 if (!shouldThrowNpe) { |
421 fail("Null pointer exception was not expected: " + ex.toString()
); | 415 fail("Null pointer exception was not expected: " + ex.toString()
); |
422 } | 416 } |
423 return; | 417 return; |
424 } | 418 } |
425 if (shouldThrowNpe) { | 419 if (shouldThrowNpe) { |
426 fail("NullPointerException was expected"); | 420 fail("NullPointerException was expected"); |
427 } | 421 } |
428 } | 422 } |
429 } | 423 } |
OLD | NEW |