Path: blob/master/test/jdk/java/net/httpclient/ALPNFailureTest.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary This test will timeout if the ALPN CF is not completed26* when a 'Connection reset by peer' exception is raised27* during the handshake.28* @bug 821709429* @modules java.net.http30* java.logging31* @build ALPNFailureTest32* @run main/othervm -Djdk.internal.httpclient.debug=true ALPNFailureTest HTTP_1_133* @run main/othervm ALPNFailureTest HTTP_234*/35import javax.net.ServerSocketFactory;36import javax.net.ssl.SSLContext;37import java.io.Closeable;38import java.io.IOException;39import java.io.InputStream;40import java.io.OutputStream;41import java.net.InetAddress;42import java.net.ProxySelector;43import java.net.ServerSocket;44import java.net.Socket;45import java.net.SocketTimeoutException;46import java.net.StandardSocketOptions;47import java.net.URI;48import java.net.http.HttpClient;49import java.net.http.HttpRequest;50import java.net.http.HttpResponse;51import java.net.http.HttpTimeoutException;52import java.util.List;53import java.util.concurrent.atomic.AtomicBoolean;54import java.util.concurrent.atomic.AtomicReference;5556public class ALPNFailureTest {575859public static void main(String[] args) throws Exception{60if (args == null || args.length == 0) {61args = new String[] {HttpClient.Version.HTTP_1_1.name()};62}63ServerSocket socket = ServerSocketFactory.getDefault()64.createServerSocket(0, 10, InetAddress.getLoopbackAddress());6566test(socket, null, null, args);67}6869public static void test(ServerSocket socket, SSLContext context,70ProxySelector ps, String... args)71throws Exception72{73System.out.println("Tests a race condition in SSLTube/SSLFlowDelegate");74System.out.println("This test will timeout if the ALPN CF is not completed" +75" when a 'Connection reset by peer' exception is raised" +76" during the handshake - see 8217094.");7778URI uri = new URI("https", null,79socket.getInetAddress().getHostAddress(), socket.getLocalPort(),80"/ReadOnlyServer/https_1_1/", null, null);81HttpRequest request1 = HttpRequest.newBuilder(uri)82.GET().build();83HttpRequest request2 = HttpRequest.newBuilder(uri)84.POST(HttpRequest.BodyPublishers.ofString("foo")).build();8586ReadOnlyServer server = new ReadOnlyServer(socket);87Thread serverThread = new Thread(server, "ServerThread");88serverThread.start();89try {90for (var arg : args) {91var version = HttpClient.Version.valueOf(arg);92HttpClient.Builder builder = HttpClient.newBuilder()93.version(version);94if (ps != null) builder.proxy(ps);95if (context != null) builder.sslContext(context);9697HttpClient client = builder.build();98for (var request : List.of(request1, request2)) {99System.out.println("Server is " + socket.getLocalSocketAddress()100+ ", Version is " + version + ", Method is " + request.method()101+ (ps == null ? ", no proxy"102: (", Proxy is " + ps.select(request.uri()))));103try {104HttpResponse<String> resp =105client.send(request, HttpResponse.BodyHandlers.ofString());106throw new AssertionError(107"Client should not have received any response: " + resp);108} catch (HttpTimeoutException x) {109System.out.println("Unexpected " + x);110x.printStackTrace();111throw new AssertionError("Unexpected exception " + x, x);112} catch (Exception x) {113// We expect IOException("Connection reset by peer"), but114// any exception would do: we just don't want to linger115// forever.116System.err.println("Client got expected exception: " + x);117x.printStackTrace(System.out);118}119}120}121} finally {122server.close();123}124}125126public static class ReadOnlyServer implements Runnable, Closeable {127final ServerSocket socket;128final AtomicReference<Throwable> errorRef = new AtomicReference<>();129final AtomicBoolean closing = new AtomicBoolean();130ReadOnlyServer(ServerSocket socket) {131this.socket = socket;132}133134@Override135public void run() {136int count = 0;137int all = 0;138try {139System.out.println("Server starting");140while (!closing.get()) {141all += count;142count = 0;143try (Socket client = socket.accept()) {144client.setSoTimeout(1000);145client.setOption(StandardSocketOptions.SO_LINGER, 0);146InputStream is = client.getInputStream();147OutputStream os = client.getOutputStream();148boolean drain = true;149int timeouts = 0;150// now read some byte from the ClientHello151// and abruptly close the socket.152while (drain) {153try {154is.read();155count++;156if (count >= 50) {157drain = false;158}159} catch (SocketTimeoutException so) {160// make sure we read something161if (count > 0) timeouts++;162if (timeouts == 5) {163// presumably the client is164// waiting for us to answer...165// but we should not reach here.166drain = false;167}168}169}170System.out.println("Got " + count + " bytes");171}172}173} catch (Throwable t) {174if (!closing.get()) {175errorRef.set(t);176t.printStackTrace();177}178} finally {179System.out.println("Server existing after reading " + (all + count) + " bytes");180close();181}182183}184185@Override186public void close() {187if (closing.getAndSet(true))188return; // already closed189try {190socket.close();191} catch (IOException x) {192System.out.println("Exception while closing: " + x);193}194}195}196}197198199