Search in sources :

Example 46 with IncomingFramesCapture

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

Example 47 with IncomingFramesCapture

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

the class ParserTest method testParseCase5_6.

/**
     * Similar to the server side 5.6 testcase. pong, then text, then close frames.
     */
@Test
public void testParseCase5_6() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new PongFrame().setPayload("ping"));
    send.add(new TextFrame().setPayload("hello, world"));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuf);
    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PONG, 1);
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) 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) Test(org.junit.Test)

Example 48 with IncomingFramesCapture

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

the class PingPayloadParserTest method testBasicPingParsing.

@Test
public void testBasicPingParsing() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    BufferUtil.clearToFill(buf);
    buf.put(new byte[] { (byte) 0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
    BufferUtil.flipToFlush(buf, 0);
    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.PING, 1);
    PingFrame ping = (PingFrame) capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(ping.getPayload());
    Assert.assertThat("PingFrame.payload", actual, is("Hello"));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) 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 49 with IncomingFramesCapture

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

the class RFC6455ExamplesParserTest method testSingleUnmasked256ByteBinaryMessage.

@Test
public void testSingleUnmasked256ByteBinaryMessage() {
    int dataSize = 256;
    ByteBuffer buf = ByteBuffer.allocate(dataSize + 10);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // 256 bytes binary message in a single unmasked frame
    buf.put(new byte[] { (byte) 0x82, 0x7E });
    // 16 bit size
    buf.putShort((short) 0x01_00);
    for (int i = 0; i < dataSize; i++) {
        buf.put((byte) 0x44);
    }
    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) 0x44));
    }
}
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 50 with IncomingFramesCapture

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

the class RFC6455ExamplesParserTest method testSingleUnmaskedTextMessage.

@Test
public void testSingleUnmaskedTextMessage() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A single-frame unmasked text message
    buf.put(new byte[] { (byte) 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
    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.TEXT, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame.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)

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