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 a51d4f47c [COMPRESS-674] Validate TarArchiveEntry checksums (#500) a51d4f47c is described below commit a51d4f47c862d89a6d30fe03988af4b28a27a657 Author: Gren Elliot <g...@fatkudu.net> AuthorDate: Thu Mar 21 00:59:30 2024 +0000 [COMPRESS-674] Validate TarArchiveEntry checksums (#500) * [COMPRESS-674] validate TarArchiveEntry checksums For test file, thought it had 1 (corrupt) directory entry only but still thought it was a tar file * [COMPRESS-674] couple more tar tests Make sure identifies a couple of real tar files as such --------- Co-authored-by: Gren Elliot <gell...@mimecast.com> --- .../compress/archivers/ArchiveStreamFactory.java | 2 +- .../archivers/ArchiveStreamFactoryTest.java | 20 ++++++++++++++++++++ src/test/resources/dirWith1TxtFile.tar | Bin 0 -> 4608 bytes src/test/resources/emptyDir.tar | Bin 0 -> 2560 bytes src/test/resources/utf16-text.txt | Bin 0 -> 512 bytes 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java index 9c13316aa..99ea2acf0 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java +++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java @@ -282,7 +282,7 @@ public class ArchiveStreamFactory implements ArchiveStreamProvider { TarArchiveEntry entry = inputStream.getNextEntry(); // try to find the first non-directory entry within the first 10 entries. int count = 0; - while (entry != null && entry.isDirectory() && count++ < TAR_TEST_ENTRY_COUNT) { + while (entry != null && entry.isDirectory() && entry.isCheckSumOK() && count++ < TAR_TEST_ENTRY_COUNT) { entry = inputStream.getNextEntry(); } if (entry != null && entry.isCheckSumOK() && !entry.isDirectory() && entry.getSize() > 0 || count > 0) { diff --git a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java index f2a172273..65f879364 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java @@ -282,6 +282,26 @@ public class ArchiveStreamFactoryTest extends AbstractTest { assertEquals("IOException while reading signature.", e3.getMessage()); } + /** + * Test case for <a href="https://issues.apache.org/jira/browse/COMPRESS-674" >COMPRESS-674</a>. + */ + @Test + void testUtf16TextIsNotTAR() { + final ArchiveException archiveException = assertThrows(ArchiveException.class, + () -> detect("utf16-text.txt")); + assertEquals("No Archiver found for the stream signature", archiveException.getMessage()); + } + + @Test + void testTarContainingEmptyDirIsTAR() throws IOException, ArchiveException { + assertEquals(ArchiveStreamFactory.TAR, detect("emptyDir.tar")); + } + + @Test + void testTarContainingDirWith1TxtFileIsTAR() throws IOException, ArchiveException { + assertEquals(ArchiveStreamFactory.TAR, detect("dirWith1TxtFile.tar")); + } + /** * Test case for <a href="https://issues.apache.org/jira/browse/COMPRESS-267" >COMPRESS-267</a>. */ diff --git a/src/test/resources/dirWith1TxtFile.tar b/src/test/resources/dirWith1TxtFile.tar new file mode 100644 index 000000000..e38d8e595 Binary files /dev/null and b/src/test/resources/dirWith1TxtFile.tar differ diff --git a/src/test/resources/emptyDir.tar b/src/test/resources/emptyDir.tar new file mode 100644 index 000000000..86c0bc961 Binary files /dev/null and b/src/test/resources/emptyDir.tar differ diff --git a/src/test/resources/utf16-text.txt b/src/test/resources/utf16-text.txt new file mode 100644 index 000000000..c45e48e74 Binary files /dev/null and b/src/test/resources/utf16-text.txt differ