SSLUtils { public static PrivateKeyStore loadServicePrivateKey(String pkeyFactoryClass) { PrivateKeyStoreFactory pkeyFactory; try { pkeyFactory = (PrivateKeyStoreFactory) Class.forName(pkeyFactoryClass).newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { LOGGER.error("Invalid PrivateKeyStoreFactory class: " + pkeyFactoryClass + " error: " + e.getMessage()); throw new IllegalArgumentException("Invalid private key store"); } return pkeyFactory.create(); }  static PrivateKeyStore loadServicePrivateKey(String pkeyFactoryClass);  }
@Test public void testClientAliasedX509ExtendedKeyManager() { SSLUtils.ClientAliasedX509ExtendedKeyManager keyManager = new SSLUtils.ClientAliasedX509ExtendedKeyManager(null, "testKeyAlias"); assertNull(keyManager.getDelegate()); assertThrows(RuntimeException.class, () -> { keyManager.chooseEngineServerAlias(null, null, null); }); assertThrows(RuntimeException.class, () -> { keyManager.getServerAliases(null, null); }); assertThrows(RuntimeException.class, () -> { keyManager.chooseServerAlias(null, null, null); }); assertThrows(IllegalArgumentException.class, () -> { SSLUtils.loadServicePrivateKey("testFactoryClassFail"); }); String[] temp = new String[1]; temp[0] = "tempTest"; String[] clientAliases = new String[2]; clientAliases[0] = "testClientAlias1"; clientAliases[1] = "testClientAlias2"; X509ExtendedKeyManager mockKeyManager = Mockito.mock(X509ExtendedKeyManager.class); when(mockKeyManager.chooseEngineClientAlias(any(), any(), any())).thenReturn("testPass"); when(mockKeyManager.chooseClientAlias(any(), any(), any())).thenReturn("testPass"); when(mockKeyManager.getClientAliases(anyString(), any())).thenReturn(clientAliases); SSLUtils.ClientAliasedX509ExtendedKeyManager keyManagerTemp = new SSLUtils.ClientAliasedX509ExtendedKeyManager(mockKeyManager, null); assertEquals(keyManagerTemp.chooseEngineClientAlias(temp, null, null), "testPass"); assertEquals(keyManagerTemp.chooseClientAlias(temp, null, null), "testPass"); assertEquals(keyManagerTemp.getClientAliases("testKeyType", null), clientAliases); when(mockKeyManager.getClientAliases(anyString(), any())).thenReturn(clientAliases); keyManagerTemp = new SSLUtils.ClientAliasedX509ExtendedKeyManager(mockKeyManager, "testClientAlias2"); assertEquals(keyManagerTemp.chooseEngineClientAlias(temp, null, null), "testClientAlias2"); when(mockKeyManager.getClientAliases(anyString(), any())).thenReturn(null); assertNull(keyManagerTemp.chooseEngineClientAlias(temp, null, null)); }
@Test public void testLoadServicePrivateKey() { PrivateKeyStore keyStore = SSLUtils.loadServicePrivateKey("com.yahoo.athenz.auth.impl.FilePrivateKeyStoreFactory"); Assert.assertNotNull(keyStore); }