Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TestWorkbookProtection.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.xssf;
  16. import static org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook;
  17. import static org.apache.poi.xssf.XSSFTestDataSamples.writeOutAndReadBack;
  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertFalse;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import static org.junit.jupiter.api.Assertions.assertTrue;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.OutputStream;
  26. import org.apache.poi.openxml4j.opc.OPCPackage;
  27. import org.apache.poi.poifs.crypt.CryptoFunctions;
  28. import org.apache.poi.poifs.crypt.Decryptor;
  29. import org.apache.poi.poifs.crypt.EncryptionInfo;
  30. import org.apache.poi.poifs.crypt.EncryptionMode;
  31. import org.apache.poi.poifs.crypt.Encryptor;
  32. import org.apache.poi.poifs.crypt.HashAlgorithm;
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  34. import org.apache.poi.util.IOUtils;
  35. import org.apache.poi.util.TempFile;
  36. import org.apache.poi.xssf.usermodel.XSSFCell;
  37. import org.apache.poi.xssf.usermodel.XSSFRow;
  38. import org.apache.poi.xssf.usermodel.XSSFSheet;
  39. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  40. import org.junit.jupiter.api.Test;
  41. class TestWorkbookProtection {
  42. @Test
  43. void workbookAndRevisionPassword() throws Exception {
  44. String password = "test";
  45. // validate password with an actual office file (Excel 2010)
  46. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection-workbook_password_user_range-2010.xlsx")) {
  47. assertTrue(workbook.validateWorkbookPassword(password));
  48. }
  49. // validate with another office file (Excel 2013)
  50. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection-workbook_password-2013.xlsx")){
  51. assertTrue(workbook.validateWorkbookPassword(password));
  52. }
  53. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
  54. // setting a null password shouldn't introduce the protection element
  55. workbook.setWorkbookPassword(null, null);
  56. assertNull(workbook.getCTWorkbook().getWorkbookProtection());
  57. // compare the hashes
  58. workbook.setWorkbookPassword(password, null);
  59. int hashVal = CryptoFunctions.createXorVerifier1(password);
  60. int actualVal = Integer.parseInt(workbook.getCTWorkbook().getWorkbookProtection().xgetWorkbookPassword().getStringValue(), 16);
  61. assertEquals(hashVal, actualVal);
  62. assertTrue(workbook.validateWorkbookPassword(password));
  63. // removing the password again
  64. workbook.setWorkbookPassword(null, null);
  65. assertFalse(workbook.getCTWorkbook().getWorkbookProtection().isSetWorkbookPassword());
  66. // removing the whole protection structure
  67. workbook.unLock();
  68. assertNull(workbook.getCTWorkbook().getWorkbookProtection());
  69. // setting a null password shouldn't introduce the protection element
  70. workbook.setRevisionsPassword(null, null);
  71. assertNull(workbook.getCTWorkbook().getWorkbookProtection());
  72. // compare the hashes
  73. password = "T\u0400ST\u0100passwordWhichIsLongerThan15Chars";
  74. workbook.setRevisionsPassword(password, null);
  75. hashVal = CryptoFunctions.createXorVerifier1(password);
  76. actualVal = Integer.parseInt(workbook.getCTWorkbook().getWorkbookProtection().xgetRevisionsPassword().getStringValue(), 16);
  77. assertEquals(hashVal, actualVal);
  78. assertTrue(workbook.validateRevisionsPassword(password));
  79. }
  80. }
  81. @Test
  82. void shouldReadWorkbookProtection() throws Exception {
  83. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
  84. assertFalse(workbook.isStructureLocked());
  85. assertFalse(workbook.isWindowsLocked());
  86. assertFalse(workbook.isRevisionLocked());
  87. }
  88. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_workbook_structure_protected.xlsx")) {
  89. assertTrue(workbook.isStructureLocked());
  90. assertFalse(workbook.isWindowsLocked());
  91. assertFalse(workbook.isRevisionLocked());
  92. }
  93. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_workbook_windows_protected.xlsx")) {
  94. assertTrue(workbook.isWindowsLocked());
  95. assertFalse(workbook.isStructureLocked());
  96. assertFalse(workbook.isRevisionLocked());
  97. }
  98. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_workbook_revision_protected.xlsx")) {
  99. assertTrue(workbook.isRevisionLocked());
  100. assertFalse(workbook.isWindowsLocked());
  101. assertFalse(workbook.isStructureLocked());
  102. }
  103. }
  104. @Test
  105. void shouldWriteStructureLock() throws Exception {
  106. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
  107. assertFalse(workbook.isStructureLocked());
  108. workbook.lockStructure();
  109. assertTrue(workbook.isStructureLocked());
  110. workbook.unLockStructure();
  111. assertFalse(workbook.isStructureLocked());
  112. }
  113. }
  114. @Test
  115. void shouldWriteWindowsLock() throws Exception {
  116. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
  117. assertFalse(workbook.isWindowsLocked());
  118. workbook.lockWindows();
  119. assertTrue(workbook.isWindowsLocked());
  120. workbook.unLockWindows();
  121. assertFalse(workbook.isWindowsLocked());
  122. }
  123. }
  124. @Test
  125. void shouldWriteRevisionLock() throws Exception {
  126. try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
  127. assertFalse(workbook.isRevisionLocked());
  128. workbook.lockRevision();
  129. assertTrue(workbook.isRevisionLocked());
  130. workbook.unLockRevision();
  131. assertFalse(workbook.isRevisionLocked());
  132. }
  133. }
  134. @SuppressWarnings("resource")
  135. @Test
  136. void testHashPassword() throws Exception {
  137. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  138. wb.lockRevision();
  139. wb.setRevisionsPassword("test", HashAlgorithm.sha1);
  140. try (XSSFWorkbook wbBack = writeOutAndReadBack(wb)) {
  141. assertTrue(wbBack.isRevisionLocked());
  142. assertTrue(wbBack.validateRevisionsPassword("test"));
  143. }
  144. }
  145. }
  146. @SuppressWarnings("resource")
  147. @Test
  148. void testIntegration() throws Exception {
  149. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  150. wb.createSheet("Testing purpose sheet");
  151. assertFalse(wb.isRevisionLocked());
  152. wb.lockRevision();
  153. wb.setRevisionsPassword("test", null);
  154. try (XSSFWorkbook wbBack = writeOutAndReadBack(wb)) {
  155. assertTrue(wbBack.isRevisionLocked());
  156. assertTrue(wbBack.validateRevisionsPassword("test"));
  157. }
  158. }
  159. }
  160. @Test
  161. void testEncryptDecrypt() throws Exception {
  162. final String password = "abc123";
  163. final String sheetName = "TestSheet1";
  164. final String cellValue = "customZipEntrySource";
  165. try (XSSFWorkbook workbook = new XSSFWorkbook()) {
  166. XSSFSheet sheet1 = workbook.createSheet(sheetName);
  167. XSSFRow row1 = sheet1.createRow(1);
  168. XSSFCell cell1 = row1.createCell(1);
  169. cell1.setCellValue(cellValue);
  170. File tf1 = TempFile.createTempFile("poitest", ".xlsx");
  171. FileOutputStream fos1 = new FileOutputStream(tf1);
  172. workbook.write(fos1);
  173. IOUtils.closeQuietly(fos1);
  174. POIFSFileSystem poiFileSystem = new POIFSFileSystem();
  175. EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
  176. Encryptor enc = encryptionInfo.getEncryptor();
  177. enc.confirmPassword(password);
  178. FileInputStream fis = new FileInputStream(tf1);
  179. OPCPackage opc = OPCPackage.open(fis);
  180. IOUtils.closeQuietly(fis);
  181. try {
  182. OutputStream os = enc.getDataStream(poiFileSystem);
  183. opc.save(os);
  184. IOUtils.closeQuietly(os);
  185. } finally {
  186. IOUtils.closeQuietly(opc);
  187. }
  188. assertTrue(tf1.delete());
  189. FileOutputStream fos2 = new FileOutputStream(tf1);
  190. poiFileSystem.writeFilesystem(fos2);
  191. IOUtils.closeQuietly(fos2);
  192. workbook.close();
  193. fis = new FileInputStream(tf1);
  194. POIFSFileSystem poiFileSystem2 = new POIFSFileSystem(fis);
  195. IOUtils.closeQuietly(fis);
  196. EncryptionInfo encryptionInfo2 = new EncryptionInfo(poiFileSystem2);
  197. Decryptor decryptor = encryptionInfo2.getDecryptor();
  198. decryptor.verifyPassword(password);
  199. XSSFWorkbook workbook2 = new XSSFWorkbook(decryptor.getDataStream(poiFileSystem2));
  200. workbook2.close();
  201. assertTrue(tf1.delete());
  202. }
  203. }
  204. }