SecretProviderFactory { public static Optional<SecretProvider> getSecretProvider(final CommandLine cli) { return cli.getKeystore() .map(keystore -> { final char[] password = cli.getPassword(); try { final KeyStoreHandler ksh = KeyStoreHandler.open(keystore, password); return Optional.of(SecretProvider.keyStoreBased(ksh)); } catch (final Exception ex) { LOGGER.error("Failed opening guarded storage.", ex); return Optional.<SecretProvider>empty(); } finally { Arrays.fill(password, ' '); } }) .orElseThrow(() -> new IllegalStateException("Could not find keystore.")); } private  SecretProviderFactory(); static Optional<SecretProvider> getSecretProvider(final CommandLine cli);  }
@Test void nonexistentKeyStoreProvided() throws Exception { final File tmp = File.createTempFile("robozonky-", ".keystore"); tmp.delete(); final CommandLine cli = SecretProviderFactoryTest.mockCli(tmp, "password".toCharArray()); assertThat(SecretProviderFactory.getSecretProvider(cli)).isEmpty(); }
@Test void wrongFormatKeyStoreProvided() throws Exception { final File tmp = File.createTempFile("robozonky-", ".keystore"); final char[] password = "pass".toCharArray(); final CommandLine cli = SecretProviderFactoryTest.mockCli(tmp, password); assertThat(SecretProviderFactory.getSecretProvider(cli)).isEmpty(); assertThat(password).isEqualTo(" ".toCharArray()); }
@Test void nullKeystoreProvided() { final char[] password = "pass".toCharArray(); final CommandLine cli = SecretProviderFactoryTest.mockCli(null, password); assertThatThrownBy(() -> SecretProviderFactory.getSecretProvider(cli)) .isInstanceOf(IllegalStateException.class); }