Skip to content

Commit ba4fd46

Browse files
Fixes intermittently failing tests.
1 parent 2b8191a commit ba4fd46

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

structurizr-client/src/main/java/com/structurizr/encryption/AesEncryptionStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.crypto.spec.PBEKeySpec;
88
import javax.crypto.spec.SecretKeySpec;
99
import javax.xml.bind.DatatypeConverter;
10+
import java.nio.charset.StandardCharsets;
1011
import java.security.NoSuchAlgorithmException;
1112
import java.security.SecureRandom;
1213
import java.security.spec.InvalidKeySpecException;
@@ -79,7 +80,7 @@ public String decrypt(String ciphertext) throws Exception {
7980
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(DatatypeConverter.parseHexBinary(iv)));
8081
byte[] unencrypted = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
8182

82-
return new String(unencrypted, "UTF-8");
83+
return new String(unencrypted, StandardCharsets.UTF_8);
8384
}
8485

8586
private SecretKey createSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException {

structurizr-client/src/test/java/com/structurizr/encryption/AesEncryptionStrategyTests.java

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@
22

33
import 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

108
public 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

Comments
 (0)