Search in sources :

Example 16 with UnitParser

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

the class TestABCase2 method testParse125OctetPingCase2_4.

@Test
public void testParse125OctetPingCase2_4() {
    byte[] bytes = new byte[125];
    for (int i = 0; i < bytes.length; ++i) {
        bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
    }
    ByteBuffer expected = ByteBuffer.allocate(bytes.length + 32);
    expected.put(new byte[] { (byte) 0x89 });
    // no masking
    byte b = 0x00;
    b |= bytes.length & 0x7F;
    expected.put(b);
    expected.put(bytes);
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.PING, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("PingFrame.payloadLength", pActual.getPayloadLength(), is(bytes.length));
    Assert.assertEquals("PingFrame.payload", bytes.length, pActual.getPayloadLength());
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 17 with UnitParser

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

the class TestABCase2 method testParseEmptyPingCase2_1.

@Test
public void testParseEmptyPingCase2_1() {
    ByteBuffer expected = ByteBuffer.allocate(5);
    expected.put(new byte[] { (byte) 0x89, (byte) 0x00 });
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.PING, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("PingFrame.payloadLength", pActual.getPayloadLength(), is(0));
    Assert.assertEquals("PingFrame.payload", 0, pActual.getPayloadLength());
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 18 with UnitParser

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

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

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

Aggregations

ByteBuffer (java.nio.ByteBuffer)53 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)53 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)53 Test (org.junit.Test)52 Parser (org.eclipse.jetty.websocket.common.Parser)27 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)24 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)23 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)19 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)13 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 ArrayList (java.util.ArrayList)6 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)6 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 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)3 MessageTooLargeException (org.eclipse.jetty.websocket.api.MessageTooLargeException)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1