UnixResolverDnsServerAddressStreamProvider should allow for empty /etc/resolver dir

Motivation:
UnixResolverDnsServerAddressStreamProvider currently throws an exception if /etc/resolver exists but it empty. This shouldn't be an exception and can be tolerated as if there is no contribution from /etc/resolver.

Modifications:
- Treat /etc/resolver as present and empty the same as not being present

Result:
UnixResolverDnsServerAddressStreamProvider initialization can tolerate empty /etc/resolver directory.
This commit is contained in:
Scott Mitchell 2017-07-05 13:12:13 -04:00
parent d040c939e5
commit d3581b575e
2 changed files with 15 additions and 5 deletions

View File

@ -87,11 +87,9 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
* @throws IOException If an error occurs while parsing the input files.
*/
public UnixResolverDnsServerAddressStreamProvider(File etcResolvConf, File... etcResolverFiles) throws IOException {
if (etcResolverFiles != null && etcResolverFiles.length == 0) {
throw new IllegalArgumentException("etcResolverFiles must either be null or non-empty");
}
Map<String, DnsServerAddresses> etcResolvConfMap = parse(checkNotNull(etcResolvConf, "etcResolvConf"));
domainToNameServerStreamMap = etcResolverFiles != null ? parse(etcResolverFiles) : etcResolvConfMap;
final boolean useEtcResolverFiles = etcResolverFiles != null && etcResolverFiles.length != 0;
domainToNameServerStreamMap = useEtcResolverFiles ? parse(etcResolverFiles) : etcResolvConfMap;
DnsServerAddresses defaultNameServerAddresses = etcResolvConfMap.get(etcResolvConf.getName());
if (defaultNameServerAddresses == null) {
@ -104,7 +102,7 @@ public final class UnixResolverDnsServerAddressStreamProvider implements DnsServ
this.defaultNameServerAddresses = defaultNameServerAddresses;
}
if (etcResolverFiles != null) {
if (useEtcResolverFiles) {
domainToNameServerStreamMap.putAll(etcResolvConfMap);
}
}

View File

@ -99,6 +99,18 @@ public class UnixResolverDnsServerAddressStreamProviderTest {
assertEquals(DEFAULT_NDOTS, parseEtcResolverFirstNdots(f));
}
@Test
public void emptyEtcResolverDirectoryDoesNotThrow() throws IOException {
File f = buildFile("domain linecorp.local\n" +
"nameserver 127.0.0.2\n" +
"nameserver 127.0.0.3\n");
UnixResolverDnsServerAddressStreamProvider p =
new UnixResolverDnsServerAddressStreamProvider(f, folder.newFolder().listFiles());
DnsServerAddressStream stream = p.nameServerAddressStream("somehost");
assertHostNameEquals("127.0.0.2", stream.next());
}
private File buildFile(String contents) throws IOException {
File f = folder.newFile();
OutputStream out = new FileOutputStream(f);