Package us.monoid.web.ssl

Source Code of us.monoid.web.ssl.IgnoreAllCertsTest

package us.monoid.web.ssl;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.net.www.protocol.https.DefaultHostnameVerifier;
import sun.security.provider.certpath.SunCertPathBuilderException;
import sun.security.ssl.SSLSocketFactoryImpl;
import sun.security.validator.ValidatorException;
import us.monoid.web.Resty;
import us.monoid.web.TextResource;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class IgnoreAllCertsTest extends AbstractSSLServerTest {

    @After
    public void tearDown() {
        super.stopServer();
    }

    @Test
    public void shouldAllowConnectionToResourceWhereCertAndHostnameAreInvalidButWhenIgnoreAllCertsIsSet() throws Exception {
        startServer("us/monoid/web/ssl/test-ssl-hostname-eq-site.com.keystore");

        Resty.ignoreAllCerts();

        Resty resty = new Resty();

        TextResource response = resty.text(String.format("https://localhost:%d%s", getPort(), HelloWorldTestResource.HELLO_WORLD_PATH));

        assertThat(response.toString(), equalTo(HelloWorldTestResource.HELLO_WORLD));
    }

    @Test(expected = SSLHandshakeException.class)
    public void shouldFailSSLHandshakeWhenHostnameMatchesButCertificateIsInvalid() throws Exception {
        HttpsURLConnection.setDefaultSSLSocketFactory(new SSLSocketFactoryImpl());
        HttpsURLConnection.setDefaultHostnameVerifier(new DefaultHostnameVerifier());

        startServer("us/monoid/web/ssl/test-ssl-hostname-eq-localhost.keystore");

        Resty resty = new Resty();

        resty.text(String.format("https://localhost:%d%s", getPort(), HelloWorldTestResource.HELLO_WORLD_PATH));
    }

    @Test(expected = SSLHandshakeException.class)
    public void shouldFailWhenCertificateHostnameDoesNotMatchAndHostnameIsNotIgnored() throws Exception {
        HttpsURLConnection.setDefaultSSLSocketFactory(new SSLSocketFactoryImpl());
        HttpsURLConnection.setDefaultHostnameVerifier(AllowAllHostnameVerifier.ALLOW_ALL_HOSTNAMES);

        startServer("us/monoid/web/ssl/test-ssl-hostname-eq-site.com.keystore");

        Resty resty = new Resty();

        resty.text(String.format("https://localhost:%d%s", getPort(), HelloWorldTestResource.HELLO_WORLD_PATH));
    }
}
TOP

Related Classes of us.monoid.web.ssl.IgnoreAllCertsTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.