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

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: Enabled user agent test Created 4 years, 11 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 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,
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 if (!shouldThrowNpe) { 425 if (!shouldThrowNpe) {
418 fail("Null pointer exception was not expected: " + ex.toString() ); 426 fail("Null pointer exception was not expected: " + ex.toString() );
419 } 427 }
420 return; 428 return;
421 } 429 }
422 if (shouldThrowNpe) { 430 if (shouldThrowNpe) {
423 fail("NullPointerException was expected"); 431 fail("NullPointerException was expected");
424 } 432 }
425 } 433 }
426 } 434 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698