diff options
author | Alex Klyubin <klyubin@google.com> | 2014-11-24 10:12:06 -0800 |
---|---|---|
committer | Alex Klyubin <klyubin@google.com> | 2014-12-04 14:20:46 -0800 |
commit | 4f3e1286fa50e720069d891bd91014fd830514b4 (patch) | |
tree | ac27a64545190e78ce204a0d74ee1a13e1c3aa63 | |
parent | 59c2a0aa48b2e7f6770e29993332fecadb145f21 (diff) | |
download | libcore-4f3e1286fa50e720069d891bd91014fd830514b4.zip libcore-4f3e1286fa50e720069d891bd91014fd830514b4.tar.gz libcore-4f3e1286fa50e720069d891bd91014fd830514b4.tar.bz2 |
Assert default SSLSocketFactory and HostnameVerifier actually used.
This adds tests to assert that default SSLSocketFactory and
HostnameVerifier of HttpsURLConnection are actually used for new
instances of HttpsURLConnection.
The contract of HttpsURLConnection.get/setDefaultHostnameVerifier
and HttpsURLConnection.get/setDefaultSSLSocketFactory is that the
HostnameVerifier and SSLSocketFactory instances returned/set via
these methods are used for new instances of HttpsURLConnection.
Bug: 18481199
Change-Id: Id976c2fc31f0e3efed9284860aa35144a729dabb
-rw-r--r-- | luni/src/test/java/libcore/javax/net/ssl/HttpsURLConnectionTest.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/luni/src/test/java/libcore/javax/net/ssl/HttpsURLConnectionTest.java b/luni/src/test/java/libcore/javax/net/ssl/HttpsURLConnectionTest.java new file mode 100644 index 0000000..cbaea20 --- /dev/null +++ b/luni/src/test/java/libcore/javax/net/ssl/HttpsURLConnectionTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.javax.net.ssl; + +import junit.framework.TestCase; + +import java.io.IOException; +import java.net.URL; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocketFactory; + +public class HttpsURLConnectionTest extends TestCase { + + /** + * HTTPS URL which cannot be resolved and is thus safe to use in tests where network traffic + * should be avoided. + */ + private static final String UNRESOLVABLE_HTTPS_URL = "https:///"; + + public void testDefaultHostnameVerifierNotNull() { + assertNotNull(HttpsURLConnection.getDefaultHostnameVerifier()); + } + + public void testDefaultHostnameVerifierUsedForNewConnectionsByDefault() throws IOException { + HostnameVerifier originalHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); + HttpsURLConnection connection = + (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); + try { + assertSame(originalHostnameVerifier, connection.getHostnameVerifier()); + } finally { + connection.disconnect(); + } + + HostnameVerifier anotherVerifier = new FakeHostnameVerifier(); + try { + HttpsURLConnection.setDefaultHostnameVerifier(anotherVerifier); + connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); + try { + assertSame(anotherVerifier, connection.getHostnameVerifier()); + } finally { + connection.disconnect(); + } + + HttpsURLConnection.setDefaultHostnameVerifier(originalHostnameVerifier); + connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); + try { + assertSame(originalHostnameVerifier, connection.getHostnameVerifier()); + } finally { + connection.disconnect(); + } + } finally { + HttpsURLConnection.setDefaultHostnameVerifier(originalHostnameVerifier); + } + } + + public void testDefaultSSLSocketFactoryNotNull() { + assertNotNull(HttpsURLConnection.getDefaultSSLSocketFactory()); + } + + public void testDefaultSSLSocketFactoryUsedForNewConnectionsByDefault() throws IOException { + SSLSocketFactory originalFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); + HttpsURLConnection connection = + (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); + try { + assertSame(originalFactory, connection.getSSLSocketFactory()); + } finally { + connection.disconnect(); + } + + SSLSocketFactory anotherFactory = new SSLSocketFactoryTest.FakeSSLSocketFactory(); + try { + HttpsURLConnection.setDefaultSSLSocketFactory(anotherFactory); + connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); + try { + assertSame(anotherFactory, connection.getSSLSocketFactory()); + } finally { + connection.disconnect(); + } + + HttpsURLConnection.setDefaultSSLSocketFactory(originalFactory); + connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); + try { + assertSame(originalFactory, connection.getSSLSocketFactory()); + } finally { + connection.disconnect(); + } + } finally { + HttpsURLConnection.setDefaultSSLSocketFactory(originalFactory); + } + } + + private static class FakeHostnameVerifier implements HostnameVerifier { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + } +} |