FileDocCategorySizeDatePackage
SSLContext2Test.javaAPI DocAndroid 1.5 API12634Wed May 06 22:41:06 BST 2009tests.api.javax.net.ssl

SSLContext2Test

public class SSLContext2Test extends TestCase
Tests for SSLContext class constructors and methods

Fields Summary
private static String
srvSSLContext
private static final String
defaultProtocol
public static final String
SSLContextProviderClass
private static final String[]
invalidValues
private static final String[]
validValues
Provider
mProv
Constructors Summary
Methods Summary
private voidcheckSSLContext(javax.net.ssl.SSLContext sslC)

        
        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) {
        }
    
protected voidsetUp()

        super.setUp();
        mProv = (new SpiEngUtils()).new MyProvider("MySSLContextProvider", "Provider for testing",
                srvSSLContext.concat(".").concat(defaultProtocol),
                SSLContextProviderClass);
        Security.insertProviderAt(mProv, 1);
    
protected voidtearDown()

        super.tearDown();
        Security.removeProvider(mProv.getName());
    
public voidtest_getInstanceLjava_lang_String()
Test for getInstance(String protocol) method Assertions: throws NullPointerException when protocol is null; throws NoSuchAlgorithmException when protocol is not correct; returns SSLContext object

        try {
            SSLContext.getInstance(null);
            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                SSLContext.getInstance(invalidValues[i]);
                fail("NoSuchAlgorithmException must be thrown (protocol: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        SSLContext sslC;
        for (int i = 0; i < validValues.length; i++) {
            sslC = SSLContext.getInstance(validValues[i]);
            assertTrue("Not instanceof SSLContext object",
                    sslC instanceof SSLContext);
            assertEquals("Incorrect protocol", sslC.getProtocol(),
                    validValues[i]);
            assertEquals("Incorrect provider", sslC.getProvider(), mProv);
            checkSSLContext(sslC);
        }
    
public voidtest_getInstanceLjava_lang_StringLjava_lang_String()
Test for getInstance(String protocol, String provider) method Assertions: throws NullPointerException when protocol is null; throws NoSuchAlgorithmException when protocol is not correct; throws IllegalArgumentException when provider is null or empty; throws NoSuchProviderException when provider is available; returns SSLContext object

        try {
            SSLContext.getInstance(null, mProv.getName());
            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                SSLContext.getInstance(invalidValues[i], mProv.getName());
                fail("NoSuchAlgorithmException must be thrown (protocol: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        String prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                SSLContext.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (protocol: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
            try {
                SSLContext.getInstance(validValues[i], "");
                fail("IllegalArgumentException must be thrown when provider is empty (protocol: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        for (int i = 0; i < validValues.length; i++) {
            for (int j = 1; j < invalidValues.length; j++) {
                try {
                    SSLContext.getInstance(validValues[i], invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (protocol: "
                            .concat(invalidValues[i]).concat(" provider: ")
                            .concat(invalidValues[j]).concat(")"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
        SSLContext sslC;
        for (int i = 0; i < validValues.length; i++) {
            sslC = SSLContext.getInstance(validValues[i], mProv.getName());
            assertTrue("Not instanceof SSLContext object",
                    sslC instanceof SSLContext);
            assertEquals("Incorrect protocol", sslC.getProtocol(),
                    validValues[i]);
            assertEquals("Incorrect provider", sslC.getProvider().getName(),
                    mProv.getName());
            checkSSLContext(sslC);
        }
    
public voidtest_getInstanceLjava_lang_StringLjava_security_Provider()
Test for getInstance(String protocol, Provider provider) method Assertions: throws NullPointerException when protocol is null; throws NoSuchAlgorithmException when protocol is not correct; throws IllegalArgumentException when provider is null; returns SSLContext object

        try {
            SSLContext.getInstance(null, mProv);
            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
        } catch (NoSuchAlgorithmException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                SSLContext.getInstance(invalidValues[i], mProv);
                fail("NoSuchAlgorithmException must be thrown (protocol: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (NoSuchAlgorithmException e) {
            }
        }
        Provider prov = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                SSLContext.getInstance(validValues[i], prov);
                fail("IllegalArgumentException must be thrown when provider is null (protocol: "
                        .concat(invalidValues[i]).concat(")"));
            } catch (IllegalArgumentException e) {
            }
        }
        SSLContext sslC;
        for (int i = 0; i < validValues.length; i++) {
            sslC = SSLContext.getInstance(validValues[i], mProv);
            assertTrue("Not instanceof SSLContext object",
                    sslC instanceof SSLContext);
            assertEquals("Incorrect protocol", sslC.getProtocol(),
                    validValues[i]);
            assertEquals("Incorrect provider", sslC.getProvider(), mProv);
            checkSSLContext(sslC);
       }