|
1 | 1 | package org.java_websocket.extensions; |
2 | 2 |
|
| 3 | +import static java.util.zip.GZIPInputStream.GZIP_MAGIC; |
3 | 4 | import static org.junit.Assert.assertArrayEquals; |
4 | 5 | import static org.junit.Assert.assertEquals; |
5 | 6 | import static org.junit.Assert.assertFalse; |
6 | 7 | import static org.junit.Assert.assertTrue; |
7 | 8 | import static org.junit.Assert.fail; |
8 | 9 |
|
9 | 10 | import java.nio.ByteBuffer; |
| 11 | +import java.util.Arrays; |
10 | 12 | import java.util.zip.Deflater; |
11 | 13 | import java.util.zip.Inflater; |
12 | 14 | import org.java_websocket.exceptions.InvalidDataException; |
@@ -51,6 +53,111 @@ public void testDecodeFrameIfRSVIsNotSet() throws InvalidDataException { |
51 | 53 | assertFalse(frame.isRSV1()); |
52 | 54 | } |
53 | 55 |
|
| 56 | + @Test |
| 57 | + public void testDecodeFrameNoCompression() throws InvalidDataException { |
| 58 | + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); |
| 59 | + deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); |
| 60 | + deflateExtension.setThreshold(0); |
| 61 | + String str = "This is a highly compressable text" |
| 62 | + + "This is a highly compressable text" |
| 63 | + + "This is a highly compressable text" |
| 64 | + + "This is a highly compressable text" |
| 65 | + + "This is a highly compressable text"; |
| 66 | + byte[] message = str.getBytes(); |
| 67 | + TextFrame frame = new TextFrame(); |
| 68 | + frame.setPayload(ByteBuffer.wrap(message)); |
| 69 | + deflateExtension.encodeFrame(frame); |
| 70 | + byte[] payloadArray = frame.getPayloadData().array(); |
| 71 | + assertArrayEquals(message, Arrays.copyOfRange(payloadArray, 5,payloadArray.length-5)); |
| 72 | + assertTrue(frame.isRSV1()); |
| 73 | + deflateExtension.decodeFrame(frame); |
| 74 | + assertArrayEquals(message, frame.getPayloadData().array()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testDecodeFrameBestSpeedCompression() throws InvalidDataException { |
| 79 | + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); |
| 80 | + deflateExtension.setDeflaterLevel(Deflater.BEST_SPEED); |
| 81 | + deflateExtension.setThreshold(0); |
| 82 | + String str = "This is a highly compressable text" |
| 83 | + + "This is a highly compressable text" |
| 84 | + + "This is a highly compressable text" |
| 85 | + + "This is a highly compressable text" |
| 86 | + + "This is a highly compressable text"; |
| 87 | + byte[] message = str.getBytes(); |
| 88 | + TextFrame frame = new TextFrame(); |
| 89 | + frame.setPayload(ByteBuffer.wrap(message)); |
| 90 | + |
| 91 | + Deflater localDeflater = new Deflater(Deflater.BEST_SPEED,true); |
| 92 | + localDeflater.setInput(ByteBuffer.wrap(message).array()); |
| 93 | + byte[] buffer = new byte[1024]; |
| 94 | + int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); |
| 95 | + |
| 96 | + deflateExtension.encodeFrame(frame); |
| 97 | + byte[] payloadArray = frame.getPayloadData().array(); |
| 98 | + assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); |
| 99 | + assertTrue(frame.isRSV1()); |
| 100 | + deflateExtension.decodeFrame(frame); |
| 101 | + assertArrayEquals(message, frame.getPayloadData().array()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testDecodeFrameBestCompression() throws InvalidDataException { |
| 106 | + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); |
| 107 | + deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); |
| 108 | + deflateExtension.setThreshold(0); |
| 109 | + String str = "This is a highly compressable text" |
| 110 | + + "This is a highly compressable text" |
| 111 | + + "This is a highly compressable text" |
| 112 | + + "This is a highly compressable text" |
| 113 | + + "This is a highly compressable text"; |
| 114 | + byte[] message = str.getBytes(); |
| 115 | + TextFrame frame = new TextFrame(); |
| 116 | + frame.setPayload(ByteBuffer.wrap(message)); |
| 117 | + |
| 118 | + Deflater localDeflater = new Deflater(Deflater.BEST_COMPRESSION,true); |
| 119 | + localDeflater.setInput(ByteBuffer.wrap(message).array()); |
| 120 | + byte[] buffer = new byte[1024]; |
| 121 | + int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); |
| 122 | + |
| 123 | + deflateExtension.encodeFrame(frame); |
| 124 | + byte[] payloadArray = frame.getPayloadData().array(); |
| 125 | + assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); |
| 126 | + assertTrue(frame.isRSV1()); |
| 127 | + deflateExtension.decodeFrame(frame); |
| 128 | + assertArrayEquals(message, frame.getPayloadData().array()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testDecodeFrameSwitchCompression() throws InvalidDataException { |
| 133 | + PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); |
| 134 | + deflateExtension.setDeflaterLevel(Deflater.NO_COMPRESSION); |
| 135 | + deflateExtension.setThreshold(0); |
| 136 | + String str = "This is a highly compressable text" |
| 137 | + + "This is a highly compressable text" |
| 138 | + + "This is a highly compressable text" |
| 139 | + + "This is a highly compressable text" |
| 140 | + + "This is a highly compressable text"; |
| 141 | + byte[] message = str.getBytes(); |
| 142 | + TextFrame frame = new TextFrame(); |
| 143 | + frame.setPayload(ByteBuffer.wrap(message)); |
| 144 | + |
| 145 | + Deflater localDeflater = new Deflater(Deflater.BEST_COMPRESSION,true); |
| 146 | + localDeflater.setInput(ByteBuffer.wrap(message).array()); |
| 147 | + byte[] buffer = new byte[1024]; |
| 148 | + int bytesCompressed = localDeflater.deflate(buffer, 0, buffer.length, Deflater.SYNC_FLUSH); |
| 149 | + |
| 150 | + // Change the deflater level after the creation and switch to a new deflater level |
| 151 | + // Compression strategy should be applied instantly since we call .deflate manually |
| 152 | + deflateExtension.setDeflaterLevel(Deflater.BEST_COMPRESSION); |
| 153 | + deflateExtension.encodeFrame(frame); |
| 154 | + byte[] payloadArray = frame.getPayloadData().array(); |
| 155 | + assertArrayEquals(Arrays.copyOfRange(buffer,0, bytesCompressed), Arrays.copyOfRange(payloadArray,0,payloadArray.length)); |
| 156 | + assertTrue(frame.isRSV1()); |
| 157 | + deflateExtension.decodeFrame(frame); |
| 158 | + assertArrayEquals(message, frame.getPayloadData().array()); |
| 159 | + } |
| 160 | + |
54 | 161 | @Test |
55 | 162 | public void testEncodeFrame() { |
56 | 163 | PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension(); |
|
0 commit comments