/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 org.apache.harmony.xnet.provider.jsse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.util.Arrays; import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * SSLSocketImplTest */ public class SSLSocketFactoriesTest extends TestCase { // turn on/off the debug logging private boolean doLog = false; /** * Sets up the test case. */ @Override public void setUp() throws Exception { super.setUp(); if (doLog) { System.out.println("========================"); System.out.println("====== Running the test: " + getName()); System.out.println("========================"); } } @Override public void tearDown() throws Exception { super.tearDown(); } /** * Tests default initialized factories. */ public void testDefaultInitialized() throws Exception { SSLServerSocketFactory ssfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLSocketFactory sfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); assertNotNull(ssfactory.getDefaultCipherSuites()); assertNotNull(ssfactory.getSupportedCipherSuites()); assertNotNull(ssfactory.createServerSocket()); assertNotNull(sfactory.getDefaultCipherSuites()); assertNotNull(sfactory.getSupportedCipherSuites()); assertNotNull(sfactory.createSocket()); } public void testSocketCreation() throws Throwable { SSLSocketFactory socketFactory = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters()); SSLServerSocketFactory serverSocketFactory = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters()); String[] enabled = {"TLS_RSA_WITH_RC4_128_MD5"}; for (int i=0; i<4; i++) { SSLServerSocket ssocket; switch (i) { case 0: if (doLog) { System.out.println( "*** ServerSocketFactory.createServerSocket()"); } ssocket = (SSLServerSocket) serverSocketFactory.createServerSocket(); ssocket.bind(null); break; case 1: if (doLog) { System.out.println( "*** ServerSocketFactory.createServerSocket(int)"); } ssocket = (SSLServerSocket) serverSocketFactory.createServerSocket(0); break; case 2: if (doLog) { System.out.println( "*** ServerSocketFactory.createServerSocket(int,int)"); } ssocket = (SSLServerSocket) serverSocketFactory.createServerSocket(0, 6); break; default: if (doLog) { System.out.println("*** ServerSocketFactory." + "createServerSocket(int,int,InetAddress)"); } ssocket = (SSLServerSocket) serverSocketFactory.createServerSocket(0, 6, null); break; } ssocket.setUseClientMode(false); ssocket.setEnabledCipherSuites(enabled); for (int j=0; j<6; j++) { SSLSocket csocket; switch (j) { case 0: if (doLog) { System.out.println( "=== SocketFactory.createSocket()"); } csocket = (SSLSocket) socketFactory.createSocket(); csocket.connect( new InetSocketAddress("localhost", ssocket.getLocalPort())); break; case 1: if (doLog) { System.out.println( "=== SocketFactory.createSocket(String,int)"); } csocket = (SSLSocket) socketFactory.createSocket("localhost", ssocket.getLocalPort()); break; case 2: if (doLog) { System.out.println("=== SocketFactory.createSocket(" + "String,int,InetAddress,int)"); } csocket = (SSLSocket) socketFactory.createSocket("localhost", ssocket.getLocalPort(), InetAddress.getByName("localhost"), 0); break; case 3: if (doLog) { System.out.println("=== SocketFactory.createSocket(" + "InetAddress,int)"); } csocket = (SSLSocket) socketFactory.createSocket( InetAddress.getByName("localhost"), ssocket.getLocalPort()); break; case 4: if (doLog) { System.out.println("=== SocketFactory.createSocket(" + "InetAddress,int,InetAddress,int)"); } csocket = (SSLSocket) socketFactory.createSocket( InetAddress.getByName("localhost"), ssocket.getLocalPort(), InetAddress.getByName("localhost"), 0); break; default: if (doLog) { System.out.println( "=== SSLSocketFactory.createSocket(" + "socket,String,int,boolean)"); } Socket socket = new Socket( InetAddress.getByName("localhost"), ssocket.getLocalPort()); csocket = (SSLSocket) socketFactory.createSocket( socket, "localhost", ssocket.getLocalPort(), true); break; } csocket.setUseClientMode(true); csocket.setEnabledCipherSuites(enabled); doTest(ssocket, csocket); } } } /** * SSLSocketFactory.getSupportedCipherSuites() method testing. */ public void testGetSupportedCipherSuites1() throws Exception { SSLSocketFactory socketFactory = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters()); String[] supported = socketFactory.getSupportedCipherSuites(); assertNotNull(supported); supported[0] = "NOT_SUPPORTED_CIPHER_SUITE"; supported = socketFactory.getSupportedCipherSuites(); for (int i=0; i