Search in sources :

Example 1 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class ClosePayloadParserTest method testGameOver.

@Test
public void testGameOver() {
    String expectedReason = "Game Over";
    byte[] utf = expectedReason.getBytes(StandardCharsets.UTF_8);
    ByteBuffer payload = ByteBuffer.allocate(utf.length + 2);
    payload.putChar((char) StatusCode.NORMAL);
    payload.put(utf, 0, utf.length);
    payload.flip();
    ByteBuffer buf = ByteBuffer.allocate(24);
    // fin + close
    buf.put((byte) (0x80 | OpCode.CLOSE));
    buf.put((byte) (0x80 | payload.remaining()));
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, payload);
    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.CLOSE, 1);
    CloseInfo close = new CloseInfo(capture.getFrames().poll());
    Assert.assertThat("CloseFrame.statusCode", close.getStatusCode(), is(StatusCode.NORMAL));
    Assert.assertThat("CloseFrame.data", close.getReason(), is(expectedReason));
}
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 2 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class RFC6455ExamplesParserTest method testSingleUnmasked64KByteBinaryMessage.

@Test
public void testSingleUnmasked64KByteBinaryMessage() {
    int dataSize = 1024 * 64;
    ByteBuffer buf = ByteBuffer.allocate((dataSize + 10));
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // 64 Kbytes binary message in a single unmasked frame
    buf.put(new byte[] { (byte) 0x82, 0x7F });
    // 64bit size
    buf.putLong(dataSize);
    for (int i = 0; i < dataSize; i++) {
        buf.put((byte) 0x77);
    }
    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) 0x77));
    }
}
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 3 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy 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 4 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy 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 5 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy 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)

Aggregations

WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)40 Test (org.junit.Test)26 ByteBuffer (java.nio.ByteBuffer)24 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)24 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)23 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)7 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)7 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)6 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)6 Parser (org.eclipse.jetty.websocket.common.Parser)5 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)4 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)3 Generator (org.eclipse.jetty.websocket.common.Generator)3 EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)3 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)3 IOException (java.io.IOException)2 URI (java.net.URI)2 ServletException (javax.servlet.ServletException)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 WebSocketBehavior (org.eclipse.jetty.websocket.api.WebSocketBehavior)2