summary refs log tree commit diff
path: root/crypto/test/src/util
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2013-06-28 15:26:06 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2013-06-28 15:26:06 +0700
commit44288db4414158ac9b98a507b15e81d0d3c66ca6 (patch)
treeaa5ef88948ebb68ed6c8df81eb5da889641a9b50 /crypto/test/src/util
parentSet up text/binary handling for existing file types (diff)
downloadBouncyCastle.NET-ed25519-44288db4414158ac9b98a507b15e81d0d3c66ca6.tar.xz
Initial import of old CVS repository
Diffstat (limited to 'crypto/test/src/util')
-rw-r--r--crypto/test/src/util/io/pem/test/AllTests.cs77
-rw-r--r--crypto/test/src/util/net/test/IPAddressTest.cs61
-rw-r--r--crypto/test/src/util/test/FixedSecureRandom.cs64
-rw-r--r--crypto/test/src/util/test/ITest.cs17
-rw-r--r--crypto/test/src/util/test/ITestResult.cs13
-rw-r--r--crypto/test/src/util/test/NumberParsing.cs40
-rw-r--r--crypto/test/src/util/test/SimpleTest.cs164
-rw-r--r--crypto/test/src/util/test/SimpleTestResult.cs91
-rw-r--r--crypto/test/src/util/test/TestFailedException.cs24
-rw-r--r--crypto/test/src/util/test/UncloseableStream.cs22
10 files changed, 573 insertions, 0 deletions
diff --git a/crypto/test/src/util/io/pem/test/AllTests.cs b/crypto/test/src/util/io/pem/test/AllTests.cs
new file mode 100644
index 000000000..b44949383
--- /dev/null
+++ b/crypto/test/src/util/io/pem/test/AllTests.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+using System.IO;
+using System.Text;
+
+using NUnit.Core;
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Generators;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities.Collections;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Utilities.IO.Pem.Tests
+{
+	[TestFixture]
+	public class AllTests
+	{
+		[Suite]
+		public static TestSuite Suite
+		{
+			get
+			{
+				TestSuite suite = new TestSuite("PEM Utilities Tests");
+				suite.Add(new AllTests());
+				return suite;
+			}
+		}
+
+		[Test]
+		public void TestPemLength()
+		{
+			for (int i = 1; i != 60; i++)
+			{
+				lengthTest("CERTIFICATE", new ArrayList(), new byte[i]);
+			}
+
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[100]);
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[101]);
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[102]);
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[103]);
+
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[1000]);
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[1001]);
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[1002]);
+			lengthTest("CERTIFICATE", new ArrayList(), new byte[1003]);
+
+			IList headers = new ArrayList();
+			headers.Add(new PemHeader("Proc-Type", "4,ENCRYPTED"));
+			headers.Add(new PemHeader("DEK-Info", "DES3,0001020304050607"));
+			lengthTest("RSA PRIVATE KEY", headers, new byte[103]);
+		}
+
+		private void lengthTest(string type, IList headers, byte[] data)
+		{
+			StringWriter sw = new StringWriter();
+			PemWriter pWrt = new PemWriter(sw);
+
+			PemObject pemObj = new PemObject(type, headers, data);
+			pWrt.WriteObject(pemObj);
+			pWrt.Writer.Close();
+
+			Assert.AreEqual(sw.ToString().Length, pWrt.GetOutputSize(pemObj));
+		}
+
+		public static void Main(
+			string[] args)
+        {
+            //junit.textui.TestRunner.run(suite());
+            EventListener el = new NullListener();
+            Suite.Run(el);
+        }
+	}
+}
diff --git a/crypto/test/src/util/net/test/IPAddressTest.cs b/crypto/test/src/util/net/test/IPAddressTest.cs
new file mode 100644
index 000000000..ec40c528a
--- /dev/null
+++ b/crypto/test/src/util/net/test/IPAddressTest.cs
@@ -0,0 +1,61 @@
+using System;
+
+using NUnit.Framework;
+
+namespace Org.BouncyCastle.Utilities.Net.Tests
+{
+	[TestFixture]
+	public class IPTest
+	{
+		private static readonly string[] validIP4v = new string[]
+		{ "0.0.0.0", "255.255.255.255", "192.168.0.0" };
+
+		private static readonly string[] invalidIP4v = new string[]
+		{ "0.0.0.0.1", "256.255.255.255", "1", "A.B.C", "1:.4.6.5" };
+
+		private static readonly string[] validIP6v = new string[]
+		{ "0:0:0:0:0:0:0:0", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
+				"0:1:2:3:FFFF:5:FFFF:1" };
+
+		private static readonly string[] invalidIP6v = new string[]
+		{ "0.0.0.0:1", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFFF" };
+
+		private void doTestIP(
+			string[]	valid,
+			string[]	invalid)
+		{
+			for (int i = 0; i < valid.Length; i++)
+			{
+				if (!IPAddress.IsValid(valid[i]))
+				{
+					Assert.Fail("Valid input string not accepted: " + valid[i] + ".");
+				}
+			}
+
+			for (int i = 0; i < invalid.Length; i++)
+			{
+				if (IPAddress.IsValid(invalid[i]))
+				{
+					Assert.Fail("Invalid input string accepted: " + invalid[i] + ".");
+				}
+			}
+		}
+
+		public string Name
+		{
+			get { return "IPTest"; }
+		}
+
+		[Test]
+		public void TestIPv4()
+		{
+			doTestIP(validIP4v, invalidIP4v);
+		}
+
+		[Test]
+		public void TestIPv6()
+		{
+			doTestIP(validIP6v, invalidIP6v);
+		}
+	}
+}
diff --git a/crypto/test/src/util/test/FixedSecureRandom.cs b/crypto/test/src/util/test/FixedSecureRandom.cs
new file mode 100644
index 000000000..15a2e9bb3
--- /dev/null
+++ b/crypto/test/src/util/test/FixedSecureRandom.cs
@@ -0,0 +1,64 @@
+using System;
+using System.IO;
+
+using Org.BouncyCastle.Security;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+	public class FixedSecureRandom
+		: SecureRandom
+	{
+		private byte[]       _data;
+		private int          _index;
+
+		protected FixedSecureRandom(
+			byte[] data)
+		{
+			_data = data;
+		}
+
+		public static FixedSecureRandom From(
+			params byte[][] values)
+		{
+			MemoryStream bOut = new MemoryStream();
+
+			for (int i = 0; i != values.Length; i++)
+			{
+				try
+				{
+					byte[] v = values[i];
+					bOut.Write(v, 0, v.Length);
+				}
+				catch (IOException)
+				{
+					throw new ArgumentException("can't save value array.");
+				}
+			}
+
+			return new FixedSecureRandom(bOut.ToArray());
+		}
+
+		public override void NextBytes(
+			byte[] buf)
+		{
+			Array.Copy(_data, _index, buf, 0, buf.Length);
+
+			_index += buf.Length;
+		}
+
+		public override void NextBytes(
+			byte[]	buf,
+			int		off,
+			int		len)
+		{
+			Array.Copy(_data, _index, buf, off, len);
+
+			_index += len;
+		}
+
+		public bool IsExhausted
+		{
+			get { return _index == _data.Length; }
+		}
+	}
+}
diff --git a/crypto/test/src/util/test/ITest.cs b/crypto/test/src/util/test/ITest.cs
new file mode 100644
index 000000000..30ad82780
--- /dev/null
+++ b/crypto/test/src/util/test/ITest.cs
@@ -0,0 +1,17 @@
+using System;
+
+using NUnit.Framework;
+
+/*
+ Basic test interface
+ */
+namespace Org.BouncyCastle.Utilities.Test
+{
+    public interface ITest
+    {
+        string Name { get; }
+
+		[Test]
+        ITestResult Perform();
+    }
+}
diff --git a/crypto/test/src/util/test/ITestResult.cs b/crypto/test/src/util/test/ITestResult.cs
new file mode 100644
index 000000000..25f0442de
--- /dev/null
+++ b/crypto/test/src/util/test/ITestResult.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+    public interface ITestResult
+    {
+        bool IsSuccessful();
+
+        Exception GetException();
+
+        string ToString();
+    }
+}
diff --git a/crypto/test/src/util/test/NumberParsing.cs b/crypto/test/src/util/test/NumberParsing.cs
new file mode 100644
index 000000000..f9624ef0a
--- /dev/null
+++ b/crypto/test/src/util/test/NumberParsing.cs
@@ -0,0 +1,40 @@
+using System;
+using System.Globalization;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+    /**
+    * Parsing
+    */
+    public sealed class NumberParsing
+    {
+        private NumberParsing()
+        {
+            // Hide constructor
+        }
+
+		public static long DecodeLongFromHex(
+			string longAsString)
+        {
+            if ((longAsString[1] == 'x')
+                || (longAsString[1] == 'X'))
+            {
+                longAsString = longAsString.Substring(2);
+            }
+
+			return long.Parse(longAsString, NumberStyles.HexNumber);
+        }
+
+		public static int DecodeIntFromHex(
+			string intAsString)
+        {
+            if ((intAsString[1] == 'x')
+                || (intAsString[1] == 'X'))
+            {
+                intAsString = intAsString.Substring(2);
+            }
+
+			return int.Parse(intAsString, NumberStyles.HexNumber);
+        }
+    }
+}
diff --git a/crypto/test/src/util/test/SimpleTest.cs b/crypto/test/src/util/test/SimpleTest.cs
new file mode 100644
index 000000000..be846e20f
--- /dev/null
+++ b/crypto/test/src/util/test/SimpleTest.cs
@@ -0,0 +1,164 @@
+using System;
+using System.Collections;
+using System.IO;
+using System.Reflection;
+using System.Text;
+
+using Org.BouncyCastle.Utilities;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+    public abstract class SimpleTest
+        : ITest
+    {
+		public abstract string Name
+		{
+			get;
+		}
+
+		private ITestResult Success()
+        {
+            return SimpleTestResult.Successful(this, "Okay");
+        }
+
+        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)
+        {
+            throw new TestFailedException(SimpleTestResult.Failed(this, message, expected, found));
+        }
+
+		internal bool AreEqual(
+            byte[] a,
+            byte[] b)
+        {
+			return Arrays.AreEqual(a, b);
+		}
+
+		public virtual ITestResult Perform()
+        {
+            try
+            {
+                PerformTest();
+
+				return Success();
+            }
+            catch (TestFailedException e)
+            {
+                return e.GetResult();
+            }
+            catch (Exception e)
+            {
+                return SimpleTestResult.Failed(this, "Exception: " +  e, e);
+            }
+        }
+
+		internal static void RunTest(
+            ITest test)
+        {
+            RunTest(test, Console.Out);
+        }
+
+		internal static void RunTest(
+            ITest		test,
+            TextWriter	outStream)
+        {
+            ITestResult result = test.Perform();
+
+			outStream.WriteLine(result.ToString());
+            if (result.GetException() != null)
+            {
+                outStream.WriteLine(result.GetException().StackTrace);
+            }
+        }
+
+		internal static Stream GetTestDataAsStream(
+			string name)
+		{
+			string fullName = GetFullName(name);
+
+			return Assembly.GetExecutingAssembly().GetManifestResourceStream(fullName);
+		}
+
+		internal static string[] GetTestDataEntries(
+			string prefix)
+		{
+			string fullPrefix = GetFullName(prefix);
+
+			ArrayList result = new ArrayList();
+			string[] fullNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
+			foreach (string fullName in fullNames)
+			{
+				if (fullName.StartsWith(fullPrefix))
+				{
+					string name = GetShortName(fullName);
+					result.Add(name);
+				}
+			}
+			return (string[])result.ToArray(typeof(String));
+		}
+
+		private static string GetFullName(
+			string name)
+		{
+// TODO MonoDevelop/Visual Studio embedded resource ids still inconsistent
+#if BC_BUILD_MONODEVELOP
+			return "test.data." + name;
+#else
+			return "crypto.test.data." + name;
+#endif
+		}
+
+		private static string GetShortName(
+			string fullName)
+		{
+// TODO MonoDevelop/Visual Studio embedded resource ids still inconsistent
+#if BC_BUILD_MONODEVELOP
+			return fullName.Substring("test.data.".Length);
+#else
+			return fullName.Substring("crypto.test.data.".Length);
+#endif
+		}
+
+#if NETCF_1_0 || NETCF_2_0
+		private static string GetNewLine()
+		{
+			MemoryStream buf = new MemoryStream();
+			StreamWriter w = new StreamWriter(buf, Encoding.ASCII);
+			w.WriteLine();
+			w.Close();
+			byte[] bs = buf.ToArray();
+			return Encoding.ASCII.GetString(bs, 0, bs.Length);
+		}
+
+		internal static string GetEnvironmentVariable(
+			string variable)
+		{
+			return null;
+		}
+#else
+		private static string GetNewLine()
+		{
+			return Environment.NewLine;
+		}
+#endif
+
+		internal static readonly string NewLine = GetNewLine();
+
+		public abstract void PerformTest();
+    }
+}
diff --git a/crypto/test/src/util/test/SimpleTestResult.cs b/crypto/test/src/util/test/SimpleTestResult.cs
new file mode 100644
index 000000000..294f575b7
--- /dev/null
+++ b/crypto/test/src/util/test/SimpleTestResult.cs
@@ -0,0 +1,91 @@
+using System;
+using System.Text;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+    public class SimpleTestResult : ITestResult
+    {
+        private static readonly string Separator = SimpleTest.NewLine;
+
+        private bool success;
+        private string message;
+        private Exception exception;
+
+        public SimpleTestResult(
+			bool	success,
+			string	message)
+        {
+            this.success = success;
+            this.message = message;
+        }
+
+        public SimpleTestResult(
+			bool		success,
+			string		message,
+			Exception	exception)
+        {
+            this.success = success;
+            this.message = message;
+            this.exception = exception;
+        }
+
+		public static ITestResult Successful(
+            ITest	test,
+            string	message)
+        {
+            return new SimpleTestResult(true, test.Name + ": " + message);
+        }
+
+        public static ITestResult Failed(
+            ITest	test,
+            string	message)
+        {
+            return new SimpleTestResult(false, test.Name + ": " + message);
+        }
+
+        public static ITestResult Failed(
+            ITest		test,
+            string		message,
+            Exception	t)
+        {
+            return new SimpleTestResult(false, test.Name + ": " + message, t);
+        }
+
+        public static ITestResult Failed(
+            ITest	test,
+            string	message,
+            object	expected,
+            object	found)
+        {
+            return Failed(test, message + Separator + "Expected: " + expected + Separator + "Found   : " + found);
+        }
+
+        public static string FailedMessage(
+			string	algorithm,
+			string	testName,
+			string	expected,
+            string	actual)
+        {
+            StringBuilder sb = new StringBuilder(algorithm);
+            sb.Append(" failing ").Append(testName);
+            sb.Append(Separator).Append("    expected: ").Append(expected);
+            sb.Append(Separator).Append("    got     : ").Append(actual);
+            return sb.ToString();
+        }
+
+		public bool IsSuccessful()
+        {
+            return success;
+        }
+
+        public override string ToString()
+        {
+            return message;
+        }
+
+		public Exception GetException()
+        {
+            return exception;
+        }
+    }
+}
diff --git a/crypto/test/src/util/test/TestFailedException.cs b/crypto/test/src/util/test/TestFailedException.cs
new file mode 100644
index 000000000..ecd7e7d7a
--- /dev/null
+++ b/crypto/test/src/util/test/TestFailedException.cs
@@ -0,0 +1,24 @@
+using System;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+#if !(NETCF_1_0 || NETCF_2_0 || SILVERLIGHT)
+    [Serializable]
+#endif
+    public class TestFailedException
+        : Exception
+    {
+        private ITestResult _result;
+
+        public TestFailedException(
+            ITestResult result)
+        {
+            _result = result;
+        }
+
+        public ITestResult GetResult()
+        {
+            return _result;
+        }
+    }
+}
diff --git a/crypto/test/src/util/test/UncloseableStream.cs b/crypto/test/src/util/test/UncloseableStream.cs
new file mode 100644
index 000000000..8b592cdd4
--- /dev/null
+++ b/crypto/test/src/util/test/UncloseableStream.cs
@@ -0,0 +1,22 @@
+using System;
+using System.IO;
+
+using Org.BouncyCastle.Asn1.Utilities;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+	public class UncloseableStream
+		: FilterStream
+	{
+		public UncloseableStream(
+			Stream s)
+			: base(s)
+		{
+		}
+
+		public override void Close()
+		{
+			throw new Exception("Close() called on UncloseableStream");
+		}
+	}
+}