Home | History | Annotate | Download | only in ssl
      1 package org.apache.harmony.tests.javax.net.ssl;
      2 
      3 import junit.framework.TestCase;
      4 
      5 import javax.net.ssl.SSLContext;
      6 import javax.net.ssl.SSLSessionContext;
      7 
      8 import java.security.KeyManagementException;
      9 import java.security.NoSuchAlgorithmException;
     10 
     11 /**
     12  * Tests for <code>SSLSessionContext</code> class constructors and methods.
     13  */
     14 public class SSLSessionContextTest extends TestCase {
     15 
     16     /**
     17      * @throws NoSuchAlgorithmException
     18      * @throws KeyManagementException
     19      * javax.net.ssl.SSLSessionContex#getSessionCacheSize()
     20      * javax.net.ssl.SSLSessionContex#setSessionCacheSize(int size)
     21      */
     22     public final void test_sessionCacheSize() throws NoSuchAlgorithmException, KeyManagementException {
     23         SSLContext context = SSLContext.getInstance("TLS");
     24         context.init(null, null, null);
     25         SSLSessionContext sc = context
     26                 .getClientSessionContext();
     27         sc.setSessionCacheSize(10);
     28         assertEquals("10 wasn't returned", 10, sc.getSessionCacheSize());
     29         sc.setSessionCacheSize(5);
     30         assertEquals("5 wasn't returned", 5, sc.getSessionCacheSize());
     31 
     32         try {
     33             sc.setSessionCacheSize(-1);
     34             fail("IllegalArgumentException wasn't thrown");
     35         } catch (IllegalArgumentException iae) {
     36             //expected
     37         }
     38     }
     39 
     40     /**
     41      * @throws NoSuchAlgorithmException
     42      * @throws KeyManagementException
     43      * javax.net.ssl.SSLSessionContex#getSessionTimeout()
     44      * javax.net.ssl.SSLSessionContex#setSessionTimeout(int seconds)
     45      */
     46     public final void test_sessionTimeout() throws NoSuchAlgorithmException, KeyManagementException {
     47         SSLContext context = SSLContext.getInstance("TLS");
     48         context.init(null, null, null);
     49         SSLSessionContext sc = context
     50                 .getClientSessionContext();
     51         sc.setSessionTimeout(100);
     52         assertEquals("100 wasn't returned", 100, sc.getSessionTimeout());
     53         sc.setSessionTimeout(5000);
     54         assertEquals("5000 wasn't returned", 5000, sc.getSessionTimeout());
     55 
     56         try {
     57             sc.setSessionTimeout(-1);
     58             fail("IllegalArgumentException wasn't thrown");
     59         } catch (IllegalArgumentException iae) {
     60             //expected
     61         }
     62     }
     63 
     64     /**
     65      * @throws NoSuchAlgorithmException
     66      * @throws KeyManagementException
     67      * javax.net.ssl.SSLSessionContex#getSession(byte[] sessionId)
     68      */
     69     public final void test_getSession() throws NoSuchAlgorithmException, KeyManagementException {
     70         SSLContext context = SSLContext.getInstance("TLS");
     71         context.init(null, null, null);
     72         SSLSessionContext sc = context
     73                 .getClientSessionContext();
     74         try {
     75             sc.getSession(null);
     76         } catch (NullPointerException e) {
     77             // expected
     78         }
     79         assertNull(sc.getSession(new byte[5]));
     80     }
     81 
     82     /**
     83      * @throws NoSuchAlgorithmException
     84      * @throws KeyManagementException
     85      * javax.net.ssl.SSLSessionContex#getIds()
     86      */
     87     public final void test_getIds() throws NoSuchAlgorithmException, KeyManagementException {
     88         SSLContext context = SSLContext.getInstance("TLS");
     89         context.init(null, null, null);
     90         SSLSessionContext sc = context
     91                 .getClientSessionContext();
     92         assertFalse(sc.getIds().hasMoreElements());
     93     }
     94 
     95 }
     96