summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2019-06-04 13:53:00 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2019-06-04 13:53:00 +0700
commit6c7a86e0b73074b2a42ea129acc831ace01d41a5 (patch)
treec0bb41783442ef197d0f10bcb56f6e015fc687ec /crypto/test
parentName constraint validation updates from bc-java (diff)
downloadBouncyCastle.NET-ed25519-6c7a86e0b73074b2a42ea129acc831ace01d41a5.tar.xz
Port LinkedCertificate from bc-java
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/UnitTests.csproj1
-rw-r--r--crypto/test/src/asn1/test/LinkedCertificateTest.cs97
-rw-r--r--crypto/test/src/asn1/test/RegressionTest.cs1
3 files changed, 99 insertions, 0 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj
index 9420cc832..1378034a6 100644
--- a/crypto/test/UnitTests.csproj
+++ b/crypto/test/UnitTests.csproj
@@ -85,6 +85,7 @@
     <Compile Include="src\asn1\test\IssuingDistributionPointTest.cs" />
     <Compile Include="src\asn1\test\KeyUsageTest.cs" />
     <Compile Include="src\asn1\test\LDSSecurityObjectUnitTest.cs" />
+    <Compile Include="src\asn1\test\LinkedCertificateTest.cs" />
     <Compile Include="src\asn1\test\MiscTest.cs" />
     <Compile Include="src\asn1\test\MonetaryLimitUnitTest.cs" />
     <Compile Include="src\asn1\test\MonetaryValueUnitTest.cs" />
diff --git a/crypto/test/src/asn1/test/LinkedCertificateTest.cs b/crypto/test/src/asn1/test/LinkedCertificateTest.cs
new file mode 100644
index 000000000..416c048c0
--- /dev/null
+++ b/crypto/test/src/asn1/test/LinkedCertificateTest.cs
@@ -0,0 +1,97 @@
+using System;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1.BC;
+using Org.BouncyCastle.Asn1.Nist;
+using Org.BouncyCastle.Asn1.X509;
+
+namespace Org.BouncyCastle.Asn1.Tests
+{
+	[TestFixture]
+    public class LinkedCertificateTest
+        : Asn1UnitTest
+    {
+        public override string Name
+        {
+            get { return "LinkedCertificate"; }
+        }
+
+        public override void PerformTest()
+        {
+            DigestInfo digInfo = new DigestInfo(new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256), new byte[32]);
+            GeneralName certLocation = new GeneralName(GeneralName.UniformResourceIdentifier, "https://www.bouncycastle.org/certs");
+            X509Name certIssuer = null;
+            GeneralNames cACerts = null;
+
+            LinkedCertificate linked = new LinkedCertificate(digInfo, certLocation);
+
+            CheckConstruction(linked, digInfo, certLocation, certIssuer, cACerts);
+
+            certIssuer = new X509Name("CN=Test");
+            cACerts = new GeneralNames(new GeneralName(new X509Name("CN=CA Test")));
+
+            linked = new LinkedCertificate(digInfo, certLocation, certIssuer, cACerts);
+
+            CheckConstruction(linked, digInfo, certLocation, certIssuer, cACerts);
+
+            linked = LinkedCertificate.GetInstance(null);
+
+            if (linked != null)
+            {
+                Fail("null getInstance() failed.");
+            }
+
+            try
+            {
+                LinkedCertificate.GetInstance(new object());
+
+                Fail("getInstance() failed to detect bad object.");
+            }
+            catch (ArgumentException e)
+            {
+                // expected
+            }
+        }
+
+        private void CheckConstruction(LinkedCertificate linked, DigestInfo digestInfo, GeneralName certLocation,
+            X509Name certIssuer, GeneralNames caCerts)
+        {
+            CheckValues(linked, digestInfo, certLocation, certIssuer, caCerts);
+
+            linked = LinkedCertificate.GetInstance(linked);
+
+            CheckValues(linked, digestInfo, certLocation, certIssuer, caCerts);
+
+            Asn1InputStream aIn = new Asn1InputStream(linked.ToAsn1Object().GetEncoded());
+
+            Asn1Sequence seq = (Asn1Sequence)aIn.ReadObject();
+
+            linked = LinkedCertificate.GetInstance(seq);
+
+            CheckValues(linked, digestInfo, certLocation, certIssuer, caCerts);
+        }
+
+        private void CheckValues(LinkedCertificate linked, DigestInfo digestInfo, GeneralName certLocation,
+            X509Name certIssuer, GeneralNames caCerts)
+        {
+            checkMandatoryField("digest", digestInfo, linked.Digest);
+            checkMandatoryField("certLocatin", certLocation, linked.CertLocation);
+            checkOptionalField("certIssuer", certIssuer, linked.CertIssuer);
+            checkOptionalField("caCerts", caCerts, linked.CACerts);
+        }
+
+        public static void Main(string[] args)
+        {
+            RunTest(new LinkedCertificateTest());
+        }
+
+        [Test]
+        public void TestFunction()
+        {
+            string resultText = Perform().ToString();
+
+            Assert.AreEqual(Name + ": Okay", resultText);
+        }
+    }
+}
diff --git a/crypto/test/src/asn1/test/RegressionTest.cs b/crypto/test/src/asn1/test/RegressionTest.cs
index 4534f2c75..67860ccd7 100644
--- a/crypto/test/src/asn1/test/RegressionTest.cs
+++ b/crypto/test/src/asn1/test/RegressionTest.cs
@@ -37,6 +37,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 			new IssuingDistributionPointUnitTest(),
 			new KeyUsageTest(),
             new LDSSecurityObjectUnitTest(),
+            new LinkedCertificateTest(),
             new MiscTest(),
 			new MonetaryLimitUnitTest(),
 			new MonetaryValueUnitTest(),