22
33import org .junit .jupiter .api .Test ;
44
5- import javax .crypto .BadPaddingException ;
6- import java .security .InvalidKeyException ;
7-
8- import static org .junit .jupiter .api .Assertions .*;
5+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
97
108public class AesEncryptionStrategyTests {
119
1210 @ Test
1311 void encrypt_EncryptsPlaintext () throws Exception {
1412 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "06DC30A48ADEEE72D98E33C2CEAEAD3E" , "ED124530AF64A5CAD8EF463CF5628434" , "password" );
15-
1613 String ciphertext = strategy .encrypt ("Hello world" );
14+
1715 assertEquals ("A/DzjV17WVS6ZAKsLOaC/Q==" , ciphertext );
1816 }
1917
2018 @ Test
2119 void decrypt_decryptsTheCiphertext_WhenTheSameStrategyInstanceIsUsed () throws Exception {
2220 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
23-
2421 String ciphertext = strategy .encrypt ("Hello world" );
22+
2523 assertEquals ("Hello world" , strategy .decrypt (ciphertext ));
2624 }
2725
2826 @ Test
2927 void decrypt_decryptsTheCiphertext_WhenTheSameConfigurationIsUsed () throws Exception {
3028 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
31-
3229 String ciphertext = strategy .encrypt ("Hello world" );
3330
3431 strategy = new AesEncryptionStrategy (strategy .getKeySize (), strategy .getIterationCount (), strategy .getSalt (), strategy .getIv (), "password" );
@@ -39,65 +36,65 @@ void decrypt_decryptsTheCiphertext_WhenTheSameConfigurationIsUsed() throws Excep
3936 void decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectKeySizeIsUsed () throws Exception {
4037 try {
4138 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
42-
4339 String ciphertext = strategy .encrypt ("Hello world" );
4440
4541 strategy = new AesEncryptionStrategy (256 , strategy .getIterationCount (), strategy .getSalt (), strategy .getIv (), "password" );
46- strategy .decrypt (ciphertext );
47- } catch (BadPaddingException | InvalidKeyException bpe ) {
48- // BadPaddingException is thrown on Mac and Linux
49- // InvalidKeyException is thrown in Windows
42+ assertNotEquals ("Hello world" , strategy .decrypt (ciphertext ));
5043 } catch (Exception e ) {
51- fail ();
44+ // this is okay
5245 }
5346 }
5447
5548 @ Test
5649 void decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectIterationCountIsUsed () throws Exception {
57- assertThrows ( BadPaddingException . class , () -> {
50+ try {
5851 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
59-
6052 String ciphertext = strategy .encrypt ("Hello world" );
6153
6254 strategy = new AesEncryptionStrategy (strategy .getKeySize (), 2000 , strategy .getSalt (), strategy .getIv (), "password" );
63- strategy .decrypt (ciphertext );
64- });
55+ assertNotEquals ("Hello world" , strategy .decrypt (ciphertext ));
56+ } catch (Exception e ) {
57+ // this is okay
58+ }
6559 }
6660
6761 @ Test
6862 void decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectSaltIsUsed () throws Exception {
69- assertThrows ( BadPaddingException . class , () -> {
63+ try {
7064 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
71-
7265 String ciphertext = strategy .encrypt ("Hello world" );
7366
7467 strategy = new AesEncryptionStrategy (strategy .getKeySize (), strategy .getIterationCount (), "133D30C2A658B3081279A97FD3B1F7CDE10C4FB61D39EEA8" , strategy .getIv (), "password" );
75- strategy .decrypt (ciphertext );
76- });
68+ assertNotEquals ("Hello world" , strategy .decrypt (ciphertext ));
69+ } catch (Exception e ) {
70+ // this is okay
71+ }
7772 }
7873
7974 @ Test
8075 void decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectIvIsUsed () throws Exception {
81- assertThrows ( BadPaddingException . class , () -> {
76+ try {
8277 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
83-
8478 String ciphertext = strategy .encrypt ("Hello world" );
8579
8680 strategy = new AesEncryptionStrategy (strategy .getKeySize (), strategy .getIterationCount (), strategy .getSalt (), "1DED89E4FB15F61DC6433E3BADA4A891" , "password" );
87- strategy .decrypt (ciphertext );
88- });
81+ assertNotEquals ("Hello world" , strategy .decrypt (ciphertext ));
82+ } catch (Exception e ) {
83+ // this is okay
84+ }
8985 }
9086
9187 @ Test
9288 void decrypt_doesNotDecryptTheCiphertext_WhenTheIncorrectPassphraseIsUsed () throws Exception {
93- assertThrows ( BadPaddingException . class , () -> {
89+ try {
9490 AesEncryptionStrategy strategy = new AesEncryptionStrategy (128 , 1000 , "password" );
95-
9691 String ciphertext = strategy .encrypt ("Hello world" );
9792
9893 strategy = new AesEncryptionStrategy (strategy .getKeySize (), strategy .getIterationCount (), strategy .getSalt (), strategy .getIv (), "The Wrong Password" );
99- strategy .decrypt (ciphertext );
100- });
94+ assertNotEquals ("Hello world" , strategy .decrypt (ciphertext ));
95+ } catch (Exception e ) {
96+ // this is okay
97+ }
10198 }
10299
103- }
100+ }
0 commit comments