Skip to content

Commit e01e7da

Browse files
committed
feat: add fuzz testing for Excel reading and improve error handling in XlsxSaxAnalyser
1 parent 5981de5 commit e01e7da

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

fastexcel/src/test/java/cn/idev/excel/test/core/exception/XlsxSaxAnalyserReadOpcPackageTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import cn.idev.excel.support.ExcelTypeEnum;
1111
import java.io.ByteArrayInputStream;
1212
import java.io.File;
13+
import java.io.FileOutputStream;
1314
import java.nio.charset.StandardCharsets;
1415
import java.nio.file.Files;
16+
import java.util.zip.ZipOutputStream;
1517
import org.junit.jupiter.api.Test;
1618

1719
/**
@@ -61,4 +63,37 @@ void decryptedStreamProvided_throwsExcelCommonException() {
6163
ExcelCommonException ex = assertThrows(ExcelCommonException.class, () -> new XlsxSaxAnalyser(ctx, decrypted));
6264
assertTrue(ex.getMessage() != null && ex.getMessage().toLowerCase().contains("invalid ooxml/zip format"));
6365
}
66+
67+
@Test
68+
void emptyZipFile_throwsExcelCommonException() throws Exception {
69+
File tmp = File.createTempFile("empty-zip", ".xlsx");
70+
try (FileOutputStream fos = new FileOutputStream(tmp);
71+
ZipOutputStream zos = new ZipOutputStream(fos)) {
72+
// write an empty zip with no entries
73+
}
74+
try {
75+
ReadWorkbook rw = new ReadWorkbook();
76+
rw.setFile(tmp);
77+
XlsxReadContext ctx = new DefaultXlsxReadContext(rw, ExcelTypeEnum.XLSX);
78+
ExcelCommonException ex = assertThrows(ExcelCommonException.class, () -> new XlsxSaxAnalyser(ctx, null));
79+
assertTrue(ex.getMessage() != null && ex.getMessage().toLowerCase().contains("invalid ooxml/zip format"));
80+
} finally {
81+
try {
82+
Files.deleteIfExists(tmp.toPath());
83+
} catch (Exception ignore) {
84+
}
85+
}
86+
}
87+
88+
@Test
89+
void inputStream_nonMandatoryUseTempFileBranch_throwsExcelCommonException() {
90+
ReadWorkbook rw = new ReadWorkbook();
91+
rw.setInputStream(new ByteArrayInputStream("not-xlsx".getBytes(StandardCharsets.UTF_8)));
92+
// mandatoryUseInputStream unset or false -> will write to temp file and then open
93+
rw.setMandatoryUseInputStream(false);
94+
XlsxReadContext ctx = new DefaultXlsxReadContext(rw, ExcelTypeEnum.XLSX);
95+
96+
ExcelCommonException ex = assertThrows(ExcelCommonException.class, () -> new XlsxSaxAnalyser(ctx, null));
97+
assertTrue(ex.getMessage() != null && ex.getMessage().toLowerCase().contains("invalid ooxml/zip format"));
98+
}
6499
}

0 commit comments

Comments
 (0)