Commit b3503014 authored by Brian Carlstrom's avatar Brian Carlstrom Committed by Gerrit Code Review
Browse files

Merge "Make sure URL.toURILenient throws the proper exception on trailing garbage escape"

parents 5088f4ce bb1546d2
......@@ -111,7 +111,7 @@ public abstract class UriCodec {
}
if (c == '%' && isPartiallyEncoded) {
// this is an encoded 3-character sequence like "%20"
builder.append(s, i, i + 3);
builder.append(s, i, Math.min(i + 3, s.length()));
i += 2;
} else if (c == ' ') {
builder.append('+');
......
......@@ -19,6 +19,8 @@ package libcore.java.net;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URI;
import java.net.URL;
import junit.framework.TestCase;
import libcore.util.SerializationTester;
......@@ -694,5 +696,35 @@ public final class URLTest extends TestCase {
assertEquals("a_b.c.d.net", url.getHost());
}
// http://b/7369778
public void testToURILeniantThrowsURISyntaxExceptionWithPartialTrailingEscape()
throws Exception {
// make sure if there a partial trailing escape that we don't throw the wrong exception
URL[] badUrls = new URL[] {
new URL("http://example.com/?foo=%%bar"),
new URL("http://example.com/?foo=%%bar%"),
new URL("http://example.com/?foo=%%bar%2"),
new URL("http://example.com/?foo=%%bar%%"),
new URL("http://example.com/?foo=%%bar%%%"),
new URL("http://example.com/?foo=%%bar%%%%"),
};
for (URL badUrl : badUrls) {
try {
badUrl.toURILenient();
fail();
} catch (URISyntaxException expected) {
}
}
// make sure we properly handle an normal escape at the end of a string
String[] goodUrls = new String[] {
"http://example.com/?foo=bar",
"http://example.com/?foo=bar%20",
};
for (String goodUrl : goodUrls) {
assertEquals(new URI(goodUrl), new URL(goodUrl).toURILenient());
}
}
// Adding a new test? Consider adding an equivalent test to URITest.java
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment