use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleUnmasked64KByteBinaryMessage.
@Test
public void testSingleUnmasked64KByteBinaryMessage() {
int dataSize = 1024 * 64;
ByteBuffer buf = ByteBuffer.allocate((dataSize + 10));
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// 64 Kbytes binary message in a single unmasked frame
buf.put(new byte[] { (byte) 0x82, 0x7F });
// 64bit size
buf.putLong(dataSize);
for (int i = 0; i < dataSize; i++) {
buf.put((byte) 0x77);
}
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.BINARY, 1);
Frame bin = capture.getFrames().poll();
Assert.assertThat("BinaryFrame.payloadLength", bin.getPayloadLength(), is(dataSize));
ByteBuffer data = bin.getPayload();
Assert.assertThat("BinaryFrame.payload.length", data.remaining(), is(dataSize));
for (int i = 0; i < dataSize; i++) {
Assert.assertThat("BinaryFrame.payload[" + i + "]", data.get(i), is((byte) 0x77));
}
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleMaskedPongRequest.
@Test
public void testSingleMaskedPongRequest() {
ByteBuffer buf = ByteBuffer.allocate(16);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// Unmasked Pong request
buf.put(new byte[] { (byte) 0x8a, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.PONG, 1);
WebSocketFrame pong = capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(pong.getPayload());
Assert.assertThat("PongFrame.payload", actual, is("Hello"));
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class TextPayloadParserTest method testFrameTooLargeDueToPolicy.
@Test
public void testFrameTooLargeDueToPolicy() throws Exception {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
// Artificially small buffer/payload
// read buffer
policy.setInputBufferSize(1024);
// streaming buffer (not used in this test)
policy.setMaxTextMessageBufferSize(1024);
// actual maximum text message size policy
policy.setMaxTextMessageSize(1024);
byte[] utf = new byte[2048];
Arrays.fill(utf, (byte) 'a');
Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 8);
// text frame, fin = true
buf.put((byte) 0x81);
// 0x7E == 126 (a 2 byte payload length)
buf.put((byte) (0x80 | 0x7E));
buf.putShort((short) utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
UnitParser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parseQuietly(buf);
capture.assertHasErrors(MessageTooLargeException.class, 1);
capture.assertHasNoFrames();
MessageTooLargeException err = (MessageTooLargeException) capture.getErrors().poll();
Assert.assertThat("Error.closeCode", err.getStatusCode(), is(StatusCode.MESSAGE_TOO_LARGE));
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class TextPayloadParserTest method testMediumMaskedText.
@Test
public void testMediumMaskedText() throws Exception {
StringBuffer sb = new StringBuffer();
;
for (int i = 0; i < 14; i++) {
sb.append("Hello Medium World ");
}
sb.append(". The end.");
String expectedText = sb.toString();
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 10);
buf.put((byte) 0x81);
// 0x7E == 126 (a 2 byte payload length)
buf.put((byte) (0x80 | 0x7E));
buf.putShort((short) utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
WebSocketFrame txt = capture.getFrames().poll();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class TextPayloadParserTest method testShortMaskedUtf8Text.
@Test
public void testShortMaskedUtf8Text() throws Exception {
String expectedText = "Hello World";
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(24);
buf.put((byte) 0x81);
buf.put((byte) (0x80 | utf.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.TEXT, 1);
WebSocketFrame txt = capture.getFrames().poll();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
Aggregations