Add integration tests against a Pebble test server

pull/55/head
Richard Körber 2017-02-12 17:47:50 +01:00
parent 3b3f379be2
commit 75383ca794
4 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,81 @@
/*
* acme4j - Java ACME client
*
* Copyright (C) 2017 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.it;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
/**
* Superclass for all Pebble related integration tests.
* <p>
* These tests require a running
* <a href="https://github.com/letsencrypt/pebble">Pebble</a> ACME test server at
* localhost port 14000.
*/
public abstract class AbstractPebbleIT {
private final String pebbleHost = System.getProperty("pebbleHost", "localhost");
private final int pebblePort = Integer.parseInt(System.getProperty("pebblePort", "14000"));
/**
* @return The {@link URI} of the pebble server to test against.
*/
protected URI pebbleURI() {
return URI.create("acme://pebble/" + pebbleHost + ":" + pebblePort);
}
/**
* Creates a fresh key pair.
*
* @return Created {@link KeyPair}, guaranteed to be unknown to the Pebble server
*/
protected KeyPair createKeyPair() {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
return keyGen.generateKeyPair();
} catch (NoSuchAlgorithmException ex) {
throw new InternalError(ex);
}
}
/**
* Asserts that the given {@link URI} is not {@code null} and refers to the Pebble
* server.
*
* @param uri
* {@link URI} to assert
*/
protected void assertIsPebbleUri(URI uri) {
assertThat(uri, not(nullValue()));
try {
URL url = uri.toURL();
assertThat(url.getProtocol(), is("http"));
assertThat(url.getHost(), is(pebbleHost));
assertThat(url.getPort(), is(pebblePort));
assertThat(url.getPath(), not(isEmptyOrNullString()));
} catch (MalformedURLException ex) {
throw new IllegalArgumentException(ex);
}
}
}

View File

@ -0,0 +1,72 @@
/*
* acme4j - Java ACME client
*
* Copyright (C) 2017 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.it;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
import java.security.KeyPair;
import org.junit.Test;
import org.shredzone.acme4j.Metadata;
import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.connector.Resource;
import org.shredzone.acme4j.exception.AcmeException;
/**
* Session related integration tests.
*/
public class SessionIT extends AbstractPebbleIT {
private final KeyPair keyPair = createKeyPair();
@Test
public void testNonce() throws AcmeException {
Session session = new Session(pebbleURI(), keyPair);
// No nonce yet on a fresh session
assertThat(session.getNonce(), is(nullValue()));
// Request directory, also providing a nonce
session.getMetadata();
// A nonce must be set now
assertThat(session.getNonce(), not(nullValue()));
assertThat(session.provider(), not(nullValue()));
}
@Test
public void testResources() throws AcmeException {
Session session = new Session(pebbleURI(), keyPair);
assertIsPebbleUri(session.resourceUri(Resource.NEW_NONCE));
assertIsPebbleUri(session.resourceUri(Resource.NEW_REG));
}
@Test
public void testMetadata() throws AcmeException {
Session session = new Session(pebbleURI(), keyPair);
Metadata meta = session.getMetadata();
assertThat(meta, not(nullValue()));
// Pebble does not currently deliver any metadata
assertThat(meta.getTermsOfService(), is(nullValue()));
assertThat(meta.getWebsite(), is(nullValue()));
assertThat(meta.getCaaIdentities(), is(empty()));
assertThat(meta.getJSON().toString(), sameJSONAs("{}"));
}
}

14
pom.xml
View File

@ -55,6 +55,7 @@
<jose4j.version>0.5.3</jose4j.version>
<slf4j.version>1.7.21</slf4j.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<skipITs>true</skipITs>
</properties>
<modules>
@ -84,6 +85,19 @@
<excludedGroups>java.net.HttpURLConnection</excludedGroups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>

View File

@ -35,3 +35,12 @@ There is also an optional utility module that will help you handling key pairs a
```
Now just have a look at [this source code](https://github.com/shred/acme4j/blob/master/acme4j-example/src/main/java/org/shredzone/acme4j/ClientTest.java) to see an example usage.
Running Integration Tests
-------------------------
_acme4j-client_ contains a number of integration tests. These tests are _not_ executed by maven by default, as they require a [Pebble ACME test server](https://github.com/letsencrypt/pebble).
To run them, install and run a Pebble server instance on your build machine. Then invoke maven with the `-DskipITs=false` option set, to enable the integration tests at `verify` phase.
If you are running a Pebble server on a different host and/or port, invoke maven with the `pebbleHost` and `pebblePort` system properties set to the appropriate host and port, respectively. For example, to connect to `acme.example.com` port 12345, use `-DpebbleHost=acme.example.com -DpebblePort=12345`.