From 75383ca79482d3f2a5c279802c4f9f211e6d18a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 12 Feb 2017 17:47:50 +0100 Subject: [PATCH] Add integration tests against a Pebble test server --- .../shredzone/acme4j/it/AbstractPebbleIT.java | 81 +++++++++++++++++++ .../org/shredzone/acme4j/it/SessionIT.java | 72 +++++++++++++++++ pom.xml | 14 ++++ src/site/markdown/index.md.vm | 9 +++ 4 files changed, 176 insertions(+) create mode 100644 acme4j-client/src/test/java/org/shredzone/acme4j/it/AbstractPebbleIT.java create mode 100644 acme4j-client/src/test/java/org/shredzone/acme4j/it/SessionIT.java diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/it/AbstractPebbleIT.java b/acme4j-client/src/test/java/org/shredzone/acme4j/it/AbstractPebbleIT.java new file mode 100644 index 00000000..203f62a2 --- /dev/null +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/it/AbstractPebbleIT.java @@ -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. + *

+ * These tests require a running + * Pebble 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); + } + } + +} diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/it/SessionIT.java b/acme4j-client/src/test/java/org/shredzone/acme4j/it/SessionIT.java new file mode 100644 index 00000000..bf47f007 --- /dev/null +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/it/SessionIT.java @@ -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("{}")); + } + +} diff --git a/pom.xml b/pom.xml index c3ea136d..59e099bb 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,7 @@ 0.5.3 1.7.21 utf-8 + true @@ -84,6 +85,19 @@ java.net.HttpURLConnection + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + + + integration-test + verify + + + + org.apache.maven.plugins maven-jar-plugin diff --git a/src/site/markdown/index.md.vm b/src/site/markdown/index.md.vm index bc2cfbd8..782b7d0b 100644 --- a/src/site/markdown/index.md.vm +++ b/src/site/markdown/index.md.vm @@ -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`.