summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-11-02 16:25:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-11-02 16:25:57 +0700
commitd92f6ef71eb96ba3ee49ffaecaaeb9d387ee91e4 (patch)
tree097d28766042ce128438dd40c1547ae04c22cc9c
parentTLS: Improve supported_groups compliance (diff)
downloadBouncyCastle.NET-ed25519-d92f6ef71eb96ba3ee49ffaecaaeb9d387ee91e4.tar.xz
Refactoring in tests
-rw-r--r--crypto/test/src/asn1/test/CscaMasterListTest.cs32
-rw-r--r--crypto/test/src/cms/test/Rfc4134Test.cs23
-rw-r--r--crypto/test/src/cms/test/SignedDataTest.cs200
-rw-r--r--crypto/test/src/util/test/SimpleTest.cs156
4 files changed, 144 insertions, 267 deletions
diff --git a/crypto/test/src/asn1/test/CscaMasterListTest.cs b/crypto/test/src/asn1/test/CscaMasterListTest.cs
index 8db7aa09a..0903d5528 100644
--- a/crypto/test/src/asn1/test/CscaMasterListTest.cs
+++ b/crypto/test/src/asn1/test/CscaMasterListTest.cs
@@ -1,10 +1,6 @@
-using System;
-
 using NUnit.Framework;
 
-using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Icao;
-using Org.BouncyCastle.Utilities.IO;
 using Org.BouncyCastle.Utilities.Test;
 
 namespace Org.BouncyCastle.Asn1.Tests
@@ -13,31 +9,17 @@ namespace Org.BouncyCastle.Asn1.Tests
     public class CscaMasterListTest
         : SimpleTest
     {
-		public override string Name
-		{
-			get { return "CscaMasterList"; }
-		}
+		public override string Name => "CscaMasterList";
 
-		public override void PerformTest() 
-		{
-			byte[] input = GetInput("masterlist-content.data");
+		public override void PerformTest()
+        {
+			byte[] input = SimpleTest.GetTestData("asn1.masterlist-content.data");
 			CscaMasterList parsedList = CscaMasterList.GetInstance(Asn1Object.FromByteArray(input));
 
-			if (parsedList.GetCertStructs().Length != 3)
-			{
-				Fail("Cert structure parsing failed: incorrect length");
-			}
+			IsEquals("Cert structure parsing failed: incorrect length", 3, parsedList.GetCertStructs().Length);
 
 			byte[] output = parsedList.GetEncoded();
-			if (!AreEqual(input, output))
-			{
-				Fail("Encoding failed after parse");
-			}
-		}
-
-		private byte[] GetInput(string name)
-		{
-			return Streams.ReadAll(SimpleTest.GetTestDataAsStream("asn1." + name));
+			FailIf("Encoding failed after parse", !AreEqual(input, output));
 		}
 
 		[Test]
@@ -47,5 +29,5 @@ namespace Org.BouncyCastle.Asn1.Tests
 
 			Assert.AreEqual(Name + ": Okay", resultText);
         }
-	}
+    }
 }
diff --git a/crypto/test/src/cms/test/Rfc4134Test.cs b/crypto/test/src/cms/test/Rfc4134Test.cs
index fb617dd64..d7e79b739 100644
--- a/crypto/test/src/cms/test/Rfc4134Test.cs
+++ b/crypto/test/src/cms/test/Rfc4134Test.cs
@@ -1,5 +1,3 @@
-using System;
-
 using NUnit.Framework;
 
 using Org.BouncyCastle.Asn1;
