aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/testcases/org/apache/poi/xssf/TestWorkbookProtection.java
blob: 11c4ff20d71fa422225433316516d47be2176f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.xssf;

import static org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook;
import static org.apache.poi.xssf.XSSFTestDataSamples.writeOutAndReadBack;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

class TestWorkbookProtection {

    @Test
    void workbookAndRevisionPassword() throws Exception {
        String password = "test";

        // validate password with an actual office file (Excel 2010)
        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection-workbook_password_user_range-2010.xlsx")) {
            assertTrue(workbook.validateWorkbookPassword(password));
        }

        // validate with another office file (Excel 2013)
        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection-workbook_password-2013.xlsx")){
            assertTrue(workbook.validateWorkbookPassword(password));
        }


        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
            // setting a null password shouldn't introduce the protection element
            workbook.setWorkbookPassword(null, null);
            assertNull(workbook.getCTWorkbook().getWorkbookProtection());

            // compare the hashes
            workbook.setWorkbookPassword(password, null);
            int hashVal = CryptoFunctions.createXorVerifier1(password);
            int actualVal = Integer.parseInt(workbook.getCTWorkbook().getWorkbookProtection().xgetWorkbookPassword().getStringValue(), 16);
            assertEquals(hashVal, actualVal);
            assertTrue(workbook.validateWorkbookPassword(password));

            // removing the password again
            workbook.setWorkbookPassword(null, null);
            assertFalse(workbook.getCTWorkbook().getWorkbookProtection().isSetWorkbookPassword());

            // removing the whole protection structure
            workbook.unLock();
            assertNull(workbook.getCTWorkbook().getWorkbookProtection());

            // setting a null password shouldn't introduce the protection element
            workbook.setRevisionsPassword(null, null);
            assertNull(workbook.getCTWorkbook().getWorkbookProtection());

            // compare the hashes
            password = "T\u0400ST\u0100passwordWhichIsLongerThan15Chars";
            workbook.setRevisionsPassword(password, null);
            hashVal = CryptoFunctions.createXorVerifier1(password);
            actualVal = Integer.parseInt(workbook.getCTWorkbook().getWorkbookProtection().xgetRevisionsPassword().getStringValue(), 16);
            assertEquals(hashVal, actualVal);
            assertTrue(workbook.validateRevisionsPassword(password));
        }
    }

    @Test
    void shouldReadWorkbookProtection() throws Exception {
        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
            assertFalse(workbook.isStructureLocked());
            assertFalse(workbook.isWindowsLocked());
            assertFalse(workbook.isRevisionLocked());
        }

        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_workbook_structure_protected.xlsx")) {
            assertTrue(workbook.isStructureLocked());
            assertFalse(workbook.isWindowsLocked());
            assertFalse(workbook.isRevisionLocked());
        }

        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_workbook_windows_protected.xlsx")) {
            assertTrue(workbook.isWindowsLocked());
            assertFalse(workbook.isStructureLocked());
            assertFalse(workbook.isRevisionLocked());
        }

        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_workbook_revision_protected.xlsx")) {
            assertTrue(workbook.isRevisionLocked());
            assertFalse(workbook.isWindowsLocked());
            assertFalse(workbook.isStructureLocked());
        }
    }

    @Test
    void shouldWriteStructureLock() throws Exception {
        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
            assertFalse(workbook.isStructureLocked());

            workbook.lockStructure();

            assertTrue(workbook.isStructureLocked());

            workbook.unLockStructure();

            assertFalse(workbook.isStructureLocked());
        }
    }

    @Test
    void shouldWriteWindowsLock() throws Exception {
        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
            assertFalse(workbook.isWindowsLocked());

            workbook.lockWindows();

            assertTrue(workbook.isWindowsLocked());

            workbook.unLockWindows();

            assertFalse(workbook.isWindowsLocked());
        }
    }

    @Test
    void shouldWriteRevisionLock() throws Exception {
        try (XSSFWorkbook workbook = openSampleWorkbook("workbookProtection_not_protected.xlsx")) {
            assertFalse(workbook.isRevisionLocked());

            workbook.lockRevision();

            assertTrue(workbook.isRevisionLocked());

            workbook.unLockRevision();

            assertFalse(workbook.isRevisionLocked());
        }
    }

    @SuppressWarnings("resource")
    @Test
    void testHashPassword() throws Exception {
        try (XSSFWorkbook wb = new XSSFWorkbook()) {
            wb.lockRevision();
            wb.setRevisionsPassword("test", HashAlgorithm.sha1);

            try (XSSFWorkbook wbBack = writeOutAndReadBack(wb)) {

                assertTrue(wbBack.isRevisionLocked());
                assertTrue(wbBack.validateRevisionsPassword("test"));
            }
        }
    }

    @SuppressWarnings("resource")
    @Test
    void testIntegration() throws Exception {
        try (XSSFWorkbook wb = new XSSFWorkbook()) {
            wb.createSheet("Testing purpose sheet");
            assertFalse(wb.isRevisionLocked());

            wb.lockRevision();
            wb.setRevisionsPassword("test", null);

            try (XSSFWorkbook wbBack = writeOutAndReadBack(wb)) {

                assertTrue(wbBack.isRevisionLocked());
                assertTrue(wbBack.validateRevisionsPassword("test"));
            }
        }
    }

    @Test
    void testEncryptDecrypt() throws Exception {
        final String password = "abc123";
        final String sheetName = "TestSheet1";
        final String cellValue = "customZipEntrySource";
        try (XSSFWorkbook workbook = new XSSFWorkbook()) {
            XSSFSheet sheet1 = workbook.createSheet(sheetName);
            XSSFRow row1 = sheet1.createRow(1);
            XSSFCell cell1 = row1.createCell(1);
            cell1.setCellValue(cellValue);
            File tf1 = TempFile.createTempFile("poitest", ".xlsx");
            FileOutputStream fos1 = new FileOutputStream(tf1);
            workbook.write(fos1);
            IOUtils.closeQuietly(fos1);
            POIFSFileSystem poiFileSystem = new POIFSFileSystem();
            EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
            Encryptor enc = encryptionInfo.getEncryptor();
            enc.confirmPassword(password);
            FileInputStream fis = new FileInputStream(tf1);
            OPCPackage opc = OPCPackage.open(fis);
            IOUtils.closeQuietly(fis);
            try {
                OutputStream os = enc.getDataStream(poiFileSystem);
                opc.save(os);
                IOUtils.closeQuietly(os);
            } finally {
                IOUtils.closeQuietly(opc);
            }
            assertTrue(tf1.delete());
            FileOutputStream fos2 = new FileOutputStream(tf1);
            poiFileSystem.writeFilesystem(fos2);
            IOUtils.closeQuietly(fos2);
            workbook.close();
            fis = new FileInputStream(tf1);
            POIFSFileSystem poiFileSystem2 = new POIFSFileSystem(fis);
            IOUtils.closeQuietly(fis);
            EncryptionInfo encryptionInfo2 = new EncryptionInfo(poiFileSystem2);
            Decryptor decryptor = encryptionInfo2.getDecryptor();
            decryptor.verifyPassword(password);
            XSSFWorkbook workbook2 = new XSSFWorkbook(decryptor.getDataStream(poiFileSystem2));
            workbook2.close();
            assertTrue(tf1.delete());
        }
    }
}