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));
}
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));
}
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));
}
}
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));
}
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);
}
Aggregations