Correct older biff detection, and add unit tests for HSSFWorkbook giving helpful exceptions on the older formats

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1642568 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-11-30 17:09:03 +00:00
parent fd9a2b0527
commit 680feb50ee
2 changed files with 39 additions and 3 deletions

View File

@ -136,7 +136,7 @@ public final class HeaderBlock implements HeaderBlockConstants {
if (_data[0] == 0x09 && _data[1] == 0x00 && // sid=0x0009
_data[2] == 0x04 && _data[3] == 0x00 && // size=0x0004
_data[4] == 0x00 && _data[5] == 0x00 && // unused
(_data[6] == 0x01 || _data[6] == 0x02 || _data[6] == 0x04) &&
(_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
_data[7] == 0x00) {
// BIFF2 raw stream
throw new OldExcelFormatException("The supplied data appears to be in BIFF2 format. " +
@ -145,7 +145,7 @@ public final class HeaderBlock implements HeaderBlockConstants {
if (_data[0] == 0x09 && _data[1] == 0x02 && // sid=0x0209
_data[2] == 0x06 && _data[3] == 0x00 && // size=0x0006
_data[4] == 0x00 && _data[5] == 0x00 && // unused
(_data[6] == 0x01 || _data[6] == 0x02 || _data[6] == 0x04) &&
(_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
_data[7] == 0x00) {
// BIFF3 raw stream
throw new OldExcelFormatException("The supplied data appears to be in BIFF3 format. " +
@ -154,7 +154,7 @@ public final class HeaderBlock implements HeaderBlockConstants {
if (_data[0] == 0x09 && _data[1] == 0x04 && // sid=0x0409
_data[2] == 0x06 && _data[3] == 0x00 && // size=0x0006
_data[4] == 0x00 && _data[5] == 0x00) { // unused
if (((_data[6] == 0x01 || _data[6] == 0x02 || _data[6] == 0x04) &&
if (((_data[6] == 0x10 || _data[6] == 0x20 || _data[6] == 0x40) &&
_data[7] == 0x00) ||
(_data[6] == 0x00 && _data[7] == 0x01)) {
// BIFF4 raw stream

View File

@ -23,6 +23,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.apache.poi.POITestCase.assertContains;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -40,6 +41,7 @@ import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
@ -561,6 +563,40 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertTrue(clsid1.equals(clsid2));
}
/**
* If we try to open an old (pre-97) workbook, we get a helpful
* Exception give to explain what we've done wrong
*/
@Test
public void helpfulExceptionOnOldFiles() throws Exception {
InputStream excel4 = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("testEXCEL_4.xls");
try {
new HSSFWorkbook(excel4);
fail("Shouldn't be able to load an Excel 4 file");
} catch (OldExcelFormatException e) {
assertContains(e.getMessage(), "BIFF4");
}
excel4.close();
InputStream excel5 = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("testEXCEL_5.xls");
try {
new HSSFWorkbook(excel5);
fail("Shouldn't be able to load an Excel 5 file");
} catch (OldExcelFormatException e) {
assertContains(e.getMessage(), "BIFF5");
}
excel5.close();
InputStream excel95 = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("testEXCEL_95.xls");
try {
new HSSFWorkbook(excel95);
fail("Shouldn't be able to load an Excel 95 file");
} catch (OldExcelFormatException e) {
assertContains(e.getMessage(), "BIFF5");
}
excel95.close();
}
/**
* Tests that we can work with both {@link POIFSFileSystem}