use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class DeflateFrameExtensionTest method assertIncoming.
private void assertIncoming(byte[] raw, String... expectedTextDatas) {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
DeflateFrameExtension ext = new DeflateFrameExtension();
ext.setBufferPool(bufferPool);
ext.setPolicy(policy);
ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
ext.setConfig(config);
// Setup capture of incoming frames
IncomingFramesCapture capture = new IncomingFramesCapture();
// Wire up stack
ext.setNextIncomingFrames(capture);
Parser parser = new UnitParser(policy);
parser.configureFromExtensions(Collections.singletonList(ext));
parser.setIncomingFramesHandler(ext);
parser.parse(ByteBuffer.wrap(raw));
int len = expectedTextDatas.length;
capture.assertFrameCount(len);
capture.assertHasFrame(OpCode.TEXT, len);
int i = 0;
for (WebSocketFrame actual : capture.getFrames()) {
String prefix = "Frame[" + i + "]";
Assert.assertThat(prefix + ".opcode", actual.getOpCode(), is(OpCode.TEXT));
Assert.assertThat(prefix + ".fin", actual.isFin(), is(true));
// RSV1 should be unset at this point
Assert.assertThat(prefix + ".rsv1", actual.isRsv1(), is(false));
Assert.assertThat(prefix + ".rsv2", actual.isRsv2(), is(false));
Assert.assertThat(prefix + ".rsv3", actual.isRsv3(), is(false));
ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i], StandardCharsets.UTF_8);
Assert.assertThat(prefix + ".payloadLength", actual.getPayloadLength(), is(expected.remaining()));
ByteBufferAssert.assertEquals(prefix + ".payload", expected, actual.getPayload().slice());
i++;
}
}
use of org.eclipse.jetty.websocket.common.test.UnitParser in project jetty.project by eclipse.
the class TestABCase7_3 method testCase7_3_4ParseCloseWithStatusReason.
@Test
public void testCase7_3_4ParseCloseWithStatusReason() {
String message = "bad cough";
byte[] messageBytes = message.getBytes();
ByteBuffer expected = ByteBuffer.allocate(32);
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(messageBytes.length + 2));
}
use of org.eclipse.jetty.websocket.common.test.UnitParser 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));
}
}
use of org.eclipse.jetty.websocket.common.test.UnitParser 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));
}
use of org.eclipse.jetty.websocket.common.test.UnitParser 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);
}
Aggregations