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
commit 10076e058b1f3d67558c304afa78f92da02f5dc6 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu May 5 14:02:56 2022 -0400 Fix broken tests. --- .../commons/compress/archivers/tar/TarUtils.java | 15 ++- .../archivers/tar/TarArchiveEntryTest.java | 8 +- .../archivers/tar/TarArchiveOutputStreamTest.java | 6 +- .../compress/archivers/tar/TarUtilsTest.java | 150 +++++++-------------- 4 files changed, 60 insertions(+), 119 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java index dcbc48c9..369eabcb 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java @@ -18,9 +18,16 @@ */ package org.apache.commons.compress.archivers.tar; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.commons.compress.archivers.tar.TarConstants.CHKSUMLEN; +import static org.apache.commons.compress.archivers.tar.TarConstants.CHKSUM_OFFSET; +import static org.apache.commons.compress.archivers.tar.TarConstants.SPARSE_NUMBYTES_LEN; +import static org.apache.commons.compress.archivers.tar.TarConstants.SPARSE_OFFSET_LEN; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -34,12 +41,6 @@ import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.compress.utils.IOUtils; -import static java.nio.charset.StandardCharsets.*; -import static org.apache.commons.compress.archivers.tar.TarConstants.CHKSUMLEN; -import static org.apache.commons.compress.archivers.tar.TarConstants.CHKSUM_OFFSET; -import static org.apache.commons.compress.archivers.tar.TarConstants.SPARSE_NUMBYTES_LEN; -import static org.apache.commons.compress.archivers.tar.TarConstants.SPARSE_OFFSET_LEN; - /** * This class provides static utility methods to work with byte streams. * @@ -854,7 +855,7 @@ public class TarUtils { try { return parseFromPAX01SparseHeaders(sparseMap); } catch (IOException ex) { - throw new RuntimeException(ex.getMessage(), ex); + throw new UncheckedIOException(ex.getMessage(), ex); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java index 8c1b474a..5af21f68 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java @@ -25,9 +25,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; -import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -262,12 +262,12 @@ public class TarArchiveEntryTest implements TarConstants { "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000" + "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000" + "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000").getBytes(UTF_8); - new TarArchiveEntry(entryContent, ZipEncodingHelper.getZipEncoding(CharsetNames.ISO_8859_1), false, -1); + assertThrows(IOException.class, () -> new TarArchiveEntry(entryContent, ZipEncodingHelper.getZipEncoding(CharsetNames.ISO_8859_1), false, -1)); } @Test public void negativeOffsetInSetterNotAllowed() { - new TarArchiveEntry("test").setDataOffset(-1); + assertThrows(IllegalArgumentException.class, () -> new TarArchiveEntry("test").setDataOffset(-1)); } @Test @@ -305,7 +305,7 @@ public class TarArchiveEntryTest implements TarConstants { final TarArchiveEntry te = new TarArchiveEntry("test"); te.setSparseHeaders(Arrays.asList(new TarArchiveStructSparse(200, 2))); te.fillStarSparseData(Collections.singletonMap("SCHILY.realsize", "201")); - te.getOrderedSparseHeaders(); + assertThrows(IOException.class, () -> te.getOrderedSparseHeaders()); } @Test diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java index 5ffb2869..e2fc01b2 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java @@ -23,9 +23,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -103,7 +103,7 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase { t.setSize(0100000000000L); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final TarArchiveOutputStream tos = new TarArchiveOutputStream(bos); - tos.putArchiveEntry(t); + assertThrows(IllegalArgumentException.class, () -> tos.putArchiveEntry(t)); } @Test @@ -790,7 +790,7 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase { + "01234567890123456789012345678901234567890123456789"; final TarArchiveEntry t = new TarArchiveEntry(n); final TarArchiveOutputStream tos = new TarArchiveOutputStream(new ByteArrayOutputStream(), "ASCII"); - tos.putArchiveEntry(t); + assertThrows(IllegalArgumentException.class, () -> tos.putArchiveEntry(t)); } private static byte[] createTarArchiveContainingOneDirectory(final String fname, 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 2830d0db..869f8176 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 @@ -18,9 +18,18 @@ package org.apache.commons.compress.archivers.tar; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.commons.compress.AbstractTestCase.getFile; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; @@ -32,24 +41,10 @@ import org.apache.commons.compress.archivers.zip.ZipEncoding; import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; import org.apache.commons.compress.utils.ByteUtils; import org.apache.commons.compress.utils.CharsetNames; -import org.apache.commons.compress.utils.IOUtils; -import org.junit.Rule; import org.junit.jupiter.api.Test; -import org.junit.rules.ExpectedException; - -import static java.nio.charset.StandardCharsets.*; -import static org.apache.commons.compress.AbstractTestCase.getFile; -import static org.hamcrest.CoreMatchers.startsWith; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; public class TarUtilsTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testName(){ byte [] buff = new byte[20]; @@ -459,27 +454,23 @@ public class TarUtilsTest { @Test public void testParseTarWithSpecialPaxHeaders() throws IOException { - thrown.expect(IOException.class); - try (InputStream in = Files.newInputStream(getFile("COMPRESS-530.tar").toPath()); + try (InputStream in = Files.newInputStream(getFile("COMPRESS-530.tar").toPath()); TarArchiveInputStream archive = new TarArchiveInputStream(in)) { - archive.getNextEntry(); - IOUtils.toByteArray(archive); + assertThrows(IOException.class, () -> archive.getNextEntry()); + // IOUtils.toByteArray(archive); } } @Test public void readPaxHeaderWithoutTrailingNewline() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Failed to read Paxheader")); - TarUtils.parsePaxHeaders( + assertThrows(IOException.class, () -> TarUtils.parsePaxHeaders( new ByteArrayInputStream("30 atime=1321711775.9720594634".getBytes(UTF_8)), - null, Collections.emptyMap()); + null, Collections.emptyMap())); } @Test public void readPax00SparseHeader() throws Exception { - final String header = "23 GNU.sparse.offset=0\n" - + "26 GNU.sparse.numbytes=10\n"; + final String header = "23 GNU.sparse.offset=0\n26 GNU.sparse.numbytes=10\n"; final List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>(); TarUtils.parsePaxHeaders( new ByteArrayInputStream(header.getBytes(UTF_8)), @@ -491,8 +482,7 @@ public class TarUtilsTest { @Test public void readPax00SparseHeaderMakesNumbytesOptional() throws Exception { - final String header = "23 GNU.sparse.offset=0\n" - + "24 GNU.sparse.offset=10\n"; + final String header = "23 GNU.sparse.offset=0\n24 GNU.sparse.offset=10\n"; final List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>(); TarUtils.parsePaxHeaders( new ByteArrayInputStream(header.getBytes(UTF_8)), @@ -506,46 +496,34 @@ public class TarUtilsTest { @Test public void readPax00SparseHeaderRejectsNonNumericOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Failed to read Paxheader")); - final String header = "23 GNU.sparse.offset=a\n" - + "26 GNU.sparse.numbytes=10\n"; - TarUtils.parsePaxHeaders( + final String header = "23 GNU.sparse.offset=a\n26 GNU.sparse.numbytes=10\n"; + assertThrows(IOException.class, () -> TarUtils.parsePaxHeaders( new ByteArrayInputStream(header.getBytes(UTF_8)), - null, Collections.emptyMap()); + null, Collections.emptyMap())); } @Test public void readPax00SparseHeaderRejectsNonNumericNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Failed to read Paxheader")); - final String header = "23 GNU.sparse.offset=0\n" - + "26 GNU.sparse.numbytes=1a\n"; - TarUtils.parsePaxHeaders( + final String header = "23 GNU.sparse.offset=0\n26 GNU.sparse.numbytes=1a\n"; + assertThrows(IOException.class, () -> TarUtils.parsePaxHeaders( new ByteArrayInputStream(header.getBytes(UTF_8)), - null, Collections.emptyMap()); + null, Collections.emptyMap())); } @Test public void readPax00SparseHeaderRejectsNegativeOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Failed to read Paxheader")); - final String header = "24 GNU.sparse.offset=-1\n" - + "26 GNU.sparse.numbytes=10\n"; - TarUtils.parsePaxHeaders( + final String header = "24 GNU.sparse.offset=-1\n26 GNU.sparse.numbytes=10\n"; + assertThrows(IOException.class, () -> TarUtils.parsePaxHeaders( new ByteArrayInputStream(header.getBytes(UTF_8)), - null, Collections.emptyMap()); + null, Collections.emptyMap())); } @Test public void readPax00SparseHeaderRejectsNegativeNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Failed to read Paxheader")); - final String header = "23 GNU.sparse.offset=0\n" - + "26 GNU.sparse.numbytes=-1\n"; - TarUtils.parsePaxHeaders( + final String header = "23 GNU.sparse.offset=0\n26 GNU.sparse.numbytes=-1\n"; + assertThrows(IOException.class, () -> TarUtils.parsePaxHeaders( new ByteArrayInputStream(header.getBytes(UTF_8)), - null, Collections.emptyMap()); + null, Collections.emptyMap())); } @Test @@ -573,42 +551,34 @@ public class TarUtilsTest { @Test public void readSparseStructsRejectsNonNumericOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); final byte[] header = "0000000000x 00000000007 ".getBytes(); - TarUtils.readSparseStructs(header, 0, 1); + assertThrows(IOException.class, () -> TarUtils.readSparseStructs(header, 0, 1)); } @Test public void readSparseStructsRejectsNegativeOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); final byte[] header = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, }; - TarUtils.readSparseStructs(header, 0, 1); + assertThrows(IOException.class, () -> TarUtils.readSparseStructs(header, 0, 1)); } @Test public void readSparseStructsRejectsNonNumericNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); final byte[] header = "00000000000 0000000000x ".getBytes(); - TarUtils.readSparseStructs(header, 0, 1); + assertThrows(IOException.class, () -> TarUtils.readSparseStructs(header, 0, 1)); } @Test public void readSparseStructsRejectsNegativeNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); final byte[] header = { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, }; - TarUtils.readSparseStructs(header, 0, 1); + assertThrows(IOException.class, () -> TarUtils.readSparseStructs(header, 0, 1)); } @Test @@ -626,50 +596,34 @@ public class TarUtilsTest { @Test public void parseFromPAX01SparseHeadersRejectsOddNumberOfEntries() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); final String map = "0,10,20,0,20"; - TarUtils.parseFromPAX01SparseHeaders(map); + assertThrows(IOException.class, () -> TarUtils.parseFromPAX01SparseHeaders(map)); } @Test public void parseFromPAX01SparseHeadersRejectsNonNumericOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); - final String map = "0,10,20,0,2a,5"; - TarUtils.parseFromPAX01SparseHeaders(map); + assertThrows(IOException.class, () -> TarUtils.parseFromPAX01SparseHeaders("0,10,20,0,2a,5")); } @Test public void parseFromPAX01SparseHeadersRejectsNonNumericNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); - final String map = "0,10,20,0,20,b"; - TarUtils.parseFromPAX01SparseHeaders(map); + assertThrows(IOException.class, () -> TarUtils.parseFromPAX01SparseHeaders("0,10,20,0,20,b")); } @Test public void parseFromPAX01SparseHeadersRejectsNegativeOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); - final String map = "0,10,20,0,-2,5"; - TarUtils.parseFromPAX01SparseHeaders(map); + assertThrows(IOException.class, () -> TarUtils.parseFromPAX01SparseHeaders("0,10,20,0,-2,5")); } @Test public void parseFromPAX01SparseHeadersRejectsNegativeNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); - final String map = "0,10,20,0,20,-5"; - TarUtils.parseFromPAX01SparseHeaders(map); + assertThrows(IOException.class, () -> TarUtils.parseFromPAX01SparseHeaders("0,10,20,0,20,-5")); } @Test public void parsePAX01SparseHeadersRejectsOddNumberOfEntries() { - thrown.expect(RuntimeException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive")); final String map = "0,10,20,0,20"; - TarUtils.parsePAX01SparseHeaders(map); + assertThrows(UncheckedIOException.class, () -> TarUtils.parsePAX01SparseHeaders(map)); } @Test @@ -691,21 +645,17 @@ public class TarUtilsTest { @Test public void parsePAX1XSparseHeadersRejectsIncompleteLastLine() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Unexpected EOF")); final byte[] header = ("1\n" + "0\n" + "20") .getBytes(); try (ByteArrayInputStream in = new ByteArrayInputStream(header)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } } @Test public void parsePAX1XSparseHeadersRejectsNonNumericNumberOfEntries() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive.")); final byte[] header = ("x\n" + "0\n" + "20\n") @@ -713,14 +663,12 @@ public class TarUtilsTest { final byte[] block = new byte[512]; System.arraycopy(header, 0, block, 0, header.length); try (ByteArrayInputStream in = new ByteArrayInputStream(block)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } } @Test public void parsePAX1XSparseHeadersRejectsNonNumericOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive.")); final byte[] header = ("1\n" + "x\n" + "20\n") @@ -728,14 +676,12 @@ public class TarUtilsTest { final byte[] block = new byte[512]; System.arraycopy(header, 0, block, 0, header.length); try (ByteArrayInputStream in = new ByteArrayInputStream(block)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } } @Test public void parsePAX1XSparseHeadersRejectsNonNumericNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive.")); final byte[] header = ("1\n" + "0\n" + "2x\n") @@ -743,13 +689,11 @@ public class TarUtilsTest { final byte[] block = new byte[512]; System.arraycopy(header, 0, block, 0, header.length); try (ByteArrayInputStream in = new ByteArrayInputStream(block)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } } @Test public void parsePAX1XSparseHeadersRejectsNegativeNumberOfEntries() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive.")); final byte[] header = ("111111111111111111111111111111111111111111111111111111111111111\n" + "0\n" + "20\n") @@ -757,14 +701,12 @@ public class TarUtilsTest { final byte[] block = new byte[512]; System.arraycopy(header, 0, block, 0, header.length); try (ByteArrayInputStream in = new ByteArrayInputStream(block)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } } @Test public void parsePAX1XSparseHeadersRejectsNegativeOffset() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive.")); final byte[] header = ("1\n" + "111111111111111111111111111111111111111111111111111111111111111\n" + "20\n") @@ -772,14 +714,12 @@ public class TarUtilsTest { final byte[] block = new byte[512]; System.arraycopy(header, 0, block, 0, header.length); try (ByteArrayInputStream in = new ByteArrayInputStream(block)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } } @Test public void parsePAX1XSparseHeadersRejectsNegativeNumbytes() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage(startsWith("Corrupted TAR archive.")); final byte[] header = ("1\n" + "0\n" + "111111111111111111111111111111111111111111111111111111111111111\n") @@ -787,7 +727,7 @@ public class TarUtilsTest { final byte[] block = new byte[512]; System.arraycopy(header, 0, block, 0, header.length); try (ByteArrayInputStream in = new ByteArrayInputStream(block)) { - TarUtils.parsePAX1XSparseHeaders(in, 512); + assertThrows(IOException.class, () -> TarUtils.parsePAX1XSparseHeaders(in, 512)); } }