Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 36 additions & 55 deletions src/test/net/jpountz/lz4/LZ4FrameIOStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -37,7 +37,6 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
Expand Down Expand Up @@ -99,18 +98,13 @@ public LZ4FrameIOStreamTest(int testSize) {

@Before
public void setUp() throws IOException {
final int fill = 0xDEADBEEF;
tmpFile = Files.createTempFile("lz4ioTest", ".dat").toFile();
final Random rnd = new Random(5378L);
int sizeRemaining = testSize;
try (OutputStream os = Files.newOutputStream(tmpFile.toPath())) {
while (sizeRemaining > 0) {
final byte[] buff = new byte[Math.min(sizeRemaining, 1 << 10)];
final IntBuffer intBuffer = ByteBuffer.wrap(buff).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
rnd.nextBytes(buff);
while (intBuffer.hasRemaining()) {
intBuffer.put(fill);
}
Comment on lines -109 to -113
Copy link
Author

@Marcono1234 Marcono1234 Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the PR / commit description, it looks like this code here effectively discarded what rnd.nextBytes(buff); did because that modified buff directly but intBuffer was unaffected (I am also not sure what the original intention here was).

So it seems the previous behavior was that it created a file filled only with the value of fill (0xDEADBEEF), which is probably not that useful for compression / decompression testing?

(Unless I am overlooking something here?)

os.write(buff);
sizeRemaining -= buff.length;
}
Expand All @@ -125,6 +119,28 @@ public void tearDown() {
}
}

/**
* Whether the native LZ4 CLI is available; can be used for comparing this library with the expected native LZ4 behavior
*/
private static boolean hasLz4CLI = false;

@BeforeClass
public static void checkLz4CLI() {
try {
ProcessBuilder checkBuilder = new ProcessBuilder().command("lz4", "-V").redirectErrorStream(true);
Process checkProcess = checkBuilder.start();
hasLz4CLI = checkProcess.waitFor() == 0;
} catch (IOException | InterruptedException e) {
// lz4 CLI not available or failed to execute; treat as unavailable to allow test skip
hasLz4CLI = false;
}

// Check if this is running in CI (env CI=true), see https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables
if (!hasLz4CLI && "true".equals(System.getenv("CI"))) {
Assert.fail("LZ4 CLI is not available, but should be for CI run");
}
Comment on lines +138 to +141
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an example actions run which shows that this works as desired:
https://github.com/Marcono1234/lz4-java/actions/runs/20029422374/job/57434525412#step:6:917

(for that run I had intentionally removed the LZ4 CLI from the Windows build)

}

private void fillBuffer(final byte[] buffer, final InputStream is) throws IOException {
int offset = 0;
while (offset < buffer.length) {
Expand Down Expand Up @@ -197,9 +213,10 @@ public void testOutputSimple() throws IOException {
copy(is, os);
}
}
final FileChannel channel = FileChannel.open(lz4File.toPath());
final ByteBuffer buffer = ByteBuffer.allocate(1024).order(ByteOrder.LITTLE_ENDIAN);
channel.read(buffer);
try (FileChannel channel = FileChannel.open(lz4File.toPath())) {
channel.read(buffer);
}
buffer.rewind();
Assert.assertEquals(LZ4FrameOutputStream.MAGIC, buffer.getInt());
final BitSet b = BitSet.valueOf(new byte[]{buffer.get()});
Expand Down Expand Up @@ -296,12 +313,7 @@ public void testSkippableOnly() throws IOException {
}
// Extra one byte at the tail
try (InputStream is = new LZ4FrameInputStream(new SequenceInputStream(new FileInputStream(lz4File), new ByteArrayInputStream(new byte[1])))) {
is.read();
Assert.assertFalse(true);
} catch (IOException ex) {
// OK
} catch (Exception ex) {
Assert.assertFalse(true);
Assert.assertThrows(IOException.class, is::read);
}
} finally {
lz4File.delete();
Expand Down Expand Up @@ -399,12 +411,7 @@ public void testInputOutputMultipleFrames() throws IOException {
}
}
try (LZ4FrameInputStream is = new LZ4FrameInputStream(new FileInputStream(lz4File))) {
try {
is.getExpectedContentSize();
Assert.assertFalse(true);
} catch (UnsupportedOperationException e) {
// OK
}
Assert.assertThrows(UnsupportedOperationException.class, is::getExpectedContentSize);
Assert.assertFalse(is.isExpectedContentSizeDefined());
validateStreamEquals(is, tmpFile);
validateStreamEquals(is, tmpFile);
Expand All @@ -427,7 +434,7 @@ public void testInputOutputMultipleFrames() throws IOException {

@Test
public void testNativeCompressIfAvailable() throws IOException, InterruptedException {
Assume.assumeTrue(hasNativeLz4CLI());
Assume.assumeTrue(hasLz4CLI);
nativeCompress();
nativeCompress("--no-frame-crc");
}
Expand All @@ -444,7 +451,7 @@ private void nativeCompress(String... args) throws IOException, InterruptedExcep
}
cmd.add(tmpFile.getAbsolutePath());
cmd.add(lz4File.getAbsolutePath());
builder.command(cmd.toArray(new String[cmd.size()]));
builder.command(cmd.toArray(new String[0]));
builder.inheritIO();
Process process = builder.start();
int retval = process.waitFor();
Expand Down Expand Up @@ -479,20 +486,9 @@ public void testUncompressableEnd() throws IOException {
}
}

private static boolean hasNativeLz4CLI() throws IOException, InterruptedException {
try {
ProcessBuilder checkBuilder = new ProcessBuilder().command("lz4", "-V").redirectErrorStream(true);
Process checkProcess = checkBuilder.start();
return checkProcess.waitFor() == 0;
} catch (IOException | InterruptedException e) {
// lz4 CLI not available or failed to execute; treat as unavailable to allow test skip
return false;
}
}

@Test
public void testNativeDecompresIfAvailable() throws IOException, InterruptedException {
Assume.assumeTrue(hasNativeLz4CLI());
public void testNativeDecompressIfAvailable() throws IOException, InterruptedException {
Assume.assumeTrue(hasLz4CLI);
final File lz4File = Files.createTempFile("lz4test", ".lz4").toFile();
final File unCompressedFile = Files.createTempFile("lz4raw", ".dat").toFile();
unCompressedFile.delete();
Expand Down Expand Up @@ -527,26 +523,16 @@ public void testNativeDecompresIfAvailable() throws IOException, InterruptedExce
}

@Test
public void testEmptyLZ4Input() {
public void testEmptyLZ4Input() throws IOException {
try (InputStream is = new LZ4FrameInputStream(new ByteArrayInputStream(new byte[0]))) {
is.read();
Assert.assertFalse(true);
} catch (IOException ex) {
// OK
} catch (Exception ex) {
Assert.assertFalse(true);
Assert.assertThrows(IOException.class, is::read);
}
}

@Test
public void testPrematureMagicNb() throws IOException {
try (InputStream is = new LZ4FrameInputStream(new ByteArrayInputStream(new byte[1]))) {
is.read();
Assert.assertFalse(true);
} catch (IOException ex) {
// OK
} catch (Exception ex) {
Assert.assertFalse(true);
Assert.assertThrows(IOException.class, is::read);
}

final File lz4File = Files.createTempFile("lz4test", ".lz4").toFile();
Expand All @@ -559,12 +545,7 @@ public void testPrematureMagicNb() throws IOException {
// Extra one byte at the tail
try (InputStream is = new LZ4FrameInputStream(new SequenceInputStream(new FileInputStream(lz4File), new ByteArrayInputStream(new byte[1])))) {
validateStreamEquals(is, tmpFile);
is.read();
Assert.assertFalse(true);
} catch (IOException ex) {
// OK
} catch (Exception ex) {
Assert.assertFalse(true);
Assert.assertThrows(IOException.class, is::read);
}
} finally {
lz4File.delete();
Expand Down