-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8367976: Validate and clamp jdk.httpclient.bufsize #27874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vy
wants to merge
5
commits into
openjdk:master
Choose a base branch
from
vy:bufSizeClamp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6713346
Clamp `Utils::BUFSIZE`
vy 148d426
Update `module-info.java` docs
vy 0cd7dee
Apply property doc suggestion
vy 5e839d4
Switch to POST and repeated-request in `BufferSize1Test`
vy 26fdcb4
Merge remote-tracking branch 'upstream/master' into bufSizeClamp
vy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import jdk.httpclient.test.lib.common.HttpServerAdapters; | ||
import jdk.httpclient.test.lib.common.HttpServerAdapters.HttpTestServer; | ||
import jdk.internal.net.http.common.Utils; | ||
import jdk.test.lib.net.SimpleSSLContext; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpClient.Version; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import static java.net.http.HttpClient.Builder.NO_PROXY; | ||
import static java.net.http.HttpClient.Version.HTTP_3; | ||
import static java.net.http.HttpOption.H3_DISCOVERY; | ||
import static java.net.http.HttpOption.Http3DiscoveryMode.HTTP_3_URI_ONLY; | ||
|
||
/* | ||
* @test | ||
* @bug 8367976 | ||
* @summary Verifies that setting the `jdk.httpclient.bufsize` system property | ||
* to its lowest possible value, 1, does not wedge the client | ||
* @library /test/jdk/java/net/httpclient/lib | ||
* /test/lib | ||
* @run main/othervm -Djdk.httpclient.bufsize=1 -Dtest.httpVersion=HTTP_1_1 BufferSize1Test | ||
* @run main/othervm -Djdk.httpclient.bufsize=1 -Dtest.httpVersion=HTTP_1_1 -Dtest.sslEnabled BufferSize1Test | ||
* @run main/othervm -Djdk.httpclient.bufsize=1 -Dtest.httpVersion=HTTP_2 BufferSize1Test | ||
* @run main/othervm -Djdk.httpclient.bufsize=1 -Dtest.httpVersion=HTTP_2 -Dtest.sslEnabled BufferSize1Test | ||
* @run main/othervm -Djdk.httpclient.bufsize=1 -Dtest.httpVersion=HTTP_3 BufferSize1Test | ||
*/ | ||
|
||
public class BufferSize1Test { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// Verify `Utils.BUFSIZE` | ||
if (Utils.BUFSIZE != 1) { | ||
throw new AssertionError("Unexpected `Utils.BUFSIZE`: " + Utils.BUFSIZE); | ||
} | ||
|
||
// Create the server | ||
var version = Version.valueOf(System.getProperty("test.httpVersion")); | ||
var sslContext = System.getProperty("test.sslEnabled") != null || HTTP_3.equals(version) | ||
? new SimpleSSLContext().get() | ||
: null; | ||
try (var server = switch (version) { | ||
case HTTP_1_1, HTTP_2 -> HttpTestServer.create(version, sslContext); | ||
case HTTP_3 -> HttpTestServer.create(HTTP_3_URI_ONLY, sslContext); | ||
}) { | ||
|
||
// Add the handler and start the server | ||
var serverHandlerPath = "/" + BufferSize1Test.class.getSimpleName(); | ||
server.addHandler(new BodyEchoingHandler(), serverHandlerPath); | ||
server.start(); | ||
|
||
// Create the client | ||
try (var client = createClient(version, sslContext)) { | ||
|
||
// Create the request with body to ensure that `ByteBuffer`s | ||
// will be used throughout the entire end-to-end interaction. | ||
byte[] requestBodyBytes = "body".repeat(1000).getBytes(StandardCharsets.US_ASCII); | ||
var request = createRequest(sslContext, server, serverHandlerPath, version, requestBodyBytes); | ||
|
||
// Execute and verify the request, twice for certainty. | ||
requestAndVerify(client, request, requestBodyBytes); | ||
requestAndVerify(client, request, requestBodyBytes); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
private static HttpClient createClient(Version version, SSLContext sslContext) { | ||
var clientBuilder = HttpServerAdapters | ||
.createClientBuilderFor(version) | ||
.proxy(NO_PROXY) | ||
.version(version); | ||
if (sslContext != null) { | ||
clientBuilder.sslContext(sslContext); | ||
} | ||
return clientBuilder.build(); | ||
} | ||
|
||
private static HttpRequest createRequest(SSLContext sslContext, HttpTestServer server, String serverHandlerPath, Version version, byte[] requestBodyBytes) { | ||
var requestUri = URI.create(String.format( | ||
"%s://%s%s/x", | ||
sslContext == null ? "http" : "https", | ||
server.serverAuthority(), | ||
serverHandlerPath)); | ||
var requestBuilder = HttpRequest | ||
.newBuilder(requestUri) | ||
.version(version) | ||
.POST(HttpRequest.BodyPublishers.ofByteArray(requestBodyBytes)); | ||
if (HTTP_3.equals(version)) { | ||
requestBuilder.setOption(H3_DISCOVERY, HTTP_3_URI_ONLY); | ||
} | ||
return requestBuilder.build(); | ||
} | ||
|
||
private static void requestAndVerify(HttpClient client, HttpRequest request, byte[] requestBodyBytes) | ||
throws IOException, InterruptedException { | ||
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); | ||
if (response.statusCode() != 200) { | ||
throw new AssertionError("Was expecting status code 200, found: " + response.statusCode()); | ||
} | ||
byte[] responseBodyBytes = response.body(); | ||
int mismatchIndex = Arrays.mismatch(requestBodyBytes, responseBodyBytes); | ||
if (mismatchIndex >= 0) { | ||
var message = String.format( | ||
"Response body (%s bytes) mismatches the request body (%s bytes) at index %s!", | ||
responseBodyBytes.length, requestBodyBytes.length, mismatchIndex); | ||
throw new AssertionError(message); | ||
} | ||
} | ||
|
||
private static final class BodyEchoingHandler implements HttpServerAdapters.HttpTestHandler { | ||
|
||
@Override | ||
public void handle(HttpServerAdapters.HttpTestExchange exchange) throws IOException { | ||
try (exchange) { | ||
byte[] body; | ||
try (var requestBodyStream = exchange.getRequestBody()) { | ||
body = requestBodyStream.readAllBytes(); | ||
} | ||
exchange.sendResponseHeaders(200, body.length); | ||
try (var responseBodyStream = exchange.getResponseBody()) { | ||
responseBodyStream.write(body); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
test/jdk/java/net/httpclient/BufferSizePropertyClampTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import jdk.test.lib.process.ProcessTools; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/* | ||
* @test | ||
* @bug 8367976 | ||
* @summary Verifies that the `jdk.httpclient.bufsize` system property is | ||
* clamped correctly | ||
* @library /test/lib | ||
* @run junit BufferSizePropertyClampTest | ||
*/ | ||
|
||
class BufferSizePropertyClampTest { | ||
|
||
private static Path scriptPath; | ||
|
||
@BeforeAll | ||
static void setUp() throws IOException { | ||
// Create a Java file that prints the `Utils::BUFSIZE` value | ||
scriptPath = Path.of("UtilsBUFSIZE.java"); | ||
Files.write(scriptPath, List.of("void main() { IO.println(jdk.internal.net.http.common.Utils.BUFSIZE); }")); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() throws IOException { | ||
Files.deleteIfExists(scriptPath); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {-1, 0, (2 << 14) + 1}) | ||
void test(int invalidBufferSize) throws Exception { | ||
|
||
// Run the Java file | ||
var outputAnalyzer = ProcessTools.executeTestJava( | ||
"--add-exports", "java.net.http/jdk.internal.net.http.common=ALL-UNNAMED", | ||
"-Djdk.httpclient.HttpClient.log=errors", | ||
"-Djdk.httpclient.bufsize=" + invalidBufferSize, | ||
scriptPath.toString()); | ||
outputAnalyzer.shouldHaveExitValue(0); | ||
|
||
// Verify stderr | ||
List<String> stderrLines = outputAnalyzer.stderrAsLines(); | ||
assertEquals(2, stderrLines.size(), "Expected 2 lines, found: " + stderrLines); | ||
assertTrue( | ||
stderrLines.get(0).endsWith("jdk.internal.net.http.common.Utils getIntegerNetProperty"), | ||
"Unexpected line: " + stderrLines.get(0)); | ||
assertEquals( | ||
"INFO: ERROR: Property value for jdk.httpclient.bufsize=" + invalidBufferSize + " not in [1..16384]: using default=16384", | ||
stderrLines.get(1).replaceAll(",", "")); | ||
|
||
// Verify stdout | ||
var stdoutLines = outputAnalyzer.stdoutAsLines(); | ||
assertEquals(1, stdoutLines.size(), "Expected one line, found: " + stdoutLines); | ||
assertEquals("16384", stdoutLines.get(0)); | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could simply use / extend the HttpServerAdapters.HttpTestEchoHandler;
Also consider implementing HttpServerAdapters in the test class to get rid of the leading HttpServerAdapters qualifier.