Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/net/HpkpTest.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/HpkpTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/HpkpTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a89096ddf53fa19c2b860761fc0a31bb3fbdcc3a |
| --- /dev/null |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/net/HpkpTest.java |
| @@ -0,0 +1,218 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net; |
| + |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.net.test.util.CertTestUtil; |
| + |
| +import java.io.ByteArrayInputStream; |
| +import java.security.cert.CertificateFactory; |
| +import java.security.cert.X509Certificate; |
| +import java.util.ArrayList; |
| +import java.util.Arrays; |
| +import java.util.Collection; |
| + |
| +/** |
| + * Public-Key-Pinning tests of Cronet Java API. |
|
xunjieli
2015/11/06 15:47:54
Should we say **dynamic** public key pinning here?
kapishnikov
2015/11/06 16:18:13
I would like to clarify the terminology here. I wa
xunjieli
2015/11/06 16:43:50
I got the impression that we do not want to do sta
davidben
2015/11/06 16:53:13
"static" vs "dynamic" is whatever we want to call
|
| + */ |
| +public class HpkpTest extends CronetTestBase { |
| + private static final String CERT_USED = "quic_test.example.com.crt"; |
| + private static final String[] CERTS_USED = {CERT_USED}; |
| + |
| + private CronetTestFramework mTestFramework; |
| + private CronetEngine.Builder mBuilder; |
| + private TestUrlRequestCallback mListener; |
| + private String mServerUrl; // https://test.example.com:6121 |
| + private String mServerHost; // test.example.com |
| + private String mDomain; // example.com |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + // Start QUIC Test Server |
| + System.loadLibrary("cronet_tests"); |
| + QuicTestServer.startQuicTestServer(getContext()); |
| + mServerUrl = QuicTestServer.getServerURL(); |
| + mServerHost = QuicTestServer.getServerHost(); |
| + mDomain = mServerHost.substring(mServerHost.indexOf('.') + 1, mServerHost.length()); |
| + |
| + // Set common CronetEngine parameters |
| + mBuilder = new CronetEngine.Builder(getContext()); |
| + mBuilder.enableQUIC(true); |
| + mBuilder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getServerPort(), |
| + QuicTestServer.getServerPort()); |
| + mBuilder.setStoragePath(CronetTestFramework.getTestStorage(getContext())); |
| + mBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1000 * 1024); |
| + mBuilder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVerifier(CERTS_USED)); |
| + } |
| + |
| + @Override |
| + protected void tearDown() throws Exception { |
| + QuicTestServer.shutdownQuicTestServer(); |
| + super.tearDown(); |
| + } |
| + |
| + /** |
| + * Tests the case when the pin hash does not match. The client is expected to |
| + * receive the error response. |
| + * |
| + * @throws Exception |
| + */ |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testErrorCodeIfPinDoesNotMatch() throws Exception { |
| + byte[] nonMatchingHash = generateSomeSha256(); |
| + addPkpSha256(mServerHost, nonMatchingHash, false); |
| + startCronetFramework(); |
| + registerHostResolver(); |
| + sendRequestAndWaitForResult(); |
| + |
| + assertErrorResponse(); |
| + } |
| + |
| + /** |
| + * Tests the case when the pin hash matches. The client is expected to |
| + * receive the successful response with the response code 200. |
| + * |
| + * @throws Exception |
| + */ |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testSuccessIfPinMatches() throws Exception { |
| + // Get PKP hash of the real certificate |
| + X509Certificate cert = readCertFromFileInPemFormat(CERT_USED); |
| + byte[] matchingHash = CertTestUtil.getPublicKeySha256(cert); |
| + |
| + addPkpSha256(mServerHost, matchingHash, false); |
| + startCronetFramework(); |
| + registerHostResolver(); |
| + sendRequestAndWaitForResult(); |
| + |
| + assertSuccessfulResponse(); |
| + } |
| + |
| + /** |
| + * Tests the case when the pin hash does not match and the client accesses the subdomain of |
| + * the configured HPKP host with includeSubdomains flag set to true. The client is |
| + * expected to receive the error response. |
| + * |
| + * @throws Exception |
| + */ |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testIncludeSubdomainsFlagEqualTrue() throws Exception { |
| + addPkpSha256(mDomain, generateSomeSha256(), true); |
| + startCronetFramework(); |
| + registerHostResolver(); |
| + sendRequestAndWaitForResult(); |
| + |
| + assertErrorResponse(); |
| + } |
| + |
| + /** |
| + * Tests the case when the pin hash does not match and the client accesses the subdomain of |
| + * the configured HPKP host with includeSubdomains flag set to false. The client is expected to |
| + * receive the successful response with the response code 200. |
| + * |
| + * @throws Exception |
| + */ |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testIncludeSubdomainsFlagEqualFalse() throws Exception { |
| + addPkpSha256(mDomain, generateSomeSha256(), false); |
| + startCronetFramework(); |
| + registerHostResolver(); |
| + sendRequestAndWaitForResult(); |
| + |
| + assertSuccessfulResponse(); |
| + } |
| + |
| + /** |
| + * Tests the case when the mismatching pin is set for some host that is different from the one |
| + * the client wants to access. In that case the other host pinning policy should not be applied |
| + * and the client is expected to receive the successful response with the response code 200. |
| + * |
| + * @throws Exception |
| + */ |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + public void testSuccessIfNoPinSpecified() throws Exception { |
| + addPkpSha256("somerandomhost.com", generateSomeSha256(), true); |
| + startCronetFramework(); |
| + registerHostResolver(); |
| + sendRequestAndWaitForResult(); |
| + |
| + assertSuccessfulResponse(); |
| + } |
| + |
| + /** |
| + * Asserts that the response from the server contains an HPKP error. |
| + * TODO(kapishnikov) currently QUIC returns ERR_QUIC_PROTOCOL_ERROR instead of expected |
|
xunjieli
2015/11/06 15:47:54
nit: Probably only one TODO is needed. There shoul
kapishnikov
2015/11/06 16:18:13
Done.
|
| + * TODO(kapishnikov) ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN error code when the pin doesn't match. |
| + * TODO(kapishnikov) This method should be changed when the bug is resolved. |
| + * TODO(kapishnikov) See http://crbug.com/548378 |
| + */ |
| + private void assertErrorResponse() { |
| + assertNotNull("Expected an error", mListener.mError); |
| + int errorCode = mListener.mError.netError(); |
| + boolean correctErrorCode = errorCode == NetError.ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN |
| + || errorCode == NetError.ERR_QUIC_PROTOCOL_ERROR; |
| + assertTrue(String.format("Incorrect error code. Expected %s or %s but received %s", |
| + NetError.ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN, |
| + NetError.ERR_QUIC_PROTOCOL_ERROR, errorCode), |
| + correctErrorCode); |
| + } |
| + |
| + /** |
| + * Asserts a successful response with response code 200. |
| + */ |
| + private void assertSuccessfulResponse() { |
| + if (mListener.mError != null) { |
| + fail("Did not expect an error but got error code " + mListener.mError.mNetError); |
| + } |
| + assertNotNull("Expected non-null response from the server", mListener.mResponseInfo); |
| + assertEquals(200, mListener.mResponseInfo.getHttpStatusCode()); |
| + } |
| + |
| + private void startCronetFramework() { |
| + mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mBuilder); |
| + } |
| + |
| + private void registerHostResolver() { |
| + long urlRequestContextAdapter = ((CronetUrlRequestContext) mTestFramework.mCronetEngine) |
| + .getUrlRequestContextAdapter(); |
| + NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, false); |
| + } |
| + |
| + private byte[] generateSomeSha256() { |
| + byte[] sha256 = new byte[32]; |
| + Arrays.fill(sha256, (byte) 58); |
| + return sha256; |
| + } |
| + |
| + private void addPkpSha256(String host, byte[] pinHashValue, boolean includeSubdomain) { |
| + Collection<byte[]> hashes = new ArrayList<>(); |
| + hashes.add(pinHashValue); |
| + mBuilder.addPublicKeyPins(host, hashes, includeSubdomain); |
| + } |
| + |
| + private void sendRequestAndWaitForResult() { |
| + mListener = new TestUrlRequestCallback(); |
| + |
| + String quicURL = mServerUrl + "/simple.txt"; |
| + UrlRequest.Builder requestBuilder = new UrlRequest.Builder( |
| + quicURL, mListener, mListener.getExecutor(), mTestFramework.mCronetEngine); |
| + requestBuilder.build().start(); |
| + mListener.blockForDone(); |
| + } |
| + |
| + private X509Certificate readCertFromFileInPemFormat(String certFileName) throws Exception { |
| + byte[] certDer = CertTestUtil.pemToDer(CertTestUtil.CERTS_DIRECTORY + certFileName); |
| + CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| + return (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(certDer)); |
| + } |
| +} |