Search in sources :

Example 31 with IncomingFramesCapture

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

the class GeneratorParserRoundtripTest method testParserAndGenerator.

@Test
public void testParserAndGenerator() throws Exception {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    Generator gen = new Generator(policy, bufferPool);
    Parser parser = new Parser(policy, bufferPool);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
    ByteBuffer out = bufferPool.acquire(8192, false);
    try {
        // Generate Buffer
        BufferUtil.flipToFill(out);
        WebSocketFrame frame = new TextFrame().setPayload(message);
        ByteBuffer header = gen.generateHeaderBytes(frame);
        ByteBuffer payload = frame.getPayload();
        out.put(header);
        out.put(payload);
        // Parse Buffer
        BufferUtil.flipToFlush(out, 0);
        parser.parse(out);
    } finally {
        bufferPool.release(out);
    }
    // Validate
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    TextFrame txt = (TextFrame) capture.getFrames().poll();
    Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 32 with IncomingFramesCapture

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

the class GeneratorParserRoundtripTest method testParserAndGeneratorMasked.

@Test
public void testParserAndGeneratorMasked() throws Exception {
    Generator gen = new Generator(WebSocketPolicy.newClientPolicy(), bufferPool);
    Parser parser = new Parser(WebSocketPolicy.newServerPolicy(), bufferPool);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
    ByteBuffer out = bufferPool.acquire(8192, false);
    BufferUtil.flipToFill(out);
    try {
        // Setup Frame
        WebSocketFrame frame = new TextFrame().setPayload(message);
        // Add masking
        byte[] mask = new byte[4];
        Arrays.fill(mask, (byte) 0xFF);
        frame.setMask(mask);
        // Generate Buffer
        ByteBuffer header = gen.generateHeaderBytes(frame);
        ByteBuffer payload = frame.getPayload();
        out.put(header);
        out.put(payload);
        // Parse Buffer
        BufferUtil.flipToFlush(out, 0);
        parser.parse(out);
    } finally {
        bufferPool.release(out);
    }
    // Validate
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);
    TextFrame txt = (TextFrame) capture.getFrames().poll();
    Assert.assertTrue("Text.isMasked", txt.isMasked());
    Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 33 with IncomingFramesCapture

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

the class GeneratorTest method testWindowedGenerateWithMasking.

@Test
public void testWindowedGenerateWithMasking() {
    // A decent sized frame, with masking
    byte[] payload = new byte[10240];
    Arrays.fill(payload, (byte) 0x55);
    byte[] mask = new byte[] { 0x2A, (byte) 0xF0, 0x0F, 0x00 };
    WebSocketFrame frame = new BinaryFrame().setPayload(payload);
    // masking!
    frame.setMask(mask);
    // Generate
    int windowSize = 2929;
    WindowHelper helper = new WindowHelper(windowSize);
    ByteBuffer completeBuffer = helper.generateWindowed(frame);
    // Validate
    int expectedHeaderSize = 8;
    int expectedSize = payload.length + expectedHeaderSize;
    int expectedParts = 1;
    helper.assertTotalParts(expectedParts);
    helper.assertTotalBytes(payload.length + expectedHeaderSize);
    Assert.assertThat("Generated Buffer", completeBuffer.remaining(), is(expectedSize));
    // Parse complete buffer.
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuffer);
    // Assert validity of frame
    WebSocketFrame actual = capture.getFrames().poll();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.BINARY));
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(payload.length));
    // Validate payload contents for proper masking
    ByteBuffer actualData = actual.getPayload().slice();
    Assert.assertThat("Frame.payload.remaining", actualData.remaining(), is(payload.length));
    while (actualData.remaining() > 0) {
        Assert.assertThat("Actual.payload[" + actualData.position() + "]", actualData.get(), is((byte) 0x55));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) 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 34 with IncomingFramesCapture

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

the class ParserTest method testParseNothing.

@Test
public void testParseNothing() {
    ByteBuffer buf = ByteBuffer.allocate(16);
    // Put nothing in the buffer.
    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();
    Assert.assertThat("Frame Count", capture.getFrames().size(), is(0));
}
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 35 with IncomingFramesCapture

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

the class ParserTest method testParseCase5_18.

/**
     * Similar to the server side 5.18 testcase. Text message fragmented as 2 frames, both as opcode=TEXT
     */
@Test
public void testParseCase5_18() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new TextFrame().setPayload("fragment1").setFin(false));
    // bad frame, must be continuation
    send.add(new TextFrame().setPayload("fragment2").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);
    // fragment 1
    capture.assertHasFrame(OpCode.TEXT, 1);
}
Also used : ArrayList(java.util.ArrayList) 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) 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