From 42bb3f2194c23bffd60f7598d8055065e7c5754e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 30 Nov 2014 17:09:03 +0000 Subject: [PATCH] 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 --- .../apache/poi/poifs/storage/HeaderBlock.java | 6 ++-- .../poi/hssf/usermodel/TestHSSFWorkbook.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index 560111fa01..06e9c4a1b9 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -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 diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index a37751dec3..2fabba9def 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -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} -- 2.39.5