From e2bfdc313c0b6395d272624688b4ed1cba7ce967 Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Thu, 30 Apr 2026 10:21:59 +0200 Subject: Move pdf-as-web.conf to Spring & add MOA timeout (#84) * move -Dpdf-as-web.conf to spring * add configurable timeout to MOA signing * update workflow trigger --- .../pdfas/web/servlets/SimpleSignServletTest.java | 92 ++++++++++++ .../web/servlets/SimpleVerifyServletTest.java | 129 ++++++++++++++++ .../gv/egiz/pdfas/web/test/MockMoaSigningTest.java | 18 +-- .../egiz/pdfas/web/test/SimpleSignServletTest.java | 118 --------------- .../pdfas/web/test/SimpleVerifyServletTest.java | 163 --------------------- .../egiz/pdfas/web/test/SimpleWebServiceTest.java | 53 ++++--- .../SimpleWebServiceWithoutVerificationTest.java | 50 ++++--- 7 files changed, 290 insertions(+), 333 deletions(-) create mode 100644 pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleSignServletTest.java create mode 100644 pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleVerifyServletTest.java delete mode 100644 pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleSignServletTest.java delete mode 100644 pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleVerifyServletTest.java (limited to 'pdf-as-web/src/test/java') diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleSignServletTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleSignServletTest.java new file mode 100644 index 00000000..da735c8c --- /dev/null +++ b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleSignServletTest.java @@ -0,0 +1,92 @@ +package at.gv.egiz.pdfas.web.servlets; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; + +import at.gv.egiz.pdfas.web.config.PdfAsWebSpringConfiguration; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.SneakyThrows; +import org.apache.commons.io.IOUtils; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import at.gv.egiz.pdfas.web.filter.UserAgentFilter; +import at.gv.egiz.pdfas.web.stats.StatisticEvent; +import at.gv.egiz.pdfas.web.stats.StatisticEvent.Operation; +import at.gv.egiz.pdfas.web.stats.StatisticEvent.Source; +import org.springframework.mock.web.MockServletConfig; +import org.springframework.mock.web.MockServletContext; +import org.springframework.stereotype.Component; +import org.springframework.test.context.junit4.SpringRunner; + +//@Ignore +@RunWith(SpringRunner.class) +@SpringBootTest +public class SimpleSignServletTest { + + + @BeforeClass + public static void classInitializer() throws IOException { + final String current = new java.io.File(".").getCanonicalPath(); + System.setProperty("pdf-as-web.conf", + current + "/src/test/resources/config/pdfas/pdf-as-web.properties"); + + } + + @Before + @SneakyThrows + public void setup() { + servlet.init( + new MockServletConfig( + new MockServletContext("src/main/webapp"), + "ExternSignServlet") + ); + } + + @Autowired private ExternSignServlet servlet; + + @Test + @SneakyThrows + public void sigBlockParameterTest() { + + byte[] pdf = IOUtils.toByteArray(SimpleSignServletTest.class.getResourceAsStream("/data/enc_own.pdf")); + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/pdfas"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + httpReq.setAttribute("REQ_DATA_URL", true); + httpReq.setAttribute("connector", "jks"); + + httpReq.setAttribute("sbp:schule", "Güssing"); + + + + // perform operation + performTest(httpReq, httpResp, pdf); + + + //validate state + assertNotNull("httpResp", httpResp); + assertNotNull("no dataUrl redirect", httpResp.getHeader("Location")); + + } + + @SneakyThrows + private void performTest(HttpServletRequest httpReq, HttpServletResponse httpResp, byte[] pdf) { + StatisticEvent statisticEvent = new StatisticEvent(); + statisticEvent.setStartNow(); + statisticEvent.setSource(Source.WEB); + statisticEvent.setOperation(Operation.SIGN); + statisticEvent.setUserAgent(UserAgentFilter.getUserAgent()); + servlet.doSignature(httpReq, httpResp, pdf, statisticEvent); + + } +} diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleVerifyServletTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleVerifyServletTest.java new file mode 100644 index 00000000..3b5d223c --- /dev/null +++ b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/servlets/SimpleVerifyServletTest.java @@ -0,0 +1,129 @@ +package at.gv.egiz.pdfas.web.servlets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Enumeration; + +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import lombok.val; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsValidationException; +import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.lib.api.PdfAsFactory; +import at.gv.egiz.pdfas.web.config.WebConfiguration; +import at.gv.egiz.pdfas.web.filter.UserAgentFilter; +import at.gv.egiz.pdfas.web.helper.PdfAsHelper; +import at.gv.egiz.pdfas.web.stats.StatisticEvent; +import at.gv.egiz.pdfas.web.stats.StatisticEvent.Operation; +import at.gv.egiz.pdfas.web.stats.StatisticEvent.Source; +import lombok.SneakyThrows; +import org.springframework.mock.web.MockServletConfig; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SimpleVerifyServletTest { + + + @BeforeClass + public static void classInitializer() throws IOException { + final String current = new java.io.File(".").getCanonicalPath(); + System.setProperty("pdf-as-web.conf", + current + "/src/test/resources/config/pdfas/pdf-as-web.properties"); + } + + @Before + @SneakyThrows + public void setup() { + servlet.init( + new MockServletConfig( + new MockServletContext("src/main/webapp"), + "VerifyServlet") + ); + } + + + @SuppressWarnings("unused") @Autowired private ExternSignServlet dummy; /* ExternSignServlet ctor does web config init */ + @Autowired private VerifyServlet servlet; + + + @Test + @SneakyThrows + public void unsignedPdf() { + executeTest("/data/enc_own.pdf"); + } + + @Test + @SneakyThrows + public void unsignedWithSigField() { + executeTest("/data/placeholder_sigfield_and_qr.pdf"); + } + + @Test + @SneakyThrows + public void signedPdf() { + executeTest("/data/dummy-pdf-signed.pdf"); + + } + + + + + @SneakyThrows + private MockHttpServletResponse executeTest(String pdfResource) { + val pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream(pdfResource)); + MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/pdfas"); + MockHttpServletResponse httpResp = new MockHttpServletResponse(); + + // perform operation + performTest(httpReq, httpResp, pdf); + + //validate state + assertNotNull("httpResp", httpResp); + assertEquals("httpStatus", 200, httpResp.getStatus()); + String body = httpResp.getContentAsString(); + assertFalse("Empty body", StringUtils.isEmpty(body)); + + return httpResp; + + } + + + @SneakyThrows + private void performTest(HttpServletRequest httpReq, HttpServletResponse httpResp, byte[] pdf) { + + Method method = servlet.getClass().getDeclaredMethod("doVerify", + HttpServletRequest.class, HttpServletResponse.class, byte[].class, StatisticEvent.class); + method.setAccessible(true); + StatisticEvent statisticEvent = new StatisticEvent(); + statisticEvent.setStartNow(); + statisticEvent.setSource(Source.WEB); + statisticEvent.setOperation(Operation.VERIFY); + statisticEvent.setUserAgent(UserAgentFilter.getUserAgent()); + + method.invoke(servlet, httpReq, httpResp, pdf, statisticEvent); + + } +} diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/MockMoaSigningTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/MockMoaSigningTest.java index 466cfcca..10e87af6 100644 --- a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/MockMoaSigningTest.java +++ b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/MockMoaSigningTest.java @@ -9,6 +9,7 @@ import at.gv.egiz.pdfas.lib.impl.configuration.ConfigurationImpl; import at.gv.egiz.pdfas.moa.MOAConnector; import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore; import at.gv.egiz.pdfas.sigs.pkcs7detached.PKCS7DetachedSigner; +import at.gv.egiz.pdfas.web.config.PdfAsWebSpringConfiguration; import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.helper.PdfAsHelper; import at.gv.egiz.pdfas.web.servlets.ExternSignServlet; @@ -42,11 +43,8 @@ import java.security.PrivateKey; import java.util.*; import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @RunWith(SpringRunner.class) @SpringBootTest(properties = { @@ -57,6 +55,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class MockMoaSigningTest { @Autowired MockMvc mvc; @Autowired ObjectMapper om; + @Autowired PdfAsWebSpringConfiguration config; static { try { @@ -78,7 +77,7 @@ public class MockMoaSigningTest { targetNamespace = "http://reference.e-government.gv.at/namespace/moa/20020822#", endpointInterface = "at.gv.e_government.reference.namespace.moa._20020822_.SignatureCreationPortType") - static class MockMoa implements AutoCloseable, SignatureCreationPortType { + class MockMoa implements AutoCloseable, SignatureCreationPortType { @SneakyThrows private static int freePort() { try (ServerSocket socket = new ServerSocket(0)) { @@ -100,8 +99,8 @@ public class MockMoaSigningTest { public final IPlainSigner signer; @SneakyThrows - private static Properties getBaseProperties() { - try (InputStream in = new FileInputStream(System.getProperty(ExternSignServlet.PDF_AS_WEB_CONF))) { + private Properties getBaseProperties() { + try (InputStream in = new FileInputStream(config.getPdfAsWebConfPath())) { val props = new Properties(); props.load(in); return props; @@ -109,7 +108,7 @@ public class MockMoaSigningTest { } @SneakyThrows - private static void injectProperties(Map overlay) { + private void injectProperties(Map overlay) { val props = getBaseProperties(); if (overlay != null) overlay.forEach(props::setProperty); try (val out = new ByteArrayOutputStream()) { @@ -141,6 +140,7 @@ public class MockMoaSigningTest { injectProperties(Map.of( "moal."+keyIdentifier+".enabled", "true", "moal."+keyIdentifier+".url", endpointURL, + "moal."+keyIdentifier+".timeout", "5000", "moal."+keyIdentifier+".KeyIdentifier", "KG_TEST", "moal."+keyIdentifier+".Certificate", "base64:"+Base64.getEncoder().encodeToString(signer.getCertificate(null).getEncoded()) @@ -231,7 +231,7 @@ public class MockMoaSigningTest { @SneakyThrows public CreateCMSSignatureResponseType createCMSSignature(CreateCMSSignatureRequest body) throws MOAFault { // this will cause a timeout - Thread.sleep(300 * 1000); + Thread.sleep(10 * 1000); throw new RuntimeException("unreachable"); } }) { diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleSignServletTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleSignServletTest.java deleted file mode 100644 index 8ab9cfaf..00000000 --- a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleSignServletTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package at.gv.egiz.pdfas.web.test; - -import static org.junit.Assert.assertNotNull; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Enumeration; -import jakarta.servlet.ServletConfig; -import jakarta.servlet.ServletContext; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.apache.commons.io.IOUtils; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.BlockJUnit4ClassRunner; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; - -import at.gv.egiz.pdfas.web.filter.UserAgentFilter; -import at.gv.egiz.pdfas.web.servlets.ExternSignServlet; -import at.gv.egiz.pdfas.web.stats.StatisticEvent; -import at.gv.egiz.pdfas.web.stats.StatisticEvent.Operation; -import at.gv.egiz.pdfas.web.stats.StatisticEvent.Source; - -//@Ignore -@RunWith(BlockJUnit4ClassRunner.class) -public class SimpleSignServletTest { - - - @BeforeClass - public static void classInitializer() throws IOException { - final String current = new java.io.File(".").getCanonicalPath(); - System.setProperty("pdf-as-web.conf", - current + "/src/test/resources/config/pdfas/pdf-as-web.properties"); - - } - - - @Test - public void sigBlockParameterTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException, IOException, ServletException { - - byte[] pdf = IOUtils.toByteArray(SimpleSignServletTest.class.getResourceAsStream("/data/enc_own.pdf")); - MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/pdfas"); - MockHttpServletResponse httpResp = new MockHttpServletResponse(); - httpReq.setAttribute("REQ_DATA_URL", true); - httpReq.setAttribute("connector", "jks"); - - httpReq.setAttribute("sbp:schule", "Güssing"); - - - - // perform operation - performTest(httpReq, httpResp, pdf); - - - //validate state - assertNotNull("httpResp", httpResp); - assertNotNull("no dataUrl redirect", httpResp.getHeader("Location")); - - } - - - private void performTest(HttpServletRequest httpReq, HttpServletResponse httpResp, byte[] pdf) throws NoSuchMethodException, - SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ServletException { - ExternSignServlet servlet = new ExternSignServlet(); - ServletConfig servletConfig = buildServletConfig(); - servlet.init(servletConfig); - - Method method = servlet.getClass().getDeclaredMethod("doSignature", - HttpServletRequest.class, HttpServletResponse.class, byte[].class, StatisticEvent.class); - method.setAccessible(true); - StatisticEvent statisticEvent = new StatisticEvent(); - statisticEvent.setStartNow(); - statisticEvent.setSource(Source.WEB); - statisticEvent.setOperation(Operation.SIGN); - statisticEvent.setUserAgent(UserAgentFilter.getUserAgent()); - - method.invoke(servlet, httpReq, httpResp, pdf, statisticEvent); - - } - - - private ServletConfig buildServletConfig() { - - return new ServletConfig() { - - private ServletContext servletContext = null; - - @Override - public String getServletName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public ServletContext getServletContext() { - return servletContext; - } - - @Override - public Enumeration getInitParameterNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getInitParameter(String name) { - // TODO Auto-generated method stub - return null; - } - }; - } -} diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleVerifyServletTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleVerifyServletTest.java deleted file mode 100644 index e0075940..00000000 --- a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleVerifyServletTest.java +++ /dev/null @@ -1,163 +0,0 @@ -package at.gv.egiz.pdfas.web.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Enumeration; - -import jakarta.servlet.ServletConfig; -import jakarta.servlet.ServletContext; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.BlockJUnit4ClassRunner; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; - -import at.gv.egiz.pdfas.common.exceptions.PdfAsSettingsValidationException; -import at.gv.egiz.pdfas.common.settings.ISettings; -import at.gv.egiz.pdfas.lib.api.PdfAsFactory; -import at.gv.egiz.pdfas.web.config.WebConfiguration; -import at.gv.egiz.pdfas.web.filter.UserAgentFilter; -import at.gv.egiz.pdfas.web.helper.PdfAsHelper; -import at.gv.egiz.pdfas.web.servlets.VerifyServlet; -import at.gv.egiz.pdfas.web.stats.StatisticEvent; -import at.gv.egiz.pdfas.web.stats.StatisticEvent.Operation; -import at.gv.egiz.pdfas.web.stats.StatisticEvent.Source; -import lombok.SneakyThrows; - -@RunWith(BlockJUnit4ClassRunner.class) -public class SimpleVerifyServletTest { - - - @BeforeClass - public static void classInitializer() throws IOException { - final String current = new java.io.File(".").getCanonicalPath(); - System.setProperty("pdf-as-web.conf", - current + "/src/test/resources/config/pdfas/pdf-as-web.properties"); - - String webconfig = System.getProperty("pdf-as-web.conf"); - - if(webconfig == null) { - throw new RuntimeException("No web configuration provided!"); - } - - WebConfiguration.configure(webconfig); - PdfAsHelper.init(); - - try { - PdfAsFactory.validateConfiguration((ISettings)PdfAsHelper.getPdfAsConfig()); - - } catch (PdfAsSettingsValidationException e) { - e.printStackTrace(); - } - } - - - @Test - @SneakyThrows - public void unsignedPdf() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/enc_own.pdf")); - executeTest(pdf); - - } - - @Test - @SneakyThrows - public void unsignedWithSigField() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/placeholder_sigfield_and_qr.pdf")); - executeTest(pdf); - - } - - @Test - @SneakyThrows - public void signedPdf() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/dummy-pdf-signed.pdf")); - executeTest(pdf); - - } - - - - - @SneakyThrows - private MockHttpServletResponse executeTest(byte[] pdf) { - MockHttpServletRequest httpReq = new MockHttpServletRequest("POST", "https://localhost/pdfas"); - MockHttpServletResponse httpResp = new MockHttpServletResponse(); - - // perform operation - performTest(httpReq, httpResp, pdf); - - //validate state - assertNotNull("httpResp", httpResp); - assertEquals("httpStatus", 200, httpResp.getStatus()); - String body = httpResp.getContentAsString(); - assertFalse("Empty body", StringUtils.isEmpty(body)); - - return httpResp; - - } - - - private void performTest(HttpServletRequest httpReq, HttpServletResponse httpResp, byte[] pdf) throws NoSuchMethodException, - SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ServletException { - VerifyServlet servlet = new VerifyServlet (); - ServletConfig servletConfig = buildServletConfig(); - servlet.init(servletConfig); - - Method method = servlet.getClass().getDeclaredMethod("doVerify", - HttpServletRequest.class, HttpServletResponse.class, byte[].class, StatisticEvent.class); - method.setAccessible(true); - StatisticEvent statisticEvent = new StatisticEvent(); - statisticEvent.setStartNow(); - statisticEvent.setSource(Source.WEB); - statisticEvent.setOperation(Operation.VERIFY); - statisticEvent.setUserAgent(UserAgentFilter.getUserAgent()); - - method.invoke(servlet, httpReq, httpResp, pdf, statisticEvent); - - } - - - private ServletConfig buildServletConfig() { - - return new ServletConfig() { - - private ServletContext servletContext = null; - - @Override - public String getServletName() { - // TODO Auto-generated method stub - return null; - } - - @Override - public ServletContext getServletContext() { - return servletContext; - } - - @Override - public Enumeration getInitParameterNames() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getInitParameter(String name) { - // TODO Auto-generated method stub - return null; - } - }; - } -} diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceTest.java index 1d958323..7d81465a 100644 --- a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceTest.java +++ b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceTest.java @@ -4,8 +4,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.IOException; +import java.net.URL; import java.util.UUID; +import at.gv.egiz.pdfas.api.ws.PDFASSigning; +import at.gv.egiz.pdfas.web.servlets.ExternSignServlet; +import at.gv.egiz.pdfas.web.servlets.SimpleVerifyServletTest; +import jakarta.xml.ws.Service; +import lombok.val; import org.apache.commons.io.IOUtils; import org.junit.BeforeClass; import org.junit.Test; @@ -23,8 +29,15 @@ import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.helper.PdfAsHelper; import at.gv.egiz.pdfas.web.ws.PDFASSigningImpl; import lombok.SneakyThrows; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(BlockJUnit4ClassRunner.class) +import javax.xml.namespace.QName; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class SimpleWebServiceTest { @BeforeClass @@ -32,28 +45,20 @@ public class SimpleWebServiceTest { final String current = new java.io.File(".").getCanonicalPath(); System.setProperty("pdf-as-web.conf", current + "/src/test/resources/config/pdfas/pdf-as-web.properties"); - - String webconfig = System.getProperty("pdf-as-web.conf"); - - if(webconfig == null) { - throw new RuntimeException("No web configuration provided!"); - } - - WebConfiguration.configure(webconfig); - PdfAsHelper.init(); - - try { - PdfAsFactory.validateConfiguration((ISettings)PdfAsHelper.getPdfAsConfig()); - - } catch (PdfAsSettingsValidationException e) { - e.printStackTrace(); - } } + + @BeforeClass + public static void jceWorkaround() { + System.setProperty("javax.net.ssl.trustStoreType", "JKS"); + } + + @LocalServerPort + int port; @Test @SneakyThrows public void sign() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/enc_own.pdf")); + byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/enc_own.pdf")); PDFASSignResponse resp = executeTest(pdf); assertNotNull("signed doc", resp.getSignedPDF()); assertEquals("sign check", 0, resp.getVerificationResponse().getValueCode()); @@ -64,7 +69,7 @@ public class SimpleWebServiceTest { @Test @SneakyThrows public void withSignatureFields() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/placeholder_sigfield_and_qr.pdf")); + byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/placeholder_sigfield_and_qr.pdf")); PDFASSignResponse resp = executeTest(pdf); assertNotNull("signed doc", resp.getSignedPDF()); assertEquals("sign check", 0, resp.getVerificationResponse().getValueCode()); @@ -74,7 +79,11 @@ public class SimpleWebServiceTest { @SneakyThrows private PDFASSignResponse executeTest(byte[] pdf) { - PDFASSigningImpl service = new PDFASSigningImpl(); + val wsdl = new URL("http://localhost:"+port+"/services/wssign?wsdl"); + val serviceName = new QName( + "http://ws.web.pdfas.egiz.gv.at/", + "PDFASSigningImplService"); + val proxy = Service.create(wsdl, serviceName).getPort(PDFASSigning.class); PDFASSignRequest req = new PDFASSignRequest(); req.setRequestID(UUID.randomUUID().toString()); @@ -82,9 +91,9 @@ public class SimpleWebServiceTest { PDFASSignParameters signParams = new PDFASSignParameters(); signParams.setConnector(Connector.JKS); signParams.setTransactionId(UUID.randomUUID().toString()); - req.setParameters(signParams ); + req.setParameters(signParams); - PDFASSignResponse resp = service.signPDFDokument(req); + PDFASSignResponse resp = proxy.signPDFDokument(req); assertNotNull(resp); return resp; diff --git a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceWithoutVerificationTest.java b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceWithoutVerificationTest.java index d7529721..7954415b 100644 --- a/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceWithoutVerificationTest.java +++ b/pdf-as-web/src/test/java/at/gv/egiz/pdfas/web/test/SimpleWebServiceWithoutVerificationTest.java @@ -4,8 +4,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.IOException; +import java.net.URL; import java.util.UUID; +import at.gv.egiz.pdfas.api.ws.PDFASSigning; +import at.gv.egiz.pdfas.web.servlets.SimpleVerifyServletTest; +import jakarta.xml.ws.Service; +import lombok.val; import org.apache.commons.io.IOUtils; import org.junit.BeforeClass; import org.junit.Test; @@ -23,8 +28,15 @@ import at.gv.egiz.pdfas.web.config.WebConfiguration; import at.gv.egiz.pdfas.web.helper.PdfAsHelper; import at.gv.egiz.pdfas.web.ws.PDFASSigningImpl; import lombok.SneakyThrows; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(BlockJUnit4ClassRunner.class) +import javax.xml.namespace.QName; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class SimpleWebServiceWithoutVerificationTest { @BeforeClass @@ -32,28 +44,20 @@ public class SimpleWebServiceWithoutVerificationTest { final String current = new java.io.File(".").getCanonicalPath(); System.setProperty("pdf-as-web.conf", current + "/src/test/resources/config/pdfas/pdf-as-web-verify-disabled.properties"); - - String webconfig = System.getProperty("pdf-as-web.conf"); - - if(webconfig == null) { - throw new RuntimeException("No web configuration provided!"); - } - - WebConfiguration.configure(webconfig); - PdfAsHelper.init(); - - try { - PdfAsFactory.validateConfiguration((ISettings)PdfAsHelper.getPdfAsConfig()); - - } catch (PdfAsSettingsValidationException e) { - e.printStackTrace(); - } } + + @BeforeClass + public static void jceWorkaround() { + System.setProperty("javax.net.ssl.trustStoreType", "JKS"); + } + + @LocalServerPort + int port; @Test @SneakyThrows public void sign() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/enc_own.pdf")); + byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/enc_own.pdf")); PDFASSignResponse resp = executeTest(pdf); assertNotNull("signed doc", resp.getSignedPDF()); assertEquals("sign check", 0, resp.getVerificationResponse().getValueCode()); @@ -64,7 +68,7 @@ public class SimpleWebServiceWithoutVerificationTest { @Test @SneakyThrows public void withSignatureFields() { - byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/placeholder_sigfield_and_qr.pdf")); + byte[] pdf = IOUtils.toByteArray(SimpleVerifyServletTest.class.getResourceAsStream("/data/placeholder_sigfield_and_qr.pdf")); PDFASSignResponse resp = executeTest(pdf); assertNotNull("signed doc", resp.getSignedPDF()); assertEquals("sign check", 0, resp.getVerificationResponse().getValueCode()); @@ -74,7 +78,11 @@ public class SimpleWebServiceWithoutVerificationTest { @SneakyThrows private PDFASSignResponse executeTest(byte[] pdf) { - PDFASSigningImpl service = new PDFASSigningImpl(); + val wsdl = new URL("http://localhost:"+port+"/services/wssign?wsdl"); + val serviceName = new QName( + "http://ws.web.pdfas.egiz.gv.at/", + "PDFASSigningImplService"); + val proxy = Service.create(wsdl, serviceName).getPort(PDFASSigning.class); PDFASSignRequest req = new PDFASSignRequest(); req.setRequestID(UUID.randomUUID().toString()); @@ -84,7 +92,7 @@ public class SimpleWebServiceWithoutVerificationTest { signParams.setTransactionId(UUID.randomUUID().toString()); req.setParameters(signParams ); - PDFASSignResponse resp = service.signPDFDokument(req); + PDFASSignResponse resp = proxy.signPDFDokument(req); assertNotNull(resp); return resp; -- cgit v1.2.3