summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-06-09 20:08:35 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-06-09 20:08:35 +0700
commitde9a5f1debfe184c48ca76aadfebb86cf08cbdb0 (patch)
treeaed08eef9b2cf8e54da82152fa6d273e4450b80d /crypto
parentUpdate xmldoc (diff)
downloadBouncyCastle.NET-ed25519-de9a5f1debfe184c48ca76aadfebb86cf08cbdb0.tar.xz
Port missing test cases from Java
- Switch to newer test format
Diffstat (limited to 'crypto')
-rw-r--r--crypto/test/src/asn1/test/MiscTest.cs126
1 files changed, 90 insertions, 36 deletions
diff --git a/crypto/test/src/asn1/test/MiscTest.cs b/crypto/test/src/asn1/test/MiscTest.cs
index 42f5dbd01..6b8411996 100644
--- a/crypto/test/src/asn1/test/MiscTest.cs
+++ b/crypto/test/src/asn1/test/MiscTest.cs
@@ -12,9 +12,72 @@ namespace Org.BouncyCastle.Asn1.Tests
 {
     [TestFixture]
     public class MiscTest
-        : ITest
+        : SimpleTest
     {
-        public ITestResult Perform()
+        private void DoShouldFailOnExtraData()
+        {
+            // basic construction
+            DerBitString s1 = new DerBitString(new byte[0], 0);
+
+            Asn1Object.FromByteArray(s1.GetEncoded());
+
+            Asn1Object.FromByteArray(new BerSequence(s1).GetEncoded());
+
+            try
+            {
+                Asn1Object obj = Asn1Object.FromByteArray(Arrays.Concatenate(s1.GetEncoded(), new byte[1]));
+                Fail("no exception");
+            }
+            catch (IOException e)
+            {
+                //if (!"Extra data detected in stream".Equals(e.Message))
+                if (!"extra data found after object".Equals(e.Message))
+                {
+                        Fail("wrong exception");
+                }
+            }
+        }
+
+        private void DoDerIntegerTest()
+        {
+            try
+            {
+                new DerInteger(new byte[] { 0, 0, 0, 1});
+            }
+            catch (ArgumentException e)
+            {
+                IsTrue("wrong exc", "malformed integer".Equals(e.Message));
+            }
+
+            try
+            {
+                new DerInteger(new byte[] {(byte)0xff, (byte)0x80, 0, 1});
+            }
+            catch (ArgumentException e)
+            {
+                IsTrue("wrong exc", "malformed integer".Equals(e.Message));
+            }
+
+            try
+            {
+                new DerEnumerated(new byte[] { 0, 0, 0, 1});
+            }
+            catch (ArgumentException e)
+            {
+                IsTrue("wrong exc", "malformed enumerated".Equals(e.Message));
+            }
+
+            try
+            {
+                new DerEnumerated(new byte[] {(byte)0xff, (byte)0x80, 0, 1});
+            }
+            catch (ArgumentException e)
+            {
+                IsTrue("wrong exc", "malformed enumerated".Equals(e.Message));
+            }
+        }
+
+        public override void PerformTest()
         {
             byte[] testIv = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
@@ -29,47 +92,41 @@ namespace Org.BouncyCastle.Asn1.Tests
 
             byte[] data = Base64.Decode("MA4ECAECAwQFBgcIAgIAgAMCBSAWBWhlbGxvMAoECAECAwQFBgcIFgtodHRwOi8vdGVzdA==");
 
-            try
+            MemoryStream bOut = new MemoryStream();
+            Asn1OutputStream aOut = new Asn1OutputStream(bOut);
+
+            for (int i = 0; i != values.Length; i++)
             {
-                MemoryStream bOut = new MemoryStream();
-                Asn1OutputStream aOut = new Asn1OutputStream(bOut);
+                aOut.WriteObject(values[i]);
+            }
 
-                for (int i = 0; i != values.Length; i++)
-                {
-                    aOut.WriteObject(values[i]);
-                }
+            if (!Arrays.AreEqual(bOut.ToArray(), data))
+            {
+                Fail("Failed data check");
+            }
 
-                if (!Arrays.AreEqual(bOut.ToArray(), data))
-                {
-                    return new SimpleTestResult(false, Name + ": Failed data check");
-                }
+            Asn1InputStream aIn = new Asn1InputStream(bOut.ToArray());
 
-                Asn1InputStream aIn = new Asn1InputStream(bOut.ToArray());
+            for (int i = 0; i != values.Length; i++)
+            {
+                Asn1Object o = aIn.ReadObject();
 
-                for (int i = 0; i != values.Length; i++)
+                if (!values[i].Equals(o))
                 {
-                    Asn1Object o = aIn.ReadObject();
-
-                    if (!values[i].Equals(o))
-                    {
-                        return new SimpleTestResult(false, Name + ": Failed equality test for " + o);
-                    }
-
-                    if (o.GetHashCode() != values[i].GetHashCode())
-                    {
-                        return new SimpleTestResult(false, Name + ": Failed hashCode test for " + o);
-                    }
+                    Fail("Failed equality test for " + o);
                 }
 
-                return new SimpleTestResult(true, Name + ": Okay");
-            }
-            catch (Exception e)
-            {
-                return new SimpleTestResult(false, Name + ": Failed - exception " + e.ToString(), e);
+                if (o.GetHashCode() != values[i].GetHashCode())
+                {
+                    Fail("Failed hashCode test for " + o);
+                }
             }
+
+            DoShouldFailOnExtraData();
+            DoDerIntegerTest();
         }
 
-        public string Name
+        public override string Name
         {
             get { return "Misc"; }
         }
@@ -77,10 +134,7 @@ namespace Org.BouncyCastle.Asn1.Tests
         public static void Main(
             string[] args)
         {
-            ITest test = new MiscTest();
-            ITestResult result = test.Perform();
-
-            Console.WriteLine(result);
+            RunTest(new MiscTest());
         }
 
         [Test]