From eed8db734094fad57c00f4c595bb6118baf32f89 Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Fri, 6 Jul 2012 15:28:46 -0500 Subject: [PATCH 1/2] updates to unit tests --- .../websocket/generator/FrameGenerator.java | 5 +- .../websocket/protocol/FrameBuilder.java | 12 +- .../jetty/websocket/util/CloseUtil.java | 10 +- .../jetty/websocket/ab/TestABCase2.java | 183 +++++++++--------- .../jetty/websocket/ab/TestABCase3.java | 120 +++++------- .../jetty/websocket/ab/TestABCase7_3.java | 43 ++-- .../generator/CloseFrameGeneratorTest.java | 18 -- .../RFC6455ExamplesGeneratorTest.java | 29 +-- 8 files changed, 195 insertions(+), 225 deletions(-) delete mode 100644 jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/CloseFrameGeneratorTest.java diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java index 6fd58d27b5f..34f34d066f2 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java @@ -6,9 +6,9 @@ import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.PolicyViolationException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; +import org.eclipse.jetty.websocket.protocol.CloseInfo; import org.eclipse.jetty.websocket.protocol.OpCode; import org.eclipse.jetty.websocket.protocol.WebSocketFrame; -import org.eclipse.jetty.websocket.util.CloseUtil; /** *
@@ -153,7 +153,8 @@ public class FrameGenerator
 
         if (frame.getOpCode() == OpCode.CLOSE)
         {
-            CloseUtil.assertValidPayload(frame);
+            // validate the close
+            new CloseInfo(frame.getPayloadData(),true);
         }
         // copy payload
         buffer.put(frame.getPayloadData());
diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/FrameBuilder.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/FrameBuilder.java
index df270b578e4..e793832fbe8 100644
--- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/FrameBuilder.java
+++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/FrameBuilder.java
@@ -71,11 +71,21 @@ public class FrameBuilder
         return new FrameBuilder(new WebSocketFrame(OpCode.PING));
     }
 
+    public static FrameBuilder ping(String message)
+    {
+        return new FrameBuilder(new WebSocketFrame(OpCode.PING)).payload(message.getBytes(StringUtil.__UTF8_CHARSET));
+    }
+
     public static FrameBuilder pong()
     {
         return new FrameBuilder(new WebSocketFrame(OpCode.PONG));
     }
 
+    public static FrameBuilder pong(String message)
+    {
+        return new FrameBuilder(new WebSocketFrame(OpCode.PONG)).payload(message.getBytes(StringUtil.__UTF8_CHARSET));
+    }
+
     public static FrameBuilder text()
     {
         return new FrameBuilder(new WebSocketFrame(OpCode.TEXT));
@@ -83,7 +93,7 @@ public class FrameBuilder
 
     public static FrameBuilder text(String text)
     {
-        return new FrameBuilder(new WebSocketFrame(OpCode.TEXT)).payload(text.getBytes());
+        return new FrameBuilder(new WebSocketFrame(OpCode.TEXT)).payload(text.getBytes(StringUtil.__UTF8_CHARSET));
     }
 
     private WebSocketFrame frame;
diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/util/CloseUtil.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/util/CloseUtil.java
index c8ac359259b..c08d1d5b6c2 100644
--- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/util/CloseUtil.java
+++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/util/CloseUtil.java
@@ -12,6 +12,12 @@ public class CloseUtil
     public static void assertValidPayload(WebSocketFrame frame)
     {
         byte payload[] = frame.getPayloadData();
+
+        if (payload.length < 2)
+        {
+            return; // no status code
+        }
+
         int statusCode = getStatusCode(payload);
 
         // Validate value
@@ -43,10 +49,6 @@ public class CloseUtil
 
     public static int getStatusCode(byte[] payload)
     {
-        if (payload.length < 2)
-        {
-            return 0; // no status code
-        }
 
         int statusCode = 0;
         ByteBuffer bb = ByteBuffer.wrap(payload);
diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase2.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase2.java
index 58a54acfbc8..791aa16e8c1 100644
--- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase2.java
+++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase2.java
@@ -1,6 +1,6 @@
 package org.eclipse.jetty.websocket.ab;
 
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.is;
 
 import java.nio.ByteBuffer;
 
@@ -8,10 +8,12 @@ import org.eclipse.jetty.websocket.ByteBufferAssert;
 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.PingFrame;
 import org.eclipse.jetty.websocket.generator.Generator;
 import org.eclipse.jetty.websocket.parser.FrameParseCapture;
 import org.eclipse.jetty.websocket.parser.Parser;
+import org.eclipse.jetty.websocket.protocol.FrameBuilder;
+import org.eclipse.jetty.websocket.protocol.OpCode;
+import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -23,14 +25,14 @@ public class TestABCase2
     public void testGenerate125OctetPingCase2_4()
     {
         byte[] bytes = new byte[125];
-        
+
         for ( int i = 0 ; i < bytes.length ; ++i )
         {
             bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
         }
-        
-        PingFrame pingFrame = new PingFrame(bytes);
-        
+
+        WebSocketFrame pingFrame = FrameBuilder.ping().payload(bytes).asFrame();
+
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(bytes.length + 32);
         generator.generate(actual, pingFrame);
@@ -39,26 +41,26 @@ public class TestABCase2
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= bytes.length & 0x7F;
         expected.put(b);
         expected.put(bytes);
-        
+
 
         actual.flip();
         expected.flip();
-        
+
         ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
     }
-    
+
     @Test
     public void testGenerateBinaryPingCase2_3()
     {
         byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
-        
-        PingFrame pingFrame = new PingFrame(bytes);
-        
+
+        WebSocketFrame pingFrame = FrameBuilder.ping().payload(bytes).asFrame();
+
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
         generator.generate(actual, pingFrame);
@@ -67,25 +69,25 @@ public class TestABCase2
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= bytes.length & 0x7F;
         expected.put(b);
         expected.put(bytes);
-        
+
 
         actual.flip();
         expected.flip();
-        
+
         ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
     }
-    
-    
+
+
     @Test
     public void testGenerateEmptyPingCase2_1()
     {
-        PingFrame pingFrame = new PingFrame();
-        
+        WebSocketFrame pingFrame = FrameBuilder.ping().asFrame();
+
 
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
@@ -95,21 +97,21 @@ public class TestABCase2
 
         expected.put(new byte[]
                 { (byte)0x89, (byte)0x00 });
-        
+
         actual.flip();
         expected.flip();
-        
+
         ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
     }
-    
+
     @Test
     public void testGenerateHelloPingCase2_2()
     {
         String message = "Hello, world!";
         byte[] messageBytes = message.getBytes();
-        
-        PingFrame pingFrame = new PingFrame(messageBytes);
-        
+
+        WebSocketFrame pingFrame = FrameBuilder.text(message).asFrame();
+
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
         generator.generate(actual, pingFrame);
@@ -118,16 +120,16 @@ public class TestABCase2
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= messageBytes.length & 0x7F;
         expected.put(b);
         expected.put(messageBytes);
-        
+
 
         actual.flip();
         expected.flip();
-        
+
         ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
     }
 
@@ -135,122 +137,123 @@ public class TestABCase2
     public void testGenerateOversizedBinaryPingCase2_5_A()
     {
         byte[] bytes = new byte[126];
-        
+
         for ( int i = 0 ; i < bytes.length ; ++i )
         {
             bytes[i] = 0x00;
         }
-             
-        new PingFrame(bytes);       
+
+        FrameBuilder.ping().payload(bytes);
     }
-    
+
     @Test( expected=WebSocketException.class )
     public void testGenerateOversizedBinaryPingCase2_5_B()
     {
         byte[] bytes = new byte[126];
-        
+
         for ( int i = 0 ; i < bytes.length ; ++i )
         {
             bytes[i] = 0x00;
         }
-             
-        PingFrame pingFrame = new PingFrame();   
-        pingFrame.setPayload(ByteBuffer.allocate(bytes.length + 32).put(bytes));
+
+        WebSocketFrame pingFrame = FrameBuilder.ping().payload(bytes).asFrame();
+
+        Generator generator = new Generator(WebSocketPolicy.newServerPolicy());
+        generator.generate(ByteBuffer.allocate(bytes.length + 32),pingFrame);
     }
- 
+
     @Test
     public void testParse125OctetPingCase2_4()
-    {      
+    {
         byte[] bytes = new byte[125];
-        
+
         for ( int i = 0 ; i < bytes.length ; ++i )
         {
             bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
         }
-        
+
         ByteBuffer expected = ByteBuffer.allocate(bytes.length + 32);
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= bytes.length & 0x7F;
         expected.put(b);
         expected.put(bytes);
-        
+
         expected.flip();
-        
+
         Parser parser = new Parser(policy);
         FrameParseCapture capture = new FrameParseCapture();
         parser.addListener(capture);
         parser.parse(expected);
-        
+
         capture.assertNoErrors();
-        capture.assertHasFrame(PingFrame.class,1);
-        
-        PingFrame pActual = (PingFrame)capture.getFrames().get(0);
+        capture.assertHasFrame(OpCode.PING,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(bytes.length));
-        ByteBufferAssert.assertSize("PingFrame.payload",bytes.length,pActual.getPayload());             
+        Assert.assertEquals("PingFrame.payload",bytes.length,pActual.getPayloadLength());
     }
-    
+
     @Test
     public void testParseBinaryPingCase2_3()
-    {      
+    {
         byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
 
         ByteBuffer expected = ByteBuffer.allocate(32);
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= bytes.length & 0x7F;
         expected.put(b);
         expected.put(bytes);
-        
+
         expected.flip();
-        
+
         Parser parser = new Parser(policy);
         FrameParseCapture capture = new FrameParseCapture();
         parser.addListener(capture);
         parser.parse(expected);
-        
+
         capture.assertNoErrors();
-        capture.assertHasFrame(PingFrame.class,1);
-        
-        PingFrame pActual = (PingFrame)capture.getFrames().get(0);
+        capture.assertHasFrame(OpCode.PING,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(bytes.length));
-        ByteBufferAssert.assertSize("PingFrame.payload",bytes.length,pActual.getPayload());        
-        
+        Assert.assertEquals("PingFrame.payload",bytes.length,pActual.getPayloadLength());
     }
-    
+
     @Test
     public void testParseEmptyPingCase2_1()
-    {        
+    {
         ByteBuffer expected = ByteBuffer.allocate(5);
 
         expected.put(new byte[]
                 { (byte)0x89, (byte)0x00 });
-        
+
         expected.flip();
-        
+
         Parser parser = new Parser(policy);
         FrameParseCapture capture = new FrameParseCapture();
         parser.addListener(capture);
         parser.parse(expected);
-        
+
         capture.assertNoErrors();
-        capture.assertHasFrame(PingFrame.class,1);
-        
-        PingFrame pActual = (PingFrame)capture.getFrames().get(0);
+        capture.assertHasFrame(OpCode.PING,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(0));
-        ByteBufferAssert.assertSize("PingFrame.payload",0,pActual.getPayload());        
-        
+        Assert.assertEquals("PingFrame.payload",0,pActual.getPayloadLength());
+
     }
-    
+
     @Test
     public void testParseHelloPingCase2_2()
-    {      
+    {
         String message = "Hello, world!";
         byte[] messageBytes = message.getBytes();
 
@@ -258,57 +261,57 @@ public class TestABCase2
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= messageBytes.length & 0x7F;
         expected.put(b);
         expected.put(messageBytes);
-        
+
         expected.flip();
-        
+
         Parser parser = new Parser(policy);
         FrameParseCapture capture = new FrameParseCapture();
         parser.addListener(capture);
         parser.parse(expected);
-        
+
         capture.assertNoErrors();
-        capture.assertHasFrame(PingFrame.class,1);
-        
-        PingFrame pActual = (PingFrame)capture.getFrames().get(0);
+        capture.assertHasFrame(OpCode.PING,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("PingFrame.payloadLength",pActual.getPayloadLength(),is(message.length()));
-        ByteBufferAssert.assertSize("PingFrame.payload",message.length(),pActual.getPayload());        
-        
+        Assert.assertEquals("PingFrame.payload",message.length(),pActual.getPayloadLength());
+
     }
 
-    
+
     @Test
     public void testParseOversizedBinaryPingCase2_5()
-    {      
+    {
         byte[] bytes = new byte[126];
-        
+
         for ( int i = 0 ; i < bytes.length ; ++i )
         {
             bytes[i] = 0x00;
         }
-        
+
         ByteBuffer expected = ByteBuffer.allocate(bytes.length + 32);
 
         expected.put(new byte[]
                 { (byte)0x89 });
-        
+
         byte b = 0x00; // no masking
         b |= bytes.length & 0x7F;
         expected.put(b);
         expected.put(bytes);
-        
+
         expected.flip();
-        
+
         Parser parser = new Parser(policy);
         FrameParseCapture capture = new FrameParseCapture();
         parser.addListener(capture);
         parser.parse(expected);
-        
+
         Assert.assertEquals( "error should be returned for too large of ping payload", 1, capture.getErrorCount(WebSocketException.class)) ;
     }
-    
+
 }
diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java
index 9a0f47cc33a..c546204efc4 100644
--- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java
+++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java
@@ -1,81 +1,65 @@
 package org.eclipse.jetty.websocket.ab;
 
-import org.eclipse.jetty.websocket.frames.CloseFrame;
-import org.eclipse.jetty.websocket.frames.PingFrame;
-import org.eclipse.jetty.websocket.frames.PongFrame;
-import org.junit.Test;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 
+import org.eclipse.jetty.websocket.api.PolicyViolationException;
+import org.eclipse.jetty.websocket.api.WebSocketPolicy;
+import org.eclipse.jetty.websocket.generator.Generator;
+import org.eclipse.jetty.websocket.protocol.FrameBuilder;
+import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
+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 TestABCase3
 {
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV1PingFrame()
+
+    @Parameters
+    public static Collection data()
     {
-        PingFrame pingFrame = new PingFrame();
-        
-        pingFrame.setRsv1(true);
+        List data = new ArrayList<>();
+        // @formatter:off
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.ping().rsv1(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.ping().rsv2(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.ping().rsv3(true).asFrame() });
+        data.add(new WebSocketFrame[]
+        { FrameBuilder.pong().rsv1(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.pong().rsv2(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.pong().rsv3(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.close().rsv1(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.close().rsv2(true).asFrame() });
+        data.add(new WebSocketFrame[]
+                { FrameBuilder.close().rsv3(true).asFrame() });
+        // @formatter:on
+        return data;
     }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV2PingFrame()
+
+    private WebSocketFrame invalidFrame;
+
+    public TestABCase3(WebSocketFrame invalidFrame)
     {
-        PingFrame pingFrame = new PingFrame();
-        
-        pingFrame.setRsv2(true);
+        this.invalidFrame = invalidFrame;
     }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV3PingFrame()
-    {
-        PingFrame pingFrame = new PingFrame();
-        
-        pingFrame.setRsv3(true);
-    }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV1PongFrame()
-    {
-        PongFrame pongFrame = new PongFrame();
-        
-        pongFrame.setRsv1(true);
-    }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV2PongFrame()
-    {
-        PongFrame pongFrame = new PongFrame();
-        
-        pongFrame.setRsv2(true);
-    }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV3PongFrame()
-    {
-        PongFrame pongFrame = new PongFrame();
-        
-        pongFrame.setRsv3(true);
-    }
-    
-    @Test( expected=IllegalArgumentException.class )
+
+    @Test(expected = PolicyViolationException.class)
     public void testGenerateRSV1CloseFrame()
     {
-        CloseFrame closeFrame = new CloseFrame();
-        
-        closeFrame.setRsv1(true);
-    }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV2CloseFrame()
-    {
-        CloseFrame closeFrame = new CloseFrame();
-        
-        closeFrame.setRsv2(true);
-    }
-    
-    @Test( expected=IllegalArgumentException.class )
-    public void testGenerateRSV3CloseFrame()
-    {
-        CloseFrame closeFrame = new CloseFrame();
-        
-        closeFrame.setRsv3(true);
+        Generator generator = new Generator(WebSocketPolicy.newServerPolicy());
+
+        generator.generate(ByteBuffer.allocate(32),invalidFrame);
     }
+
+
 }
diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java
index 85db07f1aca..85ef71ebd6e 100644
--- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java
+++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java
@@ -1,6 +1,6 @@
 package org.eclipse.jetty.websocket.ab;
 
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.is;
 
 import java.nio.ByteBuffer;
 
@@ -9,10 +9,12 @@ import org.eclipse.jetty.websocket.api.ProtocolException;
 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.generator.Generator;
 import org.eclipse.jetty.websocket.parser.FrameParseCapture;
 import org.eclipse.jetty.websocket.parser.Parser;
+import org.eclipse.jetty.websocket.protocol.FrameBuilder;
+import org.eclipse.jetty.websocket.protocol.OpCode;
+import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -23,8 +25,8 @@ public class TestABCase7_3
     @Test (expected = WebSocketException.class)
     public void testGenerate1BytePayloadCloseCase7_3_2()
     {
-        CloseFrame closeFrame = new CloseFrame();
-        closeFrame.setPayload(new byte[] {0x00});
+        WebSocketFrame closeFrame = FrameBuilder.close().payload(new byte[]
+                { 0x00 }).asFrame();
 
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
@@ -34,7 +36,7 @@ public class TestABCase7_3
     @Test
     public void testGenerateCloseWithStatusCase7_3_3()
     {
-        CloseFrame closeFrame = new CloseFrame(1000);
+        WebSocketFrame closeFrame = FrameBuilder.close(1000).asFrame();
 
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
@@ -63,7 +65,7 @@ public class TestABCase7_3
 
         byte[] messageBytes = message.toString().getBytes();
 
-        CloseFrame closeFrame = new CloseFrame(1000, message.toString());
+        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
 
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(132);
@@ -99,8 +101,11 @@ public class TestABCase7_3
 
         byte[] messageBytes = message.toString().getBytes();
 
-        CloseFrame closeFrame = new CloseFrame(1000, message.toString());
+        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
 
+        Generator generator = new Generator(policy);
+        ByteBuffer actual = ByteBuffer.allocate(32);
+        generator.generate(actual,closeFrame);
     }
 
     @Test
@@ -109,7 +114,7 @@ public class TestABCase7_3
         String message = "bad cough";
         byte[] messageBytes = message.getBytes();
 
-        CloseFrame closeFrame = new CloseFrame(1000, message);
+        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
 
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
@@ -135,7 +140,7 @@ public class TestABCase7_3
     @Test
     public void testGenerateEmptyCloseCase7_3_1()
     {
-        CloseFrame closeFrame = new CloseFrame();
+        WebSocketFrame closeFrame = FrameBuilder.close().asFrame();
 
         Generator generator = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
@@ -191,11 +196,10 @@ public class TestABCase7_3
         parser.parse(expected);
 
         capture.assertNoErrors();
-        capture.assertHasFrame(CloseFrame.class,1);
+        capture.assertHasFrame(OpCode.CLOSE,1);
 
-        CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(2));
-        ByteBufferAssert.assertSize("CloseFrame.payload",2,pActual.getPayload());
 
     }
 
@@ -230,11 +234,10 @@ public class TestABCase7_3
         parser.parse(expected);
 
         capture.assertNoErrors();
-        capture.assertHasFrame(CloseFrame.class,1);
+        capture.assertHasFrame(OpCode.CLOSE,1);
 
-        CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(125));
-        ByteBufferAssert.assertSize("CloseFrame.payload", 125,pActual.getPayload());
 
     }
 
@@ -297,11 +300,10 @@ public class TestABCase7_3
         parser.parse(expected);
 
         capture.assertNoErrors();
-        capture.assertHasFrame(CloseFrame.class,1);
+        capture.assertHasFrame(OpCode.CLOSE,1);
 
-        CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(messageBytes.length + 2));
-        ByteBufferAssert.assertSize("CloseFrame.payload",messageBytes.length + 2,pActual.getPayload());
 
     }
 
@@ -321,11 +323,10 @@ public class TestABCase7_3
         parser.parse(expected);
 
         capture.assertNoErrors();
-        capture.assertHasFrame(CloseFrame.class,1);
+        capture.assertHasFrame(OpCode.CLOSE,1);
 
-        CloseFrame pActual = (CloseFrame)capture.getFrames().get(0);
+        WebSocketFrame pActual = capture.getFrames().get(0);
         Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(0));
-        ByteBufferAssert.assertSize("CloseFrame.payload",0,pActual.getPayload());
 
     }
 }
diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/CloseFrameGeneratorTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/CloseFrameGeneratorTest.java
deleted file mode 100644
index 92bb9fdbfe3..00000000000
--- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/CloseFrameGeneratorTest.java
+++ /dev/null
@@ -1,18 +0,0 @@
-package org.eclipse.jetty.websocket.generator;
-
-import junit.framework.Assert;
-
-import org.eclipse.jetty.websocket.frames.CloseFrame;
-import org.eclipse.jetty.websocket.protocol.OpCode;
-import org.junit.Test;
-
-public class CloseFrameGeneratorTest
-{
-    @Test
-    public void testGenerator() throws Exception
-    {
-        CloseFrame close = new CloseFrame();
-
-        Assert.assertEquals(OpCode.CLOSE,close.getOpCode());
-    }
-}
diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java
index 681aefbbc8d..8085676628a 100644
--- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java
+++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java
@@ -3,12 +3,10 @@ package org.eclipse.jetty.websocket.generator;
 
 import java.nio.ByteBuffer;
 
-import org.eclipse.jetty.util.StringUtil;
+import org.eclipse.jetty.util.BufferUtil;
 import org.eclipse.jetty.websocket.ByteBufferAssert;
 import org.eclipse.jetty.websocket.api.WebSocketBehavior;
 import org.eclipse.jetty.websocket.api.WebSocketPolicy;
-import org.eclipse.jetty.websocket.frames.PingFrame;
-import org.eclipse.jetty.websocket.frames.PongFrame;
 import org.eclipse.jetty.websocket.protocol.FrameBuilder;
 import org.eclipse.jetty.websocket.protocol.WebSocketFrame;
 import org.junit.Test;
@@ -54,17 +52,11 @@ public class RFC6455ExamplesGeneratorTest
     @Test
     public void testSingleMaskedPongRequest()
     {
-        PongFrame pong = new PongFrame();
-        pong.setMask(new byte[]
-                { 0x37, (byte)0xfa, 0x21, 0x3d });
-
-        byte msg[] = "Hello".getBytes(StringUtil.__UTF8_CHARSET);
-        ByteBuffer payload = ByteBuffer.allocate(msg.length);
-        payload.put(msg);
-        pong.setPayload(payload);
+        WebSocketFrame pong = FrameBuilder.pong("Hello").mask(new byte[]
+                { 0x37, (byte)0xfa, 0x21, 0x3d }).asFrame();
 
         WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
-        PongFrameGenerator gen = new PongFrameGenerator(policy);
+        Generator gen = new Generator(policy);
 
         ByteBuffer actual = ByteBuffer.allocate(32);
         gen.generate(actual,pong);
@@ -118,7 +110,7 @@ public class RFC6455ExamplesGeneratorTest
         {
             payload.put((byte)0x44);
         }
-        binary.setPayload(payload);
+        binary.setPayload(BufferUtil.toArray(payload));
 
         WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
         Generator gen = new Generator(policy);
@@ -155,7 +147,7 @@ public class RFC6455ExamplesGeneratorTest
         {
             payload.put((byte)0x44);
         }
-        binary.setPayload(payload);
+        binary.setPayload(BufferUtil.toArray(payload));
 
         WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
         Generator gen = new Generator(policy);
@@ -197,16 +189,11 @@ public class RFC6455ExamplesGeneratorTest
     @Test
     public void testSingleUnmaskedPingRequest() throws Exception
     {
-        PingFrame ping = new PingFrame();
-
-        byte msg[] = "Hello".getBytes(StringUtil.__UTF8_CHARSET);
-        ByteBuffer payload = ByteBuffer.allocate(msg.length);
-        payload.put(msg);
-        ping.setPayload(payload);
+        WebSocketFrame ping = FrameBuilder.ping("Hello").asFrame();
 
         WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
 
-        PingFrameGenerator gen = new PingFrameGenerator(policy);
+        Generator gen = new Generator(policy);
         ByteBuffer actual = ByteBuffer.allocate(32);
         gen.generate(actual,ping);
         actual.flip(); // make readable

From c814772fc048c196f9e4c39eaccfeb69d779c6d8 Mon Sep 17 00:00:00 2001
From: Jesse McConnell 
Date: Fri, 6 Jul 2012 15:33:39 -0500
Subject: [PATCH 2/2] reorder tests

---
 .../jetty/websocket/ab/TestABCase7_3.java     | 344 +++++++++---------
 1 file changed, 172 insertions(+), 172 deletions(-)

diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java
index 85ef71ebd6e..22f93a1b10f 100644
--- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java
+++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase7_3.java
@@ -22,8 +22,52 @@ public class TestABCase7_3
 {
     WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
 
+    @Test
+    public void testCase7_3_1GenerateEmptyClose()
+    {
+        WebSocketFrame closeFrame = FrameBuilder.close().asFrame();
+
+        Generator generator = new Generator(policy);
+        ByteBuffer actual = ByteBuffer.allocate(32);
+        generator.generate(actual, closeFrame);
+
+        ByteBuffer expected = ByteBuffer.allocate(5);
+
+        expected.put(new byte[]
+                { (byte)0x88, (byte)0x00 });
+
+        actual.flip();
+        expected.flip();
+
+        ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
+    }
+
+    @Test
+    public void testCase7_3_1ParseEmptyClose()
+    {
+        ByteBuffer expected = ByteBuffer.allocate(5);
+
+        expected.put(new byte[]
+                { (byte)0x88, (byte)0x00 });
+
+        expected.flip();
+
+        Parser parser = new Parser(policy);
+        FrameParseCapture capture = new FrameParseCapture();
+        parser.addListener(capture);
+        parser.parse(expected);
+
+        capture.assertNoErrors();
+        capture.assertHasFrame(OpCode.CLOSE,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
+        Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(0));
+
+    }
+
+
     @Test (expected = WebSocketException.class)
-    public void testGenerate1BytePayloadCloseCase7_3_2()
+    public void testCase7_3_2Generate1BytePayloadClose()
     {
         WebSocketFrame closeFrame = FrameBuilder.close().payload(new byte[]
                 { 0x00 }).asFrame();
@@ -34,7 +78,29 @@ public class TestABCase7_3
     }
 
     @Test
-    public void testGenerateCloseWithStatusCase7_3_3()
+    public void testCase7_3_2Parse1BytePayloadClose()
+    {
+        ByteBuffer expected = ByteBuffer.allocate(32);
+
+        expected.put(new byte[]
+                { (byte)0x88, 0x01, 0x00 });
+
+        expected.flip();
+
+        Parser parser = new Parser(policy);
+        FrameParseCapture capture = new FrameParseCapture();
+        parser.addListener(capture);
+        parser.parse(expected);
+
+        Assert.assertEquals( "error on invalid close payload", 1, capture.getErrorCount(WebSocketException.class)) ;
+
+        WebSocketException known = capture.getErrors().get(0);
+
+        Assert.assertTrue("invalid payload should be in message",known.getMessage().contains("invalid payload length"));
+    }
+
+    @Test
+    public void testCase7_3_3GenerateCloseWithStatus()
     {
         WebSocketFrame closeFrame = FrameBuilder.close(1000).asFrame();
 
@@ -53,9 +119,92 @@ public class TestABCase7_3
         ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
     }
 
+    @Test
+    public void testCase7_3_3ParseCloseWithStatus()
+    {
+        ByteBuffer expected = ByteBuffer.allocate(5);
+
+        expected.put(new byte[]
+                { (byte)0x88, (byte)0x02, 0x03, (byte)0xe8  });
+
+        expected.flip();
+
+        Parser parser = new Parser(policy);
+        FrameParseCapture capture = new FrameParseCapture();
+        parser.addListener(capture);
+        parser.parse(expected);
+
+        capture.assertNoErrors();
+        capture.assertHasFrame(OpCode.CLOSE,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
+        Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(2));
+
+    }
+
 
     @Test
-    public void testGenerateCloseWithStatusMaxReasonCase7_3_5()
+    public void testCase7_3_4GenerateCloseWithStatusReason()
+    {
+        String message = "bad cough";
+        byte[] messageBytes = message.getBytes();
+
+        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
+
+        Generator generator = new Generator(policy);
+        ByteBuffer actual = ByteBuffer.allocate(32);
+        generator.generate(actual, closeFrame);
+
+        ByteBuffer expected = ByteBuffer.allocate(32);
+
+        expected.put(new byte[]
+                { (byte)0x88 });
+
+        byte b = 0x00; // no masking
+        b |= (message.length() + 2) & 0x7F;
+        expected.put(b);
+        expected.putShort((short)1000);
+        expected.put(messageBytes);
+
+        actual.flip();
+        expected.flip();
+
+        ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
+    }
+
+    @Test
+    public void testCase7_3_4ParseCloseWithStatusReason()
+    {
+        String message = "bad cough";
+        byte[] messageBytes = message.getBytes();
+
+        ByteBuffer expected = ByteBuffer.allocate(32);
+
+        expected.put(new byte[]
+                { (byte)0x88 });
+        byte b = 0x00; // no masking
+        b |= (messageBytes.length + 2) & 0x7F;
+        expected.put(b);
+        expected.putShort((short)1000);
+        expected.put(messageBytes);
+        expected.flip();
+
+        Parser parser = new Parser(policy);
+        FrameParseCapture capture = new FrameParseCapture();
+        parser.addListener(capture);
+        parser.parse(expected);
+
+        capture.assertNoErrors();
+        capture.assertHasFrame(OpCode.CLOSE,1);
+
+        WebSocketFrame pActual = capture.getFrames().get(0);
+        Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(messageBytes.length + 2));
+
+    }
+
+
+    @Test
+    public void testCase7_3_5GenerateCloseWithStatusMaxReason()
     {
         StringBuilder message = new StringBuilder();
         for ( int i = 0 ; i < 123 ; ++i )
@@ -90,122 +239,8 @@ public class TestABCase7_3
         ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
     }
 
-    @Test(expected = ProtocolException.class)
-    public void testGenerateCloseWithStatusMaxReasonCase7_3_6()
-    {
-        StringBuilder message = new StringBuilder();
-        for ( int i = 0 ; i < 124 ; ++i )
-        {
-            message.append("*");
-        }
-
-        byte[] messageBytes = message.toString().getBytes();
-
-        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
-
-        Generator generator = new Generator(policy);
-        ByteBuffer actual = ByteBuffer.allocate(32);
-        generator.generate(actual,closeFrame);
-    }
-
     @Test
-    public void testGenerateCloseWithStatusReasonCase7_3_4()
-    {
-        String message = "bad cough";
-        byte[] messageBytes = message.getBytes();
-
-        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
-
-        Generator generator = new Generator(policy);
-        ByteBuffer actual = ByteBuffer.allocate(32);
-        generator.generate(actual, closeFrame);
-
-        ByteBuffer expected = ByteBuffer.allocate(32);
-
-        expected.put(new byte[]
-                { (byte)0x88 });
-
-        byte b = 0x00; // no masking
-        b |= (message.length() + 2) & 0x7F;
-        expected.put(b);
-        expected.putShort((short)1000);
-        expected.put(messageBytes);
-
-        actual.flip();
-        expected.flip();
-
-        ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
-    }
-
-    @Test
-    public void testGenerateEmptyCloseCase7_3_1()
-    {
-        WebSocketFrame closeFrame = FrameBuilder.close().asFrame();
-
-        Generator generator = new Generator(policy);
-        ByteBuffer actual = ByteBuffer.allocate(32);
-        generator.generate(actual, closeFrame);
-
-        ByteBuffer expected = ByteBuffer.allocate(5);
-
-        expected.put(new byte[]
-                { (byte)0x88, (byte)0x00 });
-
-        actual.flip();
-        expected.flip();
-
-        ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
-    }
-
-
-    @Test
-    public void testParse1BytePayloadCloseCase7_3_2()
-    {
-        ByteBuffer expected = ByteBuffer.allocate(32);
-
-        expected.put(new byte[]
-                { (byte)0x88, 0x01, 0x00 });
-
-        expected.flip();
-
-        Parser parser = new Parser(policy);
-        FrameParseCapture capture = new FrameParseCapture();
-        parser.addListener(capture);
-        parser.parse(expected);
-
-        Assert.assertEquals( "error on invalid close payload", 1, capture.getErrorCount(WebSocketException.class)) ;
-
-        WebSocketException known = capture.getErrors().get(0);
-
-        Assert.assertTrue("invalid payload should be in message",known.getMessage().contains("invalid payload length"));
-    }
-
-    @Test
-    public void testParseCloseWithStatusCase7_3_3()
-    {
-        ByteBuffer expected = ByteBuffer.allocate(5);
-
-        expected.put(new byte[]
-                { (byte)0x88, (byte)0x02, 0x03, (byte)0xe8  });
-
-        expected.flip();
-
-        Parser parser = new Parser(policy);
-        FrameParseCapture capture = new FrameParseCapture();
-        parser.addListener(capture);
-        parser.parse(expected);
-
-        capture.assertNoErrors();
-        capture.assertHasFrame(OpCode.CLOSE,1);
-
-        WebSocketFrame pActual = capture.getFrames().get(0);
-        Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(2));
-
-    }
-
-
-    @Test
-    public void testParseCloseWithStatusMaxReasonCase7_3_5()
+    public void testCase7_3_5ParseCloseWithStatusMaxReason()
     {
         StringBuilder message = new StringBuilder();
         for ( int i = 0 ; i < 123 ; ++i )
@@ -241,8 +276,26 @@ public class TestABCase7_3
 
     }
 
+    @Test(expected = ProtocolException.class)
+    public void testCase7_3_6GenerateCloseWithInvalidStatusReason()
+    {
+        StringBuilder message = new StringBuilder();
+        for ( int i = 0 ; i < 124 ; ++i )
+        {
+            message.append("*");
+        }
+
+        byte[] messageBytes = message.toString().getBytes();
+
+        WebSocketFrame closeFrame = FrameBuilder.close(1000,message.toString()).asFrame();
+
+        Generator generator = new Generator(policy);
+        ByteBuffer actual = ByteBuffer.allocate(32);
+        generator.generate(actual,closeFrame);
+    }
+
     @Test
-    public void testParseCloseWithStatusMaxReasonCase7_3_6()
+    public void testCase7_3_6ParseCloseWithStatusMaxReason()
     {
         StringBuilder message = new StringBuilder();
         for ( int i = 0 ; i < 124 ; ++i )
@@ -276,57 +329,4 @@ public class TestABCase7_3
 
         Assert.assertTrue("invalid payload should be in message",known.getMessage().contains("invalid payload length"));
     }
-
-    @Test
-    public void testParseCloseWithStatusReasonCase7_3_4()
-    {
-        String message = "bad cough";
-        byte[] messageBytes = message.getBytes();
-
-        ByteBuffer expected = ByteBuffer.allocate(32);
-
-        expected.put(new byte[]
-                { (byte)0x88 });
-        byte b = 0x00; // no masking
-        b |= (messageBytes.length + 2) & 0x7F;
-        expected.put(b);
-        expected.putShort((short)1000);
-        expected.put(messageBytes);
-        expected.flip();
-
-        Parser parser = new Parser(policy);
-        FrameParseCapture capture = new FrameParseCapture();
-        parser.addListener(capture);
-        parser.parse(expected);
-
-        capture.assertNoErrors();
-        capture.assertHasFrame(OpCode.CLOSE,1);
-
-        WebSocketFrame pActual = capture.getFrames().get(0);
-        Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(messageBytes.length + 2));
-
-    }
-
-    @Test
-    public void testParseEmptyCloseCase7_3_1()
-    {
-        ByteBuffer expected = ByteBuffer.allocate(5);
-
-        expected.put(new byte[]
-                { (byte)0x88, (byte)0x00 });
-
-        expected.flip();
-
-        Parser parser = new Parser(policy);
-        FrameParseCapture capture = new FrameParseCapture();
-        parser.addListener(capture);
-        parser.parse(expected);
-
-        capture.assertNoErrors();
-        capture.assertHasFrame(OpCode.CLOSE,1);
-
-        WebSocketFrame pActual = capture.getFrames().get(0);
-        Assert.assertThat("CloseFrame.payloadLength",pActual.getPayloadLength(),is(0));
-
-    }
 }