@@ -10,13 +8,12 @@ using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Encoders;
-using Org.BouncyCastle.Utilities.IO;
 using Org.BouncyCastle.Utilities.Test;
 using Org.BouncyCastle.X509;
 
 namespace Org.BouncyCastle.Cms.Tests
 {
-	[TestFixture]
+    [TestFixture]
 	public class Rfc4134Test
 	{
 		private static readonly byte[] exContent = GetRfc4134Data("ExContent.bin");
@@ -245,7 +242,7 @@ namespace Org.BouncyCastle.Cms.Tests
 			Assert.IsTrue(attr.AttrValues[0].Equals(new DerSequence(v)));
 		}
 
-		private void VerifySignatures(CmsSignedData s, byte[] contentDigest)
+		private static void VerifySignatures(CmsSignedData s, byte[] contentDigest)
 		{
 			var x509Certs = s.GetCertificates();
 			SignerInformationStore signers = s.GetSignerInfos();
@@ -268,12 +265,9 @@ namespace Org.BouncyCastle.Cms.Tests
 			}
 		}
 
-		private void VerifySignatures(CmsSignedData s)
-		{
-			VerifySignatures(s, null);
-		}
+		private static void VerifySignatures(CmsSignedData s) => VerifySignatures(s, null);
 
-		private void VerifySignatures(CmsSignedDataParser sp)
+		private static void VerifySignatures(CmsSignedDataParser sp)
 		{
 	        CmsTypedStream sc = sp.GetSignedContent();
 	        if (sc != null)
@@ -296,7 +290,7 @@ namespace Org.BouncyCastle.Cms.Tests
 			}
 		}
 
-		private void VerifySigner(SignerInformation signer, X509Certificate cert)
+		private static void VerifySigner(SignerInformation signer, X509Certificate cert)
 		{
 			if (cert.GetPublicKey() is DsaPublicKeyParameters)
 			{
@@ -317,7 +311,7 @@ namespace Org.BouncyCastle.Cms.Tests
 			}
 		}
 
-		private DsaPublicKeyParameters GetInheritedKey(DsaPublicKeyParameters dsaPubKey)
+		private static DsaPublicKeyParameters GetInheritedKey(DsaPublicKeyParameters dsaPubKey)
 		{
 			X509Certificate cert = new X509CertificateParser().ReadCertificate(
 				GetRfc4134Data("CarlDSSSelf.cer"));
@@ -327,9 +321,6 @@ namespace Org.BouncyCastle.Cms.Tests
 			return new DsaPublicKeyParameters(dsaPubKey.Y, dsaParams);
 		}
 
-		private static byte[] GetRfc4134Data(string name)
-		{
-			return Streams.ReadAll(SimpleTest.GetTestDataAsStream("rfc4134." + name));
-		}
+		private static byte[] GetRfc4134Data(string name) => SimpleTest.GetTestData("rfc4134." + name);
 	}
 }
diff --git a/crypto/test/src/cms/test/SignedDataTest.cs b/crypto/test/src/cms/test/SignedDataTest.cs
index d536111ae..f16edb3f2 100644
--- a/crypto/test/src/cms/test/SignedDataTest.cs
+++ b/crypto/test/src/cms/test/SignedDataTest.cs
@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.IO;
 using System.Text;
 
 using NUnit.Framework;
@@ -12,14 +11,14 @@ using Org.BouncyCastle.Crypto.Operators;
 using Org.BouncyCastle.Pkcs;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Collections;
 using Org.BouncyCastle.Utilities.Encoders;
