|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2019 Nathan Rajlich |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +package org.java_websocket.server; |
| 28 | + |
| 29 | +import org.java_websocket.WebSocket; |
| 30 | +import org.java_websocket.drafts.Draft; |
| 31 | +import org.java_websocket.drafts.Draft_6455; |
| 32 | +import org.java_websocket.handshake.ClientHandshake; |
| 33 | +import org.java_websocket.util.SocketUtil; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.net.InetAddress; |
| 38 | +import java.net.InetSocketAddress; |
| 39 | +import java.nio.ByteBuffer; |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.Collections; |
| 42 | +import java.util.HashSet; |
| 43 | +import java.util.List; |
| 44 | +import java.util.concurrent.CountDownLatch; |
| 45 | + |
| 46 | +import static org.junit.Assert.*; |
| 47 | + |
| 48 | +public class WebSocketServerTest { |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testConstructor() { |
| 52 | + List<Draft> draftCollection = Collections.<Draft>singletonList(new Draft_6455()); |
| 53 | + Collection<WebSocket> webSocketCollection = new HashSet<WebSocket>(); |
| 54 | + InetSocketAddress inetAddress = new InetSocketAddress(1337); |
| 55 | + |
| 56 | + try { |
| 57 | + WebSocketServer server = new MyWebSocketServer(null, 1,draftCollection, webSocketCollection ); |
| 58 | + fail("Should fail"); |
| 59 | + } catch (IllegalArgumentException e) { |
| 60 | + //OK |
| 61 | + } |
| 62 | + try { |
| 63 | + WebSocketServer server = new MyWebSocketServer(inetAddress, 0,draftCollection, webSocketCollection ); |
| 64 | + fail("Should fail"); |
| 65 | + } catch (IllegalArgumentException e) { |
| 66 | + //OK |
| 67 | + } |
| 68 | + try { |
| 69 | + WebSocketServer server = new MyWebSocketServer(inetAddress, -1,draftCollection, webSocketCollection ); |
| 70 | + fail("Should fail"); |
| 71 | + } catch (IllegalArgumentException e) { |
| 72 | + //OK |
| 73 | + } |
| 74 | + try { |
| 75 | + WebSocketServer server = new MyWebSocketServer(inetAddress, Integer.MIN_VALUE, draftCollection, webSocketCollection ); |
| 76 | + fail("Should fail"); |
| 77 | + } catch (IllegalArgumentException e) { |
| 78 | + //OK |
| 79 | + } |
| 80 | + try { |
| 81 | + WebSocketServer server = new MyWebSocketServer(inetAddress, Integer.MIN_VALUE, draftCollection, webSocketCollection ); |
| 82 | + fail("Should fail"); |
| 83 | + } catch (IllegalArgumentException e) { |
| 84 | + //OK |
| 85 | + } |
| 86 | + try { |
| 87 | + WebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, null ); |
| 88 | + fail("Should fail"); |
| 89 | + } catch (IllegalArgumentException e) { |
| 90 | + //OK |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + WebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, webSocketCollection ); |
| 95 | + // OK |
| 96 | + } catch (IllegalArgumentException e) { |
| 97 | + fail("Should not fail"); |
| 98 | + } |
| 99 | + try { |
| 100 | + WebSocketServer server = new MyWebSocketServer(inetAddress, 1, null, webSocketCollection ); |
| 101 | + // OK |
| 102 | + } catch (IllegalArgumentException e) { |
| 103 | + fail("Should not fail"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testGetAddress() throws IOException { |
| 110 | + int port = SocketUtil.getAvailablePort(); |
| 111 | + InetSocketAddress inetSocketAddress = new InetSocketAddress(port); |
| 112 | + MyWebSocketServer server = new MyWebSocketServer(port); |
| 113 | + assertEquals(inetSocketAddress, server.getAddress()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testGetDrafts() { |
| 118 | + List<Draft> draftCollection = Collections.<Draft>singletonList(new Draft_6455()); |
| 119 | + Collection<WebSocket> webSocketCollection = new HashSet<WebSocket>(); |
| 120 | + InetSocketAddress inetAddress = new InetSocketAddress(1337); |
| 121 | + MyWebSocketServer server = new MyWebSocketServer(inetAddress, 1, draftCollection, webSocketCollection); |
| 122 | + assertEquals(1, server.getDraft().size()); |
| 123 | + assertEquals(draftCollection.get(0), server.getDraft().get(0)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetPort() throws IOException, InterruptedException { |
| 128 | + int port = SocketUtil.getAvailablePort(); |
| 129 | + CountDownLatch countServerDownLatch = new CountDownLatch( 1 ); |
| 130 | + MyWebSocketServer server = new MyWebSocketServer(port); |
| 131 | + assertEquals(port, server.getPort()); |
| 132 | + server = new MyWebSocketServer(0, countServerDownLatch); |
| 133 | + assertEquals(0, server.getPort()); |
| 134 | + server.start(); |
| 135 | + countServerDownLatch.await(); |
| 136 | + assertNotEquals(0, server.getPort()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testBroadcast() { |
| 141 | + MyWebSocketServer server = new MyWebSocketServer(1337); |
| 142 | + try { |
| 143 | + server.broadcast((byte[]) null, Collections.<WebSocket>emptyList()); |
| 144 | + fail("Should fail"); |
| 145 | + } catch (IllegalArgumentException e) { |
| 146 | + // OK |
| 147 | + } |
| 148 | + try { |
| 149 | + server.broadcast((ByteBuffer) null, Collections.<WebSocket>emptyList()); |
| 150 | + fail("Should fail"); |
| 151 | + } catch (IllegalArgumentException e) { |
| 152 | + // OK |
| 153 | + } |
| 154 | + try { |
| 155 | + server.broadcast((String) null, Collections.<WebSocket>emptyList()); |
| 156 | + fail("Should fail"); |
| 157 | + } catch (IllegalArgumentException e) { |
| 158 | + // OK |
| 159 | + } |
| 160 | + try { |
| 161 | + server.broadcast(new byte[] {(byte) 0xD0}, null); |
| 162 | + fail("Should fail"); |
| 163 | + } catch (IllegalArgumentException e) { |
| 164 | + // OK |
| 165 | + } |
| 166 | + try { |
| 167 | + server.broadcast(ByteBuffer.wrap(new byte[] {(byte) 0xD0}), null); |
| 168 | + fail("Should fail"); |
| 169 | + } catch (IllegalArgumentException e) { |
| 170 | + // OK |
| 171 | + } |
| 172 | + try { |
| 173 | + server.broadcast("", null); |
| 174 | + fail("Should fail"); |
| 175 | + } catch (IllegalArgumentException e) { |
| 176 | + // OK |
| 177 | + } |
| 178 | + try { |
| 179 | + server.broadcast("", Collections.<WebSocket>emptyList()); |
| 180 | + // OK |
| 181 | + } catch (IllegalArgumentException e) { |
| 182 | + fail("Should not fail"); |
| 183 | + } |
| 184 | + } |
| 185 | + private static class MyWebSocketServer extends WebSocketServer { |
| 186 | + private CountDownLatch serverLatch = null; |
| 187 | + |
| 188 | + public MyWebSocketServer(InetSocketAddress address , int decodercount , List<Draft> drafts , Collection<WebSocket> connectionscontainer) { |
| 189 | + super(address, decodercount, drafts, connectionscontainer); |
| 190 | + } |
| 191 | + public MyWebSocketServer(int port, CountDownLatch serverLatch) { |
| 192 | + super(new InetSocketAddress(port)); |
| 193 | + this.serverLatch = serverLatch; |
| 194 | + } |
| 195 | + public MyWebSocketServer(int port) { |
| 196 | + this(port, null); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public void onMessage(WebSocket conn, String message) { |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void onError(WebSocket conn, Exception ex) { |
| 214 | + ex.printStackTrace(); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public void onStart() { |
| 219 | + if (serverLatch != null) { |
| 220 | + serverLatch.countDown(); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
| 225 | + |
0 commit comments