Skip to content

Commit 1c7081a

Browse files
Update encrypted pdf tests.
Check with no password, wrong user password, wrong admin password, one good one bad password, other way around, check with both good passwords
1 parent 15ba6f0 commit 1c7081a

1 file changed

Lines changed: 81 additions & 37 deletions

File tree

pdf2htmlEX/src/androidTest/java/com/viliussutkus89/android/pdf2htmlex/InstrumentedTests.java

Lines changed: 81 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* InstrumentedTests.java
33
*
4-
* Copyright (C) 2019 Vilius Sutkus'89
4+
* Copyright (C) 2019,2020 Vilius Sutkus'89
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
@@ -36,7 +36,6 @@
3636
import java.io.InputStream;
3737
import java.io.OutputStream;
3838

39-
import static org.junit.Assert.assertNull;
4039
import static org.junit.Assert.assertTrue;
4140
import static org.junit.Assert.fail;
4241

@@ -95,11 +94,12 @@ private void copyFile(InputStream input, OutputStream output) throws IOException
9594
public void testAllSuppliedPDFs() throws IOException {
9695
Context ctx = InstrumentationRegistry.getInstrumentation().getTargetContext();
9796

97+
pdf2htmlEX pdf2htmlEX = new pdf2htmlEX(ctx);
98+
9899
for (String i: m_PDFsToTest) {
99100
File pdfFile = extractAssetPDF(i);
100101
File htmlFile;
101102

102-
pdf2htmlEX pdf2htmlEX = new pdf2htmlEX(ctx);
103103
try {
104104
htmlFile = pdf2htmlEX.convert(pdfFile);
105105
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
@@ -119,55 +119,99 @@ public void testAllSuppliedPDFs() throws IOException {
119119

120120
@Test
121121
public void encryptedPdfTest() throws IOException {
122-
pdf2htmlEX converter = new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
123-
124-
// encrypted_fontfile3_opentype.pdf generated using:
122+
// encrypted_fontfile3_opentype.pdf was generated using:
125123
// qpdf --encrypt sample-user-password sample-owner-password 256 -- fontfile3_opentype.pdf encrypted_fontfile3_opentype.pdf
126124
File pdfFile = extractAssetPDF("encrypted_fontfile3_opentype.pdf");
127-
File htmlFile;
125+
126+
pdf2htmlEX converter = new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
127+
converter.setInputPDF(pdfFile);
128+
128129
try {
129-
htmlFile = converter.setInputPDF(pdfFile)
130-
.setOwnerPassword("sample-owner-password")
131-
.setUserPassword("sample-user-password")
132-
.convert();
130+
converter.convert().delete();
131+
fail("Conversion succeeded when it should have failed because of no password!");
132+
} catch (pdf2htmlEX.PasswordRequiredException ignored) {
133133
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
134-
pdfFile.delete();
135134
e.printStackTrace();
136-
fail("Failed to convert PDF to HTML");
137-
return;
135+
fail("Encrypted pdf conversion failed!");
138136
}
139137

140-
pdfFile.delete();
138+
try {
139+
converter.setUserPassword("wrong-user-password").convert().delete();
140+
fail("Conversion succeeded when it should have failed because of wrong user password!");
141+
} catch (pdf2htmlEX.WrongPasswordException ignored) {
142+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
143+
e.printStackTrace();
144+
fail("Encrypted pdf conversion failed!");
145+
}
146+
converter.setUserPassword("");
141147

142-
assertTrue("Converted HTML file not found!", htmlFile.exists());
143-
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
148+
try {
149+
converter.setOwnerPassword("wrong-owner-password").convert().delete();
150+
fail("Conversion succeeded when it should have failed because of wrong user password!");
151+
} catch (pdf2htmlEX.WrongPasswordException ignored) {
152+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
153+
e.printStackTrace();
154+
fail("Encrypted pdf conversion failed!");
155+
}
156+
converter.setOwnerPassword("");
144157

145-
htmlFile.delete();
146-
}
158+
try {
159+
File htmlFile = converter.setUserPassword("sample-user-password").convert();
160+
assertTrue("Converted HTML file not found!", htmlFile.exists());
161+
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
162+
htmlFile.delete();
163+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
164+
e.printStackTrace();
165+
fail("Encrypted pdf conversion failed!");
166+
}
167+
converter.setUserPassword("");
147168

148-
@Test
149-
public void encryptedPdfWrongPasswordTest() throws IOException {
150-
pdf2htmlEX converter = new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
169+
try {
170+
File htmlFile = converter.setOwnerPassword("sample-owner-password").convert();
171+
assertTrue("Converted HTML file not found!", htmlFile.exists());
172+
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
173+
htmlFile.delete();
174+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
175+
e.printStackTrace();
176+
fail("Encrypted pdf conversion failed!");
177+
}
178+
converter.setOwnerPassword("");
151179

152-
// encrypted_fontfile3_opentype.pdf generated using:
153-
// qpdf --encrypt sample-user-password sample-owner-password 256 -- fontfile3_opentype.pdf encrypted_fontfile3_opentype.pdf
154-
File pdfFile = extractAssetPDF("encrypted_fontfile3_opentype.pdf");
155-
File htmlFile = null;
156180
try {
157-
htmlFile = converter.setInputPDF(pdfFile)
158-
.setOwnerPassword("wrong-owner-password")
159-
.setUserPassword("wrong-user-password")
160-
.convert();
161-
} catch (pdf2htmlEX.ConversionFailedException ignored) {
162-
} catch (IOException e) {
163-
pdfFile.delete();
181+
File htmlFile = converter.setUserPassword("sample-user-password").setOwnerPassword("wrong-owner-password"). convert();
182+
assertTrue("Converted HTML file not found!", htmlFile.exists());
183+
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
184+
htmlFile.delete();
185+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
164186
e.printStackTrace();
165-
fail("Failed to convert PDF to HTML");
166-
return;
187+
fail("Encrypted pdf conversion failed!");
188+
}
189+
converter.setUserPassword("");
190+
converter.setOwnerPassword("");
191+
192+
try {
193+
File htmlFile = converter.setUserPassword("wrong-user-password").setOwnerPassword("sample-owner-password"). convert();
194+
assertTrue("Converted HTML file not found!", htmlFile.exists());
195+
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
196+
htmlFile.delete();
197+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
198+
e.printStackTrace();
199+
fail("Encrypted pdf conversion failed!");
200+
}
201+
converter.setUserPassword("");
202+
converter.setOwnerPassword("");
203+
204+
try {
205+
File htmlFile = converter.setUserPassword("sample-user-password").setOwnerPassword("sample-owner-password").convert();
206+
assertTrue("Converted HTML file not found!", htmlFile.exists());
207+
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
208+
htmlFile.delete();
209+
} catch (IOException | pdf2htmlEX.ConversionFailedException e) {
210+
e.printStackTrace();
211+
fail("Encrypted pdf conversion failed!");
167212
}
168-
pdfFile.delete();
169213

170-
assertNull("Conversion succeeded when it should have failed because of wrong encryption password!", htmlFile);
214+
pdfFile.delete();
171215
}
172216

173217
}

0 commit comments

Comments
 (0)