< prev index next >
   1 /*
   2  * Copyright (c) 2016, 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 import org.testng.annotations.Test;
  25 
  26 import java.io.IOException;
  27 import java.net.InetSocketAddress;
  28 import java.net.URI;
  29 import java.net.http.HttpClient;
  30 import java.net.http.WebSocket;
  31 import java.net.http.WebSocket.CloseCode;
  32 import java.nio.ByteBuffer;
  33 import java.nio.channels.SocketChannel;
  34 import java.util.concurrent.CompletableFuture;
  35 import java.util.concurrent.TimeUnit;
  36 import java.util.function.Consumer;
  37 import java.util.stream.Stream;
  38 
  39 /*
  40  * @test
  41  * @bug 8087113
  42  * @build TestKit
  43  * @run testng/othervm BasicWebSocketAPITest
  44  */
  45 public class BasicWebSocketAPITest {
  46 
  47     @Test
  48     public void webSocket() throws Exception {
  49         checkAndClose(
  50                 (ws) ->
  51                         TestKit.assertThrows(IllegalArgumentException.class,
  52                                 "(?i).*\\bnegative\\b.*",
  53                                 () -> ws.request(-1))
  54         );
  55         checkAndClose((ws) ->
  56                 TestKit.assertNotThrows(() -> ws.request(0))
  57         );
  58         checkAndClose((ws) ->
  59                 TestKit.assertNotThrows(() -> ws.request(1))
  60         );
  61         checkAndClose((ws) ->
  62                 TestKit.assertNotThrows(() -> ws.request(Long.MAX_VALUE))
  63         );
  64         checkAndClose((ws) ->
  65                 TestKit.assertNotThrows(ws::isClosed)
  66         );
  67         checkAndClose((ws) ->
  68                 TestKit.assertNotThrows(ws::getSubprotocol)
  69         );
  70         checkAndClose(
  71                 (ws) -> {
  72                     try {
  73                         ws.abort();
  74                     } catch (IOException ignored) { }
  75                     // No matter what happens during the first abort invocation,
  76                     // other invocations must return normally
  77                     TestKit.assertNotThrows(ws::abort);
  78                     TestKit.assertNotThrows(ws::abort);
  79                 }
  80         );
  81         checkAndClose(
  82                 (ws) ->
  83                         TestKit.assertThrows(NullPointerException.class,
  84                                 "message",
  85                                 () -> ws.sendBinary((byte[]) null, true))
  86         );
  87         checkAndClose(
  88                 (ws) ->
  89                         TestKit.assertThrows(NullPointerException.class,
  90                                 "message",
  91                                 () -> ws.sendBinary((ByteBuffer) null, true))
  92         );
  93         checkAndClose(
  94                 (ws) ->
  95                         TestKit.assertThrows(NullPointerException.class,
  96                                 "message",
  97                                 () -> ws.sendPing(null))
  98         );
  99         checkAndClose(
 100                 (ws) ->
 101                         TestKit.assertThrows(NullPointerException.class,
 102                                 "message",
 103                                 () -> ws.sendPong(null))
 104         );
 105         checkAndClose(
 106                 (ws) ->
 107                         TestKit.assertThrows(NullPointerException.class,
 108                                 "message",
 109                                 () -> ws.sendText((ByteBuffer) null, true))
 110         );
 111         checkAndClose(
 112                 (ws) ->
 113                         TestKit.assertThrows(NullPointerException.class,
 114                                 "message",
 115                                 () -> ws.sendText((CharSequence) null, true))
 116         );
 117         checkAndClose(
 118                 (ws) ->
 119                         TestKit.assertThrows(NullPointerException.class,
 120                                 "message",
 121                                 () -> ws.sendText((CharSequence) null))
 122         );
 123         checkAndClose(
 124                 (ws) ->
 125                         TestKit.assertThrows(NullPointerException.class,
 126                                 "message",
 127                                 () -> ws.sendText((Stream<? extends CharSequence>) null))
 128         );
 129         checkAndClose(
 130                 (ws) ->
 131                         TestKit.assertThrows(NullPointerException.class,
 132                                 "code",
 133                                 () -> ws.sendClose(null, ""))
 134         );
 135         checkAndClose(
 136                 (ws) ->
 137                         TestKit.assertNotThrows(
 138                                 () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, ""))
 139         );
 140         checkAndClose(
 141                 (ws) ->
 142                         TestKit.assertThrows(NullPointerException.class,
 143                                 "reason",
 144                                 () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, null))
 145         );
 146         checkAndClose(
 147                 (ws) ->
 148                         TestKit.assertThrows(NullPointerException.class,
 149                                 "code|reason",
 150                                 () -> ws.sendClose(null, null))
 151         );
 152     }
 153 
 154     @Test
 155     public void builder() {
 156         URI ws = URI.create("ws://localhost:9001");
 157         // FIXME: check all 24 cases:
 158         // {null, ws, wss, incorrect} x {null, HttpClient.getDefault(), custom} x {null, listener}
 159         //
 160         // if (any null) or (any incorrect)
 161         //     NPE or IAE is thrown
 162         // else
 163         //     builder is created
 164         TestKit.assertThrows(NullPointerException.class,
 165                 "uri",
 166                 () -> WebSocket.newBuilder(null, defaultListener())
 167         );
 168         TestKit.assertThrows(NullPointerException.class,
 169                 "listener",
 170                 () -> WebSocket.newBuilder(ws, null)
 171         );
 172         URI uri = URI.create("ftp://localhost:9001");
 173         TestKit.assertThrows(IllegalArgumentException.class,
 174                 "(?i).*\\buri\\b\\s+\\bscheme\\b.*",
 175                 () -> WebSocket.newBuilder(uri, defaultListener())
 176         );
 177         TestKit.assertNotThrows(
 178                 () -> WebSocket.newBuilder(ws, defaultListener())
 179         );
 180         URI uri1 = URI.create("wss://localhost:9001");
 181         TestKit.assertNotThrows(
 182                 () -> WebSocket.newBuilder(uri1, defaultListener())
 183         );
 184         URI uri2 = URI.create("wss://localhost:9001#a");
 185         TestKit.assertThrows(IllegalArgumentException.class,
 186                 "(?i).*\\bfragment\\b.*",
 187                 () -> WebSocket.newBuilder(uri2, HttpClient.getDefault(), defaultListener())
 188         );
 189         TestKit.assertThrows(NullPointerException.class,
 190                 "uri",
 191                 () -> WebSocket.newBuilder(null, HttpClient.getDefault(), defaultListener())
 192         );
 193         TestKit.assertThrows(NullPointerException.class,
 194                 "client",
 195                 () -> WebSocket.newBuilder(ws, null, defaultListener())
 196         );
 197         TestKit.assertThrows(NullPointerException.class,
 198                 "listener",
 199                 () -> WebSocket.newBuilder(ws, HttpClient.getDefault(), null)
 200         );
 201         // FIXME: check timeout works
 202         // (i.e. it directly influences the time WebSocket waits for connection + opening handshake)
 203         TestKit.assertNotThrows(
 204                 () -> WebSocket.newBuilder(ws, defaultListener()).connectTimeout(1, TimeUnit.SECONDS)
 205         );
 206         WebSocket.Builder builder = WebSocket.newBuilder(ws, defaultListener());
 207         TestKit.assertThrows(IllegalArgumentException.class,
 208                 "(?i).*\\bnegative\\b.*",
 209                 () -> builder.connectTimeout(-1, TimeUnit.SECONDS)
 210         );
 211         WebSocket.Builder builder1 = WebSocket.newBuilder(ws, defaultListener());
 212         TestKit.assertThrows(NullPointerException.class,
 213                 "unit",
 214                 () -> builder1.connectTimeout(1, null)
 215         );
 216         // FIXME: check these headers are actually received by the server
 217         TestKit.assertNotThrows(
 218                 () -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b")
 219         );
 220         TestKit.assertNotThrows(
 221                 () -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b").header("a", "b")
 222         );
 223         // FIXME: check all 18 cases:
 224         // {null, websocket(7), custom} x {null, custom}
 225         WebSocket.Builder builder2 = WebSocket.newBuilder(ws, defaultListener());
 226         TestKit.assertThrows(NullPointerException.class,
 227                 "name",
 228                 () -> builder2.header(null, "b")
 229         );
 230         WebSocket.Builder builder3 = WebSocket.newBuilder(ws, defaultListener());
 231         TestKit.assertThrows(NullPointerException.class,
 232                 "value",
 233                 () -> builder3.header("a", null)
 234         );
 235         WebSocket.Builder builder4 = WebSocket.newBuilder(ws, defaultListener());
 236         TestKit.assertThrows(IllegalArgumentException.class,
 237                 () -> builder4.header("Sec-WebSocket-Accept", "")
 238         );
 239         WebSocket.Builder builder5 = WebSocket.newBuilder(ws, defaultListener());
 240         TestKit.assertThrows(IllegalArgumentException.class,
 241                 () -> builder5.header("Sec-WebSocket-Extensions", "")
 242         );
 243         WebSocket.Builder builder6 = WebSocket.newBuilder(ws, defaultListener());
 244         TestKit.assertThrows(IllegalArgumentException.class,
 245                 () -> builder6.header("Sec-WebSocket-Key", "")
 246         );
 247         WebSocket.Builder builder7 = WebSocket.newBuilder(ws, defaultListener());
 248         TestKit.assertThrows(IllegalArgumentException.class,
 249                 () -> builder7.header("Sec-WebSocket-Protocol", "")
 250         );
 251         WebSocket.Builder builder8 = WebSocket.newBuilder(ws, defaultListener());
 252         TestKit.assertThrows(IllegalArgumentException.class,
 253                 () -> builder8.header("Sec-WebSocket-Version", "")
 254         );
 255         WebSocket.Builder builder9 = WebSocket.newBuilder(ws, defaultListener());
 256         TestKit.assertThrows(IllegalArgumentException.class,
 257                 () -> builder9.header("Connection", "")
 258         );
 259         WebSocket.Builder builder10 = WebSocket.newBuilder(ws, defaultListener());
 260         TestKit.assertThrows(IllegalArgumentException.class,
 261                 () -> builder10.header("Upgrade", "")
 262         );
 263         // FIXME: check 3 cases (1 arg):
 264         // {null, incorrect, custom}
 265         // FIXME: check 12 cases (2 args):
 266         // {null, incorrect, custom} x {(String) null, (String[]) null, incorrect, custom}
 267         // FIXME: check 27 cases (3 args) (the interesting part in null inside var-arg):
 268         // {null, incorrect, custom}^3
 269         // FIXME: check the server receives them in the order listed
 270         TestKit.assertThrows(NullPointerException.class,
 271                 "mostPreferred",
 272                 () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols(null)
 273         );
 274         TestKit.assertThrows(NullPointerException.class,
 275                 "lesserPreferred",
 276                 () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null)
 277         );
 278         TestKit.assertThrows(NullPointerException.class,
 279                 "lesserPreferred\\[0\\]",
 280                 () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null, "b")
 281         );
 282         TestKit.assertThrows(NullPointerException.class,
 283                 "lesserPreferred\\[1\\]",
 284                 () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", null)
 285         );
 286         TestKit.assertNotThrows(
 287                 () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a")
 288         );
 289         TestKit.assertNotThrows(
 290                 () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", "c")
 291         );
 292         WebSocket.Builder builder11 = WebSocket.newBuilder(ws, defaultListener());
 293         TestKit.assertThrows(IllegalArgumentException.class,
 294                 () -> builder11.subprotocols("")
 295         );
 296         WebSocket.Builder builder12 = WebSocket.newBuilder(ws, defaultListener());
 297         TestKit.assertThrows(IllegalArgumentException.class,
 298                 () -> builder12.subprotocols("a", "a")
 299         );
 300         WebSocket.Builder builder13 = WebSocket.newBuilder(ws, defaultListener());
 301         TestKit.assertThrows(IllegalArgumentException.class,
 302                 () -> builder13.subprotocols("a" + ((char) 0x7f))
 303         );
 304     }
 305 
 306     private static WebSocket.Listener defaultListener() {
 307         return new WebSocket.Listener() { };
 308     }
 309 
 310     //
 311     // Automatically closes everything after the check has been performed
 312     //
 313     private static void checkAndClose(Consumer<? super WebSocket> c) {
 314         HandshakePhase HandshakePhase
 315                 = new HandshakePhase(new InetSocketAddress("127.0.0.1", 0));
 316         URI serverURI = HandshakePhase.getURI();
 317         CompletableFuture<SocketChannel> cfc = HandshakePhase.afterHandshake();
 318         WebSocket.Builder b = WebSocket.newBuilder(serverURI, defaultListener());
 319         CompletableFuture<WebSocket> cfw = b.buildAsync();
 320 
 321         try {
 322             WebSocket ws;
 323             try {
 324                 ws = cfw.get();
 325             } catch (Exception e) {
 326                 throw new RuntimeException(e);
 327             }
 328             c.accept(ws);
 329         } finally {
 330             try {
 331                 SocketChannel now = cfc.getNow(null);
 332                 if (now != null) {
 333                     now.close();
 334                 }
 335             } catch (Throwable ignored) { }
 336         }
 337     }
 338 }
< prev index next >