diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java index d919d953..9e285a82 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/RegistrationTest.java @@ -35,6 +35,7 @@ import org.jose4j.jws.JsonWebSignature; import org.jose4j.jwx.CompactSerializer; import org.jose4j.lang.JoseException; import org.junit.Test; +import org.shredzone.acme4j.Registration.EditableRegistration; import org.shredzone.acme4j.challenge.Challenge; import org.shredzone.acme4j.challenge.Dns01Challenge; import org.shredzone.acme4j.challenge.Http01Challenge; @@ -261,6 +262,32 @@ public class RegistrationTest { provider.close(); } + /** + * Test that a bad domain parameter is not accepted. + */ + @Test + public void testAuthorizeBadDomain() throws Exception { + TestableConnectionProvider provider = new TestableConnectionProvider(); + Session session = provider.createSession(); + Registration registration = Registration.bind(session, locationUri); + + try { + registration.authorizeDomain(null); + fail("null domain was accepted"); + } catch (NullPointerException ex) { + // expected + } + + try { + registration.authorizeDomain(""); + fail("empty domain string was accepted"); + } catch (IllegalArgumentException ex) { + // expected + } + + provider.close(); + } + /** * Test that a certificate can be requested and is delivered synchronously. */ @@ -533,15 +560,19 @@ public class RegistrationTest { Registration registration = new Registration(provider.createSession(), locationUri); - registration.modify() - .setAgreement(agreementUri) - .addContact("mailto:foo2@example.com") - .commit(); + EditableRegistration editable = registration.modify(); + assertThat(editable, notNullValue()); + + editable.setAgreement(agreementUri); + editable.addContact("mailto:foo2@example.com"); + editable.getContacts().add(URI.create("mailto:foo3@example.com")); + editable.commit(); assertThat(registration.getLocation(), is(locationUri)); assertThat(registration.getAgreement(), is(agreementUri)); - assertThat(registration.getContacts().size(), is(1)); + assertThat(registration.getContacts().size(), is(2)); assertThat(registration.getContacts().get(0), is(URI.create("mailto:foo2@example.com"))); + assertThat(registration.getContacts().get(1), is(URI.create("mailto:foo3@example.com"))); provider.close(); } diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java index 11040356..1ea88580 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/connector/DefaultConnectionTest.java @@ -415,6 +415,32 @@ public class DefaultConnectionTest { verifyNoMoreInteractions(mockUrlConnection); } + /** + * Test if an {@link AcmeException} is thrown if there is a generic error. + */ + @Test + public void testAcceptThrowsServerException() throws IOException { + when(mockUrlConnection.getHeaderField("Content-Type")) + .thenReturn("text/html"); + when(mockUrlConnection.getResponseCode()) + .thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); + when(mockUrlConnection.getResponseMessage()) + .thenReturn("Infernal Server Error"); + + try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { + conn.conn = mockUrlConnection; + conn.accept(HttpURLConnection.HTTP_OK); + fail("Expected to fail"); + } catch (AcmeException ex) { + assertThat(ex.getMessage(), is("HTTP 500: Infernal Server Error")); + } + + verify(mockUrlConnection).getHeaderField("Content-Type"); + verify(mockUrlConnection, atLeastOnce()).getResponseCode(); + verify(mockUrlConnection, atLeastOnce()).getResponseMessage(); + verifyNoMoreInteractions(mockUrlConnection); + } + /** * Test GET requests. */ @@ -503,6 +529,17 @@ public class DefaultConnectionTest { assertThat(jws.verifySignature(), is(true)); } + /** + * Test signed POST requests if there is no nonce. + */ + @Test(expected = AcmeProtocolException.class) + public void testSendSignedRequestNoNonce() throws Exception { + try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { + ClaimBuilder cb = new ClaimBuilder(); + conn.sendSignedRequest(requestUri, cb, DefaultConnectionTest.this.session); + } + } + /** * Test getting a JSON response. */ diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/exception/AcmeRetryAfterExceptionTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/exception/AcmeRetryAfterExceptionTest.java new file mode 100644 index 00000000..1fbad451 --- /dev/null +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/exception/AcmeRetryAfterExceptionTest.java @@ -0,0 +1,55 @@ +/* + * acme4j - Java ACME client + * + * Copyright (C) 2016 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.exception; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +import java.util.Date; + +import org.junit.Test; + +/** + * Unit tests for {@link AcmeRetryAfterException}. + */ +public class AcmeRetryAfterExceptionTest { + + /** + * Test that parameters are correctly returned. + */ + @Test + public void testAcmeRetryAfterException() { + String detail = "Too early"; + Date retryAfter = new Date(System.currentTimeMillis() + 60 * 1000L); + + AcmeRetryAfterException ex + = new AcmeRetryAfterException(detail, retryAfter); + + assertThat(ex.getMessage(), is(detail)); + assertThat(ex.getRetryAfter(), is(retryAfter)); + } + + /** + * Test that optional parameters are null-safe. + */ + @Test + public void testNullAcmeRetryAfterException() { + AcmeRetryAfterException ex + = new AcmeRetryAfterException(null, null); + + assertThat(ex.getMessage(), nullValue()); + assertThat(ex.getRetryAfter(), nullValue()); + } + +} diff --git a/acme4j-client/src/test/resources/json.properties b/acme4j-client/src/test/resources/json.properties index 78b5fb2b..1eeaff6c 100644 --- a/acme4j-client/src/test/resources/json.properties +++ b/acme4j-client/src/test/resources/json.properties @@ -42,11 +42,11 @@ newRegistration = \ modifyRegistration = \ {"resource":"reg",\ "agreement":"http://example.com/agreement.pdf",\ - "contact":["mailto:foo2@example.com"]} + "contact":["mailto:foo2@example.com","mailto:foo3@example.com"]} modifyRegistrationResponse = \ {"agreement":"http://example.com/agreement.pdf",\ - "contact":["mailto:foo2@example.com"]} + "contact":["mailto:foo2@example.com","mailto:foo3@example.com"]} updateRegistration = \ {"resource":"reg"}