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 Jul 5, 2024
2 parents 842e079 + 639b044 commit 81548ea
Show file tree
Hide file tree
Showing 41 changed files with 1,642 additions and 146 deletions.
88 changes: 88 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/geom/PageSizeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 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 iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Utils;
using iText.Test;

namespace iText.Kernel.Geom {
[NUnit.Framework.Category("IntegrationTest")]
public class PageSizeTest : ExtendedITextTest {
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/kernel/geom/PageSizeTest/";

public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/geom/PageSizeTest/";

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

[NUnit.Framework.OneTimeTearDown]
public static void AfterClass() {
CompareTool.Cleanup(DESTINATION_FOLDER);
}

[NUnit.Framework.Test]
public virtual void EmptyA9PageTest() {
String outPdf = DESTINATION_FOLDER + "emptyA9Page.pdf";
String cmpPdf = SOURCE_FOLDER + "cmp_emptyA9Page.pdf";
PdfDocument doc = new PdfDocument(CompareTool.CreateTestPdfWriter(outPdf));
doc.AddNewPage(PageSize.A9);
doc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff"
));
}

[NUnit.Framework.Test]
public virtual void NotEmptyA9PageTest() {
String outPdf = DESTINATION_FOLDER + "notEmptyA9Page.pdf";
String cmpPdf = SOURCE_FOLDER + "cmp_notEmptyA9Page.pdf";
PdfDocument doc = new PdfDocument(CompareTool.CreateTestPdfWriter(outPdf));
PdfPage page = doc.AddNewPage(PageSize.A9);
PdfAnnotation annotation = new PdfFreeTextAnnotation(new Rectangle(50, 10, 50, 50), new PdfString("some content"
));
page.AddAnnotation(annotation);
doc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff"
));
}

