summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-08 22:57:03 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-08 22:57:03 +0700
commit8bef2160dc81d3c29d5bf18950f4fdeaa4da06d0 (patch)
tree630cf1cf57acbe5551ccf887415510f3e1f93474 /crypto/test
parentLazy ASN.1 enumeration and refactoring (diff)
downloadBouncyCastle.NET-ed25519-8bef2160dc81d3c29d5bf18950f4fdeaa4da06d0.tar.xz
ASN.1: Add support for relative OIDs
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/UnitTests.csproj1
-rw-r--r--crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs1
-rw-r--r--crypto/test/src/asn1/test/OIDTest.cs98
-rw-r--r--crypto/test/src/asn1/test/RegressionTest.cs3
-rw-r--r--crypto/test/src/asn1/test/RelativeOidTest.cs129
5 files changed, 177 insertions, 55 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj
index 1945f1367..00f8eb087 100644
--- a/crypto/test/UnitTests.csproj
+++ b/crypto/test/UnitTests.csproj
@@ -111,6 +111,7 @@
     <Compile Include="src\asn1\test\QCStatementUnitTest.cs" />
     <Compile Include="src\asn1\test\ReasonFlagsTest.cs" />
     <Compile Include="src\asn1\test\RegressionTest.cs" />
+    <Compile Include="src\asn1\test\RelativeOidTest.cs" />
     <Compile Include="src\asn1\test\RequestedCertificateUnitTest.cs" />
     <Compile Include="src\asn1\test\RestrictionUnitTest.cs" />
     <Compile Include="src\asn1\test\SMIMETest.cs" />
diff --git a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
index 8109d656a..396268f4b 100644
--- a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
+++ b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
@@ -36,6 +36,7 @@ namespace Org.BouncyCastle.Asn1.Tests
                 DerNull.Instance,
                 new DerNumericString("123456"),
                 new DerObjectIdentifier("1.1.1.10000.1"),
+                new Asn1RelativeOid("3.2.0.123456"),
                 new DerOctetString(data),
                 new DerPrintableString("hello world"),
                 new DerSequence(new DerPrintableString("hello world")),
diff --git a/crypto/test/src/asn1/test/OIDTest.cs b/crypto/test/src/asn1/test/OIDTest.cs
index b0782db04..9a1927a41 100644
--- a/crypto/test/src/asn1/test/OIDTest.cs
+++ b/crypto/test/src/asn1/test/OIDTest.cs
@@ -1,6 +1,4 @@
 using System;
-using System.IO;
-using System.Text;
 
 using NUnit.Framework;
 
