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.

TestWorkbookProtection.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.Assert.assertEquals;
  19. import static org.junit.Assert.assertFalse;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import org.apache.poi.poifs.crypt.CryptoFunctions;
  23. import org.apache.poi.poifs.crypt.HashAlgorithm;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. import org.junit.Test;
  26. public class TestWorkbookProtection {
  27. @Test
  28. public void workbookAndRevisionPassword() throws Exception {
  29. XSSFWorkbook workbook;
  30. String password = "test";
  31. // validate password with an actual office file (Excel 2010)
  32. workbook = openSampleWorkbook("workbookProtection-workbook_password_user_range-2010.xlsx");
  33. assertTrue(workbook.validateWorkbookPassword(password));
  34. // validate with another office file (Excel 2013)
  35. workbook = openSampleWorkbook("workbookProtection-workbook_password-2013.xlsx");
  36. assertTrue(workbook.validateWorkbookPassword(password));
  37. workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx");
  38. // setting a null password shouldn't introduce the protection element
  39. workbook.setWorkbookPassword(null, null);
  40. assertNull(workbook.getCTWorkbook().getWorkbookProtection());
  41. // compare the hashes
  42. workbook.setWorkbookPassword(password, null);
  43. int hashVal = CryptoFunctions.createXorVerifier1(password);
  44. int actualVal = Integer.parseInt(workbook.getCTWorkbook().getWorkbookProtection().xgetWorkbookPassword().getStringValue(),16);
  45. assertEquals(hashVal, actualVal);
  46. assertTrue(workbook.validateWorkbookPassword(password));
  47. // removing the password again
  48. workbook.setWorkbookPassword(null, null);
  49. assertFalse(workbook.getCTWorkbook().getWorkbookProtection().isSetWorkbookPassword());
  50. // removing the whole protection structure
  51. workbook.unLock();
  52. assertNull(workbook.getCTWorkbook().getWorkbookProtection());
  53. // setting a null password shouldn't introduce the protection element
  54. workbook.setRevisionsPassword(null, null);
  55. assertNull(workbook.getCTWorkbook().getWorkbookProtection());
  56. // compare the hashes
  57. password = "T\u0400ST\u0100passwordWhichIsLongerThan15Chars";
  58. workbook.setRevisionsPassword(password, null);
  59. hashVal = CryptoFunctions.createXorVerifier1(password);
  60. actualVal = Integer.parseInt(workbook.getCTWorkbook().getWorkbookProtection().xgetRevisionsPassword().getStringValue(),16);
  61. assertEquals(hashVal, actualVal);
  62. assertTrue(workbook.validateRevisionsPassword(password));
  63. }
  64. @Test
  65. public void shouldReadWorkbookProtection() throws Exception {
  66. XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx");
  67. assertFalse(workbook.isStructureLocked());
  68. assertFalse(workbook.isWindowsLocked());
  69. assertFalse(workbook.isRevisionLocked());
  70. workbook = openSampleWorkbook("workbookProtection_workbook_structure_protected.xlsx");
  71. assertTrue(workbook.isStructureLocked());
  72. assertFalse(workbook.isWindowsLocked());
  73. assertFalse(workbook.isRevisionLocked());
  74. workbook = openSampleWorkbook("workbookProtection_workbook_windows_protected.xlsx");
  75. assertTrue(workbook.isWindowsLocked());
  76. assertFalse(workbook.isStructureLocked());
  77. assertFalse(workbook.isRevisionLocked());
  78. workbook = openSampleWorkbook("workbookProtection_workbook_revision_protected.xlsx");
  79. assertTrue(workbook.isRevisionLocked());
  80. assertFalse(workbook.isWindowsLocked());
  81. assertFalse(workbook.isStructureLocked());
  82. }
  83. @Test
  84. public void shouldWriteStructureLock() throws Exception {
  85. XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx");
  86. assertFalse(workbook.isStructureLocked());
  87. workbook.lockStructure();
  88. assertTrue(workbook.isStructureLocked());
  89. workbook.unLockStructure();
  90. assertFalse(workbook.isStructureLocked());
  91. }
  92. @Test
  93. public void shouldWriteWindowsLock() throws Exception {
  94. XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx");
  95. assertFalse(workbook.isWindowsLocked());
  96. workbook.lockWindows();
  97. assertTrue(workbook.isWindowsLocked());
  98. workbook.unLockWindows();
  99. assertFalse(workbook.isWindowsLocked());
  100. }
  101. @Test
  102. public void shouldWriteRevisionLock() throws Exception {
  103. XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx");
  104. assertFalse(workbook.isRevisionLocked());
  105. workbook.lockRevision();
  106. assertTrue(workbook.isRevisionLocked());
  107. workbook.unLockRevision();
  108. assertFalse(workbook.isRevisionLocked());
  109. }
  110. @SuppressWarnings("resource")
  111. @Test
  112. public void testHashPassword() throws Exception {
  113. XSSFWorkbook wb = new XSSFWorkbook();
  114. wb.lockRevision();
  115. wb.setRevisionsPassword("test", HashAlgorithm.sha1);
  116. wb = writeOutAndReadBack(wb);
  117. assertTrue(wb.isRevisionLocked());
  118. assertTrue(wb.validateRevisionsPassword("test"));
  119. }
  120. @SuppressWarnings("resource")
  121. @Test
  122. public void testIntegration() throws Exception {
  123. XSSFWorkbook wb = new XSSFWorkbook();
  124. wb.createSheet("Testing purpose sheet");
  125. assertFalse(wb.isRevisionLocked());
  126. wb.lockRevision();
  127. wb.setRevisionsPassword("test", null);
  128. wb = writeOutAndReadBack(wb);
  129. assertTrue(wb.isRevisionLocked());
  130. assertTrue(wb.validateRevisionsPassword("test"));
  131. }
  132. }