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.net.Socket;
     22 import java.net.InetSocketAddress;
     23 import javax.net.ssl.SSLServerSocket;
     24 
     25 import junit.framework.Test;
     26 import junit.framework.TestCase;
     27 import junit.framework.TestSuite;
     28 
     29 /**
     30  * SSLServerSocketImplTest test
     31  */
     32 public class SSLServerSocketImplTest extends TestCase {
     33 
     34     private static boolean doLog = false;
     35 
     36     /**
     37      * Sets up the test case.
     38      */
     39     @Override
     40     public void setUp() {
     41         if (doLog) {
     42             System.out.println("");
     43             System.out.println("========================");
     44             System.out.println("====== Running the test: " + getName());
     45         }
     46     }
     47 
     48     private SSLServerSocket createSSLServerSocket() throws Exception {
     49         return new SSLServerSocketImpl(JSSETestData.getSSLParameters());
     50     }
     51 
     52     /**
     53      * SSLServerSocketImpl(SSLParameters sslParameters) method testing.
     54      */
     55     public void testSSLServerSocketImpl1() throws Exception {
     56         Client client = null;
     57         SSLServerSocket ssocket = null;
     58         try {
     59             ssocket = new SSLServerSocketImpl(JSSETestData.getSSLParameters());
     60             ssocket.bind(null);
     61             ssocket.setUseClientMode(true);
     62 
     63             final SSLServerSocket s = ssocket;
     64             Thread thread = new Thread() {
     65                 @Override
     66                 public void run() {
     67                     try {
     68                         s.accept().close();
     69                     } catch (Exception e) { }
     70                 }
     71             };
     72 
     73             thread.start();
     74 
     75             client = new Client(ssocket.getLocalPort());
     76             client.start();
     77 
     78             int timeout = 10; // wait no more than 5 seconds for handshake
     79             while (!client.handshakeStarted()) {
     80                 // wait for handshake start
     81                 try {
     82                     Thread.sleep(500);
     83                 } catch (Exception e) { }
     84                 timeout--;
     85                 if (timeout < 0) {
     86                     try {
     87                         client.close();
     88                     } catch (IOException ex) { }
     89                     try {
     90                         ssocket.close();
     91                     } catch (IOException ex) { }
     92                     fail("Handshake was not started");
     93                 }
     94             }
     95         } finally {
     96             if (client != null) {
     97                 try {
     98                     client.close();
     99                 } catch (IOException ex) { }
    100             }
    101             if (ssocket != null) {
    102                 try {
    103                     ssocket.close();
    104                 } catch (IOException ex) { }
    105             }
    106         }
    107     }
    108 
    109     /**
    110      * SSLServerSocketImpl(int port, SSLParameters sslParameters) method
    111      * testing.
    112      */
    113     public void testSSLServerSocketImpl2() throws Exception {
    114         Client client = null;
    115         SSLServerSocket ssocket = null;
    116         try {
    117             ssocket = new SSLServerSocketImpl(0,
    118                     JSSETestData.getSSLParameters());
    119             ssocket.setUseClientMode(true);
    120 
    121             final SSLServerSocket s = ssocket;
    122             Thread thread = new Thread() {
    123                 @Override
    124                 public void run() {
    125                     try {
    126                         s.accept().close();
    127                     } catch (Exception e) { }
    128                 }
    129             };
    130 
    131             thread.start();
    132 
    133             client = new Client(ssocket.getLocalPort());
    134             client.start();
    135 
    136             int timeout = 10; // wait no more than 5 seconds for handshake
    137             while (!client.handshakeStarted()) {
    138                 // wait for handshake start
    139                 try {
    140                     Thread.sleep(500);
    141                 } catch (Exception e) { }
    142                 timeout--;
    143                 if (timeout < 0) {
    144                     try {
    145                         client.close();
    146                     } catch (IOException ex) { }
    147                     try {
    148                         ssocket.close();
    149                     } catch (IOException ex) { }
    150                     fail("Handshake was not started");
    151                 }
    152             }
    153         } finally {
    154             if (client != null) {
    155                 try {
    156                     client.close();
    157                 } catch (IOException ex) { }
    158             }
    159             if (ssocket != null) {
    160                 try {
    161                     ssocket.close();
    162                 } catch (IOException ex) { }
    163             }
    164         }
    165     }
    166 
    167     /**
    168      * SSLServerSocketImpl(int port, int backlog,
    169      * SSLParameters sslParameters) method testing.
    170      */
    171     public void testSSLServerSocketImpl3() throws Exception {
    172         Client client = null;
    173         SSLServerSocket ssocket = null;
    174         try {
    175             ssocket = new SSLServerSocketImpl(0, 1,
    176                     JSSETestData.getSSLParameters());
    177             ssocket.setUseClientMode(true);
    178 
    179             final SSLServerSocket s = ssocket;
    180             Thread thread = new Thread() {
    181                 @Override
    182                 public void run() {
    183                     try {
    184                         s.accept().close();
    185                     } catch (Exception e) { }
    186                 }
    187             };
    188 
    189             thread.start();
    190 
    191             client = new Client(ssocket.getLocalPort());
    192             client.start();
    193 
    194             int timeout = 10; // wait no more than 5 seconds for handshake
    195             while (!client.handshakeStarted()) {
    196                 // wait for handshake start
    197                 try {
    198                     Thread.sleep(500);
    199                 } catch (Exception e) { }
    200                 timeout--;
    201                 if (timeout < 0) {
    202                     try {
    203                         client.close();
    204                     } catch (IOException ex) { }
    205                     try {
    206                         ssocket.close();
    207                     } catch (IOException ex) { }
    208                     fail("Handshake was not started");
    209                 }
    210             }
    211         } finally {
    212             if (client != null) {
    213                 try {
    214                     client.close();
    215                 } catch (IOException ex) { }
    216             }
    217             if (ssocket != null) {
    218                 try {
    219                     ssocket.close();
    220                 } catch (IOException ex) { }
    221             }
    222         }
    223     }
    224 
    225     /**
    226      * SSLServerSocketImpl(int port, int backlog, InetAddress iAddress,
    227      * SSLParameters sslParameters) method testing.
    228      */
    229     public void testSSLServerSocketImpl4() throws Exception {
    230         Client client = null;
    231         SSLServerSocket ssocket = null;
    232         try {
    233             ssocket = new SSLServerSocketImpl(0, 1, null,
    234                     JSSETestData.getSSLParameters());
    235             ssocket.setUseClientMode(true);
    236 
    237             final SSLServerSocket s = ssocket;
    238             Thread thread = new Thread() {
    239                 @Override
    240                 public void run() {
    241                     try {
    242                         s.accept().close();
    243                     } catch (Exception e) { }
    244                 }
    245             };
    246 
    247             thread.start();
    248 
    249             client = new Client(ssocket.getLocalPort());
    250             client.start();
    251 
    252             int timeout = 10; // wait no more than 5 seconds for handshake
    253             while (!client.handshakeStarted()) {
    254                 // wait for handshake start
    255                 try {
    256                     Thread.sleep(500);
    257                 } catch (Exception e) { }
    258                 timeout--;
    259                 if (timeout < 0) {
    260                     try {
    261                         client.close();
    262                     } catch (IOException ex) { }
    263                     try {
    264                         ssocket.close();
    265                     } catch (IOException ex) { }
    266                     fail("Handshake was not started");
    267                 }
    268             }
    269         } finally {
    270             if (client != null) {
    271                 try {
    272                     client.close();
    273                 } catch (IOException ex) { }
    274             }
    275             if (ssocket != null) {
    276                 try {
    277                     ssocket.close();
    278                 } catch (IOException ex) { }
    279             }
    280         }
    281     }
    282 
    283     /**
    284      * getSupportedCipherSuites() method testing.
    285      */
    286     public void testGetSupportedCipherSuites() throws Exception {
    287         SSLServerSocket ssocket = createSSLServerSocket();
    288         String[] supported = ssocket.getSupportedCipherSuites();
    289         assertNotNull(supported);
    290         supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
    291         supported = ssocket.getEnabledCipherSuites();
    292         for (int i=0; i<supported.length; i++) {
    293             if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
    294                 fail("Modification of the returned result "
    295                         + "causes the modification of the internal state");
    296             }
    297         }
    298     }
    299 
    300     /**
    301      * getEnabledCipherSuites() method testing.
    302      */
    303     public void testGetEnabledCipherSuites() throws Exception {
    304         SSLServerSocket ssocket = createSSLServerSocket();
    305         String[] enabled = ssocket.getEnabledCipherSuites();
    306         assertNotNull(enabled);
    307         String[] supported = ssocket.getSupportedCipherSuites();
    308         for (int i=0; i<enabled.length; i++) {
    309             //System.out.println("Checking of "+enabled[i]);
    310             found: {
    311                 for (int j=0; j<supported.length; j++) {
    312                     if (enabled[i].equals(supported[j])) {
    313                         break found;
    314                     }
    315                 }
    316                 fail("Enabled suite does not belong to the set "
    317                         + "of supported cipher suites: " + enabled[i]);
    318             }
    319         }
    320         ssocket.setEnabledCipherSuites(supported);
    321         for (int i=0; i<supported.length; i++) {
    322             enabled = new String[supported.length - i];
    323             System.arraycopy(supported, 0,
    324                     enabled, 0, supported.length-i);
    325             ssocket.setEnabledCipherSuites(enabled);
    326             String[] result = ssocket.getEnabledCipherSuites();
    327             if (result.length != enabled.length) {
    328                 fail("Returned result does not correspond to expected.");
    329             }
    330             for (int k=0; k<result.length; k++) {
    331                 found: {
    332                     for (int n=0; n<enabled.length; n++) {
    333                         if (result[k].equals(enabled[n])) {
    334                             break found;
    335                         }
    336                     }
    337                     if (result.length != enabled.length) {
    338                         fail("Returned result does not correspond "
    339                                 + "to expected.");
    340                     }
    341                 }
    342             }
    343         }
    344     }
    345 
    346     /**
    347      * setEnabledCipherSuites(String[] suites) method testing.
    348      */
    349     public void testSetEnabledCipherSuites() throws Exception {
    350         SSLServerSocket ssocket = createSSLServerSocket();
    351         String[] enabled = ssocket.getEnabledCipherSuites();
    352         assertNotNull(enabled);
    353         String[] supported = ssocket.getSupportedCipherSuites();
    354         for (int i=0; i<enabled.length; i++) {
    355             //System.out.println("Checking of "+enabled[i]);
    356             found: {
    357                 for (int j=0; j<supported.length; j++) {
    358                     if (enabled[i].equals(supported[j])) {
    359                         break found;
    360                     }
    361                 }
    362                 fail("Enabled suite does not belong to the set "
    363                         + "of supported cipher suites: " + enabled[i]);
    364             }
    365         }
    366         ssocket.setEnabledCipherSuites(supported);
    367         ssocket.setEnabledCipherSuites(enabled);
    368         ssocket.setEnabledCipherSuites(supported);
    369         String[] more_than_supported = new String[supported.length+1];
    370         for (int i=0; i<supported.length+1; i++) {
    371             more_than_supported[i]
    372                 = "NOT_SUPPORTED_CIPHER_SUITE";
    373             System.arraycopy(supported, 0,
    374                     more_than_supported, 0, i);
    375             System.arraycopy(supported, i,
    376                     more_than_supported, i+1, supported.length-i);
    377             try {
    378                 ssocket.setEnabledCipherSuites(more_than_supported);
    379                 fail("Expected IllegalArgumentException was not thrown");
    380             } catch (IllegalArgumentException e) { }
    381         }
    382         enabled = ssocket.getEnabledCipherSuites();
    383         enabled[0] = "NOT_SUPPORTED_CIPHER_SUITE";
    384         enabled = ssocket.getEnabledCipherSuites();
    385         for (int i=0; i<enabled.length; i++) {
    386             if ("NOT_SUPPORTED_CIPHER_SUITE".equals(enabled[i])) {
    387                 fail("Modification of the returned result "
    388                         + "causes the modification of the internal state");
    389             }
    390         }
    391     }
    392 
    393     /**
    394      * getSupportedProtocols() method testing.
    395      */
    396     public void testGetSupportedProtocols() throws Exception {
    397         SSLServerSocket ssocket = createSSLServerSocket();
    398         String[] supported = ssocket.getSupportedProtocols();
    399         assertNotNull(supported);
    400         assertFalse(supported.length == 0);
    401         supported[0] = "NOT_SUPPORTED_PROTOCOL";
    402         supported = ssocket.getSupportedProtocols();
    403         for (int i=0; i<supported.length; i++) {
    404             if ("NOT_SUPPORTED_PROTOCOL".equals(supported[i])) {
    405                 fail("Modification of the returned result "
    406                         + "causes the modification of the internal state");
    407             }
    408         }
    409     }
    410 
    411     /**
    412      * getEnabledProtocols() method testing.
    413      */
    414     public void testGetEnabledProtocols() throws Exception {
    415         SSLServerSocket ssocket = createSSLServerSocket();
    416         String[] enabled = ssocket.getEnabledProtocols();
    417         assertNotNull(enabled);
    418         String[] supported = ssocket.getSupportedProtocols();
    419         for (int i=0; i<enabled.length; i++) {
    420             //System.out.println("Checking of "+enabled[i]);
    421             found: {
    422                 for (int j=0; j<supported.length; j++) {
    423                     if (enabled[i].equals(supported[j])) {
    424                         break found;
    425                     }
    426                 }
    427                 fail("Enabled protocol does not belong to the set "
    428                         + "of supported protocols: " + enabled[i]);
    429             }
    430         }
    431         ssocket.setEnabledProtocols(supported);
    432         for (int i=0; i<supported.length; i++) {
    433             enabled = new String[supported.length - i];
    434             System.arraycopy(supported, i,
    435                     enabled, 0, supported.length-i);
    436             //System.out.println("");
    437             //for (int k=0; k<supported.length - i; k++) {
    438             //    System.out.println("---- "+enabled[k]);
    439             //}
    440             ssocket.setEnabledProtocols(enabled);
    441             String[] result = ssocket.getEnabledProtocols();
    442             if (result.length != enabled.length) {
    443                 fail("Returned result does not correspond to expected.");
    444             }
    445             for (int k=0; k<result.length; k++) {
    446                 found: {
    447                     for (int n=0; n<enabled.length; n++) {
    448                         if (result[k].equals(enabled[n])) {
    449                             break found;
    450                         }
    451                     }
    452                     if (result.length != enabled.length) {
    453                         fail("Returned result does not correspond "
    454                                 + "to expected.");
    455                     }
    456                 }
    457             }
    458         }
    459     }
    460 
    461     /**
    462      * setEnabledProtocols(String[] protocols) method testing.
    463      */
    464     public void testSetEnabledProtocols() throws Exception {
    465         SSLServerSocket ssocket = createSSLServerSocket();
    466         String[] enabled = ssocket.getEnabledProtocols();
    467         assertNotNull(enabled);
    468         String[] supported = ssocket.getSupportedProtocols();
    469         for (int i=0; i<enabled.length; i++) {
    470             //System.out.println("Checking of "+enabled[i]);
    471             found: {
    472                 for (int j=0; j<supported.length; j++) {
    473                     if (enabled[i].equals(supported[j])) {
    474                         break found;
    475                     }
    476                 }
    477                 fail("Enabled suite does not belong to the set "
    478                         + "of supported cipher suites: " + enabled[i]);
    479             }
    480         }
    481         ssocket.setEnabledProtocols(supported);
    482         ssocket.setEnabledProtocols(enabled);
    483         ssocket.setEnabledProtocols(supported);
    484         String[] more_than_supported = new String[supported.length+1];
    485         for (int i=0; i<supported.length+1; i++) {
    486             more_than_supported[i]
    487                 = "NOT_SUPPORTED_PROTOCOL";
    488             System.arraycopy(supported, 0,
    489                     more_than_supported, 0, i);
    490             System.arraycopy(supported, i,
    491                     more_than_supported, i+1, supported.length-i);
    492             try {
    493                 ssocket.setEnabledProtocols(more_than_supported);
    494                 fail("Expected IllegalArgumentException was not thrown");
    495             } catch (IllegalArgumentException e) { }
    496         }
    497         enabled = ssocket.getEnabledProtocols();
    498         enabled[0] = "NOT_SUPPORTED_PROTOCOL";
    499         enabled = ssocket.getEnabledProtocols();
    500         for (int i=0; i<enabled.length; i++) {
    501             if ("NOT_SUPPORTED_PROTOCOL".equals(enabled[i])) {
    502                 fail("Modification of the returned result "
    503                         + "causes the modification of the internal state");
    504             }
    505         }
    506     }
    507 
    508     /**
    509      * setUseClientMode(boolean mode) method testing.
    510      * getUseClientMode() method testing.
    511      */
    512     public void testSetGetUseClientMode() throws Exception {
    513         SSLServerSocket ssocket = createSSLServerSocket();
    514 
    515         ssocket.setUseClientMode(false);
    516         assertFalse("Result does not correspond to expected",
    517                 ssocket.getUseClientMode());
    518         ssocket.setUseClientMode(true);
    519         assertTrue("Result does not correspond to expected",
    520                 ssocket.getUseClientMode());
    521     }
    522 
    523     /**
    524      * setNeedClientAuth(boolean need) method testing.
    525      * getNeedClientAuth() method testing.
    526      */
    527     public void testSetGetNeedClientAuth() throws Exception {
    528         SSLServerSocket ssocket = createSSLServerSocket();
    529 
    530         ssocket.setWantClientAuth(true);
    531         ssocket.setNeedClientAuth(false);
    532         assertFalse("Result does not correspond to expected",
    533                 ssocket.getNeedClientAuth());
    534         assertFalse("Socket did not reset its want client auth state",
    535                 ssocket.getWantClientAuth());
    536         ssocket.setWantClientAuth(true);
    537         ssocket.setNeedClientAuth(true);
    538         assertTrue("Result does not correspond to expected",
    539                 ssocket.getNeedClientAuth());
    540         assertFalse("Socket did not reset its want client auth state",
    541                 ssocket.getWantClientAuth());
    542     }
    543 
    544     /**
    545      * setWantClientAuth(boolean want) method testing.
    546      * getWantClientAuth() method testing.
    547      */
    548     public void testSetGetWantClientAuth() throws Exception {
    549         SSLServerSocket ssocket = createSSLServerSocket();
    550 
    551         ssocket.setNeedClientAuth(true);
    552         ssocket.setWantClientAuth(false);
    553         assertFalse("Result does not correspond to expected",
    554                 ssocket.getWantClientAuth());
    555         assertFalse("Socket did not reset its want client auth state",
    556                 ssocket.getNeedClientAuth());
    557         ssocket.setNeedClientAuth(true);
    558         ssocket.setWantClientAuth(true);
    559         assertTrue("Result does not correspond to expected",
    560                 ssocket.getWantClientAuth());
    561         assertFalse("Socket did not reset its want client auth state",
    562                 ssocket.getNeedClientAuth());
    563     }
    564 
    565     /**
    566      * setEnableSessionCreation(boolean flag) method testing.
    567      * getEnableSessionCreation() method testing.
    568      */
    569     public void testSetGetEnableSessionCreation() throws Exception {
    570         SSLServerSocket ssocket = createSSLServerSocket();
    571 
    572         ssocket.setEnableSessionCreation(false);
    573         assertFalse("Result does not correspond to expected",
    574                 ssocket.getEnableSessionCreation());
    575         ssocket.setEnableSessionCreation(true);
    576         assertTrue("Result does not correspond to expected",
    577                 ssocket.getEnableSessionCreation());
    578     }
    579 
    580     /**
    581      * toString() method testing.
    582      */
    583     public void testToString() throws Exception {
    584         SSLServerSocket ssocket = createSSLServerSocket();
    585         assertNotNull("String representation is null", ssocket.toString());
    586     }
    587 
    588     private static class Client extends Thread {
    589 
    590         private boolean closed;
    591         private boolean handshake_started = false;
    592         private Socket client = null;
    593         private int port;
    594 
    595         public Client(int port) throws IOException {
    596             super();
    597             this.port = port;
    598             client = new Socket();
    599             client.setSoTimeout(10000);
    600         }
    601 
    602         public int getPort() {
    603             return client.getLocalPort();
    604         }
    605 
    606         @Override
    607         public void run() {
    608             while (!closed) {
    609                 try {
    610                     if (doLog) {
    611                         System.out.print(".");
    612                     }
    613                     if (!handshake_started) {
    614                         client.connect(
    615                                 new InetSocketAddress("localhost", port));
    616                         client.getInputStream().read();
    617                         handshake_started = true;
    618                     }
    619                     Thread.sleep(1000);
    620                 } catch (Exception e) {
    621                     e.printStackTrace();
    622                 }
    623             }
    624             if (client != null) {
    625                 try {
    626                     client.close();
    627                 } catch (IOException e) { }
    628             }
    629             //System.out.println("===== client has been stopped");
    630         }
    631 
    632         public boolean handshakeStarted() {
    633             return handshake_started;
    634         }
    635 
    636         public void close() throws IOException {
    637             closed = true;
    638             client.close();
    639         }
    640 
    641     };
    642 
    643     public static Test suite() {
    644         return new TestSuite(SSLServerSocketImplTest.class);
    645     }
    646 
    647 }
    648