Code example for SSLContext

Methods: createSSLEngine, getClientSessionContext, getServerSessionContext, getServerSocketFactory, getSocketFactory, init

0
     
    private void checkSSLContext(SSLContext sslC) 
            throws KeyManagementException { 
         
        try { 
            sslC.getSocketFactory();
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
        } 
        try { 
            sslC.getServerSocketFactory();
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
        } 
        try { 
            sslC.getServerSessionContext();
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
        } 
        try { 
            sslC.getClientSessionContext();
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
        }       
        try { 
            sslC.createSSLEngine();
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
        } 
        try { 
            sslC.createSSLEngine("host",1);
            fail("RuntimeException must be thrown");
        } catch (RuntimeException e) {
            assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
        } 
        TrustManager [] tm = new TManager[10];
        KeyManager [] km = new KManager[5];
        try { 
            sslC.init(km, tm, null);
            fail("KeyManagementException must be thrown");
        } catch (KeyManagementException e) {
        } 
        sslC.init(km, tm, new SecureRandom());
         
        SSLEngine sslE = sslC.createSSLEngine();
        assertTrue("Not null result",sslE instanceof SSLEngine);
        assertNull("Incorrect host", sslE.getPeerHost());
        assertEquals("Incorrect port", 0, sslE.getPeerPort());
        String host = "ZZZ";
        int port = 8080;
        sslE = sslC.createSSLEngine(host, port);
        assertTrue("Not null result",sslE instanceof SSLEngine);
        assertEquals("Incorrect host", sslE.getPeerHost(), host);
        assertEquals("Incorrect port", sslE.getPeerPort(), port);
        try { 
            assertNull("Not null result", sslC.getServerSessionContext());
        } catch (NullPointerException e) {
        } 
        try {             
            assertNull("Not null result", sslC.getClientSessionContext());
        } catch (NullPointerException e) {
        } 
    } 
     
    /**