-using Org.BouncyCastle.Utilities.IO;
 using Org.BouncyCastle.Utilities.Test;
 using Org.BouncyCastle.X509;
 
 namespace Org.BouncyCastle.Cms.Tests
 {
-	[TestFixture]
+    [TestFixture]
 	public class SignedDataTest
 	{
 		private const string OrigDN = "O=Bouncy Castle, C=AU";
@@ -373,67 +372,6 @@ namespace Org.BouncyCastle.Cms.Tests
 			+ "g5+CWIP1dz4F4yB0rNCEGEFigVESryFAzSdJuoM0dgBDI0czNQD9lBQ7l2UW"
 			+ "4fwPDeINgCE2190+uVyEom2E");
 
-		private void VerifySignatures(
-			CmsSignedData	s,
-			byte[]			contentDigest)
-		{
-			var x509Certs = s.GetCertificates();
-
-			SignerInformationStore signers = s.GetSignerInfos();
-			var c = signers.GetSigners();
-
-			foreach (SignerInformation signer in c)
-			{
-				var certCollection = x509Certs.EnumerateMatches(signer.SignerID);
-
-				var certEnum = certCollection.GetEnumerator();
-
-				certEnum.MoveNext();
-				X509Certificate cert = certEnum.Current;
-
-				Assert.IsTrue(signer.Verify(cert));
-		
-				if (contentDigest != null)
-				{
-					Assert.IsTrue(Arrays.AreEqual(contentDigest, signer.GetContentDigest()));
-				}
-			}
-		}
-
-		private void VerifyDirectSignatures(
-			CmsSignedData s,
-			byte[] contentDigest)
-		{
-			var x509Certs = s.GetCertificates();
-
-			SignerInformationStore signers = s.GetSignerInfos();
-			var c = signers.GetSigners();
-
-			foreach (SignerInformation signer in c)
-			{
-				var certCollection = x509Certs.EnumerateMatches(signer.SignerID);
-
-				var certEnum = certCollection.GetEnumerator();
-
-				certEnum.MoveNext();
-				X509Certificate cert = certEnum.Current;
-
-				Assert.IsTrue(signer.Verify(cert));
-				Assert.IsTrue(null == signer.GetEncodedSignedAttributes());
-
-				if (contentDigest != null)
-				{
-					Assert.IsTrue(Arrays.AreEqual(contentDigest, signer.GetContentDigest()));
-				}
-			}
-		}
-
-		private void VerifySignatures(
-			CmsSignedData s)
-		{
-			VerifySignatures(s, null);
-		}
-
 		[Test]
 		public void TestDetachedVerification()
 		{
@@ -1262,21 +1200,6 @@ namespace Org.BouncyCastle.Cms.Tests
 			}
 		}
 
-	    class AsIsSignerInformation : SignerInformation
-		{
-			public AsIsSignerInformation(SignerInformation sInfo): base(sInfo)
-			{
-
-			}
-
-			public override byte[] GetEncodedSignedAttributes()
-			{
-				return signedAttributeSet == null
-					? null
-					: signedAttributeSet.GetEncoded();
-			}
-		}
-
 		[Test]
 		public void TestNullContentWithSigner()
 		{
@@ -1515,6 +1438,7 @@ namespace Org.BouncyCastle.Cms.Tests
 			VerifySignatures(sig);
 		}
 
