PasswordHelper extends ComponentSupport { @Nullable public String encrypt(@Nullable final String password) { if (password == null) { return null; } if (mavenCipher.isPasswordCipher(password)) { log.warn("Value appears to be already encrypted", log.isDebugEnabled() ? new IllegalArgumentException() : null); return password; } String encodedPassword = mavenCipher.encrypt(password, phraseService.getPhrase(ENC)); if (encodedPassword != null && !encodedPassword.equals(password)) { return phraseService.mark(encodedPassword); } return encodedPassword; } @Inject  PasswordHelper(final MavenCipher mavenCipher, final PhraseService phraseService); @Nullable String encrypt(@Nullable final String password); @Nullable String encryptChars(@Nullable final char[] chars); @Nullable String encryptChars(@Nullable final char[] chars, final int offset, final int length); @Nullable String decrypt(@Nullable final String encodedPassword); @Nullable char[] decryptChars(@Nullable final String encodedPassword); @Nullable String tryDecrypt(@Nullable final String encodedPassword); @Nullable char[] tryDecryptChars(@Nullable final String encodedPassword);  }
@Test public void testThreadSafety() throws Exception { final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); final String password = "just-some-password-for-testing"; Thread[] threads = new Thread[20]; for (int i = 0; i < threads.length; i++) { threads[i] = new Thread() { @Override public void run() { for (int i = 0; i < 20; i++) { try { assertDecrypt(legacyPasswordHelper, legacyPasswordHelper.encrypt(password), password); assertDecrypt(customPasswordHelper, customPasswordHelper.encrypt(password), password); } catch (Throwable e) { error.compareAndSet(null, e); } } } }; } for (Thread thread : threads) { thread.start(); } for (Thread thread : threads) { thread.join(); } if (error.get() != null) { Throwables.throwIfUnchecked(error.get()); throw new RuntimeException(error.get()); } }
@Test public void testLegacyPhraseFallback() throws Exception { String password = "clear-text-password"; String encodedPass = legacyPasswordHelper.encrypt(password); assertDecrypt(legacyPasswordHelper, encodedPass, password); assertDecrypt(customPasswordHelper, encodedPass, password); }
@Test public void testCustomPhraseFile() throws Exception { PhraseService phraseService = new FilePhraseService(util.resolveFile("target/test-classes/custom.enc")); PasswordHelper underTest = new PasswordHelper(new MavenCipherImpl(new CryptoHelperImpl()), phraseService); String password = "clear-text-password"; String encodedPass = underTest.encrypt(password); assertDecrypt(underTest, encodedPass, password); }
@Test public void testMissingPhraseFile() throws Exception { PhraseService phraseService = new FilePhraseService(util.resolveFile("target/test-classes/missing.enc")); PasswordHelper underTest = new PasswordHelper(new MavenCipherImpl(new CryptoHelperImpl()), phraseService); String password = "clear-text-password"; try { underTest.encrypt(password); fail("Expected RuntimeException wrapping FileNotFoundException"); } catch (RuntimeException e) { assertThat(e.getCause(), is(instanceOf(FileNotFoundException.class))); } }