summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-05 14:45:49 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-05 14:45:49 +0700
commit6b59dbbece3b1509b83b8479259a3c6059e9a60b (patch)
tree3dca66ea9a1e8aa02ac6099cde5844155783bd7b /crypto/test
parentRefactor Asn1Set classes (diff)
downloadBouncyCastle.NET-ed25519-6b59dbbece3b1509b83b8479259a3c6059e9a60b.tar.xz
Support 'leaveOpen' in Asn1 streams
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs32
-rw-r--r--crypto/test/src/asn1/test/MiscTest.cs35
-rw-r--r--crypto/test/src/asn1/test/ParsingTest.cs43
-rw-r--r--crypto/test/src/cms/test/SignedDataTest.cs20
4 files changed, 67 insertions, 63 deletions
diff --git a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
index fb035de18..2ae316f33 100644
--- a/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
+++ b/crypto/test/src/asn1/test/EqualsAndHashCodeTest.cs
@@ -54,26 +54,28 @@ namespace Org.BouncyCastle.Asn1.Tests
             };
 
 			MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = Asn1OutputStream.Create(bOut);
-
-            for (int i = 0; i != values.Length; i++)
+            using (var asn1Out = Asn1OutputStream.Create(bOut))
             {
-                aOut.WriteObject(values[i]);
-            }
-
-			Asn1InputStream aIn = new Asn1InputStream(bOut.ToArray());
-
-			for (int i = 0; i != values.Length; i++)
-            {
-                Asn1Object o = aIn.ReadObject();
-                if (!o.Equals(values[i]))
+                for (int i = 0; i != values.Length; i++)
                 {
-                    Fail("Failed equality test for " + o.GetType().Name);
+                    asn1Out.WriteObject(values[i]);
                 }
+            }
 
-                if (o.GetHashCode() != values[i].GetHashCode())
+            byte[] output = bOut.ToArray();
+            using (var asn1In = new Asn1InputStream(output))
+            {
+                for (int i = 0; i != values.Length; i++)
                 {
-                    Fail("Failed hashCode test for " + o.GetType().Name);
+                    Asn1Object o = asn1In.ReadObject();
+                    if (!o.Equals(values[i]))
+                    {
+                        Fail("Failed equality test for " + o.GetType().Name);
+                    }
+                    if (o.GetHashCode() != values[i].GetHashCode())
+                    {
+                        Fail("Failed hashCode test for " + o.GetType().Name);
+                    }
                 }
             }
         }
diff --git a/crypto/test/src/asn1/test/MiscTest.cs b/crypto/test/src/asn1/test/MiscTest.cs
index 96dfa4c3f..0cbf6e8fc 100644
--- a/crypto/test/src/asn1/test/MiscTest.cs
+++ b/crypto/test/src/asn1/test/MiscTest.cs
@@ -105,32 +105,33 @@ namespace Org.BouncyCastle.Asn1.Tests
             byte[] data = Base64.Decode("MA4ECAECAwQFBgcIAgIAgAMCBSAWBWhlbGxvMAoECAECAwQFBgcIFgtodHRwOi8vdGVzdA==");
 
             MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = Asn1OutputStream.Create(bOut);
-
-            for (int i = 0; i != values.Length; i++)
+            using (var asn1Out = Asn1OutputStream.Create(bOut))
             {
-                aOut.WriteObject(values[i]);
+                for (int i = 0; i != values.Length; i++)
+                {
+                    asn1Out.WriteObject(values[i]);
+                }
             }
 
-            if (!Arrays.AreEqual(bOut.ToArray(), data))
+            byte[] output = bOut.ToArray();
+            if (!Arrays.AreEqual(output, data))
             {
                 Fail("Failed data check");
             }
 
