|
3 | 3 | import org.junit.jupiter.api.Test; |
4 | 4 | import org.junit.jupiter.params.ParameterizedTest; |
5 | 5 | import org.junit.jupiter.params.provider.ValueSource; |
| 6 | +import software.coley.lljzip.format.ZipPatterns; |
| 7 | +import software.coley.lljzip.format.model.LocalFileHeader; |
6 | 8 | import software.coley.lljzip.format.model.ZipArchive; |
| 9 | +import software.coley.lljzip.format.read.NaiveLocalFileZipReader; |
| 10 | +import software.coley.lljzip.format.read.SimpleZipPartAllocator; |
7 | 11 | import software.coley.lljzip.format.transform.IdentityZipPartMapper; |
8 | 12 | import software.coley.lljzip.format.transform.JvmClassDirectoryMapper; |
9 | 13 | import software.coley.lljzip.format.write.ZipOutputStreamZipWriter; |
| 14 | +import software.coley.lljzip.util.MemorySegmentUtil; |
| 15 | +import software.coley.lljzip.util.data.MemorySegmentData; |
10 | 16 |
|
| 17 | +import javax.annotation.Nonnull; |
11 | 18 | import java.io.ByteArrayInputStream; |
12 | 19 | import java.io.ByteArrayOutputStream; |
13 | 20 | import java.io.IOException; |
| 21 | +import java.lang.foreign.MemorySegment; |
14 | 22 | import java.nio.file.Files; |
15 | 23 | import java.nio.file.Path; |
16 | 24 | import java.nio.file.Paths; |
17 | 25 | import java.util.zip.ZipEntry; |
18 | 26 | import java.util.zip.ZipInputStream; |
19 | 27 |
|
20 | 28 | import static org.junit.jupiter.api.Assertions.*; |
| 29 | +import static software.coley.lljzip.util.MemorySegmentUtil.readLongSlice; |
21 | 30 |
|
22 | 31 | /** |
23 | 32 | * Tests for patching the <i>"trick"</i> jars, and making them compatible with standard Java ZIP apis. |
@@ -85,6 +94,33 @@ public void testTrickJarPatched(String name) { |
85 | 94 | } |
86 | 95 | } |
87 | 96 |
|
| 97 | + @Test |
| 98 | + @SuppressWarnings("resource") |
| 99 | + public void testNaiveWithForwardScanningData() { |
| 100 | + NaiveLocalFileZipReader strategy = new NaiveLocalFileZipReader(new SimpleZipPartAllocator() { |
| 101 | + @Nonnull |
| 102 | + @Override |
| 103 | + public LocalFileHeader newLocalFileHeader() { |
| 104 | + return new LocalFileHeader() { |
| 105 | + @Nonnull |
| 106 | + @Override |
| 107 | + protected MemorySegmentData readFileData(@Nonnull MemorySegment data, long headerOffset) { |
| 108 | + long localOffset = MIN_FIXED_SIZE + getFileNameLength() + getExtraFieldLength(); |
| 109 | + long nextStart = MemorySegmentUtil.indexOfQuad(data, headerOffset + localOffset, ZipPatterns.LOCAL_FILE_HEADER_QUAD); |
| 110 | + long fileDataLength = nextStart > headerOffset ? |
| 111 | + nextStart - (headerOffset + localOffset) : |
| 112 | + data.byteSize() - (headerOffset + localOffset); |
| 113 | + return MemorySegmentData.of(readLongSlice(data, headerOffset, localOffset, fileDataLength)); |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + Path path = Paths.get("src/test/resources/resource-pack-trick-data-ioobe-no-end.zip"); |
| 120 | + ZipArchive zip = assertDoesNotThrow(() -> ZipIO.read(path, strategy)); |
| 121 | + assertNotNull(zip.getLocalFileByName("pack.mcmeta"), "IOOBE not patched"); |
| 122 | + } |
| 123 | + |
88 | 124 | @Test |
89 | 125 | @SuppressWarnings("resource") |
90 | 126 | public void testTrailingSlashTransform() { |
|
0 commit comments