FSKeystoreProvider implements IKeystoreProvider { public KeyStore getKeyStore() { if (keyStore == null) { try { keyStore = KeyStore.getInstance(keystoreType); FileInputStream fis = new FileInputStream(keystorePath); keyStore.load(fis, keystorePass.toCharArray()); } catch (Exception e) { throw new RuntimeException(e); } } return keyStore; } FSKeystoreProvider(final String keystorePath,
                              final String keystorePass,
                              final String keystoreType); KeyStore getKeyStore(); @Override void writeTo(final OutputStream outputStream);  }
@Test public void testLoadFile() throws Exception { FSKeystoreProvider provider = new FSKeystoreProvider(KS_PATH, KS_PASS, KS_TYPE); KeyStore ks = provider.getKeyStore(); assertEquals("kangaroo", ks.aliases().nextElement()); Certificate[] chain = ks.getCertificateChain("kangaroo"); assertEquals(1, chain.length); X509Certificate cert = (X509Certificate) chain[0]; assertNotNull(cert); Principal iss = cert.getIssuerDN(); Principal sub = cert.getSubjectDN(); assertEquals("EMAILADDRESS=krotscheck@gmail.com, " + "CN=localhost, OU=Kangaroo, O=Kangaroo, L=Seattle, " + "ST=Washington, C=US", iss.getName()); assertEquals("EMAILADDRESS=krotscheck@gmail.com, " + "CN=localhost, OU=Kangaroo, O=Kangaroo, L=Seattle, " + "ST=Washington, C=US", sub.getName()); }
@Test public void getKeystoreSingleton() throws Exception { FSKeystoreProvider provider = new FSKeystoreProvider(KS_PATH, KS_PASS, KS_TYPE); KeyStore ks1 = provider.getKeyStore(); KeyStore ks2 = provider.getKeyStore(); assertSame(ks1, ks2); }
@Test(expected = RuntimeException.class) public void testLoadNonexistentFile() { FSKeystoreProvider provider = new FSKeystoreProvider("", KS_PASS, KS_TYPE); provider.getKeyStore(); }
@Test(expected = RuntimeException.class) public void testLoadBadPassword() { FSKeystoreProvider provider = new FSKeystoreProvider(KS_PATH, "invalid", KS_TYPE); provider.getKeyStore(); }
@Test(expected = RuntimeException.class) public void testLoadBadType() { FSKeystoreProvider provider = new FSKeystoreProvider(KS_PATH, KS_PASS, "INVALID"); provider.getKeyStore(); }