Java tutorial
/* * HSM Proxy Project. * Copyright (C) 2013 FedICT. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version * 3.0 as published by the Free Software Foundation. * * This software 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. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, see * http://www.gnu.org/licenses/. */ package test.integ.be.fedict.hsm.ws; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.net.URL; import javax.xml.ws.BindingProvider; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; import org.junit.runner.RunWith; import be.fedict.hsm.client.HSMProxyClient; import be.fedict.hsm.model.SignatureService; import be.fedict.hsm.ws.DigitalSignatureServiceFactory; import be.fedict.hsm.ws.ResultMajor; import be.fedict.hsm.ws.impl.DigitalSignatureServicePortImpl; import be.fedict.hsm.ws.impl.LoggingSOAPHandler; import be.fedict.hsm.ws.jaxb.dss.AnyType; import be.fedict.hsm.ws.jaxb.dss.InputDocuments; import be.fedict.hsm.ws.jaxb.dss.KeySelector; import be.fedict.hsm.ws.jaxb.dss.ObjectFactory; import be.fedict.hsm.ws.jaxb.dss.ResponseBaseType; import be.fedict.hsm.ws.jaxb.dss.Result; import be.fedict.hsm.ws.jaxb.dss.SignRequest; import be.fedict.hsm.ws.jaxb.dss.SignResponse; import be.fedict.hsm.ws.jaxb.hsm.GetCertificateChainRequest; import be.fedict.hsm.ws.jaxb.xmldsig.KeyInfoType; import be.fedict.hsm.ws.jaxws.DigitalSignatureService; import be.fedict.hsm.ws.jaxws.DigitalSignatureServicePortType; @RunWith(Arquillian.class) @RunAsClient public class WebServiceTest { private static final Log LOG = LogFactory.getLog(WebServiceTest.class); @ArquillianResource private URL baseURL; @Deployment public static WebArchive createTestArchive() { WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war") .addClasses(DigitalSignatureServicePortImpl.class, LoggingSOAPHandler.class, ResultMajor.class) .addClasses(SignatureService.class, SignatureServiceTestBean.class) .addPackage(DigitalSignatureServicePortType.class.getPackage()) .addPackages(true, ObjectFactory.class.getPackage(), be.fedict.hsm.ws.jaxb.xmldsig.ObjectFactory.class.getPackage(), be.fedict.hsm.ws.jaxb.hsm.ObjectFactory.class.getPackage(), be.fedict.hsm.ws.jaxb.saml.ObjectFactory.class.getPackage()) .addAsResource(WebServiceTest.class.getResource("/test-hsm-proxy-ws-handlers.xml"), "/hsm-proxy-ws-handlers.xml") .addAsWebInfResource(WebServiceTest.class.getResource("/test-web.xml"), "/web.xml"); return war; } @Test public void testDeployment() throws Exception { // empty } private DigitalSignatureServicePortType getPort() { DigitalSignatureService digitalSignatureService = DigitalSignatureServiceFactory.getInstance(); DigitalSignatureServicePortType dssPort = digitalSignatureService.getDigitalSignatureServicePort(); BindingProvider bindingProvider = (BindingProvider) dssPort; String location = this.baseURL.toString() + "dss"; LOG.debug("location: " + location); bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, location); return dssPort; } @Test public void testSignEmptySignRequest() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); ObjectFactory objectFactory = new ObjectFactory(); SignRequest signRequest = objectFactory.createSignRequest(); SignResponse signResponse = dssPort.sign(signRequest); verifyResult(ResultMajor.REQUESTER_ERROR, signResponse); } private void verifyResult(ResultMajor resultMajor, ResponseBaseType response) { Result result = response.getResult(); assertEquals(resultMajor.getUri(), result.getResultMajor()); } @Test public void testSignMissingOptionalInputs() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); ObjectFactory objectFactory = new ObjectFactory(); SignRequest signRequest = objectFactory.createSignRequest(); InputDocuments inputDocuments = objectFactory.createInputDocuments(); signRequest.setInputDocuments(inputDocuments); SignResponse signResponse = dssPort.sign(signRequest); verifyResult(ResultMajor.REQUESTER_ERROR, signResponse); } @Test public void testSignEmptyOptionalInputsEmptyInputDocuments() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); ObjectFactory objectFactory = new ObjectFactory(); SignRequest signRequest = objectFactory.createSignRequest(); InputDocuments inputDocuments = objectFactory.createInputDocuments(); signRequest.setInputDocuments(inputDocuments); AnyType optionalInputs = objectFactory.createAnyType(); signRequest.setOptionalInputs(optionalInputs); SignResponse signResponse = dssPort.sign(signRequest); verifyResult(ResultMajor.REQUESTER_ERROR, signResponse); } @Test public void testSigning() throws Exception { HSMProxyClient hsmProxyClient = new HSMProxyClient(this.baseURL.toString() + "dss", null, null); byte[] signatureValue = hsmProxyClient.sign("digest value".getBytes(), "SHA-1", "key alias"); assertNotNull(signatureValue); } @Test public void testGetCertificateChainMissingOptionalInputs() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); be.fedict.hsm.ws.jaxb.hsm.ObjectFactory objectFactory = new be.fedict.hsm.ws.jaxb.hsm.ObjectFactory(); GetCertificateChainRequest request = objectFactory.createGetCertificateChainRequest(); ResponseBaseType response = dssPort.getCertificateChain(request); verifyResult(ResultMajor.REQUESTER_ERROR, response); } @Test public void testGetCertificateChainMissingKeySelector() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); be.fedict.hsm.ws.jaxb.hsm.ObjectFactory objectFactory = new be.fedict.hsm.ws.jaxb.hsm.ObjectFactory(); be.fedict.hsm.ws.jaxb.dss.ObjectFactory dssObjectFactory = new ObjectFactory(); GetCertificateChainRequest request = objectFactory.createGetCertificateChainRequest(); AnyType optionalInputs = dssObjectFactory.createAnyType(); request.setOptionalInputs(optionalInputs); ResponseBaseType response = dssPort.getCertificateChain(request); verifyResult(ResultMajor.REQUESTER_ERROR, response); } @Test public void testGetCertificateChainMissingKeyInfo() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); be.fedict.hsm.ws.jaxb.hsm.ObjectFactory objectFactory = new be.fedict.hsm.ws.jaxb.hsm.ObjectFactory(); be.fedict.hsm.ws.jaxb.dss.ObjectFactory dssObjectFactory = new ObjectFactory(); GetCertificateChainRequest request = objectFactory.createGetCertificateChainRequest(); AnyType optionalInputs = dssObjectFactory.createAnyType(); request.setOptionalInputs(optionalInputs); KeySelector keySelector = dssObjectFactory.createKeySelector(); optionalInputs.getAny().add(keySelector); ResponseBaseType response = dssPort.getCertificateChain(request); verifyResult(ResultMajor.REQUESTER_ERROR, response); } @Test public void testGetCertificateChainMissingKeyName() throws Exception { DigitalSignatureServicePortType dssPort = getPort(); be.fedict.hsm.ws.jaxb.hsm.ObjectFactory objectFactory = new be.fedict.hsm.ws.jaxb.hsm.ObjectFactory(); be.fedict.hsm.ws.jaxb.dss.ObjectFactory dssObjectFactory = new ObjectFactory(); be.fedict.hsm.ws.jaxb.xmldsig.ObjectFactory dsObjectFactory = new be.fedict.hsm.ws.jaxb.xmldsig.ObjectFactory(); GetCertificateChainRequest request = objectFactory.createGetCertificateChainRequest(); AnyType optionalInputs = dssObjectFactory.createAnyType(); request.setOptionalInputs(optionalInputs); KeySelector keySelector = dssObjectFactory.createKeySelector(); optionalInputs.getAny().add(keySelector); KeyInfoType keyInfo = dsObjectFactory.createKeyInfoType(); keySelector.setKeyInfo(keyInfo); ResponseBaseType response = dssPort.getCertificateChain(request); verifyResult(ResultMajor.REQUESTER_ERROR, response); } }