-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathJceMasterKeyTest.java
71 lines (59 loc) · 2.21 KB
/
JceMasterKeyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.amazonaws.encryptionsdk.jce;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.junit.Test;
public class JceMasterKeyTest {
private static final SecretKey SECRET_KEY = new SecretKeySpec(new byte[1], "AES");
private static final PrivateKey PRIVATE_KEY;
private static final PublicKey PUBLIC_KEY;
static {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PUBLIC_KEY = keyPair.getPublic();
PRIVATE_KEY = keyPair.getPrivate();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private JceMasterKey jceGetInstance(final String algorithmName) {
return JceMasterKey.getInstance(SECRET_KEY, "mockProvider", "mockKey", algorithmName);
}
private JceMasterKey jceGetInstanceAsymmetric(final String algorithmName) {
return JceMasterKey.getInstance(
PUBLIC_KEY, PRIVATE_KEY, "mockProvider", "mockKey", algorithmName);
}
@Test(expected = IllegalArgumentException.class)
public void testGetInstanceInvalidWrappingAlgorithm() {
jceGetInstance("blatently/unsupported/algorithm");
}
@Test(expected = UnsupportedOperationException.class)
public void testGetInstanceAsymmetricInvalidWrappingAlgorithm() {
jceGetInstanceAsymmetric("rsa/ec/unsupportedAlgorithm");
}
/**
* Calls JceMasterKey.getInstance with differently cased wrappingAlgorithm names. Passes if no
* Exception is thrown. Relies on passing an invalid algorithm name to result in an Exception.
*/
@Test
public void testGetInstanceAllLowercase() {
jceGetInstance("aes/gcm/nopadding");
}
@Test
public void testGetInstanceMixedCasing() {
jceGetInstance("AES/GCm/NOpadding");
}
@Test
public void testGetInstanceAsymmetricAllLowercase() {
jceGetInstanceAsymmetric("rsa/ecb/oaepwithsha-256andmgf1padding");
}
@Test
public void testGetInstanceAsymmetricMixedCasing() {
jceGetInstanceAsymmetric("RSA/ECB/OAepwithsha-256andmgf1padding");
}
}