|
| 1 | +package software.coley.lljzip; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import software.coley.lljzip.format.model.ZipArchive; |
| 5 | +import software.coley.lljzip.format.read.AdaptingZipReader; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.zip.CRC32; |
| 10 | +import java.util.zip.ZipEntry; |
| 11 | +import java.util.zip.ZipOutputStream; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for {@link AdaptingZipReader} targeting buffer loop regressions. |
| 17 | + * |
| 18 | + * @author TheWakz |
| 19 | + */ |
| 20 | +public class AdaptingZipReaderTests { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testReadArchiveExceedingBufferSize() throws IOException { |
| 24 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 25 | + try (ZipOutputStream zos = new ZipOutputStream(baos)) { |
| 26 | + byte[] dummyData = new byte[20 * 1024]; |
| 27 | + |
| 28 | + ZipEntry entry = new ZipEntry("large_entry.txt"); |
| 29 | + entry.setMethod(ZipEntry.STORED); |
| 30 | + entry.setSize(dummyData.length); |
| 31 | + entry.setCompressedSize(dummyData.length); |
| 32 | + |
| 33 | + CRC32 crc = new CRC32(); |
| 34 | + crc.update(dummyData); |
| 35 | + entry.setCrc(crc.getValue()); |
| 36 | + |
| 37 | + zos.putNextEntry(entry); |
| 38 | + zos.write(dummyData); |
| 39 | + zos.closeEntry(); |
| 40 | + } |
| 41 | + byte[] zipBytes = baos.toByteArray(); |
| 42 | + |
| 43 | + assertTrue(zipBytes.length > 16384, "Test archive size must exceed 16KB"); |
| 44 | + |
| 45 | + ZipArchive archive = assertDoesNotThrow(() -> ZipIO.read(zipBytes, new AdaptingZipReader()), |
| 46 | + "Failed to read with read(adapting) when size exceeds buffer"); |
| 47 | + |
| 48 | + assertNotNull(archive, "Archive is null"); |
| 49 | + assertNotNull(archive.getLocalFileByName("large_entry.txt"), |
| 50 | + "Missing 'large_entry.txt' file due to truncation"); |
| 51 | + } |
| 52 | +} |
0 commit comments