package at.gv.egiz.eaaf.core.test.credentials; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; import java.net.URL; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.impl.credential.inline.InlineKeyStoreParser; import at.gv.egiz.eaaf.core.test.dummy.DummyAuthConfigMap; import lombok.SneakyThrows; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/spring/test_eaaf_pvp_lazy.beans.xml") @DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD) public class InlineKeyStoreTest { @Autowired private DummyAuthConfigMap mapConfig; @Autowired private ResourceLoader resourceLoader; @Test @SneakyThrows public void inlineKeyStoreEccSuccess() throws EaafException { assertNotNull("no keystore", InlineKeyStoreParser.buildKeyStore( new URL(null, "pkcs12:keystore?private=src/test/resources/data/certs/privateEcKey.pem" + "&cert=src/test/resources/data/certs/selfSignedEcCertificate.pem", new InlineKeyStoreParser()), resourceLoader, mapConfig.getConfigurationRootDirectory())); } @Test @SneakyThrows public void inlineTrustStoreSuccess() throws EaafException { assertNotNull("no keystore", InlineKeyStoreParser.buildKeyStore( new URL(null, "pkcs12:truststore?" + "cert=src/test/resources/data/certs/selfSignedEcCertificate.pem", new InlineKeyStoreParser()), resourceLoader, mapConfig.getConfigurationRootDirectory())); } @Test @SneakyThrows public void inlineKeyStoreSymSuccess() throws EaafException { assertNotNull("no keystore", InlineKeyStoreParser.buildKeyStore( new URL(null, "pkcs12:keystore?" + "inlineSecret=mxuqEAXci2cMNU5FCdbxIaNzJoMv%2FWds7j9gY992TTw%3D", new InlineKeyStoreParser()), resourceLoader, mapConfig.getConfigurationRootDirectory())); } @Test @SneakyThrows public void invalidCertFile() throws EaafException { check("pkcs12:keystore?" + "private=src/test/resources/data/certs/privateEcKey.pem" + "&cert=src/test/resources/data/certs/invalidCertificate.pem"); } @Test @SneakyThrows public void missingKey() throws EaafException { check("pkcs12:keystore?" + "cert=src/test/resources/data/certs/selfSignedEcCertificate.pem" + "&cert=src/test/resources/data/certs/BRZStammCA201.pem"); } @Test @SneakyThrows public void missingCert() throws EaafException { check("pkcs12:keystore?" + "private=src/test/resources/data/certs/privateEcKey.pem"); } @Test @SneakyThrows public void invalidType() throws EaafException { check("pkcs12:unknown?" + "private=src/test/resources/data/certs/privateEcKey.pem"); } @Test @SneakyThrows public void twoKeyFiles() throws EaafException { check("pkcs12:keystore?" + "cert=src/test/resources/data/certs/selfSignedEcCertificate.pem" + "&private=src/test/resources/data/certs/privateEcKey.pem" + "&private=src/test/resources/data/certs/privateEcKey.pem"); } @Test @SneakyThrows public void twoSymKeyFiles() throws EaafException { check("pkcs12:keystore?" + "inlineSecret=mxuqEAXci2cMNU5FCdbxIaNzJoMv%2FWds7j9gY992TTw%3D" + "&inlineSecret=mxuqEAXci2cMNU5FCdbxIaNzJoMv%2FWds7j9gY992TTw%3D"); } @Test @SneakyThrows public void missingParams() throws EaafException { check("pkcs12:keystore"); } private void check(String url) { assertThrows(IllegalArgumentException.class, () -> InlineKeyStoreParser.buildKeyStore( new URL(null, url, new InlineKeyStoreParser()), resourceLoader, mapConfig.getConfigurationRootDirectory())); } }