ProxyCA { private X509KeyManager createKeyManager() { return new X509KeyManager() { @Override public String[] getClientAliases(String s, Principal[] principals) { return new String[]{"client"}; } @Override public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) { return "client"; } @Override public String[] getServerAliases(String s, Principal[] principals) { return null; } @Override public String chooseServerAlias(String s, Principal[] principals, Socket socket) { return null; } @Override public X509Certificate[] getCertificateChain(String s) { return new X509Certificate[]{caCert}; } @Override public PrivateKey getPrivateKey(String s) { return caKeyPair.getPrivate(); } }; } ProxyCA(); void init(); void init(X509Certificate caCert, PrivateKey caPrivateKey); byte[] createChildKeyStore(ApplicationId appId, String ksPassword); byte[] getChildTrustStore(String password); String generateKeyStorePassword(); SSLContext createSSLContext(ApplicationId appId); HostnameVerifier getHostnameVerifier(); @VisibleForTesting X509Certificate getCaCert(); @VisibleForTesting KeyPair getCaKeyPair();  }
@Test public void testCreateKeyManager() throws Exception { ProxyCA proxyCA = new ProxyCA(); proxyCA.init(); X509KeyManager keyManager = proxyCA.getX509KeyManager(); Assert.assertArrayEquals(new String[]{"client"}, keyManager.getClientAliases(null, null)); Assert.assertEquals("client", keyManager.chooseClientAlias(null, null, null)); Assert.assertNull(keyManager.getServerAliases(null, null)); Assert.assertNull(keyManager.chooseServerAlias(null, null, null)); byte[] truststoreBytes = proxyCA.getChildTrustStore("password"); KeyStore truststore = KeyStoreTestUtil.bytesToKeyStore(truststoreBytes, "password"); Assert.assertEquals(1, truststore.size()); X509Certificate caCert = (X509Certificate) truststore.getCertificate("client"); Assert.assertArrayEquals(new X509Certificate[]{caCert}, keyManager.getCertificateChain(null)); Assert.assertEquals(proxyCA.getCaCert(), caCert); PrivateKey caPrivateKey = keyManager.getPrivateKey(null); PublicKey caPublicKey = caCert.getPublicKey(); checkPrivatePublicKeys(caPrivateKey, caPublicKey); Assert.assertEquals(proxyCA.getCaKeyPair().getPublic(), caPublicKey); Assert.assertEquals(proxyCA.getCaKeyPair().getPrivate(), caPrivateKey); }