Search in sources :

Example 26 with UnitParser

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

the class DeflateFrameExtensionTest method assertIncoming.

private void assertIncoming(byte[] raw, String... expectedTextDatas) {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    DeflateFrameExtension ext = new DeflateFrameExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(policy);
    ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
    ext.setConfig(config);
    // Setup capture of incoming frames
    IncomingFramesCapture capture = new IncomingFramesCapture();
    // Wire up stack
    ext.setNextIncomingFrames(capture);
    Parser parser = new UnitParser(policy);
    parser.configureFromExtensions(Collections.singletonList(ext));
    parser.setIncomingFramesHandler(ext);
    parser.parse(ByteBuffer.wrap(raw));
    int len = expectedTextDatas.length;
    capture.assertFrameCount(len);
    capture.assertHasFrame(OpCode.TEXT, len);
    int i = 0;
    for (WebSocketFrame actual : capture.getFrames()) {
        String prefix = "Frame[" + i + "]";
        Assert.assertThat(prefix + ".opcode", actual.getOpCode(), is(OpCode.TEXT));
        Assert.assertThat(prefix + ".fin", actual.isFin(), is(true));
        // RSV1 should be unset at this point
        Assert.assertThat(prefix + ".rsv1", actual.isRsv1(), is(false));
        Assert.assertThat(prefix + ".rsv2", actual.isRsv2(), is(false));
        Assert.assertThat(prefix + ".rsv3", actual.isRsv3(), is(false));
        ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i], StandardCharsets.UTF_8);
        Assert.assertThat(prefix + ".payloadLength", actual.getPayloadLength(), is(expected.remaining()));
        ByteBufferAssert.assertEquals(prefix + ".payload", expected, actual.getPayload().slice());
        i++;
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser)

Example 27 with UnitParser

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

the class TestABCase7_3 method testCase7_3_4ParseCloseWithStatusReason.

@Test
public void testCase7_3_4ParseCloseWithStatusReason() {
    String message = "bad cough";
    byte[] messageBytes = message.getBytes();
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x88 });
    // no masking
    byte b = 0x00;
    b |= (messageBytes.length + 2) & 0x7F;
    expected.put(b);
    expected.putShort((short) 1000);
    expected.put(messageBytes);
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.CLOSE, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("CloseFrame.payloadLength", pActual.getPayloadLength(), is(messageBytes.length + 2));
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) CloseFrame(org.eclipse.jetty.websocket.common.frames.CloseFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) Matchers.containsString(org.hamcrest.Matchers.containsString) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 28 with UnitParser

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

the class GeneratorTest method testWindowedGenerateWithMasking.

@Test
public void testWindowedGenerateWithMasking() {
    // A decent sized frame, with masking
    byte[] payload = new byte[10240];
    Arrays.fill(payload, (byte) 0x55);
    byte[] mask = new byte[] { 0x2A, (byte) 0xF0, 0x0F, 0x00 };
    WebSocketFrame frame = new BinaryFrame().setPayload(payload);
    // masking!
    frame.setMask(mask);
    // Generate
    int windowSize = 2929;
    WindowHelper helper = new WindowHelper(windowSize);
    ByteBuffer completeBuffer = helper.generateWindowed(frame);
    // Validate
    int expectedHeaderSize = 8;
    int expectedSize = payload.length + expectedHeaderSize;
    int expectedParts = 1;
    helper.assertTotalParts(expectedParts);
    helper.assertTotalBytes(payload.length + expectedHeaderSize);
    Assert.assertThat("Generated Buffer", completeBuffer.remaining(), is(expectedSize));
    // Parse complete buffer.
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuffer);
    // Assert validity of frame
    WebSocketFrame actual = capture.getFrames().poll();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.BINARY));
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(payload.length));
    // Validate payload contents for proper masking
    ByteBuffer actualData = actual.getPayload().slice();
    Assert.assertThat("Frame.payload.remaining", actualData.remaining(), is(payload.length));
    while (actualData.remaining() > 0) {
        Assert.assertThat("Actual.payload[" + actualData.position() + "]", actualData.get(), is((byte) 0x55));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) 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 29 with UnitParser

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

the class ParserTest method testParseNothing.

@Test
public void testParseNothing() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Put nothing in the buffer.
    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();
    Assert.assertThat("Frame Count", capture.getFrames().size(), is(0));
}
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 30 with UnitParser

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

the class ParserTest method testParseCase5_18.

/**
     * Similar to the server side 5.18 testcase. Text message fragmented as 2 frames, both as opcode=TEXT
     */
@Test
public void testParseCase5_18() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload("fragment1").setFin(false));
    // bad frame, must be continuation
    send.add(new TextFrame().setPayload("fragment2").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(1);
    // fragment 1
    capture.assertHasFrame(OpCode.TEXT, 1);
}
Also used : 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)

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