Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Sep 7, 2023
2 parents 0e38e17 + f754054 commit 255f280
Show file tree
Hide file tree
Showing 44 changed files with 1,488 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ public virtual void CopyEncryptedDocument() {
srcDoc.CopyPagesTo(1, 1, destDoc);
PdfDictionary srcInfo = srcDoc.GetTrailer().GetAsDictionary(PdfName.Info);
PdfDictionary destInfo = destDoc.GetTrailer().GetAsDictionary(PdfName.Info);
if (destInfo == null) {
destInfo = new PdfDictionary();
destDoc.GetTrailer().Put(PdfName.Info, destInfo);
}
foreach (PdfName srcInfoKey in srcInfo.KeySet()) {
destInfo.Put((PdfName)srcInfoKey.CopyTo(destDoc), srcInfo.Get(srcInfoKey).CopyTo(destDoc));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using iText.Kernel.XMP;
using iText.Kernel.XMP.Impl;
using iText.Test;

namespace iText.Kernel.Pdf {
[NUnit.Framework.Category("UnitTest")]
public class PdfAConformanceLevelTest : ExtendedITextTest {
[NUnit.Framework.Test]
public virtual void GetConformanceTest() {
PdfAConformanceLevel level = PdfAConformanceLevel.GetConformanceLevel("4", null);
NUnit.Framework.Assert.AreEqual(PdfAConformanceLevel.PDF_A_4, level);
}

[NUnit.Framework.Test]
public virtual void GetXmpConformanceNullTest() {
XMPMeta meta = new XMPMetaImpl();
meta.SetProperty(XMPConst.NS_PDFA_ID, XMPConst.PART, "4");
PdfAConformanceLevel level = PdfAConformanceLevel.GetConformanceLevel(meta);
NUnit.Framework.Assert.AreEqual(PdfAConformanceLevel.PDF_A_4, level);
}

[NUnit.Framework.Test]
public virtual void GetXmpConformanceBTest() {
XMPMeta meta = new XMPMetaImpl();
meta.SetProperty(XMPConst.NS_PDFA_ID, XMPConst.PART, "2");
meta.SetProperty(XMPConst.NS_PDFA_ID, XMPConst.CONFORMANCE, "B");
PdfAConformanceLevel level = PdfAConformanceLevel.GetConformanceLevel(meta);
NUnit.Framework.Assert.AreEqual(PdfAConformanceLevel.PDF_A_2B, level);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public virtual void ReleaseModifiedObjectTest() {
[NUnit.Framework.Test]
public virtual void AddingReleasedObjectToDocumentTest() {
String srcFile = SOURCE_FOLDER + "releaseObjectsInSimpleDoc.pdf";
PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(SOURCE_FOLDER + "addingReleasedObjectToDocument.pdf"
PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(DESTINATION_FOLDER + "addingReleasedObjectToDocument.pdf"
));
try {
PdfObject releasedObj = doc.GetPdfObject(1);
Expand Down
Binary file not shown.
209 changes: 209 additions & 0 deletions itext.tests/itext.pdfa.tests/itext/pdfa/PdfA4CatalogCheckTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using iText.Commons.Utils;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using iText.Pdfa.Exceptions;
using iText.Test;
using iText.Test.Pdfa;

namespace iText.Pdfa {
[NUnit.Framework.Category("IntegrationTest")]
public class PdfA4CatalogCheckTest : ExtendedITextTest {
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/";

public static readonly String cmpFolder = sourceFolder + "cmp/PdfA4CatalogCheckTest/";

public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/pdfa/PdfA4CatalogCheckTest/";

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
CreateOrClearDestinationFolder(destinationFolder);
}

[NUnit.Framework.Test]
public virtual void CreateSimpleDocTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck01.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
doc.Close();
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
}

// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
[NUnit.Framework.Test]
public virtual void InvalidCatalogVersionCheckTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck02.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
doc.GetCatalog().Put(PdfName.Version, new PdfString("1.7"));
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(PdfaExceptionMessageConstant.THE_CATALOG_VERSION_SHALL_CONTAIN_RIGHT_PDF_VERSION
, "2"), e.Message);
}

[NUnit.Framework.Test]
public virtual void EncryptInTrailerTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck03.pdf";
byte[] userPassword = "user".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1);
byte[] ownerPassword = "owner".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1);
int permissions = EncryptionConstants.ALLOW_SCREENREADERS | EncryptionConstants.ALLOW_DEGRADED_PRINTING;
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0).SetStandardEncryption
(userPassword, ownerPassword, permissions, EncryptionConstants.ENCRYPTION_AES_256).SetFullCompressionMode
(false));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(PdfAConformanceException.KEYWORD_ENCRYPT_SHALL_NOT_BE_USED_IN_THE_TRAILER_DICTIONARY
, e.Message);
}

[NUnit.Framework.Test]
public virtual void EncryptedDocumentTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck03.pdf";
byte[] userPassword = "user".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1);
byte[] ownerPassword = "owner".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1);
int permissions = EncryptionConstants.ALLOW_SCREENREADERS | EncryptionConstants.ALLOW_DEGRADED_PRINTING;
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0).SetStandardEncryption
(userPassword, ownerPassword, permissions, EncryptionConstants.ENCRYPTION_AES_256).SetFullCompressionMode
(true));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(PdfAConformanceException.KEYWORD_ENCRYPT_SHALL_NOT_BE_USED_IN_THE_TRAILER_DICTIONARY
, e.Message);
}

