garydgregory commented on code in PR #435:
URL: https://github.com/apache/commons-compress/pull/435#discussion_r1389403183
##########
src/test/java/org/apache/commons/compress/compressors/z/ZCompressorInputStreamTest.java:
##########
@@ -68,4 +73,32 @@ public void
testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOExcept
}
}
+ @Test
+ public void testInvalidMaxCodeSize() throws IOException {
+ Set<Integer> invalidValues = new TreeSet<>();
+ invalidValues.addAll(IntStream.range(Byte.MIN_VALUE,
-120).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(-97,
-88).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(-65,
-56).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(-33,
-24).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(-1,
8).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(31,
40).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(63,
72).boxed().collect(Collectors.toSet()));
+ invalidValues.addAll(IntStream.range(95,
104).boxed().collect(Collectors.toSet()));
+ invalidValues.add(127);
+
+ final File input = getFile("bla.tar.Z");
+ try (final InputStream contentStream =
Files.newInputStream(input.toPath())) {
+ final byte[] content = IOUtils.toByteArray(contentStream);
+
+ for (int value : invalidValues) {
+ content[2] = (byte) value;
+
+ // Test that invalid values always throw an IOException
+ assertThrows(IOException.class, () ->
+ new ZCompressorInputStream(new
ByteArrayInputStream(content), 1024 * 1024)
+ );
+ }
+ }
Review Comment:
Simplify and clarify: You can read the file test fixture's content in one
call. You can just stream over the IntStreams.
```
final byte[] bytes =
Files.readAllBytes(AbstractTest.getPath("bla.tar.Z"));
// @formatter:off
final IntStream[] invalid = {
IntStream.range(Byte.MIN_VALUE, -120),
IntStream.range(-97, -88),
IntStream.range(-65, -56),
IntStream.range(-33, -24),
IntStream.range(-1, 8),
IntStream.range(31, 40),
IntStream.range(63, 72),
IntStream.range(95, 104),
IntStream.range(127, 127)
};
// @formatter:on
Stream.of(invalid).forEach(ints -> ints.forEach(i -> {
bytes[2] = (byte) i;
assertThrows(IllegalArgumentException.class, () -> new
ZCompressorInputStream(new ByteArrayInputStream(bytes), 1024 * 1024),
() -> "value=" + i);
}));
```
Also, the exception should be thrown in the initializeTables method where
the check is already performed and can be expanded.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]