#61266 Test for old unsupported MS Write WRI files, and give a more helpful exception if found, plus unit tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1801376 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2017-07-09 16:26:33 +00:00
parent 0cac9103ba
commit 05c7c16308
6 changed files with 118 additions and 0 deletions

View 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());

View 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. "

View File

@ -32,6 +32,7 @@ import org.junit.runners.Suite;
, TestDocumentNode.class
, TestDocumentOutputStream.class
, TestEmptyDocument.class
, TestNotOLE2Exception.class
, TestOfficeXMLException.class
, TestPOIFSDocumentPath.class
, TestPOIFSFileSystem.class

View File

@ -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");
}
}
}

View 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);
}

Binary file not shown.