[NUnit.Framework.Test]
public virtual void AbsentPieceInfoTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck04.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
PdfDictionary info = new PdfDictionary();
String timeValue = "D:19860426012347+04'00'";
info.Put(PdfName.ModDate, new PdfDate(PdfDate.Decode(timeValue)).GetPdfObject());
doc.GetTrailer().Put(PdfName.Info, info);
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(PdfaExceptionMessageConstant.DOCUMENT_SHALL_NOT_CONTAIN_INFO_UNLESS_THERE_IS_PIECE_INFO
, e.Message);
}

[NUnit.Framework.Test]
public virtual void ValidCatalogCheckTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck05.pdf";
String cmpPdf = sourceFolder + "cmp/PdfA4CatalogCheckTest/cmp_pdfA4_catalogCheck05.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
doc.GetCatalog().Put(PdfName.PieceInfo, new PdfDictionary());
doc.Close();
// This is required to check if ModDate is inside Info dictionary
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diff_"
));
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
}

// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
[NUnit.Framework.Test]
public virtual void InvalidInfoTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck05.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
doc.GetTrailer().Put(PdfName.Info, new PdfDictionary());
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(PdfaExceptionMessageConstant.DOCUMENT_INFO_DICTIONARY_SHALL_ONLY_CONTAIN_MOD_DATE
, e.Message);
}

[NUnit.Framework.Test]
public virtual void ReadValidDocumentTest() {
String outPdf = destinationFolder + "simplePdfA4_output01.pdf";
PdfWriter writer = new PdfWriter(outPdf);
PdfReader reader = new PdfReader(sourceFolder + "pdfs/simplePdfA4.pdf");
PdfDocument document = new PdfADocument(reader, writer);
document.Close();
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
}

// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
[NUnit.Framework.Test]
public virtual void InvalidVersionInCatalogTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck06.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
doc.GetCatalog().Put(PdfName.Version, new PdfString("1.7"));
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(PdfaExceptionMessageConstant.THE_CATALOG_VERSION_SHALL_CONTAIN_RIGHT_PDF_VERSION
, 2), e.Message);
}

[NUnit.Framework.Test]
public virtual void CorruptedVersionInCatalogTest() {
String outPdf = destinationFolder + "pdfA4_catalogCheck07.pdf";
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", ""
, "http://www.color.org", "sRGB IEC61966-2.1", @is));
doc.AddNewPage();
doc.GetCatalog().Put(PdfName.Version, new PdfString("2ae"));
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(PdfaExceptionMessageConstant.THE_CATALOG_VERSION_SHALL_CONTAIN_RIGHT_PDF_VERSION
, 2), e.Message);
}

[NUnit.Framework.Test]
public virtual void ReadDocumentWithInvalidVersionTest() {
PdfWriter writer = new PdfWriter(destinationFolder + "simplePdfA4_output02.pdf");
PdfReader reader = new PdfReader(sourceFolder + "pdfs/pdfA4WithInvalidVersion.pdf");
PdfDocument document = new PdfADocument(reader, writer);
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => document.Close());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(PdfaExceptionMessageConstant.THE_FILE_HEADER_SHALL_CONTAIN_RIGHT_PDF_VERSION
, 2), e.Message);
}
}
}
Loading

0 comments on commit 255f280

Please sign in to comment.