diff --git a/src/integrationtest/org/apache/poi/TestAllFiles.java b/src/integrationtest/org/apache/poi/TestAllFiles.java index 3f0d510203..9a9ba8864f 100644 --- a/src/integrationtest/org/apache/poi/TestAllFiles.java +++ b/src/integrationtest/org/apache/poi/TestAllFiles.java @@ -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()); diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index aced76bbe2..fe64f61c83 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -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. " diff --git a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java index 999f2de1be..e4dfd71efe 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java @@ -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 index 0000000000..cb156728eb --- /dev/null +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java @@ -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"); + } + } +} diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java b/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java index 9e5e0e1be4..775760aabd 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java @@ -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 index 0000000000..9391077cec Binary files /dev/null and b/test-data/document/MSWriteOld.wri differ