+        [Test]
         public void TestEncryptionAlgECPublicKey()
         {
             byte[] sigBlock = Base64.Decode(
@@ -1545,41 +1469,40 @@ namespace Org.BouncyCastle.Cms.Tests
             
 			VerifySignatures(signedData);
         }
-        private void DoTestSample(string sigName)
-		{
-			CmsSignedData sig = new CmsSignedData(GetInput(sigName));
-			VerifySignatures(sig);
-		}
-
-        private void DoTestSample(string messageName, string sigName)
-		{
-			CmsSignedData sig = new CmsSignedData(
-				new CmsProcessableByteArray(GetInput(messageName)),
-				GetInput(sigName));
-
-            VerifySignatures(sig);
-		}
-
-        private byte[] GetInput(string name)
-		{
-			return Streams.ReadAll(SimpleTest.GetTestDataAsStream("cms.sigs." + name));
-		}
 
         [Test]
 		public void TestForMultipleCounterSignatures()
 		{
 			CmsSignedData sd = new CmsSignedData(xtraCounterSig);
 
-			foreach (SignerInformation sigI in sd.GetSignerInfos().GetSigners())
+			foreach (SignerInformation sigI in sd.GetSignerInfos())
 			{
 				SignerInformationStore counter = sigI.GetCounterSignatures();
-				var sigs = counter.GetSigners();
+                Assert.AreEqual(2, counter.Count);
 
-				Assert.AreEqual(2, sigs.Count);
+				var sigs = counter.GetSigners();
+                Assert.AreEqual(2, sigs.Count);
 			}
 		}
 
-        private void RsaDigestTest(string signatureAlgorithmName)
+        private static void DoTestSample(string sigName)
+        {
+            CmsSignedData sig = new CmsSignedData(GetInput(sigName));
+            VerifySignatures(sig);
+        }
+
+        private static void DoTestSample(string messageName, string sigName)
+        {
+            CmsSignedData sig = new CmsSignedData(
+                new CmsProcessableByteArray(GetInput(messageName)),
+                GetInput(sigName));
+
+            VerifySignatures(sig);
+        }
+
+        private static byte[] GetInput(string name) => SimpleTest.GetTestData("cms.sigs." + name);
+
+        private static void RsaDigestTest(string signatureAlgorithmName)
         {
             byte[] data = Encoding.ASCII.GetBytes("Hello World!");
             CmsProcessable msg = new CmsProcessableByteArray(data);
@@ -1601,30 +1524,79 @@ namespace Org.BouncyCastle.Cms.Tests
             VerifySignatures(s, DigestUtilities.CalculateDigest(digestName, data));
         }
 
-        private void VerifySignatures(
-			CmsSignedDataParser sp)
+        private static void VerifyDirectSignatures(CmsSignedData s, byte[] contentDigest)
+        {
+            var x509Certs = s.GetCertificates();
+            var signers = s.GetSignerInfos();
+
+            foreach (var signer in signers)
+            {
+                var matches = x509Certs.EnumerateMatches(signer.SignerID);
+                var cert = CollectionUtilities.GetFirstOrNull(matches);
+
+                Assert.IsTrue(signer.Verify(cert));
+                Assert.IsTrue(null == signer.GetEncodedSignedAttributes());
+
+                if (contentDigest != null)
+                {
+                    Assert.IsTrue(Arrays.AreEqual(contentDigest, signer.GetContentDigest()));
+                }
+            }
+        }
+
+        private static void VerifySignatures(CmsSignedData s) => VerifySignatures(s, null);
+
+        private static void VerifySignatures(CmsSignedData s, byte[] contentDigest)
+        {
+            var x509Certs = s.GetCertificates();
+            var signers = s.GetSignerInfos();
+
+            foreach (var signer in signers)
+            {
+                var matches = x509Certs.EnumerateMatches(signer.SignerID);
+                var cert = CollectionUtilities.GetFirstOrNull(matches);
+
+                Assert.IsTrue(signer.Verify(cert));
+
+                if (contentDigest != null)
+                {
+                    Assert.IsTrue(Arrays.AreEqual(contentDigest, signer.GetContentDigest()));
+                }
+            }
+        }
+
+        private static void VerifySignatures(CmsSignedDataParser sp)
 		{
 			var x509Certs = sp.GetCertificates();
-			SignerInformationStore signers = sp.GetSignerInfos();
+			var signers = sp.GetSignerInfos();
 
-			foreach (SignerInformation signer in signers.GetSigners())
+			foreach (var signer in signers)
 			{
-				var certCollection = x509Certs.EnumerateMatches(signer.SignerID);
-
-				var certEnum = certCollection.GetEnumerator();
-				certEnum.MoveNext();
-				X509Certificate cert = certEnum.Current;
+				var matches = x509Certs.EnumerateMatches(signer.SignerID);
+				var cert = CollectionUtilities.GetFirstOrNull(matches);
 
 				Assert.IsTrue(signer.Verify(cert));
 				Assert.IsTrue(new MySignerInformation(signer).Verify(cert)); // test simple copy works
 			}
 		}
 
-		class MySignerInformation: SignerInformation
+        private class AsIsSignerInformation
+			: SignerInformation
+        {
+            public AsIsSignerInformation(SignerInformation sInfo)
+				: base(sInfo)
+            {
+            }
+
+			public override byte[] GetEncodedSignedAttributes() => signedAttributeSet?.GetEncoded();
+        }
+
+        private class MySignerInformation
+			: SignerInformation
 		{
-			public MySignerInformation(SignerInformation sigInf): base(sigInf)
+			public MySignerInformation(SignerInformation sigInf)
+				: base(sigInf)
 			{
-
 			}
 		}
     }
