Skip to content

Commit

Permalink
UnixResolverDnsServerAddressStreamProvider should allow for empty /et…
Browse files Browse the repository at this point in the history
…c/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.
  • Loading branch information
Scottmitch committed Jul 6, 2017
1 parent d040c93 commit d3581b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ static DnsServerAddressStreamProvider parseSilently() {
* @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) {
Expand All @@ -104,7 +102,7 @@ public UnixResolverDnsServerAddressStreamProvider(File etcResolvConf, File... et
this.defaultNameServerAddresses = defaultNameServerAddresses;
}

if (etcResolverFiles != null) {
if (useEtcResolverFiles) {
domainToNameServerStreamMap.putAll(etcResolvConfMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public void defaultValueReturnedIfNdotsNotPresent() throws IOException {
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);
Expand Down

0 comments on commit d3581b5

Please sign in to comment.