Skip to content

Commit 0413b5d

Browse files
committed
Merge branch 'develop' into devsecops
2 parents 3dd640e + a29cd39 commit 0413b5d

File tree

64 files changed

+8545
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+8545
-14
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using System.Collections.Generic;
25+
using System.IO;
26+
using System.Linq;
27+
using System.Text;
28+
using System.Xml;
29+
using System.Xml.Schema;
30+
using System.Xml.XPath;
31+
using iText.Signatures.Validation.Report.Xml;
32+
33+
namespace iText.Signatures.Testutils.Report.Xml {
34+
public class XmlReportTestTool {
35+
private static readonly String XSDROOT = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
36+
.CurrentContext.TestDirectory) + "/resources/itext/signatures/validation/report/xml/";
37+
38+
private readonly XmlDocument xml;
39+
40+
private readonly String report;
41+
private XPathNavigator navigator;
42+
private XmlNamespaceManager manager;
43+
44+
public XmlReportTestTool(String report) {
45+
this.report = report;
46+
xml = new XmlDocument();
47+
xml.LoadXml(report);
48+
49+
navigator = xml.CreateNavigator();
50+
manager = new XmlNamespaceManager(navigator.NameTable);
51+
manager.AddNamespace("r", XmlReportGenerator.DOC_NS);
52+
manager.AddNamespace("ds", XmlReportGenerator.DS_NS);
53+
manager.AddNamespace("xs", XmlReportGenerator.XS_NS);
54+
manager.AddNamespace("xsi", XmlReportGenerator.XSI_NS);
55+
}
56+
57+
public virtual XmlElement GetDocumentNode() {
58+
return xml.DocumentElement;
59+
}
60+
61+
public virtual int CountElements(String xPathQuery) {
62+
return xml.SelectNodes(xPathQuery, manager).Count;
63+
}
64+
65+
public virtual String GetElementContent(String xPathQuery) {
66+
return xml.SelectSingleNode(xPathQuery, manager).InnerText;
67+
}
68+
69+
public virtual XmlNodeList ExecuteXpathAsNodeList(String xPathQuery) {
70+
return xml.SelectNodes(xPathQuery, manager);
71+
}
72+
73+
public virtual XmlNode ExecuteXpathAsNode(String xPathQuery) {
74+
return xml.SelectSingleNode(xPathQuery, manager);
75+
}
76+
77+
public virtual String ExecuteXpathAsString(String xPathQuery) {
78+
return (String)navigator.Evaluate(xPathQuery, manager);
79+
}
80+
81+
public virtual double? ExecuteXpathAsNumber(String xPathQuery) {
82+
return (double?)navigator.Evaluate(xPathQuery, manager);
83+
}
84+
85+
public virtual bool? ExecuteXpathAsBoolean(String xPathQuery) {
86+
return (bool?)navigator.Evaluate(xPathQuery, manager);
87+
}
88+
89+
public virtual String ValidateXMLSchema() {
90+
List<string> files = new List<string>();
91+
files.AddAll(new[] {
92+
XSDROOT + "xml.xsd",
93+
XSDROOT + "XMLSchema.xsd",
94+
XSDROOT + "XAdES.xsd",
95+
XSDROOT + "ts_119612v020201_201601xsd.xsd",
96+
XSDROOT + "1910202xmlSchema.xsd",
97+
XSDROOT + "xmldsig-core-schema.xsd"
98+
});
99+
100+
var schemas = files.Select(f => XmlSchema.Read(XmlReader.Create(f,
101+
new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse }), (s, e) => {
102+
}));
103+
104+
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
105+
foreach (var s in schemas) {
106+
xmlSchemaSet.Add(s);
107+
}
108+
109+
XmlReaderSettings validationSettings = new XmlReaderSettings();
110+
validationSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.AllowXmlAttributes;
111+
validationSettings.ValidationType = ValidationType.Schema;
112+
validationSettings.Schemas.Add(xmlSchemaSet);
113+
validationSettings.Schemas.Compile();
114+
StringBuilder log = new StringBuilder();
115+
116+
validationSettings.ValidationEventHandler += (s, e) => {
117+
log.Append("***\n");
118+
log.Append("\tPosition:").Append(e.Exception.LineNumber).Append(':').Append(e.Exception.LinePosition).Append('\n');
119+
log.Append("\tSeverity:").Append(e.Severity).Append('\n');
120+
log.Append("\tMessage :").Append(e.Message).Append('\n');
121+
};
122+
123+
XmlReader reader = XmlReader.Create(new StringReader(report), validationSettings);
124+
while (reader.Read()) {}
125+
126+
String message = log.ToString();
127+
if (String.IsNullOrEmpty(message)) {
128+
return null;
129+
}
130+
return message;
131+
}
132+
}
133+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
namespace iText.Signatures.Validation.Report.Xml {
24+
public abstract class AbstractCollectableObjectTest : AbstractIdentifiableObjectTest {
25+
private AbstractCollectableObjectTest.MockCollectableObjectVisitor mockVisitor;
26+
27+
[NUnit.Framework.SetUp]
28+
public virtual void SetUpParent() {
29+
mockVisitor = new AbstractCollectableObjectTest.MockCollectableObjectVisitor();
30+
}
31+
32+
[NUnit.Framework.Test]
33+
public virtual void TestVisitorUsage() {
34+
AbstractCollectableObject sut = GetCollectableObjectUnderTest();
35+
sut.Accept(mockVisitor);
36+
NUnit.Framework.Assert.AreEqual(1, mockVisitor.calls);
37+
}
38+
39+
//\cond DO_NOT_DOCUMENT
40+
internal override AbstractIdentifiableObject GetIdentifiableObjectUnderTest() {
41+
return GetCollectableObjectUnderTest();
42+
}
43+
//\endcond
44+
45+
//\cond DO_NOT_DOCUMENT
46+
internal abstract AbstractCollectableObject GetCollectableObjectUnderTest();
47+
//\endcond
48+
49+
private class MockCollectableObjectVisitor : CollectableObjectVisitor {
50+
public int calls;
51+
52+
public virtual void Visit(CertificateWrapper certificateWrapper) {
53+
calls++;
54+
}
55+
56+
public virtual void Visit(POEValidationReport poeValidationReport) {
57+
calls++;
58+
}
59+
}
60+
}
61+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using iText.Test;
25+
26+
namespace iText.Signatures.Validation.Report.Xml {
27+
public abstract class AbstractIdentifiableObjectTest : ExtendedITextTest {
28+
[NUnit.Framework.Test]
29+
public virtual void TestIdentifiersAreUnique() {
30+
AbstractIdentifiableObject sut1 = new AbstractIdentifiableObjectTest.TestIdentifiableObject("A");
31+
AbstractIdentifiableObject sut2 = new AbstractIdentifiableObjectTest.TestIdentifiableObject("A");
32+
NUnit.Framework.Assert.AreNotEqual(sut1.GetIdentifier().GetId(), sut2.GetIdentifier().GetId());
33+
}
34+
35+
[NUnit.Framework.Test]
36+
public virtual void TestEqualsForEqualIdentity() {
37+
AbstractIdentifiableObject sut1 = GetIdentifiableObjectUnderTest();
38+
AbstractIdentifiableObject sut2 = sut1;
39+
// Equals is being tested here.
40+
NUnit.Framework.Assert.IsTrue(sut1.Equals(sut2));
41+
}
42+
43+
[NUnit.Framework.Test]
44+
public virtual void TestEqualsForNull() {
45+
AbstractIdentifiableObject sut = GetIdentifiableObjectUnderTest();
46+
// Equals is being tested here.
47+
NUnit.Framework.Assert.IsFalse(sut.Equals(null));
48+
}
49+
50+
[NUnit.Framework.Test]
51+
public virtual void TestEqualsForSomeObject() {
52+
AbstractIdentifiableObject sut = GetIdentifiableObjectUnderTest();
53+
// Equals is being tested here.
54+
NUnit.Framework.Assert.IsFalse(sut.Equals("Test"));
55+
}
56+
57+
[NUnit.Framework.Test]
58+
public virtual void TestEqualsForEqualInstances() {
59+
PerformTestEqualsForEqualInstances();
60+
}
61+
62+
[NUnit.Framework.Test]
63+
public virtual void TestHashForEqualInstances() {
64+
PerformTestHashForEqualInstances();
65+
}
66+
67+
[NUnit.Framework.Test]
68+
public virtual void TestEqualsForDifferentInstances() {
69+
PerformTestEqualsForDifferentInstances();
70+
}
71+
72+
[NUnit.Framework.Test]
73+
public virtual void TestHashForDifferentInstances() {
74+
PerformTestHashForDifferentInstances();
75+
}
76+
77+
protected internal abstract void PerformTestHashForEqualInstances();
78+
79+
protected internal abstract void PerformTestEqualsForEqualInstances();
80+
81+
protected internal abstract void PerformTestEqualsForDifferentInstances();
82+
83+
protected internal abstract void PerformTestHashForDifferentInstances();
84+
85+
//\cond DO_NOT_DOCUMENT
86+
internal abstract AbstractIdentifiableObject GetIdentifiableObjectUnderTest();
87+
//\endcond
88+
89+
private class TestIdentifiableObject : AbstractIdentifiableObject {
90+
protected internal TestIdentifiableObject(String prefix)
91+
: base(prefix) {
92+
}
93+
94+
public override bool Equals(Object o) {
95+
return false;
96+
}
97+
98+
public override int GetHashCode() {
99+
return 0;
100+
}
101+
}
102+
}
103+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using iText.Bouncycastleconnector;
25+
using iText.Commons.Bouncycastle;
26+
using iText.Commons.Bouncycastle.Cert;
27+
using iText.Commons.Utils;
28+
using iText.Signatures.Testutils;
29+
30+
namespace iText.Signatures.Validation.Report.Xml {
31+
[NUnit.Framework.Category("BouncyCastleUnitTest")]
32+
public class CertificateWrapperTest : AbstractCollectableObjectTest {
33+
private static readonly IBouncyCastleFactory FACTORY = BouncyCastleFactoryCreator.GetFactory();
34+
35+
private static readonly String CERTS_SRC = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
36+
.CurrentContext.TestDirectory) + "/resources/itext/signatures/certs/";
37+
38+
private static IX509Certificate cert1;
39+
40+
private static IX509Certificate cert2;
41+
42+
[NUnit.Framework.OneTimeSetUp]
43+
public static void SetUpFixture() {
44+
cert1 = (IX509Certificate)PemFileHelper.ReadFirstChain(CERTS_SRC + "root.pem")[0];
45+
cert2 = (IX509Certificate)PemFileHelper.ReadFirstChain(CERTS_SRC + "signCertDsa01.pem")[0];
46+
}
47+
48+
[NUnit.Framework.Test]
49+
public virtual void TestEqualInstancesHaveUniqueIds() {
50+
CertificateWrapper sut1 = new CertificateWrapper(cert1);
51+
CertificateWrapper sut2 = new CertificateWrapper(cert1);
52+
NUnit.Framework.Assert.AreNotEqual(sut1.GetIdentifier().GetId(), sut2.GetIdentifier().GetId());
53+
}
54+
55+
protected internal override void PerformTestHashForEqualInstances() {
56+
CertificateWrapper sut1 = new CertificateWrapper(cert1);
57+
CertificateWrapper sut2 = new CertificateWrapper(cert1);
58+
NUnit.Framework.Assert.AreEqual(sut1.GetHashCode(), sut2.GetHashCode());
59+
}
60+
61+
protected internal override void PerformTestEqualsForEqualInstances() {
62+
CertificateWrapper sut1 = new CertificateWrapper(cert1);
63+
CertificateWrapper sut2 = new CertificateWrapper(cert1);
64+
NUnit.Framework.Assert.AreEqual(sut1, sut2);
65+
}
66+
67+
protected internal override void PerformTestEqualsForDifferentInstances() {
68+
CertificateWrapper sut1 = new CertificateWrapper(cert1);
69+
CertificateWrapper sut2 = new CertificateWrapper(cert2);
70+
NUnit.Framework.Assert.AreNotEqual(sut1, sut2);
71+
}
72+
73+
protected internal override void PerformTestHashForDifferentInstances() {
74+
CertificateWrapper sut1 = new CertificateWrapper(cert1);
75+
CertificateWrapper sut2 = new CertificateWrapper(cert2);
76+
NUnit.Framework.Assert.AreNotEqual(sut1.GetHashCode(), sut2.GetHashCode());
77+
}
78+
79+
[NUnit.Framework.Test]
80+
public virtual void TestGetBase64ASN1Structure() {
81+
CertificateWrapper sut = new CertificateWrapper(cert1);
82+
IX509Certificate sutCert = FACTORY.CreateX509Certificate(Convert.FromBase64String(sut.GetBase64ASN1Structure
83+
()));
84+
IX509Certificate origCert = FACTORY.CreateX509Certificate(cert1.GetEncoded());
85+
NUnit.Framework.Assert.AreEqual(origCert, sutCert);
86+
}
87+
88+
//\cond DO_NOT_DOCUMENT
89+
internal override AbstractCollectableObject GetCollectableObjectUnderTest() {
90+
return new CertificateWrapper(cert1);
91+
}
92+
//\endcond
93+
}
94+
}

0 commit comments

Comments
 (0)