From 298dfbc1611de1e35b7c43999fb9c9961cf68531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sun, 13 Dec 2015 19:19:08 +0100 Subject: [PATCH] Add unit tests for Authorization --- .../shredzone/acme4j/AuthorizationTest.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java new file mode 100644 index 00000000..9dbdd417 --- /dev/null +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/AuthorizationTest.java @@ -0,0 +1,147 @@ +/* + * 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; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.shredzone.acme4j.challenge.Challenge; +import org.shredzone.acme4j.challenge.DnsChallenge; +import org.shredzone.acme4j.challenge.HttpChallenge; +import org.shredzone.acme4j.challenge.ProofOfPossessionChallenge; +import org.shredzone.acme4j.challenge.TlsSniChallenge; + +/** + * Unit tests for {@link Authorization}. + * + * @author Richard "Shred" Körber + */ +public class AuthorizationTest { + + private Authorization authorization; + + /** + * Sets up an {@link Authorization} to be tested. + */ + @Before + public void setup() { + Challenge challenge1 = setupChallenge(HttpChallenge.TYPE, new HttpChallenge()); + Challenge challenge2 = setupChallenge(DnsChallenge.TYPE, new DnsChallenge()); + Challenge challenge3 = setupChallenge(TlsSniChallenge.TYPE, new TlsSniChallenge()); + + List challenges = new ArrayList<>(); + challenges.add(challenge1); + challenges.add(challenge2); + challenges.add(challenge3); + + List> combinations = new ArrayList<>(); + combinations.add(Collections.unmodifiableList(Arrays.asList(challenge1))); + combinations.add(Collections.unmodifiableList(Arrays.asList(challenge2, challenge3))); + + authorization = new Authorization(); + authorization.setChallenges(Collections.unmodifiableList(challenges)); + authorization.setCombinations(Collections.unmodifiableList(combinations)); + } + + /** + * Test that {@link Authorization#findChallenge(String)} does only find standalone + * challenges, and nothing else. + */ + @Test + public void testFindChallenge() { + // ProofOfPossesionChallenge is not available at all + Challenge c1 = authorization.findChallenge(ProofOfPossessionChallenge.TYPE); + assertThat(c1, is(nullValue())); + + // HttpChallenge is available as standalone challenge + Challenge c2 = authorization.findChallenge(HttpChallenge.TYPE); + assertThat(c2, is(notNullValue())); + assertThat(c2, is(instanceOf(HttpChallenge.class))); + + // TlsSniChallenge is available, but not as standalone challenge + Challenge c3 = authorization.findChallenge(TlsSniChallenge.TYPE); + assertThat(c3, is(nullValue())); + } + + /** + * Test that {@link Authorization#findCombination(String...)} does only find proper + * combinations. + */ + @Test + @SuppressWarnings("unchecked") + public void testFindCombination() { + // Standalone challenge + Collection c1 = authorization.findCombination(HttpChallenge.TYPE); + assertThat(c1, hasSize(1)); + assertThat(c1, contains(instanceOf(HttpChallenge.class))); + + // Available combined challenge + Collection c2 = authorization.findCombination(DnsChallenge.TYPE, TlsSniChallenge.TYPE); + assertThat(c2, hasSize(2)); + assertThat(c2, contains(instanceOf(DnsChallenge.class), + instanceOf(TlsSniChallenge.class))); + + // Order does not matter + Collection c3 = authorization.findCombination(TlsSniChallenge.TYPE, DnsChallenge.TYPE); + assertThat(c3, hasSize(2)); + assertThat(c3, contains(instanceOf(DnsChallenge.class), + instanceOf(TlsSniChallenge.class))); + + // Finds smaller combinations as well + Collection c4 = authorization.findCombination(DnsChallenge.TYPE, TlsSniChallenge.TYPE, ProofOfPossessionChallenge.TYPE); + assertThat(c4, hasSize(2)); + assertThat(c4, contains(instanceOf(DnsChallenge.class), + instanceOf(TlsSniChallenge.class))); + + // Finds the smallest possible combination + Collection c5 = authorization.findCombination(DnsChallenge.TYPE, TlsSniChallenge.TYPE, HttpChallenge.TYPE); + assertThat(c5, hasSize(1)); + assertThat(c5, contains(instanceOf(HttpChallenge.class))); + + // Finds only entire combinations + Collection c6 = authorization.findCombination(DnsChallenge.TYPE); + assertThat(c6, is(nullValue())); + + // Does not find challenges that have not been provided + Collection c7 = authorization.findCombination(ProofOfPossessionChallenge.TYPE); + assertThat(c7, is(nullValue())); + } + + /** + * Sets up a {@link Challenge}. + * + * @param typeName + * Type name to be set in the {@link Challenge} + * @param instance + * Newly created {@link Challenge} + * @return Initialized {@link Challenge} + */ + private Challenge setupChallenge(String typeName, Challenge instance) { + Map data = new HashMap<>(); + data.put("type", typeName); + instance.unmarshall(data); + return instance; + } + +}