Search in sources :

Example 41 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class RFC6455ExamplesParserTest method testFragmentedUnmaskedTextMessage.

@Test
public void testFragmentedUnmaskedTextMessage() {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    ByteBuffer buf = ByteBuffer.allocate(16);
    BufferUtil.clearToFill(buf);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A fragmented unmasked text message (part 1 of 2 "Hel")
    buf.put(new byte[] { (byte) 0x01, (byte) 0x03, 0x48, (byte) 0x65, 0x6c });
    // Parse #1
    BufferUtil.flipToFlush(buf, 0);
    parser.parse(buf);
    // part 2 of 2 "lo" (A continuation frame of the prior text message)
    BufferUtil.flipToFill(buf);
    buf.put(new byte[] { (byte) 0x80, 0x02, 0x6c, 0x6f });
    // Parse #2
    BufferUtil.flipToFlush(buf, 0);
    parser.parse(buf);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CONTINUATION, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame[0].data", actual, is("Hel"));
    txt = capture.getFrames().poll();
    actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame[1].data", actual, is("lo"));
}
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 42 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class TextPayloadParserTest method testShortMaskedText.

@Test
public void testShortMaskedText() 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)

Example 43 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class TextPayloadParserTest method testShortMaskedFragmentedText.

@Test
public void testShortMaskedFragmentedText() throws Exception {
    String part1 = "Hello ";
    String part2 = "World";
    byte[] b1 = part1.getBytes(StandardCharsets.UTF_8);
    byte[] b2 = part2.getBytes(StandardCharsets.UTF_8);
    ByteBuffer buf = ByteBuffer.allocate(32);
    // part 1
    // no fin + text
    buf.put((byte) 0x01);
    buf.put((byte) (0x80 | b1.length));
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, b1);
    // part 2
    // fin + continuation
    buf.put((byte) 0x80);
    buf.put((byte) (0x80 | b2.length));
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, b2);
    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);
    capture.assertHasFrame(OpCode.CONTINUATION, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    Assert.assertThat("TextFrame[0].data", txt.getPayloadAsUTF8(), is(part1));
    txt = capture.getFrames().poll();
    Assert.assertThat("TextFrame[1].data", txt.getPayloadAsUTF8(), is(part2));
}
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 44 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class TextPayloadParserTest method testLongMaskedText.

@Test
public void testLongMaskedText() throws Exception {
    StringBuffer sb = new StringBuffer();
    ;
    for (int i = 0; i < 3500; i++) {
        sb.append("Hello Big World ");
    }
    sb.append(". The end.");
    String expectedText = sb.toString();
    byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
    Assert.assertThat("Must be a long length payload", utf.length, greaterThan(0xFFFF));
    ByteBuffer buf = ByteBuffer.allocate(utf.length + 32);
    // text frame, fin = true
    buf.put((byte) 0x81);
    // 0x7F == 127 (a 8 byte payload length)
    buf.put((byte) (0x80 | 0x7F));
    buf.putLong(utf.length);
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, utf);
    buf.flip();
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    policy.setMaxTextMessageSize(100000);
    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 45 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class ParserTest method testParseCase5_19.

/**
     * Similar to the server side 5.19 testcase. text message, send in 5 frames/fragments, with 2 pings in the mix.
     */
@Test
public void testParseCase5_19() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload("f1").setFin(false));
    send.add(new ContinuationFrame().setPayload(",f2").setFin(false));
    send.add(new PingFrame().setPayload("pong-1"));
    send.add(new ContinuationFrame().setPayload(",f3").setFin(false));
    send.add(new ContinuationFrame().setPayload(",f4").setFin(false));
    send.add(new PingFrame().setPayload("pong-2"));
    send.add(new ContinuationFrame().setPayload(",f5").setFin(true));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parseQuietly(completeBuf);
    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CONTINUATION, 4);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PING, 2);
}
Also used : PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)62 ByteBuffer (java.nio.ByteBuffer)60 Test (org.junit.Test)59 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)53 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)28 Parser (org.eclipse.jetty.websocket.common.Parser)27 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)26 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)24 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)21 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)9 ArrayList (java.util.ArrayList)8 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 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)6 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)5 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 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)2