Search in sources :

Example 21 with UnitParser

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));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 22 with UnitParser

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"));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 23 with UnitParser

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));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) MessageTooLargeException(org.eclipse.jetty.websocket.api.MessageTooLargeException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) MaskedByteBuffer(org.eclipse.jetty.websocket.common.util.MaskedByteBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 24 with UnitParser

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));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) MaskedByteBuffer(org.eclipse.jetty.websocket.common.util.MaskedByteBuffer) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 25 with UnitParser

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));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) MaskedByteBuffer(org.eclipse.jetty.websocket.common.util.MaskedByteBuffer) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)53 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)53 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)53 Test (org.junit.Test)52 Parser (org.eclipse.jetty.websocket.common.Parser)27 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)24 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)23 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)19 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)13 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)8 ProtocolException (org.eclipse.jetty.websocket.api.ProtocolException)7 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)7 ArrayList (java.util.ArrayList)6 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)6 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)4 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)4 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)3 MessageTooLargeException (org.eclipse.jetty.websocket.api.MessageTooLargeException)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1