### Question:
PasswordUtils { public String readPassword(String passwordProperty, String defaultPassword) { if (StringUtils.isNotBlank(passwordProperty)) { if (CredentialProvider.isAliasString(passwordProperty)) { return readPasswordFromStore(passwordProperty); } else { final String pw = readPasswordFromFile(passwordProperty, defaultPassword); return CredentialProvider.isAliasString(pw) ? readPasswordFromStore(pw) : pw; } } return defaultPassword; } private  PasswordUtils(); static PasswordUtils getInstance(); String readPassword(String passwordProperty, String defaultPassword); String readPasswordFromFile(String filePath, String defaultPassword); String readPasswordFromStore(String aliasStr, File masterKeyLocation, boolean isMasterKeyPersisted, File masterKeyStoreLocation);  }### Answer:
@Test public void shouldReadPasswordFromCredentialStoreOfAnAlias() throws Exception { final CredentialProvider credentialProvider = PowerMock.createNiceMock(CredentialProvider.class); setupBasicCredentialProviderExpectations(credentialProvider); credentialProvider.getPasswordForAlias(CS_ALIAS); PowerMock.expectLastCall().andReturn("testPassword".toCharArray()).anyTimes(); PowerMock.replay(credentialProvider, CredentialProvider.class); replayAll(); assertEquals("testPassword", passwordUtils.readPassword(CS_ALIAS, "testPasswordDefault")); verifyAll(); }
@Test public void shouldReadPasswordFromFileIfPasswordPropertyIsPasswordFilePath() throws Exception { final String testPassword = "ambariTest"; final File passwordFile = writeTestPasswordFile(testPassword); assertEquals("ambariTest", passwordUtils.readPassword(passwordFile.getAbsolutePath(), "testPasswordDefault")); }
@Test public void shouldReadDefaultPasswordIfPasswordPropertyIsPasswordFilePathButItDoesNotExists() throws Exception { final File passwordFile = new File("/my/test/password/file.dat"); assertEquals("testPasswordDefault", passwordUtils.readPassword(passwordFile.getAbsolutePath(), "testPasswordDefault")); }
@Test @Ignore("until we fix fo for ANY kind of users including root") public void shouldReadDefaultPasswordIfPasswordPropertyIsPasswordFilePathButItIsNotReadable() throws Exception { final String testPassword = "ambariTest"; final File passwordFile = writeTestPasswordFile(testPassword); passwordFile.setReadable(false); assertEquals("testPasswordDefault", passwordUtils.readPassword(passwordFile.getAbsolutePath(), "testPasswordDefault")); }
@Test public void shouldResolveEncryptedPaswordIfWeStoreTheAliasInPasswordFile() throws Exception { final String testPassword = "testPassword"; final File passwordFile = writeTestPasswordFile(CS_ALIAS); final CredentialProvider credentialProvider = PowerMock.createNiceMock(CredentialProvider.class); setupBasicCredentialProviderExpectations(credentialProvider); credentialProvider.getPasswordForAlias(CS_ALIAS); PowerMock.expectLastCall().andReturn(testPassword.toCharArray()).anyTimes(); PowerMock.replay(credentialProvider, CredentialProvider.class); replayAll(); assertEquals(testPassword, passwordUtils.readPassword(passwordFile.getAbsolutePath(), "testPasswordDefault")); verifyAll(); }