additional close frame testing

This commit is contained in:
Jesse McConnell 2012-06-29 07:51:19 -05:00
parent 54399545b1
commit acabb0a2da
3 changed files with 140 additions and 4 deletions

View File

@ -36,18 +36,43 @@ public class CloseFrame extends ControlFrame
constructPayload(statusCode,reason);
}
private void constructPayload(int statusCode, String reason)
public void assertValidPayload(int statusCode, String reason)
{
if ((statusCode <= 999) || (statusCode > 65535))
{
throw new IllegalArgumentException("Status Codes must be in the range 1000 - 65535");
}
if ((statusCode >= 1004) && (statusCode <= 1006))
{
throw new IllegalArgumentException("Status Code " + statusCode + " is reserved according to RFC6455 and can not be used in a close frame.");
}
if ((statusCode >= 1012) && (statusCode <= 1016))
{
throw new IllegalArgumentException("Status Code " + statusCode + " is reserved according to RFC6455 and can not be used in a close frame.");
}
if ((statusCode == 1100) || (statusCode == 2000) || (statusCode == 2999))
{
throw new IllegalArgumentException("Status Code " + statusCode + " is reserved according to RFC6455 and can not be used in a close frame.");
}
if ((reason != null) && (reason.length() > 123))
{
throw new IllegalArgumentException("Reason must not exceed 123 characters.");
}
// TODO add check for invalid utf-8
}
private void constructPayload(int statusCode, String reason)
{
assertValidPayload(statusCode,reason);
byte utf[] = null;
int len = 2; // status code
if (StringUtil.isNotBlank(reason))
@ -95,11 +120,28 @@ public class CloseFrame extends ControlFrame
return statusCode;
}
public boolean hasReason()
{
return getPayloadLength() > 2;
}
@Override
public void setPayload(byte[] buf)
{
super.setPayload(buf);
assertValidPayload(getStatusCode(),getReason());
}
@Override
public void setPayload(ByteBuffer payload)
{
super.setPayload(payload);
assertValidPayload(getStatusCode(),getReason());
}
@Override
public String toString()
{

View File

@ -23,12 +23,12 @@ public class TestABCase7_3
@Test (expected = WebSocketException.class)
public void testGenerate1BytePayloadCloseCase7_3_2()
{
CloseFrame pingFrame = new CloseFrame();
pingFrame.setPayload(new byte[] {0x00});
CloseFrame closeFrame = new CloseFrame();
closeFrame.setPayload(new byte[] {0x00});
Generator generator = new Generator(policy);
ByteBuffer actual = ByteBuffer.allocate(32);
generator.generate(actual, pingFrame);
generator.generate(actual, closeFrame);
}
@Test

View File

@ -0,0 +1,94 @@
package org.eclipse.jetty.websocket.ab;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.CloseFrame;
import org.eclipse.jetty.websocket.parser.FrameParseCapture;
import org.eclipse.jetty.websocket.parser.Parser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class TestABCase7_9
{
@Parameters
public static Collection<Integer[]> data()
{
List<Integer[]> data = new ArrayList<>();
// @formatter:off
data.add(new Integer[]
{ 1004 });
data.add(new Integer[]
{ 1005 });
data.add(new Integer[]
{ 1006 });
data.add(new Integer[]
{ 1012 });
data.add(new Integer[]
{ 1013 });
data.add(new Integer[]
{ 1014 });
data.add(new Integer[]
{ 1015 });
data.add(new Integer[]
{ 1016 });
data.add(new Integer[]
{ 1100 });
data.add(new Integer[]
{ 2000 });
data.add(new Integer[]
{ 2999 });
// @formatter:on
return data;
}
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
private int invalidStatusCode;
public TestABCase7_9(Integer invalidStatusCode)
{
this.invalidStatusCode = invalidStatusCode.intValue();
}
@Test(expected = IllegalArgumentException.class)
public void testCase7_9GenerateInvalidCloseStatus()
{
CloseFrame closeFrame = new CloseFrame(invalidStatusCode);
}
@Test
public void testCase7_9ParseInvalidCloseStatus()
{
ByteBuffer expected = ByteBuffer.allocate(32);
expected.put(new byte[]
{ (byte)0x88, 0x02 });
expected.putChar((char)invalidStatusCode);
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
Assert.assertEquals("error on invalid close status code",1,capture.getErrorCount(WebSocketException.class));
WebSocketException known = capture.getErrors().get(0);
Assert.assertTrue("reserved should be in message",known.getMessage().contains("reserved"));
}
}