UnixResolverDnsServerAddressStreamProvider implements DnsServerAddressStreamProvider { @Override public DnsServerAddressStream nameServerAddressStream(String hostname) { for (;;) { int i = hostname.indexOf('.', 1); if (i < 0 || i == hostname.length() - 1) { return defaultNameServerAddresses.stream(); } DnsServerAddresses addresses = domainToNameServerStreamMap.get(hostname); if (addresses != null) { return addresses.stream(); } hostname = hostname.substring(i + 1); } } UnixResolverDnsServerAddressStreamProvider(File etcResolvConf, File... etcResolverFiles);  UnixResolverDnsServerAddressStreamProvider(String etcResolvConf, String etcResolverDir); @Override DnsServerAddressStream nameServerAddressStream(String hostname);  }
@Test public void defaultLookupShouldReturnResultsIfOnlySingleFileSpecified() throws Exception { File f = buildFile("domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); UnixResolverDnsServerAddressStreamProvider p = new UnixResolverDnsServerAddressStreamProvider(f, null); DnsServerAddressStream stream = p.nameServerAddressStream("somehost"); assertHostNameEquals("127.0.0.2", stream.next()); assertHostNameEquals("127.0.0.3", stream.next()); }
@Test public void defaultReturnedWhenNoBetterMatch() throws Exception { File f = buildFile("domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); File f2 = buildFile("domain squarecorp.local\n" + "nameserver 127.0.0.4\n" + "nameserver 127.0.0.5\n"); UnixResolverDnsServerAddressStreamProvider p = new UnixResolverDnsServerAddressStreamProvider(f, f2); DnsServerAddressStream stream = p.nameServerAddressStream("somehost"); assertHostNameEquals("127.0.0.2", stream.next()); assertHostNameEquals("127.0.0.3", stream.next()); }
@Test public void moreRefinedSelectionReturnedWhenMatch() throws Exception { File f = buildFile("domain linecorp.local\n" + "nameserver 127.0.0.2\n" + "nameserver 127.0.0.3\n"); File f2 = buildFile("domain dc1.linecorp.local\n" + "nameserver 127.0.0.4\n" + "nameserver 127.0.0.5\n"); UnixResolverDnsServerAddressStreamProvider p = new UnixResolverDnsServerAddressStreamProvider(f, f2); DnsServerAddressStream stream = p.nameServerAddressStream("myhost.dc1.linecorp.local"); assertHostNameEquals("127.0.0.4", stream.next()); assertHostNameEquals("127.0.0.5", stream.next()); }
@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()); }