BAEL-6014: Check certificate name and alias in keystore file
This commit is contained in:
parent
c10a404f57
commit
ec1fde9dc1
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.keystorealias;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KeystoreCertificateNameAliasUnitTest {
|
||||
private static final String KEYSTORE_FILE = "my-keystore.jks";
|
||||
private static final String KEYSTORE_PWD = "storepw@1";
|
||||
private static final String KEYSTORE_ALIAS = "baeldung";
|
||||
|
||||
private KeyStore readKeyStore() throws Exception {
|
||||
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||
keystore.load(getClass().getResourceAsStream(KEYSTORE_FILE), KEYSTORE_PWD.toCharArray());
|
||||
return keystore;
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCheckingAliasAndName_thenMatchIsFound() throws Exception {
|
||||
KeyStore keystore = readKeyStore();
|
||||
|
||||
assertThat(keystore.containsAlias(KEYSTORE_ALIAS)).isTrue();
|
||||
|
||||
X509Certificate x509Certificate = (X509Certificate) keystore.getCertificate(KEYSTORE_ALIAS);
|
||||
String owner = x509Certificate.getSubjectX500Principal().getName();
|
||||
assertThat(owner.contains("my-cn.localhost")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCheckingAliasAndName_thenNameIsNotFound() throws Exception {
|
||||
KeyStore keystore = readKeyStore();
|
||||
|
||||
assertThat(keystore.containsAlias(KEYSTORE_ALIAS)).isTrue();
|
||||
|
||||
X509Certificate x509Certificate = (X509Certificate) keystore.getCertificate(KEYSTORE_ALIAS);
|
||||
String owner = x509Certificate.getSubjectX500Principal()
|
||||
.getName();
|
||||
assertThat(owner.contains("commonName1")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCheckingAliasAndName_thenAliasIsNotFound() throws Exception {
|
||||
KeyStore keystore = readKeyStore();
|
||||
|
||||
assertThat(keystore.containsAlias("alias1")).isFalse();
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue