use of org.eclipse.jetty.websocket.common.frames.BinaryFrame in project jetty.project by eclipse.
the class DeflateFrameExtensionTest method testCompressAndDecompressBigPayload.
@Test
public void testCompressAndDecompressBigPayload() throws Exception {
byte[] input = new byte[1024 * 1024];
// Make them not compressible.
new Random().nextBytes(input);
int maxMessageSize = (1024 * 1024) + 8192;
DeflateFrameExtension clientExtension = new DeflateFrameExtension();
clientExtension.setBufferPool(bufferPool);
clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
clientExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
serverExtension.setBufferPool(bufferPool);
serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
serverExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
// Chain the next element to decompress.
clientExtension.setNextOutgoingFrames(new OutgoingFrames() {
@Override
public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode) {
LOG.debug("outgoingFrame({})", frame);
serverExtension.incomingFrame(frame);
callback.writeSuccess();
}
});
final ByteArrayOutputStream result = new ByteArrayOutputStream(input.length);
serverExtension.setNextIncomingFrames(new IncomingFrames() {
@Override
public void incomingFrame(Frame frame) {
LOG.debug("incomingFrame({})", frame);
try {
result.write(BufferUtil.toArray(frame.getPayload()));
} catch (IOException x) {
throw new RuntimeIOException(x);
}
}
@Override
public void incomingError(Throwable t) {
}
});
BinaryFrame frame = new BinaryFrame();
frame.setPayload(input);
frame.setFin(true);
clientExtension.outgoingFrame(frame, null, BatchMode.OFF);
Assert.assertArrayEquals(input, result.toByteArray());
}
use of org.eclipse.jetty.websocket.common.frames.BinaryFrame in project jetty.project by eclipse.
the class TestABCase1_2 method testGenerate125ByteBinaryCase1_2_2.
@Test
public void testGenerate125ByteBinaryCase1_2_2() {
int length = 125;
ByteBuffer bb = ByteBuffer.allocate(length);
for (int i = 0; i < length; ++i) {
bb.put("*".getBytes());
}
bb.flip();
WebSocketFrame binaryFrame = new BinaryFrame().setPayload(bb);
ByteBuffer actual = UnitGenerator.generate(binaryFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[] { (byte) 0x82 });
// no masking
byte b = 0x00;
b |= length & 0x7F;
expected.put(b);
for (int i = 0; i < length; ++i) {
expected.put("*".getBytes());
}
BufferUtil.flipToFlush(expected, 0);
ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
use of org.eclipse.jetty.websocket.common.frames.BinaryFrame in project jetty.project by eclipse.
the class TestABCase1_2 method testGenerate126ByteBinaryCase1_2_3.
@Test
public void testGenerate126ByteBinaryCase1_2_3() {
int length = 126;
ByteBuffer bb = ByteBuffer.allocate(length);
for (int i = 0; i < length; ++i) {
bb.put("*".getBytes());
}
bb.flip();
WebSocketFrame binaryFrame = new BinaryFrame().setPayload(bb);
ByteBuffer actual = UnitGenerator.generate(binaryFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[] { (byte) 0x82 });
// no masking
byte b = 0x00;
b |= length & 0x7E;
expected.put(b);
//expected.put((byte)((length>>8) & 0xFF));
//expected.put((byte)(length & 0xFF));
expected.putShort((short) length);
for (int i = 0; i < length; ++i) {
expected.put("*".getBytes());
}
BufferUtil.flipToFlush(expected, 0);
ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
use of org.eclipse.jetty.websocket.common.frames.BinaryFrame in project jetty.project by eclipse.
the class EventDriverTest method testAnnotated_Frames.
@Test
public void testAnnotated_Frames() throws Exception {
AnnotatedFramesSocket socket = new AnnotatedFramesSocket();
EventDriver driver = wrap(socket);
try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container, testname, driver)) {
conn.open();
driver.incomingFrame(new PingFrame().setPayload("PING"));
driver.incomingFrame(new TextFrame().setPayload("Text Me"));
driver.incomingFrame(new BinaryFrame().setPayload("Hello Bin"));
driver.incomingFrame(new CloseInfo(StatusCode.SHUTDOWN, "testcase").asFrame());
socket.capture.assertEventCount(6);
socket.capture.pop().assertEventStartsWith("onConnect(");
socket.capture.pop().assertEventStartsWith("onFrame(PING[");
socket.capture.pop().assertEventStartsWith("onFrame(TEXT[");
socket.capture.pop().assertEventStartsWith("onFrame(BINARY[");
socket.capture.pop().assertEventStartsWith("onFrame(CLOSE[");
socket.capture.pop().assertEventStartsWith("onClose(1001,");
}
}
use of org.eclipse.jetty.websocket.common.frames.BinaryFrame 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));
}
}
Aggregations