diff options
author | Nick Burch <nick@apache.org> | 2015-04-23 18:01:51 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2015-04-23 18:01:51 +0000 |
commit | 76919e72c3d9bb28a40cbffe333ed4c2173350de (patch) | |
tree | 0da389f3f561ba10b8f0c3f3f8f167f1203d63fb /src/testcases/org/apache/poi/hssf/eventusermodel | |
parent | 3c1dc201a3d054993e01dd0619f64fa49f517bbe (diff) | |
download | poi-76919e72c3d9bb28a40cbffe333ed4c2173350de.tar.gz poi-76919e72c3d9bb28a40cbffe333ed4c2173350de.zip |
Add unit test for Eventmodel processing of password protected excel files
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1675689 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf/eventusermodel')
-rw-r--r-- | src/testcases/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java b/src/testcases/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java index ff76cfa19c..7970aaacd7 100644 --- a/src/testcases/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java +++ b/src/testcases/org/apache/poi/hssf/eventusermodel/TestHSSFEventFactory.java @@ -22,15 +22,19 @@ import java.util.List; import junit.framework.TestCase; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.ContinueRecord; import org.apache.poi.hssf.record.DVALRecord; import org.apache.poi.hssf.record.DVRecord; import org.apache.poi.hssf.record.EOFRecord; import org.apache.poi.hssf.record.FeatHdrRecord; +import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.SelectionRecord; import org.apache.poi.hssf.record.WindowTwoRecord; +import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * @@ -137,4 +141,63 @@ public final class TestHSSFEventFactory extends TestCase { factory = new HSSFEventFactory(); factory.processWorkbookEvents(req, fs); } + + public void testWithPasswordProtectedWorkbooks() throws Exception { + HSSFRequest req = new HSSFRequest(); + MockHSSFListener mockListen = new MockHSSFListener(); + req.addListenerForAllRecords(mockListen); + + // Without a password, can't be read + Biff8EncryptionKey.setCurrentUserPassword(null); + POIFSFileSystem fs = new POIFSFileSystem(openSample("xor-encryption-abc.xls")); + + HSSFEventFactory factory = new HSSFEventFactory(); + try { + factory.processWorkbookEvents(req, fs); + fail("Shouldn't be able to process protected workbook without the password"); + } catch (EncryptedDocumentException e) {} + + + // With the password, is properly processed + Biff8EncryptionKey.setCurrentUserPassword("abc"); + + req = new HSSFRequest(); + mockListen = new MockHSSFListener(); + req.addListenerForAllRecords(mockListen); + factory.processWorkbookEvents(req, fs); + + // Check we got the sheet and the contents + Record[] recs = mockListen.getRecords(); + assertTrue( recs.length > 50 ); + + // Has one sheet, with values 1,2,3 in column A rows 1-3 + boolean hasSheet=false, hasA1=false, hasA2=false, hasA3=false; + for (Record r : recs) { + if (r instanceof BoundSheetRecord) { + BoundSheetRecord bsr = (BoundSheetRecord)r; + assertEquals("Sheet1", bsr.getSheetname()); + hasSheet = true; + } + if (r instanceof NumberRecord) { + NumberRecord nr = (NumberRecord)r; + if (nr.getColumn() == 0 && nr.getRow() == 0) { + assertEquals(1, (int)nr.getValue()); + hasA1 = true; + } + if (nr.getColumn() == 0 && nr.getRow() == 1) { + assertEquals(2, (int)nr.getValue()); + hasA2 = true; + } + if (nr.getColumn() == 0 && nr.getRow() == 2) { + assertEquals(3, (int)nr.getValue()); + hasA3 = true; + } + } + } + + assertTrue("Sheet record not found", hasSheet); + assertTrue("Numeric record for A1 not found", hasA1); + assertTrue("Numeric record for A2 not found", hasA2); + assertTrue("Numeric record for A3 not found", hasA3); + } } |