Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2012-08-01 13:46:15 -0400
committerJoakim Erdfelt2012-08-01 13:46:15 -0400
commit07929fee884bf3a115f954bbb6ecaf89e398b914 (patch)
tree665d1166ad0c1fe47f7b4d87f35329ebb165312c /jetty-websocket
parent7c742ee4e81635de0fdd8c5fc930313f0a030243 (diff)
downloadorg.eclipse.jetty.project-07929fee884bf3a115f954bbb6ecaf89e398b914.tar.gz
org.eclipse.jetty.project-07929fee884bf3a115f954bbb6ecaf89e398b914.tar.xz
org.eclipse.jetty.project-07929fee884bf3a115f954bbb6ecaf89e398b914.zip
Fleshing out more fragment tests
Diffstat (limited to 'jetty-websocket')
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/Fuzzer.java2
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase5.java907
2 files changed, 585 insertions, 324 deletions
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
index 46ccb7e8d7..fa21f7908e 100644
--- 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
@@ -80,7 +80,7 @@ public class Fuzzer
prefix = "Frame[" + i + "]";
- Assert.assertThat(prefix + ".opcode",actual.getOpCode(),is(expected.getOpCode()));
+ Assert.assertThat(prefix + ".opcode",OpCode.name(actual.getOpCode()),is(OpCode.name(expected.getOpCode())));
prefix += "/" + actual.getOpCode();
if (expected.getOpCode() == OpCode.CLOSE)
{
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase5.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase5.java
index 18a719d66a..a3783cf528 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase5.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/ab/TestABCase5.java
@@ -15,460 +15,721 @@
//========================================================================
package org.eclipse.jetty.websocket.server.ab;
-import static org.hamcrest.Matchers.*;
-
-import java.nio.ByteBuffer;
+import java.net.SocketException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.TimeUnit;
-import org.eclipse.jetty.io.ByteBufferPool;
-import org.eclipse.jetty.io.StandardByteBufferPool;
-import org.eclipse.jetty.util.BufferUtil;
-import org.eclipse.jetty.util.StringUtil;
-import org.eclipse.jetty.websocket.api.WebSocketPolicy;
+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.SimpleServletServer;
-import org.eclipse.jetty.websocket.server.blockhead.BlockheadClient;
-import org.eclipse.jetty.websocket.server.examples.MyEchoServlet;
-import org.eclipse.jetty.websocket.server.helper.IncomingFramesCapture;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Ignore;
import org.junit.Test;
-public class TestABCase5
+/**
+ * Fragmentation Tests
+ */
+public class TestABCase5 extends AbstractABCase
{
- private static final byte FIN = (byte)0x80;
- private static final byte NOFIN = 0x00;
-
- private static SimpleServletServer server;
- private static Generator laxGenerator;
-
- @BeforeClass
- public static void initGenerators()
+ /**
+ * Send ping fragmented in 2 packets
+ */
+ @Test
+ public void testCase5_1() throws Exception
{
- WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
- ByteBufferPool bufferPool = new StandardByteBufferPool();
- laxGenerator = new Generator(policy,bufferPool,false);
- }
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.PING).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- @BeforeClass
- public static void startServer() throws Exception
- {
- server = new SimpleServletServer(new MyEchoServlet());
- server.start();
- }
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- @AfterClass
- public static void stopServer()
- {
- server.stop();
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
}
+ /**
+ * Send continuation+fin, then text+fin (framewise)
+ */
@Test
- public void testCase5_1PingIn2Packets() throws Exception
+ public void testCase5_10() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("sorry").setFin(true));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, world"));
+ 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
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf);
+ /**
+ * Send continuation+fin, then text+fin (slowly)
+ */
+ @Test
+ public void testCase5_11() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("sorry").setFin(true));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- String fragment1 = "fragment1";
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- // Intentionally bad PING (spec says control frames must be FIN==true)
- buf.put((byte)(NOFIN | OpCode.PING));
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(1);
+ try
+ {
+ fuzzer.send(send);
+ }
+ catch (SocketException ignore)
+ {
+ // Potential for SocketException (Broken Pipe) here.
+ // But not in 100% of testing scenarios. It is a safe
+ // exception to ignore in this testing scenario, as the
+ // slow writing of the frames can result in the server
+ // throwing a PROTOCOL ERROR termination/close when it
+ // encounters the bad continuation frame above (this
+ // termination is the expected behavior), and this
+ // early socket close can propagate back to the client
+ // before it has a chance to finish writing out the
+ // remaining frame octets
+ }
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- byte b = 0x00; // no masking
- b |= fragment1.length() & 0x7F;
- buf.put(b);
- buf.put(fragment1.getBytes());
- BufferUtil.flipToFlush(buf,0);
+ /**
+ * Send continuation+!fin, then text+fin
+ */
+ @Test
+ public void testCase5_12() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("sorry").setFin(false));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- client.writeRaw(buf);
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- ByteBuffer buf2 = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf2);
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- String fragment2 = "fragment2";
+ /**
+ * Send continuation+!fin, then text+fin (framewise)
+ */
+ @Test
+ public void testCase5_13() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("sorry").setFin(false));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- buf2.put((byte)(FIN | OpCode.PING));
- b = 0x00; // no masking
- b |= fragment2.length() & 0x7F;
- buf2.put(b);
- buf2.put(fragment2.getBytes());
- BufferUtil.flipToFlush(buf2,0);
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- client.writeRaw(buf2);
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- // Read frame
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().get(0);
+ /**
+ * Send continuation+!fin, then text+fin (slowly)
+ */
+ @Test
+ public void testCase5_14() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("sorry").setFin(false));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- Assert.assertThat("frame should be close frame",frame.getOpCode(),is(OpCode.CLOSE));
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- Assert.assertThat("CloseFrame.status code",new CloseInfo(frame).getStatusCode(),is(1002));
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(1);
+ try
+ {
+ fuzzer.send(send);
+ }
+ catch (SocketException ignore)
+ {
+ // Potential for SocketException (Broken Pipe) here.
+ // But not in 100% of testing scenarios. It is a safe
+ // exception to ignore in this testing scenario, as the
+ // slow writing of the frames can result in the server
+ // throwing a PROTOCOL ERROR termination/close when it
+ // encounters the bad continuation frame above (this
+ // termination is the expected behavior), and this
+ // early socket close can propagate back to the client
+ // before it has a chance to finish writing out the
+ // remaining frame octets
+ }
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
+ /**
+ * Send text fragmented properly in 2 frames, then continuation!fin, then text unfragmented.
+ */
@Test
- public void testCase5_1PingIn2PacketsWithBuilder() throws Exception
+ public void testCase5_15() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment1").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment2").setFin(true));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment3").setFin(false)); // bad frame
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment4").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ send.add(WebSocketFrame.text("fragment1fragment2"));
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- String fragment1 = "fragment1";
- WebSocketFrame frame1 = WebSocketFrame.ping().setFin(false).setPayload(fragment1);
- ByteBuffer buf1 = laxGenerator.generate(frame1);
- client.writeRaw(buf1);
-
- String fragment2 = "fragment2";
- WebSocketFrame frame2 = WebSocketFrame.ping().setPayload(fragment2);
- ByteBuffer buf2 = laxGenerator.generate(frame2);
- client.writeRaw(buf2);
-
- // Read frame
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().pop();
-
- Assert.assertThat("frame should be close frame",frame.getOpCode(),is(OpCode.CLOSE));
-
- Assert.assertThat("CloseFrame.status code",new CloseInfo(frame).getStatusCode(),is(1002));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
+ /**
+ * (continuation!fin, text!fin, continuation+fin) * 2
+ */
@Test
- public void testCase5_2PongIn2Packets() throws Exception
+ public void testCase5_16() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment1").setFin(false)); // bad frame
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment2").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment3").setFin(true));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment4").setFin(false)); // bad frame
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment5").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment6").setFin(true));
+ 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
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf);
-
- String fragment1 = "fragment1";
-
- buf.put((byte)(NOFIN | OpCode.PONG));
-
- byte b = 0x00; // no masking
- b |= fragment1.length() & 0x7F;
- buf.put(b);
- buf.put(fragment1.getBytes());
- BufferUtil.flipToFlush(buf,0);
-
- client.writeRaw(buf);
-
- ByteBuffer buf2 = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf2);
-
- String fragment2 = "fragment2";
-
- buf2.put((byte)(FIN | OpCode.CONTINUATION));
- b = 0x00; // no masking
- b |= fragment2.length() & 0x7F;
- buf2.put(b);
- buf2.put(fragment2.getBytes());
- BufferUtil.flipToFlush(buf2,0);
-
- client.writeRaw(buf2);
-
- // Read frame
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().pop();
-
- Assert.assertThat("frame should be close frame",frame.getOpCode(),is(OpCode.CLOSE));
-
- Assert.assertThat("CloseFrame.status code",new CloseInfo(frame).getStatusCode(),is(1002));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
+ /**
+ * (continuation+fin, text!fin, continuation+fin) * 2
+ */
@Test
- public void testCase5_2PongIn2PacketsWithBuilder() throws Exception
+ public void testCase5_17() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment1").setFin(true)); // nothing to continue
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment2").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment3").setFin(true));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment4").setFin(true)); // nothing to continue
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment5").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("fragment6").setFin(true));
+ 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
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- String fragment1 = "fragment1";
- WebSocketFrame frame1 = WebSocketFrame.pong().setFin(false).setPayload(fragment1);
- ByteBuffer buf1 = laxGenerator.generate(frame1);
- client.writeRaw(buf1);
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- String fragment2 = "fragment2";
- WebSocketFrame frame2 = new WebSocketFrame(OpCode.CONTINUATION).setFin(false).setPayload(fragment2);
- ByteBuffer buf2 = laxGenerator.generate(frame2);
- client.writeRaw(buf2);
+ /**
+ * text message fragmented in 2 frames, both frames as opcode=TEXT
+ */
+ @Test
+ public void testCase5_18() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment1").setFin(false));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment2").setFin(true)); // bad frame, must be continuation
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- // Read frame
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().pop();
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- Assert.assertThat("frame should be close frame",frame.getOpCode(),is(OpCode.CLOSE));
- Assert.assertThat("CloseFrame.status code",new CloseInfo(frame).getStatusCode(),is(1002));
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.disconnect();
+ fuzzer.close();
}
}
+ /**
+ * send text message fragmented in 5 frames, with 2 pings and wait between.
+ */
@Test
- public void testCase5_3TextIn2Packets() throws Exception
+ public void testCase5_19() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send1 = new ArrayList<>();
+ send1.add(new WebSocketFrame(OpCode.TEXT).setPayload("f1").setFin(false));
+ send1.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f2").setFin(false));
+ send1.add(new WebSocketFrame(OpCode.PING).setPayload("pong-1"));
+
+ List<WebSocketFrame> send2 = new ArrayList<>();
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f3").setFin(false));
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f4").setFin(false));
+ send2.add(new WebSocketFrame(OpCode.PING).setPayload("pong-2"));
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f5").setFin(true));
+ send2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect1 = new ArrayList<>();
+ expect1.add(WebSocketFrame.pong().setPayload("pong-1"));
+
+ List<WebSocketFrame> expect2 = new ArrayList<>();
+ expect2.add(WebSocketFrame.pong().setPayload("pong-2"));
+ expect2.add(WebSocketFrame.text("f1,f2,f3,f4,f5"));
+ expect2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf);
-
- String fragment1 = "fragment1";
-
- buf.put((byte)(NOFIN | OpCode.TEXT));
-
- byte b = 0x00; // no masking
- b |= fragment1.length() & 0x7F;
- buf.put(b);
- buf.put(fragment1.getBytes());
- BufferUtil.flipToFlush(buf,0);
-
- client.writeRaw(buf);
-
- ByteBuffer buf2 = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf2);
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
- String fragment2 = "fragment2";
+ fuzzer.send(send1);
+ fuzzer.expect(expect1);
- buf2.put((byte)(FIN | OpCode.CONTINUATION));
- b = 0x00; // no masking
- b |= fragment2.length() & 0x7F;
- buf2.put(b);
- buf2.put(fragment2.getBytes());
- BufferUtil.flipToFlush(buf2,0);
+ TimeUnit.SECONDS.sleep(1);
- client.writeRaw(buf2);
+ fuzzer.send(send2);
+ fuzzer.expect(expect2);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- // Read frame
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().pop();
+ /**
+ * Send pong fragmented in 2 packets
+ */
+ @Test
+ public void testCase5_2() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.PONG).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- Assert.assertThat("frame should be text frame",frame.getOpCode(),is(OpCode.TEXT));
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- Assert.assertThat("TextFrame.payload",frame.getPayloadAsUTF8(),is(fragment1 + fragment2));
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
+ /**
+ * send text message fragmented in 5 frames, with 2 pings and wait between. (framewise)
+ */
@Test
- public void testCase5_6TextPingRemainingText() throws Exception
+ public void testCase5_20() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send1 = new ArrayList<>();
+ send1.add(new WebSocketFrame(OpCode.TEXT).setPayload("f1").setFin(false));
+ send1.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f2").setFin(false));
+ send1.add(new WebSocketFrame(OpCode.PING).setPayload("pong-1"));
+
+ List<WebSocketFrame> send2 = new ArrayList<>();
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f3").setFin(false));
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f4").setFin(false));
+ send2.add(new WebSocketFrame(OpCode.PING).setPayload("pong-2"));
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f5").setFin(true));
+ send2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect1 = new ArrayList<>();
+ expect1.add(WebSocketFrame.pong().setPayload("pong-1"));
+
+ List<WebSocketFrame> expect2 = new ArrayList<>();
+ expect2.add(WebSocketFrame.pong().setPayload("pong-2"));
+ expect2.add(WebSocketFrame.text("f1,f2,f3,f4,f5"));
+ expect2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- // Send a text packet
-
- ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf);
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
- String fragment1 = "fragment1";
+ fuzzer.send(send1);
+ fuzzer.expect(expect1);
- buf.put((byte)(NOFIN | OpCode.TEXT));
+ TimeUnit.SECONDS.sleep(1);
- byte b = 0x00; // no masking
- b |= fragment1.length() & 0x7F;
- buf.put(b);
- buf.put(fragment1.getBytes());
- BufferUtil.flipToFlush(buf,0);
-
- client.writeRaw(buf);
-
- // Send a ping with payload
-
- ByteBuffer pingBuf = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(pingBuf);
-
- String pingPayload = "ping payload";
+ fuzzer.send(send2);
+ fuzzer.expect(expect2);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- pingBuf.put((byte)(FIN | OpCode.PING));
+ /**
+ * send text message fragmented in 5 frames, with 2 pings and wait between. (framewise)
+ */
+ @Test
+ public void testCase5_20_slow() throws Exception
+ {
+ List<WebSocketFrame> send1 = new ArrayList<>();
+ send1.add(new WebSocketFrame(OpCode.TEXT).setPayload("f1").setFin(false));
+ send1.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f2").setFin(false));
+ send1.add(new WebSocketFrame(OpCode.PING).setPayload("pong-1"));
+
+ List<WebSocketFrame> send2 = new ArrayList<>();
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f3").setFin(false));
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f4").setFin(false));
+ send2.add(new WebSocketFrame(OpCode.PING).setPayload("pong-2"));
+ send2.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f5").setFin(true));
+ send2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect1 = new ArrayList<>();
+ expect1.add(WebSocketFrame.pong().setPayload("pong-1"));
+
+ List<WebSocketFrame> expect2 = new ArrayList<>();
+ expect2.add(WebSocketFrame.pong().setPayload("pong-2"));
+ expect2.add(WebSocketFrame.text("f1,f2,f3,f4,f5"));
+ expect2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(1);
- b = 0x00; // no masking
- b |= pingPayload.length() & 0x7F;
- pingBuf.put(b);
- pingBuf.put(pingPayload.getBytes());
- BufferUtil.flipToFlush(pingBuf,0);
+ fuzzer.send(send1);
+ fuzzer.expect(expect1);
- client.writeRaw(pingBuf);
+ TimeUnit.SECONDS.sleep(1);
- // Send remaining text as continuation
+ fuzzer.send(send2);
+ fuzzer.expect(expect2);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- ByteBuffer buf2 = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf2);
+ /**
+ * Send text fragmented in 2 packets
+ */
+ @Test
+ public void testCase5_3() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- String fragment2 = "fragment2";
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text("hello, world"));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- buf2.put((byte)(FIN | OpCode.CONTINUATION));
- b = 0x00; // no masking
- b |= fragment2.length() & 0x7F;
- buf2.put(b);
- buf2.put(fragment2.getBytes());
- BufferUtil.flipToFlush(buf2,0);
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- client.writeRaw(buf2);
+ /**
+ * Send text fragmented in 2 packets (framewise)
+ */
+ @Test
+ public void testCase5_4() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- // Should be 2 frames, pong frame followed by combined echo'd text frame
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.SECONDS,1);
- WebSocketFrame frame = capture.getFrames().pop();
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text("hello, world"));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- Assert.assertThat("first frame should be pong frame",frame.getOpCode(),is(OpCode.PONG));
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- ByteBuffer payload1 = BufferUtil.toBuffer(pingPayload,StringUtil.__UTF8_CHARSET);
+ /**
+ * Send text fragmented in 2 packets (slowly)
+ */
+ @Test
+ public void testCase5_5() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- ByteBufferAssert.assertEquals("payloads should be equal",payload1,frame.getPayload());
- frame = capture.getFrames().pop();
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.text("hello, world"));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- Assert.assertThat("second frame should be text frame",frame.getOpCode(),is(OpCode.TEXT));
- Assert.assertThat("TextFrame.payload",frame.getPayloadAsUTF8(),is(fragment1 + fragment2));
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.SLOW);
+ fuzzer.setSlowSendSegmentSize(1);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
+ /**
+ * Send text fragmented in 2 packets, with ping between them
+ */
@Test
- public void testCase5_6TextPingRemainingTextWithBuilder() throws Exception
+ public void testCase5_6() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.PING).setPayload("ping"));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload("ping"));
+ expect.add(WebSocketFrame.text("hello, world"));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- // Send a text packet
- String textPayload1 = "fragment1";
- WebSocketFrame frame1 = WebSocketFrame.text().setFin(false).setPayload(textPayload1);
- ByteBuffer buf1 = laxGenerator.generate(frame1);
- client.writeRaw(buf1);
-
- // Send a ping with payload
- String pingPayload = "ping payload";
- WebSocketFrame frame2 = WebSocketFrame.ping().setPayload(pingPayload);
- ByteBuffer buf2 = laxGenerator.generate(frame2);
- client.writeRaw(buf2);
-
- // Send remaining text as continuation
- String textPayload2 = "fragment2";
- WebSocketFrame frame3 = new WebSocketFrame(OpCode.CONTINUATION).setPayload(textPayload2);
- ByteBuffer buf3 = laxGenerator.generate(frame3);
- client.writeRaw(buf3);
-
- // Should be 2 frames, pong frame followed by combined echo'd text frame
- IncomingFramesCapture capture = client.readFrames(2,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().pop();
-
- Assert.assertThat("first frame should be pong frame",frame.getOpCode(),is(OpCode.PONG));
-
- ByteBuffer payload1 = BufferUtil.toBuffer(pingPayload,StringUtil.__UTF8_CHARSET);
- ByteBufferAssert.assertEquals("Payload",payload1,frame.getPayload());
-
- frame = capture.getFrames().pop();
-
- Assert.assertThat("second frame should be text frame",frame.getOpCode(),is(OpCode.TEXT));
-
- Assert.assertThat("TextFrame.payload",frame.getPayloadAsUTF8(),is(textPayload1 + textPayload2));
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
+ /**
+ * Send text fragmented in 2 packets, with ping between them (framewise)
+ */
@Test
- @Ignore("AB tests have chop concepts currently unsupported by test...I think, also the string being returns is not Bad Continuation")
- public void testCase5_9BadContinuation() throws Exception
+ public void testCase5_7() throws Exception
{
- BlockheadClient client = new BlockheadClient(server.getServerUri());
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.PING).setPayload("ping"));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload("ping"));
+ expect.add(WebSocketFrame.text("hello, world"));
+ expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ Fuzzer fuzzer = new Fuzzer(this);
try
{
- client.connect();
- client.sendStandardRequest();
- client.expectUpgradeResponse();
-
- // Send a text packet
-
- ByteBuffer buf = ByteBuffer.allocate(Generator.OVERHEAD + 2);
- BufferUtil.clearToFill(buf);
-
- String fragment1 = "fragment";
-
- // continuation w / FIN
-
- buf.put((byte)(FIN | OpCode.CONTINUATION));
-
- byte b = 0x00; // no masking
- b |= fragment1.length() & 0x7F;
- buf.put(b);
- buf.put(fragment1.getBytes());
- BufferUtil.flipToFlush(buf,0);
-
- client.writeRaw(buf);
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
+ }
+ finally
+ {
+ fuzzer.close();
+ }
+ }
- // Read frame
- IncomingFramesCapture capture = client.readFrames(1,TimeUnit.MILLISECONDS,500);
- WebSocketFrame frame = capture.getFrames().pop();
+ /**
+ * Send text fragmented in 2 packets, with ping between them (slowly)
+ */
+ @Test
+ public void testCase5_8() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, ").setFin(false));
+ send.add(new WebSocketFrame(OpCode.PING).setPayload("ping"));
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("world").setFin(true));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
+
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(WebSocketFrame.pong().setPayload("ping"));
+ expect.add(WebSocketFrame.text("hello, world"));
+ 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();
+ }
+ }
- Assert.assertThat("frame should be close frame",frame.getOpCode(),is(OpCode.CLOSE));
+ /**
+ * Send continuation+fin, then text+fin
+ */
+ @Test
+ public void testCase5_9() throws Exception
+ {
+ List<WebSocketFrame> send = new ArrayList<>();
+ send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload("sorry").setFin(true));
+ send.add(new WebSocketFrame(OpCode.TEXT).setPayload("hello, world"));
+ send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
- Assert.assertThat("CloseFrame.status code",new CloseInfo(frame).getStatusCode(),is(1002));
+ List<WebSocketFrame> expect = new ArrayList<>();
+ expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
- Assert.assertThat("CloseFrame.reason",new CloseInfo(frame).getReason(),is("Bad Continuation"));
- // TODO put close reasons into public strings in impl someplace?
+ Fuzzer fuzzer = new Fuzzer(this);
+ try
+ {
+ fuzzer.connect();
+ fuzzer.setSendMode(Fuzzer.SendMode.BULK);
+ fuzzer.send(send);
+ fuzzer.expect(expect);
}
finally
{
- client.close();
+ fuzzer.close();
}
}
}

Back to the top