]> source.dussan.org Git - poi.git/commitdiff
#61266 Test for old unsupported MS Write WRI files, and give a more helpful exception...
authorNick Burch <nick@apache.org>
Sun, 9 Jul 2017 16:26:33 +0000 (16:26 +0000)
committerNick Burch <nick@apache.org>
Sun, 9 Jul 2017 16:26:33 +0000 (16:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1801376 13f79535-47bb-0310-9956-ffa450edef68

src/integrationtest/org/apache/poi/TestAllFiles.java
src/java/org/apache/poi/poifs/storage/HeaderBlock.java
src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java [new file with mode: 0644]
src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java
test-data/document/MSWriteOld.wri [new file with mode: 0644]

index 3f0d510203a6476c04262b0a90fbab6ee9c5d1de..9a9ba8864f9f3bf39d6ea37420aa5e79d71c8f3c 100644 (file)
@@ -156,6 +156,7 @@ public class TestAllFiles {
         HANDLERS.put(".dat", new HMEFFileHandler());
 
         // TODO: are these readable by some of the formats?
+        HANDLERS.put(".wri", new NullFileHandler());
         HANDLERS.put(".shw", new NullFileHandler());
         HANDLERS.put(".zvi", new NullFileHandler());
         HANDLERS.put(".mpp", new NullFileHandler());
index aced76bbe276a332531ea4fe6b98f6d43d64d4ef..fe64f61c830f161581daefbc12bc14be11707473 100644 (file)
@@ -67,6 +67,13 @@ public final class HeaderBlock implements HeaderBlockConstants {
         0x00, 0x00, // unused
         0x00, 0x01
     };
+    
+    private static final byte[] MAGIC_MSWRITEa = {
+        0x31, (byte)0xbe, 0x00, 0x00
+    };
+    private static final byte[] MAGIC_MSWRITEb = {
+        0x32, (byte)0xbe, 0x00, 0x00
+    };
 
     private static final byte _default_value = ( byte ) 0xFF;
 
@@ -159,6 +166,12 @@ public final class HeaderBlock implements HeaderBlockConstants {
                     + "Formats such as Office 2003 XML are not supported");
             }
             
+            // Old MS Write raw stream
+            if (cmp(MAGIC_MSWRITEa, data) || cmp(MAGIC_MSWRITEb, data)) {
+                throw new NotOLE2FileException("The supplied data appears to be in the old MS Write format. "
+                    + "Apache POI doesn't currently support this format");
+            }
+
             // BIFF2 raw stream
             if (cmp(MAGIC_BIFF2, data)) {
                 throw new OldExcelFormatException("The supplied data appears to be in BIFF2 format. "
index 999f2de1be31ab5e0f3be828eeb2433963eae7bc..e4dfd71efef6ae7b92e8bdd4bffe0d1bb2fbb26e 100644 (file)
@@ -32,6 +32,7 @@ import org.junit.runners.Suite;
     , TestDocumentNode.class
     , TestDocumentOutputStream.class
     , TestEmptyDocument.class
+    , TestNotOLE2Exception.class
     , TestOfficeXMLException.class
     , TestPOIFSDocumentPath.class
     , TestPOIFSFileSystem.class
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java
new file mode 100644 (file)
index 0000000..cb15672
--- /dev/null
@@ -0,0 +1,95 @@
+/* ====================================================================
+   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.poifs.filesystem;
+
+import static org.apache.poi.POITestCase.assertContains;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.OldExcelFormatException;
+
+/**
+ * Class to test that POIFS complains when given older non-OLE2
+ *  formats. See also {@link TestOfficeXMLException} for OOXML
+ *  checks 
+ */
+public class TestNotOLE2Exception extends TestCase {
+       private static final InputStream openXLSSampleStream(String sampleFileName) {
+               return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
+       }
+    private static final InputStream openDOCSampleStream(String sampleFileName) {
+        return POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName);
+    }
+    
+       public void testRawXMLException() throws IOException {
+        InputStream in = openXLSSampleStream("SampleSS.xml");
+
+        try {
+            new POIFSFileSystem(in).close();
+            fail("expected exception was not thrown");
+        } catch(NotOLE2FileException e) {
+            // expected during successful test
+            assertContains(e.getMessage(), "The supplied data appears to be a raw XML file");
+            assertContains(e.getMessage(), "Formats such as Office 2003 XML");
+        }
+    }
+       
+    public void testMSWriteException() throws IOException {
+        InputStream in = openDOCSampleStream("MSWriteOld.wri");
+
+        try {
+            new POIFSFileSystem(in).close();
+            fail("expected exception was not thrown");
+        } catch(NotOLE2FileException e) {
+            // expected during successful test
+            assertContains(e.getMessage(), "The supplied data appears to be in the old MS Write");
+            assertContains(e.getMessage(), "doesn't currently support");
+        }
+    }
+    
+       public void testBiff3Exception() throws IOException {
+        InputStream in = openXLSSampleStream("testEXCEL_3.xls");
+
+        try {
+            new POIFSFileSystem(in).close();
+            fail("expected exception was not thrown");
+        } catch(OldExcelFormatException e) {
+            // expected during successful test
+            assertContains(e.getMessage(), "The supplied data appears to be in BIFF3 format");
+            assertContains(e.getMessage(), "try OldExcelExtractor");
+        }
+       }
+
+    public void testBiff4Exception() throws IOException {
+        InputStream in = openXLSSampleStream("testEXCEL_4.xls");
+
+        try {
+            new POIFSFileSystem(in).close();
+            fail("expected exception was not thrown");
+        } catch(OldExcelFormatException e) {
+            // expected during successful test
+            assertContains(e.getMessage(), "The supplied data appears to be in BIFF4 format");
+            assertContains(e.getMessage(), "try OldExcelExtractor");
+        }
+    }
+}
index 9e5e0e1be44eee6dbd333e25c6406a954f6aa163..775760aabda8f93f88148b9b297a8ed157ab0c7c 100644 (file)
@@ -75,6 +75,14 @@ public class TestOfficeXMLException extends TestCase {
                // xls file is
                confirmIsPOIFS("SampleSS.xls", true);
                
+               // older biff formats aren't
+        confirmIsPOIFS("testEXCEL_3.xls", false);
+        confirmIsPOIFS("testEXCEL_4.xls", false);
+        
+        // newer excel formats are
+        confirmIsPOIFS("testEXCEL_5.xls", true);
+        confirmIsPOIFS("testEXCEL_95.xls", true);
+               
                // text file isn't
                confirmIsPOIFS("SampleSS.txt", false);
        }
diff --git a/test-data/document/MSWriteOld.wri b/test-data/document/MSWriteOld.wri
new file mode 100644 (file)
index 0000000..9391077
Binary files /dev/null and b/test-data/document/MSWriteOld.wri differ