package com.structurizr.encryption;

import org.junit.Test;

import javax.crypto.BadPaddingException;

import java.security.InvalidKeyException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

public class AesEncryptionStrategyTests {

    private AesEncryptionStrategy strategy;

    @Test
    public void test_encrypt_EncryptsPlaintext() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "06DC30A48ADEEE72D98E33C2CEAEAD3E", "ED124530AF64A5CAD8EF463CF5628434", "password");

        String ciphertext = strategy.encrypt("Hello world");
        assertEquals("A/DzjV17WVS6ZAKsLOaC/Q==", ciphertext);
    }

    @Test
    public void test_decrypt_decryptsTheCiphertext_WhenTheSameStrategyInstanceIsUsed() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "password");

        String ciphertext = strategy.encrypt("Hello world");
        assertEquals("Hello world", strategy.decrypt(ciphertext));
    }

    @Test
    public void test_decrypt_decryptsTheCiphertext_WhenTheSameConfigurationIsUsed() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "password");

        String ciphertext = strategy.encrypt("Hello world");

        strategy = new AesEncryptionStrategy(strategy.getKeySize(), strategy.getIterationCount(), strategy.getSalt(), strategy.getIv(), "password");
        assertEquals("Hello world", strategy.decrypt(ciphertext));
    }

    @Test
    public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectKeySizeIsUsed() throws Exception {
        try {
            strategy = new AesEncryptionStrategy(128, 1000, "password");

            String ciphertext = strategy.encrypt("Hello world");

            strategy = new AesEncryptionStrategy(256, strategy.getIterationCount(), strategy.getSalt(), strategy.getIv(), "password");
            strategy.decrypt(ciphertext);
        } catch (BadPaddingException | InvalidKeyException bpe) {
            // BadPaddingException is thrown on Mac and Linux
            // InvalidKeyException is thrown in Windows
        } catch (Exception e) {
            fail();
        }
    }

    @Test(expected = BadPaddingException.class)
    public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectIterationCountIsUsed() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "password");

        String ciphertext = strategy.encrypt("Hello world");

        strategy = new AesEncryptionStrategy(strategy.getKeySize(), 2000, strategy.getSalt(), strategy.getIv(), "password");
        strategy.decrypt(ciphertext);
    }

    @Test(expected = BadPaddingException.class)
    public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectSaltIsUsed() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "password");

        String ciphertext = strategy.encrypt("Hello world");

        strategy = new AesEncryptionStrategy(strategy.getKeySize(), strategy.getIterationCount(), "133D30C2A658B3081279A97FD3B1F7CDE10C4FB61D39EEA8", strategy.getIv(), "password");
        strategy.decrypt(ciphertext);
    }

    @Test(expected = BadPaddingException.class)
    public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectIvIsUsed() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "password");

        String ciphertext = strategy.encrypt("Hello world");

        strategy = new AesEncryptionStrategy(strategy.getKeySize(), strategy.getIterationCount(), strategy.getSalt(), "1DED89E4FB15F61DC6433E3BADA4A891", "password");
        strategy.decrypt(ciphertext);
    }

    @Test(expected = BadPaddingException.class)
    public void test_decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectPassphraseIsUsed() throws Exception {
        strategy = new AesEncryptionStrategy(128, 1000, "password");

        String ciphertext = strategy.encrypt("Hello world");

        strategy = new AesEncryptionStrategy(strategy.getKeySize(), strategy.getIterationCount(), strategy.getSalt(), strategy.getIv(), "The Wrong Password");
        strategy.decrypt(ciphertext);
    }

}

@@ -91,25 +91,24 @@
 gException;%0A
-%0A
 import java.
@@ -187,56 +187,8 @@
 ls;%0A
-import static org.junit.Assert.assertNotEquals;%0A
 impo
@@ -268,53 +268,8 @@
  %7B%0A%0A
-    private AesEncryptionStrategy strategy;%0A%0A
     
@@ -342,32 +342,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A
@@ -752,32 +752,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A
@@ -1092,32 +1092,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A
@@ -1605,32 +1605,54 @@
 y %7B%0A            
+AesEncryptionStrategy 
 strategy = new A
@@ -2356,32 +2356,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A
@@ -2833,32 +2833,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A
@@ -3364,32 +3364,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A
@@ -3889,24 +3889,24 @@
 ption %7B%0A
-
         
 strategy
@@ -3889,32 +3889,54 @@
 ption %7B%0A        
+AesEncryptionStrategy 
 strategy = new A

Switch to method local variable.