Search in sources :

Example 1 with BinaryFrame

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

the class DeflateFrameExtensionTest method testCompressAndDecompressBigPayload.

@Test
public void testCompressAndDecompressBigPayload() throws Exception {
    byte[] input = new byte[1024 * 1024];
    // Make them not compressible.
    new Random().nextBytes(input);
    int maxMessageSize = (1024 * 1024) + 8192;
    DeflateFrameExtension clientExtension = new DeflateFrameExtension();
    clientExtension.setBufferPool(bufferPool);
    clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
    clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    clientExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
    serverExtension.setBufferPool(bufferPool);
    serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
    serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    serverExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    // Chain the next element to decompress.
    clientExtension.setNextOutgoingFrames(new OutgoingFrames() {

        @Override
        public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode) {
            LOG.debug("outgoingFrame({})", frame);
            serverExtension.incomingFrame(frame);
            callback.writeSuccess();
        }
    });
    final ByteArrayOutputStream result = new ByteArrayOutputStream(input.length);
    serverExtension.setNextIncomingFrames(new IncomingFrames() {

        @Override
        public void incomingFrame(Frame frame) {
            LOG.debug("incomingFrame({})", frame);
            try {
                result.write(BufferUtil.toArray(frame.getPayload()));
            } catch (IOException x) {
                throw new RuntimeIOException(x);
            }
        }

        @Override
        public void incomingError(Throwable t) {
        }
    });
    BinaryFrame frame = new BinaryFrame();
    frame.setPayload(input);
    frame.setFin(true);
    clientExtension.outgoingFrame(frame, null, BatchMode.OFF);
    Assert.assertArrayEquals(input, result.toByteArray());
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) BatchMode(org.eclipse.jetty.websocket.api.BatchMode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) Random(java.util.Random) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 2 with BinaryFrame

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

the class TestABCase1_2 method testGenerate125ByteBinaryCase1_2_2.

@Test
public void testGenerate125ByteBinaryCase1_2_2() {
    int length = 125;
    ByteBuffer bb = ByteBuffer.allocate(length);
    for (int i = 0; i < length; ++i) {
        bb.put("*".getBytes());
    }
    bb.flip();
    WebSocketFrame binaryFrame = new BinaryFrame().setPayload(bb);
    ByteBuffer actual = UnitGenerator.generate(binaryFrame);
    ByteBuffer expected = ByteBuffer.allocate(length + 5);
    expected.put(new byte[] { (byte) 0x82 });
    // no masking
    byte b = 0x00;
    b |= length & 0x7F;
    expected.put(b);
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    BufferUtil.flipToFlush(expected, 0);
    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
Also used : BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with BinaryFrame

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

the class TestABCase1_2 method testGenerate126ByteBinaryCase1_2_3.

@Test
public void testGenerate126ByteBinaryCase1_2_3() {
    int length = 126;
    ByteBuffer bb = ByteBuffer.allocate(length);
    for (int i = 0; i < length; ++i) {
        bb.put("*".getBytes());
    }
    bb.flip();
    WebSocketFrame binaryFrame = new BinaryFrame().setPayload(bb);
    ByteBuffer actual = UnitGenerator.generate(binaryFrame);
    ByteBuffer expected = ByteBuffer.allocate(length + 5);
    expected.put(new byte[] { (byte) 0x82 });
    // no masking
    byte b = 0x00;
    b |= length & 0x7E;
    expected.put(b);
    //expected.put((byte)((length>>8) & 0xFF));
    //expected.put((byte)(length & 0xFF));
    expected.putShort((short) length);
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    BufferUtil.flipToFlush(expected, 0);
    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
Also used : BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with BinaryFrame

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

the class TestABCase9 method testCase9_2_1.

/**
     * Echo 64KB binary message (1 frame)
     * @throws Exception on test failure
     */
@Test
public void testCase9_2_1() throws Exception {
    byte[] data = new byte[64 * KBYTE];
    Arrays.fill(data, (byte) 0x21);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new BinaryFrame().setPayload(data));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new BinaryFrame().setPayload(copyOf(data)));
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 5 with BinaryFrame

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

the class TestABCase1 method testCase1_2_4.

/**
     * Echo 127 byte BINARY message (uses medium 2 byte payload length)
     * @throws Exception on test failure
     */
@Test
public void testCase1_2_4() throws Exception {
    byte[] payload = new byte[127];
    Arrays.fill(payload, (byte) 0xFE);
    ByteBuffer buf = ByteBuffer.wrap(payload);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new BinaryFrame().setPayload(buf));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new BinaryFrame().setPayload(clone(buf)));
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Aggregations

BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)33 Test (org.junit.Test)29 ByteBuffer (java.nio.ByteBuffer)25 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)25 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)16 ArrayList (java.util.ArrayList)15 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)15 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)5 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)4 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)4 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)3 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)2 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)2 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)2 AnnotatedFramesSocket (examples.AnnotatedFramesSocket)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Random (java.util.Random)1 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)1 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)1