Author: nick
Date: Sun Jul  9 16:26:33 2017
New Revision: 1801376

URL: http://svn.apache.org/viewvc?rev=1801376&view=rev
Log:
#61266 Test for old unsupported MS Write WRI files, and give a more helpful 
exception if found, plus unit tests

Added:
    
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java
   (with props)
    poi/trunk/test-data/document/MSWriteOld.wri   (with props)
Modified:
    poi/trunk/src/integrationtest/org/apache/poi/TestAllFiles.java
    poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java
    
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
    
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java

Modified: poi/trunk/src/integrationtest/org/apache/poi/TestAllFiles.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/integrationtest/org/apache/poi/TestAllFiles.java?rev=1801376&r1=1801375&r2=1801376&view=diff
==============================================================================
--- poi/trunk/src/integrationtest/org/apache/poi/TestAllFiles.java (original)
+++ poi/trunk/src/integrationtest/org/apache/poi/TestAllFiles.java Sun Jul  9 
16:26:33 2017
@@ -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());

Modified: poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java?rev=1801376&r1=1801375&r2=1801376&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java Sun Jul  9 
16:26:33 2017
@@ -67,6 +67,13 @@ public final class HeaderBlock implement
         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 implement
                     + "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. "

Modified: 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java?rev=1801376&r1=1801375&r2=1801376&view=diff
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
 (original)
+++ 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
 Sun Jul  9 16:26:33 2017
@@ -32,6 +32,7 @@ import org.junit.runners.Suite;
     , TestDocumentNode.class
     , TestDocumentOutputStream.class
     , TestEmptyDocument.class
+    , TestNotOLE2Exception.class
     , TestOfficeXMLException.class
     , TestPOIFSDocumentPath.class
     , TestPOIFSFileSystem.class

Added: 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java?rev=1801376&view=auto
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java
 (added)
+++ 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java
 Sun Jul  9 16:26:33 2017
@@ -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");
+        }
+    }
+}

Propchange: 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java?rev=1801376&r1=1801375&r2=1801376&view=diff
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java
 (original)
+++ 
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java
 Sun Jul  9 16:26:33 2017
@@ -75,6 +75,14 @@ public class TestOfficeXMLException exte
                // 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);
        }

Added: poi/trunk/test-data/document/MSWriteOld.wri
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/document/MSWriteOld.wri?rev=1801376&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/MSWriteOld.wri
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to