Merge branch 'jetty-9' of ssh://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project into jetty-9

This commit is contained in:
Joakim Erdfelt 2012-07-06 13:37:25 -07:00
commit a5424e65e3
8 changed files with 356 additions and 386 deletions

View File

@ -6,9 +6,9 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.PolicyViolationException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
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.util.CloseUtil;
/**
* <pre>
@ -153,7 +153,8 @@ public class FrameGenerator
if (frame.getOpCode() == OpCode.CLOSE)
{
CloseUtil.assertValidPayload(frame);
// validate the close
new CloseInfo(frame.getPayloadData(),true);
}
// copy payload
buffer.put(frame.getPayloadData());

View File

@ -71,11 +71,21 @@ public class FrameBuilder
return new FrameBuilder(new WebSocketFrame(OpCode.PING));
}
public static FrameBuilder ping(String message)
{
return new FrameBuilder(new WebSocketFrame(OpCode.PING)).payload(message.getBytes(StringUtil.__UTF8_CHARSET));
}
public static FrameBuilder pong()
{
return new FrameBuilder(new WebSocketFrame(OpCode.PONG));
}
public static FrameBuilder pong(String message)
{
return new FrameBuilder(new WebSocketFrame(OpCode.PONG)).payload(message.getBytes(StringUtil.__UTF8_CHARSET));
}
public static FrameBuilder text()
{
return new FrameBuilder(new WebSocketFrame(OpCode.TEXT));
@ -83,7 +93,7 @@ public class FrameBuilder
public static FrameBuilder text(String text)
{
return new FrameBuilder(new WebSocketFrame(OpCode.TEXT)).payload(text.getBytes());
return new FrameBuilder(new WebSocketFrame(OpCode.TEXT)).payload(text.getBytes(StringUtil.__UTF8_CHARSET));
}
private WebSocketFrame frame;

View File

@ -12,6 +12,12 @@ public class CloseUtil
public static void assertValidPayload(WebSocketFrame frame)
{
byte payload[] = frame.getPayloadData();
if (payload.length < 2)
{
return; // no status code
}
int statusCode = getStatusCode(payload);
// Validate value
@ -43,10 +49,6 @@ public class CloseUtil
public static int getStatusCode(byte[] payload)
{
if (payload.length < 2)
{
return 0; // no status code
}
int statusCode = 0;
ByteBuffer bb = ByteBuffer.wrap(payload);

View File

@ -1,6 +1,6 @@
package org.eclipse.jetty.websocket.ab;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import java.nio.ByteBuffer;
@ -8,10 +8,12 @@ import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.PingFrame;
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.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Assert;
import org.junit.Test;
@ -23,14 +25,14 @@ public class TestABCase2
public void testGenerate125OctetPingCase2_4()
{
byte[] bytes = new byte[125];
for ( int i = 0 ; i < bytes.length ; ++i )
{
bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
}
PingFrame pingFrame = new PingFrame(bytes);
WebSocketFrame pingFrame = FrameBuilder.ping().payload(bytes).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(bytes.length + 32);
generator.generate(actual, pingFrame);
@ -39,26 +41,26 @@ public class TestABCase2
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= bytes.length & 0x7F;
expected.put(b);
expected.put(bytes);
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testGenerateBinaryPingCase2_3()
{
byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
PingFrame pingFrame = new PingFrame(bytes);
WebSocketFrame pingFrame = FrameBuilder.ping().payload(bytes).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, pingFrame);
@ -67,25 +69,25 @@ public class TestABCase2
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= bytes.length & 0x7F;
expected.put(b);
expected.put(bytes);
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testGenerateEmptyPingCase2_1()
{
PingFrame pingFrame = new PingFrame();
WebSocketFrame pingFrame = FrameBuilder.ping().asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
@ -95,21 +97,21 @@ public class TestABCase2
expected.put(new byte[]
{ (byte)0x89, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testGenerateHelloPingCase2_2()
{
String message = "Hello, world!";
byte[] messageBytes = message.getBytes();
PingFrame pingFrame = new PingFrame(messageBytes);
WebSocketFrame pingFrame = FrameBuilder.text(message).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, pingFrame);
@ -118,16 +120,16 @@ public class TestABCase2
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= messageBytes.length & 0x7F;
expected.put(b);
expected.put(messageBytes);
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@ -135,122 +137,123 @@ public class TestABCase2
public void testGenerateOversizedBinaryPingCase2_5_A()
{
byte[] bytes = new byte[126];
for ( int i = 0 ; i < bytes.length ; ++i )
{
bytes[i] = 0x00;
}
new PingFrame(bytes);
FrameBuilder.ping().payload(bytes);
}
@Test( expected=WebSocketException.class )
public void testGenerateOversizedBinaryPingCase2_5_B()
{
byte[] bytes = new byte[126];
for ( int i = 0 ; i < bytes.length ; ++i )
{
bytes[i] = 0x00;
}
PingFrame pingFrame = new PingFrame();
pingFrame.setPayload(ByteBuffer.allocate(bytes.length + 32).put(bytes));
WebSocketFrame pingFrame = FrameBuilder.ping().payload(bytes).asFrame();
Generator generator = new Generator(WebSocketPolicy.newServerPolicy());
generator.generate(ByteBuffer.allocate(bytes.length + 32),pingFrame);
}
@Test
public void testParse125OctetPingCase2_4()
{
{
byte[] bytes = new byte[125];
for ( int i = 0 ; i < bytes.length ; ++i )
{
bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
}
ByteBuffer expected = ByteBuffer.allocate(bytes.length + 32);
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= bytes.length & 0x7F;
expected.put(b);
expected.put(bytes);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(PingFrame.class,1);
PingFrame pActual = (PingFrame)capture.getFrames().get(0);
capture.assertHasFrame(OpCode.PING,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(bytes.length));
ByteBufferAssert.assertSize("PingFrame.payload",bytes.length,pActual.getPayload());
Assert.assertEquals("PingFrame.payload",bytes.length,pActual.getPayloadLength());
}
@Test
public void testParseBinaryPingCase2_3()
{
{
byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= bytes.length & 0x7F;
expected.put(b);
expected.put(bytes);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(PingFrame.class,1);
PingFrame pActual = (PingFrame)capture.getFrames().get(0);
capture.assertHasFrame(OpCode.PING,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(bytes.length));
ByteBufferAssert.assertSize("PingFrame.payload",bytes.length,pActual.getPayload());
Assert.assertEquals("PingFrame.payload",bytes.length,pActual.getPayloadLength());
}
@Test
public void testParseEmptyPingCase2_1()
{
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x89, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(PingFrame.class,1);
PingFrame pActual = (PingFrame)capture.getFrames().get(0);
capture.assertHasFrame(OpCode.PING,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("PingFrame.payload",0,pActual.getPayload());
Assert.assertEquals("PingFrame.payload",0,pActual.getPayloadLength());
}
@Test
public void testParseHelloPingCase2_2()
{
{
String message = "Hello, world!";
byte[] messageBytes = message.getBytes();
@ -258,57 +261,57 @@ public class TestABCase2
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= messageBytes.length & 0x7F;
expected.put(b);
expected.put(messageBytes);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(PingFrame.class,1);
PingFrame pActual = (PingFrame)capture.getFrames().get(0);
capture.assertHasFrame(OpCode.PING,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(message.length()));
ByteBufferAssert.assertSize("PingFrame.payload",message.length(),pActual.getPayload());
Assert.assertEquals("PingFrame.payload",message.length(),pActual.getPayloadLength());
}
@Test
public void testParseOversizedBinaryPingCase2_5()
{
{
byte[] bytes = new byte[126];
for ( int i = 0 ; i < bytes.length ; ++i )
{
bytes[i] = 0x00;
}
ByteBuffer expected = ByteBuffer.allocate(bytes.length + 32);
expected.put(new byte[]
{ (byte)0x89 });
byte b = 0x00; // no masking
b |= bytes.length & 0x7F;
expected.put(b);
expected.put(bytes);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
Assert.assertEquals( "error should be returned for too large of ping payload", 1, capture.getErrorCount(WebSocketException.class)) ;
}
}

View File

@ -1,81 +1,65 @@
package org.eclipse.jetty.websocket.ab;
import org.eclipse.jetty.websocket.frames.CloseFrame;
import org.eclipse.jetty.websocket.frames.PingFrame;
import org.eclipse.jetty.websocket.frames.PongFrame;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.jetty.websocket.api.PolicyViolationException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.generator.Generator;
import org.eclipse.jetty.websocket.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class TestABCase3
{
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV1PingFrame()
@Parameters
public static Collection<WebSocketFrame[]> data()
{
PingFrame pingFrame = new PingFrame();
pingFrame.setRsv1(true);
List<WebSocketFrame[]> data = new ArrayList<>();
// @formatter:off
data.add(new WebSocketFrame[]
{ FrameBuilder.ping().rsv1(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.ping().rsv2(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.ping().rsv3(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.pong().rsv1(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.pong().rsv2(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.pong().rsv3(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.close().rsv1(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.close().rsv2(true).asFrame() });
data.add(new WebSocketFrame[]
{ FrameBuilder.close().rsv3(true).asFrame() });
// @formatter:on
return data;
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV2PingFrame()
private WebSocketFrame invalidFrame;
public TestABCase3(WebSocketFrame invalidFrame)
{
PingFrame pingFrame = new PingFrame();
pingFrame.setRsv2(true);
this.invalidFrame = invalidFrame;
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV3PingFrame()
{
PingFrame pingFrame = new PingFrame();
pingFrame.setRsv3(true);
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV1PongFrame()
{
PongFrame pongFrame = new PongFrame();
pongFrame.setRsv1(true);
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV2PongFrame()
{
PongFrame pongFrame = new PongFrame();
pongFrame.setRsv2(true);
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV3PongFrame()
{
PongFrame pongFrame = new PongFrame();
pongFrame.setRsv3(true);
}
@Test( expected=IllegalArgumentException.class )
@Test(expected = PolicyViolationException.class)
public void testGenerateRSV1CloseFrame()
{
CloseFrame closeFrame = new CloseFrame();
closeFrame.setRsv1(true);
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV2CloseFrame()
{
CloseFrame closeFrame = new CloseFrame();
closeFrame.setRsv2(true);
}
@Test( expected=IllegalArgumentException.class )
public void testGenerateRSV3CloseFrame()
{
CloseFrame closeFrame = new CloseFrame();
closeFrame.setRsv3(true);
Generator generator = new Generator(WebSocketPolicy.newServerPolicy());
generator.generate(ByteBuffer.allocate(32),invalidFrame);
}
}

View File

@ -1,6 +1,6 @@
package org.eclipse.jetty.websocket.ab;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import java.nio.ByteBuffer;
@ -9,10 +9,12 @@ import org.eclipse.jetty.websocket.api.ProtocolException;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.CloseFrame;
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.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Assert;
import org.junit.Test;
@ -20,11 +22,55 @@ public class TestABCase7_3
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
@Test (expected = WebSocketException.class)
public void testGenerate1BytePayloadCloseCase7_3_2()
@Test
public void testCase7_3_1GenerateEmptyClose()
{
CloseFrame closeFrame = new CloseFrame();
closeFrame.setPayload(new byte[] {0x00});
WebSocketFrame closeFrame = FrameBuilder.close().asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, closeFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x88, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testCase7_3_1ParseEmptyClose()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x88, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.CLOSE,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(0));
}
@Test (expected = WebSocketException.class)
public void testCase7_3_2Generate1BytePayloadClose()
{
WebSocketFrame closeFrame = FrameBuilder.close().payload(new byte[]
{ 0x00 }).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
@ -32,9 +78,31 @@ public class TestABCase7_3
}
@Test
public void testGenerateCloseWithStatusCase7_3_3()
public void testCase7_3_2Parse1BytePayloadClose()
{
CloseFrame closeFrame = new CloseFrame(1000);
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88, 0x01, 0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
Assert.assertEquals( "error on invalid close payload", 1, capture.getErrorCount(WebSocketException.class)) ;
WebSocketException known = capture.getErrors().get(0);
Assert.assertTrue("invalid payload should be in message",known.getMessage().contains("invalid payload length"));
}
@Test
public void testCase7_3_3GenerateCloseWithStatus()
{
WebSocketFrame closeFrame = FrameBuilder.close(1000).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
@ -51,9 +119,92 @@ public class TestABCase7_3
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testCase7_3_3ParseCloseWithStatus()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x88, (byte)0x02, 0x03, (byte)0xe8 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.CLOSE,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(2));
}
@Test
public void testGenerateCloseWithStatusMaxReasonCase7_3_5()
public void testCase7_3_4GenerateCloseWithStatusReason()
{
String message = "bad cough";
byte[] messageBytes = message.getBytes();
WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, closeFrame);
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88 });
byte b = 0x00; // no masking
b |= (message.length() + 2) & 0x7F;
expected.put(b);
expected.putShort((short)1000);
expected.put(messageBytes);
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testCase7_3_4ParseCloseWithStatusReason()
{
String message = "bad cough";
byte[] messageBytes = message.getBytes();
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88 });
byte b = 0x00; // no masking
b |= (messageBytes.length + 2) & 0x7F;
expected.put(b);
expected.putShort((short)1000);
expected.put(messageBytes);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.CLOSE,1);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(messageBytes.length + 2));
}
@Test
public void testCase7_3_5GenerateCloseWithStatusMaxReason()
{
StringBuilder message = new StringBuilder();
for ( int i = 0 ; i < 123 ; ++i )
@ -63,7 +214,7 @@ public class TestABCase7_3
byte[] messageBytes = message.toString().getBytes();
CloseFrame closeFrame = new CloseFrame(1000, message.toString());
WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(132);
@ -88,120 +239,8 @@ public class TestABCase7_3
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test(expected = ProtocolException.class)
public void testGenerateCloseWithStatusMaxReasonCase7_3_6()
{
StringBuilder message = new StringBuilder();
for ( int i = 0 ; i < 124 ; ++i )
{
message.append("*");
}
byte[] messageBytes = message.toString().getBytes();
CloseFrame closeFrame = new CloseFrame(1000, message.toString());
}
@Test
public void testGenerateCloseWithStatusReasonCase7_3_4()
{
String message = "bad cough";
byte[] messageBytes = message.getBytes();
CloseFrame closeFrame = new CloseFrame(1000, message);
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, closeFrame);
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88 });
byte b = 0x00; // no masking
b |= (message.length() + 2) & 0x7F;
expected.put(b);
expected.putShort((short)1000);
expected.put(messageBytes);
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testGenerateEmptyCloseCase7_3_1()
{
CloseFrame closeFrame = new CloseFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, closeFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x88, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse1BytePayloadCloseCase7_3_2()
{
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88, 0x01, 0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
Assert.assertEquals( "error on invalid close payload", 1, capture.getErrorCount(WebSocketException.class)) ;
WebSocketException known = capture.getErrors().get(0);
Assert.assertTrue("invalid payload should be in message",known.getMessage().contains("invalid payload length"));
}
@Test
public void testParseCloseWithStatusCase7_3_3()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x88, (byte)0x02, 0x03, (byte)0xe8 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(CloseFrame.class,1);
CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(2));
ByteBufferAssert.assertSize("CloseFrame.payload",2,pActual.getPayload());
}
@Test
public void testParseCloseWithStatusMaxReasonCase7_3_5()
public void testCase7_3_5ParseCloseWithStatusMaxReason()
{
StringBuilder message = new StringBuilder();
for ( int i = 0 ; i < 123 ; ++i )
@ -230,16 +269,33 @@ public class TestABCase7_3
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(CloseFrame.class,1);
capture.assertHasFrame(OpCode.CLOSE,1);
CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(125));
ByteBufferAssert.assertSize("CloseFrame.payload", 125,pActual.getPayload());
}
@Test(expected = ProtocolException.class)
public void testCase7_3_6GenerateCloseWithInvalidStatusReason()
{
StringBuilder message = new StringBuilder();
for ( int i = 0 ; i < 124 ; ++i )
{
message.append("*");
}
byte[] messageBytes = message.toString().getBytes();
WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual,closeFrame);
}
@Test
public void testParseCloseWithStatusMaxReasonCase7_3_6()
public void testCase7_3_6ParseCloseWithStatusMaxReason()
{
StringBuilder message = new StringBuilder();
for ( int i = 0 ; i < 124 ; ++i )
@ -273,59 +329,4 @@ public class TestABCase7_3
Assert.assertTrue("invalid payload should be in message",known.getMessage().contains("invalid payload length"));
}
@Test
public void testParseCloseWithStatusReasonCase7_3_4()
{
String message = "bad cough";
byte[] messageBytes = message.getBytes();
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88 });
byte b = 0x00; // no masking
b |= (messageBytes.length + 2) & 0x7F;
expected.put(b);
expected.putShort((short)1000);
expected.put(messageBytes);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(CloseFrame.class,1);
CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(messageBytes.length + 2));
ByteBufferAssert.assertSize("CloseFrame.payload",messageBytes.length + 2,pActual.getPayload());
}
@Test
public void testParseEmptyCloseCase7_3_1()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x88, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(CloseFrame.class,1);
CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("CloseFrame.payload",0,pActual.getPayload());
}
}

View File

@ -1,18 +0,0 @@
package org.eclipse.jetty.websocket.generator;
import junit.framework.Assert;
import org.eclipse.jetty.websocket.frames.CloseFrame;
import org.eclipse.jetty.websocket.protocol.OpCode;
import org.junit.Test;
public class CloseFrameGeneratorTest
{
@Test
public void testGenerator() throws Exception
{
CloseFrame close = new CloseFrame();
Assert.assertEquals(OpCode.CLOSE,close.getOpCode());
}
}

View File

@ -3,12 +3,10 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.BufferUtil;
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.PingFrame;
import org.eclipse.jetty.websocket.frames.PongFrame;
import org.eclipse.jetty.websocket.protocol.FrameBuilder;
import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
import org.junit.Test;
@ -54,17 +52,11 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleMaskedPongRequest()
{
PongFrame pong = new PongFrame();
pong.setMask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d });
byte msg[] = "Hello".getBytes(StringUtil.__UTF8_CHARSET);
ByteBuffer payload = ByteBuffer.allocate(msg.length);
payload.put(msg);
pong.setPayload(payload);
WebSocketFrame pong = FrameBuilder.pong("Hello").mask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d }).asFrame();
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
PongFrameGenerator gen = new PongFrameGenerator(policy);
Generator gen = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
gen.generate(actual,pong);
@ -118,7 +110,7 @@ public class RFC6455ExamplesGeneratorTest
{
payload.put((byte)0x44);
}
binary.setPayload(payload);
binary.setPayload(BufferUtil.toArray(payload));
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
@ -155,7 +147,7 @@ public class RFC6455ExamplesGeneratorTest
{
payload.put((byte)0x44);
}
binary.setPayload(payload);
binary.setPayload(BufferUtil.toArray(payload));
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
Generator gen = new Generator(policy);
@ -197,16 +189,11 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleUnmaskedPingRequest() throws Exception
{
PingFrame ping = new PingFrame();
byte msg[] = "Hello".getBytes(StringUtil.__UTF8_CHARSET);
ByteBuffer payload = ByteBuffer.allocate(msg.length);
payload.put(msg);
ping.setPayload(payload);
WebSocketFrame ping = FrameBuilder.ping("Hello").asFrame();
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
PingFrameGenerator gen = new PingFrameGenerator(policy);
Generator gen = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
gen.generate(actual,ping);
actual.flip(); // make readable