Search in sources :

Example 21 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class ClientCloseTest method confirmConnection.

private void confirmConnection(CloseTrackingSocket clientSocket, Future<Session> clientFuture, IBlockheadServerConnection serverConns) throws Exception {
    // Wait for client connect on via future
    clientFuture.get(30, TimeUnit.SECONDS);
    // Wait for client connect via client websocket
    assertThat("Client WebSocket is Open", clientSocket.openLatch.await(30, TimeUnit.SECONDS), is(true));
    try {
        // Send message from client to server
        final String echoMsg = "echo-test";
        Future<Void> testFut = clientSocket.getRemote().sendStringByFuture(echoMsg);
        // Wait for send future
        testFut.get(30, TimeUnit.SECONDS);
        // Read Frame on server side
        IncomingFramesCapture serverCapture = serverConns.readFrames(1, 30, TimeUnit.SECONDS);
        serverCapture.assertNoErrors();
        serverCapture.assertFrameCount(1);
        WebSocketFrame frame = serverCapture.getFrames().poll();
        assertThat("Server received frame", frame.getOpCode(), is(OpCode.TEXT));
        assertThat("Server received frame payload", frame.getPayloadAsUTF8(), is(echoMsg));
        // Server send echo reply
        serverConns.write(new TextFrame().setPayload(echoMsg));
        // Wait for received echo
        clientSocket.messageQueue.awaitEventCount(1, 1, TimeUnit.SECONDS);
        // Verify received message
        String recvMsg = clientSocket.messageQueue.poll();
        assertThat("Received message", recvMsg, is(echoMsg));
        // Verify that there are no errors
        assertThat("Error events", clientSocket.error.get(), nullValue());
    } finally {
        clientSocket.clearQueues();
    }
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 22 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture 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 23 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class ParserTest method testParseCase5_15.

/**
     * Similar to the server side 5.15 testcase. A normal 2 fragment text text message, followed by another continuation.
     */
@Test
public void testParseCase5_15() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload("fragment1").setFin(false));
    send.add(new ContinuationFrame().setPayload("fragment2").setFin(true));
    // bad frame
    send.add(new ContinuationFrame().setPayload("fragment3").setFin(false));
    send.add(new TextFrame().setPayload("fragment4").setFin(true));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parseQuietly(completeBuf);
    capture.assertErrorCount(1);
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CONTINUATION, 1);
}
Also used : ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 24 with IncomingFramesCapture

use of org.eclipse.jetty.websocket.common.test.IncomingFramesCapture in project jetty.project by eclipse.

the class ParserTest method testParseCase6_2_3.

/**
     * Similar to the server side 6.2.3 testcase. Lots of small 1 byte UTF8 Text frames, representing 1 overall text message.
     */
@Test
public void testParseCase6_2_3() {
    String utf8 = "Hello-습@쎟쎤쎼쎠쎡-UTF-8!!";
    byte[] msg = StringUtil.getUtf8Bytes(utf8);
    List<WebSocketFrame> send = new ArrayList<>();
    int textCount = 0;
    int continuationCount = 0;
    int len = msg.length;
    boolean continuation = false;
    byte[] mini;
    for (int i = 0; i < len; i++) {
        DataFrame frame = null;
        if (continuation) {
            frame = new ContinuationFrame();
            continuationCount++;
        } else {
            frame = new TextFrame();
            textCount++;
        }
        mini = new byte[1];
        mini[0] = msg[i];
        frame.setPayload(ByteBuffer.wrap(mini));
        boolean isLast = (i >= (len - 1));
        frame.setFin(isLast);
        send.add(frame);
        continuation = true;
    }
    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, textCount);
    capture.assertHasFrame(OpCode.CONTINUATION, continuationCount);
    capture.assertHasFrame(OpCode.CLOSE, 1);
}
Also used : ArrayList(java.util.ArrayList) DataFrame(org.eclipse.jetty.websocket.common.frames.DataFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) Test(org.junit.Test)

Example 25 with IncomingFramesCapture

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

Aggregations

IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)62 ByteBuffer (java.nio.ByteBuffer)60 Test (org.junit.Test)59 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)53 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)28 Parser (org.eclipse.jetty.websocket.common.Parser)27 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)26 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)24 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)21 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)9 ArrayList (java.util.ArrayList)8 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)8 ProtocolException (org.eclipse.jetty.websocket.api.ProtocolException)7 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)7 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)6 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)5 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)4 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)4 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)2