-            Asn1InputStream aIn = new Asn1InputStream(bOut.ToArray());
-
-            for (int i = 0; i != values.Length; i++)
+            using (var asn1In = new Asn1InputStream(output))
             {
-                Asn1Object o = aIn.ReadObject();
-
-                if (!values[i].Equals(o))
-                {
-                    Fail("Failed equality test for " + o);
-                }
-
-                if (o.GetHashCode() != values[i].GetHashCode())
+                for (int i = 0; i != values.Length; i++)
                 {
-                    Fail("Failed hashCode test for " + o);
+                    Asn1Object o = asn1In.ReadObject();
+                    if (!values[i].Equals(o))
+                    {
+                        Fail("Failed equality test for " + o);
+                    }
+                    if (o.GetHashCode() != values[i].GetHashCode())
+                    {
+                        Fail("Failed hashCode test for " + o);
+                    }
                 }
             }
 
diff --git a/crypto/test/src/asn1/test/ParsingTest.cs b/crypto/test/src/asn1/test/ParsingTest.cs
index 43a97555f..c5d2eed9c 100644
--- a/crypto/test/src/asn1/test/ParsingTest.cs
+++ b/crypto/test/src/asn1/test/ParsingTest.cs
@@ -37,7 +37,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 
 		public override void PerformTest()
 		{
-			inputStreamTest();
+			InputStreamTest();
 			parserTest();
 		}
 
@@ -68,31 +68,32 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 		}
 
-		private void inputStreamTest()
+		private void InputStreamTest()
 		{
 			foreach (string stream in streams)
 			{
-				Asn1InputStream aIn = new Asn1InputStream(Base64.Decode(stream));
-
-				try
+				using (var aIn = new Asn1InputStream(Base64.Decode(stream)))
 				{
-					object obj;
-					while ((obj = aIn.ReadObject()) != null)
-					{
-					}
+                    try
+                    {
+                        object obj;
+                        while ((obj = aIn.ReadObject()) != null)
+                        {
+                        }
 
-					Fail("bad stream parsed successfully!");
-				}
-				catch (IOException)
-				{
-					// ignore
-				}
-				// Note: C# may throw these instead, since no InMemoryRepresentable support
-				catch (Asn1ParsingException)
-				{
-					// ignore
-				}
-			}
+                        Fail("bad stream parsed successfully!");
+                    }
+                    catch (IOException)
+                    {
+                        // ignore
+                    }
+                    // Note: C# may throw these instead, since no InMemoryRepresentable support
+                    catch (Asn1ParsingException)
+                    {
+                        // ignore
+                    }
+                }
+            }
 		}
 	}
 }
diff --git a/crypto/test/src/cms/test/SignedDataTest.cs b/crypto/test/src/cms/test/SignedDataTest.cs
index d0702c836..9d1d18ad3 100644
--- a/crypto/test/src/cms/test/SignedDataTest.cs
+++ b/crypto/test/src/cms/test/SignedDataTest.cs
@@ -1057,12 +1057,12 @@ namespace Org.BouncyCastle.Cms.Tests
 
 			Assert.AreEqual(3, s.Version);
 
-			MemoryStream bIn = new MemoryStream(s.GetEncoded(), false);
-			Asn1InputStream aIn = new Asn1InputStream(bIn);
-
-			s = new CmsSignedData(ContentInfo.GetInstance(aIn.ReadObject()));
+			using (var aIn = new Asn1InputStream(s.GetEncoded()))
+			{
+                s = new CmsSignedData(ContentInfo.GetInstance(aIn.ReadObject()));
+            }
 
-			x509Certs = s.GetCertificates();
+            x509Certs = s.GetCertificates();
 			x509Crls = s.GetCrls();
 
 			SignerInformationStore signers = s.GetSignerInfos();
@@ -1101,12 +1101,12 @@ namespace Org.BouncyCastle.Cms.Tests
 
 			s = gen.Generate(msg, true);
 
-			bIn = new MemoryStream(s.GetEncoded(), false);
-			aIn = new Asn1InputStream(bIn);
-
-			s = new CmsSignedData(ContentInfo.GetInstance(aIn.ReadObject()));
+			using (var aIn = new Asn1InputStream(s.GetEncoded()))
+			{
+                s = new CmsSignedData(ContentInfo.GetInstance(aIn.ReadObject()));
+            }
 
-			x509Certs = s.GetCertificates();
+            x509Certs = s.GetCertificates();
 			x509Crls = s.GetCrls();
 
 			signers = s.GetSignerInfos();