diff --git a/crypto/test/src/util/test/SimpleTest.cs b/crypto/test/src/util/test/SimpleTest.cs
index 21b4daabb..d83c003ee 100644
--- a/crypto/test/src/util/test/SimpleTest.cs
+++ b/crypto/test/src/util/test/SimpleTest.cs
@@ -3,100 +3,59 @@ using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
 
+using Org.BouncyCastle.Utilities.IO;
+
 namespace Org.BouncyCastle.Utilities.Test
 {
     public abstract class SimpleTest
         : ITest
     {
-		public abstract string Name
-		{
-			get;
-		}
+        internal static readonly string NewLine = Environment.NewLine;
 
-		private ITestResult Success()
-        {
-            return SimpleTestResult.Successful(this, "Okay");
-        }
+        public abstract string Name { get; }
 
-        internal void Fail(
-            string message)
-        {
-            throw new TestFailedException(SimpleTestResult.Failed(this, message));
-        }
+		private ITestResult Success() => SimpleTestResult.Successful(this, "Okay");
 
-        internal void Fail(
-            string		message,
-            Exception	throwable)
-        {
+        internal void Fail(string message) => throw new TestFailedException(SimpleTestResult.Failed(this, message));
+
+        internal void Fail(string message, Exception throwable) =>
             throw new TestFailedException(SimpleTestResult.Failed(this, message, throwable));
-        }
 
-		internal void Fail(
-            string message,
-            object expected,
-            object found)
-        {
+		internal void Fail(string message, object expected, object found) =>
             throw new TestFailedException(SimpleTestResult.Failed(this, message, expected, found));
-        }
 
-        internal void IsTrue(bool value)
+        internal void FailIf(string message, bool condition)
         {
-            if (!value)
-                throw new TestFailedException(SimpleTestResult.Failed(this, "no message"));
+            if (condition)
+            {
+                Fail(message);
+            }
         }
 
-        internal void IsTrue(string message, bool value)
-        {
-            if (!value)
-                throw new TestFailedException(SimpleTestResult.Failed(this, message));
-        }
+        internal void IsTrue(bool value) => IsTrue("no message", value);
 
-        internal void IsEquals(object a, object b)
-        {
-            if (!a.Equals(b))
-                throw new TestFailedException(SimpleTestResult.Failed(this, "no message"));
-        }
+        internal void IsTrue(string message, bool value) => FailIf(message, !value);
 
-        internal void IsEquals(int a, int b)
-        {
-            if (a != b)
-                throw new TestFailedException(SimpleTestResult.Failed(this, "no message"));
-        }
+        internal void IsEquals(bool a, bool b) => IsEquals("no message", a, b);
 
-        internal void IsEquals(string message, bool a, bool b)
-        {
-            if (a != b)
-                throw new TestFailedException(SimpleTestResult.Failed(this, message));
-        }
+        internal void IsEquals(int a, int b) => IsEquals("no message", a, b);
 
-        internal void IsEquals(string message, long a, long b)
-        {
-            if (a != b)
-                throw new TestFailedException(SimpleTestResult.Failed(this, message));
-        }
+        internal void IsEquals(long a, long b) => IsEquals("no message", a, b);
 
-        internal void IsEquals(string message, object a, object b)
-        {
-            if (a == null && b == null)
-                return;
-
-            if (a == null)
-                throw new TestFailedException(SimpleTestResult.Failed(this, message));
-            if (b == null)
-                throw new TestFailedException(SimpleTestResult.Failed(this, message));
-            if (!a.Equals(b))
-                throw new TestFailedException(SimpleTestResult.Failed(this, message));
-        }
+        internal void IsEquals(object a, object b) => IsEquals("no message", a, b);
 
-        internal bool AreEqual(byte[] a, byte[] b)
-        {
-            return Arrays.AreEqual(a, b);
-        }
+        internal void IsEquals(string message, bool a, bool b) => FailIf(message, a != b);
 
-        internal bool AreEqual(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)
-        {
-            return Arrays.AreEqual(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex);
-        }
+        internal void IsEquals(string message, int a, int b) => FailIf(message, a != b);
+
+        internal void IsEquals(string message, long a, long b) => FailIf(message, a != b);
+
+        internal void IsEquals(string message, object a, object b) => FailIf(message, !Objects.Equals(a, b));
+
+        internal bool AreEqual(byte[] a, byte[] b) => Arrays.AreEqual(a, b);
+
+        internal bool AreEqual(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) =>
+            Arrays.AreEqual(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex);
 
 		public virtual ITestResult Perform()
         {
@@ -116,15 +75,9 @@ namespace Org.BouncyCastle.Utilities.Test
             }
         }
 
-		internal static void RunTest(
-            ITest test)
-        {
-            RunTest(test, Console.Out);
-        }
+		internal static void RunTest(ITest test) => RunTest(test, Console.Out);
 
-		internal static void RunTest(
-            ITest		test,
-            TextWriter	outStream)
+		internal static void RunTest(ITest test, TextWriter outStream)
         {
             ITestResult result = test.Perform();
 
@@ -135,16 +88,12 @@ namespace Org.BouncyCastle.Utilities.Test
             }
         }
 