[NUnit.Framework.Test]
public virtual void AllATypePageSizesTest() {
String outPdf = DESTINATION_FOLDER + "allATypePageSizes.pdf";
String cmpPdf = SOURCE_FOLDER + "cmp_allATypePageSizes.pdf";
PageSize[] pageSizes = new PageSize[] { PageSize.A0, PageSize.A1, PageSize.A2, PageSize.A3, PageSize.A4, PageSize
.A5, PageSize.A6, PageSize.A7, PageSize.A8, PageSize.A9, PageSize.A10 };
PdfDocument doc = new PdfDocument(CompareTool.CreateTestPdfWriter(outPdf));
foreach (PageSize pageSize in pageSizes) {
doc.AddNewPage(pageSize);
}
doc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff"
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ public virtual void ConstructFromRectangleTest() {
NUnit.Framework.Assert.AreEqual(rectangle.width, pageSize.width, 1e-5);
NUnit.Framework.Assert.AreEqual(rectangle.height, pageSize.height, 1e-5);
}

[NUnit.Framework.Test]
public virtual void A9pageSizeTest() {
PageSize size = new PageSize(PageSize.A9);
NUnit.Framework.Assert.AreEqual(148, size.height, 1e-5);
NUnit.Framework.Assert.AreEqual(105, size.width, 1e-5);
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion itext.tests/itext.layout.tests/itext/layout/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public virtual void ListNestedInTableTest01() {
List list = new List(ListNumberingType.DECIMAL).Add("first string").Add("second string").Add("third string"
).Add("fourth string");
Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
table.AddCell(new Cell().Add(list).SetVerticalAlignment(VerticalAlignment.BOTTOM));
table.AddCell(new Cell().Add(list).SetVerticalAlignment(VerticalAlignment.BOTTOM).SetFontSize(10));
document.Add(table);
document.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,16 @@ public virtual void VerifyRsaSha3SignatureTest() {
VerifyIsoExtensionExample("SHA3-256withRSA", "sample-rsa-sha3_256.pdf");
}
}

[NUnit.Framework.Test]
public virtual void VerifyRsaPssSha3SignatureTest()
{
if ("BC".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName())) {
VerifyIsoExtensionExample("RSASSA-PSS", "sample-pss-sha3_256.pdf");
} else {
// Signer "RSASSA-PSS not recognised in BCFIPS mode
NUnit.Framework.Assert.Catch(typeof(PdfException), () => VerifyIsoExtensionExample("RSASSA-PSS", "sample-pss-sha3_256.pdf"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,18 @@ public virtual void TestRsaWithSha3_512() {
}

[NUnit.Framework.Test]
public virtual void TestRsaWithSha3_256() {
public virtual void TestRsaSsaPssWithSha3_256()
{
if ("BC".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName())) {
DoRoundTrip("rsa", DigestAlgorithms.SHA3_256, "RSASSA-PSS", new DerObjectIdentifier(SecurityIDs.ID_RSASSA_PSS));
} else {
// Signer RSASSA-PSS not recognised in BCFIPS mode
NUnit.Framework.Assert.Catch(typeof(PdfException), () => DoRoundTrip("rsa", DigestAlgorithms.SHA3_256, "RSASSA-PSS", new DerObjectIdentifier(SecurityIDs.ID_RSASSA_PSS)));
}
}

[NUnit.Framework.Test]
public virtual void TestDsaWithSha3_256() {
if ("BCFIPS".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName())) {
DoRoundTrip("dsa", DigestAlgorithms.SHA3_256, NistObjectIdentifiers.IdDsaWithSha3_256);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public virtual void SignWithRsaSsaPssTest()
String cmpFileName = "cmp_simplePssSignature.pdf";
if ("BCFIPS".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName()))
{
// Signer RSASSA-PSS not recognised in BC mode
// Signer RSASSA-PSS not recognised in BCFIPS mode
NUnit.Framework.Assert.Catch(typeof(PdfException), () =>
{
DoRoundTrip(digestName, "RSASSA-PSS", outFileName,
Expand All @@ -91,7 +91,7 @@ public virtual void SignWithRsaSsaPssAlternativeNomenclatureTest()

if ("BCFIPS".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName()))
{
// Signer RSASSA-PSS not recognised in BC mode
// Signer RSASSA-PSS not recognised in BCFIPS mode
NUnit.Framework.Assert.Catch(typeof(PdfException), () =>
{
DoRoundTrip(digestName,
Expand All @@ -116,7 +116,7 @@ public virtual void SignWithRsaSsaSha384PssTest() {
String outFileName = "simplePssSignatureSha384.pdf";
if ("BCFIPS".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName()))
{
// Signer RSASSA-PSS not recognised in BC mode
// Signer RSASSA-PSS not recognised in BCFIPS mode
NUnit.Framework.Assert.Catch(typeof(PdfException), () =>
{
DoRoundTrip(digestName, "RSASSA-PSS", outFileName,
Expand All @@ -137,7 +137,7 @@ public virtual void SignWithRsaSsaCustomSaltLengthTest() {
String cmpFileName = "cmp_simplePssSignature.pdf";

if ("BCFIPS".Equals(BOUNCY_CASTLE_FACTORY.GetProviderName())) {
// Signer RSASSA-PSS not recognised in BC mode
// Signer RSASSA-PSS not recognised in BCFIPS mode
NUnit.Framework.Assert.Catch(typeof(PdfException), () =>
{
DoRoundTrip(digestName, "RSASSA-PSS", outFileName, new RSASSAPSSMechanismParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ public class TestCrlClientWrapper : ICrlClient {
private readonly IList<TestCrlClientWrapper.CrlClientCall> calls = new List<TestCrlClientWrapper.CrlClientCall
>();

private Func<TestCrlClientWrapper.CrlClientCall, ICollection<byte[]>> onGetEncoded;

public TestCrlClientWrapper(ICrlClient wrappedClient) {
this.wrappedClient = wrappedClient;
}

public virtual ICollection<byte[]> GetEncoded(IX509Certificate checkCert, String url) {
ICollection<byte[]> crlBytesCollection = wrappedClient.GetEncoded(checkCert, url);
TestCrlClientWrapper.CrlClientCall call = new TestCrlClientWrapper.CrlClientCall(checkCert, url);
ICollection<byte[]> crlBytesCollection;
if (onGetEncoded != null) {
crlBytesCollection = onGetEncoded.Invoke(call);
}
else {
crlBytesCollection = wrappedClient.GetEncoded(checkCert, url);
}
IList<IX509Crl> crlResponses = new List<IX509Crl>();
foreach (byte[] crlBytes in crlBytesCollection) {
try {
Expand All @@ -48,25 +57,35 @@ public virtual ICollection<byte[]> GetEncoded(IX509Certificate checkCert, String
throw new Exception("Deserializing CRL response failed", e);
}
}
calls.Add(new TestCrlClientWrapper.CrlClientCall(checkCert, url, crlResponses));
call.SetResponses(crlResponses);
calls.Add(call);
return crlBytesCollection;
}

public virtual IList<TestCrlClientWrapper.CrlClientCall> GetCalls() {
return calls;
}

public virtual iText.Signatures.Testutils.Client.TestCrlClientWrapper OnGetEncodedDo(Func<TestCrlClientWrapper.CrlClientCall
, ICollection<byte[]>> callBack) {
onGetEncoded = callBack;
return this;
}

public class CrlClientCall {
public readonly IX509Certificate checkCert;

public readonly String url;

public readonly IList<IX509Crl> responses;
public IList<IX509Crl> responses;

public CrlClientCall(IX509Certificate checkCert, String url, IList<IX509Crl> responses) {
public CrlClientCall(IX509Certificate checkCert, String url) {
this.checkCert = checkCert;
this.url = url;
this.responses = responses;
}

public virtual void SetResponses(IList<IX509Crl> crlResponses) {
responses = crlResponses;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,27 @@ public class TestOcspClientWrapper : IOcspClient {

private readonly IOcspClient wrappedClient;

private Func<TestOcspClientWrapper.OcspClientCall, byte[]> onGetEncoded;

public TestOcspClientWrapper(IOcspClient wrappedClient) {
this.wrappedClient = wrappedClient;
}

public virtual byte[] GetEncoded(IX509Certificate checkCert, IX509Certificate issuerCert, String url) {
byte[] response = wrappedClient.GetEncoded(checkCert, issuerCert, url);
TestOcspClientWrapper.OcspClientCall call = new TestOcspClientWrapper.OcspClientCall(checkCert, issuerCert
, url);
byte[] response;
if (onGetEncoded != null) {
response = onGetEncoded.Invoke(call);
}
else {
response = wrappedClient.GetEncoded(checkCert, issuerCert, url);
}
try {
IBasicOcspResponse basicOCSPResp = BOUNCY_CASTLE_FACTORY.CreateBasicOCSPResponse(BOUNCY_CASTLE_FACTORY.CreateASN1Primitive
(response));
calls.Add(new TestOcspClientWrapper.OcspClientCall(checkCert, issuerCert, url, basicOCSPResp));
call.SetResponce(basicOCSPResp);
calls.Add(call);
}
catch (System.IO.IOException e) {
throw new Exception("deserializing ocsp response failed", e);
Expand All @@ -59,21 +70,29 @@ public virtual byte[] GetEncoded(IX509Certificate checkCert, IX509Certificate is
return calls;
}

public virtual iText.Signatures.Testutils.Client.TestOcspClientWrapper OnGetEncodedDo(Func<TestOcspClientWrapper.OcspClientCall
, byte[]> callBack) {
onGetEncoded = callBack;
return this;
}

public class OcspClientCall {
public readonly IX509Certificate checkCert;

public readonly IX509Certificate issuerCert;

public readonly String url;

public readonly IBasicOcspResponse response;
public IBasicOcspResponse response;

public OcspClientCall(IX509Certificate checkCert, IX509Certificate issuerCert, String url, IBasicOcspResponse
response) {
public OcspClientCall(IX509Certificate checkCert, IX509Certificate issuerCert, String url) {
this.checkCert = checkCert;
this.issuerCert = issuerCert;
this.url = url;
this.response = response;
}

public virtual void SetResponce(IBasicOcspResponse basicOCSPResp) {
response = basicOCSPResp;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,37 @@ public virtual void CertExpiredAfterDateFromExpiredCertOnCrlExtensionTest() {
(0));
}

[NUnit.Framework.Test]
public virtual void CertificateRetrieverFailureTest() {
RetrieveTestResources("happyPath");
byte[] crl = CreateCrl(crlIssuerCert, crlIssuerKey, TimeTestUtil.TEST_DATE_TIME.AddDays(-5), TimeTestUtil.
TEST_DATE_TIME.AddDays(+5));
MockIssuingCertificateRetriever mockCertificateRetriever = new MockIssuingCertificateRetriever();
mockCertificateRetriever.OngetCrlIssuerCertificatesDo((c) => {
throw new Exception("just testing");
}
);
validatorChainBuilder.WithIssuingCertificateRetriever(mockCertificateRetriever);
validatorChainBuilder.WithCRLValidator(new CRLValidator(validatorChainBuilder));
ValidationReport report = PerformValidation("happyPath", TimeTestUtil.TEST_DATE_TIME, crl);
AssertValidationReport.AssertThat(report, (a) => a.HasStatus(ValidationReport.ValidationResult.INDETERMINATE
).HasLogItem((l) => l.WithMessage(CRLValidator.CRL_ISSUER_REQUEST_FAILED)));
}

[NUnit.Framework.Test]
public virtual void ChainValidatorFailureTest() {
RetrieveTestResources("happyPath");
byte[] crl = CreateCrl(crlIssuerCert, crlIssuerKey, TimeTestUtil.TEST_DATE_TIME.AddDays(-5), TimeTestUtil.
TEST_DATE_TIME.AddDays(+5));
mockChainValidator.OnCallDo((c) => {
throw new Exception("Just testing");
}
);
ValidationReport report = PerformValidation("happyPath", TimeTestUtil.TEST_DATE_TIME, crl);
AssertValidationReport.AssertThat(report, (a) => a.HasStatus(ValidationReport.ValidationResult.INDETERMINATE
).HasLogItem((l) => l.WithMessage(CRLValidator.CRL_ISSUER_CHAIN_FAILED)));
}

[NUnit.Framework.Test]
public virtual void ProvidedTimeIsUsedForResponderValidation() {
RetrieveTestResources("happyPath");
Expand Down
Loading

0 comments on commit 81548ea

Please sign in to comment.