01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /**
19: * @author Boris V. Kuznetsov
20: * @version $Revision$
21: */package javax.net.ssl;
22:
23: import java.io.IOException;
24: import java.security.Security;
25: import java.net.SocketException;
26:
27: import junit.framework.TestCase;
28:
29: /**
30: * Tests for <code>SSLSocketFactory</code> class methods.
31: *
32: */
33: public class SSLSocketFactoryTest extends TestCase {
34:
35: private SSLSocketFactory customSocketFactory;
36:
37: /*
38: * @see TestCase#setUp()
39: */
40: protected void setUp() throws Exception {
41: super .setUp();
42: String defaultName = Security
43: .getProperty("ssl.SocketFactory.provider");
44: if (defaultName != null) {
45: try {
46: customSocketFactory = (SSLSocketFactory) Class.forName(
47: defaultName, true,
48: ClassLoader.getSystemClassLoader())
49: .newInstance();
50: } catch (Exception e) {
51: }
52: }
53: if (customSocketFactory == null) {
54: SSLContext context = DefaultSSLContext.getContext();
55: if (context != null) {
56: customSocketFactory = context.getSocketFactory();
57: }
58: }
59: }
60:
61: /*
62: * @see TestCase#tearDown()
63: */
64: protected void tearDown() throws Exception {
65: super .tearDown();
66: }
67:
68: public final void testGetDefault() {
69: SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
70: .getDefault();
71: if (customSocketFactory != null) {
72: if (!factory.getClass().getName().equals(
73: customSocketFactory.getClass().getName())) {
74: fail("incorrect instance: " + factory.getClass()
75: + " expected: "
76: + customSocketFactory.getClass().getName());
77: }
78: } else {
79: if (!(factory instanceof DefaultSSLSocketFactory)) {
80: fail("incorrect instance " + factory.getClass());
81: }
82: if (factory.getDefaultCipherSuites().length != 0) {
83: fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()");
84: }
85: if (factory.getSupportedCipherSuites().length != 0) {
86: fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()");
87: }
88: try {
89: factory.createSocket("localhost", 2021);
90: fail("No expected SocketException");
91: } catch (SocketException e) {
92: } catch (IOException e) {
93: fail(e.toString());
94: }
95: }
96: }
97:
98: }
|