20
20
21
21
import androidx .test .platform .app .InstrumentationRegistry ;
22
22
23
- import java .io .ByteArrayInputStream ;
24
23
import java .io .ByteArrayOutputStream ;
25
24
import java .io .IOException ;
26
25
import java .io .InputStream ;
30
29
31
30
import javax .crypto .Cipher ;
32
31
32
+ import com .tom_roush .pdfbox .android .PDFBoxResourceLoader ;
33
33
import com .tom_roush .pdfbox .io .MemoryUsageSetting ;
34
34
import com .tom_roush .pdfbox .pdmodel .PDDocument ;
35
35
import com .tom_roush .pdfbox .pdmodel .encryption .AccessPermission ;
36
36
import com .tom_roush .pdfbox .pdmodel .encryption .PublicKeyProtectionPolicy ;
37
37
import com .tom_roush .pdfbox .pdmodel .encryption .PublicKeyRecipient ;
38
- import com .tom_roush .pdfbox .android .PDFBoxResourceLoader ;
39
38
40
- import org .junit .After ;
41
39
import org .junit .Assert ;
42
- import org .junit .Before ;
43
- import org .junit .Test ;
44
40
45
- import static org . junit .Assert . fail ;
41
+ import junit .framework . TestCase ;
46
42
47
43
/**
48
44
* Tests for public key encryption.
49
45
*
50
46
* @author Ben Litchfield
51
47
*/
52
- public class TestPublicKeyEncryption
48
+ public class TestPublicKeyEncryption extends TestCase
53
49
{
54
50
55
51
private AccessPermission permission1 ;
@@ -64,7 +60,7 @@ public class TestPublicKeyEncryption
64
60
private String password1 ;
65
61
private String password2 ;
66
62
67
- Context testContext ;
63
+ private Context testContext ;
68
64
private final String path = "pdfbox/com/tom_roush/pdfbox/pdmodel/encryption/" ;
69
65
70
66
/**
@@ -73,8 +69,11 @@ public class TestPublicKeyEncryption
73
69
private PDDocument document ;
74
70
75
71
76
- @ Before
77
- public void setUp () throws Exception
72
+ /**
73
+ * {@inheritDoc}
74
+ */
75
+ @ Override
76
+ protected void setUp () throws Exception
78
77
{
79
78
if (Cipher .getMaxAllowedKeyLength ("AES" ) != Integer .MAX_VALUE )
80
79
{
@@ -114,19 +113,14 @@ public void setUp() throws Exception
114
113
keyStore1 = "test1.pfx" ;
115
114
keyStore2 = "test2.pfx" ;
116
115
117
- InputStream input = testContext .getAssets ().open (path + "test.pdf" );
118
- try
119
- {
120
- document = PDDocument .load (input );
121
- }
122
- finally
123
- {
124
- input .close ();
125
- }
116
+ document = PDDocument .load (testContext .getAssets ().open (path + "test.pdf" ));
126
117
}
127
118
128
- @ After
129
- public void tearDown () throws Exception
119
+ /**
120
+ * {@inheritDoc}
121
+ */
122
+ @ Override
123
+ protected void tearDown () throws Exception
130
124
{
131
125
document .close ();
132
126
}
@@ -137,7 +131,6 @@ public void tearDown() throws Exception
137
131
*
138
132
* @throws Exception If there is an unexpected error during the test.
139
133
*/
140
- @ Test
141
134
public void testProtectionError () throws Exception
142
135
{
143
136
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy ();
@@ -155,7 +148,7 @@ public void testProtectionError() throws Exception
155
148
{
156
149
String msg = ex .getMessage ();
157
150
Assert .assertTrue ("not the expected exception: " + msg ,
158
- msg .contains ("serial-#: rid 2 vs. cert 3" ));
151
+ msg .contains ("serial-#: rid 2 vs. cert 3" ));
159
152
}
160
153
finally
161
154
{
@@ -173,7 +166,6 @@ public void testProtectionError() throws Exception
173
166
*
174
167
* @throws Exception If there is an unexpected error during the test.
175
168
*/
176
- @ Test
177
169
public void testProtection () throws Exception
178
170
{
179
171
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy ();
@@ -186,7 +178,7 @@ public void testProtection() throws Exception
186
178
Assert .assertTrue (encryptedDoc .isEncrypted ());
187
179
188
180
AccessPermission permission =
189
- encryptedDoc .getCurrentAccessPermission ();
181
+ encryptedDoc .getCurrentAccessPermission ();
190
182
Assert .assertFalse (permission .canAssembleDocument ());
191
183
Assert .assertFalse (permission .canExtractContent ());
192
184
Assert .assertTrue (permission .canExtractForAccessibility ());
@@ -208,7 +200,6 @@ public void testProtection() throws Exception
208
200
*
209
201
* @throws Exception If there is an error during the test.
210
202
*/
211
- @ Test
212
203
public void testMultipleRecipients () throws Exception
213
204
{
214
205
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy ();
@@ -221,7 +212,7 @@ public void testMultipleRecipients() throws Exception
221
212
try
222
213
{
223
214
AccessPermission permission =
224
- encryptedDoc1 .getCurrentAccessPermission ();
215
+ encryptedDoc1 .getCurrentAccessPermission ();
225
216
Assert .assertFalse (permission .canAssembleDocument ());
226
217
Assert .assertFalse (permission .canExtractContent ());
227
218
Assert .assertTrue (permission .canExtractForAccessibility ());
@@ -241,7 +232,7 @@ public void testMultipleRecipients() throws Exception
241
232
try
242
233
{
243
234
AccessPermission permission =
244
- encryptedDoc2 .getCurrentAccessPermission ();
235
+ encryptedDoc2 .getCurrentAccessPermission ();
245
236
Assert .assertFalse (permission .canAssembleDocument ());
246
237
Assert .assertFalse (permission .canExtractContent ());
247
238
Assert .assertTrue (permission .canExtractForAccessibility ());
@@ -265,14 +256,14 @@ public void testMultipleRecipients() throws Exception
265
256
* @param decryptionPassword password to be used to decrypt the doc
266
257
* @param keyStore password to be used to decrypt the doc
267
258
* @return reloaded document
268
- * @throws Exception if
259
+ * @throws Exception if
269
260
*/
270
261
private PDDocument reload (PDDocument doc , String decryptionPassword , InputStream keyStore )
271
- throws IOException , NoSuchAlgorithmException
262
+ throws IOException , NoSuchAlgorithmException
272
263
{
273
- ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
274
- doc .save (buffer );
275
- return PDDocument .load (new ByteArrayInputStream ( buffer .toByteArray () ), decryptionPassword ,
264
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
265
+ doc .save (baos );
266
+ return PDDocument .load (baos .toByteArray (), decryptionPassword ,
276
267
keyStore , null , MemoryUsageSetting .setupMainMemoryOnly ());
277
268
}
278
269
@@ -294,7 +285,7 @@ private PublicKeyRecipient getRecipient(String certificate, AccessPermission per
294
285
PublicKeyRecipient recipient = new PublicKeyRecipient ();
295
286
recipient .setPermission (permission );
296
287
recipient .setX509 (
297
- (X509Certificate ) factory .generateCertificate (input ));
288
+ (X509Certificate ) factory .generateCertificate (input ));
298
289
return recipient ;
299
290
}
300
291
finally
0 commit comments