More AB tests, and updates to Fuzzer

This commit is contained in:
Joakim Erdfelt 2012-08-01 13:11:24 -07:00
parent 2f4c86be35
commit 3621bec43c
8 changed files with 992 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.protocol.Generator;
import org.eclipse.jetty.websocket.server.SimpleServletServer;
@ -44,6 +45,26 @@ public abstract class AbstractABCase
server.stop();
}
public static String toUtf8String(byte[] buf)
{
String raw = StringUtil.toUTF8String(buf,0,buf.length);
StringBuilder ret = new StringBuilder();
int len = raw.length();
for (int i = 0; i < len; i++)
{
int codepoint = raw.codePointAt(i);
if (Character.isUnicodeIdentifierPart(codepoint))
{
ret.append(String.format("\\u%04X",codepoint));
}
else
{
ret.append(Character.toChars(codepoint));
}
}
return ret.toString();
}
public Generator getLaxGenerator()
{
return laxGenerator;

View File

@ -5,7 +5,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses(
{ TestABCase1.class, TestABCase2.class, TestABCase3.class, TestABCase4.class, TestABCase5.class, TestABCase7_9.class })
{ TestABCase1.class, TestABCase2.class, TestABCase3.class, TestABCase4.class, TestABCase5.class, TestABCase6.class, TestABCase7_9.class })
public class AllTests
{
/* let junit do the rest */

View File

@ -50,6 +50,26 @@ public class Fuzzer
this.generator = testcase.getLaxGenerator();
}
public ByteBuffer asNetworkBuffer(List<WebSocketFrame> send)
{
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);
return buf;
}
public void close()
{
this.client.disconnect();
@ -125,6 +145,12 @@ public class Fuzzer
}
}
public void send(ByteBuffer buf, int numBytes) throws IOException
{
client.writeRaw(buf,numBytes);
client.flush();
}
public void send(List<WebSocketFrame> send) throws IOException
{
Assert.assertThat("Client connected",client.isConnected(),is(true));

View File

@ -1,3 +1,18 @@
// ========================================================================
// Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
//========================================================================
package org.eclipse.jetty.websocket.server.ab;
import java.util.ArrayList;

View File

@ -20,15 +20,19 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.eclipse.jetty.toolchain.test.annotation.Slow;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.protocol.CloseInfo;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Fragmentation Tests
*/
@RunWith(AdvancedRunner.class)
public class TestABCase5 extends AbstractABCase
{
/**
@ -360,13 +364,19 @@ public class TestABCase5 extends AbstractABCase
* send text message fragmented in 5 frames, with 2 pings and wait between.
*/
@Test
@Slow
public void testCase5_19() throws Exception
{
// phase 1
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> expect1 = new ArrayList<>();
expect1.add(WebSocketFrame.pong().setPayload("pong-1"));
// phase 2
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));
@ -374,9 +384,6 @@ public class TestABCase5 extends AbstractABCase
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"));
@ -388,11 +395,14 @@ public class TestABCase5 extends AbstractABCase
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
// phase 1
fuzzer.send(send1);
fuzzer.expect(expect1);
// delay
TimeUnit.SECONDS.sleep(1);
// phase 2
fuzzer.send(send2);
fuzzer.expect(expect2);
}

View File

