@@ -59,7 +59,6 @@ This file is part of the iText (R) project.
59
59
import com .itextpdf .test .annotations .LogMessage ;
60
60
import com .itextpdf .test .annotations .LogMessages ;
61
61
import com .itextpdf .test .annotations .type .BouncyCastleIntegrationTest ;
62
- import com .itextpdf .test .annotations .type .IntegrationTest ;
63
62
64
63
import java .io .FileInputStream ;
65
64
import java .io .IOException ;
@@ -74,7 +73,6 @@ This file is part of the iText (R) project.
74
73
import org .junit .BeforeClass ;
75
74
import org .junit .Test ;
76
75
import org .junit .experimental .categories .Category ;
77
- import static org .junit .Assert .fail ;
78
76
79
77
/**
80
78
* Due to import control restrictions by the governments of a few countries,
@@ -195,87 +193,6 @@ public void encryptWithPasswordAes256NoCompression() throws IOException, Interru
195
193
encryptWithPassword2 (filename , encryptionType , CompressionConstants .NO_COMPRESSION );
196
194
}
197
195
198
- @ Test
199
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
200
- ignore = true ))
201
- public void encryptWithCertificateStandard128 () throws IOException , InterruptedException , GeneralSecurityException ,
202
- AbstractPKCSException , AbstractOperatorCreationException {
203
- String filename = "encryptWithCertificateStandard128.pdf" ;
204
- int encryptionType = EncryptionConstants .STANDARD_ENCRYPTION_128 ;
205
- encryptWithCertificate (filename , encryptionType , CompressionConstants .DEFAULT_COMPRESSION );
206
- }
207
-
208
- @ Test
209
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
210
- ignore = true ))
211
- public void encryptWithCertificateStandard40 () throws IOException , InterruptedException , GeneralSecurityException ,
212
- AbstractPKCSException , AbstractOperatorCreationException {
213
- String filename = "encryptWithCertificateStandard40.pdf" ;
214
- int encryptionType = EncryptionConstants .STANDARD_ENCRYPTION_40 ;
215
- encryptWithCertificate (filename , encryptionType , CompressionConstants .DEFAULT_COMPRESSION );
216
- }
217
-
218
- @ Test
219
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
220
- ignore = true ))
221
- public void encryptWithCertificateStandard128NoCompression () throws IOException , InterruptedException ,
222
- GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
223
- String filename = "encryptWithCertificateStandard128NoCompression.pdf" ;
224
- int encryptionType = EncryptionConstants .STANDARD_ENCRYPTION_128 ;
225
- encryptWithCertificate (filename , encryptionType , CompressionConstants .NO_COMPRESSION );
226
- }
227
-
228
- @ Test
229
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
230
- ignore = true ))
231
- public void encryptWithCertificateStandard40NoCompression () throws IOException , InterruptedException ,
232
- GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
233
- String filename = "encryptWithCertificateStandard40NoCompression.pdf" ;
234
- int encryptionType = EncryptionConstants .STANDARD_ENCRYPTION_40 ;
235
- encryptWithCertificate (filename , encryptionType , CompressionConstants .NO_COMPRESSION );
236
- }
237
-
238
- @ Test
239
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
240
- ignore = true ))
241
- public void encryptWithCertificateAes128 () throws IOException , InterruptedException , GeneralSecurityException ,
242
- AbstractPKCSException , AbstractOperatorCreationException {
243
- String filename = "encryptWithCertificateAes128.pdf" ;
244
- int encryptionType = EncryptionConstants .ENCRYPTION_AES_128 ;
245
- encryptWithCertificate (filename , encryptionType , CompressionConstants .DEFAULT_COMPRESSION );
246
- }
247
-
248
- @ Test
249
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
250
- ignore = true ))
251
- public void encryptWithCertificateAes256 () throws IOException , InterruptedException , GeneralSecurityException ,
252
- AbstractPKCSException , AbstractOperatorCreationException {
253
- String filename = "encryptWithCertificateAes256.pdf" ;
254
- int encryptionType = EncryptionConstants .ENCRYPTION_AES_256 ;
255
- encryptWithCertificate (filename , encryptionType , CompressionConstants .DEFAULT_COMPRESSION );
256
- }
257
-
258
- @ Test
259
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
260
- ignore = true ))
261
- public void encryptWithCertificateAes128NoCompression () throws IOException , InterruptedException ,
262
- GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
263
- String filename = "encryptWithCertificateAes128NoCompression.pdf" ;
264
- int encryptionType = EncryptionConstants .ENCRYPTION_AES_128 ;
265
- encryptWithCertificate (filename , encryptionType , CompressionConstants .NO_COMPRESSION );
266
- }
267
-
268
-
269
- @ Test
270
- @ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
271
- ignore = true ))
272
- public void encryptWithCertificateAes256NoCompression () throws IOException , InterruptedException ,
273
- GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
274
- String filename = "encryptWithCertificateAes256NoCompression.pdf" ;
275
- int encryptionType = EncryptionConstants .ENCRYPTION_AES_256 ;
276
- encryptWithCertificate (filename , encryptionType , CompressionConstants .NO_COMPRESSION );
277
- }
278
-
279
196
@ Test
280
197
@ LogMessages (messages = @ LogMessage (messageTemplate = KernelLogMessageConstant .MD5_IS_NOT_FIPS_COMPLIANT ,
281
198
ignore = true ))
@@ -713,11 +630,6 @@ public void encryptWithPassword2(String filename, int encryptionType, int compre
713
630
checkEncryptedWithPasswordDocumentAppending (filename , OWNER );
714
631
}
715
632
716
- public void encryptWithPassword (String filename , int encryptionType , int compression )
717
- throws IOException , InterruptedException {
718
- encryptWithPassword (filename , encryptionType , compression , false );
719
- }
720
-
721
633
public void encryptWithPassword (String filename , int encryptionType , int compression , boolean fullCompression )
722
634
throws IOException , InterruptedException {
723
635
String outFileName = destinationFolder + filename ;
@@ -742,44 +654,6 @@ public void encryptWithPassword(String filename, int encryptionType, int compres
742
654
checkEncryptedWithPasswordDocumentAppending (filename , OWNER );
743
655
}
744
656
745
- public void encryptWithCertificate (String filename , int encryptionType , int compression ) throws IOException ,
746
- InterruptedException , GeneralSecurityException , AbstractPKCSException , AbstractOperatorCreationException {
747
- ITextTest .removeCryptographyRestrictions ();
748
-
749
- String outFileName = destinationFolder + filename ;
750
- int permissions = EncryptionConstants .ALLOW_SCREENREADERS ;
751
- Certificate cert = getPublicCertificate (CERT );
752
- PdfWriter writer = new PdfWriter (outFileName , new WriterProperties ()
753
- .setPublicKeyEncryption (new Certificate [] {cert }, new int [] {permissions }, encryptionType )
754
- .addXmpMetadata ());
755
- writer .setCompressionLevel (compression );
756
- PdfDocument document = new PdfDocument (writer );
757
- document .getDocumentInfo ().setMoreInfo (customInfoEntryKey , customInfoEntryValue );
758
- PdfPage page = document .addNewPage ();
759
- writeTextBytesOnPageContent (page , pageTextContent );
760
-
761
- page .flush ();
762
- document .close ();
763
-
764
- checkDecryptedWithCertificateContent (filename , cert , pageTextContent );
765
-
766
- CompareTool compareTool = new CompareTool ().enableEncryptionCompare ();
767
- compareTool .getOutReaderProperties ()
768
- .setPublicKeySecurityParams (cert , getPrivateKey (), FACTORY .getProviderName (), null );
769
- compareTool .getCmpReaderProperties ()
770
- .setPublicKeySecurityParams (cert , getPrivateKey (), FACTORY .getProviderName (), null );
771
- String compareResult = compareTool .compareByContent (outFileName , sourceFolder + "cmp_" + filename ,
772
- destinationFolder , "diff_" );
773
- if (compareResult != null ) {
774
- fail (compareResult );
775
- }
776
-
777
- checkEncryptedWithCertificateDocumentStamping (filename , cert );
778
- checkEncryptedWithCertificateDocumentAppending (filename , cert );
779
-
780
- ITextTest .restoreCryptographyRestrictions ();
781
- }
782
-
783
657
public Certificate getPublicCertificate (String path ) throws IOException , CertificateException {
784
658
FileInputStream is = new FileInputStream (path );
785
659
return CryptoUtil .readPublicCertificate (is );
@@ -819,23 +693,6 @@ private static void checkDecryptedWithPasswordContent(String src, byte[] passwor
819
693
document .close ();
820
694
}
821
695
822
- public void checkDecryptedWithCertificateContent (String filename , Certificate certificate , String pageContent )
823
- throws IOException , AbstractPKCSException , AbstractOperatorCreationException {
824
- String src = destinationFolder + filename ;
825
- PdfReader reader = new PdfReader (src , new ReaderProperties ()
826
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null ));
827
- PdfDocument document = new PdfDocument (reader );
828
- PdfPage page = document .getPage (1 );
829
-
830
- String s = new String (page .getStreamBytes (0 ));
831
- Assert .assertTrue ("Expected content: \n " + pageContent , s .contains (pageContent ));
832
- Assert .assertEquals ("Encrypted custom" , customInfoEntryValue ,
833
- document .getTrailer ().getAsDictionary (PdfName .Info ).getAsString (new PdfName (customInfoEntryKey ))
834
- .toUnicodeString ());
835
-
836
- document .close ();
837
- }
838
-
839
696
// basically this is comparing content of decrypted by itext document with content of encrypted document
840
697
public void checkEncryptedWithPasswordDocumentStamping (String filename , byte [] password )
841
698
throws IOException , InterruptedException {
@@ -851,28 +708,7 @@ public void checkEncryptedWithPasswordDocumentStamping(String filename, byte[] p
851
708
destinationFolder , "diff_" , USER , USER );
852
709
853
710
if (compareResult != null ) {
854
- fail (compareResult );
855
- }
856
- }
857
-
858
- // basically this is comparing content of decrypted by itext document with content of encrypted document
859
- public void checkEncryptedWithCertificateDocumentStamping (String filename , Certificate certificate )
860
- throws IOException , InterruptedException , AbstractPKCSException , AbstractOperatorCreationException {
861
- String srcFileName = destinationFolder + filename ;
862
- String outFileName = destinationFolder + "stamped_" + filename ;
863
- PdfReader reader = new PdfReader (srcFileName , new ReaderProperties ()
864
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null ));
865
- PdfDocument document = new PdfDocument (reader , new PdfWriter (outFileName ));
866
- document .close ();
867
-
868
- CompareTool compareTool = new CompareTool ();
869
- compareTool .getCmpReaderProperties ()
870
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null );
871
- String compareResult = compareTool .compareByContent (outFileName , sourceFolder + "cmp_" + filename ,
872
- destinationFolder , "diff_" );
873
-
874
- if (compareResult != null ) {
875
- fail (compareResult );
711
+ Assert .fail (compareResult );
876
712
}
877
713
}
878
714
@@ -894,44 +730,7 @@ public void checkEncryptedWithPasswordDocumentAppending(String filename, byte[]
894
730
destinationFolder , "diff_" , USER , USER );
895
731
896
732
if (compareResult != null ) {
897
- fail (compareResult );
898
- }
899
- }
900
-
901
- public void checkEncryptedWithCertificateDocumentAppending (String filename , Certificate certificate )
902
- throws IOException , InterruptedException , AbstractPKCSException , AbstractOperatorCreationException {
903
- String srcFileName = destinationFolder + filename ;
904
- String outFileName = destinationFolder + "appended_" + filename ;
905
- PdfReader reader = new PdfReader (srcFileName , new ReaderProperties ()
906
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null ));
907
- PdfDocument document = new PdfDocument (reader , new PdfWriter (outFileName ),
908
- new StampingProperties ().useAppendMode ());
909
- PdfPage newPage = document .addNewPage ();
910
- String helloWorldStringValue = "Hello world string" ;
911
- newPage .put (PdfName .Default , new PdfString (helloWorldStringValue ));
912
- writeTextBytesOnPageContent (newPage , "Hello world page_2!" );
913
- document .close ();
914
-
915
- PdfReader appendedDocReader = new PdfReader (outFileName , new ReaderProperties ()
916
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null ));
917
- PdfDocument appendedDoc = new PdfDocument (appendedDocReader );
918
- PdfPage secondPage = appendedDoc .getPage (2 );
919
- PdfString helloWorldPdfString = secondPage .getPdfObject ().getAsString (PdfName .Default );
920
- String actualHelloWorldStringValue = helloWorldPdfString != null ? helloWorldPdfString .getValue () : null ;
921
- Assert .assertEquals (actualHelloWorldStringValue , helloWorldStringValue );
922
- appendedDoc .close ();
923
-
924
- CompareTool compareTool = new CompareTool ().enableEncryptionCompare ();
925
- compareTool .getOutReaderProperties ()
926
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null );
927
- compareTool .getCmpReaderProperties ()
928
- .setPublicKeySecurityParams (certificate , getPrivateKey (), FACTORY .getProviderName (), null );
929
-
930
- String compareResult = compareTool .compareByContent (outFileName , sourceFolder + "cmp_appended_" + filename ,
931
- destinationFolder , "diff_" );
932
-
933
- if (compareResult != null ) {
934
- fail (compareResult );
733
+ Assert .fail (compareResult );
935
734
}
936
735
}
937
736
@@ -956,7 +755,7 @@ static void compareEncryptedPdf(String filename) throws IOException, Interrupted
956
755
String compareResult = compareTool .compareByContent (destinationFolder + filename ,
957
756
sourceFolder + "cmp_" + filename , destinationFolder , "diff_" , USER , USER );
958
757
if (compareResult != null ) {
959
- fail (compareResult );
758
+ Assert . fail (compareResult );
960
759
}
961
760
}
962
761
}
0 commit comments