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 java.net.InetAddress; 20 import java.net.InetSocketAddress; 21 import java.net.ServerSocket; 22 import java.net.Socket; 23 import java.net.SocketException; 24 import libcore.java.security.StandardNames; 25 import javax.net.ServerSocketFactory; 26 import javax.net.SocketFactory; 27 import javax.net.ssl.SSLSocket; 28 import javax.net.ssl.SSLSocketFactory; 29 import junit.framework.TestCase; 30 31 public class SSLSocketFactoryTest extends TestCase { 32 public void test_SSLSocketFactory_getDefault() { 33 SocketFactory sf = SSLSocketFactory.getDefault(); 34 assertNotNull(sf); 35 assertTrue(SSLSocketFactory.class.isAssignableFrom(sf.getClass())); 36 } 37 38 public void test_SSLSocketFactory_getDefaultCipherSuites() { 39 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 40 String[] cipherSuites = sf.getDefaultCipherSuites(); 41 StandardNames.assertDefaultCipherSuites(cipherSuites); 42 assertNotSame(cipherSuites, sf.getDefaultCipherSuites()); 43 } 44 45 public void test_SSLSocketFactory_getSupportedCipherSuites() { 46 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 47 String[] cipherSuites = sf.getSupportedCipherSuites(); 48 StandardNames.assertValidCipherSuites(StandardNames.CIPHER_SUITES, cipherSuites); 49 assertNotSame(cipherSuites, sf.getSupportedCipherSuites()); 50 } 51 52 public void test_SSLSocketFactory_createSocket() throws Exception { 53 try { 54 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 55 Socket s = sf.createSocket(null, null, -1, false); 56 fail(); 57 } catch (NullPointerException expected) { 58 } 59 60 try { 61 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 62 Socket ssl = sf.createSocket(new Socket(), null, -1, false); 63 fail(); 64 } catch (SocketException expected) { 65 } 66 67 ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(0); 68 InetSocketAddress sa = (InetSocketAddress) ss.getLocalSocketAddress(); 69 InetAddress host = sa.getAddress(); 70 int port = sa.getPort(); 71 Socket s = new Socket(host, port); 72 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 73 Socket ssl = sf.createSocket(s, null, -1, false); 74 assertNotNull(ssl); 75 assertTrue(SSLSocket.class.isAssignableFrom(ssl.getClass())); 76 } 77 } 78