Search in sources :

Example 51 with IncomingFramesCapture

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

the class RFC6455ExamplesParserTest method testSingleMaskedTextMessage.

@Test
public void testSingleMaskedTextMessage() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // A single-frame masked text message
    buf.put(new byte[] { (byte) 0x81, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
    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.TEXT, 1);
    WebSocketFrame txt = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(txt.getPayload());
    Assert.assertThat("TextFrame.payload", actual, is("Hello"));
}
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) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 52 with IncomingFramesCapture

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

the class RFC6455ExamplesParserTest method testSingleUnmaskedPingRequest.

@Test
public void testSingleUnmaskedPingRequest() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Raw bytes as found in RFC 6455, Section 5.7 - Examples
    // Unmasked Ping request
    buf.put(new byte[] { (byte) 0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f });
    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.PING, 1);
    WebSocketFrame ping = capture.getFrames().poll();
    String actual = BufferUtil.toUTF8String(ping.getPayload());
    Assert.assertThat("PingFrame.payload", actual, is("Hello"));
}
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) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 53 with IncomingFramesCapture

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

the class TestABCase4 method testParserNonControlOpCode4WithPayloadCase4_1_2.

@Test
public void testParserNonControlOpCode4WithPayloadCase4_1_2() throws Exception {
    ByteBuffer expected = ByteBuffer.allocate(32);
    expected.put(new byte[] { (byte) 0x84, 0x01, 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: 4"));
}
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 54 with IncomingFramesCapture

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

the class TestABCase7_3 method testCase7_3_2Parse1BytePayloadClose.

@Test
public void testCase7_3_2Parse1BytePayloadClose() {
    ByteBuffer expected = Hex.asByteBuffer("880100");
    UnitParser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parseQuietly(expected);
    Assert.assertEquals("error on invalid close payload", 1, capture.getErrorCount(ProtocolException.class));
    ProtocolException known = (ProtocolException) capture.getErrors().poll();
    Assert.assertThat("Payload.message", known.getMessage(), containsString("Invalid close frame payload length"));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 55 with IncomingFramesCapture

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

the class TestABCase7_3 method testCase7_3_6ParseCloseWithInvalidStatusReason.

@Test
public void testCase7_3_6ParseCloseWithInvalidStatusReason() {
    byte[] messageBytes = new byte[124];
    Arrays.fill(messageBytes, (byte) '*');
    ByteBuffer expected = ByteBuffer.allocate(256);
    byte b;
    // fin + op
    b = 0x00;
    // fin on
    b |= 0x80;
    // close
    b |= 0x08;
    expected.put(b);
    // mask + len
    b = 0x00;
    // no masking
    b |= 0x00;
    // 2 byte len
    b |= 0x7E;
    expected.put(b);
    // 2 byte len
    expected.putChar((char) (messageBytes.length + 2));
    // payload
    // status code
    expected.putShort((short) 1000);
    // reason
    expected.put(messageBytes);
    expected.flip();
    UnitParser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parseQuietly(expected);
    Assert.assertEquals("error on invalid close payload", 1, capture.getErrorCount(ProtocolException.class));
    ProtocolException known = (ProtocolException) capture.getErrors().poll();
    Assert.assertThat("Payload.message", known.getMessage(), containsString("Invalid control frame payload length"));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) 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