|
39 | 39 | import org.apache.fesod.sheet.testkit.models.SimpleData; |
40 | 40 | import org.apache.fesod.sheet.testkit.params.ExcelFormatSource; |
41 | 41 | import org.apache.fesod.sheet.write.builder.ExcelWriterBuilder; |
| 42 | +import org.apache.poi.EncryptedDocumentException; |
42 | 43 | import org.junit.jupiter.api.Assertions; |
43 | 44 | import org.junit.jupiter.api.Tag; |
| 45 | +import org.junit.jupiter.api.Test; |
44 | 46 | import org.junit.jupiter.params.ParameterizedTest; |
45 | 47 |
|
46 | 48 | /** |
@@ -104,4 +106,36 @@ private void readAndWrite( |
104 | 106 | Assertions.assertEquals(10, dataList.size()); |
105 | 107 | Assertions.assertNotNull(dataList.get(0).getName()); |
106 | 108 | } |
| 109 | + |
| 110 | + /** |
| 111 | + * Verifies that an XLS file written with a password is actually encrypted at the BIFF8 record level, |
| 112 | + * not merely flagged as write-protected. Without the correct password, reading the file content |
| 113 | + * must fail. |
| 114 | + */ |
| 115 | + @Test |
| 116 | + void xlsPasswordWrite_isActuallyEncrypted() throws Exception { |
| 117 | + File file = createTempFile("enc-verify", ExcelFormat.XLS); |
| 118 | + |
| 119 | + // Write an encrypted XLS file |
| 120 | + FesodSheet.write(file, SimpleData.class) |
| 121 | + .excelType(ExcelTypeEnum.XLS) |
| 122 | + .password(PASSWORD) |
| 123 | + .sheet() |
| 124 | + .doWrite(TestDataBuilder.simpleData(10)); |
| 125 | + |
| 126 | + // Reading without the password must fail because the content is BIFF8-encrypted |
| 127 | + Assertions.assertThrows(EncryptedDocumentException.class, () -> FesodSheet.read( |
| 128 | + file, SimpleData.class, new CollectingReadListener<SimpleData>()) |
| 129 | + .excelType(ExcelTypeEnum.XLS) |
| 130 | + .sheet() |
| 131 | + .doReadSync()); |
| 132 | + |
| 133 | + // Reading with the correct password must succeed |
| 134 | + List<SimpleData> dataList = FesodSheet.read(file, SimpleData.class, new CollectingReadListener<SimpleData>()) |
| 135 | + .excelType(ExcelTypeEnum.XLS) |
| 136 | + .password(PASSWORD) |
| 137 | + .sheet() |
| 138 | + .doReadSync(); |
| 139 | + Assertions.assertEquals(10, dataList.size()); |
| 140 | + } |
107 | 141 | } |
0 commit comments