package org.apache.poi.integration;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.PrintStream;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.examples.xssf.eventusermodel.XLSX2CSV;
+import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.xssf.XSSFTestDataSamples;
assertTrue(output.contains(",\"hello, xssf\",,\"hello, xssf\""), "Had: " + output);
}
+ @Test
+ public void testInvalidSampleFile() throws Exception {
+ final UnsynchronizedByteArrayOutputStream outputBytes = UnsynchronizedByteArrayOutputStream.builder().get();
+ PrintStream out = new PrintStream(outputBytes, true, StandardCharsets.UTF_8.name());
+
+ // The package open is instantaneous, as it should be.
+ try (OPCPackage p = OPCPackage.open(XSSFTestDataSamples.getSampleFile("clusterfuzz-testcase-minimized-XLSX2CSVFuzzer-5025401116950528.xlsx").getAbsolutePath(), PackageAccess.READ)) {
+ XLSX2CSV xlsx2csv = new XLSX2CSV(p, out, -1);
+ assertThrows(POIXMLException.class,
+ xlsx2csv::process);
+ }
+
+ String errorOutput = errorBytes.toString(StandardCharsets.UTF_8);
+ assertEquals("", errorOutput);
+
+ String output = outputBytes.toString(StandardCharsets.UTF_8);
+ assertEquals("", output, "Had: " + output);
+ }
+
@Test
public void testMinColumns() throws Exception {
final UnsynchronizedByteArrayOutputStream outputBytes = UnsynchronizedByteArrayOutputStream.builder().get();
package org.apache.poi.xssf.eventusermodel;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.InputStream;
* Tests for {@link org.apache.poi.xssf.eventusermodel.XSSFReader}
*/
public final class TestReadOnlySharedStringsTable {
- private static POIDataSamples _ssTests = POIDataSamples.getSpreadSheetInstance();
+ private static final POIDataSamples _ssTests = POIDataSamples.getSpreadSheetInstance();
@Test
void testParse() throws Exception {
List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
assertEquals(1, parts.size());
- SharedStringsTable stbl = new SharedStringsTable(parts.get(0));
- ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
- ReadOnlySharedStringsTable rtbl2;
- try (InputStream stream = parts.get(0).getInputStream()){
- rtbl2 = new ReadOnlySharedStringsTable(stream);
- }
-
- assertEquals(stbl.getCount(), rtbl.getCount());
- assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
- assertEquals(stbl.getUniqueCount(), rtbl2.getUniqueCount());
-
- assertEquals(stbl.getCount(), stbl.getUniqueCount());
- assertEquals(rtbl.getCount(), rtbl.getUniqueCount());
- assertEquals(rtbl.getCount(), rtbl2.getUniqueCount());
- for (int i = 0; i < stbl.getUniqueCount(); i++) {
- RichTextString i1 = stbl.getItemAt(i);
- assertEquals(i1.getString(), rtbl.getItemAt(i).getString());
- assertEquals(i1.getString(), rtbl2.getItemAt(i).getString());
+ try (SharedStringsTable stbl = new SharedStringsTable(parts.get(0))) {
+ ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
+ ReadOnlySharedStringsTable rtbl2;
+ try (InputStream stream = parts.get(0).getInputStream()) {
+ rtbl2 = new ReadOnlySharedStringsTable(stream);
+ }
+
+ assertEquals(stbl.getCount(), rtbl.getCount());
+ assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
+ assertEquals(stbl.getUniqueCount(), rtbl2.getUniqueCount());
+
+ assertEquals(stbl.getCount(), stbl.getUniqueCount());
+ assertEquals(rtbl.getCount(), rtbl.getUniqueCount());
+ assertEquals(rtbl.getCount(), rtbl2.getUniqueCount());
+ for (int i = 0; i < stbl.getUniqueCount(); i++) {
+ RichTextString i1 = stbl.getItemAt(i);
+ assertEquals(i1.getString(), rtbl.getItemAt(i).getString());
+ assertEquals(i1.getString(), rtbl2.getItemAt(i).getString());
+ }
}
}
}
List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
assertEquals(1, parts.size());
- SharedStringsTable stbl = new SharedStringsTable(parts.get(0));
- ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
- ReadOnlySharedStringsTable rtbl2;
- try (InputStream stream = parts.get(0).getInputStream()) {
- rtbl2 = new ReadOnlySharedStringsTable(stream);
- }
-
- assertEquals(stbl.getCount(), rtbl.getCount());
- assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
- assertEquals(stbl.getUniqueCount(), rtbl2.getUniqueCount());
- for (int i = 0; i < stbl.getUniqueCount(); i++) {
- RichTextString i1 = stbl.getItemAt(i);
- assertEquals(i1.getString(), rtbl.getItemAt(i).getString());
- assertEquals(i1.getString(), rtbl2.getItemAt(i).getString());
+ try (SharedStringsTable stbl = new SharedStringsTable(parts.get(0))) {
+ ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
+ ReadOnlySharedStringsTable rtbl2;
+ try (InputStream stream = parts.get(0).getInputStream()) {
+ rtbl2 = new ReadOnlySharedStringsTable(stream);
+ }
+
+ assertEquals(stbl.getCount(), rtbl.getCount());
+ assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
+ assertEquals(stbl.getUniqueCount(), rtbl2.getUniqueCount());
+ for (int i = 0; i < stbl.getUniqueCount(); i++) {
+ RichTextString i1 = stbl.getItemAt(i);
+ assertEquals(i1.getString(), rtbl.getItemAt(i).getString());
+ assertEquals(i1.getString(), rtbl2.getItemAt(i).getString());
+ }
}
}
}
}
}
+ @Test
+ void testNullPointerException() throws Exception {
+ try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("clusterfuzz-testcase-minimized-XLSX2CSVFuzzer-5025401116950528.xlsx"))) {
+ assertEmptySST(pkg);
+ }
+
+ try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("clusterfuzz-testcase-minimized-XLSX2CSVFuzzer-5025401116950528.xlsx"))) {
+ List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
+ assertEquals(1, parts.size());
+
+ //noinspection resource
+ assertThrows(IOException.class,
+ () -> new SharedStringsTable(parts.get(0)));
+ }
+ }
+
private void assertEmptySST(OPCPackage pkg) throws IOException, SAXException {
ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg);
assertEquals(0, sst.getCount());