@@ -18,17 +16,15 @@ namespace Org.BouncyCastle.Asn1.Tests
 	public class OidTest
 		: SimpleTest
 	{
-		byte[] req1 = Hex.Decode("0603813403");
-		byte[] req2 = Hex.Decode("06082A36FFFFFFDD6311");
+		private static readonly byte[] req1 = Hex.Decode("0603813403");
+        private static readonly byte[] req2 = Hex.Decode("06082A36FFFFFFDD6311");
 
 		public override string Name
 		{
 			get { return "OID"; }
 		}
 
-		private void recodeCheck(
-			string	oid,
-			byte[]	enc)
+		private void RecodeCheck(string oid, byte[] enc)
 		{
 			DerObjectIdentifier o = new DerObjectIdentifier(oid);
 			DerObjectIdentifier encO = (DerObjectIdentifier) Asn1Object.FromByteArray(enc);
@@ -46,11 +42,10 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 		}
 
-		private void validOidCheck(
-			string oid)
+		private void CheckValid(string oid)
 		{
 			DerObjectIdentifier o = new DerObjectIdentifier(oid);
-			o = (DerObjectIdentifier) Asn1Object.FromByteArray(o.GetEncoded());
+			o = (DerObjectIdentifier)Asn1Object.FromByteArray(o.GetEncoded());
 
 			if (!o.Id.Equals(oid))
 			{
@@ -58,8 +53,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 		}
 
-		private void invalidOidCheck(
-			string oid)
+		private void CheckInvalid(string oid)
 		{
 			try
 			{
@@ -72,7 +66,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 		}
 
-		private void branchCheck(string stem, string branch)
+		private void BranchCheck(string stem, string branch)
 		{
 			string expected = stem + "." + branch;
 			string actual = new DerObjectIdentifier(stem).Branch(branch).Id;
@@ -83,7 +77,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 		}
 
-		private void onCheck(String stem, String test, bool expected)
+		private void OnCheck(String stem, String test, bool expected)
 		{
 			if (expected != new DerObjectIdentifier(test).On(new DerObjectIdentifier(stem)))
 			{
@@ -93,49 +87,45 @@ namespace Org.BouncyCastle.Asn1.Tests
 
 		public override void PerformTest()
 		{
-			recodeCheck("2.100.3", req1);
-			recodeCheck("1.2.54.34359733987.17", req2);
-
-			validOidCheck(PkcsObjectIdentifiers.Pkcs9AtContentType.Id);
-			validOidCheck("0.1");
-			validOidCheck("1.1.127.32512.8323072.2130706432.545460846592.139637976727552.35747322042253312.9151314442816847872");
-			validOidCheck("1.2.123.12345678901.1.1.1");
-			validOidCheck("2.25.196556539987194312349856245628873852187.1");
-
-			invalidOidCheck("0");
-			invalidOidCheck("1");
-			invalidOidCheck("2");
-			invalidOidCheck("3.1");
-			invalidOidCheck("..1");
-			invalidOidCheck("192.168.1.1");
-			invalidOidCheck(".123452");
-			invalidOidCheck("1.");
-			invalidOidCheck("1.345.23.34..234");
-			invalidOidCheck("1.345.23.34.234.");
-			invalidOidCheck(".12.345.77.234");
-			invalidOidCheck(".12.345.77.234.");
-			invalidOidCheck("1.2.3.4.A.5");
-			invalidOidCheck("1,2");
-
-			branchCheck("1.1", "2.2");
-
-			onCheck("1.1", "1.1", false);
-			onCheck("1.1", "1.2", false);
-			onCheck("1.1", "1.2.1", false);
-			onCheck("1.1", "2.1", false);
-			onCheck("1.1", "1.11", false);
-			onCheck("1.12", "1.1.2", false);
-			onCheck("1.1", "1.1.1", true);
-			onCheck("1.1", "1.1.2", true);
+			RecodeCheck("2.100.3", req1);
+			RecodeCheck("1.2.54.34359733987.17", req2);
+
+			CheckValid(PkcsObjectIdentifiers.Pkcs9AtContentType.Id);
+			CheckValid("0.1");
+			CheckValid("1.1.127.32512.8323072.2130706432.545460846592.139637976727552.35747322042253312.9151314442816847872");
+			CheckValid("1.2.123.12345678901.1.1.1");
+			CheckValid("2.25.196556539987194312349856245628873852187.1");
+
+			CheckInvalid("0");
+			CheckInvalid("1");
+			CheckInvalid("2");
+			CheckInvalid("3.1");
+			CheckInvalid("..1");
+			CheckInvalid("192.168.1.1");
+			CheckInvalid(".123452");
+			CheckInvalid("1.");
+			CheckInvalid("1.345.23.34..234");
+			CheckInvalid("1.345.23.34.234.");
+			CheckInvalid(".12.345.77.234");
+			CheckInvalid(".12.345.77.234.");
+			CheckInvalid("1.2.3.4.A.5");
+			CheckInvalid("1,2");
+
+			BranchCheck("1.1", "2.2");
+
+			OnCheck("1.1", "1.1", false);
+			OnCheck("1.1", "1.2", false);
+			OnCheck("1.1", "1.2.1", false);
+			OnCheck("1.1", "2.1", false);
+			OnCheck("1.1", "1.11", false);
+			OnCheck("1.12", "1.1.2", false);
+			OnCheck("1.1", "1.1.1", true);
+			OnCheck("1.1", "1.1.2", true);
 		}
 
-		public static void Main(
-			string[] args)
+		public static void Main(string[] args)
 		{
-			ITest test = new OidTest();
-			ITestResult	result = test.Perform();
-
-			Console.WriteLine(result);
+            RunTest(new OidTest());
 		}
 
 		[Test]
diff --git a/crypto/test/src/asn1/test/RegressionTest.cs b/crypto/test/src/asn1/test/RegressionTest.cs
index cf10c6c3b..7bc10b079 100644
--- a/crypto/test/src/asn1/test/RegressionTest.cs
+++ b/crypto/test/src/asn1/test/RegressionTest.cs
@@ -58,7 +58,8 @@ namespace Org.BouncyCastle.Asn1.Tests
 			new ProfessionInfoUnitTest(),
 			new QCStatementUnitTest(),
 			new ReasonFlagsTest(),
-			new RequestedCertificateUnitTest(),
+            new RelativeOidTest(),
+            new RequestedCertificateUnitTest(),
 			new RestrictionUnitTest(),
 			new SemanticsInformationUnitTest(),
 			new SetTest(),
diff --git a/crypto/test/src/asn1/test/RelativeOidTest.cs b/crypto/test/src/asn1/test/RelativeOidTest.cs
new file mode 100644
index 000000000..e52c1b715
--- /dev/null
+++ b/crypto/test/src/asn1/test/RelativeOidTest.cs
@@ -0,0 +1,129 @@
+using System;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Asn1.Tests
+{
+    [TestFixture]
+    public class RelativeOidTest
+        : SimpleTest
+    {
+        private static readonly byte[] req1 = Hex.Decode("0D03813403");
+        private static readonly byte[] req2 = Hex.Decode("0D082A36FFFFFFDD6311");
+
+        public override string Name
+        {
+            get { return "RelativeOID"; }
+        }
+
+        private void RecodeCheck(string oid, byte[] enc)
+        {
+            Asn1RelativeOid o = new Asn1RelativeOid(oid);
+            Asn1RelativeOid encO = (Asn1RelativeOid)Asn1Object.FromByteArray(enc);
+
+            if (!o.Equals(encO))
+            {
+                Fail("relative OID didn't match", o, encO);
+            }
+
+            byte[] bytes = o.GetDerEncoded();
+
+            if (!Arrays.AreEqual(bytes, enc))
+            {
+                Fail("failed comparison test", Hex.ToHexString(enc), Hex.ToHexString(bytes));
+            }
+        }
+
+        private void CheckValid(string oid)
+        {
+            Asn1RelativeOid o = new Asn1RelativeOid(oid);
+			o = (Asn1RelativeOid)Asn1Object.FromByteArray(o.GetEncoded());
+
+			if (!o.Id.Equals(oid))
+			{
+                Fail("failed relative oid check for " + oid);
+            }
+        }
+
+        private void CheckInvalid(string oid)
+        {
+            try
+            {
+                new Asn1RelativeOid(oid);
+                Fail("failed to catch bad relative oid: " + oid);
+            }
+            catch (FormatException)
+            {
+                // expected
+            }
+        }
+
+        private void BranchCheck(string stem, string branch)
+        {
+            string expected = stem + "." + branch;
+            string actual = new Asn1RelativeOid(stem).Branch(branch).Id;
+
+            if (expected != actual)
+            {
+                Fail("failed 'branch' check for " + stem + "/" + branch);
+            }
+        }
+
+        public override void PerformTest()
+        {
+            RecodeCheck("180.3", req1);
+            RecodeCheck("42.54.34359733987.17", req2);
+
+            CheckValid("0");
+            CheckValid("37");
+            CheckValid("0.1");
+            CheckValid("1.0");
+            CheckValid("1.0.2");
+            CheckValid("1.0.20");
+            CheckValid("1.0.200");
+            CheckValid("1.1.127.32512.8323072.2130706432.545460846592.139637976727552.35747322042253312.9151314442816847872");
+            CheckValid("1.2.123.12345678901.1.1.1");
+            CheckValid("2.25.196556539987194312349856245628873852187.1");
+            CheckValid("3.1");
+            CheckValid("37.196556539987194312349856245628873852187.100");
+            CheckValid("192.168.1.1");
+
+            CheckInvalid("00");
+            CheckInvalid("0.01");
+            CheckInvalid("00.1");
+            CheckInvalid("1.00.2");
+            CheckInvalid("1.0.02");
+            CheckInvalid("1.2.00");
+            CheckInvalid(".1");
+            CheckInvalid("..1");
+            CheckInvalid("3..1");
+            CheckInvalid(".123452");
+            CheckInvalid("1.");
+            CheckInvalid("1.345.23.34..234");
+            CheckInvalid("1.345.23.34.234.");
+            CheckInvalid(".12.345.77.234");
+            CheckInvalid(".12.345.77.234.");
+            CheckInvalid("1.2.3.4.A.5");
+            CheckInvalid("1,2");
+
+            BranchCheck("1.1", "2.2");
+        }
+
+        public static void Main(string[] args)
+        {
+            RunTest(new RelativeOidTest());
+        }
+
+        [Test]
+        public void TestFunction()
+        {
+            string resultText = Perform().ToString();
+
+            Assert.AreEqual(Name + ": Okay", resultText);
+        }
+    }
+}