Search in sources :

Example 6 with UnitParser

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

the class TestABCase4 method testParserControlOpCode11Case4_2_1.

@Test
public void testParserControlOpCode11Case4_2_1() throws Exception {
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x8b, 0x00 });
    expected.flip();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    try (StacklessLogging logging = new StacklessLogging(Parser.class)) {
        Parser parser = new UnitParser(policy);
        parser.setIncomingFramesHandler(capture);
        try {
            parser.parse(expected);
        } catch (ProtocolException ignore) {
        // ignore
        }
    }
    Assert.assertEquals("error on undefined opcode", 1, capture.getErrorCount(WebSocketException.class));
    Throwable known = capture.getErrors().poll();
    Assert.assertTrue("undefined option should be in message", known.getMessage().contains("Unknown opcode: 11"));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) 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 7 with UnitParser

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

the class TestABCase7_3 method testCase7_3_3ParseCloseWithStatus.

@Test
public void testCase7_3_3ParseCloseWithStatus() {
    ByteBuffer expected = ByteBuffer.allocate(5);
    expected.put(new byte[] { (byte) 0x88, (byte) 0x02, 0x03, (byte) 0xe8 });
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.CLOSE, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("CloseFrame.payloadLength", pActual.getPayloadLength(), is(2));
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) CloseFrame(org.eclipse.jetty.websocket.common.frames.CloseFrame) 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 8 with UnitParser

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

the class TestABCase7_3 method testCase7_3_1ParseEmptyClose.

@Test
public void testCase7_3_1ParseEmptyClose() {
    ByteBuffer expected = ByteBuffer.allocate(5);
    expected.put(new byte[] { (byte) 0x88, (byte) 0x00 });
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.CLOSE, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("CloseFrame.payloadLength", pActual.getPayloadLength(), is(0));
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) CloseFrame(org.eclipse.jetty.websocket.common.frames.CloseFrame) 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 9 with UnitParser

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

the class TestABCase7_3 method testCase7_3_5ParseCloseWithStatusMaxReason.

@Test
public void testCase7_3_5ParseCloseWithStatusMaxReason() {
    StringBuilder message = new StringBuilder();
    for (int i = 0; i < 123; ++i) {
        message.append("*");
    }
    byte[] messageBytes = message.toString().getBytes(StandardCharsets.UTF_8);
    ByteBuffer expected = ByteBuffer.allocate(132);
    expected.put(new byte[] { (byte) 0x88 });
    // no masking
    byte b = 0x00;
    b |= (messageBytes.length + 2) & 0x7F;
    expected.put(b);
    expected.putShort((short) 1000);
    expected.put(messageBytes);
    expected.flip();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.CLOSE, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("CloseFrame.payloadLength", pActual.getPayloadLength(), is(125));
}
Also used : Frame(org.eclipse.jetty.websocket.api.extensions.Frame) CloseFrame(org.eclipse.jetty.websocket.common.frames.CloseFrame) 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 10 with UnitParser

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

the class TestABCase1_1 method testParse65536ByteTextCase1_1_7.

@Test
public void testParse65536ByteTextCase1_1_7() {
    int length = 65536;
    ByteBuffer expected = ByteBuffer.allocate(length + 11);
    expected.put(new byte[] { (byte) 0x81 });
    // no masking
    byte b = 0x00;
    b |= 0x7F;
    expected.put(b);
    expected.put(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 });
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    expected.flip();
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
    policy.setMaxTextMessageSize(length);
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(expected);
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    Frame pActual = capture.getFrames().poll();
    Assert.assertThat("TextFrame.payloadLength", pActual.getPayloadLength(), is(length));
// Assert.assertEquals("TextFrame.payload",length,pActual.getPayloadData().length);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) 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) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) 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