Skip to content

Commit

Permalink
8051627: Invariants about java.net.URI resolve and relativize are wrong
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs
  • Loading branch information
tkiriyama authored and dfuch committed Nov 24, 2022
1 parent df6cf1e commit ab1f9ff
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/java.base/share/classes/java/net/URI.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,20 @@
* {@code demo/b/index.html}
* </blockquote>
*
* <p> <i>Relativization</i>, finally, is the inverse of resolution: For any
* two normalized URIs <i>u</i> and&nbsp;<i>v</i>,
* <p> <i>Relativization</i>, finally, can be regarded as the inverse of resolution.
* Let <i>u</i> be any normalized absolute URI ending with a slash character ({@code '/'})
* and <i>v</i> be any normalized relative URI not beginning with a period character ({@code '.'})
* or slash character ({@code '/'}). Then, the following statement is true:
*
* <blockquote>
* <i>u</i>{@code .relativize(}<i>u</i>{@code .resolve(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}&nbsp;&nbsp;and<br>
* <i>u</i>{@code .resolve(}<i>u</i>{@code .relativize(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}&nbsp;&nbsp;.<br>
* <i>u</i>{@code .relativize(}<i>u</i>{@code .resolve(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}
* </blockquote>
*
* Let <i>u</i> be any normalized absolute URI ending with a slash character ({@code '/'})
* and <i>v</i> be any normalized absolute URI. Then, the following statement is true:
*
* <blockquote>
* <i>u</i>{@code .resolve(}<i>u</i>{@code .relativize(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}
* </blockquote>
*
* This operation is often useful when constructing a document containing URIs
Expand Down
46 changes: 45 additions & 1 deletion test/jdk/java/net/URI/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/* @test
* @summary Unit test for java.net.URI
* @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
* 7171415 6339649 6933879 8037396 8272072
* 7171415 6339649 6933879 8037396 8272072 8051627
* @author Mark Reinhold
*/

Expand Down Expand Up @@ -1617,6 +1617,7 @@ static void bugs() {
b6339649();
b6933879();
b8037396();
b8051627();
b8272072();
}

Expand Down Expand Up @@ -1677,6 +1678,49 @@ private static void b8037396() {
eq("a%20b[c%20d]", u.getRawFragment());
}

// 8051627 - Invariants about java.net.URI resolve and relativize are wrong
private static void b8051627() {
try {
// Let u be a normalized absolute URI u which ends with "/" and
// v be a normalized relative URI v which does not start with "." or "/", then
// u.relativize(u.resolve(v)).equals(v) should be true
reltivizeAfterResolveTest("http://a/b/", "c/d", "c/d");
reltivizeAfterResolveTest("http://a/b/", "g;x?y#s", "g;x?y#s");

// when the URI condition is not met, u.relativize(u.resolve(v)).equals(v) may be false
// In the following examples, that should be false
reltivizeAfterResolveTest("http://a/b", "c/d", "http://a/c/d");
reltivizeAfterResolveTest("http://a/b/", "../c/d", "http://a/c/d");
reltivizeAfterResolveTest("http://a/b/", "/c/d", "http://a/c/d");
reltivizeAfterResolveTest("http://a/b/", "http://a/b/c/d", "c/d");

// Let u be a normalized absolute URI u which ends with "/" and
// v be a normalized absolute URI v, then
// u.resolve(u.relativize(v)).equals(v) should be true
resolveAfterRelativizeTest("http://a/b/", "http://a/b/c/d", "http://a/b/c/d");
resolveAfterRelativizeTest("http://a/b/", "http://a/b/c/g;x?y#s", "http://a/b/c/g;x?y#s");

// when the URI condition is not met, u.resolve(u.relativize(v)).equals(v) may be false
// In the following examples, that should be false
resolveAfterRelativizeTest("http://a/b", "http://a/b/c/d", "http://a/c/d");
resolveAfterRelativizeTest("http://a/b/", "c/d", "http://a/b/c/d");
} catch (URISyntaxException e) {
throw new AssertionError("shouldn't ever happen", e);
}
}
private static void reltivizeAfterResolveTest(String base, String target, String expected)
throws URISyntaxException {
URI baseURI = URI.create(base);
URI targetURI = URI.create(target);
eq(URI.create(expected), baseURI.relativize(baseURI.resolve(targetURI)));
}
private static void resolveAfterRelativizeTest(String base, String target, String expected)
throws URISyntaxException {
URI baseURI = URI.create(base);
URI targetURI = URI.create(target);
eq(URI.create(expected), baseURI.resolve(baseURI.relativize(targetURI)));
}

// 8272072 - Resolving URI relative path with no "/" may lead to incorrect toString
private static void b8272072() {
try {
Expand Down

1 comment on commit ab1f9ff

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.