Search in sources :

Example 26 with WebSocketPolicy

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

the class TextPayloadParserTest method testShortMaskedText.

@Test
public void testShortMaskedText() 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 27 with WebSocketPolicy

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

the class TextPayloadParserTest method testShortMaskedFragmentedText.

@Test
public void testShortMaskedFragmentedText() throws Exception {
    String part1 = "Hello ";
    String part2 = "World";
    byte[] b1 = part1.getBytes(StandardCharsets.UTF_8);
    byte[] b2 = part2.getBytes(StandardCharsets.UTF_8);
    ByteBuffer buf = ByteBuffer.allocate(32);
    // part 1
    // no fin + text
    buf.put((byte) 0x01);
    buf.put((byte) (0x80 | b1.length));
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, b1);
    // part 2
    // fin + continuation
    buf.put((byte) 0x80);
    buf.put((byte) (0x80 | b2.length));
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, b2);
    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);
    capture.assertHasFrame(OpCode.CONTINUATION, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    Assert.assertThat("TextFrame[0].data", txt.getPayloadAsUTF8(), is(part1));
    txt = capture.getFrames().poll();
    Assert.assertThat("TextFrame[1].data", txt.getPayloadAsUTF8(), is(part2));
}
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 28 with WebSocketPolicy

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

the class TextPayloadParserTest method testLongMaskedText.

@Test
public void testLongMaskedText() throws Exception {
    StringBuffer sb = new StringBuffer();
    ;
    for (int i = 0; i < 3500; i++) {
        sb.append("Hello Big World ");
    }
    sb.append(". The end.");
    String expectedText = sb.toString();
    byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
    Assert.assertThat("Must be a long length payload", utf.length, greaterThan(0xFFFF));
    ByteBuffer buf = ByteBuffer.allocate(utf.length + 32);
    // text frame, fin = true
    buf.put((byte) 0x81);
    // 0x7F == 127 (a 8 byte payload length)
    buf.put((byte) (0x80 | 0x7F));
    buf.putLong(utf.length);
    MaskedByteBuffer.putMask(buf);
    MaskedByteBuffer.putPayload(buf, utf);
    buf.flip();
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    policy.setMaxTextMessageSize(100000);
    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 WebSocketPolicy

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

the class WebSocketFrameTest method initGenerator.

@Before
public void initGenerator() {
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    strictGenerator = new Generator(policy, bufferPool);
    laxGenerator = new Generator(policy, bufferPool, false);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Before(org.junit.Before)

Example 30 with WebSocketPolicy

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

Aggregations

WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)36 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 Before (org.junit.Before)3 URI (java.net.URI)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 WebSocketContainerScope (org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope)2 DummyConnection (org.eclipse.jetty.websocket.common.test.DummyConnection)2