Search in sources :

Example 36 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser 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 37 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser 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 38 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser 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 39 with UnitParser

use of org.eclipse.jetty.websocket.common.test.UnitParser 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)

Example 40 with UnitParser

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

the class ParserTest method testWindowedParseLargeFrame.

@Test
public void testWindowedParseLargeFrame() {
    // Create frames
    byte[] payload = new byte[65536];
    Arrays.fill(payload, (byte) '*');
    List<WebSocketFrame> frames = new ArrayList<>();
    TextFrame text = new TextFrame();
    text.setPayload(ByteBuffer.wrap(payload));
    text.setMask(Hex.asByteArray("11223344"));
    frames.add(text);
    frames.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    // Build up raw (network bytes) buffer
    ByteBuffer networkBytes = UnitGenerator.generate(frames);
    // Parse, in 4096 sized windows
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    while (networkBytes.remaining() > 0) {
        ByteBuffer window = networkBytes.slice();
        int windowSize = Math.min(window.remaining(), 4096);
        window.limit(windowSize);
        parser.parse(window);
        networkBytes.position(networkBytes.position() + windowSize);
    }
    capture.assertNoErrors();
    Assert.assertThat("Frame Count", capture.getFrames().size(), is(2));
    WebSocketFrame frame = capture.getFrames().poll();
    Assert.assertThat("Frame[0].opcode", frame.getOpCode(), is(OpCode.TEXT));
    ByteBuffer actualPayload = frame.getPayload();
    Assert.assertThat("Frame[0].payload.length", actualPayload.remaining(), is(payload.length));
    // Should be all '*' characters (if masking is correct)
    for (int i = actualPayload.position(); i < actualPayload.remaining(); i++) {
        Assert.assertThat("Frame[0].payload[i]", actualPayload.get(i), is((byte) '*'));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) 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)

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