Search in sources :

Example 1 with IncomingFramesCapture

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

the class FragmentExtensionTest method testIncomingPing.

/**
     * Incoming PING (Control Frame) should pass through extension unmodified
     */
@Test
public void testIncomingPing() {
    IncomingFramesCapture capture = new IncomingFramesCapture();
    FragmentExtension ext = new FragmentExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(WebSocketPolicy.newServerPolicy());
    ExtensionConfig config = ExtensionConfig.parse("fragment;maxLength=4");
    ext.setConfig(config);
    ext.setNextIncomingFrames(capture);
    String payload = "Are you there?";
    Frame ping = new PingFrame().setPayload(payload);
    ext.incomingFrame(ping);
    capture.assertFrameCount(1);
    capture.assertHasFrame(OpCode.PING, 1);
    WebSocketFrame actual = capture.getFrames().poll();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.PING));
    Assert.assertThat("Frame.fin", actual.isFin(), is(true));
    Assert.assertThat("Frame.rsv1", actual.isRsv1(), is(false));
    Assert.assertThat("Frame.rsv2", actual.isRsv2(), is(false));
    Assert.assertThat("Frame.rsv3", actual.isRsv3(), is(false));
    ByteBuffer expected = BufferUtil.toBuffer(payload, StandardCharsets.UTF_8);
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(expected.remaining()));
    ByteBufferAssert.assertEquals("Frame.payload", expected, actual.getPayload().slice());
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) FragmentExtension(org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with IncomingFramesCapture

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

the class FragmentExtensionTest method testIncomingFrames.

/**
     * Verify that incoming frames are passed thru without modification
     */
@Test
public void testIncomingFrames() {
    IncomingFramesCapture capture = new IncomingFramesCapture();
    FragmentExtension ext = new FragmentExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(WebSocketPolicy.newClientPolicy());
    ExtensionConfig config = ExtensionConfig.parse("fragment;maxLength=4");
    ext.setConfig(config);
    ext.setNextIncomingFrames(capture);
    // Quote
    List<String> quote = new ArrayList<>();
    quote.add("No amount of experimentation can ever prove me right;");
    quote.add("a single experiment can prove me wrong.");
    quote.add("-- Albert Einstein");
    // Manually create frame and pass into extension
    for (String q : quote) {
        Frame frame = new TextFrame().setPayload(q);
        ext.incomingFrame(frame);
    }
    int len = quote.size();
    capture.assertFrameCount(len);
    capture.assertHasFrame(OpCode.TEXT, len);
    String prefix;
    int i = 0;
    for (WebSocketFrame actual : capture.getFrames()) {
        prefix = "Frame[" + i + "]";
        Assert.assertThat(prefix + ".opcode", actual.getOpCode(), is(OpCode.TEXT));
        Assert.assertThat(prefix + ".fin", actual.isFin(), is(true));
        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(quote.get(i), StandardCharsets.UTF_8);
        Assert.assertThat(prefix + ".payloadLength", actual.getPayloadLength(), is(expected.remaining()));
        ByteBufferAssert.assertEquals(prefix + ".payload", expected, actual.getPayload().slice());
        i++;
    }
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) FragmentExtension(org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with IncomingFramesCapture

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

the class TestABCase2 method testParseHelloPingCase2_2.

@Test
public void testParseHelloPingCase2_2() {
    String message = "Hello, world!";
    byte[] messageBytes = message.getBytes();
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x89 });
    // no masking
    byte b = 0x00;
    b |= messageBytes.length & 0x7F;
    expected.put(b);
    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.PING, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("PingFrame.payloadLength", pActual.getPayloadLength(), is(message.length()));
    Assert.assertEquals("PingFrame.payload", message.length(), pActual.getPayloadLength());
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 4 with IncomingFramesCapture

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

the class TestABCase2 method testParseBinaryPingCase2_3.

@Test
public void testParseBinaryPingCase2_3() {
    byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x89 });
    // no masking
    byte b = 0x00;
    b |= bytes.length & 0x7F;
    expected.put(b);
    expected.put(bytes);
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.PING, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("PingFrame.payloadLength", pActual.getPayloadLength(), is(bytes.length));
    Assert.assertEquals("PingFrame.payload", bytes.length, pActual.getPayloadLength());
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 5 with IncomingFramesCapture

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

the class TestABCase2 method testParseOversizedBinaryPingCase2_5.

@Test
public void testParseOversizedBinaryPingCase2_5() {
    byte[] bytes = new byte[126];
    Arrays.fill(bytes, (byte) 0x00);
    ByteBuffer expected = ByteBuffer.allocate(bytes.length + Generator.MAX_HEADER_LENGTH);
    byte b;
    // fin + op
    b = 0x00;
    // fin on
    b |= 0x80;
    // ping
    b |= 0x09;
    expected.put(b);
    // mask + len
    b = 0x00;
    // no masking
    b |= 0x00;
    // 2 byte len
    b |= 0x7E;
    expected.put(b);
    // 2 byte len
    expected.putChar((char) bytes.length);
    // payload
    expected.put(bytes);
    expected.flip();
    UnitParser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parseQuietly(expected);
    Assert.assertEquals("error should be returned for too large of ping payload", 1, capture.getErrorCount(ProtocolException.class));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) 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

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