Browse Source

#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
tags/REL_3_17_FINAL
Nick Burch 6 years ago
parent
commit
933f9c1201

+ 1
- 0
src/integrationtest/org/apache/poi/TestAllFiles.java 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());

+ 13
- 0
src/java/org/apache/poi/poifs/storage/HeaderBlock.java 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. "

+ 1
- 0
src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java 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

+ 95
- 0
src/testcases/org/apache/poi/poifs/filesystem/TestNotOLE2Exception.java 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");
}
}
}

+ 8
- 0
src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java 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);
}

BIN
test-data/document/MSWriteOld.wri View File


Loading…
Cancel
Save