eu.mrbussy.security.crypto.pgp.PGPDecryptorTest.java Source code

Java tutorial

Introduction

Here is the source code for eu.mrbussy.security.crypto.pgp.PGPDecryptorTest.java

Source

/*
 * Java-Password-Store
 * Copyright (C) 2016  rudi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.mrbussy.security.crypto.pgp;

import org.apache.commons.io.IOUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.security.Security;

import static org.junit.Assert.*;

/**
 * Created by rudi on 4/20/16.
 */
public class PGPDecryptorTest {
    private String privateKey;
    private String privateMessagePath;
    private InputStream privateMessageStream;

    private final String password = "testkey";

    @Before
    public void setUp() throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        privateKey = PGPDecryptor.class.getResource("/test.priv.gpg").getFile();
        privateMessagePath = PGPDecryptor.class.getResource("/message.txt.private").getFile();
        privateMessageStream = PGPDecryptor.class.getResourceAsStream("/message.txt.private");
    }

    @After
    public void tearDown() throws Exception {
        privateMessageStream.close();
    }

    @Test
    public void set_getProperties() throws Exception {
        PGPDecryptor decryptor = new PGPDecryptor();

        decryptor.setPassword("Example");
        assertEquals("Example", decryptor.getPassword());

        decryptor.setPrivateKeyFilePath("/tmp/keyfile");
        assertEquals("/tmp/keyfile", decryptor.getPrivateKeyFilePath());

        decryptor.setSigned(true);
        assertTrue(decryptor.isSigned());

        decryptor.setSigningPublicKeyFilePath("/tmp/signkeyfile");
        assertEquals("/tmp/signkeyfile", decryptor.getSigningPublicKeyFilePath());

        decryptor = null;
    }

    @Test
    public void decryptFileInputStreamOutputStream() throws Exception {
        PGPDecryptor decryptor = new PGPDecryptor();
        decryptor.setPrivateKeyFilePath(privateKey);
        decryptor.setPassword(password);
        decryptor.setSigned(false);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        decryptor.decryptFile(privateMessageStream, out);
        assertEquals("This is plain text message. This is not secure.", out.toString());
        out.close();
    }

    @Test
    public void decryptFileInputStreamReturn() throws Exception {
        PGPDecryptor decryptor = new PGPDecryptor();
        decryptor.setPrivateKeyFilePath(privateKey);
        decryptor.setPassword(password);
        decryptor.setSigned(false);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(decryptor.decryptFile(privateMessageStream), out);
        assertEquals("This is plain text message. This is not secure.", out.toString());
        out.close();
    }

    @Test
    public void decryptFileInputFileOutputFile() throws Exception {
        PGPDecryptor decryptor = new PGPDecryptor();
        decryptor.setPrivateKeyFilePath(privateKey);
        decryptor.setPassword(password);
        decryptor.setSigned(false);

        File input = new File(privateMessagePath);
        File output = File.createTempFile("pgptest", null);
        decryptor.decryptFile(input, output);
        String everything;
        try (FileInputStream inputStream = new FileInputStream(output)) {
            everything = IOUtils.toString(inputStream);
        }
        assertEquals("This is plain text message. This is not secure.", everything);

        output.delete();
    }

    @Test
    public void decryptFileInputFileNameOutputFileName() throws Exception {
        PGPDecryptor decryptor = new PGPDecryptor();
        decryptor.setPrivateKeyFilePath(privateKey);
        decryptor.setPassword(password);
        decryptor.setSigned(false);

        File output = File.createTempFile("pgptest", null);
        decryptor.decryptFile(privateMessagePath, output.getPath());
        String everything;
        try (FileInputStream inputStream = new FileInputStream(output)) {
            everything = IOUtils.toString(inputStream);
        }
        assertEquals("This is plain text message. This is not secure.", everything);

        output.delete();
    }

}