Cleaning up test cases

This commit is contained in:
Joakim Erdfelt 2012-06-26 16:41:39 -07:00
parent e32249c24f
commit fabb2e18a2
3 changed files with 651 additions and 657 deletions

View File

@ -1,72 +1,25 @@
package org.eclipse.jetty.websocket.ab;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.Debug;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.TextFrame;
import org.eclipse.jetty.websocket.generator.Generator;
import org.eclipse.jetty.websocket.parser.FrameParseCapture;
import org.eclipse.jetty.websocket.parser.Parser;
import org.eclipse.jetty.websocket.parser.TextPayloadParser;
import org.junit.Assert;
import org.junit.Test;
/**
* Text Message Spec testing the {@link Generator} and {@link Parser}
*/
public class ABCase1_1
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
@Test
public void testGenerateEmptyTextCase1_1_1()
{
TextFrame textFrame = new TextFrame("");
textFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(10);
generator.generate(actual, textFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x81, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParseEmptyTextCase1_1_1()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x81, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("TextFrame.payload",0,pActual.getPayload());
}
private WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
@Test
public void testGenerate125ByteTextCase1_1_2()
@ -108,39 +61,6 @@ public class ABCase1_1
}
@Test
public void testParse125ByteTextCase1_1_2()
{
int length = 125;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7F;
expected.put(b);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate126ByteTextCase1_1_3()
{
@ -185,40 +105,6 @@ public class ABCase1_1
}
@Test
public void testParse126ByteTextCase1_1_3()
{
int length = 126;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate127ByteTextCase1_1_4()
{
@ -263,40 +149,6 @@ public class ABCase1_1
}
@Test
public void testParse127ByteTextCase1_1_4()
{
int length = 127;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate128ByteTextCase1_1_5()
{
@ -341,6 +193,213 @@ public class ABCase1_1
}
@Test
public void testGenerate65535ByteTextCase1_1_6()
{
int length = 65535;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(length + 16);
generator.generate(actual,textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]
{ (byte)0xff, (byte)0xff });
for (int i = 0; i < length; ++i)
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testGenerate65536ByteTextCase1_1_7()
{
int length = 65536;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(length + 16);
generator.generate(actual,textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 11);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7F;
expected.put(b);
expected.put(new byte[]
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 });
for (int i = 0; i < length; ++i)
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testGenerateEmptyTextCase1_1_1()
{
TextFrame textFrame = new TextFrame("");
textFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(10);
generator.generate(actual,textFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x81, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse125ByteTextCase1_1_2()
{
int length = 125;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7F;
expected.put(b);
for (int i = 0; i < length; ++i)
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse126ByteTextCase1_1_3()
{
int length = 126;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for (int i = 0; i < length; ++i)
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse127ByteTextCase1_1_4()
{
int length = 127;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for (int i = 0; i < length; ++i)
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse128ByteTextCase1_1_5()
{
@ -375,47 +434,6 @@ public class ABCase1_1
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate65535ByteTextCase1_1_6()
{
int length = 65535;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(length+16);
generator.generate(actual, textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]{ (byte)0xff, (byte)0xff});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse65535ByteTextCase1_1_6()
{
@ -431,7 +449,8 @@ public class ABCase1_1
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]{ (byte)0xff, (byte)0xff});
expected.put(new byte[]
{ (byte)0xff, (byte)0xff });
for (int i = 0; i < length; ++i)
{
@ -454,49 +473,6 @@ public class ABCase1_1
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate65536ByteTextCase1_1_7()
{
int length = 65536;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(length+16);
generator.generate(actual, textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 11);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7F;
expected.put(b);
expected.put(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse65536ByteTextCase1_1_7()
{
@ -509,7 +485,8 @@ public class ABCase1_1
byte b = 0x00; // no masking
b |= 0x7F;
expected.put(b);
expected.put(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00});
expected.put(new byte[]
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 });
for (int i = 0; i < length; ++i)
{
@ -532,4 +509,28 @@ public class ABCase1_1
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testParseEmptyTextCase1_1_1()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x81, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("TextFrame.payload",0,pActual.getPayload());
}
}

View File

@ -1,76 +1,26 @@
package org.eclipse.jetty.websocket.ab;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.Debug;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.BinaryFrame;
import org.eclipse.jetty.websocket.frames.TextFrame;
import org.eclipse.jetty.websocket.generator.Generator;
import org.eclipse.jetty.websocket.parser.FrameParseCapture;
import org.eclipse.jetty.websocket.parser.Parser;
import org.eclipse.jetty.websocket.parser.TextPayloadParser;
import org.junit.Assert;
import org.junit.Test;
/**
* Binary Message Spec testing the {@link Generator} and {@link Parser}
*/
public class ABCase1_2
{
StandardByteBufferPool bufferPool = new StandardByteBufferPool();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
@Test
public void testGenerateEmptyBinaryCase1_2_1()
{
BinaryFrame binaryFrame = new BinaryFrame(new byte[]{});
binaryFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, binaryFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x82, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParseEmptyBinaryCase1_2_1()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x82, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("BinaryFrame.payload",0,pActual.getPayload());
}
private WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
@Test
public void testGenerate125ByteBinaryCase1_2_2()
@ -116,39 +66,6 @@ public class ABCase1_2
}
@Test
public void testParse125ByteBinaryCase1_2_2()
{
int length = 125;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= length & 0x7F;
expected.put(b);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate126ByteBinaryCase1_2_3()
{
@ -197,40 +114,6 @@ public class ABCase1_2
}
@Test
public void testParse126ByteBinaryCase1_2_3()
{
int length = 126;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate127ByteBinaryCase1_2_4()
{
@ -279,40 +162,6 @@ public class ABCase1_2
}
@Test
public void testParse127ByteBinaryCase1_2_4()
{
int length = 127;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate128ByteBinaryCase1_2_5()
{
@ -359,40 +208,6 @@ public class ABCase1_2
}
@Test
public void testParse128ByteBinaryCase1_2_5()
{
int length = 128;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate65535ByteBinaryCase1_2_6()
{
@ -437,42 +252,6 @@ public class ABCase1_2
}
@Test
public void testParse65535ByteBinaryCase1_2_6()
{
int length = 65535;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]{ (byte)0xff, (byte)0xff});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate65536ByteBinaryCase1_2_7()
{
@ -518,6 +297,199 @@ public class ABCase1_2
}
@Test
public void testGenerateEmptyBinaryCase1_2_1()
{
BinaryFrame binaryFrame = new BinaryFrame(new byte[]{});
binaryFrame.setFin(true);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, binaryFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x82, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse125ByteBinaryCase1_2_2()
{
int length = 125;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= length & 0x7F;
expected.put(b);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse126ByteBinaryCase1_2_3()
{
int length = 126;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse127ByteBinaryCase1_2_4()
{
int length = 127;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse128ByteBinaryCase1_2_5()
{
int length = 128;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse65535ByteBinaryCase1_2_6()
{
int length = 65535;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x82 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]{ (byte)0xff, (byte)0xff});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testParse65536ByteBinaryCase1_2_7()
{
@ -553,4 +525,28 @@ public class ABCase1_2
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("BinaryFrame.payload",length,pActual.getPayload());
}
@Test
public void testParseEmptyBinaryCase1_2_1()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x82, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(BinaryFrame.class,1);
BinaryFrame pActual = (BinaryFrame)capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("BinaryFrame.payload",0,pActual.getPayload());
}
}

View File

@ -5,7 +5,6 @@ import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.Debug;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
@ -18,8 +17,6 @@ public class ClosePayloadParserTest
@Test
public void testGameOver()
{
Debug.enableDebugLogging(Parser.class);
Debug.enableDebugLogging(ClosePayloadParser.class);
String expectedReason = "Game Over";
byte utf[] = expectedReason.getBytes(StringUtil.__UTF8_CHARSET);