Search in sources :

Example 26 with IncomingFramesCapture

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

the class RFC6455ExamplesParserTest method testSingleMaskedPongRequest.

@Test
public void testSingleMaskedPongRequest() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // Unmasked Pong request
    buf.put(new byte[] { (byte) 0x8a, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
    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.PONG, 1);
    WebSocketFrame pong = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(pong.getPayload());
    Assert.assertThat("PongFrame.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)

Example 27 with IncomingFramesCapture

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

the class TextPayloadParserTest method testFrameTooLargeDueToPolicy.

@Test
public void testFrameTooLargeDueToPolicy() throws Exception {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    // Artificially small buffer/payload
    // read buffer
    policy.setInputBufferSize(1024);
    // streaming buffer (not used in this test)
    policy.setMaxTextMessageBufferSize(1024);
    // actual maximum text message size policy
    policy.setMaxTextMessageSize(1024);
    byte[] utf = new byte[2048];
    Arrays.fill(utf, (byte) 'a');
    Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
    ByteBuffer buf = ByteBuffer.allocate(utf.length + 8);
    // text frame, fin = true
    buf.put((byte) 0x81);
    // 0x7E == 126 (a 2 byte payload length)
    buf.put((byte) (0x80 | 0x7E));
    buf.putShort((short) utf.length);
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, utf);
    buf.flip();
    UnitParser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parseQuietly(buf);
    capture.assertHasErrors(MessageTooLargeException.class, 1);
    capture.assertHasNoFrames();
    MessageTooLargeException err = (MessageTooLargeException) capture.getErrors().poll();
    Assert.assertThat("Error.closeCode", err.getStatusCode(), is(StatusCode.MESSAGE_TOO_LARGE));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) MessageTooLargeException(org.eclipse.jetty.websocket.api.MessageTooLargeException) 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) Test(org.junit.Test)

Example 28 with IncomingFramesCapture

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

the class TextPayloadParserTest method testMediumMaskedText.

@Test
public void testMediumMaskedText() throws Exception {
    StringBuffer sb = new StringBuffer();
    ;
    for (int i = 0; i < 14; i++) {
        sb.append("Hello Medium World ");
    }
    sb.append(". The end.");
    String expectedText = sb.toString();
    byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
    Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
    ByteBuffer buf = ByteBuffer.allocate(utf.length + 10);
    buf.put((byte) 0x81);
    // 0x7E == 126 (a 2 byte payload length)
    buf.put((byte) (0x80 | 0x7E));
    buf.putShort((short) 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 29 with IncomingFramesCapture

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

the class TextPayloadParserTest method testShortMaskedUtf8Text.

@Test
public void testShortMaskedUtf8Text() 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 30 with IncomingFramesCapture

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

the class ClientCloseTest method confirmServerReceivedCloseFrame.

private void confirmServerReceivedCloseFrame(IBlockheadServerConnection serverConn, int expectedCloseCode, Matcher<String> closeReasonMatcher) throws IOException, TimeoutException {
    IncomingFramesCapture serverCapture = serverConn.readFrames(1, 30, TimeUnit.SECONDS);
    serverCapture.assertNoErrors();
    serverCapture.assertFrameCount(1);
    serverCapture.assertHasFrame(OpCode.CLOSE, 1);
    WebSocketFrame frame = serverCapture.getFrames().poll();
    assertThat("Server received close frame", frame.getOpCode(), is(OpCode.CLOSE));
    CloseInfo closeInfo = new CloseInfo(frame);
    assertThat("Server received close code", closeInfo.getStatusCode(), is(expectedCloseCode));
    if (closeReasonMatcher == null) {
        assertThat("Server received close reason", closeInfo.getReason(), nullValue());
    } else {
        assertThat("Server received close reason", closeInfo.getReason(), closeReasonMatcher);
    }
}
Also used : IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

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