From: Dominik Stadler Date: Wed, 26 Dec 2018 13:28:32 +0000 (+0000) Subject: Add some simple coverage of HSLF-Dev-Tools X-Git-Tag: REL_4_1_0~156 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c218c3cbc4027681ab014a495ccad593557037f5;p=poi.git Add some simple coverage of HSLF-Dev-Tools git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849765 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java index 5372d5bc9c..646b316b5c 100644 --- a/src/java/org/apache/poi/util/IOUtils.java +++ b/src/java/org/apache/poi/util/IOUtils.java @@ -562,7 +562,7 @@ public final class IOUtils { public static byte[] safelyAllocate(long length, int maxLength) { if (length < 0L) { - throw new RecordFormatException("Can't allocate an array of length < 0"); + throw new RecordFormatException("Can't allocate an array of length < 0, but had " + length + " and " + maxLength); } if (length > (long)Integer.MAX_VALUE) { throw new RecordFormatException("Can't allocate an array > "+Integer.MAX_VALUE); diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/PPDrawingTextListing.java b/src/scratchpad/src/org/apache/poi/hslf/dev/PPDrawingTextListing.java index 380f9003bf..d44204331a 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/PPDrawingTextListing.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/PPDrawingTextListing.java @@ -39,43 +39,44 @@ public final class PPDrawingTextListing { System.exit(1); } - HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0]); + try (HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0])) { - // Find PPDrawings at any second level position - Record[] records = ss.getRecords(); - for(int i=0; iWriter to write out - * @throws java.io.IOException + * @throws java.io.IOException If writing to the writer fails */ public void dump(Writer outWriter) throws IOException { this.out = outWriter; @@ -98,7 +92,9 @@ public final class PPTXMLDump { //dump the structure of the powerpoint document write(out, "" + CR, padding); padding++; - dump(docstream, 0, docstream.length, padding); + if(docstream != null) { + dump(docstream, 0, docstream.length, padding); + } padding--; write(out, "" + CR, padding); padding--; @@ -111,7 +107,7 @@ public final class PPTXMLDump { * @param offset offset from the beginning of the document * @param length of the document * @param padding used for formatting results - * @throws java.io.IOException + * @throws java.io.IOException If writing out information fails */ public void dump(byte[] data, int offset, int length, int padding) throws IOException { int pos = offset; @@ -158,16 +154,24 @@ public final class PPTXMLDump { * Dumps the Pictures OLE stream into XML. * * @param data from the Pictures OLE data stream - * @param padding - * @throws java.io.IOException + * @param padding How many leading blanks to add in the output + * @throws java.io.IOException If writing out information fails */ public void dumpPictures(byte[] data, int padding) throws IOException { int pos = 0; while (pos < data.length) { byte[] header = new byte[PICT_HEADER_SIZE]; + if(data.length - pos < header.length) { + // corrupt file, cannot read header + return; + } System.arraycopy(data, pos, header, 0, header.length); int size = LittleEndian.getInt(header, 4) - 17; + if(size < 0) { + // corrupt file, negative image size + return; + } byte[] pictdata = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH); System.arraycopy(data, pos + PICT_HEADER_SIZE, pictdata, 0, pictdata.length); pos += PICT_HEADER_SIZE + size; @@ -184,7 +188,6 @@ public final class PPTXMLDump { padding--; write(out, "" + CR, padding); padding--; - } } @@ -272,5 +275,4 @@ public final class PPTXMLDump { (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'}; - } diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideAndNotesAtomListing.java b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideAndNotesAtomListing.java index 711f4ad6f5..a4ed6d28b6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideAndNotesAtomListing.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideAndNotesAtomListing.java @@ -41,7 +41,7 @@ public final class SlideAndNotesAtomListing { } HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0]); - System.out.println(""); + System.out.println(); // Find either Slides or Notes Record[] records = ss.getRecords(); @@ -55,14 +55,14 @@ public final class SlideAndNotesAtomListing { System.out.println("Found Slide at " + i); System.out.println(" Slide's master ID is " + sa.getMasterID()); System.out.println(" Slide's notes ID is " + sa.getNotesID()); - System.out.println(""); + System.out.println(); } if(r instanceof Notes) { Notes n = (Notes)r; NotesAtom na = n.getNotesAtom(); System.out.println("Found Notes at " + i); System.out.println(" Notes ID is " + na.getSlideID()); - System.out.println(""); + System.out.println(); } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java index a2c8b55ac2..10578f8a69 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideIdListing.java @@ -61,23 +61,23 @@ public final class SlideIdListing { // Grab any records that interest us Document document = null; - for(int i=0; i sheetOffsets = pph.getSlideLocationsLookup(); - for(int j=0; j sheetOffsets = pph.getSlideLocationsLookup(); + for (Integer id : sheetIDs) { Integer offset = sheetOffsets.get(id); System.out.println(" Knows about sheet " + id); @@ -143,7 +140,7 @@ public final class SlideIdListing { System.out.println(" The record at that pos is of type " + atPos.getRecordType()); System.out.println(" The record at that pos has class " + atPos.getClass().getName()); - if(! (atPos instanceof PositionDependentRecord)) { + if (!(atPos instanceof PositionDependentRecord)) { System.out.println(" ** The record class isn't position aware! **"); } } @@ -157,7 +154,7 @@ public final class SlideIdListing { ss.close(); - System.out.println(""); + System.out.println(); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowRecordDumper.java b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowRecordDumper.java index e60d1e7691..86c2335146 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowRecordDumper.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowRecordDumper.java @@ -56,7 +56,6 @@ public final class SlideShowRecordDumper { * dump of what it contains */ public static void main(String[] args) throws IOException { - String filename = ""; boolean verbose = false; boolean escher = false; @@ -81,12 +80,14 @@ public final class SlideShowRecordDumper { return; } - filename = args[ndx]; + String filename = args[ndx]; SlideShowRecordDumper foo = new SlideShowRecordDumper(System.out, filename, verbose, escher); foo.printDump(); + + foo.doc.close(); } public static void printUsage() { @@ -118,11 +119,11 @@ public final class SlideShowRecordDumper { } public String makeHex(int number, int padding) { - String hex = Integer.toHexString(number).toUpperCase(Locale.ROOT); + StringBuilder hex = new StringBuilder(Integer.toHexString(number).toUpperCase(Locale.ROOT)); while (hex.length() < padding) { - hex = "0" + hex; + hex.insert(0, "0"); } - return hex; + return hex.toString(); } public String reverseHex(String s) { @@ -186,7 +187,7 @@ public final class SlideShowRecordDumper { for (Record child : etw.getChildRecords()) { if (child instanceof StyleTextPropAtom) { // need preceding Text[Chars|Bytes]Atom to initialize the data structure - String text = null; + String text; if (prevChild instanceof TextCharsAtom) { text = ((TextCharsAtom)prevChild).getText(); } else if (prevChild instanceof TextBytesAtom) { @@ -227,8 +228,7 @@ public final class SlideShowRecordDumper { public void walkTree(int depth, int pos, Record[] records, int indent) throws IOException { String ind = tabs.substring(0, indent); - for (int i = 0; i < records.length; i++) { - Record r = records[i]; + for (Record r : records) { if (r == null) { ps.println(ind + "At position " + pos + " (" + makeHex(pos, 6) + "):"); ps.println(ind + "Warning! Null record found."); @@ -242,49 +242,49 @@ public final class SlideShowRecordDumper { String hexType = makeHex((int) r.getRecordType(), 4); String rHexType = reverseHex(hexType); - // Grab the hslf.record type - Class c = r.getClass(); - String cname = c.toString(); - if(cname.startsWith("class ")) { - cname = cname.substring(6); - } - if(cname.startsWith("org.apache.poi.hslf.record.")) { - cname = cname.substring(27); - } + // Grab the hslf.record type + Class c = r.getClass(); + String cname = c.toString(); + if (cname.startsWith("class ")) { + cname = cname.substring(6); + } + if (cname.startsWith("org.apache.poi.hslf.record.")) { + cname = cname.substring(27); + } - // Display the record - ps.println(ind + "At position " + pos + " (" + makeHex(pos,6) + "):"); - ps.println(ind + " Record is of type " + cname); - ps.println(ind + " Type is " + r.getRecordType() + " (" + hexType + " -> " + rHexType + " )"); - ps.println(ind + " Len is " + (len-8) + " (" + makeHex((len-8),8) + "), on disk len is " + len ); + // Display the record + ps.println(ind + "At position " + pos + " (" + makeHex(pos, 6) + "):"); + ps.println(ind + " Record is of type " + cname); + ps.println(ind + " Type is " + r.getRecordType() + " (" + hexType + " -> " + rHexType + " )"); + ps.println(ind + " Len is " + (len - 8) + " (" + makeHex((len - 8), 8) + "), on disk len is " + len); - // print additional information for drawings and atoms - if (optEscher && cname.equals("PPDrawing")) { - DefaultEscherRecordFactory factory = new HSLFEscherRecordFactory(); + // print additional information for drawings and atoms + if (optEscher && cname.equals("PPDrawing")) { + DefaultEscherRecordFactory factory = new HSLFEscherRecordFactory(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - r.writeOut(baos); - byte[] b = baos.toByteArray(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + r.writeOut(baos); + byte[] b = baos.toByteArray(); - EscherRecord er = factory.createRecord(b, 0); - er.fillFields(b, 0, factory); + EscherRecord er = factory.createRecord(b, 0); + er.fillFields(b, 0, factory); - printEscherRecord( er, indent+1 ); + printEscherRecord(er, indent + 1); - } else if(optVerbose && r.getChildRecords() == null) { - String recData = getPrintableRecordContents(r); - ps.println(ind + recData ); - } + } else if (optVerbose && r.getChildRecords() == null) { + String recData = getPrintableRecordContents(r); + ps.println(ind + recData); + } - ps.println(); + ps.println(); - // If it has children, show them - if(r.getChildRecords() != null) { - walkTree((depth+3),pos+8,r.getChildRecords(), indent+1); - } + // If it has children, show them + if (r.getChildRecords() != null) { + walkTree((depth + 3), pos + 8, r.getChildRecords(), indent + 1); + } - // Wind on the position marker - pos += len; - } + // Wind on the position marker + pos += len; + } } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/TextStyleListing.java b/src/scratchpad/src/org/apache/poi/hslf/dev/TextStyleListing.java index 3e313f90a0..7d001b2716 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/TextStyleListing.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/TextStyleListing.java @@ -45,25 +45,24 @@ public final class TextStyleListing { // Find the documents, and then their SLWT Record[] records = ss.getRecords(); - for(int i=0; i OLD_FILES = new HashSet<>(); + static { + OLD_FILES.add("PPT95.ppt"); + OLD_FILES.add("pp40only.ppt"); + } + + protected static final Set ENCRYPTED_FILES = new HashSet<>(); + static { + ENCRYPTED_FILES.add("cryptoapi-proc2356.ppt"); + ENCRYPTED_FILES.add("Password_Protected-np-hello.ppt"); + ENCRYPTED_FILES.add("Password_Protected-56-hello.ppt"); + ENCRYPTED_FILES.add("Password_Protected-hello.ppt"); + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + protected static final Map> EXCLUDED = + new HashMap<>(); + + @Parameterized.Parameters(name="{index}: {0}") + public static Iterable files() { + String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY); + if(dataDirName == null) { + dataDirName = "test-data"; + } + + List files = new ArrayList<>(); + findFile(files, dataDirName + "/slideshow"); + + return files; + } + + private final PrintStream save = System.out; + + @Before + public void setUpBase() throws UnsupportedEncodingException { + // set a higher max allocation limit as some test-files require more + IOUtils.setByteArrayMaxOverride(5*1024*1024); + + // redirect standard out during the test to avoid spamming the console with output + System.setOut(new PrintStream(NULL_OUTPUT_STREAM, true, LocaleUtil.CHARSET_1252.name())); + } + + @After + public void tearDownBase() { + System.setOut(save); + + // reset + IOUtils.setByteArrayMaxOverride(-1); + } + + private static void findFile(List list, String dir) { + String[] files = new File(dir).list((arg0, arg1) -> arg1.toLowerCase(Locale.ROOT).endsWith(".ppt")); + + assertNotNull("Did not find any ppt files in directory " + dir, files); + + for(String file : files) { + list.add(new Object[] { new File(dir, file) }); + } + } + + @Parameterized.Parameter + public File file; + + @Test + public void testAllFiles() throws Exception { + String fileName = file.getName(); + if (EXCLUDED.containsKey(fileName)) { + thrown.expect(EXCLUDED.get(fileName)); + } + + try { + runOneFile(file); + } catch (OldPowerPointFormatException e) { + // expected for some files + if(!OLD_FILES.contains(file.getName())) { + throw e; + } + } catch (EncryptedPowerPointFileException e) { + // expected for some files + if(!ENCRYPTED_FILES.contains(file.getName())) { + throw e; + } + } + } + + abstract void runOneFile(File pFile) throws Exception; +} diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestPPDrawingTextListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestPPDrawingTextListing.java new file mode 100644 index 0000000000..21b63d91ec --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestPPDrawingTextListing.java @@ -0,0 +1,44 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestPPDrawingTextListing extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + // calls System.exit(): PPDrawingTextListing.main(new String[0]); + + try { + PPDrawingTextListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + PPDrawingTextListing.main(new String[]{pFile.getAbsolutePath()}); + } +} diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestPPTXMLDump.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestPPTXMLDump.java new file mode 100644 index 0000000000..de2f7721c2 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestPPTXMLDump.java @@ -0,0 +1,49 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.apache.poi.hslf.HSLFTestDataSamples; +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.fail; + +public class TestPPTXMLDump extends BasePPTIteratingTest { + @Test + public void testMain() throws Exception { + PPTXMLDump.main(new String[0]); + + PPTXMLDump.main(new String[]{ + HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(), + HSLFTestDataSamples.getSampleFile("pictures.ppt").getAbsolutePath() + }); + + try { + PPTXMLDump.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + PPTXMLDump.main(new String[]{pFile.getAbsolutePath()}); + } +} diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSLWTListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSLWTListing.java new file mode 100644 index 0000000000..fbd2aa0764 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSLWTListing.java @@ -0,0 +1,43 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestSLWTListing extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + // calls System.exit(): SLWTListing.main(new String[0]); + try { + SLWTListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + SLWTListing.main(new String[]{pFile.getAbsolutePath()}); + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSLWTTextListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSLWTTextListing.java new file mode 100644 index 0000000000..83729e2838 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSLWTTextListing.java @@ -0,0 +1,44 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestSLWTTextListing extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + // calls System.exit(): SLWTTextListing.main(new String[0]); + + try { + SLWTTextListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + SLWTTextListing.main(new String[]{pFile.getAbsolutePath()}); + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideAndNotesAtomListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideAndNotesAtomListing.java new file mode 100644 index 0000000000..82f27e546a --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideAndNotesAtomListing.java @@ -0,0 +1,48 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.apache.poi.hslf.HSLFTestDataSamples; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestSlideAndNotesAtomListing extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + // calls System.exit(): SlideAndNotesAtomListing.main(new String[0]); + SlideAndNotesAtomListing.main(new String[] { + HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath() + }); + + try { + SlideAndNotesAtomListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + SlideAndNotesAtomListing.main(new String[]{pFile.getAbsolutePath()}); + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideIdListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideIdListing.java new file mode 100644 index 0000000000..4a896323bc --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideIdListing.java @@ -0,0 +1,48 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.apache.poi.hslf.HSLFTestDataSamples; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestSlideIdListing extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + // calls System.exit(): SlideIdListing.main(new String[0]); + SlideIdListing.main(new String[] { + HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath() + }); + + try { + SlideIdListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + SlideIdListing.main(new String[]{pFile.getAbsolutePath()}); + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideShowDumper.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideShowDumper.java new file mode 100644 index 0000000000..e749d7586d --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideShowDumper.java @@ -0,0 +1,79 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.apache.poi.hslf.HSLFTestDataSamples; +import org.apache.poi.util.IOUtils; +import org.junit.Test; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.fail; + +public class TestSlideShowDumper extends BasePPTIteratingTest { + private static final Set FAILING = new HashSet<>(); + static { + FAILING.add("cryptoapi-proc2356.ppt"); + FAILING.add("41384.ppt"); + FAILING.add("bug56240.ppt"); + } + + @Test + public void testMain() throws IOException { + SlideShowDumper.main(new String[0]); + + // SlideShowDumper calls IOUtils.toByteArray(is), which would fail if a different size is defined + IOUtils.setByteArrayMaxOverride(-1); + + SlideShowDumper.main(new String[] { + HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(), + HSLFTestDataSamples.getSampleFile("pictures.ppt").getAbsolutePath() + }); + + try { + SlideShowDumper.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + try { + // SlideShowDumper calls IOUtils.toByteArray(is), which would fail if a different size is defined + IOUtils.setByteArrayMaxOverride(-1); + + SlideShowDumper.main(new String[]{pFile.getAbsolutePath()}); + } catch (ArrayIndexOutOfBoundsException e) { + // some corrupted documents currently can cause this excpetion + if (!FAILING.contains(pFile.getName()) && !ENCRYPTED_FILES.contains(pFile.getName())) { + throw e; + } + } catch (FileNotFoundException e) { + // some old files are not detected correctly + if(!OLD_FILES.contains(pFile.getName())) { + throw e; + } + } + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideShowRecordDumper.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideShowRecordDumper.java new file mode 100644 index 0000000000..8158b5c547 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestSlideShowRecordDumper.java @@ -0,0 +1,59 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.apache.poi.hslf.HSLFTestDataSamples; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestSlideShowRecordDumper extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + SlideShowRecordDumper.main(new String[0]); + + SlideShowRecordDumper.main(new String[] { + HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(), + }); + + SlideShowRecordDumper.main(new String[] { + "-escher", + HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(), + }); + + SlideShowRecordDumper.main(new String[] { + "-verbose", + HSLFTestDataSamples.getSampleFile("pictures.ppt").getAbsolutePath() + }); + + try { + SlideShowRecordDumper.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + SlideShowRecordDumper.main(new String[]{pFile.getAbsolutePath()}); + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestTextStyleListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestTextStyleListing.java new file mode 100644 index 0000000000..84991da755 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestTextStyleListing.java @@ -0,0 +1,57 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.fail; + +public class TestTextStyleListing extends BasePPTIteratingTest { + private static Set FAILING = new HashSet<>(); + static { + FAILING.add("empty_textbox.ppt"); + } + + @Test + public void testMain() throws IOException { + // calls System.exit(): TextStyleListing.main(new String[0]); + try { + TextStyleListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + try { + TextStyleListing.main(new String[]{pFile.getAbsolutePath()}); + } catch (ArrayIndexOutOfBoundsException e) { + // some corrupted documents currently can cause this excpetion + if (!FAILING.contains(pFile.getName())) { + throw e; + } + } + } +} \ No newline at end of file diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestUserEditAndPersistListing.java b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestUserEditAndPersistListing.java new file mode 100644 index 0000000000..61a9e99660 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hslf/dev/TestUserEditAndPersistListing.java @@ -0,0 +1,44 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.dev; + +import org.apache.poi.EmptyFileException; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.Assert.fail; + +public class TestUserEditAndPersistListing extends BasePPTIteratingTest { + @Test + public void testMain() throws IOException { + // calls System.exit(): UserEditAndPersistListing.main(new String[0]); + + try { + UserEditAndPersistListing.main(new String[]{"invalidfile"}); + fail("Should catch exception here"); + } catch (EmptyFileException e) { + // expected here + } + } + + @Override + void runOneFile(File pFile) throws Exception { + UserEditAndPersistListing.main(new String[]{pFile.getAbsolutePath()}); + } +} \ No newline at end of file diff --git a/src/testcases/org/apache/poi/ddf/TestEscherDump.java b/src/testcases/org/apache/poi/ddf/TestEscherDump.java index 95b0745205..64da89d9c6 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherDump.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherDump.java @@ -52,7 +52,7 @@ public class TestEscherDump { @Test public void testWithData() throws Exception { - new EscherDump().dumpOld(8, new ByteArrayInputStream(new byte[] { 00, 00, 00, 00, 00, 00, 00, 00 }), nullPS); + new EscherDump().dumpOld(8, new ByteArrayInputStream(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}), nullPS); } @Test @@ -63,14 +63,11 @@ public class TestEscherDump { //new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out); data = new byte[2586114]; - InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls"); - try { + try (InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls")) { int bytes = IOUtils.readFully(stream, data); assertTrue(bytes != -1); //new EscherDump().dump(bytes, data, System.out); //new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out); - } finally { - stream.close(); } } diff --git a/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java b/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java index c87bf45e67..d57583aeb9 100644 --- a/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java +++ b/src/testcases/org/apache/poi/hssf/dev/BaseXLSIteratingTest.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertNotNull; import java.io.File; import java.io.FileInputStream; -import java.io.FilenameFilter; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; @@ -29,7 +28,6 @@ import java.util.Locale; import java.util.Map; import org.apache.poi.POIDataSamples; -import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.NullOutputStream; import org.junit.Rule; @@ -71,12 +69,7 @@ public abstract class BaseXLSIteratingTest { } private static void findFile(List list, String dir) { - String[] files = new File(dir).list(new FilenameFilter() { - @Override - public boolean accept(File arg0, String arg1) { - return arg1.toLowerCase(Locale.ROOT).endsWith(".xls"); - } - }); + String[] files = new File(dir).list((arg0, arg1) -> arg1.toLowerCase(Locale.ROOT).endsWith(".xls")); assertNotNull("Did not find any xls files in directory " + dir, files); @@ -99,17 +92,9 @@ public abstract class BaseXLSIteratingTest { runOneFile(file); } catch (Exception e) { // try to read it in HSSFWorkbook to quickly fail if we cannot read the file there at all and thus probably should use EXCLUDED instead - FileInputStream stream = new FileInputStream(file); - HSSFWorkbook wb = null; - try { - wb = new HSSFWorkbook(stream); - assertNotNull(wb); - } finally { - if (wb != null) { - wb.close(); - } - stream.close(); - } + try (FileInputStream stream = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(stream)) { + assertNotNull(wb); + } throw e; } diff --git a/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java b/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java index c739364c42..064e29839e 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestBiffDrawingToXml.java @@ -43,7 +43,6 @@ public class TestBiffDrawingToXml extends BaseXLSIteratingTest { EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 - EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class); EXCLUDED.put("61300.xls", RecordFormatException.class); @@ -55,12 +54,9 @@ public class TestBiffDrawingToXml extends BaseXLSIteratingTest { try { //System.setOut(new PrintStream(TestBiffViewer.NULL_OUTPUT_STREAM)); // use a NullOutputStream to not write the bytes anywhere for best runtime - InputStream wb = new FileInputStream(pFile); - try { - BiffDrawingToXml.writeToFile(NULL_OUTPUT_STREAM, wb, false, new String[] {}); - } finally { - wb.close(); - } + try (InputStream wb = new FileInputStream(pFile)) { + BiffDrawingToXml.writeToFile(NULL_OUTPUT_STREAM, wb, false, new String[]{}); + } } finally { System.setOut(save); } diff --git a/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java index 0ba83ae55f..3daf938d5c 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestEFBiffViewer.java @@ -43,7 +43,6 @@ public class TestEFBiffViewer extends BaseXLSIteratingTest { EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 - EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class); EXCLUDED.put("XRefCalc.xls", RuntimeException.class); // "Buffer overrun" diff --git a/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java index a272fc9314..e94d55ddfb 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestFormulaViewer.java @@ -44,7 +44,6 @@ public class TestFormulaViewer extends BaseXLSIteratingTest { EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 - EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class); EXCLUDED.put("61300.xls", RecordFormatException.class); diff --git a/src/testcases/org/apache/poi/hssf/dev/TestReSave.java b/src/testcases/org/apache/poi/hssf/dev/TestReSave.java index 09d560de7e..a8d59873e1 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestReSave.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestReSave.java @@ -47,7 +47,6 @@ public class TestReSave extends BaseXLSIteratingTest { EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 - EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class); EXCLUDED.put("XRefCalc.xls", RuntimeException.class); // "Buffer overrun" @@ -79,7 +78,6 @@ public class TestReSave extends BaseXLSIteratingTest { // clean up the re-saved file assertTrue(!reSavedFile.exists() || reSavedFile.delete()); } - } finally { System.setOut(save); } diff --git a/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java b/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java index cf0907ea6e..ec7cd8c087 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestRecordLister.java @@ -37,7 +37,6 @@ public class TestRecordLister extends BaseXLSIteratingTest { EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5 EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 - EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95 EXCLUDED.put("61300.xls", RecordFormatException.class); }