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.

TestXWPFBugs.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.xwpf;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertThrows;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. import static org.junit.jupiter.api.Assumptions.assumeTrue;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import javax.crypto.Cipher;
  25. import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
  26. import org.apache.commons.compress.archivers.zip.ZipFile;
  27. import org.apache.poi.POIDataSamples;
  28. import org.apache.poi.openxml4j.opc.OPCPackage;
  29. import org.apache.poi.poifs.crypt.CipherAlgorithm;
  30. import org.apache.poi.poifs.crypt.Decryptor;
  31. import org.apache.poi.poifs.crypt.EncryptionInfo;
  32. import org.apache.poi.poifs.crypt.HashAlgorithm;
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  34. import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
  35. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  36. import org.apache.xmlbeans.XmlException;
  37. import org.junit.jupiter.api.Test;
  38. import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
  39. class TestXWPFBugs {
  40. private static final POIDataSamples samples = POIDataSamples.getDocumentInstance();
  41. @Test()
  42. void truncatedDocx() {
  43. //started failing after uptake of commons-compress 1.21
  44. assertThrows(IOException.class, () -> {
  45. try (InputStream fis = samples.openResourceAsStream("truncated62886.docx");
  46. OPCPackage opc = OPCPackage.open(fis)) {
  47. //XWPFWordExtractor ext = new XWPFWordExtractor(opc)) {
  48. //assertNotNull(ext.getText());
  49. }
  50. });
  51. }
  52. /**
  53. * A word document that's encrypted with non-standard
  54. * Encryption options, and no cspname section. See bug 53475
  55. */
  56. @Test
  57. void bug53475NoCSPName() throws Exception {
  58. File file = samples.getFile("bug53475-password-is-solrcell.docx");
  59. POIFSFileSystem filesystem = new POIFSFileSystem(file, true);
  60. // Check the encryption details
  61. EncryptionInfo info = new EncryptionInfo(filesystem);
  62. assertEquals(128, info.getHeader().getKeySize());
  63. assertEquals(CipherAlgorithm.aes128, info.getHeader().getCipherAlgorithm());
  64. assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithm());
  65. // Check it can be decoded
  66. Decryptor d = Decryptor.getInstance(info);
  67. assertTrue(d.verifyPassword("solrcell"), "Unable to process: document is encrypted");
  68. // Check we can read the word document in that
  69. InputStream dataStream = d.getDataStream(filesystem);
  70. OPCPackage opc = OPCPackage.open(dataStream);
  71. XWPFDocument doc = new XWPFDocument(opc);
  72. XWPFWordExtractor ex = new XWPFWordExtractor(doc);
  73. String text = ex.getText();
  74. assertNotNull(text);
  75. assertEquals("This is password protected Word document.", text.trim());
  76. ex.close();
  77. filesystem.close();
  78. }
  79. /**
  80. * A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
  81. * but the key can be 128/192/256 bits
  82. */
  83. @Test
  84. void bug53475_aes256() throws Exception {
  85. int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
  86. assumeTrue(maxKeyLen == 0x7FFFFFFF, "Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256");
  87. File file = samples.getFile("bug53475-password-is-pass.docx");
  88. POIFSFileSystem filesystem = new POIFSFileSystem(file, true);
  89. // Check the encryption details
  90. EncryptionInfo info = new EncryptionInfo(filesystem);
  91. assertEquals(16, info.getHeader().getBlockSize());
  92. assertEquals(256, info.getHeader().getKeySize());
  93. assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
  94. assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithm());
  95. // Check it can be decoded
  96. Decryptor d = Decryptor.getInstance(info);
  97. assertTrue(d.verifyPassword("pass"), "Unable to process: document is encrypted");
  98. // Check we can read the word document in that
  99. InputStream dataStream = d.getDataStream(filesystem);
  100. OPCPackage opc = OPCPackage.open(dataStream);
  101. XWPFDocument doc = new XWPFDocument(opc);
  102. XWPFWordExtractor ex = new XWPFWordExtractor(doc);
  103. String text = ex.getText();
  104. assertNotNull(text);
  105. // I know ... a stupid typo, maybe next time ...
  106. assertEquals("The is a password protected document.", text.trim());
  107. ex.close();
  108. filesystem.close();
  109. }
  110. @Test
  111. void bug59058() throws IOException, XmlException {
  112. String[] files = {"bug57031.docx", "bug59058.docx"};
  113. for (String f : files) {
  114. ZipFile zf = new ZipFile(samples.getFile(f));
  115. ZipArchiveEntry entry = zf.getEntry("word/document.xml");
  116. DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry));
  117. assertNotNull(document);
  118. zf.close();
  119. }
  120. }
  121. }