use of org.apache.tika.exception.EncryptedDocumentException in project tika by apache.
the class Seven7ParserTest method testPasswordProtected.
@Test
public void testPasswordProtected() throws Exception {
Parser parser = new AutoDetectParser();
ContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
// No password, will fail with EncryptedDocumentException
boolean ex = false;
try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
parser.parse(stream, handler, metadata, recursingContext);
fail("Shouldn't be able to read a password protected 7z without the password");
} catch (EncryptedDocumentException e) {
// Good
ex = true;
}
assertTrue("test no password", ex);
ex = false;
// Wrong password currently silently gives no content
// Ideally we'd like Commons Compress to give an error, but it doesn't...
recursingContext.set(PasswordProvider.class, new PasswordProvider() {
@Override
public String getPassword(Metadata metadata) {
return "wrong";
}
});
handler = new BodyContentHandler();
try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
parser.parse(stream, handler, metadata, recursingContext);
fail("Shouldn't be able to read a password protected 7z with wrong password");
} catch (TikaException e) {
//if JCE is installed, the cause will be: Caused by: org.tukaani.xz.CorruptedInputException: Compressed data is corrupt
//if JCE is not installed, the message will include
// "(do you have the JCE Unlimited Strength Jurisdiction Policy Files installed?")
ex = true;
}
assertTrue("TikaException for bad password", ex);
// Will be empty
assertEquals("", handler.toString());
ex = false;
// Right password works fine if JCE Unlimited Strength has been installed!!!
if (isStrongCryptoAvailable()) {
recursingContext.set(PasswordProvider.class, new PasswordProvider() {
@Override
public String getPassword(Metadata metadata) {
return "Tika";
}
});
handler = new BodyContentHandler();
try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
parser.parse(stream, handler, metadata, recursingContext);
}
assertEquals(TYPE_7ZIP.toString(), metadata.get(Metadata.CONTENT_TYPE));
String content = handler.toString();
// Should get filename
assertContains("text.txt", content);
// Should get contents from the text file in the 7z file
assertContains("TEST DATA FOR TIKA.", content);
assertContains("This is text inside an encrypted 7zip (7z) file.", content);
assertContains("It should be processed by Tika just fine!", content);
assertContains("TIKA-1521", content);
} else {
//if jce is not installed, test for IOException wrapped in TikaException
boolean ioe = false;
recursingContext.set(PasswordProvider.class, new PasswordProvider() {
@Override
public String getPassword(Metadata metadata) {
return "Tika";
}
});
handler = new BodyContentHandler();
try (InputStream stream = Seven7ParserTest.class.getResourceAsStream("/test-documents/test7Z_protected_passTika.7z")) {
parser.parse(stream, handler, metadata, recursingContext);
} catch (TikaException e) {
ioe = true;
}
assertTrue("IOException because JCE was not installed", ioe);
}
}
Aggregations