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 Jan 16, 2025
2 parents 5d3dadf + 03aae8b commit c7bb68c
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 57 deletions.
17 changes: 17 additions & 0 deletions itext.tests/itext.io.tests/itext/io/font/FontProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ namespace iText.IO.Font {
public class FontProgramTest : ExtendedITextTest {
private const String notExistingFont = "some-font.ttf";

[NUnit.Framework.SetUp]
public virtual void ClearFonts() {
FontProgramFactory.ClearRegisteredFonts();
FontProgramFactory.ClearRegisteredFontFamilies();
FontCache.ClearSavedFonts();
}

[NUnit.Framework.Test]
public virtual void ExceptionMessageTest() {
Exception e = NUnit.Framework.Assert.Catch(typeof(System.IO.IOException), () => FontProgramFactory.CreateFont
Expand Down Expand Up @@ -68,6 +75,16 @@ public virtual void RegisterDirectoryType1Test() {
.CurrentContext.TestDirectory) + "/resources/itext/io/font/type1/");
FontProgram computerModern = FontProgramFactory.CreateRegisteredFont("computer modern");
FontProgram cmr10 = FontProgramFactory.CreateRegisteredFont("cmr10");
NUnit.Framework.Assert.IsNull(computerModern);
NUnit.Framework.Assert.IsNull(cmr10);
}

[NUnit.Framework.Test]
public virtual void RegisterDirectoryType1RecursivelyTest() {
FontProgramFactory.RegisterFontDirectoryRecursively(iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/io/font/type1/");
FontProgram computerModern = FontProgramFactory.CreateRegisteredFont("computer modern");
FontProgram cmr10 = FontProgramFactory.CreateRegisteredFont("cmr10");
NUnit.Framework.Assert.IsNotNull(computerModern);
NUnit.Framework.Assert.IsNotNull(cmr10);
}
Expand Down
11 changes: 11 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfFontTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,17 @@ public virtual void TestFontDirectoryRegister() {
pdfDoc.Close();
}

[NUnit.Framework.Test]
public virtual void FontDirectoryRegisterRecursivelyTest() {
PdfFontFactory.RegisterDirectoryRecursively(sourceFolder);
foreach (String name in PdfFontFactory.GetRegisteredFonts()) {
PdfFont pdfFont = PdfFontFactory.CreateRegisteredFont(name);
if (pdfFont == null) {
NUnit.Framework.Assert.IsTrue(false, "Font {" + name + "} can't be empty");
}
}
}

[NUnit.Framework.Test]
public virtual void FontRegisterTest() {
FontProgramFactory.RegisterFont(fontsFolder + "NotoSerif-Regular_v1.7.ttf", "notoSerifRegular");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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.Collections.Generic;
using iText.IO.Source;
using iText.Kernel.Exceptions;
using iText.Kernel.Geom;
Expand Down Expand Up @@ -51,5 +52,46 @@ public virtual void AnnotationHasNoReferenceToPageTest() {
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.ANNOTATION_SHALL_HAVE_REFERENCE_TO_PAGE, exception
.Message);
}

[NUnit.Framework.Test]
public virtual void AttributesAreNullTest() {
IDictionary<PdfName, PdfObject> attributesMap = new Dictionary<PdfName, PdfObject>();
PdfDictionary dictionary = new PdfDictionary(attributesMap);
PdfStructElem pdfStructElem = new PdfStructElem(dictionary);
IList<PdfStructureAttributes> actualAttributesList = pdfStructElem.GetAttributesList();
IList<PdfStructureAttributes> expectedAttributesList = new List<PdfStructureAttributes>();
NUnit.Framework.Assert.AreEqual(actualAttributesList, expectedAttributesList);
}

[NUnit.Framework.Test]
public virtual void AttributesAreDictionaryTest() {
IDictionary<PdfName, PdfObject> attributesMap = new Dictionary<PdfName, PdfObject>();
IDictionary<PdfName, PdfObject> dictionaryMap = new Dictionary<PdfName, PdfObject>();
dictionaryMap.Put(PdfName.A, new PdfName("value"));
attributesMap.Put(PdfName.A, new PdfDictionary(dictionaryMap));
PdfDictionary dictionary = new PdfDictionary(attributesMap);
PdfStructElem pdfStructElem = new PdfStructElem(dictionary);
IList<PdfStructureAttributes> actualAttributesList = pdfStructElem.GetAttributesList();
IList<PdfStructureAttributes> expectedAttributesList = new List<PdfStructureAttributes>();
expectedAttributesList.Add(new PdfStructureAttributes(new PdfDictionary(dictionaryMap)));
NUnit.Framework.Assert.AreEqual(actualAttributesList[0].GetPdfObject().Get(PdfName.A), expectedAttributesList
[0].GetPdfObject().Get(PdfName.A));
}

[NUnit.Framework.Test]
public virtual void AttributesAreArrayTest() {
IDictionary<PdfName, PdfObject> attributesMap = new Dictionary<PdfName, PdfObject>();
IDictionary<PdfName, PdfObject> dictionaryMap = new Dictionary<PdfName, PdfObject>();
dictionaryMap.Put(PdfName.A, new PdfName("value"));
PdfDictionary pdfDictionary = new PdfDictionary(dictionaryMap);
attributesMap.Put(PdfName.A, new PdfArray(pdfDictionary));
PdfDictionary dictionary = new PdfDictionary(attributesMap);
PdfStructElem pdfStructElem = new PdfStructElem(dictionary);
IList<PdfStructureAttributes> actualAttributesList = pdfStructElem.GetAttributesList();
IList<PdfStructureAttributes> expectedAttributesList = new List<PdfStructureAttributes>();
expectedAttributesList.Add(new PdfStructureAttributes(new PdfDictionary(dictionaryMap)));
NUnit.Framework.Assert.AreEqual(actualAttributesList[0].GetPdfObject().Get(PdfName.A), expectedAttributesList
[0].GetPdfObject().Get(PdfName.A));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2025 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.Collections.Generic;
using iText.Kernel.Pdf;
using iText.Test;

namespace iText.Kernel.Pdf.Tagging {
[NUnit.Framework.Category("UnitTest")]
public class PdfStructureAttributesTest : ExtendedITextTest {
[NUnit.Framework.Test]
public virtual void OwnerIsNullTest() {
PdfStructureAttributes pdfStructureAttributes = new PdfStructureAttributes(new PdfDictionary());
NUnit.Framework.Assert.IsNull(pdfStructureAttributes.GetPdfOwner());
}

[NUnit.Framework.Test]
public virtual void OwnerIsNotNullTest() {
IDictionary<PdfName, PdfObject> map = new Dictionary<PdfName, PdfObject>();
map.Put(PdfName.O, new PdfName("owner"));
PdfStructureAttributes pdfStructureAttributes = new PdfStructureAttributes(new PdfDictionary(map));
String pdfOwner = pdfStructureAttributes.GetPdfOwner();
NUnit.Framework.Assert.AreEqual("owner", pdfOwner);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ public AssertValidationReport.AssertValidationReportLogItem WithMessage(String m
return this;
}

public virtual AssertValidationReport.AssertValidationReportLogItem WithMessageContains(String expectedContent
) {
check.WithMessageContains(expectedContent);
return this;
}

public virtual AssertValidationReport.AssertValidationReportLogItem WithStatus(ReportItem.ReportItemStatus
status) {
check.WithStatus(status);
Expand Down Expand Up @@ -219,6 +225,8 @@ private class ValidationReportLogItemCheck : AssertValidationReport.CheckChain {

private String message;

private String expectedMessageContent;

private ReportItem.ReportItemStatus status;

private bool checkStatus = false;
Expand All @@ -245,6 +253,11 @@ public virtual void WithMessage(String message, params Func<ReportItem, Object>[
errorMessage.Append(" message '").Append(message).Append("'");
}

public virtual void WithMessageContains(String expectedContent) {
this.expectedMessageContent = expectedContent;
errorMessage.Append(" message containing '").Append(expectedContent).Append("'");
}

public virtual void WithStatus(ReportItem.ReportItemStatus status) {
this.status = status;
checkStatus = true;
Expand Down Expand Up @@ -276,7 +289,13 @@ protected internal override void Check(ValidationReport report, AssertValidation
errorMessage.Append("found ").Append(prefiltered.Count).Append(" matches after message filter\n");
}
else {
prefiltered = report.GetLogs();
if (expectedMessageContent != null) {
prefiltered = report.GetLogs().Where((i) => i.GetMessage().Contains(expectedMessageContent)).ToList();
errorMessage.Append("found ").Append(prefiltered.Count).Append(" matches after message filter\n");
}
else {
prefiltered = report.GetLogs();
}
}
if (checkName != null) {
prefiltered = prefiltered.Where((i) => (checkName.Equals(i.GetCheckName()))).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ You should have received a copy of the GNU Affero General Public License
*/
using System;
using System.IO;
using System.Text;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Security;
using iText.Commons.Utils;
using iText.Kernel.Crypto;
using iText.Signatures;
using iText.Signatures.Testutils;
using iText.Signatures.Validation.Context;
Expand Down Expand Up @@ -116,9 +116,11 @@ public virtual void InvalidNumericBasicConstraintsTest() {
(2).HasNumberOfLogs(3).HasLogItem((la) => la.WithCheckName(CertificateChainValidator.CERTIFICATE_CHECK
).WithMessage("Certificate {0} is trusted, revocation data checks are not required.", (l) => rootCert.
GetSubjectDN()).WithCertificate(rootCert)).HasLogItem((la) => la.WithCheckName(CertificateChainValidator
.EXTENSIONS_CHECK).WithMessage(CertificateChainValidator.EXTENSION_MISSING, (l) => "2.5.29.19").WithCertificate
(rootCert)).HasLogItem((la) => la.WithCheckName(CertificateChainValidator.EXTENSIONS_CHECK).WithMessage
(CertificateChainValidator.EXTENSION_MISSING, (l) => "2.5.29.19").WithCertificate(intermediateCert)));
.EXTENSIONS_CHECK).WithMessage(CertificateChainValidator.EXTENSION_MISSING, (l) => MessageFormatUtil.Format
(DynamicBasicConstraintsExtension.ERROR_MESSAGE, 1, 0)).WithCertificate(rootCert)).HasLogItem((la) =>
la.WithCheckName(CertificateChainValidator.EXTENSIONS_CHECK).WithMessage(CertificateChainValidator.EXTENSION_MISSING
, (l) => MessageFormatUtil.Format(DynamicBasicConstraintsExtension.ERROR_MESSAGE, 0, -1)).WithCertificate
(intermediateCert)));
}

[NUnit.Framework.Test]
Expand All @@ -127,7 +129,7 @@ public virtual void ChainWithAiaTest() {
IX509Certificate[] certificateChain = PemFileHelper.ReadFirstChain(chainName);
IX509Certificate signingCert = (IX509Certificate)certificateChain[0];
IX509Certificate rootCert = (IX509Certificate)certificateChain[2];
IssuingCertificateRetriever customRetriever = new _IssuingCertificateRetriever_182();
IssuingCertificateRetriever customRetriever = new _IssuingCertificateRetriever_183();
validatorChainBuilder.WithIssuingCertificateRetrieverFactory(() => customRetriever);
CertificateChainValidator validator = validatorChainBuilder.BuildCertificateChainValidator();
properties.SetRequiredExtensions(CertificateSources.Of(CertificateSource.CERT_ISSUER), JavaCollectionsUtil
Expand All @@ -138,8 +140,8 @@ public virtual void ChainWithAiaTest() {
AssertValidationReport.AssertThat(report, (a) => a.HasStatus(ValidationReport.ValidationResult.VALID));
}

private sealed class _IssuingCertificateRetriever_182 : IssuingCertificateRetriever {
public _IssuingCertificateRetriever_182() {
private sealed class _IssuingCertificateRetriever_183 : IssuingCertificateRetriever {
public _IssuingCertificateRetriever_183() {
}

protected internal override Stream GetIssuerCertByURI(String uri) {
Expand All @@ -155,7 +157,7 @@ public virtual void ChainWithAiaWhichPointsToRandomCertTest() {
IX509Certificate signingCert = (IX509Certificate)certificateChain[0];
IX509Certificate intermediateCert = (IX509Certificate)certificateChain[1];
IX509Certificate rootCert = (IX509Certificate)certificateChain[2];
IssuingCertificateRetriever customRetriever = new _IssuingCertificateRetriever_206();
IssuingCertificateRetriever customRetriever = new _IssuingCertificateRetriever_207();
validatorChainBuilder.WithIssuingCertificateRetrieverFactory(() => customRetriever);
CertificateChainValidator validator = validatorChainBuilder.BuildCertificateChainValidator();
properties.SetRequiredExtensions(CertificateSources.Of(CertificateSource.CERT_ISSUER), JavaCollectionsUtil
Expand All @@ -168,8 +170,8 @@ public virtual void ChainWithAiaWhichPointsToRandomCertTest() {
AssertValidationReport.AssertThat(report, (a) => a.HasStatus(ValidationReport.ValidationResult.VALID));
}

private sealed class _IssuingCertificateRetriever_206 : IssuingCertificateRetriever {
public _IssuingCertificateRetriever_206() {
private sealed class _IssuingCertificateRetriever_207 : IssuingCertificateRetriever {
public _IssuingCertificateRetriever_207() {
}

protected internal override Stream GetIssuerCertByURI(String uri) {
Expand Down Expand Up @@ -228,13 +230,13 @@ public virtual void SeveralFailuresWithProceedAfterFailTest() {
CertificateReportItem failure1 = report.GetCertificateFailures()[0];
NUnit.Framework.Assert.AreEqual(intermediateCert, failure1.GetCertificate());
NUnit.Framework.Assert.AreEqual("Required certificate extensions check.", failure1.GetCheckName());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format("Required extension {0} is missing or incorrect."
, OID.X509Extensions.KEY_USAGE), failure1.GetMessage());
NUnit.Framework.Assert.AreEqual(BuildKeyUsageWrongMessagePart(KeyUsage.DECIPHER_ONLY, KeyUsage.KEY_CERT_SIGN
), failure1.GetMessage());
CertificateReportItem failure2 = report.GetCertificateFailures()[1];
NUnit.Framework.Assert.AreEqual(rootCert, failure2.GetCertificate());
NUnit.Framework.Assert.AreEqual("Required certificate extensions check.", failure2.GetCheckName());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format("Required extension {0} is missing or incorrect."
, OID.X509Extensions.KEY_USAGE), failure2.GetMessage());
NUnit.Framework.Assert.AreEqual(BuildKeyUsageWrongMessagePart(KeyUsage.DECIPHER_ONLY, KeyUsage.KEY_CERT_SIGN
), failure2.GetMessage());
}

[NUnit.Framework.Test]
Expand All @@ -260,8 +262,8 @@ public virtual void SeveralFailuresWithoutProceedAfterFailTest() {
CertificateReportItem failure1 = report.GetCertificateFailures()[0];
NUnit.Framework.Assert.AreEqual(intermediateCert, failure1.GetCertificate());
NUnit.Framework.Assert.AreEqual("Required certificate extensions check.", failure1.GetCheckName());
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format("Required extension {0} is missing or incorrect."
, OID.X509Extensions.KEY_USAGE), failure1.GetMessage());
NUnit.Framework.Assert.AreEqual(BuildKeyUsageWrongMessagePart(KeyUsage.DECIPHER_ONLY, KeyUsage.KEY_CERT_SIGN
), failure1.GetMessage());
}

[NUnit.Framework.Test]
Expand Down Expand Up @@ -331,8 +333,8 @@ public virtual void ValidChainRequiredExtensionNegativeTest() {
AssertValidationReport.AssertThat(report, (a) => a.HasNumberOfFailures(1).HasNumberOfLogs(2).HasLogItem((la
) => la.WithCheckName(CertificateChainValidator.CERTIFICATE_CHECK).WithMessage(CertificateChainValidator
.CERTIFICATE_TRUSTED, (l) => rootCert.GetSubjectDN()).WithCertificate(rootCert)).HasLogItem((la) => la
.WithCheckName(CertificateChainValidator.EXTENSIONS_CHECK).WithMessage(CertificateChainValidator.EXTENSION_MISSING
, (l) => OID.X509Extensions.KEY_USAGE).WithCertificate(signingCert)));
.WithCheckName(CertificateChainValidator.EXTENSIONS_CHECK).WithMessageContains(BuildKeyUsageWrongMessagePart
(KeyUsage.KEY_CERT_SIGN)).WithCertificate(signingCert)));
}

[NUnit.Framework.Test]
Expand Down Expand Up @@ -672,5 +674,17 @@ public virtual void TestStopOnInvalidRevocationResultTest() {
NUnit.Framework.Assert.AreEqual(0, mockCertificateRetriever.getCrlIssuerCertificatesByNameCalls.Count);
NUnit.Framework.Assert.AreEqual(1, mockRevocationDataValidator.calls.Count);
}

private String BuildKeyUsageWrongMessagePart(KeyUsage expectedKeyUsage, params KeyUsage[] actualKeyUsage) {
StringBuilder stringBuilder = new StringBuilder();
String sep = "";
foreach (KeyUsage usage in actualKeyUsage) {
stringBuilder.Append(sep).Append(usage);
sep = ", ";
}
return MessageFormatUtil.Format(CertificateChainValidator.EXTENSION_MISSING, MessageFormatUtil.Format(KeyUsageExtension
.EXPECTED_VALUE, expectedKeyUsage) + MessageFormatUtil.Format(KeyUsageExtension.ACTUAL_VALUE, stringBuilder
.ToString()));
}
}
}
Loading

0 comments on commit c7bb68c

Please sign in to comment.