HADOOP-13353. LdapGroupsMapping getPassward shouldn't return null when IOException throws. Contributed by Wei-Chiu Chuang

This commit is contained in:
Naganarasimha 2016-08-06 23:18:53 +05:30
parent e1684f84e2
commit 8055ff1c3c
2 changed files with 24 additions and 8 deletions

View File

@ -398,19 +398,15 @@ public class LdapGroupsMapping
}
String getPassword(Configuration conf, String alias, String defaultPass) {
String password = null;
String password = defaultPass;
try {
char[] passchars = conf.getPassword(alias);
if (passchars != null) {
password = new String(passchars);
}
else {
password = defaultPass;
}
}
catch (IOException ioe) {
LOG.warn("Exception while trying to password for alias " + alias + ": "
+ ioe.getMessage());
} catch (IOException ioe) {
LOG.warn("Exception while trying to get password for alias " + alias
+ ": ", ioe);
}
return password;
}

View File

@ -52,6 +52,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -314,4 +315,23 @@ public class TestLdapGroupsMapping extends TestLdapGroupsMappingBase {
}
}
/**
* Make sure that when
* {@link Configuration#getPassword(String)} throws an IOException,
* {@link LdapGroupsMapping#setConf(Configuration)} does not throw an NPE.
*
* @throws Exception
*/
@Test(timeout = 10000)
public void testSetConf() throws Exception {
Configuration conf = new Configuration();
Configuration mockConf = Mockito.spy(conf);
when(mockConf.getPassword(anyString()))
.thenThrow(new IOException("injected IOException"));
// Set a dummy LDAP server URL.
mockConf.set(LdapGroupsMapping.LDAP_URL_KEY, "ldap://test");
mappingSpy.setConf(mockConf);
}
}