diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2018-05-01 18:22:00 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2018-05-01 18:22:00 +0000 |
commit | 28e662c5a72180e39c28b9d8cf457ea90185de50 (patch) | |
tree | 6cd4023aeaebb261123f6a8ded942c496591a3fe /src/scratchpad/testcases/org/apache/poi | |
parent | e73282a9e8425d5bc320696f68683a70c9de137e (diff) | |
download | poi-28e662c5a72180e39c28b9d8cf457ea90185de50.tar.gz poi-28e662c5a72180e39c28b9d8cf457ea90185de50.zip |
Cleanup Biff8EncryptionKey usage and use HPSF constants instead of duplicated strings
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1830705 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad/testcases/org/apache/poi')
-rw-r--r-- | src/scratchpad/testcases/org/apache/poi/TestPOIDocumentScratchpad.java | 6 | ||||
-rw-r--r-- | src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java | 206 |
2 files changed, 105 insertions, 107 deletions
diff --git a/src/scratchpad/testcases/org/apache/poi/TestPOIDocumentScratchpad.java b/src/scratchpad/testcases/org/apache/poi/TestPOIDocumentScratchpad.java index 152fd0405c..5cfb54d106 100644 --- a/src/scratchpad/testcases/org/apache/poi/TestPOIDocumentScratchpad.java +++ b/src/scratchpad/testcases/org/apache/poi/TestPOIDocumentScratchpad.java @@ -28,7 +28,9 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument; +import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl; import org.apache.poi.hwpf.HWPFTestDataSamples; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; @@ -91,8 +93,8 @@ public final class TestPOIDocumentScratchpad { doc.writeProperties(outFS); // Should now hold them - assertNotNull(outFS.createDocumentInputStream("\005SummaryInformation")); - assertNotNull(outFS.createDocumentInputStream("\005DocumentSummaryInformation")); + assertNotNull(outFS.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME)); + assertNotNull(outFS.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME)); outFS.close(); } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java index a53173a8a5..4fe1c4d051 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java @@ -59,94 +59,90 @@ import org.junit.Test; public class TestDocumentEncryption { POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); - @Before - @After // also afterwards to not affect other tests running in the same JVM - public void resetPassword() { - Biff8EncryptionKey.setCurrentUserPassword(null); - } - @Test public void cryptoAPIDecryptionOther() throws Exception { - Biff8EncryptionKey.setCurrentUserPassword("hello"); String encPpts[] = { "Password_Protected-56-hello.ppt", "Password_Protected-hello.ppt", "Password_Protected-np-hello.ppt", }; - for (String pptFile : encPpts) { - try { - NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); - HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs); - new HSLFSlideShow(hss).close(); - fs.close(); - } catch (EncryptedPowerPointFileException e) { - fail(pptFile+" can't be decrypted"); + Biff8EncryptionKey.setCurrentUserPassword("hello"); + try { + for (String pptFile : encPpts) { + try (NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); + HSLFSlideShow ppt = new HSLFSlideShow(fs)) { + assertTrue(ppt.getSlides().size() > 0); + } catch (EncryptedPowerPointFileException e) { + fail(pptFile + " can't be decrypted"); + } } + } finally { + Biff8EncryptionKey.setCurrentUserPassword(null); } - // password is reset in @After } @Test public void cryptoAPIChangeKeySize() throws Exception { String pptFile = "cryptoapi-proc2356.ppt"; Biff8EncryptionKey.setCurrentUserPassword("crypto"); - NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); - HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs); - // need to cache data (i.e. read all data) before changing the key size - List<HSLFPictureData> picsExpected = hss.getPictureData(); - hss.getDocumentSummaryInformation(); - DocumentEncryptionAtom documentEncryptionAtom = hss.getDocumentEncryptionAtom(); - assertNotNull(documentEncryptionAtom); - EncryptionInfo ei = documentEncryptionAtom.getEncryptionInfo(); - ((CryptoAPIEncryptionHeader) ei.getHeader()).setKeySize(0x78); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - hss.write(bos); - hss.close(); - fs.close(); - - fs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray())); - hss = new HSLFSlideShowImpl(fs); - List<HSLFPictureData> picsActual = hss.getPictureData(); - - assertEquals(picsExpected.size(), picsActual.size()); - for (int i = 0; i < picsExpected.size(); i++) { - assertArrayEquals(picsExpected.get(i).getRawData(), picsActual.get(i).getRawData()); + try (NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); + HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs)) { + // need to cache data (i.e. read all data) before changing the key size + List<HSLFPictureData> picsExpected = hss.getPictureData(); + hss.getDocumentSummaryInformation(); + DocumentEncryptionAtom documentEncryptionAtom = hss.getDocumentEncryptionAtom(); + assertNotNull(documentEncryptionAtom); + EncryptionInfo ei = documentEncryptionAtom.getEncryptionInfo(); + ((CryptoAPIEncryptionHeader) ei.getHeader()).setKeySize(0x78); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + hss.write(bos); + + try (NPOIFSFileSystem fs2 = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray())); + HSLFSlideShowImpl hss2 = new HSLFSlideShowImpl(fs2)) { + List<HSLFPictureData> picsActual = hss2.getPictureData(); + + assertEquals(picsExpected.size(), picsActual.size()); + for (int i = 0; i < picsExpected.size(); i++) { + assertArrayEquals(picsExpected.get(i).getRawData(), picsActual.get(i).getRawData()); + } + } + } finally { + Biff8EncryptionKey.setCurrentUserPassword(null); } - hss.close(); - fs.close(); - // password is reset in @After } @Test public void cryptoAPIEncryption() throws Exception { /* documents with multiple edits need to be normalized for encryption */ String pptFile = "57272_corrupted_usereditatom.ppt"; - NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); - HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs); - hss.normalizeRecords(); - - // normalized ppt - ByteArrayOutputStream expected = new ByteArrayOutputStream(); - hss.write(expected); - - // encrypted - Biff8EncryptionKey.setCurrentUserPassword("hello"); ByteArrayOutputStream encrypted = new ByteArrayOutputStream(); - hss.write(encrypted); - hss.close(); - fs.close(); - - // decrypted - ByteArrayInputStream bis = new ByteArrayInputStream(encrypted.toByteArray()); - fs = new NPOIFSFileSystem(bis); - hss = new HSLFSlideShowImpl(fs); - Biff8EncryptionKey.setCurrentUserPassword(null); + ByteArrayOutputStream expected = new ByteArrayOutputStream(); ByteArrayOutputStream actual = new ByteArrayOutputStream(); - hss.write(actual); - hss.close(); - fs.close(); + try { + try (NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true); + HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs)) { + hss.normalizeRecords(); + + // normalized ppt + hss.write(expected); + + // encrypted + Biff8EncryptionKey.setCurrentUserPassword("hello"); + hss.write(encrypted); + } + + // decrypted + ByteArrayInputStream bis = new ByteArrayInputStream(encrypted.toByteArray()); + try (NPOIFSFileSystem fs = new NPOIFSFileSystem(bis); + HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs)) { + Biff8EncryptionKey.setCurrentUserPassword(null); + hss.write(actual); + } + } finally { + Biff8EncryptionKey.setCurrentUserPassword(null); + } assertArrayEquals(expected.toByteArray(), actual.toByteArray()); } @@ -156,49 +152,49 @@ public class TestDocumentEncryption { // taken from a msdn blog: // http://blogs.msdn.com/b/openspecification/archive/2009/05/08/dominic-salemno.aspx Biff8EncryptionKey.setCurrentUserPassword("crypto"); - NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile("cryptoapi-proc2356.ppt")); - HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs); - HSLFSlideShow ss = new HSLFSlideShow(hss); - - HSLFSlide slide = ss.getSlides().get(0); - String rawText = HSLFTextParagraph.getRawText(slide.getTextParagraphs().get(0)); - assertEquals("Dominic Salemno", rawText); - - String picCmp[][] = { - {"0", "nKsDTKqxTCR8LFkVVWlP9GSTvZ0="}, - {"95163", "SuNOR+9V1UVYZIoeD65l3VTaLoc="}, - {"100864", "Ql3IGrr4bNq07ZTp5iPg7b+pva8="}, - {"714114", "8pdst9NjBGSfWezSZE8+aVhIRe0="}, - {"723752", "go6xqW7lvkCtlOO5tYLiMfb4oxw="}, - {"770128", "gZUM8YqRNL5kGNfyyYvEEernvCc="}, - {"957958", "CNU2iiqUFAnk3TDXsXV1ihH9eRM="}, - }; + try (NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile("cryptoapi-proc2356.ppt")); + HSLFSlideShow ss = new HSLFSlideShow(fs)) { + + HSLFSlide slide = ss.getSlides().get(0); + String rawText = HSLFTextParagraph.getRawText(slide.getTextParagraphs().get(0)); + assertEquals("Dominic Salemno", rawText); + + String picCmp[][] = { + {"0", "nKsDTKqxTCR8LFkVVWlP9GSTvZ0="}, + {"95163", "SuNOR+9V1UVYZIoeD65l3VTaLoc="}, + {"100864", "Ql3IGrr4bNq07ZTp5iPg7b+pva8="}, + {"714114", "8pdst9NjBGSfWezSZE8+aVhIRe0="}, + {"723752", "go6xqW7lvkCtlOO5tYLiMfb4oxw="}, + {"770128", "gZUM8YqRNL5kGNfyyYvEEernvCc="}, + {"957958", "CNU2iiqUFAnk3TDXsXV1ihH9eRM="}, + }; + + MessageDigest md = CryptoFunctions.getMessageDigest(HashAlgorithm.sha1); + List<HSLFPictureData> pd = ss.getSlideShowImpl().getPictureData(); + int i = 0; + for (HSLFPictureData p : pd) { + byte hash[] = md.digest(p.getData()); + assertEquals(Integer.parseInt(picCmp[i][0]), p.getOffset()); + assertEquals(picCmp[i][1], Base64.encodeBase64String(hash)); + i++; + } - MessageDigest md = CryptoFunctions.getMessageDigest(HashAlgorithm.sha1); - List<HSLFPictureData> pd = hss.getPictureData(); - int i = 0; - for (HSLFPictureData p : pd) { - byte hash[] = md.digest(p.getData()); - assertEquals(Integer.parseInt(picCmp[i][0]), p.getOffset()); - assertEquals(picCmp[i][1], Base64.encodeBase64String(hash)); - i++; + DocumentEncryptionAtom dea = ss.getSlideShowImpl().getDocumentEncryptionAtom(); + assertNotNull(dea); + + CryptoAPIDecryptor dec = (CryptoAPIDecryptor) dea.getEncryptionInfo().getDecryptor(); + try (POIFSFileSystem fs2 = dec.getSummaryEntries(fs.getRoot(), "EncryptedSummary")) { + PropertySet ps = PropertySetFactory.create(fs2.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME); + assertNotNull(ps); + assertTrue(ps.isSummaryInformation()); + assertEquals("RC4 CryptoAPI Encryption", ps.getProperties()[1].getValue()); + ps = PropertySetFactory.create(fs2.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME); + assertNotNull(ps); + assertTrue(ps.isDocumentSummaryInformation()); + assertEquals("On-screen Show (4:3)", ps.getProperties()[1].getValue()); + } + } finally { + Biff8EncryptionKey.setCurrentUserPassword(null); } - - DocumentEncryptionAtom dea = hss.getDocumentEncryptionAtom(); - assertNotNull(dea); - - POIFSFileSystem fs2 = ((CryptoAPIDecryptor) dea.getEncryptionInfo().getDecryptor()).getSummaryEntries(fs.getRoot(), "EncryptedSummary"); - PropertySet ps = PropertySetFactory.create(fs2.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME); - assertNotNull(ps); - assertTrue(ps.isSummaryInformation()); - assertEquals("RC4 CryptoAPI Encryption", ps.getProperties()[1].getValue()); - ps = PropertySetFactory.create(fs2.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME); - assertNotNull(ps); - assertTrue(ps.isDocumentSummaryInformation()); - assertEquals("On-screen Show (4:3)", ps.getProperties()[1].getValue()); - ss.close(); - fs.close(); - fs2.close(); - // password is reset in @After } } |