use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class TextPayloadParserTest method testShortMaskedText.
@Test
public void testShortMaskedText() throws Exception {
String expectedText = "Hello World";
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(24);
buf.put((byte) 0x81);
buf.put((byte) (0x80 | utf.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
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();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class TextPayloadParserTest method testShortMaskedFragmentedText.
@Test
public void testShortMaskedFragmentedText() throws Exception {
String part1 = "Hello ";
String part2 = "World";
byte[] b1 = part1.getBytes(StandardCharsets.UTF_8);
byte[] b2 = part2.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(32);
// part 1
// no fin + text
buf.put((byte) 0x01);
buf.put((byte) (0x80 | b1.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, b1);
// part 2
// fin + continuation
buf.put((byte) 0x80);
buf.put((byte) (0x80 | b2.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, b2);
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);
capture.assertHasFrame(OpCode.CONTINUATION, 1);
WebSocketFrame txt = capture.getFrames().poll();
Assert.assertThat("TextFrame[0].data", txt.getPayloadAsUTF8(), is(part1));
txt = capture.getFrames().poll();
Assert.assertThat("TextFrame[1].data", txt.getPayloadAsUTF8(), is(part2));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class TextPayloadParserTest method testLongMaskedText.
@Test
public void testLongMaskedText() throws Exception {
StringBuffer sb = new StringBuffer();
;
for (int i = 0; i < 3500; i++) {
sb.append("Hello Big World ");
}
sb.append(". The end.");
String expectedText = sb.toString();
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a long length payload", utf.length, greaterThan(0xFFFF));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 32);
// text frame, fin = true
buf.put((byte) 0x81);
// 0x7F == 127 (a 8 byte payload length)
buf.put((byte) (0x80 | 0x7F));
buf.putLong(utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setMaxTextMessageSize(100000);
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();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class WebSocketFrameTest method initGenerator.
@Before
public void initGenerator() {
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
strictGenerator = new Generator(policy, bufferPool);
laxGenerator = new Generator(policy, bufferPool, false);
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class ParserTest method testWindowedParseLargeFrame.
@Test
public void testWindowedParseLargeFrame() {
// Create frames
byte[] payload = new byte[65536];
Arrays.fill(payload, (byte) '*');
List<WebSocketFrame> frames = new ArrayList<>();
TextFrame text = new TextFrame();
text.setPayload(ByteBuffer.wrap(payload));
text.setMask(Hex.asByteArray("11223344"));
frames.add(text);
frames.add(new CloseInfo(StatusCode.NORMAL).asFrame());
// Build up raw (network bytes) buffer
ByteBuffer networkBytes = UnitGenerator.generate(frames);
// Parse, in 4096 sized windows
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
while (networkBytes.remaining() > 0) {
ByteBuffer window = networkBytes.slice();
int windowSize = Math.min(window.remaining(), 4096);
window.limit(windowSize);
parser.parse(window);
networkBytes.position(networkBytes.position() + windowSize);
}
capture.assertNoErrors();
Assert.assertThat("Frame Count", capture.getFrames().size(), is(2));
WebSocketFrame frame = capture.getFrames().poll();
Assert.assertThat("Frame[0].opcode", frame.getOpCode(), is(OpCode.TEXT));
ByteBuffer actualPayload = frame.getPayload();
Assert.assertThat("Frame[0].payload.length", actualPayload.remaining(), is(payload.length));
// Should be all '*' characters (if masking is correct)
for (int i = actualPayload.position(); i < actualPayload.remaining(); i++) {
Assert.assertThat("Frame[0].payload[i]", actualPayload.get(i), is((byte) '*'));
}
}
Aggregations