GeneratedKeystoreProvider implements IKeystoreProvider { public KeyStore getKeyStore() { if (!doesKeystoreExist()) { generateKeystore(); } return generatedProvider.getKeyStore(); } GeneratedKeystoreProvider(final String workingDirectory);  GeneratedKeystoreProvider(final String workingDirectory,
                                     final String keystorePass,
                                     final String certificatePass,
                                     final String alias); KeyStore getKeyStore(); @Override void writeTo(final OutputStream outputStream);  }
@Test public void testGeneratedFile() { File tempDir = workingDirectory.getWorkingDir(); provider.getKeyStore(); File generatedKeystore = new File(tempDir, "generated.p12"); assertTrue(generatedKeystore.exists()); assertTrue(generatedKeystore.isFile()); }
@Test public void testReuseKeystore() throws Exception { File tempDir = workingDirectory.getWorkingDir(); provider.getKeyStore(); GeneratedKeystoreProvider provider2 = new GeneratedKeystoreProvider(tempDir.getAbsolutePath()); RSAPrivateKey key1 = (RSAPrivateKey) provider.getKeyStore() .getKey("kangaroo", "kangaroo".toCharArray()); RSAPrivateKey key2 = (RSAPrivateKey) provider2.getKeyStore() .getKey("kangaroo", "kangaroo".toCharArray()); assertEquals(key1.getModulus(), key2.getModulus()); }
@Test(expected = RuntimeException.class) public void testCannotReuseKeystore() throws Exception { File tempDir = workingDirectory.getWorkingDir(); GeneratedKeystoreProvider provider2 = new GeneratedKeystoreProvider(tempDir.getAbsolutePath(), "otherpassword", "otherpassword", "otheralias"); provider2.getKeyStore(); provider.getKeyStore(); }
@Test public void validateCertificates() throws Exception { Certificate[] chain = provider.getKeyStore().getCertificateChain("kangaroo"); assertEquals(1, chain.length); X509Certificate cert = (X509Certificate) chain[0]; assertNotNull(cert); Principal iss = cert.getIssuerDN(); Principal sub = cert.getSubjectDN(); assertEquals("CN=localhost, OU=Kangaroo, O=Kangaroo, " + "L=Seattle, ST=Washington, C=US", iss.getName()); assertEquals("CN=localhost, OU=Kangaroo, O=Kangaroo, " + "L=Seattle, ST=Washington, C=US", sub.getName()); }
@Test public void getKey() throws Exception { KeyStore ks = provider.getKeyStore(); RSAPrivateKey key = (RSAPrivateKey) ks.getKey("kangaroo", "kangaroo".toCharArray()); Certificate[] chain = provider.getKeyStore().getCertificateChain("kangaroo"); assertEquals(1, chain.length); RSAPublicKey pubKey = (RSAPublicKey) chain[0].getPublicKey(); assertEquals(key.getModulus(), pubKey.getModulus()); }
@Test public void getKeystoreSingleton() throws Exception { KeyStore ks1 = provider.getKeyStore(); KeyStore ks2 = provider.getKeyStore(); assertSame(ks1, ks2); }