forked from lz4/lz4-java
-
Notifications
You must be signed in to change notification settings - Fork 20
Improve LZ4FrameIOStreamTest test
#23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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); | ||
| } | ||
| os.write(buff); | ||
| sizeRemaining -= buff.length; | ||
| } | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: (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) { | ||
|
|
@@ -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()}); | ||
|
|
@@ -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(); | ||
|
|
@@ -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); | ||
|
|
@@ -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"); | ||
| } | ||
|
|
@@ -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(); | ||
|
|
@@ -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(); | ||
|
|
@@ -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(); | ||
|
|
@@ -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(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 modifiedbuffdirectly butintBufferwas 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?)