001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2017 Richard "Shred" Körber
005 *   http://acme4j.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.acme4j.provider.pebble;
015
016import static org.assertj.core.api.Assertions.assertThat;
017import static org.junit.jupiter.api.Assertions.assertThrows;
018import static org.shredzone.acme4j.toolbox.TestUtils.url;
019
020import java.net.URI;
021import java.net.URISyntaxException;
022
023import org.assertj.core.api.AutoCloseableSoftAssertions;
024import org.junit.jupiter.api.Test;
025
026/**
027 * Unit tests for {@link PebbleAcmeProvider}.
028 */
029public class PebbleAcmeProviderTest {
030
031    /**
032     * Tests if the provider accepts the correct URIs.
033     */
034    @Test
035    public void testAccepts() throws URISyntaxException {
036        var provider = new PebbleAcmeProvider();
037
038        try (var softly = new AutoCloseableSoftAssertions()) {
039            softly.assertThat(provider.accepts(new URI("acme://pebble"))).isTrue();
040            softly.assertThat(provider.accepts(new URI("acme://pebble/"))).isTrue();
041            softly.assertThat(provider.accepts(new URI("acme://pebble:12345"))).isTrue();
042            softly.assertThat(provider.accepts(new URI("acme://pebble:12345/"))).isTrue();
043            softly.assertThat(provider.accepts(new URI("acme://pebble/some-host.example.com"))).isTrue();
044            softly.assertThat(provider.accepts(new URI("acme://pebble/some-host.example.com:12345"))).isTrue();
045            softly.assertThat(provider.accepts(new URI("acme://example.com"))).isFalse();
046            softly.assertThat(provider.accepts(new URI("http://example.com/acme"))).isFalse();
047            softly.assertThat(provider.accepts(new URI("https://example.com/acme"))).isFalse();
048        }
049    }
050
051    /**
052     * Test if acme URIs are properly resolved.
053     */
054    @Test
055    public void testResolve() throws URISyntaxException {
056        var provider = new PebbleAcmeProvider();
057
058        assertThat(provider.resolve(new URI("acme://pebble")))
059                .isEqualTo(url("https://localhost:14000/dir"));
060        assertThat(provider.resolve(new URI("acme://pebble/")))
061                .isEqualTo(url("https://localhost:14000/dir"));
062        assertThat(provider.resolve(new URI("acme://pebble:12345")))
063            .isEqualTo(url("https://localhost:12345/dir"));
064        assertThat(provider.resolve(new URI("acme://pebble:12345/")))
065            .isEqualTo(url("https://localhost:12345/dir"));
066        assertThat(provider.resolve(new URI("acme://pebble/pebble.example.com")))
067                .isEqualTo(url("https://pebble.example.com:14000/dir"));
068        assertThat(provider.resolve(new URI("acme://pebble/pebble.example.com:12345")))
069                .isEqualTo(url("https://pebble.example.com:12345/dir"));
070        assertThat(provider.resolve(new URI("acme://pebble/pebble.example.com:12345/")))
071                .isEqualTo(url("https://pebble.example.com:12345/dir"));
072
073        assertThrows(IllegalArgumentException.class, () ->
074                provider.resolve(new URI("acme://pebble/bad.example.com:port")));
075
076        assertThrows(IllegalArgumentException.class, () ->
077                provider.resolve(new URI("acme://pebble/bad.example.com:1234/foo")));
078    }
079
080}