1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.conscrypt.javax.net.ssl; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertSame; 21 22 import java.io.IOException; 23 import java.net.InetAddress; 24 import java.net.Socket; 25 import java.net.URL; 26 import javax.net.ssl.HostnameVerifier; 27 import javax.net.ssl.HttpsURLConnection; 28 import javax.net.ssl.SSLSession; 29 import javax.net.ssl.SSLSocketFactory; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 @RunWith(JUnit4.class) 35 public class HttpsURLConnectionTest { 36 /** 37 * HTTPS URL which cannot be resolved and is thus safe to use in tests where network traffic 38 * should be avoided. 39 */ 40 private static final String UNRESOLVABLE_HTTPS_URL = "https:///"; 41 42 @Test 43 public void testDefaultHostnameVerifierNotNull() { 44 assertNotNull(HttpsURLConnection.getDefaultHostnameVerifier()); 45 } 46 47 @Test 48 public void testDefaultHostnameVerifierUsedForNewConnectionsByDefault() throws IOException { 49 HostnameVerifier originalHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); 50 HttpsURLConnection connection = 51 (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); 52 try { 53 assertSame(originalHostnameVerifier, connection.getHostnameVerifier()); 54 } finally { 55 connection.disconnect(); 56 } 57 58 HostnameVerifier anotherVerifier = new FakeHostnameVerifier(); 59 try { 60 HttpsURLConnection.setDefaultHostnameVerifier(anotherVerifier); 61 connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); 62 try { 63 assertSame(anotherVerifier, connection.getHostnameVerifier()); 64 } finally { 65 connection.disconnect(); 66 } 67 68 HttpsURLConnection.setDefaultHostnameVerifier(originalHostnameVerifier); 69 connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); 70 try { 71 assertSame(originalHostnameVerifier, connection.getHostnameVerifier()); 72 } finally { 73 connection.disconnect(); 74 } 75 } finally { 76 HttpsURLConnection.setDefaultHostnameVerifier(originalHostnameVerifier); 77 } 78 } 79 80 @Test 81 public void testDefaultSSLSocketFactoryNotNull() { 82 assertNotNull(HttpsURLConnection.getDefaultSSLSocketFactory()); 83 } 84 85 @Test 86 public void testDefaultSSLSocketFactoryUsedForNewConnectionsByDefault() throws IOException { 87 SSLSocketFactory originalFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); 88 HttpsURLConnection connection = 89 (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); 90 try { 91 assertSame(originalFactory, connection.getSSLSocketFactory()); 92 } finally { 93 connection.disconnect(); 94 } 95 96 SSLSocketFactory anotherFactory = new FakeSSLSocketFactory(); 97 try { 98 HttpsURLConnection.setDefaultSSLSocketFactory(anotherFactory); 99 connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); 100 try { 101 assertSame(anotherFactory, connection.getSSLSocketFactory()); 102 } finally { 103 connection.disconnect(); 104 } 105 106 HttpsURLConnection.setDefaultSSLSocketFactory(originalFactory); 107 connection = (HttpsURLConnection) new URL(UNRESOLVABLE_HTTPS_URL).openConnection(); 108 try { 109 assertSame(originalFactory, connection.getSSLSocketFactory()); 110 } finally { 111 connection.disconnect(); 112 } 113 } finally { 114 HttpsURLConnection.setDefaultSSLSocketFactory(originalFactory); 115 } 116 } 117 118 private static final class FakeHostnameVerifier implements HostnameVerifier { 119 @Override 120 public boolean verify(String hostname, SSLSession session) { 121 return true; 122 } 123 } 124 125 private static final class FakeSSLSocketFactory extends SSLSocketFactory { 126 FakeSSLSocketFactory() {} 127 128 @Override 129 public String[] getDefaultCipherSuites() { 130 throw new UnsupportedOperationException(); 131 } 132 133 @Override 134 public String[] getSupportedCipherSuites() { 135 throw new UnsupportedOperationException(); 136 } 137 138 @Override 139 public Socket createSocket(Socket s, String host, int port, boolean autoClose) { 140 throw new UnsupportedOperationException(); 141 } 142 143 @Override 144 public Socket createSocket( 145 InetAddress address, int port, InetAddress localAddress, int localPort) { 146 throw new UnsupportedOperationException(); 147 } 148 149 @Override 150 public Socket createSocket(InetAddress host, int port) { 151 throw new UnsupportedOperationException(); 152 } 153 154 @Override 155 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) { 156 throw new UnsupportedOperationException(); 157 } 158 159 @Override 160 public Socket createSocket(String host, int port) { 161 throw new UnsupportedOperationException(); 162 } 163 } 164 } 165