You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestDecryptor.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.crypt;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertTrue;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.security.GeneralSecurityException;
  24. import java.util.zip.ZipEntry;
  25. import java.util.zip.ZipInputStream;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  28. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  29. import org.apache.poi.util.IOUtils;
  30. import org.junit.Test;
  31. /**
  32. * @author Maxim Valyanskiy
  33. * @author Gary King
  34. */
  35. public class TestDecryptor {
  36. @Test
  37. public void passwordVerification() throws IOException, GeneralSecurityException {
  38. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  39. EncryptionInfo info = new EncryptionInfo(fs);
  40. Decryptor d = Decryptor.getInstance(info);
  41. assertTrue(d.verifyPassword(Decryptor.DEFAULT_PASSWORD));
  42. }
  43. @Test
  44. public void decrypt() throws IOException, GeneralSecurityException {
  45. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  46. EncryptionInfo info = new EncryptionInfo(fs);
  47. Decryptor d = Decryptor.getInstance(info);
  48. d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
  49. zipOk(fs, d);
  50. }
  51. @Test
  52. public void agile() throws IOException, GeneralSecurityException {
  53. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"));
  54. EncryptionInfo info = new EncryptionInfo(fs);
  55. assertTrue(info.getVersionMajor() == 4 && info.getVersionMinor() == 4);
  56. Decryptor d = Decryptor.getInstance(info);
  57. assertTrue(d.verifyPassword(Decryptor.DEFAULT_PASSWORD));
  58. zipOk(fs, d);
  59. }
  60. private void zipOk(POIFSFileSystem fs, Decryptor d) throws IOException, GeneralSecurityException {
  61. ZipInputStream zin = new ZipInputStream(d.getDataStream(fs));
  62. while (true) {
  63. ZipEntry entry = zin.getNextEntry();
  64. if (entry==null) {
  65. break;
  66. }
  67. while (zin.available()>0) {
  68. zin.skip(zin.available());
  69. }
  70. }
  71. }
  72. @Test
  73. public void dataLength() throws Exception {
  74. POIFSFileSystem fs = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"));
  75. EncryptionInfo info = new EncryptionInfo(fs);
  76. Decryptor d = Decryptor.getInstance(info);
  77. d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
  78. InputStream is = d.getDataStream(fs);
  79. long len = d.getLength();
  80. assertEquals(12810, len);
  81. byte[] buf = new byte[(int)len];
  82. is.read(buf);
  83. ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(buf));
  84. while (true) {
  85. ZipEntry entry = zin.getNextEntry();
  86. if (entry==null) {
  87. break;
  88. }
  89. while (zin.available()>0) {
  90. zin.skip(zin.available());
  91. }
  92. }
  93. }
  94. @Test
  95. public void bug57080() throws Exception {
  96. // the test file contains a wrong ole entry size, produced by extenxls
  97. // the fix limits the available size and tries to read all entries
  98. File f = POIDataSamples.getPOIFSInstance().getFile("extenxls_pwd123.xlsx");
  99. NPOIFSFileSystem fs = new NPOIFSFileSystem(f, true);
  100. EncryptionInfo info = new EncryptionInfo(fs);
  101. Decryptor d = Decryptor.getInstance(info);
  102. d.verifyPassword("pwd123");
  103. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  104. ZipInputStream zis = new ZipInputStream(d.getDataStream(fs));
  105. ZipEntry ze;
  106. while ((ze = zis.getNextEntry()) != null) {
  107. bos.reset();
  108. IOUtils.copy(zis, bos);
  109. assertEquals(ze.getSize(), bos.size());
  110. }
  111. zis.close();
  112. fs.close();
  113. }
  114. }