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.

TestDocumentProtection.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import org.apache.poi.poifs.crypt.CryptoFunctions;
  24. import org.apache.poi.poifs.crypt.HashAlgorithm;
  25. import org.apache.poi.util.TempFile;
  26. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  27. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  28. import org.apache.poi.xwpf.usermodel.XWPFRun;
  29. import org.junit.jupiter.api.Test;
  30. class TestDocumentProtection {
  31. @Test
  32. void testShouldReadEnforcementProperties() throws IOException {
  33. XWPFDocument documentWithoutDocumentProtectionTag = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
  34. assertFalse(documentWithoutDocumentProtectionTag.isEnforcedReadonlyProtection());
  35. assertFalse(documentWithoutDocumentProtectionTag.isEnforcedFillingFormsProtection());
  36. assertFalse(documentWithoutDocumentProtectionTag.isEnforcedCommentsProtection());
  37. assertFalse(documentWithoutDocumentProtectionTag.isEnforcedTrackedChangesProtection());
  38. documentWithoutDocumentProtectionTag.close();
  39. XWPFDocument documentWithoutEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection_tag_existing.docx");
  40. assertFalse(documentWithoutEnforcement.isEnforcedReadonlyProtection());
  41. assertFalse(documentWithoutEnforcement.isEnforcedFillingFormsProtection());
  42. assertFalse(documentWithoutEnforcement.isEnforcedCommentsProtection());
  43. assertFalse(documentWithoutEnforcement.isEnforcedTrackedChangesProtection());
  44. documentWithoutEnforcement.close();
  45. XWPFDocument documentWithReadonlyEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_readonly_no_password.docx");
  46. assertTrue(documentWithReadonlyEnforcement.isEnforcedReadonlyProtection());
  47. assertFalse(documentWithReadonlyEnforcement.isEnforcedFillingFormsProtection());
  48. assertFalse(documentWithReadonlyEnforcement.isEnforcedCommentsProtection());
  49. assertFalse(documentWithReadonlyEnforcement.isEnforcedTrackedChangesProtection());
  50. documentWithReadonlyEnforcement.close();
  51. XWPFDocument documentWithFillingFormsEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_forms_no_password.docx");
  52. assertTrue(documentWithFillingFormsEnforcement.isEnforcedFillingFormsProtection());
  53. assertFalse(documentWithFillingFormsEnforcement.isEnforcedReadonlyProtection());
  54. assertFalse(documentWithFillingFormsEnforcement.isEnforcedCommentsProtection());
  55. assertFalse(documentWithFillingFormsEnforcement.isEnforcedTrackedChangesProtection());
  56. documentWithFillingFormsEnforcement.close();
  57. XWPFDocument documentWithCommentsEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_comments_no_password.docx");
  58. assertFalse(documentWithCommentsEnforcement.isEnforcedFillingFormsProtection());
  59. assertFalse(documentWithCommentsEnforcement.isEnforcedReadonlyProtection());
  60. assertTrue(documentWithCommentsEnforcement.isEnforcedCommentsProtection());
  61. assertFalse(documentWithCommentsEnforcement.isEnforcedTrackedChangesProtection());
  62. documentWithCommentsEnforcement.close();
  63. XWPFDocument documentWithTrackedChangesEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_trackedChanges_no_password.docx");
  64. assertFalse(documentWithTrackedChangesEnforcement.isEnforcedFillingFormsProtection());
  65. assertFalse(documentWithTrackedChangesEnforcement.isEnforcedReadonlyProtection());
  66. assertFalse(documentWithTrackedChangesEnforcement.isEnforcedCommentsProtection());
  67. assertTrue(documentWithTrackedChangesEnforcement.isEnforcedTrackedChangesProtection());
  68. documentWithTrackedChangesEnforcement.close();
  69. }
  70. @Test
  71. void testShouldEnforceForReadOnly() throws IOException {
  72. // XWPFDocument document = createDocumentFromSampleFile("test-data/document/documentProtection_no_protection.docx");
  73. XWPFDocument document = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
  74. assertFalse(document.isEnforcedReadonlyProtection());
  75. document.enforceReadonlyProtection();
  76. assertTrue(document.isEnforcedReadonlyProtection());
  77. document.close();
  78. }
  79. @Test
  80. void testShouldEnforceForFillingForms() throws IOException {
  81. XWPFDocument document = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
  82. assertFalse(document.isEnforcedFillingFormsProtection());
  83. document.enforceFillingFormsProtection();
  84. assertTrue(document.isEnforcedFillingFormsProtection());
  85. document.close();
  86. }
  87. @Test
  88. void testShouldEnforceForComments() throws IOException {
  89. XWPFDocument document = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
  90. assertFalse(document.isEnforcedCommentsProtection());
  91. document.enforceCommentsProtection();
  92. assertTrue(document.isEnforcedCommentsProtection());
  93. document.close();
  94. }
  95. @Test
  96. void testShouldEnforceForTrackedChanges() throws IOException {
  97. XWPFDocument document = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
  98. assertFalse(document.isEnforcedTrackedChangesProtection());
  99. document.enforceTrackedChangesProtection();
  100. assertTrue(document.isEnforcedTrackedChangesProtection());
  101. document.close();
  102. }
  103. @Test
  104. void testShouldUnsetEnforcement() throws IOException {
  105. XWPFDocument document = XWPFTestDataSamples.openSampleDocument("documentProtection_readonly_no_password.docx");
  106. assertTrue(document.isEnforcedReadonlyProtection());
  107. document.removeProtectionEnforcement();
  108. assertFalse(document.isEnforcedReadonlyProtection());
  109. document.close();
  110. }
  111. @Test
  112. void testIntegration() throws IOException {
  113. XWPFDocument doc1 = new XWPFDocument();
  114. XWPFParagraph p1 = doc1.createParagraph();
  115. XWPFRun r1 = p1.createRun();
  116. r1.setText("Lorem ipsum dolor sit amet.");
  117. doc1.enforceCommentsProtection();
  118. File tempFile = TempFile.createTempFile("documentProtectionFile", ".docx");
  119. FileOutputStream out = new FileOutputStream(tempFile);
  120. doc1.write(out);
  121. out.close();
  122. FileInputStream inputStream = new FileInputStream(tempFile);
  123. XWPFDocument doc2 = new XWPFDocument(inputStream);
  124. inputStream.close();
  125. assertTrue(doc2.isEnforcedCommentsProtection());
  126. doc2.close();
  127. doc1.close();
  128. }
  129. @Test
  130. void testUpdateFields() throws IOException {
  131. XWPFDocument doc = new XWPFDocument();
  132. assertFalse(doc.isEnforcedUpdateFields());
  133. doc.enforceUpdateFields();
  134. assertTrue(doc.isEnforcedUpdateFields());
  135. doc.close();
  136. }
  137. @Test
  138. void bug56076_read() throws IOException {
  139. // test legacy xored-hashed password
  140. assertEquals("64CEED7E", CryptoFunctions.xorHashPassword("Example"));
  141. // check leading 0
  142. assertEquals("0005CB00", CryptoFunctions.xorHashPassword("34579"));
  143. // test document write protection with password
  144. XWPFDocument document = XWPFTestDataSamples.openSampleDocument("bug56076.docx");
  145. boolean isValid = document.validateProtectionPassword("Example");
  146. assertTrue(isValid);
  147. document.close();
  148. }
  149. @Test
  150. void bug56076_write() throws IOException {
  151. // test document write protection with password
  152. XWPFDocument doc1 = new XWPFDocument();
  153. doc1.enforceCommentsProtection("Example", HashAlgorithm.sha512);
  154. XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc1);
  155. doc1.close();
  156. boolean isValid = doc2.validateProtectionPassword("Example");
  157. assertTrue(isValid);
  158. doc2.close();
  159. }
  160. }