Search in sources :

Example 81 with TextFrame

use of org.eclipse.jetty.websocket.common.frames.TextFrame 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)

Example 82 with TextFrame

use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.

the class ParserTest method testParseCase5_6.

/**
     * Similar to the server side 5.6 testcase. pong, then text, then close frames.
     */
@Test
public void testParseCase5_6() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new PongFrame().setPayload("ping"));
    send.add(new TextFrame().setPayload("hello, world"));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuf);
    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PONG, 1);
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) 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)

Example 83 with TextFrame

use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.

the class RFC6455ExamplesGeneratorTest method testSingleUnmaskedTextMessage.

@Test
public void testSingleUnmaskedTextMessage() {
    WebSocketFrame text = new TextFrame().setPayload("Hello");
    ByteBuffer actual = UnitGenerator.generate(text);
    ByteBuffer expected = ByteBuffer.allocate(10);
    expected.put(new byte[] { (byte) 0x81, (byte) 0x05, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f });
    expected.flip();
    ByteBufferAssert.assertEquals("t1 buffers are not equal", expected, actual);
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 84 with TextFrame

use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.

the class RFC6455ExamplesGeneratorTest method testSingleMaskedTextMessage.

@Test
public void testSingleMaskedTextMessage() {
    WebSocketFrame text = new TextFrame().setPayload("Hello");
    text.setMask(new byte[] { 0x37, (byte) 0xfa, 0x21, 0x3d });
    ByteBuffer actual = UnitGenerator.generate(text);
    ByteBuffer expected = ByteBuffer.allocate(11);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A single-frame masked text message
    expected.put(new byte[] { (byte) 0x81, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
    // make readable
    expected.flip();
    ByteBufferAssert.assertEquals("masked text buffers are not equal", expected, actual);
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 85 with TextFrame

use of org.eclipse.jetty.websocket.common.frames.TextFrame in project jetty.project by eclipse.

the class RFC6455ExamplesGeneratorTest method testFragmentedUnmaskedTextMessage.

@Test
public void testFragmentedUnmaskedTextMessage() {
    WebSocketFrame text1 = new TextFrame().setPayload("Hel").setFin(false);
    WebSocketFrame text2 = new ContinuationFrame().setPayload("lo");
    ByteBuffer actual1 = UnitGenerator.generate(text1);
    ByteBuffer actual2 = UnitGenerator.generate(text2);
    ByteBuffer expected1 = ByteBuffer.allocate(5);
    expected1.put(new byte[] { (byte) 0x01, (byte) 0x03, (byte) 0x48, (byte) 0x65, (byte) 0x6c });
    ByteBuffer expected2 = ByteBuffer.allocate(4);
    expected2.put(new byte[] { (byte) 0x80, (byte) 0x02, (byte) 0x6c, (byte) 0x6f });
    expected1.flip();
    expected2.flip();
    ByteBufferAssert.assertEquals("t1 buffers are not equal", expected1, actual1);
    ByteBufferAssert.assertEquals("t2 buffers are not equal", expected2, actual2);
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)142 Test (org.junit.Test)130 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)109 ArrayList (java.util.ArrayList)69 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)68 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)59 ByteBuffer (java.nio.ByteBuffer)54 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)38 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)35 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)33 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)23 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)14 URI (java.net.URI)13 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)12 Matchers.containsString (org.hamcrest.Matchers.containsString)10 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)9 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)8 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)6 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)5