001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2016 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.connector; 015 016import static org.assertj.core.api.Assertions.assertThat; 017import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; 018 019import java.net.URI; 020import java.net.URL; 021import java.util.ServiceLoader; 022 023import org.junit.jupiter.api.Test; 024import org.shredzone.acme4j.Login; 025import org.shredzone.acme4j.Session; 026import org.shredzone.acme4j.challenge.Challenge; 027import org.shredzone.acme4j.provider.AcmeProvider; 028import org.shredzone.acme4j.toolbox.JSON; 029 030/** 031 * Unit tests for {@link Session#provider()}. Requires that both enclosed 032 * {@link AcmeProvider} implementations are registered via Java's {@link ServiceLoader} 033 * API when the test is run. 034 */ 035public class SessionProviderTest { 036 037 /** 038 * There are no testing providers accepting {@code acme://example.org}. Test that 039 * connecting to this URI will result in an {@link IllegalArgumentException}. 040 */ 041 @Test 042 public void testNone() { 043 assertThatIllegalArgumentException() 044 .isThrownBy(() -> new Session(new URI("acme://example.org")).provider()) 045 .withMessage("No ACME provider found for acme://example.org"); 046 } 047 048 /** 049 * Test that connecting to an acme URI will return an {@link AcmeProvider}, and that 050 * the result is cached. 051 */ 052 @Test 053 public void testConnectURI() throws Exception { 054 var session = new Session(new URI("acme://example.com")); 055 056 var provider = session.provider(); 057 assertThat(provider).isInstanceOf(Provider1.class); 058 059 var provider2 = session.provider(); 060 assertThat(provider2).isInstanceOf(Provider1.class); 061 assertThat(provider2).isSameAs(provider); 062 } 063 064 /** 065 * There are two testing providers accepting {@code acme://example.net}. Test that 066 * connecting to this URI will result in an {@link IllegalArgumentException}. 067 */ 068 @Test 069 public void testDuplicate() { 070 assertThatIllegalArgumentException() 071 .isThrownBy(() -> new Session(new URI("acme://example.net")).provider()) 072 .withMessage("Both ACME providers Provider1 and Provider2 accept" + 073 " acme://example.net. Please check your classpath."); 074 } 075 076 public static class Provider1 implements AcmeProvider { 077 @Override 078 public boolean accepts(URI serverUri) { 079 return "acme".equals(serverUri.getScheme()) 080 && ("example.com".equals(serverUri.getHost()) 081 || "example.net".equals(serverUri.getHost())); 082 } 083 084 @Override 085 public Connection connect(URI serverUri, NetworkSettings networkSettings) { 086 throw new UnsupportedOperationException(); 087 } 088 089 @Override 090 public URL resolve(URI serverUri) { 091 throw new UnsupportedOperationException(); 092 } 093 094 @Override 095 public JSON directory(Session session, URI serverUri) { 096 throw new UnsupportedOperationException(); 097 } 098 099 @Override 100 public Challenge createChallenge(Login login, JSON data) { 101 throw new UnsupportedOperationException(); 102 } 103 } 104 105 public static class Provider2 implements AcmeProvider { 106 @Override 107 public boolean accepts(URI serverUri) { 108 return "acme".equals(serverUri.getScheme()) 109 && "example.net".equals(serverUri.getHost()); 110 } 111 112 @Override 113 public Connection connect(URI serverUri, NetworkSettings networkSettings) { 114 throw new UnsupportedOperationException(); 115 } 116 117 @Override 118 public URL resolve(URI serverUri) { 119 throw new UnsupportedOperationException(); 120 } 121 122 @Override 123 public JSON directory(Session session, URI serverUri) { 124 throw new UnsupportedOperationException(); 125 } 126 127 @Override 128 public Challenge createChallenge(Login login, JSON data) { 129 throw new UnsupportedOperationException(); 130 } 131 } 132 133}