Skip to content

Commit

Permalink
#154 Add tests for null and empty extra data records
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Mar 6, 2020
1 parent fad2f7e commit d604203
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/test/java/net/lingala/zip4j/headers/HeaderWriterIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.lingala.zip4j.model.AESExtraDataRecord;
import net.lingala.zip4j.model.CentralDirectory;
import net.lingala.zip4j.model.DataDescriptor;
import net.lingala.zip4j.model.ExtraDataRecord;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.LocalFileHeader;
import net.lingala.zip4j.model.Zip64ExtendedInfo;
Expand Down Expand Up @@ -222,6 +223,16 @@ public void testFinalizeZipFileForNonZip64Format() throws IOException {
}
}

@Test
public void testFinalizeZipFileWithNullExtraDataWritesSuccessfully() throws IOException {
testFinalizeZipFileWhenExtraDataRecordIsNullOrEmpty(null);
}

@Test
public void testFinalizeZipFileWithEmptyExtraDataWritesSuccessfully() throws IOException {
testFinalizeZipFileWhenExtraDataRecordIsNullOrEmpty(new byte[0]);
}

@Test
public void testFinalizeZipFileForZip64Format() throws IOException {
ZipModel zipModel = createZipModel(10, COMPRESSED_SIZE_ZIP64, UNCOMPRESSED_SIZE_ZIP64);
Expand Down Expand Up @@ -418,7 +429,29 @@ public void testUpdateLocalFileHeaderForZip64() throws IOException {
verifyEntrySizesIsMaxValueInLFHWhenZip64Format(headersFile);
}

public void createAndUpdateLocalFileHeader(File headersFile, long compressedSize, long uncompressedSize, long crc)
private void testFinalizeZipFileWhenExtraDataRecordIsNullOrEmpty(byte[] extraDataRecord) throws IOException {
ZipModel zipModel = createZipModel(10);
File headersFile = temporaryFolder.newFile();
addExtraDataRecordToFirstFileHeader(zipModel, extraDataRecord);

try(OutputStream outputStream = new FileOutputStream(headersFile)) {
headerWriter.finalizeZipFile(zipModel, outputStream, InternalZipConstants.CHARSET_UTF_8);
}

try(RandomAccessFile randomAccessFile = new RandomAccessFile(headersFile, RandomAccessFileMode.READ.getValue())) {
ZipModel readZipModel = headerReader.readAllHeaders(randomAccessFile, null);
verifyZipModel(readZipModel, 10);

for (int i = 0; i < zipModel.getCentralDirectory().getFileHeaders().size(); i++) {
FileHeader fileHeader = readZipModel.getCentralDirectory().getFileHeaders().get(i);
assertThat(fileHeader.getZip64ExtendedInfo()).isNull();
assertThat(fileHeader.getAesExtraDataRecord()).isNull();
assertThat(fileHeader.getExtraFieldLength()).isEqualTo(i == 0 ? 4 : 0);
}
}
}

private void createAndUpdateLocalFileHeader(File headersFile, long compressedSize, long uncompressedSize, long crc)
throws IOException {
ZipModel zipModel = createZipModel(3);
LocalFileHeader localFileHeaderToWrite = createLocalFileHeader("LFH", compressedSize, uncompressedSize, false);
Expand Down Expand Up @@ -660,4 +693,19 @@ private List<FileHeader> createFileHeaders(int numberOfEntriesInCentralDirectory
return fileHeaders;
}

private void addExtraDataRecordToFirstFileHeader(ZipModel zipModel, byte[] data) {
ExtraDataRecord extraDataRecord = new ExtraDataRecord();
extraDataRecord.setHeader(12345);
extraDataRecord.setSizeOfData(data == null ? 0 : data.length);
extraDataRecord.setData(data);

FileHeader firstFileHeader = zipModel.getCentralDirectory().getFileHeaders().get(0);

if (firstFileHeader.getExtraDataRecords() == null) {
firstFileHeader.setExtraDataRecords(new ArrayList<>());
}

firstFileHeader.getExtraDataRecords().add(extraDataRecord);
}

}

0 comments on commit d604203

Please sign in to comment.