Add unit tests for letsencrypt module

pull/17/merge
Richard Körber 2015-12-10 23:46:15 +01:00
parent 1e9855bf05
commit bc8c1e822d
4 changed files with 117 additions and 3 deletions

View File

@ -28,6 +28,21 @@
<name>acme4j Let's Encrypt</name>
<description>Let's Encrypt service provider for acme4j</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<parallel>classes</parallel>
<threadCount>10</threadCount>
<excludedGroups>org.shredzone.acme4j.provider.LetsEncryptAcmeClientProviderTest$RequiresNetwork</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.shredzone.acme4j</groupId>

View File

@ -92,7 +92,7 @@ public class LetsEncryptAcmeClientProvider extends AbstractAcmeClientProvider {
* Lazily creates an {@link SSLSocketFactory} that exclusively accepts the Let's
* Encrypt certificate.
*/
private SSLSocketFactory createSocketFactory() throws IOException {
protected SSLSocketFactory createSocketFactory() throws IOException {
if (sslSocketFactory == null) {
try {
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

View File

@ -0,0 +1,99 @@
/*
* acme4j - Java ACME client
*
* Copyright (C) 2015 Richard "Shred" Körber
* http://acme4j.shredzone.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.shredzone.acme4j.provider;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSocketFactory;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* Unit tests for {@link LetsEncryptAcmeClientProvider}.
*
* @author Richard "Shred" Körber
*/
public class LetsEncryptAcmeClientProviderTest {
public interface RequiresNetwork {}
/**
* Tests if the provider accepts the correct URIs.
*/
@Test
public void testAccepts() throws URISyntaxException {
LetsEncryptAcmeClientProvider provider = new LetsEncryptAcmeClientProvider();
assertThat(provider.accepts(new URI("acme://letsencrypt.org")), is(true));
assertThat(provider.accepts(new URI("acme://letsencrypt.org/")), is(true));
assertThat(provider.accepts(new URI("acme://letsencrypt.org/staging")), is(true));
assertThat(provider.accepts(new URI("acme://letsencrypt.org/v01")), is(true));
assertThat(provider.accepts(new URI("acme://example.com")), is(false));
assertThat(provider.accepts(new URI("http://example.com/acme")), is(false));
assertThat(provider.accepts(new URI("https://example.com/acme")), is(false));
}
/**
* Test if the {@link LetsEncryptAcmeClientProvider#openConnection(URI)} accepts only
* the Let's Encrypt certificate.
*/
@Test
@Category(RequiresNetwork.class)
public void testCertificate() throws IOException, URISyntaxException {
LetsEncryptAcmeClientProvider provider = new LetsEncryptAcmeClientProvider();
try {
HttpURLConnection goodConn = provider.openConnection(
new URI("https://acme-staging.api.letsencrypt.org/directory"));
assertThat(goodConn, is(instanceOf(HttpsURLConnection.class)));
goodConn.connect();
} catch (SSLHandshakeException ex) {
fail("Connection does not accept Let's Encrypt certificate");
}
try {
HttpURLConnection badConn = provider.openConnection(
new URI("https://www.google.com"));
assertThat(badConn, is(instanceOf(HttpsURLConnection.class)));
badConn.connect();
fail("Connection accepts foreign certificate");
} catch (SSLHandshakeException ex) {
// expected
}
}
/**
* Test that the {@link SSLSocketFactory} can be instantiated and is cached.
*/
@Test
public void testCreateSocketFactory() throws IOException {
LetsEncryptAcmeClientProvider provider = new LetsEncryptAcmeClientProvider();
SSLSocketFactory factory1 = provider.createSocketFactory();
assertThat(factory1, is(notNullValue()));
SSLSocketFactory factory2 = provider.createSocketFactory();
assertThat(factory1, is(sameInstance(factory2)));
}
}

View File

@ -128,7 +128,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.17</version>
<version>2.19</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -149,7 +149,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>[4,)</version>
<version>[4.7,)</version>
<scope>test</scope>
</dependency>
<dependency>