1 /*
  2  * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8240666
 27  * @summary Basic test for WebSocketHandshakeException
 28  * @library /test/lib
 29  * @build jdk.test.lib.net.SimpleSSLContext
 30  * @modules java.net.http
 31  *          jdk.httpserver
 32  * @run testng/othervm -Djdk.internal.httpclient.debug=true WSHandshakeExceptionTest
 33  */
 34 
 35 import com.sun.net.httpserver.HttpHandler;
 36 import com.sun.net.httpserver.HttpServer;
 37 import com.sun.net.httpserver.HttpsConfigurator;
 38 import com.sun.net.httpserver.HttpsServer;
 39 import com.sun.net.httpserver.HttpExchange;
 40 
 41 
 42 
 43 import java.io.IOException;
 44 import java.io.InputStream;
 45 import java.io.OutputStream;
 46 import java.net.InetAddress;
 47 import java.net.http.HttpClient;
 48 import java.net.http.WebSocket;
 49 import java.net.http.WebSocketHandshakeException;
 50 import jdk.test.lib.net.SimpleSSLContext;
 51 import org.testng.annotations.AfterTest;
 52 import org.testng.annotations.BeforeTest;
 53 import org.testng.annotations.DataProvider;
 54 import org.testng.annotations.Test;
 55 import javax.net.ssl.SSLContext;
 56 import java.net.InetSocketAddress;
 57 import java.net.URI;
 58 import java.util.concurrent.CompletionException;
 59 import java.util.concurrent.ExecutionException;
 60 import java.util.concurrent.ExecutorService;
 61 import java.util.concurrent.Executors;
 62 
 63 import static java.net.http.HttpClient.Builder.NO_PROXY;
 64 import static org.testng.Assert.assertEquals;
 65 import static org.testng.Assert.assertNotNull;
 66 import static org.testng.Assert.assertTrue;
 67 import static org.testng.Assert.fail;
 68 import static java.lang.System.out;
 69 
 70 public class WSHandshakeExceptionTest {
 71 
 72     SSLContext sslContext;
 73     HttpServer httpTestServer;         // HTTP/1.1    [ 2 servers ]
 74     HttpsServer httpsTestServer;       // HTTPS/1.1
 75     String httpURI;
 76     String httpsURI;
 77     String httpNonUtf8URI;
 78     String httpsNonUtf8URI;
 79     HttpClient sharedClient;
 80 
 81     static final int ITERATION_COUNT = 4;
 82     // a shared executor helps reduce the amount of threads created by the test
 83     static final ExecutorService executor = Executors.newCachedThreadPool();
 84 
 85     @DataProvider(name = "variants")
 86     public Object[][] variants() {
 87         return new Object[][]{
 88                 { httpURI,           false },
 89                 { httpsURI,          false },
 90                 { httpURI,           true  },
 91                 { httpsURI,          true  },
 92 
 93                 { httpNonUtf8URI,    true  },
 94                 { httpsNonUtf8URI,   true  },
 95                 { httpNonUtf8URI,    false },
 96                 { httpsNonUtf8URI,   false }
 97         };
 98     }
 99 
100     HttpClient newHttpClient() {
101         return HttpClient.newBuilder()
102                          .proxy(NO_PROXY)
103                          .executor(executor)
104                          .sslContext(sslContext)
105                          .build();
106     }
107 
108     @Test(dataProvider = "variants")
109     public void test(String uri, boolean sameClient) {
110         HttpClient client = sharedClient;
111         boolean pause;
112         for (int i = 0; i < ITERATION_COUNT; i++) {
113             System.out.printf("iteration %s%n", i);
114             if (!sameClient || client == null) {
115                 pause = client != null;
116                 client = newHttpClient();
117                 if (pause) gc(10); // give some time to gc
118             }
119             if (sharedClient == null) sharedClient = client;
120 
121             try {
122                 client.newWebSocketBuilder()
123                       .buildAsync(URI.create(uri), new WebSocket.Listener() { })
124                       .join();
125                 fail("Expected to throw");
126             } catch (CompletionException ce) {
127                 Throwable t = getCompletionCause(ce);
128                 if (!(t instanceof WebSocketHandshakeException)) {
129                     throw new AssertionError("Unexpected exception", t);
130                 }
131                 WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
132                 assertNotNull(wse.getResponse());
133                 assertNotNull(wse.getResponse().body());
134                 assertEquals(wse.getResponse().body().getClass(), String.class);
135                 String body = (String)wse.getResponse().body();
136                 out.println("Status code is " + wse.getResponse().statusCode());
137                 out.println("Response is " + body);
138                 if(uri.contains("/nonutf8body")) {
139                     // the invalid sequence 0xFF should have been replaced
140                     // by the replacement character (U+FFFD)
141                     assertTrue(body.equals("\ufffd"));
142                 }
143                 else {
144                     // default HttpServer 404 body expected
145                     assertTrue(body.contains("404"));
146                 }
147                 assertEquals(wse.getResponse().statusCode(), 404);
148             }
149         }
150     }
151 
152     static void gc(long ms) {
153         System.gc();
154         try {
155             Thread.sleep(ms);
156         } catch (InterruptedException x) {
157             // OK
158         }
159     }
160 
161     @BeforeTest
162     public void setup() throws Exception {
163         sslContext = new SimpleSSLContext().get();
164         if (sslContext == null)
165             throw new AssertionError("Unexpected null sslContext");
166 
167         // HTTP/1.1
168         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
169         httpTestServer = HttpServer.create(sa, 0);
170         httpURI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/";
171         httpNonUtf8URI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/nonutf8body";
172         httpTestServer.createContext("/nonutf8body", new BodyHandler());
173 
174         httpsTestServer = HttpsServer.create(sa, 0);
175         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
176         httpsURI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/";
177         httpsNonUtf8URI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/nonutf8body";
178         httpsTestServer.createContext("/nonutf8body", new BodyHandler());
179 
180         httpTestServer.start();
181         httpsTestServer.start();
182     }
183 
184     @AfterTest
185     public void teardown() {
186         sharedClient = null;
187         gc(100);
188         httpTestServer.stop(0);
189         httpsTestServer.stop(0);
190         executor.shutdownNow();
191     }
192 
193     private static Throwable getCompletionCause(Throwable x) {
194         if (!(x instanceof CompletionException)
195                 && !(x instanceof ExecutionException)) return x;
196         final Throwable cause = x.getCause();
197         if (cause == null) {
198             throw new InternalError("Unexpected null cause", x);
199         }
200         return cause;
201     }
202 
203     static class BodyHandler implements HttpHandler {
204 
205         @Override
206         public void handle(HttpExchange e) throws IOException {
207             try(InputStream is = e.getRequestBody();
208                 OutputStream os = e.getResponseBody()) {
209                 byte[] invalidUtf8 = {(byte)0xFF}; //Invalid utf-8 byte
210                 e.sendResponseHeaders(404, invalidUtf8.length);
211                 os.write(invalidUtf8);
212             }
213         }
214     }
215 }