Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. import org.apache.commons.io.IOUtils;
  2. import org.junit.Assert;
  3. import org.junit.Rule;
  4. import org.junit.Test;
  5. import org.junit.rules.TemporaryFolder;
  6.  
  7. import javax.crypto.BadPaddingException;
  8. import javax.crypto.IllegalBlockSizeException;
  9. import javax.crypto.NoSuchPaddingException;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.nio.file.Files;
  14. import java.security.InvalidKeyException;
  15. import java.security.NoSuchAlgorithmException;
  16. import java.sql.SQLException;
  17. import java.util.Optional;
  18.  
  19. public class CipherTest {
  20.  
  21. private static final String FILE_PATH_WITH_SOME_TEXT = "D:\\BSIszyfrowanie\\test\\main1\\inputText.txt";
  22.  
  23. private Facade facade = Configuration.get(new InMemoryRepository());
  24.  
  25. @Rule
  26. public TemporaryFolder folder = new TemporaryFolder();
  27.  
  28.  
  29. @Test
  30. public void shouldDecryptEncryptedFile() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, IOException, NoSuchPaddingException, NoSuchAlgorithmException {
  31. //given
  32. FileInputStream fileInputStream = new FileInputStream(FILE_PATH_WITH_SOME_TEXT);
  33. File outputEncrypted = folder.newFile("outputEncrypted.cfr");
  34. File outputDecrypted = folder.newFile("outputDecrypted.txt");
  35. String outputEncryptedFilePath = outputEncrypted.getPath();
  36.  
  37. //and
  38. facade.encryptFileToFile(FILE_PATH_WITH_SOME_TEXT, outputEncryptedFilePath);
  39.  
  40. //when to co testuje
  41. facade.decryptFileToFile(outputEncryptedFilePath, outputDecrypted.getPath());
  42.  
  43. //then
  44. Assert.assertArrayEquals(toByteArray(outputDecrypted), toByteArray(fileInputStream));
  45. }
  46.  
  47. @Test
  48. public void shouldEncryptFileAndPersistToDatabase() throws IOException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, SQLException, NoSuchPaddingException, InvalidKeyException {
  49. //when
  50. facade.encryptToDatabase(FILE_PATH_WITH_SOME_TEXT);
  51.  
  52. //then
  53. Optional<EncryptedDbModel> persisted = facade.findAll().stream().findFirst();
  54. Assert.assertTrue(persisted.isPresent());
  55. }
  56.  
  57. @Test
  58. public void shouldDecryptPersistedEncryptedFile() throws IllegalBlockSizeException, NoSuchAlgorithmException, IOException, SQLException, BadPaddingException, NoSuchPaddingException, InvalidKeyException {
  59. //given
  60. FileInputStream fileInputStream = new FileInputStream(FILE_PATH_WITH_SOME_TEXT);
  61. File outputDecrypted = folder.newFile("outputDecrypted.txt");
  62.  
  63. //and
  64. facade.encryptToDatabase(FILE_PATH_WITH_SOME_TEXT);
  65. EncryptedDbModel persisted = facade.findAll().stream().findFirst().get();
  66.  
  67. //when
  68. facade.decryptFromDatabase(persisted.getId(), outputDecrypted.getPath());
  69.  
  70. //then
  71. Assert.assertArrayEquals(toByteArray(outputDecrypted), toByteArray(fileInputStream));
  72. }
  73.  
  74. private byte[] toByteArray(FileInputStream fileInputStream) throws IOException {
  75. return IOUtils.toByteArray(fileInputStream);
  76. }
  77.  
  78. private byte[] toByteArray(File file) throws IOException {
  79. return Files.readAllBytes(file.toPath());
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement