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 /test/jdk/java/net/httpclient/lib
 29  * @build jdk.test.lib.net.SimpleSSLContext jdk.httpclient.test.lib.common.TestServerConfigurator
 30  * @modules java.net.http/jdk.internal.net.http.common
 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.HttpsServer;
 38 import com.sun.net.httpserver.HttpExchange;
 39 
 40 
 41 
 42 import java.io.IOException;
 43 import java.io.InputStream;
 44 import java.io.OutputStream;
 45 import java.net.InetAddress;
 46 import java.net.http.HttpClient;
 47 import java.net.http.WebSocket;
 48 import java.net.http.WebSocketHandshakeException;
 49 
 50 import jdk.httpclient.test.lib.common.TestServerConfigurator;
 51 import jdk.test.lib.net.SimpleSSLContext;
 52 import org.testng.annotations.AfterTest;
 53 import org.testng.annotations.BeforeTest;
 54 import org.testng.annotations.DataProvider;
 55 import org.testng.annotations.Test;
 56 import javax.net.ssl.SSLContext;
 57 import java.net.InetSocketAddress;
 58 import java.net.URI;
 59 import java.util.concurrent.CompletionException;
 60 import java.util.concurrent.ExecutionException;
 61 import java.util.concurrent.ExecutorService;
 62 import java.util.concurrent.Executors;
 63 
 64 import static java.net.http.HttpClient.Builder.NO_PROXY;
 65 import static org.testng.Assert.assertEquals;
 66 import static org.testng.Assert.assertNotNull;
 67 import static org.testng.Assert.assertTrue;
 68 import static org.testng.Assert.fail;
 69 import static java.lang.System.out;
 70 
 71 public class WSHandshakeExceptionTest {
 72 
 73     SSLContext sslContext;
 74     HttpServer httpTestServer;         // HTTP/1.1    [ 2 servers ]
 75     HttpsServer httpsTestServer;       // HTTPS/1.1
 76     String httpURI;
 77     String httpsURI;
 78     String httpNonUtf8URI;
 79     String httpsNonUtf8URI;
 80     HttpClient sharedClient;
 81 
 82     static final int ITERATION_COUNT = 4;
 83     // a shared executor helps reduce the amount of threads created by the test
 84     static final ExecutorService executor = Executors.newCachedThreadPool();
 85 
 86     @DataProvider(name = "variants")
 87     public Object[][] variants() {
 88         return new Object[][]{
 89                 { httpURI,           false },
 90                 { httpsURI,          false },
 91                 { httpURI,           true  },
 92                 { httpsURI,          true  },
 93 
 94                 { httpNonUtf8URI,    true  },
 95                 { httpsNonUtf8URI,   true  },
 96                 { httpNonUtf8URI,    false },
 97                 { httpsNonUtf8URI,   false }
 98         };
 99     }
100 
101     HttpClient newHttpClient() {
102         return HttpClient.newBuilder()
103                          .proxy(NO_PROXY)
104                          .executor(executor)
105                          .sslContext(sslContext)
106                          .build();
107     }
108 
109     @Test(dataProvider = "variants")
110     public void test(String uri, boolean sameClient) {
111         HttpClient client = sharedClient;
112         boolean pause;
113         for (int i = 0; i < ITERATION_COUNT; i++) {
114             System.out.printf("iteration %s%n", i);
115             if (!sameClient || client == null) {
116                 pause = client != null;
117                 client = newHttpClient();
118                 if (pause) gc(10); // give some time to gc
119             }
120             if (sharedClient == null) sharedClient = client;
121 
122             try {
123                 client.newWebSocketBuilder()
124                       .buildAsync(URI.create(uri), new WebSocket.Listener() { })
125                       .join();
126                 fail("Expected to throw");
127             } catch (CompletionException ce) {
128                 Throwable t = getCompletionCause(ce);
129                 if (!(t instanceof WebSocketHandshakeException)) {
130                     throw new AssertionError("Unexpected exception", t);
131                 }
132                 WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
133                 assertNotNull(wse.getResponse());
134                 assertNotNull(wse.getResponse().body());
135                 assertEquals(wse.getResponse().body().getClass(), String.class);
136                 String body = (String)wse.getResponse().body();
137                 out.println("Status code is " + wse.getResponse().statusCode());
138                 out.println("Response is " + body);
139                 if(uri.contains("/nonutf8body")) {
140                     // the invalid sequence 0xFF should have been replaced
141                     // by the replacement character (U+FFFD)
142                     assertTrue(body.equals("\ufffd"));
143                 }
144                 else {
145                     // default HttpServer 404 body expected
146                     assertTrue(body.contains("404"));
147                 }
148                 assertEquals(wse.getResponse().statusCode(), 404);
149             }
150         }
151     }
152 
153     static void gc(long ms) {
154         System.gc();
155         try {
156             Thread.sleep(ms);
157         } catch (InterruptedException x) {
158             // OK
159         }
160     }
161 
162     @BeforeTest
163     public void setup() throws Exception {
164         sslContext = new SimpleSSLContext().get();
165         if (sslContext == null)
166             throw new AssertionError("Unexpected null sslContext");
167 
168         // HTTP/1.1
169         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
170         httpTestServer = HttpServer.create(sa, 0);
171         httpURI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/";
172         httpNonUtf8URI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/nonutf8body";
173         httpTestServer.createContext("/nonutf8body", new BodyHandler());
174 
175         httpsTestServer = HttpsServer.create(sa, 0);
176         httpsTestServer.setHttpsConfigurator(new TestServerConfigurator(sa.getAddress(), sslContext));
177         httpsURI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/";
178         httpsNonUtf8URI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/nonutf8body";
179         httpsTestServer.createContext("/nonutf8body", new BodyHandler());
180 
181         httpTestServer.start();
182         httpsTestServer.start();
183     }
184 
185     @AfterTest
186     public void teardown() {
187         sharedClient = null;
188         gc(100);
189         httpTestServer.stop(0);
190         httpsTestServer.stop(0);
191         executor.shutdownNow();
192     }
193 
194     private static Throwable getCompletionCause(Throwable x) {
195         if (!(x instanceof CompletionException)
196                 && !(x instanceof ExecutionException)) return x;
197         final Throwable cause = x.getCause();
198         if (cause == null) {
199             throw new InternalError("Unexpected null cause", x);
200         }
201         return cause;
202     }
203 
204     static class BodyHandler implements HttpHandler {
205 
206         @Override
207         public void handle(HttpExchange e) throws IOException {
208             try(InputStream is = e.getRequestBody();
209                 OutputStream os = e.getResponseBody()) {
210                 byte[] invalidUtf8 = {(byte)0xFF}; //Invalid utf-8 byte
211                 e.sendResponseHeaders(404, invalidUtf8.length);
212                 os.write(invalidUtf8);
213             }
214         }
215     }
216 }