@Override JKSFileLoader build() { return new JKSFileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); } }
@Override public KeyStore loadKeyStore() throws IOException, GeneralSecurityException { KeyStore ks = KeyStore.getInstance(JKS_KEY_STORE_TYPE); InputStream inputStream = null; try { inputStream = new FileInputStream(new File(keyStorePath)); ks.load(inputStream, passwordStringToCharArray(keyStorePassword)); return ks; } finally { forceClose(inputStream); } }
@Test(expected = Exception.class) public void testLoadKeyStoreWithWrongPassword() throws Exception { String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS) .getAbsolutePath(); new JKSFileLoader.Builder() .setKeyStorePath(path) .setKeyStorePassword("wrong password") .build() .loadKeyStore(); }
@Test(expected = Exception.class) public void testLoadTrustStoreWithWrongPassword() throws Exception { String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS) .getAbsolutePath(); new JKSFileLoader.Builder() .setTrustStorePath(path) .setTrustStorePassword("wrong password") .build() .loadTrustStore(); }
@Test(expected = IOException.class) public void testLoadKeyStoreWithWrongFileType() throws Exception { // Trying to load a PEM file with JKS loader should fail String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM) .getAbsolutePath(); new JKSFileLoader.Builder() .setKeyStorePath(path) .setKeyStorePassword(x509TestContext.getKeyStorePassword()) .build() .loadKeyStore(); }
@Test(expected = NullPointerException.class) public void testLoadTrustStoreWithNullFilePath() throws Exception { new JKSFileLoader.Builder() .setTrustStorePassword(x509TestContext.getTrustStorePassword()) .build() .loadTrustStore(); }
@Override public KeyStore loadTrustStore() throws IOException, GeneralSecurityException { KeyStore ts = KeyStore.getInstance(JKS_KEY_STORE_TYPE); InputStream inputStream = null; try { inputStream = new FileInputStream(new File(trustStorePath)); ts.load(inputStream, passwordStringToCharArray(trustStorePassword)); return ts; } finally { forceClose(inputStream); } }
@Test(expected = NullPointerException.class) public void testLoadKeyStoreWithNullFilePath() throws Exception { new JKSFileLoader.Builder() .setKeyStorePassword(x509TestContext.getKeyStorePassword()) .build() .loadKeyStore(); }
@Test(expected = IOException.class) public void testLoadTrustStoreWithWrongFileType() throws Exception { // Trying to load a PEM file with JKS loader should fail String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM) .getAbsolutePath(); new JKSFileLoader.Builder() .setTrustStorePath(path) .setTrustStorePassword(x509TestContext.getTrustStorePassword()) .build() .loadTrustStore(); } }
@Test(expected = IOException.class) public void testLoadKeyStoreWithWrongFilePath() throws Exception { String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS) .getAbsolutePath(); new JKSFileLoader.Builder() .setKeyStorePath(path + ".does_not_exist") .setKeyStorePassword(x509TestContext.getKeyStorePassword()) .build() .loadKeyStore(); }
@Test(expected = IOException.class) public void testLoadTrustStoreWithWrongFilePath() throws Exception { String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS) .getAbsolutePath(); new JKSFileLoader.Builder() .setTrustStorePath(path + ".does_not_exist") .setTrustStorePassword(x509TestContext.getTrustStorePassword()) .build() .loadTrustStore(); }
@Test public void testLoadKeyStore() throws Exception { String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS) .getAbsolutePath(); KeyStore ks = new JKSFileLoader.Builder() .setKeyStorePath(path) .setKeyStorePassword(x509TestContext.getKeyStorePassword()) .build() .loadKeyStore(); Assert.assertEquals(1, ks.size()); }
@Test public void testLoadTrustStore() throws Exception { String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS) .getAbsolutePath(); KeyStore ts = new JKSFileLoader.Builder() .setTrustStorePath(path) .setTrustStorePassword(x509TestContext.getTrustStorePassword()) .build() .loadTrustStore(); Assert.assertEquals(1, ts.size()); }