Search in sources :

Example 1 with MockHttpServer

use of org.apache.wink.client.MockHttpServer in project project-build-plugin by axonivy.

the class TestInstallEngineMojo method testEngineDownload_defaultBehaviour.

@Test
public void testEngineDownload_defaultBehaviour() throws Exception {
    MockHttpServer mockServer = new MockHttpServer(3333);
    try {
        mockServer.startServer();
        String baseUrl = "http://localhost:" + mockServer.getServerPort();
        MockHttpServer.MockHttpServerResponse listPageResponse = new MockHttpServer.MockHttpServerResponse();
        String defaultEngineName = "AxonIvyEngine" + AbstractEngineMojo.DEFAULT_VERSION + ".46949_All_x64";
        listPageResponse.setMockResponseContent("<a href=\"" + baseUrl + "/" + defaultEngineName + ".zip\">the engine!</a>");
        File engineZip = createFakeEngineZip(mojo.ivyVersion);
        MockHttpServer.MockHttpServerResponse engineZipResponse = createFakeZipResponse(engineZip);
        mockServer.setMockHttpServerResponses(listPageResponse, engineZipResponse);
        // test setup can not expand expression ${settings.localRepository}: so we setup an explicit temp dir!
        mojo.engineCacheDirectory = Files.createTempDirectory("tmpRepo").toFile();
        mojo.engineListPageUrl = new URL(baseUrl + "/listPageUrl.html");
        File defaultEngineDir = new File(mojo.engineCacheDirectory, AbstractEngineMojo.DEFAULT_VERSION);
        assertThat(defaultEngineDir).doesNotExist();
        assertThat(mojo.engineDownloadUrl).as("Default config should favour to download an engine from the 'list page url'.").isNull();
        assertThat(mojo.autoInstallEngine).isTrue();
        mojo.execute();
        assertThat(defaultEngineDir).as("Engine must be automatically downloaded").exists().isDirectory();
        assertThat(defaultEngineDir).as("Engine directory should automatically be set to subdir of the local repository cache.").isEqualTo(mojo.getRawEngineDirectory());
    } finally {
        mockServer.stopServer();
    }
}
Also used : MockHttpServer(org.apache.wink.client.MockHttpServer) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 2 with MockHttpServer

use of org.apache.wink.client.MockHttpServer in project pdfbox by apache.

the class TestCreateSignature method testDetachedSHA256WithTSA.

/**
 * Signs a PDF using the "adbe.pkcs7.detached" SubFilter with the SHA-256 digest and a signed
 * timestamp from a Time Stamping Authority (TSA) server.
 *
 * This is not a complete test because we don't have the ability to return a valid response, so
 * we return a cached response which is well-formed, but does not match the timestamp or nonce
 * in the request. This allows us to test the basic TSA mechanism and test the nonce, which is a
 * good start.
 *
 * @throws IOException
 * @throws GeneralSecurityException
 * @throws CMSException
 * @throws OperatorCreationException
 */
@Test
public void testDetachedSHA256WithTSA() throws IOException, CMSException, OperatorCreationException, GeneralSecurityException {
    byte[] content;
    // mock TSA response content
    try (InputStream input = new FileInputStream(inDir + "tsa_response.asn1")) {
        content = IOUtils.toByteArray(input);
    }
    // mock TSA server (RFC 3161)
    MockHttpServer mockServer = new MockHttpServer(15371);
    mockServer.startServer();
    String tsaUrl = "http://localhost:" + mockServer.getServerPort() + "/";
    MockHttpServer.MockHttpServerResponse response = new MockHttpServer.MockHttpServerResponse();
    response.setMockResponseContent(content);
    response.setMockResponseContentType("application/timestamp-reply");
    response.setMockResponseCode(200);
    mockServer.setMockHttpServerResponses(response);
    // load the keystore
    KeyStore keystore = KeyStore.getInstance("PKCS12");
    keystore.load(new FileInputStream(keystorePath), password.toCharArray());
    // sign PDF (will fail due to nonce and timestamp differing)
    try {
        String inPath = inDir + "sign_me_tsa.pdf";
        String outPath = outDir + getOutputFileName("signed{0}_tsa.pdf");
        CreateSignature signing = new CreateSignature(keystore, password.toCharArray());
        signing.setExternalSigning(externallySign);
        signing.signDetached(new File(inPath), new File(outPath), tsaUrl);
    } catch (IOException e) {
        Assert.assertTrue(e.getCause() instanceof TSPValidationException);
    }
// TODO verify the signed PDF file
// TODO create a file signed with TSA
}
Also used : CreateSignature(org.apache.pdfbox.examples.signature.CreateSignature) TSPValidationException(org.bouncycastle.tsp.TSPValidationException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MockHttpServer(org.apache.wink.client.MockHttpServer) COSString(org.apache.pdfbox.cos.COSString) IOException(java.io.IOException) KeyStore(java.security.KeyStore) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

File (java.io.File)2 MockHttpServer (org.apache.wink.client.MockHttpServer)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 KeyStore (java.security.KeyStore)1 ZipFile (net.lingala.zip4j.core.ZipFile)1 COSString (org.apache.pdfbox.cos.COSString)1 CreateSignature (org.apache.pdfbox.examples.signature.CreateSignature)1 TSPValidationException (org.bouncycastle.tsp.TSPValidationException)1