Search in sources :

Example 61 with WebSocketFrame

use of org.eclipse.jetty.websocket.common.WebSocketFrame in project jetty.project by eclipse.

the class Fuzzer method send.

public void send(List<WebSocketFrame> send) throws IOException {
    Assert.assertThat("Client connected", client.isConnected(), is(true));
    LOG.debug("[{}] Sending {} frames (mode {})", testname, send.size(), sendMode);
    if ((sendMode == SendMode.BULK) || (sendMode == SendMode.SLOW)) {
        int buflen = 0;
        for (Frame f : send) {
            buflen += f.getPayloadLength() + Generator.MAX_HEADER_LENGTH;
        }
        ByteBuffer buf = ByteBuffer.allocate(buflen);
        // Generate frames
        for (WebSocketFrame f : send) {
            setClientMask(f);
            buf.put(generator.generateHeaderBytes(f));
            if (f.hasPayload()) {
                buf.put(f.getPayload());
            }
        }
        BufferUtil.flipToFlush(buf, 0);
        // Write Data Frame
        switch(sendMode) {
            case BULK:
                client.writeRaw(buf);
                break;
            case SLOW:
                client.writeRawSlowly(buf, slowSendSegmentSize);
                break;
            default:
                throw new RuntimeException("Whoops, unsupported sendMode: " + sendMode);
        }
    } else if (sendMode == SendMode.PER_FRAME) {
        for (WebSocketFrame f : send) {
            // make sure we have mask set
            f.setMask(MASK);
            // Using lax generator, generate and send
            ByteBuffer fullframe = ByteBuffer.allocate(f.getPayloadLength() + Generator.MAX_HEADER_LENGTH);
            BufferUtil.clearToFill(fullframe);
            generator.generateWholeFrame(f, fullframe);
            BufferUtil.flipToFlush(fullframe, 0);
            client.writeRaw(fullframe);
            client.flush();
        }
    }
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer)

Example 62 with WebSocketFrame

use of org.eclipse.jetty.websocket.common.WebSocketFrame in project jetty.project by eclipse.

the class TestABCase1 method testCase1_1_6.

/**
     * Echo 65535 byte TEXT message (uses medium 2 byte payload length)
     * @throws Exception on test failure
     */
@Test
public void testCase1_1_6() throws Exception {
    byte[] payload = new byte[65535];
    Arrays.fill(payload, (byte) '*');
    ByteBuffer buf = ByteBuffer.wrap(payload);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload(buf));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new TextFrame().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) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 63 with WebSocketFrame

use of org.eclipse.jetty.websocket.common.WebSocketFrame in project jetty.project by eclipse.

the class TestABCase1 method testCase1_1_3.

/**
     * Echo 126 byte TEXT message (uses medium 2 byte payload length)
     * @throws Exception on test failure
     */
@Test
public void testCase1_1_3() throws Exception {
    byte[] payload = new byte[126];
    Arrays.fill(payload, (byte) '*');
    ByteBuffer buf = ByteBuffer.wrap(payload);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload(buf));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new TextFrame().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) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 64 with WebSocketFrame

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

Example 65 with WebSocketFrame

use of org.eclipse.jetty.websocket.common.WebSocketFrame in project jetty.project by eclipse.

the class TestABCase1 method testCase1_2_6.

/**
     * Echo 65535 byte BINARY message (uses medium 2 byte payload length)
     * @throws Exception on test failure
     */
@Test
public void testCase1_2_6() throws Exception {
    byte[] payload = new byte[65535];
    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

WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)185 Test (org.junit.Test)169 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)118 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)112 ArrayList (java.util.ArrayList)111 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)108 ByteBuffer (java.nio.ByteBuffer)79 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)51 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)37 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)36 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)36 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)25 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)17 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)17 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)14 URI (java.net.URI)12 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)8 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)6