use of org.apache.poi.EncryptedDocumentException in project poi by apache.
the class TestWorkbookFactory method testCreateWithPasswordFromStream.
/**
* Check that the overloaded stream methods which take passwords work properly
*/
@Test
public void testCreateWithPasswordFromStream() throws Exception {
Workbook wb;
// Unprotected, no password given, opens normally
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls), null);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls, wb);
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx), null);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx, wb);
// Unprotected, wrong password, opens normally
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls), "wrong");
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls, wb);
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx), "wrong");
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx, wb);
// Protected, correct password, opens fine
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), xls_prot[1]);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls_prot[0], wb);
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), xlsx_prot[1]);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
// Protected, wrong password, throws Exception
try {
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), "wrong");
assertCloseDoesNotModifyFile(xls_prot[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
}
try {
wb = WorkbookFactory.create(HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), "wrong");
assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
}
}
use of org.apache.poi.EncryptedDocumentException in project poi by apache.
the class TestWorkbookFactory method testCreateWithPasswordFromFile.
/**
* Check that the overloaded file methods which take passwords work properly
*/
@Test
public void testCreateWithPasswordFromFile() throws Exception {
Workbook wb;
// Unprotected, no password given, opens normally
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls), null);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls, wb);
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx), null);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx, wb);
// Unprotected, wrong password, opens normally
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls), "wrong");
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls, wb);
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx), "wrong");
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertCloseDoesNotModifyFile(xlsx, wb);
// Protected, correct password, opens fine
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls_prot[0]), xls_prot[1]);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
assertCloseDoesNotModifyFile(xls_prot[0], wb);
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), xlsx_prot[1]);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertTrue(wb.getNumberOfSheets() > 0);
assertNotNull(wb.getSheetAt(0));
assertNotNull(wb.getSheetAt(0).getRow(0));
assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
// Protected, wrong password, throws Exception
try {
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xls_prot[0]), "wrong");
assertCloseDoesNotModifyFile(xls_prot[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
// expected here
}
try {
wb = WorkbookFactory.create(HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), "wrong");
assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
// expected here
}
}
use of org.apache.poi.EncryptedDocumentException in project poi by apache.
the class TestOldExcelExtractor method testEncryptionException.
@Test
public void testEncryptionException() throws IOException {
//test file derives from Common Crawl
File file = HSSFTestDataSamples.getSampleFile("60284.xls");
OldExcelExtractor ex = new OldExcelExtractor(file);
assertEquals(5, ex.getBiffVersion());
assertEquals(5, ex.getFileType());
try {
ex.getText();
fail();
} catch (EncryptedDocumentException e) {
assertTrue("correct exception thrown", true);
}
ex.close();
}
use of org.apache.poi.EncryptedDocumentException in project poi by apache.
the class TestHSSFEventFactory method testWithPasswordProtectedWorkbooks.
public void testWithPasswordProtectedWorkbooks() throws Exception {
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
// Without a password, can't be read
Biff8EncryptionKey.setCurrentUserPassword(null);
POIFSFileSystem fs = new POIFSFileSystem(openSample("xor-encryption-abc.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
try {
factory.processWorkbookEvents(req, fs);
fail("Shouldn't be able to process protected workbook without the password");
} catch (EncryptedDocumentException e) {
}
// With the password, is properly processed
Biff8EncryptionKey.setCurrentUserPassword("abc");
req = new HSSFRequest();
mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
factory.processWorkbookEvents(req, fs);
// Check we got the sheet and the contents
Record[] recs = mockListen.getRecords();
assertTrue(recs.length > 50);
// Has one sheet, with values 1,2,3 in column A rows 1-3
boolean hasSheet = false, hasA1 = false, hasA2 = false, hasA3 = false;
for (Record r : recs) {
if (r instanceof BoundSheetRecord) {
BoundSheetRecord bsr = (BoundSheetRecord) r;
assertEquals("Sheet1", bsr.getSheetname());
hasSheet = true;
}
if (r instanceof NumberRecord) {
NumberRecord nr = (NumberRecord) r;
if (nr.getColumn() == 0 && nr.getRow() == 0) {
assertEquals(1, (int) nr.getValue());
hasA1 = true;
}
if (nr.getColumn() == 0 && nr.getRow() == 1) {
assertEquals(2, (int) nr.getValue());
hasA2 = true;
}
if (nr.getColumn() == 0 && nr.getRow() == 2) {
assertEquals(3, (int) nr.getValue());
hasA3 = true;
}
}
}
assertTrue("Sheet record not found", hasSheet);
assertTrue("Numeric record for A1 not found", hasA1);
assertTrue("Numeric record for A2 not found", hasA2);
assertTrue("Numeric record for A3 not found", hasA3);
}
use of org.apache.poi.EncryptedDocumentException in project poi by apache.
the class OldExcelExtractor method getText.
/**
* Retrieves the text contents of the file, as best we can
* for these old file formats
*
* @return the text contents of the file
*/
public String getText() {
StringBuffer text = new StringBuffer();
// To track formats and encodings
CodepageRecord codepage = null;
// Process each record in turn, looking for interesting ones
while (ris.hasNextRecord()) {
int sid = ris.getNextSid();
ris.nextRecord();
switch(sid) {
case FILE_PASS_RECORD_SID:
throw new EncryptedDocumentException("Encryption not supported for Old Excel files");
case OldSheetRecord.sid:
OldSheetRecord shr = new OldSheetRecord(ris);
shr.setCodePage(codepage);
text.append("Sheet: ");
text.append(shr.getSheetname());
text.append('\n');
break;
case OldLabelRecord.biff2_sid:
case OldLabelRecord.biff345_sid:
OldLabelRecord lr = new OldLabelRecord(ris);
lr.setCodePage(codepage);
text.append(lr.getValue());
text.append('\n');
break;
case OldStringRecord.biff2_sid:
case OldStringRecord.biff345_sid:
OldStringRecord sr = new OldStringRecord(ris);
sr.setCodePage(codepage);
text.append(sr.getString());
text.append('\n');
break;
case NumberRecord.sid:
NumberRecord nr = new NumberRecord(ris);
handleNumericCell(text, nr.getValue());
break;
case OldFormulaRecord.biff2_sid:
case OldFormulaRecord.biff3_sid:
case OldFormulaRecord.biff4_sid:
// Biff 2 and 5+ share the same SID, due to a bug...
if (biffVersion == 5) {
FormulaRecord fr = new FormulaRecord(ris);
if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
handleNumericCell(text, fr.getValue());
}
} else {
OldFormulaRecord fr = new OldFormulaRecord(ris);
if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
handleNumericCell(text, fr.getValue());
}
}
break;
case RKRecord.sid:
RKRecord rr = new RKRecord(ris);
handleNumericCell(text, rr.getRKNumber());
break;
case CodepageRecord.sid:
codepage = new CodepageRecord(ris);
break;
default:
ris.readFully(new byte[ris.remaining()]);
}
}
close();
ris = null;
return text.toString();
}
Aggregations