/************************************************************************* * * * SignServer: The OpenSource Automated Signing Server * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.signserver.client.cli; import java.io.*; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; import org.signserver.cli.spi.CommandFailureException; import org.signserver.cli.spi.IllegalCommandArgumentsException; import org.signserver.client.cli.defaultimpl.SignDocumentCommand; import org.signserver.common.ServiceLocator; import org.signserver.common.SignServerUtil; import org.signserver.ejb.interfaces.IWorkerSession; import org.signserver.testutils.ModulesTestCase; import org.signserver.testutils.TestingSecurityManager; import static org.junit.Assert.*; import org.junit.Test; /** * Tests for the signdocument command of Client CLI. * * @author Markus KilÄs * @version $Id: DocumentSignerTest.java 3465 2013-05-01 10:24:46Z netmackan $ */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class DocumentSignerTest extends ModulesTestCase { /** Logger for this class. */ private static final Logger LOG = Logger.getLogger(DocumentSignerTest.class); /** WORKERID used in this test case as defined in * junittest-part-config.properties for XMLSigner. */ private static final int WORKERID = 5676; /** WORKERID used in this test case as defined in * junittest-part-config.properties for PDFSigner. */ private static final int WORKERID2 = 5675; private static final int[] WORKERS = new int[] {5676, 5679, 5681, 5682, 5683, 5802, 5803}; private static String signserverhome; @Before public void setUp() throws Exception { SignServerUtil.installBCProvider(); workerSession = ServiceLocator.getInstance().lookupRemote( IWorkerSession.IRemote.class); TestingSecurityManager.install(); signserverhome = System.getenv("SIGNSERVER_HOME"); LOG.info("HOME:"+signserverhome); assertNotNull("Please set SIGNSERVER_HOME environment variable", signserverhome); setupSSLKeystores(); } @After public void tearDown() throws Exception { TestingSecurityManager.remove(); } @Test public void test00SetupDatabase() throws Exception { // Worker 1 setProperties(new File(signserverhome, "modules/SignServer-Module-XMLSigner/src/conf/junittest-part-config.properties")); workerSession.reloadConfiguration(WORKERID); // Worker 2 setProperties(new File(signserverhome, "modules/SignServer-Module-PDFSigner/src/conf/junittest-part-config.properties")); workerSession.reloadConfiguration(WORKERID2); } @Test public void test01missingArguments() throws Exception { try { execute("signdocument"); fail("Should have thrown exception about missing arguments"); } catch (IllegalCommandArgumentsException expected) {} // NOPMD } /** * Tests the sample use case a from the documentation. *
     * a) signdocument -workername XMLSigner -data "<root/>"
     * 
* @throws Exception */ @Test public void test02signDocumentFromParameter() throws Exception { try { String res = new String(execute("signdocument", "-workername", "TestXMLSigner", "-data", "")); assertTrue("contains signature tag: " + res, res.contains(" * b) signdocument -workername XMLSigner -infile /tmp/document.xml * * @throws Exception */ @Test public void test02signDocumentFromFile() throws Exception { try { final File doc = File.createTempFile("test.xml", null); FileOutputStream out = null; try { out = new FileOutputStream(doc); out.write("".getBytes()); out.close(); } finally { if (out != null) { out.close(); } } String res = new String(execute("signdocument", "-workername", "TestXMLSigner", "-infile", doc.getAbsolutePath())); assertTrue("contains signature tag: " + res, res.contains("", "-protocol", "WEBSERVICES", "-servlet", "/signserver/SignServerWSService/SignServerWS?wsdl", "-truststore", signserverhome + "/p12/truststore.jks", "-truststorepwd", "changeit")); assertTrue("contains signature tag: " + res, res.contains("", "-protocol", "CLIENTWS", "-servlet", "/signserver/ClientWSService/ClientWS?wsdl", "-truststore", signserverhome + "/p12/truststore.jks", "-truststorepwd", "changeit")); assertTrue("contains signature tag: " + res, res.contains("", "-protocol", "WEBSERVICES", "-servlet", "/signserver/signserverws/signserverws?wsdl", "-truststore", signserverhome + "/p12/truststore.jks", "-truststorepwd", "changeit")); assertTrue("contains signature tag: " + res, res.contains("", "-protocol", "WEBSERVICES", "-servlet", "/signserver/nonexistant/wsurl", "-truststore", signserverhome + "/p12/truststore.jks", "-truststorepwd", "changeit")); fail("Should not accept invalid WS -servlet argument"); } catch (IllegalCommandArgumentsException ex) { LOG.error("Execution failed", ex); fail(ex.getMessage()); } catch (Exception ex) { // this is expected for the invalid URL } } /** * Test signing over webservices with the -servlet argument set as an invalid WS servlet URL * @throws Exception */ @Test public void test07signPDFOverClientWSServletArgInvalid() throws Exception { try { final String res = new String(execute("signdocument", "-workername", "TestXMLSigner", "-data", "", "-protocol", "CLIENTWS", "-servlet", "/signserver/nonexistant/wsurl", "-truststore", signserverhome + "/p12/truststore.jks", "-truststorepwd", "changeit")); fail("Should not accept invalid WS -servlet argument"); } catch (IllegalCommandArgumentsException ex) { LOG.error("Execution failed", ex); fail(ex.getMessage()); } catch (Exception ex) { // this is expected for the invalid URL } } @Test public void test99TearDownDatabase() throws Exception { removeWorker(WORKERID2); for (int workerId : WORKERS) { removeWorker(workerId); } } private byte[] execute(String... args) throws IOException, IllegalCommandArgumentsException, CommandFailureException { byte[] output; final ByteArrayOutputStream out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); try { final SignDocumentCommand cmd = new SignDocumentCommand(); cmd.execute(args); } finally { output = out.toByteArray(); System.setOut(System.out); System.out.write(output); } return output; } }