Adding policy violation testcase for parsing too large text frames

This commit is contained in:
Joakim Erdfelt 2012-06-20 14:19:55 -07:00
parent 085127c8f8
commit b61a58ad64
2 changed files with 56 additions and 2 deletions

View File

@ -1,7 +1,6 @@
package org.eclipse.jetty.websocket.parser;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import java.util.ArrayList;
import java.util.List;
@ -15,6 +14,11 @@ public class FrameParseCapture implements Parser.Listener
private List<BaseFrame> frames = new ArrayList<>();
private List<WebSocketException> errors = new ArrayList<>();
public void assertHasErrors(Class<? extends WebSocketException> errorType, int expectedCount)
{
Assert.assertThat(errorType.getSimpleName(),getErrorCount(errorType),is(expectedCount));
}
public void assertHasFrame(Class<? extends BaseFrame> frameType)
{
Assert.assertThat(frameType.getSimpleName(),getFrameCount(frameType),greaterThanOrEqualTo(1));
@ -25,11 +29,28 @@ public class FrameParseCapture implements Parser.Listener
Assert.assertThat(frameType.getSimpleName(),getFrameCount(frameType),is(expectedCount));
}
public void assertHasNoFrames()
{
Assert.assertThat("Has no frames",frames.size(),is(0));
}
public void assertNoErrors()
{
Assert.assertThat("Has no errors",errors.size(),is(0));
}
public int getErrorCount(Class<? extends WebSocketException> errorType)
{
int count = 0;
for(WebSocketException error: errors) {
if (errorType.isInstance(error))
{
count++;
}
}
return count;
}
public List<WebSocketException> getErrors()
{
return errors;

View File

@ -3,8 +3,11 @@ package org.eclipse.jetty.websocket.parser;
import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.api.PolicyViolationException;
import org.eclipse.jetty.websocket.api.WebSocket;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.TextFrame;
@ -16,6 +19,36 @@ public class TextPayloadParserTest
private final byte[] mask = new byte[]
{ 0x00, (byte)0xF0, 0x0F, (byte)0xFF };
@Test
public void testFrameTooLargeDueToPolicyText() throws Exception
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(1024); // set policy
byte utf[] = new byte[2048];
Arrays.fill(utf,(byte)'a');
Assert.assertThat("Must be a medium length payload",utf.length,allOf(greaterThan(0x7E),lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 8);
buf.put((byte)0x81);
buf.put((byte)(0x80 | 0x7E)); // 0x7E == 126 (a 2 byte payload length)
buf.putShort((short)utf.length);
writeMask(buf);
writeMaskedPayload(buf,utf);
buf.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(buf);
capture.assertHasErrors(PolicyViolationException.class,1);
capture.assertHasNoFrames();
PolicyViolationException err = (PolicyViolationException)capture.getErrors().get(0);
Assert.assertThat("Error.closeCode",err.getCloseCode(),is(WebSocket.CLOSE_POLICY_VIOLATION));
}
@Test
public void testLongMaskedText() throws Exception
{