additional test cases coming, fix to parser for empty payloads and improvement to frame parser payload length insertion to byte buffer
This commit is contained in:
parent
5fb55086d4
commit
ec3052d018
|
@ -68,23 +68,40 @@ public abstract class FrameGenerator<T extends BaseFrame>
|
|||
|
||||
// payload lengths
|
||||
int payloadLength = frame.getPayloadLength();
|
||||
if ((payloadLength >= 0x7F) && (payloadLength <= 0xFF_FF))
|
||||
{
|
||||
// we have a 16 bit length
|
||||
b |= 0x7E;
|
||||
framing.put(b); // indicate 2 byte length
|
||||
framing.putShort((short)(payloadLength & 0xFF_FF)); // write 2 byte length
|
||||
}
|
||||
else if (payloadLength >= 0xFFFF)
|
||||
|
||||
|
||||
/*
|
||||
* if length is over 65535 then its a 7 + 64 bit length
|
||||
*/
|
||||
if (payloadLength > 0xFF_FF)
|
||||
{
|
||||
// we have a 64 bit length
|
||||
b |= 0x7F;
|
||||
framing.put(b); // indicate 4 byte length
|
||||
framing.putInt(payloadLength); // write 4 byte length
|
||||
framing.put((byte)0);
|
||||
framing.put((byte)0);
|
||||
framing.put((byte)0);
|
||||
framing.put((byte)0);
|
||||
framing.put((byte)((payloadLength>>24) & 0xFF));
|
||||
framing.put((byte)((payloadLength>>16) & 0xFF));
|
||||
framing.put((byte)((payloadLength>>8) & 0xFF));
|
||||
framing.put((byte)(payloadLength & 0xFF));
|
||||
}
|
||||
/*
|
||||
* if payload is ge 126 we have a 7 + 16 bit length
|
||||
*/
|
||||
else if (payloadLength >= 0x7E)
|
||||
{
|
||||
b |= 0x7E;
|
||||
framing.put(b); // indicate 2 byte length
|
||||
framing.put((byte)((payloadLength>>8) & 0xFF));
|
||||
framing.put((byte)(payloadLength & 0xFF));
|
||||
}
|
||||
/*
|
||||
* we have a 7 bit length
|
||||
*/
|
||||
else
|
||||
{
|
||||
// we have a 7 bit length
|
||||
b |= (payloadLength & 0x7F);
|
||||
framing.put(b);
|
||||
}
|
||||
|
|
|
@ -164,6 +164,23 @@ public class Parser
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* if the payload was empty we could end up in this state
|
||||
* because there was no remaining bits to process
|
||||
*/
|
||||
if ( state == State.PAYLOAD )
|
||||
{
|
||||
parser.getFrame().setPayload(ByteBuffer.allocate(0));
|
||||
notifyFrame( parser.getFrame() );
|
||||
parser.reset();
|
||||
if (parser.getFrame().isFin())
|
||||
{
|
||||
reset();
|
||||
}
|
||||
state = State.FINOP;
|
||||
}
|
||||
|
||||
}
|
||||
catch (WebSocketException e)
|
||||
{
|
||||
|
@ -174,7 +191,7 @@ public class Parser
|
|||
notifyWebSocketException(new WebSocketException(t));
|
||||
}
|
||||
finally
|
||||
{
|
||||
{
|
||||
// Be sure to consume after exceptions
|
||||
buffer.position(buffer.limit());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,220 @@
|
|||
package org.eclipse.jetty.websocket.ab;
|
||||
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.io.StandardByteBufferPool;
|
||||
import org.eclipse.jetty.websocket.ByteBufferAssert;
|
||||
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.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ABCase1
|
||||
{
|
||||
StandardByteBufferPool bufferPool = new StandardByteBufferPool();
|
||||
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
|
||||
|
||||
|
||||
@Test
|
||||
public void testGenerateEmptyTextCase1_1_1()
|
||||
{
|
||||
TextFrame textFrame = new TextFrame("");
|
||||
textFrame.setFin(true);
|
||||
|
||||
Generator generator = new Generator(bufferPool,policy);
|
||||
ByteBuffer actual = generator.generate(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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerate125ByteTextCase1_1_2()
|
||||
{
|
||||
int length = 125;
|
||||
|
||||
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(bufferPool,policy);
|
||||
ByteBuffer actual = generator.generate(textFrame);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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 testGenerate126ByteTextCase1_1_3()
|
||||
{
|
||||
int length = 126;
|
||||
|
||||
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(bufferPool,policy);
|
||||
ByteBuffer actual = generator.generate(textFrame);
|
||||
|
||||
ByteBuffer expected = ByteBuffer.allocate(length + 5);
|
||||
|
||||
expected.put(new byte[]
|
||||
{ (byte)0x81 });
|
||||
|
||||
byte b = 0x00; // no masking
|
||||
b |= length & 0x7E;
|
||||
expected.put(b);
|
||||
|
||||
//expected.put((byte)((length>>8) & 0xFF));
|
||||
//expected.put((byte)(length & 0xFF));
|
||||
expected.putShort((short)length);
|
||||
|
||||
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 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());
|
||||
}
|
||||
}
|
|
@ -232,7 +232,7 @@ public class RFC6455ExamplesGeneratorTest
|
|||
|
||||
ByteBuffer expected = ByteBuffer.allocate(dataSize + 10);
|
||||
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
|
||||
// 256 bytes binary message in a single unmasked frame
|
||||
// 64k bytes binary message in a single unmasked frame
|
||||
expected.put(new byte[]
|
||||
{ (byte)0x82, (byte)0x7F });
|
||||
expected.putInt(0x0000000000010000);
|
||||
|
|
Loading…
Reference in New Issue