@@ -35,9 +35,11 @@ This file is part of the iText (R) project.
35
35
import com .itextpdf .kernel .pdf .PdfNumber ;
36
36
import com .itextpdf .kernel .pdf .PdfObject ;
37
37
import com .itextpdf .kernel .pdf .PdfReader ;
38
+ import com .itextpdf .kernel .pdf .PdfStream ;
38
39
import com .itextpdf .kernel .pdf .PdfString ;
39
40
import com .itextpdf .kernel .pdf .PdfVersion ;
40
41
import com .itextpdf .kernel .pdf .PdfWriter ;
42
+ import com .itextpdf .kernel .pdf .ReaderProperties ;
41
43
import com .itextpdf .kernel .pdf .VersionConforming ;
42
44
import com .itextpdf .kernel .pdf .WriterProperties ;
43
45
import com .itextpdf .kernel .utils .CompareTool ;
@@ -49,7 +51,7 @@ This file is part of the iText (R) project.
49
51
import org .junit .jupiter .api .Tag ;
50
52
import org .junit .jupiter .api .Test ;
51
53
52
- import java .io .* ;
54
+ import java .io .IOException ;
53
55
import java .nio .charset .StandardCharsets ;
54
56
import java .security .Security ;
55
57
import java .util .HashMap ;
@@ -115,23 +117,23 @@ public void testKnownOutput() throws Exception {
115
117
@ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
116
118
ignore = true ))
117
119
@ Test
118
- public void testMacTampered () {
120
+ public void testMacTampered () throws IOException {
119
121
String srcFile = SRC + "encryptedDocumentTamperedMac.pdf" ;
120
122
assertTampered (srcFile );
121
123
}
122
124
123
125
@ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
124
126
ignore = true ))
125
127
@ Test
126
- public void testIVTampered () {
128
+ public void testIVTampered () throws IOException {
127
129
String srcFile = SRC + "encryptedDocumentTamperedIv.pdf" ;
128
130
assertTampered (srcFile );
129
131
}
130
132
131
133
@ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
132
134
ignore = true ))
133
135
@ Test
134
- public void testCiphertextTampered () {
136
+ public void testCiphertextTampered () throws IOException {
135
137
String srcFile = SRC + "encryptedDocumentTamperedCiphertext.pdf" ;
136
138
assertTampered (srcFile );
137
139
}
@@ -214,8 +216,14 @@ private void tryCompare(String outPdf, String cmpPdf) throws Exception {
214
216
.compareByContent (outPdf , cmpPdf , DEST , "diff" , "secret" .getBytes (StandardCharsets .UTF_8 ), null );
215
217
}
216
218
217
- private void assertTampered (String outFile ) {
218
- String cmpFile = SRC + "cmp_simpleDocument.pdf" ;
219
- Assertions .assertThrows (Exception .class , () -> tryCompare (outFile , cmpFile ));
219
+ private void assertTampered (String outFile ) throws IOException {
220
+ PdfDocument pdfDoc = new PdfDocument (new PdfReader (outFile ,
221
+ new ReaderProperties ().setPassword ("secret" .getBytes (StandardCharsets .UTF_8 ))));
222
+
223
+ PdfObject obj = pdfDoc .getPdfObject (14 );
224
+ if (obj != null && obj .isStream ()) {
225
+ // Get decoded stream bytes.
226
+ Assertions .assertThrows (Exception .class , () -> ((PdfStream ) obj ).getBytes ());
227
+ }
220
228
}
221
229
}
0 commit comments