@Test public void testParserAndGenerator() throws Exception { WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); ByteBufferPool bufferPool = new MappedByteBufferPool(); 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 = WebSocketFrame.text(message); out = gen.generate(frame); // Parse Buffer parser.parse(out); } finally { bufferPool.release(out); } // Validate capture.assertNoErrors(); capture.assertHasFrame(OpCode.TEXT, 1); WebSocketFrame txt = capture.getFrames().get(0); Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message)); }
/** * Similar to the server side 6.2.3 testcase. Lots of small 1 byte UTF8 Text frames, representing * 1 overall text message. */ @Test public void testParseCase6_2_3() { String utf8 = "Hello-\uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-8!!"; byte msg[] = StringUtil.getUtf8Bytes(utf8); List<WebSocketFrame> send = new ArrayList<>(); int len = msg.length; byte opcode = OpCode.TEXT; byte mini[]; for (int i = 0; i < len; i++) { WebSocketFrame frame = new WebSocketFrame(opcode); mini = new byte[1]; mini[0] = msg[i]; frame.setPayload(mini); boolean isLast = (i >= (len - 1)); frame.setFin(isLast); send.add(frame); opcode = OpCode.CONTINUATION; } send.add(new CloseInfo(StatusCode.NORMAL).asFrame()); ByteBuffer completeBuf = UnitGenerator.generate(send); UnitParser parser = new UnitParser(); IncomingFramesCapture capture = new IncomingFramesCapture(); parser.setIncomingFramesHandler(capture); parser.parse(completeBuf); capture.assertErrorCount(0); capture.assertHasFrame(OpCode.TEXT, len); capture.assertHasFrame(OpCode.CLOSE, 1); }
/** Similar to the server side 5.6 testcase. pong, then text, then close frames. */ @Test public void testParseCase5_6() { List<WebSocketFrame> send = new ArrayList<>(); send.add(WebSocketFrame.pong().setPayload("ping")); send.add(WebSocketFrame.text("hello, world")); send.add(new CloseInfo(StatusCode.NORMAL).asFrame()); ByteBuffer completeBuf = UnitGenerator.generate(send); UnitParser parser = new UnitParser(); IncomingFramesCapture capture = new IncomingFramesCapture(); parser.setIncomingFramesHandler(capture); parser.parse(completeBuf); capture.assertErrorCount(0); capture.assertHasFrame(OpCode.TEXT, 1); capture.assertHasFrame(OpCode.CLOSE, 1); capture.assertHasFrame(OpCode.PONG, 1); }
/** Similar to the server side 6.4.3 testcase. */ @Test public void testParseCase6_4_3() { ByteBuffer payload = ByteBuffer.allocate(64); BufferUtil.clearToFill(payload); payload.put(TypeUtil.fromHexString("cebae1bdb9cf83cebcceb5")); // good payload.put(TypeUtil.fromHexString("f4908080")); // INVALID payload.put(TypeUtil.fromHexString("656469746564")); // good BufferUtil.flipToFlush(payload, 0); WebSocketFrame text = new WebSocketFrame(); text.setMask(TypeUtil.fromHexString("11223344")); text.setPayload(payload); text.setOpCode(OpCode.TEXT); ByteBuffer buf = new UnitGenerator().generate(text); ByteBuffer part1 = ByteBuffer.allocate(17); // header + good ByteBuffer part2 = ByteBuffer.allocate(4); // invalid ByteBuffer part3 = ByteBuffer.allocate(10); // the rest (all good utf) BufferUtil.put(buf, part1); BufferUtil.put(buf, part2); BufferUtil.put(buf, part3); BufferUtil.flipToFlush(part1, 0); BufferUtil.flipToFlush(part2, 0); BufferUtil.flipToFlush(part3, 0); LOG.debug("Part1: {}", BufferUtil.toDetailString(part1)); LOG.debug("Part2: {}", BufferUtil.toDetailString(part2)); LOG.debug("Part3: {}", BufferUtil.toDetailString(part3)); UnitParser parser = new UnitParser(); IncomingFramesCapture capture = new IncomingFramesCapture(); parser.setIncomingFramesHandler(capture); parseQuietly(parser, part1); capture.assertErrorCount(0); parseQuietly(parser, part2); capture.assertErrorCount(1); capture.assertHasErrors(BadPayloadException.class, 1); }