This is an automated email from the ASF dual-hosted git repository.
ggregory 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 2f161ee3b Test Lister with non-corrupt tar files
2f161ee3b is described below
commit 2f161ee3b90f364beb0db273ac88f8928cacda19
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Feb 1 19:40:41 2024 -0500
Test Lister with non-corrupt tar files
---
.../apache/commons/compress/archivers/Lister.java | 100 +++++++++++++--------
.../commons/compress/archivers/ListerTest.java | 31 ++++---
.../apache/commons/compress/archivers/TarTest.java | 8 +-
.../archivers/tar/TarArchiveInputStreamTest.java | 16 ++--
.../compress/archivers/tar/TarFileTest.java | 10 +--
.../compress/archivers/tar/TarUtilsTest.java | 2 +-
.../{COMPRESS-178.tar => COMPRESS-178-fail.tar} | Bin
.../{COMPRESS-279.tar => COMPRESS-279-fail.tar} | Bin
.../{COMPRESS-529.tar => COMPRESS-529-fail.tar} | Bin
.../{COMPRESS-530.tar => COMPRESS-530-fail.tar} | Bin
... => COMPRESS-544_truncated_in_content-fail.tar} | Bin
... => COMPRESS-544_truncated_in_padding-fail.tar} | Bin
.../{COMPRESS-553.tar => COMPRESS-553-fail.tar} | Bin
.../{COMPRESS-554.tar => COMPRESS-554-fail.tar} | Bin
.../{COMPRESS-569.tar => COMPRESS-569-fail.tar} | Bin
15 files changed, 95 insertions(+), 72 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/Lister.java
b/src/main/java/org/apache/commons/compress/archivers/Lister.java
index f987856e0..f5d87855d 100644
--- a/src/main/java/org/apache/commons/compress/archivers/Lister.java
+++ b/src/main/java/org/apache/commons/compress/archivers/Lister.java
@@ -62,7 +62,58 @@ public final class Lister {
}
}
- private static void list(final Path file, final String... args) throws
ArchiveException, IOException {
+ /**
+ * Runs this class from the command line.
+ * <p>
+ * The name of the archive must be given as a command line argument.
+ * </p>
+ * <p>
+ * The optional second argument defines the archive type, in case the
format is not recognized.
+ * </p>
+ *
+ * @param args name of the archive and optional argument archive type.
+ * @throws ArchiveException Archiver related Exception.
+ * @throws IOException an I/O exception.
+ */
+ public static void main(final String... args) throws ArchiveException,
IOException {
+ if (args == null || args.length == 0) {
+ usage();
+ return;
+ }
+ new Lister(false, args).go();
+ }
+
+ private static void usage() {
+ System.err.println("Parameters: archive-name [archive-type]\n");
+ System.err.println("The magic archive-type 'zipfile' prefers ZipFile
over ZipArchiveInputStream");
+ System.err.println("The magic archive-type 'tarfile' prefers TarFile
over TarArchiveInputStream");
+ }
+
+ private final boolean quiet;
+
+ private final String[] args;
+
+ /**
+ * Constructs a new instance.
+ *
+ * @deprecated No replacement.
+ */
+ @Deprecated
+ public Lister() {
+ this(false, "");
+ }
+
+ Lister(final boolean quiet, final String... args) {
+ this.quiet = quiet;
+ this.args = args.clone();
+ Objects.requireNonNull(args[0], "args[0]");
+ }
+
+ void go() throws ArchiveException, IOException {
+ list(Paths.get(args[0]), args);
+ }
+
+ private void list(final Path file, final String... args) throws
ArchiveException, IOException {
println("Analyzing " + file);
if (!Files.isRegularFile(file)) {
System.err.println(file + " doesn't exist or is a directory");
@@ -84,7 +135,7 @@ public final class Lister {
}
}
- private static void list7z(final Path file) throws IOException {
+ private void list7z(final Path file) throws IOException {
try (SevenZFile sevenZFile = SevenZFile.builder().setPath(file).get())
{
println("Created " + sevenZFile);
ArchiveEntry entry;
@@ -94,7 +145,7 @@ public final class Lister {
}
}
- private static void listStream(final Path file, final String[] args)
throws ArchiveException, IOException {
+ private void listStream(final Path file, final String[] args) throws
ArchiveException, IOException {
try (InputStream inputStream = new
BufferedInputStream(Files.newInputStream(file));
ArchiveInputStream<?> archiveInputStream =
createArchiveInputStream(args, inputStream)) {
println("Created " + archiveInputStream.toString());
@@ -105,14 +156,14 @@ public final class Lister {
}
}
- private static void listZipUsingTarFile(final Path file) throws
IOException {
+ private void listZipUsingTarFile(final Path file) throws IOException {
try (TarFile tarFile = new TarFile(file)) {
println("Created " + tarFile);
- tarFile.getEntries().forEach(Lister::println);
+ tarFile.getEntries().forEach(this::println);
}
}
- private static void listZipUsingZipFile(final Path file) throws
IOException {
+ private void listZipUsingZipFile(final Path file) throws IOException {
try (ZipFile zipFile = ZipFile.builder().setPath(file).get()) {
println("Created " + zipFile);
for (final Enumeration<ZipArchiveEntry> en = zipFile.getEntries();
en.hasMoreElements();) {
@@ -121,41 +172,14 @@ public final class Lister {
}
}
- /**
- * Runs this class from the command line.
- * <p>
- * The name of the archive must be given as a command line argument.
- * </p>
- * <p>
- * The optional second argument defines the archive type, in case the
format is not recognized.
- * </p>
- *
- * @param args name of the archive and optional argument archive type.
- * @throws ArchiveException Archiver related Exception.
- * @throws IOException an I/O exception.
- */
- public static void main(final String... args) throws ArchiveException,
IOException {
- if (args == null || args.length == 0) {
- usage();
- return;
- }
- Objects.requireNonNull(args[0], "args[0]");
- final Path file = Paths.get(args[0]);
- list(file, args);
- }
-
- private static void println(final ArchiveEntry entry) {
+ private void println(final ArchiveEntry entry) {
println(entry.getName());
}
- private static void println(final String line) {
- System.out.println(line);
- }
-
- private static void usage() {
- println("Parameters: archive-name [archive-type]\n");
- println("The magic archive-type 'zipfile' prefers ZipFile over
ZipArchiveInputStream");
- println("The magic archive-type 'tarfile' prefers TarFile over
TarArchiveInputStream");
+ private void println(final String line) {
+ if (!quiet) {
+ System.out.println(line);
+ }
}
}
diff --git
a/src/test/java/org/apache/commons/compress/archivers/ListerTest.java
b/src/test/java/org/apache/commons/compress/archivers/ListerTest.java
index c179ab2ba..b4c87b9bb 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ListerTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ListerTest.java
@@ -20,25 +20,24 @@
package org.apache.commons.compress.archivers;
import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.stream.Stream;
-import org.junit.jupiter.api.Test;
+import org.apache.commons.io.file.PathUtils;
+import org.apache.commons.io.filefilter.RegexFileFilter;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
public class ListerTest {
- @Test
- public void testCompress654() throws ArchiveException, IOException {
-// Analysing ruff-aarch64-apple-darwin.tar
-// Created
org.apache.commons.compress.archivers.tar.TarArchiveInputStream@17f052a3
-// ruff
-// Exception in thread "main" java.io.IOException: Truncated TAR archive
-// at
org.apache.commons.compress.archivers.tar.TarArchiveInputStream.read(TarArchiveInputStream.java:694)
-// at
org.apache.commons.compress.utils.IOUtils.readFully(IOUtils.java:244)
-// at org.apache.commons.compress.utils.IOUtils.skip(IOUtils.java:355)
-// at
org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextTarEntry(TarArchiveInputStream.java:451)
-// at
org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextEntry(TarArchiveInputStream.java:426)
-// at
org.apache.commons.compress.archivers.tar.TarArchiveInputStream.getNextEntry(TarArchiveInputStream.java:50)
-// at
org.apache.commons.compress.archivers.Lister.listStream(Lister.java:79)
-// at org.apache.commons.compress.archivers.Lister.main(Lister.java:133)
-
Lister.main("src/test/resources/org/apache/commons/compress/COMPRESS-654/ruff-aarch64-apple-darwin.tar");
+ public static Stream<Path> getFixtures() throws IOException {
+ return PathUtils.walk(Paths.get("src/test/resources"), new
RegexFileFilter("^(?!.*(-fail)).*\\.tar$"), 10, false);
+ }
+
+ @ParameterizedTest
+ @MethodSource("getFixtures")
+ public void testMain(final Path path) throws ArchiveException, IOException
{
+ new Lister(true, path.toString()).go();
}
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/TarTest.java
b/src/test/java/org/apache/commons/compress/archivers/TarTest.java
index 366334cba..8f9d74aad 100644
--- a/src/test/java/org/apache/commons/compress/archivers/TarTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/TarTest.java
@@ -85,7 +85,7 @@ public final class TarTest extends AbstractTest {
@Test
public void testCOMPRESS178() throws Exception {
- final File input = getFile("COMPRESS-178.tar");
+ final File input = getFile("COMPRESS-178-fail.tar");
try (InputStream is = Files.newInputStream(input.toPath());
ArchiveInputStream<?> in =
ArchiveStreamFactory.DEFAULT.createArchiveInputStream("tar", is)) {
final IOException e = assertThrows(IOException.class,
in::getNextEntry, "Expected IOException");
@@ -96,7 +96,7 @@ public final class TarTest extends AbstractTest {
@Test
public void testCOMPRESS178Lenient() throws Exception {
- final File input = getFile("COMPRESS-178.tar");
+ final File input = getFile("COMPRESS-178-fail.tar");
try (InputStream is = Files.newInputStream(input.toPath());
ArchiveInputStream<?> in = new TarArchiveInputStream(is,
true)) {
in.getNextEntry();
@@ -314,7 +314,7 @@ public final class TarTest extends AbstractTest {
@Test
public void testTarFileCOMPRESS178() throws Exception {
- final File input = getFile("COMPRESS-178.tar");
+ final File input = getFile("COMPRESS-178-fail.tar");
final IOException e = assertThrows(IOException.class, () -> {
try (TarFile tarFile = new TarFile(input)) {
// Compared to the TarArchiveInputStream all entries are read
when instantiating the tar file
@@ -326,7 +326,7 @@ public final class TarTest extends AbstractTest {
@Test
public void testTarFileCOMPRESS178Lenient() throws Exception {
- final File input = getFile("COMPRESS-178.tar");
+ final File input = getFile("COMPRESS-178-fail.tar");
try (TarFile tarFile = new TarFile(input, true)) {
// Compared to the TarArchiveInputStream all entries are read when
instantiating the tar file
}
diff --git
a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index 91f0a3390..37e4e9fed 100644
---
a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++
b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -209,7 +209,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testParseTarTruncatedInContent() throws IOException {
- try (InputStream in =
newInputStream("COMPRESS-544_truncated_in_content.tar");
+ try (InputStream in =
newInputStream("COMPRESS-544_truncated_in_content-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
getNextEntryUntilIOException(archive);
}
@@ -217,7 +217,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testParseTarTruncatedInPadding() throws IOException {
- try (InputStream in =
newInputStream("COMPRESS-544_truncated_in_padding.tar");
+ try (InputStream in =
newInputStream("COMPRESS-544_truncated_in_padding-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
getNextEntryUntilIOException(archive);
}
@@ -225,7 +225,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testParseTarWithNonNumberPaxHeaders() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-529.tar");
+ try (InputStream in = newInputStream("COMPRESS-529-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
assertThrows(IOException.class, () -> archive.getNextEntry());
}
@@ -233,7 +233,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testParseTarWithSpecialPaxHeaders() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-530.tar");
+ try (InputStream in = newInputStream("COMPRESS-530-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
assertThrows(IOException.class, () -> archive.getNextEntry());
assertThrows(IOException.class, () ->
IOUtils.toByteArray(archive));
@@ -260,7 +260,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testRejectsArchivesWithNegativeSizes() throws Exception {
- try (InputStream in = newInputStream("COMPRESS-569.tar");
+ try (InputStream in = newInputStream("COMPRESS-569-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
getNextEntryUntilIOException(archive);
}
@@ -321,7 +321,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testShouldThrowAnExceptionOnTruncatedEntries() throws
Exception {
final Path dir = createTempDirectory("COMPRESS-279");
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-279.tar")) {
+ try (TarArchiveInputStream is =
getTestStream("/COMPRESS-279-fail.tar")) {
assertThrows(IOException.class, () -> {
TarArchiveEntry entry = is.getNextTarEntry();
int count = 0;
@@ -408,7 +408,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testThrowException() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-553.tar");
+ try (InputStream in = newInputStream("COMPRESS-553-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
getNextEntryUntilIOException(archive);
}
@@ -416,7 +416,7 @@ public class TarArchiveInputStreamTest extends AbstractTest
{
@Test
public void testThrowExceptionWithNullEntry() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-554.tar");
+ try (InputStream in = newInputStream("COMPRESS-554-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
getNextEntryUntilIOException(archive);
}
diff --git
a/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
b/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
index 7b8044c1f..7cc82848e 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
@@ -198,12 +198,12 @@ public class TarFileTest extends AbstractTest {
@Test
public void testParseTarWithNonNumberPaxHeaders() {
- assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-529.tar")));
+ assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-529-fail.tar")));
}
@Test
public void testParseTarWithSpecialPaxHeaders() {
- assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-530.tar")));
+ assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-530-fail.tar")));
}
@Test
@@ -223,7 +223,7 @@ public class TarFileTest extends AbstractTest {
@Test
public void testRejectsArchivesWithNegativeSizes() throws Exception {
- assertThrows(IOException.class, () -> new
TarFile(getFile("COMPRESS-569.tar")));
+ assertThrows(IOException.class, () -> new
TarFile(getFile("COMPRESS-569-fail.tar")));
}
@Test
@@ -340,12 +340,12 @@ public class TarFileTest extends AbstractTest {
@Test
public void testThrowException() {
- assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-553.tar")));
+ assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-553-fail.tar")));
}
@Test
public void testThrowExceptionWithNullEntry() {
- assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-554.tar")));
+ assertThrows(IOException.class, () -> new
TarFile(getPath("COMPRESS-554-fail.tar")));
}
@Test
diff --git
a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
index 22547ae3c..84b8887a7 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
@@ -299,7 +299,7 @@ public class TarUtilsTest extends AbstractTest {
@Test
public void testParseTarWithSpecialPaxHeaders() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-530.tar");
+ try (InputStream in = newInputStream("COMPRESS-530-fail.tar");
TarArchiveInputStream archive = new TarArchiveInputStream(in))
{
assertThrows(IOException.class, () -> archive.getNextEntry());
// IOUtils.toByteArray(archive);
diff --git a/src/test/resources/COMPRESS-178.tar
b/src/test/resources/COMPRESS-178-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-178.tar
rename to src/test/resources/COMPRESS-178-fail.tar
diff --git a/src/test/resources/COMPRESS-279.tar
b/src/test/resources/COMPRESS-279-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-279.tar
rename to src/test/resources/COMPRESS-279-fail.tar
diff --git a/src/test/resources/COMPRESS-529.tar
b/src/test/resources/COMPRESS-529-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-529.tar
rename to src/test/resources/COMPRESS-529-fail.tar
diff --git a/src/test/resources/COMPRESS-530.tar
b/src/test/resources/COMPRESS-530-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-530.tar
rename to src/test/resources/COMPRESS-530-fail.tar
diff --git a/src/test/resources/COMPRESS-544_truncated_in_content.tar
b/src/test/resources/COMPRESS-544_truncated_in_content-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-544_truncated_in_content.tar
rename to src/test/resources/COMPRESS-544_truncated_in_content-fail.tar
diff --git a/src/test/resources/COMPRESS-544_truncated_in_padding.tar
b/src/test/resources/COMPRESS-544_truncated_in_padding-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-544_truncated_in_padding.tar
rename to src/test/resources/COMPRESS-544_truncated_in_padding-fail.tar
diff --git a/src/test/resources/COMPRESS-553.tar
b/src/test/resources/COMPRESS-553-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-553.tar
rename to src/test/resources/COMPRESS-553-fail.tar
diff --git a/src/test/resources/COMPRESS-554.tar
b/src/test/resources/COMPRESS-554-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-554.tar
rename to src/test/resources/COMPRESS-554-fail.tar
diff --git a/src/test/resources/COMPRESS-569.tar
b/src/test/resources/COMPRESS-569-fail.tar
similarity index 100%
rename from src/test/resources/COMPRESS-569.tar
rename to src/test/resources/COMPRESS-569-fail.tar