@ -0,0 +1,846 @@
// ========================================================================
// Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
//========================================================================
package org.eclipse.jetty.websocket.server.ab;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.eclipse.jetty.toolchain.test.annotation.Slow;
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.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.eclipse.jetty.websocket.server.helper.Hex;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* UTF-8 Tests
*/
@RunWith(AdvancedRunner.class)
public class TestABCase6 extends AbstractABCase
{
/**
* text message, 1 frame, 0 length
*/
@Test
public void testCase6_1_1() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(WebSocketFrame.text());
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(WebSocketFrame.text());
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();
}
}
/**
* text message, 0 length, 3 fragments
*/
@Test
public void testCase6_1_2() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setFin(false));
send.add(new WebSocketFrame(OpCode.CONTINUATION).setFin(false));
send.add(new WebSocketFrame(OpCode.CONTINUATION).setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(WebSocketFrame.text());
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();
}
}
/**
* text message, small length, 3 fragments (only middle frame has payload)
*/
@Test
public void testCase6_1_3() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setFin(false));
send.add(new WebSocketFrame(OpCode.CONTINUATION).setFin(false).setPayload("middle"));
send.add(new WebSocketFrame(OpCode.CONTINUATION).setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(WebSocketFrame.text("middle"));
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();
}
}
/**
* valid utf8 text message, 1 frame/fragment.
*/
@Test
public void testCase6_2_1() throws Exception
{
String utf8 = "Hello-\uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-8!!";
byte msg[] = StringUtil.getUtf8Bytes(utf8);
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* valid utf8 text message, 2 fragments (on UTF8 code point boundary)
*/
@Test
public void testCase6_2_2() throws Exception
{
String utf8[] =
{ "Hello-\uC2B5@\uC39F\uC3A4", "\uC3BC\uC3A0\uC3A1-UTF-8!!" };
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(utf8[0]).setFin(false));
send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(utf8[1]).setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(utf8[0] + utf8[1]));
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();
}
}
/**
* valid utf8 text message, many fragments (1 byte each)
*/
@Test
public void testCase6_2_3() throws Exception
{
String utf8 = "Hello-\uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-8!!";
byte msg[] = StringUtil.getUtf8Bytes(utf8);
List<WebSocketFrame> send = new ArrayList<>();
int len = msg.length;
byte opcode = OpCode.TEXT;
byte mini[];
for (int i = 0; i < len; i++)
{
WebSocketFrame frame = new WebSocketFrame(opcode);
mini = new byte[1];
mini[0] = msg[i];
frame.setPayload(mini);
frame.setFin(!(i < (len - 1)));
send.add(frame);
}
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* valid utf8 text message, many fragments (1 byte each)
*/
@Test
public void testCase6_2_4() throws Exception
{
byte msg[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5");
List<WebSocketFrame> send = new ArrayList<>();
int len = msg.length;
byte opcode = OpCode.TEXT;
byte mini[];
for (int i = 0; i < len; i++)
{
WebSocketFrame frame = new WebSocketFrame(opcode);
mini = new byte[1];
mini[0] = msg[i];
frame.setPayload(mini);
frame.setFin(!(i < (len - 1)));
send.add(frame);
}
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* invalid utf8 text message, 1 frame/fragments
*/
@Test
public void testCase6_3_1() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5EDA080656469746564");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid utf8 text message, many fragments (1 byte each)
*/
@Test
public void testCase6_3_2() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5EDA080656469746564");
List<WebSocketFrame> send = new ArrayList<>();
int len = invalid.length;
byte opcode = OpCode.TEXT;
byte mini[];
for (int i = 0; i < len; i++)
{
WebSocketFrame frame = new WebSocketFrame(opcode);
mini = new byte[1];
mini[0] = invalid[i];
frame.setPayload(mini);
frame.setFin(!(i < (len - 1)));
send.add(frame);
}
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid text message, 3 fragments.
* <p>
* fragment #1 and fragment #3 are both valid in themselves.
* <p>
* fragment #2 contains the invalid utf8 code point.
*/
@Test
@Slow
public void testCase6_4_1() throws Exception
{
byte part1[] = StringUtil.getUtf8Bytes("\u03BA\u1F79\u03C3\u03BC\u03B5");
byte part2[] = Hex.asByteArray("F4908080"); // invalid
byte part3[] = StringUtil.getUtf8Bytes("edited");
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(new WebSocketFrame(OpCode.TEXT).setPayload(part1).setFin(false));
TimeUnit.SECONDS.sleep(1);
fuzzer.send(new WebSocketFrame(OpCode.CONTINUATION).setPayload(part2).setFin(false));
TimeUnit.SECONDS.sleep(1);
fuzzer.send(new WebSocketFrame(OpCode.CONTINUATION).setPayload(part3).setFin(true));
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid text message, 3 fragments.
* <p>
* fragment #1 is valid and ends in the middle of an incomplete code point.
* <p>
* fragment #2 finishes the UTF8 code point but it is invalid
* <p>
* fragment #3 contains the remainder of the message.
*/
@Test
@Slow
public void testCase6_4_2() throws Exception
{
byte part1[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5F4"); // split code point
byte part2[] = Hex.asByteArray("90"); // continue code point & invalid
byte part3[] = Hex.asByteArray("8080656469746564"); // continue code point & finish
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(new WebSocketFrame(OpCode.TEXT).setPayload(part1).setFin(false));
TimeUnit.SECONDS.sleep(1);
fuzzer.send(new WebSocketFrame(OpCode.CONTINUATION).setPayload(part2).setFin(false));
TimeUnit.SECONDS.sleep(1);
fuzzer.send(new WebSocketFrame(OpCode.CONTINUATION).setPayload(part3).setFin(true));
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid text message, 1 frame/fragment (slowly, and split within code points)
*/
@Test
@Slow
public void testCase6_4_3() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5F49080808080656469746564");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
ByteBuffer net = fuzzer.asNetworkBuffer(send);
fuzzer.send(net,6);
fuzzer.send(net,11);
TimeUnit.SECONDS.sleep(1);
fuzzer.send(net,4);
TimeUnit.SECONDS.sleep(1);
fuzzer.send(net,100); // the rest
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid text message, 1 frame/fragment (slowly, and split within code points)
*/
@Test
@Slow
public void testCase6_4_4() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5F49080808080656469746564");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
ByteBuffer net = fuzzer.asNetworkBuffer(send);
fuzzer.send(net,6);
fuzzer.send(net,11);
TimeUnit.SECONDS.sleep(1);
fuzzer.send(net,1);
TimeUnit.SECONDS.sleep(1);
fuzzer.send(net,100); // the rest
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* valid utf8 text message, 1 frame/fragment.
*/
@Test
public void testCase6_5_1() throws Exception
{
byte msg[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* invalid utf8 (incomplete code point) text message, 1 frame/fragment.
*/
@Test
public void testCase6_6_1() throws Exception
{
byte incomplete[] = Hex.asByteArray("CE");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(incomplete));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid utf8 text message, 1 frame/fragment, 4 valid code points + 1 partial code point
*/
@Test
public void testCase6_6_10() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCE");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* valid utf8 text message, 1 frame/fragment, 5 valid code points (preserved on echo).
*/
@Test
public void testCase6_6_11() throws Exception
{
byte msg[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* valid utf8 text message, 1 frame/fragment, 1 valid code point (preserved on echo).
*/
@Test
public void testCase6_6_2() throws Exception
{
byte msg[] = Hex.asByteArray("CEBA");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* invalid utf8 text message, 1 frame/fragment, 1 valid code point + 1 partial code point
*/
@Test
public void testCase6_6_3() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* invalid utf8 text message, 1 frame/fragment, 1 valid code point + 1 invalid code point
*/
@Test
public void testCase6_6_4() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BD");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* valid utf8 text message, 1 frame/fragment, 2 valid code points (preserved on echo).
*/
@Test
public void testCase6_6_5() throws Exception
{
byte msg[] = Hex.asByteArray("CEBAE1BDB9");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* invalid utf8 text message, 1 frame/fragment, 2 valid code points + 1 partial code point
*/
@Test
public void testCase6_6_6() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* valid utf8 text message, 1 frame/fragment, 3 valid code points (preserved on echo).
*/
@Test
public void testCase6_6_7() throws Exception
{
byte msg[] = Hex.asByteArray("CEBAE1BDB9CF83");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
/**
* invalid utf8 text message, 1 frame/fragment, 3 valid code points + 1 partial code point
*/
@Test
public void testCase6_6_8() throws Exception
{
byte invalid[] = Hex.asByteArray("CEBAE1BDB9CF83CE");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(invalid));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try
{
fuzzer.connect();
fuzzer.setSendMode(Fuzzer.SendMode.BULK);
fuzzer.send(send);
fuzzer.expect(expect);
}
finally
{
fuzzer.close();
}
}
/**
* valid utf8 text message, 1 frame/fragment, 4 valid code points (preserved on echo).
*/
@Test
public void testCase6_6_9() throws Exception
{
byte msg[] = Hex.asByteArray("CEBAE1BDB9CF83CEBC");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new WebSocketFrame(OpCode.TEXT).setPayload(msg));
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();
}
}
}

View File

@ -550,6 +550,14 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
BufferUtil.writeTo(buf,out);
}
public void writeRaw(ByteBuffer buf, int numBytes) throws IOException
{
int len = Math.min(numBytes,buf.remaining());
byte arr[] = new byte[len];
buf.get(arr,0,len);
out.write(arr);
}
public void writeRaw(String str) throws IOException
{
LOG.debug("write((String)[{}]){}{})",str.length(),'\n',str);
@ -558,20 +566,9 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
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)
while (buf.remaining() > 0)
{
buf.position(pos);
limit = Math.min(origLimit,pos + segmentSize);
buf.limit(limit);
len = buf.remaining();
overallLeft -= len;
pos += len;
writeRaw(buf);
writeRaw(buf,segmentSize);
flush();
}
}

View File

@ -0,0 +1,60 @@
//========================================================================
//Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
//------------------------------------------------------------------------
//All rights reserved. This program and the accompanying materials
//are made available under the terms of the Eclipse Public License v1.0
//and Apache License v2.0 which accompanies this distribution.
//The Eclipse Public License is available at
//http://www.eclipse.org/legal/epl-v10.html
//The Apache License v2.0 is available at
//http://www.opensource.org/licenses/apache2.0.php
//You may elect to redistribute this code under either of these licenses.
//========================================================================
package org.eclipse.jetty.websocket.server.helper;
public final class Hex
{
private static final char[] hexcodes = "0123456789ABCDEF".toCharArray();
public static byte[] asByteArray(String hstr)
{
if ((hstr.length() < 0) || ((hstr.length() % 2) != 0))
{
throw new IllegalArgumentException(String.format("Invalid string length of <%d>",hstr.length()));
}
int size = hstr.length() / 2;
byte buf[] = new byte[size];
byte hex;
int len = hstr.length();
int idx = (int)Math.floor(((size * 2) - (double)len) / 2);
for (int i = 0; i < len; i++)
{
hex = 0;
if (i >= 0)
{
hex = (byte)(Character.digit(hstr.charAt(i),16) << 4);
}
i++;
hex += (byte)(Character.digit(hstr.charAt(i),16));
buf[idx] = hex;
idx++;
}
return buf;
}
public static String asHex(byte buf[])
{
int len = buf.length;
char out[] = new char[len * 2];
for (int i = 0; i < len; i++)
{
out[i * 2] = hexcodes[(buf[i] & 0xF0) >> 4];
out[(i * 2) + 1] = hexcodes[(buf[i] & 0x0F)];
}
return String.valueOf(out);
}
}