1 /* 2 * Copyright (C) 2010 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 libcore.javax.net.ssl; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertNotSame; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.fail; 23 24 import java.net.InetAddress; 25 import java.net.InetSocketAddress; 26 import java.net.ServerSocket; 27 import java.net.Socket; 28 import java.net.SocketException; 29 import javax.net.ServerSocketFactory; 30 import javax.net.SocketFactory; 31 import javax.net.ssl.SSLSocket; 32 import javax.net.ssl.SSLSocketFactory; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.junit.runners.JUnit4; 36 37 @RunWith(JUnit4.class) 38 public class SSLSocketFactoryTest extends AbstractSSLTest { 39 40 @Test test_SSLSocketFactory_getDefault()41 public void test_SSLSocketFactory_getDefault() { 42 SocketFactory sf = SSLSocketFactory.getDefault(); 43 assertNotNull(sf); 44 assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass())); 45 } 46 47 @Test test_SSLSocketFactory_defaultConfiguration()48 public void test_SSLSocketFactory_defaultConfiguration() throws Exception { 49 SSLConfigurationAsserts.assertSSLSocketFactoryDefaultConfiguration( 50 (SSLSocketFactory) SSLSocketFactory.getDefault()); 51 } 52 53 @Test test_SSLSocketFactory_getDefaultCipherSuitesReturnsCopies()54 public void test_SSLSocketFactory_getDefaultCipherSuitesReturnsCopies() { 55 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 56 assertNotSame(sf.getDefaultCipherSuites(), sf.getDefaultCipherSuites()); 57 } 58 59 @Test test_SSLSocketFactory_getSupportedCipherSuitesReturnsCopies()60 public void test_SSLSocketFactory_getSupportedCipherSuitesReturnsCopies() { 61 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 62 assertNotSame(sf.getSupportedCipherSuites(), sf.getSupportedCipherSuites()); 63 } 64 65 @Test test_SSLSocketFactory_createSocket()66 public void test_SSLSocketFactory_createSocket() throws Exception { 67 try { 68 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 69 sf.createSocket(null, null, -1, false); 70 fail(); 71 } catch (NullPointerException expected) { 72 // Ignored. 73 } 74 75 try { 76 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 77 sf.createSocket(new Socket(), null, -1, false); 78 fail(); 79 } catch (SocketException expected) { 80 // Ignored. 81 } 82 83 ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(0); 84 InetSocketAddress sa = (InetSocketAddress) ss.getLocalSocketAddress(); 85 InetAddress host = sa.getAddress(); 86 int port = sa.getPort(); 87 Socket s = new Socket(host, port); 88 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 89 Socket ssl = sf.createSocket(s, null, -1, false); 90 assertNotNull(ssl); 91 assertTrue(SSLSocket.class.isAssignableFrom(ssl.getClass())); 92 } 93 } 94