1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.compressors.deflate64;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.ByteArrayInputStream;
24 import java.util.Arrays;
25
26 import org.junit.jupiter.api.Test;
27
28 public class HuffmanDecoderTest {
29
30 @Test
31 public void testDecodeFixedHuffmanBlockWithMemoryLookup() throws Exception {
32 final byte[] data = {
33
34 0b11111111111111111111111111110011,
35 0b00000000000000000000000001001000,
36 0b11111111111111111111111111001101,
37 0b11111111111111111111111111001001,
38 0b11111111111111111111111111001001,
39 0b00000000000000000000000001010111,
40 0b00000000000000000000000000001000,
41 0b11111111111111111111111111001111,
42 0b00000000000000000000000000101111,
43 0b11111111111111111111111111001010,
44 0b00000000000000000000000001001001,
45 0b11111111111111111111111111100001,
46 0b00000000000000000000000000100010,
47 0b11111111111111111111111110000110,
48 0b00000000000000000000000000001101,
49 0b11111111111111111111111111111000
50 };
51 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
52 final byte[] result = new byte[100];
53 final int len = decoder.decode(result);
54 assertEquals(48, len);
55 assertEquals("Hello World\nHello World\nHello World\nHello World\n", new String(result, 0, len));
56 }
57 }
58
59 @Test
60 public void testDecodeFixedHuffmanBlockWithMemoryLookupInExactBuffer() throws Exception {
61 final byte[] data = {
62
63 0b11111111111111111111111111110011,
64 0b00000000000000000000000001001000,
65 0b11111111111111111111111111001101,
66 0b11111111111111111111111111001001,
67 0b11111111111111111111111111001001,
68 0b00000000000000000000000001010111,
69 0b00000000000000000000000000001000,
70 0b11111111111111111111111111001111,
71 0b00000000000000000000000000101111,
72 0b11111111111111111111111111001010,
73 0b00000000000000000000000001001001,
74 0b11111111111111111111111111100001,
75 0b00000000000000000000000000100010,
76 0b11111111111111111111111110000110,
77 0b00000000000000000000000000001101,
78 0b11111111111111111111111111111000
79 };
80 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
81 final byte[] result = new byte[48];
82 int len;
83 len = decoder.decode(result);
84 assertEquals(48, len);
85 assertEquals("Hello World\nHello World\nHello World\nHello World\n", new String(result, 0, len));
86 len = decoder.decode(result);
87 assertEquals(-1, len);
88 }
89 }
90
91 @Test
92 public void testDecodeFixedHuffmanBlockWithMemoryLookupInSmallBuffer() throws Exception {
93 final byte[] data = {
94
95 0b11111111111111111111111111110011,
96 0b00000000000000000000000001001000,
97 0b11111111111111111111111111001101,
98 0b11111111111111111111111111001001,
99 0b11111111111111111111111111001001,
100 0b00000000000000000000000001010111,
101 0b00000000000000000000000000001000,
102 0b11111111111111111111111111001111,
103 0b00000000000000000000000000101111,
104 0b11111111111111111111111111001010,
105 0b00000000000000000000000001001001,
106 0b11111111111111111111111111100001,
107 0b00000000000000000000000000100010,
108 0b11111111111111111111111110000110,
109 0b00000000000000000000000000001101,
110 0b11111111111111111111111111111000
111 };
112
113 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
114 final byte[] result = new byte[30];
115 int len;
116 len = decoder.decode(result);
117 assertEquals(30, len);
118 assertEquals("Hello World\nHello World\nHello ", new String(result, 0, len));
119 len = decoder.decode(result);
120 assertEquals(18, len);
121 assertEquals("World\nHello World\n", new String(result, 0, len));
122 }
123 }
124
125 @Test
126 public void testDecodeSimpleFixedHuffmanBlock() throws Exception {
127 final byte[] data = {
128
129 0b11111111111111111111111111110011,
130 0b00000000000000000000000001001000,
131 0b11111111111111111111111111001101,
132 0b11111111111111111111111111001001,
133 0b11111111111111111111111111001001,
134 0b00000000000000000000000001010111,
135 0b00000000000000000000000000001000,
136 0b11111111111111111111111111001111,
137 0b00000000000000000000000000101111,
138 0b11111111111111111111111111001010,
139 0b00000000000000000000000001001001,
140 0b00000000000000000000000000000001,
141 0b11111111111111111111111111111100
142 };
143 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
144 final byte[] result = new byte[100];
145 final int len = decoder.decode(result);
146 assertEquals(11, len);
147 assertEquals("Hello World", new String(result, 0, len));
148 }
149 }
150
151 @Test
152 public void testDecodeSimpleFixedHuffmanBlockToSmallBuffer() throws Exception {
153 final byte[] data = {
154
155 0b11111111111111111111111111110011,
156 0b00000000000000000000000001001000,
157 0b11111111111111111111111111001101,
158 0b11111111111111111111111111001001,
159 0b11111111111111111111111111001001,
160 0b00000000000000000000000001010111,
161 0b00000000000000000000000000001000,
162 0b11111111111111111111111111001111,
163 0b00000000000000000000000000101111,
164 0b11111111111111111111111111001010,
165 0b00000000000000000000000001001001,
166 0b00000000000000000000000000000001,
167 0b11111111111111111111111111111100
168 };
169 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
170 final byte[] result = new byte[10];
171 int len;
172 len = decoder.decode(result);
173 assertEquals(10, len);
174 assertEquals("Hello Worl", new String(result, 0, len));
175 len = decoder.decode(result);
176 assertEquals(1, len);
177 assertEquals("d", new String(result, 0, len));
178 }
179 }
180
181 @Test
182 public void testDecodeUncompressedBlock() throws Exception {
183 final byte[] data = { 0b1,
184 11, 0, -12, -1,
185 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
186 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
187 final byte[] result = new byte[100];
188 final int len = decoder.decode(result);
189 assertEquals(11, len);
190 assertEquals("Hello World", new String(result, 0, len));
191 }
192 }
193
194 @Test
195 public void testDecodeUncompressedBlockWithInvalidLenNLenValue() throws Exception {
196 final byte[] data = { 0b1,
197 11, 0, -12, -2,
198 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
199 try (HuffmanDecoder decoder = new HuffmanDecoder(new ByteArrayInputStream(data))) {
200 final byte[] result = new byte[100];
201 final IllegalStateException e = assertThrows(IllegalStateException.class, () -> {
202 final int len = decoder.decode(result);
203 fail("Should have failed but returned " + len + " entries: " + Arrays.toString(Arrays.copyOf(result, len)));
204 });
205 assertEquals("Illegal LEN / NLEN values", e.getMessage());
206 }
207 }
208 }