|
| 1 | +package org.java_websocket.server; |
| 2 | + |
| 3 | +import org.java_websocket.WebSocket; |
| 4 | +import org.java_websocket.WebSocketAdapter; |
| 5 | +import org.java_websocket.WebSocketImpl; |
| 6 | +import org.java_websocket.drafts.Draft; |
| 7 | +import org.java_websocket.drafts.Draft_6455; |
| 8 | +import org.java_websocket.handshake.Handshakedata; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import javax.net.ssl.SSLContext; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.InetSocketAddress; |
| 14 | +import java.nio.ByteBuffer; |
| 15 | +import java.nio.channels.ByteChannel; |
| 16 | +import java.nio.channels.NotYetConnectedException; |
| 17 | +import java.nio.channels.SocketChannel; |
| 18 | +import java.security.NoSuchAlgorithmException; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertNotNull; |
| 23 | +import static org.junit.Assert.fail; |
| 24 | + |
| 25 | +public class CustomSSLWebSocketServerFactoryTest { |
| 26 | + |
| 27 | + final String[] emptyArray = new String[0]; |
| 28 | + @Test |
| 29 | + public void testConstructor() throws NoSuchAlgorithmException { |
| 30 | + try { |
| 31 | + new CustomSSLWebSocketServerFactory(null, null, null); |
| 32 | + fail("IllegalArgumentException should be thrown"); |
| 33 | + } catch (IllegalArgumentException e) { |
| 34 | + // Good |
| 35 | + } |
| 36 | + try { |
| 37 | + new CustomSSLWebSocketServerFactory(null, null, null, null); |
| 38 | + fail("IllegalArgumentException should be thrown"); |
| 39 | + } catch (IllegalArgumentException e) { |
| 40 | + // Good |
| 41 | + } |
| 42 | + try { |
| 43 | + new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null, null); |
| 44 | + fail("IllegalArgumentException should be thrown"); |
| 45 | + } catch (IllegalArgumentException e) { |
| 46 | + } |
| 47 | + try { |
| 48 | + new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null); |
| 49 | + } catch (IllegalArgumentException e) { |
| 50 | + fail("IllegalArgumentException should not be thrown"); |
| 51 | + } |
| 52 | + try { |
| 53 | + new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), null, null); |
| 54 | + } catch (IllegalArgumentException e) { |
| 55 | + fail("IllegalArgumentException should not be thrown"); |
| 56 | + } |
| 57 | + try { |
| 58 | + new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), emptyArray, null); |
| 59 | + } catch (IllegalArgumentException e) { |
| 60 | + fail("IllegalArgumentException should not be thrown"); |
| 61 | + } |
| 62 | + try { |
| 63 | + new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), null, emptyArray); |
| 64 | + } catch (IllegalArgumentException e) { |
| 65 | + fail("IllegalArgumentException should not be thrown"); |
| 66 | + } |
| 67 | + try { |
| 68 | + new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), Executors.newCachedThreadPool(), emptyArray, emptyArray); |
| 69 | + } catch (IllegalArgumentException e) { |
| 70 | + fail("IllegalArgumentException should not be thrown"); |
| 71 | + } |
| 72 | + } |
| 73 | + @Test |
| 74 | + public void testCreateWebSocket() throws NoSuchAlgorithmException { |
| 75 | + CustomSSLWebSocketServerFactory webSocketServerFactory = new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null); |
| 76 | + CustomWebSocketAdapter webSocketAdapter = new CustomWebSocketAdapter(); |
| 77 | + WebSocketImpl webSocketImpl = webSocketServerFactory.createWebSocket(webSocketAdapter, new Draft_6455()); |
| 78 | + assertNotNull("webSocketImpl != null", webSocketImpl); |
| 79 | + webSocketImpl = webSocketServerFactory.createWebSocket(webSocketAdapter, Collections.<Draft>singletonList(new Draft_6455())); |
| 80 | + assertNotNull("webSocketImpl != null", webSocketImpl); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testWrapChannel() throws IOException, NoSuchAlgorithmException { |
| 85 | + CustomSSLWebSocketServerFactory webSocketServerFactory = new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), null, null); |
| 86 | + SocketChannel channel = SocketChannel.open(); |
| 87 | + try { |
| 88 | + ByteChannel result = webSocketServerFactory.wrapChannel(channel, null); |
| 89 | + } catch (NotYetConnectedException e) { |
| 90 | + //We do not really connect |
| 91 | + } |
| 92 | + channel.close(); |
| 93 | + webSocketServerFactory = new CustomSSLWebSocketServerFactory(SSLContext.getDefault(), new String[]{"TLSv1.2"}, new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"}); |
| 94 | + channel = SocketChannel.open(); |
| 95 | + try { |
| 96 | + ByteChannel result = webSocketServerFactory.wrapChannel(channel, null); |
| 97 | + } catch (NotYetConnectedException e) { |
| 98 | + //We do not really connect |
| 99 | + } |
| 100 | + channel.close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testClose() { |
| 105 | + DefaultWebSocketServerFactory webSocketServerFactory = new DefaultWebSocketServerFactory(); |
| 106 | + webSocketServerFactory.close(); |
| 107 | + } |
| 108 | + |
| 109 | + private static class CustomWebSocketAdapter extends WebSocketAdapter { |
| 110 | + @Override |
| 111 | + public void onWebsocketMessage(WebSocket conn, String message) { |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onWebsocketMessage(WebSocket conn, ByteBuffer blob) { |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onWebsocketOpen(WebSocket conn, Handshakedata d) { |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void onWebsocketClose(WebSocket ws, int code, String reason, boolean remote) { |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onWebsocketClosing(WebSocket ws, int code, String reason, boolean remote) { |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onWebsocketCloseInitiated(WebSocket ws, int code, String reason) { |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void onWebsocketError(WebSocket conn, Exception ex) { |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void onWriteDemand(WebSocket conn) { |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public InetSocketAddress getLocalSocketAddress(WebSocket conn) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public InetSocketAddress getRemoteSocketAddress(WebSocket conn) { |
| 157 | + return null; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments