1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/* 2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more 3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * contributor license agreements. See the NOTICE file distributed with 4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * this work for additional information regarding copyright ownership. 5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * (the "License"); you may not use this file except in compliance with 7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * the License. You may obtain a copy of the License at 8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Unless required by applicable law or agreed to in writing, software 12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * See the License for the specific language governing permissions and 15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * limitations under the License. 16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/** 19561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes* @author Boris V. Kuznetsov 20561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes*/ 21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage javax.net.ssl; 23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.IOException; 25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.Security; 26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.net.SocketException; 27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport junit.framework.TestCase; 29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/** 32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Tests for <code>SSLSocketFactory</code> class methods. 33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class SSLSocketFactoryTest extends TestCase { 36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes private SSLSocketFactory customSocketFactory; 38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /* 40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @see TestCase#setUp() 41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes @Override 43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes protected void setUp() throws Exception { 44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes super.setUp(); 45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes String defaultName = Security.getProperty("ssl.SocketFactory.provider"); 46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (defaultName != null) { 47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes customSocketFactory = (SSLSocketFactory) Class.forName( 49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes defaultName, true, ClassLoader.getSystemClassLoader()) 50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes .newInstance(); 51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (Exception e) { 52561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (customSocketFactory == null) { 55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes SSLContext context = DefaultSSLContext.getContext(); 56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (context != null) { 57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes customSocketFactory = context.getSocketFactory(); 58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /* 63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @see TestCase#tearDown() 64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes @Override 66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes protected void tearDown() throws Exception { 67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes super.tearDown(); 68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public final void testGetDefault() { 71561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault(); 72561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (customSocketFactory != null) { 73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (!factory.getClass().getName().equals(customSocketFactory.getClass().getName())) { 74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("incorrect instance: " + factory.getClass()+ 75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes " expected: " + customSocketFactory.getClass().getName()); 76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } else { 78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (!(factory instanceof DefaultSSLSocketFactory)) { 79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("incorrect instance " + factory.getClass()); 80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (factory.getDefaultCipherSuites().length != 0) { 82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()"); 83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes if (factory.getSupportedCipherSuites().length != 0) { 85561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()"); 86561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes factory.createSocket("localhost", 2021); 89561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("No expected SocketException"); 90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (SocketException e) { 91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (IOException e) { 92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail(e.toString()); 93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes} 98