-		internal static Stream GetTestDataAsStream(
-			string name)
-		{
-			string fullName = GetFullName(name);
+        internal static byte[] GetTestData(string name) => Streams.ReadAll(GetTestDataAsStream(name));
 
-			return GetAssembly().GetManifestResourceStream(fullName);
-		}
+        internal static Stream GetTestDataAsStream(string name) =>
+            GetAssembly().GetManifestResourceStream(GetFullName(name));
 
-		internal static string[] GetTestDataEntries(
-			string prefix)
+		internal static string[] GetTestDataEntries(string prefix)
 		{
 			string fullPrefix = GetFullName(prefix);
 
@@ -161,36 +110,19 @@ namespace Org.BouncyCastle.Utilities.Test
             return result.ToArray();
 		}
 
-        private static Assembly GetAssembly()
-        {
-            return typeof(SimpleTest).Assembly;
-        }
-
-        private static string GetFullName(string name)
-		{
-            return "Org.BouncyCastle.data." + name;
-        }
-
-        private static string GetShortName(string fullName)
-		{
-            return fullName.Substring("Org.BouncyCastle.data.".Length);
-		}
+        private static Assembly GetAssembly() => typeof(SimpleTest).Assembly;
 
-		private static string GetNewLine()
-		{
-			return Environment.NewLine;
-		}
+        private static string GetFullName(string name) => "Org.BouncyCastle.data." + name;
 
-		internal static readonly string NewLine = GetNewLine();
+        private static string GetShortName(string fullName) => fullName.Substring("Org.BouncyCastle.data.".Length);
 
 		public abstract void PerformTest();
 
-        public static DateTime MakeUtcDateTime(int year, int month, int day, int hour, int minute, int second)
-        {
-            return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc);
-        }
+        public static DateTime MakeUtcDateTime(int year, int month, int day, int hour, int minute, int second) =>
+            new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc);
 
-        public static DateTime MakeUtcDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
+        public static DateTime MakeUtcDateTime(int year, int month, int day, int hour, int minute, int second,
+            int millisecond)
         {
             return new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc);
         }