This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new aa69573e0 Add tests for lh5 decompression (#807)
aa69573e0 is described below
commit aa69573e0c6e0da37df224295b360ab185dad273
Author: Fredrik Kjellberg <[email protected]>
AuthorDate: Sat Aug 22 21:35:45 2026 +0200
Add tests for lh5 decompression (#807)
---
.../LhStaticHuffmanCompressorInputStreamTest.java | 31 ++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git
a/src/test/java/org/apache/commons/compress/archivers/lha/LhStaticHuffmanCompressorInputStreamTest.java
b/src/test/java/org/apache/commons/compress/archivers/lha/LhStaticHuffmanCompressorInputStreamTest.java
index 6bb3b11bf..419d05cac 100644
---
a/src/test/java/org/apache/commons/compress/archivers/lha/LhStaticHuffmanCompressorInputStreamTest.java
+++
b/src/test/java/org/apache/commons/compress/archivers/lha/LhStaticHuffmanCompressorInputStreamTest.java
@@ -20,11 +20,14 @@
package org.apache.commons.compress.archivers.lha;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.ByteOrder;
+import java.nio.charset.StandardCharsets;
import org.apache.commons.compress.AbstractTest;
import org.apache.commons.compress.compressors.CompressorException;
@@ -41,6 +44,34 @@ private LhStaticHuffmanCompressorInputStream
createLh5CompressorInputStream(fina
return
LhStaticHuffmanCompressorInputStream.lh5CompressorInputStream(new
ByteArrayInputStream(AbstractTest.toByteArray(data)));
}
+ @Test
+ void testDecompressFailureForTreeWithTooManyLeafNodes() {
+ // This compressed data contains an invalid Huffman tree with too many
leaf nodes,
+ // which should throw an exception when trying to decompress it.
+ final int[] compressedData = new int[] {
+ 0x00, 0x04, 0x28, 0x25, 0x30, 0x70, 0xB7, 0x95, 0xD0, 0x21, 0xB0
+ };
+
+ final CompressorException e = assertThrows(CompressorException.class,
() -> IOUtils.toByteArray(createLh5CompressorInputStream(compressedData)),
+ "Expected CompressorException due to too many leaf nodes in
the Huffman tree");
+ assertEquals("Tree contains too many leaf nodes for depth 1",
e.getMessage());
+ }
+
+ @Test
+ void testDecompressSingleCodeLengthHuffmanTree() throws IOException {
+ // This compressed data contains a single code length in one of the
Huffman trees, which is handled
+ // differently in LHx compressors compared to other Huffman based
compressors.
+ final int[] compressedData = new int[] {
+ 0x00, 0x09, 0x38, 0x0a, 0x2a, 0x1d, 0x0b, 0x70, 0x12, 0xc3, 0x03,
0xe0, 0x53, 0x97, 0x7e
+ };
+
+ try (InputStream is = createLh5CompressorInputStream(compressedData)) {
+ final byte[] data = IOUtils.toByteArray(is);
+ assertEquals(24, data.length);
+ assertEquals("ABCDEFGHABCDEFGHABCDEFGH", new String(data,
StandardCharsets.US_ASCII));
+ }
+ }
+
@Test
void testInputStreamStatistics() throws IOException {
final int[] compressedData = { 0x00, 0x05, 0x28, 0x04, 0x4b, 0xfc,
0x16, 0xed, 0x37, 0x00, 0x43, 0x00 };