Skip to content

Commit

Permalink
Make FakeDns more capable.
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke authored and squarejesse committed Mar 5, 2017
1 parent 89621df commit 9e0b5b8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 63 deletions.
48 changes: 35 additions & 13 deletions okhttp-testing-support/src/main/java/okhttp3/FakeDns.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,61 @@
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public final class FakeDns implements Dns {
private List<String> requestedHosts = new ArrayList<>();
private List<InetAddress> addresses = Collections.emptyList();
private final Map<String, List<InetAddress>> hostAddresses = new LinkedHashMap<>();
private final List<String> requestedHosts = new ArrayList<>();
private int nextAddress = 100;

/** Sets the addresses to be returned by this fake DNS service. */
public FakeDns addresses(List<InetAddress> addresses) {
this.addresses = new ArrayList<>(addresses);
/** Sets the results for {@code hostname}. */
public FakeDns set(String hostname, List<InetAddress> addresses) {
hostAddresses.put(hostname, addresses);
return this;
}

/** Sets the service to throw when a hostname is requested. */
public FakeDns unknownHost() {
this.addresses = Collections.emptyList();
/** Clears the results for {@code hostname}. */
public FakeDns clear(String hostname) {
hostAddresses.remove(hostname);
return this;
}

public InetAddress address(int index) {
return addresses.get(index);
public InetAddress lookup(String hostname, int index) throws UnknownHostException {
return hostAddresses.get(hostname).get(index);
}

@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
requestedHosts.add(hostname);
if (addresses.isEmpty()) throw new UnknownHostException();
return addresses;

List<InetAddress> result = hostAddresses.get(hostname);
if (result != null) return result;

throw new UnknownHostException();
}

public void assertRequests(String... expectedHosts) {
assertEquals(Arrays.asList(expectedHosts), requestedHosts);
requestedHosts.clear();
}

/** Allocates and returns {@code count} fake addresses like [255.0.0.100, 255.0.0.101]. */
public List<InetAddress> allocate(int count) {
try {
List<InetAddress> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
if (nextAddress > 255) {
throw new AssertionError("too many addresses allocated");
}
result.add(InetAddress.getByAddress(
new byte[] {(byte) 255, (byte) 0, (byte) 0, (byte) nextAddress++}));
}
return result;
} catch (UnknownHostException e) {
throw new AssertionError();
}
}
}
6 changes: 3 additions & 3 deletions okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
// Attempt conditional cache validation and a DNS miss.
client.connectionPool().evictAll();
client = client.newBuilder()
.dns(new FakeDns().unknownHost())
.dns(new FakeDns())
.build();
executeSynchronously("/").assertFailure(UnknownHostException.class);
}
Expand Down Expand Up @@ -2347,9 +2347,9 @@ private InetSocketAddress startNullServer() throws IOException {
}

@Test public void customDns() throws Exception {
// Configure a DNS that returns our MockWebServer for every hostname.
// Configure a DNS that returns our local MockWebServer for android.com.
FakeDns dns = new FakeDns();
dns.addresses(Dns.SYSTEM.lookup(server.url("/").host()));
dns.set("android.com", Dns.SYSTEM.lookup(server.url("/").host()));
client = client.newBuilder()
.dns(dns)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(0), uriPort);
dns.set(uriHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
dns.assertRequests(uriHost);

assertFalse(routeSelector.hasNext());
Expand All @@ -104,11 +104,11 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 1));
dns.set(uriHost, dns.allocate(1));
Route route = routeSelector.next();
routeDatabase.failed(route);
routeSelector = new RouteSelector(address, routeDatabase);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(0), uriPort);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
assertFalse(routeSelector.hasNext());
try {
routeSelector.next();
Expand All @@ -123,9 +123,9 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 2));
assertRoute(routeSelector.next(), address, proxyA, dns.address(0), proxyAPort);
assertRoute(routeSelector.next(), address, proxyA, dns.address(1), proxyAPort);
dns.set(proxyAHost, dns.allocate(2));
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 0), proxyAPort);
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 1), proxyAPort);

assertFalse(routeSelector.hasNext());
dns.assertRequests(proxyAHost);
Expand All @@ -138,9 +138,9 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 2));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(0), uriPort);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(1), uriPort);
dns.set(uriHost, dns.allocate(2));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 1), uriPort);

assertFalse(routeSelector.hasNext());
dns.assertRequests(uriHost);
Expand All @@ -164,8 +164,8 @@ public final class RouteSelectorTest {
authenticator, null, protocols, connectionSpecs, nullProxySelector);
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(0), uriPort);
dns.set(uriHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
dns.assertRequests(uriHost);

assertFalse(routeSelector.hasNext());
Expand All @@ -176,9 +176,9 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 2));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(0), uriPort);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(1), uriPort);
dns.set(uriHost, dns.allocate(2));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 1), uriPort);

