summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2010-09-28 17:31:05 -0700
committerJesse Wilson <jessewilson@google.com>2010-09-28 17:31:05 -0700
commit67db542e53c47a28e3f96beb7c9e46330483dc40 (patch)
tree4c31ffc04d1ac32cb95225c3db77c939f7490f1f
parent1f8243e3d2b5a3f8e0398c304d1dea0395cbc368 (diff)
downloadlibcore-67db542e53c47a28e3f96beb7c9e46330483dc40.zip
libcore-67db542e53c47a28e3f96beb7c9e46330483dc40.tar.gz
libcore-67db542e53c47a28e3f96beb7c9e46330483dc40.tar.bz2
Fix a problem where URL.equals() was returning true when both hosts couldn't resolve.
See http://b/3045007 Change-Id: I83f2a0d8888dd30aaf6099049ce8008487d4337d
-rw-r--r--luni/src/main/java/java/net/URLStreamHandler.java32
-rw-r--r--luni/src/test/java/libcore/java/net/OldURLTest.java31
2 files changed, 35 insertions, 28 deletions
diff --git a/luni/src/main/java/java/net/URLStreamHandler.java b/luni/src/main/java/java/net/URLStreamHandler.java
index d95b2a5..dae193b 100644
--- a/luni/src/main/java/java/net/URLStreamHandler.java
+++ b/luni/src/main/java/java/net/URLStreamHandler.java
@@ -387,17 +387,33 @@ public abstract class URLStreamHandler {
/**
* Compares two URL objects whether they refer to the same host.
*
- * @param url1
- * the first URL to be compared.
- * @param url2
- * the second URL to be compared.
+ * @param a the first URL to be compared.
+ * @param b the second URL to be compared.
* @return {@code true} if both URLs refer to the same host, {@code false}
* otherwise.
*/
- protected boolean hostsEqual(URL url1, URL url2) {
- String host1 = getHost(url1), host2 = getHost(url2);
- return (host1 != null && host1.equalsIgnoreCase(host2))
- || Objects.equal(getHostAddress(url1), getHostAddress(url2));
+ protected boolean hostsEqual(URL a, URL b) {
+ /*
+ * URLs with the same case-insensitive host name have equal hosts
+ */
+ String aHost = getHost(a);
+ String bHost = getHost(b);
+ if (aHost != null && aHost.equalsIgnoreCase(bHost)) {
+ return true;
+ }
+
+ /*
+ * Call out to DNS to resolve the host addresses. If this succeeds for
+ * both addresses and both addresses yield the same InetAddress, report
+ * equality.
+ *
+ * Although it's consistent with historical behavior of the RI, this
+ * approach is fundamentally broken. In particular, acting upon this
+ * result is bogus because a single server may serve content for many
+ * unrelated host names.
+ */
+ InetAddress aResolved = getHostAddress(a);
+ return aResolved != null && aResolved.equals(getHostAddress(b));
}
/**
diff --git a/luni/src/test/java/libcore/java/net/OldURLTest.java b/luni/src/test/java/libcore/java/net/OldURLTest.java
index 6b3dfa0..9953451 100644
--- a/luni/src/test/java/libcore/java/net/OldURLTest.java
+++ b/luni/src/test/java/libcore/java/net/OldURLTest.java
@@ -292,31 +292,22 @@ public class OldURLTest extends TestCase {
args = {java.lang.Object.class}
)
public void testEqualsObject() throws MalformedURLException {
- URL testURL1 = new URL("http", "www.apache.org:8080", "test.html");
- URL wrongProto = new URL("ftp", "www.apache.org:8080", "test.html");
- URL wrongPort = new URL("http", "www.apache.org:8082", "test.html");
- URL wrongHost = new URL("http", "www.apache2.org:8080", "test.html");
- URL wrongRef = new URL("http", "www.apache.org:8080",
- "test2.html#BOTTOM");
+ URL testURL1 = new URL("http", "www.apache.org", 8080, "test.html");
+ URL wrongProto = new URL("ftp", "www.apache.org", 8080, "test.html");
+ URL wrongPort = new URL("http", "www.apache.org", 8082, "test.html");
+ URL wrongHost = new URL("http", "www.apache2.org", 8080, "test.html");
+ URL wrongRef = new URL("http", "www.apache.org", 8080, "test2.html#BOTTOM");
URL testURL2 = new URL("http://www.apache.org:8080/test.html");
-
- assertFalse("Assert 0: error in equals: not same", testURL1
- .equals(wrongProto));
- assertFalse("Assert 1: error in equals: not same", testURL1
- .equals(wrongPort));
- assertFalse("Assert 2: error in equals: not same", testURL1
- .equals(wrongHost));
- assertFalse("Assert 3: error in equals: not same", testURL1
- .equals(wrongRef));
-
- assertFalse("Assert 4: error in equals: not same", testURL1
- .equals(testURL2));
+ assertFalse("Assert 0: error in equals: not same", testURL1.equals(wrongProto));
+ assertFalse("Assert 1: error in equals: not same", testURL1.equals(wrongPort));
+ assertFalse("Assert 2: error in equals: not same", testURL1.equals(wrongHost));
+ assertFalse("Assert 3: error in equals: not same", testURL1.equals(wrongRef));
+ assertFalse("Assert 4: error in equals: not same", testURL1.equals(testURL2));
URL testURL3 = new URL("http", "www.apache.org", "/test.html");
URL testURL4 = new URL("http://www.apache.org/test.html");
- assertTrue("Assert 4: error in equals: same", testURL3
- .equals(testURL4));
+ assertTrue("Assert 4: error in equals: same", testURL3.equals(testURL4));
}
/**