From a566891046b16f8c231dbcd83ed2578d8d93aea1 Mon Sep 17 00:00:00 2001 From: marci4 Date: Tue, 8 Oct 2024 22:32:30 +0200 Subject: [PATCH 1/5] Correctly clone values provided by the setter Fixes #1437 --- .../PerMessageDeflateExtension.java | 6 ++- .../PerMessageDeflateExtensionTest.java | 39 ++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java index f24f9b6c9..af7031869 100644 --- a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java +++ b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java @@ -330,7 +330,11 @@ public String getProvidedExtensionAsServer() { @Override public IExtension copyInstance() { - return new PerMessageDeflateExtension(); + PerMessageDeflateExtension clone = new PerMessageDeflateExtension(); + clone.setThreshold(this.getThreshold()); + clone.setClientNoContextTakeover(this.isClientNoContextTakeover()); + clone.setServerNoContextTakeover(this.isServerNoContextTakeover()); + return clone; } /** diff --git a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java index 0a25383b6..57ba5c228 100644 --- a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java +++ b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java @@ -191,8 +191,36 @@ public void testSetClientNoContextTakeover() { @Test public void testCopyInstance() { PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - IExtension newDeflateExtension = deflateExtension.copyInstance(); - assertEquals(deflateExtension.toString(), newDeflateExtension.toString()); + PerMessageDeflateExtension newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); + assertEquals("PerMessageDeflateExtension", newDeflateExtension.toString()); + // Also check the values + assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); + assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); + assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); + // Adjust this to the factory + //assertEquals(deflateExtension.getDeflater(), newDeflateExtension.getDeflater()); + //assertEquals(deflateExtension.getInflater(), newDeflateExtension.getInflater()); + + deflateExtension = new PerMessageDeflateExtension(); + deflateExtension.setThreshold(512); + deflateExtension.setServerNoContextTakeover(false); + deflateExtension.setClientNoContextTakeover(true); + newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); + + assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); + assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); + assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); + + + deflateExtension = new PerMessageDeflateExtension(); + deflateExtension.setThreshold(64); + deflateExtension.setServerNoContextTakeover(true); + deflateExtension.setClientNoContextTakeover(false); + newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); + + assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); + assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); + assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); } @Test @@ -222,4 +250,11 @@ public void testSetDeflater() { assertEquals(deflateExtension.getDeflater().finished(), new Deflater(Deflater.DEFAULT_COMPRESSION, false).finished()); } + @Test + public void testDefaults() { + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); + assertFalse(deflateExtension.isClientNoContextTakeover()); + assertTrue(deflateExtension.isServerNoContextTakeover()); + assertEquals(1024, deflateExtension.getThreshold()); + } } From 1e9fbbf2c9b22605505d57785848880aa2ad8154 Mon Sep 17 00:00:00 2001 From: marci4 Date: Tue, 8 Oct 2024 23:14:08 +0200 Subject: [PATCH 2/5] Reuse inflater/deflater --- .../permessage_deflate/PerMessageDeflateExtension.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java index af7031869..b38ad0928 100644 --- a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java +++ b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java @@ -166,7 +166,7 @@ We can check the getRemaining() method to see whether the data we supplied has b Note that this behavior doesn't occur if the message is "first compressed and then fragmented". */ if (inflater.getRemaining() > 0) { - inflater = new Inflater(true); + inflater.reset(); decompress(inputFrame.getPayloadData().array(), output); } @@ -174,7 +174,7 @@ We can check the getRemaining() method to see whether the data we supplied has b decompress(TAIL_BYTES, output); // If context takeover is disabled, inflater can be reset. if (clientNoContextTakeover) { - inflater = new Inflater(true); + inflater.reset(); } } } catch (DataFormatException e) { @@ -244,8 +244,7 @@ public void encodeFrame(Framedata inputFrame) { } if (serverNoContextTakeover) { - deflater.end(); - deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); + deflater.reset(); } } From fe7635bae9aa0f659da7e29194359b5d7454dc59 Mon Sep 17 00:00:00 2001 From: marci4 Date: Sun, 13 Oct 2024 11:36:42 +0200 Subject: [PATCH 3/5] Provide a setter/getter to set the deflater level --- .../PerMessageDeflateExtension.java | 32 +++++++++------- .../PerMessageDeflateExtensionTest.java | 37 ++++++------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java index b38ad0928..b90ddce3b 100644 --- a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java +++ b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java @@ -53,23 +53,28 @@ public class PerMessageDeflateExtension extends CompressionExtension { // For WebSocketClients, this variable holds the extension parameters that client himself has requested. private Map requestedParameters = new LinkedHashMap<>(); - private Inflater inflater = new Inflater(true); - private Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); - - public Inflater getInflater() { - return inflater; - } + private int deflaterLevel = Deflater.DEFAULT_COMPRESSION; - public void setInflater(Inflater inflater) { - this.inflater = inflater; - } + private Inflater inflater = new Inflater(true); + private Deflater deflater = new Deflater(this.deflaterLevel, true); - public Deflater getDeflater() { - return deflater; + /** + * Get the compression level used for the compressor. + * @return the compression level (0-9) + */ + public int getDeflaterLevel() { + return this.deflaterLevel; } - public void setDeflater(Deflater deflater) { - this.deflater = deflater; + /** + * Set the compression level used for the compressor. + * @param level the compression level (0-9) + */ + public void setDeflaterLevel(int level) { + this.deflater.setLevel(level); + this.deflaterLevel = level; + //If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation. + this.deflater.deflate(new byte[0]); } /** @@ -333,6 +338,7 @@ public IExtension copyInstance() { clone.setThreshold(this.getThreshold()); clone.setClientNoContextTakeover(this.isClientNoContextTakeover()); clone.setServerNoContextTakeover(this.isServerNoContextTakeover()); + clone.setDeflaterLevel(this.getDeflaterLevel()); return clone; } diff --git a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java index 57ba5c228..e434be9fb 100644 --- a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java +++ b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java @@ -197,64 +197,49 @@ public void testCopyInstance() { assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); - // Adjust this to the factory - //assertEquals(deflateExtension.getDeflater(), newDeflateExtension.getDeflater()); - //assertEquals(deflateExtension.getInflater(), newDeflateExtension.getInflater()); + assertEquals(deflateExtension.getDeflaterLevel(), newDeflateExtension.getDeflaterLevel()); + deflateExtension = new PerMessageDeflateExtension(); deflateExtension.setThreshold(512); deflateExtension.setServerNoContextTakeover(false); deflateExtension.setClientNoContextTakeover(true); + deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); + assertEquals(deflateExtension.getDeflaterLevel(), newDeflateExtension.getDeflaterLevel()); deflateExtension = new PerMessageDeflateExtension(); deflateExtension.setThreshold(64); deflateExtension.setServerNoContextTakeover(true); deflateExtension.setClientNoContextTakeover(false); + deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); + assertEquals(deflateExtension.getDeflaterLevel(), newDeflateExtension.getDeflaterLevel()); } @Test - public void testGetInflater() { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - assertEquals(deflateExtension.getInflater().getRemaining(), new Inflater(true).getRemaining()); - } - - @Test - public void testSetInflater() { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - deflateExtension.setInflater(new Inflater(false)); - assertEquals(deflateExtension.getInflater().getRemaining(), new Inflater(false).getRemaining()); - } - - @Test - public void testGetDeflater() { + public void testDeflaterLevel() { PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - assertEquals(deflateExtension.getDeflater().finished(), - new Deflater(Deflater.DEFAULT_COMPRESSION, true).finished()); + assertEquals(Deflater.DEFAULT_COMPRESSION, deflateExtension.getDeflaterLevel()); + deflateExtension.setDeflaterLevel(Deflater.BEST_SPEED); + assertEquals(Deflater.BEST_SPEED, deflateExtension.getDeflaterLevel()); } - @Test - public void testSetDeflater() { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - deflateExtension.setDeflater(new Deflater(Deflater.DEFAULT_COMPRESSION, false)); - assertEquals(deflateExtension.getDeflater().finished(), - new Deflater(Deflater.DEFAULT_COMPRESSION, false).finished()); - } @Test public void testDefaults() { PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); assertFalse(deflateExtension.isClientNoContextTakeover()); assertTrue(deflateExtension.isServerNoContextTakeover()); assertEquals(1024, deflateExtension.getThreshold()); + assertEquals(Deflater.DEFAULT_COMPRESSION, deflateExtension.getDeflaterLevel()); } } From 4f4aed58c9025e1c5a06f8361651bc8a6d54d221 Mon Sep 17 00:00:00 2001 From: marci4 Date: Sun, 13 Oct 2024 15:53:21 +0200 Subject: [PATCH 4/5] Extends tests to better test the deflater level --- .../PerMessageDeflateExtensionTest.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java index e434be9fb..9093ea483 100644 --- a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java +++ b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java @@ -1,5 +1,6 @@ package org.java_websocket.extensions; +import static java.util.zip.GZIPInputStream.GZIP_MAGIC; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -7,6 +8,7 @@ import static org.junit.Assert.fail; import java.nio.ByteBuffer; +import java.util.Arrays; import java.util.zip.Deflater; import java.util.zip.Inflater; import org.java_websocket.exceptions.InvalidDataException; @@ -51,6 +53,111 @@ public void testDecodeFrameIfRSVIsNotSet() throws InvalidDataException { assertFalse(frame.isRSV1()); } + @Test + public void testDecodeFrameNoCompression() throws InvalidDataException { + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); + deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); + deflateExtension.setThreshold(0); + String str = "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text"; + byte[] message = str.getBytes(); + TextFrame frame = new TextFrame(); + frame.setPayload(ByteBuffer.wrap(message)); + deflateExtension.encodeFrame(frame); + byte[] payloadArray = frame.getPayloadData().array(); + assertArrayEquals(message, Arrays.copyOfRange(payloadArray, 5,payloadArray.length-5)); + assertTrue(frame.isRSV1()); + deflateExtension.decodeFrame(frame); + assertArrayEquals(message, frame.getPayloadData().array()); + } + + @Test + public void testDecodeFrameBestSpeedCompression() throws InvalidDataException { + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); + deflateExtension.setDeflaterLevel(Deflater.BEST_SPEED); + deflateExtension.setThreshold(0); + String str = "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text"; + byte[] message = str.getBytes(); + TextFrame frame = new TextFrame(); + frame.setPayload(ByteBuffer.wrap(message)); + + Deflater localDeflater = new Deflater(Deflater.BEST_SPEED,true); + localDeflater.setInput(ByteBuffer.wrap(message).array()); + byte[] buffer = new byte[1024]; + int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); + + deflateExtension.encodeFrame(frame); + byte[] payloadArray = frame.getPayloadData().array(); + assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); + assertTrue(frame.isRSV1()); + deflateExtension.decodeFrame(frame); + assertArrayEquals(message, frame.getPayloadData().array()); + } + + @Test + public void testDecodeFrameBestCompression() throws InvalidDataException { + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); + deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); + deflateExtension.setThreshold(0); + String str = "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text"; + byte[] message = str.getBytes(); + TextFrame frame = new TextFrame(); + frame.setPayload(ByteBuffer.wrap(message)); + + Deflater localDeflater = new Deflater(Deflater.BEST_COMPRESSION,true); + localDeflater.setInput(ByteBuffer.wrap(message).array()); + byte[] buffer = new byte[1024]; + int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); + + deflateExtension.encodeFrame(frame); + byte[] payloadArray = frame.getPayloadData().array(); + assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); + assertTrue(frame.isRSV1()); + deflateExtension.decodeFrame(frame); + assertArrayEquals(message, frame.getPayloadData().array()); + } + + @Test + public void testDecodeFrameSwitchCompression() throws InvalidDataException { + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); + deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); + deflateExtension.setThreshold(0); + String str = "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text" + + "This is a highly compressable text"; + byte[] message = str.getBytes(); + TextFrame frame = new TextFrame(); + frame.setPayload(ByteBuffer.wrap(message)); + + Deflater localDeflater = new Deflater(Deflater.BEST_COMPRESSION,true); + localDeflater.setInput(ByteBuffer.wrap(message).array()); + byte[] buffer = new byte[1024]; + int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); + + // Change the deflater level after the creation and switch to a new deflater level + // Compression strategy should be applied instantly since we call .deflate manually + deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); + deflateExtension.encodeFrame(frame); + byte[] payloadArray = frame.getPayloadData().array(); + assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); + assertTrue(frame.isRSV1()); + deflateExtension.decodeFrame(frame); + assertArrayEquals(message, frame.getPayloadData().array()); + } + @Test public void testEncodeFrame() { PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); From dfca00b6ea25e0628aecdd014125c4b8c04599e5 Mon Sep 17 00:00:00 2001 From: marci4 Date: Tue, 22 Oct 2024 23:09:18 +0200 Subject: [PATCH 5/5] Add Constructor with custom compression level --- .../PerMessageDeflateExtension.java | 42 ++++++------ .../PerMessageDeflateExtensionTest.java | 64 +++---------------- 2 files changed, 34 insertions(+), 72 deletions(-) diff --git a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java index b90ddce3b..9eb16ca15 100644 --- a/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java +++ b/src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java @@ -13,13 +13,11 @@ import org.java_websocket.extensions.CompressionExtension; import org.java_websocket.extensions.ExtensionRequestData; import org.java_websocket.extensions.IExtension; -import org.java_websocket.framing.BinaryFrame; import org.java_websocket.framing.CloseFrame; import org.java_websocket.framing.ContinuousFrame; import org.java_websocket.framing.DataFrame; import org.java_websocket.framing.Framedata; import org.java_websocket.framing.FramedataImpl1; -import org.java_websocket.framing.TextFrame; /** * PerMessage Deflate Extension (7. The @@ -53,28 +51,37 @@ public class PerMessageDeflateExtension extends CompressionExtension { // For WebSocketClients, this variable holds the extension parameters that client himself has requested. private Map requestedParameters = new LinkedHashMap<>(); - private int deflaterLevel = Deflater.DEFAULT_COMPRESSION; + private final int compressionLevel; - private Inflater inflater = new Inflater(true); - private Deflater deflater = new Deflater(this.deflaterLevel, true); + private final Inflater inflater; + private final Deflater deflater; /** - * Get the compression level used for the compressor. - * @return the compression level (0-9) + * Constructor for the PerMessage Deflate Extension (7. Thepermessage-deflate" Extension) + * + * Uses {@link java.util.zip.Deflater#DEFAULT_COMPRESSION} as the compression level for the {@link java.util.zip.Deflater#Deflater(int)} + */ + public PerMessageDeflateExtension() { + this(Deflater.DEFAULT_COMPRESSION); + } + + /** + * Constructor for the PerMessage Deflate Extension (7. Thepermessage-deflate" Extension) + * + * @param compressionLevel The compression level passed to the {@link java.util.zip.Deflater#Deflater(int)} */ - public int getDeflaterLevel() { - return this.deflaterLevel; + public PerMessageDeflateExtension(int compressionLevel) { + this.compressionLevel = compressionLevel; + this.deflater = new Deflater(this.compressionLevel, true); + this.inflater = new Inflater(true); } /** - * Set the compression level used for the compressor. - * @param level the compression level (0-9) + * Get the compression level used for the compressor. + * @return the compression level */ - public void setDeflaterLevel(int level) { - this.deflater.setLevel(level); - this.deflaterLevel = level; - //If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation. - this.deflater.deflate(new byte[0]); + public int getCompressionLevel() { + return this.compressionLevel; } /** @@ -334,11 +341,10 @@ public String getProvidedExtensionAsServer() { @Override public IExtension copyInstance() { - PerMessageDeflateExtension clone = new PerMessageDeflateExtension(); + PerMessageDeflateExtension clone = new PerMessageDeflateExtension(this.getCompressionLevel()); clone.setThreshold(this.getThreshold()); clone.setClientNoContextTakeover(this.isClientNoContextTakeover()); clone.setServerNoContextTakeover(this.isServerNoContextTakeover()); - clone.setDeflaterLevel(this.getDeflaterLevel()); return clone; } diff --git a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java index 9093ea483..a4e2c3661 100644 --- a/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java +++ b/src/test/java/org/java_websocket/extensions/PerMessageDeflateExtensionTest.java @@ -1,6 +1,5 @@ package org.java_websocket.extensions; -import static java.util.zip.GZIPInputStream.GZIP_MAGIC; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -10,10 +9,9 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.zip.Deflater; -import java.util.zip.Inflater; + import org.java_websocket.exceptions.InvalidDataException; import org.java_websocket.extensions.permessage_deflate.PerMessageDeflateExtension; -import org.java_websocket.framing.BinaryFrame; import org.java_websocket.framing.ContinuousFrame; import org.java_websocket.framing.TextFrame; import org.junit.Test; @@ -55,8 +53,7 @@ public void testDecodeFrameIfRSVIsNotSet() throws InvalidDataException { @Test public void testDecodeFrameNoCompression() throws InvalidDataException { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(Deflater.NO_COMPRESSION); deflateExtension.setThreshold(0); String str = "This is a highly compressable text" + "This is a highly compressable text" @@ -76,8 +73,7 @@ public void testDecodeFrameNoCompression() throws InvalidDataException { @Test public void testDecodeFrameBestSpeedCompression() throws InvalidDataException { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - deflateExtension.setDeflaterLevel(Deflater.BEST_SPEED); + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(Deflater.BEST_SPEED); deflateExtension.setThreshold(0); String str = "This is a highly compressable text" + "This is a highly compressable text" @@ -103,8 +99,7 @@ public void testDecodeFrameBestSpeedCompression() throws InvalidDataException { @Test public void testDecodeFrameBestCompression() throws InvalidDataException { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(Deflater.BEST_COMPRESSION); deflateExtension.setThreshold(0); String str = "This is a highly compressable text" + "This is a highly compressable text" @@ -128,35 +123,6 @@ public void testDecodeFrameBestCompression() throws InvalidDataException { assertArrayEquals(message, frame.getPayloadData().array()); } - @Test - public void testDecodeFrameSwitchCompression() throws InvalidDataException { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); - deflateExtension.setThreshold(0); - String str = "This is a highly compressable text" - + "This is a highly compressable text" - + "This is a highly compressable text" - + "This is a highly compressable text" - + "This is a highly compressable text"; - byte[] message = str.getBytes(); - TextFrame frame = new TextFrame(); - frame.setPayload(ByteBuffer.wrap(message)); - - Deflater localDeflater = new Deflater(Deflater.BEST_COMPRESSION,true); - localDeflater.setInput(ByteBuffer.wrap(message).array()); - byte[] buffer = new byte[1024]; - int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); - - // Change the deflater level after the creation and switch to a new deflater level - // Compression strategy should be applied instantly since we call .deflate manually - deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); - deflateExtension.encodeFrame(frame); - byte[] payloadArray = frame.getPayloadData().array(); - assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); - assertTrue(frame.isRSV1()); - deflateExtension.decodeFrame(frame); - assertArrayEquals(message, frame.getPayloadData().array()); - } @Test public void testEncodeFrame() { @@ -304,41 +270,31 @@ public void testCopyInstance() { assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); - assertEquals(deflateExtension.getDeflaterLevel(), newDeflateExtension.getDeflaterLevel()); + assertEquals(deflateExtension.getCompressionLevel(), newDeflateExtension.getCompressionLevel()); - deflateExtension = new PerMessageDeflateExtension(); + deflateExtension = new PerMessageDeflateExtension(Deflater.BEST_COMPRESSION); deflateExtension.setThreshold(512); deflateExtension.setServerNoContextTakeover(false); deflateExtension.setClientNoContextTakeover(true); - deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); - assertEquals(deflateExtension.getDeflaterLevel(), newDeflateExtension.getDeflaterLevel()); + assertEquals(deflateExtension.getCompressionLevel(), newDeflateExtension.getCompressionLevel()); - deflateExtension = new PerMessageDeflateExtension(); + deflateExtension = new PerMessageDeflateExtension(Deflater.NO_COMPRESSION); deflateExtension.setThreshold(64); deflateExtension.setServerNoContextTakeover(true); deflateExtension.setClientNoContextTakeover(false); - deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance(); assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold()); assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover()); assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover()); - assertEquals(deflateExtension.getDeflaterLevel(), newDeflateExtension.getDeflaterLevel()); - } - - @Test - public void testDeflaterLevel() { - PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); - assertEquals(Deflater.DEFAULT_COMPRESSION, deflateExtension.getDeflaterLevel()); - deflateExtension.setDeflaterLevel(Deflater.BEST_SPEED); - assertEquals(Deflater.BEST_SPEED, deflateExtension.getDeflaterLevel()); + assertEquals(deflateExtension.getCompressionLevel(), newDeflateExtension.getCompressionLevel()); } @Test @@ -347,6 +303,6 @@ public void testDefaults() { assertFalse(deflateExtension.isClientNoContextTakeover()); assertTrue(deflateExtension.isServerNoContextTakeover()); assertEquals(1024, deflateExtension.getThreshold()); - assertEquals(Deflater.DEFAULT_COMPRESSION, deflateExtension.getDeflaterLevel()); + assertEquals(Deflater.DEFAULT_COMPRESSION, deflateExtension.getCompressionLevel()); } }