assertFalse(routeSelector.hasNext());
dns.assertRequests(uriHost);
Expand All @@ -195,15 +195,15 @@ public final class RouteSelectorTest {

// First try the IP addresses of the first proxy, in sequence.
assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 2));
assertRoute(routeSelector.next(), address, proxyA, dns.address(0), proxyAPort);
assertRoute(routeSelector.next(), address, proxyA, dns.address(1), proxyAPort);
dns.set(proxyAHost, dns.allocate(2));
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 0), proxyAPort);
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 1), proxyAPort);
dns.assertRequests(proxyAHost);

// Next try the IP address of the second proxy.
assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(254, 1));
assertRoute(routeSelector.next(), address, proxyB, dns.address(0), proxyBPort);
dns.set(proxyBHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, proxyB, dns.lookup(proxyBHost, 0), proxyBPort);
dns.assertRequests(proxyBHost);

// No more proxies to try.
Expand All @@ -219,8 +219,8 @@ public final class RouteSelectorTest {

// Only the origin server will be attempted.
assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.address(0), uriPort);
dns.set(uriHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
dns.assertRequests(uriHost);

assertFalse(routeSelector.hasNext());
Expand All @@ -236,12 +236,12 @@ public final class RouteSelectorTest {
proxySelector.assertRequests(address.url().uri());

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 1));
assertRoute(routeSelector.next(), address, proxyA, dns.address(0), proxyAPort);
dns.set(proxyAHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 0), proxyAPort);
dns.assertRequests(proxyAHost);

assertTrue(routeSelector.hasNext());
dns.unknownHost();
dns.clear(proxyBHost);
try {
routeSelector.next();
fail();
Expand All @@ -250,8 +250,8 @@ public final class RouteSelectorTest {
dns.assertRequests(proxyBHost);

assertTrue(routeSelector.hasNext());
dns.addresses(makeFakeAddresses(255, 1));
assertRoute(routeSelector.next(), address, proxyA, dns.address(0), proxyAPort);
dns.set(proxyAHost, dns.allocate(1));
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 0), proxyAPort);
dns.assertRequests(proxyAHost);

assertFalse(routeSelector.hasNext());
Expand All @@ -264,16 +264,16 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

// Proxy A
dns.addresses(makeFakeAddresses(255, 2));
assertRoute(routeSelector.next(), address, proxyA, dns.address(0), proxyAPort);
dns.set(proxyAHost, dns.allocate(2));
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 0), proxyAPort);
dns.assertRequests(proxyAHost);
assertRoute(routeSelector.next(), address, proxyA, dns.address(1), proxyAPort);
assertRoute(routeSelector.next(), address, proxyA, dns.lookup(proxyAHost, 1), proxyAPort);

// Proxy B
dns.addresses(makeFakeAddresses(254, 2));
assertRoute(routeSelector.next(), address, proxyB, dns.address(0), proxyBPort);
dns.set(proxyBHost, dns.allocate(2));
assertRoute(routeSelector.next(), address, proxyB, dns.lookup(proxyBHost, 0), proxyBPort);
dns.assertRequests(proxyBHost);
assertRoute(routeSelector.next(), address, proxyB, dns.address(1), proxyBPort);
assertRoute(routeSelector.next(), address, proxyB, dns.lookup(proxyBHost, 1), proxyBPort);

// No more proxies to attempt.
assertFalse(routeSelector.hasNext());
Expand All @@ -284,7 +284,7 @@ public final class RouteSelectorTest {
RouteSelector routeSelector = new RouteSelector(address, routeDatabase);

final int numberOfAddresses = 2;
dns.addresses(makeFakeAddresses(255, numberOfAddresses));
dns.set(uriHost, dns.allocate(numberOfAddresses));

// Extract the regular sequence of routes from selector.
List<Route> regularRoutes = new ArrayList<>();
Expand Down Expand Up @@ -351,17 +351,4 @@ private Address httpsAddress() {
return new Address(uriHost, uriPort, dns, socketFactory, sslSocketFactory,
hostnameVerifier, null, authenticator, null, protocols, connectionSpecs, proxySelector);
}

private static List<InetAddress> makeFakeAddresses(int prefix, int count) {
try {
List<InetAddress> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
result.add(InetAddress.getByAddress(
new byte[] {(byte) prefix, (byte) 0, (byte) 0, (byte) i}));
}
return result;
} catch (UnknownHostException e) {
throw new AssertionError();
}
}
}

0 comments on commit 9e0b5b8

Please sign in to comment.