Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2012-07-31 15:52:22 -0400
committerJoakim Erdfelt2012-07-31 15:52:22 -0400
commit9a568514a6c3790bf8cef345532bd3e738736295 (patch)
tree0816c444fc49ed5314644c533f7887ba08fc2377 /jetty-websocket
parent9a34c08b8e53988c708e255412e94bd8c8e9378e (diff)
downloadorg.eclipse.jetty.project-9a568514a6c3790bf8cef345532bd3e738736295.tar.gz
org.eclipse.jetty.project-9a568514a6c3790bf8cef345532bd3e738736295.tar.xz
org.eclipse.jetty.project-9a568514a6c3790bf8cef345532bd3e738736295.zip
More AB testing, reworked for Fuzzer
Diffstat (limited to 'jetty-websocket')
-rw-r--r--jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java62
-rw-r--r--jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java3
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ByteBufferAssert.java5
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java10
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AllTests.java2
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/Fuzzer.java172
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java514
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase2.java554
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase3.java216
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java40
-rw-r--r--jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties6
11 files changed, 1041 insertions, 543 deletions
diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java
index 1d6b44597b..76ef7f4d09 100644
--- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java
+++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java
@@ -54,16 +54,19 @@ public class Parser
private ByteBuffer payload;
private int payloadLength;
+ /** Is there an extension using RSV1 */
+ private boolean rsv1InUse = false;
+ /** Is there an extension using RSV2 */
+ private boolean rsv2InUse = false;
+ /** Is there an extension using RSV3 */
+ private boolean rsv3InUse = false;
+
private static final Logger LOG = Log.getLogger(Parser.class);
private IncomingFrames incomingFramesHandler;
private WebSocketPolicy policy;
public Parser(WebSocketPolicy wspolicy)
{
- /*
- * TODO: Investigate addition of decompression factory similar to SPDY work in situation of negotiated deflate extension?
- */
-
this.policy = wspolicy;
}
@@ -107,6 +110,21 @@ public class Parser
return policy;
}
+ public boolean isRsv1InUse()
+ {
+ return rsv1InUse;
+ }
+
+ public boolean isRsv2InUse()
+ {
+ return rsv2InUse;
+ }
+
+ public boolean isRsv3InUse()
+ {
+ return rsv3InUse;
+ }
+
protected void notifyFrame(final WebSocketFrame f)
{
if (LOG_FRAMES.isDebugEnabled())
@@ -228,6 +246,27 @@ public class Parser
LOG.debug("OpCode {}, fin={}",opcode.name(),fin);
+ /*
+ * RFC 6455 Section 5.2
+ *
+ * MUST be 0 unless an extension is negotiated that defines meanings for non-zero values. If a nonzero value is received and none of the
+ * negotiated extensions defines the meaning of such a nonzero value, the receiving endpoint MUST _Fail the WebSocket Connection_.
+ */
+ if (!rsv1InUse && rsv1)
+ {
+ throw new ProtocolException("RSV1 not allowed to be set");
+ }
+
+ if (!rsv2InUse && rsv2)
+ {
+ throw new ProtocolException("RSV2 not allowed to be set");
+ }
+
+ if (!rsv3InUse && rsv3)
+ {
+ throw new ProtocolException("RSV3 not allowed to be set");
+ }
+
if (opcode.isControlFrame() && !fin)
{
throw new ProtocolException("Fragmented Control Frame [" + opcode.name() + "]");
@@ -442,6 +481,21 @@ public class Parser
this.incomingFramesHandler = incoming;
}
+ public void setRsv1InUse(boolean rsv1InUse)
+ {
+ this.rsv1InUse = rsv1InUse;
+ }
+
+ public void setRsv2InUse(boolean rsv2InUse)
+ {
+ this.rsv2InUse = rsv2InUse;
+ }
+
+ public void setRsv3InUse(boolean rsv3InUse)
+ {
+ this.rsv3InUse = rsv3InUse;
+ }
+
@Override
public String toString()
{
diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
index f2e8123204..ab14da7428 100644
--- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
+++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
@@ -388,14 +388,17 @@ public class WebSocketServerFactory extends AbstractLifeCycle implements WebSock
if (ext.useRsv1())
{
connection.getGenerator().setRsv1InUse(true);
+ connection.getParser().setRsv1InUse(true);
}
if (ext.useRsv2())
{
connection.getGenerator().setRsv2InUse(true);
+ connection.getParser().setRsv2InUse(true);
}
if (ext.useRsv3())
{
connection.getGenerator().setRsv3InUse(true);
+ connection.getParser().setRsv3InUse(true);
}
}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ByteBufferAssert.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ByteBufferAssert.java
index b6afbcf631..6f170aea76 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ByteBufferAssert.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ByteBufferAssert.java
@@ -42,6 +42,11 @@ public class ByteBufferAssert
public static void assertEquals(String message, ByteBuffer expectedBuffer, ByteBuffer actualBuffer)
{
+ if (expectedBuffer == null)
+ {
+ Assert.assertThat(message,actualBuffer,nullValue());
+ return;
+ }
byte expectedBytes[] = BufferUtil.toArray(expectedBuffer);
byte actualBytes[] = BufferUtil.toArray(actualBuffer);
assertEquals(message,expectedBytes,actualBytes);
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java
index bba917bfc9..1ce3ca4613 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AbstractABCase.java
@@ -44,6 +44,16 @@ public abstract class AbstractABCase
server.stop();
}
+ public Generator getLaxGenerator()
+ {
+ return laxGenerator;
+ }
+
+ public SimpleServletServer getServer()
+ {
+ return server;
+ }
+
protected byte[] masked(final byte[] data)
{
int len = data.length;
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AllTests.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AllTests.java
index 360af21c9e..8f68c1355e 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AllTests.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/AllTests.java
@@ -5,7 +5,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses(
-{ TestABCase1.class, TestABCase5.class, TestABCase7_9.class })
+{ TestABCase1.class, TestABCase2.class, TestABCase3.class, TestABCase5.class, TestABCase7_9.class })
public class AllTests
{
/* let junit do the rest */
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/Fuzzer.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/Fuzzer.java
new file mode 100644
index 0000000000..b44ac988ac
--- /dev/null
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/Fuzzer.java
@@ -0,0 +1,172 @@
+package org.eclipse.jetty.websocket.server.ab;
+
+import static org.hamcrest.Matchers.*;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.eclipse.jetty.util.BufferUtil;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
+import org.eclipse.jetty.websocket.protocol.CloseInfo;
+import org.eclipse.jetty.websocket.protocol.Generator;
+import org.eclipse.jetty.websocket.protocol.OpCode;
+import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
+import org.eclipse.jetty.websocket.server.ByteBufferAssert;
+import org.eclipse.jetty.websocket.server.blockhead.BlockheadClient;
+import org.eclipse.jetty.websocket.server.helper.IncomingFramesCapture;
+import org.junit.Assert;
+
+/**
+ * Fuzzing utility for the AB tests.
+ */
+public class Fuzzer
+{
+ public static enum SendMode
+ {
+ BULK,
+ PER_FRAME,
+ SLOW
+ }
+
+ private static final Logger LOG = Log.getLogger(Fuzzer.class);
+
+ // Client side framing mask
+ protected static final byte[] MASK =
+ { 0x11, 0x22, 0x33, 0x44 };
+
+ private final BlockheadClient client;
+ private final Generator generator;
+ private SendMode sendMode = SendMode.BULK;
+ private int slowSendSegmentSize = 5;
+
+ public Fuzzer(AbstractABCase testcase) throws Exception
+ {
+ this.client = new BlockheadClient(testcase.getServer().getServerUri());
+ this.generator = testcase.getLaxGenerator();
+ }
+
+ public void close()
+ {
+ this.client.disconnect();
+ }
+
+ public void connect() throws IOException
+ {
+ if (!client.isConnected())
+ {
+ client.connect();
+ client.sendStandardRequest();
+ client.expectUpgradeResponse();
+ }
+ }
+
+ public void expect(List<WebSocketFrame> expect) throws IOException, TimeoutException
+ {
+ int expectedCount = expect.size();
+
+ // Read frames
+ IncomingFramesCapture capture = client.readFrames(expect.size(),TimeUnit.MILLISECONDS,500);
+
+ String prefix = "";
+ for (int i = 0; i < expectedCount; i++)
+ {
+ WebSocketFrame expected = expect.get(i);
+ WebSocketFrame actual = capture.getFrames().pop();
+
+ prefix = "Frame[" + i + "]";
+
+ Assert.assertThat(prefix + ".opcode",actual.getOpCode(),is(expected.getOpCode()));
+ prefix += "/" + actual.getOpCode();
+ if (expected.getOpCode() == OpCode.CLOSE)
+ {
+ CloseInfo expectedClose = new CloseInfo(expected);
+ CloseInfo actualClose = new CloseInfo(actual);
+ Assert.assertThat(prefix + ".statusCode",actualClose.getStatusCode(),is(expectedClose.getStatusCode()));
+ }
+ else
+ {
+ Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.getPayloadLength()));
+ ByteBufferAssert.assertEquals(prefix + ".payload",expected.getPayload(),actual.getPayload());
+ }
+ }
+ }
+
+ public void expect(WebSocketFrame expect) throws IOException, TimeoutException
+ {
+ expect(Collections.singletonList(expect));
+ }
+
+ public SendMode getSendMode()
+ {
+ return sendMode;
+ }
+
+ public int getSlowSendSegmentSize()
+ {
+ return slowSendSegmentSize;
+ }
+
+ public void send(List<WebSocketFrame> send) throws IOException
+ {
+ Assert.assertThat("Client connected",client.isConnected(),is(true));
+ LOG.debug("Sending {} frames (mode {})",send.size(),sendMode);
+ if ((sendMode == SendMode.BULK) || (sendMode == SendMode.SLOW))
+ {
+ int buflen = 0;
+ for (WebSocketFrame f : send)
+ {
+ buflen += f.getPayloadLength() + Generator.OVERHEAD;
+ }
+ ByteBuffer buf = ByteBuffer.allocate(buflen);
+ BufferUtil.clearToFill(buf);
+
+ // Generate frames
+ for (WebSocketFrame f : send)
+ {
+ f.setMask(MASK); // make sure we have mask set
+ BufferUtil.put(generator.generate(f),buf);
+ }
+ BufferUtil.flipToFlush(buf,0);
+
+ // Write Data Frame
+ switch (sendMode)
+ {
+ case BULK:
+ client.writeRaw(buf);
+ break;
+ case SLOW:
+ client.writeRawSlowly(buf,slowSendSegmentSize);
+ break;
+ }
+ }
+ else if (sendMode == SendMode.PER_FRAME)
+ {
+ for (WebSocketFrame f : send)
+ {
+ // Using lax generator, generate and send
+ client.writeRaw(generator.generate(f));
+ client.flush();
+ }
+ }
+ }
+
+ public void send(WebSocketFrame send) throws IOException
+ {
+ send(Collections.singletonList(send));
+ }
+
+ public void setSendMode(SendMode sendMode)
+ {
+ this.sendMode = sendMode;
+ }
+
+ public void setSlowSendSegmentSize(int segmentSize)
+ {
+ this.slowSendSegmentSize = segmentSize;
+ }
+}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java
index 5284068e40..90ed4497ff 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase1.java
@@ -15,204 +15,47 @@
//========================================================================
package org.eclipse.jetty.websocket.server.ab;
-import static org.hamcrest.Matchers.*;
-
-import java.nio.ByteBuffer;
+import java.util.ArrayList;
import java.util.Arrays;
-import java.util.concurrent.TimeUnit;
+import java.util.List;
-import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.protocol.CloseInfo;
-import org.eclipse.jetty.websocket.protocol.Generator;
-import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
-import org.eclipse.jetty.websocket.server.ByteBufferAssert;
-import org.eclipse.jetty.websocket.server.blockhead.BlockheadClient;
-import org.eclipse.jetty.websocket.server.helper.IncomingFramesCapture;
-import org.junit.Assert;
+import org.eclipse.jetty.websocket.server.ab.Fuzzer.SendMode;
import org.junit.Test;
public class TestABCase1 extends AbstractABCase
{
- private void assertEchoEmptyFrame(OpCode opcode) throws Exception
- {
- BlockheadClient client = new BlockheadClient(server.getServerUri());
- try
- {
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD);
- BufferUtil.clearToFill(buf);
-
- // Prepare Frame
- buf.put((byte)(0x00 | FIN | opcode.getCode()));
- putPayloadLength(buf,0);
- putMask(buf);
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
-
- // Prepare Close Frame
- CloseInfo close = new CloseInfo(StatusCode.NORMAL);
- buf = strictGenerator.generate(close.asFrame());
-
- // Write Close Frame
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,500);
-
- // Validate echo'd frame
- WebSocketFrame frame = capture.getFrames().get(0);
- Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode));
- Assert.assertThat(opcode + ".payloadLength",frame.getPayloadLength(),is(0));
-
- // Validate close
- frame = capture.getFrames().get(1);
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
- }
- finally
- {
- client.disconnect();
- }
- }
-
- private void assertEchoFrame(OpCode opcode, byte[] payload) throws Exception
+ /**
+ * Echo 0 byte TEXT message
+ */
+ @Test
+ public void testCase1_1_1() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
- try
- {
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(payload.length + Generator.OVERHEAD);
- BufferUtil.clearToFill(buf);
-
- // Prepare Frame
- buf.put((byte)(0x00 | FIN | opcode.getCode()));
- putPayloadLength(buf,payload.length);
- putMask(buf);
- buf.put(masked(payload));
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text());
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text());
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- // Prepare Close Frame
- CloseInfo close = new CloseInfo(StatusCode.NORMAL);
- WebSocketFrame closeFrame = close.asFrame();
- closeFrame.setMask(MASK);
- buf = strictGenerator.generate(closeFrame);
-
- // Write Close Frame
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,1000);
-
- // Validate echo'd frame
- WebSocketFrame frame = capture.getFrames().get(0);
- Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode));
- Assert.assertThat(opcode + ".payloadLength",frame.getPayloadLength(),is(payload.length));
- ByteBufferAssert.assertEquals(opcode + ".payload",payload,frame.getPayload());
-
- // Validate close
- frame = capture.getFrames().get(1);
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
- }
- finally
- {
- client.disconnect();
- }
- }
-
- private void assertEchoSegmentedFrame(OpCode opcode, byte payload[], int segmentSize) throws Exception
- {
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(payload.length + Generator.OVERHEAD);
- BufferUtil.clearToFill(buf);
-
- // Prepare Frame
- buf.put((byte)(0x00 | FIN | opcode.getCode()));
- putPayloadLength(buf,payload.length);
- putMask(buf);
- buf.put(masked(payload));
-
- // Write frame, in small blocks of segmentSize
- BufferUtil.flipToFlush(buf,0);
- int origLimit = buf.limit();
- int limit = buf.limit();
- int len;
- int pos = buf.position();
- int overallLeft = buf.remaining();
- while (overallLeft > 0)
- {
- buf.position(pos);
- limit = Math.min(origLimit,pos + segmentSize);
- buf.limit(limit);
- len = buf.remaining();
- overallLeft -= len;
- pos += len;
- client.writeRaw(buf);
- client.flush();
- }
-
- // Prepare Close Frame
- CloseInfo close = new CloseInfo(StatusCode.NORMAL);
- buf = strictGenerator.generate(close.asFrame());
-
- // Write Close Frame
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,500);
-
- // Validate echo'd frame
- WebSocketFrame frame = capture.getFrames().get(0);
- Assert.assertThat("frame should be " + opcode + " frame",frame.getOpCode(),is(opcode));
- Assert.assertThat(opcode + ".payloadLength",frame.getPayloadLength(),is(payload.length));
- ByteBufferAssert.assertEquals(opcode + ".payload",payload,frame.getPayload());
-
- // Validate close
- frame = capture.getFrames().get(1);
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.disconnect();
+ fuzzer.close();
}
}
/**
- * Echo 0 byte TEXT message
- */
- @Test
- public void testCase1_1_1() throws Exception
- {
- assertEchoEmptyFrame(OpCode.TEXT);
- }
-
- /**
* Echo 125 byte TEXT message (uses small 7-bit payload length)
*/
@Test
@@ -221,7 +64,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[125];
Arrays.fill(payload,(byte)'*');
- assertEchoFrame(OpCode.TEXT,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -233,7 +95,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[126];
Arrays.fill(payload,(byte)'*');
- assertEchoFrame(OpCode.TEXT,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -245,7 +126,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[127];
Arrays.fill(payload,(byte)'*');
- assertEchoFrame(OpCode.TEXT,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -257,7 +157,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[128];
Arrays.fill(payload,(byte)'*');
- assertEchoFrame(OpCode.TEXT,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -269,7 +188,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[65535];
Arrays.fill(payload,(byte)'*');
- assertEchoFrame(OpCode.TEXT,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -281,7 +219,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[65536];
Arrays.fill(payload,(byte)'*');
- assertEchoFrame(OpCode.TEXT,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -298,7 +255,27 @@ public class TestABCase1 extends AbstractABCase
Arrays.fill(payload,(byte)'*');
int segmentSize = 997;
- assertEchoSegmentedFrame(OpCode.TEXT,payload,segmentSize);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(segmentSize);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -307,7 +284,26 @@ public class TestABCase1 extends AbstractABCase
@Test
public void testCase1_2_1() throws Exception
{
- assertEchoEmptyFrame(OpCode.BINARY);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary());
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary());
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -319,7 +315,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[125];
Arrays.fill(payload,(byte)0xFE);
- assertEchoFrame(OpCode.BINARY,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -331,7 +346,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[126];
Arrays.fill(payload,(byte)0xFE);
- assertEchoFrame(OpCode.BINARY,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -343,7 +377,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[127];
Arrays.fill(payload,(byte)0xFE);
- assertEchoFrame(OpCode.BINARY,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -355,7 +408,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[128];
Arrays.fill(payload,(byte)0xFE);
- assertEchoFrame(OpCode.BINARY,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -367,7 +439,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[65535];
Arrays.fill(payload,(byte)0xFE);
- assertEchoFrame(OpCode.BINARY,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -379,7 +470,26 @@ public class TestABCase1 extends AbstractABCase
byte payload[] = new byte[65536];
Arrays.fill(payload,(byte)0xFE);
- assertEchoFrame(OpCode.BINARY,payload);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -396,6 +506,26 @@ public class TestABCase1 extends AbstractABCase
Arrays.fill(payload,(byte)0xFE);
int segmentSize = 997;
- assertEchoSegmentedFrame(OpCode.BINARY,payload,segmentSize);
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.binary().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(segmentSize);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase2.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase2.java
index 98d9a9b528..00b3f1f223 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase2.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase2.java
@@ -1,227 +1,43 @@
package org.eclipse.jetty.websocket.server.ab;
-import static org.hamcrest.Matchers.*;
-
-import java.nio.ByteBuffer;
+import java.util.ArrayList;
import java.util.Arrays;
-import java.util.concurrent.TimeUnit;
+import java.util.List;
-import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.protocol.CloseInfo;
-import org.eclipse.jetty.websocket.protocol.Generator;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
-import org.eclipse.jetty.websocket.server.ByteBufferAssert;
-import org.eclipse.jetty.websocket.server.blockhead.BlockheadClient;
-import org.eclipse.jetty.websocket.server.helper.IncomingFramesCapture;
-import org.junit.Assert;
import org.junit.Test;
public class TestABCase2 extends AbstractABCase
{
- private void assertPingFrame(byte[] payload) throws Exception
- {
- boolean hasPayload = ((payload != null) && (payload.length > 0));
-
- BlockheadClient client = new BlockheadClient(server.getServerUri());
- try
- {
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- int len = 0;
- if (hasPayload)
- {
- len = payload.length;
- }
-
- ByteBuffer buf = ByteBuffer.allocate(len + Generator.OVERHEAD);
- BufferUtil.clearToFill(buf);
-
- // Prepare PING Frame
- buf.put((byte)(0x00 | FIN | OpCode.PING.getCode()));
- putPayloadLength(buf,len);
- putMask(buf);
- if (hasPayload)
- {
- buf.put(masked(payload));
- }
-
- // Prepare CLOSE Frame
- buf.put((byte)(0x00 | FIN | OpCode.CLOSE.getCode()));
- putPayloadLength(buf,2);
- putMask(buf);
- buf.put(masked(new byte[]
- { 0x03, (byte)0xE8 }));
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,500);
-
- // Validate echo'd frame
- WebSocketFrame frame = capture.getFrames().get(0);
- Assert.assertThat("frame should be PONG frame",frame.getOpCode(),is(OpCode.PONG));
- if (hasPayload)
- {
- Assert.assertThat("PONG.payloadLength",frame.getPayloadLength(),is(payload.length));
- ByteBufferAssert.assertEquals("PONG.payload",payload,frame.getPayload());
- }
- else
- {
- Assert.assertThat("PONG.payloadLength",frame.getPayloadLength(),is(0));
- }
-
- // Validate close
- frame = capture.getFrames().get(1);
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- CloseInfo close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
- }
- finally
- {
- client.disconnect();
- }
- }
-
- private void assertProtocolError(byte[] payload) throws Exception
- {
- BlockheadClient client = new BlockheadClient(server.getServerUri());
- try
- {
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(payload.length + Generator.OVERHEAD);
- BufferUtil.clearToFill(buf);
-
- // Prepare PING Frame
- buf.put((byte)(0x00 | FIN | OpCode.PING.getCode()));
- putPayloadLength(buf,payload.length);
- putMask(buf);
- buf.put(masked(payload));
-
- // Prepare CLOSE Frame
- buf.put((byte)(0x00 | FIN | OpCode.CLOSE.getCode()));
- putPayloadLength(buf,2);
- putMask(buf);
- buf.put(masked(new byte[]
- { 0x03, (byte)0xE8 }));
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
-
- // Validate close w/ Protocol Error
- WebSocketFrame frame = capture.getFrames().pop();
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- CloseInfo close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.PROTOCOL));
- }
- finally
- {
- client.disconnect();
- }
- }
-
/**
- * Send a ping frame as separate segments, in an inefficient way.
- *
- * @param payload
- * the payload
- * @param segmentSize
- * the segment size for each inefficient segment (flush between)
+ * Ping without payload
*/
- private void assertSegmentedPingFrame(byte[] payload, int segmentSize) throws Exception
+ @Test
+ public void testCase2_1() throws Exception
{
- Assert.assertThat("payload exists for segmented send",payload,notNullValue());
- Assert.assertThat("payload exists for segmented send",payload.length,greaterThan(0));
+ WebSocketFrame send = WebSocketFrame.ping();
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ WebSocketFrame expect = WebSocketFrame.pong();
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(payload.length + Generator.OVERHEAD);
- BufferUtil.clearToFill(buf);
-
- // Prepare PING Frame
- buf.put((byte)(0x00 | FIN | OpCode.PING.getCode()));
- putPayloadLength(buf,payload.length);
- putMask(buf);
- buf.put(masked(payload));
-
- // Prepare CLOSE Frame
- buf.put((byte)(0x00 | FIN | OpCode.CLOSE.getCode()));
- putPayloadLength(buf,2);
- putMask(buf);
- buf.put(masked(new byte[]
- { 0x03, (byte)0xE8 }));
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- int origLimit = buf.limit();
- int limit = buf.limit();
- int len;
- int pos = buf.position();
- int overallLeft = buf.remaining();
- while (overallLeft > 0)
- {
- buf.position(pos);
- limit = Math.min(origLimit,pos + segmentSize);
- buf.limit(limit);
- len = buf.remaining();
- overallLeft -= len;
- pos += len;
- client.writeRaw(buf);
- client.flush();
- }
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,500);
-
- // Validate echo'd frame
- WebSocketFrame frame = capture.getFrames().get(0);
- Assert.assertThat("frame should be PONG frame",frame.getOpCode(),is(OpCode.PONG));
- Assert.assertThat("PONG.payloadLength",frame.getPayloadLength(),is(payload.length));
- ByteBufferAssert.assertEquals("PONG.payload",payload,frame.getPayload());
-
- // Validate close
- frame = capture.getFrames().get(1);
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- CloseInfo close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.disconnect();
+ fuzzer.close();
}
}
/**
- * Ping without payload
- */
- @Test
- public void testCase2_1() throws Exception
- {
- byte payload[] = new byte[0];
- assertPingFrame(payload);
- }
-
- /**
* 10 pings
*/
@Test
@@ -229,8 +45,35 @@ public class TestABCase2 extends AbstractABCase
{
// send 10 pings each with unique payload
// send close
- // expect 10 pongs with OUR payload
+ // expect 10 pongs with our unique payload
// expect close
+
+ int pingCount = 10;
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ List<WebSocketFrame> expect = new ArrayList<>();
+
+ for (int i = 0; i < pingCount; i++)
+ {
+ String payload = String.format("ping-%d[%X]",i,i);
+ send.add(WebSocketFrame.ping().setPayload(payload));
+ expect.add(WebSocketFrame.pong().setPayload(payload));
+ }
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -243,6 +86,34 @@ public class TestABCase2 extends AbstractABCase
// send close
// expect 10 pongs with OUR payload
// expect close
+
+ int pingCount = 10;
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ List<WebSocketFrame> expect = new ArrayList<>();
+
+ for (int i = 0; i < pingCount; i++)
+ {
+ String payload = String.format("ping-%d[%X]",i,i);
+ send.add(WebSocketFrame.ping().setPayload(payload));
+ expect.add(WebSocketFrame.pong().setPayload(payload));
+ }
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(5);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -252,7 +123,27 @@ public class TestABCase2 extends AbstractABCase
public void testCase2_2() throws Exception
{
byte payload[] = StringUtil.getUtf8Bytes("Hello world");
- assertPingFrame(payload);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.ping().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -263,7 +154,27 @@ public class TestABCase2 extends AbstractABCase
{
byte payload[] = new byte[]
{ 0x00, (byte)0xFF, (byte)0xFE, (byte)0xFD, (byte)0xFC, (byte)0xFB, 0x00, (byte)0xFF };
- assertPingFrame(payload);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.ping().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -274,7 +185,27 @@ public class TestABCase2 extends AbstractABCase
{
byte payload[] = new byte[125];
Arrays.fill(payload,(byte)0xFE);
- assertPingFrame(payload);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.ping().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -283,9 +214,29 @@ public class TestABCase2 extends AbstractABCase
@Test
public void testCase2_5() throws Exception
{
- byte payload[] = new byte[126];
+ byte payload[] = new byte[126]; // intentionally too big
Arrays.fill(payload,(byte)0xFE);
- assertProtocolError(payload);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ // trick websocket frame into making extra large payload for ping
+ send.add(WebSocketFrame.binary(payload).setOpCode(OpCode.PING));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -296,7 +247,28 @@ public class TestABCase2 extends AbstractABCase
{
byte payload[] = new byte[125];
Arrays.fill(payload,(byte)0xFE);
- assertSegmentedPingFrame(payload,1);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.ping().setPayload(payload));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload(payload));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(1);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
/**
@@ -305,50 +277,25 @@ public class TestABCase2 extends AbstractABCase
@Test
public void testCase2_7() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.pong()); // unsolicited pong
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- byte payload[] = new byte[0];
-
- ByteBuffer buf = ByteBuffer.allocate(256);
- BufferUtil.clearToFill(buf);
-
- // Prepare Unsolicited PONG Frame
- buf.put((byte)(0x00 | FIN | OpCode.PONG.getCode()));
- putPayloadLength(buf,payload.length);
- putMask(buf);
- // buf.put(masked(payload));
-
- // Prepare CLOSE Frame
- buf.put((byte)(0x00 | FIN | OpCode.CLOSE.getCode()));
- putPayloadLength(buf,2);
- putMask(buf);
- buf.put(masked(new byte[]
- { 0x03, (byte)0xE8 }));
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
-
- // Validate close
- WebSocketFrame frame = capture.getFrames().pop();
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- CloseInfo close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.disconnect();
+ fuzzer.close();
}
-
}
/**
@@ -357,48 +304,24 @@ public class TestABCase2 extends AbstractABCase
@Test
public void testCase2_8() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.pong().setPayload("unsolicited")); // unsolicited pong
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- byte payload[] = StringUtil.getUtf8Bytes("unsolicited");
-
- ByteBuffer buf = ByteBuffer.allocate(256);
- BufferUtil.clearToFill(buf);
-
- // Prepare Unsolicited PONG Frame
- buf.put((byte)(0x00 | FIN | OpCode.PONG.getCode()));
- putPayloadLength(buf,payload.length);
- putMask(buf);
- buf.put(masked(payload));
-
- // Prepare CLOSE Frame
- buf.put((byte)(0x00 | FIN | OpCode.CLOSE.getCode()));
- putPayloadLength(buf,2);
- putMask(buf);
- buf.put(masked(new byte[]
- { 0x03, (byte)0xE8 }));
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
-
- // Validate close
- WebSocketFrame frame = capture.getFrames().pop();
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- CloseInfo close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.disconnect();
+ fuzzer.close();
}
}
@@ -408,69 +331,26 @@ public class TestABCase2 extends AbstractABCase
@Test
public void testCase2_9() throws Exception
{
- // send unsolicited pong with payload.
- // send OUR ping with payload
- // send close
- // expect pong with OUR payload
- // expect close
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.pong().setPayload("unsolicited")); // unsolicited pong
+ send.add(WebSocketFrame.ping().setPayload("our ping")); // our ping
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload("our ping")); // our pong
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- byte pongPayload[] = StringUtil.getUtf8Bytes("unsolicited");
-
- ByteBuffer buf = ByteBuffer.allocate(512);
- BufferUtil.clearToFill(buf);
-
- // Prepare Unsolicited PONG Frame
- buf.put((byte)(0x00 | FIN | OpCode.PONG.getCode()));
- putPayloadLength(buf,pongPayload.length);
- putMask(buf);
- buf.put(masked(pongPayload));
-
- // Prepare our PING with payload
- byte pingPayload[] = StringUtil.getUtf8Bytes("ping me");
- buf.put((byte)(0x00 | FIN | OpCode.PING.getCode()));
- putPayloadLength(buf,pingPayload.length);
- putMask(buf);
- buf.put(masked(pingPayload));
-
- // Prepare CLOSE Frame
- buf.put((byte)(0x00 | FIN | OpCode.CLOSE.getCode()));
- putPayloadLength(buf,2);
- putMask(buf);
- buf.put(masked(new byte[]
- { 0x03, (byte)0xE8 }));
-
- // Write Data Frame
- BufferUtil.flipToFlush(buf,0);
- client.writeRaw(buf);
- client.flush();
-
- // Read frames
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,500);
-
- // Validate PONG
- WebSocketFrame frame = capture.getFrames().pop();
- Assert.assertThat("frame should be PONG frame",frame.getOpCode(),is(OpCode.PONG));
- Assert.assertThat("PONG.payloadLength",frame.getPayloadLength(),is(pingPayload.length));
- ByteBufferAssert.assertEquals("PONG.payload",pingPayload,frame.getPayload());
-
- // Validate close
- frame = capture.getFrames().pop();
- Assert.assertThat("CLOSE.frame.opcode",frame.getOpCode(),is(OpCode.CLOSE));
- CloseInfo close = new CloseInfo(frame);
- Assert.assertThat("CLOSE.statusCode",close.getStatusCode(),is(StatusCode.NORMAL));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.disconnect();
+ fuzzer.close();
}
-
}
-
}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase3.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase3.java
new file mode 100644
index 0000000000..6e2e075450
--- /dev/null
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase3.java
@@ -0,0 +1,216 @@
+package org.eclipse.jetty.websocket.server.ab;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.jetty.websocket.api.StatusCode;
+import org.eclipse.jetty.websocket.protocol.CloseInfo;
+import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
+import org.junit.Test;
+
+public class TestABCase3 extends AbstractABCase
+{
+ /**
+ * Send small text frame, with RSV1 == true, with no extensions defined.
+ */
+ @Test
+ public void testCase3_1() throws Exception
+ {
+ WebSocketFrame send = WebSocketFrame.text("small").setRsv1(true); // intentionally bad
+
+ WebSocketFrame expect = new CloseInfo(StatusCode.PROTOCOL).asFrame();
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+
+ /**
+ * Send small text frame, send again with RSV2 == true, then ping, with no extensions defined.
+ */
+ @Test
+ public void testCase3_2() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text("small"));
+ send.add(WebSocketFrame.text("small").setRsv2(true)); // intentionally bad
+ send.add(WebSocketFrame.ping().setPayload("ping"));
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text("small")); // echo on good frame
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+
+ /**
+ * Send small text frame, send again with (RSV1 & RSV2), then ping, with no extensions defined.
+ */
+ @Test
+ public void testCase3_3() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text("small"));
+ send.add(WebSocketFrame.text("small").setRsv1(true).setRsv2(true)); // intentionally bad
+ send.add(WebSocketFrame.ping().setPayload("ping"));
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text("small")); // echo on good frame
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+
+ /**
+ * Send small text frame, send again with (RSV3), then ping, with no extensions defined.
+ */
+ @Test
+ public void testCase3_4() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.text("small"));
+ send.add(WebSocketFrame.text("small").setRsv3(true)); // intentionally bad
+ send.add(WebSocketFrame.ping().setPayload("ping"));
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text("small")); // echo on good frame
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(1);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+
+ /**
+ * Send binary frame with (RSV3 & RSV1), with no extensions defined.
+ */
+ @Test
+ public void testCase3_5() throws Exception
+ {
+ byte payload[] = new byte[8];
+ Arrays.fill(payload,(byte)0xFF);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.binary(payload).setRsv3(true).setRsv1(true)); // intentionally bad
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+
+ /**
+ * Send ping frame with (RSV3 & RSV2), with no extensions defined.
+ */
+ @Test
+ public void testCase3_6() throws Exception
+ {
+ byte payload[] = new byte[8];
+ Arrays.fill(payload,(byte)0xFF);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(WebSocketFrame.ping().setPayload(payload).setRsv3(true).setRsv2(true)); // intentionally bad
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+
+ /**
+ * Send close frame with (RSV3 & RSV2 & RSV1), with no extensions defined.
+ */
+ @Test
+ public void testCase3_7() throws Exception
+ {
+ byte payload[] = new byte[8];
+ Arrays.fill(payload,(byte)0xFF);
+
+ List<WebSocketFrame> send = new ArrayList<>();
+ WebSocketFrame frame = new CloseInfo(StatusCode.NORMAL).asFrame();
+ frame.setRsv1(true);
+ frame.setRsv2(true);
+ frame.setRsv3(true);
+ send.add(frame); // intentionally bad
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
+}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java
index 5989117cdb..759a22bae2 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java
@@ -174,13 +174,16 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
LOG.debug("disconnect");
IO.close(in);
IO.close(out);
- try
- {
- socket.close();
- }
- catch (IOException ignore)
+ if (socket != null)
{
- /* ignore */
+ try
+ {
+ socket.close();
+ }
+ catch (IOException ignore)
+ {
+ /* ignore */
+ }
}
}
@@ -318,6 +321,11 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
incomingFrameQueue.incoming(copy);
}
+ public boolean isConnected()
+ {
+ return (socket != null) && (socket.isConnected());
+ }
+
public void lookFor(String string) throws IOException
{
String orig = string;
@@ -547,4 +555,24 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
LOG.debug("write((String)[{}]){}{})",str.length(),'\n',str);
out.write(StringUtil.getBytes(str,StringUtil.__ISO_8859_1));
}
+
+ public void writeRawSlowly(ByteBuffer buf, int segmentSize) throws IOException
+ {
+ int origLimit = buf.limit();
+ int limit = buf.limit();
+ int len;
+ int pos = buf.position();
+ int overallLeft = buf.remaining();
+ while (overallLeft > 0)
+ {
+ buf.position(pos);
+ limit = Math.min(origLimit,pos + segmentSize);
+ buf.limit(limit);
+ len = buf.remaining();
+ overallLeft -= len;
+ pos += len;
+ writeRaw(buf);
+ flush();
+ }
+ }
}
diff --git a/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties b/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties
index 887eeadef1..7000875148 100644
--- a/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties
+++ b/jetty-websocket/websocket-server/src/test/resources/jetty-logging.properties
@@ -1,15 +1,15 @@
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
-org.eclipse.jetty.io.LEVEL=WARN
+org.eclipse.jetty.LEVEL=WARN
org.eclipse.jetty.server.LEVEL=WARN
# org.eclipse.jetty.websocket.LEVEL=WARN
org.eclipse.jetty.websocket.server.helper.RFCSocket.LEVEL=OFF
# See the read/write traffic
-org.eclipse.jetty.websocket.io.Frames.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.io.Frames.LEVEL=DEBUG
# org.eclipse.jetty.websocket.io.LEVEL=DEBUG
# org.eclipse.jetty.websocket.io.WebSocketAsyncConnection.LEVEL=DEBUG
# org.eclipse.jetty.util.thread.QueuedThreadPool.LEVEL=DEBUG
# org.eclipse.jetty.io.SelectorManager.LEVEL=INFO
-org.eclipse.jetty.websocket.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.LEVEL=DEBUG
# org.eclipse.jetty.websocket.driver.WebSocketEventDriver.LEVEL=DEBUG
# org.eclipse.jetty.websocket.extensions.LEVEL=DEBUG
# org.eclipse.jetty.websocket.protocol.Generator.LEVEL=INFO

Back to the top