Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.io.IOUtils;
- import org.junit.Assert;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.TemporaryFolder;
- import javax.crypto.BadPaddingException;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.sql.SQLException;
- import java.util.Optional;
- public class CipherTest {
- private static final String FILE_PATH_WITH_SOME_TEXT = "D:\\BSIszyfrowanie\\test\\main1\\inputText.txt";
- private Facade facade = Configuration.get(new InMemoryRepository());
- @Rule
- public TemporaryFolder folder = new TemporaryFolder();
- @Test
- public void shouldDecryptEncryptedFile() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, IOException, NoSuchPaddingException, NoSuchAlgorithmException {
- //given
- FileInputStream fileInputStream = new FileInputStream(FILE_PATH_WITH_SOME_TEXT);
- File outputEncrypted = folder.newFile("outputEncrypted.cfr");
- File outputDecrypted = folder.newFile("outputDecrypted.txt");
- String outputEncryptedFilePath = outputEncrypted.getPath();
- //and
- facade.encryptFileToFile(FILE_PATH_WITH_SOME_TEXT, outputEncryptedFilePath);
- //when to co testuje
- facade.decryptFileToFile(outputEncryptedFilePath, outputDecrypted.getPath());
- //then
- Assert.assertArrayEquals(toByteArray(outputDecrypted), toByteArray(fileInputStream));
- }
- @Test
- public void shouldEncryptFileAndPersistToDatabase() throws IOException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, SQLException, NoSuchPaddingException, InvalidKeyException {
- //when
- facade.encryptToDatabase(FILE_PATH_WITH_SOME_TEXT);
- //then
- Optional<EncryptedDbModel> persisted = facade.findAll().stream().findFirst();
- Assert.assertTrue(persisted.isPresent());
- }
- @Test
- public void shouldDecryptPersistedEncryptedFile() throws IllegalBlockSizeException, NoSuchAlgorithmException, IOException, SQLException, BadPaddingException, NoSuchPaddingException, InvalidKeyException {
- //given
- FileInputStream fileInputStream = new FileInputStream(FILE_PATH_WITH_SOME_TEXT);
- File outputDecrypted = folder.newFile("outputDecrypted.txt");
- //and
- facade.encryptToDatabase(FILE_PATH_WITH_SOME_TEXT);
- EncryptedDbModel persisted = facade.findAll().stream().findFirst().get();
- //when
- facade.decryptFromDatabase(persisted.getId(), outputDecrypted.getPath());
- //then
- Assert.assertArrayEquals(toByteArray(outputDecrypted), toByteArray(fileInputStream));
- }
- private byte[] toByteArray(FileInputStream fileInputStream) throws IOException {
- return IOUtils.toByteArray(fileInputStream);
- }
- private byte[] toByteArray(File file) throws IOException {
- return Files.readAllBytes(file.toPath());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement