1 /* 2 * Copyright (C) 2007 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 package org.apache.harmony.tests.javax.net.ssl; 17 18 import java.io.IOException; 19 import java.net.ServerSocket; 20 import java.net.Socket; 21 import java.net.UnknownHostException; 22 23 import javax.net.SocketFactory; 24 import javax.net.ssl.SSLSocketFactory; 25 26 import junit.framework.TestCase; 27 28 public class SSLSocketFactoryTest extends TestCase { 29 30 private ServerSocket ss; 31 32 protected int startServer(String name) { 33 try { 34 ss = new ServerSocket(0); 35 } catch (IOException e) { 36 fail(name + ": " + e); 37 } 38 return ss.getLocalPort(); 39 } 40 41 /** 42 * javax.net.ssl.SSLSocketFactory#SSLSocketFactory() 43 */ 44 public void test_Constructor() { 45 try { 46 SocketFactory sf = SSLSocketFactory.getDefault(); 47 assertTrue(sf instanceof SSLSocketFactory); 48 } catch (Exception e) { 49 fail("Unexpected exception " + e.toString()); 50 } 51 } 52 53 /** 54 * javax.net.ssl.SSLSocketFactory#getDefault() 55 */ 56 public void test_getDefault() { 57 assertNotNull("Incorrect default socket factory", 58 SSLSocketFactory.getDefault()); 59 } 60 61 /** 62 * javax.net.ssl.SSLSocketFactory#createSocket(Socket s, String host, int port, boolean autoClose) 63 */ 64 public void test_createSocket() throws Exception { 65 SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault(); 66 int sport = startServer("test_createSocket()"); 67 int[] invalid = { 68 Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE 69 }; 70 71 72 Socket st = new Socket("localhost", sport); 73 Socket s = sf.createSocket(st, "localhost", sport, false); 74 assertFalse(s.isClosed()); 75 76 st = new Socket("localhost", sport); 77 s = sf.createSocket(st, "localhost", sport, true); 78 s.close(); 79 assertTrue(st.isClosed()); 80 81 try { 82 sf.createSocket(null, "localhost", sport, true); 83 fail(); 84 } catch (NullPointerException expected) { 85 } 86 87 for (int i = 0; i < invalid.length; i++) { 88 try { 89 s = sf.createSocket(new Socket(), "localhost", 1080, false); 90 fail(); 91 } catch (IOException expected) { 92 } 93 } 94 95 try { 96 st = new Socket("1.2.3.4hello", sport); 97 s = sf.createSocket(st, "1.2.3.4hello", sport, false); 98 fail(); 99 } catch (UnknownHostException expected) { 100 } 101 } 102 103 /** 104 * javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites() 105 */ 106 public void test_getDefaultCipherSuites() { 107 try { 108 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 109 assertTrue("no default cipher suites returned", 110 sf.getDefaultCipherSuites().length > 0); 111 } catch (Exception e) { 112 fail("Unexpected exception " + e.toString()); 113 } 114 } 115 116 /** 117 * javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites() 118 */ 119 public void test_getSupportedCipherSuites() { 120 try { 121 SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 122 assertTrue("no supported cipher suites returned", 123 sf.getSupportedCipherSuites().length > 0); 124 } catch (Exception e) { 125 fail("Unexpected exception " + e.toString()); 126 } 127 } 128 129 } 130