mirror of https://github.com/shred/acme4j
Add unit tests for letsencrypt module
parent
1e9855bf05
commit
bc8c1e822d
|
@ -28,6 +28,21 @@
|
||||||
<name>acme4j Let's Encrypt</name>
|
<name>acme4j Let's Encrypt</name>
|
||||||
<description>Let's Encrypt service provider for acme4j</description>
|
<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>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.shredzone.acme4j</groupId>
|
<groupId>org.shredzone.acme4j</groupId>
|
||||||
|
|
|
@ -92,7 +92,7 @@ public class LetsEncryptAcmeClientProvider extends AbstractAcmeClientProvider {
|
||||||
* Lazily creates an {@link SSLSocketFactory} that exclusively accepts the Let's
|
* Lazily creates an {@link SSLSocketFactory} that exclusively accepts the Let's
|
||||||
* Encrypt certificate.
|
* Encrypt certificate.
|
||||||
*/
|
*/
|
||||||
private SSLSocketFactory createSocketFactory() throws IOException {
|
protected SSLSocketFactory createSocketFactory() throws IOException {
|
||||||
if (sslSocketFactory == null) {
|
if (sslSocketFactory == null) {
|
||||||
try {
|
try {
|
||||||
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
|
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
|
|
|
@ -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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
4
pom.xml
4
pom.xml
|
@ -128,7 +128,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-report-plugin</artifactId>
|
<artifactId>maven-surefire-report-plugin</artifactId>
|
||||||
<version>2.17</version>
|
<version>2.19</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
@ -149,7 +149,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>[4,)</version>
|
<version>[4.7,)</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
Loading…
Reference in New Issue