1+ package de .dominikschadow .javasecurity ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .io .IOException ;
7+ import java .security .*;
8+
9+ import static org .junit .jupiter .api .Assertions .*;
10+
11+ class KeystoreTest {
12+ private final char [] keystorePassword = "samples" .toCharArray ();
13+
14+ @ Test
15+ void givenValidPasswordWhenLoadingKeyStoreThenReturnKeystore () throws Exception {
16+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
17+
18+ assertNotNull (ks );
19+ }
20+
21+ @ Test
22+ void givenInvalidPasswordWhenLoadingKeyStoreThenThrowException () {
23+ Exception exception = assertThrows (IOException .class , () -> Keystore .loadKeystore ("wrongPassword" .toCharArray ()));
24+
25+ assertEquals ("Keystore was tampered with, or password was incorrect" , exception .getMessage ());
26+ }
27+
28+ @ Test
29+ void givenValidAliasAndPasswordWhenLoadingPrivateKeyThenReturnKey () throws Exception {
30+ final String keyAlias = "asymmetric-sample-rsa" ;
31+ final char [] keyPassword = "asymmetric-sample-rsa" .toCharArray ();
32+
33+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
34+ PrivateKey privateKey = Keystore .loadPrivateKey (ks , keyAlias , keyPassword );
35+
36+ Assertions .assertAll (
37+ () -> assertNotNull (privateKey ),
38+ () -> assertEquals ("RSA" , privateKey .getAlgorithm ())
39+ );
40+ }
41+
42+ @ Test
43+ void givenUnknownAliasWhenLoadingPrivateKeyThenThrowException () throws Exception {
44+ final String keyAlias = "unknown" ;
45+ final char [] keyPassword = "asymmetric-sample-rsa" .toCharArray ();
46+
47+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
48+ Exception exception = assertThrows (UnrecoverableKeyException .class , () -> Keystore .loadPrivateKey (ks , keyAlias , keyPassword ));
49+
50+ assertEquals ("Private key unknown not found in keystore" , exception .getMessage ());
51+ }
52+
53+ @ Test
54+ void givenValidAliasWhenLoadingPublicKeyThenReturnKey () throws Exception {
55+ final String keyAlias = "asymmetric-sample-rsa" ;
56+
57+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
58+ PublicKey publicKey = Keystore .loadPublicKey (ks , keyAlias );
59+
60+ Assertions .assertAll (
61+ () -> assertNotNull (publicKey ),
62+ () -> assertEquals ("RSA" , publicKey .getAlgorithm ())
63+ );
64+ }
65+
66+ @ Test
67+ void givenUnknownAliasWhenLoadingPublicKeyThenThrowException () throws Exception {
68+ final String keyAlias = "unknown" ;
69+
70+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
71+ Exception exception = assertThrows (UnrecoverableKeyException .class , () -> Keystore .loadPublicKey (ks , keyAlias ));
72+
73+ assertEquals ("Public key unknown not found in keystore" , exception .getMessage ());
74+ }
75+
76+ @ Test
77+ void givenValidAliasAndPasswordWhenLoadingKeyThenReturnKey () throws Exception {
78+ final String keyAlias = "symmetric-sample" ;
79+ final char [] keyPassword = "symmetric-sample" .toCharArray ();
80+
81+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
82+ Key key = Keystore .loadKey (ks , keyAlias , keyPassword );
83+
84+ Assertions .assertAll (
85+ () -> assertNotNull (key ),
86+ () -> assertEquals ("AES" , key .getAlgorithm ())
87+ );
88+ }
89+
90+ @ Test
91+ void givenUnknownAliasWhenLoadingKeyThenThrowException () throws Exception {
92+ final String keyAlias = "unknown" ;
93+ final char [] keyPassword = "symmetric-sample" .toCharArray ();
94+
95+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
96+ Exception exception = assertThrows (UnrecoverableKeyException .class , () -> Keystore .loadKey (ks , keyAlias , keyPassword ));
97+
98+ assertEquals ("Secret key unknown not found in keystore" , exception .getMessage ());
99+ }
100+
101+ @ Test
102+ void givenValidAliasAndInvalidPasswordWhenLoadingKeyThenThrowException () throws Exception {
103+ final String keyAlias = "symmetric-sample" ;
104+ final char [] keyPassword = "wrongPassword" .toCharArray ();
105+
106+ KeyStore ks = Keystore .loadKeystore (keystorePassword );
107+ Exception exception = assertThrows (UnrecoverableKeyException .class , () -> Keystore .loadKey (ks , keyAlias , keyPassword ));
108+
109+ assertEquals ("Given final block not properly padded. Such issues can arise if a bad key is used during decryption." , exception .getMessage ());
110+ }
111+
112+ @ Test
113+ void createSecretKeySpec () {
114+ }
115+ }
0 commit comments