Home | History | Annotate | Download | only in jsse
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     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 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 import java.io.OutputStream;
     23 import java.net.InetAddress;
     24 import java.net.InetSocketAddress;
     25 import java.net.Socket;
     26 import java.util.Arrays;
     27 import javax.net.ssl.SSLServerSocket;
     28 import javax.net.ssl.SSLServerSocketFactory;
     29 import javax.net.ssl.SSLSocket;
     30 import javax.net.ssl.SSLSocketFactory;
     31 
     32 import junit.framework.Test;
     33 import junit.framework.TestCase;
     34 import junit.framework.TestSuite;
     35 
     36 /**
     37  * SSLSocketImplTest
     38  */
     39 public class SSLSocketFactoriesTest extends TestCase {
     40 
     41     // turn on/off the debug logging
     42     private boolean doLog = false;
     43 
     44     /**
     45      * Sets up the test case.
     46      */
     47     @Override
     48     public void setUp() throws Exception {
     49         super.setUp();
     50         if (doLog) {
     51             System.out.println("========================");
     52             System.out.println("====== Running the test: " + getName());
     53             System.out.println("========================");
     54         }
     55     }
     56 
     57     @Override
     58     public void tearDown() throws Exception {
     59         super.tearDown();
     60     }
     61 
     62     /**
     63      * Tests default initialized factories.
     64      */
     65     public void testDefaultInitialized() throws Exception {
     66 
     67         SSLServerSocketFactory ssfactory =
     68                 (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
     69         SSLSocketFactory sfactory =
     70                 (SSLSocketFactory) SSLSocketFactory.getDefault();
     71 
     72         assertNotNull(ssfactory.getDefaultCipherSuites());
     73         assertNotNull(ssfactory.getSupportedCipherSuites());
     74         assertNotNull(ssfactory.createServerSocket());
     75 
     76         assertNotNull(sfactory.getDefaultCipherSuites());
     77         assertNotNull(sfactory.getSupportedCipherSuites());
     78         assertNotNull(sfactory.createSocket());
     79     }
     80 
     81     public void testSocketCreation() throws Throwable {
     82         SSLSocketFactory socketFactory
     83                 = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
     84         SSLServerSocketFactory serverSocketFactory
     85                 = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
     86 
     87         String[] enabled = { "TLS_RSA_WITH_RC4_128_MD5" };
     88         for (int i = 0; i < 4; i++) {
     89             SSLServerSocket ssocket;
     90             switch (i) {
     91                 case 0:
     92                     if (doLog) {
     93                         System.out.println(
     94                                 "*** ServerSocketFactory.createServerSocket()");
     95                     }
     96                     ssocket = (SSLServerSocket)
     97                             serverSocketFactory.createServerSocket();
     98                     ssocket.bind(null);
     99                     break;
    100                 case 1:
    101                     if (doLog) {
    102                         System.out.println(
    103                                 "*** ServerSocketFactory.createServerSocket(int)");
    104                     }
    105                     ssocket = (SSLServerSocket)
    106                             serverSocketFactory.createServerSocket(0);
    107                     break;
    108                 case 2:
    109                     if (doLog) {
    110                         System.out.println(
    111                                 "*** ServerSocketFactory.createServerSocket(int,int)");
    112                     }
    113                     ssocket = (SSLServerSocket)
    114                             serverSocketFactory.createServerSocket(0, 6);
    115                     break;
    116                 default:
    117                     if (doLog) {
    118                         System.out.println("*** ServerSocketFactory."
    119                                 + "createServerSocket(int,int,InetAddress)");
    120                     }
    121                     ssocket = (SSLServerSocket)
    122                             serverSocketFactory.createServerSocket(0, 6, null);
    123                     break;
    124             }
    125             ssocket.setUseClientMode(false);
    126             ssocket.setEnabledCipherSuites(enabled);
    127             for (int j = 0; j < 6; j++) {
    128                 SSLSocket csocket;
    129                 switch (j) {
    130                     case 0:
    131                         if (doLog) {
    132                             System.out.println(
    133                                     "=== SocketFactory.createSocket()");
    134                         }
    135                         csocket = (SSLSocket) socketFactory.createSocket();
    136                         csocket.connect(
    137                                 new InetSocketAddress("localhost",
    138                                         ssocket.getLocalPort()));
    139                         break;
    140                     case 1:
    141                         if (doLog) {
    142                             System.out.println(
    143                                     "=== SocketFactory.createSocket(String,int)");
    144                         }
    145                         csocket = (SSLSocket)
    146                                 socketFactory.createSocket("localhost",
    147                                         ssocket.getLocalPort());
    148                         break;
    149                     case 2:
    150                         if (doLog) {
    151                             System.out.println("=== SocketFactory.createSocket("
    152                                     + "String,int,InetAddress,int)");
    153                         }
    154                         csocket = (SSLSocket)
    155                                 socketFactory.createSocket("localhost",
    156                                         ssocket.getLocalPort(),
    157                                         InetAddress.getByName("localhost"), 0);
    158                         break;
    159                     case 3:
    160                         if (doLog) {
    161                             System.out.println("=== SocketFactory.createSocket("
    162                                     + "InetAddress,int)");
    163                         }
    164                         csocket = (SSLSocket) socketFactory.createSocket(
    165                                 InetAddress.getByName("localhost"),
    166                                 ssocket.getLocalPort());
    167                         break;
    168                     case 4:
    169                         if (doLog) {
    170                             System.out.println("=== SocketFactory.createSocket("
    171                                     + "InetAddress,int,InetAddress,int)");
    172                         }
    173                         csocket = (SSLSocket) socketFactory.createSocket(
    174                                 InetAddress.getByName("localhost"),
    175                                 ssocket.getLocalPort(),
    176                                 InetAddress.getByName("localhost"),
    177                                 0);
    178                         break;
    179                     default:
    180                         if (doLog) {
    181                             System.out.println(
    182                                     "=== SSLSocketFactory.createSocket("
    183                                             + "socket,String,int,boolean)");
    184                         }
    185                         Socket socket = new Socket(
    186                                 InetAddress.getByName("localhost"),
    187                                 ssocket.getLocalPort());
    188                         csocket = (SSLSocket) socketFactory.createSocket(
    189                                 socket, "localhost", ssocket.getLocalPort(),
    190                                 true);
    191                         break;
    192 
    193                 }
    194                 csocket.setUseClientMode(true);
    195                 csocket.setEnabledCipherSuites(enabled);
    196                 doTest(ssocket, csocket);
    197             }
    198         }
    199     }
    200 
    201     /**
    202      * SSLSocketFactory.getSupportedCipherSuites() method testing.
    203      */
    204     public void testGetSupportedCipherSuites1() throws Exception {
    205         SSLSocketFactory socketFactory
    206                 = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
    207         String[] supported = socketFactory.getSupportedCipherSuites();
    208         assertNotNull(supported);
    209         supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
    210         supported = socketFactory.getSupportedCipherSuites();
    211         for (int i = 0; i < supported.length; i++) {
    212             if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
    213                 fail("Modification of the returned result "
    214                         + "causes the modification of the internal state");
    215             }
    216         }
    217     }
    218 
    219     /**
    220      * SSLServerSocketFactory.getSupportedCipherSuites() method testing.
    221      */
    222     public void testGetSupportedCipherSuites2() throws Exception {
    223         SSLServerSocketFactory serverSocketFactory
    224                 = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
    225         String[] supported = serverSocketFactory.getSupportedCipherSuites();
    226         assertNotNull(supported);
    227         supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
    228         supported = serverSocketFactory.getSupportedCipherSuites();
    229         for (int i = 0; i < supported.length; i++) {
    230             if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
    231                 fail("Modification of the returned result "
    232                         + "causes the modification of the internal state");
    233             }
    234         }
    235     }
    236 
    237     /**
    238      * SSLSocketFactory.getDefaultCipherSuites() method testing.
    239      */
    240     public void testGetDefaultCipherSuites1() throws Exception {
    241         SSLSocketFactory socketFactory
    242                 = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
    243         String[] supported = socketFactory.getSupportedCipherSuites();
    244         String[] defaultcs = socketFactory.getDefaultCipherSuites();
    245         assertNotNull(supported);
    246         assertNotNull(defaultcs);
    247         for (int i = 0; i < defaultcs.length; i++) {
    248             found:
    249             {
    250                 for (int j = 0; j < supported.length; j++) {
    251                     if (defaultcs[i].equals(supported[j])) {
    252                         break found;
    253                     }
    254                 }
    255                 fail("Default suite does not belong to the set "
    256                         + "of supported cipher suites: " + defaultcs[i]);
    257             }
    258         }
    259     }
    260 
    261     /**
    262      * SSLServerSocketFactory.getDefaultCipherSuites() method testing.
    263      */
    264     public void testGetDefaultCipherSuites2() throws Exception {
    265         SSLServerSocketFactory serverSocketFactory
    266                 = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
    267         String[] supported = serverSocketFactory.getSupportedCipherSuites();
    268         String[] defaultcs = serverSocketFactory.getDefaultCipherSuites();
    269         assertNotNull(supported);
    270         assertNotNull(defaultcs);
    271         for (int i = 0; i < defaultcs.length; i++) {
    272             found:
    273             {
    274                 for (int j = 0; j < supported.length; j++) {
    275                     if (defaultcs[i].equals(supported[j])) {
    276                         break found;
    277                     }
    278                 }
    279                 fail("Default suite does not belong to the set "
    280                         + "of supported cipher suites: " + defaultcs[i]);
    281             }
    282         }
    283     }
    284 
    285     /**
    286      * Performs SSL connection between the sockets
    287      *
    288      * @return
    289      */
    290     public void doTest(SSLServerSocket ssocket, SSLSocket csocket)
    291             throws Throwable {
    292         final String server_message = "Hello from SSL Server Socket!";
    293         final String client_message = "Hello from SSL Socket!";
    294         Thread server = null;
    295         Thread client = null;
    296         final Throwable[] throwed = new Throwable[1];
    297         try {
    298             final SSLServerSocket ss = ssocket;
    299             final SSLSocket s = csocket;
    300             server = new Thread() {
    301                 @Override
    302                 public void run() {
    303                     InputStream is = null;
    304                     OutputStream os = null;
    305                     SSLSocket s = null;
    306                     try {
    307                         s = (SSLSocket) ss.accept();
    308                         if (doLog) {
    309                             System.out.println("Socket accepted: " + s);
    310                         }
    311                         is = s.getInputStream();
    312                         os = s.getOutputStream();
    313                         // send the message to the client
    314                         os.write(server_message.getBytes());
    315                         // read the response
    316                         byte[] buff = new byte[client_message.length()];
    317                         int len = is.read(buff);
    318                         if (doLog) {
    319                             System.out.println("Received message of length "
    320                                     + len + ": '" + new String(buff, 0, len) + "'");
    321                         }
    322                         assertTrue("Read message does not equal to expected",
    323                                 Arrays.equals(client_message.getBytes(), buff));
    324                         os.write(-1);
    325                         assertEquals("Read data differs from expected",
    326                                 255, is.read());
    327                         if (doLog) {
    328                             System.out.println("Server is closed: "
    329                                     + s.isClosed());
    330                         }
    331                         assertEquals("Returned value should be -1",
    332                                 // initiate an exchange of closure alerts
    333                                 -1, is.read());
    334                         if (doLog) {
    335                             System.out.println("Server is closed: "
    336                                     + s.isClosed());
    337                         }
    338                         assertEquals("Returned value should be -1",
    339                                 // initiate an exchange of closure alerts
    340                                 -1, is.read());
    341                     } catch (Throwable e) {
    342                         synchronized (throwed) {
    343                             if (doLog) {
    344                                 e.printStackTrace();
    345                             }
    346                             if (throwed[0] == null) {
    347                                 throwed[0] = e;
    348                             }
    349                         }
    350                     } finally {
    351                         try {
    352                             if (is != null) {
    353                                 is.close();
    354                             }
    355                         } catch (IOException ex) {
    356                         }
    357                         try {
    358                             if (os != null) {
    359                                 os.close();
    360                             }
    361                         } catch (IOException ex) {
    362                         }
    363                         try {
    364                             if (s != null) {
    365                                 s.close();
    366                             }
    367                         } catch (IOException ex) {
    368                         }
    369                     }
    370                 }
    371             };
    372 
    373             client = new Thread() {
    374                 @Override
    375                 public void run() {
    376                     InputStream is = null;
    377                     OutputStream os = null;
    378                     try {
    379                         assertTrue("Client was not connected", s.isConnected());
    380                         if (doLog) {
    381                             System.out.println("Client connected");
    382                         }
    383                         is = s.getInputStream();
    384                         os = s.getOutputStream();
    385                         s.startHandshake();
    386                         if (doLog) {
    387                             System.out.println("Client: HS was done");
    388                         }
    389                         // read the message from the server
    390                         byte[] buff = new byte[server_message.length()];
    391                         int len = is.read(buff);
    392                         if (doLog) {
    393                             System.out.println("Received message of length "
    394                                     + len + ": '" + new String(buff, 0, len) + "'");
    395                         }
    396                         assertTrue("Read message does not equal to expected",
    397                                 Arrays.equals(server_message.getBytes(), buff));
    398                         // send the response
    399                         buff = (" " + client_message + " ").getBytes();
    400                         os.write(buff, 1, buff.length - 2);
    401                         assertEquals("Read data differs from expected",
    402                                 255, is.read());
    403                         os.write(-1);
    404                         if (doLog) {
    405                             System.out.println("Client is closed: "
    406                                     + s.isClosed());
    407                         }
    408                         s.close();
    409                         if (doLog) {
    410                             System.out.println("Client is closed: "
    411                                     + s.isClosed());
    412                         }
    413                     } catch (Throwable e) {
    414                         synchronized (throwed) {
    415                             if (doLog) {
    416                                 e.printStackTrace();
    417                             }
    418                             if (throwed[0] == null) {
    419                                 throwed[0] = e;
    420                             }
    421                         }
    422                     } finally {
    423                         try {
    424                             if (is != null) {
    425                                 is.close();
    426                             }
    427                         } catch (IOException ex) {
    428                         }
    429                         try {
    430                             if (os != null) {
    431                                 os.close();
    432                             }
    433                         } catch (IOException ex) {
    434                         }
    435                         try {
    436                             if (s != null) {
    437                                 s.close();
    438                             }
    439                         } catch (IOException ex) {
    440                         }
    441                     }
    442                 }
    443             };
    444 
    445             server.start();
    446             client.start();
    447 
    448             while (server.isAlive() || client.isAlive()) {
    449                 if (throwed[0] != null) {
    450                     throw throwed[0];
    451                 }
    452                 try {
    453                     Thread.sleep(500);
    454                 } catch (Exception e) {
    455                 }
    456             }
    457         } finally {
    458             if (server != null) {
    459                 server.stop();
    460             }
    461             if (client != null) {
    462                 client.stop();
    463             }
    464         }
    465         if (throwed[0] != null) {
    466             throw throwed[0];
    467         }
    468     }
    469 
    470     public static Test suite() {
    471         return new TestSuite(SSLSocketFactoriesTest.class);
    472     }
    473 
    474 }
    475